@restregular/dsh-memory 0.3.0 → 0.4.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,106 @@
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'
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
+ }
18
104
 
19
- export const name = 'dsh-tool-memory'
20
- export const inject = ['tools', 'memory']
21
105
 
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
- }
104
-
105
-
106
- //# sourceURL=D:\.deepseek\memory-plugin\packages\tool-memory\src\apply.ts
106
+ //# sourceURL=source/tool-memory/src/apply.ts
@@ -1,224 +1,224 @@
1
- /**
2
- * Tool implementations as plain data + functions. Keeping them free of both
3
- * cordis and dsh-tools imports lets us unit-test the exact argument/response
4
- * contracts standalone; the DSH entry (apply.ts) wraps them with defineTool.
5
- */
6
-
7
-
8
- /** Shape each tool shares: JSON-schema parameters + canonical output value. */
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
- export class MemoryToolError extends Error {
24
- code
25
-
26
- constructor(code , message ) {
27
- super(message)
28
- this.code = code
29
- }
30
- }
31
-
32
- function svc(opts ) {
33
- const s = opts.getService()
34
- if (!s) throw new MemoryToolError('SERVICE_UNAVAILABLE', 'memory service is not loaded; check that dsh-memory-local is enabled')
35
- return s
36
- }
37
-
38
- function scopeOr(opts , scope ) {
39
- if (scope === 'global') return 'global'
40
- if (typeof scope === 'string' && scope.startsWith('project:')) return scope
41
- if (scope === 'project') return opts.defaultScope()
42
- return opts.defaultScope()
43
- }
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
- export function memorySaveTool(opts ) {
61
- return {
62
- name: 'memory_save',
63
- description: 'Save a durable memory (fact, decision, preference, procedure) for future sessions.',
64
- parameters: {
65
- content: { type: 'string', description: 'Self-contained declarative sentence, readable without context', required: true },
66
- scope: { type: 'string', enum: ['global', 'project'], description: 'global = all projects; project = current workspace' },
67
- kind: { type: 'string', enum: ['fact', 'decision', 'preference', 'procedure', 'note'] },
68
- tags: { type: 'array', items: { type: 'string' } },
69
- importance: { type: 'number', description: '0~1, higher = more important' },
70
- supersedes: { type: 'string', description: 'Record id this corrects/replaces' },
71
- },
72
- async execute(args ) {
73
- if (typeof args.content !== 'string' || args.content.trim().length === 0) {
74
- throw new MemoryToolError('CONTENT_EMPTY', 'content must be a non-empty string')
75
- }
76
- if (args.importance !== undefined && (args.importance < 0 || args.importance > 1)) {
77
- throw new MemoryToolError('IMPORTANCE_RANGE', 'importance must be between 0 and 1')
78
- }
79
- const record = await svc(opts).save({
80
- scope: scopeOr(opts, args.scope),
81
- kind: args.kind ?? 'note',
82
- content: args.content.trim(),
83
- ...(args.tags ? { tags: args.tags } : {}),
84
- ...(args.importance !== undefined ? { importance: args.importance } : {}),
85
- ...(args.supersedes ? { supersedes: args.supersedes } : {}),
86
- source: { origin: 'explicit' },
87
- })
88
- return { id: record.id, scope: record.scope, deduped: false }
89
- },
90
- }
91
- }
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
- export function memorySearchTool(opts ) {
113
- return {
114
- name: 'memory_search',
115
- description: 'Search saved memories by keyword or meaning (lexical + local vector recall).',
116
- parameters: {
117
- query: { type: 'string', required: true },
118
- scope: { type: 'string', enum: ['global', 'project'] },
119
- topK: { type: 'number', description: '1~20' },
120
- },
121
- async execute(args ) {
122
- if (typeof args.query !== 'string' || args.query.trim().length === 0) {
123
- throw new MemoryToolError('QUERY_EMPTY', 'query must be a non-empty string')
124
- }
125
- const hits = await svc(opts).search({
126
- text: args.query,
127
- ...(args.scope ? { scope: scopeOr(opts, args.scope) } : {}),
128
- ...(args.topK !== undefined ? { topK: args.topK } : {}),
129
- })
130
- return {
131
- records: hits.map((h) => ({
132
- id: h.record.id,
133
- score: h.score,
134
- scope: h.record.scope,
135
- kind: h.record.kind,
136
- updatedAt: h.record.updatedAt,
137
- content: h.record.content,
138
- })),
139
- }
140
- },
141
- }
142
- }
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
- export function memoryForgetTool(opts ) {
156
- return {
157
- name: 'memory_forget',
158
- description: 'Delete a memory by id, or all memories matching a query in one scope.',
159
- parameters: {
160
- id: { type: 'string', description: 'Exact record id' },
161
- query: { type: 'string', description: 'Substring match within one scope' },
162
- scope: { type: 'string', enum: ['global', 'project'] },
163
- },
164
- async execute(args ) {
165
- if (!args.id && !args.query) throw new MemoryToolError('TARGET_REQUIRED', 'provide id or query')
166
- if (args.id && args.query) throw new MemoryToolError('TARGET_AMBIGUOUS', 'provide id or query, not both')
167
- return svc(opts).forget({
168
- ...(args.id ? { id: args.id } : {}),
169
- ...(args.query ? { query: args.query } : {}),
170
- ...(args.scope ? { scope: scopeOr(opts, args.scope) } : {}),
171
- })
172
- },
173
- }
174
- }
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
- export function memoryListTool(opts ) {
187
- return {
188
- name: 'memory_list',
189
- description: 'List saved memories, most recently updated first.',
190
- parameters: {
191
- scope: { type: 'string', enum: ['global', 'project'] },
192
- kind: { type: 'string' },
193
- limit: { type: 'number', description: '1~200' },
194
- },
195
- async execute(args ) {
196
- const records = await svc(opts).list({
197
- ...(args.scope ? { scope: scopeOr(opts, args.scope) } : {}),
198
- ...(args.kind ? { kind: args.kind } : {}),
199
- ...(args.limit !== undefined ? { limit: args.limit } : {}),
200
- })
201
- return {
202
- records: records.map((r) => ({
203
- id: r.id,
204
- scope: r.scope,
205
- kind: r.kind,
206
- updatedAt: r.updatedAt,
207
- content: r.content,
208
- })),
209
- }
210
- },
211
- }
212
- }
213
-
214
- export function memoryTools(opts ) {
215
- return {
216
- memory_save: memorySaveTool(opts),
217
- memory_search: memorySearchTool(opts),
218
- memory_forget: memoryForgetTool(opts),
219
- memory_list: memoryListTool(opts),
220
- }
221
- }
222
-
223
-
224
- //# sourceURL=D:\.deepseek\memory-plugin\packages\tool-memory\src\index.ts
1
+ /**
2
+ * Tool implementations as plain data + functions. Keeping them free of both
3
+ * cordis and dsh-tools imports lets us unit-test the exact argument/response
4
+ * contracts standalone; the DSH entry (apply.ts) wraps them with defineTool.
5
+ */
6
+
7
+
8
+ /** Shape each tool shares: JSON-schema parameters + canonical output value. */
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+ export class MemoryToolError extends Error {
24
+ code
25
+
26
+ constructor(code , message ) {
27
+ super(message)
28
+ this.code = code
29
+ }
30
+ }
31
+
32
+ function svc(opts ) {
33
+ const s = opts.getService()
34
+ if (!s) throw new MemoryToolError('SERVICE_UNAVAILABLE', 'memory service is not loaded; check that dsh-memory-local is enabled')
35
+ return s
36
+ }
37
+
38
+ function scopeOr(opts , scope ) {
39
+ if (scope === 'global') return 'global'
40
+ if (typeof scope === 'string' && scope.startsWith('project:')) return scope
41
+ if (scope === 'project') return opts.defaultScope()
42
+ return opts.defaultScope()
43
+ }
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+ export function memorySaveTool(opts ) {
61
+ return {
62
+ name: 'memory_save',
63
+ description: 'Save a durable memory (fact, decision, preference, procedure) for future sessions.',
64
+ parameters: {
65
+ content: { type: 'string', description: 'Self-contained declarative sentence, readable without context', required: true },
66
+ scope: { type: 'string', enum: ['global', 'project'], description: 'global = all projects; project = current workspace' },
67
+ kind: { type: 'string', enum: ['fact', 'decision', 'preference', 'procedure', 'note'] },
68
+ tags: { type: 'array', items: { type: 'string' } },
69
+ importance: { type: 'number', description: '0~1, higher = more important' },
70
+ supersedes: { type: 'string', description: 'Record id this corrects/replaces' },
71
+ },
72
+ async execute(args ) {
73
+ if (typeof args.content !== 'string' || args.content.trim().length === 0) {
74
+ throw new MemoryToolError('CONTENT_EMPTY', 'content must be a non-empty string')
75
+ }
76
+ if (args.importance !== undefined && (args.importance < 0 || args.importance > 1)) {
77
+ throw new MemoryToolError('IMPORTANCE_RANGE', 'importance must be between 0 and 1')
78
+ }
79
+ const record = await svc(opts).save({
80
+ scope: scopeOr(opts, args.scope),
81
+ kind: args.kind ?? 'note',
82
+ content: args.content.trim(),
83
+ ...(args.tags ? { tags: args.tags } : {}),
84
+ ...(args.importance !== undefined ? { importance: args.importance } : {}),
85
+ ...(args.supersedes ? { supersedes: args.supersedes } : {}),
86
+ source: { origin: 'explicit' },
87
+ })
88
+ return { id: record.id, scope: record.scope, deduped: false }
89
+ },
90
+ }
91
+ }
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+ export function memorySearchTool(opts ) {
113
+ return {
114
+ name: 'memory_search',
115
+ description: 'Search saved memories by keyword or meaning (lexical + local vector recall).',
116
+ parameters: {
117
+ query: { type: 'string', required: true },
118
+ scope: { type: 'string', enum: ['global', 'project'] },
119
+ topK: { type: 'number', description: '1~20' },
120
+ },
121
+ async execute(args ) {
122
+ if (typeof args.query !== 'string' || args.query.trim().length === 0) {
123
+ throw new MemoryToolError('QUERY_EMPTY', 'query must be a non-empty string')
124
+ }
125
+ const hits = await svc(opts).search({
126
+ text: args.query,
127
+ ...(args.scope ? { scope: scopeOr(opts, args.scope) } : {}),
128
+ ...(args.topK !== undefined ? { topK: args.topK } : {}),
129
+ })
130
+ return {
131
+ records: hits.map((h) => ({
132
+ id: h.record.id,
133
+ score: h.score,
134
+ scope: h.record.scope,
135
+ kind: h.record.kind,
136
+ updatedAt: h.record.updatedAt,
137
+ content: h.record.content,
138
+ })),
139
+ }
140
+ },
141
+ }
142
+ }
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+ export function memoryForgetTool(opts ) {
156
+ return {
157
+ name: 'memory_forget',
158
+ description: 'Delete a memory by id, or all memories matching a query in one scope.',
159
+ parameters: {
160
+ id: { type: 'string', description: 'Exact record id' },
161
+ query: { type: 'string', description: 'Substring match within one scope' },
162
+ scope: { type: 'string', enum: ['global', 'project'] },
163
+ },
164
+ async execute(args ) {
165
+ if (!args.id && !args.query) throw new MemoryToolError('TARGET_REQUIRED', 'provide id or query')
166
+ if (args.id && args.query) throw new MemoryToolError('TARGET_AMBIGUOUS', 'provide id or query, not both')
167
+ return svc(opts).forget({
168
+ ...(args.id ? { id: args.id } : {}),
169
+ ...(args.query ? { query: args.query } : {}),
170
+ ...(args.scope ? { scope: scopeOr(opts, args.scope) } : {}),
171
+ })
172
+ },
173
+ }
174
+ }
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+ export function memoryListTool(opts ) {
187
+ return {
188
+ name: 'memory_list',
189
+ description: 'List saved memories, most recently updated first.',
190
+ parameters: {
191
+ scope: { type: 'string', enum: ['global', 'project'] },
192
+ kind: { type: 'string' },
193
+ limit: { type: 'number', description: '1~200' },
194
+ },
195
+ async execute(args ) {
196
+ const records = await svc(opts).list({
197
+ ...(args.scope ? { scope: scopeOr(opts, args.scope) } : {}),
198
+ ...(args.kind ? { kind: args.kind } : {}),
199
+ ...(args.limit !== undefined ? { limit: args.limit } : {}),
200
+ })
201
+ return {
202
+ records: records.map((r) => ({
203
+ id: r.id,
204
+ scope: r.scope,
205
+ kind: r.kind,
206
+ updatedAt: r.updatedAt,
207
+ content: r.content,
208
+ })),
209
+ }
210
+ },
211
+ }
212
+ }
213
+
214
+ export function memoryTools(opts ) {
215
+ return {
216
+ memory_save: memorySaveTool(opts),
217
+ memory_search: memorySearchTool(opts),
218
+ memory_forget: memoryForgetTool(opts),
219
+ memory_list: memoryListTool(opts),
220
+ }
221
+ }
222
+
223
+
224
+ //# sourceURL=source/tool-memory/src/index.ts