我在写一个查看 Cursor 对话历史的 CLI 时,偶然发现 Cursor 用一种很有意思的优化来提高工具调用成功率。这让我重新思考,在大语言模型时代,开发软件的思路发生了什么变化。
一个 bug 暴露出的规律
我当时在排查,为什么历史查看器里的终端命令显示不正常。症状很奇怪:有的命令格式完全正确,有的却落进了通用处理逻辑。
数据库里存的是:
{
"name": "run_terminal_cmd",
"params": {"command": "sed -n '440,980p' ...", "requireUserApproval": true}
}
而我原来的代码是:
} else if (toolName === 'run_terminal_command' || toolName === 'execute_command') {
// Format terminal command nicely...
}
看出问题了吗?数据库写的是 run_terminal_cmd,我检查的却是 run_terminal_command。典型的名称不匹配。
等等,Cursor 为什么会这样做?
我继续翻工具调用日志,渐渐发现这在 Cursor 那边是有意设计的。Cursor 会把多种工具名写法映射到同一个底层函数:
| 数据库中的名称 | 可用的同义名称 |
|---|---|
run_terminal_cmd | run_terminal_command, execute_command, terminal, shell |
file_edit | edit_file, modify_file, update_file |
codebase_search | search_codebase, search_code, code_search, grep |
read_file | file_read, get_file, view_file, cat |
这就是通过同义词映射实现容错。大语言模型想运行终端命令时,输出的可能是:
run_terminal_command(完整写法)execute_command(强调动作)cmd(缩写)shell(Unix 用户的习惯叫法)
这些名称都被规范化,最终进入同一处理程序。大语言模型不用记住精确的函数名,合理的同义写法也能工作。
修复:接受输出的不确定性
我的修改很简单,照着 Cursor 的处理思路写:
// Before: Brittle exact matching
} else if (toolName === 'run_terminal_command') {
// After: Flexible synonym matching
} else if (['run_terminal_cmd', 'run_terminal_command', 'execute_command', 'terminal', 'cmd'].includes(toolName)) {
但这个经历带来的启发还不止于此。
思路的变化:在不确定性中写代码
传统编程建立在确定性之上:
- 函数名必须准确
- 类型严格
- API 有精确定义的契约
- 只要能编译,(大多数时候)就能运行
大语言模型应用面对的是不确定性:
- 自然语言有歧义
- 模型输出有概率性
- 同一提示可能产生不同工具名
- “正确”的边界并不总是清晰
这让人从根本上改变思路。传统程序员优化精确性,大语言模型时代的程序员优化健壮性。
旧写法:防御式编程
def process_command(cmd):
if cmd != "run_terminal_command":
raise ValueError(f"Unknown command: {cmd}")
# execute...
新写法:按匹配程度接受输入
TERMINAL_SYNONYMS = {
"run_terminal_cmd", "run_terminal_command",
"execute_command", "terminal", "cmd", "shell", "bash"
}
def process_command(cmd):
if cmd.lower().strip() in TERMINAL_SYNONYMS:
# execute...
elif fuzz.ratio(cmd, "run_terminal_command") > 80:
# probably meant terminal command, execute...
else:
# truly unknown
这对 AI 智能体开发有什么意义?
开发智能体时,你也在构建概率性的接口。大语言模型就像一个难以预测的同事:
- 懂意图,表达方式却不固定:它知道想做什么,但每次的写法可能不一样。
- 会犯有创造性的错:
cmd和command显然表达相同的意思,但你的===判断并不知道。 - 能从平滑降级中受益:一次“差一点就对”的调用,多半有办法让它正常工作。
实用模式
1. 同义词表:将各种合理变体映射为规范名称。
const TOOL_SYNONYMS = {
'run_terminal_cmd': ['run_terminal_command', 'execute_command', 'terminal', 'cmd', 'shell'],
'file_edit': ['edit_file', 'modify_file', 'update_file', 'write_file'],
'read_file': ['file_read', 'get_file', 'view_file', 'cat', 'open_file'],
};
function normalizeToolName(name: string): string {
for (const [canonical, synonyms] of Object.entries(TOOL_SYNONYMS)) {
if (name === canonical || synonyms.includes(name)) {
return canonical;
}
}
return name; // Unknown, pass through
}
2. 参数名模糊匹配:同样的思路也适用于参数名。
function getParam(params: object, ...keys: string[]): any {
for (const key of keys) {
if (params[key] !== undefined) return params[key];
}
return undefined;
}
// Usage: Works for 'command', 'cmd', 'shell_command', etc.
const command = getParam(params, 'command', 'cmd', 'shell_command', 'script');
3. 按置信度执行:拿不准时,请求确认。
function matchTool(input: string): { tool: string; confidence: number } {
// Exact match
if (KNOWN_TOOLS.includes(input)) {
return { tool: input, confidence: 1.0 };
}
// Synonym match
const normalized = normalizeToolName(input);
if (normalized !== input) {
return { tool: normalized, confidence: 0.9 };
}
// Fuzzy match
const fuzzyMatch = findClosestTool(input);
if (fuzzyMatch.score > 0.8) {
return { tool: fuzzyMatch.name, confidence: fuzzyMatch.score };
}
return { tool: input, confidence: 0.5 };
}
修完之后,我一直在想什么
修完这个 bug 后,有句话一直留在我脑子里:
Traditional software engineering treats ambiguity as a bug to be eliminated. LLM engineering treats ambiguity as a feature to be handled gracefully.
传统软件工程把歧义当作要消除的错误。大语言模型工程把歧义当作系统需要妥善处理的特性。
几十年来,我们打造类型系统、linter 和编译器,为的是尽早发现错误。现在我们构建的系统,还得能解释意图、推断含义并作出调整。
严谨性依然重要,只是用在了不同的地方:
- 覆盖范围要严谨:尽量把合理的同义写法都纳入映射。
- 降级处理要严谨:始终保留后备路径。
- 测试要严谨:除了手写用例,也用大语言模型生成的变体测试。
我见过的优秀智能体开发者,会把自己的代码看作大语言模型的概率世界与 API 的确定世界之间的翻译者。即使其中一方有些偏差,两边仍然可以正常对接。
小结
下次开发大语言模型驱动的工具时,可以问问自己:
- 模型可能以哪些方式表达这一意图?
- 怎样把这些变体规范化为标准写法?
- 模型“差一点就对”时会发生什么?
Cursor 的同义词映射是项小优化,却体现了一个更大的想法:在 AI 时代,灵活性本身就是一种功能。
参考资料
- Cursor History CLI. (2025). 工具调用格式排查,个人项目笔记。
- OpenAI Function Calling Documentation. (n.d.). OpenAI. 检索于 2025 年 1 月 5 日,来源: https://platform.openai.com/docs/guides/function-calling