@cloudflare/ai-chat 0.1.6 → 0.1.8

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.
@@ -9,6 +9,7 @@ declare function withDurableChat<TBase extends AIChatAgentLike>(Base: TBase): {
9
9
  new (...args: any[]): {
10
10
  scheduleEvery: <T = string>(intervalSeconds: number, callback: keyof AIChatAgent<Cloudflare.Env, unknown>, payload?: T | undefined, options?: {
11
11
  retry?: agents.RetryOptions;
12
+ _idempotent?: boolean;
12
13
  }) => Promise<agents.Schedule<T>>;
13
14
  cancelSchedule: (id: string) => Promise<boolean>;
14
15
  keepAlive: () => Promise<() => void>;
package/dist/index.js CHANGED
@@ -474,11 +474,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
474
474
  type: MessageType.CF_AGENT_CHAT_MESSAGES
475
475
  }, [connection.id]);
476
476
  await this.persistMessages(transformedMessages, [connection.id], { _deleteStaleRows: true });
477
- this.observability?.emit({
478
- payload: {},
479
- timestamp: Date.now(),
480
- type: "message:request"
481
- });
477
+ this._emit("message:request");
482
478
  const chatMessageId = data.id;
483
479
  const abortSignal = this._getAbortSignal(chatMessageId);
484
480
  return this._tryCatchChat(async () => {
@@ -518,11 +514,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
518
514
  this._persistedMessageCache.clear();
519
515
  this.messages = [];
520
516
  this._broadcastChatMessage({ type: MessageType.CF_AGENT_CHAT_CLEAR }, [connection.id]);
521
- this.observability?.emit({
522
- type: "message:clear",
523
- payload: {},
524
- timestamp: Date.now()
525
- });
517
+ this._emit("message:clear");
526
518
  return;
527
519
  }
528
520
  if (data.type === MessageType.CF_AGENT_CHAT_MESSAGES) {
@@ -532,15 +524,12 @@ var AIChatAgent = class AIChatAgent extends Agent {
532
524
  }
533
525
  if (data.type === MessageType.CF_AGENT_CHAT_REQUEST_CANCEL) {
534
526
  this._cancelChatRequest(data.id);
535
- this.observability?.emit({
536
- type: "message:cancel",
537
- payload: { requestId: data.id },
538
- timestamp: Date.now()
539
- });
527
+ this._emit("message:cancel", { requestId: data.id });
540
528
  return;
541
529
  }
542
530
  if (data.type === MessageType.CF_AGENT_STREAM_RESUME_REQUEST) {
543
531
  if (this._resumableStream.hasActiveStream()) this._notifyStreamResuming(connection);
532
+ else connection.send(JSON.stringify({ type: MessageType.CF_AGENT_STREAM_RESUME_NONE }));
544
533
  return;
545
534
  }
546
535
  if (data.type === MessageType.CF_AGENT_STREAM_RESUME_ACK) {
@@ -558,13 +547,9 @@ var AIChatAgent = class AIChatAgent extends Agent {
558
547
  this._persistRequestContext();
559
548
  }
560
549
  const overrideState = state === "output-error" ? "output-error" : void 0;
561
- this.observability?.emit({
562
- type: "tool:result",
563
- payload: {
564
- toolCallId,
565
- toolName
566
- },
567
- timestamp: Date.now()
550
+ this._emit("tool:result", {
551
+ toolCallId,
552
+ toolName
568
553
  });
569
554
  this._applyToolResult(toolCallId, toolName, output, overrideState, errorText).then((applied) => {
570
555
  if (applied && autoContinue) {
@@ -603,13 +588,9 @@ var AIChatAgent = class AIChatAgent extends Agent {
603
588
  }
604
589
  if (data.type === MessageType.CF_AGENT_TOOL_APPROVAL) {
605
590
  const { toolCallId, approved, autoContinue } = data;
606
- this.observability?.emit({
607
- type: "tool:approval",
608
- payload: {
609
- toolCallId,
610
- approved
611
- },
612
- timestamp: Date.now()
591
+ this._emit("tool:approval", {
592
+ toolCallId,
593
+ approved
613
594
  });
614
595
  this._applyToolApproval(toolCallId, approved).then((applied) => {
615
596
  if (applied && autoContinue) {
@@ -1550,11 +1531,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
1550
1531
  type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
1551
1532
  ...continuation && { continuation: true }
1552
1533
  });
1553
- this.observability?.emit({
1554
- type: "message:error",
1555
- payload: { error: error instanceof Error ? error.message : String(error) },
1556
- timestamp: Date.now()
1557
- });
1534
+ this._emit("message:error", { error: error instanceof Error ? error.message : String(error) });
1558
1535
  }
1559
1536
  throw error;
1560
1537
  } finally {
@@ -1569,11 +1546,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
1569
1546
  }
1570
1547
  if (chatMessageId) {
1571
1548
  this._removeAbortController(chatMessageId);
1572
- if (streamCompleted.value) this.observability?.emit({
1573
- payload: {},
1574
- timestamp: Date.now(),
1575
- type: "message:response"
1576
- });
1549
+ if (streamCompleted.value) this._emit("message:response");
1577
1550
  }
1578
1551
  }
1579
1552
  if (message.parts.length > 0) if (earlyPersistedId) {