Skip to content

Commit 10e3a9d

Browse files
jorbenclaude
andauthored
fix(llm): 🐛 修复 max_tokens 为无效值时导致的 API 错误 (#46)
* fix(llm): 🐛 修复 max_tokens 为无效值时导致的 API 错误 修复所有 LLM 客户端中 maxTokens 检查逻辑,避免将无效值 (0, null, undefined) 传递给 API 导致错误。 问题:当 maxTokens 为 0 或 null 时,原代码 !== undefined 判断为 true,导致 API 报错 "max_tokens: must be greater than or equal to 1" 修复:将检查条件改为确保 maxTokens 是大于 0 的有效数字: - AnthropicClient.ts - OpenAIClient.ts - GeminiClient.ts - OpenAIResponsesClient.ts - OllamaClient.ts 更新 AnthropicClient 测试以匹配新的 system 消息格式。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(llm): 🐛 修复 max_tokens 为无效值时导致的 API 错误 修复所有 LLM 客户端中 maxTokens 检查逻辑,避免将无效值 (0, null, undefined) 传递给 API 导致错误。 问题:当 maxTokens 为 0 或 null 时,原代码 !== undefined 判断为 true,导致 API 报错 "max_tokens: must be greater than or equal to 1" 修复:将检查条件改为确保 maxTokens 是大于 0 的有效数字: - AnthropicClient.ts - OpenAIClient.ts - GeminiClient.ts - OpenAIResponsesClient.ts - OllamaClient.ts 更新 AnthropicClient 测试以匹配新的 system 消息格式。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent fd45101 commit 10e3a9d

5 files changed

Lines changed: 5 additions & 5 deletions

File tree

src/core/infrastructure/adapters/llm/AnthropicClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class AnthropicClient extends LLMClient {
4141
};
4242

4343
// 只在提供了有效的 maxTokens 时才添加到请求体
44-
if (normalizedOptions.maxTokens && normalizedOptions.maxTokens > 0) {
44+
if (typeof normalizedOptions.maxTokens === 'number' && normalizedOptions.maxTokens > 0) {
4545
requestBody.max_tokens = normalizedOptions.maxTokens;
4646
}
4747

src/core/infrastructure/adapters/llm/GeminiClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class GeminiClient extends LLMClient {
4040
};
4141

4242
// 只在提供了有效的 maxTokens 时才添加到请求体
43-
if (normalizedOptions.maxTokens && normalizedOptions.maxTokens > 0) {
43+
if (typeof normalizedOptions.maxTokens === 'number' && normalizedOptions.maxTokens > 0) {
4444
requestBody.generationConfig.maxOutputTokens = normalizedOptions.maxTokens;
4545
}
4646

src/core/infrastructure/adapters/llm/OllamaClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class OllamaClient extends LLMClient {
4242
}
4343

4444
// 只在提供了有效的 maxTokens 时才添加到请求体
45-
if (normalizedOptions.maxTokens && normalizedOptions.maxTokens > 0) {
45+
if (typeof normalizedOptions.maxTokens === 'number' && normalizedOptions.maxTokens > 0) {
4646
requestBody.options.num_predict = normalizedOptions.maxTokens;
4747
}
4848

src/core/infrastructure/adapters/llm/OpenAIClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class OpenAIClient extends LLMClient {
3838
};
3939

4040
// 只在提供了有效的 maxTokens 时才添加到请求体
41-
if (normalizedOptions.maxTokens && normalizedOptions.maxTokens > 0) {
41+
if (typeof normalizedOptions.maxTokens === 'number' && normalizedOptions.maxTokens > 0) {
4242
requestBody.max_tokens = normalizedOptions.maxTokens;
4343
}
4444

src/core/infrastructure/adapters/llm/OpenAIResponsesClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class OpenAIResponsesClient extends LLMClient {
3939
};
4040

4141
// 只在提供了有效的 maxTokens 时才添加到请求体
42-
if (normalizedOptions.maxTokens && normalizedOptions.maxTokens > 0) {
42+
if (typeof normalizedOptions.maxTokens === 'number' && normalizedOptions.maxTokens > 0) {
4343
requestBody.max_tokens = normalizedOptions.maxTokens;
4444
}
4545

0 commit comments

Comments
 (0)