/** * Personal Assistant Supervisor Example * * This example demonstrates the tool calling pattern for multi-agent systems. * A supervisor agent coordinates specialized sub-agents (calendar and email) * that are wrapped as tools. */ import { tool, createAgent } from "langchain"; import { ChatOpenAI } from "@langchain/openai"; import { z } from "zod"; // ============================================================================ // Step 1: Define low-level API tools (stubbed) // ============================================================================ const createCalendarEvent = tool( async ({ title, startTime, endTime, attendees, location }) => { // Stub: In practice, this would call Google Calendar API, Outlook API, etc. return `Event created: ${title} from ${startTime} to ${endTime} with ${attendees.length} attendees in ${location}`; }, { name: "create_calendar_event", description: "Create a calendar event. Requires exact ISO datetime format.", schema: z.object({ title: z.string(), startTime: z.string().describe("ISO format: '2024-01-15T14:00:00'"), endTime: z.string().describe("ISO format: '2024-01-15T15:00:00'"), attendees: z.array(z.string()).describe("email addresses"), location: z.string().optional(), }), } ); const sendEmail = tool( async ({ to, subject, body, cc }) => { // Stub: In practice, this would call SendGrid, Gmail API, etc. return `Email sent to ${to.join(", ")} - Subject: ${subject} - Body: ${body} - CC: ${cc?.join(", ") ?? ""}`; }, { name: "send_email", description: "Send an email via email API. Requires properly formatted addresses.", schema: z.object({ to: z.array(z.string()).describe("email addresses"), subject: z.string(), body: z.string(), cc: z.array(z.string()).optional(), }), } ); const getAvailableTimeSlots = tool( async (/*{ attendees, date, durationMinutes }*/) => { // Stub: In practice, this would query calendar APIs return ["09:00", "14:00", "16:00"]; }, { name: "get_available_time_slots", description: "Check calendar availability for given attendees on a specific date.", schema: z.object({ attendees: z.array(z.string()), date: z.string().describe("ISO format: '2024-01-15'"), durationMinutes: z.number(), }), } ); const CALENDAR_AGENT_PROMPT = ` You are a calendar scheduling assistant. Parse natural language scheduling requests (e.g., 'next Tuesday at 2pm') into proper ISO datetime formats. Use get_available_time_slots to check availability when needed. Use create_calendar_event to schedule events. Always confirm what was scheduled in your final response. `.trim(); // ============================================================================ // Step 2: Create specialized sub-agents // ============================================================================ const model = new ChatOpenAI({ model: "gpt-4o-mini", temperature: 0, }); const calendarAgent = createAgent({ model, tools: [createCalendarEvent, getAvailableTimeSlots], systemPrompt: CALENDAR_AGENT_PROMPT, }); const query = "Schedule a team meeting next Tuesday at 2pm for 1 hour"; const stream = await calendarAgent.stream({ messages: [{ role: "user", content: query }], }); for await (const step of stream) { for (const update of Object.values(step)) { if (update && typeof update === "object" && "messages" in update) { for (const message of update.messages) { console.log(message.toFormattedString()); } } } } const EMAIL_AGENT_PROMPT = ` You are an email assistant. Compose professional emails based on natural language requests. Extract recipient information and craft appropriate subject lines and body text. Use send_email to send the message. Always confirm what was sent in your final response. `.trim(); const emailAgent = createAgent({ model, tools: [sendEmail], systemPrompt: EMAIL_AGENT_PROMPT, }); const emailStream = await emailAgent.stream({ messages: [ { role: "user", content: "Send the design team a reminder about reviewing the new mockups", }, ], }); for await (const step of emailStream) { for (const update of Object.values(step)) { if (update && typeof update === "object" && "messages" in update) { for (const message of update.messages) { console.log(message.toFormattedString()); } } } } // ============================================================================ // Step 3: Wrap sub-agents as tools for the supervisor agent // ============================================================================ const scheduleEvent = tool( async ({ request }) => { const result = await calendarAgent.invoke({ messages: [{ role: "user", content: request }], }); const lastMessage = result.messages[result.messages.length - 1]; return lastMessage.text; }, { name: "schedule_event", description: ` Schedule calendar events using natural language. Use this when the user wants to create, modify, or check calendar appointments. Handles date/time parsing, availability checking, and event creation. Input: Natural language scheduling request (e.g., 'meeting with design team next Tuesday at 2pm') `.trim(), schema: z.object({ request: z.string().describe("Natural language scheduling request"), }), } ); const manageEmail = tool( async ({ request }) => { const result = await emailAgent.invoke({ messages: [{ role: "user", content: request }], }); const lastMessage = result.messages[result.messages.length - 1]; return lastMessage.text; }, { name: "manage_email", description: ` Send emails using natural language. Use this when the user wants to send notifications, reminders, or any email communication. Handles recipient extraction, subject generation, and email composition. Input: Natural language email request (e.g., 'send them a reminder about the meeting') `.trim(), schema: z.object({ request: z.string().describe("Natural language email request"), }), } ); const SUPERVISOR_PROMPT = ` You are a helpful personal assistant. You can schedule calendar events and send emails. Break down user requests into appropriate tool calls and coordinate the results. When a request involves multiple actions, use multiple tools in sequence. `.trim(); // ============================================================================ // Step 4: Create the supervisor agent // ============================================================================ const supervisorAgent = createAgent({ model, tools: [scheduleEvent, manageEmail], systemPrompt: SUPERVISOR_PROMPT, }); // ============================================================================ // Step 5: Use the supervisor // ============================================================================ const supervisorStream = await supervisorAgent.stream({ messages: [ { role: "user", content: "Schedule a team standup for tomorrow at 9am" }, ], }); for await (const step of supervisorStream) { for (const update of Object.values(step)) { if (update && typeof update === "object" && "messages" in update) { for (const message of update.messages) { console.log(message.toFormattedString()); } } } }