@anvia/core 1.0.0-rc.5 → 1.0.0-rc.6

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 (58) hide show
  1. package/README.md +22 -6
  2. package/dist/agent/index.d.ts +20 -14
  3. package/dist/agent/index.js +5 -5
  4. package/dist/agent/interactions/index.d.ts +1 -1
  5. package/dist/{agent-LlCD_2UQ.d.ts → agent-BK3jud9q.d.ts} +10 -10
  6. package/dist/{chunk-E5J4OY4K.js → chunk-7O45T5F2.js} +2 -2
  7. package/dist/{chunk-YDJA4R37.js → chunk-BOCJCECN.js} +2 -2
  8. package/dist/{chunk-HSA6NL2E.js → chunk-HT4P3SSG.js} +61 -5
  9. package/dist/chunk-HT4P3SSG.js.map +1 -0
  10. package/dist/{chunk-DNQNOHD5.js → chunk-QXQQ2DBM.js} +2 -2
  11. package/dist/{chunk-FCANRQ5Q.js → chunk-QXSVVYQK.js} +151 -28
  12. package/dist/chunk-QXSVVYQK.js.map +1 -0
  13. package/dist/{chunk-TEV3OB7N.js → chunk-VGJRXUPT.js} +10 -3
  14. package/dist/chunk-VGJRXUPT.js.map +1 -0
  15. package/dist/{client-DvUatElp.d.ts → client-BdQ_QZsY.d.ts} +2 -2
  16. package/dist/completion/index.d.ts +3 -3
  17. package/dist/completion/index.js +3 -1
  18. package/dist/{dynamic-tools-AEBOTgu1.d.ts → dynamic-tools--Aw37H9d.d.ts} +3 -3
  19. package/dist/evals/index.d.ts +6 -6
  20. package/dist/evals/index.js +3 -3
  21. package/dist/extractor/index.d.ts +1 -1
  22. package/dist/extractor/index.js +2 -2
  23. package/dist/guardrails/index.d.ts +4 -4
  24. package/dist/image-generation/index.d.ts +1 -1
  25. package/dist/index.d.ts +14 -14
  26. package/dist/index.js +7 -5
  27. package/dist/internal/agent.d.ts +11 -11
  28. package/dist/internal/agent.js +5 -5
  29. package/dist/mcp/index.d.ts +4 -4
  30. package/dist/memory/index.d.ts +3 -3
  31. package/dist/memory/index.js +2 -2
  32. package/dist/{message-schema-B2M7AYzR.d.ts → message-schema-_sP_RKhh.d.ts} +18 -2
  33. package/dist/{middleware-BqtLB_Xt.d.ts → middleware-QZsbz7Hf.d.ts} +1 -1
  34. package/dist/observability/index.d.ts +6 -6
  35. package/dist/observability/index.js +2 -2
  36. package/dist/pipeline/index.d.ts +11 -11
  37. package/dist/pipeline/index.js +3 -3
  38. package/dist/skills/index.d.ts +4 -4
  39. package/dist/speech-generation/index.d.ts +1 -1
  40. package/dist/{text-C_6eKrbC.d.ts → text-DTg1a8Mg.d.ts} +1 -1
  41. package/dist/{think-tool-Cz3QYNPX.d.ts → think-tool-C0n_dF2K.d.ts} +1 -1
  42. package/dist/tool/index.d.ts +6 -6
  43. package/dist/{tool-r62gf3QY.d.ts → tool-xHM_GmzT.d.ts} +1 -1
  44. package/dist/transcription/index.d.ts +1 -1
  45. package/dist/{types-1KpjDfoN.d.ts → types-2DBdXArn.d.ts} +7 -5
  46. package/dist/{types-DbolvLtZ.d.ts → types-B1KL4w14.d.ts} +1 -1
  47. package/dist/{types-C3gv9EX6.d.ts → types-D0Y2ZWS3.d.ts} +1 -1
  48. package/dist/{types-CIPaIbZo.d.ts → types-D7A6CIGt.d.ts} +1 -1
  49. package/dist/{types-D9rKQ_as.d.ts → types-DboZvHR_.d.ts} +8 -1
  50. package/dist/{types-RT3cuyRk.d.ts → types-Dyp7arZQ.d.ts} +1 -1
  51. package/dist/vector-store/index.d.ts +4 -4
  52. package/package.json +1 -1
  53. package/dist/chunk-FCANRQ5Q.js.map +0 -1
  54. package/dist/chunk-HSA6NL2E.js.map +0 -1
  55. package/dist/chunk-TEV3OB7N.js.map +0 -1
  56. /package/dist/{chunk-E5J4OY4K.js.map → chunk-7O45T5F2.js.map} +0 -0
  57. /package/dist/{chunk-YDJA4R37.js.map → chunk-BOCJCECN.js.map} +0 -0
  58. /package/dist/{chunk-DNQNOHD5.js.map → chunk-QXQQ2DBM.js.map} +0 -0
