Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/pi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"build": "rm -rf dist && tsc -p tsconfig.build.json",
"typecheck": "tsc",
"types": "tsc",
"test": "bun test src/tests",
"prepublishOnly": "bun run build"
},
"dependencies": {
Expand Down
53 changes: 35 additions & 18 deletions packages/pi/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ export type AnthropicRequestBody = {
}

function sanitize(text: string): string {
return text.replace(/[\uD800-\uDFFF]/g, '\uFFFD')
return text.replace(/[\uD800-\uDFFF]/gu, '\uFFFD')
}

/**
* Sanitize a tool-call ID to match Anthropic's `^[a-zA-Z0-9_-]+$` pattern.
* Cross-provider IDs (e.g. OpenAI Codex `call_xxx|fc_xxx`) contain characters
* Anthropic rejects. Deterministic — same input always yields the same output.
*/
function sanitizeToolId(id: string): string {
if (!id) return 'tool_call_unknown'
const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, '_')
if (!sanitized) return 'tool_call_unknown'
return sanitized.slice(0, 256)
const cleaned = id.replace(/[^a-zA-Z0-9_-]/g, '_')
return cleaned.length > 256 ? cleaned.slice(0, 256) : cleaned
}

function toClaudeCodeToolName(name: string): string {
Expand Down Expand Up @@ -157,17 +161,20 @@ function convertMessages(
if (message.role === 'toolResult') {
const toolResult = message as ToolResultMessage
const toolResults: Array<Record<string, unknown>> = []
const firstContent = convertTextAndImages(toolResult.content)
const firstContentArr = Array.isArray(firstContent)
? firstContent
: [{ type: 'text', text: firstContent }]
if (toolResult.isError && firstContentArr.length === 0) {
firstContentArr.push({ type: 'text', text: 'Error' })
let content = convertTextAndImages(toolResult.content)
// Anthropic rejects tool_result with is_error=true but empty content
if (
toolResult.isError &&
(!content ||
(Array.isArray(content) && content.length === 0) ||
content === '')
) {
content = [{ type: 'text', text: 'Error' }]
}
toolResults.push({
type: 'tool_result',
tool_use_id: sanitizeToolId(toolResult.toolCallId),
content: firstContentArr,
content: content,
is_error: toolResult.isError,
})

Expand All @@ -177,17 +184,20 @@ function convertMessages(
messages[nextIndex]?.role === 'toolResult'
) {
const next = messages[nextIndex] as ToolResultMessage
const nextContent = convertTextAndImages(next.content)
const nextContentArr = Array.isArray(nextContent)
? nextContent
: [{ type: 'text', text: nextContent }]
if (next.isError && nextContentArr.length === 0) {
nextContentArr.push({ type: 'text', text: 'Error' })
let nextContent = convertTextAndImages(next.content)
// Anthropic rejects tool_result with is_error=true but empty content
if (
next.isError &&
(!nextContent ||
(Array.isArray(nextContent) && nextContent.length === 0) ||
nextContent === '')
) {
nextContent = [{ type: 'text', text: 'Error' }]
}
toolResults.push({
type: 'tool_result',
tool_use_id: sanitizeToolId(next.toolCallId),
content: nextContentArr,
content: nextContent,
is_error: next.isError,
})
nextIndex += 1
Expand Down Expand Up @@ -275,6 +285,13 @@ export async function buildAnthropicRequest(
identity?: ClaudeCodeIdentity,
): Promise<{ body: AnthropicRequestBody; bodyText: string }> {
const messages = convertMessages(context.messages)
// Strip trailing assistant messages — Anthropic rejects prefill on some models
while (
messages.length &&
messages[messages.length - 1]?.role === 'assistant'
) {
messages.pop()
}
const system = [
{
type: 'text',
Expand Down
Loading
Loading