@agent-native/core 0.114.12 → 0.114.13

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.
Files changed (101) hide show
  1. package/corpus/README.md +1 -1
  2. package/corpus/core/CHANGELOG.md +6 -0
  3. package/corpus/core/package.json +1 -1
  4. package/corpus/core/src/application-state/index.ts +3 -0
  5. package/corpus/core/src/application-state/script-helpers.ts +12 -1
  6. package/corpus/core/src/application-state/store.ts +216 -15
  7. package/corpus/core/src/db/client.ts +44 -1
  8. package/corpus/templates/clips/changelog/2026-07-21-live-dictation-text-now-appears-above-the-desktop-recording-.md +6 -0
  9. package/corpus/templates/clips/desktop/src/lib/voice-dictation.ts +14 -3
  10. package/corpus/templates/clips/desktop/src/overlays/flow-bar.tsx +25 -0
  11. package/corpus/templates/clips/desktop/src/styles.css +20 -0
  12. package/corpus/templates/clips/desktop/src-tauri/src/clips/mod.rs +3 -4
  13. package/corpus/templates/clips/desktop/src-tauri/src/native_screen/custom_capture.rs +11 -4
  14. package/corpus/templates/design/AGENTS.md +7 -3
  15. package/corpus/templates/design/actions/begin-node-rewrite-request.ts +78 -0
  16. package/corpus/templates/design/actions/cancel-node-rewrite-request.ts +17 -7
  17. package/corpus/templates/design/actions/propose-node-rewrite.ts +26 -18
  18. package/corpus/templates/design/actions/resolve-node-rewrite.ts +67 -19
  19. package/corpus/templates/design/app/components/visual-editor/NodeRewriteProposal.tsx +1 -4
  20. package/corpus/templates/design/app/components/visual-editor/ReviewCanvasPins.tsx +2 -6
  21. package/corpus/templates/design/shared/node-rewrite.ts +32 -0
  22. package/corpus/templates/forms/app/components/layout/Sidebar.tsx +6 -3
  23. package/corpus/templates/forms/app/pages/FormBuilderPage.tsx +11 -18
  24. package/corpus/templates/forms/app/pages/FormsListPage.tsx +9 -5
  25. package/corpus/templates/slides/app/context/DeckContext.tsx +18 -0
  26. package/corpus/templates/slides/changelog/2026-07-21-decks-list-now-refreshes-when-you-switch-organizations.md +6 -0
  27. package/corpus/templates/tasks/app/components/custom-fields/FieldCreateBar.tsx +13 -19
  28. package/corpus/templates/tasks/app/components/custom-fields/FieldListRow.tsx +27 -22
  29. package/corpus/templates/tasks/app/components/custom-fields/FieldsList.tsx +26 -22
  30. package/corpus/templates/tasks/app/components/custom-fields/FieldsListSkeleton.tsx +4 -1
  31. package/corpus/templates/tasks/app/components/custom-fields/FieldsPage.tsx +4 -2
  32. package/corpus/templates/tasks/app/components/custom-fields/editor/FieldEditorSidebar.tsx +12 -3
  33. package/corpus/templates/tasks/app/components/custom-fields/editor/FieldTitleSection.tsx +6 -3
  34. package/corpus/templates/tasks/app/components/custom-fields/editor/config/CurrencyConfigControl.tsx +5 -2
  35. package/corpus/templates/tasks/app/components/custom-fields/editor/config/NumberConfigControl.tsx +5 -2
  36. package/corpus/templates/tasks/app/components/custom-fields/editor/config/PercentConfigControl.tsx +4 -1
  37. package/corpus/templates/tasks/app/components/custom-fields/editor/config/SelectConfigControl.tsx +17 -7
  38. package/corpus/templates/tasks/app/components/custom-fields/editor/config/select-colors.ts +37 -9
  39. package/corpus/templates/tasks/app/components/custom-fields/field-types.ts +23 -0
  40. package/corpus/templates/tasks/app/components/inbox/InboxList.tsx +24 -16
  41. package/corpus/templates/tasks/app/components/inbox/InboxListPage.tsx +5 -3
  42. package/corpus/templates/tasks/app/components/inbox/InboxListRow.tsx +7 -5
  43. package/corpus/templates/tasks/app/components/layout/Header.tsx +10 -10
  44. package/corpus/templates/tasks/app/components/layout/Layout.tsx +11 -7
  45. package/corpus/templates/tasks/app/components/layout/Sidebar.tsx +17 -10
  46. package/corpus/templates/tasks/app/components/shared/AddListItemInput.tsx +14 -8
  47. package/corpus/templates/tasks/app/components/shared/AlertDialog.tsx +6 -2
  48. package/corpus/templates/tasks/app/components/shared/BulkDeleteDialog.tsx +13 -6
  49. package/corpus/templates/tasks/app/components/shared/ChipSelect.tsx +12 -5
  50. package/corpus/templates/tasks/app/components/shared/DeleteItemDialog.tsx +6 -6
  51. package/corpus/templates/tasks/app/components/shared/ListViewHeader.tsx +3 -1
  52. package/corpus/templates/tasks/app/components/shared/SidePanel.tsx +5 -2
  53. package/corpus/templates/tasks/app/components/shared/list/ListRowDragHandle.tsx +3 -1
  54. package/corpus/templates/tasks/app/components/shared/selection/ListSelectionBar.tsx +46 -20
  55. package/corpus/templates/tasks/app/components/shared/selection/ListSelectionHeaderToggle.tsx +3 -1
  56. package/corpus/templates/tasks/app/components/shared/selection/ListSelectionToolbar.tsx +10 -6
  57. package/corpus/templates/tasks/app/components/tasks/TaskList.tsx +24 -13
  58. package/corpus/templates/tasks/app/components/tasks/TaskListHeaderRow.tsx +4 -1
  59. package/corpus/templates/tasks/app/components/tasks/TaskListPage.tsx +5 -3
  60. package/corpus/templates/tasks/app/components/tasks/TaskListRow.tsx +15 -6
  61. package/corpus/templates/tasks/app/components/tasks/fields/TaskFieldsSidebar.tsx +11 -6
  62. package/corpus/templates/tasks/app/components/tasks/fields/TaskTitleSection.tsx +8 -3
  63. package/corpus/templates/tasks/app/components/tasks/fields/controls/RichTextValueControl.tsx +12 -6
  64. package/corpus/templates/tasks/app/components/tasks/fields/controls/SingleSelectValueControl.tsx +6 -1
  65. package/corpus/templates/tasks/app/i18n/en-US.ts +209 -0
  66. package/corpus/templates/tasks/app/i18n/index.ts +34 -0
  67. package/corpus/templates/tasks/app/root.tsx +34 -2
  68. package/corpus/templates/tasks/app/routes/extensions.$id.tsx +2 -1
  69. package/corpus/templates/tasks/app/routes/extensions._index.tsx +2 -1
  70. package/corpus/templates/tasks/app/routes/fields.tsx +2 -1
  71. package/corpus/templates/tasks/app/routes/inbox.tsx +2 -1
  72. package/corpus/templates/tasks/app/routes/tasks.tsx +2 -1
  73. package/corpus/templates/tasks/app/routes/team.tsx +6 -3
  74. package/corpus/templates/tasks/changelog/2026-07-20-tasks-is-now-available-in-10-additional-languages.md +6 -0
  75. package/dist/application-state/index.d.ts +2 -2
  76. package/dist/application-state/index.d.ts.map +1 -1
  77. package/dist/application-state/index.js +2 -2
  78. package/dist/application-state/index.js.map +1 -1
  79. package/dist/application-state/script-helpers.d.ts +3 -1
  80. package/dist/application-state/script-helpers.d.ts.map +1 -1
  81. package/dist/application-state/script-helpers.js +7 -1
  82. package/dist/application-state/script-helpers.js.map +1 -1
  83. package/dist/application-state/store.d.ts +7 -1
  84. package/dist/application-state/store.d.ts.map +1 -1
  85. package/dist/application-state/store.js +143 -17
  86. package/dist/application-state/store.js.map +1 -1
  87. package/dist/collab/routes.d.ts +1 -1
  88. package/dist/collab/struct-routes.d.ts +1 -1
  89. package/dist/db/client.d.ts +7 -0
  90. package/dist/db/client.d.ts.map +1 -1
  91. package/dist/db/client.js +39 -1
  92. package/dist/db/client.js.map +1 -1
  93. package/dist/notifications/routes.d.ts +1 -1
  94. package/dist/observability/routes.d.ts +3 -3
  95. package/dist/server/agent-engine-api-key-route.d.ts +1 -1
  96. package/dist/server/transcribe-voice.d.ts +1 -1
  97. package/package.json +1 -1
  98. package/src/application-state/index.ts +3 -0
  99. package/src/application-state/script-helpers.ts +12 -1
  100. package/src/application-state/store.ts +216 -15
  101. package/src/db/client.ts +44 -1
