@anvia/core 1.0.0-rc.5 → 1.0.0-rc.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.
Files changed (109) hide show
  1. package/README.md +42 -9
  2. package/dist/agent/index.d.ts +20 -14
  3. package/dist/agent/index.js +15 -15
  4. package/dist/agent/interactions/index.d.ts +1 -1
  5. package/dist/agent/interactions/index.js +2 -2
  6. package/dist/{agent-LlCD_2UQ.d.ts → agent-XPQBM4rX.d.ts} +10 -10
  7. package/dist/{chunk-UBE4WH6G.js → chunk-3RWESPUG.js} +122 -3
  8. package/dist/chunk-3RWESPUG.js.map +1 -0
  9. package/dist/{chunk-BAEFBTOW.js → chunk-3SKZ2BKP.js} +7 -5
  10. package/dist/chunk-3SKZ2BKP.js.map +1 -0
  11. package/dist/{chunk-SROQA7I7.js → chunk-3XQGVDU5.js} +2 -2
  12. package/dist/chunk-3XQGVDU5.js.map +1 -0
  13. package/dist/{chunk-DNQNOHD5.js → chunk-5C6B6ZRH.js} +2 -2
  14. package/dist/{chunk-E5J4OY4K.js → chunk-6BBOCFHV.js} +2 -2
  15. package/dist/{chunk-7YAH2F3J.js → chunk-76JLMBVT.js} +24 -12
  16. package/dist/chunk-76JLMBVT.js.map +1 -0
  17. package/dist/chunk-7JLAIN6E.js +15 -0
  18. package/dist/chunk-7JLAIN6E.js.map +1 -0
  19. package/dist/chunk-7PHMK7W4.js +1516 -0
  20. package/dist/chunk-7PHMK7W4.js.map +1 -0
  21. package/dist/{chunk-MGNTXGTX.js → chunk-D4PACGPA.js} +1 -1
  22. package/dist/{chunk-7OCVDT2X.js → chunk-EFLT7XZD.js} +9 -3
  23. package/dist/chunk-EFLT7XZD.js.map +1 -0
  24. package/dist/{chunk-RZDI7B6Z.js → chunk-EMZKPRE6.js} +9 -8
  25. package/dist/chunk-EMZKPRE6.js.map +1 -0
  26. package/dist/{chunk-HVHTVGPP.js → chunk-I3Q7XSBB.js} +9 -3
  27. package/dist/chunk-I3Q7XSBB.js.map +1 -0
  28. package/dist/{chunk-YDJA4R37.js → chunk-LTXABCOO.js} +4 -4
  29. package/dist/{chunk-BEU6FCTN.js → chunk-OJBFDBLG.js} +2 -2
  30. package/dist/{chunk-G5OACQEI.js → chunk-QGX73TSQ.js} +2 -2
  31. package/dist/{chunk-JXZTTNCR.js → chunk-SJMSS5SI.js} +3 -3
  32. package/dist/{chunk-JXZTTNCR.js.map → chunk-SJMSS5SI.js.map} +1 -1
  33. package/dist/{chunk-YXLDR2O4.js → chunk-T2C3CGDQ.js} +9 -3
  34. package/dist/chunk-T2C3CGDQ.js.map +1 -0
  35. package/dist/{chunk-TEV3OB7N.js → chunk-X4LK4ODN.js} +10 -3
  36. package/dist/chunk-X4LK4ODN.js.map +1 -0
  37. package/dist/{chunk-FCANRQ5Q.js → chunk-YA2655FX.js} +308 -448
  38. package/dist/chunk-YA2655FX.js.map +1 -0
  39. package/dist/{chunk-H7BI3BSP.js → chunk-ZA564323.js} +2 -2
  40. package/dist/{client-DvUatElp.d.ts → client-DTPl0hZv.d.ts} +2 -2
  41. package/dist/completion/index.d.ts +3 -3
  42. package/dist/completion/index.js +12 -5
  43. package/dist/{dynamic-tools-AEBOTgu1.d.ts → dynamic-tools-aIZlg5x1.d.ts} +3 -3
  44. package/dist/embeddings/index.js +2 -2
  45. package/dist/evals/index.d.ts +6 -6
  46. package/dist/evals/index.js +51 -19
  47. package/dist/evals/index.js.map +1 -1
  48. package/dist/extractor/index.d.ts +1 -1
  49. package/dist/extractor/index.js +7 -6
  50. package/dist/guardrails/index.d.ts +4 -4
  51. package/dist/image-generation/index.d.ts +1 -1
  52. package/dist/image-generation/index.js +4 -2
  53. package/dist/index.d.ts +14 -14
  54. package/dist/index.js +25 -19
  55. package/dist/internal/agent.d.ts +11 -11
  56. package/dist/internal/agent.js +15 -15
  57. package/dist/mcp/index.d.ts +4 -4
  58. package/dist/mcp/index.js +10 -8
  59. package/dist/memory/index.d.ts +3 -3
  60. package/dist/memory/index.js +6 -5
  61. package/dist/{middleware-BqtLB_Xt.d.ts → middleware-kcF8AusP.d.ts} +8 -5
  62. package/dist/observability/index.d.ts +6 -6
  63. package/dist/observability/index.js +7 -6
  64. package/dist/pipeline/index.d.ts +11 -11
  65. package/dist/pipeline/index.js +8 -7
  66. package/dist/pipeline/index.js.map +1 -1
  67. package/dist/provider-output-error-BEwDgVv3.d.ts +99 -0
  68. package/dist/skills/index.d.ts +4 -4
  69. package/dist/skills/index.js +7 -8
  70. package/dist/speech-generation/index.d.ts +1 -1
  71. package/dist/speech-generation/index.js +4 -2
  72. package/dist/{text-C_6eKrbC.d.ts → text-ZLECpQE7.d.ts} +1 -1
  73. package/dist/{think-tool-Cz3QYNPX.d.ts → think-tool-BwCsJ365.d.ts} +1 -1
  74. package/dist/tool/index.d.ts +6 -6
  75. package/dist/tool/index.js +6 -6
  76. package/dist/{tool-r62gf3QY.d.ts → tool-BpqpoRSE.d.ts} +1 -1
  77. package/dist/transcription/index.d.ts +1 -1
  78. package/dist/transcription/index.js +4 -2
  79. package/dist/{types-C3gv9EX6.d.ts → types-CXNE592e.d.ts} +1 -1
  80. package/dist/{types-RT3cuyRk.d.ts → types-DC1U1XwW.d.ts} +1 -1
  81. package/dist/{types-DbolvLtZ.d.ts → types-DKOXVTcq.d.ts} +1 -1
  82. package/dist/{types-D9rKQ_as.d.ts → types-DgvozfPc.d.ts} +11 -3
  83. package/dist/{types-CIPaIbZo.d.ts → types-DhkodEft.d.ts} +1 -1
  84. package/dist/{types-1KpjDfoN.d.ts → types-Sue2hb_L.d.ts} +7 -5
  85. package/dist/vector-store/index.d.ts +4 -4
  86. package/dist/vector-store/index.js +5 -5
  87. package/package.json +1 -1
  88. package/dist/chunk-7OCVDT2X.js.map +0 -1
  89. package/dist/chunk-7YAH2F3J.js.map +0 -1
  90. package/dist/chunk-BAEFBTOW.js.map +0 -1
  91. package/dist/chunk-CQNNSZPG.js +0 -9
  92. package/dist/chunk-CQNNSZPG.js.map +0 -1
  93. package/dist/chunk-FCANRQ5Q.js.map +0 -1
  94. package/dist/chunk-HSA6NL2E.js +0 -517
  95. package/dist/chunk-HSA6NL2E.js.map +0 -1
  96. package/dist/chunk-HVHTVGPP.js.map +0 -1
  97. package/dist/chunk-RZDI7B6Z.js.map +0 -1
  98. package/dist/chunk-SROQA7I7.js.map +0 -1
  99. package/dist/chunk-TEV3OB7N.js.map +0 -1
  100. package/dist/chunk-UBE4WH6G.js.map +0 -1
  101. package/dist/chunk-YXLDR2O4.js.map +0 -1
  102. package/dist/message-schema-B2M7AYzR.d.ts +0 -52
  103. /package/dist/{chunk-DNQNOHD5.js.map → chunk-5C6B6ZRH.js.map} +0 -0
  104. /package/dist/{chunk-E5J4OY4K.js.map → chunk-6BBOCFHV.js.map} +0 -0
  105. /package/dist/{chunk-MGNTXGTX.js.map → chunk-D4PACGPA.js.map} +0 -0
  106. /package/dist/{chunk-YDJA4R37.js.map → chunk-LTXABCOO.js.map} +0 -0
  107. /package/dist/{chunk-BEU6FCTN.js.map → chunk-OJBFDBLG.js.map} +0 -0
  108. /package/dist/{chunk-G5OACQEI.js.map → chunk-QGX73TSQ.js.map} +0 -0
  109. /package/dist/{chunk-H7BI3BSP.js.map → chunk-ZA564323.js.map} +0 -0
