mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 12:05:34 +08:00
fix(sdk): keep Vercel AI SDK tool-result output kinds through compression (#4670)
## Description
`headroomMiddleware` / `withHeadroom` convert the Vercel AI SDK prompt
to OpenAI chat shape for `/v1/compress` and back. A `tool-result` part's
`output` is one of six kinds (`@ai-sdk/provider`, LanguageModelV3 and V4
alike): `text`, `json`, `error-text`, `error-json`, `execution-denied`,
and `content` (a list of text, file and custom members). The converters
flattened every kind to one string and the reverse side guessed the kind
back by trying `JSON.parse`. So after a round trip:
- **`error-text` / `error-json` came back as `text` / `json`.** The AI
SDK produces the error kinds for every tool that throws
(`createToolModelOutput`, `errorMode` text/json). `@ai-sdk/anthropic`
sets `is_error: true` only for those two kinds and `@ai-sdk/openai`
wraps them as `{"error": ...}`; both were gone, so a failed tool read as
a successful one.
- **`execution-denied` came back as a `json` value holding the denial
object** (`{ type: "json", value: { type: "execution-denied", reason }
}`), because it has no `value` and the whole output was stringified. The
providers send its `reason` (or "Tool call execution denied."), and
`@ai-sdk/openai` reads `output.providerOptions.openai.approvalId` from
it.
- **A `content` output** — what a tool's `toModelOutput` returns for a
screenshot, a PDF or an MCP result — **was JSON-stringified, compressed
as text (base64 included) and restored as `json`**, so the provider sent
the base64 as text instead of the `image` / `document` blocks
(`@ai-sdk/anthropic`) or `input_image` / `input_file` items
(`@ai-sdk/openai`, Responses) it builds from the members.
- Output-level `providerOptions` were dropped, and the guess flipped
kinds on its own: `text` `"42"` became `json` `42`, `json` `"ok"` became
`text` `"ok"`.
Captured on `main` (`ecbaf86d`) by the new tests before the change:
```text
error-text "ECONNREFUSED 10.0.0.7:443" -> { type: "text", value: "ECONNREFUSED 10.0.0.7:443" }
error-json { code: 7 } -> { type: "json", value: { code: 7 } }
execution-denied "Not from CI." -> { type: "json", value: { type: "execution-denied", reason: "Not from CI." } }
content [text, file, custom] -> { type: "json", value: [ ...the members as JSON... ] }
text "42" -> { type: "json", value: 42 }
json "ok" -> { type: "text", value: "ok" }
output providerOptions -> dropped
```
Same carrier pattern as #4564. The output's `value` still travels as the
OpenAI tool message `content`, so the proxy compresses it exactly as
before; the rest of the output object (`type`, `reason`,
`providerOptions`) rides on the message under `output` and rebuilds the
same kind on the way back. A `content` output goes out as the tool
message's content array: `/v1/compress` compresses the top-level `text`
blocks of a tool message as fragments of one tool result and passes
every other block through untouched (the same block walk that already
carries the `reasoning` parts), so text members shrink and file / custom
members come back byte for byte, with no per-version mapping (V3
`image-data` / `file-url` / `file-id` and V4 `file` / `custom` members
alike). A JSON body the proxy no longer returned as JSON would come back
as the kind's text twin (`json` → `text`, `error-json` → `error-text`);
in practice the proxy keeps JSON tool bodies parseable (see the proof).
Closes # (none — follow-up to #4564, which listed this under "Not in
this PR")
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- **`sdk/typescript/src/utils/format.ts`**
- `vercelToOpenAI`: a `tool-result` whose `output` is an object sends
its `value` as `content` — `text` / `error-text` as the string, `json` /
`error-json` JSON-encoded, `execution-denied` as `""`, `content` as the
member array — and the rest of the output under `output`. A bare
`output` value or the pre-v6 `result` field behaves as before.
- `openAIToVercel`: with the carrier present, rebuilds an output of that
kind from the returned content (`execution-denied` without a value;
`content` from the array; `json` / `error-json` parsed, text twin if the
body no longer parses; any other kind as the string). Without it, the
old JSON.parse guess stays for OpenAI-native tool messages.
- `openAIToGemini`: reads the tool body as text whichever shape it has
(follows the type widening; Gemini input never produces the array).
- **`sdk/typescript/src/types.ts`** — `ToolMessage.content` is `string |
Array<TextContentPart | ToolResultMediaPart>`, `ToolMessage.output?` is
the carrier, `ToolResultMediaPart` is the verbatim member type.
- **Tests** — `test/utils/format.test.ts` (13): a round trip per kind
including output-level `providerOptions` and the V4 and V3 member
shapes; the wire shape for `error-text`, `content` and
`execution-denied`; the `error-json` → `error-text` twin when a body
stops parsing, and a guard for the `json` → `text` twin.
`test/adapters/vercel-ai.test.ts` (1): a typed `LanguageModelV4Prompt`
with a denied, a multi-part and an error result through
`transformParams`, asserting on the restored results (one OpenAI tool
message is one result, so a multi-result tool message comes back as one
message per result, as before this PR).
- **Docs** — `docs/content/docs/vercel-ai-sdk.mdx`: one sentence on what
the output kinds keep.
**Not in this PR.** Regrouping the split tool messages into the AI SDK's
one-message-per-step shape (pre-existing; the providers group
consecutive tool messages themselves). The Gemini-native converter's
`thought` parts and `thoughtSignature` (separate PR).
## Testing
- [x] Unit tests pass (`npm test` — vitest, incl. typecheck)
- [x] Type checking passes (`npm run typecheck` — tsc --noEmit)
- [x] Build passes (`npm run build` — tsup)
- [x] New tests added for new functionality
- [x] Manual testing performed (below)
### Test Output
```text
$ cd sdk/typescript && npm test
Test Files 19 passed | 5 skipped (24)
Tests 449 passed | 33 skipped (482)
Type Errors no errors
$ npm run typecheck # tsc --noEmit — clean
$ npm run build # tsup — ESM and DTS built
```
## Real Behavior Proof
- Environment: published proxy `headroom-ai 0.43.1` (`uvx --from
'headroom-ai[proxy]==0.43.1' headroom proxy --host 127.0.0.1 --port
8799`), this branch built with `npm run build`, `ai` 7.0.137 /
`@ai-sdk/provider` 4.0.26 types, Node v22.16.0, macOS.
- Exact command / steps: a LanguageModelV4 prompt whose one tool message
carries five results — `execution-denied` with a reason and
`openai.approvalId`; a `content` result with a 56 KB text member, a 24
KB base64 `file` member and a `custom` member; an `error-text` and an
`error-json` with 56 KB bodies; a 56 KB `json` — passed through
`headroomMiddleware().transformParams` against the live proxy, with the
request the proxy received and its answer captured.
- Observed result: every kind came back as itself, the text bodies
shrank by about 44%, the file and custom members and the denial came
back byte-identical. Full output:
```text
== wire shape the proxy received (tool messages) ==
call_1: content=string(0) output={"type":"execution-denied","reason":"Not from CI.","providerOptions":{"openai":{"approvalId":"apr_1"}}}
call_2: content=[text, file, custom] output={"type":"content"}
call_3: content=string(56064) output={"type":"error-text"}
call_4: content=string(56043) output={"type":"error-json"}
call_5: content=string(56002) output={"type":"json"} messageProviderOptions=yes
== proxy answer: tokens 81695 -> 57772, transforms=["router:protected:system_message","router:text_block:mixed","router:mixed:0.45","router:smart_crusher:0.57","router:smart_crusher:0.45"]
== what the AI SDK gets back ==
system/user/assistant byte-identical: true
results back: call_1=execution-denied, call_2=content, call_3=error-text, call_4=error-json, call_5=json; toolNames kept: true
call_1 execution-denied byte-identical (reason + output providerOptions): true
call_2 content: members back [text, file, custom]; text member 56022 -> 31308 chars; file member byte-identical: true; custom member byte-identical: true
call_3 error-text: type=error-text, 56064 -> 31350 chars, still starts with the error line: true
call_4 error-json: type=error-json, 56043 -> 31731 chars, value is an object with keys error/last_rows, last_rows now a string
call_5 json: type=json, 56002 -> 31690 chars, value is a JSON string (the proxy returned the compact table as a JSON string literal): "[400]{id:int,level:string,msg:string,path:string,service:str"...
message-level providerOptions on the last tool message only: -,-,-,-,yes
```
The JSON kinds: the proxy keeps a JSON tool body parseable — the array
body came back as a JSON string holding the compact table and the object
body kept its shape with the nested array compacted — so both parse and
keep their kind; the text-twin fallback did not trigger. The same prompt
through the converters on `main` loses every item listed in the
Description (the new tests, run before the change).
- Not tested: an end-to-end multi-step `generateText` with a denied
approval or an image-returning tool against a provider (no provider keys
in this environment); the provider behaviour is taken from the installed
`@ai-sdk/anthropic` 4.0.78 and `@ai-sdk/openai` 4.0.91 sources and the
`ai` 7.0.137 core cited above.
## Runtime Rollout Safety
- Rollout-managed feature(s): none (TypeScript SDK only, no proxy
change)
- Minimum rollout channel: n/a
- Stable/default behavior changed: for SDK users, a Vercel tool result's
OpenAI-shaped message now carries an `output` key, and a `content`
result sends its member array instead of a JSON string; `/v1/compress`
already passes both through. `ToolMessage.content` widens to `string |
array` (additive).
- Kill switch / disable path: unchanged — `fallback: true` returns the
original prompt when the proxy is unreachable; unwrap the model to
disable compression.
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert this PR
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)
## Screenshots (if applicable)
N/A
## Additional Notes
- The `output` carrier never reaches an OpenAI endpoint through the
SDK's own paths, like the #4564 carriers: the universal `compress()`
returns messages in the input format, and the OpenAI adapter never sees
Vercel input.
- Enumerated before writing: `LanguageModelV3ToolResultOutput` and
`LanguageModelV4ToolResultOutput` in `@ai-sdk/provider` 4.0.26. `ai` 7's
`LanguageModelMiddleware` is V4-based while the SDK's peer range (`ai >=
6.0.0`) still includes V3 prompts; both member shapes are carried the
same way, which is why the members are not mapped.
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4670"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
### Maintainer current-main requalification
Exact head `e26525d382a135f1fe2a7f0b8671bfbae1f2d64a`, upstream main
`c01cf07d943d816cfd9d97e6838ed4e38f1ff9a4`.
Reviewed the five-file SDK delta, adjacent conversion/middleware paths,
existing discussion and Devin no-issues result. The output carrier
retains denial/error/content kinds and output-level providerOptions;
JSON parse failures retain the error flag via error-text. Mixed content
members stay in the tool-message array. Current proxy route accepts
message dictionaries and returns pipeline messages; no new schema
stripping is introduced. Author runtime proof and its provider-inference
limitations were considered. Fresh verification on the clean mechanical
main merge: 449 tests passed, 33 skipped; typecheck and SDK build
passed; main-relative whitespace passed. No contributor logic changed.
Hosted CI must qualify the published merge head; no new provider-runtime
test or inference claim.
Exact-head hosted checks are being handled by maintainers. No new
finding in the reviewed scope; formal approval awaits exact-head hosted
qualification; nothing merged.
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
JerrettDavis
parent
bfbadec8f9
commit
ce42754912
@@ -136,4 +136,4 @@ const { output } = await generateText({
|
||||
3. Compressed messages are converted back to Vercel format
|
||||
4. The original model receives the smaller prompt
|
||||
|
||||
All other model behavior (tool calling, structured output, streaming) is unchanged. Provider state the AI SDK attaches to the prompt travels through untouched: `reasoning` parts (with the Anthropic `signature` or `redactedData`, OpenAI `itemId`, Google `thoughtSignature` they carry) stay in place, tool calls return to their place among them, and `providerOptions` on messages, text and file parts, tool calls and tool results come back exactly as sent. Only large tool results change.
|
||||
All other model behavior (tool calling, structured output, streaming) is unchanged. Provider state the AI SDK attaches to the prompt travels through untouched: `reasoning` parts (with the Anthropic `signature` or `redactedData`, OpenAI `itemId`, Google `thoughtSignature` they carry) stay in place, tool calls return to their place among them, and `providerOptions` on messages, text and file parts, tool calls and tool results come back exactly as sent. Tool results keep their output kind: an `error-text` or `error-json` result is still an error, an `execution-denied` result keeps its reason, and a multi-part `content` result (a screenshot, a PDF, an MCP result) keeps its file and custom members byte for byte while its text members compress like any other tool output. Only large tool results change.
|
||||
|
||||
@@ -88,10 +88,33 @@ export interface AssistantMessage {
|
||||
providerOptions?: ProviderOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* A member of a Vercel AI SDK tool-result `content` output other than text: a
|
||||
* LanguageModelV4 `file` or `custom` part, or a LanguageModelV3 `file-data`,
|
||||
* `file-url`, `file-id`, `image-data`, `image-url` or `image-file-id` part.
|
||||
* Carried verbatim through `/v1/compress`, which passes unknown parts through.
|
||||
*/
|
||||
export interface ToolResultMediaPart {
|
||||
type: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface ToolMessage {
|
||||
role: "tool";
|
||||
content: string;
|
||||
/**
|
||||
* The tool output. A Vercel AI SDK `content` output keeps its member list: the
|
||||
* proxy compresses the text members as fragments of one tool result and passes
|
||||
* the rest through.
|
||||
*/
|
||||
content: string | Array<TextContentPart | ToolResultMediaPart>;
|
||||
tool_call_id: string;
|
||||
/**
|
||||
* Vercel AI SDK: the result's `output` without its `value`, which travels as
|
||||
* `content`. Brings the output kind (`error-text`, `execution-denied`, ...), the
|
||||
* denial reason and the output's own providerOptions back. Carried, never sent
|
||||
* to OpenAI.
|
||||
*/
|
||||
output?: { type: string; reason?: string; providerOptions?: ProviderOptions };
|
||||
providerOptions?: ProviderOptions;
|
||||
}
|
||||
|
||||
|
||||
@@ -622,19 +622,27 @@ export function vercelToOpenAI(messages: any[]): OpenAIMessage[] {
|
||||
for (const part of parts) {
|
||||
let toolMsg: ToolMessage;
|
||||
if (part.type === "tool-result") {
|
||||
// AI SDK v6 uses `output: { type, value }`, earlier versions used `result`
|
||||
let contentStr: string;
|
||||
if (part.output !== undefined) {
|
||||
// v6 format: output is { type: 'json', value } or { type: 'text', value }
|
||||
const val = part.output?.value ?? part.output;
|
||||
contentStr = typeof val === "string" ? val : JSON.stringify(val);
|
||||
} else if (part.result !== undefined) {
|
||||
// Legacy format: result field directly
|
||||
contentStr = typeof part.result === "string" ? part.result : JSON.stringify(part.result);
|
||||
const output = part.output;
|
||||
if (output && typeof output === "object" && typeof output.type === "string") {
|
||||
// AI SDK v6+: `output` is { type, value?, reason?, providerOptions? }. The value goes
|
||||
// out as the tool message content so the proxy compresses it; a `content` output
|
||||
// keeps its member list (text members compress as fragments of one tool result,
|
||||
// file and custom members pass through). The rest of the output rides on `output`
|
||||
// and brings the kind, the denial reason and the output's providerOptions back.
|
||||
const { value, ...rest } = output;
|
||||
const content =
|
||||
rest.type === "content" && Array.isArray(value) ? value
|
||||
: rest.type === "json" || rest.type === "error-json" ? JSON.stringify(value ?? null)
|
||||
: typeof value === "string" ? value
|
||||
: value === undefined ? ""
|
||||
: JSON.stringify(value);
|
||||
toolMsg = withProviderOptions({ role: "tool", content, tool_call_id: part.toolCallId, output: rest }, part);
|
||||
} else {
|
||||
contentStr = "";
|
||||
// A bare `output` value, or the pre-v6 `result` field.
|
||||
const raw = output !== undefined ? output : part.result;
|
||||
const contentStr = raw === undefined ? "" : typeof raw === "string" ? raw : JSON.stringify(raw);
|
||||
toolMsg = withProviderOptions({ role: "tool", content: contentStr, tool_call_id: part.toolCallId }, part);
|
||||
}
|
||||
toolMsg = withProviderOptions({ role: "tool", content: contentStr, tool_call_id: part.toolCallId }, part);
|
||||
} else {
|
||||
// tool-approval-response (or any other AI SDK tool part): no OpenAI equivalent,
|
||||
// carried verbatim on `part` and restored as-is.
|
||||
@@ -725,12 +733,25 @@ export function openAIToVercel(messages: OpenAIMessage[]): any[] {
|
||||
if (carried && typeof carried === "object") {
|
||||
part = carried;
|
||||
} else {
|
||||
let parsed: any;
|
||||
try { parsed = JSON.parse(msg.content); } catch { parsed = msg.content; }
|
||||
// AI SDK v6 requires output: { type: 'json' | 'text', value }
|
||||
const output = typeof parsed === "string"
|
||||
? { type: "text" as const, value: parsed }
|
||||
: { type: "json" as const, value: parsed };
|
||||
const body: any = msg.content;
|
||||
const kind = msg.output;
|
||||
let output: any;
|
||||
if (kind && typeof kind === "object" && typeof kind.type === "string") {
|
||||
const { type, ...meta } = kind;
|
||||
if (type === "execution-denied") output = { type, ...meta };
|
||||
else if (type === "content") output = { type, value: Array.isArray(body) ? body : [{ type: "text", text: String(body ?? "") }], ...meta };
|
||||
else if (type === "json" || type === "error-json") {
|
||||
// A JSON body the proxy rewrote into prose comes back as the kind's text twin.
|
||||
try { output = { type, value: JSON.parse(body), ...meta }; }
|
||||
catch { output = { type: type === "json" ? "text" : "error-text", value: body, ...meta }; }
|
||||
} else output = { type, value: typeof body === "string" ? body : JSON.stringify(body), ...meta };
|
||||
} else {
|
||||
// No carrier (an OpenAI-native tool message): guess the kind from the body.
|
||||
const text = typeof body === "string" ? body : JSON.stringify(body);
|
||||
let parsed: any;
|
||||
try { parsed = JSON.parse(text); } catch { parsed = text; }
|
||||
output = typeof parsed === "string" ? { type: "text", value: parsed } : { type: "json", value: parsed };
|
||||
}
|
||||
part = withProviderOptions({
|
||||
type: "tool-result",
|
||||
toolCallId: msg.tool_call_id,
|
||||
@@ -923,7 +944,8 @@ export function openAIToGemini(messages: OpenAIMessage[]): any[] {
|
||||
|
||||
if (msg.role === "tool") {
|
||||
let response: any;
|
||||
try { response = JSON.parse(msg.content); } catch { response = { result: msg.content }; }
|
||||
const text = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
|
||||
try { response = JSON.parse(text); } catch { response = { result: text }; }
|
||||
result.push({
|
||||
role: "user",
|
||||
parts: [{ functionResponse: { name: msg.tool_call_id?.replace("gemini_", "") ?? "unknown", response } }],
|
||||
|
||||
@@ -596,3 +596,52 @@ describe("headroomMiddleware keeps provider-executed tool calls in place", () =>
|
||||
expect(out.prompt).toEqual(prompt);
|
||||
});
|
||||
});
|
||||
|
||||
describe("headroomMiddleware keeps tool-result output kinds", () => {
|
||||
beforeEach(() => {
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
it("returns denied, multi-part and error results exactly as the AI SDK sent them", async () => {
|
||||
const prompt: LanguageModelV4Prompt = [
|
||||
{ role: "user", content: [{ type: "text", text: "Deploy, then screenshot the dashboard." }] },
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{ type: "tool-call", toolCallId: "call_1", toolName: "deploy", input: { env: "prod" } },
|
||||
{ type: "tool-call", toolCallId: "call_2", toolName: "screenshot", input: {} },
|
||||
{ type: "tool-call", toolCallId: "call_3", toolName: "fetch_gateway_metrics", input: {} },
|
||||
],
|
||||
},
|
||||
{
|
||||
role: "tool",
|
||||
content: [
|
||||
{ type: "tool-result", toolCallId: "call_1", toolName: "deploy", output: { type: "execution-denied", reason: "Not from CI." } },
|
||||
{
|
||||
type: "tool-result",
|
||||
toolCallId: "call_2",
|
||||
toolName: "screenshot",
|
||||
output: {
|
||||
type: "content",
|
||||
value: [
|
||||
{ type: "text", text: "Dashboard at 12:00." },
|
||||
{ type: "file", data: { type: "data", data: "iVBORw0KGgo=" }, mediaType: "image/png", filename: "dashboard.png" },
|
||||
],
|
||||
},
|
||||
},
|
||||
{ type: "tool-result", toolCallId: "call_3", toolName: "fetch_gateway_metrics", output: { type: "error-text", value: "ECONNREFUSED 10.0.0.7:443" } },
|
||||
],
|
||||
},
|
||||
];
|
||||
// The proxy passes this prompt through unchanged.
|
||||
mockFetch.mockImplementationOnce(async (_url: string, init: any) => mockCompressResponse(JSON.parse(init.body).messages));
|
||||
|
||||
const middleware = headroomMiddleware({ baseUrl: "http://localhost:8787" });
|
||||
const out = await middleware.transformParams({ params: { prompt }, model: { specificationVersion: "v4" }, type: "generate" });
|
||||
|
||||
expect(out.prompt.slice(0, 2)).toEqual(prompt.slice(0, 2));
|
||||
// One OpenAI tool message per result, so a multi-result tool message comes back as one
|
||||
// message per result; the results themselves are the AI SDK's own.
|
||||
expect(out.prompt.slice(2).flatMap((m: any) => m.content)).toEqual(prompt[2].content);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1670,3 +1670,95 @@ describe("Vercel tool messages with several results carry message-level provider
|
||||
expect(back.map((m: any) => m.providerOptions)).toEqual([undefined, { anthropic: { cacheControl: { type: "ephemeral" } } }]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Vercel tool-result output kinds survive the round trip", () => {
|
||||
// The result's value travels as the OpenAI tool message content, so the proxy still compresses
|
||||
// it; the rest of the output object rides beside it and brings the kind back on the way out.
|
||||
const turn = (output: any) => [
|
||||
{ role: "assistant", content: [{ type: "tool-call", toolCallId: "call_1", toolName: "deploy", input: {} }] },
|
||||
{ role: "tool", content: [{ type: "tool-result", toolCallId: "call_1", toolName: "deploy", output }] },
|
||||
];
|
||||
const roundTrip = (output: any) => openAIToVercel(vercelToOpenAI(turn(output)))[1].content[0].output;
|
||||
|
||||
it("keeps an error-text result an error", () => {
|
||||
expect(roundTrip({ type: "error-text", value: "ECONNREFUSED 10.0.0.7:443" })).toEqual({ type: "error-text", value: "ECONNREFUSED 10.0.0.7:443" });
|
||||
});
|
||||
|
||||
it("keeps an error-json result an error", () => {
|
||||
expect(roundTrip({ type: "error-json", value: { code: 7 } })).toEqual({ type: "error-json", value: { code: 7 } });
|
||||
});
|
||||
|
||||
it("restores an execution-denied result with its reason and no value", () => {
|
||||
expect(roundTrip({ type: "execution-denied", reason: "Not from CI." })).toEqual({ type: "execution-denied", reason: "Not from CI." });
|
||||
expect(roundTrip({ type: "execution-denied" })).toEqual({ type: "execution-denied" });
|
||||
});
|
||||
|
||||
it("keeps a text result whose body parses as JSON a text result", () => {
|
||||
expect(roundTrip({ type: "text", value: "42" })).toEqual({ type: "text", value: "42" });
|
||||
});
|
||||
|
||||
it("keeps a json result whose value is a string a json result", () => {
|
||||
expect(roundTrip({ type: "json", value: "ok" })).toEqual({ type: "json", value: "ok" });
|
||||
});
|
||||
|
||||
it("keeps the output's own providerOptions", () => {
|
||||
const output = { type: "text", value: "ok", providerOptions: { openai: { approvalId: "apr_1" } } };
|
||||
expect(roundTrip(output)).toEqual(output);
|
||||
});
|
||||
|
||||
it("keeps a content result's members: text as text, file and custom parts verbatim", () => {
|
||||
const output = {
|
||||
type: "content",
|
||||
value: [
|
||||
{ type: "text", text: "Dashboard at 12:00.", providerOptions: { anthropic: { cacheControl: { type: "ephemeral" } } } },
|
||||
{ type: "file", data: { type: "data", data: "iVBORw0KGgo=" }, mediaType: "image/png", filename: "shot.png", providerOptions: { openai: { imageDetail: "low" } } },
|
||||
{ type: "custom", providerOptions: { anthropic: { toolReference: { type: "tool_reference", toolName: "bash" } } } },
|
||||
],
|
||||
};
|
||||
expect(roundTrip(output)).toEqual(output);
|
||||
});
|
||||
|
||||
it("carries LanguageModelV3 media members (image-data, file-url, file-id) the same way", () => {
|
||||
const output = {
|
||||
type: "content",
|
||||
value: [
|
||||
{ type: "text", text: "Three attachments." },
|
||||
{ type: "image-data", data: "iVBORw0KGgo=", mediaType: "image/png" },
|
||||
{ type: "file-url", url: "https://example.com/report.pdf" },
|
||||
{ type: "file-id", fileId: { openai: "file-abc" } },
|
||||
],
|
||||
};
|
||||
expect(roundTrip(output)).toEqual(output);
|
||||
});
|
||||
|
||||
it("sends an error-text body as the tool message content, with the kind beside it", () => {
|
||||
const [, toolMsg] = vercelToOpenAI(turn({ type: "error-text", value: "ECONNREFUSED 10.0.0.7:443" })) as any[];
|
||||
expect(toolMsg.content).toBe("ECONNREFUSED 10.0.0.7:443");
|
||||
expect(toolMsg.output).toEqual({ type: "error-text" });
|
||||
});
|
||||
|
||||
it("sends a content result as the tool message's content array", () => {
|
||||
const value = [{ type: "text", text: "Dashboard at 12:00." }, { type: "file", data: { type: "data", data: "iVBORw0KGgo=" }, mediaType: "image/png" }];
|
||||
const [, toolMsg] = vercelToOpenAI(turn({ type: "content", value })) as any[];
|
||||
expect(toolMsg.content).toEqual(value);
|
||||
expect(toolMsg.output).toEqual({ type: "content" });
|
||||
});
|
||||
|
||||
it("sends an execution-denied result as empty content with the reason beside it", () => {
|
||||
const [, toolMsg] = vercelToOpenAI(turn({ type: "execution-denied", reason: "Not from CI." })) as any[];
|
||||
expect(toolMsg.content).toBe("");
|
||||
expect(toolMsg.output).toEqual({ type: "execution-denied", reason: "Not from CI." });
|
||||
});
|
||||
|
||||
it("keeps the error flag when a compressed error-json body no longer parses", () => {
|
||||
const converted = vercelToOpenAI(turn({ type: "error-json", value: { rows: [1, 2, 3, 4] } })) as any[];
|
||||
converted[1].content = "[rows: 4 items, 1-4]";
|
||||
expect(openAIToVercel(converted)[1].content[0].output).toEqual({ type: "error-text", value: "[rows: 4 items, 1-4]" });
|
||||
});
|
||||
|
||||
it("returns a json result whose compressed body no longer parses as its text twin", () => {
|
||||
const converted = vercelToOpenAI(turn({ type: "json", value: { rows: [1, 2, 3, 4] } })) as any[];
|
||||
converted[1].content = "[rows: 4 items, 1-4]";
|
||||
expect(openAIToVercel(converted)[1].content[0].output).toEqual({ type: "text", value: "[rows: 4 items, 1-4]" });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user