@nestim/koishi-plugin-qq-group-manager 1.4.13 → 1.4.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/lib/ai/engine.d.ts +26 -0
- package/lib/ai/engine.js +117 -0
- package/lib/service.js +48 -1
- package/package.json +1 -1
- package/readme.md +2 -0
- package/src/ai/engine.ts +125 -0
- package/src/service.js +48 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
本文件记录所有值得注意的变更。版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
4
4
|
|
|
5
|
+
## 1.4.14
|
|
6
|
+
|
|
7
|
+
### 新增
|
|
8
|
+
|
|
9
|
+
- **`[memory] summarize`**:立即触发一次归纳。优先消费「已挤出但还没归纳」的消息;
|
|
10
|
+
没有待归纳消息时退化为最近上下文(最多 `aiContextWindow` 条),不会什么都不做。
|
|
11
|
+
- **`[memory] organize`**:立即整理自动记忆,把每个分类里 ≥2 条合并成 1 条;
|
|
12
|
+
手动 `[memory]` 记忆不动,不同分类不混。
|
|
13
|
+
- 两个指令都写进 `[bot] help` 菜单和 `[memory]` 用法提示;需要主人/管理员权限,
|
|
14
|
+
避免群友反复触发模型调用。
|
|
15
|
+
|
|
16
|
+
### 说明
|
|
17
|
+
|
|
18
|
+
- 支持中文别名:`归纳` / `总结` / `整理`。
|
|
19
|
+
- 触发时先回一条「正在…」,完成后回报归纳条数或整理净减条数。
|
|
20
|
+
|
|
5
21
|
## 1.4.13
|
|
6
22
|
|
|
7
23
|
### 修复
|
package/lib/ai/engine.d.ts
CHANGED
|
@@ -141,6 +141,32 @@ export declare class AiEngine {
|
|
|
141
141
|
dispose(): void;
|
|
142
142
|
private learnAndReport;
|
|
143
143
|
/** 把一批消息归纳成一条记忆并写入对应作用域;返回写入结果或 null */
|
|
144
|
+
/** 解析会话对应的记忆作用域 */
|
|
145
|
+
private scopeForSession;
|
|
146
|
+
/**
|
|
147
|
+
* [memory] summarize:立即触发一次归纳。
|
|
148
|
+
*
|
|
149
|
+
* 优先用「已被挤出、还没归纳」的待归纳桶;桶空时退化为最近上下文,
|
|
150
|
+
* 这样手动触发不会因为还没挤出消息而什么都不做。
|
|
151
|
+
*/
|
|
152
|
+
learnNow(session: any): Promise<{
|
|
153
|
+
ok: boolean;
|
|
154
|
+
entries: number;
|
|
155
|
+
text: string;
|
|
156
|
+
reason: string;
|
|
157
|
+
}>;
|
|
158
|
+
/**
|
|
159
|
+
* [memory] organize:立即整理一次。
|
|
160
|
+
*
|
|
161
|
+
* 把每个分类里 ≥2 条的自动记忆合并成 1 条;手动记忆不动。
|
|
162
|
+
*/
|
|
163
|
+
organizeNow(session: any): Promise<{
|
|
164
|
+
ok: boolean;
|
|
165
|
+
categories: number;
|
|
166
|
+
removed: number;
|
|
167
|
+
after: number;
|
|
168
|
+
reason: string;
|
|
169
|
+
}>;
|
|
144
170
|
private summarizeRecords;
|
|
145
171
|
/**
|
|
146
172
|
* 自动归纳写入策略(分类版):
|
package/lib/ai/engine.js
CHANGED
|
@@ -694,6 +694,123 @@ class AiEngine {
|
|
|
694
694
|
}
|
|
695
695
|
}
|
|
696
696
|
/** 把一批消息归纳成一条记忆并写入对应作用域;返回写入结果或 null */
|
|
697
|
+
/** 解析会话对应的记忆作用域 */
|
|
698
|
+
scopeForSession(session) {
|
|
699
|
+
const kind = session?.guildId ? "group" : "private";
|
|
700
|
+
const id = String(session?.guildId || session?.userId || "");
|
|
701
|
+
if (!id)
|
|
702
|
+
return null;
|
|
703
|
+
const scope = typeof this.host.memoryScopeOf === "function"
|
|
704
|
+
? this.host.memoryScopeOf(session)
|
|
705
|
+
: { kind, id };
|
|
706
|
+
return { kind, id, scope };
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* [memory] summarize:立即触发一次归纳。
|
|
710
|
+
*
|
|
711
|
+
* 优先用「已被挤出、还没归纳」的待归纳桶;桶空时退化为最近上下文,
|
|
712
|
+
* 这样手动触发不会因为还没挤出消息而什么都不做。
|
|
713
|
+
*/
|
|
714
|
+
async learnNow(session) {
|
|
715
|
+
const ctx = this.scopeForSession(session);
|
|
716
|
+
if (!ctx)
|
|
717
|
+
return { ok: false, entries: 0, text: "", reason: "no-scope" };
|
|
718
|
+
const bucket = this.learnStore.get(ctx.kind, ctx.id);
|
|
719
|
+
// 先快照、成功后只移除这一次消费的消息:模型失败时不丢待归纳数据
|
|
720
|
+
let records = bucket.records.slice();
|
|
721
|
+
let source = "bucket";
|
|
722
|
+
if (!records.length) {
|
|
723
|
+
const platform = String(session?.platform || "onebot");
|
|
724
|
+
let state = this.state.peek(`${platform}:${ctx.id}`);
|
|
725
|
+
if (!state) {
|
|
726
|
+
for (const [key, value] of this.state.entries()) {
|
|
727
|
+
if (key.endsWith(`:${ctx.id}`)) {
|
|
728
|
+
state = value;
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
const limit = Math.max(10, Math.min(40, Number(this.config.aiContextWindow) || 30));
|
|
734
|
+
records = state
|
|
735
|
+
? state.records
|
|
736
|
+
.filter((record) => !record.command && String(record.text || "").trim())
|
|
737
|
+
.slice(-limit)
|
|
738
|
+
: [];
|
|
739
|
+
source = "context";
|
|
740
|
+
}
|
|
741
|
+
if (!records.length)
|
|
742
|
+
return { ok: false, entries: 0, text: "", reason: "empty" };
|
|
743
|
+
const saved = await this.summarizeRecords(records, ctx.kind, ctx.scope, `manual-${source}`);
|
|
744
|
+
if (!saved)
|
|
745
|
+
return { ok: false, entries: 0, text: "", reason: "none" };
|
|
746
|
+
if (source === "bucket") {
|
|
747
|
+
const consumed = new Set(records);
|
|
748
|
+
bucket.records = bucket.records.filter((record) => !consumed.has(record));
|
|
749
|
+
}
|
|
750
|
+
return { ok: true, entries: saved.entries || 0, text: saved.text, reason: source };
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* [memory] organize:立即整理一次。
|
|
754
|
+
*
|
|
755
|
+
* 把每个分类里 ≥2 条的自动记忆合并成 1 条;手动记忆不动。
|
|
756
|
+
*/
|
|
757
|
+
async organizeNow(session) {
|
|
758
|
+
const ctx = this.scopeForSession(session);
|
|
759
|
+
if (!ctx)
|
|
760
|
+
return { ok: false, categories: 0, removed: 0, after: 0, reason: "no-scope" };
|
|
761
|
+
let autoBlocks = [];
|
|
762
|
+
try {
|
|
763
|
+
const content = await this.host.readMemoryFile(ctx.scope);
|
|
764
|
+
autoBlocks = (0, memory_1.splitMemoryBlocks)(content)
|
|
765
|
+
.map((item) => item.text)
|
|
766
|
+
.filter((item) => (0, learn_1.isAutoMemoryBlock)(item));
|
|
767
|
+
}
|
|
768
|
+
catch {
|
|
769
|
+
autoBlocks = [];
|
|
770
|
+
}
|
|
771
|
+
if (!autoBlocks.length) {
|
|
772
|
+
return { ok: false, categories: 0, removed: 0, after: 0, reason: "empty" };
|
|
773
|
+
}
|
|
774
|
+
const byCategory = new Map();
|
|
775
|
+
for (const block of autoBlocks) {
|
|
776
|
+
const category = (0, learn_1.memoryBlockCategory)(block);
|
|
777
|
+
const list = byCategory.get(category) || [];
|
|
778
|
+
list.push(block);
|
|
779
|
+
byCategory.set(category, list);
|
|
780
|
+
}
|
|
781
|
+
const maxChars = Math.max(40, Number(this.config.memoryLearnConsolidateMaxChars) || 90);
|
|
782
|
+
let categories = 0;
|
|
783
|
+
let removed = 0;
|
|
784
|
+
for (const [category, list] of byCategory) {
|
|
785
|
+
if (list.length < 2)
|
|
786
|
+
continue;
|
|
787
|
+
try {
|
|
788
|
+
const merged = await this.consolidateMemories(ctx.kind, list, "", maxChars, category);
|
|
789
|
+
if (!merged)
|
|
790
|
+
continue;
|
|
791
|
+
await this.host.commitAutoMemory(ctx.scope, {
|
|
792
|
+
add: merged,
|
|
793
|
+
remove: list,
|
|
794
|
+
label: learn_1.AUTO_MEMORY_LABEL,
|
|
795
|
+
category,
|
|
796
|
+
});
|
|
797
|
+
categories += 1;
|
|
798
|
+
removed += list.length - 1;
|
|
799
|
+
}
|
|
800
|
+
catch (error) {
|
|
801
|
+
this.host.logger?.warn?.(`[memory-organize] 压缩失败(${category}): ${String(error)}`);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
let after = autoBlocks.length - removed;
|
|
805
|
+
try {
|
|
806
|
+
const content = await this.host.readMemoryFile(ctx.scope);
|
|
807
|
+
after = (0, memory_1.splitMemoryBlocks)(content)
|
|
808
|
+
.map((item) => item.text)
|
|
809
|
+
.filter((item) => (0, learn_1.isAutoMemoryBlock)(item)).length;
|
|
810
|
+
}
|
|
811
|
+
catch { /* ignore */ }
|
|
812
|
+
return { ok: true, categories, removed, after, reason: "manual" };
|
|
813
|
+
}
|
|
697
814
|
async summarizeRecords(records, kind, scope, reason) {
|
|
698
815
|
const cfg = this.config || {};
|
|
699
816
|
// 每日上限
|
package/lib/service.js
CHANGED
|
@@ -2808,6 +2808,8 @@ class QQGroupManagerService extends koishi_1.Service {
|
|
|
2808
2808
|
'解除禁言 ' + P('cmdUnmute', '[unmute]') + ' qq reason',
|
|
2809
2809
|
'记忆保存 ' + P('cmdMemory', '[memory]') + ' content',
|
|
2810
2810
|
'记忆查询 ' + P('cmdMemory', '[memory]') + ' search keyword',
|
|
2811
|
+
'记忆归纳 ' + P('cmdMemory', '[memory]') + ' summarize',
|
|
2812
|
+
'记忆整理 ' + P('cmdMemory', '[memory]') + ' organize',
|
|
2811
2813
|
'拉黑 ' + P('cmdBan', '[ban]') + ' qq reason',
|
|
2812
2814
|
'解除拉黑 ' + P('cmdUnban', '[unban]') + ' qq reason',
|
|
2813
2815
|
'黑名单查询 ' + P('cmdBanList', '[banlist]'),
|
|
@@ -2986,7 +2988,7 @@ class QQGroupManagerService extends koishi_1.Service {
|
|
|
2986
2988
|
}
|
|
2987
2989
|
const sub = rest.trim();
|
|
2988
2990
|
if (!sub) {
|
|
2989
|
-
await session.send(this.styleText(`用法:${this.cmdPrefix('cmdMemory', '[memory]')} content 保存|${this.cmdPrefix('cmdMemory', '[memory]')} list 列出|${this.cmdPrefix('cmdMemory', '[memory]')} search keyword 查询|${this.cmdPrefix('cmdMemory', '[memory]')} delete keyword
|
|
2991
|
+
await session.send(this.styleText(`用法:${this.cmdPrefix('cmdMemory', '[memory]')} content 保存|${this.cmdPrefix('cmdMemory', '[memory]')} list 列出|${this.cmdPrefix('cmdMemory', '[memory]')} search keyword 查询|${this.cmdPrefix('cmdMemory', '[memory]')} delete keyword 删除|${this.cmdPrefix('cmdMemory', '[memory]')} summarize 归纳|${this.cmdPrefix('cmdMemory', '[memory]')} organize 整理`));
|
|
2990
2992
|
return true;
|
|
2991
2993
|
}
|
|
2992
2994
|
const delMatch = sub.match(/^(?:delete|del)\s*([\s\S]*)$/i);
|
|
@@ -3043,6 +3045,51 @@ class QQGroupManagerService extends koishi_1.Service {
|
|
|
3043
3045
|
this._logger.info(`[cmd:memory] 查询 keyword=${keyword || '-'} 命中=${results.length} | ${this.sessionTag(session)}`);
|
|
3044
3046
|
return true;
|
|
3045
3047
|
}
|
|
3048
|
+
// 手动归纳:[memory] summarize / [memory] 归纳 / [memory] 总结
|
|
3049
|
+
const summarizeMatch = /^(?:summarize|summary|归纳|总结)\s*$/i.exec(sub);
|
|
3050
|
+
if (summarizeMatch) {
|
|
3051
|
+
const auth = await this.authorizeCommand('memory', session);
|
|
3052
|
+
if (!auth.ok)
|
|
3053
|
+
return this.denyAdmin(session, auth.userMessage || '只有主人或管理员可以触发记忆归纳。');
|
|
3054
|
+
await session.send(this.styleText('正在归纳,稍等…'));
|
|
3055
|
+
const r = await this.ai.learnNow(session);
|
|
3056
|
+
if (!r.ok) {
|
|
3057
|
+
const message = r.reason === 'empty'
|
|
3058
|
+
? '没有可归纳的消息。'
|
|
3059
|
+
: r.reason === 'no-scope'
|
|
3060
|
+
? '当前会话无法确定记忆作用域。'
|
|
3061
|
+
: '这次没有提炼出值得长期记住的内容。';
|
|
3062
|
+
await session.send(this.styleText(message));
|
|
3063
|
+
return true;
|
|
3064
|
+
}
|
|
3065
|
+
const body = r.text.length > 400 ? `${r.text.slice(0, 400)}…` : r.text;
|
|
3066
|
+
await session.send(this.styleText(`已归纳 ${r.entries} 条:\n${body}`));
|
|
3067
|
+
this._logger.info(`[cmd:memory] 手动归纳 entries=${r.entries} source=${r.reason} | ${this.sessionTag(session)}`);
|
|
3068
|
+
return true;
|
|
3069
|
+
}
|
|
3070
|
+
// 手动整理:[memory] organize / [memory] 整理
|
|
3071
|
+
const organizeMatch = /^(?:organize|tidy|整理)\s*$/i.exec(sub);
|
|
3072
|
+
if (organizeMatch) {
|
|
3073
|
+
const auth = await this.authorizeCommand('memory', session);
|
|
3074
|
+
if (!auth.ok)
|
|
3075
|
+
return this.denyAdmin(session, auth.userMessage || '只有主人或管理员可以触发记忆整理。');
|
|
3076
|
+
await session.send(this.styleText('正在整理,稍等…'));
|
|
3077
|
+
const r = await this.ai.organizeNow(session);
|
|
3078
|
+
if (!r.ok) {
|
|
3079
|
+
const message = r.reason === 'empty'
|
|
3080
|
+
? '记忆库里还没有自动归纳的内容。'
|
|
3081
|
+
: '当前会话无法确定记忆作用域。';
|
|
3082
|
+
await session.send(this.styleText(message));
|
|
3083
|
+
return true;
|
|
3084
|
+
}
|
|
3085
|
+
if (!r.categories) {
|
|
3086
|
+
await session.send(this.styleText(`当前自动记忆已经足够精简(${r.after} 条),没有可合并的同类条目。`));
|
|
3087
|
+
return true;
|
|
3088
|
+
}
|
|
3089
|
+
await session.send(this.styleText(`已整理:合并 ${r.categories} 个分类,净减 ${r.removed} 条,现在 ${r.after} 条自动记忆。`));
|
|
3090
|
+
this._logger.info(`[cmd:memory] 手动整理 categories=${r.categories} removed=${r.removed} after=${r.after} | ${this.sessionTag(session)}`);
|
|
3091
|
+
return true;
|
|
3092
|
+
}
|
|
3046
3093
|
// 保存
|
|
3047
3094
|
try {
|
|
3048
3095
|
const mscope = this.memoryScopeOf(session);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestim/koishi-plugin-qq-group-manager",
|
|
3
3
|
"description": "Koishi QQ群管理插件(OneBot/LLOneBot):群管命令、权限校验、图片菜单、状态卡片、复读、AI群聊回复、记忆库、识图与自动管控(禁言/踢人)。",
|
|
4
|
-
"version": "1.4.
|
|
4
|
+
"version": "1.4.14",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
package/readme.md
CHANGED
|
@@ -61,6 +61,8 @@ npm i @nestim/koishi-plugin-qq-group-manager
|
|
|
61
61
|
| `[memory] list` | 列出最近 10 条记忆 |
|
|
62
62
|
| `[memory] search 关键词` | 查询记忆(`query` / `find` 同义) |
|
|
63
63
|
| `[memory] delete 关键词` | 删除含该关键词的记忆(`del` 同义) |
|
|
64
|
+
| `[memory] summarize` | 立即触发一次归纳(优先用待归纳消息,没有就用最近上下文;`归纳` / `总结` 同义) |
|
|
65
|
+
| `[memory] organize` | 立即整理自动记忆:同分类 ≥2 条合并成 1 条(`整理` 同义) |
|
|
64
66
|
| `[ban] QQ [原因]` | 加入本群黑名单 |
|
|
65
67
|
| `[unban] QQ [原因]` | 移出本群黑名单 |
|
|
66
68
|
| `[banlist]` | 列出本群黑名单 |
|
package/src/ai/engine.ts
CHANGED
|
@@ -743,6 +743,131 @@ export class AiEngine {
|
|
|
743
743
|
}
|
|
744
744
|
|
|
745
745
|
/** 把一批消息归纳成一条记忆并写入对应作用域;返回写入结果或 null */
|
|
746
|
+
/** 解析会话对应的记忆作用域 */
|
|
747
|
+
private scopeForSession(session: any): { kind: "group" | "private"; id: string; scope: any } | null {
|
|
748
|
+
const kind: "group" | "private" = session?.guildId ? "group" : "private";
|
|
749
|
+
const id = String(session?.guildId || session?.userId || "");
|
|
750
|
+
if (!id) return null;
|
|
751
|
+
const scope = typeof (this.host as any).memoryScopeOf === "function"
|
|
752
|
+
? (this.host as any).memoryScopeOf(session)
|
|
753
|
+
: { kind, id };
|
|
754
|
+
return { kind, id, scope };
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* [memory] summarize:立即触发一次归纳。
|
|
759
|
+
*
|
|
760
|
+
* 优先用「已被挤出、还没归纳」的待归纳桶;桶空时退化为最近上下文,
|
|
761
|
+
* 这样手动触发不会因为还没挤出消息而什么都不做。
|
|
762
|
+
*/
|
|
763
|
+
async learnNow(session: any): Promise<{ ok: boolean; entries: number; text: string; reason: string }> {
|
|
764
|
+
const ctx = this.scopeForSession(session);
|
|
765
|
+
if (!ctx) return { ok: false, entries: 0, text: "", reason: "no-scope" };
|
|
766
|
+
|
|
767
|
+
const bucket = this.learnStore.get(ctx.kind, ctx.id);
|
|
768
|
+
// 先快照、成功后只移除这一次消费的消息:模型失败时不丢待归纳数据
|
|
769
|
+
let records: any[] = bucket.records.slice();
|
|
770
|
+
let source = "bucket";
|
|
771
|
+
|
|
772
|
+
if (!records.length) {
|
|
773
|
+
const platform = String(session?.platform || "onebot");
|
|
774
|
+
let state = this.state.peek(`${platform}:${ctx.id}`);
|
|
775
|
+
if (!state) {
|
|
776
|
+
for (const [key, value] of this.state.entries()) {
|
|
777
|
+
if (key.endsWith(`:${ctx.id}`)) {
|
|
778
|
+
state = value;
|
|
779
|
+
break;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
const limit = Math.max(10, Math.min(40, Number(this.config.aiContextWindow) || 30));
|
|
784
|
+
records = state
|
|
785
|
+
? state.records
|
|
786
|
+
.filter((record) => !record.command && String(record.text || "").trim())
|
|
787
|
+
.slice(-limit)
|
|
788
|
+
: [];
|
|
789
|
+
source = "context";
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
if (!records.length) return { ok: false, entries: 0, text: "", reason: "empty" };
|
|
793
|
+
|
|
794
|
+
const saved = await this.summarizeRecords(records, ctx.kind, ctx.scope, `manual-${source}`);
|
|
795
|
+
if (!saved) return { ok: false, entries: 0, text: "", reason: "none" };
|
|
796
|
+
if (source === "bucket") {
|
|
797
|
+
const consumed = new Set(records);
|
|
798
|
+
bucket.records = bucket.records.filter((record) => !consumed.has(record));
|
|
799
|
+
}
|
|
800
|
+
return { ok: true, entries: saved.entries || 0, text: saved.text, reason: source };
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/**
|
|
804
|
+
* [memory] organize:立即整理一次。
|
|
805
|
+
*
|
|
806
|
+
* 把每个分类里 ≥2 条的自动记忆合并成 1 条;手动记忆不动。
|
|
807
|
+
*/
|
|
808
|
+
async organizeNow(session: any): Promise<{
|
|
809
|
+
ok: boolean;
|
|
810
|
+
categories: number;
|
|
811
|
+
removed: number;
|
|
812
|
+
after: number;
|
|
813
|
+
reason: string;
|
|
814
|
+
}> {
|
|
815
|
+
const ctx = this.scopeForSession(session);
|
|
816
|
+
if (!ctx) return { ok: false, categories: 0, removed: 0, after: 0, reason: "no-scope" };
|
|
817
|
+
|
|
818
|
+
let autoBlocks: string[] = [];
|
|
819
|
+
try {
|
|
820
|
+
const content = await this.host.readMemoryFile(ctx.scope);
|
|
821
|
+
autoBlocks = splitMemoryBlocks(content)
|
|
822
|
+
.map((item) => item.text)
|
|
823
|
+
.filter((item) => isAutoMemoryBlock(item));
|
|
824
|
+
} catch {
|
|
825
|
+
autoBlocks = [];
|
|
826
|
+
}
|
|
827
|
+
if (!autoBlocks.length) {
|
|
828
|
+
return { ok: false, categories: 0, removed: 0, after: 0, reason: "empty" };
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
const byCategory = new Map<string, string[]>();
|
|
832
|
+
for (const block of autoBlocks) {
|
|
833
|
+
const category = memoryBlockCategory(block);
|
|
834
|
+
const list = byCategory.get(category) || [];
|
|
835
|
+
list.push(block);
|
|
836
|
+
byCategory.set(category, list);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const maxChars = Math.max(40, Number(this.config.memoryLearnConsolidateMaxChars) || 90);
|
|
840
|
+
let categories = 0;
|
|
841
|
+
let removed = 0;
|
|
842
|
+
for (const [category, list] of byCategory) {
|
|
843
|
+
if (list.length < 2) continue;
|
|
844
|
+
try {
|
|
845
|
+
const merged = await this.consolidateMemories(ctx.kind, list, "", maxChars, category);
|
|
846
|
+
if (!merged) continue;
|
|
847
|
+
await (this.host as any).commitAutoMemory(ctx.scope, {
|
|
848
|
+
add: merged,
|
|
849
|
+
remove: list,
|
|
850
|
+
label: AUTO_MEMORY_LABEL,
|
|
851
|
+
category,
|
|
852
|
+
});
|
|
853
|
+
categories += 1;
|
|
854
|
+
removed += list.length - 1;
|
|
855
|
+
} catch (error: any) {
|
|
856
|
+
this.host.logger?.warn?.(`[memory-organize] 压缩失败(${category}): ${String(error)}`);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
let after = autoBlocks.length - removed;
|
|
861
|
+
try {
|
|
862
|
+
const content = await this.host.readMemoryFile(ctx.scope);
|
|
863
|
+
after = splitMemoryBlocks(content)
|
|
864
|
+
.map((item) => item.text)
|
|
865
|
+
.filter((item) => isAutoMemoryBlock(item)).length;
|
|
866
|
+
} catch { /* ignore */ }
|
|
867
|
+
|
|
868
|
+
return { ok: true, categories, removed, after, reason: "manual" };
|
|
869
|
+
}
|
|
870
|
+
|
|
746
871
|
private async summarizeRecords(
|
|
747
872
|
records: any[],
|
|
748
873
|
kind: "group" | "private",
|
package/src/service.js
CHANGED
|
@@ -2816,6 +2816,8 @@ class QQGroupManagerService extends koishi_1.Service {
|
|
|
2816
2816
|
'解除禁言 ' + P('cmdUnmute', '[unmute]') + ' qq reason',
|
|
2817
2817
|
'记忆保存 ' + P('cmdMemory', '[memory]') + ' content',
|
|
2818
2818
|
'记忆查询 ' + P('cmdMemory', '[memory]') + ' search keyword',
|
|
2819
|
+
'记忆归纳 ' + P('cmdMemory', '[memory]') + ' summarize',
|
|
2820
|
+
'记忆整理 ' + P('cmdMemory', '[memory]') + ' organize',
|
|
2819
2821
|
'拉黑 ' + P('cmdBan', '[ban]') + ' qq reason',
|
|
2820
2822
|
'解除拉黑 ' + P('cmdUnban', '[unban]') + ' qq reason',
|
|
2821
2823
|
'黑名单查询 ' + P('cmdBanList', '[banlist]'),
|
|
@@ -2996,7 +2998,7 @@ class QQGroupManagerService extends koishi_1.Service {
|
|
|
2996
2998
|
}
|
|
2997
2999
|
const sub = rest.trim();
|
|
2998
3000
|
if (!sub) {
|
|
2999
|
-
await session.send(this.styleText(`用法:${this.cmdPrefix('cmdMemory', '[memory]')} content 保存|${this.cmdPrefix('cmdMemory', '[memory]')} list 列出|${this.cmdPrefix('cmdMemory', '[memory]')} search keyword 查询|${this.cmdPrefix('cmdMemory', '[memory]')} delete keyword
|
|
3001
|
+
await session.send(this.styleText(`用法:${this.cmdPrefix('cmdMemory', '[memory]')} content 保存|${this.cmdPrefix('cmdMemory', '[memory]')} list 列出|${this.cmdPrefix('cmdMemory', '[memory]')} search keyword 查询|${this.cmdPrefix('cmdMemory', '[memory]')} delete keyword 删除|${this.cmdPrefix('cmdMemory', '[memory]')} summarize 归纳|${this.cmdPrefix('cmdMemory', '[memory]')} organize 整理`));
|
|
3000
3002
|
return true;
|
|
3001
3003
|
}
|
|
3002
3004
|
const delMatch = sub.match(/^(?:delete|del)\s*([\s\S]*)$/i);
|
|
@@ -3055,6 +3057,51 @@ class QQGroupManagerService extends koishi_1.Service {
|
|
|
3055
3057
|
this._logger.info(`[cmd:memory] 查询 keyword=${keyword || '-'} 命中=${results.length} | ${this.sessionTag(session)}`);
|
|
3056
3058
|
return true;
|
|
3057
3059
|
}
|
|
3060
|
+
// 手动归纳:[memory] summarize / [memory] 归纳 / [memory] 总结
|
|
3061
|
+
const summarizeMatch = /^(?:summarize|summary|归纳|总结)\s*$/i.exec(sub);
|
|
3062
|
+
if (summarizeMatch) {
|
|
3063
|
+
const auth = await this.authorizeCommand('memory', session);
|
|
3064
|
+
if (!auth.ok)
|
|
3065
|
+
return this.denyAdmin(session, auth.userMessage || '只有主人或管理员可以触发记忆归纳。');
|
|
3066
|
+
await session.send(this.styleText('正在归纳,稍等…'));
|
|
3067
|
+
const r = await this.ai.learnNow(session);
|
|
3068
|
+
if (!r.ok) {
|
|
3069
|
+
const message = r.reason === 'empty'
|
|
3070
|
+
? '没有可归纳的消息。'
|
|
3071
|
+
: r.reason === 'no-scope'
|
|
3072
|
+
? '当前会话无法确定记忆作用域。'
|
|
3073
|
+
: '这次没有提炼出值得长期记住的内容。';
|
|
3074
|
+
await session.send(this.styleText(message));
|
|
3075
|
+
return true;
|
|
3076
|
+
}
|
|
3077
|
+
const body = r.text.length > 400 ? `${r.text.slice(0, 400)}…` : r.text;
|
|
3078
|
+
await session.send(this.styleText(`已归纳 ${r.entries} 条:\n${body}`));
|
|
3079
|
+
this._logger.info(`[cmd:memory] 手动归纳 entries=${r.entries} source=${r.reason} | ${this.sessionTag(session)}`);
|
|
3080
|
+
return true;
|
|
3081
|
+
}
|
|
3082
|
+
// 手动整理:[memory] organize / [memory] 整理
|
|
3083
|
+
const organizeMatch = /^(?:organize|tidy|整理)\s*$/i.exec(sub);
|
|
3084
|
+
if (organizeMatch) {
|
|
3085
|
+
const auth = await this.authorizeCommand('memory', session);
|
|
3086
|
+
if (!auth.ok)
|
|
3087
|
+
return this.denyAdmin(session, auth.userMessage || '只有主人或管理员可以触发记忆整理。');
|
|
3088
|
+
await session.send(this.styleText('正在整理,稍等…'));
|
|
3089
|
+
const r = await this.ai.organizeNow(session);
|
|
3090
|
+
if (!r.ok) {
|
|
3091
|
+
const message = r.reason === 'empty'
|
|
3092
|
+
? '记忆库里还没有自动归纳的内容。'
|
|
3093
|
+
: '当前会话无法确定记忆作用域。';
|
|
3094
|
+
await session.send(this.styleText(message));
|
|
3095
|
+
return true;
|
|
3096
|
+
}
|
|
3097
|
+
if (!r.categories) {
|
|
3098
|
+
await session.send(this.styleText(`当前自动记忆已经足够精简(${r.after} 条),没有可合并的同类条目。`));
|
|
3099
|
+
return true;
|
|
3100
|
+
}
|
|
3101
|
+
await session.send(this.styleText(`已整理:合并 ${r.categories} 个分类,净减 ${r.removed} 条,现在 ${r.after} 条自动记忆。`));
|
|
3102
|
+
this._logger.info(`[cmd:memory] 手动整理 categories=${r.categories} removed=${r.removed} after=${r.after} | ${this.sessionTag(session)}`);
|
|
3103
|
+
return true;
|
|
3104
|
+
}
|
|
3058
3105
|
// 保存
|
|
3059
3106
|
try {
|
|
3060
3107
|
const mscope = this.memoryScopeOf(session);
|