@deepagents/experimental 7.1.2 → 7.1.4

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.
@@ -188,6 +188,7 @@ function defineInstructions(...fragments) {
188
188
  }
189
189
 
190
190
  // packages/experimental/src/zukhruf/mailbox/sqlite.store.ts
191
+ import { transaction } from "@zukhruf/sqlite-transaction";
191
192
  import { DatabaseSync } from "node:sqlite";
192
193
  import { z } from "zod";
193
194
 
@@ -305,52 +306,64 @@ var SqliteMailboxStore = class extends MailboxStore {
305
306
  this.#database.exec(DDL);
306
307
  }
307
308
  async beginTurn(recipient, turnId) {
308
- this.#transaction(() => {
309
- this.#database.prepare(
310
- `INSERT INTO zukhruf_mailbox_activity (
309
+ transaction(
310
+ this.#database,
311
+ () => {
312
+ this.#database.prepare(
313
+ `INSERT INTO zukhruf_mailbox_activity (
311
314
  recipient_chat_id,
312
315
  recipient_user_id,
313
316
  turn_id
314
317
  ) VALUES (?, ?, ?)
315
318
  ON CONFLICT (recipient_chat_id, recipient_user_id)
316
319
  DO UPDATE SET turn_id = excluded.turn_id`
317
- ).run(recipient.chatId, recipient.userId, turnId);
318
- });
320
+ ).run(recipient.chatId, recipient.userId, turnId);
321
+ },
322
+ { mode: "immediate" }
323
+ );
319
324
  }
320
325
  async enqueue(communication) {
321
- return this.#transaction(() => {
322
- const consumedTerminal = communication.type === InterAgentCommunicationTypes.FinalAnswer && this.#hasConsumedTerminal(communication.id);
323
- if (!consumedTerminal) {
324
- this.#database.prepare(
325
- `INSERT OR IGNORE INTO zukhruf_mailbox_items (
326
+ return transaction(
327
+ this.#database,
328
+ () => {
329
+ const consumedTerminal = communication.type === InterAgentCommunicationTypes.FinalAnswer && this.#hasConsumedTerminal(communication.id);
330
+ if (!consumedTerminal) {
331
+ this.#database.prepare(
332
+ `INSERT OR IGNORE INTO zukhruf_mailbox_items (
326
333
  recipient_chat_id,
327
334
  recipient_user_id,
328
335
  communication
329
336
  ) VALUES (?, ?, ?)`
330
- ).run(
331
- communication.recipient.chatId,
332
- communication.recipient.userId,
333
- JSON.stringify(communication)
334
- );
335
- }
336
- return {
337
- recipientActive: this.#isTurnActive(communication.recipient)
338
- };
339
- });
337
+ ).run(
338
+ communication.recipient.chatId,
339
+ communication.recipient.userId,
340
+ JSON.stringify(communication)
341
+ );
342
+ }
343
+ return {
344
+ recipientActive: this.#isTurnActive(communication.recipient)
345
+ };
346
+ },
347
+ { mode: "immediate" }
348
+ );
340
349
  }
341
350
  async endTurn(recipient, turnId) {
342
- return this.#transaction(() => {
343
- const result = this.#database.prepare(
344
- `DELETE FROM zukhruf_mailbox_activity
351
+ return transaction(
352
+ this.#database,
353
+ () => {
354
+ const result = this.#database.prepare(
355
+ `DELETE FROM zukhruf_mailbox_activity
345
356
  WHERE recipient_chat_id = ?
346
357
  AND recipient_user_id = ?
347
358
  AND turn_id = ?`
348
- ).run(recipient.chatId, recipient.userId, turnId);
349
- return {
350
- hasPending: this.#hasPending(recipient),
351
- turnEnded: result.changes > 0
352
- };
353
- });
359
+ ).run(recipient.chatId, recipient.userId, turnId);
360
+ return {
361
+ hasPending: this.#hasPending(recipient),
362
+ turnEnded: result.changes > 0
363
+ };
364
+ },
365
+ { mode: "immediate" }
366
+ );
354
367
  }
355
368
  async hasPending(recipient) {
356
369
  return this.#hasPending(recipient);
@@ -391,53 +404,43 @@ var SqliteMailboxStore = class extends MailboxStore {
391
404
  return this.#drain(recipient, true);
392
405
  }
393
406
  #drain(recipient, leadingQueueOnly) {
394
- return this.#transaction(() => {
395
- const items = mailboxItemRowsSchema.parse(
396
- this.#database.prepare(
397
- `SELECT sequence, communication
407
+ return transaction(
408
+ this.#database,
409
+ () => {
410
+ const items = mailboxItemRowsSchema.parse(
411
+ this.#database.prepare(
412
+ `SELECT sequence, communication
398
413
  FROM zukhruf_mailbox_items
399
414
  WHERE recipient_chat_id = ?
400
415
  AND recipient_user_id = ?
401
416
  ORDER BY sequence`
402
- ).all(recipient.chatId, recipient.userId)
403
- );
404
- const firstTrigger = leadingQueueOnly ? items.findIndex(({ communication }) => communication.triggerTurn) : -1;
405
- const consumed = firstTrigger === -1 ? items : items.slice(0, firstTrigger);
406
- const lastConsumed = consumed.at(-1);
407
- if (lastConsumed) {
408
- const tombstone = this.#database.prepare(
409
- `INSERT OR IGNORE
417
+ ).all(recipient.chatId, recipient.userId)
418
+ );
419
+ const firstTrigger = leadingQueueOnly ? items.findIndex(({ communication }) => communication.triggerTurn) : -1;
420
+ const consumed = firstTrigger === -1 ? items : items.slice(0, firstTrigger);
421
+ const lastConsumed = consumed.at(-1);
422
+ if (lastConsumed) {
423
+ const tombstone = this.#database.prepare(
424
+ `INSERT OR IGNORE
410
425
  INTO zukhruf_mailbox_consumed_terminal_ids (communication_id)
411
426
  VALUES (?)`
412
- );
413
- for (const { communication } of consumed) {
414
- if (communication.type === InterAgentCommunicationTypes.FinalAnswer) {
415
- tombstone.run(communication.id);
427
+ );
428
+ for (const { communication } of consumed) {
429
+ if (communication.type === InterAgentCommunicationTypes.FinalAnswer) {
430
+ tombstone.run(communication.id);
431
+ }
416
432
  }
417
- }
418
- this.#database.prepare(
419
- `DELETE FROM zukhruf_mailbox_items
433
+ this.#database.prepare(
434
+ `DELETE FROM zukhruf_mailbox_items
420
435
  WHERE recipient_chat_id = ?
421
436
  AND recipient_user_id = ?
422
437
  AND sequence <= ?`
423
- ).run(recipient.chatId, recipient.userId, lastConsumed.sequence);
424
- }
425
- return consumed.map(({ communication }) => communication);
426
- });
427
- }
428
- #transaction(operation) {
429
- this.#database.exec("BEGIN IMMEDIATE");
430
- try {
431
- const result = operation();
432
- this.#database.exec("COMMIT");
433
- return result;
434
- } catch (error) {
435
- try {
436
- this.#database.exec("ROLLBACK");
437
- } catch {
438
- }
439
- throw error;
440
- }
438
+ ).run(recipient.chatId, recipient.userId, lastConsumed.sequence);
439
+ }
440
+ return consumed.map(({ communication }) => communication);
441
+ },
442
+ { mode: "immediate" }
443
+ );
441
444
  }
442
445
  close() {
443
446
  if (!this.#database.isOpen) return;