-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple-agent-style.ts
More file actions
307 lines (265 loc) · 9.63 KB
/
Copy pathsimple-agent-style.ts
File metadata and controls
307 lines (265 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env tsx
import type { TaskInput } from "../src/workflow/Task";
import { type DAGTask, WorkflowBuilder } from "../src/workflow/WorkflowBuilder";
/**
* 🤖 简化Agent风格API - 类似OpenAI Agent SDK
*
* 本示例展示:
* 1. 简洁的Agent定义方式
* 2. 类似OpenAI Agent SDK的Runner.run_sync用法
* 3. Agent间的协作和handoff机制
* 4. 工具函数的装饰器风格定义
*/
// 🛠️ 工具函数定义(类似@function_tool装饰器)
function functionTool<T extends (...args: any[]) => any>(fn: T): T {
(fn as any).isTool = true;
return fn;
}
// 定义工具函数
const submitRefundRequest = functionTool((itemId: string, reason: string) => {
console.log(`📝 处理退款请求: 商品ID=${itemId}, 原因=${reason}`);
return { status: "success", refundId: `RF-${Date.now()}` };
});
const webSearch = functionTool((query: string) => {
console.log(`🔍 网络搜索: ${query}`);
// 模拟搜索结果
return {
results: [
{
title: "最佳运动鞋推荐 2024",
url: "example.com/shoes",
snippet: "专业运动鞋评测...",
},
{
title: "时尚搭配指南",
url: "example.com/style",
snippet: "服装搭配技巧...",
},
],
};
});
const analyzeOutfit = functionTool((description: string) => {
console.log(`👗 分析服装搭配: ${description}`);
return {
style: "casual-smart",
colors: ["navy", "white", "brown"],
suggestions: ["添加一双棕色皮鞋会很好搭配"],
};
});
// 🤖 Agent类定义
class Agent {
constructor(
public name: string,
public instructions: string,
public tools: Function[] = [],
public handoffs: Agent[] = [],
) {}
// 转换为WorkflowBuilder任务
toTask(): DAGTask {
const agent = this;
return {
name: this.name.replace(/\s+/g, ""),
dependsOn: [],
async execute(input: TaskInput): Promise<Record<string, any>> {
console.log(`\n🤖 Agent [${agent.name}] 开始处理请求`);
console.log(`📋 指令: ${agent.instructions}`);
const userQuery = input.userQuery || input.query || "";
console.log(`💭 用户查询: ${userQuery}`);
// 模拟Agent思考过程
await new Promise((resolve) => setTimeout(resolve, 300));
// 决定使用哪个工具或转交给哪个Agent
let result: any = { agentName: agent.name };
if (agent.name === "Triage Agent") {
// 分流逻辑
if (
userQuery.toLowerCase().includes("shoes") ||
userQuery.toLowerCase().includes("outfit")
) {
result.handoff = "Shopping Assistant";
result.reason = "用户询问关于服装搭配,转交给购物助手";
} else if (
userQuery.toLowerCase().includes("refund") ||
userQuery.toLowerCase().includes("return")
) {
result.handoff = "Support & Returns";
result.reason = "用户询问退款事宜,转交给客服";
} else {
result.handoff = "Shopping Assistant";
result.reason = "默认转交给购物助手";
}
} else if (agent.name === "Shopping Assistant") {
// 购物助手逻辑
const outfitAnalysis = analyzeOutfit(userQuery);
const searchResults = webSearch("鞋子推荐 " + outfitAnalysis.style);
result = {
...result,
outfitAnalysis,
searchResults,
recommendation: "基于您的搭配,建议选择棕色或深蓝色的休闲皮鞋",
suggestedProducts: [
{ name: "Clarks沙漠靴", price: "$120", match: "95%" },
{ name: "Cole Haan休闲鞋", price: "$150", match: "90%" },
],
};
} else if (agent.name === "Support & Returns") {
// 客服逻辑
if (userQuery.toLowerCase().includes("refund")) {
const refundResult = submitRefundRequest(
"ITEM-123",
"不满意商品质量",
);
result = {
...result,
action: "refund_processed",
refundDetails: refundResult,
message: "退款请求已提交,预计3-5个工作日处理完成",
};
} else {
result.message = "请提供更多详细信息以便我们为您提供帮助";
}
}
console.log(`✅ Agent [${agent.name}] 处理完成`);
return { ...input, [agent.name.replace(/\s+/g, "")]: result };
},
};
}
}
// 🏃♂️ Runner类 - 简化的执行器
class Runner {
static async runSync(options: {
startingAgent: Agent;
input: string;
maxHops?: number;
}): Promise<any> {
const { startingAgent, input, maxHops = 5 } = options;
console.log("🚀 Runner 开始执行工作流");
console.log(`🎯 起始Agent: ${startingAgent.name}`);
console.log(`📝 用户输入: ${input}`);
// 收集所有相关Agent
const allAgents = new Set<Agent>();
const collectAgents = (agent: Agent) => {
allAgents.add(agent);
agent.handoffs.forEach(collectAgents);
};
collectAgents(startingAgent);
// 构建工作流
const workflow = WorkflowBuilder.create();
// 添加所有Agent作为任务
Array.from(allAgents).forEach((agent) => {
workflow.addTask(agent.toTask());
});
// 添加智能路由策略
workflow.addDynamicStrategy({
name: "agent_handoff_routing",
condition: (context) => {
// 检查是否有Agent建议转交
const triageResult = context.get("TriageAgent") as any;
return triageResult?.handoff;
},
generator: async (context) => {
const triageResult = context.get("TriageAgent") as any;
const targetAgentName = triageResult?.handoff;
console.log(`🔄 Agent转交: ${triageResult?.reason}`);
console.log(`➡️ 转交给: ${targetAgentName}`);
// 返回空数组,因为目标Agent已经在静态任务中
return [];
},
priority: 10,
});
// 执行工作流
const result = await workflow.build().execute({
userQuery: input,
query: input,
});
if (result.success) {
console.log("\n🎉 工作流执行成功!");
console.log("📊 执行统计:");
console.log(` - 总执行时间: ${result.executionTime}ms`);
console.log(` - 任务数量: ${result.taskResults.size}`);
console.log(` - 动态任务: ${result.dynamicTasksGenerated || 0}`);
// 提取最终结果
const triageResult = result.data?.TriageAgent;
const targetAgent = triageResult?.handoff?.replace(/\s+/g, "");
if (targetAgent && result.data?.[targetAgent]) {
console.log(`\n📋 最终结果来自 [${triageResult.handoff}]:`);
return result.data[targetAgent];
} else {
return result.data;
}
} else {
console.error("❌ 工作流执行失败:", result.error?.message);
throw result.error;
}
}
}
// 🎯 Agent定义 - 完全模仿OpenAI Agent SDK的风格
const supportAgent = new Agent(
"Support & Returns",
"You are a support agent who can submit refunds and handle customer service issues.",
[submitRefundRequest],
);
const shoppingAgent = new Agent(
"Shopping Assistant",
"You are a shopping assistant who can search the web for products and analyze outfit compatibility.",
[webSearch, analyzeOutfit],
);
const triageAgent = new Agent(
"Triage Agent",
"Route the user to the correct agent based on their query. Analyze the intent and decide which specialist can best help.",
[],
[shoppingAgent, supportAgent], // handoffs
);
// 🚀 主函数 - 展示简化的使用方式
async function runSimpleAgentExample() {
console.log("🤖 简化Agent风格API示例\n");
try {
// 测试1: 服装搭配查询
console.log("=".repeat(60));
console.log("🧪 测试1: 服装搭配查询");
const output1 = await Runner.runSync({
startingAgent: triageAgent,
input:
"What shoes might work best with my navy blazer and white shirt outfit?",
});
console.log("\n📋 最终结果:");
console.log(JSON.stringify(output1, null, 2));
// 测试2: 退款申请
console.log("\n" + "=".repeat(60));
console.log("🧪 测试2: 退款申请");
const output2 = await Runner.runSync({
startingAgent: triageAgent,
input: "I want to request a refund for my recent purchase",
});
console.log("\n📋 最终结果:");
console.log(JSON.stringify(output2, null, 2));
// 展示API对比
console.log("\n" + "=".repeat(60));
console.log("📊 API对比展示:");
console.log(`
// OpenAI Agent SDK 风格:
output = Runner.run_sync(
starting_agent=triage_agent,
input="What shoes might work best with my outfit?"
)
// 我们的实现 (几乎完全一致):
const output = await Runner.runSync({
startingAgent: triageAgent,
input: "What shoes might work best with my outfit?"
});
🎯 核心优势:
✅ API简洁性: 与OpenAI Agent SDK几乎一致
✅ 更强大: 支持复杂的工作流和动态策略
✅ 类型安全: 完整的TypeScript支持
✅ 灵活性: 可以扩展为复杂的多步骤工作流
✅ 性能: 自动并行执行和优化
`);
} catch (error) {
console.error("💥 执行异常:", error);
}
}
// 🚀 运行示例
if (import.meta.url === `file://${process.argv[1]}`) {
console.log("🎯 本示例演示如何用简洁的API创建类似OpenAI Agent SDK的功能\n");
runSimpleAgentExample().catch(console.error);
}
export { Agent, Runner, functionTool, runSimpleAgentExample };