@anvia/core 1.0.0-rc.6 → 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 (108) hide show
  1. package/README.md +30 -13
  2. package/dist/agent/index.d.ts +13 -13
  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-BK3jud9q.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-QXQQ2DBM.js → chunk-5C6B6ZRH.js} +2 -2
  14. package/dist/{chunk-7O45T5F2.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-BOCJCECN.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-VGJRXUPT.js → chunk-X4LK4ODN.js} +2 -2
  36. package/dist/chunk-X4LK4ODN.js.map +1 -0
  37. package/dist/{chunk-QXSVVYQK.js → chunk-YA2655FX.js} +169 -432
  38. package/dist/chunk-YA2655FX.js.map +1 -0
  39. package/dist/{chunk-H7BI3BSP.js → chunk-ZA564323.js} +2 -2
  40. package/dist/{client-BdQ_QZsY.d.ts → client-DTPl0hZv.d.ts} +2 -2
  41. package/dist/completion/index.d.ts +3 -3
  42. package/dist/completion/index.js +10 -5
  43. package/dist/{dynamic-tools--Aw37H9d.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 +23 -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-QZsbz7Hf.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/{message-schema-_sP_RKhh.d.ts → provider-output-error-BEwDgVv3.d.ts} +34 -3
  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-DTg1a8Mg.d.ts → text-ZLECpQE7.d.ts} +1 -1
  73. package/dist/{think-tool-C0n_dF2K.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-xHM_GmzT.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-D0Y2ZWS3.d.ts → types-CXNE592e.d.ts} +1 -1
  80. package/dist/{types-Dyp7arZQ.d.ts → types-DC1U1XwW.d.ts} +1 -1
  81. package/dist/{types-B1KL4w14.d.ts → types-DKOXVTcq.d.ts} +1 -1
  82. package/dist/{types-DboZvHR_.d.ts → types-DgvozfPc.d.ts} +3 -2
  83. package/dist/{types-D7A6CIGt.d.ts → types-DhkodEft.d.ts} +1 -1
  84. package/dist/{types-2DBdXArn.d.ts → types-Sue2hb_L.d.ts} +5 -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-HT4P3SSG.js +0 -573
  94. package/dist/chunk-HT4P3SSG.js.map +0 -1
  95. package/dist/chunk-HVHTVGPP.js.map +0 -1
  96. package/dist/chunk-QXSVVYQK.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-UBE4WH6G.js.map +0 -1
  100. package/dist/chunk-VGJRXUPT.js.map +0 -1
  101. package/dist/chunk-YXLDR2O4.js.map +0 -1
  102. /package/dist/{chunk-QXQQ2DBM.js.map → chunk-5C6B6ZRH.js.map} +0 -0
  103. /package/dist/{chunk-7O45T5F2.js.map → chunk-6BBOCFHV.js.map} +0 -0
  104. /package/dist/{chunk-MGNTXGTX.js.map → chunk-D4PACGPA.js.map} +0 -0
  105. /package/dist/{chunk-BOCJCECN.js.map → chunk-LTXABCOO.js.map} +0 -0
  106. /package/dist/{chunk-BEU6FCTN.js.map → chunk-OJBFDBLG.js.map} +0 -0
  107. /package/dist/{chunk-G5OACQEI.js.map → chunk-QGX73TSQ.js.map} +0 -0
  108. /package/dist/{chunk-H7BI3BSP.js.map → chunk-ZA564323.js.map} +0 -0