@@ -1,4 +1,4 @@
1
- import { getDbExec, isLocalDatabase, isConnectionError, isPostgres, intType, } from "../db/client.js";
1
+ import { getDbExec, getDialect, isLocalDatabase, isConnectionError, isPostgres, intType, } from "../db/client.js";
2
2
  import { ensureIndexExists, ensureTableExists } from "../db/ddl-guard.js";
3
3
  import { widenIntColumnsToBigInt } from "../db/widen-columns.js";
4
4
  import { emitAppStateChange, emitAppStateDelete } from "./emitter.js";
@@ -160,30 +160,156 @@ export async function appStateDelete(sessionId, key, options) {
160
160
  export async function appStateCompareAndSet(sessionId, key, expectedValue, nextValue, options) {
161
161
  await ensureTable();
162
162
  const client = getDbExec();
163
+ const changed = await executeAppStateCompareAndSet(client, sessionId, key, expectedValue, nextValue);
164
+ if (changed) {
165
+ if (nextValue === null) {
166
+ emitAppStateDelete(key, options?.requestSource, sessionId);
167
+ }
168
+ else {
169
+ emitAppStateChange(key, options?.requestSource, sessionId);
170
+ }
171
+ }
172
+ return changed;
173
+ }
174
+ const APP_STATE_CAS_MISMATCH = Symbol("app-state-cas-mismatch");
175
+ async function executeAppStateCompareAndSet(client, sessionId, key, expectedValue, nextValue) {
176
+ const statement = buildAppStateCompareAndSetStatement(sessionId, key, expectedValue, nextValue);
177
+ const result = await client.execute(statement);
178
+ return result.rowsAffected > 0;
179
+ }
180
+ function buildAppStateCompareAndSetStatement(sessionId, key, expectedValue, nextValue) {
181
+ if (expectedValue === null) {
182
+ if (nextValue === null) {
183
+ throw new Error("Application state CAS cannot replace absence with absence.");
184
+ }
185
+ const next = serializeAppStateValue(key, nextValue);
186
+ return {
187
+ sql: isPostgres()
188
+ ? `INSERT INTO application_state (session_id, key, value, updated_at) SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM application_state WHERE session_id = ? AND key = ?) ON CONFLICT (session_id, key) DO NOTHING`
189
+ : `INSERT OR IGNORE INTO application_state (session_id, key, value, updated_at) SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM application_state WHERE session_id = ? AND key = ?)`,
190
+ args: [sessionId, key, next, Date.now(), sessionId, key],
191
+ };
192
+ }
163
193
  const expected = JSON.stringify(expectedValue);
164
194
  if (nextValue === null) {
165
- const result = await client.execute({
195
+ return {
166
196
  sql: `DELETE FROM application_state WHERE session_id = ? AND key = ? AND value = ?`,
167
197
  args: [sessionId, key, expected],
168
- });
169
- const changed = result.rowsAffected > 0;
170
- if (changed)
171
- emitAppStateDelete(key, options?.requestSource, sessionId);
172
- return changed;
198
+ };
173
199
  }
174
- const next = JSON.stringify(nextValue);
200
+ const next = serializeAppStateValue(key, nextValue);
201
+ return {
202
+ sql: `UPDATE application_state SET value = ?, updated_at = ? WHERE session_id = ? AND key = ? AND value = ?`,
203
+ args: [next, Date.now(), sessionId, key, expected],
204
+ };
205
+ }
206
+ function buildD1CompareAndSetGuard(sessionId, guardKey, operation) {
207
+ const expected = operation.expectedValue;
208
+ const condition = expected === null
209
+ ? "NOT EXISTS (SELECT 1 FROM application_state WHERE session_id = ? AND key = ?)"
210
+ : "EXISTS (SELECT 1 FROM application_state WHERE session_id = ? AND key = ? AND value = ?)";
211
+ return {
212
+ sql: `INSERT INTO application_state (session_id, key, value, updated_at) SELECT ?, ?, '{}', ? WHERE NOT (${condition})`,
213
+ args: expected === null
214
+ ? [sessionId, guardKey, Date.now(), sessionId, operation.key]
215
+ : [
216
+ sessionId,
217
+ guardKey,
218
+ Date.now(),
219
+ sessionId,
220
+ operation.key,
221
+ JSON.stringify(expected),
222
+ ],
223
+ };
224
+ }
225
+ function isD1CompareAndSetMismatch(error) {
226
+ const message = error instanceof Error ? error.message : String(error);
227
+ return /unique constraint failed:\s*application_state\.session_id,\s*application_state\.key/i.test(message);
228
+ }
229
+ function serializeAppStateValue(key, value) {
230
+ const serialized = JSON.stringify(value);
175
231
  if (!isLocalDatabase() &&
176
- Buffer.byteLength(next, "utf8") > MAX_HOSTED_APP_STATE_VALUE_BYTES) {
232
+ Buffer.byteLength(serialized, "utf8") > MAX_HOSTED_APP_STATE_VALUE_BYTES) {
177
233
  throw new Error(`application_state value "${key}" is too large for hosted SQL storage. Store large files, base64, or blobs in file storage and write only a URL or handle.`);
178
234
  }
179
- const result = await client.execute({
180
- sql: `UPDATE application_state SET value = ?, updated_at = ? WHERE session_id = ? AND key = ? AND value = ?`,
181
- args: [next, Date.now(), sessionId, key, expected],
182
- });
183
- const changed = result.rowsAffected > 0;
184
- if (changed)
185
- emitAppStateChange(key, options?.requestSource, sessionId);
186
- return changed;
235
+ return serialized;
236
+ }
237
+ export async function appStateCompareAndSetMany(sessionId, operations, options) {
238
+ if (operations.length === 0)
239
+ return true;
240
+ const keys = new Set(operations.map(({ key }) => key));
241
+ if (keys.size !== operations.length) {
242
+ throw new Error("Application state multi-key CAS requires unique keys.");
243
+ }
244
+ for (const { key, nextValue } of operations) {
245
+ if (nextValue)
246
+ serializeAppStateValue(key, nextValue);
247
+ }
248
+ const orderedOperations = [...operations].sort((a, b) => a.key.localeCompare(b.key));
249
+ await ensureTable();
250
+ const client = getDbExec();
251
+ if (getDialect() === "d1") {
252
+ if (!client.atomicBatch) {
253
+ throw new Error("D1 application-state CAS requires atomic batch support.");
254
+ }
255
+ const guardKey = `__agent_native_cas_guard__:${Date.now()}:${Math.random().toString(36).slice(2)}`;
256
+ const guardInsert = {
257
+ sql: `INSERT INTO application_state (session_id, key, value, updated_at) VALUES (?, ?, '{}', ?)`,
258
+ args: [sessionId, guardKey, Date.now()],
259
+ };
260
+ const guards = orderedOperations.map((operation) => buildD1CompareAndSetGuard(sessionId, guardKey, operation));
261
+ const mutations = orderedOperations.map((operation) => buildAppStateCompareAndSetStatement(sessionId, operation.key, operation.expectedValue, operation.nextValue));
262
+ let results;
263
+ try {
264
+ results = await client.atomicBatch([
265
+ guardInsert,
266
+ ...guards,
267
+ ...mutations,
268
+ {
269
+ sql: `DELETE FROM application_state WHERE session_id = ? AND key = ?`,
270
+ args: [sessionId, guardKey],
271
+ },
272
+ ]);
273
+ }
274
+ catch (error) {
275
+ if (isD1CompareAndSetMismatch(error))
276
+ return false;
277
+ throw error;
278
+ }
279
+ const mutationResults = results.slice(1 + guards.length, 1 + guards.length + mutations.length);
280
+ if (mutationResults.length !== mutations.length ||
281
+ mutationResults.some((result) => result.rowsAffected !== 1)) {
282
+ throw new Error("D1 application-state CAS completed without applying every mutation.");
283
+ }
284
+ }
285
+ else {
286
+ if (!client.transaction) {
287
+ throw new Error("Application state multi-key CAS requires transactions.");
288
+ }
289
+ try {
290
+ await client.transaction(async (tx) => {
291
+ for (const operation of orderedOperations) {
292
+ const changed = await executeAppStateCompareAndSet(tx, sessionId, operation.key, operation.expectedValue, operation.nextValue);
293
+ if (!changed)
294
+ throw APP_STATE_CAS_MISMATCH;
295
+ }
296
+ });
297
+ }
298
+ catch (error) {
299
+ if (error === APP_STATE_CAS_MISMATCH)
300
+ return false;
301
+ throw error;
302
+ }
303
+ }
304
+ for (const { key, nextValue } of operations) {
305
+ if (nextValue === null) {
306
+ emitAppStateDelete(key, options?.requestSource, sessionId);
307
+ }
308
+ else {
309
+ emitAppStateChange(key, options?.requestSource, sessionId);
310
+ }
311
+ }
312
+ return true;
187
313
  }
188
314
  export async function appStateList(sessionId, keyPrefix) {
189
315
  await ensureTable();
@@ -1 +1 @@
1
- {"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/application-state/store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,UAAU,EACV,OAAO,GACR,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAEjE,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAEtE,IAAI,YAAuC,CAAC;AAC5C,MAAM,gCAAgC,GAAG,IAAI,GAAG,IAAI,CAAC;AAErD,6EAA6E;AAC7E,+EAA+E;AAC/E,6EAA6E;AAC7E,qEAAqE;AACrE,SAAS,UAAU,CAAC,CAAS;IAC3B,OAAO,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC;AACrD,CAAC;AAED,KAAK,UAAU,WAAW;IACxB,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,YAAY,GAAG,CAAC,KAAK,IAAI,EAAE;YACzB,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;YAC3B,MAAM,SAAS,GAAG;;;;;uBAKD,OAAO,EAAE;;;OAGzB,CAAC;YAEF,IAAI,UAAU,EAAE,EAAE,CAAC;gBACjB,mEAAmE;gBACnE,kEAAkE;gBAClE,6DAA6D;gBAC7D,kEAAkE;gBAClE,uEAAuE;gBACvE,oEAAoE;gBACpE,u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CAAC,SAAS,CAAC,GAAG,GAAG,CAAC;KAC/C,CAAC,CAAC;IAEH,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IAEhC,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC;QAClC,GAAG,EAAE,8EAA8E;QACnF,IAAI,EAAE,CAAC,SAAS,EAAE,UAAU,CAAC,SAAS,CAAC,GAAG,GAAG,CAAC;KAC/C,CAAC,CAAC;IAEH,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,kBAAkB,CAAC,GAAG,CAAC,GAAa,EAAE,OAAO,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC;IAC3E,CAAC;IAED,OAAO,MAAM,CAAC,YAAY,CAAC;AAC7B,CAAC","sourcesContent":["import {\n getDbExec,\n isLocalDatabase,\n isConnectionError,\n isPostgres,\n intType,\n} from \"../db/client.js\";\nimport { ensureIndexExists, ensureTableExists } from \"../db/ddl-guard.js\";\nimport { widenIntColumnsToBigInt } from \"../db/widen-columns.js\";\nimport type { StoreWriteOptions } from \"../settings/store.js\";\nimport { emitAppStateChange, emitAppStateDelete } from \"./emitter.js\";\n\nlet _initPromise: Promise<void> | undefined;\nconst MAX_HOSTED_APP_STATE_VALUE_BYTES = 1024 * 1024;\n\n// Escapes LIKE wildcards (`%`, `_`) and the escape char itself so a caller's\n// literal prefix is matched verbatim. Used with `ESCAPE '!'` in prefix queries\n// below; without this, a prefix such as `user_settings` would treat `_` as a\n// single-char wildcard and over-match (e.g. delete `userXsettings`).\nfunction escapeLike(s: string): string {\n return s.replace(/[!%_]/g, (match) => `!${match}`);\n}\n\nasync function ensureTable(): Promise<void> {\n if (!_initPromise) {\n _initPromise = (async () => {\n const client = getDbExec();\n const createSql = `\n CREATE TABLE IF NOT EXISTS application_state (\n session_id TEXT NOT NULL,\n key TEXT NOT NULL,\n value TEXT NOT NULL,\n updated_at ${intType()} NOT NULL,\n PRIMARY KEY (session_id, key)\n )\n `;\n\n if (isPostgres()) {\n // Hot path: the `application_state` table and its poll indexes are\n // virtually always already present in production. Issuing `CREATE\n // TABLE`/ `CREATE INDEX` still takes a lock that, in a fresh\n // background-worker process behind a concurrent connection on the\n // shared Neon DB, can block ~indefinitely (ACCESS EXCLUSIVE for CREATE\n // TABLE; a write-blocking SHARE lock for CREATE INDEX). The ensure*\n // wrappers probe `information_schema`/`pg_indexes` first (plain reads,\n // no lock) and run DDL ONLY for what is actually missing, bounded by a\n // transaction-scoped `lock_timeout`. If a swallowed lock-timeout leaves\n // the schema still missing they RE-PROBE and THROW rather than letting\n // init memoize success against absent schema.\n await ensureTableExists(\"application_state\", createSql);\n // Older deployments created `updated_at` as 32-bit `INTEGER`; on Postgres\n // the `Date.now()` written by appStatePut() on every turn overflows int4.\n // Widen it in place (no-op once done / on fresh BIGINT databases).\n await widenIntColumnsToBigInt(\"application_state\", [\"updated_at\"]);\n // Indexes for the two hot poll paths. Probe pg_indexes first (no lock)\n // and skip the SHARE-locking CREATE INDEX when already present.\n await ensureIndexExists(\n \"app_state_updated_at_idx\",\n `CREATE INDEX IF NOT EXISTS app_state_updated_at_idx ON application_state (updated_at)`,\n );\n await ensureIndexExists(\n \"app_state_key_updated_idx\",\n `CREATE INDEX IF NOT EXISTS app_state_key_updated_idx ON application_state (key, updated_at)`,\n );\n return;\n }\n\n // SQLite (local dev): no lock problem — keep the original behaviour.\n await client.execute(createSql);\n // Older deployments created `updated_at` as 32-bit `INTEGER`; on Postgres\n // the `Date.now()` written by appStatePut() on every turn overflows int4.\n // Widen it in place (no-op once done / on fresh BIGINT databases).\n await widenIntColumnsToBigInt(\"application_state\", [\"updated_at\"]);\n // Indexes for the two hot poll paths:\n // - `SELECT … WHERE updated_at > ?` (watermark scan, every poll cycle)\n // - `SELECT … WHERE key = ? … ORDER BY updated_at ASC` (marker lookups)\n // Both are dialect-agnostic (no DESC/partial/PG-only syntax) so they\n // apply identically on SQLite and Postgres. IF NOT EXISTS makes them\n // idempotent across restarts on existing databases.\n for (const ddl of [\n `CREATE INDEX IF NOT EXISTS app_state_updated_at_idx ON application_state (updated_at)`,\n `CREATE INDEX IF NOT EXISTS app_state_key_updated_idx ON application_state (key, updated_at)`,\n ]) {\n try {\n await client.execute(ddl);\n } catch {\n // Index already exists or the dialect rejected a duplicate.\n }\n }\n })().catch((err) => {\n // Retry init on the next call after a failed startup.\n _initPromise = undefined;\n throw err;\n });\n }\n return _initPromise;\n}\n\nexport async function appStateGet(\n sessionId: string,\n key: string,\n): Promise<Record<string, unknown> | null> {\n try {\n await ensureTable();\n const client = getDbExec();\n const { rows } = await client.execute({\n sql: `SELECT value FROM application_state WHERE session_id = ? AND key = ?`,\n args: [sessionId, key],\n });\n if (rows.length === 0) return null;\n return JSON.parse(rows[0].value as string);\n } catch (err) {\n // Transient WS / connection drops (Neon serverless) — caller polls every\n // 2s and will see the value on the next tick. Swallow rather than 500.\n if (isConnectionError(err)) return null;\n throw err;\n }\n}\n\n/**\n * Read several application-state keys for one session in a single SQL query.\n * Missing keys are returned as `null` so callers can preserve the requested\n * shape without issuing one fallback query per key.\n */\nexport async function appStateGetMany(\n sessionId: string,\n keys: readonly string[],\n): Promise<Record<string, Record<string, unknown> | null>> {\n const uniqueKeys = [...new Set(keys)];\n const values: Record<string, Record<string, unknown> | null> = {};\n for (const key of uniqueKeys) values[key] = null;\n if (uniqueKeys.length === 0) return values;\n\n try {\n await ensureTable();\n const client = getDbExec();\n const placeholders = uniqueKeys.map(() => \"?\").join(\", \");\n const { rows } = await client.execute({\n sql: `SELECT key, value FROM application_state WHERE session_id = ? AND key IN (${placeholders})`,\n args: [sessionId, ...uniqueKeys],\n });\n for (const row of rows) {\n values[row.key as string] = JSON.parse(row.value as string);\n }\n return values;\n } catch (err) {\n if (isConnectionError(err)) return values;\n throw err;\n }\n}\n\nexport async function appStatePut(\n sessionId: string,\n key: string,\n value: Record<string, unknown>,\n options?: StoreWriteOptions,\n): Promise<void> {\n await ensureTable();\n const client = getDbExec();\n const serialized = JSON.stringify(value);\n if (\n !isLocalDatabase() &&\n Buffer.byteLength(serialized, \"utf8\") > MAX_HOSTED_APP_STATE_VALUE_BYTES\n ) {\n throw new Error(\n `application_state value \"${key}\" is too large for hosted SQL storage. Store large files, base64, or blobs in file storage and write only a URL or handle.`,\n );\n }\n await client.execute({\n sql: isPostgres()\n ? `INSERT INTO application_state (session_id, key, value, updated_at) VALUES (?, ?, ?, ?) ON CONFLICT (session_id, key) DO UPDATE SET value=EXCLUDED.value, updated_at=EXCLUDED.updated_at`\n : `INSERT OR REPLACE INTO application_state (session_id, key, value, updated_at) VALUES (?, ?, ?, ?)`,\n args: [sessionId, key, serialized, Date.now()],\n });\n emitAppStateChange(key, options?.requestSource, sessionId);\n}\n\nexport async function appStateDelete(\n sessionId: string,\n key: string,\n options?: StoreWriteOptions,\n): Promise<boolean> {\n await ensureTable();\n const client = getDbExec();\n const result = await client.execute({\n sql: `DELETE FROM application_state WHERE session_id = ? AND key = ?`,\n args: [sessionId, key],\n });\n const deleted = result.rowsAffected > 0;\n if (deleted) emitAppStateDelete(key, options?.requestSource, sessionId);\n return deleted;\n}\n\nexport async function appStateCompareAndSet(\n sessionId: string,\n key: string,\n expectedValue: Record<string, unknown>,\n nextValue: Record<string, unknown> | null,\n options?: StoreWriteOptions,\n): Promise<boolean> {\n await ensureTable();\n const client = getDbExec();\n const expected = JSON.stringify(expectedValue);\n if (nextValue === null) {\n const result = await client.execute({\n sql: `DELETE FROM application_state WHERE session_id = ? AND key = ? AND value = ?`,\n args: [sessionId, key, expected],\n });\n const changed = result.rowsAffected > 0;\n if (changed) emitAppStateDelete(key, options?.requestSource, sessionId);\n return changed;\n }\n\n const next = JSON.stringify(nextValue);\n if (\n !isLocalDatabase() &&\n Buffer.byteLength(next, \"utf8\") > MAX_HOSTED_APP_STATE_VALUE_BYTES\n ) {\n throw new Error(\n `application_state value \"${key}\" is too large for hosted SQL storage. Store large files, base64, or blobs in file storage and write only a URL or handle.`,\n );\n }\n const result = await client.execute({\n sql: `UPDATE application_state SET value = ?, updated_at = ? WHERE session_id = ? AND key = ? AND value = ?`,\n args: [next, Date.now(), sessionId, key, expected],\n });\n const changed = result.rowsAffected > 0;\n if (changed) emitAppStateChange(key, options?.requestSource, sessionId);\n return changed;\n}\n\nexport async function appStateList(\n sessionId: string,\n keyPrefix: string,\n): Promise<Array<{ key: string; value: Record<string, unknown> }>> {\n await ensureTable();\n const client = getDbExec();\n const { rows } = await client.execute({\n sql: `SELECT key, value FROM application_state WHERE session_id = ? AND key LIKE ? ESCAPE '!'`,\n args: [sessionId, escapeLike(keyPrefix) + \"%\"],\n });\n return rows.map((row) => ({\n key: row.key as string,\n value: JSON.parse(row.value as string),\n }));\n}\n\nexport async function appStateDeleteByPrefix(\n sessionId: string,\n keyPrefix: string,\n options?: StoreWriteOptions,\n): Promise<number> {\n await ensureTable();\n const client = getDbExec();\n\n // Get keys first so we can emit events\n const { rows } = await client.execute({\n sql: `SELECT key FROM application_state WHERE session_id = ? AND key LIKE ? ESCAPE '!'`,\n args: [sessionId, escapeLike(keyPrefix) + \"%\"],\n });\n\n if (rows.length === 0) return 0;\n\n const result = await client.execute({\n sql: `DELETE FROM application_state WHERE session_id = ? AND key LIKE ? ESCAPE '!'`,\n args: [sessionId, escapeLike(keyPrefix) + \"%\"],\n });\n\n for (const row of rows) {\n emitAppStateDelete(row.key as string, options?.requestSource, sessionId);\n }\n\n return result.rowsAffected;\n}\n"]}
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/application-state/store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,UAAU,EACV,eAAe,EACf,iBAAiB,EACjB,UAAU,EACV,OAAO,GAER,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAEjE,OAAO,EAAE,kBAAkB,EAAE,kB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AAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,kBAAkB,CAAC,GAAG,CAAC,GAAa,EAAE,OAAO,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC;IAC3E,CAAC;IAED,OAAO,MAAM,CAAC,YAAY,CAAC;AAC7B,CAAC","sourcesContent":["import {\n getDbExec,\n getDialect,\n isLocalDatabase,\n isConnectionError,\n isPostgres,\n intType,\n type DbExec,\n} from \"../db/client.js\";\nimport { ensureIndexExists, ensureTableExists } from \"../db/ddl-guard.js\";\nimport { widenIntColumnsToBigInt } from \"../db/widen-columns.js\";\nimport type { StoreWriteOptions } from \"../settings/store.js\";\nimport { emitAppStateChange, emitAppStateDelete } from \"./emitter.js\";\n\nlet _initPromise: Promise<void> | undefined;\nconst MAX_HOSTED_APP_STATE_VALUE_BYTES = 1024 * 1024;\n\n// Escapes LIKE wildcards (`%`, `_`) and the escape char itself so a caller's\n// literal prefix is matched verbatim. Used with `ESCAPE '!'` in prefix queries\n// below; without this, a prefix such as `user_settings` would treat `_` as a\n// single-char wildcard and over-match (e.g. delete `userXsettings`).\nfunction escapeLike(s: string): string {\n return s.replace(/[!%_]/g, (match) => `!${match}`);\n}\n\nasync function ensureTable(): Promise<void> {\n if (!_initPromise) {\n _initPromise = (async () => {\n const client = getDbExec();\n const createSql = `\n CREATE TABLE IF NOT EXISTS application_state (\n session_id TEXT NOT NULL,\n key TEXT NOT NULL,\n value TEXT NOT NULL,\n updated_at ${intType()} NOT NULL,\n PRIMARY KEY (session_id, key)\n )\n `;\n\n if (isPostgres()) {\n // Hot path: the `application_state` table and its poll indexes are\n // virtually always already present in production. Issuing `CREATE\n // TABLE`/ `CREATE INDEX` still takes a lock that, in a fresh\n // background-worker process behind a concurrent connection on the\n // shared Neon DB, can block ~indefinitely (ACCESS EXCLUSIVE for CREATE\n // TABLE; a write-blocking SHARE lock for CREATE INDEX). The ensure*\n // wrappers probe `information_schema`/`pg_indexes` first (plain reads,\n // no lock) and run DDL ONLY for what is actually missing, bounded by a\n // transaction-scoped `lock_timeout`. If a swallowed lock-timeout leaves\n // the schema still missing they RE-PROBE and THROW rather than letting\n // init memoize success against absent schema.\n await ensureTableExists(\"application_state\", createSql);\n // Older deployments created `updated_at` as 32-bit `INTEGER`; on Postgres\n // the `Date.now()` written by appStatePut() on every turn overflows int4.\n // Widen it in place (no-op once done / on fresh BIGINT databases).\n await widenIntColumnsToBigInt(\"application_state\", [\"updated_at\"]);\n // Indexes for the two hot poll paths. Probe pg_indexes first (no lock)\n // and skip the SHARE-locking CREATE INDEX when already present.\n await ensureIndexExists(\n \"app_state_updated_at_idx\",\n `CREATE INDEX IF NOT EXISTS app_state_updated_at_idx ON application_state (updated_at)`,\n );\n await ensureIndexExists(\n \"app_state_key_updated_idx\",\n `CREATE INDEX IF NOT EXISTS app_state_key_updated_idx ON application_state (key, updated_at)`,\n );\n return;\n }\n\n // SQLite (local dev): no lock problem — keep the original behaviour.\n await client.execute(createSql);\n // Older deployments created `updated_at` as 32-bit `INTEGER`; on Postgres\n // the `Date.now()` written by appStatePut() on every turn overflows int4.\n // Widen it in place (no-op once done / on fresh BIGINT databases).\n await widenIntColumnsToBigInt(\"application_state\", [\"updated_at\"]);\n // Indexes for the two hot poll paths:\n // - `SELECT … WHERE updated_at > ?` (watermark scan, every poll cycle)\n // - `SELECT … WHERE key = ? … ORDER BY updated_at ASC` (marker lookups)\n // Both are dialect-agnostic (no DESC/partial/PG-only syntax) so they\n // apply identically on SQLite and Postgres. IF NOT EXISTS makes them\n // idempotent across restarts on existing databases.\n for (const ddl of [\n `CREATE INDEX IF NOT EXISTS app_state_updated_at_idx ON application_state (updated_at)`,\n `CREATE INDEX IF NOT EXISTS app_state_key_updated_idx ON application_state (key, updated_at)`,\n ]) {\n try {\n await client.execute(ddl);\n } catch {\n // Index already exists or the dialect rejected a duplicate.\n }\n }\n })().catch((err) => {\n // Retry init on the next call after a failed startup.\n _initPromise = undefined;\n throw err;\n });\n }\n return _initPromise;\n}\n\nexport async function appStateGet(\n sessionId: string,\n key: string,\n): Promise<Record<string, unknown> | null> {\n try {\n await ensureTable();\n const client = getDbExec();\n const { rows } = await client.execute({\n sql: `SELECT value FROM application_state WHERE session_id = ? AND key = ?`,\n args: [sessionId, key],\n });\n if (rows.length === 0) return null;\n return JSON.parse(rows[0].value as string);\n } catch (err) {\n // Transient WS / connection drops (Neon serverless) — caller polls every\n // 2s and will see the value on the next tick. Swallow rather than 500.\n if (isConnectionError(err)) return null;\n throw err;\n }\n}\n\n/**\n * Read several application-state keys for one session in a single SQL query.\n * Missing keys are returned as `null` so callers can preserve the requested\n * shape without issuing one fallback query per key.\n */\nexport async function appStateGetMany(\n sessionId: string,\n keys: readonly string[],\n): Promise<Record<string, Record<string, unknown> | null>> {\n const uniqueKeys = [...new Set(keys)];\n const values: Record<string, Record<string, unknown> | null> = {};\n for (const key of uniqueKeys) values[key] = null;\n if (uniqueKeys.length === 0) return values;\n\n try {\n await ensureTable();\n const client = getDbExec();\n const placeholders = uniqueKeys.map(() => \"?\").join(\", \");\n const { rows } = await client.execute({\n sql: `SELECT key, value FROM application_state WHERE session_id = ? AND key IN (${placeholders})`,\n args: [sessionId, ...uniqueKeys],\n });\n for (const row of rows) {\n values[row.key as string] = JSON.parse(row.value as string);\n }\n return values;\n } catch (err) {\n if (isConnectionError(err)) return values;\n throw err;\n }\n}\n\nexport async function appStatePut(\n sessionId: string,\n key: string,\n value: Record<string, unknown>,\n options?: StoreWriteOptions,\n): Promise<void> {\n await ensureTable();\n const client = getDbExec();\n const serialized = JSON.stringify(value);\n if (\n !isLocalDatabase() &&\n Buffer.byteLength(serialized, \"utf8\") > MAX_HOSTED_APP_STATE_VALUE_BYTES\n ) {\n throw new Error(\n `application_state value \"${key}\" is too large for hosted SQL storage. Store large files, base64, or blobs in file storage and write only a URL or handle.`,\n );\n }\n await client.execute({\n sql: isPostgres()\n ? `INSERT INTO application_state (session_id, key, value, updated_at) VALUES (?, ?, ?, ?) ON CONFLICT (session_id, key) DO UPDATE SET value=EXCLUDED.value, updated_at=EXCLUDED.updated_at`\n : `INSERT OR REPLACE INTO application_state (session_id, key, value, updated_at) VALUES (?, ?, ?, ?)`,\n args: [sessionId, key, serialized, Date.now()],\n });\n emitAppStateChange(key, options?.requestSource, sessionId);\n}\n\nexport async function appStateDelete(\n sessionId: string,\n key: string,\n options?: StoreWriteOptions,\n): Promise<boolean> {\n await ensureTable();\n const client = getDbExec();\n const result = await client.execute({\n sql: `DELETE FROM application_state WHERE session_id = ? AND key = ?`,\n args: [sessionId, key],\n });\n const deleted = result.rowsAffected > 0;\n if (deleted) emitAppStateDelete(key, options?.requestSource, sessionId);\n return deleted;\n}\n\nexport async function appStateCompareAndSet(\n sessionId: string,\n key: string,\n expectedValue: Record<string, unknown> | null,\n nextValue: Record<string, unknown> | null,\n options?: StoreWriteOptions,\n): Promise<boolean> {\n await ensureTable();\n const client = getDbExec();\n const changed = await executeAppStateCompareAndSet(\n client,\n sessionId,\n key,\n expectedValue,\n nextValue,\n );\n if (changed) {\n if (nextValue === null) {\n emitAppStateDelete(key, options?.requestSource, sessionId);\n } else {\n emitAppStateChange(key, options?.requestSource, sessionId);\n }\n }\n return changed;\n}\n\nexport interface AppStateCompareAndSetOperation {\n key: string;\n expectedValue: Record<string, unknown> | null;\n nextValue: Record<string, unknown> | null;\n}\n\nconst APP_STATE_CAS_MISMATCH = Symbol(\"app-state-cas-mismatch\");\n\nasync function executeAppStateCompareAndSet(\n client: DbExec,\n sessionId: string,\n key: string,\n expectedValue: Record<string, unknown> | null,\n nextValue: Record<string, unknown> | null,\n): Promise<boolean> {\n const statement = buildAppStateCompareAndSetStatement(\n sessionId,\n key,\n expectedValue,\n nextValue,\n );\n const result = await client.execute(statement);\n return result.rowsAffected > 0;\n}\n\nfunction buildAppStateCompareAndSetStatement(\n sessionId: string,\n key: string,\n expectedValue: Record<string, unknown> | null,\n nextValue: Record<string, unknown> | null,\n): { sql: string; args: unknown[] } {\n if (expectedValue === null) {\n if (nextValue === null) {\n throw new Error(\n \"Application state CAS cannot replace absence with absence.\",\n );\n }\n const next = serializeAppStateValue(key, nextValue);\n return {\n sql: isPostgres()\n ? `INSERT INTO application_state (session_id, key, value, updated_at) SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM application_state WHERE session_id = ? AND key = ?) ON CONFLICT (session_id, key) DO NOTHING`\n : `INSERT OR IGNORE INTO application_state (session_id, key, value, updated_at) SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM application_state WHERE session_id = ? AND key = ?)`,\n args: [sessionId, key, next, Date.now(), sessionId, key],\n };\n }\n\n const expected = JSON.stringify(expectedValue);\n if (nextValue === null) {\n return {\n sql: `DELETE FROM application_state WHERE session_id = ? AND key = ? AND value = ?`,\n args: [sessionId, key, expected],\n };\n }\n\n const next = serializeAppStateValue(key, nextValue);\n return {\n sql: `UPDATE application_state SET value = ?, updated_at = ? WHERE session_id = ? AND key = ? AND value = ?`,\n args: [next, Date.now(), sessionId, key, expected],\n };\n}\n\nfunction buildD1CompareAndSetGuard(\n sessionId: string,\n guardKey: string,\n operation: AppStateCompareAndSetOperation,\n): { sql: string; args: unknown[] } {\n const expected = operation.expectedValue;\n const condition =\n expected === null\n ? \"NOT EXISTS (SELECT 1 FROM application_state WHERE session_id = ? AND key = ?)\"\n : \"EXISTS (SELECT 1 FROM application_state WHERE session_id = ? AND key = ? AND value = ?)\";\n return {\n sql: `INSERT INTO application_state (session_id, key, value, updated_at) SELECT ?, ?, '{}', ? WHERE NOT (${condition})`,\n args:\n expected === null\n ? [sessionId, guardKey, Date.now(), sessionId, operation.key]\n : [\n sessionId,\n guardKey,\n Date.now(),\n sessionId,\n operation.key,\n JSON.stringify(expected),\n ],\n };\n}\n\nfunction isD1CompareAndSetMismatch(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error);\n return /unique constraint failed:\\s*application_state\\.session_id,\\s*application_state\\.key/i.test(\n message,\n );\n}\n\nfunction serializeAppStateValue(\n key: string,\n value: Record<string, unknown>,\n): string {\n const serialized = JSON.stringify(value);\n if (\n !isLocalDatabase() &&\n Buffer.byteLength(serialized, \"utf8\") > MAX_HOSTED_APP_STATE_VALUE_BYTES\n ) {\n throw new Error(\n `application_state value \"${key}\" is too large for hosted SQL storage. Store large files, base64, or blobs in file storage and write only a URL or handle.`,\n );\n }\n return serialized;\n}\n\nexport async function appStateCompareAndSetMany(\n sessionId: string,\n operations: readonly AppStateCompareAndSetOperation[],\n options?: StoreWriteOptions,\n): Promise<boolean> {\n if (operations.length === 0) return true;\n const keys = new Set(operations.map(({ key }) => key));\n if (keys.size !== operations.length) {\n throw new Error(\"Application state multi-key CAS requires unique keys.\");\n }\n for (const { key, nextValue } of operations) {\n if (nextValue) serializeAppStateValue(key, nextValue);\n }\n const orderedOperations = [...operations].sort((a, b) =>\n a.key.localeCompare(b.key),\n );\n\n await ensureTable();\n const client = getDbExec();\n if (getDialect() === \"d1\") {\n if (!client.atomicBatch) {\n throw new Error(\n \"D1 application-state CAS requires atomic batch support.\",\n );\n }\n const guardKey = `__agent_native_cas_guard__:${Date.now()}:${Math.random().toString(36).slice(2)}`;\n const guardInsert = {\n sql: `INSERT INTO application_state (session_id, key, value, updated_at) VALUES (?, ?, '{}', ?)`,\n args: [sessionId, guardKey, Date.now()],\n };\n const guards = orderedOperations.map((operation) =>\n buildD1CompareAndSetGuard(sessionId, guardKey, operation),\n );\n const mutations = orderedOperations.map((operation) =>\n buildAppStateCompareAndSetStatement(\n sessionId,\n operation.key,\n operation.expectedValue,\n operation.nextValue,\n ),\n );\n let results;\n try {\n results = await client.atomicBatch([\n guardInsert,\n ...guards,\n ...mutations,\n {\n sql: `DELETE FROM application_state WHERE session_id = ? AND key = ?`,\n args: [sessionId, guardKey],\n },\n ]);\n } catch (error) {\n if (isD1CompareAndSetMismatch(error)) return false;\n throw error;\n }\n const mutationResults = results.slice(\n 1 + guards.length,\n 1 + guards.length + mutations.length,\n );\n if (\n mutationResults.length !== mutations.length ||\n mutationResults.some((result) => result.rowsAffected !== 1)\n ) {\n throw new Error(\n \"D1 application-state CAS completed without applying every mutation.\",\n );\n }\n } else {\n if (!client.transaction) {\n throw new Error(\"Application state multi-key CAS requires transactions.\");\n }\n try {\n await client.transaction(async (tx) => {\n for (const operation of orderedOperations) {\n const changed = await executeAppStateCompareAndSet(\n tx,\n sessionId,\n operation.key,\n operation.expectedValue,\n operation.nextValue,\n );\n if (!changed) throw APP_STATE_CAS_MISMATCH;\n }\n });\n } catch (error) {\n if (error === APP_STATE_CAS_MISMATCH) return false;\n throw error;\n }\n }\n\n for (const { key, nextValue } of operations) {\n if (nextValue === null) {\n emitAppStateDelete(key, options?.requestSource, sessionId);\n } else {\n emitAppStateChange(key, options?.requestSource, sessionId);\n }\n }\n return true;\n}\n\nexport async function appStateList(\n sessionId: string,\n keyPrefix: string,\n): Promise<Array<{ key: string; value: Record<string, unknown> }>> {\n await ensureTable();\n const client = getDbExec();\n const { rows } = await client.execute({\n sql: `SELECT key, value FROM application_state WHERE session_id = ? AND key LIKE ? ESCAPE '!'`,\n args: [sessionId, escapeLike(keyPrefix) + \"%\"],\n });\n return rows.map((row) => ({\n key: row.key as string,\n value: JSON.parse(row.value as string),\n }));\n}\n\nexport async function appStateDeleteByPrefix(\n sessionId: string,\n keyPrefix: string,\n options?: StoreWriteOptions,\n): Promise<number> {\n await ensureTable();\n const client = getDbExec();\n\n // Get keys first so we can emit events\n const { rows } = await client.execute({\n sql: `SELECT key FROM application_state WHERE session_id = ? AND key LIKE ? ESCAPE '!'`,\n args: [sessionId, escapeLike(keyPrefix) + \"%\"],\n });\n\n if (rows.length === 0) return 0;\n\n const result = await client.execute({\n sql: `DELETE FROM application_state WHERE session_id = ? AND key LIKE ? ESCAPE '!'`,\n args: [sessionId, escapeLike(keyPrefix) + \"%\"],\n });\n\n for (const row of rows) {\n emitAppStateDelete(row.key as string, options?.requestSource, sessionId);\n }\n\n return result.rowsAffected;\n}\n"]}
@@ -26,8 +26,8 @@ export declare const getCollabState: import("h3").EventHandlerWithFetch<import("
26
26
  * Body: { update: string (base64), requestSource?: string }
27
27
  */
28
28
  export declare const postCollabUpdate: import("h3").EventHandlerWithFetch<import("h3").EventHandlerRequest, Promise<{
29
- error: string;
30
29
  ok?: undefined;
30
+ error: string;
31
31
  } | {
32
32
  error?: undefined;
33
33
  ok: boolean;
@@ -13,8 +13,8 @@
13
13
  * Body: { json: any, fieldName?: string, type?: "map"|"array", requestSource?: string }
14
14
  */
15
15
  export declare const postCollabJson: import("h3").EventHandlerWithFetch<import("h3").EventHandlerRequest, Promise<{
16
- error: string;
17
16
  ok?: undefined;
17
+ error: string;
18
18
  } | {
19
19
  error?: undefined;
20
20
  ok: boolean;
@@ -8,6 +8,13 @@ export interface DbExec {
8
8
  rowsAffected: number;
9
9
  }>;
10
10
  transaction?<T>(fn: (tx: DbExec) => Promise<T>): Promise<T>;
11
+ atomicBatch?(statements: readonly (string | {
12
+ sql: string;
13
+ args?: unknown[];
14
+ })[]): Promise<Array<{
15
+ rows: any[];
16
+ rowsAffected: number;
17
+ }>>;
11
18
  /**
12
19
  * Release the underlying connection/pool held by this exec.
13
20
  * Only non-singleton execs created via `createDbExec()` (e.g. the migration
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/db/client.ts"],"names":[],"mappings":"AAwBA,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,UAAU,GAAG,IAAI,CAAC;AAEnD,MAAM,WAAW,MAAM;IACrB,OAAO,CACL,GAAG,EAAE,MAAM,GAAG;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAA;KAAE,GAC9C,OAAO,CAAC;QAAE,IAAI,EAAE,GAAG,EAAE,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAClD,WAAW,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC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ED,wBAAgB,yBAAyB,CACvC,IAAI,EAAE,OAAO,EACb,KAAK,SAAY,GAChB,IAAI,CA2CN;AAqeD,wBAAsB,YAAY,CAAC,MAAM,GAAE,YAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,CAE7E;AAiBD;;;GAGG;AACH,wBAAgB,SAAS,IAAI,MAAM,CA2GlC;AAED,qEAAqE;AACrE,wBAAsB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAgBjD"}
package/dist/db/client.js CHANGED
@@ -908,7 +908,18 @@ async function createDbExecInternal(config = {}, trackSingletonResources = false
908
908
  };
909
909
  return {
910
910
  execute,
911
- transaction: explicitTransaction(execute),
911
+ async atomicBatch(statements) {
912
+ const prepared = statements.map((statement) => {
913
+ if (typeof statement === "string")
914
+ return d1.prepare(statement);
915
+ return d1.prepare(statement.sql).bind(...(statement.args ?? []));
916
+ });
917
+ const results = await d1.batch(prepared);
918
+ return results.map((result) => ({
919
+ rows: result.results || [],
920
+ rowsAffected: result.meta?.changes ?? 0,
921
+ }));
922
+ },
912
923
  };
913
924
  }
914
925
  let url = config.url || "file:./data/app.db";
@@ -1284,6 +1295,9 @@ export function getDbExec() {
1284
1295
  // After init, swap to a sanitizing wrapper around the real client
1285
1296
  const wrapper = {
1286
1297
  execute: (s) => _exec.execute(sanitize(s)),
1298
+ atomicBatch: _exec.atomicBatch
1299
+ ? (statements) => _exec.atomicBatch(statements.map((s) => sanitize(s)))
1300
+ : undefined,
1287
1301
  transaction: _exec.transaction
1288
1302
  ? (fn) => _exec.transaction((tx) => fn({
1289
1303
  execute: (s) => tx.execute(sanitize(s)),
@@ -1307,6 +1321,9 @@ export function getDbExec() {
1307
1321
  }
1308
1322
  const wrapper = {
1309
1323
  execute: (s) => _exec.execute(sanitize(s)),
1324
+ atomicBatch: _exec.atomicBatch
1325
+ ? (statements) => _exec.atomicBatch(statements.map((s) => sanitize(s)))
1326
+ : undefined,
1310
1327
  transaction: _exec.transaction
1311
1328
  ? (innerFn) => _exec.transaction((tx) => innerFn({
1312
1329
  execute: (s) => tx.execute(sanitize(s)),
@@ -1321,8 +1338,29 @@ export function getDbExec() {
1321
1338
  transaction: tx.transaction,
1322
1339
  }));
1323
1340
  }
1341
+ if (_exec.atomicBatch) {
1342
+ throw new Error("This database supports atomic batches, not interactive transactions.");
1343
+ }
1324
1344
  return explicitTransaction(wrapper.execute)(fn);
1325
1345
  },
1346
+ async atomicBatch(statements) {
1347
+ if (!_initPromise)
1348
+ _initPromise = initClient();
1349
+ try {
1350
+ await _initPromise;
1351
+ }
1352
+ catch (err) {
1353
+ _initPromise = undefined;
1354
+ _exec = undefined;
1355
+ throw err;
1356
+ }
1357
+ if (!_exec.atomicBatch) {
1358
+ throw new Error("This database does not support atomic batches.");
1359
+ }
1360
+ const batch = (items) => _exec.atomicBatch(items.map((item) => sanitize(item)));
1361
+ Object.assign(proxy, { atomicBatch: batch });
1362
+ return batch(statements);
1363
+ },
1326
1364
  };
1327
1365
  return proxy;
1328
1366
  }