Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,5 @@ jobs:
with:
node-version: 24
cache: npm
# deno fmt until the switch to oxfmt
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- run: npm ci
- run: npm run ci
7 changes: 7 additions & 0 deletions .oxfmtrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "./node_modules/oxfmt/configuration_schema.json",
"printWidth": 92,
"semi": false,
"proseWrap": "always",
"ignorePatterns": ["fixtures/terminal-math.md", "package-lock.json", ".claude"]
}
33 changes: 20 additions & 13 deletions adapters/claude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ function claudeMsg(

function renderClaudeContentBlock(msg: Anthropic.Beta.Messages.BetaContentBlock) {
if (msg.type === "text") {
const citations = (msg.citations || [])
.filter((c) => c.type === "web_search_result_location")
const citations = (msg.citations || []).filter(
(c) => c.type === "web_search_result_location",
)
if (citations.length === 0) return msg.text
const unique = uniqueBy(citations, (c) => c.url)
const sources = unique.map((c) => `[${c.title || c.url}](${c.url})`).join(" · ")
Expand Down Expand Up @@ -79,9 +80,13 @@ export function claudeThinkParams(think: ThinkLevel | undefined): ClaudeThinkPar
return { thinking, output_config: { effort }, max_tokens, effort }
}

export async function claudeCreateMessage(
{ chat, model, config, signal, outputSchema }: ChatInput,
) {
export async function claudeCreateMessage({
chat,
model,
config,
signal,
outputSchema,
}: ChatInput) {
const toolsList: Anthropic.Beta.BetaToolUnion[] = []
if (config.search) {
toolsList.push({ type: "web_search_20260209", name: "web_search", max_uses: 5 })
Expand All @@ -101,24 +106,26 @@ export async function claudeCreateMessage(
cache_control: { type: "ephemeral" },
system: chat.systemPrompt,
messages: chat.messages.map((m) =>
claudeMsg(m.role, m.content, m.role === "user" ? m.image_urls : undefined)
claudeMsg(m.role, m.content, m.role === "user" ? m.image_urls : undefined),
),
max_tokens,
thinking,
output_config: format ? { ...output_config, format } : output_config,
tools: toolsList.length > 0 ? toolsList : undefined,
}
validateRequestSize("Claude", params)
const response = await new Anthropic().beta.messages.create({
...params,
betas: ["code-execution-web-tools-2026-02-09"],
}, { signal })
const response = await new Anthropic().beta.messages.create(
{
...params,
betas: ["code-execution-web-tools-2026-02-09"],
},
{ signal },
)

const searches = response.usage.server_tool_use?.web_search_requests ?? 0

const blocks = response.content.filter((msg) =>
msg.type === "text" || (!prep && msg.type === "server_tool_use")
)
const blocks = response.content
.filter((msg) => msg.type === "text" || (!prep && msg.type === "server_tool_use"))
.map(renderClaudeContentBlock)
.filter((x): x is string => !!x)

Expand Down
57 changes: 35 additions & 22 deletions adapters/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ export function geminiThinkParams(key: string, think: ThinkLevel | undefined) {
: { thinkingLevel: ThinkingLevel.LOW, effort: "low" }
}

export async function geminiCreateMessage(
{ chat, model, config, signal, outputSchema }: ChatInput,
) {
export async function geminiCreateMessage({
chat,
model,
config,
signal,
outputSchema,
}: ChatInput) {
const apiKey = process.env.GEMINI_API_KEY
if (!apiKey) throw Error("GEMINI_API_KEY missing")

Expand All @@ -66,17 +70,19 @@ export async function geminiCreateMessage(
{ urlContext: {} },
...(config.search ? [{ googleSearch: {} }] : []),
]
const contents = await Promise.all(chat.messages.map(async (msg) => {
const parts: Part[] = msg.content ? [{ text: msg.content }] : []
if (msg.role === "user" && msg.image_urls?.length) {
parts.unshift(...await Promise.all(msg.image_urls.map(imageToInlinePart)))
}
return {
// gemini uses model instead of assistant
role: msg.role === "assistant" ? "model" : "user",
parts,
}
}))
const contents = await Promise.all(
chat.messages.map(async (msg) => {
const parts: Part[] = msg.content ? [{ text: msg.content }] : []
if (msg.role === "user" && msg.image_urls?.length) {
parts.unshift(...(await Promise.all(msg.image_urls.map(imageToInlinePart))))
}
return {
// gemini uses model instead of assistant
role: msg.role === "assistant" ? "model" : "user",
parts,
}
}),
)
// The SDK moves systemInstruction/tools out of config and places its remaining
// fields in generationConfig. Measure that REST body after inlining URL images.
validateRequestSize("Gemini", { contents, systemInstruction, tools, generationConfig })
Expand All @@ -89,17 +95,23 @@ export async function geminiCreateMessage(
// console.log(JSON.stringify(result, null, 2))

const parts = result.candidates?.[0].content?.parts ?? []
const reasoning = parts.filter((p) => p.text && p.thought).map((p) => p.text!).join(
"\n\n",
)
let content = parts.filter((p) => p.text && !p.thought).map((p) => p.text!).join("\n\n")
const reasoning = parts
.filter((p) => p.text && p.thought)
.map((p) => p.text!)
.join("\n\n")
let content = parts
.filter((p) => p.text && !p.thought)
.map((p) => p.text!)
.join("\n\n")

const searchResults = result.candidates?.[0].groundingMetadata?.groundingChunks
const searches = searchResults && searchResults.length > 0 ? 1 : 0
const searchResultsMd = searchResults
? "\n\n### Sources\n\n" + searchResults
.filter((chunk) => chunk.web)
.map((chunk) => `- [${chunk.web!.title}](${chunk.web!.uri})`).join("\n")
? "\n\n### Sources\n\n" +
searchResults
.filter((chunk) => chunk.web)
.map((chunk) => `- [${chunk.web!.title}](${chunk.web!.uri})`)
.join("\n")
: ""

if (!prep) content += searchResultsMd
Expand All @@ -108,7 +120,8 @@ export async function geminiCreateMessage(

const tokens = {
input: result.usageMetadata?.promptTokenCount || 0,
output: (result.usageMetadata?.candidatesTokenCount || 0) +
output:
(result.usageMetadata?.candidatesTokenCount || 0) +
(result.usageMetadata?.thoughtsTokenCount || 0),
input_cache_hit: result.usageMetadata?.cachedContentTokenCount || 0,
}
Expand Down
131 changes: 67 additions & 64 deletions adapters/images_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ for (const testCase of cases) {
headers: { "content-type": "image/webp; charset=binary" },
})
}
requestBody = await request.json() as Record<string, unknown>
requestBody = (await request.json()) as Record<string, unknown>
return Response.json(testCase.response)
}
try {
Expand Down Expand Up @@ -197,7 +197,7 @@ test("OpenAI background requests use high image detail and send only the chained
let requestBody: Record<string, unknown> | undefined
process.env["OPENAI_API_KEY"] = "test-key"
globalThis.fetch = async (input, init) => {
requestBody = await new Request(input, init).json() as Record<string, unknown>
requestBody = (await new Request(input, init).json()) as Record<string, unknown>
return Response.json({ id: "new-response", output: [], status: "queued" })
}
try {
Expand Down Expand Up @@ -235,13 +235,15 @@ test("OpenAI background requests use high image detail and send only the chained
],
},
})
assert.deepEqual(requestBody?.input, [{
role: "user",
content: [
{ type: "input_image", image_url: secondImage, detail: "high" },
{ type: "input_image", image_url: imageUrl, detail: "high" },
],
}])
assert.deepEqual(requestBody?.input, [
{
role: "user",
content: [
{ type: "input_image", image_url: secondImage, detail: "high" },
{ type: "input_image", image_url: imageUrl, detail: "high" },
],
},
])
assert.deepEqual(requestBody?.previous_response_id, "previous-response")
assert.deepEqual(requestBody?.background, true)
assert.deepEqual(requestBody?.store, true)
Expand All @@ -252,42 +254,40 @@ test("OpenAI background requests use high image detail and send only the chained
}
})

for (
const testCase of [
{
model: "sonnet",
env: "ANTHROPIC_API_KEY",
images: ["data:image/png;base64," + "A".repeat(10_000_004)],
prompt: "Describe this",
error: "Claude image is larger than the 10 MB base64 limit",
},
{
model: "sonnet",
env: "ANTHROPIC_API_KEY",
images: Array.from(
{ length: 4 },
() => "data:image/png;base64," + "A".repeat(8_000_000),
),
prompt: "Compare these",
error: "Claude request is larger than the 32 MB limit",
},
{
model: "gemini-3.8-flash",
env: "GEMINI_API_KEY",
images: ["data:image/png;base64," + "A".repeat(19_999_000)],
// UTF-8 text and JSON overhead push an otherwise in-limit image over the limit.
prompt: "猫".repeat(400),
error: "Gemini request is larger than the 20 MB limit",
},
{
model: "gemini-3.8-flash",
env: "GEMINI_API_KEY",
images: [imageUrl],
prompt: "Describe this",
error: "Gemini request is larger than the 20 MB limit",
},
]
) {
for (const testCase of [
{
model: "sonnet",
env: "ANTHROPIC_API_KEY",
images: ["data:image/png;base64," + "A".repeat(10_000_004)],
prompt: "Describe this",
error: "Claude image is larger than the 10 MB base64 limit",
},
{
model: "sonnet",
env: "ANTHROPIC_API_KEY",
images: Array.from(
{ length: 4 },
() => "data:image/png;base64," + "A".repeat(8_000_000),
),
prompt: "Compare these",
error: "Claude request is larger than the 32 MB limit",
},
{
model: "gemini-3.8-flash",
env: "GEMINI_API_KEY",
images: ["data:image/png;base64," + "A".repeat(19_999_000)],
// UTF-8 text and JSON overhead push an otherwise in-limit image over the limit.
prompt: "猫".repeat(400),
error: "Gemini request is larger than the 20 MB limit",
},
{
model: "gemini-3.8-flash",
env: "GEMINI_API_KEY",
images: [imageUrl],
prompt: "Describe this",
error: "Gemini request is larger than the 20 MB limit",
},
]) {
test(`${testCase.model} rejects oversized encoded images and requests before sending: ${testCase.error}`, async () => {
const originalFetch = globalThis.fetch
const oldKey = process.env[testCase.env]
Expand All @@ -307,25 +307,28 @@ for (
return Promise.resolve(Response.json(response))
}
try {
await assert.rejects(() =>
createMessage({
model: resolveModel(testCase.model),
config: { search: false, think: undefined },
chat: {
id: "test",
createdAt: new Date(0),
systemPrompt: "test",
messages: [
{
role: "user",
content: testCase.prompt,
image_urls: testCase.images,
createdAt: new Date(0),
},
{ role: "user", content: "Follow up", createdAt: new Date(0) },
],
},
}), errorIncluding(ValidationError, testCase.error))
await assert.rejects(
() =>
createMessage({
model: resolveModel(testCase.model),
config: { search: false, think: undefined },
chat: {
id: "test",
createdAt: new Date(0),
systemPrompt: "test",
messages: [
{
role: "user",
content: testCase.prompt,
image_urls: testCase.images,
createdAt: new Date(0),
},
{ role: "user", content: "Follow up", createdAt: new Date(0) },
],
},
}),
errorIncluding(ValidationError, testCase.error),
)
assert.deepEqual(apiCalls, 0)
} finally {
globalThis.fetch = originalFetch
Expand Down
Loading
Loading