@@ -13,10 +13,10 @@ import {
13
13
  assertPositiveInteger,
14
14
  createMemoryCompactionSummary,
15
15
  cumulativeCompactedMessageCount
16
- } from "./chunk-DNQNOHD5.js";
16
+ } from "./chunk-QXQQ2DBM.js";
17
17
  import {
18
18
  startAgentRunObservers
19
- } from "./chunk-E5J4OY4K.js";
19
+ } from "./chunk-7O45T5F2.js";
20
20
  import {
21
21
  isSkillTool
22
22
  } from "./chunk-YK4WAAS4.js";
@@ -44,7 +44,7 @@ import {
44
44
  AgentStructuredOutputError,
45
45
  AgentToolSuspensionError,
46
46
  MaxTurnsError
47
- } from "./chunk-TEV3OB7N.js";
47
+ } from "./chunk-VGJRXUPT.js";
48
48
  import {
49
49
  mapWithConcurrency
50
50
  } from "./chunk-3RM57ZT2.js";
@@ -56,7 +56,7 @@ import {
56
56
  isProviderTool,
57
57
  isStreamingCompletionModel,
58
58
  textFromAssistantContent
59
- } from "./chunk-HSA6NL2E.js";
59
+ } from "./chunk-HT4P3SSG.js";
60
60
  import {
61
61
  parseMessage,
62
62
  parseMessages,
@@ -963,6 +963,12 @@ var CompletionStreamAccumulator = class {
963
963
  usage: finalResponse.usage,
964
964
  rawResponse: finalResponse.rawResponse
965
965
  };
966
+ if (finalResponse.finishReason !== void 0) {
967
+ mergedResponse.finishReason = finalResponse.finishReason;
968
+ }
969
+ if (finalResponse.providerFinishReason !== void 0) {
970
+ mergedResponse.providerFinishReason = finalResponse.providerFinishReason;
971
+ }
966
972
  if (finalResponse.messageId !== void 0) {
967
973
  mergedResponse.messageId = finalResponse.messageId;
968
974
  }
@@ -1254,6 +1260,21 @@ var UNLABELED_FENCE_START = "```\n";
1254
1260
  var UNLABELED_FENCE_CRLF_START = "```\r\n";
1255
1261
  var FENCE_END = "\n```";
1256
1262
  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.";
1263
+ 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.";
1264
+ var STRUCTURED_OUTPUT_REPAIR_PREVIEW_MAX_LENGTH = 8192;
1265
+ var STRUCTURED_OUTPUT_REPAIR_PREVIEW_MARKER = "\n...[output omitted]...\n";
1266
+ function structuredOutputRepairPreview(text) {
1267
+ if (text.length <= STRUCTURED_OUTPUT_REPAIR_PREVIEW_MAX_LENGTH) {
1268
+ return { text, includedOutputLength: text.length };
1269
+ }
1270
+ const availableLength = STRUCTURED_OUTPUT_REPAIR_PREVIEW_MAX_LENGTH - STRUCTURED_OUTPUT_REPAIR_PREVIEW_MARKER.length;
1271
+ const headLength = Math.ceil(availableLength / 2);
1272
+ const tailLength = availableLength - headLength;
1273
+ return {
1274
+ text: `${text.slice(0, headLength)}${STRUCTURED_OUTPUT_REPAIR_PREVIEW_MARKER}${text.slice(-tailLength)}`,
1275
+ includedOutputLength: headLength + tailLength
1276
+ };
1277
+ }
1257
1278
  function normalizeStructuredOutput(text) {
1258
1279
  const trimmed = text.trim();
1259
1280
  const opening = fenceOpening(trimmed);
@@ -2938,14 +2959,16 @@ var AgentRun = class _AgentRun {
2938
2959
  }
2939
2960
  structuredRetryUsage = cumulativeUsage;
2940
2961
  this.failedCompletionUsage = structuredRetryUsage;
2941
- currentRequest = structuredOutputRetryRequest(currentRequest, response);
2962
+ const retryRequest = structuredOutputRetryRequest(request, response, error);
2963
+ currentRequest = retryRequest.request;
2942
2964
  await this.scheduleCompletionRetry(
2943
2965
  error,
2944
2966
  attempt,
2945
2967
  turn,
2946
2968
  false,
2947
2969
  retryOptions,
2948
- runObservers
2970
+ runObservers,
2971
+ structuredOutputRetryEventAttributes(error, retryRequest)
2949
2972
  );
2950
2973
  continue;
2951
2974
  }
@@ -3029,14 +3052,16 @@ var AgentRun = class _AgentRun {
3029
3052
  args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, response.usage);
3030
3053
  args.state.firstDeltaMs = void 0;
3031
3054
  args.state.emittedToolCallIds.clear();
3032
- currentRequest = structuredOutputRetryRequest(currentRequest, response);
3055
+ const retryRequest = structuredOutputRetryRequest(args.request, response, error);
3056
+ currentRequest = retryRequest.request;
3033
3057
  await this.scheduleCompletionRetry(
3034
3058
  error,
3035
3059
  attempt,
3036
3060
  args.turn,
3037
3061
  true,
3038
3062
  retryOptions,
3039
- args.runObservers
3063
+ args.runObservers,
3064
+ structuredOutputRetryEventAttributes(error, retryRequest)
3040
3065
  );
3041
3066
  continue;
3042
3067
  }