@@ -3,40 +3,42 @@ import {
3
3
  parseAgentContinuation,
4
4
  parseAgentInteractionResponse,
5
5
  parseAgentQuestionPrompts
6
- } from "./chunk-BEU6FCTN.js";
6
+ } from "./chunk-OJBFDBLG.js";
7
7
  import {
8
8
  isMcpTool
9
- } from "./chunk-7YAH2F3J.js";
9
+ } from "./chunk-76JLMBVT.js";
10
10
  import {
11
11
  MemoryCompactionConflictError,
12
12
  MemoryCompactionError,
13
13
  assertPositiveInteger,
14
14
  createMemoryCompactionSummary,
15
15
  cumulativeCompactedMessageCount
16
- } from "./chunk-DNQNOHD5.js";
16
+ } from "./chunk-5C6B6ZRH.js";
17
17
  import {
18
18
  startAgentRunObservers
19
- } from "./chunk-E5J4OY4K.js";
19
+ } from "./chunk-6BBOCFHV.js";
20
20
  import {
21
21
  isSkillTool
22
22
  } from "./chunk-YK4WAAS4.js";
23
23
  import {
24
24
  isQuestionTool,
25
25
  isToolIndex
26
- } from "./chunk-JXZTTNCR.js";
26
+ } from "./chunk-SJMSS5SI.js";
27
27
  import {
28
28
  ToolNotFoundError,
29
+ ToolOutput,
29
30
  assertFiniteMinScore,
30
31
  assertPositiveSearchLimit,
31
32
  assertToolApprovalRequirement,
32
33
  createTool,
34
+ normalizeToolResultOutput,
33
35
  parseToolArgs,
34
36
  prepareToolCall,
35
37
  prepareToolCallFromInput,
36
38
  retrieveDocuments,
37
39
  toolMayRequireApproval,
38
40
  toolResultContentToText
39
- } from "./chunk-RZDI7B6Z.js";
41
+ } from "./chunk-EMZKPRE6.js";
40
42
  import {
41
43
  AgentRunBlockedError,
42
44
  AgentRunCancelledError,
@@ -44,36 +46,42 @@ import {
44
46
  AgentStructuredOutputError,
45
47
  AgentToolSuspensionError,
46
48
  MaxTurnsError
47
- } from "./chunk-TEV3OB7N.js";
49
+ } from "./chunk-X4LK4ODN.js";
48
50
  import {
49
51
  mapWithConcurrency
50
52
  } from "./chunk-3RM57ZT2.js";
