@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/core.d.ts CHANGED
@@ -1,25 +1,32 @@
1
1
  /**
2
2
  * @persql/context/core — zero-runtime-dependency engine.
3
3
  *
4
- * Schema DDL, SQL builders, and the extraction/compaction contracts shared by
5
- * the `@persql/context` SDK (client-side, over `@persql/sdk`) and the hosted
6
- * PerSQL Context worker (server-side, over `@persql/ai`). Keeping both sides on
7
- * the same builders means client-recall and server-recall can never drift.
4
+ * Schema DDL, SQL builders, and the extraction contract shared by the
5
+ * `@persql/context` SDK (client-side, over `@persql/sdk`) and the hosted
6
+ * PerSQL Context worker (server-side, over `@persql/ai`). Structured memories
7
+ * with a name-keyed UPSERT + always-loaded index replace the old episode model.
8
8
  *
9
9
  * Retrieval is lexical: FTS5 with the porter stemmer, BM25-ranked. No vectors.
10
10
  */
11
- declare const SCHEMA_VERSION = 1;
11
+ declare const SCHEMA_VERSION = 2;
12
12
  declare const TABLE_PREFIX = "ctx_";
13
- type MemoryKind = "fact" | "episode" | "artifact";
13
+ type MemoryType = "user" | "feedback" | "project" | "reference";
14
14
  interface MemoryRow {
15
15
  id: string;
16
- kind: MemoryKind;
17
- topic: string | null;
16
+ name: string;
17
+ description: string;
18
+ type: MemoryType;
18
19
  body: string;
19
- tags: string | null;
20
20
  source: string | null;
21
21
  createdAt: number;
22
- supersededBy: string | null;
22
+ updatedAt: number;
23
+ }
24
+ interface IndexRow {
25
+ id: string;
26
+ name: string;
27
+ description: string;
28
+ type: MemoryType;
29
+ updatedAt: number;
23
30
  }
