[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"i-ri:search-line":3,"i-ri:moon-line":8,"i-ri:sun-line":10,"i-ri:menu-3-line":12,"i-ri:close-line":14,"notes-module-items-ai":16,"notes-page-ai-02-agent-architecture\u002F01-tool-use":74,"i-ri:arrow-left-s-line":1350,"i-ri:menu-2-line":1352,"i-ri:arrow-right-s-line":1354,"i-ep:copy-document":1356},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"currentColor\" d=\"m18.031 16.617l4.283 4.282l-1.415 1.415l-4.282-4.283A8.96 8.96 0 0 1 11 20c-4.968 0-9-4.032-9-9s4.032-9 9-9s9 4.032 9 9a8.96 8.96 0 0 1-1.969 5.617m-2.006-.742A6.98 6.98 0 0 0 18 11c0-3.867-3.133-7-7-7s-7 3.133-7 7s3.133 7 7 7a6.98 6.98 0 0 0 4.875-1.975z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"currentColor\" d=\"M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12S6.477 2 12 2h.1A6.98 6.98 0 0 0 10 7m-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938A8 8 0 0 0 4 12\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":11},"\u003Cpath fill=\"currentColor\" d=\"M12 18a6 6 0 1 1 0-12a6 6 0 0 1 0 12m0-2a4 4 0 1 0 0-8a4 4 0 0 0 0 8M11 1h2v3h-2zm0 19h2v3h-2zM3.515 4.929l1.414-1.414L7.05 5.636L5.636 7.05zM16.95 18.364l1.414-1.414l2.121 2.121l-1.414 1.414zm2.121-14.85l1.414 1.415l-2.121 2.121l-1.414-1.414zM5.636 16.95l1.414 1.414l-2.121 2.121l-1.414-1.414zM23 11v2h-3v-2zM4 11v2H1v-2z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":13},"\u003Cpath fill=\"currentColor\" d=\"M3 4h18v2H3zm6 7h12v2H9zm-6 7h18v2H3z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":15},"\u003Cpath fill=\"currentColor\" d=\"m12 10.587l4.95-4.95l1.414 1.414l-4.95 4.95l4.95 4.95l-1.415 1.414l-4.95-4.95l-4.949 4.95l-1.414-1.415l4.95-4.95l-4.95-4.95L7.05 5.638z\"\u002F>",[17,22,26,30,34,38,42,46,50,54,58,62,66,70],{"path":18,"title":19,"meta":20},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F01-hello-llm","LLM 交互基础：透过现象看本质",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},"2026-11-05T09:47:15.000Z",{"path":23,"title":24,"meta":25},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F02-streaming-and-prompts","进阶交互：流式传输与提示词工程",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":27,"title":28,"meta":29},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F03-function-calling","结构化输出与 Function Calling",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":31,"title":32,"meta":33},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F01-tool-use","Agent 核心能力 ①：Tool Use（工具调用）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":35,"title":36,"meta":37},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F02-mini-cursor-cli","实战项目：Mini Cursor（CLI 版）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":39,"title":40,"meta":41},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F03-memory","Agent 核心能力 ②：Memory（记忆管理）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":43,"title":44,"meta":45},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F04-planning","Agent 核心能力 ③：Planning（规划能力）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":47,"title":48,"meta":49},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F05-review-diff-chunking","代码改了很多，AI 为什么只审了前面？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":51,"title":52,"meta":53},"\u002Fnotes\u002Fai\u002F02-agent-architecture","Agent 架构笔记",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":55,"title":56,"meta":57},"\u002Fnotes\u002Fai\u002F03-rag\u002F01-vector-db-and-embeddings-node","RAG ①：Embeddings 与向量数据库入门（Node.js 优先）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":59,"title":60,"meta":61},"\u002Fnotes\u002Fai\u002F04-document-extraction\u002F01-ocr-field-evidence","OCR 读对了字，为什么地址仍然错？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":63,"title":64,"meta":65},"\u002Fnotes\u002Fai\u002F05-development-workflow\u002F01-task-recovery","AI 任务做一半，中断后怎么继续？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":67,"title":68,"meta":69},"\u002Fnotes\u002Fai","AI 应用开发",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":71,"title":72,"meta":73},"\u002Fnotes\u002Fai\u002Froadmap","AI Agent 全栈开发学习路线图 (Roadmap)",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"id":75,"type":76,"sourcePath":77,"routePath":31,"path":31,"title":32,"updatedAt":21,"meta":78,"body":80,"toc":1338},"bm90ZTpub3Rlcy9haS8wMi1hZ2VudC1hcmNoaXRlY3R1cmUvMDEtdG9vbC11c2UubWQ","note","notes\u002Fai\u002F02-agent-architecture\u002F01-tool-use.md",{"id":75,"type":76,"sourcePath":77,"slug":79,"title":32,"updatedAt":21,"lastUpdated":21},"ai\u002F02-agent-architecture\u002F01-tool-use",{"type":81,"children":82},"root",[83,91,107,119,123,130,135,172,184,187,193,207,244,249,526,536,549,552,558,571,604,609,1083,1093,1096,1102,1109,1114,1132,1138,1143,1161,1166,1172,1177,1203,1209,1214,1227,1232,1259,1262,1268,1280,1285,1332],{"type":84,"tag":85,"props":86,"children":88},"element","h1",{"id":87},"agent-核心能力-tool-use工具调用",[89],{"type":90,"value":32},"text",{"type":84,"tag":92,"props":93,"children":94},"blockquote",{},[95],{"type":84,"tag":96,"props":97,"children":98},"p",{},[99,105],{"type":84,"tag":100,"props":101,"children":102},"strong",{},[103],{"type":90,"value":104},"摘要",{"type":90,"value":106},"：Chatbot 只能“说”，Agent 必须能“做”。所谓 Tool Use，就是把 LLM 的意图（要做什么）变成可执行的动作（调用函数 \u002F API \u002F 文件系统 \u002F 浏览器），再把执行结果反馈给模型，让它继续决策。",{"type":84,"tag":96,"props":108,"children":109},{},[110,112,117],{"type":90,"value":111},"Agent = LLM + Planning + Memory + Tools。本章先把 ",{"type":84,"tag":100,"props":113,"children":114},{},[115],{"type":90,"value":116},"Tools",{"type":90,"value":118}," 这条链路打通。",{"type":84,"tag":120,"props":121,"children":122},"hr",{},[],{"type":84,"tag":124,"props":125,"children":127},"h2",{"id":126},"_1-tool-use-在系统里的位置",[128],{"type":90,"value":129},"1. Tool Use 在系统里的位置",{"type":84,"tag":96,"props":131,"children":132},{},[133],{"type":90,"value":134},"一个最小的 Agent 循环通常长这样：",{"type":84,"tag":136,"props":137,"children":138},"ol",{},[139,145,157,162,167],{"type":84,"tag":140,"props":141,"children":142},"li",{},[143],{"type":90,"value":144},"用户提问",{"type":84,"tag":140,"props":146,"children":147},{},[148,150,155],{"type":90,"value":149},"LLM 选择：直接回答 ",{"type":84,"tag":100,"props":151,"children":152},{},[153],{"type":90,"value":154},"or",{"type":90,"value":156}," 请求调用工具（tool call）",{"type":84,"tag":140,"props":158,"children":159},{},[160],{"type":90,"value":161},"你的程序执行工具（真实世界动作）",{"type":84,"tag":140,"props":163,"children":164},{},[165],{"type":90,"value":166},"把工具结果回传给 LLM",{"type":84,"tag":140,"props":168,"children":169},{},[170],{"type":90,"value":171},"LLM 基于结果输出最终答复（或继续调用下一个工具）",{"type":84,"tag":96,"props":173,"children":174},{},[175,177,182],{"type":90,"value":176},"关键点：",{"type":84,"tag":100,"props":178,"children":179},{},[180],{"type":90,"value":181},"LLM 不会真的执行代码",{"type":90,"value":183},"。真正的副作用（读写文件、发请求、改 DB）都发生在你的程序里。",{"type":84,"tag":120,"props":185,"children":186},{},[],{"type":84,"tag":124,"props":188,"children":190},{"id":189},"_2-tools-schema你要给模型什么信息",[191],{"type":90,"value":192},"2. Tools Schema：你要给模型什么信息",{"type":84,"tag":96,"props":194,"children":195},{},[196,198,205],{"type":90,"value":197},"以 OpenAI 兼容协议为例，工具通常以 ",{"type":84,"tag":199,"props":200,"children":202},"code",{"className":201},[],[203],{"type":90,"value":204},"tools",{"type":90,"value":206}," 数组声明，每个工具包含：",{"type":84,"tag":208,"props":209,"children":210},"ul",{},[211,222,233],{"type":84,"tag":140,"props":212,"children":213},{},[214,220],{"type":84,"tag":199,"props":215,"children":217},{"className":216},[],[218],{"type":90,"value":219},"name",{"type":90,"value":221},"：工具名（唯一、稳定、尽量短）",{"type":84,"tag":140,"props":223,"children":224},{},[225,231],{"type":84,"tag":199,"props":226,"children":228},{"className":227},[],[229],{"type":90,"value":230},"description",{"type":90,"value":232},"：给模型看的说明（决定它会不会用、怎么用）",{"type":84,"tag":140,"props":234,"children":235},{},[236,242],{"type":84,"tag":199,"props":237,"children":239},{"className":238},[],[240],{"type":90,"value":241},"parameters",{"type":90,"value":243},"：JSON Schema（决定参数结构稳定性）",{"type":84,"tag":96,"props":245,"children":246},{},[247],{"type":90,"value":248},"示例（读文件）：",{"type":84,"tag":250,"props":251,"children":256},"pre",{"className":252,"code":253,"language":254,"meta":255,"style":255},"language-json shiki shiki-themes github-light github-dark","{\n  \"type\": \"function\",\n  \"function\": {\n    \"name\": \"read_file\",\n    \"description\": \"读取本地文件内容，用于理解代码或文档\",\n    \"parameters\": {\n      \"type\": \"object\",\n      \"properties\": {\n        \"path\": {\n          \"type\": \"string\",\n          \"description\": \"相对路径或绝对路径，例如 content\u002Fnotes\u002Fai\u002Froadmap.md\"\n        }\n      },\n      \"required\": [\"path\"]\n    }\n  }\n}\n","json","",[257],{"type":84,"tag":199,"props":258,"children":259},{"__ignoreMap":255},[260,272,298,312,334,356,369,391,404,417,439,457,466,475,499,508,517],{"type":84,"tag":261,"props":262,"children":265},"span",{"class":263,"line":264},"line",1,[266],{"type":84,"tag":261,"props":267,"children":269},{"style":268},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[270],{"type":90,"value":271},"{\n",{"type":84,"tag":261,"props":273,"children":275},{"class":263,"line":274},2,[276,282,287,293],{"type":84,"tag":261,"props":277,"children":279},{"style":278},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[280],{"type":90,"value":281},"  \"type\"",{"type":84,"tag":261,"props":283,"children":284},{"style":268},[285],{"type":90,"value":286},": ",{"type":84,"tag":261,"props":288,"children":290},{"style":289},"--shiki-default:#032F62;--shiki-dark:#9ECBFF",[291],{"type":90,"value":292},"\"function\"",{"type":84,"tag":261,"props":294,"children":295},{"style":268},[296],{"type":90,"value":297},",\n",{"type":84,"tag":261,"props":299,"children":301},{"class":263,"line":300},3,[302,307],{"type":84,"tag":261,"props":303,"children":304},{"style":278},[305],{"type":90,"value":306},"  \"function\"",{"type":84,"tag":261,"props":308,"children":309},{"style":268},[310],{"type":90,"value":311},": {\n",{"type":84,"tag":261,"props":313,"children":315},{"class":263,"line":314},4,[316,321,325,330],{"type":84,"tag":261,"props":317,"children":318},{"style":278},[319],{"type":90,"value":320},"    \"name\"",{"type":84,"tag":261,"props":322,"children":323},{"style":268},[324],{"type":90,"value":286},{"type":84,"tag":261,"props":326,"children":327},{"style":289},[328],{"type":90,"value":329},"\"read_file\"",{"type":84,"tag":261,"props":331,"children":332},{"style":268},[333],{"type":90,"value":297},{"type":84,"tag":261,"props":335,"children":337},{"class":263,"line":336},5,[338,343,347,352],{"type":84,"tag":261,"props":339,"children":340},{"style":278},[341],{"type":90,"value":342},"    \"description\"",{"type":84,"tag":261,"props":344,"children":345},{"style":268},[346],{"type":90,"value":286},{"type":84,"tag":261,"props":348,"children":349},{"style":289},[350],{"type":90,"value":351},"\"读取本地文件内容，用于理解代码或文档\"",{"type":84,"tag":261,"props":353,"children":354},{"style":268},[355],{"type":90,"value":297},{"type":84,"tag":261,"props":357,"children":359},{"class":263,"line":358},6,[360,365],{"type":84,"tag":261,"props":361,"children":362},{"style":278},[363],{"type":90,"value":364},"    \"parameters\"",{"type":84,"tag":261,"props":366,"children":367},{"style":268},[368],{"type":90,"value":311},{"type":84,"tag":261,"props":370,"children":372},{"class":263,"line":371},7,[373,378,382,387],{"type":84,"tag":261,"props":374,"children":375},{"style":278},[376],{"type":90,"value":377},"      \"type\"",{"type":84,"tag":261,"props":379,"children":380},{"style":268},[381],{"type":90,"value":286},{"type":84,"tag":261,"props":383,"children":384},{"style":289},[385],{"type":90,"value":386},"\"object\"",{"type":84,"tag":261,"props":388,"children":389},{"style":268},[390],{"type":90,"value":297},{"type":84,"tag":261,"props":392,"children":394},{"class":263,"line":393},8,[395,400],{"type":84,"tag":261,"props":396,"children":397},{"style":278},[398],{"type":90,"value":399},"      \"properties\"",{"type":84,"tag":261,"props":401,"children":402},{"style":268},[403],{"type":90,"value":311},{"type":84,"tag":261,"props":405,"children":407},{"class":263,"line":406},9,[408,413],{"type":84,"tag":261,"props":409,"children":410},{"style":278},[411],{"type":90,"value":412},"        \"path\"",{"type":84,"tag":261,"props":414,"children":415},{"style":268},[416],{"type":90,"value":311},{"type":84,"tag":261,"props":418,"children":420},{"class":263,"line":419},10,[421,426,430,435],{"type":84,"tag":261,"props":422,"children":423},{"style":278},[424],{"type":90,"value":425},"          \"type\"",{"type":84,"tag":261,"props":427,"children":428},{"style":268},[429],{"type":90,"value":286},{"type":84,"tag":261,"props":431,"children":432},{"style":289},[433],{"type":90,"value":434},"\"string\"",{"type":84,"tag":261,"props":436,"children":437},{"style":268},[438],{"type":90,"value":297},{"type":84,"tag":261,"props":440,"children":442},{"class":263,"line":441},11,[443,448,452],{"type":84,"tag":261,"props":444,"children":445},{"style":278},[446],{"type":90,"value":447},"          \"description\"",{"type":84,"tag":261,"props":449,"children":450},{"style":268},[451],{"type":90,"value":286},{"type":84,"tag":261,"props":453,"children":454},{"style":289},[455],{"type":90,"value":456},"\"相对路径或绝对路径，例如 content\u002Fnotes\u002Fai\u002Froadmap.md\"\n",{"type":84,"tag":261,"props":458,"children":460},{"class":263,"line":459},12,[461],{"type":84,"tag":261,"props":462,"children":463},{"style":268},[464],{"type":90,"value":465},"        }\n",{"type":84,"tag":261,"props":467,"children":469},{"class":263,"line":468},13,[470],{"type":84,"tag":261,"props":471,"children":472},{"style":268},[473],{"type":90,"value":474},"      },\n",{"type":84,"tag":261,"props":476,"children":478},{"class":263,"line":477},14,[479,484,489,494],{"type":84,"tag":261,"props":480,"children":481},{"style":278},[482],{"type":90,"value":483},"      \"required\"",{"type":84,"tag":261,"props":485,"children":486},{"style":268},[487],{"type":90,"value":488},": [",{"type":84,"tag":261,"props":490,"children":491},{"style":289},[492],{"type":90,"value":493},"\"path\"",{"type":84,"tag":261,"props":495,"children":496},{"style":268},[497],{"type":90,"value":498},"]\n",{"type":84,"tag":261,"props":500,"children":502},{"class":263,"line":501},15,[503],{"type":84,"tag":261,"props":504,"children":505},{"style":268},[506],{"type":90,"value":507},"    }\n",{"type":84,"tag":261,"props":509,"children":511},{"class":263,"line":510},16,[512],{"type":84,"tag":261,"props":513,"children":514},{"style":268},[515],{"type":90,"value":516},"  }\n",{"type":84,"tag":261,"props":518,"children":520},{"class":263,"line":519},17,[521],{"type":84,"tag":261,"props":522,"children":523},{"style":268},[524],{"type":90,"value":525},"}\n",{"type":84,"tag":96,"props":527,"children":528},{},[529,534],{"type":84,"tag":100,"props":530,"children":531},{},[532],{"type":90,"value":533},"工程经验",{"type":90,"value":535},"：",{"type":84,"tag":208,"props":537,"children":538},{},[539,544],{"type":84,"tag":140,"props":540,"children":541},{},[542],{"type":90,"value":543},"description 要“可操作”，不要抽象（例如“读取文件内容（必要时分段）”比“读取文件”更好）",{"type":84,"tag":140,"props":545,"children":546},{},[547],{"type":90,"value":548},"参数 schema 要“紧”，该必填必填、枚举就枚举（越松越容易产生垃圾参数）",{"type":84,"tag":120,"props":550,"children":551},{},[],{"type":84,"tag":124,"props":553,"children":555},{"id":554},"_3-tool-call-的闭环从模型到代码再回模型",[556],{"type":90,"value":557},"3. Tool Call 的闭环：从模型到代码再回模型",{"type":84,"tag":96,"props":559,"children":560},{},[561,563,569],{"type":90,"value":562},"当模型决定使用工具，它会返回 ",{"type":84,"tag":199,"props":564,"children":566},{"className":565},[],[567],{"type":90,"value":568},"tool_calls",{"type":90,"value":570},"（或类似结构），此时：",{"type":84,"tag":208,"props":572,"children":573},{},[574,585],{"type":84,"tag":140,"props":575,"children":576},{},[577,583],{"type":84,"tag":199,"props":578,"children":580},{"className":579},[],[581],{"type":90,"value":582},"assistant.content",{"type":90,"value":584}," 可能为空",{"type":84,"tag":140,"props":586,"children":587},{},[588,590,596,598],{"type":90,"value":589},"你要解析出 ",{"type":84,"tag":199,"props":591,"children":593},{"className":592},[],[594],{"type":90,"value":595},"function.name",{"type":90,"value":597}," 和 ",{"type":84,"tag":199,"props":599,"children":601},{"className":600},[],[602],{"type":90,"value":603},"function.arguments",{"type":84,"tag":96,"props":605,"children":606},{},[607],{"type":90,"value":608},"伪代码（语言无关）：",{"type":84,"tag":250,"props":610,"children":614},{"className":611,"code":612,"language":613,"meta":255,"style":255},"language-ts shiki shiki-themes github-light github-dark","while (true) {\n  const resp = await llm(messages, tools)\n  const msg = resp.choices[0].message\n\n  if (!msg.tool_calls?.length) {\n    \u002F\u002F 没有工具调用：直接结束\n    return msg.content\n  }\n\n  \u002F\u002F 逐个执行工具\n  for (const call of msg.tool_calls) {\n    const { name, arguments: argsJson } = call.function\n    const args = JSON.parse(argsJson)\n\n    const result = await runTool(name, args)\n\n    messages.push(msg) \u002F\u002F 把 tool_calls 这条 assistant 消息也放进历史\n    messages.push({\n      role: \"tool\",\n      tool_call_id: call.id,\n      name,\n      content: JSON.stringify(result)\n    })\n  }\n}\n","ts",[615],{"type":84,"tag":199,"props":616,"children":617},{"__ignoreMap":255},[618,642,676,707,716,747,756,769,776,783,791,823,875,911,918,948,955,978,995,1013,1022,1031,1059,1068,1075],{"type":84,"tag":261,"props":619,"children":620},{"class":263,"line":264},[621,627,632,637],{"type":84,"tag":261,"props":622,"children":624},{"style":623},"--shiki-default:#D73A49;--shiki-dark:#F97583",[625],{"type":90,"value":626},"while",{"type":84,"tag":261,"props":628,"children":629},{"style":268},[630],{"type":90,"value":631}," (",{"type":84,"tag":261,"props":633,"children":634},{"style":278},[635],{"type":90,"value":636},"true",{"type":84,"tag":261,"props":638,"children":639},{"style":268},[640],{"type":90,"value":641},") {\n",{"type":84,"tag":261,"props":643,"children":644},{"class":263,"line":274},[645,650,655,660,665,671],{"type":84,"tag":261,"props":646,"children":647},{"style":623},[648],{"type":90,"value":649},"  const",{"type":84,"tag":261,"props":651,"children":652},{"style":278},[653],{"type":90,"value":654}," resp",{"type":84,"tag":261,"props":656,"children":657},{"style":623},[658],{"type":90,"value":659}," =",{"type":84,"tag":261,"props":661,"children":662},{"style":623},[663],{"type":90,"value":664}," await",{"type":84,"tag":261,"props":666,"children":668},{"style":667},"--shiki-default:#6F42C1;--shiki-dark:#B392F0",[669],{"type":90,"value":670}," llm",{"type":84,"tag":261,"props":672,"children":673},{"style":268},[674],{"type":90,"value":675},"(messages, tools)\n",{"type":84,"tag":261,"props":677,"children":678},{"class":263,"line":300},[679,683,688,692,697,702],{"type":84,"tag":261,"props":680,"children":681},{"style":623},[682],{"type":90,"value":649},{"type":84,"tag":261,"props":684,"children":685},{"style":278},[686],{"type":90,"value":687}," msg",{"type":84,"tag":261,"props":689,"children":690},{"style":623},[691],{"type":90,"value":659},{"type":84,"tag":261,"props":693,"children":694},{"style":268},[695],{"type":90,"value":696}," resp.choices[",{"type":84,"tag":261,"props":698,"children":699},{"style":278},[700],{"type":90,"value":701},"0",{"type":84,"tag":261,"props":703,"children":704},{"style":268},[705],{"type":90,"value":706},"].message\n",{"type":84,"tag":261,"props":708,"children":709},{"class":263,"line":314},[710],{"type":84,"tag":261,"props":711,"children":713},{"emptyLinePlaceholder":712},true,[714],{"type":90,"value":715},"\n",{"type":84,"tag":261,"props":717,"children":718},{"class":263,"line":336},[719,724,728,733,738,743],{"type":84,"tag":261,"props":720,"children":721},{"style":623},[722],{"type":90,"value":723},"  if",{"type":84,"tag":261,"props":725,"children":726},{"style":268},[727],{"type":90,"value":631},{"type":84,"tag":261,"props":729,"children":730},{"style":623},[731],{"type":90,"value":732},"!",{"type":84,"tag":261,"props":734,"children":735},{"style":268},[736],{"type":90,"value":737},"msg.tool_calls?.",{"type":84,"tag":261,"props":739,"children":740},{"style":278},[741],{"type":90,"value":742},"length",{"type":84,"tag":261,"props":744,"children":745},{"style":268},[746],{"type":90,"value":641},{"type":84,"tag":261,"props":748,"children":749},{"class":263,"line":358},[750],{"type":84,"tag":261,"props":751,"children":753},{"style":752},"--shiki-default:#6A737D;--shiki-dark:#6A737D",[754],{"type":90,"value":755},"    \u002F\u002F 没有工具调用：直接结束\n",{"type":84,"tag":261,"props":757,"children":758},{"class":263,"line":371},[759,764],{"type":84,"tag":261,"props":760,"children":761},{"style":623},[762],{"type":90,"value":763},"    return",{"type":84,"tag":261,"props":765,"children":766},{"style":268},[767],{"type":90,"value":768}," msg.content\n",{"type":84,"tag":261,"props":770,"children":771},{"class":263,"line":393},[772],{"type":84,"tag":261,"props":773,"children":774},{"style":268},[775],{"type":90,"value":516},{"type":84,"tag":261,"props":777,"children":778},{"class":263,"line":406},[779],{"type":84,"tag":261,"props":780,"children":781},{"emptyLinePlaceholder":712},[782],{"type":90,"value":715},{"type":84,"tag":261,"props":784,"children":785},{"class":263,"line":419},[786],{"type":84,"tag":261,"props":787,"children":788},{"style":752},[789],{"type":90,"value":790},"  \u002F\u002F 逐个执行工具\n",{"type":84,"tag":261,"props":792,"children":793},{"class":263,"line":441},[794,799,803,808,813,818],{"type":84,"tag":261,"props":795,"children":796},{"style":623},[797],{"type":90,"value":798},"  for",{"type":84,"tag":261,"props":800,"children":801},{"style":268},[802],{"type":90,"value":631},{"type":84,"tag":261,"props":804,"children":805},{"style":623},[806],{"type":90,"value":807},"const",{"type":84,"tag":261,"props":809,"children":810},{"style":278},[811],{"type":90,"value":812}," call",{"type":84,"tag":261,"props":814,"children":815},{"style":623},[816],{"type":90,"value":817}," of",{"type":84,"tag":261,"props":819,"children":820},{"style":268},[821],{"type":90,"value":822}," msg.tool_calls) {\n",{"type":84,"tag":261,"props":824,"children":825},{"class":263,"line":459},[826,831,836,840,845,851,855,860,865,870],{"type":84,"tag":261,"props":827,"children":828},{"style":623},[829],{"type":90,"value":830},"    const",{"type":84,"tag":261,"props":832,"children":833},{"style":268},[834],{"type":90,"value":835}," { ",{"type":84,"tag":261,"props":837,"children":838},{"style":278},[839],{"type":90,"value":219},{"type":84,"tag":261,"props":841,"children":842},{"style":268},[843],{"type":90,"value":844},", ",{"type":84,"tag":261,"props":846,"children":848},{"style":847},"--shiki-default:#E36209;--shiki-dark:#FFAB70",[849],{"type":90,"value":850},"arguments",{"type":84,"tag":261,"props":852,"children":853},{"style":268},[854],{"type":90,"value":286},{"type":84,"tag":261,"props":856,"children":857},{"style":278},[858],{"type":90,"value":859},"argsJson",{"type":84,"tag":261,"props":861,"children":862},{"style":268},[863],{"type":90,"value":864}," } ",{"type":84,"tag":261,"props":866,"children":867},{"style":623},[868],{"type":90,"value":869},"=",{"type":84,"tag":261,"props":871,"children":872},{"style":268},[873],{"type":90,"value":874}," call.function\n",{"type":84,"tag":261,"props":876,"children":877},{"class":263,"line":468},[878,882,887,891,896,901,906],{"type":84,"tag":261,"props":879,"children":880},{"style":623},[881],{"type":90,"value":830},{"type":84,"tag":261,"props":883,"children":884},{"style":278},[885],{"type":90,"value":886}," args",{"type":84,"tag":261,"props":888,"children":889},{"style":623},[890],{"type":90,"value":659},{"type":84,"tag":261,"props":892,"children":893},{"style":278},[894],{"type":90,"value":895}," JSON",{"type":84,"tag":261,"props":897,"children":898},{"style":268},[899],{"type":90,"value":900},".",{"type":84,"tag":261,"props":902,"children":903},{"style":667},[904],{"type":90,"value":905},"parse",{"type":84,"tag":261,"props":907,"children":908},{"style":268},[909],{"type":90,"value":910},"(argsJson)\n",{"type":84,"tag":261,"props":912,"children":913},{"class":263,"line":477},[914],{"type":84,"tag":261,"props":915,"children":916},{"emptyLinePlaceholder":712},[917],{"type":90,"value":715},{"type":84,"tag":261,"props":919,"children":920},{"class":263,"line":501},[921,925,930,934,938,943],{"type":84,"tag":261,"props":922,"children":923},{"style":623},[924],{"type":90,"value":830},{"type":84,"tag":261,"props":926,"children":927},{"style":278},[928],{"type":90,"value":929}," result",{"type":84,"tag":261,"props":931,"children":932},{"style":623},[933],{"type":90,"value":659},{"type":84,"tag":261,"props":935,"children":936},{"style":623},[937],{"type":90,"value":664},{"type":84,"tag":261,"props":939,"children":940},{"style":667},[941],{"type":90,"value":942}," runTool",{"type":84,"tag":261,"props":944,"children":945},{"style":268},[946],{"type":90,"value":947},"(name, args)\n",{"type":84,"tag":261,"props":949,"children":950},{"class":263,"line":510},[951],{"type":84,"tag":261,"props":952,"children":953},{"emptyLinePlaceholder":712},[954],{"type":90,"value":715},{"type":84,"tag":261,"props":956,"children":957},{"class":263,"line":519},[958,963,968,973],{"type":84,"tag":261,"props":959,"children":960},{"style":268},[961],{"type":90,"value":962},"    messages.",{"type":84,"tag":261,"props":964,"children":965},{"style":667},[966],{"type":90,"value":967},"push",{"type":84,"tag":261,"props":969,"children":970},{"style":268},[971],{"type":90,"value":972},"(msg) ",{"type":84,"tag":261,"props":974,"children":975},{"style":752},[976],{"type":90,"value":977},"\u002F\u002F 把 tool_calls 这条 assistant 消息也放进历史\n",{"type":84,"tag":261,"props":979,"children":981},{"class":263,"line":980},18,[982,986,990],{"type":84,"tag":261,"props":983,"children":984},{"style":268},[985],{"type":90,"value":962},{"type":84,"tag":261,"props":987,"children":988},{"style":667},[989],{"type":90,"value":967},{"type":84,"tag":261,"props":991,"children":992},{"style":268},[993],{"type":90,"value":994},"({\n",{"type":84,"tag":261,"props":996,"children":998},{"class":263,"line":997},19,[999,1004,1009],{"type":84,"tag":261,"props":1000,"children":1001},{"style":268},[1002],{"type":90,"value":1003},"      role: ",{"type":84,"tag":261,"props":1005,"children":1006},{"style":289},[1007],{"type":90,"value":1008},"\"tool\"",{"type":84,"tag":261,"props":1010,"children":1011},{"style":268},[1012],{"type":90,"value":297},{"type":84,"tag":261,"props":1014,"children":1016},{"class":263,"line":1015},20,[1017],{"type":84,"tag":261,"props":1018,"children":1019},{"style":268},[1020],{"type":90,"value":1021},"      tool_call_id: call.id,\n",{"type":84,"tag":261,"props":1023,"children":1025},{"class":263,"line":1024},21,[1026],{"type":84,"tag":261,"props":1027,"children":1028},{"style":268},[1029],{"type":90,"value":1030},"      name,\n",{"type":84,"tag":261,"props":1032,"children":1034},{"class":263,"line":1033},22,[1035,1040,1045,1049,1054],{"type":84,"tag":261,"props":1036,"children":1037},{"style":268},[1038],{"type":90,"value":1039},"      content: ",{"type":84,"tag":261,"props":1041,"children":1042},{"style":278},[1043],{"type":90,"value":1044},"JSON",{"type":84,"tag":261,"props":1046,"children":1047},{"style":268},[1048],{"type":90,"value":900},{"type":84,"tag":261,"props":1050,"children":1051},{"style":667},[1052],{"type":90,"value":1053},"stringify",{"type":84,"tag":261,"props":1055,"children":1056},{"style":268},[1057],{"type":90,"value":1058},"(result)\n",{"type":84,"tag":261,"props":1060,"children":1062},{"class":263,"line":1061},23,[1063],{"type":84,"tag":261,"props":1064,"children":1065},{"style":268},[1066],{"type":90,"value":1067},"    })\n",{"type":84,"tag":261,"props":1069,"children":1070},{"class":263,"line":5},[1071],{"type":84,"tag":261,"props":1072,"children":1073},{"style":268},[1074],{"type":90,"value":516},{"type":84,"tag":261,"props":1076,"children":1078},{"class":263,"line":1077},25,[1079],{"type":84,"tag":261,"props":1080,"children":1081},{"style":268},[1082],{"type":90,"value":525},{"type":84,"tag":96,"props":1084,"children":1085},{},[1086,1091],{"type":84,"tag":100,"props":1087,"children":1088},{},[1089],{"type":90,"value":1090},"为什么要把 tool 的结果也放进 messages？",{"type":90,"value":1092},"\n因为模型是无状态的；你不把结果给它，它就不知道你执行了什么。",{"type":84,"tag":120,"props":1094,"children":1095},{},[],{"type":84,"tag":124,"props":1097,"children":1099},{"id":1098},"_4-常见坑与护栏必须做",[1100],{"type":90,"value":1101},"4. 常见坑与护栏（必须做）",{"type":84,"tag":1103,"props":1104,"children":1106},"h3",{"id":1105},"_41-参数不可信必须校验",[1107],{"type":90,"value":1108},"4.1 参数不可信：必须校验",{"type":84,"tag":96,"props":1110,"children":1111},{},[1112],{"type":90,"value":1113},"模型生成的参数只是“建议”，不是事实。",{"type":84,"tag":208,"props":1115,"children":1116},{},[1117,1122,1127],{"type":84,"tag":140,"props":1118,"children":1119},{},[1120],{"type":90,"value":1121},"路径必须做白名单\u002F沙箱",{"type":84,"tag":140,"props":1123,"children":1124},{},[1125],{"type":90,"value":1126},"URL 必须做 allowlist",{"type":84,"tag":140,"props":1128,"children":1129},{},[1130],{"type":90,"value":1131},"数值范围必须校验",{"type":84,"tag":1103,"props":1133,"children":1135},{"id":1134},"_42-工具要可观测记录-trace",[1136],{"type":90,"value":1137},"4.2 工具要可观测：记录 trace",{"type":84,"tag":96,"props":1139,"children":1140},{},[1141],{"type":90,"value":1142},"至少记录：",{"type":84,"tag":208,"props":1144,"children":1145},{},[1146,1151,1156],{"type":84,"tag":140,"props":1147,"children":1148},{},[1149],{"type":90,"value":1150},"tool name \u002F args \u002F result（可脱敏）",{"type":84,"tag":140,"props":1152,"children":1153},{},[1154],{"type":90,"value":1155},"latency",{"type":84,"tag":140,"props":1157,"children":1158},{},[1159],{"type":90,"value":1160},"token usage",{"type":84,"tag":96,"props":1162,"children":1163},{},[1164],{"type":90,"value":1165},"否则你很难 debug：到底是模型不行，还是工具不行。",{"type":84,"tag":1103,"props":1167,"children":1169},{"id":1168},"_43-工具结果要给模型看得懂",[1170],{"type":90,"value":1171},"4.3 工具结果要“给模型看得懂”",{"type":84,"tag":96,"props":1173,"children":1174},{},[1175],{"type":90,"value":1176},"工具输出不要塞一坨原始数据（尤其是大 JSON）。建议：",{"type":84,"tag":208,"props":1178,"children":1179},{},[1180,1185,1190],{"type":84,"tag":140,"props":1181,"children":1182},{},[1183],{"type":90,"value":1184},"做摘要（summary）",{"type":84,"tag":140,"props":1186,"children":1187},{},[1188],{"type":90,"value":1189},"给出关键字段",{"type":84,"tag":140,"props":1191,"children":1192},{},[1193,1195,1201],{"type":90,"value":1194},"给出可选的 ",{"type":84,"tag":199,"props":1196,"children":1198},{"className":1197},[],[1199],{"type":90,"value":1200},"raw",{"type":90,"value":1202},"（必要时再取）",{"type":84,"tag":1103,"props":1204,"children":1206},{"id":1205},"_44-多工具调用与循环",[1207],{"type":90,"value":1208},"4.4 多工具调用与循环",{"type":84,"tag":96,"props":1210,"children":1211},{},[1212],{"type":90,"value":1213},"模型可能：",{"type":84,"tag":208,"props":1215,"children":1216},{},[1217,1222],{"type":84,"tag":140,"props":1218,"children":1219},{},[1220],{"type":90,"value":1221},"一次请求多个工具",{"type":84,"tag":140,"props":1223,"children":1224},{},[1225],{"type":90,"value":1226},"或不断重复同一个工具（陷入循环）",{"type":84,"tag":96,"props":1228,"children":1229},{},[1230],{"type":90,"value":1231},"护栏：",{"type":84,"tag":208,"props":1233,"children":1234},{},[1235,1254],{"type":84,"tag":140,"props":1236,"children":1237},{},[1238,1240,1246,1248],{"type":90,"value":1239},"设置 ",{"type":84,"tag":199,"props":1241,"children":1243},{"className":1242},[],[1244],{"type":90,"value":1245},"max_tool_calls",{"type":90,"value":1247}," \u002F ",{"type":84,"tag":199,"props":1249,"children":1251},{"className":1250},[],[1252],{"type":90,"value":1253},"max_steps",{"type":84,"tag":140,"props":1255,"children":1256},{},[1257],{"type":90,"value":1258},"对重复调用做去重或阈值限制",{"type":84,"tag":120,"props":1260,"children":1261},{},[],{"type":84,"tag":124,"props":1263,"children":1265},{"id":1264},"_5-从-tool-use-到-mini-cursor",[1266],{"type":90,"value":1267},"5. 从 Tool Use 到 Mini Cursor",{"type":84,"tag":96,"props":1269,"children":1270},{},[1271,1273,1278],{"type":90,"value":1272},"Tool Use 的终极练习是：让模型能够 ",{"type":84,"tag":100,"props":1274,"children":1275},{},[1276],{"type":90,"value":1277},"读代码 -> 理解 -> 修改文件 -> 再读 -> 自检",{"type":90,"value":1279},"。",{"type":84,"tag":96,"props":1281,"children":1282},{},[1283],{"type":90,"value":1284},"下一篇我们做一个 CLI 版 Mini Cursor：",{"type":84,"tag":208,"props":1286,"children":1287},{},[1288,1322,1327],{"type":84,"tag":140,"props":1289,"children":1290},{},[1291,1293,1299,1300,1306,1307,1313,1314,1320],{"type":90,"value":1292},"提供 ",{"type":84,"tag":199,"props":1294,"children":1296},{"className":1295},[],[1297],{"type":90,"value":1298},"read_file",{"type":90,"value":1247},{"type":84,"tag":199,"props":1301,"children":1303},{"className":1302},[],[1304],{"type":90,"value":1305},"write_file",{"type":90,"value":1247},{"type":84,"tag":199,"props":1308,"children":1310},{"className":1309},[],[1311],{"type":90,"value":1312},"list_dir",{"type":90,"value":1247},{"type":84,"tag":199,"props":1315,"children":1317},{"className":1316},[],[1318],{"type":90,"value":1319},"search",{"type":90,"value":1321}," 等工具",{"type":84,"tag":140,"props":1323,"children":1324},{},[1325],{"type":90,"value":1326},"用一个简单的 agent loop 驱动",{"type":84,"tag":140,"props":1328,"children":1329},{},[1330],{"type":90,"value":1331},"重点解决：token 成本、补丁粒度、修改可靠性",{"type":84,"tag":1333,"props":1334,"children":1335},"style",{},[1336],{"type":90,"value":1337},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":255,"searchDepth":274,"depth":274,"links":1339},[1340,1341,1342,1343,1349],{"id":126,"depth":274,"text":129},{"id":189,"depth":274,"text":192},{"id":554,"depth":274,"text":557},{"id":1098,"depth":274,"text":1101,"children":1344},[1345,1346,1347,1348],{"id":1105,"depth":300,"text":1108},{"id":1134,"depth":300,"text":1137},{"id":1168,"depth":300,"text":1171},{"id":1205,"depth":300,"text":1208},{"id":1264,"depth":274,"text":1267},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1351},"\u003Cpath fill=\"currentColor\" d=\"m10.828 12l4.95 4.95l-1.414 1.415L8 12l6.364-6.364l1.414 1.414z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1353},"\u003Cpath fill=\"currentColor\" d=\"M3 4h18v2H3zm0 7h12v2H3zm0 7h18v2H3z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1355},"\u003Cpath fill=\"currentColor\" d=\"m13.172 12l-4.95-4.95l1.414-1.413L16 12l-6.364 6.364l-1.414-1.415z\"\u002F>",{"left":4,"top":4,"width":1357,"height":1357,"rotate":4,"vFlip":6,"hFlip":6,"body":1358},1024,"\u003Cpath fill=\"currentColor\" d=\"M768 832a128 128 0 0 1-128 128H192A128 128 0 0 1 64 832V384a128 128 0 0 1 128-128v64a64 64 0 0 0-64 64v448a64 64 0 0 0 64 64h448a64 64 0 0 0 64-64z\"\u002F>\u003Cpath fill=\"currentColor\" d=\"M384 128a64 64 0 0 0-64 64v448a64 64 0 0 0 64 64h448a64 64 0 0 0 64-64V192a64 64 0 0 0-64-64zm0-64h448a128 128 0 0 1 128 128v448a128 128 0 0 1-128 128H384a128 128 0 0 1-128-128V192A128 128 0 0 1 384 64\"\u002F>"]