fix: enhance anthropic provider configuration and middleware handling

- Updated providerToAiSdkConfig to support both OpenAI and Anthropic providers, improving flexibility in API host formatting.
- Refactored thinkingTimeMiddleware to ensure all chunks are correctly enqueued, enhancing middleware functionality.
- Corrected parameter naming in getAnthropicReasoningParams for consistency and clarity in configuration.
This commit is contained in:
suyao 2025-06-21 22:38:54 +08:00
parent 09080f0755
commit ebe85ba24a
No known key found for this signature in database
3 changed files with 6 additions and 4 deletions

View File

@ -45,7 +45,7 @@ function providerToAiSdkConfig(provider: Provider): {
actualProvider = createVertexProvider(provider)
}
if (actualProvider.type === 'openai') {
if (actualProvider.type === 'openai' || actualProvider.type === 'anthropic') {
actualProvider.apiHost = formatApiHost(actualProvider.apiHost)
}

View File

@ -25,6 +25,8 @@ export default function thinkingTimeMiddleware(): LanguageModelV1Middleware {
thinkingStartTime = Date.now()
}
accumulatedThinkingContent += chunk.textDelta || ''
// 将所有 chunk 原样传递下去
controller.enqueue(chunk)
} else {
if (hasThinkingContent && thinkingStartTime > 0) {
const thinkingTime = Date.now() - thinkingStartTime
@ -37,10 +39,10 @@ export default function thinkingTimeMiddleware(): LanguageModelV1Middleware {
hasThinkingContent = false
thinkingStartTime = 0
accumulatedThinkingContent = ''
} else {
controller.enqueue(chunk)
}
}
// 将所有 chunk 原样传递下去
controller.enqueue(chunk)
},
flush(controller) {
// 如果流的末尾都是 reasoning也需要发送 complete 事件

View File

@ -382,7 +382,7 @@ function getAnthropicReasoningParams(assistant: Assistant, model: Model): Record
return {
thinking: {
type: 'enabled',
budget_tokens: budgetTokens
budgetTokens: budgetTokens
}
}
}