@@ -3180,6 +3205,10 @@ var AgentRun = class _AgentRun {
3180
3205
  modelId: this.agent.model.modelId,
3181
3206
  usage: { ...response.usage }
3182
3207
  };
3208
+ if (response.finishReason !== void 0) generation.finishReason = response.finishReason;
3209
+ if (response.providerFinishReason !== void 0) {
3210
+ generation.providerFinishReason = response.providerFinishReason;
3211
+ }
3183
3212
  if (response.contextUsage !== void 0) generation.contextUsage = response.contextUsage;
3184
3213
  if (response.sources !== void 0) generation.sources = response.sources;
3185
3214
  if (response.providerToolCalls !== void 0) {
@@ -3225,20 +3254,24 @@ var AgentRun = class _AgentRun {
3225
3254
  streaming
3226
3255
  });
3227
3256
  }
3228
- async scheduleCompletionRetry(error, attempt, turn, streaming, options, runObservers) {
3257
+ async scheduleCompletionRetry(error, attempt, turn, streaming, options, runObservers, additionalAttributes) {
3229
3258
  const delayMs = retryDelayMs(options, attempt);
3259
+ const attributes = {
3260
+ turn,
3261
+ attempt,
3262
+ nextAttempt: attempt + 1,
3263
+ maxAttempts: options.maxAttempts,
3264
+ delayMs,
3265
+ streaming,
3266
+ ...retryErrorAttributes(error)
3267
+ };
3268
+ if (additionalAttributes !== void 0) {
3269
+ Object.assign(attributes, additionalAttributes);
3270
+ }
3230
3271
  await runObservers.event({
3231
3272
  name: "completion.retry",
3232
3273
  level: "WARNING",
3233
- attributes: {
3234
- turn,
3235
- attempt,
3236
- nextAttempt: attempt + 1,
3237
- maxAttempts: options.maxAttempts,
3238
- delayMs,
3239
- streaming,
3240
- ...retryErrorAttributes(error)
3241
- }
3274
+ attributes
3242
3275
  });
3243
3276
  await waitForRetry(delayMs, this.abortController.signal);
3244
3277
  }
@@ -3476,7 +3509,27 @@ var AgentRun = class _AgentRun {
3476
3509
  if (this.agent.outputSchema === void 0) return;
3477
3510
  if (response.choice.some((item) => item.type === "tool-call")) return;
3478
3511
  const text = textFromAssistantContent(response.choice);
3479
- const output = this.parseOutput(text, attempt, usage, { useValidatedOutput: false });
3512
+ const normalized = normalizeStructuredOutput(text);
3513
+ if (response.finishReason === "length") {
3514
+ throw new AgentStructuredOutputError({
3515
+ phase: "truncated",
3516
+ attempt,
3517
+ maxAttempts: this.completionRetryOptions?.maxAttempts ?? 1,
3518
+ outputLength: text.length,
3519
+ normalizedLength: normalized.text.length,
3520
+ outputFormat: normalized.format,
3521
+ attemptUsage: response.usage,
3522
+ usage,
3523
+ finishReason: response.finishReason,
3524
+ providerFinishReason: response.providerFinishReason
3525
+ });
3526
+ }
3527
+ const output = this.parseOutput(text, attempt, usage, {
3528
+ useValidatedOutput: false,
3529
+ attemptUsage: response.usage,
3530
+ finishReason: response.finishReason,
3531
+ providerFinishReason: response.providerFinishReason
3532
+ });
3480
3533
  this.validatedStructuredOutput = { text, output };
3481
3534
  }
3482
3535
  takeFailedCompletionUsage() {
@@ -3503,7 +3556,10 @@ var AgentRun = class _AgentRun {
3503
3556
  outputLength: text.length,
3504
3557
  normalizedLength: normalized.text.length,
3505
3558
  outputFormat: normalized.format,
3559
+ attemptUsage: options.attemptUsage ?? usage,
3506
3560
  usage,
3561
+ finishReason: options.finishReason,
3562
+ providerFinishReason: options.providerFinishReason,
3507
3563
  cause: error
3508
3564
  });
3509
3565
  }
