@persql/context 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/README.md +89 -0
- package/dist/chunk-RMG66FDI.js +339 -0
- package/dist/chunk-RMG66FDI.js.map +1 -0
- package/dist/core.cjs +384 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +188 -0
- package/dist/core.d.ts +188 -0
- package/dist/core.js +49 -0
- package/dist/core.js.map +1 -0
- package/dist/index.cjs +549 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +87 -0
- package/dist/index.d.ts +87 -0
- package/dist/index.js +210 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @persql/context/core — zero-runtime-dependency engine.
|
|
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.
|
|
8
|
+
*
|
|
9
|
+
* Retrieval is lexical: FTS5 with the porter stemmer, BM25-ranked. No vectors.
|
|
10
|
+
*/
|
|
11
|
+
declare const SCHEMA_VERSION = 1;
|
|
12
|
+
declare const TABLE_PREFIX = "ctx_";
|
|
13
|
+
type MemoryKind = "fact" | "episode" | "artifact";
|
|
14
|
+
interface MemoryRow {
|
|
15
|
+
id: string;
|
|
16
|
+
kind: MemoryKind;
|
|
17
|
+
topic: string | null;
|
|
18
|
+
body: string;
|
|
19
|
+
tags: string | null;
|
|
20
|
+
source: string | null;
|
|
21
|
+
createdAt: number;
|
|
22
|
+
supersededBy: string | null;
|
|
23
|
+
}
|
|
24
|
+
interface Entity {
|
|
25
|
+
id: string;
|
|
26
|
+
name: string;
|
|
27
|
+
kind: string | null;
|
|
28
|
+
body: string | null;
|
|
29
|
+
createdAt: number;
|
|
30
|
+
}
|
|
31
|
+
interface Edge {
|
|
32
|
+
id: string;
|
|
33
|
+
src: string;
|
|
34
|
+
rel: string;
|
|
35
|
+
dst: string;
|
|
36
|
+
source: string | null;
|
|
37
|
+
createdAt: number;
|
|
38
|
+
}
|
|
39
|
+
interface RememberInput {
|
|
40
|
+
body: string;
|
|
41
|
+
topic?: string;
|
|
42
|
+
kind?: MemoryKind;
|
|
43
|
+
tags?: string[] | string;
|
|
44
|
+
source?: string;
|
|
45
|
+
supersedes?: string | string[];
|
|
46
|
+
id?: string;
|
|
47
|
+
}
|
|
48
|
+
interface EntityInput {
|
|
49
|
+
name: string;
|
|
50
|
+
kind?: string;
|
|
51
|
+
body?: string;
|
|
52
|
+
id?: string;
|
|
53
|
+
}
|
|
54
|
+
interface EdgeInput {
|
|
55
|
+
src: string;
|
|
56
|
+
rel: string;
|
|
57
|
+
dst: string;
|
|
58
|
+
source?: string;
|
|
59
|
+
}
|
|
60
|
+
interface RecallOptions {
|
|
61
|
+
limit?: number;
|
|
62
|
+
kind?: MemoryKind;
|
|
63
|
+
operator?: "or" | "and";
|
|
64
|
+
mode?: "terms" | "raw";
|
|
65
|
+
sinceMs?: number;
|
|
66
|
+
withinDays?: number;
|
|
67
|
+
includeSuperseded?: boolean;
|
|
68
|
+
}
|
|
69
|
+
interface ListOptions {
|
|
70
|
+
limit?: number;
|
|
71
|
+
kind?: MemoryKind;
|
|
72
|
+
includeSuperseded?: boolean;
|
|
73
|
+
}
|
|
74
|
+
/** The shape an extractor (LLM or the hosted worker) returns from raw text. */
|
|
75
|
+
interface ExtractedContext {
|
|
76
|
+
facts?: Array<{
|
|
77
|
+
body: string;
|
|
78
|
+
topic?: string;
|
|
79
|
+
tags?: string[];
|
|
80
|
+
kind?: MemoryKind;
|
|
81
|
+
}>;
|
|
82
|
+
entities?: EntityInput[];
|
|
83
|
+
edges?: EdgeInput[];
|
|
84
|
+
}
|
|
85
|
+
interface SqlStatement {
|
|
86
|
+
sql: string;
|
|
87
|
+
params: unknown[];
|
|
88
|
+
}
|
|
89
|
+
declare const SCHEMA_SQL: string[];
|
|
90
|
+
declare function buildInit(): SqlStatement[];
|
|
91
|
+
declare function normalizeTags(tags?: string[] | string | null): string | null;
|
|
92
|
+
/**
|
|
93
|
+
* Turn arbitrary user text into a safe FTS5 MATCH expression. `terms` mode
|
|
94
|
+
* (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.
|
|
98
|
+
*/
|
|
99
|
+
declare function toFtsQuery(input: string, opts?: {
|
|
100
|
+
operator?: "or" | "and";
|
|
101
|
+
mode?: "terms" | "raw";
|
|
102
|
+
}): string | null;
|
|
103
|
+
/** Deterministic id for an entity, keyed on its name (case-insensitive). */
|
|
104
|
+
declare function entityId(name: string): string;
|
|
105
|
+
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;
|
|
109
|
+
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
|
+
declare function buildStats(): SqlStatement;
|
|
113
|
+
declare function buildUpsertEntity(e: EntityInput, now: number): SqlStatement;
|
|
114
|
+
declare function buildUpsertEdge(e: EdgeInput, now: number): SqlStatement;
|
|
115
|
+
/** Entities reachable from a seed within `depth` hops (seed excluded). */
|
|
116
|
+
declare function buildNeighborEntities(seedId: string, depth: number, limit: number): SqlStatement;
|
|
117
|
+
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.";
|
|
119
|
+
declare function buildExtractionMessages(raw: string, opts?: {
|
|
120
|
+
hint?: string;
|
|
121
|
+
}): Array<{
|
|
122
|
+
role: "system" | "user";
|
|
123
|
+
content: string;
|
|
124
|
+
}>;
|
|
125
|
+
declare const EXTRACTION_JSON_SCHEMA: {
|
|
126
|
+
readonly type: "object";
|
|
127
|
+
readonly properties: {
|
|
128
|
+
readonly facts: {
|
|
129
|
+
readonly type: "array";
|
|
130
|
+
readonly items: {
|
|
131
|
+
readonly type: "object";
|
|
132
|
+
readonly properties: {
|
|
133
|
+
readonly body: {
|
|
134
|
+
readonly type: "string";
|
|
135
|
+
};
|
|
136
|
+
readonly topic: {
|
|
137
|
+
readonly type: "string";
|
|
138
|
+
};
|
|
139
|
+
readonly tags: {
|
|
140
|
+
readonly type: "array";
|
|
141
|
+
readonly items: {
|
|
142
|
+
readonly type: "string";
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
readonly required: readonly ["body"];
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
readonly entities: {
|
|
150
|
+
readonly type: "array";
|
|
151
|
+
readonly items: {
|
|
152
|
+
readonly type: "object";
|
|
153
|
+
readonly properties: {
|
|
154
|
+
readonly name: {
|
|
155
|
+
readonly type: "string";
|
|
156
|
+
};
|
|
157
|
+
readonly kind: {
|
|
158
|
+
readonly type: "string";
|
|
159
|
+
};
|
|
160
|
+
readonly body: {
|
|
161
|
+
readonly type: "string";
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
readonly required: readonly ["name"];
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
readonly edges: {
|
|
168
|
+
readonly type: "array";
|
|
169
|
+
readonly items: {
|
|
170
|
+
readonly type: "object";
|
|
171
|
+
readonly properties: {
|
|
172
|
+
readonly src: {
|
|
173
|
+
readonly type: "string";
|
|
174
|
+
};
|
|
175
|
+
readonly rel: {
|
|
176
|
+
readonly type: "string";
|
|
177
|
+
};
|
|
178
|
+
readonly dst: {
|
|
179
|
+
readonly type: "string";
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
readonly required: readonly ["src", "rel", "dst"];
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
|
|
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 };
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EXTRACTION_JSON_SCHEMA,
|
|
3
|
+
EXTRACTION_SYSTEM_PROMPT,
|
|
4
|
+
SCHEMA_SQL,
|
|
5
|
+
SCHEMA_VERSION,
|
|
6
|
+
TABLE_PREFIX,
|
|
7
|
+
buildByTag,
|
|
8
|
+
buildEdgesAmong,
|
|
9
|
+
buildExtractionMessages,
|
|
10
|
+
buildForget,
|
|
11
|
+
buildGet,
|
|
12
|
+
buildInit,
|
|
13
|
+
buildNeighborEntities,
|
|
14
|
+
buildRecall,
|
|
15
|
+
buildRecent,
|
|
16
|
+
buildRemember,
|
|
17
|
+
buildStats,
|
|
18
|
+
buildSupersede,
|
|
19
|
+
buildUpsertEdge,
|
|
20
|
+
buildUpsertEntity,
|
|
21
|
+
entityId,
|
|
22
|
+
normalizeTags,
|
|
23
|
+
toFtsQuery
|
|
24
|
+
} from "./chunk-RMG66FDI.js";
|
|
25
|
+
export {
|
|
26
|
+
EXTRACTION_JSON_SCHEMA,
|
|
27
|
+
EXTRACTION_SYSTEM_PROMPT,
|
|
28
|
+
SCHEMA_SQL,
|
|
29
|
+
SCHEMA_VERSION,
|
|
30
|
+
TABLE_PREFIX,
|
|
31
|
+
buildByTag,
|
|
32
|
+
buildEdgesAmong,
|
|
33
|
+
buildExtractionMessages,
|
|
34
|
+
buildForget,
|
|
35
|
+
buildGet,
|
|
36
|
+
buildInit,
|
|
37
|
+
buildNeighborEntities,
|
|
38
|
+
buildRecall,
|
|
39
|
+
buildRecent,
|
|
40
|
+
buildRemember,
|
|
41
|
+
buildStats,
|
|
42
|
+
buildSupersede,
|
|
43
|
+
buildUpsertEdge,
|
|
44
|
+
buildUpsertEntity,
|
|
45
|
+
entityId,
|
|
46
|
+
normalizeTags,
|
|
47
|
+
toFtsQuery
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=core.js.map
|
package/dist/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|