@restregular/dsh-memory 0.4.1 → 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.
@@ -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
- import { defineTool } from '@deepseek-ai/dsh-tools'
12
- import {
13
- memoryTools,
14
-
15
-
16
-
17
- } from './index.js'
18
-
19
- export const name = 'dsh-tool-memory'
20
- export const inject = ['tools', 'memory']
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
- export function apply(ctx , config = {}) {
30
- const memory = ctx.memory
31
- const projectScope = (config.projectScope ?? `project:${process.cwd()}`)
32
- const opts = {
33
- getService: () => memory,
34
- defaultScope: () => (config.defaultScope === 'global' ? 'global' : projectScope),
35
- }
36
- const tools = memoryTools(opts)
37
-
38
- ctx.tools.register(defineTool({
39
- name: tools.memory_save.name,
40
- description: tools.memory_save.description,
41
- parameters: tools.memory_save.parameters ,
42
- output: {
43
- schema: { type: 'json' },
44
- render: (_args , value ) => [{
45
- type: 'text',
46
- text: value.deduped
47
- ? `已更新既有记忆(内容相同,去重):${value.id} @ ${value.scope}`
48
- : `已保存记忆 ${value.id} @ ${value.scope}`,
49
- }],
50
- },
51
- execute: (args ) => tools.memory_save.execute(args),
52
- }))
53
-
54
- ctx.tools.register(defineTool({
55
- name: tools.memory_search.name,
56
- description: tools.memory_search.description,
57
- parameters: tools.memory_search.parameters ,
58
- output: {
59
- schema: { type: 'json' },
60
- render: (_args , value ) => [{
61
- type: 'text',
62
- text: value.records.length === 0
63
- ? '没有找到相关记忆。'
64
- : value.records
65
- .map((h) => `[${h.score.toFixed(3)}] (${h.scope}/${h.kind}) ${h.content}`)
66
- .join('\n'),
67
- }],
68
- },
69
- execute: (args ) => tools.memory_search.execute(args),
70
- }))
71
-
72
- ctx.tools.register(defineTool({
73
- name: tools.memory_forget.name,
74
- description: tools.memory_forget.description,
75
- parameters: tools.memory_forget.parameters ,
76
- output: {
77
- schema: { type: 'json' },
78
- render: (_args , value ) => [{
79
- type: 'text',
80
- text: value.removed === 0 ? '没有匹配的记忆。' : `已删除 ${value.removed} 条记忆。`,
81
- }],
82
- },
83
- execute: (args ) => tools.memory_forget.execute(args),
84
- }))
85
-
86
- ctx.tools.register(defineTool({
87
- name: tools.memory_list.name,
88
- description: tools.memory_list.description,
89
- parameters: tools.memory_list.parameters ,
90
- output: {
91
- schema: { type: 'json' },
92
- render: (_args , value ) => [{
93
- type: 'text',
94
- text: value.records.length === 0
95
- ? '该作用域还没有记忆。'
96
- : value.records
97
- .map((r) => `(${r.scope}/${r.kind}) ${r.content}`)
98
- .join('\n'),
99
- }],
100
- },
101
- execute: (args ) => tools.memory_list.execute(args),
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