Spaces:
Running
Running
File size: 4,161 Bytes
98c9143 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import consola from "consola"
import { events } from "fetch-event-stream"
import type { CompactType } from "~/lib/compact"
import type { SubagentMarker } from "~/lib/subagent"
import type {
AnthropicMessagesPayload,
AnthropicResponse,
} from "~/routes/messages/anthropic-types"
import {
copilotBaseUrl,
copilotHeaders,
prepareForCompact,
prepareInteractionHeaders,
prepareMessageProxyHeaders,
} from "~/lib/api-config"
import { logCopilotRateLimits } from "~/lib/copilot-rate-limit"
import { HTTPError } from "~/lib/error"
import { state } from "~/lib/state"
import { parseUserIdMetadata } from "~/lib/utils"
export type MessagesStream = ReturnType<typeof events>
export type CreateMessagesReturn = AnthropicResponse | MessagesStream
const INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14"
const ADVANCED_TOOL_USE_BETA = "advanced-tool-use-2025-11-20"
const allowedAnthropicBetas = new Set([
INTERLEAVED_THINKING_BETA,
"context-management-2025-06-27",
ADVANCED_TOOL_USE_BETA,
])
const buildAnthropicBetaHeader = (
anthropicBetaHeader: string | undefined,
thinking: AnthropicMessagesPayload["thinking"],
_model: string,
): string | undefined => {
const isAdaptiveThinking = thinking?.type === "adaptive"
if (anthropicBetaHeader) {
const filteredBeta = anthropicBetaHeader
.split(",")
.map((item) => item.trim())
.filter((item) => item.length > 0)
.filter((item) => allowedAnthropicBetas.has(item))
// in vscode copilot extension, advanced-tool-use is enabled by default
// align header with vscode copilot extension
const uniqueFilteredBetas = [...filteredBeta]
if (uniqueFilteredBetas.length > 0) {
return uniqueFilteredBetas.join(",")
}
return undefined
}
if (thinking?.budget_tokens && !isAdaptiveThinking) {
return INTERLEAVED_THINKING_BETA
}
return undefined
}
export const createMessages = async (
payload: AnthropicMessagesPayload,
anthropicBetaHeader: string | undefined,
options: {
subagentMarker?: SubagentMarker | null
requestId: string
sessionId?: string
compactType?: CompactType
},
): Promise<CreateMessagesReturn> => {
if (!state.copilotToken) throw new Error("Copilot token not found")
const enableVision = payload.messages.some((message) => {
if (!Array.isArray(message.content)) return false
return message.content.some(
(block) =>
block.type === "image"
|| (block.type === "tool_result"
&& Array.isArray(block.content)
&& block.content.some((inner) => inner.type === "image")),
)
})
let isInitiateRequest = false
const lastMessage = payload.messages.at(-1)
if (lastMessage?.role === "user") {
isInitiateRequest =
Array.isArray(lastMessage.content) ?
lastMessage.content.some((block) => block.type !== "tool_result")
: true
}
const headers: Record<string, string> = {
...copilotHeaders(state, options.requestId, enableVision),
"x-initiator": isInitiateRequest ? "user" : "agent",
}
prepareInteractionHeaders(
options.sessionId,
Boolean(options.subagentMarker),
headers,
)
prepareForCompact(headers, options.compactType)
const { safetyIdentifier, sessionId } = parseUserIdMetadata(
payload.metadata?.user_id,
)
// from claude code
if (safetyIdentifier && sessionId) {
prepareMessageProxyHeaders(headers)
}
// align with vscode copilot extension anthropic-beta
const anthropicBeta = buildAnthropicBetaHeader(
anthropicBetaHeader,
payload.thinking,
payload.model,
)
if (anthropicBeta) {
headers["anthropic-beta"] = anthropicBeta
}
consola.log(`<-- model: ${payload.model}`)
const response = await fetch(`${copilotBaseUrl(state)}/v1/messages`, {
method: "POST",
headers,
body: JSON.stringify(payload),
})
logCopilotRateLimits(response.headers)
if (!response.ok) {
consola.error("Failed to create messages", response)
throw new HTTPError("Failed to create messages", response)
}
if (payload.stream) {
return events(response)
}
return (await response.json()) as AnthropicResponse
}
|