@copilotkit/core 1.87.1 → 1.88.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/index.cjs CHANGED
@@ -291,6 +291,7 @@ const STOP_RUN_EVENT = "stop_run";
291
291
  const CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS = 100;
292
292
  const CONNECT_IDLE_WARNING_MS = 3e4;
293
293
  const MAX_UNOPENED_CREDENTIAL_REFRESHES = 2;
294
+ const MAX_DELIVERED_UNNUMBERED_IDS = 1024;
294
295
  function isMcpResourceRead(input) {
295
296
  const forwardedProps = input.forwardedProps;
296
297
  const request = forwardedProps?.__proxiedMCPRequest;
@@ -323,7 +324,10 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
323
324
  */
324
325
  runGeneration = 0;
325
326
  sharedState;
326
- constructor(config, sharedState = { lastSeenEventIds: /* @__PURE__ */ new Map() }) {
327
+ constructor(config, sharedState = {
328
+ lastSeenEventIds: /* @__PURE__ */ new Map(),
329
+ lastSeenEventSeqs: /* @__PURE__ */ new Map()
330
+ }) {
327
331
  super();
328
332
  this.config = config;
329
333
  this.sharedState = sharedState;
@@ -623,7 +627,20 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
623
627
  bufferSize: 1,
624
628
  refCount: true
625
629
  }));
626
- const params = this.createThreadChannelParams(input, options.channelMode ?? options.streamMode, options.replayCursor);
630
+ const sessionCursor = {
631
+ latest: null,
632
+ delivered: {
633
+ runs: /* @__PURE__ */ new Map(),
634
+ unnumberedIds: /* @__PURE__ */ new Set()
635
+ }
636
+ };
637
+ let reconnectCursor = this.readDurableEventId(options.replayCursor ?? this.getReconnectCursor(input));
638
+ const initialParams = this.createThreadChannelParams(input, options.channelMode ?? options.streamMode, options.replayCursor);
639
+ const params = () => {
640
+ if (sessionCursor.latest === null) return initialParams;
641
+ reconnectCursor = sessionCursor.latest;
642
+ return this.createThreadChannelParams(input, "connect", sessionCursor.latest);
643
+ };
627
644
  const channel$ = ɵphoenixChannel$({
628
645
  socket$,
629
646
  topic: credentials.realtime.topic,
@@ -635,9 +652,8 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
635
652
  bufferSize: 1,
636
653
  refCount: true
637
654
  }));
638
- const reconnectCursor = this.readDurableEventId(options.replayCursor ?? this.getReconnectCursor(input));
639
655
  let latestObservedReplayCursor = null;
640
- const threadEvents$ = this.observeThreadEvents$(input.threadId, channel$, options).pipe((0, rxjs_operators.tap)((payload) => {
656
+ const threadEvents$ = this.observeThreadEvents$(input.threadId, channel$, options, sessionCursor).pipe((0, rxjs_operators.tap)((payload) => {
641
657
  latestObservedReplayCursor = this.readEventId(payload) ?? latestObservedReplayCursor;
642
658
  scheduleIdleWarning();
643
659
  }), (0, rxjs_operators.share)());
@@ -648,13 +664,13 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
648
664
  const replayFailed$ = channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, REPLAY_FAILED_EVENT)), (0, rxjs_operators.tap)(() => {
649
665
  controlCursor.replayFailed = true;
650
666
  }));
