Commit 40d218d4 authored by nanahira's avatar nanahira

bump to Koishi 4.10.4

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