@persql/sdk 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-ZRZANHPI.js → chunk-YZEQFTCX.js} +60 -3
- package/dist/chunk-YZEQFTCX.js.map +1 -0
- package/dist/cli.cjs +59 -2
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +59 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +55 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-ZRZANHPI.js.map +0 -1
|
@@ -767,6 +767,55 @@ var PerSQLDatabase = class {
|
|
|
767
767
|
}
|
|
768
768
|
};
|
|
769
769
|
}
|
|
770
|
+
/**
|
|
771
|
+
* Long-poll for row-changes on this database. Returns immediately
|
|
772
|
+
* if changes newer than `since` are already buffered; otherwise
|
|
773
|
+
* blocks server-side for up to `waitMs` (default 25s, max 25s).
|
|
774
|
+
*
|
|
775
|
+
* Most callers want `changes()` instead — an async iterator that
|
|
776
|
+
* loops `waitForChanges` forever, surfacing each batch as it
|
|
777
|
+
* arrives.
|
|
778
|
+
*/
|
|
779
|
+
async waitForChanges(opts = {}) {
|
|
780
|
+
if (this.client.local) {
|
|
781
|
+
throw new Error(
|
|
782
|
+
"PerSQL: waitForChanges requires the DO change ring \u2014 not available in local mode."
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
const qs = new URLSearchParams();
|
|
786
|
+
if (typeof opts.since === "number") qs.set("since", String(opts.since));
|
|
787
|
+
if (typeof opts.waitMs === "number") qs.set("waitMs", String(opts.waitMs));
|
|
788
|
+
if (opts.tables && opts.tables.length > 0) qs.set("tables", opts.tables.join(","));
|
|
789
|
+
const q = qs.toString();
|
|
790
|
+
return this.client.request(
|
|
791
|
+
"GET",
|
|
792
|
+
`/v1/db/${this.namespace}/${this.slug}/changes${q ? `?${q}` : ""}`
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Async-iterator change feed. Loops `waitForChanges` forever and
|
|
797
|
+
* yields one batch per server response. Pass an `AbortSignal` to
|
|
798
|
+
* stop the loop cleanly.
|
|
799
|
+
*
|
|
800
|
+
* ```ts
|
|
801
|
+
* const ctl = new AbortController();
|
|
802
|
+
* for await (const batch of db.changes({ signal: ctl.signal })) {
|
|
803
|
+
* for (const c of batch.changes) console.log(c.kind, c.table);
|
|
804
|
+
* }
|
|
805
|
+
* ```
|
|
806
|
+
*/
|
|
807
|
+
async *changes(opts = {}) {
|
|
808
|
+
let cursor = opts.since ?? 0;
|
|
809
|
+
while (!opts.signal?.aborted) {
|
|
810
|
+
const batch = await this.waitForChanges({
|
|
811
|
+
since: cursor,
|
|
812
|
+
waitMs: opts.waitMs,
|
|
813
|
+
tables: opts.tables
|
|
814
|
+
});
|
|
815
|
+
cursor = batch.cursor;
|
|
816
|
+
yield batch;
|
|
817
|
+
}
|
|
818
|
+
}
|
|
770
819
|
/**
|
|
771
820
|
* Returns the callback shape `drizzle-orm/sqlite-proxy` expects.
|
|
772
821
|
* Pair with `drizzle()` from that module to get a typed,
|
|
@@ -803,7 +852,7 @@ var PerSQLDatabase = class {
|
|
|
803
852
|
* `execute`/`invoke` callbacks, which call `runTool` internally).
|
|
804
853
|
*/
|
|
805
854
|
asTool(name = "persql_query") {
|
|
806
|
-
const description = `Run a SQL query against the PerSQL database "${this.namespace}/${this.slug}".
|
|
855
|
+
const description = `Run a SQL query against the PerSQL database "${this.namespace}/${this.slug}". A single result is capped at 100,000 rows and the query errors above that \u2014 add a LIMIT for large tables. Use parameter binding (?) to avoid injection.`;
|
|
807
856
|
const inputSchema = {
|
|
808
857
|
type: "object",
|
|
809
858
|
properties: {
|
|
@@ -814,7 +863,10 @@ var PerSQLDatabase = class {
|
|
|
814
863
|
description: "Positional parameters for the SQL statement"
|
|
815
864
|
}
|
|
816
865
|
},
|
|
817
|
-
required: ["sql"]
|
|
866
|
+
required: ["sql"],
|
|
867
|
+
// OpenAI strict function-calling rejects a schema without this; the
|
|
868
|
+
// per-table asTools() schemas already set it. Keep them consistent.
|
|
869
|
+
additionalProperties: false
|
|
818
870
|
};
|
|
819
871
|
const execute = (input) => this.runTool({
|
|
820
872
|
sql: String(input.sql ?? ""),
|
|
@@ -1773,6 +1825,11 @@ var PerSQLProposals = class {
|
|
|
1773
1825
|
* rows, and returns a single-use `executionToken` valid for 10 min
|
|
1774
1826
|
* (override with `ttlSec`, max 1 hour). Nothing is mutated until
|
|
1775
1827
|
* `apply()` is called.
|
|
1828
|
+
*
|
|
1829
|
+
* Local mode keeps the token in-process: single-use still holds, but
|
|
1830
|
+
* `estimatedAffectedRows` is always `null` (no server estimator) and a
|
|
1831
|
+
* stale/unknown token throws a plain `Error` rather than the HTTP
|
|
1832
|
+
* 404/403 of the server path.
|
|
1776
1833
|
*/
|
|
1777
1834
|
async propose(sql, opts = {}) {
|
|
1778
1835
|
if (this.client.local) {
|
|
@@ -1932,4 +1989,4 @@ export {
|
|
|
1932
1989
|
PerSQLProposals,
|
|
1933
1990
|
PerSQLBranches
|
|
1934
1991
|
};
|
|
1935
|
-
//# sourceMappingURL=chunk-
|
|
1992
|
+
//# sourceMappingURL=chunk-YZEQFTCX.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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). Subscribe,\n// branches, approvals have no local equivalent and throw.\n\nimport type { BatchExpect, Statement } from \"./index\";\n\nfunction checkLocalExpect(\n e: BatchExpect,\n r: RawQueryResult\n): string | null {\n const rows = r.rows.length;\n if (e.rows != null && rows !== e.rows) {\n return `expected rows=${e.rows}, got ${rows}`;\n }\n if (e.rowsAtLeast != null && rows < e.rowsAtLeast) {\n return `expected rows>=${e.rowsAtLeast}, got ${rows}`;\n }\n if (e.rowsAtMost != null && rows > e.rowsAtMost) {\n return `expected rows<=${e.rowsAtMost}, got ${rows}`;\n }\n if (e.rowsWritten != null && r.rowsWritten !== e.rowsWritten) {\n return `expected rowsWritten=${e.rowsWritten}, got ${r.rowsWritten}`;\n }\n if (e.rowsWrittenAtLeast != null && r.rowsWritten < e.rowsWrittenAtLeast) {\n return `expected rowsWritten>=${e.rowsWrittenAtLeast}, got ${r.rowsWritten}`;\n }\n if (e.rowsWrittenAtMost != null && r.rowsWritten > e.rowsWrittenAtMost) {\n return `expected rowsWritten<=${e.rowsWrittenAtMost}, got ${r.rowsWritten}`;\n }\n return null;\n}\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 = () =>\n statements.map((s, i) => {\n const r = runOne(db, s.sql, s.params ?? []);\n if (s.expect) {\n const violation = checkLocalExpect(s.expect, r);\n if (violation) {\n throw new Error(`Assertion failed on statement ${i}: ${violation}`);\n }\n }\n return r;\n });\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 (`subscribe`) throw — they have no local\n * 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\n/**\n * Per-statement assertion attached to a `batch` entry. Evaluated\n * server-side inside the same transactionSync as the statement —\n * a violation throws, which rolls a transactional batch back. For\n * non-transactional batches, the rest of the batch does not run.\n *\n * Use this to write defensive multi-step writes without a follow-up\n * read: `UPDATE … WHERE id = ?` with `expect: { rowsWritten: 1 }`\n * fails fast if zero rows match.\n *\n * Shape mirrors `@persql/shared`'s `BatchExpect`. Inlined here so the\n * published npm package has no monorepo-only deps.\n */\nexport interface BatchExpect {\n /** Result has exactly this many rows. */\n rows?: number;\n rowsAtLeast?: number;\n rowsAtMost?: number;\n /** Statement wrote exactly this many rows. */\n rowsWritten?: number;\n rowsWrittenAtLeast?: number;\n rowsWrittenAtMost?: number;\n}\n\nexport interface Statement {\n sql: string;\n params?: unknown[];\n expect?: BatchExpect;\n}\n\n/**\n * Returned by `client.me()` — the bearer token's namespace, scope, and\n * current prepaid balance. Agents call this to learn what they're\n * allowed to touch and how much credit remains before the gate returns\n * 402.\n */\nexport interface WhoamiInfo {\n namespaceId: string;\n namespaceSlug: string;\n tokenId: string;\n role: \"admin\" | \"readwrite\" | \"readonly\";\n scopes: unknown[];\n tableScope: string[] | null;\n balanceUsdMicros: number | null;\n}\n\n/** Output of `db.sampleTable(...)`. */\nexport interface TableSample {\n table: string;\n rowCount: number;\n rows: Array<Record<string, unknown>>;\n columns: Array<{\n name: string;\n type: string;\n pk: boolean;\n notNull: boolean;\n nullCount: number;\n distinctCount: number;\n min: unknown;\n max: unknown;\n }>;\n}\n\n/** Output of `db.validate(...)`. */\nexport type ValidateResult =\n | { ok: true }\n | { ok: false; error: string; errorDetail?: SqlErrorDetail };\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 * Resolve the bearer token's namespace + live usage. Use this once\n * per agent run so you know the slug to put in URLs and how much\n * monthly budget / prepaid balance remains before a 402.\n *\n * Throws in local mode — no server, nothing to ask.\n */\n async me(): Promise<WhoamiInfo> {\n if (this.local) {\n throw new Error(\"PerSQL: me() is not available in local mode.\");\n }\n return this.request<WhoamiInfo>(\"GET\", \"/v1/me\");\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 fetch returning the underlying Response. */\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 /**\n * File a support ticket directly from your SDK code. Tickets land in\n * the customer console at `/support`; staff reply from the admin\n * app. Useful for agents and background jobs that hit a PerSQL\n * error and want a human to see it.\n *\n * ```ts\n * try {\n * await db.query(\"UPDATE ...\");\n * } catch (err) {\n * await persql.support.createTicket({\n * subject: \"Branch merge keeps 500ing\",\n * body: \"Repro: ...\",\n * error: err,\n * });\n * }\n * ```\n */\n get support(): SupportClient {\n return new SupportClient(this);\n }\n}\n\nexport interface CreateSupportTicketOptions {\n subject: string;\n body: string;\n /** Optional error to attach for triage (PerSQLError or any throwable). */\n error?: unknown;\n /** Caller-supplied extras merged into the ticket's diagnostic context. */\n errorContext?: Record<string, unknown>;\n}\n\nfunction errorContextFor(err: unknown): Record<string, unknown> {\n if (err instanceof PerSQLError) {\n return {\n errorName: err.name,\n errorMessage: err.message,\n status: err.status,\n detail: err.detail ?? null,\n };\n }\n if (err instanceof Error) {\n return { errorName: err.name, errorMessage: err.message };\n }\n if (err !== undefined && err !== null) {\n return { errorMessage: String(err) };\n }\n return {};\n}\n\nexport class SupportClient {\n constructor(private readonly client: PerSQL) {}\n\n async createTicket(\n opts: CreateSupportTicketOptions\n ): Promise<{ id: string }> {\n if (this.client.local) {\n throw new Error(\"support.createTicket is not supported in local mode\");\n }\n const errorContext: Record<string, unknown> = {\n ...errorContextFor(opts.error),\n ...(opts.errorContext ?? {}),\n };\n return this.client.request<{ id: string }>(\n \"POST\",\n \"/v1/support/tickets\",\n {\n subject: opts.subject,\n body: opts.body,\n errorContext:\n Object.keys(errorContext).length > 0 ? errorContext : undefined,\n }\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 * Mint a short-lived session JWT for an end user. The server (which\n * holds the bearer token) calls this and hands the resulting token\n * to an untrusted client (browser, mobile app, agent run). The\n * client uses the JWT to call this database's published `/p/*`\n * endpoints on behalf of `userId`. TTL defaults to 1 hour; max 24h.\n */\n async createSession(opts: {\n userId: string;\n email?: string;\n expiresIn?: number;\n }): Promise<{ token: string; expiresAt: string; expiresIn: number }> {\n if (this.client.local) {\n throw new Error(\"createSession is not supported in local mode\");\n }\n return this.client.request<{\n token: string;\n expiresAt: string;\n expiresIn: number;\n }>(\"POST\", \"/v1/sessions\", {\n namespaceSlug: this.namespace,\n databaseSlug: this.slug,\n userId: opts.userId,\n email: opts.email,\n expiresIn: opts.expiresIn,\n });\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 * Parse + bind-check a SQL statement without executing it. Faster\n * and cheaper than running the query just to catch unknown columns\n * or wrong param counts.\n *\n * Returns `{ ok: true }` on success or `{ ok: false, error, errorDetail }`\n * with the same `errorDetail` envelope as `PerSQLError.detail`.\n */\n async validate(sql: string, params: unknown[] = []): Promise<ValidateResult> {\n if (this.client.local) {\n throw new Error(\"PerSQL: validate is not available in local mode.\");\n }\n const data = await this.client.request<\n | { ok: true }\n | { ok: false; error: string }\n >(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/validate`,\n { sql, params }\n );\n return data as ValidateResult;\n }\n\n /**\n * Compact text rendering of the database's schema — one line per\n * table with columns, types, PKs, and FK arrows inlined. Designed\n * to stuff into an LLM prompt with minimum token overhead. See\n * `db.describe()` for the structured form.\n */\n async describeCompact(): Promise<string> {\n if (this.client.local) {\n throw new Error(\"PerSQL: describeCompact is not available in local mode.\");\n }\n const data = await this.client.request<{ text: string }>(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/describe?format=compact`\n );\n return data.text;\n }\n\n /**\n * First N rows of a table plus per-column stats (null count,\n * distinct count, min, max). One call to size up an unfamiliar\n * table — replaces a hand-rolled `SELECT *`, `COUNT(*)`, and\n * `PRAGMA table_info` sequence.\n *\n * `n` defaults to 10 and is capped at 100.\n */\n async sampleTable(\n table: string,\n opts: { n?: number } = {}\n ): Promise<TableSample> {\n if (this.client.local) {\n throw new Error(\"PerSQL: sampleTable is not available in local mode.\");\n }\n const qs = opts.n ? `?n=${Math.max(1, Math.min(100, opts.n | 0))}` : \"\";\n return this.client.request<TableSample>(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/tables/${encodeURIComponent(\n table\n )}/sample${qs}`\n );\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 * 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 * Manage the require_approval / deny rules for this database. Reads\n * are open to any bearer with read access; create + delete require\n * an admin-role bearer token.\n */\n get approvalRules(): PerSQLApprovalRules {\n if (this.client.local) {\n throw new Error(\n \"PerSQL: approval rules live on the server; local mode does not enforce them.\"\n );\n }\n return new PerSQLApprovalRules(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 * 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 * Long-poll for row-changes on this database. Returns immediately\n * if changes newer than `since` are already buffered; otherwise\n * blocks server-side for up to `waitMs` (default 25s, max 25s).\n *\n * Most callers want `changes()` instead — an async iterator that\n * loops `waitForChanges` forever, surfacing each batch as it\n * arrives.\n */\n async waitForChanges(opts: {\n since?: number;\n waitMs?: number;\n tables?: string[];\n } = {}): Promise<{\n changes: Array<{ seq: number; ts: number; table: string; kind: SubscribeChangeKind }>;\n cursor: number;\n restarted: boolean;\n }> {\n if (this.client.local) {\n throw new Error(\n \"PerSQL: waitForChanges requires the DO change ring — not available in local mode.\"\n );\n }\n const qs = new URLSearchParams();\n if (typeof opts.since === \"number\") qs.set(\"since\", String(opts.since));\n if (typeof opts.waitMs === \"number\") qs.set(\"waitMs\", String(opts.waitMs));\n if (opts.tables && opts.tables.length > 0) qs.set(\"tables\", opts.tables.join(\",\"));\n const q = qs.toString();\n return this.client.request(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/changes${q ? `?${q}` : \"\"}`\n );\n }\n\n /**\n * Async-iterator change feed. Loops `waitForChanges` forever and\n * yields one batch per server response. Pass an `AbortSignal` to\n * stop the loop cleanly.\n *\n * ```ts\n * const ctl = new AbortController();\n * for await (const batch of db.changes({ signal: ctl.signal })) {\n * for (const c of batch.changes) console.log(c.kind, c.table);\n * }\n * ```\n */\n async *changes(opts: {\n since?: number;\n tables?: string[];\n waitMs?: number;\n signal?: AbortSignal;\n } = {}): AsyncGenerator<{\n changes: Array<{ seq: number; ts: number; table: string; kind: SubscribeChangeKind }>;\n cursor: number;\n restarted: boolean;\n }> {\n let cursor = opts.since ?? 0;\n while (!opts.signal?.aborted) {\n const batch = await this.waitForChanges({\n since: cursor,\n waitMs: opts.waitMs,\n tables: opts.tables,\n });\n cursor = batch.cursor;\n yield batch;\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 single SQL-query tool definition in every shape PerSQL\n * supports (Anthropic, OpenAI Chat, Vercel AI SDK, Mastra, LangChain,\n * OpenAI Agents SDK). Same input contract as `asTools()` — the only\n * difference is one fat `query` tool instead of typed-per-table tools.\n *\n * Pair the format-specific fields with `runTool` (or use the bundled\n * `execute`/`invoke` callbacks, which call `runTool` internally).\n */\n asTool(name = \"persql_query\"): SingleToolBundle {\n const description = `Run a SQL query against the PerSQL database \"${this.namespace}/${this.slug}\". A single result is capped at 100,000 rows and the query errors above that — add a LIMIT for large tables. Use parameter binding (?) to avoid injection.`;\n const inputSchema = {\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 // OpenAI strict function-calling rejects a schema without this; the\n // per-table asTools() schemas already set it. Keep them consistent.\n additionalProperties: false,\n } as const;\n const execute = (input: Record<string, unknown>) =>\n this.runTool({\n sql: String(input.sql ?? \"\"),\n params: Array.isArray(input.params) ? (input.params as unknown[]) : [],\n });\n return {\n anthropic: { name, description, input_schema: inputSchema },\n openai: {\n type: \"function\" as const,\n function: { name, description, parameters: inputSchema },\n },\n aiSdk: () => ({\n [name]: { description, inputSchema, execute },\n }),\n mastra: () => ({\n [name]: {\n id: name,\n description,\n inputSchema,\n execute: ({ context }) => execute(context),\n },\n }),\n langchain: () => [\n { name, description, schema: inputSchema, invoke: execute },\n ],\n openaiAgents: () => [\n {\n type: \"function\" as const,\n name,\n description,\n parameters: inputSchema,\n invoke: execute,\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 SingleToolBundle {\n /** Anthropic shape — pass as one entry of `tools` to messages.create. */\n anthropic: AnthropicTool;\n /** OpenAI Chat shape — pass as one entry of `tools` to chat.completions.create. */\n openai: OpenAiTool;\n /** Vercel AI SDK shape — spread into `streamText({ tools: { ...db.asTool().aiSdk() } })`. */\n aiSdk: () => Record<string, AiSdkTool>;\n /** Mastra shape — wrap with `createTool()` if you need the Mastra envelope. */\n mastra: () => Record<string, MastraTool>;\n /** LangChain/LangGraph shape — pass to `DynamicStructuredTool` or use `invoke` directly. */\n langchain: () => LangChainTool[];\n /** OpenAI Agents SDK shape — hand to `Agent({ tools: [...] })`. */\n openaiAgents: () => OpenAiAgentsTool[];\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\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 interface ApprovalStatus {\n status: \"pending\" | \"approved\" | \"denied\";\n hits: ApprovalRuleHit[];\n createdAt: string;\n expiresAt: string;\n decidedAt: string | null;\n}\n\nexport interface PollApprovalOptions {\n /** Default 2000ms. */\n intervalMs?: number;\n /** Default 10 minutes. */\n timeoutMs?: number;\n /** Cancellation signal. */\n signal?: AbortSignal;\n}\n\nexport interface SubscribeApprovalOptions {\n onApprovalRequired?: (event: ApprovalEvent) => void;\n onApprovalResolved?: (event: ApprovalEvent) => void;\n /** Poll interval. Default 5000ms. */\n intervalMs?: number;\n /** Cancellation signal. */\n signal?: AbortSignal;\n /** Logged-by-default error sink. */\n onError?: (err: Error) => void;\n}\n\nexport interface ApprovalEvent {\n approvalToken: string;\n status: \"pending\" | \"approved\" | \"denied\";\n hits: ApprovalRuleHit[];\n}\n\nexport class PerSQLApprovals {\n // Tokens this client has seen via subscribe — used to dedupe poll\n // results so onApprovalRequired fires once per token regardless of\n // how many subscribe ticks observe it.\n private readonly seen = new Map<string, \"pending\" | \"approved\" | \"denied\">();\n\n constructor(\n private readonly client: PerSQL,\n private readonly namespace: string,\n private readonly slug: string\n ) {}\n\n /**\n * Look up the status of an approval token without consuming it. The\n * bearer must match the one that minted the original 403; cross-bearer\n * lookup returns 403.\n */\n async get(approvalToken: string): Promise<ApprovalStatus> {\n return this.client.request<ApprovalStatus>(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/approval/${encodeURIComponent(\n approvalToken\n )}`\n );\n }\n\n /**\n * Poll until a reviewer decides (or the token expires). Resolves with\n * the final status — `approved` is your green light for `redeem()`.\n */\n async poll(\n approvalToken: string,\n opts: PollApprovalOptions = {}\n ): Promise<ApprovalStatus> {\n const intervalMs = Math.max(250, opts.intervalMs ?? 2000);\n const deadline = Date.now() + (opts.timeoutMs ?? 10 * 60 * 1000);\n while (true) {\n if (opts.signal?.aborted) {\n throw new Error(\"approval poll aborted\");\n }\n const status = await this.get(approvalToken);\n if (status.status !== \"pending\") return status;\n if (Date.now() > deadline) return status;\n await new Promise((r) => setTimeout(r, intervalMs));\n }\n }\n\n /**\n * Long-running poll subscription for new approval events on this\n * database. Returns a `stop()` handle. Webhook delivery is the push\n * path (events `approval_required` / `approval_resolved`); subscribe\n * is the pull fallback for clients that can't host a webhook.\n *\n * Today this iterates over a known set of tokens supplied by the\n * caller; an SDK-only client that wants discovery should pair this\n * with the webhook path. Pass tokens you already hold (e.g. from a\n * prior `ApprovalRequiredError`).\n */\n subscribe(\n tokens: string[],\n opts: SubscribeApprovalOptions\n ): { stop: () => void } {\n const intervalMs = Math.max(1000, opts.intervalMs ?? 5000);\n let cancelled = false;\n const onAbort = () => {\n cancelled = true;\n };\n opts.signal?.addEventListener(\"abort\", onAbort);\n const tick = async () => {\n for (const token of tokens) {\n if (cancelled) return;\n try {\n const status = await this.get(token);\n const prev = this.seen.get(token);\n if (status.status !== prev) {\n this.seen.set(token, status.status);\n const event: ApprovalEvent = {\n approvalToken: token,\n status: status.status,\n hits: status.hits,\n };\n if (status.status === \"pending\") {\n opts.onApprovalRequired?.(event);\n } else {\n opts.onApprovalResolved?.(event);\n }\n }\n } catch (e) {\n opts.onError?.(e instanceof Error ? e : new Error(String(e)));\n }\n }\n };\n const id = setInterval(() => {\n void tick();\n }, intervalMs);\n void tick();\n return {\n stop: () => {\n cancelled = true;\n clearInterval(id);\n opts.signal?.removeEventListener(\"abort\", onAbort);\n },\n };\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 interface ApprovalRule {\n id: string;\n databaseId: string;\n tableGlob: string;\n action: \"require_approval\" | \"deny\";\n note: string | null;\n createdById: string;\n createdAt: string;\n}\n\nexport interface CreateApprovalRuleInput {\n tableGlob: string;\n action: \"require_approval\" | \"deny\";\n note?: string;\n}\n\nexport class PerSQLApprovalRules {\n constructor(\n private readonly client: PerSQL,\n private readonly namespace: string,\n private readonly slug: string\n ) {}\n\n async list(): Promise<ApprovalRule[]> {\n return this.client.request<ApprovalRule[]>(\n \"GET\",\n `/v1/db/${this.namespace}/${this.slug}/approval-rules`\n );\n }\n\n /** Requires an admin-role bearer token. */\n async create(input: CreateApprovalRuleInput): Promise<ApprovalRule> {\n return this.client.request<ApprovalRule>(\n \"POST\",\n `/v1/db/${this.namespace}/${this.slug}/approval-rules`,\n input\n );\n }\n\n /** Requires an admin-role bearer token. */\n async delete(ruleId: string): Promise<{ deleted: true }> {\n return this.client.request<{ deleted: true }>(\n \"DELETE\",\n `/v1/db/${this.namespace}/${this.slug}/approval-rules/${encodeURIComponent(\n ruleId\n )}`\n );\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 * Local mode keeps the token in-process: single-use still holds, but\n * `estimatedAffectedRows` is always `null` (no server estimator) and a\n * stale/unknown token throws a plain `Error` rather than the HTTP\n * 404/403 of the server path.\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":";AAYA,SAAS,iBACP,GACA,GACe;AACf,QAAM,OAAO,EAAE,KAAK;AACpB,MAAI,EAAE,QAAQ,QAAQ,SAAS,EAAE,MAAM;AACrC,WAAO,iBAAiB,EAAE,IAAI,SAAS,IAAI;AAAA,EAC7C;AACA,MAAI,EAAE,eAAe,QAAQ,OAAO,EAAE,aAAa;A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AAK,SAAS,IAAI,KAAK,IAAI,aAAa,mBAAmB,GAAG,CAAC;AAAA,MACzE,EAAE,aAAa,KAAK,gBAAgB,KAAK;AAAA,IAC3C;AAAA,EACF;AACF;AA8BA,SAAS,cAAiB,SAAmB,MAAwB;AACnE,SAAO,KAAK,IAAI,CAAC,MAAM;AACrB,UAAM,MAA+B,CAAC;AACtC,aAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,IAAK,KAAI,QAAQ,CAAC,CAAE,IAAI,EAAE,CAAC;AAC/D,WAAO;AAAA,EACT,CAAC;AACH;","names":["raw","params","limit","where","r"]}
|
package/dist/cli.cjs
CHANGED
|
@@ -773,6 +773,55 @@ var PerSQLDatabase = class {
|
|
|
773
773
|
}
|
|
774
774
|
};
|
|
775
775
|
}
|
|
776
|
+
/**
|
|
777
|
+
* Long-poll for row-changes on this database. Returns immediately
|
|
778
|
+
* if changes newer than `since` are already buffered; otherwise
|
|
779
|
+
* blocks server-side for up to `waitMs` (default 25s, max 25s).
|
|
780
|
+
*
|
|
781
|
+
* Most callers want `changes()` instead — an async iterator that
|
|
782
|
+
* loops `waitForChanges` forever, surfacing each batch as it
|
|
783
|
+
* arrives.
|
|
784
|
+
*/
|
|
785
|
+
async waitForChanges(opts = {}) {
|
|
786
|
+
if (this.client.local) {
|
|
787
|
+
throw new Error(
|
|
788
|
+
"PerSQL: waitForChanges requires the DO change ring \u2014 not available in local mode."
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
const qs = new URLSearchParams();
|
|
792
|
+
if (typeof opts.since === "number") qs.set("since", String(opts.since));
|
|
793
|
+
if (typeof opts.waitMs === "number") qs.set("waitMs", String(opts.waitMs));
|
|
794
|
+
if (opts.tables && opts.tables.length > 0) qs.set("tables", opts.tables.join(","));
|
|
795
|
+
const q = qs.toString();
|
|
796
|
+
return this.client.request(
|
|
797
|
+
"GET",
|
|
798
|
+
`/v1/db/${this.namespace}/${this.slug}/changes${q ? `?${q}` : ""}`
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Async-iterator change feed. Loops `waitForChanges` forever and
|
|
803
|
+
* yields one batch per server response. Pass an `AbortSignal` to
|
|
804
|
+
* stop the loop cleanly.
|
|
805
|
+
*
|
|
806
|
+
* ```ts
|
|
807
|
+
* const ctl = new AbortController();
|
|
808
|
+
* for await (const batch of db.changes({ signal: ctl.signal })) {
|
|
809
|
+
* for (const c of batch.changes) console.log(c.kind, c.table);
|
|
810
|
+
* }
|
|
811
|
+
* ```
|
|
812
|
+
*/
|
|
813
|
+
async *changes(opts = {}) {
|
|
814
|
+
let cursor = opts.since ?? 0;
|
|
815
|
+
while (!opts.signal?.aborted) {
|
|
816
|
+
const batch = await this.waitForChanges({
|
|
817
|
+
since: cursor,
|
|
818
|
+
waitMs: opts.waitMs,
|
|
819
|
+
tables: opts.tables
|
|
820
|
+
});
|
|
821
|
+
cursor = batch.cursor;
|
|
822
|
+
yield batch;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
776
825
|
/**
|
|
777
826
|
* Returns the callback shape `drizzle-orm/sqlite-proxy` expects.
|
|
778
827
|
* Pair with `drizzle()` from that module to get a typed,
|
|
@@ -809,7 +858,7 @@ var PerSQLDatabase = class {
|
|
|
809
858
|
* `execute`/`invoke` callbacks, which call `runTool` internally).
|
|
810
859
|
*/
|
|
811
860
|
asTool(name = "persql_query") {
|
|
812
|
-
const description = `Run a SQL query against the PerSQL database "${this.namespace}/${this.slug}".
|
|
861
|
+
const description = `Run a SQL query against the PerSQL database "${this.namespace}/${this.slug}". A single result is capped at 100,000 rows and the query errors above that \u2014 add a LIMIT for large tables. Use parameter binding (?) to avoid injection.`;
|
|
813
862
|
const inputSchema = {
|
|
814
863
|
type: "object",
|
|
815
864
|
properties: {
|
|
@@ -820,7 +869,10 @@ var PerSQLDatabase = class {
|
|
|
820
869
|
description: "Positional parameters for the SQL statement"
|
|
821
870
|
}
|
|
822
871
|
},
|
|
823
|
-
required: ["sql"]
|
|
872
|
+
required: ["sql"],
|
|
873
|
+
// OpenAI strict function-calling rejects a schema without this; the
|
|
874
|
+
// per-table asTools() schemas already set it. Keep them consistent.
|
|
875
|
+
additionalProperties: false
|
|
824
876
|
};
|
|
825
877
|
const execute = (input) => this.runTool({
|
|
826
878
|
sql: String(input.sql ?? ""),
|
|
@@ -1779,6 +1831,11 @@ var PerSQLProposals = class {
|
|
|
1779
1831
|
* rows, and returns a single-use `executionToken` valid for 10 min
|
|
1780
1832
|
* (override with `ttlSec`, max 1 hour). Nothing is mutated until
|
|
1781
1833
|
* `apply()` is called.
|
|
1834
|
+
*
|
|
1835
|
+
* Local mode keeps the token in-process: single-use still holds, but
|
|
1836
|
+
* `estimatedAffectedRows` is always `null` (no server estimator) and a
|
|
1837
|
+
* stale/unknown token throws a plain `Error` rather than the HTTP
|
|
1838
|
+
* 404/403 of the server path.
|
|
1782
1839
|
*/
|
|
1783
1840
|
async propose(sql, opts = {}) {
|
|
1784
1841
|
if (this.client.local) {
|