@sqlite-sync/ai 0.9.0 → 0.9.1

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 CHANGED
@@ -8,7 +8,7 @@ AI agent tools for [@sqlite-sync](https://github.com/krolebord-dev/sqlite-sync)
8
8
  - `createAiDbAccess` — server-side access object living next to the storage; its methods double as an RPC contract for cross-Durable-Object setups.
9
9
  - `createDbTools` — AI SDK v6 `ToolSet` (`getDbSchema` and `queryDb` tools, plus optional `mutateDb`) backed by an `AiDbAccess` or a stub proxying to one.
10
10
 
11
- `queryDb` is strictly read-only: a single `SELECT`/`WITH`/`VALUES` statement, verified against SQLite's `EXPLAIN` bytecode for write opcodes, and executed inside a transaction that is always rolled back. Reads are **not** restricted by table — the agent can query every table in the database file (including sqlite-sync's internal event log), so don't colocate data the agent must not see. Results are capped (default 200 rows, 2000 chars per cell) and report `truncated` so the agent can narrow its query.
11
+ `queryDb` is strictly read-only: a single `SELECT`/`WITH`/`VALUES` statement, verified against SQLite's `EXPLAIN` bytecode for write opcodes, and executed inside a transaction that is always rolled back. By default reads are **not** restricted by table — the agent can query every table in the database file (including sqlite-sync's internal event log), so don't colocate data the agent must not see unless you hide it (see below). Results are capped (default 200 rows, 2000 chars per cell) and report `truncated` so the agent can narrow its query.
12
12
 
13
13
  `mutateDb` is opt-in. It applies `item-created`, `item-updated`, and `item-deleted` CRDT events through sqlite-sync's own-event path, so writes are validated, persisted to the event log, applied locally, and synced normally. It does not run arbitrary write SQL. For `item-created` events, omit `item_id` and `payload.id`; the tool generates ids, injects them into the CRDT events, and returns them as `createdIds`.
14
14
 
@@ -65,3 +65,34 @@ getTools() {
65
65
  The doc skips the internal `tombstone` column and renders enum columns with their allowed values and boolean columns with a `0/1` hint. Table and column descriptions come from `.describe()` on the builders.
66
66
 
67
67
  The generated doc always includes a built-in preamble (after your `overview`) explaining sqlite-sync mechanics — that the database syncs between devices and that the listed tables are read-only views with soft-deleted rows already filtered out. Prefer keeping domain semantics in the schema with `.describe()` and using `context.overview` for app-level notes.
68
+
69
+ ## Limiting what the agent may touch
70
+
71
+ Declare AI access next to the schema. Tables are `"read-write"` unless narrowed:
72
+
73
+ ```ts
74
+ const syncDbSchema = defineSyncSchema({
75
+ tables: {
76
+ todos: t.table({ title: t.text() }),
77
+ // Queryable and documented, never mutated by the agent.
78
+ audit: t.table({ note: t.text() }).ai("read-only"),
79
+ // Not in the doc, and queries that read it are rejected.
80
+ billing: t.table({ card_last4: t.text() }).ai("hidden"),
81
+ },
82
+ migrations,
83
+ });
84
+ ```
85
+
86
+ What each level enforces:
87
+
88
+ - `"read-only"` keeps the table in the schema doc, labelled read-only, and makes `mutateDb` reject events for it.
89
+ - `"hidden"` removes it from the doc and makes `queryDb` reject any statement that reads it. Enforcement works off the root pages a compiled statement opens, so views, aliases, CTEs, subqueries and quoting tricks all resolve to the same check.
90
+
91
+ Access is table-level. There is no per-column setting: SQLite bytecode identifies tables rather than columns, so a column could not be hidden from reads, and blocking writes to a required column would make rows impossible to create. Put data the agent must not touch in its own table.
92
+
93
+ Hiding a table has two side effects worth knowing:
94
+
95
+ - Reads switch to an allow-list of the remaining base tables, so any other table in the same database file (including non-synced tables you created yourself) becomes unreadable too.
96
+ - `change_history` is dropped from the doc and denied. That view reads the raw event log, which holds every dataset's payloads, so it cannot be filtered per table.
97
+
98
+ Names and columns of hidden tables can still be discovered through SQLite's schema introspection in principle; the guard rejects `pragma_*` table-valued functions and reads of `sqlite_schema` while restricted, but treat hiding as row-level protection rather than proof the table does not exist.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CrdtEventType, CrdtStorage, SyncDbSchema } from "@sqlite-sync/core";
1
+ import { AiAccess, CrdtEventType, CrdtStorage, SyncDbSchema } from "@sqlite-sync/core";
2
2
  import { ToolSet } from "ai";
3
3
  //#region src/schema-doc.d.ts
4
4
  type SchemaDocContext = {
@@ -10,6 +10,9 @@ type SchemaDocContext = {
10
10
  * queries; descriptions come from `.describe()` on the table and column builders.
11
11
  * The internal `tombstone` column is omitted.
12
12
  *
13
+ * Tables declared `.ai("hidden")` are left out entirely, and read-only ones are labelled so the
14
+ * agent does not attempt a mutation that would be rejected.
15
+ *
13
16
  * The doc always includes a built-in preamble explaining sqlite-sync mechanics (read-only
14
17
  * views, soft-deletes already filtered) after the consumer's `overview` — consumers only
15
18
  * need to describe their own domain.
@@ -74,11 +77,13 @@ type AiMutationResult = {
74
77
  * (promise-wrapped) and satisfies the tool layer's `DbToolsAccess`.
75
78
  *
76
79
  * `query` enforces read-only (single SELECT/WITH/VALUES statement, no write opcodes, executed
77
- * in a forced-rollback transaction) but reads are not restricted by table — the whole database
78
- * file is in scope for the agent, so don't colocate data the agent must not see.
80
+ * in a forced-rollback transaction). Reads are restricted by table only once the schema declares
81
+ * a table `.ai("hidden")`; until then the whole database file is in scope for the agent, so don't
82
+ * colocate data the agent must not see.
79
83
  *
80
84
  * `mutate` is only present when `createAiDbAccess` receives a CRDT storage. Mutations are CRDT
81
- * events applied through sqlite-sync's normal own-event path, never direct SQL writes.
85
+ * events applied through sqlite-sync's normal own-event path, never direct SQL writes, and are
86
+ * rejected for tables the schema declares read-only or hidden.
82
87
  */
83
88
  type AiDbAccess = {
84
89
  getSchemaDoc(): string;
@@ -96,6 +101,30 @@ declare function createAiDbAccess(opts: {
96
101
  };
97
102
  }): AiDbAccess;
98
103
  //#endregion
104
+ //#region src/policy.d.ts
105
+ type ResolvedTablePolicy = {
106
+ crdtTableName: string;
107
+ baseTableName: string;
108
+ access: AiAccess;
109
+ };
110
+ type ResolvedAiPolicy = {
111
+ /** In schema declaration order, hidden tables included. */
112
+ tables: ResolvedTablePolicy[];
113
+ hasHiddenTables: boolean;
114
+ /** Base table names the agent may read. Only meaningful when `hasHiddenTables` is true. */
115
+ readableBaseTableNames: string[];
116
+ /** Resolves either the crdt or the base table name; anything unknown is "hidden". */
117
+ tableAccess(dataset: string): AiAccess;
118
+ };
119
+ /**
120
+ * Flattens the AI access declared on the schema's table builders into the lookups the doc
121
+ * generator and the enforcement points share, so they cannot disagree. Access is table-level:
122
+ * see {@link AiAccess}.
123
+ */
124
+ declare function resolveAiPolicy(opts: {
125
+ syncDbSchema: SyncDbSchema;
126
+ }): ResolvedAiPolicy;
127
+ //#endregion
99
128
  //#region src/query-guard.d.ts
100
129
  type QueryGuardInput = {
101
130
  sql: string;
@@ -103,7 +132,7 @@ type QueryGuardInput = {
103
132
  };
104
133
  type QueryGuardRejection = {
105
134
  allowed: false;
106
- code: "invalid-statement" | "multi-statement" | "invalid-sql" | "write-detected";
135
+ code: "invalid-statement" | "multi-statement" | "invalid-sql" | "write-detected" | "table-denied";
107
136
  message: string;
108
137
  };
109
138
  type QueryGuardVerdict = {
@@ -115,9 +144,10 @@ declare class QueryGuardError extends Error {
115
144
  }
116
145
  type QueryGuard = {
117
146
  /**
118
- * Statically verifies the query is a read-only single statement. Reads are not restricted
119
- * by table — the whole database file is in scope for the agent, so don't colocate data the
120
- * agent must not see.
147
+ * Statically verifies the query is a read-only single statement, and — when the guard was
148
+ * created with `readableTables` — that it only reads those tables. Without `readableTables`
149
+ * reads are unrestricted: the whole database file is in scope for the agent, so don't
150
+ * colocate data the agent must not see.
121
151
  */
122
152
  check(input: QueryGuardInput): QueryGuardVerdict;
123
153
  /**
@@ -130,6 +160,12 @@ type QueryGuard = {
130
160
  };
131
161
  declare function createQueryGuard(opts: {
132
162
  executor: AiDbExecutor;
163
+ /**
164
+ * Restrict reads to these tables (matched case-insensitively, indexes on them included).
165
+ * Views are resolved through their base tables, so pass base table names. Omit for
166
+ * unrestricted reads.
167
+ */
168
+ readableTables?: readonly string[];
133
169
  }): QueryGuard;
134
170
  /**
135
171
  * Runs `callback` in a transaction that is always rolled back (via a sentinel throw, relying
@@ -160,5 +196,5 @@ type CreateDbToolsOptions = {
160
196
  */
161
197
  declare function createDbTools(opts: CreateDbToolsOptions): ToolSet;
162
198
  //#endregion
163
- export { type AiDbAccess, type AiDbExecuteParams, type AiDbExecutor, type AiMutationEvent, type AiMutationInput, type AiMutationResult, type AiQueryInput, type AiQueryResult, type CreateDbToolsOptions, type DbToolsAccess, type QueryGuard, QueryGuardError, type QueryGuardInput, type QueryGuardRejection, type QueryGuardVerdict, type SchemaDocContext, createAiDbAccess, createDbTools, createQueryGuard, createSchemaDoc, runWithForcedRollback };
199
+ export { type AiDbAccess, type AiDbExecuteParams, type AiDbExecutor, type AiMutationEvent, type AiMutationInput, type AiMutationResult, type AiQueryInput, type AiQueryResult, type CreateDbToolsOptions, type DbToolsAccess, type QueryGuard, QueryGuardError, type QueryGuardInput, type QueryGuardRejection, type QueryGuardVerdict, type ResolvedAiPolicy, type ResolvedTablePolicy, type SchemaDocContext, createAiDbAccess, createDbTools, createQueryGuard, createSchemaDoc, resolveAiPolicy, runWithForcedRollback };
164
200
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,5 +1,32 @@
1
1
  import { CrdtEventValidationError, generateId } from "@sqlite-sync/core";
2
2
  import { jsonSchema, tool } from "ai";
3
+ //#region src/policy.ts
4
+ /**
5
+ * Flattens the AI access declared on the schema's table builders into the lookups the doc
6
+ * generator and the enforcement points share, so they cannot disagree. Access is table-level:
7
+ * see {@link AiAccess}.
8
+ */
9
+ function resolveAiPolicy(opts) {
10
+ const tables = opts.syncDbSchema.tablesConfig.map(({ crdtTableName, baseTableName }) => ({
11
+ crdtTableName,
12
+ baseTableName,
13
+ access: opts.syncDbSchema.tables[crdtTableName].aiAccess
14
+ }));
15
+ const byName = /* @__PURE__ */ new Map();
16
+ for (const table of tables) {
17
+ byName.set(table.crdtTableName, table);
18
+ byName.set(table.baseTableName, table);
19
+ }
20
+ return {
21
+ tables,
22
+ hasHiddenTables: tables.some((table) => table.access === "hidden"),
23
+ readableBaseTableNames: tables.filter((table) => table.access !== "hidden").map((table) => table.baseTableName),
24
+ tableAccess(dataset) {
25
+ return byName.get(dataset)?.access ?? "hidden";
26
+ }
27
+ };
28
+ }
29
+ //#endregion
3
30
  //#region src/query-guard.ts
4
31
  var QueryGuardError = class extends Error {
5
32
  rejection;
@@ -41,12 +68,56 @@ const WRITE_OPCODES = /* @__PURE__ */ new Set([
41
68
  "VDestroy",
42
69
  "Program"
43
70
  ]);
71
+ /**
72
+ * Opcodes that open a b-tree cursor for reading. `p2` is the root page of the table or index
73
+ * and `p3` the database it lives in (0 = main), which is how a statement's real table set is
74
+ * recovered — views are already flattened into their base tables by the time bytecode exists,
75
+ * so aliases, CTEs, subqueries and quoting tricks all resolve here.
76
+ */
77
+ const READ_CURSOR_OPCODES = /* @__PURE__ */ new Set(["OpenRead", "ReopenIdx"]);
78
+ /**
79
+ * Table-valued pragma functions (`pragma_table_list()`, ...) read the schema through a virtual
80
+ * table, so no root page identifies them. They expose names and columns rather than rows, but a
81
+ * hidden table should not be enumerable either, so they are rejected outright when restricted.
82
+ */
83
+ const PRAGMA_FUNCTION_PATTERN = /\bpragma_[a-z_]+\s*\(/i;
44
84
  const READ_STATEMENT_PREFIXES = [
45
85
  "select",
46
86
  "with",
47
87
  "values"
48
88
  ];
49
89
  function createQueryGuard(opts) {
90
+ const readableTables = opts.readableTables ? new Set(opts.readableTables.map((name) => name.toLowerCase())) : null;
91
+ const readableTablesLabel = opts.readableTables?.join(", ") ?? "";
92
+ /**
93
+ * Root page to table name, for every b-tree in the main database. Re-read per query instead of
94
+ * cached: a migration can drop a table and hand its root page to another one, and a stale map
95
+ * would then resolve that page to the wrong (possibly allowed) name.
96
+ */
97
+ function readRootPages() {
98
+ const rows = opts.executor.execute({
99
+ sql: `SELECT "tbl_name", "rootpage" FROM "sqlite_master" WHERE "rootpage" IS NOT NULL AND "rootpage" > 0`,
100
+ parameters: []
101
+ }).rows;
102
+ return new Map(rows.map((row) => [Number(row.rootpage), row.tbl_name]));
103
+ }
104
+ function checkReadableTables(sql, operations) {
105
+ if (!readableTables) return null;
106
+ if (PRAGMA_FUNCTION_PATTERN.test(sql)) return reject("table-denied", `Pragma functions are not available. You can only read these tables: ${readableTablesLabel}.`);
107
+ const cursors = operations.filter((operation) => READ_CURSOR_OPCODES.has(operation.opcode));
108
+ if (cursors.length === 0) return null;
109
+ let rootPages;
110
+ try {
111
+ rootPages = readRootPages();
112
+ } catch {
113
+ return reject("table-denied", "The tables this statement reads could not be verified, so it was rejected.");
114
+ }
115
+ for (const cursor of cursors) {
116
+ const name = Number(cursor.p3) === 0 ? rootPages.get(Number(cursor.p2)) : void 0;
117
+ if (!name || !readableTables.has(name.toLowerCase())) return reject("table-denied", `${name ? `Table "${name}"` : "A table this statement reads"} is not available to you. You can only read these tables: ${readableTablesLabel}.`);
118
+ }
119
+ return null;
120
+ }
50
121
  function reject(code, message) {
51
122
  return {
52
123
  allowed: false,
@@ -70,6 +141,8 @@ function createQueryGuard(opts) {
70
141
  return reject("invalid-sql", `SQL error: ${error instanceof Error ? error.message : String(error)}`);
71
142
  }
72
143
  for (const operation of operations) if (WRITE_OPCODES.has(operation.opcode)) return reject("write-detected", `The statement was rejected because it would modify the database (${operation.opcode}). This tool is strictly read-only; rewrite the query as a pure SELECT.`);
144
+ const denied = checkReadableTables(body, operations);
145
+ if (denied) return denied;
73
146
  return { allowed: true };
74
147
  }
75
148
  return {
@@ -115,6 +188,7 @@ const SCHEMA_DOC_PREAMBLE = [
115
188
  "read-only SQL views; soft-deleted rows are already filtered out, so query them directly",
116
189
  "without any tombstone filtering. Every table has a unique `id` text primary key."
117
190
  ].join("\n");
191
+ const RESTRICTED_READS_NOTE = ["Only the tables documented below are readable. Queries that touch any other table are", "rejected, including the internal sync event log, so there is no change history available."].join("\n");
118
192
  function renderColumn(name, meta) {
119
193
  let line = `- \`${name}\` ${meta.sqlType.toUpperCase()}`;
120
194
  if (!meta.nullable) line += " NOT NULL";
@@ -128,18 +202,26 @@ function renderColumn(name, meta) {
128
202
  * queries; descriptions come from `.describe()` on the table and column builders.
129
203
  * The internal `tombstone` column is omitted.
130
204
  *
205
+ * Tables declared `.ai("hidden")` are left out entirely, and read-only ones are labelled so the
206
+ * agent does not attempt a mutation that would be rejected.
207
+ *
131
208
  * The doc always includes a built-in preamble explaining sqlite-sync mechanics (read-only
132
209
  * views, soft-deletes already filtered) after the consumer's `overview` — consumers only
133
210
  * need to describe their own domain.
134
211
  */
135
212
  function createSchemaDoc(opts) {
213
+ const policy = resolveAiPolicy({ syncDbSchema: opts.syncDbSchema });
136
214
  const sections = [];
137
215
  const overview = opts.context?.overview?.trim();
138
216
  if (overview) sections.push(overview);
139
217
  sections.push(SCHEMA_DOC_PREAMBLE);
218
+ if (policy.hasHiddenTables) sections.push(RESTRICTED_READS_NOTE);
140
219
  for (const [crdtTableName, table] of Object.entries(opts.syncDbSchema.tables)) {
220
+ const access = policy.tableAccess(crdtTableName);
221
+ if (access === "hidden") continue;
141
222
  const lines = [`## ${crdtTableName}`];
142
223
  if (table.description) lines.push("", table.description.trim());
224
+ if (access === "read-only") lines.push("", "Read-only: you can query this table but cannot create, update, or delete its rows.");
143
225
  lines.push("", "Columns:");
144
226
  for (const [name, meta] of Object.entries(table.columns)) {
145
227
  if (name === "tombstone") continue;
@@ -147,7 +229,7 @@ function createSchemaDoc(opts) {
147
229
  }
148
230
  sections.push(lines.join("\n"));
149
231
  }
150
- sections.push(CHANGE_HISTORY_DOC);
232
+ if (!policy.hasHiddenTables) sections.push(CHANGE_HISTORY_DOC);
151
233
  return sections.join("\n\n");
152
234
  }
153
235
  const CHANGE_HISTORY_DOC = [
@@ -175,11 +257,15 @@ function toBase64(bytes) {
175
257
  return btoa(binary);
176
258
  }
177
259
  function createAiDbAccess(opts) {
260
+ const policy = resolveAiPolicy({ syncDbSchema: opts.syncDbSchema });
178
261
  const schemaDoc = createSchemaDoc({
179
262
  syncDbSchema: opts.syncDbSchema,
180
263
  context: opts.context
181
264
  });
182
- const guard = createQueryGuard({ executor: opts.executor });
265
+ const guard = createQueryGuard({
266
+ executor: opts.executor,
267
+ readableTables: policy.hasHiddenTables ? policy.readableBaseTableNames : void 0
268
+ });
183
269
  const maxRows = opts.limits?.maxRows ?? 200;
184
270
  const maxCellChars = opts.limits?.maxCellChars ?? 2e3;
185
271
  const access = {
@@ -220,6 +306,15 @@ function createAiDbAccess(opts) {
220
306
  const createdIds = [];
221
307
  const events = [];
222
308
  for (const [index, event] of input.events.entries()) {
309
+ const tableAccess = policy.tableAccess(event.dataset);
310
+ if (tableAccess === "hidden") {
311
+ errors.push(`[${index}] unknown or unavailable dataset "${event.dataset}"`);
312
+ continue;
313
+ }
314
+ if (tableAccess === "read-only") {
315
+ errors.push(`[${index}] dataset "${event.dataset}" is read-only and cannot be modified`);
316
+ continue;
317
+ }
223
318
  if (event.type === "item-created") {
224
319
  const looseEvent = event;
225
320
  const payload = event.payload ?? {};
@@ -384,7 +479,7 @@ function createDbTools(opts) {
384
479
  })
385
480
  };
386
481
  if (opts.mutations) tools.mutateDb = tool({
387
- description: "Apply one or more CRDT mutation events to the synced database. Use this for writes instead of SQL. Query the current data first when updating or deleting existing rows. Create events must omit ids: do not provide item_id or payload.id, because the tool generates ids and returns them. Create payloads must include all required non-id columns, update events should include only changed columns, and delete events should use an empty payload.",
482
+ description: "Apply one or more CRDT mutation events to the synced database. Use this for writes instead of SQL. Query the current data first when updating or deleting existing rows. Create events must omit ids: do not provide item_id or payload.id, because the tool generates ids and returns them. Create payloads must include all required non-id columns, update events should include only changed columns, and delete events should use an empty payload. Tables the schema documentation marks read-only cannot be written.",
388
483
  inputSchema: mutationInputSchema,
389
484
  execute: async (input) => {
390
485
  const access = await opts.access();
@@ -395,6 +490,6 @@ function createDbTools(opts) {
395
490
  return tools;
396
491
  }
397
492
  //#endregion
398
- export { QueryGuardError, createAiDbAccess, createDbTools, createQueryGuard, createSchemaDoc, runWithForcedRollback };
493
+ export { QueryGuardError, createAiDbAccess, createDbTools, createQueryGuard, createSchemaDoc, resolveAiPolicy, runWithForcedRollback };
399
494
 
400
495
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/query-guard.ts","../src/schema-doc.ts","../src/db-access.ts","../src/tools.ts"],"sourcesContent":["import type { AiDbExecutor } from \"./db-access\";\n\nexport type QueryGuardInput = {\n sql: string;\n parameters?: readonly unknown[];\n};\n\nexport type QueryGuardRejection = {\n allowed: false;\n code: \"invalid-statement\" | \"multi-statement\" | \"invalid-sql\" | \"write-detected\";\n message: string;\n};\n\nexport type QueryGuardVerdict = { allowed: true } | QueryGuardRejection;\n\nexport class QueryGuardError extends Error {\n readonly rejection: QueryGuardRejection;\n\n constructor(rejection: QueryGuardRejection) {\n super(rejection.message);\n this.name = \"QueryGuardError\";\n this.rejection = rejection;\n }\n}\n\nexport type QueryGuard = {\n /**\n * Statically verifies the query is a read-only single statement. Reads are not restricted\n * by table — the whole database file is in scope for the agent, so don't colocate data the\n * agent must not see.\n */\n check(input: QueryGuardInput): QueryGuardVerdict;\n /**\n * `check` + execute inside a forced-rollback transaction (the unconditional backstop for\n * anything the analysis might miss). Throws {@link QueryGuardError} when the check rejects.\n */\n execute<TResult = unknown>(input: QueryGuardInput): { rows: TResult[] };\n};\n\n/**\n * Opcodes that prove a statement is not read-only. `OpenWrite` covers row-level writes (every\n * real-table mutation opens its cursor through it), `Clear`/`Destroy` cover whole-table\n * deletes that skip cursors (truncate optimization), the rest cover DDL, pragma-class\n * statements (which a transaction rollback would NOT undo), virtual-table writes, and trigger\n * subprograms. `Insert`/`Delete`/`IdxInsert` are deliberately absent — they also run against\n * ephemeral/sorter cursors in ordinary SELECTs (DISTINCT, ORDER BY) and are only dangerous on\n * a cursor an `OpenWrite` would have created.\n */\nconst WRITE_OPCODES = new Set([\n \"OpenWrite\",\n \"Clear\",\n \"Destroy\",\n \"CreateBtree\",\n \"SqlExec\",\n \"ParseSchema\",\n \"DropTable\",\n \"DropIndex\",\n \"DropTrigger\",\n \"SetCookie\",\n \"JournalMode\",\n \"Vacuum\",\n \"IncrVacuum\",\n \"Checkpoint\",\n \"MaxPgcnt\",\n \"Expire\",\n \"AutoCommit\",\n \"VUpdate\",\n \"VCreate\",\n \"VDestroy\",\n \"Program\",\n]);\n\ntype ExplainRow = {\n opcode: string;\n};\n\nconst READ_STATEMENT_PREFIXES = [\"select\", \"with\", \"values\"];\n\nexport function createQueryGuard(opts: { executor: AiDbExecutor }): QueryGuard {\n function reject(code: QueryGuardRejection[\"code\"], message: string): QueryGuardRejection {\n return { allowed: false, code, message };\n }\n\n function check(input: QueryGuardInput): QueryGuardVerdict {\n let body = input.sql.trim();\n while (body.endsWith(\";\")) {\n body = body.slice(0, -1).trimEnd();\n }\n\n if (body.includes(\";\")) {\n return reject(\n \"multi-statement\",\n \"Only a single SQL statement is allowed per query, and semicolons are only allowed at the very end. If the semicolon is inside a string literal, pass the value as a bound parameter instead.\",\n );\n }\n\n const lowered = body.toLowerCase();\n if (!READ_STATEMENT_PREFIXES.some((keyword) => lowered.startsWith(keyword))) {\n return reject(\n \"invalid-statement\",\n \"Only read-only queries are allowed: the statement must start directly with SELECT, WITH, or VALUES (no leading comments).\",\n );\n }\n\n let operations: ExplainRow[];\n try {\n operations = opts.executor.execute<ExplainRow>({\n sql: `EXPLAIN ${input.sql}`,\n parameters: input.parameters ?? [],\n }).rows;\n } catch (error) {\n return reject(\"invalid-sql\", `SQL error: ${error instanceof Error ? error.message : String(error)}`);\n }\n\n for (const operation of operations) {\n if (WRITE_OPCODES.has(operation.opcode)) {\n return reject(\n \"write-detected\",\n `The statement was rejected because it would modify the database (${operation.opcode}). This tool is strictly read-only; rewrite the query as a pure SELECT.`,\n );\n }\n }\n\n return { allowed: true };\n }\n\n return {\n check,\n execute(input) {\n const verdict = check(input);\n if (!verdict.allowed) {\n throw new QueryGuardError(verdict);\n }\n return runWithForcedRollback(opts.executor, (tx) => {\n return tx.execute({ sql: input.sql, parameters: input.parameters ?? [] });\n });\n },\n };\n}\n\n/**\n * Runs `callback` in a transaction that is always rolled back (via a sentinel throw, relying\n * on the {@link AiDbExecutor} contract that a throwing callback rolls back). This is the\n * unconditional write guard backing any gap in the static analysis.\n */\nexport function runWithForcedRollback<T>(\n executor: Pick<AiDbExecutor, \"transaction\">,\n callback: (tx: Pick<AiDbExecutor, \"execute\">) => T,\n): T {\n let result: T | undefined;\n let completed = false;\n const rollbackSentinel = new Error(\"forced read-only rollback\");\n\n try {\n executor.transaction((tx) => {\n result = callback(tx);\n completed = true;\n throw rollbackSentinel;\n });\n } catch (error) {\n if (error !== rollbackSentinel) {\n throw error;\n }\n }\n\n if (!completed) {\n throw new Error(\"Transaction completed without running the callback\");\n }\n return result as T;\n}\n","import type { ColumnMeta, SyncDbSchema } from \"@sqlite-sync/core\";\n\nexport type SchemaDocContext = {\n overview?: string;\n};\n\n// Library mechanics every generated doc should explain, so consumers only have to describe\n// their own domain in `context`. Kept free of tombstone-column details the agent never sees.\nconst SCHEMA_DOC_PREAMBLE = [\n \"This is a synced SQLite database — data replicates automatically between the user's devices.\",\n \"All writes go through a sync event log, which is why the tables listed below are exposed as\",\n \"read-only SQL views; soft-deleted rows are already filtered out, so query them directly\",\n \"without any tombstone filtering. Every table has a unique `id` text primary key.\",\n].join(\"\\n\");\n\nfunction renderColumn(name: string, meta: ColumnMeta): string {\n let line = `- \\`${name}\\` ${meta.sqlType.toUpperCase()}`;\n if (!meta.nullable) {\n line += \" NOT NULL\";\n }\n if (meta.kind === \"boolean\") {\n line += \" (boolean 0/1)\";\n } else if (meta.kind === \"enum\") {\n line += ` (one of ${(meta.enumValues ?? []).map((value) => `\"${value}\"`).join(\" | \")})`;\n }\n return meta.description ? `${line} — ${meta.description}` : line;\n}\n\n/**\n * Generates a markdown schema doc for an AI agent from the declared sync schema's table\n * builders — no database access needed. Tables are presented under the view names the agent\n * queries; descriptions come from `.describe()` on the table and column builders.\n * The internal `tombstone` column is omitted.\n *\n * The doc always includes a built-in preamble explaining sqlite-sync mechanics (read-only\n * views, soft-deletes already filtered) after the consumer's `overview` — consumers only\n * need to describe their own domain.\n */\nexport function createSchemaDoc(opts: { syncDbSchema: SyncDbSchema; context?: SchemaDocContext }): string {\n const sections: string[] = [];\n\n const overview = opts.context?.overview?.trim();\n if (overview) {\n sections.push(overview);\n }\n sections.push(SCHEMA_DOC_PREAMBLE);\n\n for (const [crdtTableName, table] of Object.entries(opts.syncDbSchema.tables)) {\n const lines = [`## ${crdtTableName}`];\n if (table.description) {\n lines.push(\"\", table.description.trim());\n }\n lines.push(\"\", \"Columns:\");\n for (const [name, meta] of Object.entries(table.columns)) {\n if (name === \"tombstone\") continue;\n lines.push(renderColumn(name, meta));\n }\n sections.push(lines.join(\"\\n\"));\n }\n\n sections.push(CHANGE_HISTORY_DOC);\n\n return sections.join(\"\\n\\n\");\n}\n\n// Documents the curated `change_history` view created over the sync event log. Unlike the table\n// views above, it is NOT tombstone-filtered — it intentionally surfaces the full change log,\n// including the contents of since-deleted items.\nconst CHANGE_HISTORY_DOC = [\n \"## change_history\",\n \"\",\n \"A read-only, append-only log of every change across all tables, one row per sync event.\",\n \"Query it like any other view. Unlike the tables above, soft-delete filtering does NOT apply\",\n \"here — it includes changes to items that were later deleted, so treat it as an audit log.\",\n \"\",\n \"Columns:\",\n \"- `seq` INTEGER NOT NULL — monotonic sequence number; **order history by this** (ascending = oldest first)\",\n \"- `dataset` TEXT NOT NULL — which table the change applies to (matches a table name above)\",\n \"- `item_id` TEXT NOT NULL — the `id` of the affected row\",\n '- `change_type` TEXT NOT NULL (one of \"item-created\" | \"item-updated\" | \"item-deleted\")',\n '- `status` TEXT NOT NULL (one of \"applied\" | \"pending\" | \"failed\" | \"deduped\") — \"applied\" took effect; \"failed\" did not; filter to `status = \\'applied\\'` for the effective history',\n '- `origin` TEXT NOT NULL — where the change came from (e.g. \"own\", \"remote\", \"local\")',\n \"- `timestamp` TEXT NOT NULL — opaque hybrid-logical-clock string; do NOT parse it as a date, order by `seq` instead\",\n \"- `changes` TEXT NOT NULL — JSON of what the change set: the changed columns only for item-updated, the full initial row for item-created, `{}` for item-deleted. Use `json_extract(changes, '$.column')` to read a field.\",\n].join(\"\\n\");\n","import {\n type CrdtEventType,\n CrdtEventValidationError,\n type CrdtStorage,\n generateId,\n type OwnCrdtEvent,\n type SyncDbSchema,\n} from \"@sqlite-sync/core\";\nimport { createQueryGuard, QueryGuardError } from \"./query-guard\";\nimport { createSchemaDoc, type SchemaDocContext } from \"./schema-doc\";\n\nexport type AiDbExecuteParams = {\n sql: string;\n parameters: readonly unknown[];\n};\n\n/**\n * Minimal executor contract for AI database access. Runtime-specific — inject the raw SQL\n * capability that matches where the storage lives; a Cloudflare `ServerSyncDb`'s `unsafe`\n * executor satisfies it.\n */\nexport type AiDbExecutor = {\n execute<TResult = unknown>(query: AiDbExecuteParams): { rows: TResult[] };\n transaction(callback: (tx: Pick<AiDbExecutor, \"execute\">) => void): void;\n};\n\nexport type AiQueryInput = {\n sql: string;\n parameters?: readonly unknown[];\n};\n\nexport type AiQueryResult =\n | {\n rows: Record<string, unknown>[];\n rowCount: number;\n truncated: boolean;\n }\n | {\n error: string;\n };\n\nexport type AiMutationEvent =\n | {\n type: \"item-created\";\n dataset: string;\n item_id?: never;\n payload?: Record<string, unknown>;\n }\n | {\n type: Exclude<CrdtEventType, \"item-created\">;\n dataset: string;\n item_id: string;\n payload?: Record<string, unknown>;\n };\n\nexport type AiMutationInput = {\n events: AiMutationEvent[];\n};\n\nexport type AiMutationResult =\n | {\n applied: true;\n eventCount: number;\n createdIds: string[];\n }\n | {\n error: string;\n errors?: string[];\n };\n\n/**\n * AI access to a synced database. Lives where the storage lives; its method names\n * are the RPC contract, so a DO stub proxying to these methods exposes the same surface\n * (promise-wrapped) and satisfies the tool layer's `DbToolsAccess`.\n *\n * `query` enforces read-only (single SELECT/WITH/VALUES statement, no write opcodes, executed\n * in a forced-rollback transaction) but reads are not restricted by table — the whole database\n * file is in scope for the agent, so don't colocate data the agent must not see.\n *\n * `mutate` is only present when `createAiDbAccess` receives a CRDT storage. Mutations are CRDT\n * events applied through sqlite-sync's normal own-event path, never direct SQL writes.\n */\nexport type AiDbAccess = {\n getSchemaDoc(): string;\n query(input: AiQueryInput): AiQueryResult;\n mutate?(input: AiMutationInput): AiMutationResult;\n};\n\nfunction toBase64(bytes: Uint8Array): string {\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n\nexport function createAiDbAccess(opts: {\n executor: AiDbExecutor;\n storage?: Pick<CrdtStorage, \"applyOwnEvents\">;\n syncDbSchema: SyncDbSchema;\n context?: SchemaDocContext;\n limits?: { maxRows?: number; maxCellChars?: number };\n}): AiDbAccess {\n const schemaDoc = createSchemaDoc({ syncDbSchema: opts.syncDbSchema, context: opts.context });\n const guard = createQueryGuard({ executor: opts.executor });\n const maxRows = opts.limits?.maxRows ?? 200;\n const maxCellChars = opts.limits?.maxCellChars ?? 2000;\n\n const access: AiDbAccess = {\n getSchemaDoc() {\n return schemaDoc;\n },\n query(input) {\n let resultRows: Record<string, unknown>[];\n try {\n resultRows = guard.execute<Record<string, unknown>>(input).rows;\n } catch (error) {\n if (error instanceof QueryGuardError) {\n return { error: error.message };\n }\n throw error;\n }\n\n let truncated = resultRows.length > maxRows;\n\n function shapeCell(value: unknown): unknown {\n if (value instanceof Uint8Array) {\n // Base64 inflates by 4/3, so budget the encoded length against the cell cap.\n if (Math.ceil(value.byteLength / 3) * 4 <= maxCellChars) {\n return `<blob base64 ${toBase64(value)}>`;\n }\n truncated = true;\n return `<blob ${value.byteLength} bytes>`;\n }\n if (typeof value === \"string\" && value.length > maxCellChars) {\n truncated = true;\n return `${value.slice(0, maxCellChars)}…`;\n }\n return value;\n }\n\n const rows = resultRows\n .slice(0, maxRows)\n .map((row) => Object.fromEntries(Object.entries(row).map(([column, value]) => [column, shapeCell(value)])));\n\n return { rows, rowCount: resultRows.length, truncated };\n },\n };\n\n const storage = opts.storage;\n if (storage) {\n access.mutate = (input) => {\n const errors: string[] = [];\n const createdIds: string[] = [];\n const events: OwnCrdtEvent[] = [];\n\n for (const [index, event] of input.events.entries()) {\n if (event.type === \"item-created\") {\n const looseEvent = event as { item_id?: unknown };\n const payload = event.payload ?? {};\n if (looseEvent.item_id !== undefined) {\n errors.push(`[${index}] item-created events must omit item_id; an id is generated automatically`);\n }\n if (\"id\" in payload) {\n errors.push(`[${index}] item-created payload must omit id; an id is generated automatically`);\n }\n if (looseEvent.item_id !== undefined || \"id\" in payload) {\n continue;\n }\n\n const id = generateId();\n createdIds.push(id);\n events.push({\n type: \"item-created\",\n dataset: event.dataset,\n item_id: id,\n payload: JSON.stringify({ ...payload, id }),\n });\n continue;\n }\n\n events.push({\n type: event.type,\n dataset: event.dataset,\n item_id: event.item_id,\n payload: JSON.stringify(event.payload ?? {}),\n });\n }\n\n if (errors.length > 0) {\n return { error: `Invalid mutation events: ${errors.join(\"; \")}`, errors };\n }\n\n try {\n storage.applyOwnEvents(events);\n } catch (error) {\n if (error instanceof CrdtEventValidationError) {\n return { error: error.message, errors: error.errors };\n }\n throw error;\n }\n\n return { applied: true, eventCount: events.length, createdIds };\n };\n }\n\n return access;\n}\n","import { jsonSchema, type ToolSet, tool } from \"ai\";\nimport type { AiMutationInput, AiMutationResult, AiQueryInput, AiQueryResult } from \"./db-access\";\n\ntype MaybePromise<T> = T | Promise<T>;\n\n/**\n * What the tools need from the database side. Satisfied directly by `AiDbAccess`, or by a\n * Durable Object stub whose RPC methods delegate to one (RPC wraps returns in promises).\n */\nexport type DbToolsAccess = {\n getSchemaDoc(): MaybePromise<string>;\n query(input: AiQueryInput): MaybePromise<AiQueryResult>;\n mutate?(input: AiMutationInput): MaybePromise<AiMutationResult>;\n};\n\nexport type CreateDbToolsOptions = {\n access: () => MaybePromise<DbToolsAccess>;\n /** Expose the write-capable `mutateDb` tool. The access object must also implement `mutate`. */\n mutations?: boolean;\n};\n\nconst emptyInputSchema = jsonSchema<Record<string, never>>({\n type: \"object\",\n properties: {},\n additionalProperties: false,\n});\n\nconst queryInputSchema = jsonSchema<{ sql: string; parameters?: unknown[] }>({\n type: \"object\",\n properties: {\n sql: {\n type: \"string\",\n description:\n \"A single read-only SQLite statement starting with SELECT, WITH, or VALUES. Use ? placeholders for values.\",\n },\n parameters: {\n type: \"array\",\n items: { type: [\"string\", \"number\", \"boolean\", \"null\"] },\n description: \"Values bound to the ? placeholders, in order.\",\n },\n },\n required: [\"sql\"],\n additionalProperties: false,\n});\n\nconst mutationInputSchema = jsonSchema<AiMutationInput>({\n type: \"object\",\n properties: {\n events: {\n type: \"array\",\n minItems: 1,\n items: {\n anyOf: [\n {\n type: \"object\",\n properties: {\n type: {\n type: \"string\",\n enum: [\"item-created\"],\n description: \"Create a synced row. Omit item_id and payload.id; the tool generates the id.\",\n },\n dataset: {\n type: \"string\",\n description: \"The synced dataset/table name from the schema documentation.\",\n },\n payload: {\n type: \"object\",\n additionalProperties: true,\n not: { required: [\"id\"] },\n description:\n \"Column values for the new row, excluding id. Include all required non-id columns from the schema.\",\n },\n },\n required: [\"type\", \"dataset\", \"payload\"],\n additionalProperties: false,\n },\n {\n type: \"object\",\n properties: {\n type: {\n type: \"string\",\n enum: [\"item-updated\", \"item-deleted\"],\n description: \"Update or delete an existing synced row.\",\n },\n dataset: {\n type: \"string\",\n description: \"The synced dataset/table name from the schema documentation.\",\n },\n item_id: {\n type: \"string\",\n description: \"The stable id of the row being updated or deleted.\",\n },\n payload: {\n type: \"object\",\n additionalProperties: true,\n description: \"Changed column values for item-updated. Omit or pass {} for item-deleted.\",\n },\n },\n required: [\"type\", \"dataset\", \"item_id\"],\n additionalProperties: false,\n },\n ],\n },\n description: \"One or more CRDT mutation events to apply atomically.\",\n },\n },\n required: [\"events\"],\n additionalProperties: false,\n});\n\n/**\n * AI SDK tools for a synced database. `access` is a factory because acquiring the database\n * may itself be async per call (e.g. resolving a Durable Object stub from another DO).\n */\nexport function createDbTools(opts: CreateDbToolsOptions): ToolSet {\n const tools: ToolSet = {\n getDbSchema: tool({\n description:\n \"Get the schema documentation for the synced SQLite database: tables, columns, types, and data conventions. Call this before reasoning about the data.\",\n inputSchema: emptyInputSchema,\n execute: async () => {\n const access = await opts.access();\n return await access.getSchemaDoc();\n },\n }),\n queryDb: tool({\n description:\n \"Run a read-only SQL query against the synced SQLite database. Only a single SELECT/WITH/VALUES statement is allowed — anything that writes is rejected. Pass values as ? placeholders via `parameters` instead of inlining them as literals. Results are capped; ask for fewer columns or add LIMIT/WHERE if `truncated` is true.\",\n inputSchema: queryInputSchema,\n execute: async ({ sql, parameters }) => {\n const access = await opts.access();\n return await access.query({ sql, parameters });\n },\n }),\n };\n\n if (opts.mutations) {\n tools.mutateDb = tool({\n description:\n \"Apply one or more CRDT mutation events to the synced database. Use this for writes instead of SQL. Query the current data first when updating or deleting existing rows. Create events must omit ids: do not provide item_id or payload.id, because the tool generates ids and returns them. Create payloads must include all required non-id columns, update events should include only changed columns, and delete events should use an empty payload.\",\n inputSchema: mutationInputSchema,\n execute: async (input) => {\n const access = await opts.access();\n if (!access.mutate) {\n return { error: \"Database mutations are not enabled for this access object.\" } satisfies AiMutationResult;\n }\n return await access.mutate(input);\n },\n });\n }\n\n return tools;\n}\n"],"mappings":";;;AAeA,IAAa,kBAAb,cAAqC,MAAM;CACzC;CAEA,YAAY,WAAgC;EAC1C,MAAM,UAAU,OAAO;EACvB,KAAK,OAAO;EACZ,KAAK,YAAY;CACnB;AACF;;;;;;;;;;AAyBA,MAAM,gCAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAMD,MAAM,0BAA0B;CAAC;CAAU;CAAQ;AAAQ;AAE3D,SAAgB,iBAAiB,MAA8C;CAC7E,SAAS,OAAO,MAAmC,SAAsC;EACvF,OAAO;GAAE,SAAS;GAAO;GAAM;EAAQ;CACzC;CAEA,SAAS,MAAM,OAA2C;EACxD,IAAI,OAAO,MAAM,IAAI,KAAK;EAC1B,OAAO,KAAK,SAAS,GAAG,GACtB,OAAO,KAAK,MAAM,GA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AS,OAAO,UAAU;GACxB,MAAM,SAAS,MAAM,KAAK,OAAO;GACjC,IAAI,CAAC,OAAO,QACV,OAAO,EAAE,OAAO,6DAA6D;GAE/E,OAAO,MAAM,OAAO,OAAO,KAAK;EAClC;CACF,CAAC;CAGH,OAAO;AACT"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/policy.ts","../src/query-guard.ts","../src/schema-doc.ts","../src/db-access.ts","../src/tools.ts"],"sourcesContent":["import type { AiAccess, SyncDbSchema } from \"@sqlite-sync/core\";\n\nexport type ResolvedTablePolicy = {\n crdtTableName: string;\n baseTableName: string;\n access: AiAccess;\n};\n\nexport type ResolvedAiPolicy = {\n /** In schema declaration order, hidden tables included. */\n tables: ResolvedTablePolicy[];\n hasHiddenTables: boolean;\n /** Base table names the agent may read. Only meaningful when `hasHiddenTables` is true. */\n readableBaseTableNames: string[];\n /** Resolves either the crdt or the base table name; anything unknown is \"hidden\". */\n tableAccess(dataset: string): AiAccess;\n};\n\n/**\n * Flattens the AI access declared on the schema's table builders into the lookups the doc\n * generator and the enforcement points share, so they cannot disagree. Access is table-level:\n * see {@link AiAccess}.\n */\nexport function resolveAiPolicy(opts: { syncDbSchema: SyncDbSchema }): ResolvedAiPolicy {\n const tables: ResolvedTablePolicy[] = opts.syncDbSchema.tablesConfig.map(({ crdtTableName, baseTableName }) => ({\n crdtTableName,\n baseTableName,\n access: opts.syncDbSchema.tables[crdtTableName].aiAccess,\n }));\n\n // CRDT events address a table by either name, so both resolve to the same policy.\n const byName = new Map<string, ResolvedTablePolicy>();\n for (const table of tables) {\n byName.set(table.crdtTableName, table);\n byName.set(table.baseTableName, table);\n }\n\n return {\n tables,\n hasHiddenTables: tables.some((table) => table.access === \"hidden\"),\n readableBaseTableNames: tables.filter((table) => table.access !== \"hidden\").map((table) => table.baseTableName),\n tableAccess(dataset) {\n return byName.get(dataset)?.access ?? \"hidden\";\n },\n };\n}\n","import type { AiDbExecutor } from \"./db-access\";\n\nexport type QueryGuardInput = {\n sql: string;\n parameters?: readonly unknown[];\n};\n\nexport type QueryGuardRejection = {\n allowed: false;\n code: \"invalid-statement\" | \"multi-statement\" | \"invalid-sql\" | \"write-detected\" | \"table-denied\";\n message: string;\n};\n\nexport type QueryGuardVerdict = { allowed: true } | QueryGuardRejection;\n\nexport class QueryGuardError extends Error {\n readonly rejection: QueryGuardRejection;\n\n constructor(rejection: QueryGuardRejection) {\n super(rejection.message);\n this.name = \"QueryGuardError\";\n this.rejection = rejection;\n }\n}\n\nexport type QueryGuard = {\n /**\n * Statically verifies the query is a read-only single statement, and — when the guard was\n * created with `readableTables` — that it only reads those tables. Without `readableTables`\n * reads are unrestricted: the whole database file is in scope for the agent, so don't\n * colocate data the agent must not see.\n */\n check(input: QueryGuardInput): QueryGuardVerdict;\n /**\n * `check` + execute inside a forced-rollback transaction (the unconditional backstop for\n * anything the analysis might miss). Throws {@link QueryGuardError} when the check rejects.\n */\n execute<TResult = unknown>(input: QueryGuardInput): { rows: TResult[] };\n};\n\n/**\n * Opcodes that prove a statement is not read-only. `OpenWrite` covers row-level writes (every\n * real-table mutation opens its cursor through it), `Clear`/`Destroy` cover whole-table\n * deletes that skip cursors (truncate optimization), the rest cover DDL, pragma-class\n * statements (which a transaction rollback would NOT undo), virtual-table writes, and trigger\n * subprograms. `Insert`/`Delete`/`IdxInsert` are deliberately absent — they also run against\n * ephemeral/sorter cursors in ordinary SELECTs (DISTINCT, ORDER BY) and are only dangerous on\n * a cursor an `OpenWrite` would have created.\n */\nconst WRITE_OPCODES = new Set([\n \"OpenWrite\",\n \"Clear\",\n \"Destroy\",\n \"CreateBtree\",\n \"SqlExec\",\n \"ParseSchema\",\n \"DropTable\",\n \"DropIndex\",\n \"DropTrigger\",\n \"SetCookie\",\n \"JournalMode\",\n \"Vacuum\",\n \"IncrVacuum\",\n \"Checkpoint\",\n \"MaxPgcnt\",\n \"Expire\",\n \"AutoCommit\",\n \"VUpdate\",\n \"VCreate\",\n \"VDestroy\",\n \"Program\",\n]);\n\n/**\n * Opcodes that open a b-tree cursor for reading. `p2` is the root page of the table or index\n * and `p3` the database it lives in (0 = main), which is how a statement's real table set is\n * recovered — views are already flattened into their base tables by the time bytecode exists,\n * so aliases, CTEs, subqueries and quoting tricks all resolve here.\n */\nconst READ_CURSOR_OPCODES = new Set([\"OpenRead\", \"ReopenIdx\"]);\n\ntype ExplainRow = {\n opcode: string;\n p2: number | string;\n p3: number | string;\n};\n\n/**\n * Table-valued pragma functions (`pragma_table_list()`, ...) read the schema through a virtual\n * table, so no root page identifies them. They expose names and columns rather than rows, but a\n * hidden table should not be enumerable either, so they are rejected outright when restricted.\n */\nconst PRAGMA_FUNCTION_PATTERN = /\\bpragma_[a-z_]+\\s*\\(/i;\n\nconst READ_STATEMENT_PREFIXES = [\"select\", \"with\", \"values\"];\n\nexport function createQueryGuard(opts: {\n executor: AiDbExecutor;\n /**\n * Restrict reads to these tables (matched case-insensitively, indexes on them included).\n * Views are resolved through their base tables, so pass base table names. Omit for\n * unrestricted reads.\n */\n readableTables?: readonly string[];\n}): QueryGuard {\n const readableTables = opts.readableTables ? new Set(opts.readableTables.map((name) => name.toLowerCase())) : null;\n const readableTablesLabel = opts.readableTables?.join(\", \") ?? \"\";\n\n /**\n * Root page to table name, for every b-tree in the main database. Re-read per query instead of\n * cached: a migration can drop a table and hand its root page to another one, and a stale map\n * would then resolve that page to the wrong (possibly allowed) name.\n */\n function readRootPages(): Map<number, string> {\n const rows = opts.executor.execute<{ tbl_name: string; rootpage: number }>({\n sql: `SELECT \"tbl_name\", \"rootpage\" FROM \"sqlite_master\" WHERE \"rootpage\" IS NOT NULL AND \"rootpage\" > 0`,\n parameters: [],\n }).rows;\n return new Map(rows.map((row) => [Number(row.rootpage), row.tbl_name]));\n }\n\n function checkReadableTables(sql: string, operations: ExplainRow[]): QueryGuardRejection | null {\n if (!readableTables) {\n return null;\n }\n\n if (PRAGMA_FUNCTION_PATTERN.test(sql)) {\n return reject(\n \"table-denied\",\n `Pragma functions are not available. You can only read these tables: ${readableTablesLabel}.`,\n );\n }\n\n const cursors = operations.filter((operation) => READ_CURSOR_OPCODES.has(operation.opcode));\n if (cursors.length === 0) {\n return null;\n }\n\n let rootPages: Map<number, string>;\n try {\n rootPages = readRootPages();\n } catch {\n // Fail closed: without the schema mapping we cannot tell which tables the statement reads.\n return reject(\"table-denied\", \"The tables this statement reads could not be verified, so it was rejected.\");\n }\n\n for (const cursor of cursors) {\n // Only the main database is mapped; nothing the agent can legitimately read lives elsewhere.\n const name = Number(cursor.p3) === 0 ? rootPages.get(Number(cursor.p2)) : undefined;\n if (!name || !readableTables.has(name.toLowerCase())) {\n const subject = name ? `Table \"${name}\"` : \"A table this statement reads\";\n return reject(\n \"table-denied\",\n `${subject} is not available to you. You can only read these tables: ${readableTablesLabel}.`,\n );\n }\n }\n\n return null;\n }\n\n function reject(code: QueryGuardRejection[\"code\"], message: string): QueryGuardRejection {\n return { allowed: false, code, message };\n }\n\n function check(input: QueryGuardInput): QueryGuardVerdict {\n let body = input.sql.trim();\n while (body.endsWith(\";\")) {\n body = body.slice(0, -1).trimEnd();\n }\n\n if (body.includes(\";\")) {\n return reject(\n \"multi-statement\",\n \"Only a single SQL statement is allowed per query, and semicolons are only allowed at the very end. If the semicolon is inside a string literal, pass the value as a bound parameter instead.\",\n );\n }\n\n const lowered = body.toLowerCase();\n if (!READ_STATEMENT_PREFIXES.some((keyword) => lowered.startsWith(keyword))) {\n return reject(\n \"invalid-statement\",\n \"Only read-only queries are allowed: the statement must start directly with SELECT, WITH, or VALUES (no leading comments).\",\n );\n }\n\n let operations: ExplainRow[];\n try {\n operations = opts.executor.execute<ExplainRow>({\n sql: `EXPLAIN ${input.sql}`,\n parameters: input.parameters ?? [],\n }).rows;\n } catch (error) {\n return reject(\"invalid-sql\", `SQL error: ${error instanceof Error ? error.message : String(error)}`);\n }\n\n for (const operation of operations) {\n if (WRITE_OPCODES.has(operation.opcode)) {\n return reject(\n \"write-detected\",\n `The statement was rejected because it would modify the database (${operation.opcode}). This tool is strictly read-only; rewrite the query as a pure SELECT.`,\n );\n }\n }\n\n const denied = checkReadableTables(body, operations);\n if (denied) {\n return denied;\n }\n\n return { allowed: true };\n }\n\n return {\n check,\n execute(input) {\n const verdict = check(input);\n if (!verdict.allowed) {\n throw new QueryGuardError(verdict);\n }\n return runWithForcedRollback(opts.executor, (tx) => {\n return tx.execute({ sql: input.sql, parameters: input.parameters ?? [] });\n });\n },\n };\n}\n\n/**\n * Runs `callback` in a transaction that is always rolled back (via a sentinel throw, relying\n * on the {@link AiDbExecutor} contract that a throwing callback rolls back). This is the\n * unconditional write guard backing any gap in the static analysis.\n */\nexport function runWithForcedRollback<T>(\n executor: Pick<AiDbExecutor, \"transaction\">,\n callback: (tx: Pick<AiDbExecutor, \"execute\">) => T,\n): T {\n let result: T | undefined;\n let completed = false;\n const rollbackSentinel = new Error(\"forced read-only rollback\");\n\n try {\n executor.transaction((tx) => {\n result = callback(tx);\n completed = true;\n throw rollbackSentinel;\n });\n } catch (error) {\n if (error !== rollbackSentinel) {\n throw error;\n }\n }\n\n if (!completed) {\n throw new Error(\"Transaction completed without running the callback\");\n }\n return result as T;\n}\n","import type { ColumnMeta, SyncDbSchema } from \"@sqlite-sync/core\";\nimport { resolveAiPolicy } from \"./policy\";\n\nexport type SchemaDocContext = {\n overview?: string;\n};\n\n// Library mechanics every generated doc should explain, so consumers only have to describe\n// their own domain in `context`. Kept free of tombstone-column details the agent never sees.\nconst SCHEMA_DOC_PREAMBLE = [\n \"This is a synced SQLite database — data replicates automatically between the user's devices.\",\n \"All writes go through a sync event log, which is why the tables listed below are exposed as\",\n \"read-only SQL views; soft-deleted rows are already filtered out, so query them directly\",\n \"without any tombstone filtering. Every table has a unique `id` text primary key.\",\n].join(\"\\n\");\n\n// Added when the schema hides a table, because then reads are restricted to an allow-list and a\n// query touching anything else fails. Worth spending tokens on to save the agent a wasted turn.\nconst RESTRICTED_READS_NOTE = [\n \"Only the tables documented below are readable. Queries that touch any other table are\",\n \"rejected, including the internal sync event log, so there is no change history available.\",\n].join(\"\\n\");\n\nfunction renderColumn(name: string, meta: ColumnMeta): string {\n let line = `- \\`${name}\\` ${meta.sqlType.toUpperCase()}`;\n if (!meta.nullable) {\n line += \" NOT NULL\";\n }\n if (meta.kind === \"boolean\") {\n line += \" (boolean 0/1)\";\n } else if (meta.kind === \"enum\") {\n line += ` (one of ${(meta.enumValues ?? []).map((value) => `\"${value}\"`).join(\" | \")})`;\n }\n return meta.description ? `${line} — ${meta.description}` : line;\n}\n\n/**\n * Generates a markdown schema doc for an AI agent from the declared sync schema's table\n * builders — no database access needed. Tables are presented under the view names the agent\n * queries; descriptions come from `.describe()` on the table and column builders.\n * The internal `tombstone` column is omitted.\n *\n * Tables declared `.ai(\"hidden\")` are left out entirely, and read-only ones are labelled so the\n * agent does not attempt a mutation that would be rejected.\n *\n * The doc always includes a built-in preamble explaining sqlite-sync mechanics (read-only\n * views, soft-deletes already filtered) after the consumer's `overview` — consumers only\n * need to describe their own domain.\n */\nexport function createSchemaDoc(opts: { syncDbSchema: SyncDbSchema; context?: SchemaDocContext }): string {\n const policy = resolveAiPolicy({ syncDbSchema: opts.syncDbSchema });\n const sections: string[] = [];\n\n const overview = opts.context?.overview?.trim();\n if (overview) {\n sections.push(overview);\n }\n sections.push(SCHEMA_DOC_PREAMBLE);\n if (policy.hasHiddenTables) {\n sections.push(RESTRICTED_READS_NOTE);\n }\n\n for (const [crdtTableName, table] of Object.entries(opts.syncDbSchema.tables)) {\n const access = policy.tableAccess(crdtTableName);\n if (access === \"hidden\") continue;\n\n const lines = [`## ${crdtTableName}`];\n if (table.description) {\n lines.push(\"\", table.description.trim());\n }\n if (access === \"read-only\") {\n lines.push(\"\", \"Read-only: you can query this table but cannot create, update, or delete its rows.\");\n }\n lines.push(\"\", \"Columns:\");\n for (const [name, meta] of Object.entries(table.columns)) {\n if (name === \"tombstone\") continue;\n lines.push(renderColumn(name, meta));\n }\n sections.push(lines.join(\"\\n\"));\n }\n\n if (!policy.hasHiddenTables) {\n sections.push(CHANGE_HISTORY_DOC);\n }\n\n return sections.join(\"\\n\\n\");\n}\n\n// Documents the curated `change_history` view created over the sync event log. Unlike the table\n// views above, it is NOT tombstone-filtered — it intentionally surfaces the full change log,\n// including the contents of since-deleted items. Omitted when any table is hidden: the view reads\n// the raw event log, which holds every dataset's payloads, so it cannot be filtered per table.\nconst CHANGE_HISTORY_DOC = [\n \"## change_history\",\n \"\",\n \"A read-only, append-only log of every change across all tables, one row per sync event.\",\n \"Query it like any other view. Unlike the tables above, soft-delete filtering does NOT apply\",\n \"here — it includes changes to items that were later deleted, so treat it as an audit log.\",\n \"\",\n \"Columns:\",\n \"- `seq` INTEGER NOT NULL — monotonic sequence number; **order history by this** (ascending = oldest first)\",\n \"- `dataset` TEXT NOT NULL — which table the change applies to (matches a table name above)\",\n \"- `item_id` TEXT NOT NULL — the `id` of the affected row\",\n '- `change_type` TEXT NOT NULL (one of \"item-created\" | \"item-updated\" | \"item-deleted\")',\n '- `status` TEXT NOT NULL (one of \"applied\" | \"pending\" | \"failed\" | \"deduped\") — \"applied\" took effect; \"failed\" did not; filter to `status = \\'applied\\'` for the effective history',\n '- `origin` TEXT NOT NULL — where the change came from (e.g. \"own\", \"remote\", \"local\")',\n \"- `timestamp` TEXT NOT NULL — opaque hybrid-logical-clock string; do NOT parse it as a date, order by `seq` instead\",\n \"- `changes` TEXT NOT NULL — JSON of what the change set: the changed columns only for item-updated, the full initial row for item-created, `{}` for item-deleted. Use `json_extract(changes, '$.column')` to read a field.\",\n].join(\"\\n\");\n","import {\n type CrdtEventType,\n CrdtEventValidationError,\n type CrdtStorage,\n generateId,\n type OwnCrdtEvent,\n type SyncDbSchema,\n} from \"@sqlite-sync/core\";\nimport { resolveAiPolicy } from \"./policy\";\nimport { createQueryGuard, QueryGuardError } from \"./query-guard\";\nimport { createSchemaDoc, type SchemaDocContext } from \"./schema-doc\";\n\nexport type AiDbExecuteParams = {\n sql: string;\n parameters: readonly unknown[];\n};\n\n/**\n * Minimal executor contract for AI database access. Runtime-specific — inject the raw SQL\n * capability that matches where the storage lives; a Cloudflare `ServerSyncDb`'s `unsafe`\n * executor satisfies it.\n */\nexport type AiDbExecutor = {\n execute<TResult = unknown>(query: AiDbExecuteParams): { rows: TResult[] };\n transaction(callback: (tx: Pick<AiDbExecutor, \"execute\">) => void): void;\n};\n\nexport type AiQueryInput = {\n sql: string;\n parameters?: readonly unknown[];\n};\n\nexport type AiQueryResult =\n | {\n rows: Record<string, unknown>[];\n rowCount: number;\n truncated: boolean;\n }\n | {\n error: string;\n };\n\nexport type AiMutationEvent =\n | {\n type: \"item-created\";\n dataset: string;\n item_id?: never;\n payload?: Record<string, unknown>;\n }\n | {\n type: Exclude<CrdtEventType, \"item-created\">;\n dataset: string;\n item_id: string;\n payload?: Record<string, unknown>;\n };\n\nexport type AiMutationInput = {\n events: AiMutationEvent[];\n};\n\nexport type AiMutationResult =\n | {\n applied: true;\n eventCount: number;\n createdIds: string[];\n }\n | {\n error: string;\n errors?: string[];\n };\n\n/**\n * AI access to a synced database. Lives where the storage lives; its method names\n * are the RPC contract, so a DO stub proxying to these methods exposes the same surface\n * (promise-wrapped) and satisfies the tool layer's `DbToolsAccess`.\n *\n * `query` enforces read-only (single SELECT/WITH/VALUES statement, no write opcodes, executed\n * in a forced-rollback transaction). Reads are restricted by table only once the schema declares\n * a table `.ai(\"hidden\")`; until then the whole database file is in scope for the agent, so don't\n * colocate data the agent must not see.\n *\n * `mutate` is only present when `createAiDbAccess` receives a CRDT storage. Mutations are CRDT\n * events applied through sqlite-sync's normal own-event path, never direct SQL writes, and are\n * rejected for tables the schema declares read-only or hidden.\n */\nexport type AiDbAccess = {\n getSchemaDoc(): string;\n query(input: AiQueryInput): AiQueryResult;\n mutate?(input: AiMutationInput): AiMutationResult;\n};\n\nfunction toBase64(bytes: Uint8Array): string {\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n\nexport function createAiDbAccess(opts: {\n executor: AiDbExecutor;\n storage?: Pick<CrdtStorage, \"applyOwnEvents\">;\n syncDbSchema: SyncDbSchema;\n context?: SchemaDocContext;\n limits?: { maxRows?: number; maxCellChars?: number };\n}): AiDbAccess {\n const policy = resolveAiPolicy({ syncDbSchema: opts.syncDbSchema });\n const schemaDoc = createSchemaDoc({ syncDbSchema: opts.syncDbSchema, context: opts.context });\n // A hidden table flips reads to an allow-list, which also cuts off the event log (it holds every\n // dataset's payloads) and any non-synced table sharing the database file.\n const guard = createQueryGuard({\n executor: opts.executor,\n readableTables: policy.hasHiddenTables ? policy.readableBaseTableNames : undefined,\n });\n const maxRows = opts.limits?.maxRows ?? 200;\n const maxCellChars = opts.limits?.maxCellChars ?? 2000;\n\n const access: AiDbAccess = {\n getSchemaDoc() {\n return schemaDoc;\n },\n query(input) {\n let resultRows: Record<string, unknown>[];\n try {\n resultRows = guard.execute<Record<string, unknown>>(input).rows;\n } catch (error) {\n if (error instanceof QueryGuardError) {\n return { error: error.message };\n }\n throw error;\n }\n\n let truncated = resultRows.length > maxRows;\n\n function shapeCell(value: unknown): unknown {\n if (value instanceof Uint8Array) {\n // Base64 inflates by 4/3, so budget the encoded length against the cell cap.\n if (Math.ceil(value.byteLength / 3) * 4 <= maxCellChars) {\n return `<blob base64 ${toBase64(value)}>`;\n }\n truncated = true;\n return `<blob ${value.byteLength} bytes>`;\n }\n if (typeof value === \"string\" && value.length > maxCellChars) {\n truncated = true;\n return `${value.slice(0, maxCellChars)}…`;\n }\n return value;\n }\n\n const rows = resultRows\n .slice(0, maxRows)\n .map((row) => Object.fromEntries(Object.entries(row).map(([column, value]) => [column, shapeCell(value)])));\n\n return { rows, rowCount: resultRows.length, truncated };\n },\n };\n\n const storage = opts.storage;\n if (storage) {\n access.mutate = (input) => {\n const errors: string[] = [];\n const createdIds: string[] = [];\n const events: OwnCrdtEvent[] = [];\n\n for (const [index, event] of input.events.entries()) {\n const tableAccess = policy.tableAccess(event.dataset);\n if (tableAccess === \"hidden\") {\n errors.push(`[${index}] unknown or unavailable dataset \"${event.dataset}\"`);\n continue;\n }\n if (tableAccess === \"read-only\") {\n errors.push(`[${index}] dataset \"${event.dataset}\" is read-only and cannot be modified`);\n continue;\n }\n if (event.type === \"item-created\") {\n const looseEvent = event as { item_id?: unknown };\n const payload = event.payload ?? {};\n if (looseEvent.item_id !== undefined) {\n errors.push(`[${index}] item-created events must omit item_id; an id is generated automatically`);\n }\n if (\"id\" in payload) {\n errors.push(`[${index}] item-created payload must omit id; an id is generated automatically`);\n }\n if (looseEvent.item_id !== undefined || \"id\" in payload) {\n continue;\n }\n\n const id = generateId();\n createdIds.push(id);\n events.push({\n type: \"item-created\",\n dataset: event.dataset,\n item_id: id,\n payload: JSON.stringify({ ...payload, id }),\n });\n continue;\n }\n\n events.push({\n type: event.type,\n dataset: event.dataset,\n item_id: event.item_id,\n payload: JSON.stringify(event.payload ?? {}),\n });\n }\n\n if (errors.length > 0) {\n return { error: `Invalid mutation events: ${errors.join(\"; \")}`, errors };\n }\n\n try {\n storage.applyOwnEvents(events);\n } catch (error) {\n if (error instanceof CrdtEventValidationError) {\n return { error: error.message, errors: error.errors };\n }\n throw error;\n }\n\n return { applied: true, eventCount: events.length, createdIds };\n };\n }\n\n return access;\n}\n","import { jsonSchema, type ToolSet, tool } from \"ai\";\nimport type { AiMutationInput, AiMutationResult, AiQueryInput, AiQueryResult } from \"./db-access\";\n\ntype MaybePromise<T> = T | Promise<T>;\n\n/**\n * What the tools need from the database side. Satisfied directly by `AiDbAccess`, or by a\n * Durable Object stub whose RPC methods delegate to one (RPC wraps returns in promises).\n */\nexport type DbToolsAccess = {\n getSchemaDoc(): MaybePromise<string>;\n query(input: AiQueryInput): MaybePromise<AiQueryResult>;\n mutate?(input: AiMutationInput): MaybePromise<AiMutationResult>;\n};\n\nexport type CreateDbToolsOptions = {\n access: () => MaybePromise<DbToolsAccess>;\n /** Expose the write-capable `mutateDb` tool. The access object must also implement `mutate`. */\n mutations?: boolean;\n};\n\nconst emptyInputSchema = jsonSchema<Record<string, never>>({\n type: \"object\",\n properties: {},\n additionalProperties: false,\n});\n\nconst queryInputSchema = jsonSchema<{ sql: string; parameters?: unknown[] }>({\n type: \"object\",\n properties: {\n sql: {\n type: \"string\",\n description:\n \"A single read-only SQLite statement starting with SELECT, WITH, or VALUES. Use ? placeholders for values.\",\n },\n parameters: {\n type: \"array\",\n items: { type: [\"string\", \"number\", \"boolean\", \"null\"] },\n description: \"Values bound to the ? placeholders, in order.\",\n },\n },\n required: [\"sql\"],\n additionalProperties: false,\n});\n\nconst mutationInputSchema = jsonSchema<AiMutationInput>({\n type: \"object\",\n properties: {\n events: {\n type: \"array\",\n minItems: 1,\n items: {\n anyOf: [\n {\n type: \"object\",\n properties: {\n type: {\n type: \"string\",\n enum: [\"item-created\"],\n description: \"Create a synced row. Omit item_id and payload.id; the tool generates the id.\",\n },\n dataset: {\n type: \"string\",\n description: \"The synced dataset/table name from the schema documentation.\",\n },\n payload: {\n type: \"object\",\n additionalProperties: true,\n not: { required: [\"id\"] },\n description:\n \"Column values for the new row, excluding id. Include all required non-id columns from the schema.\",\n },\n },\n required: [\"type\", \"dataset\", \"payload\"],\n additionalProperties: false,\n },\n {\n type: \"object\",\n properties: {\n type: {\n type: \"string\",\n enum: [\"item-updated\", \"item-deleted\"],\n description: \"Update or delete an existing synced row.\",\n },\n dataset: {\n type: \"string\",\n description: \"The synced dataset/table name from the schema documentation.\",\n },\n item_id: {\n type: \"string\",\n description: \"The stable id of the row being updated or deleted.\",\n },\n payload: {\n type: \"object\",\n additionalProperties: true,\n description: \"Changed column values for item-updated. Omit or pass {} for item-deleted.\",\n },\n },\n required: [\"type\", \"dataset\", \"item_id\"],\n additionalProperties: false,\n },\n ],\n },\n description: \"One or more CRDT mutation events to apply atomically.\",\n },\n },\n required: [\"events\"],\n additionalProperties: false,\n});\n\n/**\n * AI SDK tools for a synced database. `access` is a factory because acquiring the database\n * may itself be async per call (e.g. resolving a Durable Object stub from another DO).\n */\nexport function createDbTools(opts: CreateDbToolsOptions): ToolSet {\n const tools: ToolSet = {\n getDbSchema: tool({\n description:\n \"Get the schema documentation for the synced SQLite database: tables, columns, types, and data conventions. Call this before reasoning about the data.\",\n inputSchema: emptyInputSchema,\n execute: async () => {\n const access = await opts.access();\n return await access.getSchemaDoc();\n },\n }),\n queryDb: tool({\n description:\n \"Run a read-only SQL query against the synced SQLite database. Only a single SELECT/WITH/VALUES statement is allowed — anything that writes is rejected. Pass values as ? placeholders via `parameters` instead of inlining them as literals. Results are capped; ask for fewer columns or add LIMIT/WHERE if `truncated` is true.\",\n inputSchema: queryInputSchema,\n execute: async ({ sql, parameters }) => {\n const access = await opts.access();\n return await access.query({ sql, parameters });\n },\n }),\n };\n\n if (opts.mutations) {\n tools.mutateDb = tool({\n description:\n \"Apply one or more CRDT mutation events to the synced database. Use this for writes instead of SQL. Query the current data first when updating or deleting existing rows. Create events must omit ids: do not provide item_id or payload.id, because the tool generates ids and returns them. Create payloads must include all required non-id columns, update events should include only changed columns, and delete events should use an empty payload. Tables the schema documentation marks read-only cannot be written.\",\n inputSchema: mutationInputSchema,\n execute: async (input) => {\n const access = await opts.access();\n if (!access.mutate) {\n return { error: \"Database mutations are not enabled for this access object.\" } satisfies AiMutationResult;\n }\n return await access.mutate(input);\n },\n });\n }\n\n return tools;\n}\n"],"mappings":";;;;;;;;AAuBA,SAAgB,gBAAgB,MAAwD;CACtF,MAAM,SAAgC,KAAK,aAAa,aAAa,KAAK,EAAE,eAAe,qBAAqB;EAC9G;EACA;EACA,QAAQ,KAAK,aAAa,OAAO,cAAc,CAAC;CAClD,EAAE;CAGF,MAAM,yBAAS,IAAI,IAAiC;CACpD,KAAK,MAAM,SAAS,QAAQ;EAC1B,OAAO,IAAI,MAAM,eAAe,KAAK;EACrC,OAAO,IAAI,MAAM,eAAe,KAAK;CACvC;CAEA,OAAO;EACL;EACA,iBAAiB,OAAO,MAAM,UAAU,MAAM,WAAW,QAAQ;EACjE,wBAAwB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C/C;EACF,CAAC;CACH;CAEA,IAAI,KAAK,WACP,MAAM,WAAW,KAAK;EACpB,aACE;EACF,aAAa;EACb,SAAS,OAAO,UAAU;GACxB,MAAM,SAAS,MAAM,KAAK,OAAO;GACjC,IAAI,CAAC,OAAO,QACV,OAAO,EAAE,OAAO,6DAA6D;GAE/E,OAAO,MAAM,OAAO,OAAO,KAAK;EAClC;CACF,CAAC;CAGH,OAAO;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqlite-sync/ai",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "AI agent tools for @sqlite-sync databases",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  "dist"
32
32
  ],
33
33
  "peerDependencies": {
34
- "@sqlite-sync/core": "^0.9.0",
34
+ "@sqlite-sync/core": "^0.9.1",
35
35
  "ai": "^6.0.0"
36
36
  },
37
37
  "devDependencies": {
@@ -39,7 +39,7 @@
39
39
  "tsdown": "0.22.14",
40
40
  "typescript": "npm:@typescript/typescript6@6.0.2",
41
41
  "vitest": "^4.1.8",
42
- "@sqlite-sync/core": "0.9.0"
42
+ "@sqlite-sync/core": "0.9.1"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "tsdown",