Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

Make compress() format-aware: auto-detect OpenAI, Anthropic, Vercel, Gemini

chopratejas7d3f09f028b2
diff --git a/sdk/typescript/package-lock.json b/sdk/typescript/package-lock.json
index a3dfcf126..2fdfe2b87 100644
--- a/sdk/typescript/package-lock.json
+++ b/sdk/typescript/package-lock.json
@@ -9,9 +9,12 @@
"version": "0.1.0",
"license": "Apache-2.0",
"devDependencies": {
+ "@ai-sdk/anthropic": "^3.0.64",
+ "@ai-sdk/openai": "^3.0.48",
"@ai-sdk/provider": "^1.0.0",
"@anthropic-ai/sdk": "^0.39.0",
"ai": "^6.0.0",
+ "dotenv": "^17.3.1",
"openai": "^4.80.0",
"tsup": "^8.0.0",
"typescript": "^5.5.0",
@@ -41,6 +44,36 @@
}
}
},
+ "node_modules/@ai-sdk/anthropic": {
+ "version": "3.0.64",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/anthropic/-/anthropic-3.0.64.tgz",
+ "integrity": "sha512-rwLi/Rsuj2pYniQXIrvClHvXDzgM4UQHHnvHTWEF14efnlKclG/1ghpNC+adsRujAbCTr6gRsSbDE2vEqriV7g==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "@ai-sdk/provider": "3.0.8",
+ "@ai-sdk/provider-utils": "4.0.21"
+ },
+ "engines": {
+ "node": ">=18"
+ },
+ "peerDependencies": {
+ "zod": "^3.25.76 || ^4.1.8"
+ }
+ },
+ "node_modules/@ai-sdk/anthropic/node_modules/@ai-sdk/provider": {
+ "version": "3.0.8",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-3.0.8.tgz",
+ "integrity": "sha512-oGMAgGoQdBXbZqNG0Ze56CHjDZ1IDYOwGYxYjO5KLSlz5HiNQ9udIXsPZ61VWaHGZ5XW/jyjmr6t2xz2jGVwbQ==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "json-schema": "^0.4.0"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+ },
"node_modules/@ai-sdk/gateway": {
"version": "3.0.80",
"resolved": "https://registry.npmjs.org/@ai-sdk/gateway/-/gateway-3.0.80.tgz",
@@ -72,6 +105,36 @@
"node": ">=18"
}
},
+ "node_modules/@ai-sdk/openai": {
+ "version": "3.0.48",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/openai/-/openai-3.0.48.tgz",
+ "integrity": "sha512-ALmj/53EXpcRqMbGpPJPP4UOSWw0q4VGpnDo7YctvsynjkrKDmoneDG/1a7VQnSPYHnJp6tTRMf5ZdxZ5whulg==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "@ai-sdk/provider": "3.0.8",
+ "@ai-sdk/provider-utils": "4.0.21"
+ },
+ "engines": {
+ "node": ">=18"
+ },
+ "peerDependencies": {
+ "zod": "^3.25.76 || ^4.1.8"
+ }
+ },
+ "node_modules/@ai-sdk/openai/node_modules/@ai-sdk/provider": {
+ "version": "3.0.8",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-3.0.8.tgz",
+ "integrity": "sha512-oGMAgGoQdBXbZqNG0Ze56CHjDZ1IDYOwGYxYjO5KLSlz5HiNQ9udIXsPZ61VWaHGZ5XW/jyjmr6t2xz2jGVwbQ==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "json-schema": "^0.4.0"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+ },
"node_modules/@ai-sdk/provider": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-1.1.3.tgz",
@@ -1401,6 +1464,19 @@
"node": ">=0.4.0"
}
},
+ "node_modules/dotenv": {
+ "version": "17.3.1",
+ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.3.1.tgz",
+ "integrity": "sha512-IO8C/dzEb6O3F9/twg6ZLXz164a2fhTnEWb95H23Dm4OuN+92NmEAlTrupP9VW6Jm3sO26tQlqyvyi4CsnY9GA==",
+ "dev": true,
+ "license": "BSD-2-Clause",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://dotenvx.com"
+ }
+ },
"node_modules/dunder-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json
index 5940846b4..7f798a2ad 100644
--- a/sdk/typescript/package.json
+++ b/sdk/typescript/package.json
@@ -6,26 +6,56 @@
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
- "files": ["dist", "README.md", "LICENSE"],
+ "files": [
+ "dist",
+ "README.md",
+ "LICENSE"
+ ],
"exports": {
".": {
- "import": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
- "require": { "types": "./dist/index.d.cts", "default": "./dist/index.cjs" }
+ "import": {
+ "types": "./dist/index.d.ts",
+ "default": "./dist/index.js"
+ },
+ "require": {
+ "types": "./dist/index.d.cts",
+ "default": "./dist/index.cjs"
+ }
},
"./vercel-ai": {
- "import": { "types": "./dist/adapters/vercel-ai.d.ts", "default": "./dist/adapters/vercel-ai.js" },
- "require": { "types": "./dist/adapters/vercel-ai.d.cts", "default": "./dist/adapters/vercel-ai.cjs" }
+ "import": {
+ "types": "./dist/adapters/vercel-ai.d.ts",
+ "default": "./dist/adapters/vercel-ai.js"
+ },
+ "require": {
+ "types": "./dist/adapters/vercel-ai.d.cts",
+ "default": "./dist/adapters/vercel-ai.cjs"
+ }
},
"./openai": {
- "import": { "types": "./dist/adapters/openai.d.ts", "default": "./dist/adapters/openai.js" },
- "require": { "types": "./dist/adapters/openai.d.cts", "default": "./dist/adapters/openai.cjs" }
+ "import": {
+ "types": "./dist/adapters/openai.d.ts",
+ "default": "./dist/adapters/openai.js"
+ },
+ "require": {
+ "types": "./dist/adapters/openai.d.cts",
+ "default": "./dist/adapters/openai.cjs"
+ }
},
"./anthropic": {
- "import": { "types": "./dist/adapters/anthropic.d.ts", "default": "./dist/adapters/anthropic.js" },
- "require": { "types": "./dist/adapters/anthropic.d.cts", "default": "./dist/adapters/anthropic.cjs" }
+ "import": {
+ "types": "./dist/adapters/anthropic.d.ts",
+ "default": "./dist/adapters/anthropic.js"
+ },
+ "require": {
+ "types": "./dist/adapters/anthropic.d.cts",
+ "default": "./dist/adapters/anthropic.cjs"
+ }
}
},
- "engines": { "node": ">=18.0.0" },
+ "engines": {
+ "node": ">=18.0.0"
+ },
"scripts": {
"build": "tsup",
"test": "vitest run",
@@ -33,25 +63,36 @@
"typecheck": "tsc --noEmit"
},
"peerDependencies": {
- "ai": ">=6.0.0",
"@ai-sdk/provider": ">=1.0.0",
- "openai": ">=4.0.0",
- "@anthropic-ai/sdk": ">=0.30.0"
+ "@anthropic-ai/sdk": ">=0.30.0",
+ "ai": ">=6.0.0",
+ "openai": ">=4.0.0"
},
"peerDependenciesMeta": {
- "ai": { "optional": true },
- "@ai-sdk/provider": { "optional": true },
- "openai": { "optional": true },
- "@anthropic-ai/sdk": { "optional": true }
+ "ai": {
+ "optional": true
+ },
+ "@ai-sdk/provider": {
+ "optional": true
+ },
+ "openai": {
+ "optional": true
+ },
+ "@anthropic-ai/sdk": {
+ "optional": true
+ }
},
"devDependencies": {
- "typescript": "^5.5.0",
- "vitest": "^2.0.0",
- "tsup": "^8.0.0",
+ "@ai-sdk/anthropic": "^3.0.64",
+ "@ai-sdk/openai": "^3.0.48",
"@ai-sdk/provider": "^1.0.0",
+ "@anthropic-ai/sdk": "^0.39.0",
"ai": "^6.0.0",
+ "dotenv": "^17.3.1",
"openai": "^4.80.0",
- "@anthropic-ai/sdk": "^0.39.0"
+ "tsup": "^8.0.0",
+ "typescript": "^5.5.0",
+ "vitest": "^2.0.0"
},
"license": "Apache-2.0"
}
diff --git a/sdk/typescript/src/compress.ts b/sdk/typescript/src/compress.ts
index 3b30a09ce..5c2d3d188 100644
--- a/sdk/typescript/src/compress.ts
+++ b/sdk/typescript/src/compress.ts
@@ -1,32 +1,51 @@
import { HeadroomClient } from "./client.js";
import type {
- OpenAIMessage,
CompressResult,
CompressOptions,
} from "./types.js";
+import { detectFormat, toOpenAI, fromOpenAI } from "./utils/format.js";
/**
* Compress an array of messages using the Headroom proxy or cloud.
*
- * This is the main entry point for the SDK. It accepts OpenAI-format messages,
- * sends them to the Headroom compression pipeline, and returns compressed
- * messages with stats.
+ * Accepts messages in any format: OpenAI, Anthropic, Vercel AI SDK, or Google Gemini.
+ * Detects the format automatically, compresses via the proxy, and returns
+ * compressed messages in the same format as the input.
*
* @example
* ```typescript
* import { compress } from 'headroom-ai';
*
- * const result = await compress(messages, { model: 'gpt-4o' });
- * console.log(`Saved ${result.tokensSaved} tokens`);
+ * // Works with any message format:
+ * const result = await compress(openaiMessages, { model: 'gpt-4o' });
+ * const result = await compress(anthropicMessages, { model: 'claude-sonnet-4-5-20250929' });
+ * const result = await compress(vercelMessages, { model: 'gpt-4o' });
+ * const result = await compress(geminiContents, { model: 'gemini-2.0-flash' });
+ *
+ * // result.messages is in the same format as input
* ```
*/
export async function compress(
- messages: OpenAIMessage[],
+ messages: any[],
options: CompressOptions = {},
): Promise<CompressResult> {
const { client: providedClient, model, ...clientOptions } = options;
+ // Detect input format
+ const inputFormat = detectFormat(messages);
+
+ // Convert to OpenAI format (the proxy's lingua franca)
+ const openaiMessages = toOpenAI(messages);
+
+ // Compress via proxy
const client = providedClient ?? new HeadroomClient(clientOptions);
+ const result = await client.compress(openaiMessages, { model });
+
+ // Convert compressed messages back to original format
+ const outputMessages = fromOpenAI(result.messages, inputFormat);
- return client.compress(messages, { model });
+ return {
+ ...result,
+ messages: outputMessages,
+ };
}
diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.ts
index 9e71ffd2a..3f50d90ef 100644
--- a/sdk/typescript/src/index.ts
+++ b/sdk/typescript/src/index.ts
@@ -1,5 +1,7 @@
export { compress } from "./compress.js";
export { HeadroomClient } from "./client.js";
+export { detectFormat, toOpenAI, fromOpenAI } from "./utils/format.js";
+export type { MessageFormat } from "./utils/format.js";
export type {
TextContentPart,
diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.ts
index f6abdefdf..cc25329e4 100644
--- a/sdk/typescript/src/types.ts
+++ b/sdk/typescript/src/types.ts
@@ -59,7 +59,8 @@ export interface CompressOptions {
}
export interface CompressResult {
- messages: OpenAIMessage[];
+ /** Compressed messages in the same format as input. */
+ messages: any[];
tokensBefore: number;
tokensAfter: number;
tokensSaved: number;
diff --git a/sdk/typescript/src/utils/format.ts b/sdk/typescript/src/utils/format.ts
index 65501312a..ef84d9a56 100644
--- a/sdk/typescript/src/utils/format.ts
+++ b/sdk/typescript/src/utils/format.ts
@@ -1,11 +1,14 @@
/**
- * Convert between Vercel AI SDK ModelMessage format and OpenAI message format.
+ * Universal message format detection and conversion.
*
- * Vercel uses typed content parts (TextPart, ToolCallPart, ToolResultPart, etc.)
- * OpenAI uses { role, content, tool_calls, tool_call_id }.
+ * Supports 4 formats:
+ * - OpenAI: { role, content, tool_calls?, tool_call_id? }
+ * - Anthropic: { role, content (string | ContentBlock[]) } with tool_use/tool_result blocks
+ * - Vercel AI SDK: { role, content (string | Part[]) } with tool-call/tool-result parts
+ * - Google Gemini: { role, parts[] } with functionCall/functionResponse
*
- * We use loose types (any) for Vercel messages to avoid forcing users to install
- * @ai-sdk/provider. The adapter (vercel-ai.ts) provides the typed wrapper.
+ * Detection is structural (unique keys/values per format), not heuristic.
+ * Conversion always targets OpenAI format (the proxy's lingua franca).
*/
import type {
@@ -15,45 +18,216 @@ import type {
} from "../types.js";
/* eslint-disable @typescript-eslint/no-explicit-any */
-type VercelMessage = any;
+
+export type MessageFormat = "openai" | "anthropic" | "vercel" | "gemini";
/**
- * Convert Vercel AI SDK ModelMessage[] to OpenAI message format.
+ * Detect which format an array of messages is in.
*
- * Handles: system, user (text + image), assistant (text + tool-call),
- * tool (tool-result). Skips reasoning and file parts (non-compressible).
+ * Uses structural markers unique to each format:
+ * - Gemini: messages have `parts` field (not `content`)
+ * - Vercel: content parts use hyphenated types: `tool-call`, `tool-result`
+ * - Anthropic: content blocks use underscored types: `tool_use`, `tool_result`
+ * - OpenAI: assistant messages have `tool_calls` field; tool messages have `tool_call_id`
*/
-export function vercelToOpenAI(messages: VercelMessage[]): OpenAIMessage[] {
+export function detectFormat(messages: any[]): MessageFormat {
+ for (const msg of messages) {
+ // Gemini: uses `parts` instead of `content`
+ if ("parts" in msg && !("content" in msg)) return "gemini";
+
+ // Gemini: role "model" instead of "assistant"
+ if (msg.role === "model") return "gemini";
+
+ // OpenAI: tool_calls on assistant messages
+ if (msg.tool_calls && msg.role === "assistant") return "openai";
+
+ // OpenAI: tool role with tool_call_id
+ if (msg.role === "tool" && "tool_call_id" in msg && typeof msg.content === "string") return "openai";
+
+ // Check content blocks/parts for format-specific markers
+ if (Array.isArray(msg.content)) {
+ for (const part of msg.content) {
+ // Vercel: hyphenated types
+ if (part.type === "tool-call" || part.type === "tool-result") return "vercel";
+ // Anthropic: underscored types
+ if (part.type === "tool_use" || part.type === "tool_result") return "anthropic";
+ // Anthropic: image with source.type
+ if (part.type === "image" && part.source?.type) return "anthropic";
+ }
+ }
+ }
+
+ // Default: OpenAI (simple {role, content: string} is valid OpenAI)
+ return "openai";
+}
+
+// ============================================================
+// OpenAI ↔ OpenAI (passthrough)
+// ============================================================
+
+// No conversion needed — this is the canonical format.
+
+// ============================================================
+// Anthropic → OpenAI
+// ============================================================
+
+export function anthropicToOpenAI(messages: any[]): OpenAIMessage[] {
const result: OpenAIMessage[] = [];
+ for (const msg of messages) {
+ if (msg.role === "user") {
+ if (typeof msg.content === "string") {
+ result.push({ role: "user", content: msg.content });
+ continue;
+ }
+ if (Array.isArray(msg.content)) {
+ const textBlocks = msg.content.filter((b: any) => b.type === "text");
+ const toolResults = msg.content.filter((b: any) => b.type === "tool_result");
+
+ if (textBlocks.length > 0) {
+ result.push({
+ role: "user",
+ content: textBlocks.map((b: any) => b.text).join("\n"),
+ });
+ }
+ for (const tr of toolResults) {
+ const content = typeof tr.content === "string"
+ ? tr.content
+ : Array.isArray(tr.content)
+ ? tr.content.map((b: any) => b.text ?? JSON.stringify(b)).join("\n")
+ : JSON.stringify(tr.content);
+ result.push({
+ role: "tool",
+ content,
+ tool_call_id: tr.tool_use_id,
+ });
+ }
+ }
+ continue;
+ }
+
+ if (msg.role === "assistant") {
+ if (typeof msg.content === "string") {
+ result.push({ role: "assistant", content: msg.content });
+ continue;
+ }
+ if (Array.isArray(msg.content)) {
+ const textBlocks = msg.content.filter((b: any) => b.type === "text");
+ const toolUseBlocks = msg.content.filter((b: any) => b.type === "tool_use");
+
+ const content = textBlocks.length > 0
+ ? textBlocks.map((b: any) => b.text).join("\n")
+ : null;
+
+ const openaiMsg: AssistantMessage = { role: "assistant", content };
+ if (toolUseBlocks.length > 0) {
+ openaiMsg.tool_calls = toolUseBlocks.map((b: any): ToolCall => ({
+ id: b.id,
+ type: "function",
+ function: {
+ name: b.name,
+ arguments: typeof b.input === "string" ? b.input : JSON.stringify(b.input),
+ },
+ }));
+ }
+ result.push(openaiMsg);
+ }
+ continue;
+ }
+ }
+
+ return result;
+}
+
+export function openAIToAnthropic(messages: OpenAIMessage[]): any[] {
+ const result: any[] = [];
+
for (const msg of messages) {
if (msg.role === "system") {
- result.push({ role: "system", content: msg.content });
+ // Anthropic system is top-level, but if it appears in messages, convert to user
+ result.push({ role: "user", content: msg.content });
continue;
}
if (msg.role === "user") {
- const parts = Array.isArray(msg.content) ? msg.content : [msg.content];
+ if (typeof msg.content === "string") {
+ result.push({ role: "user", content: msg.content });
+ } else if (Array.isArray(msg.content)) {
+ result.push({
+ role: "user",
+ content: msg.content.map((p) =>
+ p.type === "text" ? { type: "text", text: p.text } : { type: "text", text: "" },
+ ),
+ });
+ }
+ continue;
+ }
+
+ if (msg.role === "assistant") {
+ const blocks: any[] = [];
+ if (msg.content) blocks.push({ type: "text", text: msg.content });
+ if (msg.tool_calls) {
+ for (const tc of msg.tool_calls) {
+ blocks.push({
+ type: "tool_use",
+ id: tc.id,
+ name: tc.function.name,
+ input: JSON.parse(tc.function.arguments),
+ });
+ }
+ }
+ result.push({
+ role: "assistant",
+ content: blocks.length === 1 && blocks[0].type === "text" ? blocks[0].text : blocks,
+ });
+ continue;
+ }
+
+ if (msg.role === "tool") {
+ result.push({
+ role: "user",
+ content: [
+ { type: "tool_result", tool_use_id: msg.tool_call_id, content: msg.content },
+ ],
+ });
+ continue;
+ }
+ }
+
+ return result;
+}
+
+// ============================================================
+// Vercel AI SDK → OpenAI
+// ============================================================
+
+export function vercelToOpenAI(messages: any[]): OpenAIMessage[] {
+ const result: OpenAIMessage[] = [];
+
+ for (const msg of messages) {
+ if (msg.role === "system") {
+ result.push({ role: "system", content: typeof msg.content === "string" ? msg.content : String(msg.content) });
+ continue;
+ }
+
+ if (msg.role === "user") {
+ if (typeof msg.content === "string") {
+ result.push({ role: "user", content: msg.content });
+ continue;
+ }
+ const parts = Array.isArray(msg.content) ? msg.content : [];
const textParts = parts.filter((p: any) => p.type === "text");
const imageParts = parts.filter((p: any) => p.type === "image");
if (imageParts.length === 0 && textParts.length > 0) {
- // Text-only: flatten to string
- result.push({
- role: "user",
- content: textParts.map((p: any) => p.text).join(""),
- });
+ result.push({ role: "user", content: textParts.map((p: any) => p.text).join("") });
} else {
- // Mixed content: use content parts array
const openaiParts = parts
.filter((p: any) => p.type === "text" || p.type === "image")
.map((p: any) => {
- if (p.type === "text") {
- return { type: "text" as const, text: p.text };
- }
+ if (p.type === "text") return { type: "text" as const, text: p.text };
if (p.type === "image") {
- const url =
- p.image instanceof URL ? p.image.toString() : String(p.image);
+ const url = p.image instanceof URL ? p.image.toString() : String(p.image);
return { type: "image_url" as const, image_url: { url } };
}
return { type: "text" as const, text: "" };
@@ -64,30 +238,24 @@ export function vercelToOpenAI(messages: VercelMessage[]): OpenAIMessage[] {
}
if (msg.role === "assistant") {
+ if (typeof msg.content === "string") {
+ result.push({ role: "assistant", content: msg.content });
+ continue;
+ }
const parts = Array.isArray(msg.content) ? msg.content : [];
const textParts = parts.filter((p: any) => p.type === "text");
const toolCallParts = parts.filter((p: any) => p.type === "tool-call");
- const content =
- textParts.length > 0
- ? textParts.map((p: any) => p.text).join("")
- : null;
-
+ const content = textParts.length > 0 ? textParts.map((p: any) => p.text).join("") : null;
const openaiMsg: AssistantMessage = { role: "assistant", content };
if (toolCallParts.length > 0) {
- openaiMsg.tool_calls = toolCallParts.map(
- (p: any): ToolCall => ({
- id: p.toolCallId,
- type: "function",
- function: {
- name: p.toolName,
- arguments: JSON.stringify(p.args),
- },
- }),
- );
+ openaiMsg.tool_calls = toolCallParts.map((p: any): ToolCall => ({
+ id: p.toolCallId,
+ type: "function",
+ function: { name: p.toolName, arguments: JSON.stringify(p.args) },
+ }));
}
-
result.push(openaiMsg);
continue;
}
@@ -96,12 +264,21 @@ export function vercelToOpenAI(messages: VercelMessage[]): OpenAIMessage[] {
const parts = Array.isArray(msg.content) ? msg.content : [];
for (const part of parts) {
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);
+ } else {
+ contentStr = "";
+ }
result.push({
role: "tool",
- content:
- typeof part.result === "string"
- ? part.result
- : JSON.stringify(part.result),
+ content: contentStr,
tool_call_id: part.toolCallId,
});
}
@@ -113,11 +290,8 @@ export function vercelToOpenAI(messages: VercelMessage[]): OpenAIMessage[] {
return result;
}
-/**
- * Convert OpenAI message format back to Vercel AI SDK ModelMessage[].
- */
-export function openAIToVercel(messages: OpenAIMessage[]): VercelMessage[] {
- const result: VercelMessage[] = [];
+export function openAIToVercel(messages: OpenAIMessage[]): any[] {
+ const result: any[] = [];
for (const msg of messages) {
if (msg.role === "system") {
@@ -127,16 +301,11 @@ export function openAIToVercel(messages: OpenAIMessage[]): VercelMessage[] {
if (msg.role === "user") {
if (typeof msg.content === "string") {
- result.push({
- role: "user",
- content: [{ type: "text", text: msg.content }],
- });
+ result.push({ role: "user", content: [{ type: "text", text: msg.content }] });
} else if (Array.isArray(msg.content)) {
const parts = msg.content.map((p) => {
if (p.type === "text") return { type: "text", text: p.text };
- if (p.type === "image_url") {
- return { type: "image", image: new URL(p.image_url.url) };
- }
+ if (p.type === "image_url") return { type: "image", image: new URL(p.image_url.url) };
return { type: "text", text: "" };
});
result.push({ role: "user", content: parts });
@@ -146,16 +315,16 @@ export function openAIToVercel(messages: OpenAIMessage[]): VercelMessage[] {
if (msg.role === "assistant") {
const parts: any[] = [];
- if (msg.content) {
- parts.push({ type: "text", text: msg.content });
- }
+ if (msg.content) parts.push({ type: "text", text: msg.content });
if (msg.tool_calls) {
for (const tc of msg.tool_calls) {
+ let args: any;
+ try { args = JSON.parse(tc.function.arguments); } catch { args = tc.function.arguments ?? {}; }
parts.push({
type: "tool-call",
toolCallId: tc.id,
toolName: tc.function.name,
- args: JSON.parse(tc.function.arguments),
+ args,
});
}
}
@@ -164,22 +333,120 @@ export function openAIToVercel(messages: OpenAIMessage[]): VercelMessage[] {
}
if (msg.role === "tool") {
- let parsedResult: any;
- try {
- parsedResult = JSON.parse(msg.content);
- } catch {
- parsedResult = msg.content;
- }
+ 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 };
result.push({
role: "tool",
- content: [
- {
- type: "tool-result",
- toolCallId: msg.tool_call_id,
- toolName: "unknown",
- result: parsedResult,
+ content: [{
+ type: "tool-result",
+ toolCallId: msg.tool_call_id,
+ toolName: "unknown",
+ output,
+ }],
+ });
+ continue;
+ }
+ }
+
+ return result;
+}
+
+// ============================================================
+// Google Gemini → OpenAI
+// ============================================================
+
+export function geminiToOpenAI(messages: any[]): OpenAIMessage[] {
+ const result: OpenAIMessage[] = [];
+
+ for (const msg of messages) {
+ const role = msg.role === "model" ? "assistant" : "user";
+ const parts: any[] = msg.parts ?? [];
+
+ if (role === "user") {
+ // Check for functionResponse parts
+ const funcResponses = parts.filter((p: any) => p.functionResponse);
+ const textParts = parts.filter((p: any) => p.text !== undefined);
+
+ if (textParts.length > 0) {
+ result.push({ role: "user", content: textParts.map((p: any) => p.text).join("\n") });
+ }
+ for (const fr of funcResponses) {
+ result.push({
+ role: "tool",
+ content: JSON.stringify(fr.functionResponse.response),
+ tool_call_id: `gemini_${fr.functionResponse.name}`,
+ });
+ }
+ continue;
+ }
+
+ if (role === "assistant") {
+ const textParts = parts.filter((p: any) => p.text !== undefined);
+ const funcCalls = parts.filter((p: any) => p.functionCall);
+
+ const content = textParts.length > 0 ? textParts.map((p: any) => p.text).join("\n") : null;
+ const openaiMsg: AssistantMessage = { role: "assistant", content };
+
+ if (funcCalls.length > 0) {
+ openaiMsg.tool_calls = funcCalls.map((p: any): ToolCall => ({
+ id: `gemini_${p.functionCall.name}`,
+ type: "function",
+ function: {
+ name: p.functionCall.name,
+ arguments: JSON.stringify(p.functionCall.args),
},
- ],
+ }));
+ }
+ result.push(openaiMsg);
+ continue;
+ }
+ }
+
+ return result;
+}
+
+export function openAIToGemini(messages: OpenAIMessage[]): any[] {
+ const result: any[] = [];
+
+ for (const msg of messages) {
+ if (msg.role === "system") {
+ // Gemini system is top-level; if in messages, convert to user
+ result.push({ role: "user", parts: [{ text: msg.content }] });
+ continue;
+ }
+
+ if (msg.role === "user") {
+ const text = typeof msg.content === "string"
+ ? msg.content
+ : (msg.content ?? []).filter((p) => p.type === "text").map((p) => (p as any).text).join("\n");
+ result.push({ role: "user", parts: [{ text }] });
+ continue;
+ }
+
+ if (msg.role === "assistant") {
+ const parts: any[] = [];
+ if (msg.content) parts.push({ text: msg.content });
+ if (msg.tool_calls) {
+ for (const tc of msg.tool_calls) {
+ parts.push({
+ functionCall: { name: tc.function.name, args: JSON.parse(tc.function.arguments) },
+ });
+ }
+ }
+ result.push({ role: "model", parts });
+ continue;
+ }
+
+ if (msg.role === "tool") {
+ let response: any;
+ try { response = JSON.parse(msg.content); } catch { response = { result: msg.content }; }
+ result.push({
+ role: "user",
+ parts: [{ functionResponse: { name: msg.tool_call_id?.replace("gemini_", "") ?? "unknown", response } }],
});
continue;
}
@@ -187,3 +454,26 @@ export function openAIToVercel(messages: OpenAIMessage[]): VercelMessage[] {
return result;
}
+
+// ============================================================
+// Universal: any format → OpenAI, and OpenAI → original format
+// ============================================================
+
+export function toOpenAI(messages: any[]): OpenAIMessage[] {
+ const format = detectFormat(messages);
+ switch (format) {
+ case "openai": return messages as OpenAIMessage[];
+ case "anthropic": return anthropicToOpenAI(messages);
+ case "vercel": return vercelToOpenAI(messages);
+ case "gemini": return geminiToOpenAI(messages);
+ }
+}
+
+export function fromOpenAI(messages: OpenAIMessage[], targetFormat: MessageFormat): any[] {
+ switch (targetFormat) {
+ case "openai": return messages;
+ case "anthropic": return openAIToAnthropic(messages);
+ case "vercel": return openAIToVercel(messages);
+ case "gemini": return openAIToGemini(messages);
+ }
+}
diff --git a/sdk/typescript/test/format-integration.test.ts b/sdk/typescript/test/format-integration.test.ts
new file mode 100644
index 000000000..d43aa74ac
--- /dev/null
+++ b/sdk/typescript/test/format-integration.test.ts
@@ -0,0 +1,252 @@
+/**
+ * Integration tests: compress() with ALL message formats against real proxy.
+ *
+ * Tests that compress() auto-detects and round-trips:
+ * - OpenAI format
+ * - Anthropic format
+ * - Vercel AI SDK format
+ * - Google Gemini format
+ *
+ * Requires: proxy running on http://localhost:8787
+ * Run with: HEADROOM_INTEGRATION=1 npx vitest run test/format-integration.test.ts
+ */
+import { describe, it, expect, beforeAll } from "vitest";
+import { config } from "dotenv";
+import { resolve } from "path";
+
+config({ path: resolve(__dirname, "../../../.env") });
+
+const PROXY_URL = "http://localhost:8787";
+const RUN_INTEGRATION = process.env.HEADROOM_INTEGRATION === "1";
+
+// Realistic large tool output — needs enough content to cross compression threshold
+function bigToolOutput(n: number): string {
+ const items = Array.from({ length: n }, (_, i) => ({
+ id: i + 1,
+ name: `item_${i + 1}`,
+ status: i % 10 === 0 ? "error" : "ok",
+ value: Math.round(Math.random() * 1000),
+ timestamp: new Date(Date.now() - i * 60000).toISOString(),
+ metadata: {
+ category: ["A", "B", "C"][i % 3],
+ tags: [`tag_${i % 5}`, `tag_${(i + 1) % 5}`],
+ description: `This is item number ${i + 1} with some descriptive text that takes up tokens`,
+ },
+ }));
+ return JSON.stringify({ results: items, total: n, page: 1 });
+}
+
+describe.skipIf(!RUN_INTEGRATION)("Format Integration: compress() auto-detects all formats", () => {
+ beforeAll(async () => {
+ const res = await fetch(`${PROXY_URL}/health`);
+ if (!res.ok) throw new Error(`Proxy not running at ${PROXY_URL}`);
+ });
+
+ // ======== OpenAI FORMAT ========
+ it("OpenAI format: detects, compresses, returns OpenAI format", async () => {
+ const { compress } = await import("../src/compress.js");
+ const { detectFormat } = await import("../src/utils/format.js");
+
+ const messages = [
+ { role: "system", content: "You are a data analyst" },
+ { role: "user", content: "Search the database" },
+ {
+ role: "assistant",
+ content: null,
+ tool_calls: [{
+ id: "call_abc",
+ type: "function",
+ function: { name: "db_query", arguments: '{"sql":"SELECT * FROM items"}' },
+ }],
+ },
+ {
+ role: "tool",
+ tool_call_id: "call_abc",
+ content: bigToolOutput(80),
+ },
+ { role: "user", content: "Summarize the errors" },
+ ];
+
+ expect(detectFormat(messages)).toBe("openai");
+
+ const result = await compress(messages, { model: "gpt-4o", baseUrl: PROXY_URL });
+
+ expect(result.compressed).toBe(true);
+ expect(result.tokensBefore).toBeGreaterThan(0);
+ expect(result.tokensSaved).toBeGreaterThan(0);
+
+ // Output should still be OpenAI format
+ const out = result.messages;
+ expect(out[0].role).toBe("system");
+ expect(typeof out[0].content).toBe("string");
+ // tool messages should have tool_call_id (OpenAI marker)
+ const toolMsg = out.find((m: any) => m.role === "tool");
+ if (toolMsg) {
+ expect("tool_call_id" in toolMsg).toBe(true);
+ expect(typeof toolMsg.content).toBe("string");
+ }
+
+ console.log(` OpenAI: ${result.tokensBefore} → ${result.tokensAfter} (saved ${result.tokensSaved})`);
+ });
+
+ // ======== ANTHROPIC FORMAT ========
+ it("Anthropic format: detects, compresses, returns Anthropic format", async () => {
+ const { compress } = await import("../src/compress.js");
+ const { detectFormat } = await import("../src/utils/format.js");
+
+ const messages = [
+ { role: "user", content: "Search the database" },
+ {
+ role: "assistant",
+ content: [
+ { type: "tool_use", id: "toolu_01ABC", name: "db_query", input: { sql: "SELECT * FROM items" } },
+ ],
+ },
+ {
+ role: "user",
+ content: [
+ { type: "tool_result", tool_use_id: "toolu_01ABC", content: bigToolOutput(80) },
+ ],
+ },
+ { role: "user", content: "Summarize the errors" },
+ ];
+
+ expect(detectFormat(messages)).toBe("anthropic");
+
+ const result = await compress(messages, { model: "claude-sonnet-4-5-20250929", baseUrl: PROXY_URL });
+
+ expect(result.compressed).toBe(true);
+ expect(result.tokensBefore).toBeGreaterThan(0);
+
+ // Output should be back in Anthropic format
+ const out = result.messages;
+ // Anthropic tool results are inside user messages with type: "tool_result"
+ const hasToolResult = out.some((m: any) =>
+ Array.isArray(m.content) && m.content.some((b: any) => b.type === "tool_result"),
+ );
+ // Tool use blocks should have type: "tool_use" (underscore, not hyphen)
+ const hasToolUse = out.some((m: any) =>
+ Array.isArray(m.content) && m.content.some((b: any) => b.type === "tool_use"),
+ );
+
+ if (hasToolResult) {
+ expect(hasToolResult).toBe(true); // Anthropic format preserved
+ }
+ if (hasToolUse) {
+ expect(hasToolUse).toBe(true);
+ }
+
+ console.log(` Anthropic: ${result.tokensBefore} → ${result.tokensAfter} (saved ${result.tokensSaved})`);
+ });
+
+ // ======== VERCEL AI SDK FORMAT ========
+ it("Vercel AI SDK format: detects, compresses, returns Vercel format", async () => {
+ const { compress } = await import("../src/compress.js");
+ const { detectFormat } = await import("../src/utils/format.js");
+
+ const messages = [
+ { role: "system", content: "You are a data analyst" },
+ { role: "user", content: [{ type: "text", text: "Search the database" }] },
+ {
+ role: "assistant",
+ content: [
+ { type: "text", text: "Let me query that" },
+ { type: "tool-call", toolCallId: "tc_1", toolName: "db_query", args: { sql: "SELECT *" } },
+ ],
+ },
+ {
+ role: "tool",
+ content: [
+ { type: "tool-result", toolCallId: "tc_1", toolName: "db_query", result: JSON.parse(bigToolOutput(80)) },
+ ],
+ },
+ { role: "user", content: [{ type: "text", text: "Summarize the errors" }] },
+ ];
+
+ expect(detectFormat(messages)).toBe("vercel");
+
+ const result = await compress(messages, { model: "gpt-4o", baseUrl: PROXY_URL });
+
+ expect(result.compressed).toBe(true);
+ expect(result.tokensBefore).toBeGreaterThan(0);
+
+ // Output should be back in Vercel format
+ const out = result.messages;
+ expect(out[0].role).toBe("system");
+ // Vercel user messages have content as array of parts
+ const userMsg = out.find((m: any) => m.role === "user");
+ if (userMsg && Array.isArray(userMsg.content)) {
+ expect(userMsg.content[0].type).toBe("text");
+ }
+ // Vercel tool results use "tool-result" (hyphenated)
+ const toolMsg = out.find((m: any) => m.role === "tool");
+ if (toolMsg && Array.isArray(toolMsg.content)) {
+ expect(toolMsg.content[0].type).toBe("tool-result");
+ }
+
+ console.log(` Vercel: ${result.tokensBefore} → ${result.tokensAfter} (saved ${result.tokensSaved})`);
+ });
+
+ // ======== GOOGLE GEMINI FORMAT ========
+ it("Gemini format: detects, compresses, returns Gemini format", async () => {
+ const { compress } = await import("../src/compress.js");
+ const { detectFormat } = await import("../src/utils/format.js");
+
+ const messages = [
+ { role: "user", parts: [{ text: "Search the database" }] },
+ {
+ role: "model",
+ parts: [
+ { functionCall: { name: "db_query", args: { sql: "SELECT * FROM items" } } },
+ ],
+ },
+ {
+ role: "user",
+ parts: [
+ { functionResponse: { name: "db_query", response: JSON.parse(bigToolOutput(80)) } },
+ ],
+ },
+ { role: "user", parts: [{ text: "Summarize the errors" }] },
+ ];
+
+ expect(detectFormat(messages)).toBe("gemini");
+
+ const result = await compress(messages, { model: "gemini-2.0-flash", baseUrl: PROXY_URL });
+
+ expect(result.compressed).toBe(true);
+ expect(result.tokensBefore).toBeGreaterThan(0);
+
+ // Output should be back in Gemini format
+ const out = result.messages;
+ // Gemini uses "parts" not "content"
+ expect(out[0].parts).toBeDefined();
+ // Gemini uses "model" role not "assistant"
+ const modelMsg = out.find((m: any) => m.role === "model");
+ if (modelMsg) {
+ expect(modelMsg.parts).toBeDefined();
+ }
+
+ console.log(` Gemini: ${result.tokensBefore} → ${result.tokensAfter} (saved ${result.tokensSaved})`);
+ });
+
+ // ======== SIMPLE STRING MESSAGES (ambiguous → defaults to OpenAI) ========
+ it("Simple string messages work (default OpenAI detection)", async () => {
+ const { compress } = await import("../src/compress.js");
+ const { detectFormat } = await import("../src/utils/format.js");
+
+ const messages = [
+ { role: "user", content: "Hello" },
+ { role: "assistant", content: "Hi there" },
+ { role: "user", content: "What's the weather?" },
+ ];
+
+ expect(detectFormat(messages)).toBe("openai");
+
+ const result = await compress(messages, { model: "gpt-4o", baseUrl: PROXY_URL });
+
+ // Small messages may not compress but should round-trip cleanly
+ expect(result.messages).toHaveLength(3);
+ expect(result.messages[0].role).toBe("user");
+ expect(result.messages[0].content).toBe("Hello");
+ });
+});
diff --git a/sdk/typescript/test/utils/detect-format.test.ts b/sdk/typescript/test/utils/detect-format.test.ts
new file mode 100644
index 000000000..d4f49c482
--- /dev/null
+++ b/sdk/typescript/test/utils/detect-format.test.ts
@@ -0,0 +1,133 @@
+import { describe, it, expect } from "vitest";
+import { detectFormat } from "../../src/utils/format.js";
+
+describe("detectFormat", () => {
+ // ---- OpenAI ----
+ it("detects OpenAI: assistant with tool_calls field", () => {
+ const messages = [
+ { role: "user", content: "hello" },
+ {
+ role: "assistant",
+ content: null,
+ tool_calls: [{ id: "call_1", type: "function", function: { name: "search", arguments: "{}" } }],
+ },
+ ];
+ expect(detectFormat(messages)).toBe("openai");
+ });
+
+ it("detects OpenAI: tool role with tool_call_id and string content", () => {
+ const messages = [
+ { role: "tool", content: '{"result": true}', tool_call_id: "call_1" },
+ ];
+ expect(detectFormat(messages)).toBe("openai");
+ });
+
+ it("defaults to OpenAI for simple string messages", () => {
+ const messages = [
+ { role: "user", content: "hello" },
+ { role: "assistant", content: "hi" },
+ ];
+ expect(detectFormat(messages)).toBe("openai");
+ });
+
+ it("defaults to OpenAI for empty array", () => {
+ expect(detectFormat([])).toBe("openai");
+ });
+
+ // ---- Anthropic ----
+ it("detects Anthropic: tool_use content block", () => {
+ const messages = [
+ {
+ role: "assistant",
+ content: [{ type: "tool_use", id: "toolu_01", name: "search", input: {} }],
+ },
+ ];
+ expect(detectFormat(messages)).toBe("anthropic");
+ });
+
+ it("detects Anthropic: tool_result content block", () => {
+ const messages = [
+ {
+ role: "user",
+ content: [{ type: "tool_result", tool_use_id: "toolu_01", content: "result" }],
+ },
+ ];
+ expect(detectFormat(messages)).toBe("anthropic");
+ });
+
+ it("detects Anthropic: image with source.type", () => {
+ const messages = [
+ {
+ role: "user",
+ content: [
+ { type: "text", text: "What's this?" },
+ { type: "image", source: { type: "base64", media_type: "image/jpeg", data: "..." } },
+ ],
+ },
+ ];
+ expect(detectFormat(messages)).toBe("anthropic");
+ });
+
+ // ---- Vercel AI SDK ----
+ it("detects Vercel: tool-call part (hyphenated)", () => {
+ const messages = [
+ {
+ role: "assistant",
+ content: [{ type: "tool-call", toolCallId: "tc_1", toolName: "search", args: {} }],
+ },
+ ];
+ expect(detectFormat(messages)).toBe("vercel");
+ });
+
+ it("detects Vercel: tool-result part (hyphenated)", () => {
+ const messages = [
+ {
+ role: "tool",
+ content: [{ type: "tool-result", toolCallId: "tc_1", toolName: "search", result: {} }],
+ },
+ ];
+ expect(detectFormat(messages)).toBe("vercel");
+ });
+
+ // ---- Google Gemini ----
+ it("detects Gemini: parts field instead of content", () => {
+ const messages = [
+ { role: "user", parts: [{ text: "hello" }] },
+ ];
+ expect(detectFormat(messages)).toBe("gemini");
+ });
+
+ it("detects Gemini: model role", () => {
+ const messages = [
+ { role: "model", parts: [{ text: "hi" }] },
+ ];
+ expect(detectFormat(messages)).toBe("gemini");
+ });
+
+ it("detects Gemini: functionCall in parts", () => {
+ const messages = [
+ { role: "model", parts: [{ functionCall: { name: "search", args: {} } }] },
+ ];
+ expect(detectFormat(messages)).toBe("gemini");
+ });
+
+ it("detects Gemini: functionResponse in parts", () => {
+ const messages = [
+ { role: "user", parts: [{ functionResponse: { name: "search", response: {} } }] },
+ ];
+ expect(detectFormat(messages)).toBe("gemini");
+ });
+
+ // ---- Mixed/edge cases ----
+ it("detects format from first distinguishing message", () => {
+ // First message is ambiguous (plain text), second has Vercel marker
+ const messages = [
+ { role: "user", content: "hello" },
+ {
+ role: "assistant",
+ content: [{ type: "tool-call", toolCallId: "tc_1", toolName: "fn", args: {} }],
+ },
+ ];
+ expect(detectFormat(messages)).toBe("vercel");
+ });
+});
diff --git a/sdk/typescript/test/utils/format.test.ts b/sdk/typescript/test/utils/format.test.ts
index 6ef01ca9c..f463b84e7 100644
--- a/sdk/typescript/test/utils/format.test.ts
+++ b/sdk/typescript/test/utils/format.test.ts
@@ -364,7 +364,7 @@ describe("openAIToVercel", () => {
type: "tool-result",
toolCallId: "tc_1",
toolName: "unknown",
- result: { data: true },
+ output: { type: "json", value: { data: true } },
},
],
},
@@ -380,7 +380,7 @@ describe("openAIToVercel", () => {
},
];
const result = openAIToVercel(msgs);
- expect(result[0].content[0].result).toBe("plain text result");
+ expect(result[0].content[0].output).toEqual({ type: "text", value: "plain text result" });
});
});
diff --git a/sdk/typescript/test/vercel-ai-e2e.test.ts b/sdk/typescript/test/vercel-ai-e2e.test.ts
new file mode 100644
index 000000000..596921782
--- /dev/null
+++ b/sdk/typescript/test/vercel-ai-e2e.test.ts
@@ -0,0 +1,179 @@
+/**
+ * End-to-end test: Vercel AI SDK + Headroom compress()
+ *
+ * Tests REAL calls through the Vercel AI SDK with Headroom compression.
+ * Uses actual OpenAI and Anthropic API keys.
+ *
+ * Requires:
+ * - Proxy running on http://localhost:8787
+ * - OPENAI_API_KEY in .env
+ * - ANTHROPIC_API_KEY in .env
+ *
+ * Run: HEADROOM_INTEGRATION=1 npx vitest run test/vercel-ai-e2e.test.ts
+ */
+import { describe, it, expect, beforeAll } from "vitest";
+import { config } from "dotenv";
+import { resolve } from "path";
+
+config({ path: resolve(__dirname, "../../../.env") });
+
+const PROXY_URL = "http://localhost:8787";
+const RUN = process.env.HEADROOM_INTEGRATION === "1";
+
+describe.skipIf(!RUN)("E2E: Vercel AI SDK + Headroom", () => {
+ beforeAll(async () => {
+ const res = await fetch(`${PROXY_URL}/health`);
+ if (!res.ok) throw new Error("Proxy not running");
+ if (!process.env.OPENAI_API_KEY) throw new Error("OPENAI_API_KEY not set");
+ });
+
+ it("compress() reduces tokens, then generateText() produces valid response (OpenAI)", { timeout: 30000 }, async () => {
+ const { generateText } = await import("ai");
+ const { createOpenAI } = await import("@ai-sdk/openai");
+ const { compress } = await import("../src/compress.js");
+
+ const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
+
+ // Build a conversation where user pastes a big data dump
+ const serverData = Array.from({ length: 100 }, (_, i) => ({
+ id: i + 1,
+ name: `server-${i + 1}`,
+ status: i % 15 === 0 ? "critical" : i % 5 === 0 ? "warning" : "healthy",
+ cpu: Math.round(Math.random() * 100),
+ memory: Math.round(Math.random() * 100),
+ region: ["us-east-1", "eu-west-1", "ap-southeast-1"][i % 3],
+ description: `Production server ${i + 1} running service-${["auth", "payment", "user", "api"][i % 4]}`,
+ last_alert: i % 15 === 0 ? `Disk usage at ${90 + (i % 10)}%` : null,
+ }));
+
+ // Simple messages — no tool calls (avoids Responses API quirks)
+ const messages: any[] = [
+ { role: "system", content: "You are an SRE assistant. Be concise — one sentence." },
+ {
+ role: "user",
+ content: `Here is our fleet status:\n\n${JSON.stringify(serverData, null, 2)}\n\nWhich servers are critical? Just list the server names.`,
+ },
+ ];
+
+ // Compress
+ const compressed = await compress(messages, {
+ model: "gpt-4o-mini",
+ baseUrl: PROXY_URL,
+ });
+
+ console.log(
+ ` Compressed: ${compressed.tokensBefore} → ${compressed.tokensAfter} tokens ` +
+ `(saved ${compressed.tokensSaved}, ${((1 - compressed.compressionRatio) * 100).toFixed(0)}%)`,
+ );
+
+ expect(compressed.tokensBefore).toBeGreaterThan(0);
+ // User messages are protected by default — compression happens on tool outputs
+ // The important thing is the round-trip works and generateText succeeds
+
+ // Call OpenAI via Vercel AI SDK with compressed messages
+ const { text, usage } = await generateText({
+ model: openai("gpt-4o-mini"),
+ messages: compressed.messages,
+ });
+
+ console.log(` LLM response: "${text.substring(0, 200)}"`);
+ console.log(` Usage: ${usage?.promptTokens} prompt, ${usage?.completionTokens} completion`);
+
+ expect(text.length).toBeGreaterThan(0);
+ // Should mention critical servers
+ expect(text.toLowerCase()).toMatch(/server/i);
+ });
+
+ it("headroomMiddleware() transparently compresses before LLM call (OpenAI)", { timeout: 30000 }, async () => {
+ const { generateText, wrapLanguageModel } = await import("ai");
+ const { createOpenAI } = await import("@ai-sdk/openai");
+ const { headroomMiddleware } = await import("../src/adapters/vercel-ai.js");
+
+ const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
+
+ // Wrap model with Headroom middleware
+ const model = wrapLanguageModel({
+ model: openai("gpt-4o-mini"),
+ middleware: headroomMiddleware({ baseUrl: PROXY_URL }),
+ });
+
+ // Big log dump as user message
+ const logEntries = Array.from({ length: 100 }, (_, i) => ({
+ timestamp: new Date(Date.now() - i * 1000).toISOString(),
+ level: i === 42 ? "FATAL" : i % 10 === 0 ? "ERROR" : "INFO",
+ service: `service-${["auth", "payment", "user", "notification"][i % 4]}`,
+ message: i === 42
+ ? "Connection pool exhausted - max_connections=100 reached, 47 pending requests dropped"
+ : `Request processed in ${Math.round(Math.random() * 500)}ms for endpoint /${["login", "checkout", "profile", "notify"][i % 4]}`,
+ trace_id: `trace-${Math.random().toString(36).substring(2, 10)}`,
+ }));
+
+ // Call generateText — middleware compresses automatically
+ const { text, usage } = await generateText({
+ model,
+ system: "You are an SRE assistant. Identify the single most critical issue. One sentence only.",
+ prompt: `Here are the recent logs:\n\n${JSON.stringify(logEntries, null, 2)}\n\nWhat is the most critical issue?`,
+ });
+
+ console.log(` Middleware response: "${text.substring(0, 200)}"`);
+ console.log(` Usage: ${usage?.promptTokens} prompt, ${usage?.completionTokens} completion`);
+
+ expect(text.length).toBeGreaterThan(0);
+ // Should identify the FATAL error about connection pool
+ expect(text.toLowerCase()).toMatch(/connection|pool|exhaust|fatal/i);
+ });
+
+ it("compress() works end-to-end with Anthropic via Vercel AI SDK", { timeout: 30000 }, async () => {
+ if (!process.env.ANTHROPIC_API_KEY) {
+ console.log(" Skipping: ANTHROPIC_API_KEY not set");
+ return;
+ }
+
+ const { generateText } = await import("ai");
+ const { createAnthropic } = await import("@ai-sdk/anthropic");
+ const { compress } = await import("../src/compress.js");
+
+ const anthropic = createAnthropic({
+ apiKey: process.env.ANTHROPIC_API_KEY,
+ baseURL: "https://api.anthropic.com/v1",
+ });
+
+ const searchResults = Array.from({ length: 80 }, (_, i) => ({
+ title: `Result ${i + 1}: ${["API design patterns", "Database optimization", "Cache invalidation strategies", "Load balancing algorithms"][i % 4]}`,
+ url: `https://docs.example.com/article-${i + 1}`,
+ snippet: `This article covers ${["best practices", "common pitfalls", "advanced techniques", "getting started"][i % 4]} for ${["microservices", "distributed systems", "cloud native", "serverless"][i % 4]} architecture with detailed examples and benchmarks.`,
+ relevance: (100 - i) / 100,
+ }));
+
+ const messages: any[] = [
+ { role: "system", content: "You are a technical writer. Be concise — one sentence per result." },
+ {
+ role: "user",
+ content: `Here are search results:\n\n${JSON.stringify(searchResults, null, 2)}\n\nWhat are the top 3 most relevant results? One sentence each.`,
+ },
+ ];
+
+ // Compress
+ const compressed = await compress(messages, {
+ model: "claude-haiku-4-5",
+ baseUrl: PROXY_URL,
+ });
+
+ console.log(
+ ` Anthropic compressed: ${compressed.tokensBefore} → ${compressed.tokensAfter} ` +
+ `(saved ${compressed.tokensSaved})`,
+ );
+
+ expect(compressed.tokensBefore).toBeGreaterThan(0);
+
+ // Call Anthropic via Vercel AI SDK
+ const { text } = await generateText({
+ model: anthropic("claude-haiku-4-5-20251001"),
+ messages: compressed.messages,
+ maxTokens: 300,
+ });
+
+ console.log(` Anthropic response: "${text.substring(0, 200)}"`);
+ expect(text.length).toBeGreaterThan(0);
+ });
+});