@@ -1,573 +0,0 @@
1
- import {
2
- parseMessage,
3
- parseMessages,
4
- toProviderJsonSchema
5
- } from "./chunk-G5OACQEI.js";
6
- import {
7
- isJsonValue
8
- } from "./chunk-SROQA7I7.js";
9
- import {
10
- abortError,
11
- resolveRetryOptions,
12
- retryDelayMs,
13
- retryOptionsForFailure,
14
- runWithRetries,
15
- throwIfAborted,
16
- waitForRetry
17
- } from "./chunk-UBE4WH6G.js";
18
-
19
- // src/completion/types.ts
20
- function reasoningDisplayText(reasoning) {
21
- const details = "type" in reasoning ? reasoning.details : reasoning;
22
- if (details === void 0) {
23
- return "type" in reasoning ? reasoning.text : "";
24
- }
25
- return details.flatMap((item) => {
26
- if (item.type === "text" || item.type === "summary") {
27
- return [item.text];
28
- }
29
- return [];
30
- }).join("");
31
- }
32
- function isProviderTool(value) {
33
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
34
- return false;
35
- }
36
- const candidate = value;
37
- return candidate.kind === "provider" && typeof candidate.provider === "string" && candidate.provider.trim().length > 0 && typeof candidate.name === "string" && candidate.name.trim().length > 0 && (candidate.configuration === void 0 || typeof candidate.configuration === "object" && candidate.configuration !== null && !Array.isArray(candidate.configuration) && isJsonValue(candidate.configuration));
38
- }
39
- function calculateContextUsage(usage, model) {
40
- if (model === void 0 || !Number.isFinite(usage.inputTokens) || usage.inputTokens <= 0 || !Number.isFinite(model.context.contextWindow) || model.context.contextWindow <= 0) {
41
- return void 0;
42
- }
43
- const usedTokens = Math.max(0, usage.inputTokens);
44
- const remainingTokens = Math.max(0, model.context.contextWindow - usedTokens);
45
- const usedPercent = Math.min(100, usedTokens / model.context.contextWindow * 100);
46
- return {
47
- model,
48
- usedTokens,
49
- remainingTokens,
50
- usedPercent,
51
- remainingPercent: 100 - usedPercent
52
- };
53
- }
54
- function withContextUsage(response, model) {
55
- const contextUsage = calculateContextUsage(response.usage, model);
56
- return contextUsage === void 0 ? response : { ...response, contextUsage };
57
- }
58
- function resolveModelContextLimits(modelId, catalog, override) {
59
- return override ?? catalog[modelId];
60
- }
61
- var Usage = {
62
- empty() {
63
- return {
64
- inputTokens: 0,
65
- outputTokens: 0,
66
- totalTokens: 0,
67
- cachedInputTokens: 0,
68
- cacheCreationInputTokens: 0
69
- };
70
- },
71
- add(left, right) {
72
- const result = {
73
- inputTokens: left.inputTokens + right.inputTokens,
74
- outputTokens: left.outputTokens + right.outputTokens,
75
- totalTokens: left.totalTokens + right.totalTokens,
76
- cachedInputTokens: left.cachedInputTokens + right.cachedInputTokens,
77
- cacheCreationInputTokens: left.cacheCreationInputTokens + right.cacheCreationInputTokens
78
- };
79
- const details = addUsageDetails(left, right);
80
- if (details !== void 0) {
81
- result.details = details;
82
- }
83
- return result;
84
- }
85
- };
86
- function addUsageDetails(left, right) {
87
- if (isEmptyUsage(left) && left.details === void 0) {
88
- return right.details === void 0 ? void 0 : { ...right.details };
89
- }
90
- if (isEmptyUsage(right) && right.details === void 0) {
91
- return left.details === void 0 ? void 0 : { ...left.details };
92
- }
93
- if (left.details === void 0 || right.details === void 0) {
94
- return void 0;
95
- }
96
- const details = { ...left.details };
97
- for (const [key, value] of Object.entries(right.details)) {
98
- details[key] = (details[key] ?? 0) + value;
99
- }
100
- return details;
101
- }
102
- function isEmptyUsage(usage) {
103
- return usage.inputTokens === 0 && usage.outputTokens === 0 && usage.totalTokens === 0 && usage.cachedInputTokens === 0 && usage.cacheCreationInputTokens === 0;
104
- }
105
- function getAssistantGenerationMetadata(message) {
106
- if (message.role !== "assistant" || !isJsonObjectValue(message.metadata)) {
107
- return void 0;
108
- }
109
- const frameworkMetadata = message.metadata.anvia;
110
- if (!isJsonObjectValue(frameworkMetadata)) {
111
- return void 0;
112
- }
113
- const generation = frameworkMetadata.generation;
114
- if (!isJsonObjectValue(generation) || typeof generation.provider !== "string" || typeof generation.modelId !== "string" || !isUsageValue(generation.usage)) {
115
- return void 0;
116
- }
117
- let usage = { ...generation.usage };
118
- if (generation.usage.details !== void 0) {
119
- usage = { ...usage, details: { ...generation.usage.details } };
120
- }
121
- const metadata = {
122
- provider: generation.provider,
123
- modelId: generation.modelId,
124
- usage
125
- };
126
- if (isCompletionFinishReason(generation.finishReason)) {
127
- metadata.finishReason = generation.finishReason;
128
- }
129
- if (typeof generation.providerFinishReason === "string") {
130
- metadata.providerFinishReason = generation.providerFinishReason;
131
- }
132
- if (isContextUsageValue(generation.contextUsage)) {
133
- metadata.contextUsage = {
134
- ...generation.contextUsage,
135
- model: {
136
- ...generation.contextUsage.model,
137
- context: { ...generation.contextUsage.model.context }
138
- }
139
- };
140
- }
141
- if (isCompletionSourceArray(generation.sources)) {
142
- metadata.sources = generation.sources.map((source) => ({ ...source }));
143
- }
144
- if (isProviderToolCallArray(generation.providerToolCalls)) {
145
- metadata.providerToolCalls = generation.providerToolCalls.map((toolCall) => {
146
- let copy = { ...toolCall };
147
- if (toolCall.details !== void 0) {
148
- copy = { ...copy, details: { ...toolCall.details } };
149
- }
150
- return copy;
151
- });
152
- }
153
- return metadata;
154
- }
155
- function isJsonObjectValue(value) {
156
- return typeof value === "object" && value !== null && !Array.isArray(value);
157
- }
158
- function isContextUsageValue(value) {
159
- if (!isJsonObjectValue(value) || !isJsonObjectValue(value.model)) {
160
- return false;
161
- }
162
- const context = value.model.context;
163
- if (typeof value.model.modelId === "string" && isJsonObjectValue(context) && isPositiveFiniteNumber(context.contextWindow) && isOptionalPositiveFiniteNumber(context.maxInputTokens) && isOptionalPositiveFiniteNumber(context.maxOutputTokens) && isNonnegativeFiniteNumber(value.usedTokens) && isNonnegativeFiniteNumber(value.remainingTokens) && isPercentage(value.usedPercent) && isPercentage(value.remainingPercent)) {
164
- const contextWindow = context.contextWindow;
165
- const remainingTokens = Math.max(0, contextWindow - value.usedTokens);
166
- const usedPercent = Math.min(100, value.usedTokens / contextWindow * 100);
167
- const remainingPercent = remainingTokens / contextWindow * 100;
168
- return value.remainingTokens === remainingTokens && approximatelyEqual(value.usedPercent, usedPercent) && approximatelyEqual(value.remainingPercent, remainingPercent);
169
- }
170
- return false;
171
- }
172
- function approximatelyEqual(left, right) {
173
- const scale = Math.max(1, Math.abs(left), Math.abs(right));
174
- return Math.abs(left - right) <= Number.EPSILON * scale * 8;
175
- }
176
- function isPositiveFiniteNumber(value) {
177
- return isNonnegativeFiniteNumber(value) && value > 0;
178
- }
179
- function isOptionalPositiveFiniteNumber(value) {
180
- return value === void 0 || isPositiveFiniteNumber(value);
181
- }
182
- function isPercentage(value) {
183
- return isNonnegativeFiniteNumber(value) && value <= 100;
184
- }
185
- function isUsageValue(value) {
186
- if (!isJsonObjectValue(value)) {
187
- return false;
188
- }
189
- return isNonnegativeFiniteNumber(value.inputTokens) && isNonnegativeFiniteNumber(value.outputTokens) && isNonnegativeFiniteNumber(value.totalTokens) && isNonnegativeFiniteNumber(value.cachedInputTokens) && isNonnegativeFiniteNumber(value.cacheCreationInputTokens) && isUsageDetailsValue(value.details);
190
- }
191
- function isNonnegativeFiniteNumber(value) {
192
- return typeof value === "number" && Number.isFinite(value) && value >= 0;
193
- }
194
- function isUsageDetailsValue(value) {
195
- if (value === void 0) {
196
- return true;
197
- }
198
- if (!isJsonObjectValue(value)) {
199
- return false;
200
- }
201
- let total;
202
- let bucketSum = 0;
203
- for (const [key, detail] of Object.entries(value)) {
204
- if (detail === void 0 || !isNonnegativeFiniteNumber(detail)) {
205
- return false;
206
- }
207
- if (key === "total") {
208
- total = detail;
209
- } else {
210
- bucketSum += detail;
211
- }
212
- }
213
- return total !== void 0 && total === bucketSum;
214
- }
215
- function isCompletionSourceArray(value) {
216
- return Array.isArray(value) && value.every(
217
- (source) => isJsonObjectValue(source) && source.type === "url" && typeof source.url === "string" && (source.title === void 0 || typeof source.title === "string") && (source.id === void 0 || typeof source.id === "string") && (source.startIndex === void 0 || typeof source.startIndex === "number") && (source.endIndex === void 0 || typeof source.endIndex === "number")
218
- );
219
- }
220
- function isProviderToolCallArray(value) {
221
- return Array.isArray(value) && value.every(
222
- (toolCall) => isJsonObjectValue(toolCall) && typeof toolCall.id === "string" && typeof toolCall.name === "string" && (toolCall.status === void 0 || typeof toolCall.status === "string") && (toolCall.details === void 0 || isJsonObjectValue(toolCall.details))
223
- );
224
- }
225
- function isCompletionFinishReason(value) {
226
- return value === "stop" || value === "length" || value === "content-filter" || value === "tool-calls" || value === "other";
227
- }
228
- var CompletionCapabilityError = class extends Error {
229
- constructor(message) {
230
- super(message);
231
- this.name = "CompletionCapabilityError";
232
- }
233
- };
234
- function assertCompletionRequestSupported(model, request, options = {}) {
235
- const modelLabel = `${model.provider}:${model.modelId}`;
236
- const capabilities = model.capabilities;
237
- if (options.streaming === true && !capabilities.streaming) {
238
- throw new CompletionCapabilityError(`${modelLabel} does not support streaming completions.`);
239
- }
240
- if (request.tools.length > 0 && !capabilities.tools) {
241
- throw new CompletionCapabilityError(`${modelLabel} does not support tool definitions.`);
242
- }
243
- if ((request.providerTools?.length ?? 0) > 0 && capabilities.providerTools !== true) {
244
- throw new CompletionCapabilityError(`${modelLabel} does not support provider-executed tools.`);
245
- }
246
- if (request.toolChoice !== void 0 && !capabilities.toolChoice) {
247
- throw new CompletionCapabilityError(`${modelLabel} does not support tool choice.`);
248
- }
249
- if (request.outputSchema !== void 0 && !capabilities.outputSchema) {
250
- throw new CompletionCapabilityError(`${modelLabel} does not support output schemas.`);
251
- }
252
- if (!capabilities.imageInput && requestHasImageInput(request)) {
253
- throw new CompletionCapabilityError(`${modelLabel} does not support image input.`);
254
- }
255
- if (!capabilities.documentInput && requestHasFileDocumentInput(request)) {
256
- throw new CompletionCapabilityError(`${modelLabel} does not support document file input.`);
257
- }
258
- }
259
- function textFromAssistantContent(content) {
260
- return content.flatMap((item) => item.type === "text" ? [item.text] : []).join("\n");
261
- }
262
- function requestHasImageInput(request) {
263
- return request.chatHistory.some(
264
- (message) => message.role === "system" || typeof message.content === "string" ? false : message.content.some((content) => content.type === "image")
265
- );
266
- }
267
- function requestHasFileDocumentInput(request) {
268
- return request.chatHistory.some(
269
- (message) => message.role === "user" && typeof message.content !== "string" ? message.content.some((content) => content.type === "file" && content.data.type !== "text") : false
270
- );
271
- }
272
-
273
- // src/internal/completion-request.ts
274
- function createCompletionRequest(input, options) {
275
- const configuredTools = options.tools ?? [];
276
- const chatHistory = messagesFromInput(input);
277
- assertNoAgentInteractionParts(chatHistory);
278
- const request = {
279
- chatHistory,
280
- documents: [...options.documents ?? []],
281
- tools: configuredTools.filter((tool) => !isProviderTool(tool))
282
- };
283
- const providerTools = configuredTools.filter(isProviderTool);
284
- if (providerTools.length > 0) request.providerTools = providerTools;
285
- if (options.instructions !== void 0 && options.instructions.length > 0) {
286
- request.instructions = options.instructions;
287
- }
288
- if (options.temperature !== void 0) request.temperature = options.temperature;
289
- if (options.maxTokens !== void 0) request.maxTokens = options.maxTokens;
290
- if (options.toolChoice !== void 0) request.toolChoice = options.toolChoice;
291
- if (options.outputSchema !== void 0) request.outputSchema = options.outputSchema;
292
- if (options.providerOptions !== void 0) request.providerOptions = options.providerOptions;
293
- return request;
294
- }
295
- function assertNoAgentInteractionParts(messages) {
296
- for (const message of messages) {
297
- if (message.role === "tool" && message.content.some((part) => part.type !== "tool-result")) {
298
- throw new TypeError(
299
- "Completion messages contain an unresolved Agent interaction response. Resume the Agent with its continuation instead of sending interaction parts directly to a provider."
300
- );
301
- }
302
- }
303
- }
304
- function messagesFromInput(input) {
305
- if (typeof input === "string") {
306
- return [{ role: "user", content: input }];
307
- }
308
- if (Array.isArray(input)) {
309
- if (input.length === 0) {
310
- throw new Error("input must contain at least one Message.");
311
- }
312
- return parseMessages(input);
313
- }
314
- return [parseMessage(input)];
315
- }
316
-
317
- // src/completion/generate-completion.ts
318
- var CompletionStructuredOutputError = class extends Error {
319
- phase;
320
- outputLength;
321
- usage;
322
- finishReason;
323
- providerFinishReason;
324
- constructor(options) {
325
- const failure = options.phase === "truncated" ? "because the provider reached its output limit" : options.phase === "parse" ? "during JSON parsing" : "during schema validation";
326
- super(`Structured completion output failed ${failure}.`, { cause: options.cause });
327
- this.name = "CompletionStructuredOutputError";
328
- this.phase = options.phase;
329
- this.outputLength = options.outputLength;
330
- this.usage = options.usage;
331
- this.finishReason = options.finishReason;
332
- this.providerFinishReason = options.providerFinishReason;
333
- }
334
- };
335
- async function generateCompletion(options) {
336
- throwIfAborted(options.abortSignal);
337
- const request = requestFromOptions(options);
338
- assertCompletionRequestSupported(options.model, request);
339
- const retries = resolveOptionalRetries(options.retries);
340
- const response = await sendCompletion(options.model, request, retries, options.abortSignal);
341
- return resultFromResponse(response, structuredOutputSchema(options));
342
- }
343
- function streamCompletion(options) {
344
- throwIfAborted(options.abortSignal);
345
- const request = requestFromOptions(options);
346
- if (!isStreamingCompletionModel(options.model) || !options.model.capabilities.streaming) {
347
- throw new Error("This completion model does not support streaming");
348
- }
349
- assertCompletionRequestSupported(options.model, request, { streaming: true });
350
- const retries = resolveOptionalRetries(options.retries);
351
- return streamCompletionWithRetries(
352
- options.model,
353
- request,
354
- retries,
355
- options.abortSignal,
356
- structuredOutputSchema(options)
357
- );
358
- }
359
- async function sendCompletion(model, request, retries, abortSignal) {
360
- const callOptions = modelCallOptions(abortSignal);
361
- return runWithRetries(
362
- () => {
363
- throwIfAborted(abortSignal);
364
- return model.completion(request, callOptions);
365
- },
366
- retries,
367
- { streaming: false, abortSignal }
368
- );
369
- }
370
- function resolveOptionalRetries(setting) {
371
- return setting === void 0 || setting === false ? void 0 : resolveRetryOptions(setting);
372
- }
373
- async function* streamCompletionWithRetries(model, request, retries, abortSignal, outputSchema) {
374
- let attempt = 1;
375
- let swallowedUsage = Usage.empty();
376
- const callOptions = modelCallOptions(abortSignal);
377
- while (true) {
378
- let exposedProgress = false;
379
- let retryDelay;
380
- try {
381
- throwIfAborted(abortSignal);
382
- const events = model.streamCompletion(request, callOptions);
383
- for await (const event of events) {
384
- if (event.type === "error" && !exposedProgress) {
385
- const retryOptions = retryOptionsForFailure(retries, {
386
- error: event.error,
387
- attempt,
388
- streaming: true
389
- });
390
- if (retryOptions !== void 0) {
391
- swallowedUsage = Usage.add(swallowedUsage, event.usage ?? Usage.empty());
392
- retryDelay = retryDelayMs(retryOptions, attempt);
393
- break;
394
- }
395
- }
396
- if (event.type === "error") {
397
- yield {
398
- type: "error",
399
- error: event.error,
400
- usage: Usage.add(swallowedUsage, event.usage ?? Usage.empty())
401
- };
402
- return;
403
- }
404
- if (event.type === "final") {
405
- const response = swallowedUsage.totalTokens === 0 ? event.response : {
406
- ...event.response,
407
- usage: Usage.add(swallowedUsage, event.response.usage)
408
- };
409
- try {
410
- yield {
411
- type: "final",
412
- result: resultFromResponse(response, outputSchema)
413
- };
414
- } catch (error) {
415
- yield { type: "error", error, usage: response.usage };
416
- }
417
- return;
418
- }
419
- exposedProgress = true;
420
- yield event;
421
- }
422
- if (retryDelay !== void 0) {
423
- await waitForRetry(retryDelay, abortSignal);
424
- attempt += 1;
425
- continue;
426
- }
427
- yield {
428
- type: "error",
429
- error: new Error("The completion model stream ended without a final event."),
430
- usage: swallowedUsage
431
- };
432
- return;
433
- } catch (error) {
434
- const normalizedError = abortSignal?.aborted === true ? abortError(abortSignal.reason) : error;
435
- if (!exposedProgress) {
436
- const retryOptions = retryOptionsForFailure(retries, {
437
- error: normalizedError,
438
- attempt,
439
- streaming: true
440
- });
441
- if (retryOptions !== void 0) {
442
- try {
443
- await waitForRetry(retryDelayMs(retryOptions, attempt), abortSignal);
444
- attempt += 1;
445
- continue;
446
- } catch (waitError) {
447
- yield { type: "error", error: waitError, usage: swallowedUsage };
448
- return;
449
- }
450
- }
451
- }
452
- yield { type: "error", error: normalizedError, usage: swallowedUsage };
453
- return;
454
- }
455
- }
456
- }
457
- function requestFromOptions(options) {
458
- const input = inputFromOptions(options);
459
- return createCompletionRequest(input, {
460
- instructions: options.instructions,
461
- documents: options.documents,
462
- tools: options.tools,
463
- temperature: options.temperature,
464
- maxTokens: options.maxTokens,
465
- toolChoice: options.toolChoice,
466
- providerOptions: options.providerOptions,
467
- outputSchema: "outputSchema" in options && options.outputSchema !== void 0 ? toProviderJsonSchema(options.outputSchema) : void 0
468
- });
469
- }
470
- function inputFromOptions(options) {
471
- const prompt = options.prompt;
472
- const messages = options.messages;
473
- const hasPrompt = prompt !== void 0;
474
- const hasMessages = messages !== void 0;
475
- if (hasPrompt === hasMessages) {
476
- throw new TypeError("Exactly one of prompt or messages must be provided.");
477
- }
478
- if (hasPrompt) {
479
- if (typeof prompt !== "string" || prompt.trim().length === 0) {
480
- throw new TypeError("Completion prompt must be a non-empty string.");
481
- }
482
- return prompt;
483
- }
484
- if (!Array.isArray(messages)) {
485
- throw new TypeError("Completion messages must be an array of Message values.");
486
- }
487
- return messages;
488
- }
489
- function structuredOutputSchema(options) {
490
- return options.outputSchema;
491
- }
492
- function resultFromResponse(response, outputSchema) {
493
- const text = textFromAssistantContent(response.choice);
494
- const result = {
495
- output: outputSchema === void 0 ? text : parseCompletionOutput(text, outputSchema, response),
496
- text,
497
- content: [...response.choice],
498
- usage: response.usage,
499
- rawResponse: response.rawResponse
500
- };
501
- if (response.finishReason !== void 0) result.finishReason = response.finishReason;
502
- if (response.providerFinishReason !== void 0) {
503
- result.providerFinishReason = response.providerFinishReason;
504
- }
505
- if (response.contextUsage !== void 0) result.contextUsage = response.contextUsage;
506
- if (response.messageId !== void 0) result.messageId = response.messageId;
507
- if (response.sources !== void 0) result.sources = [...response.sources];
508
- if (response.providerToolCalls !== void 0) {
509
- result.providerToolCalls = [...response.providerToolCalls];
510
- }
511
- return result;
512
- }
513
- function parseCompletionOutput(text, schema, response) {
514
- if (response.finishReason === "length") {
515
- throw new CompletionStructuredOutputError({
516
- phase: "truncated",
517
- outputLength: text.length,
518
- usage: response.usage,
519
- finishReason: response.finishReason,
520
- providerFinishReason: response.providerFinishReason
521
- });
522
- }
523
- let json;
524
- try {
525
- json = JSON.parse(text);
526
- } catch (error) {
527
- throw new CompletionStructuredOutputError({
528
- phase: "parse",
529
- outputLength: text.length,
530
- usage: response.usage,
531
- finishReason: response.finishReason,
532
- providerFinishReason: response.providerFinishReason,
533
- cause: error
534
- });
535
- }
536
- try {
537
- return schema.parse(json);
538
- } catch (error) {
539
- throw new CompletionStructuredOutputError({
540
- phase: "schema",
541
- outputLength: text.length,
542
- usage: response.usage,
543
- finishReason: response.finishReason,
544
- providerFinishReason: response.providerFinishReason,
545
- cause: error
546
- });
547
- }
548
- }
549
- function modelCallOptions(abortSignal) {
550
- return abortSignal === void 0 ? void 0 : { abortSignal };
551
- }
552
- function isStreamingCompletionModel(model) {
553
- return typeof model.streamCompletion === "function";
554
- }
555
-
556
- export {
557
- reasoningDisplayText,
558
- isProviderTool,
559
- calculateContextUsage,
560
- withContextUsage,
561
- resolveModelContextLimits,
562
- Usage,
563
- getAssistantGenerationMetadata,
564
- CompletionCapabilityError,
565
- assertCompletionRequestSupported,
566
- textFromAssistantContent,
567
- createCompletionRequest,
568
- CompletionStructuredOutputError,
569
- generateCompletion,
570
- streamCompletion,
571
- isStreamingCompletionModel
572
- };
573
- //# sourceMappingURL=chunk-HT4P3SSG.js.map