@librechat/agents 3.3.0 → 3.3.2
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/cjs/agents/AgentContext.cjs +116 -2
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +2 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +229 -48
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +102 -27
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +11 -2
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/contextOverflowRecovery.cjs +132 -0
- package/dist/cjs/llm/contextOverflowRecovery.cjs.map +1 -0
- package/dist/cjs/llm/google/index.cjs +1 -1
- package/dist/cjs/llm/invoke.cjs +60 -3
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/main.cjs +20 -7
- package/dist/cjs/messages/prune.cjs +16 -5
- package/dist/cjs/messages/prune.cjs.map +1 -1
- package/dist/cjs/messages/recency.cjs +2 -0
- package/dist/cjs/messages/recency.cjs.map +1 -1
- package/dist/cjs/run.cjs +15 -2
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +205 -49
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +55 -7
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +2 -2
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +2 -2
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +69 -8
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +19 -15
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +32 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/utils/errors.cjs +317 -52
- package/dist/cjs/utils/errors.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +117 -3
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +2 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +227 -46
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +102 -27
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +11 -2
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/contextOverflowRecovery.mjs +130 -0
- package/dist/esm/llm/contextOverflowRecovery.mjs.map +1 -0
- package/dist/esm/llm/google/index.mjs +1 -1
- package/dist/esm/llm/invoke.mjs +59 -4
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/main.mjs +9 -9
- package/dist/esm/messages/prune.mjs +14 -6
- package/dist/esm/messages/prune.mjs.map +1 -1
- package/dist/esm/messages/recency.mjs +2 -1
- package/dist/esm/messages/recency.mjs.map +1 -1
- package/dist/esm/run.mjs +15 -2
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +205 -49
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +55 -7
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +3 -3
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +3 -3
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +62 -9
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +20 -16
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +32 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/utils/errors.mjs +317 -53
- package/dist/esm/utils/errors.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +62 -3
- package/dist/types/common/enum.d.ts +2 -0
- package/dist/types/graphs/Graph.d.ts +16 -2
- package/dist/types/graphs/MultiAgentGraph.d.ts +1 -0
- package/dist/types/llm/contextOverflowRecovery.d.ts +85 -0
- package/dist/types/llm/invoke.d.ts +35 -6
- package/dist/types/messages/prune.d.ts +10 -2
- package/dist/types/messages/recency.d.ts +1 -0
- package/dist/types/run.d.ts +2 -0
- package/dist/types/tools/CodeExecutor.d.ts +14 -1
- package/dist/types/types/llm.d.ts +7 -4
- package/dist/types/types/stream.d.ts +5 -4
- package/dist/types/types/summarize.d.ts +22 -0
- package/dist/types/utils/__tests__/fixtures/contextOverflowSignatures.d.ts +40 -0
- package/dist/types/utils/errors.d.ts +65 -16
- package/dist/types/utils/redactSecrets.d.ts +3 -0
- package/package.json +10 -11
- package/src/agents/AgentContext.ts +188 -7
- package/src/agents/__tests__/AgentContext.overflow.test.ts +205 -0
- package/src/common/enum.ts +2 -0
- package/src/graphs/Graph.ts +389 -58
- package/src/graphs/MultiAgentGraph.ts +184 -46
- package/src/graphs/__tests__/Graph.contextOverflow.test.ts +631 -0
- package/src/llm/__tests__/contextOverflowRecovery.test.ts +401 -0
- package/src/llm/__tests__/fallbackOverflow.test.ts +287 -0
- package/src/llm/anthropic/llm.spec.ts +8 -0
- package/src/llm/bedrock/index.ts +25 -12
- package/src/llm/contextOverflowRecovery.ts +292 -0
- package/src/llm/invoke.ts +119 -4
- package/src/messages/prune.ts +24 -11
- package/src/messages/recency.ts +3 -1
- package/src/run.ts +24 -1
- package/src/scripts/context-overflow-probe.ts +997 -0
- package/src/specs/agent-handoffs.test.ts +903 -1
- package/src/specs/context-overflow-recovery.live.test.ts +213 -0
- package/src/splitStream.test.ts +882 -0
- package/src/stream.ts +315 -51
- package/src/summarization/__tests__/aggregator.test.ts +83 -0
- package/src/summarization/__tests__/node.test.ts +139 -0
- package/src/summarization/node.ts +99 -14
- package/src/tools/BashExecutor.ts +4 -2
- package/src/tools/BashProgrammaticToolCalling.ts +4 -7
- package/src/tools/CodeExecutor.ts +119 -8
- package/src/tools/ProgrammaticToolCalling.ts +29 -27
- package/src/tools/ToolNode.ts +50 -8
- package/src/tools/__tests__/CodeApiAuthHeaders.test.ts +297 -3
- package/src/types/llm.ts +8 -1
- package/src/types/stream.ts +5 -4
- package/src/types/summarize.ts +22 -0
- package/src/utils/__tests__/errors.test.ts +270 -0
- package/src/utils/__tests__/fixtures/contextOverflowSignatures.ts +336 -0
- package/src/utils/__tests__/redactSecrets.test.ts +56 -0
- package/src/utils/errors.ts +484 -66
- package/src/utils/redactSecrets.ts +61 -0
package/dist/cjs/stream.cjs
CHANGED
|
@@ -782,12 +782,98 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
782
782
|
function createContentAggregator() {
|
|
783
783
|
const contentParts = [];
|
|
784
784
|
const stepMap = /* @__PURE__ */ new Map();
|
|
785
|
-
const
|
|
785
|
+
const toolCallContentIndexMap = /* @__PURE__ */ new Map();
|
|
786
|
+
const sourceContentIndexMap = /* @__PURE__ */ new Map();
|
|
787
|
+
const toolStepContentMap = /* @__PURE__ */ new Map();
|
|
788
|
+
let indexedContentLength = 0;
|
|
789
|
+
/** Physical append cursor; event/chunk indices are correlation keys, not array offsets. */
|
|
790
|
+
let nextContentIndex = 0;
|
|
786
791
|
const contentMetaMap = /* @__PURE__ */ new Map();
|
|
787
792
|
const getFirstContentPart = (content) => {
|
|
788
793
|
if (content == null) return;
|
|
789
794
|
return Array.isArray(content) ? content[0] : content;
|
|
790
795
|
};
|
|
796
|
+
const indexContentPart = (index, contentPart) => {
|
|
797
|
+
if (contentPart == null) return;
|
|
798
|
+
if (contentPart.type === "tool_call") {
|
|
799
|
+
const toolCallId = contentPart.tool_call.id;
|
|
800
|
+
if (toolCallId != null && toolCallId !== "") toolCallContentIndexMap.set(toolCallId, index);
|
|
801
|
+
}
|
|
802
|
+
const hasAgentId = contentPart.agentId != null && contentPart.agentId !== "";
|
|
803
|
+
const hasGroupId = contentPart.groupId != null;
|
|
804
|
+
if (hasAgentId || hasGroupId) {
|
|
805
|
+
const existingMeta = contentMetaMap.get(index) ?? {};
|
|
806
|
+
if (hasAgentId) existingMeta.agentId = contentPart.agentId;
|
|
807
|
+
if (hasGroupId) existingMeta.groupId = contentPart.groupId;
|
|
808
|
+
contentMetaMap.set(index, existingMeta);
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
const syncSeededContent = () => {
|
|
812
|
+
/** Hosts can seed pre-pause content after creating the aggregator. */
|
|
813
|
+
for (let index = indexedContentLength; index < contentParts.length; index++) indexContentPart(index, contentParts[index]);
|
|
814
|
+
indexedContentLength = contentParts.length;
|
|
815
|
+
nextContentIndex = Math.max(nextContentIndex, contentParts.length);
|
|
816
|
+
};
|
|
817
|
+
const getToolCallContentIndex = (toolCallId) => {
|
|
818
|
+
if (toolCallId == null || toolCallId === "") return;
|
|
819
|
+
syncSeededContent();
|
|
820
|
+
return toolCallContentIndexMap.get(toolCallId);
|
|
821
|
+
};
|
|
822
|
+
const allocateContentIndex = (minimumIndex = 0) => {
|
|
823
|
+
syncSeededContent();
|
|
824
|
+
const contentIndex = Math.max(nextContentIndex, minimumIndex);
|
|
825
|
+
nextContentIndex = contentIndex + 1;
|
|
826
|
+
return contentIndex;
|
|
827
|
+
};
|
|
828
|
+
const resolveSourceContentIndex = (sourceIndex) => {
|
|
829
|
+
const existingIndex = sourceContentIndexMap.get(sourceIndex);
|
|
830
|
+
if (existingIndex != null) return existingIndex;
|
|
831
|
+
const contentIndex = allocateContentIndex(sourceIndex);
|
|
832
|
+
sourceContentIndexMap.set(sourceIndex, contentIndex);
|
|
833
|
+
return contentIndex;
|
|
834
|
+
};
|
|
835
|
+
const createToolStepContentState = (contentIndex) => ({
|
|
836
|
+
indices: new Set([contentIndex]),
|
|
837
|
+
chunkIndices: /* @__PURE__ */ new Map(),
|
|
838
|
+
unclaimedIndices: new Set([contentIndex]),
|
|
839
|
+
unboundIndices: new Set([contentIndex]),
|
|
840
|
+
callIdsByIndex: /* @__PURE__ */ new Map()
|
|
841
|
+
});
|
|
842
|
+
const registerToolContentIndex = (state, contentIndex, toolCallId) => {
|
|
843
|
+
if (!state.indices.has(contentIndex)) {
|
|
844
|
+
state.indices.add(contentIndex);
|
|
845
|
+
state.unclaimedIndices.add(contentIndex);
|
|
846
|
+
state.unboundIndices.add(contentIndex);
|
|
847
|
+
}
|
|
848
|
+
if (toolCallId != null && toolCallId !== "") {
|
|
849
|
+
const existingToolCallId = state.callIdsByIndex.get(contentIndex);
|
|
850
|
+
if (existingToolCallId != null && existingToolCallId !== toolCallId && toolCallContentIndexMap.get(existingToolCallId) === contentIndex) toolCallContentIndexMap.delete(existingToolCallId);
|
|
851
|
+
toolCallContentIndexMap.set(toolCallId, contentIndex);
|
|
852
|
+
state.callIdsByIndex.set(contentIndex, toolCallId);
|
|
853
|
+
state.unboundIndices.delete(contentIndex);
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
const takeFirstIndex = (indices) => {
|
|
857
|
+
const contentIndex = indices.values().next().value;
|
|
858
|
+
if (contentIndex != null) indices.delete(contentIndex);
|
|
859
|
+
return contentIndex;
|
|
860
|
+
};
|
|
861
|
+
const setContentMeta = (index, runStep) => {
|
|
862
|
+
const hasAgentId = runStep.agentId != null && runStep.agentId !== "";
|
|
863
|
+
const hasGroupId = runStep.groupId != null;
|
|
864
|
+
if (!hasAgentId && !hasGroupId) return;
|
|
865
|
+
const existingMeta = contentMetaMap.get(index) ?? {};
|
|
866
|
+
if (hasAgentId) existingMeta.agentId = runStep.agentId;
|
|
867
|
+
if (hasGroupId) existingMeta.groupId = runStep.groupId;
|
|
868
|
+
contentMetaMap.set(index, existingMeta);
|
|
869
|
+
};
|
|
870
|
+
const applyContentMetadata = (index) => {
|
|
871
|
+
const contentPart = contentParts[index];
|
|
872
|
+
if (contentPart == null) return;
|
|
873
|
+
const meta = contentMetaMap.get(index);
|
|
874
|
+
if (meta?.agentId != null) contentPart.agentId = meta.agentId;
|
|
875
|
+
if (meta?.groupId != null) contentPart.groupId = meta.groupId;
|
|
876
|
+
};
|
|
791
877
|
const updateContent = (index, contentPart, finalUpdate = false) => {
|
|
792
878
|
if (!contentPart) {
|
|
793
879
|
console.warn("No content part found in 'updateContent'");
|
|
@@ -837,9 +923,13 @@ function createContentAggregator() {
|
|
|
837
923
|
/** When args are a valid object, they are likely already invoked */
|
|
838
924
|
let args = finalUpdate || typeof existingContent?.tool_call?.args === "object" || typeof toolCallArgs === "object" ? contentPart.tool_call.args : (existingContent?.tool_call?.args ?? "") + (toolCallArgs ?? "");
|
|
839
925
|
if (finalUpdate && args == null && existingContent?.tool_call?.args != null) args = existingContent.tool_call.args;
|
|
926
|
+
const id = getNonEmptyValue([incomingId, existingContent?.tool_call?.id]) ?? "";
|
|
927
|
+
const name = getNonEmptyValue([incomingName, existingContent?.tool_call?.name]) ?? "";
|
|
928
|
+
const existingToolCallId = existingContent?.tool_call?.id;
|
|
929
|
+
if (existingToolCallId != null && existingToolCallId !== "" && existingToolCallId !== id && toolCallContentIndexMap.get(existingToolCallId) === index) toolCallContentIndexMap.delete(existingToolCallId);
|
|
840
930
|
const newToolCall = {
|
|
841
|
-
id
|
|
842
|
-
name
|
|
931
|
+
id,
|
|
932
|
+
name,
|
|
843
933
|
args,
|
|
844
934
|
type: "tool_call"
|
|
845
935
|
};
|
|
@@ -857,10 +947,10 @@ function createContentAggregator() {
|
|
|
857
947
|
type: "tool_call",
|
|
858
948
|
tool_call: newToolCall
|
|
859
949
|
};
|
|
950
|
+
indexContentPart(index, contentParts[index]);
|
|
951
|
+
indexedContentLength = Math.max(indexedContentLength, index + 1);
|
|
860
952
|
}
|
|
861
|
-
|
|
862
|
-
if (meta?.agentId != null) contentParts[index].agentId = meta.agentId;
|
|
863
|
-
if (meta?.groupId != null) contentParts[index].groupId = meta.groupId;
|
|
953
|
+
applyContentMetadata(index);
|
|
864
954
|
};
|
|
865
955
|
const aggregateContent = ({ event, data }) => {
|
|
866
956
|
if (event === "on_summarize_delta") {
|
|
@@ -879,33 +969,52 @@ function createContentAggregator() {
|
|
|
879
969
|
const runStep = stepMap.get(summary.boundary.messageId);
|
|
880
970
|
if (!runStep) return;
|
|
881
971
|
contentParts[runStep.index] = summary;
|
|
972
|
+
applyContentMetadata(runStep.index);
|
|
882
973
|
return;
|
|
883
974
|
}
|
|
884
975
|
if (event === "on_run_step") {
|
|
885
|
-
const
|
|
976
|
+
const incomingRunStep = data;
|
|
977
|
+
const toolCalls = incomingRunStep.stepDetails.type === "tool_calls" ? incomingRunStep.stepDetails.tool_calls : void 0;
|
|
978
|
+
syncSeededContent();
|
|
979
|
+
let runStepIndex;
|
|
980
|
+
const toolCallIndices = [];
|
|
981
|
+
if (toolCalls && toolCalls.length > 0) {
|
|
982
|
+
for (let index = 0; index < toolCalls.length; index++) {
|
|
983
|
+
const toolCallId = toolCalls[index].id;
|
|
984
|
+
let contentIndex = getToolCallContentIndex(toolCallId);
|
|
985
|
+
if (contentIndex == null) contentIndex = index === 0 ? resolveSourceContentIndex(incomingRunStep.index) : allocateContentIndex();
|
|
986
|
+
else if (index === 0) sourceContentIndexMap.set(incomingRunStep.index, contentIndex);
|
|
987
|
+
toolCallIndices.push(contentIndex);
|
|
988
|
+
}
|
|
989
|
+
runStepIndex = toolCallIndices[0];
|
|
990
|
+
} else runStepIndex = resolveSourceContentIndex(incomingRunStep.index);
|
|
991
|
+
const runStep = runStepIndex !== incomingRunStep.index ? {
|
|
992
|
+
...incomingRunStep,
|
|
993
|
+
index: runStepIndex
|
|
994
|
+
} : incomingRunStep;
|
|
886
995
|
stepMap.set(runStep.id, runStep);
|
|
887
|
-
|
|
888
|
-
const hasGroupId = runStep.groupId != null;
|
|
889
|
-
if (hasAgentId || hasGroupId) {
|
|
890
|
-
const existingMeta = contentMetaMap.get(runStep.index) ?? {};
|
|
891
|
-
if (hasAgentId) existingMeta.agentId = runStep.agentId;
|
|
892
|
-
if (hasGroupId) existingMeta.groupId = runStep.groupId;
|
|
893
|
-
contentMetaMap.set(runStep.index, existingMeta);
|
|
894
|
-
}
|
|
996
|
+
setContentMeta(runStep.index, runStep);
|
|
895
997
|
if (runStep.summary != null) updateContent(runStep.index, runStep.summary);
|
|
896
|
-
if (runStep.stepDetails.type === "tool_calls"
|
|
897
|
-
const
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
998
|
+
if (runStep.stepDetails.type === "tool_calls") {
|
|
999
|
+
const toolStepContent = toolStepContentMap.get(runStep.id) ?? createToolStepContentState(runStep.index);
|
|
1000
|
+
registerToolContentIndex(toolStepContent, runStep.index);
|
|
1001
|
+
runStep.stepDetails.tool_calls?.forEach((toolCall, toolCallIndex) => {
|
|
1002
|
+
const contentIndex = toolCallIndices[toolCallIndex] ?? runStep.index;
|
|
1003
|
+
const toolCallId = toolCall.id ?? "";
|
|
1004
|
+
registerToolContentIndex(toolStepContent, contentIndex, toolCallId);
|
|
1005
|
+
const contentPart = {
|
|
1006
|
+
type: "tool_call",
|
|
1007
|
+
tool_call: {
|
|
1008
|
+
args: toolCall.args,
|
|
1009
|
+
name: toolCall.name,
|
|
1010
|
+
id: toolCallId
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
setContentMeta(contentIndex, runStep);
|
|
1014
|
+
updateContent(contentIndex, contentPart);
|
|
1015
|
+
});
|
|
1016
|
+
toolStepContentMap.set(runStep.id, toolStepContent);
|
|
1017
|
+
}
|
|
909
1018
|
} else if (event === "on_message_delta") {
|
|
910
1019
|
const messageDelta = data;
|
|
911
1020
|
const runStep = stepMap.get(messageDelta.id);
|
|
@@ -918,7 +1027,14 @@ function createContentAggregator() {
|
|
|
918
1027
|
} else if (event === "on_agent_update" && data?.agent_update) {
|
|
919
1028
|
const contentPart = data;
|
|
920
1029
|
if (!contentPart) return;
|
|
921
|
-
|
|
1030
|
+
const contentIndex = resolveSourceContentIndex(contentPart.agent_update.index);
|
|
1031
|
+
updateContent(contentIndex, {
|
|
1032
|
+
...contentPart,
|
|
1033
|
+
agent_update: {
|
|
1034
|
+
...contentPart.agent_update,
|
|
1035
|
+
index: contentIndex
|
|
1036
|
+
}
|
|
1037
|
+
});
|
|
922
1038
|
} else if (event === "on_reasoning_delta") {
|
|
923
1039
|
const reasoningDelta = data;
|
|
924
1040
|
const runStep = stepMap.get(reasoningDelta.id);
|
|
@@ -935,35 +1051,75 @@ function createContentAggregator() {
|
|
|
935
1051
|
console.warn("No run step or runId found for run step delta event");
|
|
936
1052
|
return;
|
|
937
1053
|
}
|
|
938
|
-
if (runStepDelta.delta.type === "tool_calls" && runStepDelta.delta.tool_calls)
|
|
939
|
-
const
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
1054
|
+
if (runStepDelta.delta.type === "tool_calls" && runStepDelta.delta.tool_calls) {
|
|
1055
|
+
const toolStepContent = toolStepContentMap.get(runStepDelta.id) ?? createToolStepContentState(runStep.index);
|
|
1056
|
+
runStepDelta.delta.tool_calls.forEach((toolCallDelta) => {
|
|
1057
|
+
const chunkIndex = typeof toolCallDelta.index === "number" ? toolCallDelta.index : void 0;
|
|
1058
|
+
const explicitToolCallId = toolCallDelta.id != null && toolCallDelta.id !== "" ? toolCallDelta.id : void 0;
|
|
1059
|
+
let contentIndex = getToolCallContentIndex(explicitToolCallId);
|
|
1060
|
+
if (contentIndex == null && chunkIndex != null) contentIndex = toolStepContent.chunkIndices.get(chunkIndex);
|
|
1061
|
+
const soleContentIndex = toolStepContent.indices.size === 1 ? toolStepContent.indices.values().next().value : void 0;
|
|
1062
|
+
if (contentIndex == null && explicitToolCallId == null && soleContentIndex != null && (chunkIndex == null || toolStepContent.callIdsByIndex.has(soleContentIndex) || toolStepContent.unclaimedIndices.has(soleContentIndex))) contentIndex = soleContentIndex;
|
|
1063
|
+
if (contentIndex == null) {
|
|
1064
|
+
if (chunkIndex == null && explicitToolCallId == null) {
|
|
1065
|
+
console.warn("No tool call id or chunk index found for run step delta event");
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
contentIndex = explicitToolCallId == null ? takeFirstIndex(toolStepContent.unclaimedIndices) : takeFirstIndex(toolStepContent.unboundIndices);
|
|
1069
|
+
contentIndex ??= allocateContentIndex();
|
|
948
1070
|
}
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
1071
|
+
registerToolContentIndex(toolStepContent, contentIndex, explicitToolCallId);
|
|
1072
|
+
if (chunkIndex != null) {
|
|
1073
|
+
toolStepContent.chunkIndices.set(chunkIndex, contentIndex);
|
|
1074
|
+
toolStepContent.unclaimedIndices.delete(contentIndex);
|
|
1075
|
+
toolStepContent.unboundIndices.delete(contentIndex);
|
|
1076
|
+
}
|
|
1077
|
+
const toolCallId = explicitToolCallId ?? toolStepContent.callIdsByIndex.get(contentIndex);
|
|
1078
|
+
const contentPart = {
|
|
1079
|
+
type: "tool_call",
|
|
1080
|
+
tool_call: {
|
|
1081
|
+
args: toolCallDelta.args ?? "",
|
|
1082
|
+
name: toolCallDelta.name,
|
|
1083
|
+
id: toolCallId,
|
|
1084
|
+
auth: runStepDelta.delta.auth,
|
|
1085
|
+
expires_at: runStepDelta.delta.expires_at
|
|
1086
|
+
}
|
|
1087
|
+
};
|
|
1088
|
+
setContentMeta(contentIndex, runStep);
|
|
1089
|
+
updateContent(contentIndex, contentPart);
|
|
1090
|
+
});
|
|
1091
|
+
toolStepContentMap.set(runStepDelta.id, toolStepContent);
|
|
1092
|
+
}
|
|
952
1093
|
} else if (event === "on_run_step_completed") {
|
|
953
1094
|
const { result } = data;
|
|
954
1095
|
const { id: stepId } = result;
|
|
955
1096
|
const runStep = stepMap.get(stepId);
|
|
956
|
-
if (
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
1097
|
+
if (result.type === "summary" && "summary" in result) {
|
|
1098
|
+
if (!runStep) {
|
|
1099
|
+
console.warn("No run step or runId found for completed step event");
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
contentParts[runStep.index] = result.summary;
|
|
1103
|
+
applyContentMetadata(runStep.index);
|
|
1104
|
+
} else if ("tool_call" in result) {
|
|
1105
|
+
let contentIndex = getToolCallContentIndex(result.tool_call.id);
|
|
1106
|
+
if (contentIndex == null && runStep != null) {
|
|
1107
|
+
const toolStepContent = toolStepContentMap.get(runStep.id);
|
|
1108
|
+
if (toolStepContent?.indices.size === 1) contentIndex = toolStepContent.indices.values().next().value;
|
|
1109
|
+
else if (toolStepContent == null) {
|
|
1110
|
+
if (((runStep.stepDetails.type === "tool_calls" ? runStep.stepDetails.tool_calls : void 0)?.length ?? 0) <= 1) contentIndex = runStep.index;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
if (contentIndex == null) {
|
|
1114
|
+
console.warn("No run step or tool call found for completed step event");
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
if (runStep != null) setContentMeta(contentIndex, runStep);
|
|
962
1118
|
const contentPart = {
|
|
963
1119
|
type: "tool_call",
|
|
964
1120
|
tool_call: result.tool_call
|
|
965
1121
|
};
|
|
966
|
-
updateContent(
|
|
1122
|
+
updateContent(contentIndex, contentPart, true);
|
|
967
1123
|
}
|
|
968
1124
|
}
|
|
969
1125
|
};
|