{"_id":"p-api-agent","_rev":"9-49858f3b0cd644930f7bdc03484d384e","name":"p-api-agent","dist-tags":{"latest":"0.0.9"},"versions":{"0.0.1":{"name":"p-api-agent","version":"0.0.1","author":"","license":"ISC","_id":"p-api-agent@0.0.1","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"dist":{"shasum":"9c2afb81b4752cded21362fc79a75c6a956644ab","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.1.tgz","fileCount":6,"integrity":"sha512-MU0qpq3xktYI5LAO2UODnTC/C7CZ2CbSMEy7J/rz/lTe1TgsRZ4SqCt1pk4EyPuUguk5mlSYZyPERxsWtgH72Q==","signatures":[{"sig":"MEQCIDW9nrg39HpWX29JgTa+KAvAaI3B2HZaOze/gGmiLFzoAiBBt2f2zPgK39th9YkUdO9ohd1UL4kVPHh0B72soqSzwA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":26584},"main":"./dist/index.cjs","type":"module","types":"dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"scripts":{"build":"tsup"},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"_npmVersion":"10.8.2","description":"一个基于 LLM 的智能 Agent 框架，支持工具函数调用和多轮对话。","directories":{},"_nodeVersion":"20.18.1","dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"^8.4.0","typescript":"5.8.3","@types/node":"^25.3.5","@types/lodash":"4.17.16","javascript-obfuscator":"^4.1.1"},"_npmOperationalInternal":{"tmp":"tmp/p-api-agent_0.0.1_1772875252001_0.0319761909952303","host":"s3://npm-registry-packages-npm-production"}},"0.0.2":{"name":"p-api-agent","version":"0.0.2","author":"","license":"ISC","_id":"p-api-agent@0.0.2","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"dist":{"shasum":"82b978f096f55382c8f6c9dd38b58c346d88ccde","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.2.tgz","fileCount":6,"integrity":"sha512-XK3iOWyXcTUjdCe2UDxoOLrW7Y5/tGbSjfPKSL/zHV8TbDcq5LgVdlSoekkDeYXtGcJYSsyYtmprQr0Evoed2g==","signatures":[{"sig":"MEYCIQCY/mnq9vNDHv1ftF8AOyatUa7MIYyOZp3ZKJajgf8qRQIhAI8TaY8n5ctigQ5XMxg/ZVzD0aTYOlz9v+iw7U1Bfc0t","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":39225},"main":"./dist/index.cjs","type":"commonjs","types":"dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"gitHead":"de9da82e5fa19d533f16a5535733e13ed5b1a324","scripts":{"build":"tsup"},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"_npmVersion":"10.8.2","description":"一个基于 LLM 的智能 Agent 框架，支持工具函数调用和多轮对话。","directories":{},"_nodeVersion":"20.18.1","dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","tsup":"^8.4.0","lodash":"^4.17.23","ts-node":"^10.9.2","typescript":"5.8.3","@types/node":"^25.3.5","@types/lodash":"4.17.16","javascript-obfuscator":"^4.1.1"},"_npmOperationalInternal":{"tmp":"tmp/p-api-agent_0.0.2_1773057510441_0.7558483869465515","host":"s3://npm-registry-packages-npm-production"}},"0.0.3":{"name":"p-api-agent","version":"0.0.3","author":"","license":"ISC","_id":"p-api-agent@0.0.3","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"dist":{"shasum":"431f8a51004bcb4c2ebc3faa36ae681419487895","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.3.tgz","fileCount":6,"integrity":"sha512-Un+NFEpvpOlC4v8OswsQ5YxcLXJ/+CMDoREzOzi5yaOE39FOwIJCAOudDaH6lAhO14AgjMDmR/wfDsu0lrh2sw==","signatures":[{"sig":"MEUCIFEqvrsymw9YDBjORjIsXAKwR4vDCjbe0iHRqjWyB+7QAiEAnxi1w1/sBbAiEHO9lY5ZqGZxKEmlna+G5BgL7B5OeTg=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":38725},"main":"./dist/index.cjs","type":"module","types":"dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"gitHead":"fe77725d9eae0144e185b0469c53bac98a2ebe99","scripts":{"build":"tsup"},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"_npmVersion":"10.8.2","description":"一个基于 LLM 的智能 Agent 框架，支持工具函数调用和多轮对话。","directories":{},"_nodeVersion":"20.18.1","dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","tsup":"^8.4.0","lodash":"^4.17.23","ts-node":"^10.9.2","typescript":"5.8.3","@types/node":"^25.3.5","@types/lodash":"4.17.16","javascript-obfuscator":"^4.1.1"},"_npmOperationalInternal":{"tmp":"tmp/p-api-agent_0.0.3_1773559282470_0.7213327798616513","host":"s3://npm-registry-packages-npm-production"}},"0.0.4":{"name":"p-api-agent","version":"0.0.4","author":"","license":"ISC","_id":"p-api-agent@0.0.4","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"dist":{"shasum":"3d81544aa0da62d6047d58c6087d6a2a099acf17","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.4.tgz","fileCount":6,"integrity":"sha512-3ZbTsuZwiiGFt1Nztkx5ARhp7FLGiIr7q/xbVcmQfZt5qMMEdefWBu2it4XXviko6o9PL88WwlEBmE7chzrkRg==","signatures":[{"sig":"MEYCIQC81WRQsWChlgeXJQ/95YRf4D0NH8QG73v4NndNfUCtlQIhAL4k4SxwV3aNoRILgme6kOTQWo1lV09/Wp/rEw+FEv+l","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":40669},"main":"./dist/index.cjs","type":"module","types":"dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"gitHead":"fe77725d9eae0144e185b0469c53bac98a2ebe99","scripts":{"build":"tsup"},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"_npmVersion":"10.8.2","description":"一个轻量、高效、模型无关的 LLM Agent 框架。","directories":{},"_nodeVersion":"20.18.1","dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","tsup":"^8.4.0","lodash":"^4.17.23","ts-node":"^10.9.2","typescript":"5.8.3","@types/node":"^25.3.5","@types/lodash":"4.17.16","javascript-obfuscator":"^4.1.1"},"_npmOperationalInternal":{"tmp":"tmp/p-api-agent_0.0.4_1773559812307_0.06557121572744573","host":"s3://npm-registry-packages-npm-production"}},"0.0.5":{"name":"p-api-agent","version":"0.0.5","keywords":["agent","llm","react","function-call","openai","ai"],"author":"","license":"ISC","_id":"p-api-agent@0.0.5","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"homepage":"https://github.com/p-shoko/p-api-agent#readme","bugs":{"url":"https://github.com/p-shoko/p-api-agent/issues"},"dist":{"shasum":"4625cfc4cb4261260bc514316b1a5d4e17d2deca","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.5.tgz","fileCount":6,"integrity":"sha512-p1bDOKnpWPyCotSJ/qsaR0XoUMpm0LYK6Ld+XGYwDfuKAojGU79nSSg3S6DQg3Z5vGtCOwNCbW8ncBpP9oA8BA==","signatures":[{"sig":"MEUCIFnf+U8mQtj9itoOP3xbyI9LZklV5Uu+Popf68rwyEQFAiEAnBv143R1HZ9sHx7mrL3O3l37vN6z1JkqeVUZZ2wMz1o=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":41101},"main":"./dist/index.cjs","type":"module","types":"dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"gitHead":"1aad6927f3a729e4a67f86abe43b6ae8b317b14b","scripts":{"build":"tsup"},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"repository":{"url":"git+https://github.com/p-shoko/p-api-agent.git","type":"git"},"_npmVersion":"10.8.2","description":"一个轻量、高效、模型无关的 LLM Agent 框架，采用 ReAct 单调用循环架构","directories":{},"_nodeVersion":"20.18.1","dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","tsup":"^8.4.0","lodash":"^4.17.23","ts-node":"^10.9.2","typescript":"5.8.3","@types/node":"^25.3.5","@types/lodash":"4.17.16","javascript-obfuscator":"^4.1.1"},"_npmOperationalInternal":{"tmp":"tmp/p-api-agent_0.0.5_1773560456164_0.9072254624676228","host":"s3://npm-registry-packages-npm-production"}},"0.0.6":{"name":"p-api-agent","version":"0.0.6","keywords":["agent","llm","react","function-call","openai","ai"],"author":"","license":"ISC","_id":"p-api-agent@0.0.6","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"homepage":"https://github.com/p-shoko/p-api-agent#readme","bugs":{"url":"https://github.com/p-shoko/p-api-agent/issues"},"dist":{"shasum":"b133d122c8cb53627de75873886da4056e798e78","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.6.tgz","fileCount":6,"integrity":"sha512-uTUJG82smpSKalCv6JjpZyqNk2JanNAIVMA3JP9HYt6opiFTyMuLz/rgGZPNhbd7eeWR8xuQ3T0MPoIR9gJBNQ==","signatures":[{"sig":"MEQCIH7yOPn0oaVYrt4p1bh8hrTeL0bOuiMSxBRyUM2xCa7uAiBVSrVADSjMMLGpPw/U+zNbYBwuD/nomXAX9K5c+K80rQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":48435},"main":"./dist/index.cjs","type":"module","types":"dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"gitHead":"1aad6927f3a729e4a67f86abe43b6ae8b317b14b","scripts":{"build":"tsup"},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"repository":{"url":"git+https://github.com/p-shoko/p-api-agent.git","type":"git"},"_npmVersion":"10.8.2","description":"一个轻量、高效、模型无关的 LLM Agent 框架，采用 ReAct 单调用循环架构","directories":{},"_nodeVersion":"20.18.1","dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","tsup":"^8.4.0","lodash":"^4.17.23","ts-node":"^10.9.2","typescript":"5.8.3","@types/node":"^25.3.5","@types/lodash":"4.17.16","javascript-obfuscator":"^4.1.1"},"_npmOperationalInternal":{"tmp":"tmp/p-api-agent_0.0.6_1773564979654_0.680780571884966","host":"s3://npm-registry-packages-npm-production"}},"0.0.7":{"name":"p-api-agent","version":"0.0.7","keywords":["agent","llm","react","function-call","openai","ai"],"author":"","license":"ISC","_id":"p-api-agent@0.0.7","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"homepage":"https://github.com/p-shoko/p-api-agent#readme","bugs":{"url":"https://github.com/p-shoko/p-api-agent/issues"},"dist":{"shasum":"eeb46bf6e3cef65931a7da8729091437f686baf1","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.7.tgz","fileCount":6,"integrity":"sha512-T15zNTjnHcsHn90wUTN4vMq3mOipg+8Yb2RlxQS6EDi5vvONVdVnBd5Yt4hQxL53lMiJWR9vtsX0/iZI9jjvPA==","signatures":[{"sig":"MEUCIQD2TD6MSVRB9M/4X+dOr8Cs9agdMx9GInIjsWAghH6HTQIgKWpqCPk7WihF9oNEqEUXS9livHQXtQBdBW2dn+f3R2A=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":49429},"main":"./dist/index.cjs","type":"module","types":"dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"gitHead":"1aad6927f3a729e4a67f86abe43b6ae8b317b14b","scripts":{"build":"tsup"},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"repository":{"url":"git+https://github.com/p-shoko/p-api-agent.git","type":"git"},"_npmVersion":"10.8.2","description":"一个轻量、高效、模型无关的 LLM Agent 框架，采用 ReAct 单调用循环架构","directories":{},"_nodeVersion":"20.18.1","dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","tsup":"^8.4.0","lodash":"^4.17.23","ts-node":"^10.9.2","typescript":"5.8.3","@types/node":"^25.3.5","@types/lodash":"4.17.16","javascript-obfuscator":"^4.1.1"},"_npmOperationalInternal":{"tmp":"tmp/p-api-agent_0.0.7_1773565343754_0.3685273708473278","host":"s3://npm-registry-packages-npm-production"}},"0.0.8":{"name":"p-api-agent","version":"0.0.8","keywords":["agent","llm","react","function-call","openai","ai"],"author":"","license":"ISC","_id":"p-api-agent@0.0.8","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"homepage":"https://github.com/p-shoko/p-api-agent#readme","bugs":{"url":"https://github.com/p-shoko/p-api-agent/issues"},"dist":{"shasum":"a68e3c75aa22092e07704ec95c085b089a0b380d","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.8.tgz","fileCount":6,"integrity":"sha512-BJvmgfhaPWoPkMjnfkvQDz9HV54Nn1cXMgMMLqdwynBm3T70AcyzTi22X52KElnpK0VZrXSPOiAjpK2tY0Qslw==","signatures":[{"sig":"MEUCIDMAuDB9TAoBLKlQhdYgnDMy+Tx6oPAd1ZpzYz3yclymAiEA/m4kmhug60WNlZLUCzZeWbMbPbbwo6YVJvSyLT/ph9Q=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":42695},"main":"./dist/index.cjs","type":"module","types":"dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"gitHead":"0b71f42864c49f4f9a6e15afbc83053c72a01d37","scripts":{"build":"tsup"},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"repository":{"url":"git+https://github.com/p-shoko/p-api-agent.git","type":"git"},"_npmVersion":"10.8.2","description":"一个轻量、高效、模型无关的 LLM Agent 框架，采用 ReAct 单调用循环架构","directories":{},"_nodeVersion":"20.18.1","dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","tsup":"^8.4.0","lodash":"^4.17.23","ts-node":"^10.9.2","typescript":"5.8.3","@types/node":"^25.3.5","@types/lodash":"4.17.16","javascript-obfuscator":"^4.1.1"},"_npmOperationalInternal":{"tmp":"tmp/p-api-agent_0.0.8_1773566067896_0.43179850716211576","host":"s3://npm-registry-packages-npm-production"}},"0.0.9":{"name":"p-api-agent","version":"0.0.9","main":"./dist/index.cjs","module":"./dist/index.mjs","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"types":"dist/index.d.ts","scripts":{"build":"tsup"},"author":"","license":"ISC","description":"一个轻量、高效、模型无关的 LLM Agent 框架，采用 ReAct 单调用循环架构","repository":{"type":"git","url":"git+https://github.com/p-shoko/p-api-agent.git"},"homepage":"https://github.com/p-shoko/p-api-agent#readme","bugs":{"url":"https://github.com/p-shoko/p-api-agent/issues"},"keywords":["agent","llm","react","function-call","openai","ai"],"devDependencies":{"@types/lodash":"4.17.16","@types/node":"^25.3.5","javascript-obfuscator":"^4.1.1","lodash":"^4.17.23","ts-node":"^10.9.2","tsup":"^8.4.0","tsx":"^4.21.0","typescript":"5.8.3"},"dependencies":{"axios":"^1.13.6","jsonrepair":"^3.13.2"},"type":"module","_id":"p-api-agent@0.0.9","gitHead":"0b71f42864c49f4f9a6e15afbc83053c72a01d37","_nodeVersion":"20.18.1","_npmVersion":"10.8.2","dist":{"integrity":"sha512-A5comfo6P3XH/E86GUW+jX0QEuX79vyJDkc92aw7Ycwgz3qYGBKEVEURDlN5FB7tcdEhK0iIrHhKaWcCA9aPQw==","shasum":"8cc0dfcd62c607435cc1cfbb81327e0e0182108a","tarball":"https://registry.npmjs.org/p-api-agent/-/p-api-agent-0.0.9.tgz","fileCount":6,"unpackedSize":49601,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCID+jobKkGEZ3VbPcwPdkTX6VKA/ydvg2TuNNIcH4d0OHAiAdFSRicXdGKD97ut2Q5k1M78L03+vruu4t8n1k5FHVCA=="}]},"_npmUser":{"name":"peibaba","email":"1431011536@qq.com"},"directories":{},"maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/p-api-agent_0.0.9_1773566313403_0.8411287026941681"},"_hasShrinkwrap":false}},"time":{"created":"2026-03-07T09:20:52.000Z","modified":"2026-03-15T09:18:33.645Z","0.0.1":"2026-03-07T09:20:52.146Z","0.0.2":"2026-03-09T11:58:30.589Z","0.0.3":"2026-03-15T07:21:22.635Z","0.0.4":"2026-03-15T07:30:12.454Z","0.0.5":"2026-03-15T07:40:56.318Z","0.0.6":"2026-03-15T08:56:19.795Z","0.0.7":"2026-03-15T09:02:23.897Z","0.0.8":"2026-03-15T09:14:28.044Z","0.0.9":"2026-03-15T09:18:33.550Z"},"bugs":{"url":"https://github.com/p-shoko/p-api-agent/issues"},"license":"ISC","homepage":"https://github.com/p-shoko/p-api-agent#readme","keywords":["agent","llm","react","function-call","openai","ai"],"repository":{"type":"git","url":"git+https://github.com/p-shoko/p-api-agent.git"},"description":"一个轻量、高效、模型无关的 LLM Agent 框架，采用 ReAct 单调用循环架构","maintainers":[{"name":"peibaba","email":"1431011536@qq.com"}],"readme":"# p-api-agent\n\n一个轻量、高效、模型无关的 LLM Agent 框架。\n\n采用 **ReAct 单调用循环**架构：每轮推理只发起一次 LLM 调用，通过将完整工具 Schema 注入系统提示词，让模型在单次响应中完成「是否用工具 + 选哪个工具 + 填什么参数」三步决策，相比传统多步判断方案减少 60% 以上的 API 调用次数。\n\n## 特性\n\n- ⚡ **ReAct 单调用循环** — 每轮只调用一次 LLM，无冗余中间步骤\n- 🔌 **模型无关** — 只需注册一个 `async (messages) => string` 函数，兼容任意 LLM（OpenAI / Claude / 豆包 / 通义 / DeepSeek …）\n- 🔧 **动态工具注册** — 工具以独立文件方式组织，按目录自动加载\n- 🔗 **链式 API** — 注册方法均返回 `this`，支持链式调用\n- 🛡️ **完善容错** — LLM 重试、工具失败兜底、JSON 解析保护、循环上限防护\n- 📦 **零侵入** — 不绑定任何 LLM SDK，按需引入\n\n---\n\n## 安装\n\n```bash\nnpm install p-api-agent\n# 或\npnpm add p-api-agent\n# 或\nyarn add p-api-agent\n```\n\n---\n\n## 快速开始\n\n```typescript\nimport { Agent, FunctionCall } from 'p-api-agent';\nimport path from 'path';\n\nconst agent = new Agent({ maxLoop: 10 });\n\n// 1. 注册 LLM 能力（替换成你自己的 LLM 调用）\nagent.register_llm_text_ability(async (messages) => {\n  const res = await yourLLMClient.chat(messages);\n  return res.content;\n});\n\n// 2. 注册工具函数目录（可选）\nconst fc = new FunctionCall(path.join(__dirname, 'tools'));\nagent.register_function_call(fc);\n\n// 3. 发起对话\nconst result = await agent.create_chat('帮我查一下订单 12345 的收货地址');\nconsole.log(result.result);     // 最终答案\nconsole.log(result.use_tools);  // 调用过的工具记录\n```\n\n支持链式注册：\n\n```typescript\nconst result = await new Agent({ maxLoop: 10 })\n  .register_llm_text_ability(llmFunc)\n  .register_function_call(fc)\n  .create_chat('你好');\n```\n\n---\n\n## 工作原理\n\n```\n用户输入\n  │\n  ▼\n构建系统提示词（含所有工具完整 Schema）\n  │\n  ▼  ◄────────────────────────────────────────────────────────┐\n单次 LLM 调用                                                  │\n  │                                                            │\n  ├─ { command: \"use_tool\", tool_name, params }                │\n  │       └─ 执行工具 → 结果作为 observation 注入历史 ──────────┘\n  │\n  ├─ { command: \"end\", result }\n  │       └─ 返回最终答案 ✓\n  │\n  └─ { command: \"no_ability\", result }\n          └─ 返回无法处理说明 ✓\n```\n\n每轮推理固定 **1 次** LLM 调用。工具结果以 `[工具调用结果]` 前缀注入对话历史，模型可在下一轮基于结果继续推理、链式调用多个工具，最终输出 `end` 指令结束。\n\n---\n\n## 创建工具函数\n\n在工具目录下为每个工具创建一个独立文件，导出 `register()` 函数：\n\n```typescript\n// tools/get_weather.ts\nimport type { RegisterInfo } from 'p-api-agent';\n\nexport const register = (): RegisterInfo => ({\n  name: 'get_weather',\n  description: '获取指定城市的实时天气信息',\n  input_schema: {\n    type: 'object',\n    properties: {\n      city: {\n        type: 'string',\n        description: '城市名称',\n        examples: ['北京', '上海', '深圳'],\n      },\n      date: {\n        type: 'string',\n        description: '日期，格式 YYYY-MM-DD，不传则默认今天',\n      },\n    },\n    required: ['city'],\n  },\n  register_func: async (params: { city: string; date?: string }) => {\n    // 实现你的业务逻辑\n    return { city: params.city, temperature: 25, weather: '晴' };\n  },\n});\n```\n\n`register_func` 接收 LLM 生成的参数对象，返回值会被序列化后注入对话上下文供模型读取。\n\n---\n\n## API 文档\n\n### `new Agent(options?)`\n\n| 选项 | 类型 | 默认值 | 说明 |\n|---|---|---|---|\n| `maxLoop` | `number` | `20` | 最大推理循环轮次，防止无限循环 |\n| `retryTimes` | `number` | `2` | LLM 调用失败时的重试次数 |\n| `customSystemPrompt` | `string` | — | 完全覆盖内置系统提示词 |\n\n---\n\n### `agent.register_llm_text_ability(func)`\n\n注册 LLM 文字调用函数，返回 `this`。\n\n```typescript\nagent.register_llm_text_ability(\n  async (messages: Message[]) => Promise<string>\n)\n```\n\n`messages` 格式遵循 OpenAI Chat Completions 标准，兼容绝大多数主流 LLM。\n\n---\n\n### `agent.register_function_call(fc)`\n\n注册工具函数集合，返回 `this`。`FunctionCall` 会自动扫描目录下所有文件并调用 `register()` 加载。\n\n```typescript\nconst fc = new FunctionCall('/absolute/path/to/tools');\nagent.register_function_call(fc);\n```\n\n---\n\n### `agent.create_chat(input)`\n\n发起一轮对话，返回 `Promise<CreateChatResult>`。\n\n```typescript\n// 字符串输入\nconst result = await agent.create_chat('查询用户 123 的信息');\n\n// 消息数组输入（携带多轮历史）\nconst result = await agent.create_chat([\n  { role: 'user', content: [{ type: 'text', text: '你好' }] },\n  { role: 'assistant', content: [{ type: 'text', text: '你好！' }] },\n  { role: 'user', content: [{ type: 'text', text: '帮我查一下...' }] },\n]);\n```\n\n**返回值：**\n\n```typescript\ninterface CreateChatResult {\n  result: string       // 最终返回给用户的文本答案\n  use_tools: {         // 本次对话中调用过的工具记录\n    tool_name: string\n    params: any\n    exec_result: any\n  }[]\n}\n```\n\n---\n\n### `agent.set_system_prompt(prompt)`\n\n运行时覆盖系统提示词，返回 `this`。**注意：** 覆盖后工具列表不再自动注入，需在自定义提示词中手动声明。\n\n---\n\n### `agent.set_max_loop(n)`\n\n动态调整最大循环轮次，返回 `this`。\n\n---\n\n### `new FunctionCall(toolPath)`\n\n| 方法 | 说明 |\n|---|---|\n| `get_tools_list()` | 返回工具名称 + 描述列表 |\n| `get_tools_with_schema()` | 返回工具完整 Schema（含参数定义） |\n| `gen_tool_doc(name)` | 生成单个工具的可读说明文档字符串 |\n| `exec_function(name, params)` | 执行指定工具，返回工具执行结果 |\n\n---\n\n## 类型定义\n\n```typescript\nimport type {\n  AgentOptions,\n  UserChatInput,\n  Message,\n  CreateChatResult,\n  ToolRecord,\n  RegisterInfo,\n} from 'p-api-agent';\n```\n\n---\n\n## 完整示例（OpenAI）\n\n```typescript\nimport { Agent, FunctionCall } from 'p-api-agent';\nimport OpenAI from 'openai';\nimport path from 'path';\n\nconst openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\nconst agent = new Agent({ maxLoop: 15, retryTimes: 3 })\n  .register_llm_text_ability(async (messages) => {\n    const res = await openai.chat.completions.create({\n      model: 'gpt-4o',\n      messages: messages as any,\n    });\n    return res.choices[0].message.content ?? '';\n  })\n  .register_function_call(\n    new FunctionCall(path.join(__dirname, 'tools'))\n  );\n\nconst { result, use_tools } = await agent.create_chat(\n  '帮我查询订单 12345 的购买者信息和商品价格'\n);\n\nconsole.log('答案:', result);\nconsole.log('调用了工具:', use_tools.map(t => t.tool_name));\n```\n\n---\n\n## 自定义系统提示词\n\n如需完全控制 Agent 人设，使用 `customSystemPrompt` 选项：\n\n```typescript\nconst agent = new Agent({\n  customSystemPrompt: `\n你是一个专业的电商客服助手，负责处理订单查询和退换货问题。\n\n## 指令格式\n每次只能输出以下 JSON 之一，不能附带多余文字：\n- 调用工具: {\"command\":\"use_tool\",\"tool_name\":\"工具名\",\"params\":{...}}\n- 任务完成: {\"command\":\"end\",\"result\":\"最终答案\"}\n- 无法处理: {\"command\":\"no_ability\",\"result\":\"原因说明\"}\n\n## 可用工具\n- get_order_info: 根据订单号查询订单详情\n- get_user_info: 根据用户 ID 查询用户信息\n  `.trim(),\n});\n```\n\n---\n\n## 开发\n\n```bash\n# 安装依赖\nnpm install\n\n# 构建\nnpm run build\n\n# 运行示例\nnpx tsx examples/test.ts\n```\n\n---\n\n## License\n\nISC\n","readmeFilename":"README.md"}