dsh-tdai-memory 0.4.1 → 0.4.3
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 +14 -0
- package/README.zh.md +13 -0
- package/client.js +119 -33
- package/index.js +11 -11
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +102 -97
- package/recall-inject.js +88 -86
- package/recall-runtime.js +42 -0
package/README.md
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
# dsh-tdai-memory
|
|
2
|
+
|
|
3
|
+
## DSH 0.2.1-rc.1
|
|
4
|
+
|
|
5
|
+
Supports DSH 0.2.1-rc.1. Compatibility is declared through `engines.dsh` and matching peer ranges, including this exact prerelease. The existing 0.2.0-rc.2 support is retained.
|
|
2
6
|
|
|
7
|
+
The plugin follows the DSH language setting (Chinese and English in DSH 0.2.0-rc.2), including configuration, status messages and plugin-list metadata. Language-pack locales use the host fallback chain. Switching languages preserves unsaved settings; there is no separate plugin language selector.
|
|
8
|
+
|
|
3
9
|
## Configuration page (DSH 0.2.0-rc.2 and later)
|
|
4
10
|
|
|
5
11
|
Open **Plugins → Installed → dsh-tdai-memory** from the homepage sidebar to configure and save this plugin. The page uses the official `plugins.bundle.config` interface, without a duplicate entry in global Settings. Web and Desktop share the page. This version requires DSH 0.2.0-rc.2 or a later 0.2.x host; existing configuration is retained.
|
|
6
12
|
|
|
13
|
+
Choose **Automatic memory** for capture, extraction, recall and search together; **Search existing memory only** disables capture, extraction and automatic recall while keeping search tools; **Pause memory features** disables all four. The main page contains the two model connections. Individual switches, storage paths and tuning remain under **Advanced**. Existing mixed switch values appear as **Custom combination** and are preserved. Select a mode, Save, then restart DSH for pipeline changes (the search-tool switch applies immediately). Only edited fields are written; blank keys and untouched advanced options remain unchanged.
|
|
14
|
+
|
|
7
15
|
[](README.zh.md)
|
|
8
16
|
|
|
9
17
|
**GitHub**: [Scorp1o117/dsh-tdai-memory](https://github.com/Scorp1o117/dsh-tdai-memory) · **npm**: [dsh-tdai-memory](https://www.npmjs.com/package/dsh-tdai-memory)
|
|
@@ -57,6 +65,12 @@ The data directory reuses the existing `~/.memory-tencentdb/memory-tdai`, so
|
|
|
57
65
|
| dsh shell | `index.js`: config mapping, `session/event` + `session/flush` capture, `system-prompt/assemble` recall injection on `agent.ctx`, tool registration, lifecycle |
|
|
58
66
|
| Fallback | `recall-inject.js`: preset-row recall injection (used when mounted inside an agent preset) |
|
|
59
67
|
|
|
68
|
+
Recall caches expire after 30 seconds and retain at most 128 sessions per
|
|
69
|
+
injection instance. Failed or timed-out recalls are retried on the next assembly.
|
|
70
|
+
The preset row also accepts `timeoutMs` (default 4000); reaching this deadline
|
|
71
|
+
continues prompt assembly without memory context. Deadline timers are cleared
|
|
72
|
+
when recall settles early. Underlying recall work may continue after the deadline.
|
|
73
|
+
|
|
60
74
|
Hard-won wiring details:
|
|
61
75
|
|
|
62
76
|
- **Capture**: `session/flush` listener (await semantics; must complete before
|
package/README.zh.md
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
# dsh-tdai-memory
|
|
2
|
+
|
|
3
|
+
## DSH 0.2.1-rc.1
|
|
4
|
+
|
|
5
|
+
支持 DSH 0.2.1-rc.1,通过 `engines.dsh` 和一致的 peer 范围声明兼容,明确包含该预发布版本,并保留已有 0.2.0-rc.2 支持。
|
|
2
6
|
|
|
7
|
+
插件跟随 DSH 的语言设置(DSH 0.2.0-rc.2 内置中文和 English),配置页面、状态提示和插件列表名称/简介同步切换。扩展语言使用宿主的回退链。切换语言保留未保存的设置,无需单独选择插件语言。
|
|
8
|
+
|
|
3
9
|
## 配置入口(DSH 0.2.0-rc.2 起)
|
|
4
10
|
|
|
5
11
|
在首页侧边栏打开 **插件 → 已安装 → dsh-tdai-memory**,直接在插件详情页配置并保存。配置页注册到官方的 `plugins.bundle.config` 接口;全局设置页不再重复显示配置入口。Web 与桌面版使用相同界面,本版要求 DSH 0.2.0-rc.2 或更新的 0.2.x 版本。现有配置无需迁移。
|
|
6
12
|
|
|
13
|
+
选择 **自动记忆**,一次开启捕获、提取、召回和搜索;**仅搜索已有记忆** 关闭捕获、提取和自动召回,保留搜索工具;**暂停记忆功能** 关闭全部四项。主页面只展示两个模型的连接信息,独立开关、存储路径和调优参数保留在 **高级设置** 中。已有开关组合显示为 **自定义组合**,不会自动覆盖。选择模式后保存,并重启 DSH 使记忆流水线设置生效(搜索工具开关即时生效)。只写入编辑过的字段,密钥留空及未编辑的高级设置保持原值。
|
|
14
|
+
|
|
7
15
|
**GitHub**: [Scorp1o117/dsh-tdai-memory](https://github.com/Scorp1o117/dsh-tdai-memory) · **npm**: [dsh-tdai-memory](https://www.npmjs.com/package/dsh-tdai-memory) · [English](README.md)
|
|
8
16
|
|
|
9
17
|
[](https://github.com/Scorp1o117/dsh-enhancement-suite) [](https://www.npmjs.com/package/dsh-enhancement-suite)
|
|
@@ -52,6 +60,11 @@ dsh plugin --profile desktop add dsh-tdai-memory@0.3.6
|
|
|
52
60
|
| dsh 壳 | `index.js`:配置映射、`session/event`+`session/flush` 捕获、`agent.ctx` 上的 `system-prompt/assemble` waterfall 召回注入、工具注册、生命周期 |
|
|
53
61
|
| 备用 | `recall-inject.js`:preset 行形态的召回注入(挂在 agent preset 内时用) |
|
|
54
62
|
|
|
63
|
+
召回缓存默认 30 秒过期,每个注入实例最多保留 128 个会话。失败或超时的召回
|
|
64
|
+
会在下次组装时重试。preset 行还支持 `timeoutMs`(默认 4000),到达期限后
|
|
65
|
+
不注入记忆并继续组装提示词;提前完成时会清除超时定时器。期限到达后,底层
|
|
66
|
+
召回任务可能仍会继续运行。
|
|
67
|
+
|
|
55
68
|
关键接线(都是踩坑换来的):
|
|
56
69
|
|
|
57
70
|
- **捕获**:`session/flush` 监听器(await 语义,headless 退出前必完成);`turn/start` 时间戳做 L0 cursor 下限;turn id 去重
|
package/client.js
CHANGED
|
@@ -36,6 +36,35 @@ window.__ModuleLoader__.load({
|
|
|
36
36
|
".__tm_status{font-size:12px;color:var(--dsw-alias-label-tertiary)}" +
|
|
37
37
|
".__tm_error{font-size:12px;color:var(--dsw-alias-state-error-primary)}" +
|
|
38
38
|
".__tm_unavailable{font-size:13px;color:var(--dsw-alias-label-tertiary)}";
|
|
39
|
+
// Scoped flat controls retain native keyboard and form behavior.
|
|
40
|
+
CSS += `
|
|
41
|
+
.dsh-flat.__tm_root{width:100%;max-width:720px;gap:14px;font-size:13px;line-height:1.65;color:var(--dsw-alias-label-primary);--flat-accent:var(--dsw-alias-state-business-primary,#3964fe);--flat-border:var(--dsw-alias-border-l2,#dce2eb)}
|
|
42
|
+
.dsh-flat.__tm_root *{box-sizing:border-box;min-width:0}
|
|
43
|
+
.dsh-flat.__tm_root p{margin:0}
|
|
44
|
+
.dsh-flat.__tm_root label[class$="_field"]{gap:7px}
|
|
45
|
+
.dsh-flat.__tm_root [class$="_label"]{font-size:13px;font-weight:500}
|
|
46
|
+
.dsh-flat.__tm_root [class$="_hint"]{font-size:12px;line-height:1.65}
|
|
47
|
+
.dsh-flat.__tm_root input:not([type=checkbox]),.dsh-flat.__tm_root select,.dsh-flat.__tm_root textarea{width:100%;border:1px solid var(--flat-border);border-radius:6px;background:var(--dsw-alias-bg-layer-3);color:inherit;font:inherit;padding:9px 12px;min-height:40px;box-shadow:none;transition:border-color .15s}
|
|
48
|
+
.dsh-flat.__tm_root input:hover:not(:disabled),.dsh-flat.__tm_root select:hover:not(:disabled),.dsh-flat.__tm_root textarea:hover:not(:disabled){border-color:var(--dsw-alias-label-tertiary)}
|
|
49
|
+
.dsh-flat.__tm_root select{appearance:none;padding-right:34px;background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='m2 2 4 4 4-4' fill='none' stroke='%23778091' stroke-width='1.5'/%3E%3C/svg%3E");background-repeat:no-repeat;background-position:right 12px center}
|
|
50
|
+
.dsh-flat.__tm_root input[type=checkbox]{appearance:none;flex:none;width:30px;height:18px;margin:0;border:1px solid var(--flat-border);border-radius:12px;background:var(--dsw-alias-bg-layer-2);position:relative;cursor:pointer;transition:background .15s,border-color .15s}
|
|
51
|
+
.dsh-flat.__tm_root input[type=checkbox]::before{content:"";position:absolute;left:2px;top:2px;width:12px;height:12px;border-radius:50%;background:var(--dsw-alias-label-secondary);transition:transform .15s}
|
|
52
|
+
.dsh-flat.__tm_root input[type=checkbox]:checked{background:var(--flat-accent);border-color:var(--flat-accent)}
|
|
53
|
+
.dsh-flat.__tm_root input[type=checkbox]:checked::before{transform:translateX(12px);background:#fff}
|
|
54
|
+
.dsh-flat.__tm_root :is(input,select,textarea,button,summary,a):focus-visible{outline:2px solid var(--flat-accent);outline-offset:3px}
|
|
55
|
+
.dsh-flat.__tm_root :is(input,select,textarea,button):disabled{opacity:.5;cursor:default}
|
|
56
|
+
.dsh-flat.__tm_root [class$="_actions"]{flex-wrap:wrap;gap:10px;margin-top:4px;padding-top:16px;border-top:1px solid var(--flat-border)}
|
|
57
|
+
.dsh-flat.__tm_root details{border-top:1px solid var(--flat-border);padding:0}
|
|
58
|
+
.dsh-flat.__tm_root summary{list-style:none;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:14px 0;font-size:13px;font-weight:500;cursor:pointer;color:var(--dsw-alias-label-secondary)}
|
|
59
|
+
.dsh-flat.__tm_root summary::-webkit-details-marker{display:none}
|
|
60
|
+
.dsh-flat.__tm_root summary::after{content:"+";font-size:18px;font-weight:400;flex:none}
|
|
61
|
+
.dsh-flat.__tm_root details[open]>summary::after{content:"−"}
|
|
62
|
+
.dsh-flat.__tm_root details>div{padding-bottom:18px}
|
|
63
|
+
.dsh-flat.__tm_root .__tm_group{border:0;font-weight:600;font-size:14px;margin:8px 0 0;padding:0}
|
|
64
|
+
@media(prefers-reduced-motion:reduce){.dsh-flat.__tm_root *,.dsh-flat.__tm_root input[type=checkbox]::before{transition:none}}
|
|
65
|
+
.dsh-flat.__tm_root button{border-radius:6px;min-height:34px;padding:7px 14px;font:inherit;font-size:12px;box-shadow:none}
|
|
66
|
+
.dsh-flat.__tm_root :is(h2,h3){margin:0;font-size:14px;font-weight:600}
|
|
67
|
+
`;
|
|
39
68
|
var tagId = "dsh-tdai-memory/main.css";
|
|
40
69
|
if (typeof document !== "undefined" && document.querySelector("style[data-plugin-css=" + JSON.stringify(tagId) + "]") === null) {
|
|
41
70
|
var tag = document.createElement("style");
|
|
@@ -50,10 +79,18 @@ window.__ModuleLoader__.load({
|
|
|
50
79
|
var inject = ["slots", "locale", "configForms"];
|
|
51
80
|
var zh = {
|
|
52
81
|
nav: "记忆",
|
|
53
|
-
|
|
82
|
+
mode: "记忆模式",
|
|
83
|
+
modeAuto: "自动记忆(推荐)",
|
|
84
|
+
modeSearch: "仅搜索已有记忆",
|
|
85
|
+
modePaused: "暂停记忆功能",
|
|
86
|
+
modeCustom: "自定义组合",
|
|
87
|
+
modeHint: "自动:捕获、提取、召回和搜索全部开启;仅搜索:不记录新对话、不自动注入记忆;暂停:四项全部关闭。选择后点击保存。",
|
|
88
|
+
advanced: "高级设置:独立开关、存储与请求参数",
|
|
89
|
+
setupHint: "填写提取模型和向量模型的连接信息,再保存。已有自定义设置会保留;密钥留空不修改。",
|
|
90
|
+
intro: "记录对话、整理长期记忆,并在相关对话中自动召回。保存后请重启 DSH 生效;密钥只写不读。",
|
|
54
91
|
groupData: "数据",
|
|
55
|
-
groupLlm: "
|
|
56
|
-
groupEmbedding: "
|
|
92
|
+
groupLlm: "记忆整理模型",
|
|
93
|
+
groupEmbedding: "向量检索模型",
|
|
57
94
|
groupCapture: "捕获",
|
|
58
95
|
groupExtraction: "提取",
|
|
59
96
|
groupRecall: "召回",
|
|
@@ -95,10 +132,18 @@ window.__ModuleLoader__.load({
|
|
|
95
132
|
};
|
|
96
133
|
var en = {
|
|
97
134
|
nav: "Memory",
|
|
98
|
-
|
|
135
|
+
mode: "Memory mode",
|
|
136
|
+
modeAuto: "Automatic memory (recommended)",
|
|
137
|
+
modeSearch: "Search existing memory only",
|
|
138
|
+
modePaused: "Pause memory features",
|
|
139
|
+
modeCustom: "Custom combination",
|
|
140
|
+
modeHint: "Automatic enables capture, extraction, recall and search. Search only records no new conversations and injects no memory. Pause disables all four. Click Save to apply your selection.",
|
|
141
|
+
advanced: "Advanced: individual switches, storage and request options",
|
|
142
|
+
setupHint: "Enter the extraction and embedding connections, then save. Existing custom settings are preserved; blank keys stay unchanged.",
|
|
143
|
+
intro: "Record conversations, organize long-term memory and recall it in relevant chats. Restart DSH after saving to apply changes; keys are write-only.",
|
|
99
144
|
groupData: "Data",
|
|
100
|
-
groupLlm: "
|
|
101
|
-
groupEmbedding: "
|
|
145
|
+
groupLlm: "Memory extraction model",
|
|
146
|
+
groupEmbedding: "Memory search embeddings",
|
|
102
147
|
groupCapture: "Capture",
|
|
103
148
|
groupExtraction: "Extraction",
|
|
104
149
|
groupRecall: "Recall",
|
|
@@ -165,6 +210,9 @@ window.__ModuleLoader__.load({
|
|
|
165
210
|
{ path: ["toolsEnabled"], label: "fieldToolsEnabled", type: "checkbox", group: "groupTools" }
|
|
166
211
|
];
|
|
167
212
|
FIELDS.forEach(function (f) { f.key = f.path.join("."); });
|
|
213
|
+
var BASIC_KEYS = ["llm.baseUrl", "llm.apiKey", "llm.model", "embedding.baseUrl", "embedding.apiKey", "embedding.model"];
|
|
214
|
+
var MODE_KEYS = ["captureEnabled", "extraction.enabled", "recall.enabled", "toolsEnabled"];
|
|
215
|
+
var MODES = { auto: [true, true, true, true], search: [false, false, false, true], paused: [false, false, false, false] };
|
|
168
216
|
|
|
169
217
|
function getPath(obj, path) {
|
|
170
218
|
var cur = obj;
|
|
@@ -176,6 +224,7 @@ window.__ModuleLoader__.load({
|
|
|
176
224
|
}
|
|
177
225
|
|
|
178
226
|
function MemorySection(props) {
|
|
227
|
+
useLocale(props.locale);
|
|
179
228
|
var t = props.t;
|
|
180
229
|
var scope = props.scope;
|
|
181
230
|
var [snapshot, setSnapshot] = react.useState(function () { return scope.getSnapshot(); });
|
|
@@ -204,7 +253,7 @@ window.__ModuleLoader__.load({
|
|
|
204
253
|
// call, so depending on snapshot.value would reset user input on every
|
|
205
254
|
// render (typing appears dead).
|
|
206
255
|
react.useEffect(function () {
|
|
207
|
-
if (ready) setDraft(
|
|
256
|
+
if (ready) setDraft({});
|
|
208
257
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
209
258
|
}, [ready]);
|
|
210
259
|
|
|
@@ -226,6 +275,23 @@ window.__ModuleLoader__.load({
|
|
|
226
275
|
setNotice(null);
|
|
227
276
|
setError(null);
|
|
228
277
|
}
|
|
278
|
+
function memoryMode() {
|
|
279
|
+
var flags = MODE_KEYS.map(function (key) {
|
|
280
|
+
return Object.prototype.hasOwnProperty.call(draft, key) ? draft[key] : getPath(value, key.split(".")) !== false;
|
|
281
|
+
});
|
|
282
|
+
return Object.keys(MODES).find(function (mode) {
|
|
283
|
+
return MODES[mode].every(function (flag, i) { return flag === flags[i]; });
|
|
284
|
+
}) || "custom";
|
|
285
|
+
}
|
|
286
|
+
function setMode(mode) {
|
|
287
|
+
if (!MODES[mode] || busy || !snapshot.writable) return;
|
|
288
|
+
setDraft(function (prev) {
|
|
289
|
+
var next = Object.assign({}, prev);
|
|
290
|
+
MODE_KEYS.forEach(function (key, i) { next[key] = MODES[mode][i]; });
|
|
291
|
+
return next;
|
|
292
|
+
});
|
|
293
|
+
setNotice(null); setError(null);
|
|
294
|
+
}
|
|
229
295
|
|
|
230
296
|
function onSave() {
|
|
231
297
|
if (busy || !snapshot.writable) return;
|
|
@@ -233,6 +299,7 @@ window.__ModuleLoader__.load({
|
|
|
233
299
|
var ops = [];
|
|
234
300
|
for (var i = 0; i < FIELDS.length; i += 1) {
|
|
235
301
|
var f = FIELDS[i];
|
|
302
|
+
if (!Object.prototype.hasOwnProperty.call(draft, f.key)) continue;
|
|
236
303
|
var d = fieldDraft(f);
|
|
237
304
|
var current = getPath(value, f.path);
|
|
238
305
|
if (f.type === "password") {
|
|
@@ -250,7 +317,7 @@ window.__ModuleLoader__.load({
|
|
|
250
317
|
if (String(d).trim() === "" && getPath(user, f.path) === void 0) continue;
|
|
251
318
|
ops.push(String(d).trim() === "" ? { op: "unset", path: f.path } : { op: "set", path: f.path, value: f.type === "number" ? Number(d) : d });
|
|
252
319
|
}
|
|
253
|
-
if (ops.length === 0) { setBusy(false); setNotice(
|
|
320
|
+
if (ops.length === 0) { setBusy(false); setNotice({ key: "saved" }); return; }
|
|
254
321
|
commit(ops);
|
|
255
322
|
}
|
|
256
323
|
|
|
@@ -269,36 +336,38 @@ window.__ModuleLoader__.load({
|
|
|
269
336
|
var next = scope.getSnapshot();
|
|
270
337
|
setSnapshot(next);
|
|
271
338
|
setBusy(false);
|
|
272
|
-
if (!ok) { setError(
|
|
273
|
-
setNotice(
|
|
274
|
-
if (next.value) setDraft(
|
|
339
|
+
if (!ok) { setError({ key: "error", detailKey: "notApplied" }); return; }
|
|
340
|
+
setNotice({ key: "saved" });
|
|
341
|
+
if (next.value) setDraft({});
|
|
275
342
|
}).catch(function (e) {
|
|
276
|
-
setBusy(false); setError(
|
|
343
|
+
setBusy(false); setError({ key: "error", detail: String(e && e.message || e) });
|
|
277
344
|
});
|
|
278
345
|
}
|
|
279
346
|
|
|
280
|
-
var nodes = [];
|
|
281
|
-
var
|
|
347
|
+
var nodes = [], advancedNodes = [];
|
|
348
|
+
var lastGroups = { basic: null, advanced: null };
|
|
282
349
|
// forEach callback gives each handler its own `f` — a `for (var i)`
|
|
283
350
|
// loop would share one `f` across every onChange closure, so typing
|
|
284
351
|
// updated the LAST field's draft and the input appeared dead.
|
|
285
352
|
FIELDS.forEach(function (f) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
353
|
+
var section = BASIC_KEYS.indexOf(f.key) !== -1 ? "basic" : "advanced";
|
|
354
|
+
var target = section === "basic" ? nodes : advancedNodes;
|
|
355
|
+
if (f.group !== lastGroups[section]) {
|
|
356
|
+
lastGroups[section] = f.group;
|
|
357
|
+
target.push(h("div", { key: "g" + f.group, className: "__tm_group" }, t(f.group)));
|
|
289
358
|
}
|
|
290
359
|
var overridden = getPath(user, f.path) !== void 0;
|
|
291
360
|
if (f.type === "checkbox") {
|
|
292
|
-
|
|
361
|
+
target.push(h("label", { key: f.path.join("."), className: "__tm_field" },
|
|
293
362
|
h("span", { className: "__tm_row" },
|
|
294
|
-
h("input", { className: "__tm_check", type: "checkbox", checked: Boolean(fieldDraft(f)), onChange: function (e) { setField(f, e.target.checked); } }),
|
|
363
|
+
h("input", { className: "__tm_check", type: "checkbox", disabled: busy || !snapshot.writable, checked: Boolean(fieldDraft(f)), onChange: function (e) { setField(f, e.target.checked); } }),
|
|
295
364
|
h("span", { className: "__tm_label" }, t(f.label)),
|
|
296
365
|
overridden ? h("span", { className: "__tm_override" }, t("overridden")) : null
|
|
297
366
|
)
|
|
298
367
|
));
|
|
299
368
|
return;
|
|
300
369
|
}
|
|
301
|
-
|
|
370
|
+
target.push(h("label", { key: f.path.join("."), className: "__tm_field" },
|
|
302
371
|
h("span", { className: "__tm_label" },
|
|
303
372
|
t(f.label),
|
|
304
373
|
overridden ? h("span", { className: "__tm_override" }, t("overridden")) : null
|
|
@@ -307,6 +376,7 @@ window.__ModuleLoader__.load({
|
|
|
307
376
|
className: "__tm_input",
|
|
308
377
|
type: f.type === "password" ? "password" : f.type === "number" ? "number" : "text",
|
|
309
378
|
value: fieldDraft(f),
|
|
379
|
+
disabled: busy || !snapshot.writable,
|
|
310
380
|
placeholder: f.type === "password" ? (overridden ? "••••••••" : t("secretHint")) : "",
|
|
311
381
|
onChange: function (e) { setField(f, e.target.value); }
|
|
312
382
|
}),
|
|
@@ -314,29 +384,45 @@ window.__ModuleLoader__.load({
|
|
|
314
384
|
));
|
|
315
385
|
});
|
|
316
386
|
|
|
317
|
-
return h("div", { className: "__tm_root" },
|
|
387
|
+
return h("div", { className: "__tm_root dsh-flat" },
|
|
318
388
|
h("p", { className: "__tm_hint", style: { margin: "0 0 4px" } }, t("intro")),
|
|
389
|
+
h("label", { className: "__tm_field" },
|
|
390
|
+
h("span", { className: "__tm_label" }, t("mode")),
|
|
391
|
+
h("select", { className: "__tm_input", "aria-label": t("mode"), value: memoryMode(), disabled: busy || !snapshot.writable,
|
|
392
|
+
onChange: function (e) { setMode(e.target.value); } },
|
|
393
|
+
h("option", { value: "auto" }, t("modeAuto")), h("option", { value: "search" }, t("modeSearch")),
|
|
394
|
+
h("option", { value: "paused" }, t("modePaused")), h("option", { value: "custom", disabled: true }, t("modeCustom"))),
|
|
395
|
+
h("span", { className: "__tm_hint" }, t("modeHint"))),
|
|
396
|
+
h("p", { className: "__tm_hint" }, t("setupHint")),
|
|
319
397
|
nodes,
|
|
398
|
+
h("details", null, h("summary", { style: { cursor: "pointer" } }, t("advanced")),
|
|
399
|
+
h("div", { style: { display: "flex", flexDirection: "column", gap: 10, paddingTop: 12 } }, advancedNodes)),
|
|
320
400
|
h("div", { className: "__tm_actions" },
|
|
321
401
|
h("button", { type: "button", className: "__tm_btn __tm_btnPrimary", onClick: onSave, disabled: busy || !snapshot.writable }, t("save")),
|
|
322
402
|
h("button", { type: "button", className: "__tm_btn", onClick: onReset, disabled: busy || !snapshot.writable }, t("reset")),
|
|
323
|
-
notice ? h("span", { className: "__tm_status" }, notice) : null,
|
|
403
|
+
notice ? h("span", { className: "__tm_status" }, messageText(t, notice)) : null,
|
|
324
404
|
busy ? h("span", { className: "__tm_status" }, t("saving")) : null,
|
|
325
|
-
error ? h("span", { className: "__tm_error" }, error) : null
|
|
405
|
+
error ? h("span", { className: "__tm_error" }, messageText(t, error)) : null
|
|
326
406
|
)
|
|
327
407
|
);
|
|
328
408
|
}
|
|
329
409
|
|
|
330
|
-
function valueToDraft(value) {
|
|
331
|
-
var out = {};
|
|
332
|
-
for (var i = 0; i < FIELDS.length; i += 1) {
|
|
333
|
-
var f = FIELDS[i];
|
|
334
|
-
out[f.key] = f.secret ? "" : f.type === "checkbox" ? Boolean(getPath(value, f.path)) : String(getPath(value, f.path) ?? "");
|
|
335
|
-
}
|
|
336
|
-
return out;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
410
|
// ── plugin ────────────────────────────────────────────────────────────
|
|
411
|
+
|
|
412
|
+
// Follow the host language without remounting the form or losing drafts.
|
|
413
|
+
function useLocale(locale) {
|
|
414
|
+
var refresh = react.useState(0)[1];
|
|
415
|
+
react.useEffect(function () {
|
|
416
|
+
if (!locale || typeof locale.subscribe !== "function") return;
|
|
417
|
+
return locale.subscribe(function () { refresh(function (revision) { return revision + 1; }); });
|
|
418
|
+
}, [locale]);
|
|
419
|
+
}
|
|
420
|
+
// Keep translation keys in state so feedback follows later language changes.
|
|
421
|
+
function messageText(t, message) {
|
|
422
|
+
if (!message) return "";
|
|
423
|
+
return t(message.key) + (message.detailKey ? ": " + t(message.detailKey) : message.detail ? ": " + message.detail : "");
|
|
424
|
+
}
|
|
425
|
+
|
|
340
426
|
function apply(ctx) {
|
|
341
427
|
var t = ctx.locale.bind(NS);
|
|
342
428
|
ctx.effect(function () { return ctx.locale.register(NS, { zh: zh, en: en }); }, "dsh-tdai-memory: dictionaries");
|
|
@@ -347,7 +433,7 @@ window.__ModuleLoader__.load({
|
|
|
347
433
|
key: "dsh-tdai-memory",
|
|
348
434
|
locale: NS
|
|
349
435
|
}, function (props) {
|
|
350
|
-
return h(MemorySection, Object.assign({}, props, { scope: scope, t: t }));
|
|
436
|
+
return h(MemorySection, Object.assign({}, props, { scope: scope, t: t, locale: ctx.locale }));
|
|
351
437
|
});
|
|
352
438
|
});
|
|
353
439
|
}
|
package/index.js
CHANGED
|
@@ -24,6 +24,7 @@ import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
|
24
24
|
import { TdaiCore } from "./vendor/tdai/core/tdai-core.js";
|
|
25
25
|
import { parseConfig } from "./vendor/tdai/config.js";
|
|
26
26
|
import { StandaloneHostAdapter } from "./vendor/tdai/adapters/standalone/host-adapter.js";
|
|
27
|
+
import { createRecallCache, recallWithTimeout } from "./recall-runtime.js";
|
|
27
28
|
|
|
28
29
|
/** Cordis plugin name. */
|
|
29
30
|
const name = "tdai-memory";
|
|
@@ -145,8 +146,7 @@ function apply(ctx, config) {
|
|
|
145
146
|
/** session keys seen this process (for final L1 flush on teardown). */
|
|
146
147
|
const seenSessions = new Set();
|
|
147
148
|
/** recall cache per session: same user text within TTL reuses the result. */
|
|
148
|
-
const recallCache =
|
|
149
|
-
const RECALL_CACHE_TTL_MS = 30_000;
|
|
149
|
+
const recallCache = createRecallCache();
|
|
150
150
|
|
|
151
151
|
// One-shot mode (dsh --profile headless): the process exits right after
|
|
152
152
|
// flush, so L1 must finish inside the flush listener. In web mode the
|
|
@@ -231,6 +231,7 @@ function apply(ctx, config) {
|
|
|
231
231
|
// new core; initialize() only sets up stores/pipeline.
|
|
232
232
|
tdaiConfig = nextConfig;
|
|
233
233
|
core = nextCore;
|
|
234
|
+
recallCache.clear();
|
|
234
235
|
await nextCore.initialize();
|
|
235
236
|
const key = nextConfig.llm.apiKey || "";
|
|
236
237
|
log("info", `initialized (dataDir=${dataDir}, llm=${nextConfig.llm.model}, apiKey=${key ? `${key.slice(0, 6)}…` : "<empty>"}, embedding=${nextConfig.embedding.model})`);
|
|
@@ -321,16 +322,15 @@ function apply(ctx, config) {
|
|
|
321
322
|
if (!lastUser) return assembly;
|
|
322
323
|
const text = extractText(lastUser.content);
|
|
323
324
|
if (!text) return assembly;
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
return injectRecall(assembly, cached.result);
|
|
325
|
+
const cached = recallCache.get(session.id, text);
|
|
326
|
+
if (cached !== undefined) {
|
|
327
|
+
return injectRecall(assembly, cached);
|
|
328
328
|
}
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
recallCache.set(session.id,
|
|
329
|
+
const recallCore = core;
|
|
330
|
+
const result = await recallWithTimeout(
|
|
331
|
+
() => recallCore.handleBeforeRecall(text, session.id), tdaiConfig.recall.timeoutMs + 1000,
|
|
332
|
+
);
|
|
333
|
+
if (core === recallCore) recallCache.set(session.id, text, result);
|
|
334
334
|
return injectRecall(assembly, result ?? {});
|
|
335
335
|
} catch (error) {
|
|
336
336
|
ctx.logger.warn(`[tdai-memory] recall failed: ${String(error)}`);
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,97 +1,102 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-tdai-memory",
|
|
3
|
-
"version": "0.4.
|
|
4
|
-
"description": "DeepSeek Harness 的 TencentDB Agent Memory 移植:L0 对话捕获 → L1 结构化记忆提取 → L2 场景/L3 画像,自动召回注入 + 记忆/对话搜索工具;复用现有 ~/.memory-tencentdb/memory-tdai 数据;附 Web UI 设置栏。",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "index.js",
|
|
7
|
-
"exports": {
|
|
8
|
-
".": "./index.js",
|
|
9
|
-
"./client": "./client.js",
|
|
10
|
-
"./package.json": "./package.json"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
"0.1.5-rc.
|
|
24
|
-
"0.1.
|
|
25
|
-
"0.1.6-alpha.
|
|
26
|
-
"0.1.
|
|
27
|
-
"0.1.7-alpha.
|
|
28
|
-
"0.1.7-
|
|
29
|
-
"0.1.7-rc.
|
|
30
|
-
"0.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
"
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
"
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
"
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
},
|
|
94
|
-
"
|
|
95
|
-
"
|
|
96
|
-
|
|
97
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-tdai-memory",
|
|
3
|
+
"version": "0.4.3",
|
|
4
|
+
"description": "DeepSeek Harness 的 TencentDB Agent Memory 移植:L0 对话捕获 → L1 结构化记忆提取 → L2 场景/L3 画像,自动召回注入 + 记忆/对话搜索工具;复用现有 ~/.memory-tencentdb/memory-tdai 数据;附 Web UI 设置栏。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./client": "./client.js",
|
|
10
|
+
"./package.json": "./package.json",
|
|
11
|
+
"./locale/*.json": "./locale/*.json"
|
|
12
|
+
},
|
|
13
|
+
"dsh": {
|
|
14
|
+
"bundle": {
|
|
15
|
+
"patch": "./cordis.patch.yml"
|
|
16
|
+
},
|
|
17
|
+
"client": {
|
|
18
|
+
"platform": "web"
|
|
19
|
+
},
|
|
20
|
+
"compatibility": {
|
|
21
|
+
"dsh": ">=0.2.0-rc.2 <0.3.0 || 0.2.1-rc.1",
|
|
22
|
+
"dshReleases": {
|
|
23
|
+
"0.1.5-rc.2": "incompatible",
|
|
24
|
+
"0.1.5-rc.3": "incompatible",
|
|
25
|
+
"0.1.6-alpha.1": "unknown",
|
|
26
|
+
"0.1.6-alpha.2": "unknown",
|
|
27
|
+
"0.1.7-alpha.1": "unknown",
|
|
28
|
+
"0.1.7-alpha.2": "unknown",
|
|
29
|
+
"0.1.7-rc.1": "incompatible",
|
|
30
|
+
"0.1.7-rc.2": "incompatible",
|
|
31
|
+
"0.2.0-rc.2": "compatible",
|
|
32
|
+
"0.2.1-rc.1": "compatible"
|
|
33
|
+
},
|
|
34
|
+
"node": ">=22.18",
|
|
35
|
+
"profiles": [
|
|
36
|
+
"web",
|
|
37
|
+
"desktop"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/Scorp1o117/dsh-tdai-memory",
|
|
42
|
+
"files": [
|
|
43
|
+
"locale",
|
|
44
|
+
"index.js",
|
|
45
|
+
"recall-inject.js",
|
|
46
|
+
"recall-runtime.js",
|
|
47
|
+
"client.js",
|
|
48
|
+
"cordis.patch.yml",
|
|
49
|
+
"vendor",
|
|
50
|
+
"README.md",
|
|
51
|
+
"README.zh.md",
|
|
52
|
+
"LICENSE",
|
|
53
|
+
"THIRD-PARTY-NOTICES.md"
|
|
54
|
+
],
|
|
55
|
+
"keywords": [
|
|
56
|
+
"dsh",
|
|
57
|
+
"dsh-plugin",
|
|
58
|
+
"deepseek-harness",
|
|
59
|
+
"memory",
|
|
60
|
+
"tencentdb",
|
|
61
|
+
"agent-memory",
|
|
62
|
+
"rag",
|
|
63
|
+
"vector-search",
|
|
64
|
+
"persona"
|
|
65
|
+
],
|
|
66
|
+
"license": "MIT",
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": ">=22.18",
|
|
69
|
+
"dsh": ">=0.2.0-rc.2 <0.3.0 || 0.2.1-rc.1"
|
|
70
|
+
},
|
|
71
|
+
"repository": {
|
|
72
|
+
"type": "git",
|
|
73
|
+
"url": "git+https://github.com/Scorp1o117/dsh-tdai-memory.git"
|
|
74
|
+
},
|
|
75
|
+
"bugs": {
|
|
76
|
+
"url": "https://github.com/Scorp1o117/dsh-tdai-memory/issues"
|
|
77
|
+
},
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"@ai-sdk/openai": "^3.0.53",
|
|
80
|
+
"@deepseek-ai/schemastery": "^3.18.4",
|
|
81
|
+
"@node-rs/jieba": "^2.0.1",
|
|
82
|
+
"@tencentdb-agent-memory/tcvdb-text": "^0.1.1",
|
|
83
|
+
"ai": "^6.0.164",
|
|
84
|
+
"json5": "^2.2.3",
|
|
85
|
+
"sqlite-vec": "0.1.7-alpha.2",
|
|
86
|
+
"undici": "^8.1.0"
|
|
87
|
+
},
|
|
88
|
+
"peerDependencies": {
|
|
89
|
+
"react": "^18.2.0",
|
|
90
|
+
"@deepseek-ai/dsh-tools": ">=0.2.0-rc.2 <0.3.0 || 0.2.1-rc.1",
|
|
91
|
+
"@deepseek-ai/dsh-settings": ">=0.2.0-rc.2 <0.3.0 || 0.2.1-rc.1",
|
|
92
|
+
"node-llama-cpp": "^3.19.1"
|
|
93
|
+
},
|
|
94
|
+
"peerDependenciesMeta": {
|
|
95
|
+
"node-llama-cpp": {
|
|
96
|
+
"optional": true
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
"scripts": {
|
|
100
|
+
"test": "node tests/settings-scope-compat.test.mjs && node tests/install-contract.test.mjs && node --test tests/plugin-page.test.mjs tests/client-write-flow.test.mjs tests/tools-registration.test.mjs tests/recall-runtime.test.mjs && node --test tests/locale.test.mjs"
|
|
101
|
+
}
|
|
102
|
+
}
|
package/recall-inject.js
CHANGED
|
@@ -1,86 +1,88 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* tdai-recall-inject — agent-plane recall injection row for dsh-tdai-memory.
|
|
3
|
-
*
|
|
4
|
-
* Mounted INSIDE an agent preset (agent.cordis.yml), this row listens on the
|
|
5
|
-
* `system-prompt/assemble` waterfall in the preset's scope, so it runs for
|
|
6
|
-
* every assembly of every agent joined to that preset (agent -> preset scope
|
|
7
|
-
* chain). It asks the `tdaiMemory` service (provided by the root-level
|
|
8
|
-
* dsh-tdai-memory plugin) for relevant long-term memories and appends them
|
|
9
|
-
* as dynamic context sections (user-role snapshots):
|
|
10
|
-
*
|
|
11
|
-
* - tdai:recall — relevant L1 memories for the current user message
|
|
12
|
-
* - tdai:profile — stable persona / scene navigation context
|
|
13
|
-
*
|
|
14
|
-
* The recall call is bounded by `
|
|
15
|
-
* injection and never break the assembly.
|
|
16
|
-
*/
|
|
17
|
-
import z from "@deepseek-ai/schemastery";
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return assembly;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return content
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
.
|
|
75
|
-
.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (extras.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
1
|
+
/**
|
|
2
|
+
* tdai-recall-inject — agent-plane recall injection row for dsh-tdai-memory.
|
|
3
|
+
*
|
|
4
|
+
* Mounted INSIDE an agent preset (agent.cordis.yml), this row listens on the
|
|
5
|
+
* `system-prompt/assemble` waterfall in the preset's scope, so it runs for
|
|
6
|
+
* every assembly of every agent joined to that preset (agent -> preset scope
|
|
7
|
+
* chain). It asks the `tdaiMemory` service (provided by the root-level
|
|
8
|
+
* dsh-tdai-memory plugin) for relevant long-term memories and appends them
|
|
9
|
+
* as dynamic context sections (user-role snapshots):
|
|
10
|
+
*
|
|
11
|
+
* - tdai:recall — relevant L1 memories for the current user message
|
|
12
|
+
* - tdai:profile — stable persona / scene navigation context
|
|
13
|
+
*
|
|
14
|
+
* The recall call is bounded by this row's `timeoutMs`; failures degrade to no
|
|
15
|
+
* injection and never break the assembly.
|
|
16
|
+
*/
|
|
17
|
+
import z from "@deepseek-ai/schemastery";
|
|
18
|
+
import { createRecallCache, recallWithTimeout } from "./recall-runtime.js";
|
|
19
|
+
|
|
20
|
+
/** Cordis plugin name. */
|
|
21
|
+
const name = "tdai-recall-inject";
|
|
22
|
+
/** Services consumed: the prompt registry and the memory core service. */
|
|
23
|
+
const inject = ["systemPrompt", "tdaiMemory"];
|
|
24
|
+
|
|
25
|
+
/** Runtime schema for the recall-inject row. */
|
|
26
|
+
const Config = z.object({
|
|
27
|
+
/** Skip recall when the user message is shorter than this. */
|
|
28
|
+
minUserTextChars: z.number().default(1),
|
|
29
|
+
/** Per-session cache TTL for identical user text (ms). */
|
|
30
|
+
cacheTtlMs: z.number().default(30_000),
|
|
31
|
+
/** Assembly deadline even when the memory service does not settle. */
|
|
32
|
+
timeoutMs: z.number().default(4000),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
function apply(ctx, config) {
|
|
36
|
+
const cache = createRecallCache(config.cacheTtlMs ?? 30_000);
|
|
37
|
+
|
|
38
|
+
ctx.on("system-prompt/assemble", async (assembly, context) => {
|
|
39
|
+
try {
|
|
40
|
+
const agent = context?.agent;
|
|
41
|
+
if (!agent) return assembly;
|
|
42
|
+
const session = agent.session;
|
|
43
|
+
if (!session) return assembly;
|
|
44
|
+
// Only real human input triggers recall. DSH injects synthetic
|
|
45
|
+
// user-role context (system reminders, runtime context, skill
|
|
46
|
+
// catalogs, …) whose source.kind is not "user"; recalling on those
|
|
47
|
+
// would pollute the prompt with irrelevant memories (issue #1).
|
|
48
|
+
const lastUser = [...session.deriveMessages()]
|
|
49
|
+
.reverse()
|
|
50
|
+
.find((m) => m.role === "user" && (m.source == null || m.source.kind === "user"));
|
|
51
|
+
if (!lastUser) return assembly;
|
|
52
|
+
const text = extractText(lastUser.content);
|
|
53
|
+
if (!text || text.length < config.minUserTextChars) return assembly;
|
|
54
|
+
|
|
55
|
+
const cached = cache.get(session.id, text);
|
|
56
|
+
if (cached !== undefined) {
|
|
57
|
+
return injectRecall(assembly, cached);
|
|
58
|
+
}
|
|
59
|
+
const result = await recallWithTimeout(
|
|
60
|
+
() => ctx.tdaiMemory.handleBeforeRecall(text, session.id), config.timeoutMs,
|
|
61
|
+
);
|
|
62
|
+
cache.set(session.id, text, result);
|
|
63
|
+
return injectRecall(assembly, result ?? {});
|
|
64
|
+
} catch {
|
|
65
|
+
return assembly;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function extractText(content) {
|
|
71
|
+
if (typeof content === "string") return content.trim();
|
|
72
|
+
if (!Array.isArray(content)) return "";
|
|
73
|
+
return content
|
|
74
|
+
.filter((b) => b && b.type === "text" && typeof b.text === "string")
|
|
75
|
+
.map((b) => b.text)
|
|
76
|
+
.join("\n")
|
|
77
|
+
.trim();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function injectRecall(assembly, result) {
|
|
81
|
+
const extras = [];
|
|
82
|
+
if (result.prependContext) extras.push({ name: "tdai:recall", order: 1000, text: result.prependContext });
|
|
83
|
+
if (result.appendSystemContext) extras.push({ name: "tdai:profile", order: 1001, text: result.appendSystemContext });
|
|
84
|
+
if (extras.length === 0) return assembly;
|
|
85
|
+
return { ...assembly, contexts: [...assembly.contexts, ...extras] };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export { Config, apply, inject, name };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Bound recall latency without leaving a timer behind after a fast result.
|
|
2
|
+
* The underlying operation may continue; late failures remain handled. */
|
|
3
|
+
export async function recallWithTimeout(operation, timeoutMs = 4000) {
|
|
4
|
+
const delay = Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 4000;
|
|
5
|
+
let timer;
|
|
6
|
+
try {
|
|
7
|
+
return await Promise.race([
|
|
8
|
+
Promise.resolve().then(operation),
|
|
9
|
+
new Promise(resolve => { timer = setTimeout(() => resolve(null), delay); }),
|
|
10
|
+
]);
|
|
11
|
+
} catch {
|
|
12
|
+
return null;
|
|
13
|
+
} finally {
|
|
14
|
+
clearTimeout(timer);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Expiring, capacity-limited per-session cache for long-running hosts. */
|
|
19
|
+
export function createRecallCache(ttlMs = 30_000, capacity = 128, now = Date.now) {
|
|
20
|
+
const entries = new Map();
|
|
21
|
+
function prune() {
|
|
22
|
+
const time = now();
|
|
23
|
+
for (const [key, entry] of entries) {
|
|
24
|
+
if (time - entry.ts >= ttlMs) entries.delete(key);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
get(sessionId, text) {
|
|
29
|
+
prune();
|
|
30
|
+
const entry = entries.get(sessionId);
|
|
31
|
+
return entry?.text === text ? entry.result : undefined;
|
|
32
|
+
},
|
|
33
|
+
set(sessionId, text, result) {
|
|
34
|
+
prune();
|
|
35
|
+
if (ttlMs <= 0 || capacity <= 0 || result == null) return;
|
|
36
|
+
entries.delete(sessionId);
|
|
37
|
+
entries.set(sessionId, { text, result, ts: now() });
|
|
38
|
+
while (entries.size > capacity) entries.delete(entries.keys().next().value);
|
|
39
|
+
},
|
|
40
|
+
clear() { entries.clear(); },
|
|
41
|
+
};
|
|
42
|
+
}
|