24
31
  interface Entity {
25
32
  id: string;
@@ -37,13 +44,11 @@ interface Edge {
37
44
  createdAt: number;
38
45
  }
39
46
  interface RememberInput {
47
+ name: string;
48
+ description: string;
49
+ type?: MemoryType;
40
50
  body: string;
41
- topic?: string;
42
- kind?: MemoryKind;
43
- tags?: string[] | string;
44
51
  source?: string;
45
- supersedes?: string | string[];
46
- id?: string;
47
52
  }
48
53
  interface EntityInput {
49
54
  name: string;
@@ -59,25 +64,20 @@ interface EdgeInput {
59
64
  }
60
65
  interface RecallOptions {
61
66
  limit?: number;
62
- kind?: MemoryKind;
63
67
  operator?: "or" | "and";
64
68
  mode?: "terms" | "raw";
65
- sinceMs?: number;
66
- withinDays?: number;
67
- includeSuperseded?: boolean;
68
69
  }
69
70
  interface ListOptions {
70
71
  limit?: number;
71
- kind?: MemoryKind;
72
- includeSuperseded?: boolean;
72
+ type?: MemoryType;
73
73
  }
74
- /** The shape an extractor (LLM or the hosted worker) returns from raw text. */
74
+ /** Structured memories extracted by an LLM from raw text. */
75
75
  interface ExtractedContext {
76
- facts?: Array<{
76
+ memories?: Array<{
77
+ name: string;
78
+ description: string;
79
+ type?: MemoryType;
77
80
  body: string;
78
- topic?: string;
79
- tags?: string[];
80
- kind?: MemoryKind;
81
81
  }>;
82
82
  entities?: EntityInput[];
83
83
  edges?: EdgeInput[];
@@ -88,34 +88,39 @@ interface SqlStatement {
88
88
  }
89
89
  declare const SCHEMA_SQL: string[];
90
90
  declare function buildInit(): SqlStatement[];
91
- declare function normalizeTags(tags?: string[] | string | null): string | null;
91
+ /** Returns a non-zero count when the old (topic-based) schema is present. */
92
+ declare function buildDetectLegacySchema(): SqlStatement;
93
+ /**
94
+ * Drop all ctx_ tables + triggers so buildInit() can recreate them cleanly.
95
+ * Run in order — FTS must go first (it references the content table).
96
+ */
97
+ declare function buildDropLegacy(): SqlStatement[];
92
98
  /**
93
99
  * Turn arbitrary user text into a safe FTS5 MATCH expression. `terms` mode
94
100
  * (default) extracts word tokens, drops bare boolean operators, and ORs the
95
- * rest as quoted terms so a caller can paste "invoice OR billing" or just
96
- * "invoice billing" and neither crashes the parser nor injects FTS syntax.
97
- * `raw` mode passes a hand-written FTS expression through untouched.
101
+ * rest as quoted terms. `raw` mode passes a hand-written FTS expression through.
98
102
  */
99
103
  declare function toFtsQuery(input: string, opts?: {
100
104
  operator?: "or" | "and";
101
105
  mode?: "terms" | "raw";
102
106
  }): string | null;
103
- /** Deterministic id for an entity, keyed on its name (case-insensitive). */
104
107
  declare function entityId(name: string): string;
108
+ /** UPSERT by name — same name overwrites description/body/type/source. */
105
109
  declare function buildRemember(input: RememberInput, now: number, id: string): SqlStatement;
106
- declare function buildSupersede(oldId: string, newId: string): SqlStatement;
107
- declare function buildForget(id: string): SqlStatement;
108
- declare function buildGet(id: string): SqlStatement;
110
+ /** Delete a memory by its name slug. */
111
+ declare function buildForget(name: string): SqlStatement;
112
+ /** Fetch one memory row by name, or null if absent. */
113
+ declare function buildGet(name: string): SqlStatement;
114
+ /** Always-loaded index: name+description+type, newest first. */
115
+ declare function buildIndex(opts?: ListOptions): SqlStatement;
116
+ /** BM25 recall across name+description+body. */
109
117
  declare function buildRecall(query: string, opts?: RecallOptions): SqlStatement | null;
110
- declare function buildRecent(opts?: ListOptions): SqlStatement;
111
- declare function buildByTag(tag: string, opts?: ListOptions): SqlStatement;
112
118
  declare function buildStats(): SqlStatement;
113
119
  declare function buildUpsertEntity(e: EntityInput, now: number): SqlStatement;
114
120
  declare function buildUpsertEdge(e: EdgeInput, now: number): SqlStatement;
115
- /** Entities reachable from a seed within `depth` hops (seed excluded). */
116
121
  declare function buildNeighborEntities(seedId: string, depth: number, limit: number): SqlStatement;
117
122
  declare function buildEdgesAmong(ids: string[]): SqlStatement;
118
- declare 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 \u2014 decisions, conventions, constraints, identifiers, preferences. Each: { \"body\": string, \"topic\"?: string, \"tags\"?: string[] }. Prefer specific and stable over transient chatter.\n- entities: named things \u2014 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.";
123
+ declare const EXTRACTION_SYSTEM_PROMPT = "You extract durable, structured memories from a conversation or document for an AI agent that will recall them across sessions.\n\nReturn JSON: { \"memories\": [...], \"entities\": [...], \"edges\": [...] }.\n- memories: facts worth remembering across sessions \u2014 decisions, conventions, constraints, identifiers, preferences, schema details. Each:\n {\n \"name\": string (kebab-case slug, unique key \u2014 e.g. \"auth-provider-choice\", \"todos-table-schema\", \"user-prefers-metrics\"),\n \"description\": string (one-liner shown in the always-loaded index),\n \"type\": \"user\"|\"feedback\"|\"project\"|\"reference\",\n \"body\": string (full markdown content with all relevant detail)\n }\n Types: user=who they are/preferences; feedback=guidance for agents; project=ongoing work/decisions; reference=where to find things.\n- entities: named things \u2014 services, people, tables, repos. Each: { \"name\": string, \"kind\"?: string, \"body\"?: string }\n- edges: relationships between entities. Each: { \"src\": name, \"rel\": string, \"dst\": name }\n\nBe conservative. Use unique, meaningful names. If nothing durable is found, return empty arrays.";
119
124
  declare function buildExtractionMessages(raw: string, opts?: {
120
125
  hint?: string;
121
126
  }): Array<{
@@ -125,25 +130,26 @@ declare function buildExtractionMessages(raw: string, opts?: {
125
130
  declare const EXTRACTION_JSON_SCHEMA: {
126
131
  readonly type: "object";
127
132
  readonly properties: {
128
- readonly facts: {
133
+ readonly memories: {
129
134
  readonly type: "array";
130
135
  readonly items: {
131
136
  readonly type: "object";
132
137
  readonly properties: {
133
- readonly body: {
138
+ readonly name: {
134
139
  readonly type: "string";
135
140
  };
136
- readonly topic: {
141
+ readonly description: {
137
142
  readonly type: "string";
138
143
  };
139
- readonly tags: {
140
- readonly type: "array";
141
- readonly items: {
142
- readonly type: "string";
143
- };
144
+ readonly type: {
145
+ readonly type: "string";
146
+ readonly enum: readonly ["user", "feedback", "project", "reference"];
147
+ };
148
+ readonly body: {
149
+ readonly type: "string";
144
150
  };
145
151
  };
146
- readonly required: readonly ["body"];
152
+ readonly required: readonly ["name", "description", "body"];
147
153
  };
148
154
  };
149
155
  readonly entities: {
@@ -185,4 +191,4 @@ declare const EXTRACTION_JSON_SCHEMA: {
185
191
  };
186
192
  };
187
193
 
188
- export { EXTRACTION_JSON_SCHEMA, EXTRACTION_SYSTEM_PROMPT, type Edge, type EdgeInput, type Entity, type EntityInput, type ExtractedContext, type ListOptions, type MemoryKind, type MemoryRow, type RecallOptions, type RememberInput, SCHEMA_SQL, SCHEMA_VERSION, type SqlStatement, TABLE_PREFIX, buildByTag, buildEdgesAmong, buildExtractionMessages, buildForget, buildGet, buildInit, buildNeighborEntities, buildRecall, buildRecent, buildRemember, buildStats, buildSupersede, buildUpsertEdge, buildUpsertEntity, entityId, normalizeTags, toFtsQuery };
194
+ export { EXTRACTION_JSON_SCHEMA, EXTRACTION_SYSTEM_PROMPT, type Edge, type EdgeInput, type Entity, type EntityInput, type ExtractedContext, type IndexRow, type ListOptions, type MemoryRow, type MemoryType, type RecallOptions, type RememberInput, SCHEMA_SQL, SCHEMA_VERSION, type SqlStatement, TABLE_PREFIX, buildDetectLegacySchema, buildDropLegacy, buildEdgesAmong, buildExtractionMessages, buildForget, buildGet, buildIndex, buildInit, buildNeighborEntities, buildRecall, buildRemember, buildStats, buildUpsertEdge, buildUpsertEntity, entityId, toFtsQuery };
package/dist/core.js CHANGED
@@ -4,46 +4,44 @@ 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
  export {
26
25
  EXTRACTION_JSON_SCHEMA,
27
26
  EXTRACTION_SYSTEM_PROMPT,
28
27
  SCHEMA_SQL,
29
28
  SCHEMA_VERSION,
30
29
  TABLE_PREFIX,
31
- buildByTag,
30
+ buildDetectLegacySchema,
31
+ buildDropLegacy,
32
32
  buildEdgesAmong,
33
33
  buildExtractionMessages,
34
34
  buildForget,
35
35
  buildGet,
36
+ buildIndex,
36
37
  buildInit,
37
38
  buildNeighborEntities,
38
39
  buildRecall,
39
- buildRecent,
40
40
  buildRemember,
41
41
  buildStats,
42
- buildSupersede,
43
42
  buildUpsertEdge,
44
43
  buildUpsertEntity,
45
44
  entityId,
46
- normalizeTags,
47
45
  toFtsQuery
48
46
  };
49
47
  //# sourceMappingURL=core.js.map