chatluna-plugin-livingmemory 0.0.9 → 0.0.11
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/README.md +5 -3
- package/client/index.ts +1 -0
- package/client/locales/en.yml +16 -5
- package/client/locales/zh.yml +17 -6
- package/client/page.css +65 -0
- package/client/page.tsx +246 -131
- package/dist/.vite/manifest.json +5 -1
- package/dist/client.css +3 -1
- package/dist/client.js +1 -1
- package/dist/page-Cv573unw.js +7 -0
- package/lib/contracts/rpc.d.ts +3 -1
- package/lib/contracts/rpc.d.ts.map +1 -1
- package/lib/contracts/webui.d.ts +3 -1
- package/lib/contracts/webui.d.ts.map +1 -1
- package/lib/contracts/webui_live.d.ts +2 -1
- package/lib/contracts/webui_live.d.ts.map +1 -1
- package/lib/index.js +116 -49
- package/lib/index.js.map +1 -1
- package/lib/query.d.ts +3 -3
- package/lib/query.d.ts.map +1 -1
- package/lib/routing.d.ts.map +1 -1
- package/lib/service/app/admin_service.d.ts +2 -2
- package/lib/service/app/admin_service.d.ts.map +1 -1
- package/lib/service/app/webui.d.ts.map +1 -1
- package/lib/service/persistence/repository.d.ts +3 -0
- package/lib/service/persistence/repository.d.ts.map +1 -1
- package/lib/service.d.ts.map +1 -1
- package/package.json +4 -1
- package/src/contracts/rpc.ts +6 -1
- package/src/contracts/webui.ts +3 -1
- package/src/contracts/webui_live.ts +2 -0
- package/dist/page-EGG2hcC3.js +0 -3
package/lib/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import Schema from "schemastery";
|
|
2
2
|
import { Schema as Schema$1, modelAddressSchema, schemasteryToJsonSchemaNode } from "@chatluna/schema";
|
|
3
3
|
import { routeIdOf } from "@chatluna/satori";
|
|
4
|
+
import { assignDefined, isRecord } from "@chatluna/utils/object";
|
|
4
5
|
import { Service } from "cordis";
|
|
6
|
+
import { existsSync, watch } from "node:fs";
|
|
5
7
|
import { createHash, randomUUID } from "node:crypto";
|
|
6
8
|
import { defineTool } from "@chatluna/tool";
|
|
7
|
-
import { isRecord } from "@chatluna/utils/object";
|
|
8
9
|
import { $ } from "@cordisjs/plugin-database";
|
|
9
10
|
import { estimateMessageTokens, resolveChatlunaDataDir } from "@chatluna/utils";
|
|
10
11
|
import { dirname, resolve } from "node:path";
|
|
11
|
-
import { existsSync } from "node:fs";
|
|
12
12
|
import { rename, rm } from "node:fs/promises";
|
|
13
13
|
import { setImmediate, setTimeout } from "node:timers/promises";
|
|
14
14
|
import { performance as performance$1 } from "node:perf_hooks";
|
|
@@ -21,7 +21,7 @@ import { ChatMessage } from "@chatluna/message";
|
|
|
21
21
|
// src/routing.ts
|
|
22
22
|
const ROUTE_SCHEMA = Schema$1.object({
|
|
23
23
|
extractionWindowMessages: Schema$1.natural().max(360).default(60).label("从聊天中提取记忆的间隔").description("累计多少条消息后从聊天中提取记忆;0 表示关闭记忆提取。"),
|
|
24
|
-
extractionIncludeOverheard: Schema$1.boolean().default(false).label("允许群友闲聊触发记忆提取").description("开启后,bot 未参与的群友闲聊也会触发记忆提取。"),
|
|
24
|
+
extractionIncludeOverheard: Schema$1.boolean().default(false).label("允许群友闲聊触发记忆提取").description("开启后,bot 未参与的群友闲聊也会触发记忆提取。").extra("chat", "group"),
|
|
25
25
|
enableAutoDream: Schema$1.boolean().default(true).label("是否在提取新记忆后进行 Dream 整理"),
|
|
26
26
|
recallIntervalMessages: Schema$1.natural().max(200).default(20).label("记忆自动召回的间隔").description("bot 发完一轮回复后检查,自上次召回起累计到此条数则自动召回;0 表示关闭记忆召回。"),
|
|
27
27
|
recallHistoryMessages: Schema$1.natural().min(1).max(200).default(20).label("用于召回记忆的聊天消息数").description("使用多少条聊天信息作为上下文来召回记忆,建议与记忆自动召回的间隔保持一致。"),
|
|
@@ -45,12 +45,12 @@ function registerRouteSettings(ctx) {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
/** 首次通知可以先于任何私聊消息;chat.wake 已将目标地址写入会话环境。 */
|
|
48
|
-
function privateWakeAddress(ctx, sessionId) {
|
|
48
|
+
function privateWakeAddress(ctx, sessionId, routeId) {
|
|
49
49
|
if (sessionId === void 0) return;
|
|
50
50
|
const target = ctx.chat.environmentOf(sessionId);
|
|
51
51
|
if (!target?.isDirect || !target.platform || !target.selfId || !target.userId) return;
|
|
52
52
|
const { platform, selfId, guildId, channelId, userId } = target;
|
|
53
|
-
|
|
53
|
+
const address = {
|
|
54
54
|
platform,
|
|
55
55
|
selfId,
|
|
56
56
|
guildId,
|
|
@@ -58,48 +58,74 @@ function privateWakeAddress(ctx, sessionId) {
|
|
|
58
58
|
userId,
|
|
59
59
|
isDirect: true
|
|
60
60
|
};
|
|
61
|
+
const targetRoute = routeIdOf(address);
|
|
62
|
+
if (!routeId || targetRoute === routeId) return {
|
|
63
|
+
...address,
|
|
64
|
+
routeId: targetRoute
|
|
65
|
+
};
|
|
61
66
|
}
|
|
62
67
|
/** 上游的自定义会话键不含地点,可能同时连接多个群或私聊。 */
|
|
63
68
|
const isCustomConversation = (key) => key?.startsWith("custom:") === true;
|
|
64
|
-
function
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
69
|
+
function routingAddress(ctx, key, location, routeId, noticeSessionId) {
|
|
70
|
+
const shared = isCustomConversation(key);
|
|
71
|
+
if (shared && !routeId) return {};
|
|
72
|
+
const observed = key !== void 0 ? ctx.chat_observer.channelOf(key) : void 0;
|
|
73
|
+
if (observed && (!routeId || observed.routeId === routeId)) return {
|
|
74
|
+
...observed.identity,
|
|
75
|
+
routeId: observed.routeId
|
|
76
|
+
};
|
|
77
|
+
const target = privateWakeAddress(ctx, noticeSessionId, routeId);
|
|
78
|
+
if (target) return target;
|
|
79
|
+
if (shared || !location || location.route && location.route !== routeId) return { routeId };
|
|
80
|
+
const { platform, selfId, isDirect, guild: guildId, channel: channelId, user: userId } = location;
|
|
81
|
+
if (platform && selfId && (isDirect === false || isDirect && userId)) {
|
|
82
|
+
const locationRoute = routeIdOf({
|
|
83
|
+
platform,
|
|
84
|
+
selfId,
|
|
85
|
+
isDirect,
|
|
86
|
+
guildId,
|
|
87
|
+
channelId,
|
|
88
|
+
userId: userId ?? ""
|
|
89
|
+
});
|
|
90
|
+
if (routeId && locationRoute !== routeId) return { routeId };
|
|
91
|
+
return {
|
|
92
|
+
platform,
|
|
93
|
+
selfId,
|
|
94
|
+
isDirect,
|
|
95
|
+
guildId,
|
|
96
|
+
channelId,
|
|
97
|
+
userId,
|
|
98
|
+
routeId: locationRoute
|
|
99
|
+
};
|
|
84
100
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
guildId
|
|
90
|
-
channelId
|
|
91
|
-
|
|
92
|
-
|
|
101
|
+
return {
|
|
102
|
+
platform,
|
|
103
|
+
selfId,
|
|
104
|
+
isDirect,
|
|
105
|
+
guildId,
|
|
106
|
+
channelId,
|
|
107
|
+
routeId
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function routeSettings(ctx, key, location, routeId, noticeSessionId) {
|
|
111
|
+
const address = routingAddress(ctx, key, location, routeId ?? location?.route, noticeSessionId);
|
|
112
|
+
const { platform = "", selfId = "", isDirect, guildId, channelId, userId } = address;
|
|
113
|
+
const input = assignDefined({
|
|
114
|
+
platform,
|
|
115
|
+
selfId,
|
|
116
|
+
isDirect: isDirect ?? false
|
|
117
|
+
}, {
|
|
118
|
+
guildId,
|
|
119
|
+
channelId,
|
|
120
|
+
userId: isDirect ? userId : void 0,
|
|
121
|
+
routeId: address.routeId
|
|
93
122
|
});
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
route
|
|
101
|
-
}, "livingmemory");
|
|
102
|
-
return ROUTE_SCHEMA.parse(values ?? {});
|
|
123
|
+
const settings = ctx.routing.resolve(input).get("livingmemory");
|
|
124
|
+
if (isDirect !== false && settings.extractionIncludeOverheard) return {
|
|
125
|
+
...settings,
|
|
126
|
+
extractionIncludeOverheard: false
|
|
127
|
+
};
|
|
128
|
+
return settings;
|
|
103
129
|
}
|
|
104
130
|
// src/service/memory/errors.ts
|
|
105
131
|
var MemoryInputError = class extends Error {};
|
|
@@ -2383,6 +2409,16 @@ var LivingMemoryRepository = class {
|
|
|
2383
2409
|
async getEntriesWithSources(presetId, ids) {
|
|
2384
2410
|
return this.withSourceLabels(await this.getEntriesByPresetAndIds(presetId, ids));
|
|
2385
2411
|
}
|
|
2412
|
+
async listProfileMemories(presetId, profileId) {
|
|
2413
|
+
const [profile] = await this.database.get("living_memory_user_profile", {
|
|
2414
|
+
presetId,
|
|
2415
|
+
id: profileId
|
|
2416
|
+
}, ["sourceMemoryIds"]);
|
|
2417
|
+
const ids = profile?.sourceMemoryIds ?? [];
|
|
2418
|
+
const entries = await this.getEntriesWithSources(presetId, ids);
|
|
2419
|
+
const byId = new Map(entries.map((entry) => [entry.id, entry]));
|
|
2420
|
+
return ids.map((id) => byId.get(id)).filter((entry) => entry !== void 0);
|
|
2421
|
+
}
|
|
2386
2422
|
async getSearchEntriesByPresetAndIds(presetId, ids) {
|
|
2387
2423
|
return this.withSourceLabels(await this.entries.getSearchEntriesByPresetAndIds(presetId, ids));
|
|
2388
2424
|
}
|
|
@@ -7621,10 +7657,8 @@ const filterJobList = (items, query) => {
|
|
|
7621
7657
|
return paginate(sorted, query.page, query.pageSize);
|
|
7622
7658
|
};
|
|
7623
7659
|
const filterUserProfileList = (items, query) => {
|
|
7624
|
-
const
|
|
7625
|
-
|
|
7626
|
-
return left.speakerLabel.localeCompare(right.speakerLabel);
|
|
7627
|
-
});
|
|
7660
|
+
const keyword = query.keyword?.trim().toLowerCase();
|
|
7661
|
+
const sorted = (keyword ? items.filter((profile) => profile.speakerLabel.toLowerCase().includes(keyword) || profile.speakerId?.toLowerCase().includes(keyword)) : items).toSorted((left, right) => +right.updatedAt - +left.updatedAt || left.speakerLabel.localeCompare(right.speakerLabel));
|
|
7628
7662
|
return paginate(sorted, query.page, query.pageSize);
|
|
7629
7663
|
};
|
|
7630
7664
|
// src/service/app/admin_service.ts
|
|
@@ -7690,7 +7724,16 @@ var LivingMemoryAdminService = class {
|
|
|
7690
7724
|
return this.deps.dreamCoordinator.start(presetId);
|
|
7691
7725
|
}
|
|
7692
7726
|
async listUserProfiles(query) {
|
|
7693
|
-
|
|
7727
|
+
const [profiles, speakers] = await Promise.all([this.deps.repository.listUserProfilesByPreset(query.presetId), this.listPresetSpeakers(query.presetId)]);
|
|
7728
|
+
const byKey = new Map(speakers.map((speaker) => [speaker.speakerKey, speaker]));
|
|
7729
|
+
return filterUserProfileList(profiles.map((profile) => {
|
|
7730
|
+
const speaker = byKey.get(profile.speakerKey);
|
|
7731
|
+
return {
|
|
7732
|
+
...profile,
|
|
7733
|
+
speakerId: speaker?.speakerId ?? null,
|
|
7734
|
+
platform: speaker?.platform ?? null
|
|
7735
|
+
};
|
|
7736
|
+
}), query);
|
|
7694
7737
|
}
|
|
7695
7738
|
listPresetSpeakers(presetId) {
|
|
7696
7739
|
return this.deps.repository.listPresetSpeakers(presetId);
|
|
@@ -7868,6 +7911,7 @@ const livingMemoryWebUI = (ctx, ui) => {
|
|
|
7868
7911
|
ctx.chatluna_webui.state("livingmemory.profiles", {
|
|
7869
7912
|
params: Schema$1.object({
|
|
7870
7913
|
presetId: Schema$1.string().required(),
|
|
7914
|
+
keyword: Schema$1.string(),
|
|
7871
7915
|
page: Schema$1.natural(),
|
|
7872
7916
|
pageSize: Schema$1.natural()
|
|
7873
7917
|
}),
|
|
@@ -7883,6 +7927,16 @@ const livingMemoryWebUI = (ctx, ui) => {
|
|
|
7883
7927
|
},
|
|
7884
7928
|
refreshOn
|
|
7885
7929
|
});
|
|
7930
|
+
ctx.chatluna_webui.state("livingmemory.profileMemories", {
|
|
7931
|
+
params: Schema$1.object({
|
|
7932
|
+
presetId: Schema$1.string().required(),
|
|
7933
|
+
profileId: Schema$1.string().required()
|
|
7934
|
+
}),
|
|
7935
|
+
get: async ({ presetId, profileId }) => {
|
|
7936
|
+
return (await ui.repository.listProfileMemories(presetId, profileId)).map(toEntryView);
|
|
7937
|
+
},
|
|
7938
|
+
refreshOn
|
|
7939
|
+
});
|
|
7886
7940
|
ctx.chatluna_webui.state("livingmemory.snapshots", {
|
|
7887
7941
|
params: Schema$1.object({
|
|
7888
7942
|
presetId: Schema$1.string().required(),
|
|
@@ -8106,8 +8160,10 @@ const toProfileView = (profile) => ({
|
|
|
8106
8160
|
id: profile.id,
|
|
8107
8161
|
speakerKey: profile.speakerKey,
|
|
8108
8162
|
speakerLabel: profile.speakerLabel,
|
|
8163
|
+
speakerId: profile.speakerId,
|
|
8164
|
+
platform: profile.platform,
|
|
8109
8165
|
content: profile.content,
|
|
8110
|
-
|
|
8166
|
+
sourceMemoryCount: profile.sourceMemoryIds.length,
|
|
8111
8167
|
updatedAt: profile.updatedAt.getTime()
|
|
8112
8168
|
});
|
|
8113
8169
|
function toSnapshotView(snapshot, sources) {
|
|
@@ -8635,10 +8691,21 @@ var LivingmemoryService = class extends Service {
|
|
|
8635
8691
|
name: entry.options.name
|
|
8636
8692
|
} : null;
|
|
8637
8693
|
} });
|
|
8638
|
-
|
|
8694
|
+
const files = {
|
|
8639
8695
|
baseUrl: import.meta.url,
|
|
8640
8696
|
source: "../client/index.ts",
|
|
8641
8697
|
bundle: "../dist/client.js"
|
|
8698
|
+
};
|
|
8699
|
+
const webui = ctx.chatluna_webui;
|
|
8700
|
+
let entry = webui.addEntry(files);
|
|
8701
|
+
if (!webui.config.devMode && existsSync(new URL("../scripts/build-client.mjs", import.meta.url))) ctx.effect(() => {
|
|
8702
|
+
const watcher = watch(new URL("../", import.meta.url), (_event, file) => {
|
|
8703
|
+
if (file !== ".webui-reload") return;
|
|
8704
|
+
const previous = entry;
|
|
8705
|
+
entry = webui.addEntry(files);
|
|
8706
|
+
previous.dispose();
|
|
8707
|
+
});
|
|
8708
|
+
return () => watcher.close();
|
|
8642
8709
|
});
|
|
8643
8710
|
livingMemoryWebUI(ctx, ui);
|
|
8644
8711
|
}
|