feat(ai): route copilot claude via anthropic messages api

This commit is contained in:
Nate Smyth 2026-02-06 05:09:52 -05:00 committed by Mario Zechner
parent cf1353b8e7
commit 0a132a30a1
9 changed files with 196 additions and 76 deletions

View file

@ -28,6 +28,7 @@ import { AssistantMessageEventStream } from "../utils/event-stream.js";
import { parseStreamingJson } from "../utils/json-parse.js";
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.js";
import { transformMessages } from "./transform-messages.js";
@ -217,11 +218,22 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
try {
const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
let copilotDynamicHeaders: Record<string, string> | undefined;
if (model.provider === "github-copilot") {
const hasImages = hasCopilotVisionInput(context.messages);
copilotDynamicHeaders = buildCopilotDynamicHeaders({
messages: context.messages,
hasImages,
});
}
const { client, isOAuthToken } = createClient(
model,
apiKey,
options?.interleavedThinking ?? true,
options?.headers,
copilotDynamicHeaders,
);
const params = buildParams(model, context, isOAuthToken, options);
options?.onPayload?.(params);
@ -471,12 +483,54 @@ function isOAuthToken(apiKey: string): boolean {
return apiKey.includes("sk-ant-oat");
}
function createClient(
model: Model<"anthropic-messages">,
apiKey: string,
interleavedThinking: boolean,
optionsHeaders?: Record<string, string>,
): { client: Anthropic; isOAuthToken: boolean } {
export interface BuildAnthropicClientOptionsParams {
model: Model<"anthropic-messages">;
apiKey: string;
interleavedThinking: boolean;
dynamicHeaders?: Record<string, string>;
optionsHeaders?: Record<string, string>;
}
export interface AnthropicClientConfig {
apiKey: string | null;
authToken?: string;
baseURL: string;
defaultHeaders: Record<string, string>;
dangerouslyAllowBrowser: boolean;
isOAuthToken: boolean;
}
export function buildAnthropicClientOptions(params: BuildAnthropicClientOptionsParams): AnthropicClientConfig {
const { model, apiKey, interleavedThinking, dynamicHeaders, optionsHeaders } = params;
// Copilot: Bearer auth, selective betas
if (model.provider === "github-copilot") {
const betaFeatures: string[] = [];
if (interleavedThinking) {
betaFeatures.push("interleaved-thinking-2025-05-14");
}
const defaultHeaders = mergeHeaders(
{
accept: "application/json",
"anthropic-dangerous-direct-browser-access": "true",
...(betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}),
Authorization: `Bearer ${apiKey}`,
},
dynamicHeaders,
model.headers,
optionsHeaders,
);
return {
apiKey: null,
baseURL: model.baseUrl,
defaultHeaders,
dangerouslyAllowBrowser: true,
isOAuthToken: false,
};
}
const betaFeatures = ["fine-grained-tool-streaming-2025-05-14"];
if (interleavedThinking) {
betaFeatures.push("interleaved-thinking-2025-05-14");
@ -484,7 +538,6 @@ function createClient(
const oauthToken = isOAuthToken(apiKey);
if (oauthToken) {
// Stealth mode: Mimic Claude Code's headers exactly
const defaultHeaders = mergeHeaders(
{
accept: "application/json",
@ -497,15 +550,14 @@ function createClient(
optionsHeaders,
);
const client = new Anthropic({
return {
apiKey: null,
authToken: apiKey,
baseURL: model.baseUrl,
defaultHeaders,
dangerouslyAllowBrowser: true,
});
return { client, isOAuthToken: true };
isOAuthToken: true,
};
}
const defaultHeaders = mergeHeaders(
@ -518,14 +570,39 @@ function createClient(
optionsHeaders,
);
const client = new Anthropic({
return {
apiKey,
baseURL: model.baseUrl,
dangerouslyAllowBrowser: true,
defaultHeaders,
dangerouslyAllowBrowser: true,
isOAuthToken: false,
};
}
function createClient(
model: Model<"anthropic-messages">,
apiKey: string,
interleavedThinking: boolean,
optionsHeaders?: Record<string, string>,
dynamicHeaders?: Record<string, string>,
): { client: Anthropic; isOAuthToken: boolean } {
const config = buildAnthropicClientOptions({
model,
apiKey,
interleavedThinking,
dynamicHeaders,
optionsHeaders,
});
return { client, isOAuthToken: false };
const client = new Anthropic({
apiKey: config.apiKey,
...(config.authToken ? { authToken: config.authToken } : {}),
baseURL: config.baseURL,
defaultHeaders: config.defaultHeaders,
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser,
});
return { client, isOAuthToken: config.isOAuthToken };
}
function buildParams(

View file

@ -0,0 +1,59 @@
import type { Message } from "../types.js";
/**
* Infer whether the current request to Copilot is user-initiated or agent-initiated.
* Accepts `unknown[]` because providers may pass pre-converted message shapes.
*/
export function inferCopilotInitiator(messages: unknown[]): "user" | "agent" {
if (messages.length === 0) return "user";
const last = messages[messages.length - 1] as Record<string, unknown>;
const role = last.role as string | undefined;
if (!role) return "user";
if (role !== "user") return "agent";
// Check if last content block is a tool_result (Anthropic-converted shape)
const content = last.content;
if (Array.isArray(content) && content.length > 0) {
const lastBlock = content[content.length - 1] as Record<string, unknown>;
if (lastBlock.type === "tool_result") {
return "agent";
}
}
return "user";
}
/** Check whether any message in the conversation contains image content. */
export function hasCopilotVisionInput(messages: Message[]): boolean {
return messages.some((msg) => {
if (msg.role === "user" && Array.isArray(msg.content)) {
return msg.content.some((c) => c.type === "image");
}
if (msg.role === "toolResult" && Array.isArray(msg.content)) {
return msg.content.some((c) => c.type === "image");
}
return false;
});
}
/**
* Build dynamic Copilot headers that vary per-request.
* Static headers (User-Agent, Editor-Version, etc.) come from model.headers.
*/
export function buildCopilotDynamicHeaders(params: {
messages: unknown[];
hasImages: boolean;
}): Record<string, string> {
const headers: Record<string, string> = {
"X-Initiator": inferCopilotInitiator(params.messages),
"Openai-Intent": "conversation-edits",
};
if (params.hasImages) {
headers["Copilot-Vision-Request"] = "true";
}
return headers;
}

View file

@ -29,6 +29,7 @@ import type {
import { AssistantMessageEventStream } from "../utils/event-stream.js";
import { parseStreamingJson } from "../utils/json-parse.js";
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
import { buildBaseOptions, clampReasoning } from "./simple-options.js";
import { transformMessages } from "./transform-messages.js";
@ -359,28 +360,12 @@ function createClient(
const headers = { ...model.headers };
if (model.provider === "github-copilot") {
// Copilot expects X-Initiator to indicate whether the request is user-initiated
// or agent-initiated (e.g. follow-up after assistant/tool messages). If there is
// no prior message, default to user-initiated.
const messages = context.messages || [];
const lastMessage = messages[messages.length - 1];
const isAgentCall = lastMessage ? lastMessage.role !== "user" : false;
headers["X-Initiator"] = isAgentCall ? "agent" : "user";
headers["Openai-Intent"] = "conversation-edits";
// Copilot requires this header when sending images
const hasImages = messages.some((msg) => {
if (msg.role === "user" && Array.isArray(msg.content)) {
return msg.content.some((c) => c.type === "image");
}
if (msg.role === "toolResult" && Array.isArray(msg.content)) {
return msg.content.some((c) => c.type === "image");
}
return false;
const hasImages = hasCopilotVisionInput(context.messages);
const copilotHeaders = buildCopilotDynamicHeaders({
messages: context.messages,
hasImages,
});
if (hasImages) {
headers["Copilot-Vision-Request"] = "true";
}
Object.assign(headers, copilotHeaders);
}
// Merge options headers last so they can override defaults

View file

@ -14,6 +14,7 @@ import type {
Usage,
} from "../types.js";
import { AssistantMessageEventStream } from "../utils/event-stream.js";
import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
import { convertResponsesMessages, convertResponsesTools, processResponsesStream } from "./openai-responses-shared.js";
import { buildBaseOptions, clampReasoning } from "./simple-options.js";
@ -159,28 +160,12 @@ function createClient(
const headers = { ...model.headers };
if (model.provider === "github-copilot") {
// Copilot expects X-Initiator to indicate whether the request is user-initiated
// or agent-initiated (e.g. follow-up after assistant/tool messages). If there is
// no prior message, default to user-initiated.
const messages = context.messages || [];
const lastMessage = messages[messages.length - 1];
const isAgentCall = lastMessage ? lastMessage.role !== "user" : false;
headers["X-Initiator"] = isAgentCall ? "agent" : "user";
headers["Openai-Intent"] = "conversation-edits";
// Copilot requires this header when sending images
const hasImages = messages.some((msg) => {
if (msg.role === "user" && Array.isArray(msg.content)) {
return msg.content.some((c) => c.type === "image");
}
if (msg.role === "toolResult" && Array.isArray(msg.content)) {
return msg.content.some((c) => c.type === "image");
}
return false;
const hasImages = hasCopilotVisionInput(context.messages);
const copilotHeaders = buildCopilotDynamicHeaders({
messages: context.messages,
hasImages,
});
if (hasImages) {
headers["Copilot-Vision-Request"] = "true";
}
Object.assign(headers, copilotHeaders);
}
// Merge options headers last so they can override defaults