Commit 40d218d4 authored by nanahira's avatar nanahira

bump to Koishi 4.10.4

parent 94128fe7
This diff is collapsed.
...@@ -44,15 +44,15 @@ ...@@ -44,15 +44,15 @@
"testEnvironment": "node" "testEnvironment": "node"
}, },
"dependencies": { "dependencies": {
"koishi-thirdeye": "^11.1.13" "koishi-thirdeye": "^11.1.14"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.10.3" "koishi": "^4.10.4"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-console": "^4.5.4", "@koishijs/plugin-console": "^4.7.1",
"@koishijs/plugin-database-memory": "^2.0.0", "@koishijs/plugin-database-memory": "^2.0.1",
"@koishijs/plugin-sandbox": "^2.3.5", "@koishijs/plugin-sandbox": "^2.4.0",
"@types/jest": "^29.2.0", "@types/jest": "^29.2.0",
"@types/node": "^17.0.22", "@types/node": "^17.0.22",
"@types/supertest": "^2.0.12", "@types/supertest": "^2.0.12",
......
import { getSessionId, Random, Session } from 'koishi'; import { PromptOptions, Random, Session } from 'koishi';
import ApiBot from './index'; import ApiBot from './index';
import { Prompt } from './def/prompt'; import { Prompt } from './def/prompt';
export class ApiSession extends Session { export class ApiSession extends Session {
storedMessages: string[] = []; storedMessages: string[] = [];
private midResolver: () => void; private midResolver: () => void;
...@@ -20,6 +21,10 @@ export class ApiSession extends Session { ...@@ -20,6 +21,10 @@ export class ApiSession extends Session {
return this.gatherResponseMessages(); return this.gatherResponseMessages();
} }
getIdentifier() {
return '' + this.userId + this.channelId;
}
midResolve(finish = false) { midResolve(finish = false) {
if (!this.midResolver) { if (!this.midResolver) {
return; return;
...@@ -45,8 +50,15 @@ export class ApiSession extends Session { ...@@ -45,8 +50,15 @@ export class ApiSession extends Session {
return result; return result;
} }
prompt(timeout = this.app.options.delay.prompt) { prompt(...args: any[]) {
const identifier = getSessionId(this); const callback: (session: Session) => any =
typeof args[0] === 'function'
? args.shift()
: (session) => session.content;
const options: PromptOptions =
typeof args[0] === 'number' ? { timeout: args[0] } : args[0] ?? {};
const timeout = options.timeout ?? this.app.config.delay.prompt;
const identifier = this.getIdentifier();
const prom = new Promise<string>((resolve) => { const prom = new Promise<string>((resolve) => {
const prompt: Prompt = { const prompt: Prompt = {
resolver: resolve, resolver: resolve,
...@@ -54,6 +66,7 @@ export class ApiSession extends Session { ...@@ -54,6 +66,7 @@ export class ApiSession extends Session {
this.bot.resolvePrompt(identifier, undefined); this.bot.resolvePrompt(identifier, undefined);
}, timeout), }, timeout),
session: this, session: this,
contentCallback: callback,
}; };
this.bot.prompts.set(identifier, prompt); this.bot.prompts.set(identifier, prompt);
}); });
......
...@@ -10,6 +10,9 @@ export class ApiPluginConfig { ...@@ -10,6 +10,9 @@ export class ApiPluginConfig {
@SchemaProperty({ description: '验证 Bearer 令牌。' }) @SchemaProperty({ description: '验证 Bearer 令牌。' })
token?: string; token?: string;
@SchemaProperty({ description: '机器人 ID。', default: 'koishi' })
selfId?: string;
} }
export type ApiPluginConfigLike = Partial<ApiPluginConfig>; export type ApiPluginConfigLike = Partial<ApiPluginConfig>;
...@@ -4,4 +4,5 @@ export interface Prompt { ...@@ -4,4 +4,5 @@ export interface Prompt {
resolver: (value: string) => void; resolver: (value: string) => void;
timeout: NodeJS.Timeout; timeout: NodeJS.Timeout;
session: ApiSession; session: ApiSession;
contentCallback: (session: ApiSession) => any;
} }
...@@ -8,12 +8,15 @@ import { ...@@ -8,12 +8,15 @@ import {
Post, Post,
KoaContext, KoaContext,
PluginSchema, PluginSchema,
OnSelf,
Reusable,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Bot, Context, getSessionId, Next, Random } from 'koishi'; import { Bot, Context, Fragment, Next, Random } from 'koishi';
import { ApiSession } from './api-session'; import { ApiSession } from './api-session';
import { Prompt } from './def/prompt'; import { Prompt } from './def/prompt';
export * from './config'; export * from './config';
@Reusable()
@PluginSchema(ApiPluginConfig) @PluginSchema(ApiPluginConfig)
@DefinePlugin() @DefinePlugin()
export default class ApiBot extends Bot { export default class ApiBot extends Bot {
...@@ -23,13 +26,13 @@ export default class ApiBot extends Bot { ...@@ -23,13 +26,13 @@ export default class ApiBot extends Bot {
prompts = new Map<string, Prompt>(); prompts = new Map<string, Prompt>();
constructor(public ctx: Context, config: ApiPluginConfigLike) { constructor(public ctx: Context, config: ApiPluginConfigLike) {
super(ctx, { platform: 'api', selfId: 'koishi' }); super(ctx, { platform: 'api', selfId: config.selfId });
} }
resolvePrompt(key: string, value: string) { resolvePrompt(key: string, session: ApiSession) {
const prompt = this.prompts.get(key); const prompt = this.prompts.get(key);
if (prompt) { if (prompt) {
prompt.resolver(value); prompt.resolver(prompt.contentCallback(session));
clearTimeout(prompt.timeout); clearTimeout(prompt.timeout);
this.prompts.delete(key); this.prompts.delete(key);
return prompt; return prompt;
...@@ -37,11 +40,12 @@ export default class ApiBot extends Bot { ...@@ -37,11 +40,12 @@ export default class ApiBot extends Bot {
return; return;
} }
@OnSelf('{{selfId}}')
@OnPlatform('api') @OnPlatform('api')
@UseMiddleware() @UseMiddleware()
async handlePrompt(session: ApiSession, next: Next) { private async handlePrompt(session: ApiSession, next: Next) {
const identifier = getSessionId(session); const identifier = session.getIdentifier();
const prompt = this.resolvePrompt(identifier, session.content); const prompt = this.resolvePrompt(identifier, session);
if (!prompt) { if (!prompt) {
return next(); return next();
} }
...@@ -55,7 +59,7 @@ export default class ApiBot extends Bot { ...@@ -55,7 +59,7 @@ export default class ApiBot extends Bot {
private pluginConfig: ApiPluginConfig; private pluginConfig: ApiPluginConfig;
@Post('{{path}}') @Post('{{path}}')
async onHttpPost(ctx: KoaContext) { private async onHttpPost(ctx: KoaContext) {
if (this.pluginConfig.token) { if (this.pluginConfig.token) {
const header = ctx.request.headers['authorization']; const header = ctx.request.headers['authorization'];
const tokenFromRequest = header?.startsWith('Bearer ') const tokenFromRequest = header?.startsWith('Bearer ')
...@@ -106,11 +110,11 @@ export default class ApiBot extends Bot { ...@@ -106,11 +110,11 @@ export default class ApiBot extends Bot {
ctx.body = { messages: await session.waitForPattern() }; ctx.body = { messages: await session.waitForPattern() };
} }
async sendMessage(channelId: string, content: string) { async sendMessage(channelId: string, content: Fragment) {
return []; return [];
} }
async sendPrivateMessage(userId: string, content: string) { async sendPrivateMessage(userId: string, content: Fragment) {
return []; return [];
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment