@sqlite-sync/ai 0.10.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +17 -4
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AiAccess, CrdtEventType,
|
|
1
|
+
import { AiAccess, CrdtEventType, SyncDbSchema } from "@sqlite-sync/core";
|
|
2
2
|
import { ToolSet } from "ai";
|
|
3
3
|
//#region src/schema-doc.d.ts
|
|
4
4
|
type SchemaDocContext = {
|
|
@@ -58,6 +58,19 @@ type AiMutationEvent = {
|
|
|
58
58
|
item_id: string;
|
|
59
59
|
payload?: Record<string, unknown>;
|
|
60
60
|
};
|
|
61
|
+
type AiDbMutatorEvent = {
|
|
62
|
+
type: CrdtEventType;
|
|
63
|
+
dataset: string;
|
|
64
|
+
item_id: string;
|
|
65
|
+
payload?: Record<string, unknown>;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Minimal mutator contract for AI database access. A Cloudflare `ServerSyncDb` satisfies it.
|
|
69
|
+
* Declared as a method so schema-typed `applyEvents` implementations stay assignable.
|
|
70
|
+
*/
|
|
71
|
+
type AiDbMutator = {
|
|
72
|
+
applyEvents(events: AiDbMutatorEvent[]): void;
|
|
73
|
+
};
|
|
61
74
|
type AiMutationInput = {
|
|
62
75
|
events: AiMutationEvent[];
|
|
63
76
|
};
|
|
@@ -78,7 +91,7 @@ type AiMutationResult = {
|
|
|
78
91
|
* `{ ai: "hidden" }` on a table. Until then the whole database file is in scope, so do not
|
|
79
92
|
* put secret data in the same file.
|
|
80
93
|
*
|
|
81
|
-
* `mutate` exists only when `createAiDbAccess` gets a
|
|
94
|
+
* `mutate` exists only when `createAiDbAccess` gets a `storage` mutator. Mutations are CRDT
|
|
82
95
|
* events on the normal own-event path, never direct SQL writes. Tables marked read-only or
|
|
83
96
|
* hidden are rejected.
|
|
84
97
|
*/
|
|
@@ -89,7 +102,7 @@ type AiDbAccess = {
|
|
|
89
102
|
};
|
|
90
103
|
declare function createAiDbAccess(opts: {
|
|
91
104
|
executor: AiDbExecutor;
|
|
92
|
-
storage?:
|
|
105
|
+
storage?: AiDbMutator;
|
|
93
106
|
syncDbSchema: SyncDbSchema;
|
|
94
107
|
context?: SchemaDocContext;
|
|
95
108
|
limits?: {
|
|
@@ -193,5 +206,5 @@ type CreateDbToolsOptions = {
|
|
|
193
206
|
*/
|
|
194
207
|
declare function createDbTools(opts: CreateDbToolsOptions): ToolSet;
|
|
195
208
|
//#endregion
|
|
196
|
-
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 };
|
|
209
|
+
export { type AiDbAccess, type AiDbExecuteParams, type AiDbExecutor, type AiDbMutator, type AiDbMutatorEvent, 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 };
|
|
197
210
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -326,10 +326,10 @@ function createAiDbAccess(opts) {
|
|
|
326
326
|
type: "item-created",
|
|
327
327
|
dataset: event.dataset,
|
|
328
328
|
item_id: id,
|
|
329
|
-
payload:
|
|
329
|
+
payload: {
|
|
330
330
|
...payload,
|
|
331
331
|
id
|
|
332
|
-
}
|
|
332
|
+
}
|
|
333
333
|
});
|
|
334
334
|
continue;
|
|
335
335
|
}
|
|
@@ -337,7 +337,7 @@ function createAiDbAccess(opts) {
|
|
|
337
337
|
type: event.type,
|
|
338
338
|
dataset: event.dataset,
|
|
339
339
|
item_id: event.item_id,
|
|
340
|
-
payload:
|
|
340
|
+
payload: event.payload ?? {}
|
|
341
341
|
});
|
|
342
342
|
}
|
|
343
343
|
if (errors.length > 0) return {
|
|
@@ -345,7 +345,7 @@ function createAiDbAccess(opts) {
|
|
|
345
345
|
errors
|
|
346
346
|
};
|
|
347
347
|
try {
|
|
348
|
-
storage.
|
|
348
|
+
storage.applyEvents(events);
|
|
349
349
|
} catch (error) {
|
|
350
350
|
if (error instanceof CrdtEventValidationError) return {
|
|
351
351
|
error: error.message,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 * Checks that the query is a read-only single statement. When the guard was created with\n * `readableTables`, also checks that it only reads those tables. Without `readableTables`,\n * reads are unrestricted: the whole database file is in scope, so do not put secret data\n * in the same file.\n */\n check(input: QueryGuardInput): QueryGuardVerdict;\n /**\n * Runs `check`, then executes inside a forced-rollback transaction (backstop for anything\n * 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// Mechanics every generated doc should explain, so `context` only needs domain notes.\n// Skips 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 any table is hidden: reads become an allow-list, and anything else fails.\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 * Builds a markdown schema doc for an AI agent from the declared sync schema.\n * No database access. Tables use the view names the agent queries. Descriptions come from\n * `.describe()` on the table and column builders. The internal `tombstone` column is omitted.\n *\n * Tables with `{ ai: \"hidden\" }` are left out. Read-only tables are labelled so the agent\n * does not try a mutation that would be rejected.\n *\n * After the consumer's `overview`, the doc always adds a short preamble about sqlite-sync\n * (read-only views, soft-deletes already filtered). Put domain notes in `overview` or\n * `.describe()`.\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 over the sync event log. Unlike the table\n// views above, it is NOT tombstone-filtered: it shows the full change log, including\n// since-deleted items. Omitted when any table is hidden, because the view reads the raw\n// event log (every dataset's payloads) and 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. Inject the raw SQL capability that\n * matches where the storage lives. A Cloudflare `ServerSyncDb`'s `unsafe` 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 next to the storage. Method names are the RPC\n * contract: a DO stub that proxies them (promise-wrapped) satisfies `DbToolsAccess`.\n *\n * `query` is read-only: one SELECT/WITH/VALUES statement, no write opcodes, run in a\n * forced-rollback transaction. Table-level read limits apply only after the schema sets\n * `{ ai: \"hidden\" }` on a table. Until then the whole database file is in scope, so do not\n * put secret data in the same file.\n *\n * `mutate` exists only when `createAiDbAccess` gets a CRDT storage. Mutations are CRDT\n * events on the normal own-event path, never direct SQL writes. Tables marked read-only or\n * hidden are rejected.\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\n // every 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,a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E;GACF,aAAa;GACb,SAAS,YAAY;IAEnB,OAAO,OAAM,MADQ,KAAK,OAAO,EAAA,CACb,aAAa;GACnC;EACF,CAAC;EACD,SAAS,KAAK;GACZ,aACE;GACF,aAAa;GACb,SAAS,OAAO,EAAE,KAAK,iBAAiB;IAEtC,OAAO,OAAM,MADQ,KAAK,OAAO,EAAA,CACb,MAAM;KAAE;KAAK;IAAW,CAAC;GAC/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"}
|
|
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 * Checks that the query is a read-only single statement. When the guard was created with\n * `readableTables`, also checks that it only reads those tables. Without `readableTables`,\n * reads are unrestricted: the whole database file is in scope, so do not put secret data\n * in the same file.\n */\n check(input: QueryGuardInput): QueryGuardVerdict;\n /**\n * Runs `check`, then executes inside a forced-rollback transaction (backstop for anything\n * 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// Mechanics every generated doc should explain, so `context` only needs domain notes.\n// Skips 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 any table is hidden: reads become an allow-list, and anything else fails.\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 * Builds a markdown schema doc for an AI agent from the declared sync schema.\n * No database access. Tables use the view names the agent queries. Descriptions come from\n * `.describe()` on the table and column builders. The internal `tombstone` column is omitted.\n *\n * Tables with `{ ai: \"hidden\" }` are left out. Read-only tables are labelled so the agent\n * does not try a mutation that would be rejected.\n *\n * After the consumer's `overview`, the doc always adds a short preamble about sqlite-sync\n * (read-only views, soft-deletes already filtered). Put domain notes in `overview` or\n * `.describe()`.\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 over the sync event log. Unlike the table\n// views above, it is NOT tombstone-filtered: it shows the full change log, including\n// since-deleted items. Omitted when any table is hidden, because the view reads the raw\n// event log (every dataset's payloads) and 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 { type CrdtEventType, CrdtEventValidationError, generateId, type SyncDbSchema } 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. Inject the raw SQL capability that\n * matches where the storage lives. A Cloudflare `ServerSyncDb`'s `unsafe` 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 AiDbMutatorEvent = {\n type: CrdtEventType;\n dataset: string;\n item_id: string;\n payload?: Record<string, unknown>;\n};\n\n/**\n * Minimal mutator contract for AI database access. A Cloudflare `ServerSyncDb` satisfies it.\n * Declared as a method so schema-typed `applyEvents` implementations stay assignable.\n */\nexport type AiDbMutator = {\n applyEvents(events: AiDbMutatorEvent[]): void;\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 next to the storage. Method names are the RPC\n * contract: a DO stub that proxies them (promise-wrapped) satisfies `DbToolsAccess`.\n *\n * `query` is read-only: one SELECT/WITH/VALUES statement, no write opcodes, run in a\n * forced-rollback transaction. Table-level read limits apply only after the schema sets\n * `{ ai: \"hidden\" }` on a table. Until then the whole database file is in scope, so do not\n * put secret data in the same file.\n *\n * `mutate` exists only when `createAiDbAccess` gets a `storage` mutator. Mutations are CRDT\n * events on the normal own-event path, never direct SQL writes. Tables marked read-only or\n * hidden are rejected.\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?: AiDbMutator;\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\n // every 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: AiDbMutatorEvent[] = [];\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: { ...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: 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.applyEvents(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,MA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C;CACjE,MAAM,QAAiB;EACrB,aAAa,KAAK;GAChB,aACE;GACF,aAAa;GACb,SAAS,YAAY;IAEnB,OAAO,OAAM,MADQ,KAAK,OAAO,EAAA,CACb,aAAa;GACnC;EACF,CAAC;EACD,SAAS,KAAK;GACZ,aACE;GACF,aAAa;GACb,SAAS,OAAO,EAAE,KAAK,iBAAiB;IAEtC,OAAO,OAAM,MADQ,KAAK,OAAO,EAAA,CACb,MAAM;KAAE;KAAK;IAAW,CAAC;GAC/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.
|
|
3
|
+
"version": "0.11.0",
|
|
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.
|
|
34
|
+
"@sqlite-sync/core": "^0.11.0",
|
|
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.
|
|
42
|
+
"@sqlite-sync/core": "0.11.0"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "tsdown",
|