651
- const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor).pipe((0, rxjs_operators.tap)(() => {
667
+ const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor, sessionCursor).pipe((0, rxjs_operators.tap)(() => {
652
668
  replayFinished = true;
653
669
  scheduleIdleWarning();
654
670
  options.lifecycle?.onReplayFinished?.();
655
671
  }), (0, rxjs_operators.share)());
656
- const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT, controlCursor).pipe((0, rxjs_operators.share)());
657
- const replayRestart$ = (0, rxjs.merge)(this.observeControlEvent$(input.threadId, channel$, "phx_error", controlCursor), ɵobservePhoenixSocketSignals$(socket$).pipe((0, rxjs_operators.filter)((signal) => signal.type === "error"))).pipe((0, rxjs_operators.tap)(() => {
672
+ const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT, controlCursor, sessionCursor).pipe((0, rxjs_operators.share)());
673
+ const replayRestart$ = (0, rxjs.merge)(this.observeControlEvent$(input.threadId, channel$, "phx_error", controlCursor, sessionCursor), ɵobservePhoenixSocketSignals$(socket$).pipe((0, rxjs_operators.filter)((signal) => signal.type === "error"))).pipe((0, rxjs_operators.tap)(() => {
658
674
  replayFinished = false;
659
675
  warnedAboutIdle = false;
660
676
  clearIdleWarning();
@@ -685,17 +701,30 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
685
701
  if (signal.type === "open") onSocketOpen?.();
686
702
  })), 5);
687
703
  }
688
- observeThreadEvents$(threadId, channel$, options) {
689
- return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, CLIENT_AG_UI_EVENT)), (0, rxjs_operators.tap)((payload) => {
690
- this.updateLastSeenEventId(threadId, payload);
704
+ observeThreadEvents$(threadId, channel$, options, sessionCursor) {
705
+ return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, CLIENT_AG_UI_EVENT)), (0, rxjs_operators.filter)((payload) => {
706
+ const eventId = this.readEventId(payload);
707
+ if (eventId === null) return true;
708
+ if (this.markDelivered(sessionCursor.delivered, eventId, payload)) return true;
709
+ this.debugLogger?.lifecycle("INTELLIGENCE", "Dropping a replayed event this connection already delivered.", {
710
+ threadId,
711
+ eventId,
712
+ type: payload.type
713
+ });
714
+ return false;
715
+ }), (0, rxjs_operators.tap)((payload) => {
716
+ if (this.updateLastSeenEventId(threadId, payload)) sessionCursor.latest = this.readEventId(payload);
691
717
  }), (0, rxjs_operators.map)((payload) => options.streamMode === "run" ? (0, _copilotkit_shared.stripIntelligenceRoutingFields)(payload) : payload), (0, rxjs_operators.mergeMap)((payload) => this.createThreadNotifications(payload, {
692
718
  completeOnRunError: options.completeOnRunError,
693
719
  completeOnRunFinished: options.streamMode === "run",
694
720
  errorOnRunError: options.streamMode === "run"
695
721
  })), (0, rxjs.dematerialize)());
696
722
  }
697
- observeControlEvent$(threadId, channel$, eventName, controlCursor) {
698
- return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, eventName)), (0, rxjs_operators.tap)((payload) => this.updateLastSeenEventIdFromControl(threadId, payload, controlCursor)));
723
+ observeControlEvent$(threadId, channel$, eventName, controlCursor, sessionCursor) {
724
+ return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, eventName)), (0, rxjs_operators.tap)((payload) => {
725
+ const applied = this.updateLastSeenEventIdFromControl(threadId, payload, controlCursor);
726
+ if (applied !== null) sessionCursor.latest = applied;
727
+ }));
699
728
  }
