orchajs 0.25.0 → 0.26.0

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.
@@ -34,6 +34,7 @@ const SUPPORTED_AMARSIA_PROVIDERS = /* @__PURE__ */ new Set([
34
34
  "vertexai",
35
35
  "deepseek"
36
36
  ]);
37
+ const TEXT_ONLY_AMARSIA_PROVIDERS = /* @__PURE__ */ new Set(["deepseek"]);
37
38
  function createAmarsiaInferenceGenerator(configuration) {
38
39
  const endpoint = (0, import_endpoint.resolveAmarsiaEndpoint)(configuration.endpoint);
39
40
  return (provider, _providerConfiguration, request) => generateThroughAmarsia(endpoint, configuration.key, provider, request);
@@ -56,6 +57,7 @@ async function generateThroughAmarsia(endpoint, key, provider, request) {
56
57
  `Amarsia Responses does not support "${request.outputType}" as a direct agent output.`
57
58
  );
58
59
  }
60
+ assertProviderSupportsMessageContent(provider, request.messages);
59
61
  const gatewayProvider = provider === "vertexai" ? "googlegenai" : provider;
60
62
  const body = {
61
63
  provider: gatewayProvider,
@@ -106,13 +108,47 @@ async function generateThroughAmarsia(endpoint, key, provider, request) {
106
108
  });
107
109
  if (!response.ok) {
108
110
  const message = await responseError(response);
109
- throw new Error(
110
- message ?? `Amarsia inference request failed with HTTP ${response.status}.`
111
- );
111
+ throw gatewayRequestError(message, response.status);
112
112
  }
113
113
  const responseBody = response.headers.get("content-type")?.includes("text/event-stream") ? await readGatewayStream(response, request.publishOutput) : await response.json();
114
114
  return toProviderResponse(request, responseBody);
115
115
  }
