agents 0.14.3 → 0.14.5

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.
@@ -677,6 +677,13 @@ declare class ResumableStream {
677
677
  private _isFlushingChunks;
678
678
  private _lastCleanupTime;
679
679
  constructor(sql: SqlTaggedTemplate);
680
+ /**
681
+ * Add the #1691 recovery column to the metadata table for rows created before
682
+ * it existed. Inspects the current schema and only runs `alter table` when the
683
+ * column is absent — idempotent across Durable Object restarts, with no
684
+ * error-driven control flow.
685
+ */
686
+ private _migrateMetadataColumns;
680
687
  get activeStreamId(): string | null;
681
688
  get activeRequestId(): string | null;
682
689
  hasActiveStream(): boolean;
@@ -691,7 +698,19 @@ declare class ResumableStream {
691
698
  * @param requestId - The unique ID of the chat request
692
699
  * @returns The generated stream ID
693
700
  */
694
- start(requestId: string): string;
701
+ start(
702
+ requestId: string,
703
+ options?: {
704
+ messageId?: string;
705
+ }
706
+ ): string;
707
+ /**
708
+ * The assistant message id an orphaned stream was producing — the same id the
709
+ * live path persists under, so recovery re-associates reconstructed chunks
710
+ * with the correct message (#1691). Returns null when the row is missing or
711
+ * is a legacy row written before the `message_id` column existed.
712
+ */
713
+ getStreamMessageId(streamId: string): string | null;
695
714
  /**
696
715
  * Mark a stream as completed and flush any pending chunks.
697
716
  * @param streamId - The stream to mark as completed
@@ -606,10 +606,22 @@ var ResumableStream = class ResumableStream {
606
606
  created_at integer not null,
607
607
  completed_at integer
608
608
  )`;
609
+ this._migrateMetadataColumns();
609
610
  this.sql`create index if not exists idx_stream_chunks_stream_id
610
611
  on cf_ai_chat_stream_chunks(stream_id, chunk_index)`;
611
612
  this.restore();
612
613
  }
614
+ /**
615
+ * Add the #1691 recovery column to the metadata table for rows created before
616
+ * it existed. Inspects the current schema and only runs `alter table` when the
617
+ * column is absent — idempotent across Durable Object restarts, with no
618
+ * error-driven control flow.
619
+ */
620
+ _migrateMetadataColumns() {
621
+ if (!(this.sql`
622
+ select name from pragma_table_info('cf_ai_chat_stream_metadata')
623
+ ` ?? []).some((column) => column.name === "message_id")) this.sql`alter table cf_ai_chat_stream_metadata add column message_id text`;
624
+ }
613
625
  get activeStreamId() {
614
626
  return this._activeStreamId;
615
627
  }
@@ -632,20 +644,35 @@ var ResumableStream = class ResumableStream {
632
644
  * @param requestId - The unique ID of the chat request
633
645
  * @returns The generated stream ID
634
646
  */
635
- start(requestId) {
647
+ start(requestId, options = {}) {
636
648
  this.flushBuffer();
637
649
  const streamId = nanoid();
638
650
  this._activeStreamId = streamId;
639
651
  this._activeRequestId = requestId;
640
652
  this._segmentIndex = 0;
641
653
  this._isLive = true;
654
+ const messageId = options.messageId ?? null;
642
655
  this.sql`
643
- insert into cf_ai_chat_stream_metadata (id, request_id, status, created_at)
644
- values (${streamId}, ${requestId}, 'streaming', ${Date.now()})
656
+ insert into cf_ai_chat_stream_metadata (id, request_id, status, created_at, message_id)
657
+ values (${streamId}, ${requestId}, 'streaming', ${Date.now()}, ${messageId})
645
658
  `;
646
659
  return streamId;
647
660
  }
648
661
  /**
662
+ * The assistant message id an orphaned stream was producing — the same id the
663
+ * live path persists under, so recovery re-associates reconstructed chunks
664
+ * with the correct message (#1691). Returns null when the row is missing or
665
+ * is a legacy row written before the `message_id` column existed.
666
+ */
667
+ getStreamMessageId(streamId) {
668
+ const rows = this.sql`
669
+ select message_id from cf_ai_chat_stream_metadata
670
+ where id = ${streamId}
671
+ `;
672
+ if (!rows || rows.length === 0) return null;
673
+ return rows[0].message_id ?? null;
674
+ }
675
+ /**
649
676
  * Mark a stream as completed and flush any pending chunks.
650
677
  * @param streamId - The stream to mark as completed
651
678
  */