fix: Handle undefined command names in getCommandName function #217
fix: Handle undefined command names in getCommandName function #217claude-code-best merged 9 commits intoclaude-code-best:mainfrom
Conversation
…ude-code-best#168) Fixes claude-code-best#168 OpenAI requires that an assistant message with tool_calls be immediately followed by tool messages. Previously, convertInternalUserMessage output user content before tool results, causing 400 errors. Now tool messages are pushed first.
提交描述:
修复了在使用OpenAI兼容API时TaskCreate工具调用失败的问题。
问题:
- 当使用OpenAI兼容API模型时,调用TaskCreate工具出现"InputValidationError: The required
parameter `subject` is missing"错误
- OpenAI兼容层没有正确处理deferred tools的过滤逻辑,导致工具schema没有被正确发送给模型
修复:
1. 在OpenAI兼容层中添加了与Anthropic API路径一致的deferred tools处理逻辑
2. 导入必要的工具搜索相关函数: isToolSearchEnabled, extractDiscoveredToolNames,
isDeferredTool等
3. 实现工具过滤逻辑:
- 检查工具搜索是否启用
- 构建deferred tools集合
- 过滤工具列表: 只包含非deferred工具或已发现的deferred工具
- 为deferred tools设置deferLoading标志
4. 修正了extractDiscoveredToolNames函数的导入路径错误
影响:
- 解决了TaskCreate工具调用时的参数验证错误
- 确保OpenAI兼容层与Anthropic API路径在处理deferred tools时行为一致
- 支持工具搜索功能在OpenAI兼容模式下正常工作
修改的文件:
- src/services/api/openai/index.ts - 主要修复文件
测试建议:
1. 使用OpenAI兼容API模型时,TaskCreate工具应该可以正常调用
2. 如果工具搜索功能启用,可能需要先使用ToolSearchTool来发现TaskCreate工具
3. 验证工具调用时不再出现"InputValidationError"错误
这个修复确保了当使用OpenAI兼容API(如Ollama、DeepSeek、vLLM等)时,deferred
tools(如TaskCreate)能够被正确处理,解决了工具调用失败的问题。
- Modified getCommandName in src/types/command.ts to return empty string instead of undefined when cmd.name is undefined - Added null checks in src/hooks/useTypeahead.tsx to safely handle command names - Prevents "undefined is not an object" error when FEATURE_BUDDY=1 and FEATURE_FORK_SUBAGENT=1 are enabled The error occurred because getCommandName(cmd) could return undefined when cmd.name was undefined, causing .length access to fail.
📝 WalkthroughWalkthroughThe PR reformats Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/types/command.ts (1)
208-211: Consider updating the JSDoc to include empty-string fallback.Line 208 describes fallback to
cmd.name, while Line 211 also allows''for malformed inputs. Aligning the comment with behavior would reduce ambiguity.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/types/command.ts` around lines 208 - 211, Update the JSDoc for getCommandName (exported function getCommandName(cmd: CommandBase)) to accurately describe behavior: state that it resolves the user-visible name by calling cmd.userFacingName() and falling back to cmd.name, and that it ultimately returns an empty string ('') if both are missing or falsy; keep the comment concise and reflect the empty-string fallback for malformed inputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/types/command.ts`:
- Around line 208-211: Update the JSDoc for getCommandName (exported function
getCommandName(cmd: CommandBase)) to accurately describe behavior: state that it
resolves the user-visible name by calling cmd.userFacingName() and falling back
to cmd.name, and that it ultimately returns an empty string ('') if both are
missing or falsy; keep the comment concise and reflect the empty-string fallback
for malformed inputs.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8775ced2-ad30-4c56-81b3-10e73b74a89a
📒 Files selected for processing (2)
src/hooks/useTypeahead.tsxsrc/types/command.ts
This PR fixes a runtime error that occurs when
FEATURE_BUDDY=1andFEATURE_FORK_SUBAGENT=1are enabled. The error"undefined is not an object (evaluating 'getCommandName(cmd).length')"was caused by thegetCommandNamefunction returningundefinedwhen command objects lacked anameproperty.Changes Made
1. Fixed
src/types/command.ts-getCommandNamefunctionreturn cmd.userFacingName?.() ?? cmd.nameconst name = cmd.userFacingName?.() ?? cmd.name; return name || ''undefined) whencmd.nameisundefined.2. Enhanced
src/hooks/useTypeahead.tsx- Added null safety checksLine 485: Added null check in
allCommandsMaxWidthcalculation:Line 951: Added null check in command matching logic:
Root Cause Analysis
The
getCommandNamefunction incommand.tscould returnundefinedwhen:cmd.userFacingName?.()returnsundefined(or the function doesn't exist)cmd.nameis alsoundefinedThis happened with certain command objects, particularly when
FEATURE_BUDDY=1andFEATURE_FORK_SUBAGENT=1were enabled, potentially creating incomplete command objects.Error Scenario
Testing
FEATURE_BUDDY=1 FEATURE_FORK_SUBAGENT=1 bun run devFiles Changed
src/types/command.ts(3 lines changed)src/hooks/useTypeahead.tsx(formatting changes + 2 functional fixes)Impact
Related Issues
This fix addresses edge cases where command objects may be created without proper name properties, which can occur with:
Checklist
Summary by CodeRabbit
Bug Fixes
Style