|
| 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 ANALYST_SYSTEM_PROMPT = `You are a document analyst who excels at extracting insights and summarizing content. |
| 7 | +
|
| 8 | +Your analysis approach: |
| 9 | +- Identify the main themes and key points |
| 10 | +- Extract important facts, figures, and arguments |
| 11 | +- Note relationships between concepts |
| 12 | +- Highlight actionable insights |
| 13 | +- Provide clear, structured summaries |
| 14 | +
|
| 15 | +For document analysis, provide: |
| 16 | +1. Executive summary (2-3 sentences) |
| 17 | +2. Key points and main arguments |
| 18 | +3. Important details and supporting evidence |
| 19 | +4. Connections to broader context |
| 20 | +5. Actionable takeaways or study notes |
| 21 | +
|
| 22 | +Be thorough but concise. Focus on what would be most valuable for learning and retention.`; |
| 23 | + |
| 24 | +/** |
| 25 | + * Analyst Agent - Analyzes documents and extracts key insights |
| 26 | + * |
| 27 | + * Triggers: "analyze this", "summarize", "key points", "what's important" |
| 28 | + * Output: Returns analysis that the orchestrator will present |
| 29 | + */ |
| 30 | +export const createAnalystAgent = (_props: CreateAgentProps) => |
| 31 | + tool({ |
| 32 | + description: |
| 33 | + "Analyze content, extract key insights, and create summaries. Use when the user wants to understand, summarize, or extract key points from text, documents, or concepts. Triggers: analyze, summarize, key points, main ideas, extract insights, break down.", |
| 34 | + inputSchema: z.object({ |
| 35 | + content: z.string().describe("The text or content to analyze"), |
| 36 | + analysisType: z |
| 37 | + .enum(["summary", "key-points", "deep-analysis", "study-notes"]) |
| 38 | + .default("summary") |
| 39 | + .describe("Type of analysis to perform"), |
| 40 | + focusOn: z |
| 41 | + .string() |
| 42 | + .optional() |
| 43 | + .describe("Specific aspect to focus the analysis on"), |
| 44 | + outputLength: z |
| 45 | + .enum(["brief", "moderate", "detailed"]) |
| 46 | + .default("moderate") |
| 47 | + .describe("Desired length of the analysis output"), |
| 48 | + }), |
| 49 | + execute: async ({ |
| 50 | + content, |
| 51 | + analysisType, |
| 52 | + focusOn, |
| 53 | + outputLength, |
| 54 | + }): Promise<AgentResult> => { |
| 55 | + const focusContext = focusOn |
| 56 | + ? `\n\nFocus particularly on: ${focusOn}` |
| 57 | + : ""; |
| 58 | + |
| 59 | + const lengthGuide = { |
| 60 | + brief: "Keep the analysis concise, around 100-200 words.", |
| 61 | + moderate: "Provide a balanced analysis, around 300-500 words.", |
| 62 | + detailed: "Provide a comprehensive analysis, around 600-800 words.", |
| 63 | + }; |
| 64 | + |
| 65 | + const analysisGuide = { |
| 66 | + summary: |
| 67 | + "Create a clear summary highlighting the main message and supporting points.", |
| 68 | + "key-points": |
| 69 | + "Extract and list the most important points as bullet points with brief explanations.", |
| 70 | + "deep-analysis": |
| 71 | + "Provide thorough analysis including themes, arguments, evidence, and implications.", |
| 72 | + "study-notes": |
| 73 | + "Create study-friendly notes with headings, key terms, and memorable takeaways.", |
| 74 | + }; |
| 75 | + |
| 76 | + const prompt = `Analyze the following content: |
| 77 | +
|
| 78 | +--- |
| 79 | +${content} |
| 80 | +--- |
| 81 | +
|
| 82 | +Analysis type: ${analysisType} |
| 83 | +${analysisGuide[analysisType]} |
| 84 | +${focusContext} |
| 85 | +
|
| 86 | +${lengthGuide[outputLength]}`; |
| 87 | + |
| 88 | + const { text } = await generateText({ |
| 89 | + model: myProvider.languageModel("chat-model"), |
| 90 | + system: ANALYST_SYSTEM_PROMPT, |
| 91 | + prompt, |
| 92 | + }); |
| 93 | + |
| 94 | + return { |
| 95 | + agentName: "analyst", |
| 96 | + success: true, |
| 97 | + summary: text, |
| 98 | + data: { |
| 99 | + analysisType, |
| 100 | + focusOn, |
| 101 | + outputLength, |
| 102 | + contentLength: content.length, |
| 103 | + }, |
| 104 | + }; |
| 105 | + }, |
| 106 | + }); |
0 commit comments