agent-lyra 0.7.0 → 0.10.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 -21
- package/README.md +301 -276
- package/bin/agent-lyra.js +51 -51
- package/bin/lyra-oss-preview.js +16 -16
- package/package.json +60 -60
- package/src/app-builder/export.js +109 -109
- package/src/app-builder/gateway.js +62 -62
- package/src/app-builder/init.js +115 -72
- package/src/app-builder/manifest-contract.js +46 -46
- package/src/app-builder/native-adapter.js +29 -29
- package/src/app-builder/preview.js +35 -31
- package/src/app-builder/publish.js +14 -14
- package/src/app-builder/saved-app.js +70 -70
- package/src/app-builder/saved-preview-browser.js +33 -33
- package/src/app-builder/saved-preview.html +24 -24
- package/src/app-builder/saved-preview.js +61 -61
- package/src/app-builder/support-manifest.js +50 -50
- package/src/app-builder/support-template/manifest.json +55 -55
- package/src/app-builder/support-template/support-preview.html +56 -56
- package/src/app-builder/support-template/support-preview.js +37 -37
- package/src/app-builder/support-template/support-preview.mjs +67 -67
- package/src/app-builder/support-template/support-workflow.mjs +36 -36
- package/src/app-builder/use.js +37 -37
- package/src/app-builder/validate.js +31 -21
- package/src/app-builder/value-schema.js +37 -37
- package/src/catalog.js +147 -147
- package/src/coding/catalog.js +56 -56
- package/src/coding/cloud-catalog.js +64 -64
- package/src/coding/detect-stack.mjs +110 -110
- package/src/coding/export.js +133 -133
- package/src/coding/setup.js +175 -175
- package/src/coding/skills/lyra-android/SKILL.md +37 -37
- package/src/coding/skills/lyra-angular/SKILL.md +37 -37
- package/src/coding/skills/lyra-cucumber/SKILL.md +37 -37
- package/src/coding/skills/lyra-dbt/SKILL.md +37 -37
- package/src/coding/skills/lyra-django/SKILL.md +37 -37
- package/src/coding/skills/lyra-docker/SKILL.md +37 -37
- package/src/coding/skills/lyra-dotnet/SKILL.md +37 -37
- package/src/coding/skills/lyra-express/SKILL.md +37 -37
- package/src/coding/skills/lyra-fastapi/SKILL.md +37 -37
- package/src/coding/skills/lyra-flutter/SKILL.md +37 -37
- package/src/coding/skills/lyra-github-actions/SKILL.md +37 -37
- package/src/coding/skills/lyra-gitlab-ci/SKILL.md +37 -37
- package/src/coding/skills/lyra-go/SKILL.md +37 -37
- package/src/coding/skills/lyra-ios/SKILL.md +37 -37
- package/src/coding/skills/lyra-kubernetes/SKILL.md +37 -37
- package/src/coding/skills/lyra-mongodb/SKILL.md +37 -37
- package/src/coding/skills/lyra-mysql/SKILL.md +37 -37
- package/src/coding/skills/lyra-nextjs/SKILL.md +37 -37
- package/src/coding/skills/lyra-node/SKILL.md +37 -37
- package/src/coding/skills/lyra-playwright/SKILL.md +37 -37
- package/src/coding/skills/lyra-postgresql/SKILL.md +37 -37
- package/src/coding/skills/lyra-python/SKILL.md +37 -37
- package/src/coding/skills/lyra-react/SKILL.md +37 -37
- package/src/coding/skills/lyra-react-native/SKILL.md +37 -37
- package/src/coding/skills/lyra-redis/SKILL.md +37 -37
- package/src/coding/skills/lyra-rust/SKILL.md +37 -37
- package/src/coding/skills/lyra-spark/SKILL.md +37 -37
- package/src/coding/skills/lyra-spring-boot/SKILL.md +37 -37
- package/src/coding/skills/lyra-svelte/SKILL.md +37 -37
- package/src/coding/skills/lyra-terraform/SKILL.md +37 -37
- package/src/coding/skills/lyra-typescript/SKILL.md +37 -37
- package/src/coding/skills/lyra-vue/SKILL.md +37 -37
- package/src/framework/agent.js +181 -173
- package/src/framework/mda.js +65 -0
- package/src/framework/run.py +111 -111
- package/src/gated/hostedAgents.js +56 -56
- package/src/gated/index.d.ts +25 -25
- package/src/gated/index.js +5 -5
- package/src/index.d.ts +2 -2
- package/src/index.js +2 -2
- package/src/init.js +960 -960
- package/src/intelligence/graphsnip-provider.d.ts +5 -5
- package/src/intelligence/graphsnip-provider.js +57 -57
- package/src/intelligence/lifecycle.js +25 -0
- package/src/memory/LICENSE +21 -21
- package/src/memory/README.md +5 -5
- package/src/memory/UPSTREAM.json +24 -14
- package/src/memory/bridge-cli.js +16 -16
- package/src/memory/bridge.js +18 -18
- package/src/memory/cli.js +21 -21
- package/src/memory/cloud-sync-actions.js +61 -61
- package/src/memory/contract.json +45 -45
- package/src/memory/index.d.ts +50 -49
- package/src/memory/index.js +7 -7
- package/src/memory/lifecycle.js +29 -29
- package/src/memory/local-cli.js +34 -34
- package/src/memory/local-store.js +209 -209
- package/src/memory/package.json +33 -33
- package/src/memory/proposals.js +90 -90
- package/src/memory/skills-cli.js +26 -26
- package/src/memory/standalone.d.ts +50 -50
- package/src/memory/standalone.js +1 -1
- package/src/memory/sync.js +89 -89
- package/src/memory/validation.js +25 -25
- package/src/onboardingAgents.js +532 -532
- package/src/promptTemplates.js +178 -178
- package/src/resource-files.js +18 -18
- package/src/runtime/contracts.d.ts +47 -47
- package/src/runtime/local-cli.js +161 -160
- package/src/runtime/mcp-stdio.js +111 -111
- package/src/runtime/orchestration.js +59 -59
- package/src/runtime/run-agent.js +54 -53
- package/src/standalone/index.d.ts +45 -45
- package/src/standalone/index.js +13 -13
- package/src/standalone/init.js +12 -12
- package/src/standalone/orchestration.js +6 -6
- package/src/standalone/rag.js +113 -113
- package/src/studio/agent-ui.css +43 -43
- package/src/studio/agent-ui.js +32 -32
- package/src/studio/browser.js +542 -345
- package/src/studio/export.js +83 -85
- package/src/studio/index.html +74 -37
- package/src/studio/memory-actions.js +37 -37
- package/src/studio/memory-panel.js +200 -200
- package/src/studio/project.js +308 -240
- package/src/studio/pull-agent.js +72 -72
- package/src/studio/push.js +54 -54
- package/src/studio/python-agent.js +47 -47
- package/src/studio/python-agent.py +25 -25
- package/src/studio/server.js +164 -150
- package/src/studio/session.js +83 -83
- package/src/studio/studio.css +257 -10
- package/src/studio/sync.js +42 -42
- package/src/studio/workflow-editor.js +240 -0
- package/src/studio/workflow-sync.js +229 -157
|
@@ -1,209 +1,209 @@
|
|
|
1
|
-
import { proposalActions } from "./proposals.js";
|
|
2
|
-
import { reviewEvidence, eligibleText, hasMemoryAdapter, safeContext } from "./validation.js";
|
|
3
|
-
import { randomUUID } from "node:crypto";
|
|
4
|
-
import { chmod, lstat, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
5
|
-
import { join, resolve } from "node:path";
|
|
6
|
-
|
|
7
|
-
const spec = JSON.parse(await readFile(new URL("./contract.json", import.meta.url), "utf8"));
|
|
8
|
-
const { limits } = spec;
|
|
9
|
-
const safeRef = /^[A-Za-z0-9][A-Za-z0-9_.:-]*$/;
|
|
10
|
-
const time = () => new Date().toISOString();
|
|
11
|
-
const queryTokens = value => value.toLowerCase().match(/[\p{L}\p{N}_]+/gu)?.slice(0, 12) ?? [];
|
|
12
|
-
|
|
13
|
-
function text(value, max, name) {
|
|
14
|
-
if (typeof value !== "string" || !value.trim() || [...value.trim()].length > max || value.includes("\0")) {
|
|
15
|
-
throw new Error(name + " must contain 1–" + max + " characters.");
|
|
16
|
-
}
|
|
17
|
-
return value.trim();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function record(value) {
|
|
21
|
-
if (!spec.types.includes(value?.type)) throw new Error("Choose episodic, semantic, procedural, or corrective memory.");
|
|
22
|
-
const evidence = value.evidence ?? [];
|
|
23
|
-
if (!Array.isArray(evidence) || evidence.length > limits.evidence ||
|
|
24
|
-
evidence.some(item => typeof item !== "string" || item.length > limits.id || !safeRef.test(item))) {
|
|
25
|
-
throw new Error("Use up to 16 evidence IDs, such as run:abc123.");
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
type: value.type, title: text(value.title, limits.title, "Title"),
|
|
29
|
-
learning: text(value.learning, limits.learning, "Learning"), evidence: [...new Set(evidence)],
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function present(row) {
|
|
34
|
-
return row ? { id: row.id, type: row.type, title: row.title, learning: row.learning,
|
|
35
|
-
evidence: JSON.parse(row.evidence), scope: row.scope, status: row.status, revision: row.revision,
|
|
36
|
-
createdAt: row.created_at, updatedAt: row.updated_at, ...(row.verification ? { verification: JSON.parse(row.verification) } : {}) } : null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function bounded(limit) {
|
|
40
|
-
if (!Number.isInteger(limit) || limit < 1 || limit > limits.results) throw new Error("Limit must be between 1 and 100.");
|
|
41
|
-
return limit;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async function directory(root, namespace) {
|
|
45
|
-
let path = resolve(root);
|
|
46
|
-
for (const part of [namespace, "memory"]) {
|
|
47
|
-
path = join(path, part);
|
|
48
|
-
await mkdir(path, { mode: 0o700 }).catch(error => { if (error.code !== "EEXIST") throw error; });
|
|
49
|
-
if (!(await lstat(path)).isDirectory()) throw new Error("Memory folders must be regular directories.");
|
|
50
|
-
}
|
|
51
|
-
await chmod(path, 0o700);
|
|
52
|
-
await writeFile(join(path, ".gitignore"), "*\n", { flag: "wx", mode: 0o600 })
|
|
53
|
-
.catch(error => { if (error.code !== "EEXIST") throw error; });
|
|
54
|
-
return path;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export async function openLocalMemoryStore(root = process.cwd(), { forceKeyword = false, namespace = ".memory-engine", extension } = {}) {
|
|
58
|
-
if (!/^\.[A-Za-z0-9][A-Za-z0-9_-]*$/.test(namespace)) throw new Error("Invalid memory namespace.");
|
|
59
|
-
const dir = await directory(root, namespace);
|
|
60
|
-
const file = join(dir, "memory.db");
|
|
61
|
-
await writeFile(file, "", { flag: "wx", mode: 0o600 })
|
|
62
|
-
.catch(error => { if (error.code !== "EEXIST") throw error; });
|
|
63
|
-
if (!(await lstat(file)).isFile()) throw new Error("The memory database must be a regular file.");
|
|
64
|
-
await chmod(file, 0o600);
|
|
65
|
-
const { DatabaseSync } = await import("node:sqlite");
|
|
66
|
-
const db = new DatabaseSync(file);
|
|
67
|
-
function transaction(fn) {
|
|
68
|
-
db.exec("BEGIN IMMEDIATE");
|
|
69
|
-
try {
|
|
70
|
-
const result = fn();
|
|
71
|
-
db.exec("COMMIT");
|
|
72
|
-
return result;
|
|
73
|
-
} catch (error) {
|
|
74
|
-
db.exec("ROLLBACK");
|
|
75
|
-
throw error;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
let fts = false;
|
|
79
|
-
try {
|
|
80
|
-
db.exec("PRAGMA busy_timeout = 3000");
|
|
81
|
-
transaction(() => {
|
|
82
|
-
const { user_version: version } = db.prepare("PRAGMA user_version").get();
|
|
83
|
-
const { application_id: app } = db.prepare("PRAGMA application_id").get();
|
|
84
|
-
if (version > spec.version) throw new Error("Update the memory engine before opening this newer memory database.");
|
|
85
|
-
if ((app !== 0 && app !== spec.applicationId) || (version !== 0 && app !== spec.applicationId) ||
|
|
86
|
-
(version === 0 && db.prepare("SELECT 1 FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%' LIMIT 1").get())) {
|
|
87
|
-
throw new Error("The file is not a compatible memory database.");
|
|
88
|
-
}
|
|
89
|
-
if (version === 0) db.exec(spec.migration + "PRAGMA application_id = " + spec.applicationId);
|
|
90
|
-
if (version < 2) db.exec(spec.migration2);
|
|
91
|
-
if (version < 3) db.exec(spec.migration3);
|
|
92
|
-
if (version < 4) db.exec("PRAGMA user_version = 4");
|
|
93
|
-
});
|
|
94
|
-
db.exec("PRAGMA journal_mode = WAL; PRAGMA secure_delete = ON");
|
|
95
|
-
if (!forceKeyword) {
|
|
96
|
-
try {
|
|
97
|
-
db.exec("CREATE VIRTUAL TABLE IF NOT EXISTS memory_fts USING fts5(id UNINDEXED, title, learning)");
|
|
98
|
-
fts = true;
|
|
99
|
-
} catch (error) {
|
|
100
|
-
if (!/no such module: fts5/i.test(error.message)) throw error;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
} catch (error) { db.close(); throw error; }
|
|
104
|
-
|
|
105
|
-
const get = id => present(db.prepare("SELECT * FROM memories WHERE id = ?").get(id));
|
|
106
|
-
const dirty = () => db.exec("UPDATE memory_index_state SET revision = revision + 1 WHERE id = 1");
|
|
107
|
-
const erase = (id, revision) => {
|
|
108
|
-
db.prepare("INSERT INTO memory_tombstones VALUES (?,?,?) ON CONFLICT(id) DO NOTHING").run(id, revision, time());
|
|
109
|
-
if (fts) db.prepare("DELETE FROM memory_fts WHERE id=?").run(id);
|
|
110
|
-
db.prepare("DELETE FROM skill_proposals WHERE EXISTS (SELECT 1 FROM json_each(sources) WHERE json_extract(value, '$.id')=?)").run(id);
|
|
111
|
-
db.prepare("DELETE FROM memories WHERE id=?").run(id);
|
|
112
|
-
dirty();
|
|
113
|
-
};
|
|
114
|
-
let extensions;
|
|
115
|
-
try {
|
|
116
|
-
extensions = extension?.({ db, transaction, get, erase, dirty, record, eligibleText, limits, time }) ?? {};
|
|
117
|
-
} catch (error) { db.close(); throw error; }
|
|
118
|
-
return {
|
|
119
|
-
...extensions,
|
|
120
|
-
...proposalActions(db, transaction),
|
|
121
|
-
file,
|
|
122
|
-
close: () => db.close(),
|
|
123
|
-
get,
|
|
124
|
-
list(limit = 20) {
|
|
125
|
-
return db.prepare("SELECT * FROM memories ORDER BY updated_at DESC, id LIMIT ?").all(bounded(limit)).map(present);
|
|
126
|
-
},
|
|
127
|
-
record(value) {
|
|
128
|
-
const item = record(value);
|
|
129
|
-
return transaction(() => {
|
|
130
|
-
if (db.prepare("SELECT count(*) AS count FROM memories").get().count >= limits.records) {
|
|
131
|
-
throw new Error("Memory is full. Forget a record before adding one.");
|
|
132
|
-
}
|
|
133
|
-
const id = randomUUID();
|
|
134
|
-
const now = time();
|
|
135
|
-
db.prepare("INSERT INTO memories (id,type,title,learning,evidence,created_at,updated_at) VALUES (?,?,?,?,?,?,?)")
|
|
136
|
-
.run(id, item.type, item.title, item.learning, JSON.stringify(item.evidence), now, now);
|
|
137
|
-
dirty();
|
|
138
|
-
return get(id);
|
|
139
|
-
});
|
|
140
|
-
},
|
|
141
|
-
search(query, limit = 20) {
|
|
142
|
-
bounded(limit);
|
|
143
|
-
const tokens = queryTokens(text(query, 240, "Query"));
|
|
144
|
-
if (!tokens.length) return [];
|
|
145
|
-
if (!fts) {
|
|
146
|
-
const terms = tokens.map(() => "instr(lower(title || ' ' || learning), ?) > 0").join(" AND ");
|
|
147
|
-
return db.prepare("SELECT * FROM memories WHERE status != 'archived' AND " + terms + " ORDER BY updated_at DESC, id LIMIT ?")
|
|
148
|
-
.all(...tokens, limit).map(present);
|
|
149
|
-
}
|
|
150
|
-
return transaction(() => {
|
|
151
|
-
const state = db.prepare("SELECT revision, indexed_revision FROM memory_index_state WHERE id=1").get();
|
|
152
|
-
if (state.revision !== state.indexed_revision) {
|
|
153
|
-
db.exec("DELETE FROM memory_fts; INSERT INTO memory_fts SELECT id,title,learning FROM memories;" +
|
|
154
|
-
"UPDATE memory_index_state SET indexed_revision=revision WHERE id=1");
|
|
155
|
-
}
|
|
156
|
-
const expression = tokens.map(token => '"' + token + '"').join(" AND ");
|
|
157
|
-
return db.prepare("SELECT memories.* FROM memory_fts JOIN memories ON memories.id = memory_fts.id " +
|
|
158
|
-
"WHERE memory_fts MATCH ? AND memories.status != 'archived' ORDER BY bm25(memory_fts), memories.id LIMIT ?").all(expression, limit).map(present);
|
|
159
|
-
});
|
|
160
|
-
},
|
|
161
|
-
verify(id, evidence) {
|
|
162
|
-
return transaction(() => {
|
|
163
|
-
const receipt = reviewEvidence(get(id), evidence);
|
|
164
|
-
db.prepare("UPDATE memories SET status='verified', verification=?, revision=revision+1, updated_at=? WHERE id=?")
|
|
165
|
-
.run(JSON.stringify(receipt), time(), id);
|
|
166
|
-
return get(id);
|
|
167
|
-
});
|
|
168
|
-
},
|
|
169
|
-
retrieve(query, context, adapter = "coding") {
|
|
170
|
-
if (!safeContext(context) || !hasMemoryAdapter(adapter)) return [];
|
|
171
|
-
const tokens = new Set(queryTokens(query).filter(token => token.length >= 4));
|
|
172
|
-
if (!tokens.size) return [];
|
|
173
|
-
const result = [];
|
|
174
|
-
let remaining = 2400;
|
|
175
|
-
const rows = db.prepare("SELECT * FROM memories WHERE status='verified' AND json_extract(verification, '$.context') = ? AND json_extract(verification, '$.adapter') = ? ORDER BY updated_at DESC, id LIMIT 100").all(context, adapter);
|
|
176
|
-
for (const row of rows) {
|
|
177
|
-
const item = present(row);
|
|
178
|
-
const body = item.title + " " + item.learning;
|
|
179
|
-
if (!eligibleText(body) || !queryTokens(body).some(token => tokens.has(token)) || body.length > remaining) continue;
|
|
180
|
-
result.push(item);
|
|
181
|
-
remaining -= body.length;
|
|
182
|
-
if (result.length === 4) break;
|
|
183
|
-
}
|
|
184
|
-
return result;
|
|
185
|
-
},
|
|
186
|
-
archive(id) {
|
|
187
|
-
return transaction(() => {
|
|
188
|
-
const item = get(id);
|
|
189
|
-
if (!item || item.status === "archived") return item;
|
|
190
|
-
db.prepare("UPDATE memories SET status = 'archived', revision = revision + 1, updated_at = ? WHERE id = ?")
|
|
191
|
-
.run(time(), id);
|
|
192
|
-
return get(id);
|
|
193
|
-
});
|
|
194
|
-
},
|
|
195
|
-
stats() {
|
|
196
|
-
const counts = Object.fromEntries(spec.statuses.map(status => [status, 0]));
|
|
197
|
-
for (const row of db.prepare("SELECT status, count(*) AS count FROM memories GROUP BY status").all()) counts[row.status] = row.count;
|
|
198
|
-
return { schemaVersion: spec.version, search: fts ? "fts5" : "keyword", ...counts };
|
|
199
|
-
},
|
|
200
|
-
forget(id) {
|
|
201
|
-
return transaction(() => {
|
|
202
|
-
const item = get(id);
|
|
203
|
-
if (!item) return false;
|
|
204
|
-
erase(id, item.revision + 1);
|
|
205
|
-
return true;
|
|
206
|
-
});
|
|
207
|
-
},
|
|
208
|
-
};
|
|
209
|
-
}
|
|
1
|
+
import { proposalActions } from "./proposals.js";
|
|
2
|
+
import { reviewEvidence, eligibleText, hasMemoryAdapter, safeContext } from "./validation.js";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { chmod, lstat, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
5
|
+
import { join, resolve } from "node:path";
|
|
6
|
+
|
|
7
|
+
const spec = JSON.parse(await readFile(new URL("./contract.json", import.meta.url), "utf8"));
|
|
8
|
+
const { limits } = spec;
|
|
9
|
+
const safeRef = /^[A-Za-z0-9][A-Za-z0-9_.:-]*$/;
|
|
10
|
+
const time = () => new Date().toISOString();
|
|
11
|
+
const queryTokens = value => value.toLowerCase().match(/[\p{L}\p{N}_]+/gu)?.slice(0, 12) ?? [];
|
|
12
|
+
|
|
13
|
+
function text(value, max, name) {
|
|
14
|
+
if (typeof value !== "string" || !value.trim() || [...value.trim()].length > max || value.includes("\0")) {
|
|
15
|
+
throw new Error(name + " must contain 1–" + max + " characters.");
|
|
16
|
+
}
|
|
17
|
+
return value.trim();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function record(value) {
|
|
21
|
+
if (!spec.types.includes(value?.type)) throw new Error("Choose episodic, semantic, procedural, or corrective memory.");
|
|
22
|
+
const evidence = value.evidence ?? [];
|
|
23
|
+
if (!Array.isArray(evidence) || evidence.length > limits.evidence ||
|
|
24
|
+
evidence.some(item => typeof item !== "string" || item.length > limits.id || !safeRef.test(item))) {
|
|
25
|
+
throw new Error("Use up to 16 evidence IDs, such as run:abc123.");
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
type: value.type, title: text(value.title, limits.title, "Title"),
|
|
29
|
+
learning: text(value.learning, limits.learning, "Learning"), evidence: [...new Set(evidence)],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function present(row) {
|
|
34
|
+
return row ? { id: row.id, type: row.type, title: row.title, learning: row.learning,
|
|
35
|
+
evidence: JSON.parse(row.evidence), scope: row.scope, status: row.status, revision: row.revision,
|
|
36
|
+
createdAt: row.created_at, updatedAt: row.updated_at, ...(row.verification ? { verification: JSON.parse(row.verification) } : {}) } : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function bounded(limit) {
|
|
40
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > limits.results) throw new Error("Limit must be between 1 and 100.");
|
|
41
|
+
return limit;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function directory(root, namespace) {
|
|
45
|
+
let path = resolve(root);
|
|
46
|
+
for (const part of [namespace, "memory"]) {
|
|
47
|
+
path = join(path, part);
|
|
48
|
+
await mkdir(path, { mode: 0o700 }).catch(error => { if (error.code !== "EEXIST") throw error; });
|
|
49
|
+
if (!(await lstat(path)).isDirectory()) throw new Error("Memory folders must be regular directories.");
|
|
50
|
+
}
|
|
51
|
+
await chmod(path, 0o700);
|
|
52
|
+
await writeFile(join(path, ".gitignore"), "*\n", { flag: "wx", mode: 0o600 })
|
|
53
|
+
.catch(error => { if (error.code !== "EEXIST") throw error; });
|
|
54
|
+
return path;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function openLocalMemoryStore(root = process.cwd(), { forceKeyword = false, namespace = ".memory-engine", extension } = {}) {
|
|
58
|
+
if (!/^\.[A-Za-z0-9][A-Za-z0-9_-]*$/.test(namespace)) throw new Error("Invalid memory namespace.");
|
|
59
|
+
const dir = await directory(root, namespace);
|
|
60
|
+
const file = join(dir, "memory.db");
|
|
61
|
+
await writeFile(file, "", { flag: "wx", mode: 0o600 })
|
|
62
|
+
.catch(error => { if (error.code !== "EEXIST") throw error; });
|
|
63
|
+
if (!(await lstat(file)).isFile()) throw new Error("The memory database must be a regular file.");
|
|
64
|
+
await chmod(file, 0o600);
|
|
65
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
66
|
+
const db = new DatabaseSync(file);
|
|
67
|
+
function transaction(fn) {
|
|
68
|
+
db.exec("BEGIN IMMEDIATE");
|
|
69
|
+
try {
|
|
70
|
+
const result = fn();
|
|
71
|
+
db.exec("COMMIT");
|
|
72
|
+
return result;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
db.exec("ROLLBACK");
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
let fts = false;
|
|
79
|
+
try {
|
|
80
|
+
db.exec("PRAGMA busy_timeout = 3000");
|
|
81
|
+
transaction(() => {
|
|
82
|
+
const { user_version: version } = db.prepare("PRAGMA user_version").get();
|
|
83
|
+
const { application_id: app } = db.prepare("PRAGMA application_id").get();
|
|
84
|
+
if (version > spec.version) throw new Error("Update the memory engine before opening this newer memory database.");
|
|
85
|
+
if ((app !== 0 && app !== spec.applicationId) || (version !== 0 && app !== spec.applicationId) ||
|
|
86
|
+
(version === 0 && db.prepare("SELECT 1 FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%' LIMIT 1").get())) {
|
|
87
|
+
throw new Error("The file is not a compatible memory database.");
|
|
88
|
+
}
|
|
89
|
+
if (version === 0) db.exec(spec.migration + "PRAGMA application_id = " + spec.applicationId);
|
|
90
|
+
if (version < 2) db.exec(spec.migration2);
|
|
91
|
+
if (version < 3) db.exec(spec.migration3);
|
|
92
|
+
if (version < 4) db.exec("PRAGMA user_version = 4");
|
|
93
|
+
});
|
|
94
|
+
db.exec("PRAGMA journal_mode = WAL; PRAGMA secure_delete = ON");
|
|
95
|
+
if (!forceKeyword) {
|
|
96
|
+
try {
|
|
97
|
+
db.exec("CREATE VIRTUAL TABLE IF NOT EXISTS memory_fts USING fts5(id UNINDEXED, title, learning)");
|
|
98
|
+
fts = true;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (!/no such module: fts5/i.test(error.message)) throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
} catch (error) { db.close(); throw error; }
|
|
104
|
+
|
|
105
|
+
const get = id => present(db.prepare("SELECT * FROM memories WHERE id = ?").get(id));
|
|
106
|
+
const dirty = () => db.exec("UPDATE memory_index_state SET revision = revision + 1 WHERE id = 1");
|
|
107
|
+
const erase = (id, revision) => {
|
|
108
|
+
db.prepare("INSERT INTO memory_tombstones VALUES (?,?,?) ON CONFLICT(id) DO NOTHING").run(id, revision, time());
|
|
109
|
+
if (fts) db.prepare("DELETE FROM memory_fts WHERE id=?").run(id);
|
|
110
|
+
db.prepare("DELETE FROM skill_proposals WHERE EXISTS (SELECT 1 FROM json_each(sources) WHERE json_extract(value, '$.id')=?)").run(id);
|
|
111
|
+
db.prepare("DELETE FROM memories WHERE id=?").run(id);
|
|
112
|
+
dirty();
|
|
113
|
+
};
|
|
114
|
+
let extensions;
|
|
115
|
+
try {
|
|
116
|
+
extensions = extension?.({ db, transaction, get, erase, dirty, record, eligibleText, limits, time }) ?? {};
|
|
117
|
+
} catch (error) { db.close(); throw error; }
|
|
118
|
+
return {
|
|
119
|
+
...extensions,
|
|
120
|
+
...proposalActions(db, transaction),
|
|
121
|
+
file,
|
|
122
|
+
close: () => db.close(),
|
|
123
|
+
get,
|
|
124
|
+
list(limit = 20) {
|
|
125
|
+
return db.prepare("SELECT * FROM memories ORDER BY updated_at DESC, id LIMIT ?").all(bounded(limit)).map(present);
|
|
126
|
+
},
|
|
127
|
+
record(value) {
|
|
128
|
+
const item = record(value);
|
|
129
|
+
return transaction(() => {
|
|
130
|
+
if (db.prepare("SELECT count(*) AS count FROM memories").get().count >= limits.records) {
|
|
131
|
+
throw new Error("Memory is full. Forget a record before adding one.");
|
|
132
|
+
}
|
|
133
|
+
const id = randomUUID();
|
|
134
|
+
const now = time();
|
|
135
|
+
db.prepare("INSERT INTO memories (id,type,title,learning,evidence,created_at,updated_at) VALUES (?,?,?,?,?,?,?)")
|
|
136
|
+
.run(id, item.type, item.title, item.learning, JSON.stringify(item.evidence), now, now);
|
|
137
|
+
dirty();
|
|
138
|
+
return get(id);
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
search(query, limit = 20) {
|
|
142
|
+
bounded(limit);
|
|
143
|
+
const tokens = queryTokens(text(query, 240, "Query"));
|
|
144
|
+
if (!tokens.length) return [];
|
|
145
|
+
if (!fts) {
|
|
146
|
+
const terms = tokens.map(() => "instr(lower(title || ' ' || learning), ?) > 0").join(" AND ");
|
|
147
|
+
return db.prepare("SELECT * FROM memories WHERE status != 'archived' AND " + terms + " ORDER BY updated_at DESC, id LIMIT ?")
|
|
148
|
+
.all(...tokens, limit).map(present);
|
|
149
|
+
}
|
|
150
|
+
return transaction(() => {
|
|
151
|
+
const state = db.prepare("SELECT revision, indexed_revision FROM memory_index_state WHERE id=1").get();
|
|
152
|
+
if (state.revision !== state.indexed_revision) {
|
|
153
|
+
db.exec("DELETE FROM memory_fts; INSERT INTO memory_fts SELECT id,title,learning FROM memories;" +
|
|
154
|
+
"UPDATE memory_index_state SET indexed_revision=revision WHERE id=1");
|
|
155
|
+
}
|
|
156
|
+
const expression = tokens.map(token => '"' + token + '"').join(" AND ");
|
|
157
|
+
return db.prepare("SELECT memories.* FROM memory_fts JOIN memories ON memories.id = memory_fts.id " +
|
|
158
|
+
"WHERE memory_fts MATCH ? AND memories.status != 'archived' ORDER BY bm25(memory_fts), memories.id LIMIT ?").all(expression, limit).map(present);
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
verify(id, evidence) {
|
|
162
|
+
return transaction(() => {
|
|
163
|
+
const receipt = reviewEvidence(get(id), evidence);
|
|
164
|
+
db.prepare("UPDATE memories SET status='verified', verification=?, revision=revision+1, updated_at=? WHERE id=?")
|
|
165
|
+
.run(JSON.stringify(receipt), time(), id);
|
|
166
|
+
return get(id);
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
retrieve(query, context, adapter = "coding") {
|
|
170
|
+
if (!safeContext(context) || !hasMemoryAdapter(adapter)) return [];
|
|
171
|
+
const tokens = new Set(queryTokens(query).filter(token => token.length >= 4));
|
|
172
|
+
if (!tokens.size) return [];
|
|
173
|
+
const result = [];
|
|
174
|
+
let remaining = 2400;
|
|
175
|
+
const rows = db.prepare("SELECT * FROM memories WHERE status='verified' AND json_extract(verification, '$.context') = ? AND json_extract(verification, '$.adapter') = ? ORDER BY updated_at DESC, id LIMIT 100").all(context, adapter);
|
|
176
|
+
for (const row of rows) {
|
|
177
|
+
const item = present(row);
|
|
178
|
+
const body = item.title + " " + item.learning;
|
|
179
|
+
if (!eligibleText(body) || !queryTokens(body).some(token => tokens.has(token)) || body.length > remaining) continue;
|
|
180
|
+
result.push(item);
|
|
181
|
+
remaining -= body.length;
|
|
182
|
+
if (result.length === 4) break;
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
},
|
|
186
|
+
archive(id) {
|
|
187
|
+
return transaction(() => {
|
|
188
|
+
const item = get(id);
|
|
189
|
+
if (!item || item.status === "archived") return item;
|
|
190
|
+
db.prepare("UPDATE memories SET status = 'archived', revision = revision + 1, updated_at = ? WHERE id = ?")
|
|
191
|
+
.run(time(), id);
|
|
192
|
+
return get(id);
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
stats() {
|
|
196
|
+
const counts = Object.fromEntries(spec.statuses.map(status => [status, 0]));
|
|
197
|
+
for (const row of db.prepare("SELECT status, count(*) AS count FROM memories GROUP BY status").all()) counts[row.status] = row.count;
|
|
198
|
+
return { schemaVersion: spec.version, search: fts ? "fts5" : "keyword", ...counts };
|
|
199
|
+
},
|
|
200
|
+
forget(id) {
|
|
201
|
+
return transaction(() => {
|
|
202
|
+
const item = get(id);
|
|
203
|
+
if (!item) return false;
|
|
204
|
+
erase(id, item.revision + 1);
|
|
205
|
+
return true;
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
package/src/memory/package.json
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "memory-engine-local",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"private": true,
|
|
5
|
-
"type": "module",
|
|
6
|
-
"description": "Standalone local memory engine (staging package)",
|
|
7
|
-
"engines": {
|
|
8
|
-
"node": ">=22.13.0"
|
|
9
|
-
},
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"exports": {
|
|
12
|
-
".": {
|
|
13
|
-
"types": "./standalone.d.ts",
|
|
14
|
-
"default": "./standalone.js"
|
|
15
|
-
},
|
|
16
|
-
"./bridge": "./bridge.js"
|
|
17
|
-
},
|
|
18
|
-
"bin": {
|
|
19
|
-
"memory-engine": "./bridge-cli.js"
|
|
20
|
-
},
|
|
21
|
-
"files": [
|
|
22
|
-
"standalone.js",
|
|
23
|
-
"standalone.d.ts",
|
|
24
|
-
"local-store.js",
|
|
25
|
-
"contract.json",
|
|
26
|
-
"validation.js",
|
|
27
|
-
"proposals.js",
|
|
28
|
-
"bridge.js",
|
|
29
|
-
"bridge-cli.js",
|
|
30
|
-
"LICENSE",
|
|
31
|
-
"README.md"
|
|
32
|
-
]
|
|
33
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "memory-engine-local",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Standalone local memory engine (staging package)",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22.13.0"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./standalone.d.ts",
|
|
14
|
+
"default": "./standalone.js"
|
|
15
|
+
},
|
|
16
|
+
"./bridge": "./bridge.js"
|
|
17
|
+
},
|
|
18
|
+
"bin": {
|
|
19
|
+
"memory-engine": "./bridge-cli.js"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"standalone.js",
|
|
23
|
+
"standalone.d.ts",
|
|
24
|
+
"local-store.js",
|
|
25
|
+
"contract.json",
|
|
26
|
+
"validation.js",
|
|
27
|
+
"proposals.js",
|
|
28
|
+
"bridge.js",
|
|
29
|
+
"bridge-cli.js",
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"README.md"
|
|
32
|
+
]
|
|
33
|
+
}
|