/**
* 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 { ChatAnthropic } from "@langchain/anthropic";
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`;
},
{
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().default(""),
}),
}
);
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}`;
},
{
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().default([]),
}),
}
);
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(),
}),
}
);
// ============================================================================
// Step 2: Create specialized sub-agents
// ============================================================================
const llm = new ChatAnthropic({
model: "claude-haiku-4-5-20251001",
});
const calendarAgent = createAgent({
model: llm,
tools: [createCalendarEvent, getAvailableTimeSlots],
systemPrompt: `
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(),
});
const emailAgent = createAgent({
model: llm,
tools: [sendEmail],
systemPrompt: `
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(),
});
// ============================================================================
// Step 3: Wrap sub-agents as tools for the supervisor
// ============================================================================
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"),
}),
}
);
// ============================================================================
// Step 4: Create the supervisor agent
// ============================================================================
const supervisorAgent = createAgent({
model: llm,
tools: [scheduleEvent, manageEmail],
systemPrompt: `
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 5: Use the supervisor
// ============================================================================
// Example: User request requiring both calendar and email coordination
const userRequest =
"Schedule a meeting with the design team next Tuesday at 2pm for 1 hour, " +
"and send them an email reminder about reviewing the new mockups.";
console.log("User Request:", userRequest);
console.log(`\n${"=".repeat(80)}\n`);
const stream = await supervisorAgent.stream({
messages: [{ role: "user", content: userRequest }],
});
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());
}
}
}
}