@lmzhen/dsh-tool-memory 0.1.0-rc.1
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 +24 -0
- package/lib/index.js +161 -0
- package/lib/invariant.js +8 -0
- package/lib/types/index.d.ts +16 -0
- package/lib/types/invariant.d.ts +5 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @deepseek-ai/dsh-tool-memory
|
|
2
|
+
|
|
3
|
+
Model-facing memory tool and prompt context
|
|
4
|
+
|
|
5
|
+
## Model Experience
|
|
6
|
+
|
|
7
|
+
### Memory tool and runtime snapshot
|
|
8
|
+
|
|
9
|
+
#### What the model sees
|
|
10
|
+
|
|
11
|
+
The model sees the `memory` tool schema, one fixed guidance section, and a runtime snapshot containing the current memory entries.
|
|
12
|
+
|
|
13
|
+
#### Token effect
|
|
14
|
+
|
|
15
|
+
Fixed guidance has a constant token cost. The runtime snapshot scales with stored memory entries and is absent when memory is empty.
|
|
16
|
+
|
|
17
|
+
#### KV Cache effect
|
|
18
|
+
|
|
19
|
+
Guidance text is prefix-stable. The runtime snapshot is replaced after successful memory writes and is otherwise unchanged between requests.
|
|
20
|
+
|
|
21
|
+
## Known Limitations and Deferred Work
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
- No known durable consumer gaps at this time. Runtime contracts are covered by package and boundary tests.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
3
|
+
//#region lib/types/index.js
|
|
4
|
+
/**
|
|
5
|
+
* Model-facing memory tool and runtime-context memory snapshot.
|
|
6
|
+
* @module @lmzhen/dsh-tool-memory
|
|
7
|
+
*/
|
|
8
|
+
const name = "tool-memory";
|
|
9
|
+
const inject = [
|
|
10
|
+
"tools",
|
|
11
|
+
"systemPrompt",
|
|
12
|
+
"memory"
|
|
13
|
+
];
|
|
14
|
+
const Config = z.object({
|
|
15
|
+
memoryEnabled: z.boolean().default(true),
|
|
16
|
+
entryPreviewChars: z.number().default(200)
|
|
17
|
+
});
|
|
18
|
+
async function apply(ctx, rawConfig) {
|
|
19
|
+
if (!rawConfig.memoryEnabled) return;
|
|
20
|
+
let snapshotText = await ctx.memory.renderContext();
|
|
21
|
+
ctx.systemPrompt.section({
|
|
22
|
+
name: "evolution:memory-guidance",
|
|
23
|
+
order: 150,
|
|
24
|
+
text: "You have durable memory. Save stable user preferences and environment facts with the `memory` tool. Prefer one atomic `operations` batch."
|
|
25
|
+
});
|
|
26
|
+
ctx.systemPrompt.context({
|
|
27
|
+
name: "evolution:memory-snapshot",
|
|
28
|
+
order: 150,
|
|
29
|
+
text: () => snapshotText
|
|
30
|
+
});
|
|
31
|
+
async function executeCore(normalized) {
|
|
32
|
+
const result = normalized.operations ? await ctx.memory.applyBatch(normalized.target, normalized.operations) : await ctx.memory.applyBatch(normalized.target, [{
|
|
33
|
+
action: normalized.action ?? "add",
|
|
34
|
+
facts: normalized.facts,
|
|
35
|
+
old_text: normalized.old_text
|
|
36
|
+
}]);
|
|
37
|
+
if (result.ok) snapshotText = await ctx.memory.renderContext();
|
|
38
|
+
return {
|
|
39
|
+
ok: result.ok,
|
|
40
|
+
message: result.message,
|
|
41
|
+
entries: result.entries.map((entry) => entry.slice(0, rawConfig.entryPreviewChars ?? 200)),
|
|
42
|
+
chars: result.chars,
|
|
43
|
+
limit: result.limit
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
ctx.tools.register(defineTool({
|
|
47
|
+
name: "memory",
|
|
48
|
+
description: "Save durable facts to persistent memory. target \"user\" = who the user is; target \"memory\" = your notes. Use operations for atomic add/replace/remove. Save proactively; do not save task progress or one-off narratives.",
|
|
49
|
+
parameters: {
|
|
50
|
+
target: {
|
|
51
|
+
type: "string",
|
|
52
|
+
enum: ["memory", "user"],
|
|
53
|
+
required: true
|
|
54
|
+
},
|
|
55
|
+
action: {
|
|
56
|
+
type: "string",
|
|
57
|
+
enum: [
|
|
58
|
+
"add",
|
|
59
|
+
"replace",
|
|
60
|
+
"remove"
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
facts: { type: "string" },
|
|
64
|
+
content: { type: "string" },
|
|
65
|
+
old_text: { type: "string" },
|
|
66
|
+
operations: {
|
|
67
|
+
type: "array",
|
|
68
|
+
items: {
|
|
69
|
+
type: "object",
|
|
70
|
+
additionalProperties: false,
|
|
71
|
+
properties: {
|
|
72
|
+
action: {
|
|
73
|
+
type: "string",
|
|
74
|
+
enum: [
|
|
75
|
+
"add",
|
|
76
|
+
"replace",
|
|
77
|
+
"remove"
|
|
78
|
+
],
|
|
79
|
+
required: true
|
|
80
|
+
},
|
|
81
|
+
facts: { type: "string" },
|
|
82
|
+
content: { type: "string" },
|
|
83
|
+
old_text: { type: "string" }
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
output: {
|
|
89
|
+
schema: {
|
|
90
|
+
type: "object",
|
|
91
|
+
additionalProperties: false,
|
|
92
|
+
properties: {
|
|
93
|
+
ok: {
|
|
94
|
+
type: "boolean",
|
|
95
|
+
required: true
|
|
96
|
+
},
|
|
97
|
+
message: {
|
|
98
|
+
type: "string",
|
|
99
|
+
required: true
|
|
100
|
+
},
|
|
101
|
+
entries: {
|
|
102
|
+
type: "array",
|
|
103
|
+
required: true,
|
|
104
|
+
items: { type: "string" }
|
|
105
|
+
},
|
|
106
|
+
chars: {
|
|
107
|
+
type: "integer",
|
|
108
|
+
required: true
|
|
109
|
+
},
|
|
110
|
+
limit: {
|
|
111
|
+
type: "integer",
|
|
112
|
+
required: true
|
|
113
|
+
},
|
|
114
|
+
pending_id: { type: "string" }
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
render: (_args, value) => [{
|
|
118
|
+
type: "text",
|
|
119
|
+
text: `${value.ok ? "OK" : "Error"}: ${value.message} (${value.chars}/${value.limit} chars)`
|
|
120
|
+
}]
|
|
121
|
+
},
|
|
122
|
+
isConcurrencySafe: () => false,
|
|
123
|
+
async execute(args, exec) {
|
|
124
|
+
const target = args.target === "user" ? "user" : "memory";
|
|
125
|
+
const normalized = Array.isArray(args.operations) ? {
|
|
126
|
+
target,
|
|
127
|
+
operations: args.operations
|
|
128
|
+
} : {
|
|
129
|
+
target,
|
|
130
|
+
action: args.action ?? "add",
|
|
131
|
+
facts: args.facts ?? args.content,
|
|
132
|
+
old_text: args.old_text
|
|
133
|
+
};
|
|
134
|
+
const origin = exec.agent?.session.header.origin === "subagent" ? "background_review" : "foreground";
|
|
135
|
+
const approval = ctx.get("evolutionApproval");
|
|
136
|
+
if (approval) {
|
|
137
|
+
const decision = await approval.request({
|
|
138
|
+
kind: "memory",
|
|
139
|
+
summary: `memory ${target} ${Array.isArray(args.operations) ? `${args.operations.length} ops` : args.action ?? "add"}`,
|
|
140
|
+
args: normalized,
|
|
141
|
+
origin
|
|
142
|
+
});
|
|
143
|
+
if (decision.action === "staged") return {
|
|
144
|
+
ok: true,
|
|
145
|
+
message: decision.message,
|
|
146
|
+
entries: [],
|
|
147
|
+
chars: 0,
|
|
148
|
+
limit: 0,
|
|
149
|
+
pending_id: decision.pendingId ?? ""
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
return await executeCore(normalized);
|
|
153
|
+
}
|
|
154
|
+
}));
|
|
155
|
+
ctx.inject(["evolutionApproval"], (approvalCtx) => {
|
|
156
|
+
const dispose = approvalCtx.evolutionApproval.registerRunner("memory", (args) => executeCore(args));
|
|
157
|
+
approvalCtx.effect(() => dispose, "tool-memory.approval-runner");
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
export { Config, apply, inject, name };
|
package/lib/invariant.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region lib/types/invariant.js
|
|
2
|
+
const PACKAGE_NAME = "@deepseek-ai/dsh-tool-memory";
|
|
3
|
+
const name = "tool-memory-invariant";
|
|
4
|
+
const inject = ["invariants"];
|
|
5
|
+
const install = () => {};
|
|
6
|
+
const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
7
|
+
//#endregion
|
|
8
|
+
export { apply, inject, name };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing memory tool and runtime-context memory snapshot.
|
|
3
|
+
* @module @deepseek-ai/dsh-tool-memory
|
|
4
|
+
*/
|
|
5
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
6
|
+
import z from '@deepseek-ai/schemastery';
|
|
7
|
+
export declare const name = "tool-memory";
|
|
8
|
+
export declare const inject: string[];
|
|
9
|
+
export interface Config {
|
|
10
|
+
memoryEnabled?: boolean;
|
|
11
|
+
/** Maximum characters of each memory entry echoed back in tool results. */
|
|
12
|
+
entryPreviewChars?: number;
|
|
13
|
+
}
|
|
14
|
+
export declare const Config: z<Config>;
|
|
15
|
+
export declare function apply(ctx: Context, rawConfig: Config): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lmzhen/dsh-tool-memory",
|
|
3
|
+
"description": "Model-facing memory tool and prompt context (community build)",
|
|
4
|
+
"version": "0.1.0-rc.1",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/lmzhen/dsh-evolution.git",
|
|
11
|
+
"directory": "packages/dsh-tool-memory"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"types": "lib/types/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./lib/types/index.d.ts",
|
|
19
|
+
"default": "./lib/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./invariant": {
|
|
22
|
+
"types": "./lib/types/invariant.d.ts",
|
|
23
|
+
"default": "./lib/invariant.js"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"lib/index.js",
|
|
29
|
+
"lib/invariant.js",
|
|
30
|
+
"lib/types/**/*.d.ts",
|
|
31
|
+
"lib/types/invariant.d.ts"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@deepseek-ai/schemastery": "^3.18.1"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@deepseek-ai/dsh-invariants": "^0.1.0-rc.6",
|
|
39
|
+
"@deepseek-ai/cordis": "^4.0.1",
|
|
40
|
+
"@deepseek-ai/dsh-tools": "^0.1.0-rc.6",
|
|
41
|
+
"@deepseek-ai/dsh-system-prompt": "^0.1.0-rc.6",
|
|
42
|
+
"@lmzhen/dsh-memory": "^0.1.0-rc.1",
|
|
43
|
+
"@lmzhen/dsh-memory-files": "^0.1.0-rc.1"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@deepseek-ai/dsh-invariants": "^0.1.0-rc.6",
|
|
47
|
+
"@deepseek-ai/dsh-tools": "^0.1.0-rc.6",
|
|
48
|
+
"@deepseek-ai/dsh-system-prompt": "^0.1.0-rc.6",
|
|
49
|
+
"@deepseek-ai/dsh-agent-loop-testkit": "^0.1.0-rc.6",
|
|
50
|
+
"@lmzhen/dsh-memory": "^0.1.0-rc.1",
|
|
51
|
+
"@lmzhen/dsh-memory-files": "^0.1.0-rc.1"
|
|
52
|
+
}
|
|
53
|
+
}
|