@copilotkit/core 1.87.0 → 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/generative-ui/index.d.cts +1 -1
- package/dist/generative-ui/index.d.mts +1 -1
- package/dist/{index-BC1dzn_F.d.mts → index-CDKkFjm_.d.cts} +49 -6
- package/dist/index-CDKkFjm_.d.cts.map +1 -0
- package/dist/{index-wj1XowZI.d.cts → index-CoZgNZB2.d.mts} +49 -6
- package/dist/index-CoZgNZB2.d.mts.map +1 -0
- package/dist/index.cjs +182 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +182 -23
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +187 -24
- package/dist/index.umd.js.map +1 -1
- package/package.json +3 -3
- package/dist/index-BC1dzn_F.d.mts.map +0 -1
- package/dist/index-wj1XowZI.d.cts.map +0 -1
package/dist/index.umd.js
CHANGED
|
@@ -330,6 +330,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
330
330
|
const CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS = 100;
|
|
331
331
|
const CONNECT_IDLE_WARNING_MS = 3e4;
|
|
332
332
|
const MAX_UNOPENED_CREDENTIAL_REFRESHES = 2;
|
|
333
|
+
const MAX_DELIVERED_UNNUMBERED_IDS = 1024;
|
|
333
334
|
function isMcpResourceRead(input) {
|
|
334
335
|
const forwardedProps = input.forwardedProps;
|
|
335
336
|
const request = forwardedProps === null || forwardedProps === void 0 ? void 0 : forwardedProps.__proxiedMCPRequest;
|
|
@@ -351,7 +352,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
351
352
|
return typeof agent === "object" && agent !== null && "activeRunCompletionPromise" in agent;
|
|
352
353
|
}
|
|
353
354
|
var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAgent {
|
|
354
|
-
constructor(config, sharedState = {
|
|
355
|
+
constructor(config, sharedState = {
|
|
356
|
+
lastSeenEventIds: /* @__PURE__ */ new Map(),
|
|
357
|
+
lastSeenEventSeqs: /* @__PURE__ */ new Map()
|
|
358
|
+
}) {
|
|
355
359
|
super();
|
|
356
360
|
_defineProperty(this, "config", void 0);
|
|
357
361
|
_defineProperty(this, "socket", null);
|
|
@@ -623,7 +627,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
623
627
|
}
|
|
624
628
|
observeThreadSession$(input, credentials, options) {
|
|
625
629
|
return (0, rxjs.defer)(() => {
|
|
626
|
-
var _options$lifecycle, _options$lifecycle$on, _options$
|
|
630
|
+
var _options$lifecycle, _options$lifecycle$on, _options$replayCursor, _options$channelMode;
|
|
627
631
|
(_options$lifecycle = options.lifecycle) === null || _options$lifecycle === void 0 || (_options$lifecycle$on = _options$lifecycle.onReplayStarted) === null || _options$lifecycle$on === void 0 || _options$lifecycle$on.call(_options$lifecycle);
|
|
628
632
|
let ownSocket = null;
|
|
629
633
|
let ownChannel = null;
|
|
@@ -661,7 +665,20 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
661
665
|
bufferSize: 1,
|
|
662
666
|
refCount: true
|
|
663
667
|
}));
|
|
664
|
-
const
|
|
668
|
+
const sessionCursor = {
|
|
669
|
+
latest: null,
|
|
670
|
+
delivered: {
|
|
671
|
+
runs: /* @__PURE__ */ new Map(),
|
|
672
|
+
unnumberedIds: /* @__PURE__ */ new Set()
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
let reconnectCursor = this.readDurableEventId((_options$replayCursor = options.replayCursor) !== null && _options$replayCursor !== void 0 ? _options$replayCursor : this.getReconnectCursor(input));
|
|
676
|
+
const initialParams = this.createThreadChannelParams(input, (_options$channelMode = options.channelMode) !== null && _options$channelMode !== void 0 ? _options$channelMode : options.streamMode, options.replayCursor);
|
|
677
|
+
const params = () => {
|
|
678
|
+
if (sessionCursor.latest === null) return initialParams;
|
|
679
|
+
reconnectCursor = sessionCursor.latest;
|
|
680
|
+
return this.createThreadChannelParams(input, "connect", sessionCursor.latest);
|
|
681
|
+
};
|
|
665
682
|
const channel$ = ɵphoenixChannel$({
|
|
666
683
|
socket$,
|
|
667
684
|
topic: credentials.realtime.topic,
|
|
@@ -673,9 +690,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
673
690
|
bufferSize: 1,
|
|
674
691
|
refCount: true
|
|
675
692
|
}));
|
|
676
|
-
const reconnectCursor = this.readDurableEventId((_options$replayCursor = options.replayCursor) !== null && _options$replayCursor !== void 0 ? _options$replayCursor : this.getReconnectCursor(input));
|
|
677
693
|
let latestObservedReplayCursor = null;
|
|
678
|
-
const threadEvents$ = this.observeThreadEvents$(input.threadId, channel$, options).pipe((0, rxjs_operators.tap)((payload) => {
|
|
694
|
+
const threadEvents$ = this.observeThreadEvents$(input.threadId, channel$, options, sessionCursor).pipe((0, rxjs_operators.tap)((payload) => {
|
|
679
695
|
var _this$readEventId;
|
|
680
696
|
latestObservedReplayCursor = (_this$readEventId = this.readEventId(payload)) !== null && _this$readEventId !== void 0 ? _this$readEventId : latestObservedReplayCursor;
|
|
681
697
|
scheduleIdleWarning();
|
|
@@ -687,14 +703,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
687
703
|
const replayFailed$ = channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, REPLAY_FAILED_EVENT)), (0, rxjs_operators.tap)(() => {
|
|
688
704
|
controlCursor.replayFailed = true;
|
|
689
705
|
}));
|
|
690
|
-
const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor).pipe((0, rxjs_operators.tap)(() => {
|
|
706
|
+
const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor, sessionCursor).pipe((0, rxjs_operators.tap)(() => {
|
|
691
707
|
var _options$lifecycle2, _options$lifecycle2$o;
|
|
692
708
|
replayFinished = true;
|
|
693
709
|
scheduleIdleWarning();
|
|
694
710
|
(_options$lifecycle2 = options.lifecycle) === null || _options$lifecycle2 === void 0 || (_options$lifecycle2$o = _options$lifecycle2.onReplayFinished) === null || _options$lifecycle2$o === void 0 || _options$lifecycle2$o.call(_options$lifecycle2);
|
|
695
711
|
}), (0, rxjs_operators.share)());
|
|
696
|
-
const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT, controlCursor).pipe((0, rxjs_operators.share)());
|
|
697
|
-
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)(() => {
|
|
712
|
+
const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT, controlCursor, sessionCursor).pipe((0, rxjs_operators.share)());
|
|
713
|
+
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)(() => {
|
|
698
714
|
var _options$lifecycle3, _options$lifecycle3$o;
|
|
699
715
|
replayFinished = false;
|
|
700
716
|
warnedAboutIdle = false;
|
|
@@ -727,17 +743,31 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
727
743
|
if (signal.type === "open") onSocketOpen === null || onSocketOpen === void 0 || onSocketOpen();
|
|
728
744
|
})), 5);
|
|
729
745
|
}
|
|
730
|
-
observeThreadEvents$(threadId, channel$, options) {
|
|
731
|
-
return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, CLIENT_AG_UI_EVENT)), (0, rxjs_operators.
|
|
732
|
-
|
|
746
|
+
observeThreadEvents$(threadId, channel$, options, sessionCursor) {
|
|
747
|
+
return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, CLIENT_AG_UI_EVENT)), (0, rxjs_operators.filter)((payload) => {
|
|
748
|
+
var _this$debugLogger3;
|
|
749
|
+
const eventId = this.readEventId(payload);
|
|
750
|
+
if (eventId === null) return true;
|
|
751
|
+
if (this.markDelivered(sessionCursor.delivered, eventId, payload)) return true;
|
|
752
|
+
(_this$debugLogger3 = this.debugLogger) === null || _this$debugLogger3 === void 0 || _this$debugLogger3.lifecycle("INTELLIGENCE", "Dropping a replayed event this connection already delivered.", {
|
|
753
|
+
threadId,
|
|
754
|
+
eventId,
|
|
755
|
+
type: payload.type
|
|
756
|
+
});
|
|
757
|
+
return false;
|
|
758
|
+
}), (0, rxjs_operators.tap)((payload) => {
|
|
759
|
+
if (this.updateLastSeenEventId(threadId, payload)) sessionCursor.latest = this.readEventId(payload);
|
|
733
760
|
}), (0, rxjs_operators.map)((payload) => options.streamMode === "run" ? (0, _copilotkit_shared.stripIntelligenceRoutingFields)(payload) : payload), (0, rxjs_operators.mergeMap)((payload) => this.createThreadNotifications(payload, {
|
|
734
761
|
completeOnRunError: options.completeOnRunError,
|
|
735
762
|
completeOnRunFinished: options.streamMode === "run",
|
|
736
763
|
errorOnRunError: options.streamMode === "run"
|
|
737
764
|
})), (0, rxjs.dematerialize)());
|
|
738
765
|
}
|
|
739
|
-
observeControlEvent$(threadId, channel$, eventName, controlCursor) {
|
|
740
|
-
return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, eventName)), (0, rxjs_operators.tap)((payload) =>
|
|
766
|
+
observeControlEvent$(threadId, channel$, eventName, controlCursor, sessionCursor) {
|
|
767
|
+
return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, eventName)), (0, rxjs_operators.tap)((payload) => {
|
|
768
|
+
const applied = this.updateLastSeenEventIdFromControl(threadId, payload, controlCursor);
|
|
769
|
+
if (applied !== null) sessionCursor.latest = applied;
|
|
770
|
+
}));
|
|
741
771
|
}
|
|
742
772
|
observeChannelEvent$(channel, eventName) {
|
|
743
773
|
return ɵobservePhoenixEvent$(channel, eventName);
|
|
@@ -781,19 +811,39 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
781
811
|
* churn so the gateway can resume.
|
|
782
812
|
*/
|
|
783
813
|
clearReconnectCursor(threadId) {
|
|
814
|
+
var _this$sharedState$las2;
|
|
784
815
|
this.sharedState.lastSeenEventIds.delete(threadId);
|
|
816
|
+
(_this$sharedState$las2 = this.sharedState.lastSeenEventSeqs) === null || _this$sharedState$las2 === void 0 || _this$sharedState$las2.delete(threadId);
|
|
785
817
|
}
|
|
818
|
+
/**
|
|
819
|
+
* Move the thread's cursor to `payload`'s durable event id. Returns whether
|
|
820
|
+
* it moved.
|
|
821
|
+
*
|
|
822
|
+
* The cursor never moves back within a run: an event whose `cpki_event_seq`
|
|
823
|
+
* is at or below the one already recorded for the same run is a replay, and
|
|
824
|
+
* is ignored. `cpki_event_seq` is ordered per run only, so an event of
|
|
825
|
+
* another run, or one without a run id or sequence, cannot be compared and
|
|
826
|
+
* moves the cursor as before (the gateway delivers thread history in order).
|
|
827
|
+
*/
|
|
786
828
|
updateLastSeenEventId(threadId, payload) {
|
|
787
829
|
const eventId = this.readEventId(payload);
|
|
788
|
-
if (!eventId) return;
|
|
830
|
+
if (!eventId) return false;
|
|
831
|
+
const position = this.readEventSequencePosition(payload);
|
|
832
|
+
const seqs = this.sharedState.lastSeenEventSeqs;
|
|
833
|
+
const previous = seqs === null || seqs === void 0 ? void 0 : seqs.get(threadId);
|
|
834
|
+
if (position && previous && position.runId === previous.runId && position.seq <= previous.seq) return false;
|
|
789
835
|
this.advanceLastSeenEventId(threadId, eventId);
|
|
836
|
+
if (position) seqs === null || seqs === void 0 || seqs.set(threadId, position);
|
|
837
|
+
else seqs === null || seqs === void 0 || seqs.delete(threadId);
|
|
838
|
+
return true;
|
|
790
839
|
}
|
|
791
840
|
updateLastSeenEventIdFromControl(threadId, payload, controlCursor) {
|
|
792
841
|
const eventId = this.readControlEventId(payload);
|
|
793
|
-
if (!eventId) return;
|
|
794
|
-
if (eventId === controlCursor.applied && !controlCursor.replayFailed) return;
|
|
842
|
+
if (!eventId) return null;
|
|
843
|
+
if (eventId === controlCursor.applied && !controlCursor.replayFailed) return null;
|
|
795
844
|
controlCursor.applied = eventId;
|
|
796
845
|
this.advanceLastSeenEventId(threadId, eventId);
|
|
846
|
+
return eventId;
|
|
797
847
|
}
|
|
798
848
|
advanceLastSeenEventId(threadId, eventId) {
|
|
799
849
|
this.sharedState.lastSeenEventIds.set(threadId, eventId);
|
|
@@ -804,6 +854,53 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
804
854
|
const eventMetadata = metadata;
|
|
805
855
|
return this.readDurableEventId(eventMetadata.cpki_event_id);
|
|
806
856
|
}
|
|
857
|
+
/**
|
|
858
|
+
* Record `payload` as delivered. Returns false if this session already
|
|
859
|
+
* delivered it, true if it is new.
|
|
860
|
+
*/
|
|
861
|
+
markDelivered(delivered, eventId, payload) {
|
|
862
|
+
const position = this.readEventSequencePosition(payload);
|
|
863
|
+
if (position === null) {
|
|
864
|
+
if (delivered.unnumberedIds.has(eventId)) return false;
|
|
865
|
+
if (delivered.unnumberedIds.size >= MAX_DELIVERED_UNNUMBERED_IDS) {
|
|
866
|
+
const oldest = delivered.unnumberedIds.values().next().value;
|
|
867
|
+
if (oldest !== void 0) delivered.unnumberedIds.delete(oldest);
|
|
868
|
+
}
|
|
869
|
+
delivered.unnumberedIds.add(eventId);
|
|
870
|
+
return true;
|
|
871
|
+
}
|
|
872
|
+
let run = delivered.runs.get(position.runId);
|
|
873
|
+
if (!run) {
|
|
874
|
+
run = {
|
|
875
|
+
from: position.seq,
|
|
876
|
+
through: position.seq - 1,
|
|
877
|
+
above: /* @__PURE__ */ new Set(),
|
|
878
|
+
below: /* @__PURE__ */ new Set()
|
|
879
|
+
};
|
|
880
|
+
delivered.runs.set(position.runId, run);
|
|
881
|
+
}
|
|
882
|
+
if (position.seq < run.from) {
|
|
883
|
+
if (run.below.has(position.seq)) return false;
|
|
884
|
+
run.below.add(position.seq);
|
|
885
|
+
return true;
|
|
886
|
+
}
|
|
887
|
+
if (position.seq <= run.through || run.above.has(position.seq)) return false;
|
|
888
|
+
run.above.add(position.seq);
|
|
889
|
+
while (run.above.delete(run.through + 1)) run.through += 1;
|
|
890
|
+
return true;
|
|
891
|
+
}
|
|
892
|
+
readEventSequencePosition(payload) {
|
|
893
|
+
var _event$run_id;
|
|
894
|
+
const event = payload;
|
|
895
|
+
const runId = (_event$run_id = event.run_id) !== null && _event$run_id !== void 0 ? _event$run_id : event.runId;
|
|
896
|
+
const metadata = event.metadata;
|
|
897
|
+
const seq = metadata && typeof metadata === "object" ? metadata.cpki_event_seq : void 0;
|
|
898
|
+
if (typeof runId !== "string" || runId === "" || typeof seq !== "number" || !Number.isFinite(seq)) return null;
|
|
899
|
+
return {
|
|
900
|
+
runId,
|
|
901
|
+
seq
|
|
902
|
+
};
|
|
903
|
+
}
|
|
807
904
|
readControlEventId(payload) {
|
|
808
905
|
var _controlPayload$lates;
|
|
809
906
|
if (!payload || typeof payload !== "object") return null;
|
|
@@ -4584,7 +4681,28 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4584
4681
|
if (!logicalHandoff) logicalRunId = params.input.runId;
|
|
4585
4682
|
return onRunStartedEvent === null || onRunStartedEvent === void 0 ? void 0 : onRunStartedEvent(params);
|
|
4586
4683
|
};
|
|
4684
|
+
let finishedCleanly = false;
|
|
4685
|
+
let leftCallsOpen = false;
|
|
4686
|
+
const onRunFinishedEvent = agentSubscriber.onRunFinishedEvent;
|
|
4687
|
+
const onRunErrorEvent = agentSubscriber.onRunErrorEvent;
|
|
4688
|
+
const onCustomEvent = agentSubscriber.onCustomEvent;
|
|
4689
|
+
agentSubscriber.onRunFinishedEvent = (params) => {
|
|
4690
|
+
var _params$event$outcome;
|
|
4691
|
+
const outcome = (_params$event$outcome = params.event.outcome) === null || _params$event$outcome === void 0 ? void 0 : _params$event$outcome.type;
|
|
4692
|
+
if (outcome === void 0 || outcome === "success") finishedCleanly = true;
|
|
4693
|
+
else leftCallsOpen = true;
|
|
4694
|
+
return onRunFinishedEvent === null || onRunFinishedEvent === void 0 ? void 0 : onRunFinishedEvent(params);
|
|
4695
|
+
};
|
|
4696
|
+
agentSubscriber.onRunErrorEvent = (params) => {
|
|
4697
|
+
leftCallsOpen = true;
|
|
4698
|
+
return onRunErrorEvent === null || onRunErrorEvent === void 0 ? void 0 : onRunErrorEvent(params);
|
|
4699
|
+
};
|
|
4700
|
+
agentSubscriber.onCustomEvent = (params) => {
|
|
4701
|
+
if (params.event.name === "on_interrupt") leftCallsOpen = true;
|
|
4702
|
+
return onCustomEvent === null || onCustomEvent === void 0 ? void 0 : onCustomEvent(params);
|
|
4703
|
+
};
|
|
4587
4704
|
const pinRunIdOnWire = runId !== void 0 && !continuationHandoff;
|
|
4705
|
+
const tools = this.buildFrontendTools(agent.agentId);
|
|
4588
4706
|
const agentRunInput = {
|
|
4589
4707
|
forwardedProps: {
|
|
4590
4708
|
...this._internal.getPropertiesForAgent(agent.agentId),
|
|
@@ -4593,7 +4711,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4593
4711
|
},
|
|
4594
4712
|
...resume !== void 0 ? { resume } : {},
|
|
4595
4713
|
...pinRunIdOnWire ? { runId } : {},
|
|
4596
|
-
tools
|
|
4714
|
+
tools,
|
|
4597
4715
|
context: this._internal.getContextForAgent(agent.agentId)
|
|
4598
4716
|
};
|
|
4599
4717
|
const runAgentResult = await agent.runAgent(agentRunInput, agentSubscriber);
|
|
@@ -4603,7 +4721,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4603
4721
|
runAgentResult,
|
|
4604
4722
|
agent,
|
|
4605
4723
|
runId: logicalRunId,
|
|
4606
|
-
runController: controller
|
|
4724
|
+
runController: controller,
|
|
4725
|
+
advertisedToolNames: finishedCleanly && !leftCallsOpen ? new Set(tools.map((tool) => tool.name)) : void 0
|
|
4607
4726
|
});
|
|
4608
4727
|
} catch (error) {
|
|
4609
4728
|
logicalHandoff === null || logicalHandoff === void 0 || logicalHandoff.cancel();
|
|
@@ -4650,7 +4769,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4650
4769
|
/**
|
|
4651
4770
|
* Process agent result and execute tools
|
|
4652
4771
|
*/
|
|
4653
|
-
async processAgentResult({ runAgentResult, agent, runId, toolExecutionMode = "all", runController, signal = runController === null || runController === void 0 ? void 0 : runController.signal, connectionGeneration }) {
|
|
4772
|
+
async processAgentResult({ runAgentResult, agent, runId, toolExecutionMode = "all", runController, signal = runController === null || runController === void 0 ? void 0 : runController.signal, advertisedToolNames, connectionGeneration }) {
|
|
4654
4773
|
var _agent$pendingInterru, _this$_executingToolC;
|
|
4655
4774
|
const { newMessages } = runAgentResult;
|
|
4656
4775
|
const agentId = agent.agentId;
|
|
@@ -4693,11 +4812,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4693
4812
|
continue;
|
|
4694
4813
|
}
|
|
4695
4814
|
if (toolExecutionMode === "restore" && (!executableTool || executableTool.handler && executableTool.type !== "human-in-the-loop" && executableTool.reconnectBehavior !== "resume-pending")) continue;
|
|
4696
|
-
|
|
4697
|
-
|
|
4815
|
+
const runResults = newMessages.filter((m) => m.role === "tool" && m.toolCallId === toolCall.id);
|
|
4816
|
+
const answersRemovedTool = !executableTool && !!(advertisedToolNames === null || advertisedToolNames === void 0 ? void 0 : advertisedToolNames.has(toolCall.function.name)) && runResults.every((m) => this.isFrontendPlaceholderResult(m));
|
|
4817
|
+
if (existingResult && this.isFrontendPlaceholderResult(existingResult) && (toolExecutionMode === "restore" || (executableTool === null || executableTool === void 0 ? void 0 : executableTool.handler) || answersRemovedTool)) {
|
|
4818
|
+
if (answersRemovedTool) {
|
|
4819
|
+
this.removePlaceholderResults(newMessages, toolCall.id);
|
|
4820
|
+
this.removePlaceholderResults(agent.messages, toolCall.id);
|
|
4821
|
+
} else {
|
|
4822
|
+
if (existingResultIndex !== -1) newMessages.splice(existingResultIndex, 1);
|
|
4823
|
+
const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
|
|
4824
|
+
if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
|
|
4825
|
+
}
|
|
4698
4826
|
existingResult = void 0;
|
|
4699
|
-
const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
|
|
4700
|
-
if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
|
|
4701
4827
|
}
|
|
4702
4828
|
if (!existingResult) {
|
|
4703
4829
|
var _this$_interactionAbo2, _interactionControlle;
|
|
@@ -4734,7 +4860,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4734
4860
|
ownsHandlerFollowUp || (ownsHandlerFollowUp = !!fallbackTool.handler);
|
|
4735
4861
|
needsFollowUp = true;
|
|
4736
4862
|
}
|
|
4737
|
-
}
|
|
4863
|
+
} else if (advertisedToolNames === null || advertisedToolNames === void 0 ? void 0 : advertisedToolNames.has(toolCall.function.name)) await this.rejectUnregisteredTool(toolCall, message, agent, agentId);
|
|
4738
4864
|
}
|
|
4739
4865
|
} finally {
|
|
4740
4866
|
executing.delete(executionKey);
|
|
@@ -4801,6 +4927,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4801
4927
|
}
|
|
4802
4928
|
return insertAt;
|
|
4803
4929
|
}
|
|
4930
|
+
/** Removes, in place, every placeholder result for `toolCallId`. */
|
|
4931
|
+
removePlaceholderResults(messages, toolCallId) {
|
|
4932
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
4933
|
+
const m = messages[i];
|
|
4934
|
+
if (m.role === "tool" && m.toolCallId === toolCallId && this.isFrontendPlaceholderResult(m)) messages.splice(i, 1);
|
|
4935
|
+
}
|
|
4936
|
+
}
|
|
4804
4937
|
/** Identify transport placeholders that must not count as completed answers. */
|
|
4805
4938
|
isFrontendPlaceholderResult(message) {
|
|
4806
4939
|
return message.role === "tool" && isForwardedToClientPlaceholder(message.content);
|
|
@@ -5028,6 +5161,36 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5028
5161
|
return false;
|
|
5029
5162
|
}
|
|
5030
5163
|
/**
|
|
5164
|
+
* Answer a call to a tool the run was offered but that no frontend tool (and
|
|
5165
|
+
* no wildcard) handles any more, e.g. because its component unmounted. Left
|
|
5166
|
+
* open, the call has no result, and providers reject every later request in
|
|
5167
|
+
* the thread. Like a failed handler, the error result starts no follow-up:
|
|
5168
|
+
* the model sees it on the next turn.
|
|
5169
|
+
*/
|
|
5170
|
+
async rejectUnregisteredTool(toolCall, message, agent, agentId) {
|
|
5171
|
+
const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
|
|
5172
|
+
if (messageIndex === -1 || agent.messages.some((m) => m.role === "tool" && m.toolCallId === toolCall.id)) return;
|
|
5173
|
+
const error = /* @__PURE__ */ new Error(`Tool not found: ${toolCall.function.name}`);
|
|
5174
|
+
const errorMessage = toolFailureMessage(error);
|
|
5175
|
+
agent.messages.splice(this.toolResultInsertionIndex(agent.messages, messageIndex, toolCall.id), 0, {
|
|
5176
|
+
id: (0, _copilotkit_shared.randomUUID)(),
|
|
5177
|
+
role: "tool",
|
|
5178
|
+
toolCallId: toolCall.id,
|
|
5179
|
+
content: `Error: ${errorMessage}`,
|
|
5180
|
+
error: errorMessage
|
|
5181
|
+
});
|
|
5182
|
+
await this._internal.emitError({
|
|
5183
|
+
error,
|
|
5184
|
+
code: CopilotKitCoreErrorCode.TOOL_NOT_FOUND,
|
|
5185
|
+
context: {
|
|
5186
|
+
agentId,
|
|
5187
|
+
toolCallId: toolCall.id,
|
|
5188
|
+
toolName: toolCall.function.name,
|
|
5189
|
+
messageId: message.id
|
|
5190
|
+
}
|
|
5191
|
+
});
|
|
5192
|
+
}
|
|
5193
|
+
/**
|
|
5031
5194
|
* Programmatically execute a registered frontend tool without going through an LLM turn.
|
|
5032
5195
|
* The handler runs, render components show up in the UI, and both the tool call and
|
|
5033
5196
|
* result messages are added to `agent.messages`.
|