pi-mono:TypeScript AI Agent 全栈工具包
目录
为什么需要 pi-mono
AI Agent 开发的主流工具链是 Python:LangChain、LlamaIndex、Dify、CrewAI。但如果你在 TypeScript / Node.js 生态里做产品,想加 AI 能力,选择就少得多。
大多数团队的现状:
- 后端:Go / Node.js / Java
- 前端:React / Flutter
- AI 集成:要么直接调 OpenAI API,要么套一层 Python 微服务
pi-mono 试图解决这个问题。它是一个 TypeScript monorepo,提供从底层 LLM API 到上层 coding agent 的完整工具链。
架构概览
pi-mono/
├── packages/
│ ├── ai/ # 统一 LLM API
│ ├── agent/ # Agent runtime + tool calling
│ ├── coding-agent/ # 交互式 coding agent CLI
│ ├── tui/ # 终端 UI 库
│ ├── web-ui/ # Web chat 组件
│ ├── mom/ # Slack bot
│ └── pods/ # vLLM 部署 CLI核心组件解析
1. @mariozechner/pi-ai — 统一多 Provider LLM API
import { createAI } from "@mariozechner/pi-ai";
// 一个接口,切换不同 provider
const ai = createAI({
provider: "openai", // 或 "anthropic", "google"
model: "gpt-4o",
apiKey: process.env.OPENAI_API_KEY
});
// 统一 API,不管底层用哪家
const response = await ai.complete("Hello, world");支持的主流 Provider:
- OpenAI (GPT-4o, GPT-4o-mini, etc.)
- Anthropic (Claude 3.5 Sonnet, Opus, etc.)
- Google (Gemini 1.5, etc.)
这个的价值:如果你的产品需要在不同模型之间切换(比如成本优化、regional compliance),统一 API 层可以减少 lock-in。
2. @mariozechner/pi-agent-core — Agent runtime
这是最核心的部分。pi-agent-core 实现了:
- Tool calling:给 Agent 提供工具调用能力
- State management:跨 turn 的状态持久化
- Streaming:支持流式输出
import { createAgent } from "@mariozechner/pi-agent-core";
const agent = createAgent({
ai: openAIInstance,
tools: [
{
name: "read_file",
description: "读取文件内容",
parameters: {
path: "string"
},
execute: async ({ path }) => {
return readFileSync(path, "utf-8");
}
}
]
});
// Agent 会自动判断何时调用工具
const result = await agent.run("读取 package.json 看看依赖");3. @mariozechner/pi-coding-agent — 交互式 coding agent CLI
这个包是给开发者用的。装了之后可以直接在 terminal 里跟 AI 对话,让它帮你写代码、跑命令。
# 安装
npm install -g @mariozechner/pi-coding-agent
# 运行
pi-agent
# 在命令行里直接对话
> 帮我把这个 React component 改成支持 dark mode4. @mariozechner/pi-pods — vLLM 部署 CLI
如果你想自己 host 模型,pi-pods 提供了一个 CLI 来管理 GPU pod 上的 vLLM 部署。
# 部署一个 vLLM 实例
pi-pods deploy --model mistralai/Mistral-7B-Instruct-v0.2
# 查看状态
pi-pods list这个适合的场景:你想在本地或自有 GPU 上跑开源模型,用 API 方式调用。
与 LangChain.js 的对比
| LangChain.js | pi-mono | |
|---|---|---|
| 定位 | 通用框架 | 全栈工具链 |
| 包大小 | 较大 | 按需选用 |
| Provider 覆盖 | 广泛 | 主流 (OpenAI, Anthropic, Google) |
| Coding agent | 无 | 有 (pi-coding-agent) |
| TUI/Web UI | 无 | 有 |
| vLLM 支持 | 无 | 有 (pi-pods) |
| 学习曲线 | 陡(概念多) | 平缓(模块独立) |
实际使用场景
场景 1:Node.js 后端加 AI 能力
假设你有个 Express 服务,需要接 AI 分析用户输入:
import { createAI } from "@mariozechner/pi-ai";
import { createAgent } from "@mariozechner/pi-agent-core";
const ai = createAI({ provider: "anthropic", model: "claude-3-5-sonnet" });
const agent = createAgent({
ai,
tools: [yourTools]
});
app.post("/analyze", async (req, res) => {
const result = await agent.run(req.body.userInput);
res.json({ result });
});场景 2:内部 coding agent
团队内部搭一个 coding agent CLI,让开发者用自然语言操作代码库:
# 开发者本地运行
pi-agent
> add unit tests for the payment module
> refactor this function to use async/await
> explain what this regex does局限
- 不是 Dify/CrewAI 那种可视化平台:pi-mono 是代码库,你需要写 TypeScript 来集成
- Provider 支持有限:没有覆盖所有 LLM provider(比如 Cohere、Azure OpenAI)
- 生产验证:相比 LangChain,pi-mono 在生产环境的案例较少
安装
# 全量安装
npm install @mariozechner/pi-ai @mariozechner/pi-agent-core ...
# 按需安装单个包
npm install @mariozechner/pi-ai开发环境依赖 Node.js 20+。
总结
pi-mono 的价值主张:TypeScript 原生 + 全栈覆盖 + 模块可拆分。
如果你在 TypeScript 生态里,想快速给产品加 AI 能力,不需要 Python 微服务,pi-mono 值得看看。它的模块设计让你可以只用 pi-ai 做 API 统一,也可以全量使用搭一个完整的 coding agent。