Commit 6c3b1727 authored by nanahira's avatar nanahira

ReplySession: add process timeout

parent 3e8a9082
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
"ws": "^8.12.0" "ws": "^8.12.0"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.11.5" "koishi": "^4.11.6"
} }
}, },
"node_modules/@ampproject/remapping": { "node_modules/@ampproject/remapping": {
...@@ -1109,23 +1109,23 @@ ...@@ -1109,23 +1109,23 @@
} }
}, },
"node_modules/@koishijs/core": { "node_modules/@koishijs/core": {
"version": "4.11.5", "version": "4.11.6",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.11.5.tgz", "resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.11.6.tgz",
"integrity": "sha512-F+zR1rHKImPGVbx8S9dXq/fY+y/TGyRjlBOIld96eazWhUjYDOxM9ZYbapBKD8w+fFj/T/b4cjmeiKiJ4Cpx1g==", "integrity": "sha512-7Ky1q+9wPSACk5T5Oi+UjWQIQfwgZao/QnHmZjK7NaW+17NSG6MBRvu9davcayMKrs9HVcnP0kaqvS2n9z7+bA==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@koishijs/utils": "^7.0.0", "@koishijs/utils": "^7.0.0",
"@minatojs/core": "^2.2.0", "@minatojs/core": "^2.2.2",
"@satorijs/core": "^2.1.1", "@satorijs/core": "^2.1.2",
"cordis": "^2.7.2", "cordis": "^2.7.2",
"cosmokit": "^1.4.0", "cosmokit": "^1.4.0",
"fastest-levenshtein": "^1.0.16" "fastest-levenshtein": "^1.0.16"
} }
}, },
"node_modules/@koishijs/loader": { "node_modules/@koishijs/loader": {
"version": "2.5.5", "version": "2.5.6",
"resolved": "https://registry.npmjs.org/@koishijs/loader/-/loader-2.5.5.tgz", "resolved": "https://registry.npmjs.org/@koishijs/loader/-/loader-2.5.6.tgz",
"integrity": "sha512-SHk0rkdYjpBym3intZ4RZiX/2B57UjfV2EzdjZh3IkM44UCMfTDGicL6Nkzyvd0TLHLFCLsZADe9mGjTaQY/Ug==", "integrity": "sha512-2LswjTMsfqO0TCZpRs69ZbRI3RHh97/9KSitFFhplO++ZkKuqFHUwVnmFi7AbgtobybUZ+2llGtvO1cNZL13Kw==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
...@@ -1133,7 +1133,7 @@ ...@@ -1133,7 +1133,7 @@
"ns-require": "^1.1.4" "ns-require": "^1.1.4"
}, },
"peerDependencies": { "peerDependencies": {
"@koishijs/core": "4.11.5" "@koishijs/core": "4.11.6"
} }
}, },
"node_modules/@koishijs/loader/node_modules/argparse": { "node_modules/@koishijs/loader/node_modules/argparse": {
...@@ -1178,9 +1178,9 @@ ...@@ -1178,9 +1178,9 @@
} }
}, },
"node_modules/@minatojs/core": { "node_modules/@minatojs/core": {
"version": "2.2.0", "version": "2.2.2",
"resolved": "https://registry.npmjs.org/@minatojs/core/-/core-2.2.0.tgz", "resolved": "https://registry.npmjs.org/@minatojs/core/-/core-2.2.2.tgz",
"integrity": "sha512-7fax/CXuml8MIBSkxo0LRAutZgsfBsaaZ8zobUijRREvxMRv+FZ814gGxsTFPUxyjG+6Y2Wj2avS+tj+8LcnKA==", "integrity": "sha512-bZJ58RGtZ1LSw6zsAGoejvhAfgyhPhgrBmaNbTtAam3z+8XsJtR3dU4iWEa7cV6BcWfKTyTHNBAJM+9MvkZ4mg==",
"dependencies": { "dependencies": {
"cosmokit": "^1.4.0" "cosmokit": "^1.4.0"
} }
...@@ -1221,12 +1221,12 @@ ...@@ -1221,12 +1221,12 @@
} }
}, },
"node_modules/@satorijs/core": { "node_modules/@satorijs/core": {
"version": "2.1.1", "version": "2.1.3",
"resolved": "https://registry.npmjs.org/@satorijs/core/-/core-2.1.1.tgz", "resolved": "https://registry.npmjs.org/@satorijs/core/-/core-2.1.3.tgz",
"integrity": "sha512-TTg5n1Rw7MJqfbPtcdqHLebfnbpn2txS/6wGFXCTV+qIXGgJk10bahSSfd9EQgiQaRl6Qm4HM6PPpcQuGWaq6g==", "integrity": "sha512-YTX4VohtHl8kHEZmQDWzxGTI0+m5fDPTu8czBeztlTQofqouW6tUtP5h1TRJC7JqvjN/+Du6o0ilyIgIcl0Snw==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@satorijs/element": "^2.3.1", "@satorijs/element": "^2.3.2",
"cordis": "^2.7.2", "cordis": "^2.7.2",
"cordis-axios": "^3.0.0", "cordis-axios": "^3.0.0",
"cosmokit": "^1.4.0", "cosmokit": "^1.4.0",
...@@ -1236,22 +1236,22 @@ ...@@ -1236,22 +1236,22 @@
} }
}, },
"node_modules/@satorijs/element": { "node_modules/@satorijs/element": {
"version": "2.3.1", "version": "2.3.2",
"resolved": "https://registry.npmjs.org/@satorijs/element/-/element-2.3.1.tgz", "resolved": "https://registry.npmjs.org/@satorijs/element/-/element-2.3.2.tgz",
"integrity": "sha512-p1hQVy56nfJ2dw1noEHkV7hKpC9yqe5u3Vla+CSPqXU12uB4zqv96rjT9n4WulNGl8XTzqWxp8q8KWqhSI8JFA==", "integrity": "sha512-XOA5/2NGFjlD7Vb/qqkesqFY3PdP7Xvbg72MspSodYbSQVJoPSPuQooa9EWB8GBkxGPDA1y8GRQoL0jlLqcm2w==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"cosmokit": "^1.4.0" "cosmokit": "^1.4.0"
} }
}, },
"node_modules/@satorijs/satori": { "node_modules/@satorijs/satori": {
"version": "2.1.1", "version": "2.1.3",
"resolved": "https://registry.npmjs.org/@satorijs/satori/-/satori-2.1.1.tgz", "resolved": "https://registry.npmjs.org/@satorijs/satori/-/satori-2.1.3.tgz",
"integrity": "sha512-zXRhnf29zABL/R8Wch7t4vtmUwXszd6eaYwZVAF+THfLk4zVkNWEYjCLQfX2sS742oDER4DPoIKbJnkYbK0Shw==", "integrity": "sha512-7gd4q/ACRrBvCyTvODvxgf/iKxNpDg9BJy3PtbmB4dMDRixISUPmqOLVV1B1Q8XBVrtTWAuEivpOucgw7BZ8qQ==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@koa/router": "^10.1.1", "@koa/router": "^10.1.1",
"@satorijs/core": "2.1.1", "@satorijs/core": "2.1.3",
"@types/koa": "*", "@types/koa": "*",
"@types/koa__router": "*", "@types/koa__router": "*",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
...@@ -4648,15 +4648,15 @@ ...@@ -4648,15 +4648,15 @@
} }
}, },
"node_modules/koishi": { "node_modules/koishi": {
"version": "4.11.5", "version": "4.11.6",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.11.5.tgz", "resolved": "https://registry.npmjs.org/koishi/-/koishi-4.11.6.tgz",
"integrity": "sha512-nMDv9iRaJGm8wyMUs3Oefd6KIGaAKN/LmmeW+xv9JqRslxZAUl/hbtFy4qPkrWpJp/vZk0AAoFo0rstOEX3GCA==", "integrity": "sha512-XvsBmdVXHIzgU2v3IFe4wJakzqpKBH/dZtYCScDkq13zn/pLamI88rMxs7BYUd131lyJndzxa6jMwFVMvtYyUA==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@koishijs/core": "4.11.5", "@koishijs/core": "4.11.6",
"@koishijs/loader": "2.5.5", "@koishijs/loader": "2.5.6",
"@koishijs/utils": "^7.0.0", "@koishijs/utils": "^7.0.0",
"@satorijs/satori": "^2.1.1", "@satorijs/satori": "^2.1.2",
"cac": "^6.7.14", "cac": "^6.7.14",
"chokidar": "^3.5.3", "chokidar": "^3.5.3",
"kleur": "^4.1.5", "kleur": "^4.1.5",
...@@ -7329,23 +7329,23 @@ ...@@ -7329,23 +7329,23 @@
} }
}, },
"@koishijs/core": { "@koishijs/core": {
"version": "4.11.5", "version": "4.11.6",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.11.5.tgz", "resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.11.6.tgz",
"integrity": "sha512-F+zR1rHKImPGVbx8S9dXq/fY+y/TGyRjlBOIld96eazWhUjYDOxM9ZYbapBKD8w+fFj/T/b4cjmeiKiJ4Cpx1g==", "integrity": "sha512-7Ky1q+9wPSACk5T5Oi+UjWQIQfwgZao/QnHmZjK7NaW+17NSG6MBRvu9davcayMKrs9HVcnP0kaqvS2n9z7+bA==",
"peer": true, "peer": true,
"requires": { "requires": {
"@koishijs/utils": "^7.0.0", "@koishijs/utils": "^7.0.0",
"@minatojs/core": "^2.2.0", "@minatojs/core": "^2.2.2",
"@satorijs/core": "^2.1.1", "@satorijs/core": "^2.1.2",
"cordis": "^2.7.2", "cordis": "^2.7.2",
"cosmokit": "^1.4.0", "cosmokit": "^1.4.0",
"fastest-levenshtein": "^1.0.16" "fastest-levenshtein": "^1.0.16"
} }
}, },
"@koishijs/loader": { "@koishijs/loader": {
"version": "2.5.5", "version": "2.5.6",
"resolved": "https://registry.npmjs.org/@koishijs/loader/-/loader-2.5.5.tgz", "resolved": "https://registry.npmjs.org/@koishijs/loader/-/loader-2.5.6.tgz",
"integrity": "sha512-SHk0rkdYjpBym3intZ4RZiX/2B57UjfV2EzdjZh3IkM44UCMfTDGicL6Nkzyvd0TLHLFCLsZADe9mGjTaQY/Ug==", "integrity": "sha512-2LswjTMsfqO0TCZpRs69ZbRI3RHh97/9KSitFFhplO++ZkKuqFHUwVnmFi7AbgtobybUZ+2llGtvO1cNZL13Kw==",
"peer": true, "peer": true,
"requires": { "requires": {
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
...@@ -7391,9 +7391,9 @@ ...@@ -7391,9 +7391,9 @@
} }
}, },
"@minatojs/core": { "@minatojs/core": {
"version": "2.2.0", "version": "2.2.2",
"resolved": "https://registry.npmjs.org/@minatojs/core/-/core-2.2.0.tgz", "resolved": "https://registry.npmjs.org/@minatojs/core/-/core-2.2.2.tgz",
"integrity": "sha512-7fax/CXuml8MIBSkxo0LRAutZgsfBsaaZ8zobUijRREvxMRv+FZ814gGxsTFPUxyjG+6Y2Wj2avS+tj+8LcnKA==", "integrity": "sha512-bZJ58RGtZ1LSw6zsAGoejvhAfgyhPhgrBmaNbTtAam3z+8XsJtR3dU4iWEa7cV6BcWfKTyTHNBAJM+9MvkZ4mg==",
"requires": { "requires": {
"cosmokit": "^1.4.0" "cosmokit": "^1.4.0"
} }
...@@ -7425,12 +7425,12 @@ ...@@ -7425,12 +7425,12 @@
} }
}, },
"@satorijs/core": { "@satorijs/core": {
"version": "2.1.1", "version": "2.1.3",
"resolved": "https://registry.npmjs.org/@satorijs/core/-/core-2.1.1.tgz", "resolved": "https://registry.npmjs.org/@satorijs/core/-/core-2.1.3.tgz",
"integrity": "sha512-TTg5n1Rw7MJqfbPtcdqHLebfnbpn2txS/6wGFXCTV+qIXGgJk10bahSSfd9EQgiQaRl6Qm4HM6PPpcQuGWaq6g==", "integrity": "sha512-YTX4VohtHl8kHEZmQDWzxGTI0+m5fDPTu8czBeztlTQofqouW6tUtP5h1TRJC7JqvjN/+Du6o0ilyIgIcl0Snw==",
"peer": true, "peer": true,
"requires": { "requires": {
"@satorijs/element": "^2.3.1", "@satorijs/element": "^2.3.2",
"cordis": "^2.7.2", "cordis": "^2.7.2",
"cordis-axios": "^3.0.0", "cordis-axios": "^3.0.0",
"cosmokit": "^1.4.0", "cosmokit": "^1.4.0",
...@@ -7440,22 +7440,22 @@ ...@@ -7440,22 +7440,22 @@
} }
}, },
"@satorijs/element": { "@satorijs/element": {
"version": "2.3.1", "version": "2.3.2",
"resolved": "https://registry.npmjs.org/@satorijs/element/-/element-2.3.1.tgz", "resolved": "https://registry.npmjs.org/@satorijs/element/-/element-2.3.2.tgz",
"integrity": "sha512-p1hQVy56nfJ2dw1noEHkV7hKpC9yqe5u3Vla+CSPqXU12uB4zqv96rjT9n4WulNGl8XTzqWxp8q8KWqhSI8JFA==", "integrity": "sha512-XOA5/2NGFjlD7Vb/qqkesqFY3PdP7Xvbg72MspSodYbSQVJoPSPuQooa9EWB8GBkxGPDA1y8GRQoL0jlLqcm2w==",
"peer": true, "peer": true,
"requires": { "requires": {
"cosmokit": "^1.4.0" "cosmokit": "^1.4.0"
} }
}, },
"@satorijs/satori": { "@satorijs/satori": {
"version": "2.1.1", "version": "2.1.3",
"resolved": "https://registry.npmjs.org/@satorijs/satori/-/satori-2.1.1.tgz", "resolved": "https://registry.npmjs.org/@satorijs/satori/-/satori-2.1.3.tgz",
"integrity": "sha512-zXRhnf29zABL/R8Wch7t4vtmUwXszd6eaYwZVAF+THfLk4zVkNWEYjCLQfX2sS742oDER4DPoIKbJnkYbK0Shw==", "integrity": "sha512-7gd4q/ACRrBvCyTvODvxgf/iKxNpDg9BJy3PtbmB4dMDRixISUPmqOLVV1B1Q8XBVrtTWAuEivpOucgw7BZ8qQ==",
"peer": true, "peer": true,
"requires": { "requires": {
"@koa/router": "^10.1.1", "@koa/router": "^10.1.1",
"@satorijs/core": "2.1.1", "@satorijs/core": "2.1.3",
"@types/koa": "*", "@types/koa": "*",
"@types/koa__router": "*", "@types/koa__router": "*",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
...@@ -10036,15 +10036,15 @@ ...@@ -10036,15 +10036,15 @@
} }
}, },
"koishi": { "koishi": {
"version": "4.11.5", "version": "4.11.6",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.11.5.tgz", "resolved": "https://registry.npmjs.org/koishi/-/koishi-4.11.6.tgz",
"integrity": "sha512-nMDv9iRaJGm8wyMUs3Oefd6KIGaAKN/LmmeW+xv9JqRslxZAUl/hbtFy4qPkrWpJp/vZk0AAoFo0rstOEX3GCA==", "integrity": "sha512-XvsBmdVXHIzgU2v3IFe4wJakzqpKBH/dZtYCScDkq13zn/pLamI88rMxs7BYUd131lyJndzxa6jMwFVMvtYyUA==",
"peer": true, "peer": true,
"requires": { "requires": {
"@koishijs/core": "4.11.5", "@koishijs/core": "4.11.6",
"@koishijs/loader": "2.5.5", "@koishijs/loader": "2.5.6",
"@koishijs/utils": "^7.0.0", "@koishijs/utils": "^7.0.0",
"@satorijs/satori": "^2.1.1", "@satorijs/satori": "^2.1.2",
"cac": "^6.7.14", "cac": "^6.7.14",
"chokidar": "^3.5.3", "chokidar": "^3.5.3",
"kleur": "^4.1.5", "kleur": "^4.1.5",
......
...@@ -72,6 +72,6 @@ ...@@ -72,6 +72,6 @@
"testEnvironment": "node" "testEnvironment": "node"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.11.5" "koishi": "^4.11.6"
} }
} }
...@@ -38,7 +38,7 @@ export class ReplySession< ...@@ -38,7 +38,7 @@ export class ReplySession<
this.midResolver = resolve; this.midResolver = resolve;
}); });
async process() { async process(timeout = 0) {
if (!this.app.lifecycle.isActive) return; if (!this.app.lifecycle.isActive) return;
const events: string[] = [this.type]; const events: string[] = [this.type];
if (this.subtype) { if (this.subtype) {
...@@ -47,17 +47,24 @@ export class ReplySession< ...@@ -47,17 +47,24 @@ export class ReplySession<
events.unshift(events[0] + '/' + this.subsubtype); events.unshift(events[0] + '/' + this.subsubtype);
} }
} }
this.inRequest = true;
this.emitPromise = Promise.all( this.emitPromise = Promise.all(
events.map((event) => this.app.root.parallel(this, event as any, this)), events.map((event) => this.app.root.parallel(this, event as any, this)),
); );
return this.waitForPattern(); return this.waitForPattern(timeout);
} }
async waitForPattern() { inRequest = false;
await Promise.race([
async waitForPattern(timeout = 0) {
const promises = [
this.emitPromise.then(() => this.midResolve(true)), this.emitPromise.then(() => this.midResolve(true)),
this.midPromise, this.midPromise,
]); ];
if (timeout) {
promises.push(new Promise((resolve) => setTimeout(resolve, timeout)));
}
await Promise.race(promises);
return this.gatherReplyMessages(); return this.gatherReplyMessages();
} }
...@@ -80,6 +87,9 @@ export class ReplySession< ...@@ -80,6 +87,9 @@ export class ReplySession<
} }
async send(content: Fragment, options: SendOptions = {}) { async send(content: Fragment, options: SendOptions = {}) {
if (!this.inRequest) {
return super.send(content, options);
}
if (!content) return; if (!content) return;
options.session = this; options.session = this;
const children = await this.transform(segment.normalize(content)); const children = await this.transform(segment.normalize(content));
...@@ -96,13 +106,17 @@ export class ReplySession< ...@@ -96,13 +106,17 @@ export class ReplySession<
return [messageId]; return [messageId];
} }
gatherReplyMessages() { private gatherReplyMessages() {
const result = this.replyMessages.filter((m) => !!m); const result = this.replyMessages.filter((m) => !!m);
this.replyMessages = []; this.replyMessages = [];
this.inRequest = false;
return result; return result;
} }
prompt(...args: any[]) { prompt(...args: any[]) {
if (!this.inRequest) {
return super.prompt(...args);
}
if (!this.app.__prompt_resolver__) { if (!this.app.__prompt_resolver__) {
this.app.root.plugin(PromptResolver); this.app.root.plugin(PromptResolver);
} }
...@@ -163,6 +177,7 @@ class PromptResolver extends StarterPlugin() { ...@@ -163,6 +177,7 @@ class PromptResolver extends StarterPlugin() {
async resolvePrompt(identifier: string, session: ReplySession) { async resolvePrompt(identifier: string, session: ReplySession) {
const prompt = this.prompts.get(identifier); const prompt = this.prompts.get(identifier);
if (prompt) { if (prompt) {
prompt.session.inRequest = true;
prompt.resolver(session ? await prompt.callback(session) : undefined); prompt.resolver(session ? await prompt.callback(session) : undefined);
clearTimeout(prompt.timeout); clearTimeout(prompt.timeout);
this.prompts.delete(identifier); this.prompts.delete(identifier);
......
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