Compare commits

...

1 Commits

Author SHA1 Message Date
0xtoshii 98fe73b13a base 2025-06-24 17:30:30 -07:00
+25 -2
View File
@@ -38,8 +38,7 @@ export interface SingleCompletionHandler {
completePrompt(prompt: string): Promise<string>
}
export function buildApiHandler(configuration: ApiConfiguration): ApiHandler {
const { apiProvider, ...options } = configuration
function createHandlerForProvider(apiProvider: string | undefined, options: any): ApiHandler {
switch (apiProvider) {
case "anthropic":
return new AnthropicHandler(options)
@@ -97,3 +96,27 @@ export function buildApiHandler(configuration: ApiConfiguration): ApiHandler {
return new AnthropicHandler(options)
}
}
export function buildApiHandler(configuration: ApiConfiguration): ApiHandler {
const { apiProvider, ...options } = configuration
// Validate thinking budget tokens against model's maxTokens to prevent API errors
// wrapped in a try-catch for safety, but this should never throw
try {
if (options.thinkingBudgetTokens && options.thinkingBudgetTokens > 0) {
const handler = createHandlerForProvider(apiProvider, options)
const modelInfo = handler.getModel().info
if (modelInfo.maxTokens && options.thinkingBudgetTokens > modelInfo.maxTokens) {
const clippedValue = modelInfo.maxTokens - 1
options.thinkingBudgetTokens = clippedValue
} else {
return handler // don't rebuild unless its necessary
}
}
} catch (error) {
console.error("buildApiHandler error:", error)
}
return createHandlerForProvider(apiProvider, options)
}