orchajs 0.27.1 → 0.27.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ export declare class AmarsiaTelemetryClient {
7
7
  endpoint?: string;
8
8
  });
9
9
  enqueue(fact: AmarsiaRunFact): void;
10
+ updateActiveRun(fact: AmarsiaRunFact): void;
10
11
  /** @internal Tracks telemetry projection work that must settle before drain completes. */
11
12
  trackProjection(projection: Promise<void>): void;
12
13
  drain(options?: {
@@ -7,20 +7,28 @@ const RELEVANT_EVENTS = new Set([
7
7
  "evaluation.completed",
8
8
  "evaluation.failed",
9
9
  ]);
10
+ const PROGRESS_EVENTS = new Set([
11
+ "message.created",
12
+ "action.completed",
13
+ "action.failed",
14
+ "subagent.completed",
15
+ "subagent.failed",
16
+ ]);
10
17
  const MAX_BATCH_RUNS = 100;
11
18
  const MAX_KEEPALIVE_BYTES = 48 * 1024;
12
19
  const HEARTBEAT_INTERVAL_MS = 30_000;
13
- const EVALUATION_FLUSH_TIMEOUT_MS = 5_000;
20
+ const MAX_CONCURRENT_FLUSHES = 2;
14
21
  export class AmarsiaTelemetryClient {
15
22
  #key;
16
23
  #baseUrl;
17
24
  #queue = new Map();
18
25
  #activeRuns = new Map();
26
+ #terminalRuns = new Set();
19
27
  #pendingProjections = new Set();
28
+ #inFlightFlushes = new Set();
20
29
  #timer;
21
30
  #heartbeatTimer;
22
31
  #attempt = 0;
23
- #flushPromise;
24
32
  constructor(options) {
25
33
  this.#key = options.key;
26
34
  this.#baseUrl = resolveAmarsiaEndpoint(options.endpoint);
@@ -28,25 +36,38 @@ export class AmarsiaTelemetryClient {
28
36
  enqueue(fact) {
29
37
  this.#queue.set(fact.event_id, fact);
30
38
  if (fact.status === "running") {
39
+ this.#terminalRuns.delete(fact.event_id);
31
40
  this.#activeRuns.set(fact.event_id, fact);
32
41
  this.#scheduleHeartbeat();
33
- }
34
- else {
35
- this.#activeRuns.delete(fact.event_id);
36
- if (this.#activeRuns.size === 0 && this.#heartbeatTimer) {
37
- clearTimeout(this.#heartbeatTimer);
38
- this.#heartbeatTimer = undefined;
42
+ if (this.#queue.size >= 20) {
43
+ void this.#flush();
44
+ return;
39
45
  }
40
- }
41
- if (fact.status !== "running") {
42
- this.#scheduleTerminal();
46
+ this.#schedule(250);
43
47
  return;
44
48
  }
45
- if (this.#queue.size >= 20) {
46
- void this.#flush();
49
+ this.#terminalRuns.add(fact.event_id);
50
+ this.#activeRuns.delete(fact.event_id);
51
+ if (this.#activeRuns.size === 0 && this.#heartbeatTimer) {
52
+ clearTimeout(this.#heartbeatTimer);
53
+ this.#heartbeatTimer = undefined;
54
+ }
55
+ if (this.#timer) {
56
+ clearTimeout(this.#timer);
57
+ this.#timer = undefined;
58
+ }
59
+ void this.#flush();
60
+ }
61
+ updateActiveRun(fact) {
62
+ if (fact.status !== "running" || this.#terminalRuns.has(fact.event_id)) {
47
63
  return;
48
64
  }
49
- this.#schedule(250);
65
+ if (this.#activeRuns.has(fact.event_id)) {
66
+ this.#activeRuns.set(fact.event_id, fact);
67
+ }
68
+ if (this.#queue.has(fact.event_id)) {
69
+ this.#queue.set(fact.event_id, fact);
70
+ }
50
71
  }
51
72
  #schedule(delay) {
52
73
  if (this.#timer)
@@ -65,21 +86,15 @@ export class AmarsiaTelemetryClient {
65
86
  this.#pendingProjections.delete(projection);
66
87
  });
67
88
  }
68
- #scheduleTerminal() {
69
- if (this.#timer)
70
- clearTimeout(this.#timer);
71
- this.#timer = setTimeout(() => {
72
- this.#timer = undefined;
73
- void this.#flush();
74
- }, 0);
75
- }
76
89
  #scheduleHeartbeat() {
77
90
  if (this.#heartbeatTimer || this.#activeRuns.size === 0)
78
91
  return;
79
92
  this.#heartbeatTimer = setTimeout(() => {
80
93
  this.#heartbeatTimer = undefined;
81
94
  for (const fact of this.#activeRuns.values()) {
82
- this.#queue.set(fact.event_id, fact);
95
+ if (!this.#terminalRuns.has(fact.event_id)) {
96
+ this.#queue.set(fact.event_id, fact);
97
+ }
83
98
  }
84
99
  void this.#flush();
85
100
  this.#scheduleHeartbeat();
@@ -92,7 +107,7 @@ export class AmarsiaTelemetryClient {
92
107
  const deadline = Date.now() + timeoutMs;
93
108
  while (this.#pendingProjections.size > 0 ||
94
109
  this.#queue.size > 0 ||
95
- this.#flushPromise) {
110
+ this.#inFlightFlushes.size > 0) {
96
111
  const remaining = deadline - Date.now();
97
112
  if (remaining <= 0)
98
113
  return;
@@ -107,10 +122,19 @@ export class AmarsiaTelemetryClient {
107
122
  clearTimeout(this.#timer);
108
123
  this.#timer = undefined;
109
124
  }
110
- await Promise.race([
111
- this.#flush(),
112
- new Promise((resolve) => setTimeout(resolve, remaining)),
113
- ]);
125
+ if (this.#queue.size > 0) {
126
+ await Promise.race([
127
+ this.#flush(),
128
+ new Promise((resolve) => setTimeout(resolve, remaining)),
129
+ ]);
130
+ continue;
131
+ }
132
+ if (this.#inFlightFlushes.size > 0) {
133
+ await Promise.race([
134
+ Promise.allSettled([...this.#inFlightFlushes]).then(() => undefined),
135
+ new Promise((resolve) => setTimeout(resolve, remaining)),
136
+ ]);
137
+ }
114
138
  }
115
139
  }
116
140
  async close(options = {}) {
@@ -127,22 +151,31 @@ export class AmarsiaTelemetryClient {
127
151
  this.#activeRuns.clear();
128
152
  }
129
153
  async #flush() {
130
- if (this.#flushPromise)
131
- return this.#flushPromise;
132
- if (this.#queue.size === 0)
154
+ if (this.#queue.size === 0) {
155
+ if (this.#inFlightFlushes.size > 0) {
156
+ await Promise.allSettled([...this.#inFlightFlushes]);
157
+ }
158
+ return;
159
+ }
160
+ const hasTerminalFact = [...this.#queue.values()].some((fact) => fact.status !== "running");
161
+ const maxConcurrent = hasTerminalFact ? MAX_CONCURRENT_FLUSHES : 1;
162
+ if (this.#inFlightFlushes.size >= maxConcurrent) {
163
+ await Promise.allSettled([...this.#inFlightFlushes]);
133
164
  return;
134
- this.#flushPromise = this.#performFlush();
165
+ }
166
+ const flushPromise = this.#performFlush();
167
+ this.#inFlightFlushes.add(flushPromise);
135
168
  try {
136
- await this.#flushPromise;
169
+ await flushPromise;
137
170
  }
138
171
  finally {
139
- this.#flushPromise = undefined;
172
+ this.#inFlightFlushes.delete(flushPromise);
140
173
  if (this.#queue.size > 0 && !this.#timer) {
141
- const hasTerminalFact = [...this.#queue.values()].some((fact) => fact.status !== "running");
142
- if (hasTerminalFact) {
174
+ const remainingHasTerminal = [...this.#queue.values()].some((fact) => fact.status !== "running");
175
+ if (remainingHasTerminal) {
143
176
  void this.#flush();
144
177
  }
145
- else {
178
+ else if (this.#inFlightFlushes.size === 0) {
146
179
  this.#schedule(250);
147
180
  }
148
181
  }
@@ -177,6 +210,10 @@ export class AmarsiaTelemetryClient {
177
210
  this.#attempt += 1;
178
211
  if (this.#attempt <= 6) {
179
212
  for (const fact of batch) {
213
+ if (fact.status === "running" &&
214
+ this.#terminalRuns.has(fact.event_id)) {
215
+ continue;
216
+ }
180
217
  if (!this.#queue.has(fact.event_id)) {
181
218
  this.#queue.set(fact.event_id, fact);
182
219
  }
@@ -215,29 +252,67 @@ export function withAmarsiaTelemetry(options) {
215
252
  async append(sessionId, events) {
216
253
  await store.append(sessionId, events);
217
254
  const runs = relevantRuns(events);
255
+ const progressRuns = progressOnlyRuns(events, runs);
256
+ const hasSessionCreated = events.some((event) => event.type === "session.created");
218
257
  if (runs.size === 0 &&
258
+ progressRuns.size === 0 &&
259
+ !hasSessionCreated &&
219
260
  !projections.has(sessionId) &&
220
261
  !pending.has(sessionId)) {
221
262
  return;
222
263
  }
264
+ if (!pending.has(sessionId)) {
265
+ let projection = projections.get(sessionId);
266
+ let hasNewEvents = false;
267
+ if (!projection && hasSessionCreated) {
268
+ projection = createProjection(events);
269
+ projections.set(sessionId, projection);
270
+ hasNewEvents = projection.seenSequences.size > 0;
271
+ }
272
+ else if (projection) {
273
+ hasNewEvents = appendToProjection(projection, events);
274
+ }
275
+ if (projection) {
276
+ if (projection.isTest || !hasNewEvents)
277
+ return;
278
+ for (const run of runs) {
279
+ const fact = projectRunFact(manifest, sessionId, run, projection);
280
+ if (fact)
281
+ client.enqueue(fact);
282
+ }
283
+ for (const run of progressRuns) {
284
+ const fact = projectRunFact(manifest, sessionId, run, projection);
285
+ if (fact)
286
+ client.updateActiveRun(fact);
287
+ }
288
+ return;
289
+ }
290
+ }
223
291
  const previous = pending.get(sessionId) ?? Promise.resolve();
224
292
  const processing = previous
225
293
  .then(async () => {
226
294
  let projection = projections.get(sessionId);
295
+ let hasNewEvents = false;
227
296
  if (!projection) {
228
297
  projection = createProjection(await store.read(sessionId));
229
298
  projections.set(sessionId, projection);
299
+ hasNewEvents = projection.seenSequences.size > 0;
230
300
  }
231
301
  else {
232
- appendToProjection(projection, events);
302
+ hasNewEvents = appendToProjection(projection, events);
233
303
  }
234
- if (projection.isTest)
304
+ if (projection.isTest || !hasNewEvents)
235
305
  return;
236
306
  for (const run of runs) {
237
307
  const fact = projectRunFact(manifest, sessionId, run, projection);
238
308
  if (fact)
239
309
  client.enqueue(fact);
240
310
  }
311
+ for (const run of progressRuns) {
312
+ const fact = projectRunFact(manifest, sessionId, run, projection);
313
+ if (fact)
314
+ client.updateActiveRun(fact);
315
+ }
241
316
  })
242
317
  .catch(() => undefined);
243
318
  pending.set(sessionId, processing);
@@ -247,17 +322,6 @@ export function withAmarsiaTelemetry(options) {
247
322
  pending.delete(sessionId);
248
323
  }
249
324
  });
250
- const hasTerminalRunEvent = events.some((event) => event.type === "run.completed" ||
251
- event.type === "run.failed" ||
252
- event.type === "run.paused");
253
- const hasEvaluationResult = events.some((event) => event.type === "evaluation.completed" ||
254
- event.type === "evaluation.failed");
255
- if (hasTerminalRunEvent || hasEvaluationResult) {
256
- await processing;
257
- }
258
- if (hasEvaluationResult) {
259
- await client.drain({ timeoutMs: EVALUATION_FLUSH_TIMEOUT_MS });
260
- }
261
325
  },
262
326
  };
263
327
  }
@@ -271,10 +335,12 @@ function createProjection(events) {
271
335
  return projection;
272
336
  }
273
337
  function appendToProjection(projection, events) {
338
+ let added = false;
274
339
  for (const event of events) {
275
340
  if (projection.seenSequences.has(event.sequence))
276
341
  continue;
277
342
  projection.seenSequences.add(event.sequence);
343
+ added = true;
278
344
  if (event.type === "session.created") {
279
345
  projection.sessionCreated = event;
280
346
  const metadata = asRecord(event.data.metadata);
@@ -290,6 +356,7 @@ function appendToProjection(projection, events) {
290
356
  projection.runs.set(event.run, [event]);
291
357
  }
292
358
  }
359
+ return added;
293
360
  }
294
361
  function relevantRuns(events) {
295
362
  const runs = new Set();
@@ -300,6 +367,17 @@ function relevantRuns(events) {
300
367
  }
301
368
  return runs;
302
369
  }
370
+ function progressOnlyRuns(events, relevant) {
371
+ const runs = new Set();
372
+ for (const event of events) {
373
+ if (typeof event.run === "number" &&
374
+ !relevant.has(event.run) &&
375
+ PROGRESS_EVENTS.has(event.type)) {
376
+ runs.add(event.run);
377
+ }
378
+ }
379
+ return runs;
380
+ }
303
381
  function projectRunFact(manifest, sessionId, run, projection) {
304
382
  const runEvents = projection.runs.get(run);
305
383
  if (!runEvents)
@@ -1,7 +1,7 @@
1
1
  import { OrchaError } from "../errors.js";
2
2
  import { readLocalFileContent } from "../file-content.js";
3
3
  import { readServerSentEvents } from "../providers/sse.js";
4
- import { parseStructuredOutput } from "../runtime/structured-output.js";
4
+ import { resolveProviderStructuredOutput } from "../runtime/structured-output.js";
5
5
  import { resolveAmarsiaEndpoint } from "./endpoint.js";
6
6
  const SUPPORTED_AMARSIA_PROVIDERS = new Set([
7
7
  "openai",
@@ -309,22 +309,17 @@ function toProviderResponse(request, response) {
309
309
  .map((block) => block.text)
310
310
  .join("");
311
311
  const stopReason = normalizeStopReason(response, toolCalls, refused);
312
- const output = request.outputType === "json" &&
313
- toolCalls.length === 0 &&
314
- stopReason !== "max_tokens"
315
- ? parseStructuredOutput(textOutput, request.outputSchema ?? {})
316
- : textOutput;
317
312
  if (textOutput) {
318
313
  request.publishOutput?.(textOutput);
319
314
  }
320
- return {
321
- output,
315
+ return resolveProviderStructuredOutput(request, {
316
+ output: textOutput,
322
317
  responseId: response.id,
323
318
  stopReason,
324
319
  content,
325
320
  toolCalls,
326
321
  usage: normalizeUsage(response.usage),
327
- };
322
+ }, textOutput);
328
323
  }
329
324
  function replay(item) {
330
325
  const value = {
package/dist/errors.d.ts CHANGED
@@ -9,8 +9,10 @@ export declare class OrchaError extends Error {
9
9
  cause?: unknown;
10
10
  });
11
11
  }
12
+ export declare function createProviderHttpError(response: Response, providerLabel: string): Promise<Error>;
12
13
  export declare function describeProviderError(error: unknown): {
13
14
  message: string;
15
+ retryable: boolean;
14
16
  details?: Record<string, unknown>;
15
17
  };
16
18
  export declare function isRetryableProviderConnectionError(error: unknown): boolean;
package/dist/errors.js CHANGED
@@ -10,22 +10,92 @@ export class OrchaError extends Error {
10
10
  this.details = options.details;
11
11
  }
12
12
  }
13
+ const NON_RETRYABLE_STATUS_CODES = new Set([400, 401, 403, 404, 413, 422]);
14
+ const NON_RETRYABLE_ERROR_NAMES = new Set([
15
+ "ValidationException",
16
+ "AccessDeniedException",
17
+ "ResourceNotFoundException",
18
+ "UnrecognizedClientException",
19
+ "InvalidSignatureException",
20
+ "ExpiredTokenException",
21
+ ]);
22
+ export async function createProviderHttpError(response, providerLabel) {
23
+ const status = response.status;
24
+ const requestId = response.headers.get("request-id") ??
25
+ response.headers.get("x-request-id") ??
26
+ response.headers.get("x-amzn-requestid") ??
27
+ undefined;
28
+ let rawText = "";
29
+ try {
30
+ rawText = (await response.text()).trim();
31
+ }
32
+ catch {
33
+ rawText = "";
34
+ }
35
+ let parsedMessage;
36
+ let errorCode;
37
+ if (rawText) {
38
+ try {
39
+ const parsed = JSON.parse(rawText);
40
+ const nestedError = typeof parsed.error === "object" && parsed.error !== null
41
+ ? parsed.error
42
+ : undefined;
43
+ if (typeof nestedError?.message === "string" && nestedError.message.trim()) {
44
+ parsedMessage = nestedError.message.trim();
45
+ }
46
+ else if (typeof parsed.message === "string" && parsed.message.trim()) {
47
+ parsedMessage = parsed.message.trim();
48
+ }
49
+ else if (typeof parsed.detail === "string" && parsed.detail.trim()) {
50
+ parsedMessage = parsed.detail.trim();
51
+ }
52
+ errorCode =
53
+ stringOrNumberField(nestedError, "code") ??
54
+ stringOrNumberField(nestedError, "type") ??
55
+ stringOrNumberField(parsed, "code");
56
+ }
57
+ catch {
58
+ const compact = rawText.replace(/\s+/g, " ").trim();
59
+ if (compact) {
60
+ parsedMessage =
61
+ compact.length > 300 ? `${compact.slice(0, 300)}...` : compact;
62
+ }
63
+ }
64
+ }
65
+ const message = parsedMessage ?? `${providerLabel} request failed with HTTP ${status}.`;
66
+ const error = new Error(message);
67
+ error.statusCode = status;
68
+ if (requestId) {
69
+ error.requestId = requestId;
70
+ }
71
+ if (errorCode !== undefined) {
72
+ error.code = errorCode;
73
+ }
74
+ return error;
75
+ }
13
76
  export function describeProviderError(error) {
14
77
  const rootMessage = errorMessage(error);
78
+ const statusCode = extractStatusCode(error);
79
+ const requestId = extractRequestId(error);
80
+ const errorName = extractErrorName(error);
81
+ const errorCode = stringOrNumberField(error, "code") ??
82
+ stringOrNumberField(error, "type");
83
+ const retryable = isProviderErrorRetryable(statusCode, errorName);
15
84
  const cause = deepestUsefulCause(error);
16
- if (!cause) {
17
- return { message: rootMessage };
18
- }
19
- const causeMessage = errorMessage(cause);
20
- const causeCode = stringOrNumberField(cause, "code");
21
- const syscall = stringField(cause, "syscall");
22
- const hostname = stringField(cause, "hostname");
23
- const explanation = causeMessage !== rootMessage
85
+ const causeMessage = cause ? errorMessage(cause) : undefined;
86
+ const causeCode = cause ? stringOrNumberField(cause, "code") : undefined;
87
+ const syscall = cause ? stringField(cause, "syscall") : undefined;
88
+ const hostname = cause ? stringField(cause, "hostname") : undefined;
89
+ const explanation = causeMessage && causeMessage !== rootMessage
24
90
  ? causeMessage
25
- : causeCode
91
+ : causeCode !== undefined
26
92
  ? String(causeCode)
27
93
  : undefined;
28
94
  const details = {
95
+ ...(statusCode !== undefined ? { statusCode } : {}),
96
+ ...(requestId ? { requestId } : {}),
97
+ ...(errorName ? { errorName } : {}),
98
+ ...(errorCode !== undefined ? { errorCode } : {}),
29
99
  ...(causeCode !== undefined ? { causeCode } : {}),
30
100
  ...(syscall ? { syscall } : {}),
31
101
  ...(hostname ? { hostname } : {}),
@@ -33,6 +103,7 @@ export function describeProviderError(error) {
33
103
  };
34
104
  return {
35
105
  message: explanation ? `${rootMessage}: ${explanation}` : rootMessage,
106
+ retryable,
36
107
  ...(Object.keys(details).length > 0 ? { details } : {}),
37
108
  };
38
109
  }
@@ -66,6 +137,54 @@ export function isRetryableProviderConnectionError(error) {
66
137
  }
67
138
  return false;
68
139
  }
140
+ function isProviderErrorRetryable(statusCode, errorName) {
141
+ if (errorName && NON_RETRYABLE_ERROR_NAMES.has(errorName)) {
142
+ return false;
143
+ }
144
+ if (statusCode !== undefined && NON_RETRYABLE_STATUS_CODES.has(statusCode)) {
145
+ return false;
146
+ }
147
+ return true;
148
+ }
149
+ function extractStatusCode(error) {
150
+ if (typeof error !== "object" || error === null)
151
+ return undefined;
152
+ const record = error;
153
+ if (typeof record.statusCode === "number")
154
+ return record.statusCode;
155
+ if (typeof record.status === "number")
156
+ return record.status;
157
+ const metadata = typeof record.$metadata === "object" && record.$metadata !== null
158
+ ? record.$metadata
159
+ : undefined;
160
+ if (typeof metadata?.httpStatusCode === "number") {
161
+ return metadata.httpStatusCode;
162
+ }
163
+ return undefined;
164
+ }
165
+ function extractRequestId(error) {
166
+ if (typeof error !== "object" || error === null)
167
+ return undefined;
168
+ const record = error;
169
+ if (typeof record.requestId === "string" && record.requestId.trim()) {
170
+ return record.requestId.trim();
171
+ }
172
+ const metadata = typeof record.$metadata === "object" && record.$metadata !== null
173
+ ? record.$metadata
174
+ : undefined;
175
+ if (typeof metadata?.requestId === "string" && metadata.requestId.trim()) {
176
+ return metadata.requestId.trim();
177
+ }
178
+ return undefined;
179
+ }
180
+ function extractErrorName(error) {
181
+ if (!(error instanceof Error))
182
+ return undefined;
183
+ if (error.name && error.name !== "Error" && error.name !== "OrchaError") {
184
+ return error.name;
185
+ }
186
+ return undefined;
187
+ }
69
188
  function deepestUsefulCause(error) {
70
189
  let current = error;
71
190
  let useful;
@@ -1,3 +1,4 @@
1
+ import { OrchaError } from "./errors.js";
1
2
  const MIME_TYPES = {
2
3
  ".csv": "text/csv",
3
4
  ".doc": "application/msword",
@@ -47,7 +48,18 @@ export function normalizeUrlContent(url, mimeType) {
47
48
  export async function readLocalFileContent(content) {
48
49
  const fsModule = "node:fs/promises";
49
50
  const { readFile } = await import(fsModule);
50
- const bytes = await readFile(content.filePath);
51
+ let bytes;
52
+ try {
53
+ bytes = await readFile(content.filePath);
54
+ }
55
+ catch (error) {
56
+ const reason = error instanceof Error ? error.message : String(error);
57
+ throw new OrchaError("invalid_input", `Failed to read local file "${content.filePath}": ${reason}`, {
58
+ retryable: false,
59
+ cause: error,
60
+ details: { filePath: content.filePath },
61
+ });
62
+ }
51
63
  const base64 = bytes.toString("base64");
52
64
  return {
53
65
  bytes,
@@ -1,6 +1,6 @@
1
- import { OrchaError } from "../errors.js";
1
+ import { createProviderHttpError, OrchaError } from "../errors.js";
2
2
  import { readLocalFileContent } from "../file-content.js";
3
- import { parseStructuredOutput } from "../runtime/structured-output.js";
3
+ import { resolveProviderStructuredOutput } from "../runtime/structured-output.js";
4
4
  import { readServerSentEvents } from "./sse.js";
5
5
  export const anthropicProvider = {
6
6
  name: "anthropic",
@@ -49,8 +49,7 @@ async function generateAnthropic(provider, request) {
49
49
  signal: request.signal,
50
50
  });
51
51
  if (!response.ok) {
52
- const body = (await response.json());
53
- throw new Error(body.error?.message ?? `Anthropic request failed with HTTP ${response.status}.`);
52
+ throw await createProviderHttpError(response, "Anthropic");
54
53
  }
55
54
  const body = response.headers
56
55
  .get("content-type")
@@ -67,16 +66,11 @@ function toProviderResponse(request, body) {
67
66
  .join("");
68
67
  const toolCalls = content.filter((item) => item.type === "tool_call");
69
68
  const stopReason = normalizeStopReason(body.stop_reason);
70
- const output = request.outputType === "json" &&
71
- toolCalls.length === 0 &&
72
- stopReason !== "max_tokens"
73
- ? parseStructuredOutput(textOutput, request.outputSchema ?? {})
74
- : textOutput;
75
69
  if (textOutput && request.publishOutput) {
76
70
  request.publishOutput(textOutput);
77
71
  }
78
- return {
79
- output,
72
+ return resolveProviderStructuredOutput(request, {
73
+ output: textOutput,
80
74
  responseId: body.id,
81
75
  stopReason,
82
76
  content,
@@ -88,7 +82,7 @@ function toProviderResponse(request, body) {
88
82
  cacheReadTokens: body.usage?.cache_read_input_tokens ?? 0,
89
83
  cacheWriteTokens: body.usage?.cache_creation_input_tokens ?? 0,
90
84
  },
91
- };
85
+ }, textOutput);
92
86
  }
93
87
  function normalizeStopReason(reason) {
94
88
  if (reason === undefined || reason === null) {
@@ -3,7 +3,7 @@ import { createHash } from "node:crypto";
3
3
  import { basename, extname } from "node:path";
4
4
  import { OrchaError } from "../errors.js";
5
5
  import { readLocalFileContent } from "../file-content.js";
6
- import { parseStructuredOutput } from "../runtime/structured-output.js";
6
+ import { resolveProviderStructuredOutput } from "../runtime/structured-output.js";
7
7
  export const bedrockProvider = {
8
8
  name: "bedrock",
9
9
  generate: generateBedrock,
@@ -455,12 +455,8 @@ function toProviderResponse(request, response) {
455
455
  .join("");
456
456
  const toolCalls = response.blocks.filter((block) => block.type === "tool_call");
457
457
  const stopReason = normalizeStopReason(response.stopReason);
458
- return {
459
- output: request.outputType === "json" &&
460
- toolCalls.length === 0 &&
461
- stopReason !== "max_tokens"
462
- ? parseStructuredOutput(textOutput, request.outputSchema ?? {})
463
- : textOutput,
458
+ return resolveProviderStructuredOutput(request, {
459
+ output: textOutput,
464
460
  responseId: response.requestId,
465
461
  stopReason,
466
462
  content: response.blocks,
@@ -472,7 +468,7 @@ function toProviderResponse(request, response) {
472
468
  cacheReadTokens: response.usage?.cacheReadInputTokens ?? 0,
473
469
  cacheWriteTokens: response.usage?.cacheWriteInputTokens ?? 0,
474
470
  },
475
- };
471
+ }, textOutput);
476
472
  }
477
473
  function normalizeStopReason(reason) {
478
474
  if (!reason) {
@@ -1,5 +1,5 @@
1
- import { OrchaError } from "../errors.js";
2
- import { parseStructuredOutput } from "../runtime/structured-output.js";
1
+ import { createProviderHttpError, OrchaError } from "../errors.js";
2
+ import { resolveProviderStructuredOutput } from "../runtime/structured-output.js";
3
3
  import { readServerSentEvents } from "./sse.js";
4
4
  export const deepSeekProvider = {
5
5
  name: "deepseek",
@@ -54,9 +54,7 @@ async function generateDeepSeek(configuration, request) {
54
54
  signal: request.signal,
55
55
  });
56
56
  if (!response.ok) {
57
- const error = (await response.json());
58
- throw new Error(error.error?.message ??
59
- `DeepSeek request failed with HTTP ${response.status}.`);
57
+ throw await createProviderHttpError(response, "DeepSeek");
60
58
  }
61
59
  const collected = await collectDeepSeekStream(response, request.publishOutput);
62
60
  return toProviderResponse(request, collected);
@@ -214,19 +212,14 @@ function toProviderResponse(request, response) {
214
212
  }
215
213
  const toolCalls = content.filter((block) => block.type === "tool_call");
216
214
  const stopReason = normalizeStopReason(response.finishReason, toolCalls);
217
- const output = request.outputType === "json" &&
218
- toolCalls.length === 0 &&
219
- stopReason !== "max_tokens"
220
- ? parseStructuredOutput(response.content, request.outputSchema ?? {})
221
- : response.content;
222
- return {
223
- output,
215
+ return resolveProviderStructuredOutput(request, {
216
+ output: response.content,
224
217
  responseId: response.id,
225
218
  stopReason,
226
219
  content,
227
220
  toolCalls,
228
221
  usage: normalizeUsage(response.usage),
229
- };
222
+ }, response.content);
230
223
  }
231
224
  function parseToolArguments(value, toolName) {
232
225
  let parsed;