116
+ function assertProviderSupportsMessageContent(provider, messages) {
117
+ if (!TEXT_ONLY_AMARSIA_PROVIDERS.has(provider)) {
118
+ return;
119
+ }
120
+ for (const message of messages) {
121
+ if (message.role === "user" && Array.isArray(message.content)) {
122
+ for (const block of message.content) {
123
+ if (block.type !== "text") {
124
+ throw deepSeekMultimodalError(block.type);
125
+ }
126
+ }
127
+ }
128
+ if (message.role === "tool") {
129
+ for (const result of message.results) {
130
+ for (const block of result.contents ?? []) {
131
+ if (block.type !== "text") {
132
+ throw deepSeekMultimodalError(block.type);
133
+ }
134
+ }
135
+ }
136
+ }
137
+ }
138
+ }
139
+ function deepSeekMultimodalError(contentType) {
140
+ return new import_errors.OrchaError(
141
+ "unsupported_content_type",
142
+ `DeepSeek does not support multimodal content (received "${contentType}"). DeepSeek is text-only and cannot accept images, PDFs, or other file attachments.`
143
+ );
144
+ }
145
+ function gatewayRequestError(message, statusCode) {
146
+ const detail = message?.trim() || `Amarsia inference request failed with HTTP ${statusCode}.`;
147
+ if (/does not support multimodal|text-only/i.test(detail)) {
148
+ return new import_errors.OrchaError("unsupported_content_type", detail);
149
+ }
150
+ return new Error(detail);
151
+ }
116
152
  async function toResponsesInput(messages) {
117
153
  const input = [];
118
154
  for (const message of messages) {
@@ -127,10 +163,12 @@ async function toResponsesInput(messages) {
127
163
  }
128
164
  if (message.role === "tool") {
129
165
  for (const result of message.results) {
166
+ const contents = result.contents?.length ? await Promise.all(result.contents.map(toResponsesUserContent)) : void 0;
130
167
  input.push({
131
168
  type: "function_call_output",
132
169
  call_id: result.callId,
133
170
  output: JSON.stringify(result.output ?? null),
171
+ ...contents ? { contents } : {},
134
172
  ...result.isError ? { is_error: true } : {}
135
173
  });
136
174
  }
@@ -210,9 +248,8 @@ async function readGatewayStream(response, publishOutput) {
210
248
  terminal = event.response;
211
249
  } else if (event.type === "response.failed" || event.type === "error") {
212
250
  const error = isRecord(event.error) ? event.error : event;
213
- throw new Error(
214
- typeof error.message === "string" ? error.message : "Amarsia inference streaming request failed."
215
- );
251
+ const message = typeof error.message === "string" ? error.message : "Amarsia inference streaming request failed.";
252
+ throw gatewayRequestError(message, 500);
216
253
  }
217
254
  }
218
255
  if (!terminal) {
@@ -321,7 +358,7 @@ function normalizeUsage(usage) {
321
358
  async function responseError(response) {
322
359
  try {
323
360
  const body = await response.json();
324
- return body.error?.message ?? body.message;
361
+ return body.error?.message ?? body.message ?? (typeof body.detail === "string" ? body.detail : void 0);
325
362
  } catch {
326
363
  return void 0;
327
364
  }
@@ -388,7 +388,7 @@ environment. The complete pause/continue flow is:
388
388
  }
389
389
  5. Application executes every requested action.
390
390
  6. Application calls agent.resume(sessionId, {
391
- toolResults: [{ callId, output, isError? }]
391
+ toolResults: [{ callId, output, contents?, isError? }]
392
392
  }).
393
393
  7. Orcha validates every callId and output, stores the results, and continues
394
394
  the same paused run from its prior model context.
@@ -428,9 +428,9 @@ const result = await execution.result;
428
428
  const evaluations = await execution.evaluations;
429
429
  \`\`\`
430
430
 
431
- \`run()\` input fields:
431
+ \`run()\` and conversational \`resume()\` accept either a direct \`AgentContentInput\` (a string, a single content item, or an array of content items) or an object with \`content\` and optional fields:
432
432
 
433
- - \`content\` (required): a non-empty string, one content item, or an array.
433
+ - \`content\` (required when passing an options object): a non-empty string, one content item, or an array.
434
434
  Use \`{ filePath: "./document.pdf" }\` for a local file or
435
435
  \`{ url: "https://example.com/document.pdf" }\` for a remote file.
436
436
  \`mimeType\` is optional when it can be inferred from the extension. Durable
@@ -460,7 +460,11 @@ client actions in the application and submit every result:
460
460
  \`\`\`ts
461
461
  orcha.supportBot.resume(sessionId, {
462
462
  toolResults: [
463
- { callId: "call_123", output: { approved: true } }
463
+ {
464
+ callId: "call_123",
465
+ output: { approved: true },
466
+ contents: [{ filePath: "./signed-approval.pdf" }], // optional media/content items
467
+ }
464
468
  ],
465
469
  });
466
470
  \`\`\`
@@ -568,6 +572,11 @@ The local action signature is
568
572
  \`sessionId\`, immutable session \`metadata\`, a stable \`idempotencyKey\`,
569
573
  allowlisted \`env\`, guarded \`fetch\`, and prefixed \`log\`.
570
574
 
575
+ Actions can also return media or multimodal content back to the model alongside
576
+ their structured JSON output by returning \`{ output: jsonOutput, contents: [...] }\`.
577
+ \`contents\` accepts any \`AgentContentInput\` (such as \`{ filePath }\`,
578
+ \`{ url }\`, or content items). \`outputSchema\` validates \`output\` directly.
579
+
571
580
  ## Skills
572
581
 
573
582
  Skills are lazy-loaded procedural instructions. Direct child folders under
@@ -640,6 +640,7 @@ function parseToolResults(value) {
640
640
  return {
641
641
  callId: result.callId,
642
642
  output: result.output,
643
+ ...result.contents !== void 0 ? { contents: parseContent(result.contents) } : {},
643
644
  ...result.isError === true ? { isError: true } : {}
644
645
  };
645
646
  });
@@ -326,11 +326,15 @@ async function toAnthropicMessages(messages) {
326
326
  if (message.role === "tool") {
327
327
  return {
328
328
  role: "user",
329
- content: message.results.map((result) => ({
330
- type: "tool_result",
331
- tool_use_id: result.callId,
332
- content: JSON.stringify(result.output ?? null),
333
- ...result.isError ? { is_error: true } : {}
329
+ content: await Promise.all(message.results.map(async (result) => {
330
+ const serializedOutput = JSON.stringify(result.output ?? null);
331
+ const extraBlocks = result.contents?.length ? await Promise.all(result.contents.map(toAnthropicUserBlock)) : [];
332
+ return {
333
+ type: "tool_result",
334
+ tool_use_id: result.callId,
335
+ content: extraBlocks.length > 0 ? [{ type: "text", text: serializedOutput }, ...extraBlocks] : serializedOutput,
336
+ ...result.isError ? { is_error: true } : {}
337
+ };
334
338
  }))
335
339
  };
336
340
  }
@@ -339,64 +343,65 @@ async function toAnthropicMessages(messages) {
339
343
  }
340
344
  return {
341
345
  role: "user",
342
- content: await Promise.all(message.content.map(async (block) => {
343
- if (!isRecord(block) || typeof block.type !== "string") {
344
- throw new import_errors.OrchaError(
345
- "invalid_input",
346
- "Anthropic message content contains an invalid block."
347
- );
348
- }
349
- if (block.type === "text") {
350
- return block;
351
- }
352
- if (block.type === "file") {
353
- const file = await (0, import_file_content.readLocalFileContent)(block);
354
- if (block.mimeType.startsWith("image/")) {
355
- return {
356
- type: "image",
357
- source: {
358
- type: "base64",
359
- media_type: block.mimeType,
360
- data: file.base64
361
- }
362
- };
363
- }
364
- if (block.mimeType === "application/pdf") {
365
- return {
366
- type: "document",
367
- source: {
368
- type: "base64",
369
- media_type: block.mimeType,
370
- data: file.base64
371
- }
372
- };
373
- }
374
- }
375
- if (block.type === "image" && typeof block.fileUri === "string") {
376
- return {
377
- type: "image",
378
- source: {
379
- type: "url",
380
- url: block.fileUri
381
- }
382
- };
346
+ content: await Promise.all(message.content.map(toAnthropicUserBlock))
347
+ };
348
+ }));
349
+ }
350
+ async function toAnthropicUserBlock(block) {
351
+ if (!isRecord(block) || typeof block.type !== "string") {
352
+ throw new import_errors.OrchaError(
353
+ "invalid_input",
354
+ "Anthropic message content contains an invalid block."
355
+ );
356
+ }
357
+ if (block.type === "text") {
358
+ return block;
359
+ }
360
+ if (block.type === "file") {
361
+ const file = await (0, import_file_content.readLocalFileContent)(block);
362
+ if (block.mimeType.startsWith("image/")) {
363
+ return {
364
+ type: "image",
365
+ source: {
366
+ type: "base64",
367
+ media_type: block.mimeType,
368
+ data: file.base64
383
369
  }
384
- if (block.type === "url" && typeof block.fileUri === "string" && block.mimeType === "application/pdf") {
385
- return {
386
- type: "document",
387
- source: {
388
- type: "url",
389
- url: block.fileUri
390
- }
391
- };
370
+ };
371
+ }
372
+ if (block.mimeType === "application/pdf") {
373
+ return {
374
+ type: "document",
375
+ source: {
376
+ type: "base64",
377
+ media_type: block.mimeType,
378
+ data: file.base64
392
379
  }
393
- throw new import_errors.OrchaError(
394
- "unsupported_content_type",
395
- `Anthropic does not support content type "${block.type}" with MIME type "${String(block.mimeType)}".`
396
- );
397
- }))
380
+ };
381
+ }
382
+ }
383
+ if (block.type === "image" && typeof block.fileUri === "string") {
384
+ return {
385
+ type: "image",
386
+ source: {
387
+ type: "url",
388
+ url: block.fileUri
389
+ }
398
390
  };
399
- }));
391
+ }
392
+ if (block.type === "url" && typeof block.fileUri === "string" && block.mimeType === "application/pdf") {
393
+ return {
394
+ type: "document",
395
+ source: {
396
+ type: "url",
397
+ url: block.fileUri
398
+ }
399
+ };
400
+ }
401
+ throw new import_errors.OrchaError(
402
+ "unsupported_content_type",
403
+ `Anthropic does not support content type "${block.type}" with MIME type "${String(block.mimeType)}".`
404
+ );
400
405
  }
401
406
  function isRecord(value) {
402
407
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -126,15 +126,23 @@ async function toBedrockMessages(messages) {
126
126
  if (message.role === "tool") {
127
127
  output.push({
128
128
  role: "user",
129
- content: message.results.map((result) => ({
130
- toolResult: {
131
- toolUseId: result.callId,
132
- content: [
133
- { json: toBedrockDocument(result.output) }
134
- ],
135
- ...result.isError ? { status: "error" } : {}
136
- }
137
- }))
129
+ content: await Promise.all(
130
+ message.results.map(async (result) => {
131
+ const extraBlocks = result.contents?.length ? await Promise.all(
132
+ result.contents.map(toBedrockUserBlock)
133
+ ) : [];
134
+ return {
135
+ toolResult: {
136
+ toolUseId: result.callId,
137
+ content: [
138
+ { json: toBedrockDocument(result.output) },
139
+ ...extraBlocks
140
+ ],
141
+ ...result.isError ? { status: "error" } : {}
142
+ }
143
+ };
144
+ })
145
+ )
138
146
  });
139
147
  continue;
140
148
  }
@@ -118,10 +118,18 @@ function toDeepSeekMessages(messages) {
118
118
  }
119
119
  if (message.role === "tool") {
120
120
  for (const result of message.results) {
121
+ let text2 = JSON.stringify(result.output ?? null);
122
+ if (result.contents && result.contents.length > 0) {
123
+ const extraText = textOnlyContent(result.contents);
124
+ if (extraText) {
125
+ text2 = `${text2}
126
+ ${extraText}`;
127
+ }
128
+ }
121
129
  output.push({
122
130
  role: "tool",
123
131
  tool_call_id: result.callId,
124
- content: JSON.stringify(result.output ?? null)
132
+ content: text2
125
133
  });
126
134
  }
127
135
  continue;
@@ -164,7 +172,7 @@ function textOnlyContent(content) {
164
172
  if (block.type !== "text") {
165
173
  throw new import_errors.OrchaError(
166
174
  "unsupported_content_type",
167
- `DeepSeek does not support "${block.type}" message content.`
175
+ `DeepSeek does not support multimodal content (received "${block.type}"). DeepSeek is text-only and cannot accept images, PDFs, or other file attachments.`
168
176
  );
169
177
  }
170
178
  text.push(block.text);
@@ -202,7 +202,7 @@ async function toGoogleContents(messages, provider) {
202
202
  continue;
203
203
  }
204
204
  if (message.role === "assistant") {
205
- const parts = message.content.map((block) => {
205
+ const parts2 = message.content.map((block) => {
206
206
  if (block.type === "tool_call") {
207
207
  previousCalls.set(block.callId, {
208
208
  name: block.name,
@@ -215,29 +215,36 @@ async function toGoogleContents(messages, provider) {
215
215
  provider
216
216
  );
217
217
  });
218
- contents.push({ role: "model", parts });
218
+ contents.push({ role: "model", parts: parts2 });
219
219
  continue;
220
220
  }
221
+ const parts = [];
222
+ for (const result of message.results) {
223
+ const call = previousCalls.get(result.callId);
224
+ if (!call) {
225
+ throw new Error(
226
+ `Google Gemini could not match tool result "${result.callId}" to a prior function call.`
227
+ );
228
+ }
229
+ parts.push({
230
+ functionResponse: {
231
+ name: call.name,
232
+ ...call.providerCallId ? { id: call.providerCallId } : {},
233
+ response: normalizeFunctionResponse(
234
+ result.output,
235
+ result.isError
236
+ )
237
+ }
238
+ });
239
+ if (result.contents && result.contents.length > 0) {
240
+ parts.push(
241
+ ...await Promise.all(result.contents.map(toGoogleUserPart))
242
+ );
243
+ }
244
+ }
221
245
  contents.push({
222
246
  role: "user",
223
- parts: message.results.map((result) => {
224
- const call = previousCalls.get(result.callId);
225
- if (!call) {
226
- throw new Error(
227
- `Google Gemini could not match tool result "${result.callId}" to a prior function call.`
228
- );
229
- }
230
- return {
231
- functionResponse: {
232
- name: call.name,
233
- ...call.providerCallId ? { id: call.providerCallId } : {},
234
- response: normalizeFunctionResponse(
235
- result.output,
236
- result.isError
237
- )
238
- }
239
- };
240
- })
247
+ parts
241
248
  });
242
249
  }
243
250
  return contents;
@@ -115,6 +115,14 @@ async function toOpenAIInput(messages) {
115
115
  call_id: result.callId,
116
116
  output: JSON.stringify(result.output ?? null)
117
117
  });
118
+ if (result.contents && result.contents.length > 0) {
119
+ input.push({
120
+ role: "user",
121
+ content: await Promise.all(
122
+ result.contents.map(toOpenAIUserContent)
123
+ )
124
+ });
125
+ }
118
126
  }
119
127
  continue;
120
128
  }