700
729
  observeChannelEvent$(channel, eventName) {
701
730
  return ɵobservePhoenixEvent$(channel, eventName);
@@ -738,18 +767,37 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
738
767
  */
739
768
  clearReconnectCursor(threadId) {
740
769
  this.sharedState.lastSeenEventIds.delete(threadId);
770
+ this.sharedState.lastSeenEventSeqs?.delete(threadId);
741
771
  }
772
+ /**
773
+ * Move the thread's cursor to `payload`'s durable event id. Returns whether
774
+ * it moved.
775
+ *
776
+ * The cursor never moves back within a run: an event whose `cpki_event_seq`
777
+ * is at or below the one already recorded for the same run is a replay, and
778
+ * is ignored. `cpki_event_seq` is ordered per run only, so an event of
779
+ * another run, or one without a run id or sequence, cannot be compared and
780
+ * moves the cursor as before (the gateway delivers thread history in order).
781
+ */
742
782
  updateLastSeenEventId(threadId, payload) {
743
783
  const eventId = this.readEventId(payload);
744
- if (!eventId) return;
784
+ if (!eventId) return false;
785
+ const position = this.readEventSequencePosition(payload);
786
+ const seqs = this.sharedState.lastSeenEventSeqs;
787
+ const previous = seqs?.get(threadId);
788
+ if (position && previous && position.runId === previous.runId && position.seq <= previous.seq) return false;
745
789
  this.advanceLastSeenEventId(threadId, eventId);
790
+ if (position) seqs?.set(threadId, position);
791
+ else seqs?.delete(threadId);
792
+ return true;
746
793
  }
747
794
  updateLastSeenEventIdFromControl(threadId, payload, controlCursor) {
748
795
  const eventId = this.readControlEventId(payload);
749
- if (!eventId) return;
750
- if (eventId === controlCursor.applied && !controlCursor.replayFailed) return;
796
+ if (!eventId) return null;
797
+ if (eventId === controlCursor.applied && !controlCursor.replayFailed) return null;
751
798
  controlCursor.applied = eventId;
752
799
  this.advanceLastSeenEventId(threadId, eventId);
800
+ return eventId;
753
801
  }
754
802
  advanceLastSeenEventId(threadId, eventId) {
755
803
  this.sharedState.lastSeenEventIds.set(threadId, eventId);
@@ -760,6 +808,52 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
760
808
  const eventMetadata = metadata;
761
809
  return this.readDurableEventId(eventMetadata.cpki_event_id);
762
810
  }
811
+ /**
812
+ * Record `payload` as delivered. Returns false if this session already
813
+ * delivered it, true if it is new.
814
+ */
815
+ markDelivered(delivered, eventId, payload) {
816
+ const position = this.readEventSequencePosition(payload);
817
+ if (position === null) {
818
+ if (delivered.unnumberedIds.has(eventId)) return false;
819
+ if (delivered.unnumberedIds.size >= MAX_DELIVERED_UNNUMBERED_IDS) {
820
+ const oldest = delivered.unnumberedIds.values().next().value;
821
+ if (oldest !== void 0) delivered.unnumberedIds.delete(oldest);
822
+ }
823
+ delivered.unnumberedIds.add(eventId);
824
+ return true;
825
+ }
826
+ let run = delivered.runs.get(position.runId);
827
+ if (!run) {
828
+ run = {
829
+ from: position.seq,
830
+ through: position.seq - 1,
831
+ above: /* @__PURE__ */ new Set(),
832
+ below: /* @__PURE__ */ new Set()
833
+ };
834
+ delivered.runs.set(position.runId, run);
835
+ }
836
+ if (position.seq < run.from) {
837
+ if (run.below.has(position.seq)) return false;
838
+ run.below.add(position.seq);
839
+ return true;
840
+ }
841
+ if (position.seq <= run.through || run.above.has(position.seq)) return false;
842
+ run.above.add(position.seq);
843
+ while (run.above.delete(run.through + 1)) run.through += 1;
844
+ return true;
845
+ }
846
+ readEventSequencePosition(payload) {
847
+ const event = payload;
848
+ const runId = event.run_id ?? event.runId;
849
+ const metadata = event.metadata;
850
+ const seq = metadata && typeof metadata === "object" ? metadata.cpki_event_seq : void 0;
851
+ if (typeof runId !== "string" || runId === "" || typeof seq !== "number" || !Number.isFinite(seq)) return null;
852
+ return {
853
+ runId,
854
+ seq
855
+ };
856
+ }
763
857
  readControlEventId(payload) {
764
858
  if (!payload || typeof payload !== "object") return null;
765
859
  const controlPayload = payload;
@@ -4475,7 +4569,27 @@ var RunHandler = class {
4475
4569
  if (!logicalHandoff) logicalRunId = params.input.runId;
4476
4570
  return onRunStartedEvent?.(params);
4477
4571
  };
4572
+ let finishedCleanly = false;
4573
+ let leftCallsOpen = false;
4574
+ const onRunFinishedEvent = agentSubscriber.onRunFinishedEvent;
4575
+ const onRunErrorEvent = agentSubscriber.onRunErrorEvent;
4576
+ const onCustomEvent = agentSubscriber.onCustomEvent;
4577
+ agentSubscriber.onRunFinishedEvent = (params) => {
4578
+ const outcome = params.event.outcome?.type;
4579
+ if (outcome === void 0 || outcome === "success") finishedCleanly = true;
4580
+ else leftCallsOpen = true;
4581
+ return onRunFinishedEvent?.(params);
4582
+ };
4583
+ agentSubscriber.onRunErrorEvent = (params) => {
4584
+ leftCallsOpen = true;
4585
+ return onRunErrorEvent?.(params);
4586
+ };
4587
+ agentSubscriber.onCustomEvent = (params) => {
4588
+ if (params.event.name === "on_interrupt") leftCallsOpen = true;
4589
+ return onCustomEvent?.(params);
4590
+ };
4478
4591
  const pinRunIdOnWire = runId !== void 0 && !continuationHandoff;
4592
+ const tools = this.buildFrontendTools(agent.agentId);
4479
4593
  const agentRunInput = {
4480
4594
  forwardedProps: {
4481
4595
  ...this._internal.getPropertiesForAgent(agent.agentId),
@@ -4484,7 +4598,7 @@ var RunHandler = class {
4484
4598
  },
4485
4599
  ...resume !== void 0 ? { resume } : {},
4486
4600
  ...pinRunIdOnWire ? { runId } : {},
4487
- tools: this.buildFrontendTools(agent.agentId),
4601
+ tools,
4488
4602
  context: this._internal.getContextForAgent(agent.agentId)
4489
4603
  };
4490
4604
  const runAgentResult = await agent.runAgent(agentRunInput, agentSubscriber);
@@ -4494,7 +4608,8 @@ var RunHandler = class {
4494
4608
  runAgentResult,
4495
4609
  agent,
4496
4610
  runId: logicalRunId,
4497
- runController: controller
4611
+ runController: controller,
4612
+ advertisedToolNames: finishedCleanly && !leftCallsOpen ? new Set(tools.map((tool) => tool.name)) : void 0
4498
4613
  });
4499
4614
  } catch (error) {
4500
4615
  logicalHandoff?.cancel();
@@ -4540,7 +4655,7 @@ var RunHandler = class {
4540
4655
  /**
4541
4656
  * Process agent result and execute tools
4542
4657
  */
4543
- async processAgentResult({ runAgentResult, agent, runId, toolExecutionMode = "all", runController, signal = runController?.signal, connectionGeneration }) {
4658
+ async processAgentResult({ runAgentResult, agent, runId, toolExecutionMode = "all", runController, signal = runController?.signal, advertisedToolNames, connectionGeneration }) {
4544
4659
  const { newMessages } = runAgentResult;
4545
4660
  const agentId = agent.agentId;
4546
4661
  let needsFollowUp = false;
@@ -4577,11 +4692,18 @@ var RunHandler = class {
4577
4692
  continue;
4578
4693
  }
4579
4694
  if (toolExecutionMode === "restore" && (!executableTool || executableTool.handler && executableTool.type !== "human-in-the-loop" && executableTool.reconnectBehavior !== "resume-pending")) continue;
4580
- if (existingResult && this.isFrontendPlaceholderResult(existingResult) && (toolExecutionMode === "restore" || executableTool?.handler)) {
4581
- if (existingResultIndex !== -1) newMessages.splice(existingResultIndex, 1);
4695
+ const runResults = newMessages.filter((m) => m.role === "tool" && m.toolCallId === toolCall.id);
4696
+ const answersRemovedTool = !executableTool && !!advertisedToolNames?.has(toolCall.function.name) && runResults.every((m) => this.isFrontendPlaceholderResult(m));
4697
+ if (existingResult && this.isFrontendPlaceholderResult(existingResult) && (toolExecutionMode === "restore" || executableTool?.handler || answersRemovedTool)) {
4698
+ if (answersRemovedTool) {
4699
+ this.removePlaceholderResults(newMessages, toolCall.id);
4700
+ this.removePlaceholderResults(agent.messages, toolCall.id);
4701
+ } else {
4702
+ if (existingResultIndex !== -1) newMessages.splice(existingResultIndex, 1);
4703
+ const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
4704
+ if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
4705
+ }
4582
4706
  existingResult = void 0;
4583
- const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
4584
- if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
4585
4707
  }
4586
4708
  if (!existingResult) {
4587
4709
  executing.add(executionKey);
@@ -4617,7 +4739,7 @@ var RunHandler = class {
4617
4739
  ownsHandlerFollowUp ||= !!fallbackTool.handler;
4618
4740
  needsFollowUp = true;
4619
4741
  }
4620
- }
4742
+ } else if (advertisedToolNames?.has(toolCall.function.name)) await this.rejectUnregisteredTool(toolCall, message, agent, agentId);
4621
4743
  }
4622
4744
  } finally {
4623
4745
  executing.delete(executionKey);
@@ -4679,6 +4801,13 @@ var RunHandler = class {
4679
4801
  }
4680
4802
  return insertAt;
4681
4803
  }
4804
+ /** Removes, in place, every placeholder result for `toolCallId`. */
4805
+ removePlaceholderResults(messages, toolCallId) {
4806
+ for (let i = messages.length - 1; i >= 0; i--) {
4807
+ const m = messages[i];
4808
+ if (m.role === "tool" && m.toolCallId === toolCallId && this.isFrontendPlaceholderResult(m)) messages.splice(i, 1);
4809
+ }
4810
+ }
4682
4811
  /** Identify transport placeholders that must not count as completed answers. */
4683
4812
  isFrontendPlaceholderResult(message) {
4684
4813
  return message.role === "tool" && isForwardedToClientPlaceholder(message.content);
@@ -4894,6 +5023,36 @@ var RunHandler = class {
4894
5023
  return false;
4895
5024
  }
4896
5025
  /**
5026
+ * Answer a call to a tool the run was offered but that no frontend tool (and
5027
+ * no wildcard) handles any more, e.g. because its component unmounted. Left
5028
+ * open, the call has no result, and providers reject every later request in
5029
+ * the thread. Like a failed handler, the error result starts no follow-up:
5030
+ * the model sees it on the next turn.
5031
+ */
5032
+ async rejectUnregisteredTool(toolCall, message, agent, agentId) {
5033
+ const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
5034
+ if (messageIndex === -1 || agent.messages.some((m) => m.role === "tool" && m.toolCallId === toolCall.id)) return;
5035
+ const error = /* @__PURE__ */ new Error(`Tool not found: ${toolCall.function.name}`);
5036
+ const errorMessage = toolFailureMessage(error);
5037
+ agent.messages.splice(this.toolResultInsertionIndex(agent.messages, messageIndex, toolCall.id), 0, {
5038
+ id: (0, _copilotkit_shared.randomUUID)(),
5039
+ role: "tool",
5040
+ toolCallId: toolCall.id,
5041
+ content: `Error: ${errorMessage}`,
5042
+ error: errorMessage
5043
+ });
5044
+ await this._internal.emitError({
5045
+ error,
5046
+ code: CopilotKitCoreErrorCode.TOOL_NOT_FOUND,
5047
+ context: {
5048
+ agentId,
5049
+ toolCallId: toolCall.id,
5050
+ toolName: toolCall.function.name,
5051
+ messageId: message.id
5052
+ }
5053
+ });
5054
+ }
5055
+ /**
4897
5056
  * Programmatically execute a registered frontend tool without going through an LLM turn.
4898
5057
  * The handler runs, render components show up in the UI, and both the tool call and
4899
5058
  * result messages are added to `agent.messages`.