Spaces:
Build error
Build error
Commit ·
df11ca5
1
Parent(s): ba9ea76
Fix Vercel AI SDK v6 format: use input/output instead of args/result
Browse files- ToolCallPart: read `input` (v6) with fallback to `args` (legacy) in vercelToOpenAI
- ToolCallPart: produce `input` instead of `args` in openAIToVercel
- ToolResultPart: produce `output: { type, value }` instead of `result` in openAIToVercel
- ToolResultPart: read `output` (v6) with fallback to `result` (legacy) in vercelToOpenAI
- Add MDX example tests: verify every code sample from PR docs against real APIs
- 116 tests passing (unit + integration + e2e with OpenAI + Anthropic)
sdk/typescript/src/utils/format.ts
CHANGED
|
@@ -253,7 +253,8 @@ export function vercelToOpenAI(messages: any[]): OpenAIMessage[] {
|
|
| 253 |
openaiMsg.tool_calls = toolCallParts.map((p: any): ToolCall => ({
|
| 254 |
id: p.toolCallId,
|
| 255 |
type: "function",
|
| 256 |
-
|
|
|
|
| 257 |
}));
|
| 258 |
}
|
| 259 |
result.push(openaiMsg);
|
|
@@ -318,13 +319,13 @@ export function openAIToVercel(messages: OpenAIMessage[]): any[] {
|
|
| 318 |
if (msg.content) parts.push({ type: "text", text: msg.content });
|
| 319 |
if (msg.tool_calls) {
|
| 320 |
for (const tc of msg.tool_calls) {
|
| 321 |
-
let
|
| 322 |
-
try {
|
| 323 |
parts.push({
|
| 324 |
type: "tool-call",
|
| 325 |
toolCallId: tc.id,
|
| 326 |
toolName: tc.function.name,
|
| 327 |
-
|
| 328 |
});
|
| 329 |
}
|
| 330 |
}
|
|
|
|
| 253 |
openaiMsg.tool_calls = toolCallParts.map((p: any): ToolCall => ({
|
| 254 |
id: p.toolCallId,
|
| 255 |
type: "function",
|
| 256 |
+
// AI SDK v6 uses `input`, earlier versions used `args`
|
| 257 |
+
function: { name: p.toolName, arguments: JSON.stringify(p.input ?? p.args) },
|
| 258 |
}));
|
| 259 |
}
|
| 260 |
result.push(openaiMsg);
|
|
|
|
| 319 |
if (msg.content) parts.push({ type: "text", text: msg.content });
|
| 320 |
if (msg.tool_calls) {
|
| 321 |
for (const tc of msg.tool_calls) {
|
| 322 |
+
let input: any;
|
| 323 |
+
try { input = JSON.parse(tc.function.arguments); } catch { input = tc.function.arguments ?? {}; }
|
| 324 |
parts.push({
|
| 325 |
type: "tool-call",
|
| 326 |
toolCallId: tc.id,
|
| 327 |
toolName: tc.function.name,
|
| 328 |
+
input, // AI SDK v6 uses `input`, not `args`
|
| 329 |
});
|
| 330 |
}
|
| 331 |
}
|
sdk/typescript/test/mdx-examples.test.ts
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Tests every code example from the Vercel AI SDK PR MDX files.
|
| 3 |
+
*
|
| 4 |
+
* If these pass, the examples in the docs are correct.
|
| 5 |
+
*
|
| 6 |
+
* Run: HEADROOM_INTEGRATION=1 npx vitest run test/mdx-examples.test.ts
|
| 7 |
+
*/
|
| 8 |
+
import { describe, it, expect, beforeAll } from "vitest";
|
| 9 |
+
import { config } from "dotenv";
|
| 10 |
+
import { resolve } from "path";
|
| 11 |
+
|
| 12 |
+
config({ path: resolve(__dirname, "../../../.env") });
|
| 13 |
+
|
| 14 |
+
const PROXY_URL = "http://localhost:8787";
|
| 15 |
+
const RUN = process.env.HEADROOM_INTEGRATION === "1";
|
| 16 |
+
|
| 17 |
+
describe.skipIf(!RUN)("MDX Examples: 51-headroom.mdx", () => {
|
| 18 |
+
beforeAll(async () => {
|
| 19 |
+
const res = await fetch(`${PROXY_URL}/health`);
|
| 20 |
+
if (!res.ok) throw new Error("Proxy not running");
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
// =====================================================
|
| 24 |
+
// Example 1 from 51-headroom.mdx: "Compress messages before calling the model"
|
| 25 |
+
// =====================================================
|
| 26 |
+
it("compress() with AI SDK format messages → generateText()", { timeout: 30000 }, async () => {
|
| 27 |
+
const { compress } = await import("../src/compress.js");
|
| 28 |
+
const { generateText } = await import("ai");
|
| 29 |
+
const { createOpenAI } = await import("@ai-sdk/openai");
|
| 30 |
+
|
| 31 |
+
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
| 32 |
+
|
| 33 |
+
// Simulated large tool result (the MDX shows `largeLogData`)
|
| 34 |
+
const largeLogData = Array.from({ length: 100 }, (_, i) => ({
|
| 35 |
+
timestamp: new Date(Date.now() - i * 1000).toISOString(),
|
| 36 |
+
level: i === 42 ? "FATAL" : i % 10 === 0 ? "ERROR" : "INFO",
|
| 37 |
+
service: `service-${["auth", "payment", "user", "api"][i % 4]}`,
|
| 38 |
+
message:
|
| 39 |
+
i === 42
|
| 40 |
+
? "Connection pool exhausted — max_connections=100 reached, 47 pending requests"
|
| 41 |
+
: `Request processed in ${Math.round(Math.random() * 500)}ms for /${["login", "checkout", "profile", "notify"][i % 4]}`,
|
| 42 |
+
trace_id: `trace-${Math.random().toString(36).substring(2, 10)}`,
|
| 43 |
+
}));
|
| 44 |
+
|
| 45 |
+
// EXACT pattern from MDX (adapted to use AI SDK message format)
|
| 46 |
+
const messages: any[] = [
|
| 47 |
+
{ role: "user", content: "Analyze the server logs" },
|
| 48 |
+
{
|
| 49 |
+
role: "assistant",
|
| 50 |
+
content: [
|
| 51 |
+
{
|
| 52 |
+
type: "tool-call",
|
| 53 |
+
toolCallId: "tc_1",
|
| 54 |
+
toolName: "get_logs",
|
| 55 |
+
args: { limit: 100 },
|
| 56 |
+
},
|
| 57 |
+
],
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
role: "tool",
|
| 61 |
+
content: [
|
| 62 |
+
{
|
| 63 |
+
type: "tool-result",
|
| 64 |
+
toolCallId: "tc_1",
|
| 65 |
+
toolName: "get_logs",
|
| 66 |
+
output: { type: "json", value: largeLogData },
|
| 67 |
+
},
|
| 68 |
+
],
|
| 69 |
+
},
|
| 70 |
+
{ role: "user", content: "What is the critical issue?" },
|
| 71 |
+
];
|
| 72 |
+
|
| 73 |
+
const compressed = await compress(messages, {
|
| 74 |
+
model: "gpt-4o",
|
| 75 |
+
baseUrl: PROXY_URL,
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
console.log(
|
| 79 |
+
` Example 1: ${compressed.tokensBefore} → ${compressed.tokensAfter} tokens (saved ${compressed.tokensSaved})`,
|
| 80 |
+
);
|
| 81 |
+
|
| 82 |
+
// Verify compression happened
|
| 83 |
+
expect(compressed.tokensBefore).toBeGreaterThan(0);
|
| 84 |
+
// Messages should still be in a format generateText accepts
|
| 85 |
+
expect(compressed.messages.length).toBeGreaterThan(0);
|
| 86 |
+
|
| 87 |
+
// Now call generateText with compressed messages
|
| 88 |
+
const { text } = await generateText({
|
| 89 |
+
model: openai("gpt-4o-mini"),
|
| 90 |
+
messages: compressed.messages,
|
| 91 |
+
});
|
| 92 |
+
|
| 93 |
+
console.log(` LLM response: "${text.substring(0, 150)}"`);
|
| 94 |
+
expect(text.length).toBeGreaterThan(0);
|
| 95 |
+
// Should find the FATAL connection pool issue
|
| 96 |
+
expect(text.toLowerCase()).toMatch(/connection|pool|fatal|exhaust/i);
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
// =====================================================
|
| 100 |
+
// Example 2 from 51-headroom.mdx: "Use as middleware"
|
| 101 |
+
// =====================================================
|
| 102 |
+
it("headroomMiddleware() with wrapLanguageModel → generateText()", { timeout: 30000 }, async () => {
|
| 103 |
+
const { headroomMiddleware } = await import("../src/adapters/vercel-ai.js");
|
| 104 |
+
const { wrapLanguageModel, generateText } = await import("ai");
|
| 105 |
+
const { createOpenAI } = await import("@ai-sdk/openai");
|
| 106 |
+
|
| 107 |
+
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
| 108 |
+
|
| 109 |
+
// EXACT pattern from MDX
|
| 110 |
+
const model = wrapLanguageModel({
|
| 111 |
+
model: openai("gpt-4o-mini"),
|
| 112 |
+
middleware: headroomMiddleware({ baseUrl: PROXY_URL }),
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
// Feed it a big prompt that will get compressed
|
| 116 |
+
const serverData = Array.from({ length: 100 }, (_, i) => ({
|
| 117 |
+
name: `server-${i + 1}`,
|
| 118 |
+
status: i % 15 === 0 ? "critical" : "healthy",
|
| 119 |
+
cpu: Math.round(Math.random() * 100),
|
| 120 |
+
alert: i % 15 === 0 ? `Disk at ${90 + (i % 10)}%` : null,
|
| 121 |
+
description: `Production server ${i + 1} running service-${["auth", "payment"][i % 2]}`,
|
| 122 |
+
}));
|
| 123 |
+
|
| 124 |
+
const { text } = await generateText({
|
| 125 |
+
model,
|
| 126 |
+
system: "List only the critical servers. One line each.",
|
| 127 |
+
prompt: `Fleet status:\n${JSON.stringify(serverData)}`,
|
| 128 |
+
});
|
| 129 |
+
|
| 130 |
+
console.log(` Example 2 (middleware): "${text.substring(0, 150)}"`);
|
| 131 |
+
expect(text.length).toBeGreaterThan(0);
|
| 132 |
+
expect(text.toLowerCase()).toMatch(/server/i);
|
| 133 |
+
});
|
| 134 |
+
|
| 135 |
+
// =====================================================
|
| 136 |
+
// Example 3 from 51-headroom.mdx: "Works with any provider" (Anthropic)
|
| 137 |
+
// =====================================================
|
| 138 |
+
it("compress() → Anthropic via AI SDK", { timeout: 30000 }, async () => {
|
| 139 |
+
if (!process.env.ANTHROPIC_API_KEY) {
|
| 140 |
+
console.log(" Skipping: ANTHROPIC_API_KEY not set");
|
| 141 |
+
return;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
const { compress } = await import("../src/compress.js");
|
| 145 |
+
const { generateText } = await import("ai");
|
| 146 |
+
const { createAnthropic } = await import("@ai-sdk/anthropic");
|
| 147 |
+
|
| 148 |
+
const anthropic = createAnthropic({
|
| 149 |
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
| 150 |
+
baseURL: "https://api.anthropic.com/v1",
|
| 151 |
+
});
|
| 152 |
+
|
| 153 |
+
const searchResults = Array.from({ length: 80 }, (_, i) => ({
|
| 154 |
+
title: `${["API Design", "Database Tuning", "Cache Strategy", "Load Balancing"][i % 4]} Guide ${i + 1}`,
|
| 155 |
+
url: `https://docs.example.com/${i + 1}`,
|
| 156 |
+
snippet: `Covers ${["best practices", "pitfalls", "advanced techniques", "getting started"][i % 4]} for ${["microservices", "distributed systems", "cloud native", "serverless"][i % 4]}.`,
|
| 157 |
+
score: (100 - i) / 100,
|
| 158 |
+
}));
|
| 159 |
+
|
| 160 |
+
// Simple messages (no tool calls — just user content)
|
| 161 |
+
const messages: any[] = [
|
| 162 |
+
{
|
| 163 |
+
role: "user",
|
| 164 |
+
content: `Search results:\n${JSON.stringify(searchResults)}\n\nTop 3 results? One sentence each.`,
|
| 165 |
+
},
|
| 166 |
+
];
|
| 167 |
+
|
| 168 |
+
// EXACT pattern from MDX
|
| 169 |
+
const compressed = await compress(messages, {
|
| 170 |
+
model: "claude-haiku-4-5-20251001",
|
| 171 |
+
baseUrl: PROXY_URL,
|
| 172 |
+
});
|
| 173 |
+
|
| 174 |
+
console.log(
|
| 175 |
+
` Example 3 (Anthropic): ${compressed.tokensBefore} → ${compressed.tokensAfter} tokens`,
|
| 176 |
+
);
|
| 177 |
+
|
| 178 |
+
const { text } = await generateText({
|
| 179 |
+
model: anthropic("claude-haiku-4-5-20251001"),
|
| 180 |
+
messages: compressed.messages,
|
| 181 |
+
maxTokens: 300,
|
| 182 |
+
});
|
| 183 |
+
|
| 184 |
+
console.log(` Anthropic response: "${text.substring(0, 150)}"`);
|
| 185 |
+
expect(text.length).toBeGreaterThan(0);
|
| 186 |
+
});
|
| 187 |
+
});
|
| 188 |
+
|
| 189 |
+
describe.skipIf(!RUN)("MDX Examples: context-compression-middleware.mdx", () => {
|
| 190 |
+
beforeAll(async () => {
|
| 191 |
+
const res = await fetch(`${PROXY_URL}/health`);
|
| 192 |
+
if (!res.ok) throw new Error("Proxy not running");
|
| 193 |
+
});
|
| 194 |
+
|
| 195 |
+
// =====================================================
|
| 196 |
+
// The cookbook's compressionMiddleware — test the EXACT code from the MDX
|
| 197 |
+
// =====================================================
|
| 198 |
+
it("compressionMiddleware from cookbook works end-to-end", { timeout: 30000 }, async () => {
|
| 199 |
+
const { compress } = await import("../src/compress.js");
|
| 200 |
+
const { wrapLanguageModel, generateText } = await import("ai");
|
| 201 |
+
const { createOpenAI } = await import("@ai-sdk/openai");
|
| 202 |
+
|
| 203 |
+
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
| 204 |
+
|
| 205 |
+
// EXACT middleware from the cookbook MDX
|
| 206 |
+
const compressionMiddleware = {
|
| 207 |
+
transformParams: async ({ params }: { params: any }) => {
|
| 208 |
+
const prompt = params.prompt;
|
| 209 |
+
if (!prompt || prompt.length === 0) return params;
|
| 210 |
+
|
| 211 |
+
const result = await compress(prompt, {
|
| 212 |
+
model: params.modelId ?? "gpt-4o",
|
| 213 |
+
baseUrl: PROXY_URL,
|
| 214 |
+
});
|
| 215 |
+
|
| 216 |
+
if (!result.compressed) return params;
|
| 217 |
+
|
| 218 |
+
console.log(
|
| 219 |
+
` Compressed: ${result.tokensBefore} → ${result.tokensAfter} tokens (saved ${result.tokensSaved})`,
|
| 220 |
+
);
|
| 221 |
+
|
| 222 |
+
return { ...params, prompt: result.messages };
|
| 223 |
+
},
|
| 224 |
+
};
|
| 225 |
+
|
| 226 |
+
// EXACT pattern from cookbook: wrap model with middleware
|
| 227 |
+
const model = wrapLanguageModel({
|
| 228 |
+
model: openai("gpt-4o-mini"),
|
| 229 |
+
middleware: compressionMiddleware,
|
| 230 |
+
});
|
| 231 |
+
|
| 232 |
+
// Simulate the SRE agent scenario from the cookbook
|
| 233 |
+
const serverData = Array.from({ length: 100 }, (_, i) => ({
|
| 234 |
+
id: i + 1,
|
| 235 |
+
name: `server-${i + 1}`,
|
| 236 |
+
status: i % 15 === 0 ? "critical" : i % 5 === 0 ? "warning" : "healthy",
|
| 237 |
+
cpu: Math.round(Math.random() * 100),
|
| 238 |
+
memory: Math.round(Math.random() * 100),
|
| 239 |
+
region: ["us-east-1", "eu-west-1", "ap-southeast-1"][i % 3],
|
| 240 |
+
lastAlert: i % 15 === 0 ? `Disk usage at ${90 + (i % 10)}%` : null,
|
| 241 |
+
}));
|
| 242 |
+
|
| 243 |
+
const { text } = await generateText({
|
| 244 |
+
model,
|
| 245 |
+
system: "You are an SRE assistant. List only the critical servers.",
|
| 246 |
+
prompt: `Fleet status:\n${JSON.stringify(serverData)}`,
|
| 247 |
+
});
|
| 248 |
+
|
| 249 |
+
console.log(` Cookbook middleware response: "${text.substring(0, 200)}"`);
|
| 250 |
+
expect(text.length).toBeGreaterThan(0);
|
| 251 |
+
expect(text.toLowerCase()).toMatch(/server/i);
|
| 252 |
+
});
|
| 253 |
+
});
|
sdk/typescript/test/utils/format.test.ts
CHANGED
|
@@ -327,7 +327,7 @@ describe("openAIToVercel", () => {
|
|
| 327 |
type: "tool-call",
|
| 328 |
toolCallId: "tc_1",
|
| 329 |
toolName: "search",
|
| 330 |
-
|
| 331 |
});
|
| 332 |
});
|
| 333 |
|
|
@@ -347,7 +347,7 @@ describe("openAIToVercel", () => {
|
|
| 347 |
];
|
| 348 |
const result = openAIToVercel(msgs);
|
| 349 |
expect(result[0].content).toEqual([
|
| 350 |
-
{ type: "tool-call", toolCallId: "tc_1", toolName: "fn",
|
| 351 |
]);
|
| 352 |
});
|
| 353 |
|
|
@@ -428,7 +428,7 @@ describe("round-trip conversion", () => {
|
|
| 428 |
type: "tool-call",
|
| 429 |
toolCallId: "tc_1",
|
| 430 |
toolName: "search",
|
| 431 |
-
|
| 432 |
});
|
| 433 |
});
|
| 434 |
});
|
|
|
|
| 327 |
type: "tool-call",
|
| 328 |
toolCallId: "tc_1",
|
| 329 |
toolName: "search",
|
| 330 |
+
input: { q: "test" },
|
| 331 |
});
|
| 332 |
});
|
| 333 |
|
|
|
|
| 347 |
];
|
| 348 |
const result = openAIToVercel(msgs);
|
| 349 |
expect(result[0].content).toEqual([
|
| 350 |
+
{ type: "tool-call", toolCallId: "tc_1", toolName: "fn", input: {} },
|
| 351 |
]);
|
| 352 |
});
|
| 353 |
|
|
|
|
| 428 |
type: "tool-call",
|
| 429 |
toolCallId: "tc_1",
|
| 430 |
toolName: "search",
|
| 431 |
+
input: { q: "test" },
|
| 432 |
});
|
| 433 |
});
|
| 434 |
});
|