@astratra/memory 0.1.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/LICENSE +21 -0
- package/README.md +239 -0
- package/package.json +39 -0
- package/src/consolidation.js +64 -0
- package/src/handlers.js +84 -0
- package/src/index.d.ts +398 -0
- package/src/index.js +10 -0
- package/src/memory.js +672 -0
- package/src/memoryStore.js +181 -0
- package/src/ranking.js +74 -0
- package/src/rules.js +153 -0
- package/src/testing/storeContract.js +194 -0
- package/src/tools.js +172 -0
package/src/tools.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four memory tools, shaped for @astratra/ai's createToolRegistry():
|
|
3
|
+
* { name, description, type, roles, params, handler(params, ctx) }.
|
|
4
|
+
*
|
|
5
|
+
* Results are data and codes, never sentences: `code` says what happened,
|
|
6
|
+
* `reason` why a write was refused. Pass `translate(code, ctx)` to add a
|
|
7
|
+
* `message` in the person's language from your own catalog.
|
|
8
|
+
*
|
|
9
|
+
* `forget` erases. Put it behind runAgentLoop's `confirmTool` (or
|
|
10
|
+
* createPendingActions) so a person says yes first.
|
|
11
|
+
*/
|
|
12
|
+
const { REASONS } = require('./rules');
|
|
13
|
+
|
|
14
|
+
const DEFAULT_NAMES = Object.freeze({ remember: 'remember', recall: 'recall', update: 'update_memory', forget: 'forget' });
|
|
15
|
+
|
|
16
|
+
/* Instructions to the model, not text for a person. Override with `descriptions`. */
|
|
17
|
+
const DEFAULT_DESCRIPTIONS = Object.freeze({
|
|
18
|
+
remember: 'Keeps one new durable fact about the current person for future conversations (a goal, a preference, a person in their life, an event). Use it on your own when they tell you such a thing, not for small talk. If recall shows a memory this changes, use the update tool on that memory instead.',
|
|
19
|
+
recall: 'Searches the current person\'s memories by meaning and by words.',
|
|
20
|
+
update: 'Corrects one memory of the current person, found with recall (give its id), when they say it changed or was wrong. The new text replaces the old one; kind and importance stay unless given.',
|
|
21
|
+
forget: 'Erases one memory of the current person (give its id, found with recall). Erasing needs the person\'s confirmation.'
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {object} options
|
|
26
|
+
* @param {object} options.memory from createMemory().
|
|
27
|
+
* @param {string[]} options.roles roles that may use these tools.
|
|
28
|
+
* @param {Function} options.whereOf (ctx) => { ownerId, scope } — required: the
|
|
29
|
+
* tools never guess whose memories they touch.
|
|
30
|
+
* @param {Function} [options.roleOf] (ctx) => role. Default ctx.userRole ?? ctx.role.
|
|
31
|
+
* @param {Function} [options.personNameOf] (ctx) => the person's own name (allowed in their memories).
|
|
32
|
+
* @param {Function} [options.isExplicit] (params, ctx) => the person asked to remember this.
|
|
33
|
+
* @param {Function} [options.sourceOf] (params, ctx) => extra source fields (conversation id…).
|
|
34
|
+
* @param {Function} [options.isAiEnabled] (ctx) => boolean. Off: every tool refuses with 'ai_disabled'.
|
|
35
|
+
* @param {Function} [options.translate] (code, ctx) => string, added as `message`.
|
|
36
|
+
* @param {object} [options.names] rename the tools.
|
|
37
|
+
* @param {object} [options.descriptions] replace the model instructions.
|
|
38
|
+
*/
|
|
39
|
+
function createMemoryTools(options = {}) {
|
|
40
|
+
const memory = options.memory;
|
|
41
|
+
if (!memory || typeof memory.remember !== 'function') throw new Error('createMemoryTools requires options.memory from createMemory().');
|
|
42
|
+
if (!Array.isArray(options.roles) || !options.roles.length) throw new Error('createMemoryTools requires options.roles.');
|
|
43
|
+
if (typeof options.whereOf !== 'function') throw new Error('createMemoryTools requires options.whereOf(ctx).');
|
|
44
|
+
|
|
45
|
+
const roles = [...options.roles];
|
|
46
|
+
const whereOf = options.whereOf;
|
|
47
|
+
const roleOf = options.roleOf || ((ctx) => (ctx.userRole !== undefined ? ctx.userRole : ctx.role));
|
|
48
|
+
const personNameOf = options.personNameOf || (() => '');
|
|
49
|
+
const isExplicit = options.isExplicit || (() => false);
|
|
50
|
+
const sourceOf = options.sourceOf || (() => ({}));
|
|
51
|
+
const isAiEnabled = options.isAiEnabled || null;
|
|
52
|
+
const translate = options.translate || null;
|
|
53
|
+
const names = { ...DEFAULT_NAMES, ...(options.names || {}) };
|
|
54
|
+
const descriptions = { ...DEFAULT_DESCRIPTIONS, ...(options.descriptions || {}) };
|
|
55
|
+
const kindList = memory.kinds.join('|');
|
|
56
|
+
|
|
57
|
+
const reply = (body, ctx) => {
|
|
58
|
+
if (!translate) return body;
|
|
59
|
+
const key = body.ok ? body.code : `${body.code}.${body.reason}`;
|
|
60
|
+
return { ...body, message: translate(key, ctx) };
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const guard = (fn) => async (params = {}, ctx = {}) => {
|
|
64
|
+
if (isAiEnabled && !(await isAiEnabled(ctx))) return reply({ ok: false, code: 'refused', reason: REASONS.AI_DISABLED }, ctx);
|
|
65
|
+
return fn(params || {}, ctx || {});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const rememberTool = {
|
|
69
|
+
name: names.remember,
|
|
70
|
+
description: descriptions.remember,
|
|
71
|
+
type: 'write',
|
|
72
|
+
roles,
|
|
73
|
+
params: { text: 'string', kind: kindList, importance: 'integer 1-5' },
|
|
74
|
+
handler: guard(async (params, ctx) => {
|
|
75
|
+
const result = await memory.remember(whereOf(ctx), {
|
|
76
|
+
text: params.text,
|
|
77
|
+
kind: params.kind,
|
|
78
|
+
importance: params.importance,
|
|
79
|
+
role: roleOf(ctx),
|
|
80
|
+
personName: personNameOf(ctx),
|
|
81
|
+
explicit: Boolean(await isExplicit(params, ctx)),
|
|
82
|
+
source: sourceOf(params, ctx),
|
|
83
|
+
ctx
|
|
84
|
+
});
|
|
85
|
+
if (!result.ok) return reply({ ok: false, code: 'refused', reason: result.reason }, ctx);
|
|
86
|
+
return reply({
|
|
87
|
+
ok: true,
|
|
88
|
+
code: 'memory_saved',
|
|
89
|
+
memory: { id: result.memory.id, text: result.memory.text, kind: result.memory.kind, importance: result.memory.importance },
|
|
90
|
+
undo: { id: result.memory.id }
|
|
91
|
+
}, ctx);
|
|
92
|
+
})
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const recallTool = {
|
|
96
|
+
name: names.recall,
|
|
97
|
+
description: descriptions.recall,
|
|
98
|
+
type: 'read',
|
|
99
|
+
roles,
|
|
100
|
+
params: { query: 'string', kinds: `array of ${kindList} (optional)`, limit: 'integer 1-20 (optional)' },
|
|
101
|
+
handler: guard(async (params, ctx) => {
|
|
102
|
+
let found;
|
|
103
|
+
try {
|
|
104
|
+
found = await memory.recall(whereOf(ctx), {
|
|
105
|
+
query: params.query,
|
|
106
|
+
kinds: params.kinds,
|
|
107
|
+
after: params.after,
|
|
108
|
+
before: params.before,
|
|
109
|
+
limit: params.limit === undefined ? undefined : Number(params.limit)
|
|
110
|
+
});
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error && error.name === 'MemoryError' && /^invalid_(after|before)$/.test(error.code)) {
|
|
113
|
+
return reply({ ok: false, code: 'refused', reason: error.code }, ctx);
|
|
114
|
+
}
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
return reply({
|
|
118
|
+
ok: true,
|
|
119
|
+
code: 'memories_found',
|
|
120
|
+
memories: found.map(({ id, text, kind, importance, createdAt, lastUsedAt }) => ({ id, text, kind, importance, createdAt, lastUsedAt }))
|
|
121
|
+
}, ctx);
|
|
122
|
+
})
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const updateTool = {
|
|
126
|
+
name: names.update,
|
|
127
|
+
description: descriptions.update,
|
|
128
|
+
type: 'write',
|
|
129
|
+
roles,
|
|
130
|
+
params: { id: 'string', text: 'string', kind: `${kindList} (optional)`, importance: 'integer 1-5 (optional)' },
|
|
131
|
+
handler: guard(async (params, ctx) => {
|
|
132
|
+
if (await memory.isPaused(whereOf(ctx))) return reply({ ok: false, code: 'refused', reason: REASONS.PAUSED }, ctx);
|
|
133
|
+
const result = await memory.update(whereOf(ctx), String(params.id ?? ''), {
|
|
134
|
+
text: params.text,
|
|
135
|
+
kind: params.kind,
|
|
136
|
+
importance: params.importance
|
|
137
|
+
}, {
|
|
138
|
+
personName: personNameOf(ctx),
|
|
139
|
+
explicit: Boolean(await isExplicit(params, ctx)),
|
|
140
|
+
ctx
|
|
141
|
+
});
|
|
142
|
+
if (!result.ok) return reply({ ok: false, code: 'refused', reason: result.reason }, ctx);
|
|
143
|
+
return reply({
|
|
144
|
+
ok: true,
|
|
145
|
+
code: 'memory_corrected',
|
|
146
|
+
memory: { id: result.memory.id, text: result.memory.text, kind: result.memory.kind, importance: result.memory.importance },
|
|
147
|
+
undo: result.supersededId ? { id: result.memory.id } : null
|
|
148
|
+
}, ctx);
|
|
149
|
+
})
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const forgetTool = {
|
|
153
|
+
name: names.forget,
|
|
154
|
+
description: descriptions.forget,
|
|
155
|
+
type: 'write',
|
|
156
|
+
roles,
|
|
157
|
+
params: { id: 'string' },
|
|
158
|
+
handler: guard(async (params, ctx) => {
|
|
159
|
+
const erased = await memory.forget(whereOf(ctx), String(params.id ?? ''));
|
|
160
|
+
if (!erased) return reply({ ok: false, code: 'refused', reason: REASONS.NOT_FOUND }, ctx);
|
|
161
|
+
return reply({ ok: true, code: 'memory_forgotten', id: String(params.id) }, ctx);
|
|
162
|
+
})
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
return [rememberTool, recallTool, updateTool, forgetTool];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
module.exports = {
|
|
169
|
+
createMemoryTools,
|
|
170
|
+
DEFAULT_TOOL_NAMES: DEFAULT_NAMES,
|
|
171
|
+
DEFAULT_TOOL_DESCRIPTIONS: DEFAULT_DESCRIPTIONS
|
|
172
|
+
};
|