better typing

This commit is contained in:
centdix
2025-08-28 15:07:27 +00:00
parent 79cf5dbf68
commit 0bbb368c3f
2 changed files with 34 additions and 17 deletions
@@ -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
+1 -1
View File
@@ -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]