@@ -3517,7 +3573,10 @@ var AgentRun = class _AgentRun {
3517
3573
  outputLength: text.length,
3518
3574
  normalizedLength: normalized.text.length,
3519
3575
  outputFormat: normalized.format,
3576
+ attemptUsage: options.attemptUsage ?? usage,
3520
3577
  usage,
3578
+ finishReason: options.finishReason,
3579
+ providerFinishReason: options.providerFinishReason,
3521
3580
  cause: error
3522
3581
  });
3523
3582
  }
@@ -3984,10 +4043,14 @@ function responseStreamEvents(turn, response, includeProviderArtifacts = true) {
3984
4043
  function generationArtifacts(messages) {
3985
4044
  const sources = /* @__PURE__ */ new Map();
3986
4045
  const providerToolCalls = /* @__PURE__ */ new Map();
4046
+ let finishReason;
4047
+ let providerFinishReason;
3987
4048
  let contextUsage;
3988
4049
  for (const message of messages) {
3989
4050
  const metadata = getAssistantGenerationMetadata(message);
3990
4051
  if (metadata !== void 0) {
4052
+ finishReason = metadata.finishReason;
4053
+ providerFinishReason = metadata.providerFinishReason;
3991
4054
  contextUsage = metadata.contextUsage;
3992
4055
  }
3993
4056
  for (const source of metadata?.sources ?? []) {
@@ -4003,6 +4066,10 @@ function generationArtifacts(messages) {
4003
4066
  if (providerToolCalls.size > 0) {
4004
4067
  artifacts.providerToolCalls = [...providerToolCalls.values()];
4005
4068
  }
4069
+ if (finishReason !== void 0) artifacts.finishReason = finishReason;
4070
+ if (providerFinishReason !== void 0) {
4071
+ artifacts.providerFinishReason = providerFinishReason;
4072
+ }
4006
4073
  if (contextUsage !== void 0) artifacts.contextUsage = contextUsage;
4007
4074
  return artifacts;
4008
4075
  }
@@ -4049,19 +4116,75 @@ function latestAssistantText(messages) {
4049
4116
  }
4050
4117
  return "";
4051
4118
  }
4052
- function structuredOutputRetryRequest(request, response) {
4053
- const invalidResponse = {
4054
- role: "assistant",
4055
- content: [...response.choice]
4056
- };
4119
+ function structuredOutputRetryRequest(request, response, error) {
4120
+ const truncated = error instanceof AgentStructuredOutputError && error.phase === "truncated";
4057
4121
  const correction = {
4058
4122
  role: "user",
4059
- content: STRUCTURED_OUTPUT_RETRY_PROMPT
4123
+ content: truncated ? STRUCTURED_OUTPUT_TRUNCATED_RETRY_PROMPT : STRUCTURED_OUTPUT_RETRY_PROMPT
4124
+ };
4125
+ if (truncated) {
4126
+ return {
4127
+ request: {
4128
+ ...request,
4129
+ chatHistory: [...request.chatHistory, correction]
4130
+ },
4131
+ previousResponse: "omitted",
4132
+ includedOutputLength: 0
4133
+ };
4134
+ }
4135
+ const preview = structuredOutputRepairPreview(textFromAssistantContent(response.choice));
4136
+ if (preview.includedOutputLength === 0) {
4137
+ return {
4138
+ request: {
4139
+ ...request,
4140
+ chatHistory: [...request.chatHistory, correction]
4141
+ },
4142
+ previousResponse: "omitted",
4143
+ includedOutputLength: 0
4144
+ };
4145
+ }
4146
+ const invalidResponse = {
4147
+ role: "assistant",
4148
+ content: [{ type: "text", text: preview.text }]
4060
4149
  };
4061
4150
  return {
4062
- ...request,
4063
- chatHistory: [...request.chatHistory, invalidResponse, correction]
4151
+ request: {
4152
+ ...request,
4153
+ chatHistory: [...request.chatHistory, invalidResponse, correction]
4154
+ },
4155
+ previousResponse: "preview",
4156
+ includedOutputLength: preview.includedOutputLength
4157
+ };
4158
+ }
4159
+ function structuredOutputRetryEventAttributes(error, retryRequest) {
4160
+ const attributes = {
4161
+ previousResponse: retryRequest.previousResponse,
4162
+ includedOutputLength: retryRequest.includedOutputLength
4064
4163
  };
4164
+ if (!(error instanceof AgentStructuredOutputError)) return attributes;
4165
+ Object.assign(attributes, {
4166
+ failurePhase: error.phase,
4167
+ outputLength: error.outputLength,
4168
+ normalizedLength: error.normalizedLength,
4169
+ attemptUsage: usageEventValue(error.attemptUsage),
4170
+ cumulativeUsage: usageEventValue(error.usage)
4171
+ });
4172
+ if (error.finishReason !== void 0) attributes.finishReason = error.finishReason;
4173
+ if (error.providerFinishReason !== void 0) {
4174
+ attributes.providerFinishReason = error.providerFinishReason;
4175
+ }
4176
+ return attributes;
4177
+ }
4178
+ function usageEventValue(usage) {
4179
+ const value = {
4180
+ inputTokens: usage.inputTokens,
4181
+ outputTokens: usage.outputTokens,
4182
+ totalTokens: usage.totalTokens,
4183
+ cachedInputTokens: usage.cachedInputTokens,
4184
+ cacheCreationInputTokens: usage.cacheCreationInputTokens
4185
+ };
4186
+ if (usage.details !== void 0) value.details = { ...usage.details };
4187
+ return value;
4065
4188
  }
4066
4189
  function providerMessages(messages) {
4067
4190
  const providerMessages2 = [];
@@ -4706,4 +4829,4 @@ export {
4706
4829
  markResolvedAgentOptions,
4707
4830
  Agent
4708
4831
  };
4709
- //# sourceMappingURL=chunk-FCANRQ5Q.js.map
4832
+ //# sourceMappingURL=chunk-QXSVVYQK.js.map