@persql/context 0.1.0 → 1.0.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/dist/index.js CHANGED
@@ -4,24 +4,23 @@ import {
4
4
  SCHEMA_SQL,
5
5
  SCHEMA_VERSION,
6
6
  TABLE_PREFIX,
7
- buildByTag,
7
+ buildDetectLegacySchema,
8
+ buildDropLegacy,
8
9
  buildEdgesAmong,
9
10
  buildExtractionMessages,
10
11
  buildForget,
11
12
  buildGet,
13
+ buildIndex,
12
14
  buildInit,
13
15
  buildNeighborEntities,
14
16
  buildRecall,
15
- buildRecent,
16
17
  buildRemember,
17
18
  buildStats,
18
- buildSupersede,
19
19
  buildUpsertEdge,
20
20
  buildUpsertEntity,
21
21
  entityId,
22
- normalizeTags,
23
22
  toFtsQuery
24
- } from "./chunk-RMG66FDI.js";
23
+ } from "./chunk-HRHBXOPL.js";
25
24
 
26
25
  // src/index.ts
27
26
  function uid(prefix) {
@@ -39,44 +38,29 @@ var ContextStore = class {
39
38
  await this.db.batch(buildInit());
40
39
  }
41
40
  // --- writes -------------------------------------------------------------
42
- /** Store one structured fact/episode/artifact. Returns its id. */
41
+ /** UPSERT a named memory. Same name → update; new name → insert. */
43
42
  async remember(input) {
44
- const id = input.id ?? uid("m_");
43
+ const id = uid("m_");
45
44
  const now = Date.now();
46
- const stmts = [
47
- buildRemember({ ...input, source: input.source ?? this.opts.source }, now, id)
48
- ];
49
- const sup = input.supersedes ? Array.isArray(input.supersedes) ? input.supersedes : [input.supersedes] : [];
50
- for (const old of sup) stmts.push(buildSupersede(old, id));
51
- await this.db.transaction(stmts);
52
- return id;
53
- }
54
- /**
55
- * Extract durable context from raw text and store it. The LLM runs on the
56
- * write path; pass `extract` here or to the constructor (or use the hosted
57
- * Context MCP, which runs extraction server-side and meters it).
58
- */
59
- async rememberRaw(raw, opts = {}) {
60
- const extract = opts.extract ?? this.opts.extract;
61
- if (!extract) {
62
- throw new Error(
63
- "ContextStore.rememberRaw needs an extractor: pass { extract } here or to the constructor, or use the hosted PerSQL Context MCP."
64
- );
65
- }
66
- const extracted = await extract(raw, { hint: opts.hint });
67
- return this.write(extracted, { source: opts.source });
45
+ const s = buildRemember(
46
+ { ...input, source: input.source ?? this.opts.source },
47
+ now,
48
+ id
49
+ );
50
+ await this.db.query(s.sql, s.params);
68
51
  }
69
- /** Persist an already-extracted bundle of facts, entities, and edges. */
52
+ /** Persist an already-extracted bundle of memories, entities, and edges. */
70
53
  async write(extracted, opts = {}) {
71
54
  const now = Date.now();
72
55
  const source = opts.source ?? this.opts.source;
73
56
  const stmts = [];
74
- const memoryIds = [];
57
+ const names = [];
75
58
  const entityIds = /* @__PURE__ */ new Set();
76
- for (const f of extracted.facts ?? []) {
59
+ for (const m of extracted.memories ?? []) {
60
+ if (!m?.name || !m?.body) continue;
77
61
  const id = uid("m_");
78
- memoryIds.push(id);
79
- stmts.push(buildRemember({ ...f, source }, now, id));
62
+ names.push(m.name);
63
+ stmts.push(buildRemember({ ...m, source }, now, id));
80
64
  }
81
65
  for (const e of extracted.entities ?? []) {
82
66
  stmts.push(buildUpsertEntity(e, now));
@@ -91,19 +75,14 @@ var ContextStore = class {
91
75
  }
92
76
  if (stmts.length) await this.db.transaction(stmts);
93
77
  return {
94
- memoryIds,
78
+ names,
95
79
  entityIds: [...entityIds],
96
80
  edgeCount: (extracted.edges ?? []).length
97
81
  };
98
82
  }
99
- /** Mark older memory rows as replaced by `newId` (filtered out of recall). */
100
- async supersede(oldIds, newId) {
101
- const ids = Array.isArray(oldIds) ? oldIds : [oldIds];
102
- await this.db.transaction(ids.map((o) => buildSupersede(o, newId)));
103
- }
104
- /** Hard-delete a memory row. */
105
- async forget(id) {
106
- const s = buildForget(id);
83
+ /** Hard-delete a memory row by name. */
84
+ async forget(name) {
85
+ const s = buildForget(name);
107
86
  await this.db.query(s.sql, s.params);
108
87
  }
109
88
  /** Add a relationship between two entities (created if absent). */
@@ -119,35 +98,25 @@ var ContextStore = class {
119
98
  ]);
120
99
  }
121
100
  // --- reads (no AI, pure lexical SQL) ------------------------------------
122
- /** Keyword recall, BM25-ranked, most relevant first. */
123
- async recall(query, opts = {}) {
124
- const resolved = { ...opts };
125
- if (opts.withinDays != null && opts.sinceMs == null) {
126
- resolved.sinceMs = Date.now() - opts.withinDays * 864e5;
127
- }
128
- const s = buildRecall(query, resolved);
129
- if (!s) return [];
101
+ /** Always-loaded index: name+description+type, newest first. */
102
+ async index(opts = {}) {
103
+ const s = buildIndex(opts);
130
104
  const { data } = await this.db.query(s.sql, s.params);
131
105
  return data;
132
106
  }
133
- /** Most recent memory rows. */
134
- async recent(opts = {}) {
135
- const s = buildRecent(opts);
107
+ /** Fetch one memory by name (with full body). */
108
+ async get(name) {
109
+ const s = buildGet(name);
136
110
  const { data } = await this.db.query(s.sql, s.params);
137
- return data;
111
+ return data[0] ?? null;
138
112
  }
139
- /** Memory rows carrying a given tag. */
140
- async byTag(tag, opts = {}) {
141
- const s = buildByTag(tag, opts);
113
+ /** Keyword recall, BM25-ranked, most relevant first. */
114
+ async recall(query, opts = {}) {
115
+ const s = buildRecall(query, opts);
116
+ if (!s) return [];
142
117
  const { data } = await this.db.query(s.sql, s.params);
143
118
  return data;
144
119
  }
145
- /** Fetch one memory row by id, or null. */
146
- async get(id) {
147
- const s = buildGet(id);
148
- const { data } = await this.db.query(s.sql, s.params);
149
- return data[0] ?? null;
150
- }
151
120
  /** The subgraph around an entity: reachable entities + the edges among them. */
152
121
  async neighbors(name, opts = {}) {
153
122
  const seed = name.startsWith("e_") ? name : entityId(name);
@@ -165,14 +134,13 @@ var ContextStore = class {
165
134
  );
166
135
  return { entities, edges };
167
136
  }
168
- /** Counts: current facts, total facts, entities, edges. */
137
+ /** Counts: memories, entities, edges. */
169
138
  async stats() {
170
139
  const s = buildStats();
171
140
  const { data } = await this.db.query(s.sql, s.params);
172
141
  const r = data[0];
173
142
  return {
174
- facts: r?.facts ?? 0,
175
- factsTotal: r?.facts_total ?? 0,
143
+ memories: r?.memories ?? 0,
176
144
  entities: r?.entities ?? 0,
177
145
  edges: r?.edges ?? 0
178
146
  };
@@ -181,6 +149,91 @@ var ContextStore = class {
181
149
  function context(db, opts) {
182
150
  return new ContextStore(db, opts);
183
151
  }
152
+ function memoryTools(store) {
153
+ return [
154
+ {
155
+ type: "function",
156
+ name: "remember_memory",
157
+ description: "Save or update a named memory. Use for facts worth keeping across sessions: schema details, user preferences, decisions. Same name overwrites the previous entry.",
158
+ parameters: {
159
+ type: "object",
160
+ properties: {
161
+ name: {
162
+ type: "string",
163
+ description: "Short kebab-case key, e.g. 'billing-preference'"
164
+ },
165
+ description: {
166
+ type: "string",
167
+ description: "One-line summary shown in the memory index"
168
+ },
169
+ type: {
170
+ type: "string",
171
+ enum: ["user", "feedback", "project", "reference"],
172
+ description: "Memory category"
173
+ },
174
+ body: {
175
+ type: "string",
176
+ description: "Full content of the memory"
177
+ }
178
+ },
179
+ required: ["name", "description", "body"],
180
+ additionalProperties: false
181
+ },
182
+ invoke: async (input) => {
183
+ await store.remember({
184
+ name: String(input.name),
185
+ description: String(input.description),
186
+ type: input.type ?? "project",
187
+ body: String(input.body)
188
+ });
189
+ return { saved: input.name };
190
+ }
191
+ },
192
+ {
193
+ type: "function",
194
+ name: "recall_memory",
195
+ description: "Search memories by keyword. Returns the most relevant entries (BM25-ranked). Use when the answer might be in memory before querying a live data source.",
196
+ parameters: {
197
+ type: "object",
198
+ properties: {
199
+ query: {
200
+ type: "string",
201
+ description: "Keywords to search for"
202
+ },
203
+ limit: {
204
+ type: "number",
205
+ description: "Max results (default 10)"
206
+ }
207
+ },
208
+ required: ["query"],
209
+ additionalProperties: false
210
+ },
211
+ invoke: async (input) => {
212
+ const rows = await store.recall(String(input.query), {
213
+ limit: typeof input.limit === "number" ? input.limit : 10
214
+ });
215
+ return { memories: rows };
216
+ }
217
+ },
218
+ {
219
+ type: "function",
220
+ name: "forget_memory",
221
+ description: "Delete a saved memory by name.",
222
+ parameters: {
223
+ type: "object",
224
+ properties: {
225
+ name: { type: "string", description: "Name of the memory to delete" }
226
+ },
227
+ required: ["name"],
228
+ additionalProperties: false
229
+ },
230
+ invoke: async (input) => {
231
+ await store.forget(String(input.name));
232
+ return { deleted: input.name };
233
+ }
234
+ }
235
+ ];
236
+ }
184
237
  export {
185
238
  ContextStore,
186
239
  EXTRACTION_JSON_SCHEMA,
@@ -188,23 +241,23 @@ export {
188
241
  SCHEMA_SQL,
189
242
  SCHEMA_VERSION,
190
243
  TABLE_PREFIX,
191
- buildByTag,
244
+ buildDetectLegacySchema,
245
+ buildDropLegacy,
192
246
  buildEdgesAmong,
193
247
  buildExtractionMessages,
194
248
  buildForget,
195
249
  buildGet,
250
+ buildIndex,
196
251
  buildInit,
197
252
  buildNeighborEntities,
198
253
  buildRecall,
199
- buildRecent,
200
254
  buildRemember,
201
255
  buildStats,
202
- buildSupersede,
203
256
  buildUpsertEdge,
204
257
  buildUpsertEntity,
205
258
  context,
206
259
  entityId,
207
- normalizeTags,
260
+ memoryTools,
208
261
  toFtsQuery
209
262
  };
210
263
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @persql/context — shared, structured agent context on the PerSQL substrate.\n *\n * One store, readable and writable from every agent surface PerSQL speaks\n * (SDK, MCP, published endpoints, CLI). Reads and structured writes are plain\n * SQL over `@persql/sdk` — cheap, exact, lexical (FTS5). The raw-dump extract\n * path puts the LLM on the *write* side: file once, recall by keyword forever.\n */\n\nimport type { PerSQLDatabase } from \"@persql/sdk\";\nimport {\n buildInit,\n buildRemember,\n buildSupersede,\n buildForget,\n buildGet,\n buildRecall,\n buildRecent,\n buildByTag,\n buildStats,\n buildUpsertEntity,\n buildUpsertEdge,\n buildNeighborEntities,\n buildEdgesAmong,\n entityId,\n type RememberInput,\n type RecallOptions,\n type ListOptions,\n type MemoryRow,\n type EntityInput,\n type Edge,\n type Entity,\n type ExtractedContext,\n type SqlStatement,\n} from \"./core\";\n\nexport * from \"./core\";\n\n/** Bring-your-own-LLM extractor, or use the hosted PerSQL Context worker. */\nexport type Extractor = (\n raw: string,\n ctx: { hint?: string }\n) => Promise<ExtractedContext>;\n\nexport interface ContextStoreOptions {\n /** Default extractor for `rememberRaw` (model-agnostic; you supply it). */\n extract?: Extractor;\n /** Default provenance label stamped on writes (e.g. \"claude-code\"). */\n source?: string;\n}\n\nexport interface WriteResult {\n memoryIds: string[];\n entityIds: string[];\n edgeCount: number;\n}\n\nexport interface ContextStats {\n facts: number;\n factsTotal: number;\n entities: number;\n edges: number;\n}\n\nfunction uid(prefix: string): string {\n const g = globalThis as { crypto?: { randomUUID?: () => string } };\n const r =\n g.crypto?.randomUUID?.() ??\n `${Date.now().toString(36)}-${Math.floor(Math.random() * 1e9).toString(36)}`;\n return prefix + r;\n}\n\nexport class ContextStore {\n constructor(\n private readonly db: PerSQLDatabase,\n private readonly opts: ContextStoreOptions = {}\n ) {}\n\n /** Create the schema (idempotent). Run once before first use. */\n async init(): Promise<void> {\n await this.db.batch(buildInit());\n }\n\n // --- writes -------------------------------------------------------------\n\n /** Store one structured fact/episode/artifact. Returns its id. */\n async remember(input: RememberInput): Promise<string> {\n const id = input.id ?? uid(\"m_\");\n const now = Date.now();\n const stmts: SqlStatement[] = [\n buildRemember({ ...input, source: input.source ?? this.opts.source }, now, id),\n ];\n const sup = input.supersedes\n ? Array.isArray(input.supersedes)\n ? input.supersedes\n : [input.supersedes]\n : [];\n for (const old of sup) stmts.push(buildSupersede(old, id));\n await this.db.transaction(stmts);\n return id;\n }\n\n /**\n * Extract durable context from raw text and store it. The LLM runs on the\n * write path; pass `extract` here or to the constructor (or use the hosted\n * Context MCP, which runs extraction server-side and meters it).\n */\n async rememberRaw(\n raw: string,\n opts: { extract?: Extractor; hint?: string; source?: string } = {}\n ): Promise<WriteResult> {\n const extract = opts.extract ?? this.opts.extract;\n if (!extract) {\n throw new Error(\n \"ContextStore.rememberRaw needs an extractor: pass { extract } here or to the constructor, or use the hosted PerSQL Context MCP.\"\n );\n }\n const extracted = await extract(raw, { hint: opts.hint });\n return this.write(extracted, { source: opts.source });\n }\n\n /** Persist an already-extracted bundle of facts, entities, and edges. */\n async write(\n extracted: ExtractedContext,\n opts: { source?: string } = {}\n ): Promise<WriteResult> {\n const now = Date.now();\n const source = opts.source ?? this.opts.source;\n const stmts: SqlStatement[] = [];\n const memoryIds: string[] = [];\n const entityIds = new Set<string>();\n\n for (const f of extracted.facts ?? []) {\n const id = uid(\"m_\");\n memoryIds.push(id);\n stmts.push(buildRemember({ ...f, source }, now, id));\n }\n for (const e of extracted.entities ?? []) {\n stmts.push(buildUpsertEntity(e, now));\n entityIds.add(e.id ?? entityId(e.name));\n }\n for (const ed of extracted.edges ?? []) {\n // Ensure both endpoints exist before linking them.\n stmts.push(buildUpsertEntity({ name: ed.src }, now));\n stmts.push(buildUpsertEntity({ name: ed.dst }, now));\n stmts.push(buildUpsertEdge({ ...ed, source: ed.source ?? source }, now));\n entityIds.add(entityId(ed.src));\n entityIds.add(entityId(ed.dst));\n }\n\n if (stmts.length) await this.db.transaction(stmts);\n return {\n memoryIds,\n entityIds: [...entityIds],\n edgeCount: (extracted.edges ?? []).length,\n };\n }\n\n /** Mark older memory rows as replaced by `newId` (filtered out of recall). */\n async supersede(oldIds: string | string[], newId: string): Promise<void> {\n const ids = Array.isArray(oldIds) ? oldIds : [oldIds];\n await this.db.transaction(ids.map((o) => buildSupersede(o, newId)));\n }\n\n /** Hard-delete a memory row. */\n async forget(id: string): Promise<void> {\n const s = buildForget(id);\n await this.db.query(s.sql, s.params);\n }\n\n /** Add a relationship between two entities (created if absent). */\n async link(\n src: string,\n rel: string,\n dst: string,\n opts: { source?: string } = {}\n ): Promise<void> {\n const now = Date.now();\n await this.db.transaction([\n buildUpsertEntity({ name: src }, now),\n buildUpsertEntity({ name: dst }, now),\n buildUpsertEdge(\n { src, rel, dst, source: opts.source ?? this.opts.source },\n now\n ),\n ]);\n }\n\n // --- reads (no AI, pure lexical SQL) ------------------------------------\n\n /** Keyword recall, BM25-ranked, most relevant first. */\n async recall(query: string, opts: RecallOptions = {}): Promise<MemoryRow[]> {\n const resolved: RecallOptions = { ...opts };\n if (opts.withinDays != null && opts.sinceMs == null) {\n resolved.sinceMs = Date.now() - opts.withinDays * 86_400_000;\n }\n const s = buildRecall(query, resolved);\n if (!s) return [];\n const { data } = await this.db.query<MemoryRow>(s.sql, s.params);\n return data;\n }\n\n /** Most recent memory rows. */\n async recent(opts: ListOptions = {}): Promise<MemoryRow[]> {\n const s = buildRecent(opts);\n const { data } = await this.db.query<MemoryRow>(s.sql, s.params);\n return data;\n }\n\n /** Memory rows carrying a given tag. */\n async byTag(tag: string, opts: ListOptions = {}): Promise<MemoryRow[]> {\n const s = buildByTag(tag, opts);\n const { data } = await this.db.query<MemoryRow>(s.sql, s.params);\n return data;\n }\n\n /** Fetch one memory row by id, or null. */\n async get(id: string): Promise<MemoryRow | null> {\n const s = buildGet(id);\n const { data } = await this.db.query<MemoryRow>(s.sql, s.params);\n return data[0] ?? null;\n }\n\n /** The subgraph around an entity: reachable entities + the edges among them. */\n async neighbors(\n name: string,\n opts: { depth?: number; limit?: number } = {}\n ): Promise<{ entities: Entity[]; edges: Edge[] }> {\n const seed = name.startsWith(\"e_\") ? name : entityId(name);\n const depth = Math.min(Math.max(opts.depth ?? 1, 1), 4);\n const entStmt = buildNeighborEntities(seed, depth, opts.limit ?? 50);\n const { data: entities } = await this.db.query<Entity>(\n entStmt.sql,\n entStmt.params\n );\n const ids = [seed, ...entities.map((e) => e.id)];\n const edgeStmt = buildEdgesAmong(ids);\n const { data: edges } = await this.db.query<Edge>(\n edgeStmt.sql,\n edgeStmt.params\n );\n return { entities, edges };\n }\n\n /** Counts: current facts, total facts, entities, edges. */\n async stats(): Promise<ContextStats> {\n const s = buildStats();\n const { data } = await this.db.query<{\n facts: number;\n facts_total: number;\n entities: number;\n edges: number;\n }>(s.sql, s.params);\n const r = data[0];\n return {\n facts: r?.facts ?? 0,\n factsTotal: r?.facts_total ?? 0,\n entities: r?.entities ?? 0,\n edges: r?.edges ?? 0,\n };\n }\n}\n\n/** Wrap a PerSQL database handle as a shared context store. */\nexport function context(\n db: PerSQLDatabase,\n opts?: ContextStoreOptions\n): ContextStore {\n return new ContextStore(db, opts);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAgEA,SAAS,IAAI,QAAwB;AACnC,QAAM,IAAI;AACV,QAAM,IACJ,EAAE,QAAQ,aAAa,KACvB,GAAG,KAAK,IAAI,EAAE,SAAS,EAAE,CAAC,IAAI,KAAK,MAAM,KAAK,OAAO,IAAI,GAAG,EAAE,SAAS,EAAE,CAAC;AAC5E,SAAO,SAAS;AAClB;AAEO,IAAM,eAAN,MAAmB;AAAA,EACxB,YACmB,IACA,OAA4B,CAAC,GAC9C;AAFiB;AACA;AAAA,EAChB;AAAA;AAAA,EAGH,MAAM,OAAsB;AAC1B,UAAM,KAAK,GAAG,MAAM,UAAU,CAAC;AAAA,EACjC;AAAA;AAAA;AAAA,EAKA,MAAM,SAAS,OAAuC;AACpD,UAAM,KAAK,MAAM,MAAM,IAAI,IAAI;AAC/B,UAAM,MAAM,KAAK,IAAI;AACrB,UAAM,QAAwB;AAAA,MAC5B,cAAc,EAAE,GAAG,OAAO,QAAQ,MAAM,UAAU,KAAK,KAAK,OAAO,GAAG,KAAK,EAAE;AAAA,IAC/E;AACA,UAAM,MAAM,MAAM,aACd,MAAM,QAAQ,MAAM,UAAU,IAC5B,MAAM,aACN,CAAC,MAAM,UAAU,IACnB,CAAC;AACL,eAAW,OAAO,IAAK,OAAM,KAAK,eAAe,KAAK,EAAE,CAAC;AACzD,UAAM,KAAK,GAAG,YAAY,KAAK;AAC/B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,YACJ,KACA,OAAgE,CAAC,GAC3C;AACtB,UAAM,UAAU,KAAK,WAAW,KAAK,K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MAAM;AAClB,UAAM,IAAI,KAAK,CAAC;AAChB,WAAO;AAAA,MACL,OAAO,GAAG,SAAS;AAAA,MACnB,YAAY,GAAG,eAAe;AAAA,MAC9B,UAAU,GAAG,YAAY;AAAA,MACzB,OAAO,GAAG,SAAS;AAAA,IACrB;AAAA,EACF;AACF;AAGO,SAAS,QACd,IACA,MACc;AACd,SAAO,IAAI,aAAa,IAAI,IAAI;AAClC;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @persql/context — shared, structured agent context on the PerSQL substrate.\n *\n * Structured memories with name+description+type+body, UPSERT by name, and\n * an always-loadable index replace the old episode/fact model. One store per\n * project; every agent surface can read and write it over `@persql/sdk`.\n */\n\nimport type { PerSQLDatabase } from \"@persql/sdk\";\nimport {\n buildInit,\n buildRemember,\n buildForget,\n buildGet,\n buildIndex,\n buildRecall,\n buildStats,\n buildUpsertEntity,\n buildUpsertEdge,\n buildNeighborEntities,\n buildEdgesAmong,\n entityId,\n type MemoryType,\n type RememberInput,\n type RecallOptions,\n type ListOptions,\n type MemoryRow,\n type IndexRow,\n type EntityInput,\n type Edge,\n type Entity,\n type ExtractedContext,\n type SqlStatement,\n} from \"./core\";\n\nexport * from \"./core\";\n\nexport interface ContextStoreOptions {\n /** Default provenance label stamped on writes (e.g. \"claude-code\"). */\n source?: string;\n}\n\nexport interface WriteResult {\n names: string[];\n entityIds: string[];\n edgeCount: number;\n}\n\nexport interface ContextStats {\n memories: number;\n entities: number;\n edges: number;\n}\n\nfunction uid(prefix: string): string {\n const g = globalThis as { crypto?: { randomUUID?: () => string } };\n const r =\n g.crypto?.randomUUID?.() ??\n `${Date.now().toString(36)}-${Math.floor(Math.random() * 1e9).toString(36)}`;\n return prefix + r;\n}\n\nexport class ContextStore {\n constructor(\n private readonly db: PerSQLDatabase,\n private readonly opts: ContextStoreOptions = {}\n ) {}\n\n /** Create the schema (idempotent). Run once before first use. */\n async init(): Promise<void> {\n await this.db.batch(buildInit());\n }\n\n // --- writes -------------------------------------------------------------\n\n /** UPSERT a named memory. Same name → update; new name → insert. */\n async remember(input: RememberInput): Promise<void> {\n const id = uid(\"m_\");\n const now = Date.now();\n const s = buildRemember(\n { ...input, source: input.source ?? this.opts.source },\n now,\n id\n );\n await this.db.query(s.sql, s.params);\n }\n\n /** Persist an already-extracted bundle of memories, entities, and edges. */\n async write(\n extracted: ExtractedContext,\n opts: { source?: string } = {}\n ): Promise<WriteResult> {\n const now = Date.now();\n const source = opts.source ?? this.opts.source;\n const stmts: SqlStatement[] = [];\n const names: string[] = [];\n const entityIds = new Set<string>();\n\n for (const m of extracted.memories ?? []) {\n if (!m?.name || !m?.body) continue;\n const id = uid(\"m_\");\n names.push(m.name);\n stmts.push(buildRemember({ ...m, source }, now, id));\n }\n for (const e of extracted.entities ?? []) {\n stmts.push(buildUpsertEntity(e, now));\n entityIds.add(e.id ?? entityId(e.name));\n }\n for (const ed of extracted.edges ?? []) {\n stmts.push(buildUpsertEntity({ name: ed.src }, now));\n stmts.push(buildUpsertEntity({ name: ed.dst }, now));\n stmts.push(buildUpsertEdge({ ...ed, source: ed.source ?? source }, now));\n entityIds.add(entityId(ed.src));\n entityIds.add(entityId(ed.dst));\n }\n\n if (stmts.length) await this.db.transaction(stmts);\n return {\n names,\n entityIds: [...entityIds],\n edgeCount: (extracted.edges ?? []).length,\n };\n }\n\n /** Hard-delete a memory row by name. */\n async forget(name: string): Promise<void> {\n const s = buildForget(name);\n await this.db.query(s.sql, s.params);\n }\n\n /** Add a relationship between two entities (created if absent). */\n async link(\n src: string,\n rel: string,\n dst: string,\n opts: { source?: string } = {}\n ): Promise<void> {\n const now = Date.now();\n await this.db.transaction([\n buildUpsertEntity({ name: src }, now),\n buildUpsertEntity({ name: dst }, now),\n buildUpsertEdge(\n { src, rel, dst, source: opts.source ?? this.opts.source },\n now\n ),\n ]);\n }\n\n // --- reads (no AI, pure lexical SQL) ------------------------------------\n\n /** Always-loaded index: name+description+type, newest first. */\n async index(opts: ListOptions = {}): Promise<IndexRow[]> {\n const s = buildIndex(opts);\n const { data } = await this.db.query<IndexRow>(s.sql, s.params);\n return data;\n }\n\n /** Fetch one memory by name (with full body). */\n async get(name: string): Promise<MemoryRow | null> {\n const s = buildGet(name);\n const { data } = await this.db.query<MemoryRow>(s.sql, s.params);\n return data[0] ?? null;\n }\n\n /** Keyword recall, BM25-ranked, most relevant first. */\n async recall(query: string, opts: RecallOptions = {}): Promise<MemoryRow[]> {\n const s = buildRecall(query, opts);\n if (!s) return [];\n const { data } = await this.db.query<MemoryRow>(s.sql, s.params);\n return data;\n }\n\n /** The subgraph around an entity: reachable entities + the edges among them. */\n async neighbors(\n name: string,\n opts: { depth?: number; limit?: number } = {}\n ): Promise<{ entities: Entity[]; edges: Edge[] }> {\n const seed = name.startsWith(\"e_\") ? name : entityId(name);\n const depth = Math.min(Math.max(opts.depth ?? 1, 1), 4);\n const entStmt = buildNeighborEntities(seed, depth, opts.limit ?? 50);\n const { data: entities } = await this.db.query<Entity>(\n entStmt.sql,\n entStmt.params\n );\n const ids = [seed, ...entities.map((e) => e.id)];\n const edgeStmt = buildEdgesAmong(ids);\n const { data: edges } = await this.db.query<Edge>(\n edgeStmt.sql,\n edgeStmt.params\n );\n return { entities, edges };\n }\n\n /** Counts: memories, entities, edges. */\n async stats(): Promise<ContextStats> {\n const s = buildStats();\n const { data } = await this.db.query<{\n memories: number;\n entities: number;\n edges: number;\n }>(s.sql, s.params);\n const r = data[0];\n return {\n memories: r?.memories ?? 0,\n entities: r?.entities ?? 0,\n edges: r?.edges ?? 0,\n };\n }\n}\n\n/** Wrap a PerSQL database handle as a shared context store. */\nexport function context(\n db: PerSQLDatabase,\n opts?: ContextStoreOptions\n): ContextStore {\n return new ContextStore(db, opts);\n}\n\n// ---------------------------------------------------------------------------\n// Framework adapters\n// ---------------------------------------------------------------------------\n\n/** Tool definition shape for the OpenAI Agents SDK (`@openai/agents`). */\nexport interface MemoryTool {\n type: \"function\";\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n invoke: (input: Record<string, unknown>) => Promise<unknown>;\n}\n\n/**\n * Returns three memory tools ready to spread into an OpenAI Agents SDK\n * `Agent({ tools })` call: `remember_memory`, `recall_memory`, `forget_memory`.\n *\n * The store must already be initialised (`await store.init()`) before the\n * first tool call.\n */\nexport function memoryTools(store: ContextStore): MemoryTool[] {\n return [\n {\n type: \"function\",\n name: \"remember_memory\",\n description:\n \"Save or update a named memory. Use for facts worth keeping across sessions: schema details, user preferences, decisions. Same name overwrites the previous entry.\",\n parameters: {\n type: \"object\",\n properties: {\n name: {\n type: \"string\",\n description: \"Short kebab-case key, e.g. 'billing-preference'\",\n },\n description: {\n type: \"string\",\n description: \"One-line summary shown in the memory index\",\n },\n type: {\n type: \"string\",\n enum: [\"user\", \"feedback\", \"project\", \"reference\"],\n description: \"Memory category\",\n },\n body: {\n type: \"string\",\n description: \"Full content of the memory\",\n },\n },\n required: [\"name\", \"description\", \"body\"],\n additionalProperties: false,\n },\n invoke: async (input) => {\n await store.remember({\n name: String(input.name),\n description: String(input.description),\n type: (input.type as MemoryType) ?? \"project\",\n body: String(input.body),\n });\n return { saved: input.name };\n },\n },\n {\n type: \"function\",\n name: \"recall_memory\",\n description:\n \"Search memories by keyword. Returns the most relevant entries (BM25-ranked). Use when the answer might be in memory before querying a live data source.\",\n parameters: {\n type: \"object\",\n properties: {\n query: {\n type: \"string\",\n description: \"Keywords to search for\",\n },\n limit: {\n type: \"number\",\n description: \"Max results (default 10)\",\n },\n },\n required: [\"query\"],\n additionalProperties: false,\n },\n invoke: async (input) => {\n const rows = await store.recall(String(input.query), {\n limit: typeof input.limit === \"number\" ? input.limit : 10,\n });\n return { memories: rows };\n },\n },\n {\n type: \"function\",\n name: \"forget_memory\",\n description: \"Delete a saved memory by name.\",\n parameters: {\n type: \"object\",\n properties: {\n name: { type: \"string\", description: \"Name of the memory to delete\" },\n },\n required: [\"name\"],\n additionalProperties: false,\n },\n invoke: async (input) => {\n await store.forget(String(input.name));\n return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Aa;AAAA,UACf;AAAA,UACA,OAAO;AAAA,YACL,MAAM;AAAA,YACN,aAAa;AAAA,UACf;AAAA,QACF;AAAA,QACA,UAAU,CAAC,OAAO;AAAA,QAClB,sBAAsB;AAAA,MACxB;AAAA,MACA,QAAQ,OAAO,UAAU;AACvB,cAAM,OAAO,MAAM,MAAM,OAAO,OAAO,MAAM,KAAK,GAAG;AAAA,UACnD,OAAO,OAAO,MAAM,UAAU,WAAW,MAAM,QAAQ;AAAA,QACzD,CAAC;AACD,eAAO,EAAE,UAAU,KAAK;AAAA,MAC1B;AAAA,IACF;AAAA,IACA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,aAAa;AAAA,MACb,YAAY;AAAA,QACV,MAAM;AAAA,QACN,YAAY;AAAA,UACV,MAAM,EAAE,MAAM,UAAU,aAAa,+BAA+B;AAAA,QACtE;AAAA,QACA,UAAU,CAAC,MAAM;AAAA,QACjB,sBAAsB;AAAA,MACxB;AAAA,MACA,QAAQ,OAAO,UAAU;AACvB,cAAM,MAAM,OAAO,OAAO,MAAM,IAAI,CAAC;AACrC,eAAO,EAAE,SAAS,MAAM,KAAK;AAAA,MAC/B;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@persql/context",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "Shared, structured agent context for PerSQL — durable facts, entities, and a keyword-recallable memory graph in plain SQL. No vector database.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -24,7 +24,7 @@
24
24
  "README.md"
25
25
  ],
26
26
  "peerDependencies": {
27
- "@persql/sdk": "^1.4.0"
27
+ "@persql/sdk": "^1.4.1"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "^22.10.0",
@@ -32,7 +32,7 @@
32
32
  "tsup": "^8.4.0",
33
33
  "typescript": "^5.8.3",
34
34
  "vitest": "^3.1.1",
35
- "@persql/sdk": "1.4.0"
35
+ "@persql/sdk": "1.4.1"
36
36
  },
37
37
  "keywords": [
38
38
  "persql",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core.ts"],"sourcesContent":["/**\n * @persql/context/core — zero-runtime-dependency engine.\n *\n * Schema DDL, SQL builders, and the extraction/compaction contracts shared by\n * the `@persql/context` SDK (client-side, over `@persql/sdk`) and the hosted\n * PerSQL Context worker (server-side, over `@persql/ai`). Keeping both sides on\n * the same builders means client-recall and server-recall can never drift.\n *\n * Retrieval is lexical: FTS5 with the porter stemmer, BM25-ranked. No vectors.\n */\n\nexport const SCHEMA_VERSION = 1;\nexport const TABLE_PREFIX = \"ctx_\";\nconst T = TABLE_PREFIX;\n\nexport type MemoryKind = \"fact\" | \"episode\" | \"artifact\";\n\nexport interface MemoryRow {\n id: string;\n kind: MemoryKind;\n topic: string | null;\n body: string;\n tags: string | null;\n source: string | null;\n createdAt: number;\n supersededBy: string | null;\n}\n\nexport interface Entity {\n id: string;\n name: string;\n kind: string | null;\n body: string | null;\n createdAt: number;\n}\n\nexport interface Edge {\n id: string;\n src: string;\n rel: string;\n dst: string;\n source: string | null;\n createdAt: number;\n}\n\nexport interface RememberInput {\n body: string;\n topic?: string;\n kind?: MemoryKind;\n tags?: string[] | string;\n source?: string;\n supersedes?: string | string[];\n id?: string;\n}\n\nexport interface EntityInput {\n name: string;\n kind?: string;\n body?: string;\n id?: string;\n}\n\nexport interface EdgeInput {\n src: string;\n rel: string;\n dst: string;\n source?: string;\n}\n\nexport interface RecallOptions {\n limit?: number;\n kind?: MemoryKind;\n operator?: \"or\" | \"and\";\n mode?: \"terms\" | \"raw\";\n sinceMs?: number;\n withinDays?: number;\n includeSuperseded?: boolean;\n}\n\nexport interface ListOptions {\n limit?: number;\n kind?: MemoryKind;\n includeSuperseded?: boolean;\n}\n\n/** The shape an extractor (LLM or the hosted worker) returns from raw text. */\nexport interface ExtractedContext {\n facts?: Array<{ body: string; topic?: string; tags?: string[]; kind?: MemoryKind }>;\n entities?: EntityInput[];\n edges?: EdgeInput[];\n}\n\nexport interface SqlStatement {\n sql: string;\n params: unknown[];\n}\n\n// ---------------------------------------------------------------------------\n// Schema\n// ---------------------------------------------------------------------------\n\nexport const SCHEMA_SQL: string[] = [\n `CREATE TABLE IF NOT EXISTS ${T}memory (\n id TEXT PRIMARY KEY,\n kind TEXT NOT NULL DEFAULT 'fact',\n topic TEXT,\n body TEXT NOT NULL,\n tags TEXT,\n source TEXT,\n created_at INTEGER NOT NULL,\n superseded_by TEXT\n )`,\n `CREATE INDEX IF NOT EXISTS ${T}memory_kind ON ${T}memory(kind)`,\n `CREATE INDEX IF NOT EXISTS ${T}memory_created ON ${T}memory(created_at)`,\n `CREATE INDEX IF NOT EXISTS ${T}memory_superseded ON ${T}memory(superseded_by)`,\n // External-content FTS index: body text is not duplicated, only indexed.\n // Porter stemmer so \"invoice\" recalls \"invoicing\" without embeddings.\n `CREATE VIRTUAL TABLE IF NOT EXISTS ${T}memory_fts USING fts5(\n topic, body, tags,\n content='${T}memory', content_rowid='rowid',\n tokenize='porter unicode61'\n )`,\n `CREATE TRIGGER IF NOT EXISTS ${T}memory_ai AFTER INSERT ON ${T}memory BEGIN\n INSERT INTO ${T}memory_fts(rowid, topic, body, tags)\n VALUES (new.rowid, new.topic, new.body, new.tags);\n END`,\n `CREATE TRIGGER IF NOT EXISTS ${T}memory_ad AFTER DELETE ON ${T}memory BEGIN\n INSERT INTO ${T}memory_fts(${T}memory_fts, rowid, topic, body, tags)\n VALUES ('delete', old.rowid, old.topic, old.body, old.tags);\n END`,\n `CREATE TRIGGER IF NOT EXISTS ${T}memory_au AFTER UPDATE ON ${T}memory BEGIN\n INSERT INTO ${T}memory_fts(${T}memory_fts, rowid, topic, body, tags)\n VALUES ('delete', old.rowid, old.topic, old.body, old.tags);\n INSERT INTO ${T}memory_fts(rowid, topic, body, tags)\n VALUES (new.rowid, new.topic, new.body, new.tags);\n END`,\n `CREATE TABLE IF NOT EXISTS ${T}entity (\n id TEXT PRIMARY KEY,\n name TEXT NOT NULL,\n kind TEXT,\n body TEXT,\n created_at INTEGER NOT NULL\n )`,\n `CREATE TABLE IF NOT EXISTS ${T}edge (\n id TEXT PRIMARY KEY,\n src TEXT NOT NULL,\n rel TEXT NOT NULL,\n dst TEXT NOT NULL,\n source TEXT,\n created_at INTEGER NOT NULL\n )`,\n `CREATE INDEX IF NOT EXISTS ${T}edge_src ON ${T}edge(src)`,\n `CREATE INDEX IF NOT EXISTS ${T}edge_dst ON ${T}edge(dst)`,\n];\n\nexport function buildInit(): SqlStatement[] {\n return SCHEMA_SQL.map((sql) => ({ sql, params: [] }));\n}\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nconst FTS_OPERATORS = new Set([\"and\", \"or\", \"not\", \"near\"]);\n\nexport function normalizeTags(tags?: string[] | string | null): string | null {\n if (!tags) return null;\n const arr = Array.isArray(tags) ? tags : String(tags).split(/[,\\s]+/);\n const norm = Array.from(\n new Set(arr.map((t) => t.trim().toLowerCase()).filter(Boolean))\n );\n return norm.length ? norm.join(\" \") : null;\n}\n\n/**\n * Turn arbitrary user text into a safe FTS5 MATCH expression. `terms` mode\n * (default) extracts word tokens, drops bare boolean operators, and ORs the\n * rest as quoted terms — so a caller can paste \"invoice OR billing\" or just\n * \"invoice billing\" and neither crashes the parser nor injects FTS syntax.\n * `raw` mode passes a hand-written FTS expression through untouched.\n */\nexport function toFtsQuery(\n input: string,\n opts: { operator?: \"or\" | \"and\"; mode?: \"terms\" | \"raw\" } = {}\n): string | null {\n if (opts.mode === \"raw\") return input.trim() || null;\n const tokens = (input.match(/[\\p{L}\\p{N}_]+/gu) ?? []).filter(\n (t) => !FTS_OPERATORS.has(t.toLowerCase())\n );\n if (!tokens.length) return null;\n const joiner = opts.operator === \"and\" ? \" AND \" : \" OR \";\n return tokens.map((t) => `\"${t}\"`).join(joiner);\n}\n\nfunction slugId(prefix: string, s: string): string {\n const base = s\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, \"-\")\n .replace(/^-+|-+$/g, \"\")\n .slice(0, 96);\n return prefix + (base || \"x\");\n}\n\n/** Deterministic id for an entity, keyed on its name (case-insensitive). */\nexport function entityId(name: string): string {\n return slugId(\"e_\", name);\n}\n\nfunction resolveEntityRef(ref: string): string {\n return ref.startsWith(\"e_\") ? ref : entityId(ref);\n}\n\nfunction edgeId(srcId: string, rel: string, dstId: string): string {\n return slugId(\"x_\", `${srcId}|${rel}|${dstId}`);\n}\n\nconst MEMORY_COLS =\n \"id, kind, topic, body, tags, source, created_at AS createdAt, superseded_by AS supersededBy\";\n\n// ---------------------------------------------------------------------------\n// Memory builders\n// ---------------------------------------------------------------------------\n\nexport function buildRemember(\n input: RememberInput,\n now: number,\n id: string\n): SqlStatement {\n return {\n sql: `INSERT INTO ${T}memory (id, kind, topic, body, tags, source, created_at, superseded_by)\n VALUES (?, ?, ?, ?, ?, ?, ?, NULL)`,\n params: [\n id,\n input.kind ?? \"fact\",\n input.topic ?? null,\n input.body,\n normalizeTags(input.tags),\n input.source ?? null,\n now,\n ],\n };\n}\n\nexport function buildSupersede(oldId: string, newId: string): SqlStatement {\n return {\n sql: `UPDATE ${T}memory SET superseded_by = ? WHERE id = ? AND superseded_by IS NULL`,\n params: [newId, oldId],\n };\n}\n\nexport function buildForget(id: string): SqlStatement {\n return { sql: `DELETE FROM ${T}memory WHERE id = ?`, params: [id] };\n}\n\nexport function buildGet(id: string): SqlStatement {\n return {\n sql: `SELECT ${MEMORY_COLS} FROM ${T}memory WHERE id = ?`,\n params: [id],\n };\n}\n\nexport function buildRecall(\n query: string,\n opts: RecallOptions = {}\n): SqlStatement | null {\n const match = toFtsQuery(query, { operator: opts.operator, mode: opts.mode });\n if (!match) return null;\n const where: string[] = [`${T}memory_fts MATCH ?`];\n const params: unknown[] = [match];\n if (!opts.includeSuperseded) where.push(\"m.superseded_by IS NULL\");\n if (opts.kind) {\n where.push(\"m.kind = ?\");\n params.push(opts.kind);\n }\n if (opts.sinceMs != null) {\n where.push(\"m.created_at >= ?\");\n params.push(opts.sinceMs);\n }\n params.push(opts.limit ?? 8);\n return {\n sql: `SELECT m.id, m.kind, m.topic, m.body, m.tags, m.source,\n m.created_at AS createdAt, m.superseded_by AS supersededBy\n FROM ${T}memory_fts\n JOIN ${T}memory m ON m.rowid = ${T}memory_fts.rowid\n WHERE ${where.join(\" AND \")}\n ORDER BY ${T}memory_fts.rank\n LIMIT ?`,\n params,\n };\n}\n\nexport function buildRecent(opts: ListOptions = {}): SqlStatement {\n const where: string[] = [];\n const params: unknown[] = [];\n if (!opts.includeSuperseded) where.push(\"superseded_by IS NULL\");\n if (opts.kind) {\n where.push(\"kind = ?\");\n params.push(opts.kind);\n }\n params.push(opts.limit ?? 20);\n return {\n sql: `SELECT ${MEMORY_COLS} FROM ${T}memory\n ${where.length ? \"WHERE \" + where.join(\" AND \") : \"\"}\n ORDER BY created_at DESC\n LIMIT ?`,\n params,\n };\n}\n\nexport function buildByTag(tag: string, opts: ListOptions = {}): SqlStatement {\n const t = tag.trim().toLowerCase();\n const where: string[] = [\"(' ' || COALESCE(tags, '') || ' ') LIKE ?\"];\n const params: unknown[] = [`% ${t} %`];\n if (!opts.includeSuperseded) where.push(\"superseded_by IS NULL\");\n if (opts.kind) {\n where.push(\"kind = ?\");\n params.push(opts.kind);\n }\n params.push(opts.limit ?? 20);\n return {\n sql: `SELECT ${MEMORY_COLS} FROM ${T}memory\n WHERE ${where.join(\" AND \")}\n ORDER BY created_at DESC\n LIMIT ?`,\n params,\n };\n}\n\nexport function buildStats(): SqlStatement {\n return {\n sql: `SELECT\n (SELECT count(*) FROM ${T}memory WHERE superseded_by IS NULL) AS facts,\n (SELECT count(*) FROM ${T}memory) AS facts_total,\n (SELECT count(*) FROM ${T}entity) AS entities,\n (SELECT count(*) FROM ${T}edge) AS edges`,\n params: [],\n };\n}\n\n// ---------------------------------------------------------------------------\n// Graph builders\n// ---------------------------------------------------------------------------\n\nexport function buildUpsertEntity(e: EntityInput, now: number): SqlStatement {\n const id = e.id ?? entityId(e.name);\n return {\n sql: `INSERT INTO ${T}entity (id, name, kind, body, created_at)\n VALUES (?, ?, ?, ?, ?)\n ON CONFLICT(id) DO UPDATE SET\n name = excluded.name,\n kind = COALESCE(excluded.kind, ${T}entity.kind),\n body = COALESCE(excluded.body, ${T}entity.body)`,\n params: [id, e.name, e.kind ?? null, e.body ?? null, now],\n };\n}\n\nexport function buildUpsertEdge(e: EdgeInput, now: number): SqlStatement {\n const srcId = resolveEntityRef(e.src);\n const dstId = resolveEntityRef(e.dst);\n const id = edgeId(srcId, e.rel, dstId);\n return {\n sql: `INSERT INTO ${T}edge (id, src, rel, dst, source, created_at)\n VALUES (?, ?, ?, ?, ?, ?)\n ON CONFLICT(id) DO UPDATE SET\n source = COALESCE(excluded.source, ${T}edge.source)`,\n params: [id, srcId, e.rel, dstId, e.source ?? null, now],\n };\n}\n\n/** Entities reachable from a seed within `depth` hops (seed excluded). */\nexport function buildNeighborEntities(\n seedId: string,\n depth: number,\n limit: number\n): SqlStatement {\n return {\n sql: `WITH RECURSIVE reach(id, depth) AS (\n SELECT ?, 0\n UNION\n SELECT CASE WHEN e.src = reach.id THEN e.dst ELSE e.src END,\n reach.depth + 1\n FROM reach\n JOIN ${T}edge e ON (e.src = reach.id OR e.dst = reach.id)\n WHERE reach.depth < ?\n )\n SELECT en.id, en.name, en.kind, en.body,\n en.created_at AS createdAt, MIN(reach.depth) AS depth\n FROM reach\n JOIN ${T}entity en ON en.id = reach.id\n WHERE reach.depth > 0 AND en.id <> ?\n GROUP BY en.id\n ORDER BY depth, en.name\n LIMIT ?`,\n // The seed reappears at depth >0 via a round-trip on undirected edges;\n // exclude it explicitly so it isn't listed as its own neighbor.\n params: [seedId, depth, seedId, limit],\n };\n}\n\nexport function buildEdgesAmong(ids: string[]): SqlStatement {\n if (!ids.length) {\n return { sql: `SELECT id, src, rel, dst, source, created_at AS createdAt FROM ${T}edge WHERE 0`, params: [] };\n }\n const ph = ids.map(() => \"?\").join(\", \");\n return {\n sql: `SELECT id, src, rel, dst, source, created_at AS createdAt\n FROM ${T}edge\n WHERE src IN (${ph}) AND dst IN (${ph})`,\n params: [...ids, ...ids],\n };\n}\n\n// ---------------------------------------------------------------------------\n// Extraction / compaction contracts (used by the hosted worker + documented\n// for bring-your-own-LLM callers; kept here so the prompt is versioned with\n// the schema it has to populate).\n// ---------------------------------------------------------------------------\n\nexport const EXTRACTION_SYSTEM_PROMPT = `You extract durable, shareable context from raw text for a team of AI agents that will read it later by keyword.\n\nReturn JSON: { \"facts\": [...], \"entities\": [...], \"edges\": [...] }.\n- facts: atomic, self-contained statements worth remembering across sessions — decisions, conventions, constraints, identifiers, preferences. Each: { \"body\": string, \"topic\"?: string, \"tags\"?: string[] }. Prefer specific and stable over transient chatter.\n- entities: named things — services, people, tables, repos, concepts. Each: { \"name\": string, \"kind\"?: string, \"body\"?: short description }.\n- edges: relationships between entities, referencing entity names. Each: { \"src\": name, \"rel\": string, \"dst\": name }.\n\nBe conservative: omit anything ephemeral or low-value. Use lowercase tags. If nothing is worth keeping, return empty arrays.`;\n\nexport function buildExtractionMessages(\n raw: string,\n opts: { hint?: string } = {}\n): Array<{ role: \"system\" | \"user\"; content: string }> {\n return [\n { role: \"system\", content: EXTRACTION_SYSTEM_PROMPT },\n {\n role: \"user\",\n content: (opts.hint ? `Context: ${opts.hint}\\n\\n` : \"\") + raw,\n },\n ];\n}\n\nexport const EXTRACTION_JSON_SCHEMA = {\n type: \"object\",\n properties: {\n facts: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n body: { type: \"string\" },\n topic: { type: \"string\" },\n tags: { type: \"array\", items: { type: \"string\" } },\n },\n required: [\"body\"],\n },\n },\n entities: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n kind: { type: \"string\" },\n body: { type: \"string\" },\n },\n required: [\"name\"],\n },\n },\n edges: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n src: { type: \"string\" },\n rel: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