51
53
  import {
54
+ CompletionStreamAccumulator,
52
55
  Usage,
53
56
  assertCompletionRequestSupported,
57
+ assertCompletionResponseIntegrity,
54
58
  createCompletionRequest,
55
59
  getAssistantGenerationMetadata,
56
60
  isProviderTool,
57
61
  isStreamingCompletionModel,
58
62
  textFromAssistantContent
59
- } from "./chunk-HSA6NL2E.js";
63
+ } from "./chunk-7PHMK7W4.js";
64
+ import {
65
+ assertJsonObject
66
+ } from "./chunk-7JLAIN6E.js";
60
67
  import {
61
68
  parseMessage,
62
69
  parseMessages,
63
70
  toProviderJsonSchema
64
- } from "./chunk-G5OACQEI.js";
71
+ } from "./chunk-QGX73TSQ.js";
65
72
  import {
66
73
  isJsonValue
67
- } from "./chunk-SROQA7I7.js";
74
+ } from "./chunk-3XQGVDU5.js";
68
75
  import {
69
76
  abortError,
77
+ completionProviderOutputErrorUsage,
70
78
  resolveRetryOptions,
71
79
  retryDelayMs,
72
80
  retryErrorAttributes,
73
81
  retryOptionsForFailure,
74
82
  throwIfAborted,
75
83
  waitForRetry
76
- } from "./chunk-UBE4WH6G.js";
84
+ } from "./chunk-3RWESPUG.js";
77
85
  import {
78
86
  appendGuardrailPolicies,
79
87
  hasEnforcedOutputGuardrails,
@@ -828,388 +836,6 @@ function assertPositiveSafeInteger(value, name) {
828
836
  return value;
829
837
  }
830
838
 
831
- // src/internal/agent-runtime/stream-accumulator.ts
832
- var CompletionStreamAccumulator = class {
833
- orderedParts = [];
834
- textParts = /* @__PURE__ */ new Map();
835
- reasoningByKey = /* @__PURE__ */ new Map();
836
- reasoningKeyById = /* @__PURE__ */ new Map();
837
- toolCalls = /* @__PURE__ */ new Map();
838
- sources = /* @__PURE__ */ new Map();
839
- providerToolCalls = /* @__PURE__ */ new Map();
840
- finalResponse;
841
- messageId;
842
- nextTextKey = 0;
843
- nextReasoningKey = 0;
844
- accept(event) {
845
- if (event.type === "text_delta") {
846
- this.appendText(event.delta);
847
- return { type: "text_delta", delta: event.delta };
848
- }
849
- if (event.type === "reasoning_delta") {
850
- const reasoning = this.reasoningStateForEvent(event);
851
- this.appendReasoning(reasoning, event);
852
- return reasoningDeltaEvent(event);
853
- }
854
- if (event.type === "tool_call_delta") {
855
- const toolCall = this.toolCallStateForId(event.id);
856
- if (event.callId !== void 0 && event.callId.length > 0) toolCall.callId = event.callId;
857
- if (event.name !== void 0 && event.name.length > 0) toolCall.name = event.name;
858
- if (event.signature !== void 0) toolCall.signature = event.signature;
859
- if (event.argumentsDelta !== void 0) {
860
- if (event.argumentsMode === "replace") {
861
- toolCall.argumentsText = event.argumentsDelta;
862
- } else {
863
- toolCall.argumentsText += event.argumentsDelta;
864
- }
865
- }
866
- return void 0;
867
- }
868
- if (event.type === "tool_call") {
869
- this.upsertToolCall(event.toolCall);
870
- return { type: "tool_call", toolCall: event.toolCall };
871
- }
872
- if (event.type === "source") {
873
- this.sources.set(sourceKey(event.source), event.source);
874
- return { type: "source", source: event.source };
875
- }
876
- if (event.type === "provider_tool_call") {
877
- this.providerToolCalls.set(event.toolCall.id, event.toolCall);
878
- return { type: "provider_tool_call", toolCall: event.toolCall };
879
- }
880
- if (event.type === "message_id") {
881
- this.messageId = event.id;
882
- return void 0;
883
- }
884
- if (event.type === "final") {
885
- this.finalResponse = event.response;
886
- return void 0;
887
- }
888
- return void 0;
889
- }
890
- response() {
891
- const accumulatedResponse = this.buildAccumulatedResponse();
892
- if (this.finalResponse !== void 0) {
893
- if (accumulatedResponse.choice.length === 0) {
894
- return this.withAccumulatedArtifacts(this.finalResponse, accumulatedResponse);
895
- }
896
- return this.mergeFinalResponse(accumulatedResponse, this.finalResponse);
897
- }
898
- return accumulatedResponse;
899
- }
900
- buildAccumulatedResponse() {
901
- const choice = [];
902
- for (const part of this.orderedParts) {
903
- if (part.type === "text") {
904
- const text = this.textParts.get(part.key) ?? "";
905
- if (text.length > 0) {
906
- choice.push({ type: "text", text });
907
- }
908
- continue;
909
- }
910
- if (part.type === "reasoning") {
911
- const reasoning = this.reasoningByKey.get(part.key);
912
- if (reasoning !== void 0) {
913
- choice.push(reasoningContent(reasoning));
914
- }
915
- continue;
916
- }
917
- const toolCall = this.toolCalls.get(part.key);
918
- if (toolCall !== void 0) {
919
- choice.push(
920
- toolCallContent(toolCall, matchingFinalToolCall(toolCall, this.finalResponse?.choice))
921
- );
922
- }
923
- }
924
- const response = {
925
- choice,
926
- usage: Usage.empty(),
927
- rawResponse: void 0
928
- };
929
- if (this.messageId !== void 0) {
930
- response.messageId = this.messageId;
931
- }
932
- const sources = [...this.sources.values()];
933
- if (sources.length > 0) {
934
- response.sources = sources;
935
- }
936
- const providerToolCalls = [...this.providerToolCalls.values()];
937
- if (providerToolCalls.length > 0) {
938
- response.providerToolCalls = providerToolCalls;
939
- }
940
- return response;
941
- }
942
- upsertToolCall(toolCall) {
943
- if (!this.toolCalls.has(toolCall.toolCallId)) {
944
- this.orderedParts.push({ type: "tool_call", key: toolCall.toolCallId });
945
- }
946
- const partial = {
947
- id: toolCall.toolCallId,
948
- name: toolCall.toolName,
949
- argumentsText: JSON.stringify(toolCall.input ?? {})
950
- };
951
- if (toolCall.callId !== void 0) {
952
- partial.callId = toolCall.callId;
953
- }
954
- if (toolCall.signature !== void 0) {
955
- partial.signature = toolCall.signature;
956
- }
957
- this.toolCalls.set(toolCall.toolCallId, partial);
958
- }
959
- mergeFinalResponse(accumulatedResponse, finalResponse) {
960
- if (finalResponse.choice.length === 0) {
961
- const mergedResponse = {
962
- ...accumulatedResponse,
963
- usage: finalResponse.usage,
964
- rawResponse: finalResponse.rawResponse
965
- };
966
- if (finalResponse.messageId !== void 0) {
967
- mergedResponse.messageId = finalResponse.messageId;
968
- }
969
- return this.withAccumulatedArtifacts(mergedResponse, accumulatedResponse);
970
- }
971
- const finalById = /* @__PURE__ */ new Map();
972
- const finalByCallId = /* @__PURE__ */ new Map();
973
- for (const content of finalResponse.choice) {
974
- if (content.type !== "tool-call") {
975
- continue;
976
- }
977
- finalById.set(content.toolCallId, content);
978
- if (content.callId !== void 0) {
979
- finalByCallId.set(content.callId, content);
980
- }
981
- }
982
- const matchedFinalToolCalls = /* @__PURE__ */ new Set();
983
- const choice = accumulatedResponse.choice.map((content) => {
984
- if (content.type !== "tool-call") {
985
- return content;
986
- }
987
- const finalToolCall = finalById.get(content.toolCallId) ?? (content.callId === void 0 ? void 0 : finalByCallId.get(content.callId));
988
- if (finalToolCall === void 0) {
989
- return content;
990
- }
991
- matchedFinalToolCalls.add(finalToolCall);
992
- return mergeFinalToolCall(content, finalToolCall);
993
- });
994
- for (const content of finalResponse.choice) {
995
- if (content.type !== "tool-call") {
996
- continue;
997
- }
998
- if (!matchedFinalToolCalls.has(content)) {
999
- choice.push(content);
1000
- }
1001
- }
1002
- return this.withAccumulatedArtifacts({ ...finalResponse, choice }, accumulatedResponse);
1003
- }
1004
- appendText(delta) {
1005
- const lastPart = this.orderedParts.at(-1);
1006
- const key = lastPart?.type === "text" ? lastPart.key : this.createTextKey();
1007
- if (lastPart?.type !== "text") {
1008
- this.orderedParts.push({ type: "text", key });
1009
- }
1010
- this.textParts.set(key, `${this.textParts.get(key) ?? ""}${delta}`);
1011
- }
1012
- reasoningStateForEvent(event) {
1013
- if (event.id !== void 0) {
1014
- const existingKey = this.reasoningKeyById.get(event.id);
1015
- if (existingKey !== void 0) {
1016
- const existing = this.reasoningByKey.get(existingKey);
1017
- if (existing !== void 0) {
1018
- return existing;
1019
- }
1020
- }
1021
- const key2 = this.createReasoningKey();
1022
- const reasoning2 = { id: event.id, text: "" };
1023
- this.reasoningKeyById.set(event.id, key2);
1024
- this.reasoningByKey.set(key2, reasoning2);
1025
- this.orderedParts.push({ type: "reasoning", key: key2 });
1026
- return reasoning2;
1027
- }
1028
- const lastPart = this.orderedParts.at(-1);
1029
- if (lastPart?.type === "reasoning") {
1030
- const lastReasoning = this.reasoningByKey.get(lastPart.key);
1031
- if (lastReasoning !== void 0 && lastReasoning.id === void 0) {
1032
- return lastReasoning;
1033
- }
1034
- }
1035
- const key = this.createReasoningKey();
1036
- const reasoning = { text: "" };
1037
- this.reasoningByKey.set(key, reasoning);
1038
- this.orderedParts.push({ type: "reasoning", key });
1039
- return reasoning;
1040
- }
1041
- toolCallStateForId(id) {
1042
- const existing = this.toolCalls.get(id);
1043
- if (existing !== void 0) {
1044
- return existing;
1045
- }
1046
- const toolCall = {
1047
- id,
1048
- name: "",
1049
- argumentsText: ""
1050
- };
1051
- this.toolCalls.set(id, toolCall);
1052
- this.orderedParts.push({ type: "tool_call", key: id });
1053
- return toolCall;
1054
- }
1055
- withMessageIdFallback(response, accumulatedResponse) {
1056
- if (response.messageId !== void 0 || accumulatedResponse.messageId === void 0) {
1057
- return response;
1058
- }
1059
- return { ...response, messageId: accumulatedResponse.messageId };
1060
- }
1061
- withAccumulatedArtifacts(response, accumulatedResponse) {
1062
- const withMessageId = this.withMessageIdFallback(response, accumulatedResponse);
1063
- const sources = mergeSources(accumulatedResponse.sources, response.sources);
1064
- const providerToolCalls = mergeProviderToolCalls(
1065
- accumulatedResponse.providerToolCalls,
1066
- response.providerToolCalls
1067
- );
1068
- let accumulated = { ...withMessageId };
1069
- if (sources.length > 0) accumulated = { ...accumulated, sources };
1070
- if (providerToolCalls.length > 0) {
1071
- accumulated = { ...accumulated, providerToolCalls };
1072
- }
1073
- return accumulated;
1074
- }
1075
- createTextKey() {
1076
- this.nextTextKey += 1;
1077
- return `text_${this.nextTextKey.toString()}`;
1078
- }
1079
- createReasoningKey() {
1080
- this.nextReasoningKey += 1;
1081
- return `reasoning_${this.nextReasoningKey.toString()}`;
1082
- }
1083
- appendReasoning(reasoning, event) {
1084
- const contentType = event.contentType ?? "text";
1085
- if (contentType === "text" || contentType === "summary") {
1086
- reasoning.text += event.delta;
1087
- }
1088
- if (event.contentType === void 0 && event.signature === void 0) {
1089
- return;
1090
- }
1091
- reasoning.details ??= [];
1092
- const last = reasoning.details.at(-1);
1093
- if (contentType === "text") {
1094
- if (last?.type === "text") {
1095
- let detail = {
1096
- ...last,
1097
- text: `${last.text}${event.delta}`
1098
- };
1099
- if (event.signature !== void 0) detail = { ...detail, signature: event.signature };
1100
- reasoning.details[reasoning.details.length - 1] = detail;
1101
- } else {
1102
- reasoning.details.push(
1103
- event.signature === void 0 ? { type: "text", text: event.delta } : { type: "text", text: event.delta, signature: event.signature }
1104
- );
1105
- }
1106
- return;
1107
- }
1108
- if (contentType === "summary") {
1109
- if (last?.type === "summary") {
1110
- reasoning.details[reasoning.details.length - 1] = {
1111
- ...last,
1112
- text: `${last.text}${event.delta}`
1113
- };
1114
- } else {
1115
- reasoning.details.push({ type: "summary", text: event.delta });
1116
- }
1117
- return;
1118
- }
1119
- if (contentType === "encrypted") {
1120
- reasoning.details.push({ type: "encrypted", data: event.delta });
1121
- return;
1122
- }
1123
- reasoning.details.push({ type: "redacted", data: event.delta });
1124
- }
1125
- };
1126
- function sourceKey(source) {
1127
- return `${source.url}\0${source.startIndex ?? ""}\0${source.endIndex ?? ""}`;
1128
- }
1129
- function mergeSources(accumulated, final) {
1130
- const sources = /* @__PURE__ */ new Map();
1131
- for (const source of [...accumulated ?? [], ...final ?? []]) {
1132
- sources.set(sourceKey(source), source);
1133
- }
1134
- return [...sources.values()];
1135
- }
1136
- function mergeProviderToolCalls(accumulated, final) {
1137
- const toolCalls = /* @__PURE__ */ new Map();
1138
- for (const toolCall of [...accumulated ?? [], ...final ?? []]) {
1139
- toolCalls.set(toolCall.id, toolCall);
1140
- }
1141
- return [...toolCalls.values()];
1142
- }
1143
- function reasoningContent(reasoning) {
1144
- const content = reasoning.details === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, details: reasoning.details };
1145
- return reasoning.id === void 0 ? content : { ...content, id: reasoning.id };
1146
- }
1147
- function toolCallContent(toolCall, finalToolCall) {
1148
- const argumentsValue = finalToolCall !== void 0 && !isEmptyToolArguments(finalToolCall.input) ? finalToolCall.input : parseToolArguments(toolCall.id, toolCall.argumentsText);
1149
- let content = {
1150
- type: "tool-call",
1151
- toolCallId: toolCall.id,
1152
- toolName: toolCall.name,
1153
- input: argumentsValue
1154
- };
1155
- if (toolCall.callId !== void 0) content = { ...content, callId: toolCall.callId };
1156
- if (toolCall.signature !== void 0) content = { ...content, signature: toolCall.signature };
1157
- return content;
1158
- }
1159
- function matchingFinalToolCall(accumulated, finalChoice) {
1160
- const byId = finalChoice?.find(
1161
- (content) => content.type === "tool-call" && content.toolCallId === accumulated.id
1162
- );
1163
- if (byId !== void 0 || accumulated.callId === void 0) {
1164
- return byId;
1165
- }
1166
- return finalChoice?.find(
1167
- (content) => content.type === "tool-call" && content.callId === accumulated.callId
1168
- );
1169
- }
1170
- function mergeFinalToolCall(accumulated, finalToolCall) {
1171
- const input = isEmptyToolArguments(finalToolCall.input) ? accumulated.input : finalToolCall.input;
1172
- return {
1173
- ...accumulated,
1174
- ...finalToolCall,
1175
- input
1176
- };
1177
- }
1178
- function reasoningDeltaEvent(event) {
1179
- const mapped = { type: "reasoning_delta", delta: event.delta };
1180
- if (event.id !== void 0) mapped.id = event.id;
1181
- if (event.contentType !== void 0) mapped.contentType = event.contentType;
1182
- if (event.signature !== void 0) mapped.signature = event.signature;
1183
- return mapped;
1184
- }
1185
- function isEmptyToolArguments(value) {
1186
- if (value === void 0 || value === null) {
1187
- return true;
1188
- }
1189
- if (typeof value === "string") {
1190
- return value.trim().length === 0;
1191
- }
1192
- if (Array.isArray(value)) {
1193
- return value.length === 0;
1194
- }
1195
- if (typeof value === "object") {
1196
- return Object.values(value).every((item) => item === void 0);
1197
- }
1198
- return false;
1199
- }
1200
- function parseToolArguments(toolCallId, text) {
1201
- if (text.trim().length === 0) {
1202
- return {};
1203
- }
1204
- try {
1205
- return JSON.parse(text);
1206
- } catch {
1207
- throw new Error(
1208
- `Completion returned tool call "${toolCallId}" with malformed JSON arguments; this indicates invalid provider output or incomplete stream assembly.`
1209
- );
1210
- }
1211
- }
1212
-
1213
839
  // src/internal/agent-runtime/stream-events.ts
1214
840
  function addTurn(turn, event) {
1215
841
  if (event.type === "text_delta") {
@@ -1254,6 +880,21 @@ var UNLABELED_FENCE_START = "```\n";
1254
880
  var UNLABELED_FENCE_CRLF_START = "```\r\n";
1255
881
  var FENCE_END = "\n```";
1256
882
  var STRUCTURED_OUTPUT_RETRY_PROMPT = "Your previous response was invalid structured output. Return only raw JSON that matches the supplied JSON schema. Do not use Markdown fences or include commentary.";
883
+ var STRUCTURED_OUTPUT_TRUNCATED_RETRY_PROMPT = "Your previous response exceeded the provider output limit. Return substantially shorter raw JSON that matches the supplied JSON schema. Do not use Markdown fences or include commentary.";
884
+ var STRUCTURED_OUTPUT_REPAIR_PREVIEW_MAX_LENGTH = 8192;
885
+ var STRUCTURED_OUTPUT_REPAIR_PREVIEW_MARKER = "\n...[output omitted]...\n";
886
+ function structuredOutputRepairPreview(text) {
887
+ if (text.length <= STRUCTURED_OUTPUT_REPAIR_PREVIEW_MAX_LENGTH) {
888
+ return { text, includedOutputLength: text.length };
889
+ }
890
+ const availableLength = STRUCTURED_OUTPUT_REPAIR_PREVIEW_MAX_LENGTH - STRUCTURED_OUTPUT_REPAIR_PREVIEW_MARKER.length;
891
+ const headLength = Math.ceil(availableLength / 2);
892
+ const tailLength = availableLength - headLength;
893
+ return {
894
+ text: `${text.slice(0, headLength)}${STRUCTURED_OUTPUT_REPAIR_PREVIEW_MARKER}${text.slice(-tailLength)}`,
895
+ includedOutputLength: headLength + tailLength
896
+ };
897
+ }
1257
898
  function normalizeStructuredOutput(text) {
1258
899
  const trimmed = text.trim();
1259
900
  const opening = fenceOpening(trimmed);
@@ -1307,13 +948,13 @@ var ToolCallExecutor = class {
1307
948
  abortSignal;
1308
949
  cancel;
1309
950
  async execute(toolCalls, onResult, onStreamEvent, observation) {
1310
- for (const toolCall of toolCalls) {
1311
- if (toolCall.toolName.length === 0) {
1312
- throw new Error(
1313
- `Completion returned tool call "${toolCall.toolCallId}" with an empty tool name; this indicates invalid provider output or provider mapping.`
1314
- );
951
+ assertCompletionResponseIntegrity({
952
+ response: {
953
+ choice: [...toolCalls],
954
+ usage: Usage.empty(),
955
+ rawResponse: void 0
1315
956
  }
1316
- }
957
+ });
1317
958
  const executeOne = async (toolCall) => {
1318
959
  throwIfAborted(this.abortSignal);
1319
960
  const args = JSON.stringify(toolCall.input);
@@ -1932,7 +1573,14 @@ var ToolCallExecutor = class {
1932
1573
  args: current
1933
1574
  });
1934
1575
  if (replacement?.args !== void 0) {
1935
- current = typeof replacement.args === "string" ? replacement.args : JSON.stringify(replacement.args);
1576
+ if (typeof replacement.args === "string") {
1577
+ current = replacement.args;
1578
+ } else {
1579
+ if (!isJsonValue(replacement.args)) {
1580
+ throw new TypeError("Tool input middleware args must be a strict JSON value.");
1581
+ }
1582
+ current = JSON.stringify(replacement.args);
1583
+ }
1936
1584
  }
1937
1585
  }
1938
1586
  return current;
@@ -2024,20 +1672,51 @@ function createApprovalContext(input, hookArgs, agent, run) {
2024
1672
  return context;
2025
1673
  }
2026
1674
  function approvalEventAttributes(request, turn) {
2027
- return {
1675
+ const attributes = {
2028
1676
  turn,
2029
1677
  approvalId: request.id,
2030
1678
  toolName: request.toolName,
2031
- toolCallId: request.toolCallId,
2032
- internalCallId: request.internalCallId,
2033
- reason: request.reason
1679
+ internalCallId: request.internalCallId
2034
1680
  };
1681
+ if (request.toolCallId !== void 0) attributes.toolCallId = request.toolCallId;
1682
+ if (request.reason !== void 0) attributes.reason = request.reason;
1683
+ return attributes;
2035
1684
  }
2036
1685
  function normalizeToolOutputMiddlewareResult(result) {
2037
1686
  if (typeof result === "string") {
2038
1687
  return { result };
2039
1688
  }
2040
- return result ?? {};
1689
+ if (typeof result !== "object" || result === null) {
1690
+ throw new TypeError("Tool output middleware must return text or structured content.");
1691
+ }
1692
+ const prototype = Object.getPrototypeOf(result);
1693
+ if (prototype !== Object.prototype && prototype !== null) {
1694
+ throw new TypeError("Tool output middleware must return text or structured content.");
1695
+ }
1696
+ const keys = Reflect.ownKeys(result);
1697
+ const hasResult = keys.includes("result");
1698
+ const hasStructuredResult = keys.includes("structuredResult");
1699
+ if (hasResult === hasStructuredResult || keys.some((key) => key !== "result" && key !== "structuredResult")) {
1700
+ throw new TypeError(
1701
+ "Tool output middleware must return exactly one of result or structuredResult."
1702
+ );
1703
+ }
1704
+ if (hasResult) {
1705
+ const descriptor2 = Object.getOwnPropertyDescriptor(result, "result");
1706
+ if (descriptor2 === void 0 || !("value" in descriptor2) || typeof descriptor2.value !== "string") {
1707
+ throw new TypeError("Tool output middleware result must be a string.");
1708
+ }
1709
+ return { result: descriptor2.value };
1710
+ }
1711
+ const descriptor = Object.getOwnPropertyDescriptor(result, "structuredResult");
1712
+ if (descriptor === void 0 || !("value" in descriptor)) {
1713
+ throw new TypeError("Tool output middleware structuredResult must be valid tool content.");
1714
+ }
1715
+ const normalized = normalizeToolResultOutput(ToolOutput.content(descriptor.value));
1716
+ if (normalized.type !== "content") {
1717
+ throw new TypeError("Tool output middleware structuredResult must be valid tool content.");
1718
+ }
1719
+ return { structuredResult: normalized.value };
2041
1720
  }
2042
1721
  function toolTraceMetadata(tool) {
2043
1722
  if (tool === void 0) {
@@ -2317,13 +1996,19 @@ var AgentRun = class _AgentRun {
2317
1996
  pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
2318
1997
  await this.runRunStartHook(newMessages);
2319
1998
  if (this.continuationState !== void 0) {
1999
+ const interactionAttributes = {};
2000
+ if (this.resumedFrom?.runId !== void 0) {
2001
+ interactionAttributes.sourceRunId = this.resumedFrom.runId;
2002
+ }
2003
+ if (this.resumedFrom?.interactionId !== void 0) {
2004
+ interactionAttributes.interactionId = this.resumedFrom.interactionId;
2005
+ }
2006
+ if (this.interactionResponse?.type !== void 0) {
2007
+ interactionAttributes.interactionType = this.interactionResponse.type;
2008
+ }
2320
2009
  await runObservers.event({
2321
2010
  name: "agent.interaction_response",
2322
- attributes: {
2323
- sourceRunId: this.resumedFrom?.runId,
2324
- interactionId: this.resumedFrom?.interactionId,
2325
- interactionType: this.interactionResponse?.type
2326
- }
2011
+ attributes: interactionAttributes
2327
2012
  });
2328
2013
  try {
2329
2014
  const toolResults = await this.resolveContinuationTools(
@@ -2378,6 +2063,13 @@ var AgentRun = class _AgentRun {
2378
2063
  throw error;
2379
2064
  }
2380
2065
  response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
2066
+ try {
2067
+ assertCompletionResponseIntegrity({ response });
2068
+ } catch (error) {
2069
+ const providerOutputUsage = completionProviderOutputErrorUsage(error);
2070
+ if (providerOutputUsage !== void 0) usage = Usage.add(usage, providerOutputUsage);
2071
+ throw error;
2072
+ }
2381
2073
  usage = Usage.add(usage, response.usage);
2382
2074
  this.updateRunProgress(newMessages);
2383
2075
  await this.runCompletionResponseHook(prompt, response, newMessages);
@@ -2729,6 +2421,13 @@ var AgentRun = class _AgentRun {
2729
2421
  this.activeGeneration = void 0;
2730
2422
  await generationObservers.end(generationEndArgs);
2731
2423
  response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
2424
+ try {
2425
+ assertCompletionResponseIntegrity({ response });
2426
+ } catch (error) {
2427
+ const providerOutputUsage = completionProviderOutputErrorUsage(error);
2428
+ if (providerOutputUsage !== void 0) usage = Usage.add(usage, providerOutputUsage);
2429
+ throw error;
2430
+ }
2732
2431
  usage = Usage.add(usage, response.usage);
2733
2432
  this.updateRunProgress(newMessages);
2734
2433
  await this.runCompletionResponseHook(prompt, response, newMessages);
@@ -2906,14 +2605,20 @@ var AgentRun = class _AgentRun {
2906
2605
  this.generationStartArgs(turn, request, providerRequest)
2907
2606
  );
2908
2607
  let currentRequest = request;
2909
- let structuredRetryUsage = Usage.empty();
2608
+ let failedAttemptUsage = Usage.empty();
2910
2609
  try {
2911
2610
  for (let attempt = 1; ; attempt += 1) {
2912
2611
  let response;
2913
2612
  try {
2914
2613
  this.throwIfCancelled();
2915
2614
  response = await this.agent.model.completion(currentRequest, this.modelCallOptions());
2615
+ assertCompletionResponseIntegrity({ response });
2916
2616
  } catch (error) {
2617
+ const attemptUsage = completionProviderOutputErrorUsage(error);
2618
+ if (attemptUsage !== void 0) {
2619
+ failedAttemptUsage = Usage.add(failedAttemptUsage, attemptUsage);
2620
+ this.failedCompletionUsage = failedAttemptUsage;
2621
+ }
2917
2622
  const retryOptions = this.retryOptionsForFailure(error, attempt, turn, false);
2918
2623
  if (retryOptions === void 0) {
2919
2624
  throw error;
@@ -2924,11 +2629,12 @@ var AgentRun = class _AgentRun {
2924
2629
  turn,
2925
2630
  false,
2926
2631
  retryOptions,
2927
- runObservers
2632
+ runObservers,
2633
+ providerOutputRetryEventAttributes(error, failedAttemptUsage)
2928
2634
  );
2929
2635
  continue;
2930
2636
  }
2931
- const cumulativeUsage = Usage.add(structuredRetryUsage, response.usage);
2637
+ const cumulativeUsage = Usage.add(failedAttemptUsage, response.usage);
2932
2638
  try {
2933
2639
  this.validateStructuredResponse(response, attempt, cumulativeUsage);
2934
2640
  } catch (error) {
@@ -2936,20 +2642,22 @@ var AgentRun = class _AgentRun {
2936
2642
  if (retryOptions === void 0) {
2937
2643
  throw error;
2938
2644
  }
2939
- structuredRetryUsage = cumulativeUsage;
2940
- this.failedCompletionUsage = structuredRetryUsage;
2941
- currentRequest = structuredOutputRetryRequest(currentRequest, response);
2645
+ failedAttemptUsage = cumulativeUsage;
2646
+ this.failedCompletionUsage = failedAttemptUsage;
2647
+ const retryRequest = structuredOutputRetryRequest(request, response, error);
2648
+ currentRequest = retryRequest.request;
2942
2649
  await this.scheduleCompletionRetry(
2943
2650
  error,
2944
2651
  attempt,
2945
2652
  turn,
2946
2653
  false,
2947
2654
  retryOptions,
2948
- runObservers
2655
+ runObservers,
2656
+ structuredOutputRetryEventAttributes(error, retryRequest)
2949
2657
  );
2950
2658
  continue;
2951
2659
  }
2952
- const finalResponse = structuredRetryUsage.totalTokens === 0 ? response : { ...response, usage: cumulativeUsage };
2660
+ const finalResponse = Usage.isEmpty(failedAttemptUsage) ? response : { ...response, usage: cumulativeUsage };
2953
2661
  await generationObservers.end({ turn, response: finalResponse });
2954
2662
  this.failedCompletionUsage = Usage.empty();
2955
2663
  return finalResponse;
@@ -2986,20 +2694,28 @@ var AgentRun = class _AgentRun {
2986
2694
  for (let attempt = 1; ; attempt += 1) {
2987
2695
  const accumulator = new CompletionStreamAccumulator();
2988
2696
  let hasProviderProgress = false;
2697
+ let recordedErrorUsage = false;
2698
+ let attemptErrorUsage;
2989
2699
  try {
2990
2700
  this.throwIfCancelled();
2991
2701
  for await (const event of model.streamCompletion(currentRequest, this.modelCallOptions())) {
2992
2702
  if (event.type === "error") {
2993
- if (event.usage !== void 0) {
2994
- args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, event.usage);
2703
+ const eventUsage = event.usage ?? completionProviderOutputErrorUsage(event.error);
2704
+ if (eventUsage !== void 0) {
2705
+ args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, eventUsage);
2706
+ recordedErrorUsage = true;
2707
+ attemptErrorUsage = eventUsage;
2995
2708
  }
2996
2709
  throw event.error;
2997
2710
  }
2998
- hasProviderProgress = true;
2711
+ const mapped = accumulator.accept(event);
2712
+ if (event.type === "final") break;
2713
+ if (event.type === "tool_call_delta" || mapped !== void 0) {
2714
+ hasProviderProgress = true;
2715
+ }
2999
2716
  if (args.state.firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
3000
2717
  args.state.firstDeltaMs = Date.now() - args.generationStartedAt;
3001
2718
  }
3002
- const mapped = accumulator.accept(event);
3003
2719
  if (event.type === "tool_call_delta") {
3004
2720
  yield addTurnToToolCallDelta(args.turn, event);
3005
2721
  }
@@ -3015,6 +2731,7 @@ var AgentRun = class _AgentRun {
3015
2731
  }
3016
2732
  }
3017
2733
  const response = accumulator.response();
2734
+ assertCompletionResponseIntegrity({ response });
3018
2735
  const cumulativeStructuredUsage = Usage.add(args.state.providerErrorUsage, response.usage);
3019
2736
  try {
3020
2737
  this.validateStructuredResponse(response, attempt, cumulativeStructuredUsage);
@@ -3029,21 +2746,30 @@ var AgentRun = class _AgentRun {
3029
2746
  args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, response.usage);
3030
2747
  args.state.firstDeltaMs = void 0;
3031
2748
  args.state.emittedToolCallIds.clear();
3032
- currentRequest = structuredOutputRetryRequest(currentRequest, response);
2749
+ const retryRequest = structuredOutputRetryRequest(args.request, response, error);
2750
+ currentRequest = retryRequest.request;
3033
2751
  await this.scheduleCompletionRetry(
3034
2752
  error,
3035
2753
  attempt,
3036
2754
  args.turn,
3037
2755
  true,
3038
2756
  retryOptions,
3039
- args.runObservers
2757
+ args.runObservers,
2758
+ structuredOutputRetryEventAttributes(error, retryRequest)
3040
2759
  );
3041
2760
  continue;
3042
2761
  }
3043
- args.state.response = args.state.providerErrorUsage.totalTokens === 0 ? response : { ...response, usage: cumulativeStructuredUsage };
2762
+ args.state.response = Usage.isEmpty(args.state.providerErrorUsage) ? response : { ...response, usage: cumulativeStructuredUsage };
3044
2763
  args.state.providerErrorUsage = Usage.empty();
3045
2764
  return;
3046
2765
  } catch (error) {
2766
+ if (!recordedErrorUsage) {
2767
+ const attemptUsage = completionProviderOutputErrorUsage(error);
2768
+ if (attemptUsage !== void 0) {
2769
+ args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, attemptUsage);
2770
+ attemptErrorUsage = attemptUsage;
2771
+ }
2772
+ }
3047
2773
  const retryOptions = hasProviderProgress ? void 0 : this.retryOptionsForFailure(error, attempt, args.turn, true);
3048
2774
  if (retryOptions === void 0) {
3049
2775
  throw error;
@@ -3054,7 +2780,12 @@ var AgentRun = class _AgentRun {
3054
2780
  args.turn,
3055
2781
  true,
3056
2782
  retryOptions,
3057
- args.runObservers
2783
+ args.runObservers,
2784
+ providerOutputRetryEventAttributes(
2785
+ error,
2786
+ args.state.providerErrorUsage,
2787
+ attemptErrorUsage
2788
+ )
3058
2789
  );
3059
2790
  }
3060
2791
  }
@@ -3180,6 +2911,10 @@ var AgentRun = class _AgentRun {
3180
2911
  modelId: this.agent.model.modelId,
3181
2912
  usage: { ...response.usage }
3182
2913
  };
2914
+ if (response.finishReason !== void 0) generation.finishReason = response.finishReason;
2915
+ if (response.providerFinishReason !== void 0) {
2916
+ generation.providerFinishReason = response.providerFinishReason;
2917
+ }
3183
2918
  if (response.contextUsage !== void 0) generation.contextUsage = response.contextUsage;
3184
2919
  if (response.sources !== void 0) generation.sources = response.sources;
3185
2920
  if (response.providerToolCalls !== void 0) {
@@ -3225,20 +2960,24 @@ var AgentRun = class _AgentRun {
3225
2960
  streaming
3226
2961
  });
3227
2962
  }
3228
- async scheduleCompletionRetry(error, attempt, turn, streaming, options, runObservers) {
2963
+ async scheduleCompletionRetry(error, attempt, turn, streaming, options, runObservers, additionalAttributes) {
3229
2964
  const delayMs = retryDelayMs(options, attempt);
2965
+ const attributes = {
2966
+ turn,
2967
+ attempt,
2968
+ nextAttempt: attempt + 1,
2969
+ maxAttempts: options.maxAttempts,
2970
+ delayMs,
2971
+ streaming,
2972
+ ...retryErrorAttributes(error)
2973
+ };
2974
+ if (additionalAttributes !== void 0) {
2975
+ Object.assign(attributes, additionalAttributes);
2976
+ }
3230
2977
  await runObservers.event({
3231
2978
  name: "completion.retry",
3232
2979
  level: "WARNING",
3233
- attributes: {
3234
- turn,
3235
- attempt,
3236
- nextAttempt: attempt + 1,
3237
- maxAttempts: options.maxAttempts,
3238
- delayMs,
3239
- streaming,
3240
- ...retryErrorAttributes(error)
3241
- }
2980
+ attributes
3242
2981
  });
3243
2982
  await waitForRetry(delayMs, this.abortController.signal);
3244
2983
  }
@@ -3476,7 +3215,41 @@ var AgentRun = class _AgentRun {
3476
3215
  if (this.agent.outputSchema === void 0) return;
3477
3216
  if (response.choice.some((item) => item.type === "tool-call")) return;
3478
3217
  const text = textFromAssistantContent(response.choice);
3479
- const output = this.parseOutput(text, attempt, usage, { useValidatedOutput: false });
3218
+ const normalized = normalizeStructuredOutput(text);
3219
+ if (response.finishReason === "content-filter") {
3220
+ throw new AgentStructuredOutputError({
3221
+ phase: "content-filter",
3222
+ attempt,
3223
+ maxAttempts: this.completionRetryOptions?.maxAttempts ?? 1,
3224
+ outputLength: text.length,
3225
+ normalizedLength: normalized.text.length,
3226
+ outputFormat: normalized.format,
3227
+ attemptUsage: response.usage,
3228
+ usage,
3229
+ finishReason: response.finishReason,
3230
+ providerFinishReason: response.providerFinishReason
3231
+ });
3232
+ }
3233
+ if (response.finishReason === "length") {
3234
+ throw new AgentStructuredOutputError({
3235
+ phase: "truncated",
3236
+ attempt,
3237
+ maxAttempts: this.completionRetryOptions?.maxAttempts ?? 1,
3238
+ outputLength: text.length,
3239
+ normalizedLength: normalized.text.length,
3240
+ outputFormat: normalized.format,
3241
+ attemptUsage: response.usage,
3242
+ usage,
3243
+ finishReason: response.finishReason,
3244
+ providerFinishReason: response.providerFinishReason
3245
+ });
3246
+ }
3247
+ const output = this.parseOutput(text, attempt, usage, {
3248
+ useValidatedOutput: false,
3249
+ attemptUsage: response.usage,
3250
+ finishReason: response.finishReason,
3251
+ providerFinishReason: response.providerFinishReason
3252
+ });
3480
3253
  this.validatedStructuredOutput = { text, output };
3481
3254
  }
3482
3255
  takeFailedCompletionUsage() {
@@ -3495,6 +3268,9 @@ var AgentRun = class _AgentRun {
3495
3268
  let json;
3496
3269
  try {
3497
3270
  json = JSON.parse(normalized.text);
3271
+ if (!isJsonValue(json)) {
3272
+ throw new TypeError("Agent structured output is not a JSON value.");
3273
+ }
3498
3274
  } catch (error) {
3499
3275
  throw new AgentStructuredOutputError({
3500
3276
  phase: "parse",
@@ -3503,7 +3279,10 @@ var AgentRun = class _AgentRun {
3503
3279
  outputLength: text.length,
3504
3280
  normalizedLength: normalized.text.length,
3505
3281
  outputFormat: normalized.format,
3282
+ attemptUsage: options.attemptUsage ?? usage,
3506
3283
  usage,
3284
+ finishReason: options.finishReason,
3285
+ providerFinishReason: options.providerFinishReason,
3507
3286
  cause: error
3508
3287
  });
3509
3288
  }
@@ -3517,7 +3296,10 @@ var AgentRun = class _AgentRun {
3517
3296
  outputLength: text.length,
3518
3297
  normalizedLength: normalized.text.length,
3519
3298
  outputFormat: normalized.format,
3299
+ attemptUsage: options.attemptUsage ?? usage,
3520
3300
  usage,
3301
+ finishReason: options.finishReason,
3302
+ providerFinishReason: options.providerFinishReason,
3521
3303
  cause: error
3522
3304
  });
3523
3305
  }
@@ -3667,6 +3449,9 @@ var AgentRun = class _AgentRun {
3667
3449
  });
3668
3450
  if (replacement?.request !== void 0) {
3669
3451
  current = replacement.request;
3452
+ if (current.providerOptions !== void 0) {
3453
+ assertJsonObject(current.providerOptions, "providerOptions");
3454
+ }
3670
3455
  }
3671
3456
  }
3672
3457
  return current;
@@ -3951,14 +3736,14 @@ function responseStreamEvents(turn, response, includeProviderArtifacts = true) {
3951
3736
  if (item.type === "reasoning") {
3952
3737
  if (item.details === void 0) {
3953
3738
  if (item.text.length > 0) {
3954
- events.push(reasoningDeltaEvent2(turn, item.text, { id: item.id }));
3739
+ events.push(reasoningDeltaEvent(turn, item.text, { id: item.id }));
3955
3740
  }
3956
3741
  continue;
3957
3742
  }
3958
3743
  for (const content of item.details) {
3959
3744
  const delta = content.type === "encrypted" || content.type === "redacted" ? content.data : content.text;
3960
3745
  events.push(
3961
- reasoningDeltaEvent2(turn, delta, {
3746
+ reasoningDeltaEvent(turn, delta, {
3962
3747
  id: item.id,
3963
3748
  contentType: content.type,
3964
3749
  signature: content.type === "text" ? content.signature : void 0
@@ -3984,10 +3769,14 @@ function responseStreamEvents(turn, response, includeProviderArtifacts = true) {
3984
3769
  function generationArtifacts(messages) {
3985
3770
  const sources = /* @__PURE__ */ new Map();
3986
3771
  const providerToolCalls = /* @__PURE__ */ new Map();
3772
+ let finishReason;
3773
+ let providerFinishReason;
3987
3774
  let contextUsage;
3988
3775
  for (const message of messages) {
3989
3776
  const metadata = getAssistantGenerationMetadata(message);
3990
3777
  if (metadata !== void 0) {
3778
+ finishReason = metadata.finishReason;
3779
+ providerFinishReason = metadata.providerFinishReason;
3991
3780
  contextUsage = metadata.contextUsage;
3992
3781
  }
3993
3782
  for (const source of metadata?.sources ?? []) {
@@ -4003,10 +3792,14 @@ function generationArtifacts(messages) {
4003
3792
  if (providerToolCalls.size > 0) {
4004
3793
  artifacts.providerToolCalls = [...providerToolCalls.values()];
4005
3794
  }
3795
+ if (finishReason !== void 0) artifacts.finishReason = finishReason;
3796
+ if (providerFinishReason !== void 0) {
3797
+ artifacts.providerFinishReason = providerFinishReason;
3798
+ }
4006
3799
  if (contextUsage !== void 0) artifacts.contextUsage = contextUsage;
4007
3800
  return artifacts;
4008
3801
  }
4009
- function reasoningDeltaEvent2(turn, delta, details = {}) {
3802
+ function reasoningDeltaEvent(turn, delta, details = {}) {
4010
3803
  const event = {
4011
3804
  type: "reasoning_delta",
4012
3805
  turn,
@@ -4049,19 +3842,83 @@ function latestAssistantText(messages) {
4049
3842
  }
4050
3843
  return "";
4051
3844
  }
4052
- function structuredOutputRetryRequest(request, response) {
3845
+ function structuredOutputRetryRequest(request, response, error) {
3846
+ const truncated = error instanceof AgentStructuredOutputError && error.phase === "truncated";
3847
+ const correction = {
3848
+ role: "user",
3849
+ content: truncated ? STRUCTURED_OUTPUT_TRUNCATED_RETRY_PROMPT : STRUCTURED_OUTPUT_RETRY_PROMPT
3850
+ };
3851
+ if (truncated) {
3852
+ return {
3853
+ request: {
3854
+ ...request,
3855
+ chatHistory: [...request.chatHistory, correction]
3856
+ },
3857
+ previousResponse: "omitted",
3858
+ includedOutputLength: 0
3859
+ };
3860
+ }
3861
+ const preview = structuredOutputRepairPreview(textFromAssistantContent(response.choice));
3862
+ if (preview.includedOutputLength === 0) {
3863
+ return {
3864
+ request: {
3865
+ ...request,
3866
+ chatHistory: [...request.chatHistory, correction]
3867
+ },
3868
+ previousResponse: "omitted",
3869
+ includedOutputLength: 0
3870
+ };
3871
+ }
4053
3872
  const invalidResponse = {
4054
3873
  role: "assistant",
4055
- content: [...response.choice]
3874
+ content: [{ type: "text", text: preview.text }]
4056
3875
  };
4057
- const correction = {
4058
- role: "user",
4059
- content: STRUCTURED_OUTPUT_RETRY_PROMPT
3876
+ return {
3877
+ request: {
3878
+ ...request,
3879
+ chatHistory: [...request.chatHistory, invalidResponse, correction]
3880
+ },
3881
+ previousResponse: "preview",
3882
+ includedOutputLength: preview.includedOutputLength
4060
3883
  };
3884
+ }
3885
+ function structuredOutputRetryEventAttributes(error, retryRequest) {
3886
+ const attributes = {
3887
+ previousResponse: retryRequest.previousResponse,
3888
+ includedOutputLength: retryRequest.includedOutputLength
3889
+ };
3890
+ if (!(error instanceof AgentStructuredOutputError)) return attributes;
3891
+ Object.assign(attributes, {
3892
+ failurePhase: error.phase,
3893
+ outputLength: error.outputLength,
3894
+ normalizedLength: error.normalizedLength,
3895
+ attemptUsage: usageEventValue(error.attemptUsage),
3896
+ cumulativeUsage: usageEventValue(error.usage)
3897
+ });
3898
+ if (error.finishReason !== void 0) attributes.finishReason = error.finishReason;
3899
+ if (error.providerFinishReason !== void 0) {
3900
+ attributes.providerFinishReason = error.providerFinishReason;
3901
+ }
3902
+ return attributes;
3903
+ }
3904
+ function providerOutputRetryEventAttributes(error, cumulativeUsage, attemptUsage) {
3905
+ const resolvedAttemptUsage = attemptUsage ?? completionProviderOutputErrorUsage(error);
3906
+ if (resolvedAttemptUsage === void 0) return void 0;
4061
3907
  return {
4062
- ...request,
4063
- chatHistory: [...request.chatHistory, invalidResponse, correction]
3908
+ attemptUsage: usageEventValue(resolvedAttemptUsage),
3909
+ cumulativeUsage: usageEventValue(cumulativeUsage)
3910
+ };
3911
+ }
3912
+ function usageEventValue(usage) {
3913
+ const value = {
3914
+ inputTokens: usage.inputTokens,
3915
+ outputTokens: usage.outputTokens,
3916
+ totalTokens: usage.totalTokens,
3917
+ cachedInputTokens: usage.cachedInputTokens,
3918
+ cacheCreationInputTokens: usage.cacheCreationInputTokens
4064
3919
  };
3920
+ if (usage.details !== void 0) value.details = { ...usage.details };
3921
+ return value;
4065
3922
  }
4066
3923
  function providerMessages(messages) {
4067
3924
  const providerMessages2 = [];
@@ -4653,6 +4510,9 @@ var Agent = class {
4653
4510
  this.context = snapshotAgentContext(resolved.context);
4654
4511
  this.temperature = resolved.temperature;
4655
4512
  this.maxTokens = resolved.maxTokens;
4513
+ if (resolved.providerOptions !== void 0) {
4514
+ assertJsonObject(resolved.providerOptions, "Agent providerOptions");
4515
+ }
4656
4516
  this.providerOptions = cloneFrozenPlainData(resolved.providerOptions);
4657
4517
  this.retries = cloneFrozenPlainData(resolved.retries);
4658
4518
  const preparedTools = prepareAgentTools(resolved);
@@ -4706,4 +4566,4 @@ export {
4706
4566
  markResolvedAgentOptions,
4707
4567
  Agent
4708
4568
  };
4709
- //# sourceMappingURL=chunk-FCANRQ5Q.js.map
4569
+ //# sourceMappingURL=chunk-YA2655FX.js.map