From 0bbb368c3f3de036c49635f6aff3341c8d08f17d Mon Sep 17 00:00:00 2001 From: centdix Date: Thu, 28 Aug 2025 15:07:27 +0000 Subject: [PATCH] better typing --- .../lib/components/copilot/chat/anthropic.ts | 49 +++++++++++++------ frontend/src/lib/components/copilot/lib.ts | 2 +- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/frontend/src/lib/components/copilot/chat/anthropic.ts b/frontend/src/lib/components/copilot/chat/anthropic.ts index cd487dd3c4..c7f9d8f6d3 100644 --- a/frontend/src/lib/components/copilot/chat/anthropic.ts +++ b/frontend/src/lib/components/copilot/chat/anthropic.ts @@ -45,13 +45,38 @@ export interface AnthropicStreamEvent { [key: string]: any } +export interface AnthropicTool { + name: string + description?: string + input_schema: any + cache_control?: { type: 'ephemeral' } +} + +export interface AnthropicSystemMessage { + type: 'text' + text: string + cache_control?: { type: 'ephemeral' } +} + +export interface AnthropicContentBlockToolUse { + type: 'tool_use' + id: string + name: string + input: any +} + +export interface AnthropicContentBlockText { + type: 'text' + text: string +} + +export type AnthropicContentBlock = AnthropicContentBlockToolUse | AnthropicContentBlockText + export function convertOpenAIToAnthropicMessages(messages: ChatCompletionMessageParam[]): { - system: Array<{ type: 'text'; text: string; cache_control?: { type: 'ephemeral' } }> | undefined + system: AnthropicSystemMessage[] | undefined messages: AnthropicMessage[] } { - let system: - | Array<{ type: 'text'; text: string; cache_control?: { type: 'ephemeral' } }> - | undefined + let system: AnthropicSystemMessage[] | undefined const anthropicMessages: AnthropicMessage[] = [] for (const message of messages) { @@ -76,7 +101,7 @@ export function convertOpenAIToAnthropicMessages(messages: ChatCompletionMessage typeof message.content === 'string' ? message.content : JSON.stringify(message.content) }) } else if (message.role === 'assistant') { - const content: any[] = [] + const content: AnthropicContentBlock[] = [] if (message.content) { content.push({ @@ -149,21 +174,13 @@ export function convertOpenAIToAnthropicMessages(messages: ChatCompletionMessage export function convertOpenAIToolsToAnthropic( tools?: OpenAI.Chat.Completions.ChatCompletionTool[] -): - | Array<{ - name: string - description?: string - input_schema: any - cache_control?: { type: 'ephemeral' } - }> - | undefined { +): AnthropicTool[] | undefined { if (!tools || tools.length === 0) return undefined - const anthropicTools = tools.map((tool) => ({ + const anthropicTools: AnthropicTool[] = tools.map((tool) => ({ name: tool.function.name, description: tool.function.description, - input_schema: tool.function.parameters || { type: 'object', properties: {} }, - cache_control: undefined as { type: 'ephemeral' } | undefined + input_schema: tool.function.parameters || { type: 'object', properties: {} } })) // Add cache_control to the last tool to cache all tool definitions diff --git a/frontend/src/lib/components/copilot/lib.ts b/frontend/src/lib/components/copilot/lib.ts index ab65e06244..9bffdaa9cb 100644 --- a/frontend/src/lib/components/copilot/lib.ts +++ b/frontend/src/lib/components/copilot/lib.ts @@ -802,7 +802,7 @@ export async function deltaCodeCompletion( } if (!match[1].endsWith('`')) { - // skip udpating if possible that part of three ticks (end of code block)s + // skip updating if possible that part of three ticks (end of code block)s delta = getStringEndDelta(code, match[1]) generatedCodeDelta.set(delta) code = match[1]