git clone https://cnb.cool/codebuddy/agent-sdk-demos.gitcd agent-sdk-demos
示例 | 语言 | 核心功能 | 适用场景 |
quick-start | TypeScript | 基础 API、消息流、Hooks | SDK 入门 |
multi-turn-session | TypeScript | 多轮对话、会话恢复 | 交互式应用 |
research-assistant | Python | 多 Agent 协作 | 复杂任务分解 |
profile-builder | TypeScript | 网络搜索、文档生成 | 信息收集 |
chat-demo | TypeScript | WebSocket、流式响应 | Web 应用 |
mail-assistant | TypeScript | MCP 协议、自定义工具 | 业务系统对接 |
spreadsheet-assistant | TypeScript | Electron IPC | 桌面应用 |
npm install @tencent-ai/agent-sdk
pip install codebuddy-agent-sdk
codebuddy 命令登录,SDK 自动使用现有凭据query() API 的基本用法,包括消息流处理和 Hooks 机制。import { query } from '@tencent-ai/agent-sdk';const conversation = query({prompt: '你好!请介绍一下你能做什么。',options: {model: 'claude-4.5',maxTurns: 100,allowedTools: ['Read', 'Write', 'Bash', 'Glob', 'Grep'],},});for await (const message of conversation) {if (message.type === 'assistant') {const text = message.message.content.find(c => c.type === 'text');if (text) console.log(text.text);}if (message.type === 'result') {console.log(`完成,耗时 ${message.duration_ms}ms`);}}
cd quick-startnpm installnpx tsx quick-start.ts
import { unstable_v2_createSession, unstable_v2_resumeSession } from '@tencent-ai/agent-sdk';// 创建会话await using session = unstable_v2_createSession({ model: 'claude-4.5' });// 第一轮await session.send('今年是哪一年?');for await (const msg of session.stream()) { /* ... */ }// 第二轮(保持上下文)await session.send('再往后推 10 年是哪一年?');for await (const msg of session.stream()) { /* ... */ }
cd multi-turn-sessionnpm installnpx tsx examples.ts basic # 基础会话npx tsx examples.ts multi-turn # 多轮对话npx tsx examples.ts resume # 会话恢复
files/research_notes/files/charts/files/reports/from codebuddy_agent_sdk import CodeBuddySDKClient, CodeBuddyAgentOptions, AgentDefinitionagents = {"researcher": AgentDefinition(description="使用网络搜索收集研究信息",tools=["WebSearch", "Write"],model="claude-haiku-4.5"),"data-analyst": AgentDefinition(description="从研究笔记提取数据并生成图表",tools=["Glob", "Read", "Bash", "Write"],model="claude-haiku-4.5"),"report-writer": AgentDefinition(description="整合研究和数据生成 PDF 报告",tools=["Skill", "Glob", "Read", "Write", "Bash"],model="claude-haiku-4.5")}options = CodeBuddyAgentOptions(allowed_tools=["Task"], # 主 Agent 只能委托任务agents=agents,model="claude-haiku-4.5")async with CodeBuddySDKClient(options=options) as client:await client.query("研究 2025 年量子计算发展")async for msg in client.receive_response():# 处理消息
cd research-assistantuv syncuv run python research_agent/agent.py
const q = query({prompt: `搜索 "${personName}" 的资料,创建一份专业简历`,options: {allowedTools: ['WebSearch', 'WebFetch', 'Bash', 'Write', 'Read'],systemPrompt: '你是简历撰写专家...',},});
cd profile-buildernpm installnpm start "姓名"# 输出:agent/custom_scripts/resume.docx
Browser (React) ←─ WebSocket ─→ Express Server ←─ SDK query()
import { query } from "@tencent-ai/agent-sdk";export class Agent {async sendMessage(content: string) {this.stream = query({prompt: content,options: {maxTurns: 1,allowedTools: ['Bash', 'Read', 'Write', 'WebSearch'],},})[Symbol.asyncIterator]();}async *getOutputStream() {while (true) {const { value, done } = await this.stream.next();if (done) break;yield value;}}}
cd chat-demonpm installnpm run dev# 后端:http://localhost:3001# 前端:http://localhost:5173
const q = query({prompt: '查找本周未读的重要邮件',options: {allowedTools: ['Read', 'Write', 'Bash','mcp__email__search_inbox', // MCP 工具'mcp__email__read_emails'],mcpServers: {"email": customEmailServer},},});
cd mail-assistantcp .env.example .env # 配置 IMAP 凭据bun installbun run dev# 访问 http://localhost:3000
import { query } from '@tencent-ai/agent-sdk';ipcMain.on('agent:query', async (event, data) => {for await (const message of query({ prompt: data.content, options })) {event.reply('agent:response', message);}});
window.electron.ipcRenderer.on('agent:response', (message) => {// 更新 UI});window.electron.ipcRenderer.sendMessage('agent:query', { content: '创建销售报表' });
cd spreadsheet-assistantnpm installnpm start
const q = query({prompt: '...',options: {hooks: {PreToolUse: [{matcher: 'Write|Edit',hooks: [async (input) => {const filePath = input.tool_input.file_path;// 限制写入目录if (!filePath.startsWith('/allowed/path/')) {return { decision: 'block', stopReason: '路径不允许' };}return { continue: true };}]}]}}});
文档反馈