Commit f85b3c7d authored by nanahira's avatar nanahira

rework with cache api

parent 866aa51f
Pipeline #6327 passed with stages
in 53 seconds
{
"name": "koishi-plugin-thesaurus",
"version": "1.0.0",
"version": "1.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "koishi-plugin-thesaurus",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"dependencies": {
"source-map-support": "^0.5.20"
},
"devDependencies": {
"@types/lodash": "^4.14.175",
"@types/lru-cache": "^5.1.1",
"@types/node": "^16.11.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"koishi": "^4.0.0-alpha.9",
"koishi": "^4.0.0-alpha.10",
"koishi-utils-schemagen": "^1.1.3",
"lodash": "^4.17.21",
"lru-cache": "^6.0.0",
"prettier": "^2.4.1",
"proxy-agent": "^5.0.0",
"raw-loader": "^4.0.2",
......@@ -214,22 +212,34 @@
}
},
"node_modules/@koishijs/core": {
"version": "4.0.0-alpha.9",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.0.0-alpha.9.tgz",
"integrity": "sha512-dVnK3+XGV0uee1hQo67gcYHx1Vjn7j1JD4Ztl884nlCmrGkF3Yglv2H24HSdZq1/0/SanfehVbF9g4YhvoLNgw==",
"version": "4.0.0-alpha.10",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.0.0-alpha.10.tgz",
"integrity": "sha512-VoCRV2pGyFsqxvvZnUz7a0/rvzwNoS4c61cdyKX05K3fjiHuzBWezk5vNwkgE5pvtCGmMXaOmMwSfazSZ/rLLQ==",
"dev": true,
"dependencies": {
"@koishijs/utils": "^5.0.0-alpha.5",
"@koishijs/utils": "^5.0.0-alpha.6",
"fastest-levenshtein": "^1.0.12"
},
"engines": {
"node": ">=12.0.0"
}
},
"node_modules/@koishijs/plugin-cache-lru": {
"version": "1.0.0-alpha.1",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-cache-lru/-/plugin-cache-lru-1.0.0-alpha.1.tgz",
"integrity": "sha512-GaLkOMMOy7djNSOT/eGHSUEu4kikmUjmxJIvOrAA/Y7oJAdXaTcY4gcZi5Vtie6NWXUIBssqqJmcJBgF/Bwwvw==",
"dev": true,
"dependencies": {
"lru-cache": "^6.0.0"
},
"peerDependencies": {
"koishi": "^4.0.0-alpha.10"
}
},
"node_modules/@koishijs/utils": {
"version": "5.0.0-alpha.5",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.0.0-alpha.5.tgz",
"integrity": "sha512-zYgsYu5ukD5xqtan1y/RlE198w1bYVngKo49wefERsmDLMplHUf/3R6qhmPad6gvYy5ALVmkTQLwX646yReMPg==",
"version": "5.0.0-alpha.6",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.0.0-alpha.6.tgz",
"integrity": "sha512-nvSZmDErNwAuTE+bVnVRn1s+Tt9ni7n5B6GbD2if7cNiLbWaJJkUgHLiiJaRRIhHJOmN/OEPaUwijf4j5LGXrg==",
"dev": true,
"dependencies": {
"supports-color": "^8.1.0"
......@@ -453,12 +463,6 @@
"integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==",
"dev": true
},
"node_modules/@types/lru-cache": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/@types/lru-cache/-/lru-cache-5.1.1.tgz",
"integrity": "sha512-ssE3Vlrys7sdIzs5LOxCzTVMsU7i9oa/IaW92wF32JFb3CVczqOkru2xspuKczHEbG3nvmPY7IFqVmGGHdNbYw==",
"dev": true
},
"node_modules/@types/mime": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
......@@ -2699,20 +2703,22 @@
}
},
"node_modules/koishi": {
"version": "4.0.0-alpha.9",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.0.0-alpha.9.tgz",
"integrity": "sha512-rgGqktMxyM5XiuYPGNn5kCcsXpW0ceTT7mLYxyH5PBTIz1aF0FthzbaC+J0i5gLyDlHAC43lhND/ZxaudLS5Kg==",
"version": "4.0.0-alpha.10",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.0.0-alpha.10.tgz",
"integrity": "sha512-1d1Sztg4i6ob+voet0nbpaOt0RRkL7HJfTPQDAbzSZow49xFe4M0QyvyFJa3t96Mg60aQZCQZHrBkiAEQzPbYg==",
"dev": true,
"dependencies": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.0.0-alpha.9",
"@koishijs/utils": "^5.0.0-alpha.5",
"@koishijs/core": "^4.0.0-alpha.10",
"@koishijs/plugin-cache-lru": "^1.0.0-alpha.1",
"@koishijs/utils": "^5.0.0-alpha.6",
"@types/koa": "*",
"@types/koa__router": "*",
"@types/ws": "^7.4.7",
"axios": "^0.21.4",
"koa": "^2.13.1",
"koa-bodyparser": "^4.3.0"
"koa-bodyparser": "^4.3.0",
"proxy-agent": "^5.0.0"
},
"engines": {
"node": ">=12.0.0"
......@@ -4489,19 +4495,28 @@
}
},
"@koishijs/core": {
"version": "4.0.0-alpha.9",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.0.0-alpha.9.tgz",
"integrity": "sha512-dVnK3+XGV0uee1hQo67gcYHx1Vjn7j1JD4Ztl884nlCmrGkF3Yglv2H24HSdZq1/0/SanfehVbF9g4YhvoLNgw==",
"version": "4.0.0-alpha.10",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.0.0-alpha.10.tgz",
"integrity": "sha512-VoCRV2pGyFsqxvvZnUz7a0/rvzwNoS4c61cdyKX05K3fjiHuzBWezk5vNwkgE5pvtCGmMXaOmMwSfazSZ/rLLQ==",
"dev": true,
"requires": {
"@koishijs/utils": "^5.0.0-alpha.5",
"@koishijs/utils": "^5.0.0-alpha.6",
"fastest-levenshtein": "^1.0.12"
}
},
"@koishijs/plugin-cache-lru": {
"version": "1.0.0-alpha.1",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-cache-lru/-/plugin-cache-lru-1.0.0-alpha.1.tgz",
"integrity": "sha512-GaLkOMMOy7djNSOT/eGHSUEu4kikmUjmxJIvOrAA/Y7oJAdXaTcY4gcZi5Vtie6NWXUIBssqqJmcJBgF/Bwwvw==",
"dev": true,
"requires": {
"lru-cache": "^6.0.0"
}
},
"@koishijs/utils": {
"version": "5.0.0-alpha.5",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.0.0-alpha.5.tgz",
"integrity": "sha512-zYgsYu5ukD5xqtan1y/RlE198w1bYVngKo49wefERsmDLMplHUf/3R6qhmPad6gvYy5ALVmkTQLwX646yReMPg==",
"version": "5.0.0-alpha.6",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.0.0-alpha.6.tgz",
"integrity": "sha512-nvSZmDErNwAuTE+bVnVRn1s+Tt9ni7n5B6GbD2if7cNiLbWaJJkUgHLiiJaRRIhHJOmN/OEPaUwijf4j5LGXrg==",
"dev": true,
"requires": {
"supports-color": "^8.1.0"
......@@ -4709,12 +4724,6 @@
"integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==",
"dev": true
},
"@types/lru-cache": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/@types/lru-cache/-/lru-cache-5.1.1.tgz",
"integrity": "sha512-ssE3Vlrys7sdIzs5LOxCzTVMsU7i9oa/IaW92wF32JFb3CVczqOkru2xspuKczHEbG3nvmPY7IFqVmGGHdNbYw==",
"dev": true
},
"@types/mime": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
......@@ -6416,20 +6425,22 @@
}
},
"koishi": {
"version": "4.0.0-alpha.9",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.0.0-alpha.9.tgz",
"integrity": "sha512-rgGqktMxyM5XiuYPGNn5kCcsXpW0ceTT7mLYxyH5PBTIz1aF0FthzbaC+J0i5gLyDlHAC43lhND/ZxaudLS5Kg==",
"version": "4.0.0-alpha.10",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.0.0-alpha.10.tgz",
"integrity": "sha512-1d1Sztg4i6ob+voet0nbpaOt0RRkL7HJfTPQDAbzSZow49xFe4M0QyvyFJa3t96Mg60aQZCQZHrBkiAEQzPbYg==",
"dev": true,
"requires": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.0.0-alpha.9",
"@koishijs/utils": "^5.0.0-alpha.5",
"@koishijs/core": "^4.0.0-alpha.10",
"@koishijs/plugin-cache-lru": "^1.0.0-alpha.1",
"@koishijs/utils": "^5.0.0-alpha.6",
"@types/koa": "*",
"@types/koa__router": "*",
"@types/ws": "^7.4.7",
"axios": "^0.21.4",
"koa": "^2.13.1",
"koa-bodyparser": "^4.3.0"
"koa-bodyparser": "^4.3.0",
"proxy-agent": "^5.0.0"
}
},
"koishi-utils-schemagen": {
......
{
"name": "koishi-plugin-thesaurus",
"version": "1.0.1",
"version": "1.0.2",
"description": "Koishi.js chat plugin for https://github.com/Kyomotoi/AnimeThesaurus",
"main": "dist/index.js",
"types": "dist/index.d.ts",
......@@ -32,17 +32,15 @@
},
"devDependencies": {
"@types/lodash": "^4.14.175",
"@types/lru-cache": "^5.1.1",
"@types/node": "^16.11.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"koishi": "^4.0.0-alpha.9",
"koishi": "^4.0.0-alpha.10",
"koishi-utils-schemagen": "^1.1.3",
"lodash": "^4.17.21",
"lru-cache": "^6.0.0",
"prettier": "^2.4.1",
"proxy-agent": "^5.0.0",
"raw-loader": "^4.0.2",
......
......@@ -3,6 +3,7 @@ import { Context } from 'koishi';
import { MyPlugin } from './plugin';
import { MyPluginConfigLike } from './config';
export { MyPluginConfig } from './config';
export { ChatSession } from './plugin';
export const name = 'thesaurus';
const plugin = new MyPlugin();
......
......@@ -4,14 +4,22 @@ import { MyPluginConfig, MyPluginConfigLike } from './config';
import { schemaFromClass, schemaTransform } from 'koishi-utils-schemagen';
import { promises as fs } from 'fs';
import { sleep } from 'koishi';
import LRUCache from 'lru-cache';
import _ from 'lodash';
interface ChatSession {
export interface ChatSession {
name: string;
playedWords: string[];
}
declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cache {
interface Tables {
thesaurusChatSession: ChatSession;
}
}
}
export class MyPlugin {
private config: MyPluginConfig;
private ctx: Context;
......@@ -40,30 +48,34 @@ export class MyPlugin {
return this.loadWords();
}
}
// should be replaced with Koishi cache in future version
private lru: LRUCache<string, ChatSession>;
private async getFromCache(userId: string) {
if (!userId) {
private async getFromCache(sessionId: string) {
if (!sessionId) {
return;
}
return this.lru.get(userId);
return this.ctx.cache.get('thesaurusChatSession', sessionId);
}
private async setToCache(userId: string, chatSession: ChatSession) {
if (!userId) {
private async setToCache(sessionId: string, chatSession: ChatSession) {
if (!sessionId) {
return;
}
return this.lru.set(userId, chatSession, this.config.chatTimeout);
return this.ctx.cache.set(
'thesaurusChatSession',
sessionId,
chatSession,
this.config.chatTimeout,
);
}
private async clearCacheOf(userId: string) {
if (!userId) {
private async clearCacheOf(sessionId: string) {
if (!sessionId) {
return;
}
return this.lru.del(userId);
return this.ctx.cache.set('thesaurusChatSession', sessionId, null);
}
private async replyWith(
word: string,
userId: string,
sessionId: string,
chatSession: ChatSession,
) {
if (!word) {
......@@ -73,7 +85,7 @@ export class MyPlugin {
if (chatSession.playedWords.length > this.config.trackingLength) {
chatSession.playedWords.shift();
}
await this.setToCache(userId, chatSession);
await this.setToCache(sessionId, chatSession);
return word.replace(/你/g, chatSession.name).trim();
}
......@@ -94,7 +106,7 @@ export class MyPlugin {
private async replyChat(
word: string,
userId: string,
sessionId: string,
chatSession: ChatSession,
) {
const matchingPatterns = Object.keys(this.wordData).filter((w) =>
......@@ -110,7 +122,7 @@ export class MyPlugin {
}
return this.replyWith(
Random.pick(remainingAllWords),
userId,
sessionId,
chatSession,
);
}
......@@ -119,7 +131,7 @@ export class MyPlugin {
matchingPatterns,
new Set<string>(chatSession.playedWords),
) || this.tryPatterns(matchingPatterns, new Set<string>());
return this.replyWith(replyWord, userId, chatSession);
return this.replyWith(replyWord, sessionId, chatSession);
}
private formatName(name: string) {
if (name.match(/^[a-zA-Z0-9]/)) {
......@@ -138,23 +150,24 @@ export class MyPlugin {
ctx.on('connect', () => {
if (ctx.database && this.config.useDatabase) this.useDatabase = true;
});
this.lru = new LRUCache<string, ChatSession>({
ctx.cache.table('thesaurusChatSession', {
maxAge: this.config.chatTimeout,
});
await this.loadWords();
ctx.middleware(async (session, next) => {
const chatSession = await this.getFromCache(session.userId);
const sessionId = `${session.platform}.${session.selfId}.${session.userId}`;
const chatSession = await this.getFromCache(sessionId);
if (chatSession) {
//ctx
// .logger('thesaurus')
// .warn(`${session.userId} in chat ${JSON.stringify(chatSession)}`);
if (session.content === 'quit') {
await this.clearCacheOf(session.userId);
await this.clearCacheOf(sessionId);
return session.send('记得下次再找我聊喔~');
}
const reply = await this.replyChat(
session.content,
session.userId,
sessionId,
chatSession,
);
return session.send(reply);
......@@ -170,7 +183,7 @@ export class MyPlugin {
.userFields(['name'])
.action(async (argv) => {
const { session, options } = argv;
const userId = session.userId;
const sessionId = `${session.platform}.${session.selfId}.${session.userId}`;
let name =
session.author?.nickname ||
session.author?.username ||
......@@ -187,7 +200,7 @@ export class MyPlugin {
name = '';
}
const formattedName = this.formatName(name);
await this.setToCache(userId, {
await this.setToCache(sessionId, {
name: formattedName,
playedWords: [],
});
......
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