@restregular/dsh-memory 0.4.0 → 0.5.0
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 +231 -36
- package/lib/client.js +19 -7
- package/lib/inject/apply.js +27 -8
- package/lib/inject/inject.js +75 -3
- package/lib/panel.js +106 -12
- package/lib/provider/apply.js +40 -1
- package/lib/provider/index.js +270 -31
- package/lib/provider/scope.js +338 -0
- package/lib/provider/store.js +56 -9
- package/lib/tools/apply.js +123 -103
- package/lib/tools/index.js +370 -221
- package/package.json +1 -1
package/lib/tools/apply.js
CHANGED
|
@@ -1,106 +1,126 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cordis wiring for dsh-tool-memory. Loaded only inside DSH: wraps the pure
|
|
3
|
-
* tool specs with the official defineTool DSL and registers them.
|
|
4
|
-
*
|
|
5
|
-
* inject: ['tools', 'memory'] is REQUIRED:
|
|
6
|
-
* - 'tools' → the tool registry must be ready
|
|
7
|
-
* - 'memory' → this fiber stays PENDING until the provider provides the
|
|
8
|
-
* service, so `ctx.memory` is guaranteed active when apply runs (accessing
|
|
9
|
-
* it without declaring made apply throw and the entry silently die)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Cordis wiring for dsh-tool-memory. Loaded only inside DSH: wraps the pure
|
|
3
|
+
* tool specs with the official defineTool DSL and registers them.
|
|
4
|
+
*
|
|
5
|
+
* inject: ['tools', 'memory'] is REQUIRED:
|
|
6
|
+
* - 'tools' → the tool registry must be ready
|
|
7
|
+
* - 'memory' → this fiber stays PENDING until the provider provides the
|
|
8
|
+
* service, so `ctx.memory` is guaranteed active when apply runs (accessing
|
|
9
|
+
* it without declaring made apply throw and the entry silently die)
|
|
10
|
+
*
|
|
11
|
+
* 0.5.0 起 scope 的默认值是**当前会话**:handler 必须把宿主的 `exec` 透传给工具实现
|
|
12
|
+
* (会话 id / cwd 全在 `exec.agent.session.header` 上),这里同时负责把 scope 键渲染成
|
|
13
|
+
* 人看得懂的标签(会话标题只有这一层能拿到)。
|
|
14
|
+
*/
|
|
15
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
16
|
+
import {
|
|
17
|
+
memoryTools,
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
} from './index.js'
|
|
22
|
+
import { resolveCurrentScopes, scopeKind, } from '../provider/scope.js'
|
|
23
|
+
|
|
24
|
+
export const name = 'dsh-tool-memory'
|
|
25
|
+
export const inject = ['tools', 'memory']
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
export function apply(ctx , config = {}) {
|
|
35
|
+
const memory = ctx.memory
|
|
36
|
+
/** 调用时的项目键:显式配置 > 会话 cwd > 进程 cwd。 */
|
|
37
|
+
const projectScopeFor = (exec ) => {
|
|
38
|
+
if (config.projectScope !== undefined) return config.projectScope
|
|
39
|
+
return resolveCurrentScopes((exec )?.agent).project
|
|
40
|
+
}
|
|
41
|
+
const opts = {
|
|
42
|
+
getService: () => memory,
|
|
43
|
+
defaultScope: config.defaultScope ?? 'session',
|
|
44
|
+
projectScope: config.projectScope,
|
|
45
|
+
}
|
|
46
|
+
const tools = memoryTools(opts)
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* scope 键 → 展示标签。会话标题只有宿主服务能给:`ctx.get('sessionTitle')` 的 `get(session)`
|
|
50
|
+
* 返回 `{ title }`(见 @deepseek-ai/dsh-session-title)。整套访问都在 try/catch 里 ——
|
|
51
|
+
* 旧宿主没有这个服务、会话已经不在内存里,都不能影响工具结果,回退成"会话 <id 前 8 位>"。
|
|
52
|
+
*/
|
|
53
|
+
const labelFor = (scope , agent ) => {
|
|
54
|
+
if (scope === 'global') return '全局'
|
|
55
|
+
if (scope.startsWith('project:')) return scope.slice('project:'.length)
|
|
56
|
+
if (scope.startsWith('session:')) {
|
|
57
|
+
const id = scope.slice('session:'.length)
|
|
58
|
+
const short = id.slice(0, 8)
|
|
59
|
+
try {
|
|
60
|
+
const session = (agent )?.session
|
|
61
|
+
if (session?.id === id) {
|
|
62
|
+
const titles = ctx.get?.('sessionTitle')
|
|
63
|
+
const snapshot = titles?.get?.(session)
|
|
64
|
+
const title = snapshot?.title
|
|
65
|
+
if (typeof title === 'string' && title.length > 0) return `${title}(${short})`
|
|
66
|
+
}
|
|
67
|
+
} catch { /* 拿不到标题就用短 id */ }
|
|
68
|
+
return `会话 ${short}`
|
|
69
|
+
}
|
|
70
|
+
return scope
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const renderScope = (scope , agent ) =>
|
|
74
|
+
scopeKind(scope) === 'custom' ? `自定义「${scope}」` : labelFor(scope, agent)
|
|
75
|
+
|
|
76
|
+
/** 作用域清单文案(名字 + 条数);最多 20 个,其余写"…另有 N 个"。 */
|
|
77
|
+
const scopeSummary = (value , agent ) => {
|
|
78
|
+
const stats = value.scopeStats ?? []
|
|
79
|
+
if (stats.length === 0) return ''
|
|
80
|
+
const lines = stats.slice(0, 20).map((s) => `${renderScope(s.scope, agent)}(${s.records})`)
|
|
81
|
+
const rest = stats.length - lines.length
|
|
82
|
+
return `作用域:${lines.join('、')}${rest > 0 ? `,…另有 ${rest} 个` : ''}`
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const register = (spec , render ) => {
|
|
86
|
+
ctx.tools.register(defineTool({
|
|
87
|
+
name: spec.name,
|
|
88
|
+
description: spec.description,
|
|
89
|
+
parameters: spec.parameters ,
|
|
90
|
+
output: {
|
|
91
|
+
schema: { type: 'json' },
|
|
92
|
+
render: (_args , value , exec ) => [{ type: 'text', text: render(value, _args , exec) }],
|
|
93
|
+
},
|
|
94
|
+
execute: (args , exec ) => spec.execute(args, exec),
|
|
95
|
+
}))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
register(tools.memory_save, (value, _args, exec) => {
|
|
99
|
+
const agent = (exec )?.agent
|
|
100
|
+
return value.deduped
|
|
101
|
+
? `已更新既有记忆(内容相同,去重):${value.id} @ ${renderScope(value.scope, agent)}`
|
|
102
|
+
: `已保存记忆 ${value.id} @ ${renderScope(value.scope, agent)}`
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
register(tools.memory_search, (value, _args, exec) => {
|
|
106
|
+
const agent = (exec )?.agent
|
|
107
|
+
if (value.records.length === 0) return '没有找到相关记忆。'
|
|
108
|
+
return value.records
|
|
109
|
+
.map((h) => `[${h.score.toFixed(3)}] (${renderScope(h.scope, agent)}/${h.kind}) ${h.content}`)
|
|
110
|
+
.join('\n')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
register(tools.memory_forget, (value) => (value.removed === 0 ? '没有匹配的记忆。' : `已删除 ${value.removed} 条记忆。`))
|
|
114
|
+
|
|
115
|
+
register(tools.memory_list, (value, _args, exec) => {
|
|
116
|
+
const agent = (exec )?.agent
|
|
117
|
+
const body = value.records.length === 0
|
|
118
|
+
? '该作用域还没有记忆。'
|
|
119
|
+
: value.records.map((r) => `(${renderScope(r.scope, agent)}/${r.kind}) ${r.content}`).join('\n')
|
|
120
|
+
const summary = scopeSummary(value, agent)
|
|
121
|
+
return summary ? `${body}\n${summary}` : body
|
|
122
|
+
})
|
|
123
|
+
}
|
|
104
124
|
|
|
105
125
|
|
|
106
126
|
//# sourceURL=source/tool-memory/src/apply.ts
|