mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 06:04:44 +00:00
Remove Anthropic OAuth support
This commit is contained in:
parent
2e362fbfd2
commit
f5e6bcac1b
12 changed files with 121 additions and 183 deletions
|
|
@ -157,7 +157,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
|
|||
const block: Block = {
|
||||
type: "toolCall",
|
||||
id: event.content_block.id,
|
||||
name: event.content_block.name,
|
||||
name: isOAuthToken ? event.content_block.name.substring(4) : event.content_block.name,
|
||||
arguments: event.content_block.input as Record<string, any>,
|
||||
partialJson: "",
|
||||
index: event.index,
|
||||
|
|
@ -278,6 +278,10 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
|
|||
return stream;
|
||||
};
|
||||
|
||||
function isOAuthToken(apiKey: string): boolean {
|
||||
return apiKey.includes("sk-ant-oat");
|
||||
}
|
||||
|
||||
function createClient(
|
||||
model: Model<"anthropic-messages">,
|
||||
apiKey: string,
|
||||
|
|
@ -288,7 +292,8 @@ function createClient(
|
|||
betaFeatures.push("interleaved-thinking-2025-05-14");
|
||||
}
|
||||
|
||||
if (apiKey.includes("sk-ant-oat")) {
|
||||
const oauthToken = isOAuthToken(apiKey);
|
||||
if (oauthToken) {
|
||||
const defaultHeaders = {
|
||||
accept: "application/json",
|
||||
"anthropic-dangerous-direct-browser-access": "true",
|
||||
|
|
@ -305,23 +310,23 @@ function createClient(
|
|||
});
|
||||
|
||||
return { client, isOAuthToken: true };
|
||||
} else {
|
||||
const defaultHeaders = {
|
||||
accept: "application/json",
|
||||
"anthropic-dangerous-direct-browser-access": "true",
|
||||
"anthropic-beta": betaFeatures.join(","),
|
||||
...(model.headers || {}),
|
||||
};
|
||||
|
||||
const client = new Anthropic({
|
||||
apiKey,
|
||||
baseURL: model.baseUrl,
|
||||
dangerouslyAllowBrowser: true,
|
||||
defaultHeaders,
|
||||
});
|
||||
|
||||
return { client, isOAuthToken: false };
|
||||
}
|
||||
|
||||
const defaultHeaders = {
|
||||
accept: "application/json",
|
||||
"anthropic-dangerous-direct-browser-access": "true",
|
||||
"anthropic-beta": betaFeatures.join(","),
|
||||
...(model.headers || {}),
|
||||
};
|
||||
|
||||
const client = new Anthropic({
|
||||
apiKey,
|
||||
baseURL: model.baseUrl,
|
||||
dangerouslyAllowBrowser: true,
|
||||
defaultHeaders,
|
||||
});
|
||||
|
||||
return { client, isOAuthToken: false };
|
||||
}
|
||||
|
||||
function buildParams(
|
||||
|
|
@ -332,7 +337,7 @@ function buildParams(
|
|||
): MessageCreateParamsStreaming {
|
||||
const params: MessageCreateParamsStreaming = {
|
||||
model: model.id,
|
||||
messages: convertMessages(context.messages, model),
|
||||
messages: convertMessages(context.messages, model, isOAuthToken),
|
||||
max_tokens: options?.maxTokens || (model.maxTokens / 3) | 0,
|
||||
stream: true,
|
||||
};
|
||||
|
|
@ -375,7 +380,7 @@ function buildParams(
|
|||
}
|
||||
|
||||
if (context.tools) {
|
||||
params.tools = convertTools(context.tools);
|
||||
params.tools = convertTools(context.tools, isOAuthToken);
|
||||
}
|
||||
|
||||
if (options?.thinkingEnabled && model.reasoning) {
|
||||
|
|
@ -402,7 +407,11 @@ function sanitizeToolCallId(id: string): string {
|
|||
return id.replace(/[^a-zA-Z0-9_-]/g, "_");
|
||||
}
|
||||
|
||||
function convertMessages(messages: Message[], model: Model<"anthropic-messages">): MessageParam[] {
|
||||
function convertMessages(
|
||||
messages: Message[],
|
||||
model: Model<"anthropic-messages">,
|
||||
isOAuthToken: boolean,
|
||||
): MessageParam[] {
|
||||
const params: MessageParam[] = [];
|
||||
|
||||
// Transform messages for cross-provider compatibility
|
||||
|
|
@ -481,7 +490,7 @@ function convertMessages(messages: Message[], model: Model<"anthropic-messages">
|
|||
blocks.push({
|
||||
type: "tool_use",
|
||||
id: sanitizeToolCallId(block.id),
|
||||
name: block.name,
|
||||
name: isOAuthToken ? `mcp_${block.name}` : block.name,
|
||||
input: block.arguments,
|
||||
});
|
||||
}
|
||||
|
|
@ -547,14 +556,14 @@ function convertMessages(messages: Message[], model: Model<"anthropic-messages">
|
|||
return params;
|
||||
}
|
||||
|
||||
function convertTools(tools: Tool[]): Anthropic.Messages.Tool[] {
|
||||
function convertTools(tools: Tool[], isOAuthToken: boolean): Anthropic.Messages.Tool[] {
|
||||
if (!tools) return [];
|
||||
|
||||
return tools.map((tool) => {
|
||||
const jsonSchema = tool.parameters as any; // TypeBox already generates JSON Schema
|
||||
|
||||
return {
|
||||
name: tool.name,
|
||||
name: isOAuthToken ? `mcp_${tool.name}` : tool.name,
|
||||
description: tool.description,
|
||||
input_schema: {
|
||||
type: "object" as const,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue