@persql/sdk 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/{chunk-CDNTQOBK.js → chunk-ZRZANHPI.js} +357 -159
- package/dist/chunk-ZRZANHPI.js.map +1 -0
- package/dist/cli.cjs +354 -156
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +360 -162
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +263 -128
- package/dist/index.d.ts +263 -128
- package/dist/index.js +7 -7
- package/package.json +1 -1
- package/dist/chunk-CDNTQOBK.js.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/local.ts","../src/index.ts"],"sourcesContent":["// Local mode — runs SQL against a `better-sqlite3` file (or\n// `:memory:`) instead of the HTTP API. The peer dep is loaded\n// lazily so customers who only use the live SDK never pull\n// the native module.\n//\n// Surface intentionally narrow: query / batch / tables / explain\n// + the propose/apply pre-flight pair (the agent-shape primitive\n// that's worth keeping testable without a worker). Vectors, blob,\n// subscribe, branches, approvals have no local equivalent and throw.\n\nimport type { Statement } from \"./index\";\n\ninterface LocalProposal {\n sql: string;\n params: unknown[];\n expiresAt: number;\n}\n\nconst PROPOSAL_TTL_DEFAULT_SEC = 600;\nconst PROPOSAL_TTL_MAX_SEC = 3600;\n\nfunction newLocalProposalId(): string {\n // pmut_local_<16 hex> — distinguishable from server-issued tokens\n // so a stray cross-mode redemption fails loud.\n const buf = new Uint8Array(8);\n crypto.getRandomValues(buf);\n let hex = \"\";\n for (const b of buf) hex += b.toString(16).padStart(2, \"0\");\n return `pmut_local_${hex}`;\n}\n\ninterface BetterSqliteStmt {\n reader: boolean;\n raw(toggle: boolean): BetterSqliteStmt;\n columns(): Array<{ name: string }>;\n all(...params: unknown[]): unknown[];\n run(...params: unknown[]): { changes: number };\n}\n\ninterface BetterSqliteDb {\n prepare(sql: string): BetterSqliteStmt;\n transaction<T>(fn: () => T): () => T;\n pragma(s: string): unknown;\n close(): void;\n}\n\nexport interface RawQueryResult {\n columns: string[];\n rows: unknown[][];\n rowsRead: number;\n rowsWritten: number;\n}\n\nexport class LocalDriver {\n private db: BetterSqliteDb | null = null;\n private readonly proposals = new Map<string, LocalProposal>();\n\n constructor(private readonly path: string) {}\n\n private async open(): Promise<BetterSqliteDb> {\n if (this.db) return this.db;\n // String-via-variable defeats TypeScript's static module\n // resolution so the optional peer dep doesn't need its type\n // declarations at SDK build time.\n const moduleName = \"better-sqlite3\";\n let mod: unknown;\n try {\n mod = await import(moduleName);\n } catch {\n throw new Error(\n \"PerSQL local mode requires the `better-sqlite3` peer dep. \" +\n \"Install with `npm i -D better-sqlite3` (or pnpm/yarn equivalent).\"\n );\n }\n const m = mod as { default?: unknown };\n const Ctor = (m.default ?? mod) as new (path: string) => BetterSqliteDb;\n this.db = new Ctor(this.path);\n this.db.pragma(\"foreign_keys = ON\");\n return this.db;\n }\n\n async query(sql: string, params: unknown[]): Promise<RawQueryResult> {\n const db = await this.open();\n return runOne(db, sql, params);\n }\n\n async batch(\n statements: Statement[],\n transaction: boolean\n ): Promise<RawQueryResult[]> {\n const db = await this.open();\n const exec = () => statements.map((s) => runOne(db, s.sql, s.params ?? []));\n if (transaction) return db.transaction(exec)();\n return exec();\n }\n\n async tables(): Promise<Array<{ name: string; rowCount: number }>> {\n const db = await this.open();\n const rows = runOne(\n db,\n \"SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name\",\n []\n ).rows as Array<[string]>;\n return rows.map(([name]) => {\n const c = runOne(db, `SELECT COUNT(*) FROM \"${name.replace(/\"/g, '\"\"')}\"`, [])\n .rows[0] as [number];\n return { name, rowCount: Number(c?.[0] ?? 0) };\n });\n }\n\n async explain(\n sql: string,\n params: unknown[]\n ): Promise<Array<{ id: number; parent: number; detail: string }>> {\n const db = await this.open();\n const r = runOne(db, `EXPLAIN QUERY PLAN ${sql}`, params);\n // EXPLAIN QUERY PLAN returns rows: id, parent, notused, detail\n return (r.rows as Array<[number, number, number, string]>).map((row) => ({\n id: Number(row[0]),\n parent: Number(row[1]),\n detail: String(row[3]),\n }));\n }\n\n async propose(\n sql: string,\n params: unknown[],\n ttlSec?: number\n ): Promise<{\n sql: string;\n plan: unknown[][];\n estimatedAffectedRows: number | null;\n executionToken: string;\n expiresAt: string;\n action: \"read\" | \"write\" | \"admin\";\n }> {\n const db = await this.open();\n const planRaw = runOne(db, `EXPLAIN QUERY PLAN ${sql}`, params).rows;\n const plan = planRaw as unknown[][];\n const ttl = Math.min(\n Math.max(1, Math.floor(ttlSec ?? PROPOSAL_TTL_DEFAULT_SEC)),\n PROPOSAL_TTL_MAX_SEC\n );\n const expiresAt = Date.now() + ttl * 1000;\n const executionToken = newLocalProposalId();\n this.proposals.set(executionToken, { sql, params, expiresAt });\n this.gcProposals();\n return {\n sql,\n plan,\n estimatedAffectedRows: null,\n executionToken,\n expiresAt: new Date(expiresAt).toISOString(),\n action: actionForSql(sql),\n };\n }\n\n async apply(executionToken: string): Promise<RawQueryResult> {\n const rec = this.proposals.get(executionToken);\n if (!rec) {\n throw new Error(\n \"PerSQL: executionToken is unknown, expired, or already redeemed\"\n );\n }\n this.proposals.delete(executionToken);\n if (rec.expiresAt < Date.now()) {\n throw new Error(\"PerSQL: executionToken has expired\");\n }\n return this.query(rec.sql, rec.params);\n }\n\n private gcProposals(): void {\n const now = Date.now();\n for (const [k, v] of this.proposals) {\n if (v.expiresAt < now) this.proposals.delete(k);\n }\n }\n\n close(): void {\n if (this.db) {\n try {\n this.db.close();\n } catch {\n // ignore\n }\n this.db = null;\n }\n }\n}\n\nfunction actionForSql(sql: string): \"read\" | \"write\" | \"admin\" {\n const head = sql.trim().slice(0, 24).toLowerCase();\n if (\n head.startsWith(\"create\") ||\n head.startsWith(\"drop\") ||\n head.startsWith(\"alter\")\n ) {\n return \"admin\";\n }\n if (\n head.startsWith(\"insert\") ||\n head.startsWith(\"update\") ||\n head.startsWith(\"delete\") ||\n head.startsWith(\"replace\")\n ) {\n return \"write\";\n }\n return \"read\";\n}\n\nfunction runOne(\n db: BetterSqliteDb,\n sql: string,\n params: unknown[]\n): RawQueryResult {\n const stmt = db.prepare(sql);\n if (stmt.reader) {\n const cols = stmt.columns().map((c) => c.name);\n const rows = stmt.raw(true).all(...params) as unknown[][];\n return { columns: cols, rows, rowsRead: rows.length, rowsWritten: 0 };\n }\n const info = stmt.run(...params);\n return { columns: [], rows: [], rowsRead: 0, rowsWritten: info.changes ?? 0 };\n}\n","/**\n * @persql/sdk — Talk to your PerSQL databases from anywhere.\n *\n * @example\n * const persql = new PerSQL({ token: process.env.PERSQL_TOKEN! });\n * const db = persql.database(\"acme/orders\");\n * const rows = await db.query<{ id: number; email: string }>(\n * \"SELECT id, email FROM customers WHERE id = ?\",\n * [42]\n * );\n */\n\nimport { LocalDriver } from \"./local\";\nexport type { RawQueryResult } from \"./local\";\n\nexport type SqlErrorKind =\n | \"unique_violation\"\n | \"not_null_violation\"\n | \"fk_violation\"\n | \"check_violation\"\n | \"unknown_table\"\n | \"unknown_column\"\n | \"syntax_error\"\n | \"type_mismatch\"\n | \"database_locked\"\n | \"too_many_params\"\n | \"readonly\"\n | \"unknown\";\n\nexport interface SqlErrorDetail {\n kind: SqlErrorKind;\n message: string;\n table?: string;\n column?: string;\n columns?: Array<{ table: string; column: string }>;\n constraint?: string;\n near?: string;\n hint?: string;\n}\n\nexport interface PerSQLOptions {\n /** Bearer token. Get one at https://console.persql.com → Tokens. */\n token?: string;\n /** API base URL. Defaults to https://api.persql.com */\n baseURL?: string;\n /** Custom fetch implementation (defaults to global fetch). */\n fetch?: typeof fetch;\n /**\n * Run against a local SQLite file (via the `better-sqlite3` peer\n * dependency) instead of the HTTP API. Pass `\":memory:\"` for an\n * in-memory test database. When set, `token` is not required and\n * network features (`vectors`, `blob`, `subscribe`) throw —\n * they have no local equivalent.\n *\n * ```ts\n * const persql = new PerSQL({ local: \":memory:\" });\n * const db = persql.database(\"test/db\");\n * await db.query(\"CREATE TABLE t (id INTEGER)\");\n * ```\n */\n local?: string;\n}\n\nexport interface QueryResult<T = Record<string, unknown>> {\n /** Column names in result-set order. */\n columns: string[];\n /** Rows as positional arrays (faster wire format). */\n rows: unknown[][];\n /** Rows reshaped as objects keyed by column name. */\n data: T[];\n rowsRead: number;\n rowsWritten: number;\n}\n\nexport interface BatchOptions {\n /** Wrap all statements in BEGIN/COMMIT. Rolls back on first error. */\n transaction?: boolean;\n /** Pass an Idempotency-Key for safe retries. */\n idempotencyKey?: string;\n /**\n * Multi-step plan idempotency. Pair `planKey` (stable across the\n * agent's plan) with `planStep` (stable per step). On retry, every\n * step that already succeeded replays from cache; failed and\n * never-reached steps re-run.\n */\n planKey?: string;\n planStep?: string;\n}\n\n/**\n * Metadata returned by `PerSQL.fromHandoff` describing what the\n * minted token can touch. The token itself is already wired into the\n * returned client; this is just the handoff's resource info so the\n * agent doesn't have to round-trip again to know its scope.\n */\nexport interface HandoffClaim {\n tokenId: string;\n databaseId: string;\n databaseSlug: string;\n namespaceSlug: string;\n branchRef: string | null;\n role: \"read\" | \"write\" | \"admin\";\n}\n\n/**\n * Engine telemetry row — one entry per /v1/query or /v1/batch call.\n * Returned by `db.queryLog()`. The same data is also exposed inside\n * the DO as `_persql_meta_query_log` so agents can SELECT/JOIN it\n * alongside their own tables.\n */\nexport interface QueryLogEntry {\n id: string;\n ts: number;\n sqlText: string;\n paramsJson: string | null;\n status: \"ok\" | \"error\";\n errorMessage: string | null;\n rowsRead: number;\n rowsWritten: number;\n durationMs: number;\n statementCount: number;\n tokenId: string | null;\n snapshotId: string | null;\n}\n\n/**\n * Output of `db.describe()`. Mirrors the schema graph with any\n * stored semantic docs folded in. Designed to be JSON-stringified\n * straight into an LLM prompt.\n */\nexport interface DescribeBundle {\n databaseDescription: string | null;\n tables: Array<{\n table: string;\n description: string;\n columns: Array<{\n name: string;\n type: string;\n pk: boolean;\n notNull: boolean;\n description: string;\n }>;\n foreignKeys: Array<{ from: string; toTable: string; toColumn: string }>;\n }>;\n}\n\n/** Output of `db.search(query)`. */\nexport interface SchemaSearchResponse {\n query: string;\n hits: Array<{\n kind: \"table\" | \"column\";\n table: string;\n column: string | null;\n description: string;\n score: number;\n }>;\n}\n\n/** Output of `db.doctor()`. */\nexport interface SchemaDoctorReport {\n findings: Array<{\n code: string;\n severity: \"info\" | \"warning\" | \"error\";\n table: string;\n column: string | null;\n message: string;\n suggestion: string;\n }>;\n tablesScanned: number;\n generatedAt: string;\n}\n\nexport interface QueryOptions {\n idempotencyKey?: string;\n /** See `BatchOptions.planKey`. */\n planKey?: string;\n /** See `BatchOptions.planStep`. */\n planStep?: string;\n}\n\nexport interface TableInfo {\n name: string;\n rowCount: number;\n}\n\nexport interface Statement {\n sql: string;\n params?: unknown[];\n}\n\nexport class PerSQLError extends Error {\n status: number;\n // Set on /v1/query and /v1/batch failures with a parsed envelope\n // (kind, table, column, hint, etc.) so callers can branch on\n // `error.detail.kind` instead of string-matching the message.\n detail?: SqlErrorDetail;\n constructor(status: number, message: string, detail?: SqlErrorDetail) {\n super(message);\n this.name = \"PerSQLError\";\n this.status = status;\n this.detail = detail;\n }\n}\n\nexport class RateLimitError extends PerSQLError {\n retryAfterSeconds: number;\n constructor(retryAfterSeconds: number, message = \"Rate limit exceeded\") {\n super(429, message);\n this.name = \"RateLimitError\";\n this.retryAfterSeconds = retryAfterSeconds;\n }\n}\n\nexport interface ApprovalRuleHit {\n ruleId: string;\n tableGlob: string;\n action: \"require_approval\" | \"deny\";\n matchedTable: string;\n note: string | null;\n}\n\n/**\n * Thrown when a write hits an approval rule. The agent can either\n * stop and surface `approvalUrl` to a human, or poll\n * `db.approvals.poll(approvalToken)` until a member decides; once\n * approved, redeem with `db.approvals.redeem(approvalToken)`.\n */\nexport class ApprovalRequiredError extends PerSQLError {\n approvalToken: string;\n approvalUrl: string;\n hits: ApprovalRuleHit[];\n expiresAt: string;\n constructor(opts: {\n message: string;\n approvalToken: string;\n approvalUrl: string;\n hits: ApprovalRuleHit[];\n expiresAt: string;\n }) {\n super(403, opts.message);\n this.name = \"ApprovalRequiredError\";\n this.approvalToken = opts.approvalToken;\n this.approvalUrl = opts.approvalUrl;\n this.hits = opts.hits;\n this.expiresAt = opts.expiresAt;\n }\n}\n\nexport class PerSQL {\n /** @internal — exposed read-only so PerSQLDatabase can build URLs. */\n readonly token: string;\n /** @internal — same. */\n readonly baseURL: string;\n /** @internal — set when running in local mode. */\n readonly local: LocalDriver | null;\n private readonly _fetch: typeof fetch;\n\n constructor(opts: PerSQLOptions) {\n if (opts.local) {\n this.local = new LocalDriver(opts.local);\n this.token = \"\";\n this.baseURL = \"\";\n this._fetch = opts.fetch ?? (globalThis.fetch?.bind(globalThis) as typeof fetch);\n return;\n }\n if (!opts.token) {\n throw new Error(\n 'PerSQL: token is required (or pass { local: \":memory:\" } for tests)'\n );\n }\n this.local = null;\n this.token = opts.token;\n this.baseURL = (opts.baseURL ?? \"https://api.persql.com\").replace(/\\/$/, \"\");\n this._fetch = opts.fetch ?? globalThis.fetch.bind(globalThis);\n }\n\n /** Close the local SQLite connection (no-op in HTTP mode). */\n close(): void {\n this.local?.close();\n }\n\n /**\n * Redeem a handoff token (`phand_…`) for a regular PerSQL client\n * scoped to the handed-off (database, branch, role). Single use:\n * the handoff token is consumed by the call.\n *\n * Common pattern — agent A pins a branch and hands the token to\n * agent B; agent B does:\n *\n * const persql = await PerSQL.fromHandoff(handoffToken);\n * const db = persql.database(persql.handedOff!.namespaceSlug, persql.handedOff!.databaseSlug);\n */\n static async fromHandoff(\n handoffToken: string,\n opts: {\n baseURL?: string;\n name?: string;\n fetch?: typeof fetch;\n } = {}\n ): Promise<PerSQL & { handedOff: HandoffClaim }> {\n const baseURL = (opts.baseURL ?? \"https://api.persql.com\").replace(/\\/$/, \"\");\n const fetcher = opts.fetch ?? globalThis.fetch.bind(globalThis);\n const res = await fetcher(`${baseURL}/v1/handoff/claim`, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ token: handoffToken, name: opts.name }),\n });\n type Envelope = {\n success: boolean;\n data?: HandoffClaim & { plaintext: string };\n error?: string;\n };\n const envelope = (await res.json().catch(() => null)) as Envelope | null;\n if (!res.ok || !envelope?.success || !envelope.data) {\n throw new PerSQLError(\n res.status,\n envelope?.error ?? `Handoff claim failed (${res.status})`\n );\n }\n const { plaintext, ...handedOff } = envelope.data;\n const client = new PerSQL({ token: plaintext, baseURL, fetch: opts.fetch });\n return Object.assign(client, { handedOff });\n }\n\n /**\n * Reference a database by its `<namespace>/<slug>` path.\n * Both forms work: `db(\"acme/orders\")` or `db(\"acme\", \"orders\")`.\n */\n database(path: string): PerSQLDatabase;\n database(namespace: string, slug: string): PerSQLDatabase;\n database(a: string, b?: string): PerSQLDatabase {\n if (b !== undefined) return new PerSQLDatabase(this, a, b);\n const [ns, slug] = a.split(\"/\");\n if (!ns || !slug) {\n throw new Error('PerSQL: database path must be \"<namespace>/<slug>\"');\n }\n return new PerSQLDatabase(this, ns, slug);\n }\n\n /** @internal */\n async request<T>(\n method: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\",\n path: string,\n body?: unknown,\n headers?: Record<string, string>\n ): Promise<T> {\n const res = await this._fetch(`${this.baseURL}${path}`, {\n method,\n headers: {\n Authorization: `Bearer ${this.token}`,\n \"Content-Type\": \"application/json\",\n ...(headers ?? {}),\n },\n body: body !== undefined ? JSON.stringify(body) : undefined,\n });\n\n if (res.status === 429) {\n const retryAfter = Number(res.headers.get(\"retry-after\") ?? \"60\");\n throw new RateLimitError(retryAfter);\n }\n\n type Envelope = {\n success: boolean;\n data?: T;\n error?: string;\n errorDetail?: SqlErrorDetail;\n approvalToken?: string;\n approvalUrl?: string;\n hits?: ApprovalRuleHit[];\n expiresAt?: string;\n };\n let envelope: Envelope | null = null;\n try {\n envelope = (await res.json()) as Envelope;\n } catch {\n // empty body\n }\n\n if (!res.ok || !envelope?.success) {\n const message = envelope?.error ?? `Request failed (${res.status})`;\n if (\n res.status === 403 &&\n envelope?.approvalToken &&\n envelope?.approvalUrl &&\n envelope?.expiresAt\n ) {\n throw new ApprovalRequiredError({\n message,\n approvalToken: envelope.approvalToken,\n approvalUrl: envelope.approvalUrl,\n hits: envelope.hits ?? [],\n expiresAt: envelope.expiresAt,\n });\n }\n throw new PerSQLError(res.status, message, envelope?.errorDetail);\n }\n return envelope.data as T;\n }\n\n /** @internal — raw upload (e.g. blob PUT) returning the ApiResponse data. */\n async requestRaw<T>(\n method: \"PUT\" | \"POST\",\n path: string,\n body: ArrayBuffer | Blob | ReadableStream | string,\n contentType?: string\n ): Promise<T> {\n const res = await this._fetch(`${this.baseURL}${path}`, {\n method,\n headers: {\n Authorization: `Bearer ${this.token}`,\n \"Content-Type\": contentType ?? \"application/octet-stream\",\n },\n body,\n // @ts-expect-error — duplex is required by Node fetch for streaming bodies\n duplex: \"half\",\n });\n if (res.status === 429) {\n const retryAfter = Number(res.headers.get(\"retry-after\") ?? \"60\");\n throw new RateLimitError(retryAfter);\n }\n type Envelope = {\n success: boolean;\n data?: T;\n error?: string;\n errorDetail?: SqlErrorDetail;\n };\n let envelope: Envelope | null = null;\n try {\n envelope = (await res.json()) as Envelope;\n } catch {\n // empty body\n }\n if (!res.ok || !envelope?.success) {\n const message = envelope?.error ?? `Request failed (${res.status})`;\n throw new PerSQLError(res.status, message, envelope?.errorDetail);\n }\n return envelope.data as T;\n }\n\n /** @internal — raw fetch returning the underlying Response (used by blob GET). */\n fetchRaw(method: \"GET\", path: string): Promise<Response> {\n return this._fetch(`${this.baseURL}${path}`, {\n method,\n headers: { Authorization: `Bearer ${this.token}` },\n });\n }\n}\n\nexport class PerSQLDatabase {\n constructor(\n private readonly client: PerSQL,\n public readonly namespace: string,\n public readonly slug: string\n ) {}\n\n /** Run a single SQL statement. */\n async query<T = Record<string, unknown>>(\n sql: string,\n params: unknown[] = [],\n options: QueryOptions = {}\n ): Promise<QueryResult<T>> {\n if (this.client.local) {\n const raw = await this.client.local.query(sql, params);\n return { ...raw, data: rowsToObjects<T>(raw.columns, raw.rows) };\n }\n const headers: Record<string, string> = {};\n if (options.idempotencyKey) headers[\"Idempotency-Key\"] = options.idempotencyKey;\n if (options.planKey) headers[\"Plan-Key\"] = options.planKey;\n if (options.planStep) headers[\"Plan-Step\"] = options.planStep;\n const raw = await this.client.request<{\n columns: string[];\n rows: unknown[][];\n rowsRead: number;\n rowsWritten: number;\n }>(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/query`,\n { sql, params },\n headers\n );\n return {\n ...raw,\n data: rowsToObjects<T>(raw.columns, raw.rows),\n };\n }\n\n /** Run multiple statements in one round-trip, optionally in a transaction. */\n async batch<T = Record<string, unknown>>(\n statements: Statement[],\n options: BatchOptions = {}\n ): Promise<QueryResult<T>[]> {\n if (this.client.local) {\n const raw = await this.client.local.batch(\n statements,\n options.transaction ?? false\n );\n return raw.map((r) => ({ ...r, data: rowsToObjects<T>(r.columns, r.rows) }));\n }\n const headers: Record<string, string> = {};\n if (options.idempotencyKey) headers[\"Idempotency-Key\"] = options.idempotencyKey;\n if (options.planKey) headers[\"Plan-Key\"] = options.planKey;\n if (options.planStep) headers[\"Plan-Step\"] = options.planStep;\n const raw = await this.client.request<\n Array<{\n columns: string[];\n rows: unknown[][];\n rowsRead: number;\n rowsWritten: number;\n }>\n >(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/batch`,\n { statements, transaction: options.transaction ?? false },\n headers\n );\n return raw.map((r) => ({ ...r, data: rowsToObjects<T>(r.columns, r.rows) }));\n }\n\n /** Convenience for a transactional batch. */\n transaction<T = Record<string, unknown>>(\n statements: Statement[],\n options: Omit<BatchOptions, \"transaction\"> = {}\n ): Promise<QueryResult<T>[]> {\n return this.batch<T>(statements, { ...options, transaction: true });\n }\n\n /**\n * Engine telemetry — recent /v1/query and /v1/batch calls issued\n * against this database. Backed by the in-DO `_persql_meta_query_log`\n * table; 7-day retention. Useful for agents that need to replay\n * what they (or another agent) did, audit cost, or stream a live\n * tail via `db.subscribe`.\n */\n async queryLog(opts: {\n since?: string | Date;\n until?: string | Date;\n tokenId?: string;\n status?: \"ok\" | \"error\";\n cursor?: string;\n pageSize?: number;\n } = {}): Promise<{\n data: QueryLogEntry[];\n nextCursor: string | null;\n }> {\n if (this.client.local) {\n throw new Error(\"PerSQL: queryLog is not available in local mode.\");\n }\n const qs = new URLSearchParams();\n if (opts.since) {\n qs.set(\n \"since\",\n typeof opts.since === \"string\" ? opts.since : opts.since.toISOString()\n );\n }\n if (opts.until) {\n qs.set(\n \"until\",\n typeof opts.until === \"string\" ? opts.until : opts.until.toISOString()\n );\n }\n if (opts.tokenId) qs.set(\"tokenId\", opts.tokenId);\n if (opts.status) qs.set(\"status\", opts.status);\n if (opts.cursor) qs.set(\"cursor\", opts.cursor);\n if (opts.pageSize) qs.set(\"pageSize\", String(opts.pageSize));\n const tail = qs.toString() ? `?${qs.toString()}` : \"\";\n const res = await this.client.fetchRaw(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/queries${tail}`\n );\n const body = (await res.json()) as {\n success: boolean;\n data?: QueryLogEntry[];\n error?: string;\n nextCursor?: string | null;\n };\n if (!res.ok || !body.success) {\n throw new PerSQLError(res.status, body.error ?? `Request failed (${res.status})`);\n }\n return { data: body.data ?? [], nextCursor: body.nextCursor ?? null };\n }\n\n /**\n * Returns a structured description of the database — columns, FK\n * graph, and any human-/AI-authored docs persisted via\n * `db.describe(...)`. The shape is designed for direct injection\n * into an LLM prompt: one round-trip and the agent has everything\n * it needs to write correct SQL.\n */\n async describe(): Promise<DescribeBundle> {\n if (this.client.local) {\n throw new Error(\n \"PerSQL: describe is not available in local mode (yet).\"\n );\n }\n return this.client.request<DescribeBundle>(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/describe`\n );\n }\n\n /**\n * Persist semantic descriptions for the database / tables / columns.\n * Pass any subset — only the fields you set are updated. Requires\n * an admin-role token because docs change how every other client\n * (and every agent) interprets the schema.\n */\n async setDescription(input: {\n databaseDescription?: string;\n tables?: Array<{\n table: string;\n description?: string;\n columns?: Array<{ column: string; description: string }>;\n }>;\n }): Promise<DescribeBundle> {\n if (this.client.local) {\n throw new Error(\"PerSQL: setDescription is not available in local mode.\");\n }\n return this.client.request<DescribeBundle>(\n \"PUT\",\n `/v1/db/${this.namespace}/${this.slug}/describe`,\n input\n );\n }\n\n /**\n * Natural-language ranked search across table names, column names,\n * and any descriptions you've stored via `setDescription`. Use this\n * before writing SQL when you don't know the schema cold — one call\n * narrows a 200-table DB down to the handful that match the intent.\n */\n async search(query: string, opts: { limit?: number } = {}): Promise<SchemaSearchResponse> {\n if (this.client.local) {\n throw new Error(\"PerSQL: search is not available in local mode.\");\n }\n const qs = new URLSearchParams({ q: query });\n if (opts.limit) qs.set(\"limit\", String(opts.limit));\n return this.client.request<SchemaSearchResponse>(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/search?${qs.toString()}`\n );\n }\n\n /**\n * Lints the schema for issues that hurt LLM consumption: missing\n * primary keys, ambiguous column names (\"data\", \"value\"), unindexed\n * foreign keys, etc. Pure read-only analysis — emits suggestions\n * for the agent to act on, never modifies the schema itself.\n */\n async doctor(): Promise<SchemaDoctorReport> {\n if (this.client.local) {\n throw new Error(\"PerSQL: doctor is not available in local mode.\");\n }\n return this.client.request<SchemaDoctorReport>(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/doctor`\n );\n }\n\n /** List user-defined tables in this database. */\n async tables(): Promise<TableInfo[]> {\n if (this.client.local) return this.client.local.tables();\n return this.client.request<TableInfo[]>(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/tables`\n );\n }\n\n /**\n * Returns SQLite's `EXPLAIN QUERY PLAN` output for the given SQL.\n * Read-only — does not execute the statement.\n */\n async explain(\n sql: string,\n params: unknown[] = []\n ): Promise<Array<{ id: number; parent: number; detail: string }>> {\n if (this.client.local) return this.client.local.explain(sql, params);\n return this.client.request<\n Array<{ id: number; parent: number; detail: string }>\n >(\"POST\", `/v1/db/${this.namespace}/${this.slug}/explain`, { sql, params });\n }\n\n /**\n * Introspect the database schema. Returns one entry per user table\n * with column definitions, suitable for codegen tools (the\n * `persql-codegen` CLI uses this to emit Drizzle schema files).\n */\n async schema(): Promise<DatabaseSchema> {\n const tables = await this.tables();\n const out: DatabaseSchema = { tables: [] };\n for (const t of tables) {\n const r = await this.query<{\n cid: number;\n name: string;\n type: string;\n notnull: number;\n dflt_value: unknown;\n pk: number;\n }>(`PRAGMA table_info(\"${t.name.replace(/\"/g, '\"\"')}\")`);\n out.tables.push({\n name: t.name,\n columns: r.data.map((c) => ({\n name: c.name,\n type: c.type,\n notNull: !!c.notnull,\n primaryKey: !!c.pk,\n default: c.dflt_value === null ? null : String(c.dflt_value),\n })),\n });\n }\n return out;\n }\n\n /**\n * Per-database semantic search via Cloudflare Vectorize. Use\n * `vectors.upsert` to embed and store rows; `vectors.query` to\n * retrieve the most similar by free text. Embeddings are computed\n * server-side using bge-base-en-v1.5 (768 dim, cosine distance).\n */\n get vectors(): PerSQLVectors {\n if (this.client.local) {\n throw new Error(\n \"PerSQL: vectors require Cloudflare Vectorize and Workers AI — not available in local mode. Use a server-mode token (psql_live_/psql_test_) to call vectors.upsert/query/delete.\"\n );\n }\n return new PerSQLVectors(this.client, this.namespace, this.slug);\n }\n\n /**\n * Manage preview/PR-style branches of this database. Each branch is\n * its own DO with its own SQLite file; create-or-reset by ref is\n * idempotent (PUT semantics) so CI pipelines can call it on every\n * build. Requires an admin-role bearer token for create / delete /\n * merge; list is open to any role.\n */\n get branches(): PerSQLBranches {\n if (this.client.local) {\n throw new Error(\n \"PerSQL: branches model fork-by-Durable-Object — not available in local mode. Use a server-mode token to call branches.upsert/claim/merge, or stub branch isolation in tests by spinning up multiple `new PerSQL({ local: ':memory:' })` instances.\"\n );\n }\n return new PerSQLBranches(this.client, this.namespace, this.slug);\n }\n\n /**\n * Redeem an approval that was minted when a write hit a\n * `require_approval` rule. Throws `ApprovalRequiredError` until a\n * namespace member decides; on approve, runs the original SQL.\n */\n get approvals(): PerSQLApprovals {\n if (this.client.local) {\n throw new Error(\n \"PerSQL: approvals require a server-side approval gate — not available in local mode. Local mode runs every statement immediately; use a server-mode token if you need the require_approval rule.\"\n );\n }\n return new PerSQLApprovals(this.client, this.namespace, this.slug);\n }\n\n /**\n * Pre-flight a write before running it. `propose()` validates the\n * SQL via EXPLAIN, estimates affected rows, and returns a single-use\n * `executionToken` to redeem with `apply()`. Use this when an agent\n * (or its parent / a human) should review a mutation before it runs.\n * Works in both HTTP and local modes — local mode keeps the\n * executionToken in-process.\n */\n get proposals(): PerSQLProposals {\n return new PerSQLProposals(this.client, this.namespace, this.slug);\n }\n\n /**\n * Per-database BLOB storage backed by R2. Use this for anything\n * larger than a SQLite cell (images, PDFs, model weights). Each\n * database has its own private namespace; keys may be hierarchical\n * (`avatars/2025/foo.jpg`) but never start with `/`.\n */\n get blob(): PerSQLBlob {\n if (this.client.local) {\n throw new Error(\n \"PerSQL: blob storage is backed by R2 — not available in local mode. Use a server-mode token, or store blobs in your test fixtures directly.\"\n );\n }\n return new PerSQLBlob(this.client, this.namespace, this.slug);\n }\n\n /**\n * Subscribe to row-changes via WebSocket — the SQL equivalent of\n * Postgres `LISTEN`. The callback fires once per write that\n * matches the table filter. Returns an `unsubscribe` function;\n * call it to close the socket.\n *\n * ```ts\n * const off = db.subscribe({\n * tables: [\"orders\"],\n * onChange: (e) => console.log(e.kind, e.table),\n * });\n * // …later\n * off();\n * ```\n *\n * Authentication uses the `persql.bearer.<token>` sub-protocol so\n * the token doesn't leak via URL logs. In environments without\n * sub-protocol support (some serverless WebSocket clients), the\n * fallback `?token=` query string is also accepted by the\n * server.\n */\n subscribe(options: SubscribeOptions): () => void {\n if (this.client.local) {\n throw new Error(\n \"PerSQL: subscribe requires the DO's WebSocket fan-out — not available in local mode. Use a server-mode token to call subscribe().\"\n );\n }\n const baseHttp = this.client.baseURL;\n const wsBase = baseHttp.replace(/^http/, \"ws\");\n const tablesParam =\n options.tables && options.tables.length > 0\n ? `?tables=${encodeURIComponent(options.tables.join(\",\"))}`\n : \"\";\n const url = `${wsBase}/v1/db/${this.namespace}/${this.slug}/subscribe${tablesParam}`;\n const ws = new WebSocket(url, [`persql.bearer.${this.client.token}`]);\n let closed = false;\n ws.addEventListener(\"message\", (ev) => {\n try {\n const msg = JSON.parse(String((ev as MessageEvent).data)) as {\n type: string;\n table?: string;\n kind?: SubscribeChangeKind;\n message?: string;\n tables?: string[] | \"*\";\n };\n if (msg.type === \"change\" && msg.table && msg.kind) {\n options.onChange({ table: msg.table, kind: msg.kind });\n } else if (msg.type === \"error\") {\n options.onError?.(new Error(msg.message ?? \"Subscription error\"));\n } else if (msg.type === \"subscribed\") {\n options.onReady?.(msg.tables ?? \"*\");\n }\n } catch {\n // ignore malformed frame\n }\n });\n ws.addEventListener(\"error\", () => {\n options.onError?.(new Error(\"WebSocket error\"));\n });\n ws.addEventListener(\"close\", () => {\n if (!closed) options.onClose?.();\n });\n return () => {\n closed = true;\n try {\n ws.close();\n } catch {\n // ignore\n }\n };\n }\n\n /**\n * Returns the callback shape `drizzle-orm/sqlite-proxy` expects.\n * Pair with `drizzle()` from that module to get a typed,\n * fluent ORM client over the PerSQL HTTP API:\n *\n * ```ts\n * import { drizzle } from \"drizzle-orm/sqlite-proxy\";\n * import { PerSQL } from \"@persql/sdk\";\n *\n * const persql = new PerSQL({ token });\n * const db = drizzle(persql.database(\"acme/orders\").driver());\n * ```\n *\n * Drizzle's `prepare` interface maps onto our `query` /\n * `batch`; `method` tells us how to shape the rows.\n */\n driver(): SqliteProxyDriver {\n const callback: SqliteProxyDriver = async (sql, params, method) => {\n const result = await this.query(sql, params ?? []);\n // Drizzle's `get` and `values` expect a tuple of column values\n // (rows[0]); `all` and `run` expect an array of tuples.\n if (method === \"values\" || method === \"get\") {\n return { rows: result.rows[0] ?? [] };\n }\n return { rows: result.rows };\n };\n return callback;\n }\n\n /**\n * Returns a tool definition for use with Anthropic / OpenAI / function-calling\n * agents. Pair it with `runTool` to execute the call.\n */\n asTool(name = \"persql_query\") {\n return {\n anthropic: {\n name,\n description: `Run a SQL query against the PerSQL database \"${this.namespace}/${this.slug}\". Returns up to 1000 rows. Use parameter binding (?) to avoid injection.`,\n input_schema: {\n type: \"object\",\n properties: {\n sql: { type: \"string\", description: \"SQLite SQL statement\" },\n params: {\n type: \"array\",\n items: {},\n description: \"Positional parameters for the SQL statement\",\n },\n },\n required: [\"sql\"],\n },\n },\n openai: {\n type: \"function\" as const,\n function: {\n name,\n description: `Run a SQL query against the PerSQL database \"${this.namespace}/${this.slug}\".`,\n parameters: {\n type: \"object\",\n properties: {\n sql: { type: \"string\" },\n params: { type: \"array\", items: {} },\n },\n required: [\"sql\"],\n },\n },\n },\n };\n }\n\n /**\n * Executes a tool-call payload returned from the model. Pair with `asTool`.\n */\n runTool(input: { sql: string; params?: unknown[] }) {\n return this.query(input.sql, input.params ?? []);\n }\n\n /**\n * Generate a *bundle* of typed tools — one per table — instead of one\n * generic `query` tool. Agents perform dramatically better with narrow\n * typed surfaces than they do with one fat SQL function. Each table\n * gets:\n * - `select_<table>(where?, limit?, orderBy?)` — typed column filter\n * - `count_<table>(where?)` — quick row count\n * - `describe_<table>()` — columns, row count, foreign keys\n * Plus a fallback `sql_query(sql, params)` for anything the typed\n * tools can't express.\n *\n * The returned `run(name, input)` dispatcher executes whichever tool\n * the model invoked. Format the tools for the LLM via the\n * `anthropic` or `openai` arrays (matches the shapes of `asTool`).\n *\n * ```ts\n * const tools = await db.asTools();\n * const reply = await anthropic.messages.create({\n * model: \"claude-opus-4-7\",\n * tools: tools.anthropic,\n * messages: [...],\n * });\n * for (const block of reply.content) {\n * if (block.type === \"tool_use\") {\n * const result = await tools.run(block.name, block.input);\n * }\n * }\n * ```\n */\n async asTools(): Promise<DatabaseToolBundle> {\n const schema = await this.schema();\n const anthropic: AnthropicTool[] = [];\n const openai: OpenAiTool[] = [];\n\n for (const t of schema.tables) {\n const safe = sanitizeToolPart(t.name);\n const columnList = t.columns\n .map((c) => `${c.name} ${c.type}${c.notNull ? \" NOT NULL\" : \"\"}${c.primaryKey ? \" PRIMARY KEY\" : \"\"}`)\n .join(\", \");\n const whereProps: Record<string, { type: string[]; description?: string }> = {};\n for (const c of t.columns) {\n whereProps[c.name] = {\n type: [\"string\", \"number\", \"boolean\", \"null\"],\n description: `Equality filter on ${t.name}.${c.name} (${c.type})`,\n };\n }\n\n const selectName = `select_${safe}`;\n const selectDesc =\n `Read rows from ${t.name}. Columns: ${columnList}. ` +\n `Use \\`where\\` for equality predicates, \\`orderBy\\` to sort, \\`limit\\` to bound the result (default 100, max 1000).`;\n const selectInput = {\n type: \"object\" as const,\n properties: {\n where: {\n type: \"object\" as const,\n description: \"Equality filter — keys are column names, values are scalars.\",\n properties: whereProps,\n additionalProperties: false,\n },\n orderBy: {\n type: \"string\" as const,\n description: \"Column name optionally followed by ASC/DESC.\",\n },\n limit: {\n type: \"integer\" as const,\n minimum: 1,\n maximum: 1000,\n default: 100,\n },\n },\n additionalProperties: false,\n };\n anthropic.push({ name: selectName, description: selectDesc, input_schema: selectInput });\n openai.push({\n type: \"function\",\n function: { name: selectName, description: selectDesc, parameters: selectInput },\n });\n\n const countName = `count_${safe}`;\n const countDesc = `Count rows in ${t.name}, optionally filtered by equality on any column.`;\n const countInput = {\n type: \"object\" as const,\n properties: {\n where: {\n type: \"object\" as const,\n properties: whereProps,\n additionalProperties: false,\n },\n },\n additionalProperties: false,\n };\n anthropic.push({ name: countName, description: countDesc, input_schema: countInput });\n openai.push({\n type: \"function\",\n function: { name: countName, description: countDesc, parameters: countInput },\n });\n\n const descName = `describe_${safe}`;\n const descDesc = `Return columns, row count, and foreign keys for ${t.name}.`;\n const descInput = {\n type: \"object\" as const,\n properties: {},\n additionalProperties: false,\n };\n anthropic.push({ name: descName, description: descDesc, input_schema: descInput });\n openai.push({\n type: \"function\",\n function: { name: descName, description: descDesc, parameters: descInput },\n });\n\n // Mutating per-table tools. We emit these for every user table —\n // the bearer's role still gates execution server-side, so a\n // readonly token calling `insert_*` will get a 403. Skipping\n // generation by role would mean a token upgrade requires a tool\n // re-render at the agent layer.\n const valueProps: Record<\n string,\n { type: string[]; description?: string }\n > = {};\n const requiredForInsert: string[] = [];\n for (const c of t.columns) {\n valueProps[c.name] = {\n type: [\"string\", \"number\", \"boolean\", \"null\"],\n description: `${t.name}.${c.name} (${c.type})${c.notNull ? \" NOT NULL\" : \"\"}`,\n };\n if (c.notNull && !c.primaryKey) {\n // PKs are usually integer-autoinc — leave them optional even\n // when notNull. If the user has a non-rowid PK they'd want to\n // pass it explicitly anyway and the server will fail loudly.\n requiredForInsert.push(c.name);\n }\n }\n\n const insertName = `insert_${safe}`;\n const insertDesc =\n `Insert one row into ${t.name}. ` +\n `Provide column values in \\`values\\`. Required (NOT NULL): ${requiredForInsert.join(\", \") || \"(none)\"}.`;\n const insertInput = {\n type: \"object\" as const,\n properties: {\n values: {\n type: \"object\" as const,\n description: \"Column → value map for the new row.\",\n properties: valueProps,\n required: requiredForInsert,\n additionalProperties: false,\n },\n },\n required: [\"values\"],\n additionalProperties: false,\n };\n anthropic.push({ name: insertName, description: insertDesc, input_schema: insertInput });\n openai.push({\n type: \"function\",\n function: { name: insertName, description: insertDesc, parameters: insertInput },\n });\n\n const updateName = `update_${safe}`;\n const updateDesc =\n `Update rows in ${t.name} matching \\`where\\` (equality on any column) ` +\n `with the columns provided in \\`set\\`. Both must be non-empty — to clear the filter use \\`sql_query\\`.`;\n const updateInput = {\n type: \"object\" as const,\n properties: {\n where: {\n type: \"object\" as const,\n properties: whereProps,\n additionalProperties: false,\n },\n set: {\n type: \"object\" as const,\n properties: valueProps,\n additionalProperties: false,\n },\n },\n required: [\"where\", \"set\"],\n additionalProperties: false,\n };\n anthropic.push({ name: updateName, description: updateDesc, input_schema: updateInput });\n openai.push({\n type: \"function\",\n function: { name: updateName, description: updateDesc, parameters: updateInput },\n });\n\n const deleteName = `delete_${safe}`;\n const deleteDesc =\n `Delete rows from ${t.name} matching \\`where\\`. \\`where\\` must be non-empty — ` +\n `to truncate use \\`sql_query\\` with an explicit DELETE.`;\n const deleteInput = {\n type: \"object\" as const,\n properties: {\n where: {\n type: \"object\" as const,\n properties: whereProps,\n additionalProperties: false,\n },\n },\n required: [\"where\"],\n additionalProperties: false,\n };\n anthropic.push({ name: deleteName, description: deleteDesc, input_schema: deleteInput });\n openai.push({\n type: \"function\",\n function: { name: deleteName, description: deleteDesc, parameters: deleteInput },\n });\n }\n\n const sqlName = \"sql_query\";\n const sqlDesc =\n `Run an arbitrary parameterised SQL statement against the PerSQL database \"${this.namespace}/${this.slug}\". ` +\n `Prefer the table-specific select_/count_/describe_ tools when they fit; fall back to this for joins, aggregates, or DDL.`;\n const sqlInput = {\n type: \"object\" as const,\n properties: {\n sql: { type: \"string\" as const },\n params: { type: \"array\" as const, items: {} },\n },\n required: [\"sql\"],\n additionalProperties: false,\n };\n anthropic.push({ name: sqlName, description: sqlDesc, input_schema: sqlInput });\n openai.push({\n type: \"function\",\n function: { name: sqlName, description: sqlDesc, parameters: sqlInput },\n });\n\n // ---- Discovery tools ---------------------------------------------------\n // What an agent should call BEFORE writing SQL on an unfamiliar\n // database. These don't take args (or just one), are read-scoped,\n // and answer \"what's in here?\" / \"what should I avoid?\".\n const describeDbName = \"describe_database\";\n const describeDbDesc =\n `Return the full schema graph plus stored semantic descriptions for \"${this.namespace}/${this.slug}\". ` +\n `Call this FIRST on any unfamiliar database — it includes table descriptions, column docs, and foreign keys ` +\n `in one shot, sized to JSON-stringify into a system prompt.`;\n const describeDbInput = {\n type: \"object\" as const,\n properties: {},\n additionalProperties: false,\n };\n anthropic.push({\n name: describeDbName,\n description: describeDbDesc,\n input_schema: describeDbInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: describeDbName,\n description: describeDbDesc,\n parameters: describeDbInput,\n },\n });\n\n const searchSchemaName = \"search_schema\";\n const searchSchemaDesc =\n `Natural-language ranked search across table names, column names, and stored descriptions. ` +\n `Use this when you have a goal (\"billing addresses\", \"abandoned carts\") but don't know which table holds it.`;\n const searchSchemaInput = {\n type: \"object\" as const,\n properties: {\n q: {\n type: \"string\" as const,\n description: \"Free-text query — table/column/description tokens are matched.\",\n },\n limit: {\n type: \"integer\" as const,\n minimum: 1,\n maximum: 100,\n default: 25,\n },\n },\n required: [\"q\"] as const,\n additionalProperties: false,\n };\n anthropic.push({\n name: searchSchemaName,\n description: searchSchemaDesc,\n input_schema: searchSchemaInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: searchSchemaName,\n description: searchSchemaDesc,\n parameters: searchSchemaInput,\n },\n });\n\n const doctorName = \"schema_doctor\";\n const doctorDesc =\n `Lint the schema for LLM-hostile patterns — missing primary keys, ambiguous column names, unindexed foreign keys. ` +\n `Read-only; useful before generating SQL so you know what the schema gets wrong.`;\n const doctorInput = {\n type: \"object\" as const,\n properties: {},\n additionalProperties: false,\n };\n anthropic.push({\n name: doctorName,\n description: doctorDesc,\n input_schema: doctorInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: doctorName,\n description: doctorDesc,\n parameters: doctorInput,\n },\n });\n\n // ---- Safety primitives -------------------------------------------------\n // Pre-flight a write before running it. Use propose_mutation for\n // anything that touches > a handful of rows, then either run\n // apply_mutation if the plan looks right, or discard the token.\n const proposeName = \"propose_mutation\";\n const proposeDesc =\n `Pre-flight a SQL write — EXPLAIN-validate, estimate affected rows, and return a single-use executionToken. ` +\n `Nothing is mutated until apply_mutation is called with that token. Use for any UPDATE/DELETE that isn't tightly ` +\n `scoped to a single PK, or any INSERT batch you want to verify.`;\n const proposeInput = {\n type: \"object\" as const,\n properties: {\n sql: { type: \"string\" as const, description: \"SQLite SQL statement.\" },\n params: { type: \"array\" as const, items: {} },\n ttlSec: {\n type: \"integer\" as const,\n minimum: 30,\n maximum: 3600,\n description: \"Token TTL in seconds. Default 600 (10 min).\",\n },\n },\n required: [\"sql\"] as const,\n additionalProperties: false,\n };\n anthropic.push({\n name: proposeName,\n description: proposeDesc,\n input_schema: proposeInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: proposeName,\n description: proposeDesc,\n parameters: proposeInput,\n },\n });\n\n const applyName = \"apply_mutation\";\n const applyDesc =\n `Redeem an executionToken returned by propose_mutation. Single-use — calling twice fails. ` +\n `The actual write runs through the same auto-snapshot + query-log + pricing pipeline as a normal SQL call.`;\n const applyInput = {\n type: \"object\" as const,\n properties: {\n executionToken: {\n type: \"string\" as const,\n description: \"pmut_… token from propose_mutation.\",\n },\n },\n required: [\"executionToken\"] as const,\n additionalProperties: false,\n };\n anthropic.push({\n name: applyName,\n description: applyDesc,\n input_schema: applyInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: applyName,\n description: applyDesc,\n parameters: applyInput,\n },\n });\n\n // ---- Telemetry ---------------------------------------------------------\n // Self-debugging. After a failed write the agent should consult\n // this rather than guess at what went wrong.\n const queryLogName = \"recent_queries\";\n const queryLogDesc =\n `Recent /query and /batch calls against this database — durations, errors, statement counts. ` +\n `Use to debug a failed write you just made, or to spot N+1 patterns in your own behavior.`;\n const queryLogInput = {\n type: \"object\" as const,\n properties: {\n since: {\n type: \"string\" as const,\n description: \"ISO timestamp lower bound.\",\n },\n status: {\n type: \"string\" as const,\n enum: [\"ok\", \"error\"] as const,\n },\n pageSize: {\n type: \"integer\" as const,\n minimum: 1,\n maximum: 200,\n default: 50,\n },\n },\n additionalProperties: false,\n };\n anthropic.push({\n name: queryLogName,\n description: queryLogDesc,\n input_schema: queryLogInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: queryLogName,\n description: queryLogDesc,\n parameters: queryLogInput,\n },\n });\n\n // ---- Branch-management tools ------------------------------------------\n // Branches are full per-database SQLite forks an agent can use as\n // a sandbox — try a migration on a branch, verify the result, then\n // merge or discard. Requires an admin-role bearer token; if the\n // token can't, the server replies 403 which propagates as\n // PerSQLError to the agent.\n const dbRef = `\"${this.namespace}/${this.slug}\"`;\n const branchesList = \"branches_list\";\n const branchesListInput = {\n type: \"object\" as const,\n properties: {},\n additionalProperties: false,\n };\n const branchesListDesc =\n `List preview branches of ${dbRef}. Each branch is its own ` +\n `SQLite database forked from the parent at create time.`;\n anthropic.push({\n name: branchesList,\n description: branchesListDesc,\n input_schema: branchesListInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: branchesList,\n description: branchesListDesc,\n parameters: branchesListInput,\n },\n });\n\n const branchesCreate = \"branches_create\";\n const branchesCreateInput = {\n type: \"object\" as const,\n properties: {\n ref: {\n type: \"string\" as const,\n description:\n \"Stable identifier for the branch (e.g. 'pr-42' or 'agent-run-7c2f'). PUT semantics — the same ref re-runs as a reset from the parent's current state.\",\n },\n name: {\n type: \"string\" as const,\n description: \"Optional human-readable name.\",\n },\n ttlDays: {\n type: [\"integer\", \"null\"] as const,\n minimum: 1,\n maximum: 30,\n description:\n \"Auto-delete after N days (1..30). Use for ephemeral / scratch experiments.\",\n },\n },\n required: [\"ref\"] as const,\n additionalProperties: false,\n };\n const branchesCreateDesc =\n `Create-or-reset a branch of ${dbRef} keyed by ref. Idempotent: ` +\n `repeat the same ref to refresh from the current parent. Requires an ` +\n `admin-role token.`;\n anthropic.push({\n name: branchesCreate,\n description: branchesCreateDesc,\n input_schema: branchesCreateInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: branchesCreate,\n description: branchesCreateDesc,\n parameters: branchesCreateInput,\n },\n });\n\n const branchesDelete = \"branches_delete\";\n const branchesDeleteInput = {\n type: \"object\" as const,\n properties: {\n ref: { type: \"string\" as const, description: \"The branch ref to delete.\" },\n },\n required: [\"ref\"] as const,\n additionalProperties: false,\n };\n const branchesDeleteDesc =\n `Delete a branch of ${dbRef}. Removes the branch's database and ` +\n `all of its data. Requires an admin-role token.`;\n anthropic.push({\n name: branchesDelete,\n description: branchesDeleteDesc,\n input_schema: branchesDeleteInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: branchesDelete,\n description: branchesDeleteDesc,\n parameters: branchesDeleteInput,\n },\n });\n\n const branchesMergeInput = {\n type: \"object\" as const,\n properties: {\n ref: { type: \"string\" as const, description: \"The branch ref to apply.\" },\n mode: {\n type: \"string\" as const,\n enum: [\"schema\", \"promote\"] as const,\n description:\n \"schema (default): apply the DDL delta — adds, plus replaces for views/triggers/indexes. Refuses table-shape changes. promote: replace the parent's full contents with the branch's (the parent keeps its identity).\",\n },\n dropRemoved: {\n type: \"boolean\" as const,\n description:\n \"Schema mode only — also drop objects present in the parent but absent in the branch.\",\n },\n },\n required: [\"ref\"] as const,\n additionalProperties: false,\n };\n const branchesPreview = \"branches_preview_merge\";\n const branchesPreviewDesc =\n `Preview merging a branch of ${dbRef} back into its parent — ` +\n `returns the plan (added/changed/removed objects) without writing. ` +\n `Use this before branches_merge to show the user what will happen.`;\n anthropic.push({\n name: branchesPreview,\n description: branchesPreviewDesc,\n input_schema: branchesMergeInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: branchesPreview,\n description: branchesPreviewDesc,\n parameters: branchesMergeInput,\n },\n });\n\n const claimBranchName = \"claim_branch\";\n const claimBranchDesc =\n `One-shot lease: create a fresh branch of ${dbRef} AND mint a scoped psql_live_ token in the same call. ` +\n `Use this when delegating work to a sub-agent — return the token to the sub-agent, walk away, and the branch + ` +\n `token both expire when the lease ends. Requires an admin-role token.`;\n const claimBranchInput = {\n type: \"object\" as const,\n properties: {\n purpose: {\n type: \"string\" as const,\n description:\n \"Free-text label baked into the auto-generated branch ref (e.g. 'agent-run-fix-issue-742').\",\n },\n ttlSec: {\n type: \"integer\" as const,\n minimum: 60,\n maximum: 60 * 60 * 24 * 30,\n description: \"Lease duration in seconds. Default 3600 (1 hour).\",\n },\n role: {\n type: \"string\" as const,\n enum: [\"read\", \"write\", \"admin\"] as const,\n description: \"Role for the minted token. Default 'write'.\",\n },\n },\n additionalProperties: false,\n };\n anthropic.push({\n name: claimBranchName,\n description: claimBranchDesc,\n input_schema: claimBranchInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: claimBranchName,\n description: claimBranchDesc,\n parameters: claimBranchInput,\n },\n });\n\n const branchesMerge = \"branches_merge\";\n const branchesMergeDesc =\n `Apply a branch back into ${dbRef}. Auto-snapshots the parent first ` +\n `(rollback via /restore with the returned snapshotId). Requires an ` +\n `admin-role token.`;\n anthropic.push({\n name: branchesMerge,\n description: branchesMergeDesc,\n input_schema: branchesMergeInput,\n });\n openai.push({\n type: \"function\",\n function: {\n name: branchesMerge,\n description: branchesMergeDesc,\n parameters: branchesMergeInput,\n },\n });\n\n const tableBySafe = new Map<string, (typeof schema)[\"tables\"][number]>();\n for (const t of schema.tables) tableBySafe.set(sanitizeToolPart(t.name), t);\n\n const run = async (name: string, input: Record<string, unknown> = {}) => {\n if (name === sqlName) {\n const sql = String(input.sql ?? \"\");\n const params = Array.isArray(input.params) ? (input.params as unknown[]) : [];\n return this.query(sql, params);\n }\n if (name === describeDbName) {\n return this.describe();\n }\n if (name === searchSchemaName) {\n const q = String(input.q ?? \"\");\n if (!q) throw new Error(\"q is required\");\n const limit =\n typeof input.limit === \"number\" ? input.limit : undefined;\n return this.search(q, limit !== undefined ? { limit } : {});\n }\n if (name === doctorName) {\n return this.doctor();\n }\n if (name === proposeName) {\n const sql = String(input.sql ?? \"\");\n if (!sql) throw new Error(\"sql is required\");\n const params = Array.isArray(input.params)\n ? (input.params as unknown[])\n : undefined;\n const ttlSec =\n typeof input.ttlSec === \"number\" ? input.ttlSec : undefined;\n return this.proposals.propose(sql, {\n params,\n ttlSec,\n });\n }\n if (name === applyName) {\n const token = String(input.executionToken ?? \"\");\n if (!token) throw new Error(\"executionToken is required\");\n return this.proposals.apply(token);\n }\n if (name === queryLogName) {\n const opts: {\n since?: string;\n status?: \"ok\" | \"error\";\n pageSize?: number;\n } = {};\n if (typeof input.since === \"string\") opts.since = input.since;\n if (input.status === \"ok\" || input.status === \"error\")\n opts.status = input.status;\n if (typeof input.pageSize === \"number\") opts.pageSize = input.pageSize;\n return this.queryLog(opts);\n }\n if (name === claimBranchName) {\n const opts: {\n purpose?: string;\n ttlSec?: number;\n role?: \"read\" | \"write\" | \"admin\";\n } = {};\n if (typeof input.purpose === \"string\") opts.purpose = input.purpose;\n if (typeof input.ttlSec === \"number\") opts.ttlSec = input.ttlSec;\n if (\n input.role === \"read\" ||\n input.role === \"write\" ||\n input.role === \"admin\"\n )\n opts.role = input.role;\n return this.branches.claim(opts);\n }\n if (name === branchesList) {\n return this.branches.list();\n }\n if (name === branchesCreate) {\n const ref = String(input.ref ?? \"\");\n if (!ref) throw new Error(\"ref is required\");\n const ttl = input.ttlDays;\n return this.branches.upsert(ref, {\n name: typeof input.name === \"string\" ? input.name : undefined,\n ttlDays: typeof ttl === \"number\" ? ttl : ttl === null ? null : undefined,\n });\n }\n if (name === branchesDelete) {\n const ref = String(input.ref ?? \"\");\n if (!ref) throw new Error(\"ref is required\");\n return this.branches.delete(ref);\n }\n if (name === branchesPreview || name === branchesMerge) {\n const ref = String(input.ref ?? \"\");\n if (!ref) throw new Error(\"ref is required\");\n const mode =\n input.mode === \"promote\" ? \"promote\" : (\"schema\" as BranchMergeMode);\n return this.branches.merge(ref, {\n mode,\n preview: name === branchesPreview,\n dropRemoved: input.dropRemoved === true,\n });\n }\n const m = name.match(/^(select|count|describe|insert|update|delete)_(.+)$/);\n if (!m) throw new Error(`Unknown tool: ${name}`);\n const [, op, tableSafe] = m;\n const t = tableBySafe.get(tableSafe);\n if (!t) throw new Error(`Unknown table for tool ${name}`);\n const ident = quoteIdent(t.name);\n const colSetForT = new Set(t.columns.map((c) => c.name));\n if (op === \"insert\") {\n const values = (input.values ?? {}) as Record<string, unknown>;\n const cols = Object.keys(values);\n if (cols.length === 0) throw new Error(\"values must be non-empty\");\n for (const k of cols) {\n if (!colSetForT.has(k)) throw new Error(`Unknown column: ${k}`);\n }\n const placeholders = cols.map(() => \"?\").join(\", \");\n const colSql = cols.map(quoteIdent).join(\", \");\n return this.query(\n `INSERT INTO ${ident} (${colSql}) VALUES (${placeholders})`,\n cols.map((c) => values[c])\n );\n }\n if (op === \"update\") {\n const where = (input.where ?? {}) as Record<string, unknown>;\n const setObj = (input.set ?? {}) as Record<string, unknown>;\n const setKeys = Object.keys(setObj);\n const whereKeys = Object.keys(where);\n if (setKeys.length === 0) throw new Error(\"set must be non-empty\");\n if (whereKeys.length === 0)\n throw new Error(\"where must be non-empty — use sql_query for unfiltered updates\");\n for (const k of [...setKeys, ...whereKeys]) {\n if (!colSetForT.has(k)) throw new Error(`Unknown column: ${k}`);\n }\n const setSql = setKeys.map((k) => `${quoteIdent(k)} = ?`).join(\", \");\n const whereSqlParts: string[] = [];\n const whereParams: unknown[] = [];\n for (const k of whereKeys) {\n const v = where[k];\n if (v === null) {\n whereSqlParts.push(`${quoteIdent(k)} IS NULL`);\n } else {\n whereSqlParts.push(`${quoteIdent(k)} = ?`);\n whereParams.push(v);\n }\n }\n return this.query(\n `UPDATE ${ident} SET ${setSql} WHERE ${whereSqlParts.join(\" AND \")}`,\n [...setKeys.map((k) => setObj[k]), ...whereParams]\n );\n }\n if (op === \"delete\") {\n const where = (input.where ?? {}) as Record<string, unknown>;\n const whereKeys = Object.keys(where);\n if (whereKeys.length === 0)\n throw new Error(\"where must be non-empty — use sql_query for unfiltered deletes\");\n for (const k of whereKeys) {\n if (!colSetForT.has(k)) throw new Error(`Unknown column: ${k}`);\n }\n const whereSqlParts: string[] = [];\n const whereParams: unknown[] = [];\n for (const k of whereKeys) {\n const v = where[k];\n if (v === null) {\n whereSqlParts.push(`${quoteIdent(k)} IS NULL`);\n } else {\n whereSqlParts.push(`${quoteIdent(k)} = ?`);\n whereParams.push(v);\n }\n }\n return this.query(\n `DELETE FROM ${ident} WHERE ${whereSqlParts.join(\" AND \")}`,\n whereParams\n );\n }\n if (op === \"describe\") {\n const [info, fks, count] = await Promise.all([\n this.query(`PRAGMA table_info(${ident})`),\n this.query(`PRAGMA foreign_key_list(${ident})`),\n this.query(`SELECT COUNT(*) AS n FROM ${ident}`),\n ]);\n return {\n table: t.name,\n rowCount: Number((count.rows[0]?.[0] as number) ?? 0),\n columns: info.data,\n foreignKeys: fks.data,\n };\n }\n const where = (input.where ?? {}) as Record<string, unknown>;\n const colSet = new Set(t.columns.map((c) => c.name));\n const clauses: string[] = [];\n const params: unknown[] = [];\n for (const [k, v] of Object.entries(where)) {\n if (!colSet.has(k)) throw new Error(`Unknown column: ${k}`);\n if (v === null) {\n clauses.push(`${quoteIdent(k)} IS NULL`);\n } else {\n clauses.push(`${quoteIdent(k)} = ?`);\n params.push(v);\n }\n }\n const whereSql = clauses.length ? ` WHERE ${clauses.join(\" AND \")}` : \"\";\n\n if (op === \"count\") {\n const r = await this.query(`SELECT COUNT(*) AS n FROM ${ident}${whereSql}`, params);\n return { count: Number((r.rows[0]?.[0] as number) ?? 0) };\n }\n\n const limit = Math.min(1000, Math.max(1, Number(input.limit ?? 100)));\n let orderSql = \"\";\n if (typeof input.orderBy === \"string\" && input.orderBy.trim()) {\n const ob = input.orderBy.trim();\n const obMatch = ob.match(/^([A-Za-z_][A-Za-z0-9_]*)\\s*(asc|desc)?$/i);\n if (!obMatch) throw new Error(\"orderBy must be `<col>` or `<col> ASC|DESC`\");\n const [, col, dir] = obMatch;\n if (!colSet.has(col)) throw new Error(`Unknown column: ${col}`);\n orderSql = ` ORDER BY ${quoteIdent(col)} ${(dir ?? \"ASC\").toUpperCase()}`;\n }\n const r = await this.query(\n `SELECT * FROM ${ident}${whereSql}${orderSql} LIMIT ${limit}`,\n params\n );\n return r;\n };\n\n const aiSdk = (): Record<string, AiSdkTool> => {\n const out: Record<string, AiSdkTool> = {};\n for (const t of anthropic) {\n out[t.name] = {\n description: t.description,\n inputSchema: t.input_schema,\n execute: (input) => run(t.name, input),\n };\n }\n return out;\n };\n\n const mastra = (): Record<string, MastraTool> => {\n const out: Record<string, MastraTool> = {};\n for (const t of anthropic) {\n out[t.name] = {\n id: t.name,\n description: t.description,\n inputSchema: t.input_schema,\n execute: ({ context }) => run(t.name, context),\n };\n }\n return out;\n };\n\n const langchain = (): LangChainTool[] =>\n anthropic.map((t) => ({\n name: t.name,\n description: t.description,\n schema: t.input_schema,\n invoke: (input: Record<string, unknown>) => run(t.name, input),\n }));\n\n const openaiAgents = (): OpenAiAgentsTool[] =>\n anthropic.map((t) => ({\n type: \"function\" as const,\n name: t.name,\n description: t.description,\n parameters: t.input_schema,\n invoke: (input: Record<string, unknown>) => run(t.name, input),\n }));\n\n return { anthropic, openai, run, aiSdk, mastra, langchain, openaiAgents };\n }\n}\n\n// SQL identifiers in tool names — `.` in `schema.table` names, hyphens,\n// etc. — get sanitized to underscores so tool names match\n// `^[a-zA-Z0-9_-]+$` everywhere.\nfunction sanitizeToolPart(s: string): string {\n return s.replace(/[^A-Za-z0-9_]/g, \"_\");\n}\n\nfunction quoteIdent(s: string): string {\n return `\"${s.replace(/\"/g, '\"\"')}\"`;\n}\n\nexport interface AnthropicTool {\n name: string;\n description: string;\n input_schema: Record<string, unknown>;\n}\n\nexport interface OpenAiTool {\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n };\n}\n\nexport interface DatabaseToolBundle {\n /** Tool definitions in Anthropic shape — pass as `tools` to messages.create. */\n anthropic: AnthropicTool[];\n /** Tool definitions in OpenAI shape — pass as `tools` to chat.completions.create. */\n openai: OpenAiTool[];\n /** Dispatch a model-emitted tool call back to the database. */\n run: (name: string, input?: Record<string, unknown>) => Promise<unknown>;\n /**\n * Vercel AI SDK shape — record of `{ [toolName]: { description, inputSchema, execute } }`.\n * Spread into `streamText({ tools: { ...db.asTools().aiSdk() } })`. The\n * `inputSchema` field is JSON Schema; pass through `jsonSchema()` from\n * the `ai` package if your version of AI SDK requires it explicitly.\n */\n aiSdk: () => Record<string, AiSdkTool>;\n /**\n * Mastra shape — record of `{ [toolName]: { id, description, inputSchema, execute } }`.\n * Useful for `createTool({...})` callsites. `inputSchema` is JSON Schema;\n * if Mastra's runtime needs a Zod schema, run it through `json-schema-to-zod`\n * or your preferred converter.\n */\n mastra: () => Record<string, MastraTool>;\n /**\n * LangChain shape — array of `{ name, description, schema, invoke }`.\n * Wrap with `DynamicStructuredTool` or pass `invoke` directly to a\n * custom agent loop.\n */\n langchain: () => LangChainTool[];\n /**\n * OpenAI Agents SDK shape — array of `{ type, name, description, parameters, invoke }`.\n * Hand to `Agent({ tools: [...] })`.\n */\n openaiAgents: () => OpenAiAgentsTool[];\n}\n\nexport interface AiSdkTool {\n description: string;\n inputSchema: Record<string, unknown>;\n execute: (input: Record<string, unknown>) => Promise<unknown>;\n}\n\nexport interface MastraTool {\n id: string;\n description: string;\n inputSchema: Record<string, unknown>;\n execute: (context: { context: Record<string, unknown> }) => Promise<unknown>;\n}\n\n/**\n * LangChain shape — pass to `DynamicStructuredTool` or use `invoke`\n * directly inside a custom agent. `schema` is JSON Schema; if your\n * LangChain version requires Zod, convert with `json-schema-to-zod`.\n */\nexport interface LangChainTool {\n name: string;\n description: string;\n schema: Record<string, unknown>;\n invoke: (input: Record<string, unknown>) => Promise<unknown>;\n}\n\n/**\n * OpenAI Agents SDK shape — assign as a tool in `Agent` config. The\n * Agents SDK uses `parameters` (JSON Schema) and a callable; our\n * `invoke` matches the `function_tool` callback signature.\n */\nexport interface OpenAiAgentsTool {\n type: \"function\";\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n invoke: (input: Record<string, unknown>) => Promise<unknown>;\n}\n\nexport type SubscribeChangeKind =\n | \"update\"\n | \"insert\"\n | \"delete\"\n | \"schema\"\n | \"unknown\";\n\nexport interface SubscribeChange {\n table: string;\n kind: SubscribeChangeKind;\n}\n\nexport interface SubscribeOptions {\n /** Tables to listen to. Omit / empty / `[\"*\"]` = all tables. */\n tables?: string[];\n onChange: (change: SubscribeChange) => void;\n /** Fires once when the server confirms the subscription. */\n onReady?: (tables: string[] | \"*\") => void;\n onError?: (error: Error) => void;\n onClose?: () => void;\n}\n\nexport interface BlobListItem {\n key: string;\n sizeBytes: number;\n etag: string;\n uploadedAt: string;\n contentType?: string;\n}\n\nexport interface BlobListResponse {\n items: BlobListItem[];\n truncated: boolean;\n cursor?: string;\n}\n\nexport class PerSQLBlob {\n constructor(\n private readonly client: PerSQL,\n private readonly namespace: string,\n private readonly slug: string\n ) {}\n\n /** Upload a blob. Body can be ArrayBuffer, Blob, ReadableStream, or string. */\n async put(\n key: string,\n body: ArrayBuffer | Blob | ReadableStream | string,\n opts: { contentType?: string } = {}\n ): Promise<{ key: string; sizeBytes: number; etag: string }> {\n const path = `/v1/db/${this.namespace}/${this.slug}/blobs/${encodeBlobKey(key)}`;\n return this.client.requestRaw<{ key: string; sizeBytes: number; etag: string }>(\n \"PUT\",\n path,\n body,\n opts.contentType\n );\n }\n\n /** Fetch a blob. Returns null if missing. */\n async get(key: string): Promise<Response | null> {\n const path = `/v1/db/${this.namespace}/${this.slug}/blobs/${encodeBlobKey(key)}`;\n const res = await this.client.fetchRaw(\"GET\", path);\n if (res.status === 404) return null;\n if (!res.ok) {\n throw new PerSQLError(res.status, `Blob fetch failed (${res.status})`);\n }\n return res;\n }\n\n /** Delete a blob. */\n async delete(key: string): Promise<void> {\n const path = `/v1/db/${this.namespace}/${this.slug}/blobs/${encodeBlobKey(key)}`;\n await this.client.request<{ deleted: true }>(\"DELETE\", path);\n }\n\n /** List blobs by prefix. */\n async list(\n opts: { prefix?: string; cursor?: string; limit?: number } = {}\n ): Promise<BlobListResponse> {\n const params = new URLSearchParams();\n if (opts.prefix) params.set(\"prefix\", opts.prefix);\n if (opts.cursor) params.set(\"cursor\", opts.cursor);\n if (opts.limit) params.set(\"limit\", String(opts.limit));\n const qs = params.toString();\n const path =\n `/v1/db/${this.namespace}/${this.slug}/blobs` + (qs ? `?${qs}` : \"\");\n return this.client.request<BlobListResponse>(\"GET\", path);\n }\n}\n\nfunction encodeBlobKey(key: string): string {\n return key.split(\"/\").map(encodeURIComponent).join(\"/\");\n}\n\nexport interface VectorUpsertItem {\n id: string;\n text: string;\n metadata?: Record<string, string | number | boolean>;\n}\n\nexport interface VectorMatch {\n id: string;\n score: number;\n metadata: Record<string, unknown>;\n}\n\nexport class PerSQLVectors {\n constructor(\n private readonly client: PerSQL,\n private readonly namespace: string,\n private readonly slug: string\n ) {}\n\n /** Embed and upsert up to 100 items in one call. */\n async upsert(items: VectorUpsertItem[]): Promise<{ count: number }> {\n return this.client.request<{ count: number }>(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/vectors`,\n { items }\n );\n }\n\n /** Top-K nearest neighbours by free-text query. */\n async query(\n text: string,\n opts: {\n topK?: number;\n filter?: Record<string, string | number | boolean>;\n } = {}\n ): Promise<VectorMatch[]> {\n return this.client.request<VectorMatch[]>(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/vectors/query`,\n { query: text, topK: opts.topK, filter: opts.filter }\n );\n }\n\n /** Delete vectors by id. Up to 1000 ids per call. */\n async delete(ids: string[]): Promise<{ count: number }> {\n return this.client.request<{ count: number }>(\n \"DELETE\",\n `/v1/db/${this.namespace}/${this.slug}/vectors`,\n { ids }\n );\n }\n}\n\n/**\n * Database row as returned by /v1 list/upsert branch responses.\n * Mirrors the `Database` DTO from @persql/shared but kept narrow on\n * purpose so the SDK doesn't drift if the server adds fields.\n */\nexport interface BranchInfo {\n id: string;\n namespaceId: string;\n slug: string;\n name: string;\n status: string;\n region: string;\n branchRef: string | null;\n forkedAt: string | null;\n expiresAt: string | null;\n createdAt: string;\n updatedAt: string;\n forkedFrom?: { id: string; slug: string; name: string };\n}\n\nexport type BranchMergeMode = \"schema\" | \"promote\";\n\nexport type BranchMergePlanStep =\n | { op: \"create\"; type: string; name: string; sql: string }\n | { op: \"drop\"; type: string; name: string }\n | {\n op: \"replace\";\n type: string;\n name: string;\n beforeSql: string;\n afterSql: string;\n };\n\nexport interface BranchMergeResult {\n mode: BranchMergeMode;\n preview: boolean;\n applied: number;\n snapshotId: string | null;\n plan?: BranchMergePlanStep[];\n summary?: { added: number; changed: number; removed: number };\n note?: string;\n}\n\nexport interface BranchUpsertOptions {\n /** Human-readable name. Defaults to `<parent name> (<ref>)`. */\n name?: string;\n /** Region hint: auto, wnam, enam, weur, eeur, apac. Default: auto. */\n region?: string;\n /** Auto-delete after N days (1..30). Null/undefined = keep until deleted. */\n ttlDays?: number | null;\n}\n\nexport interface BranchMergeOptions {\n /** Default `schema`. `promote` replaces parent contents wholesale. */\n mode?: BranchMergeMode;\n /** If true, returns the plan without writing. */\n preview?: boolean;\n /** Schema mode only — drop objects present in parent but not in branch. */\n dropRemoved?: boolean;\n /** Default true — captures `pre-merge:<ref>` snapshot of the parent. */\n snapshot?: boolean;\n}\n\nexport interface ProposalPlan {\n sql: string;\n plan: unknown[][];\n estimatedAffectedRows: number | null;\n executionToken: string;\n expiresAt: string;\n action: \"read\" | \"write\" | \"admin\";\n}\n\nexport interface ProposeOptions {\n params?: unknown[];\n ttlSec?: number;\n}\n\nexport class PerSQLApprovals {\n constructor(\n private readonly client: PerSQL,\n private readonly namespace: string,\n private readonly slug: string\n ) {}\n\n /**\n * Redeem an approved approvalToken. The bearer must be the same one\n * that minted it, and the request must target the same database.\n * Returns the result of the originally-blocked query or batch.\n */\n async redeem<T = Record<string, unknown>>(\n approvalToken: string\n ): Promise<QueryResult<T> | QueryResult<T>[]> {\n const raw = await this.client.request<\n | {\n columns: string[];\n rows: unknown[][];\n rowsRead: number;\n rowsWritten: number;\n }\n | Array<{\n columns: string[];\n rows: unknown[][];\n rowsRead: number;\n rowsWritten: number;\n }>\n >(\"POST\", `/v1/db/${this.namespace}/${this.slug}/redeem_approval`, {\n approvalToken,\n });\n if (Array.isArray(raw)) {\n return raw.map((r) => ({\n ...r,\n data: rowsToObjects<T>(r.columns, r.rows),\n }));\n }\n return { ...raw, data: rowsToObjects<T>(raw.columns, raw.rows) };\n }\n}\n\nexport class PerSQLProposals {\n constructor(\n private readonly client: PerSQL,\n private readonly namespace: string,\n private readonly slug: string\n ) {}\n\n /**\n * Pre-flight an SQL statement. EXPLAIN-validates, estimates affected\n * rows, and returns a single-use `executionToken` valid for 10 min\n * (override with `ttlSec`, max 1 hour). Nothing is mutated until\n * `apply()` is called.\n */\n async propose(sql: string, opts: ProposeOptions = {}): Promise<ProposalPlan> {\n if (this.client.local) {\n return this.client.local.propose(sql, opts.params ?? [], opts.ttlSec);\n }\n return this.client.request<ProposalPlan>(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/propose`,\n {\n sql,\n params: opts.params ?? [],\n ttlSec: opts.ttlSec,\n }\n );\n }\n\n /**\n * Redeem an `executionToken` from `propose()`. Single-use — calling\n * twice with the same token throws 404. The token is also pinned to\n * the originating bearer + database; cross-token redemption throws\n * 403.\n */\n async apply<T = Record<string, unknown>>(\n executionToken: string\n ): Promise<QueryResult<T>> {\n if (this.client.local) {\n const raw = await this.client.local.apply(executionToken);\n return { ...raw, data: rowsToObjects<T>(raw.columns, raw.rows) };\n }\n const raw = await this.client.request<{\n columns: string[];\n rows: unknown[][];\n rowsRead: number;\n rowsWritten: number;\n }>(\"POST\", `/v1/db/${this.namespace}/${this.slug}/apply`, { executionToken });\n return {\n ...raw,\n data: rowsToObjects<T>(raw.columns, raw.rows),\n };\n }\n}\n\nexport interface ClaimedBranch {\n branchRef: string;\n databaseId: string;\n databaseSlug: string;\n namespaceSlug: string;\n /** Plaintext psql_live_/psql_test_ token. Returned once. */\n token: string;\n tokenId: string;\n role: \"read\" | \"write\" | \"admin\";\n expiresAt: string;\n outcome: \"created\" | \"reset\";\n}\n\nexport interface ClaimBranchOptions {\n /** Explicit ref. Omit to auto-generate from `purpose`. */\n ref?: string;\n /** Free-form label baked into the auto-ref + token name. */\n purpose?: string;\n /** Lease length in seconds. Default 3600 (1 hour), max 30 days. */\n ttlSec?: number;\n /** Default \"write\". */\n role?: \"read\" | \"write\" | \"admin\";\n region?: string;\n}\n\nexport class PerSQLBranches {\n constructor(\n private readonly client: PerSQL,\n private readonly namespace: string,\n private readonly slug: string\n ) {}\n\n /**\n * One-shot lease — create or reset a branch with a TTL and mint a\n * scoped token in the same call. Use at the start of an agent run\n * instead of chaining create + token mint.\n */\n async claim(opts: ClaimBranchOptions = {}): Promise<ClaimedBranch> {\n return this.client.request<ClaimedBranch>(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/claim_branch`,\n opts\n );\n }\n\n /** List branches of this database. Any-role token. */\n async list(opts: { cursor?: string; pageSize?: number } = {}): Promise<{\n data: BranchInfo[];\n nextCursor: string | null;\n }> {\n const qs = new URLSearchParams();\n if (opts.cursor) qs.set(\"cursor\", opts.cursor);\n if (opts.pageSize) qs.set(\"pageSize\", String(opts.pageSize));\n const tail = qs.toString() ? `?${qs.toString()}` : \"\";\n // The list endpoint returns the data envelope plus a `nextCursor`\n // sibling; request the raw envelope by going through fetchRaw.\n const res = await this.client.fetchRaw(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/branches${tail}`\n );\n const body = (await res.json()) as {\n success: boolean;\n data?: BranchInfo[];\n error?: string;\n nextCursor?: string | null;\n };\n if (!res.ok || !body.success) {\n throw new PerSQLError(res.status, body.error ?? `Request failed (${res.status})`);\n }\n return { data: body.data ?? [], nextCursor: body.nextCursor ?? null };\n }\n\n /**\n * Create-or-reset a branch by ref. Idempotent: re-PUTting the same\n * ref refreshes the branch from the parent's current state.\n * Requires an admin-role token.\n */\n async upsert(\n ref: string,\n opts: BranchUpsertOptions = {}\n ): Promise<BranchInfo> {\n return this.client.request<BranchInfo>(\n \"PUT\",\n `/v1/db/${this.namespace}/${this.slug}/branches/${encodeURIComponent(ref)}`,\n {\n name: opts.name,\n region: opts.region,\n ttlDays: opts.ttlDays ?? null,\n }\n );\n }\n\n /** Delete a branch by ref. Requires an admin-role token. */\n async delete(ref: string): Promise<{ deleted: true }> {\n return this.client.request<{ deleted: true }>(\n \"DELETE\",\n `/v1/db/${this.namespace}/${this.slug}/branches/${encodeURIComponent(ref)}`\n );\n }\n\n /**\n * Apply a branch back into its parent. `preview: true` returns the\n * plan without writing. Requires an admin-role token.\n */\n async merge(\n ref: string,\n opts: BranchMergeOptions = {}\n ): Promise<BranchMergeResult> {\n return this.client.request<BranchMergeResult>(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/branches/${encodeURIComponent(ref)}/merge`,\n {\n mode: opts.mode ?? \"schema\",\n preview: opts.preview === true,\n dropRemoved: opts.dropRemoved === true,\n snapshot: opts.snapshot !== false,\n }\n );\n }\n\n /** Convenience for `merge(ref, { preview: true, ...opts })`. */\n preview(\n ref: string,\n opts: Omit<BranchMergeOptions, \"preview\"> = {}\n ): Promise<BranchMergeResult> {\n return this.merge(ref, { ...opts, preview: true });\n }\n\n /**\n * Mint a single-use handoff token for this branch. The receiving\n * agent calls `PerSQL.fromHandoff(token)` to redeem it for a\n * regular bearer token scoped to exactly this (database, branch,\n * role) — the agent that minted the handoff never has to share\n * its own token. Single-use, ≤24h TTL.\n */\n async pin(\n ref: string,\n opts: { role?: \"read\" | \"write\" | \"admin\"; ttlSec?: number } = {}\n ): Promise<{\n token: string;\n expiresAt: string;\n role: \"read\" | \"write\" | \"admin\";\n databaseId: string;\n branchRef: string | null;\n }> {\n return this.client.request(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/branches/${encodeURIComponent(ref)}/pin`,\n { role: opts.role ?? \"write\", ttlSec: opts.ttlSec }\n );\n }\n\n /**\n * Render the branch's schema delta as a committable SQL migration.\n * Read-only. Returns `{ name, sql, plan, summary }` — `name` is a\n * timestamped filename (`YYYYMMDDHHMMSS_branch_<ref>.sql`) safe to\n * write to disk; `sql` is the migration body wrapped in a single\n * BEGIN/COMMIT.\n */\n async migration(\n ref: string,\n opts: { dropRemoved?: boolean } = {}\n ): Promise<{\n name: string;\n sql: string;\n plan: BranchMergePlanStep[];\n summary: { added: number; changed: number; removed: number };\n }> {\n return this.client.request(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/branches/${encodeURIComponent(ref)}/migration`,\n { dropRemoved: opts.dropRemoved === true }\n );\n }\n}\n\n/**\n * Schema returned by `db.schema()`. One entry per user table.\n */\nexport interface DatabaseSchema {\n tables: Array<{\n name: string;\n columns: Array<{\n name: string;\n /** SQLite column type (TEXT, INTEGER, REAL, BLOB, NUMERIC). */\n type: string;\n notNull: boolean;\n primaryKey: boolean;\n default: string | null;\n }>;\n }>;\n}\n\n/**\n * Callback shape used by Drizzle's `drizzle-orm/sqlite-proxy`.\n * Returned by `db.driver()` so callers can plug PerSQL into Drizzle\n * without taking a hard dep on it.\n */\nexport type SqliteProxyDriver = (\n sql: string,\n params: unknown[],\n method: \"all\" | \"run\" | \"get\" | \"values\"\n) => Promise<{ rows: unknown }>;\n\nfunction rowsToObjects<T>(columns: string[], rows: unknown[][]): T[] {\n return rows.map((r) => {\n const obj: Record<string, unknown> = {};\n for (let i = 0; i < columns.length; i++) obj[columns[i]!] = r[i];\n return obj as T;\n });\n}\n"],"mappings":";AAkBA,IAAM,2BAA2B;AACjC,IAAM,uBAAuB;AAE7B,SAAS,qBAA6B;AAGpC,QAAM,MAAM,IAAI,WAAW,CAAC;AAC5B,SAAO,gBAAgB,GAAG;AAC1B,MAAI,MAAM;AACV,aAAW,KAAK,IAAK,QAAO,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AAC1D,SAAO,cAAc,GAAG;AAC1B;AAwBO,IAAM,cAAN,MAAkB;AAAA,EAIvB,YAA6B,MAAc;AAAd;AAAA,EAAe;AAAA,EAHpC,KAA4B;AAAA,EACnB,YAAY,oBAAI,IAA2B;AAAA,EAI5D,MAAc,OAAgC;AAC5C,QAAI,KAAK,GAAI,QAAO,KAAK;AAIzB,UAAM,aAAa;AACnB,QAAI;AACJ,QAAI;AACF,YAAM,MAAM,OAAO;AAAA,IACrB,QAAQ;AACN,YAAM,IAAI;AAAA,QACR;AAAA,MAEF;AAAA,IACF;AACA,UAAM,IAAI;AACV,UAAM,OAAQ,EAAE,WAAW;AAC3B,SAAK,KAAK,IAAI,KAAK,KAAK,IAAI;AAC5B,SAAK,GAAG,OAAO,mBAAmB;AAClC,WAAO,KAAK;AAAA,EACd;AAAA,EAEA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