|
| 1 | +import { generateText, tool } from "ai"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { myProvider } from "../providers"; |
| 4 | +import type { AgentResult, CreateAgentProps } from "./types"; |
| 5 | + |
| 6 | +const TUTOR_SYSTEM_PROMPT = `You are a patient, encouraging tutor who excels at explaining complex topics. |
| 7 | +
|
| 8 | +Your teaching approach: |
| 9 | +- Start with what the student likely already knows |
| 10 | +- Use relatable analogies and real-world examples |
| 11 | +- Break complex ideas into digestible steps |
| 12 | +- Include brief knowledge checks when appropriate |
| 13 | +- Encourage curiosity and questions |
| 14 | +- Adapt explanation depth based on the topic complexity |
| 15 | +
|
| 16 | +Structure your explanations with: |
| 17 | +1. A simple overview (1-2 sentences) |
| 18 | +2. The main explanation with examples |
| 19 | +3. Key takeaways or summary points |
| 20 | +
|
| 21 | +Keep responses focused and educational. Avoid unnecessary fluff.`; |
| 22 | + |
| 23 | +/** |
| 24 | + * Tutor Agent - Explains concepts with examples and analogies |
| 25 | + * |
| 26 | + * Triggers: "explain", "teach me", "how does X work", "what is" |
| 27 | + * Output: Returns explanation text that the orchestrator will present |
| 28 | + * |
| 29 | + * Note: We use generateText instead of streaming because tool results |
| 30 | + * are displayed in the chat UI, not the artifact panel. The orchestrator |
| 31 | + * (main chat model) can then present the explanation conversationally. |
| 32 | + */ |
| 33 | +export const createTutorAgent = (_props: CreateAgentProps) => |
| 34 | + tool({ |
| 35 | + description: |
| 36 | + "Explain a concept, topic, or idea in detail with examples and analogies. Use when the user asks to understand, learn about, or needs explanation of something. Triggers: explain, teach me, how does X work, what is X.", |
| 37 | + inputSchema: z.object({ |
| 38 | + topic: z.string().describe("The topic or concept to explain"), |
| 39 | + depth: z |
| 40 | + .enum(["beginner", "intermediate", "advanced"]) |
| 41 | + .default("intermediate") |
| 42 | + .describe("The depth of explanation needed based on user context"), |
| 43 | + context: z |
| 44 | + .string() |
| 45 | + .optional() |
| 46 | + .describe( |
| 47 | + "Additional context about what the user already knows or specific aspects to focus on" |
| 48 | + ), |
| 49 | + }), |
| 50 | + execute: async ({ topic, depth, context }): Promise<AgentResult> => { |
| 51 | + const prompt = `Explain "${topic}" at a ${depth} level.${ |
| 52 | + context ? `\n\nAdditional context: ${context}` : "" |
| 53 | + }`; |
| 54 | + |
| 55 | + const { text } = await generateText({ |
| 56 | + model: myProvider.languageModel("chat-model"), |
| 57 | + system: TUTOR_SYSTEM_PROMPT, |
| 58 | + prompt, |
| 59 | + }); |
| 60 | + |
| 61 | + return { |
| 62 | + agentName: "tutor", |
| 63 | + success: true, |
| 64 | + summary: text, |
| 65 | + data: { topic, depth, contentLength: text.length }, |
| 66 | + }; |
| 67 | + }, |
| 68 | + }); |
0 commit comments