-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathagentHandoff.ts
More file actions
160 lines (137 loc) · 5.4 KB
/
agentHandoff.ts
File metadata and controls
160 lines (137 loc) · 5.4 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
import { Agent, handoff, run } from "@openai/agents";
import { logger, task } from "@trigger.dev/sdk";
import { z } from "zod";
// Example payload for testing:
// {
// "userQuestion": "What is the square root of 144?"
// }
export interface AgentHandoffPayload {
userQuestion: string;
}
// Define handoff schemas to enable logging
const MathInput = z.object({
problem: z.string(),
});
const HistoryInput = z.object({
topic: z.string(),
});
const ScienceInput = z.object({
subject: z.string(),
});
export const agentHandoff = task({
id: "agent-handoff",
maxDuration: 120,
run: async (payload: AgentHandoffPayload) => {
logger.info("🎬 Processing user question", {
question: payload.userQuestion,
});
// Create specialist agents
const mathAgent = new Agent({
name: "Math Specialist",
instructions:
`You are a mathematics expert with strict limitations. You can ONLY:
1. Solve mathematical calculations and equations
2. Explain mathematical concepts
3. Work with numbers and mathematical proofs
You MUST refuse to answer anything that isn't purely mathematical.
If a question involves history (even math history) or science, inform the user that you can only handle mathematical calculations and concepts.`,
model: "gpt-4o-mini",
});
logger.info("📐 Math agent ready");
const historyAgent = new Agent({
name: "History Specialist",
instructions:
`You are a history expert with strict limitations. You can ONLY:
1. Provide historical dates and timelines
2. Explain historical events and their context
3. Discuss historical figures and periods
You MUST refuse to answer anything that isn't purely historical.
If a question involves calculations or scientific concepts, inform the user that you can only handle historical information.`,
model: "gpt-4o-mini",
});
logger.info("📚 History agent ready");
const scienceAgent = new Agent({
name: "Science Specialist",
instructions:
`You are a science expert with strict limitations. You can ONLY:
1. Explain scientific concepts and principles
2. Discuss physics, chemistry, and biology topics
3. Describe scientific processes
You MUST refuse to answer anything that isn't purely scientific.
If a question involves historical dates or mathematical calculations, inform the user that you can only handle scientific concepts.`,
model: "gpt-4o-mini",
});
logger.info("🔬 Science agent ready");
// Create handoff configurations with real-time logging
let handoffReason = "";
let handoffSpecialist = "";
const mathHandoff = handoff(mathAgent, {
inputType: MathInput,
toolDescriptionOverride:
"Transfer ONLY mathematical calculations and concepts - no history or science allowed",
onHandoff: (ctx, input) => {
handoffSpecialist = "Math Specialist";
handoffReason = "Mathematical concepts or calculations required";
logger.info("📐 Math question handed off to Math Specialist", {
problem: input?.problem ?? "unknown",
reason: handoffReason,
});
},
});
const historyHandoff = handoff(historyAgent, {
inputType: HistoryInput,
toolDescriptionOverride:
"Transfer ONLY historical information and dates - no calculations or science allowed",
onHandoff: (ctx, input) => {
handoffSpecialist = "History Specialist";
handoffReason = "Historical information required";
logger.info("📚 History question handed off to History Specialist", {
topic: input?.topic ?? "unknown",
reason: handoffReason,
});
},
});
const scienceHandoff = handoff(scienceAgent, {
inputType: ScienceInput,
toolDescriptionOverride:
"Transfer ONLY scientific concepts - no historical dates or calculations allowed",
onHandoff: (ctx, input) => {
handoffSpecialist = "Science Specialist";
handoffReason = "Scientific concepts required";
logger.info("🔬 Science question handed off to Science Specialist", {
subject: input?.subject ?? "unknown",
reason: handoffReason,
});
},
});
// Create triage agent with handoffs to specialists using Agent.create()
const triageAgent = Agent.create({
name: "Triage Agent",
instructions:
`You are a triage agent that routes questions to the right specialist. Keep it simple:
Math Specialist: For calculations and math concepts
History Specialist: For dates and historical events
Science Specialist: For scientific concepts
Just give the direct answer to the question. No explanations about why you chose the specialist.`,
model: "gpt-4o-mini",
handoffs: [mathHandoff, historyHandoff, scienceHandoff],
});
logger.info("🎯 Triaging question through triage agent...", {
question: payload.userQuestion,
});
// Run the triage agent - it will decide whether to handle the question itself or hand off
const result = await run(triageAgent, payload.userQuestion);
logger.info("✨ Response generated", {
question: payload.userQuestion,
answer: result.finalOutput,
specialist: handoffSpecialist,
reason: handoffReason,
});
return {
question: payload.userQuestion,
answer: result.finalOutput,
specialist: handoffSpecialist || "No specialist used",
reason: handoffReason || "No specialist was used",
};
},
});