-
Notifications
You must be signed in to change notification settings - Fork 90
Add advanced agent orchestration examples (#313) #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seanaguinaga
wants to merge
1
commit into
get-convex:main
Choose a base branch
from
seanaguinaga:feature/313-advanced-agent-orchestration-examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /// <reference types="vite/client" /> | ||
| import { expect, test } from "vitest"; | ||
| import { api } from "../_generated/api.js"; | ||
| import { initConvexTest } from "../setup.test.js"; | ||
|
|
||
| test("orchestration preserves the planned agent handoff", async () => { | ||
| const result = await initConvexTest().action( | ||
| api.workflows.coordination.orchestrate, | ||
| { prompt: "Plan a small release" }, | ||
| ); | ||
|
|
||
| expect(result.steps.map(({ agent }) => agent)).toEqual([ | ||
| "coordinator", | ||
| "analyst", | ||
| "critic", | ||
| "coordinator", | ||
| ]); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,311 @@ | ||
| // See the docs at https://docs.convex.dev/agents/workflows | ||
| import { Agent, createTool, stepCountIs } from "@convex-dev/agent"; | ||
| import { | ||
| defineEvent, | ||
| type WorkflowId, | ||
| WorkflowManager, | ||
| vWorkflowId, | ||
| } from "@convex-dev/workflow"; | ||
| import { type Infer, v } from "convex/values"; | ||
| import { z } from "zod/v3"; | ||
| import { components, internal } from "../_generated/api.js"; | ||
| import { action, mutation, query } from "../_generated/server.js"; | ||
| import { defaultConfig } from "../agents/config.js"; | ||
|
|
||
| const resultValidator = v.object({ | ||
| steps: v.array(v.object({ agent: v.string(), output: v.string() })), | ||
| }); | ||
|
|
||
| type ExampleResult = Infer<typeof resultValidator>; | ||
|
|
||
| const standaloneScope = { userId: "advanced-workflow-example" }; | ||
|
|
||
| const analystAgent = new Agent(components.agent, { | ||
| name: "Analyst", | ||
| instructions: | ||
| "Analyze the request using concrete facts, constraints, and tradeoffs. Keep responses under 120 words.", | ||
| ...defaultConfig, | ||
| }); | ||
|
|
||
| const creativeAgent = new Agent(components.agent, { | ||
| name: "Creative", | ||
| instructions: | ||
| "Generate practical, original options for the request. Keep responses under 120 words.", | ||
| ...defaultConfig, | ||
| }); | ||
|
|
||
| const criticAgent = new Agent(components.agent, { | ||
| name: "Critic", | ||
| instructions: | ||
| "Find risks, weak assumptions, and useful improvements. Keep responses under 120 words.", | ||
| ...defaultConfig, | ||
| }); | ||
|
|
||
| const coordinatorAgent = new Agent(components.agent, { | ||
| name: "Coordinator", | ||
| instructions: | ||
| "Coordinate specialists and produce concise, actionable answers. Keep responses under 160 words.", | ||
| ...defaultConfig, | ||
| }); | ||
|
|
||
| const reactAgent = new Agent(components.agent, { | ||
| name: "ReAct Agent", | ||
| instructions: | ||
| "Reason about the request, call both available tools, then give a concise recommendation based on their results.", | ||
| tools: { | ||
| lookupProjectFacts: createTool({ | ||
| description: "Look up fixed facts about the example software project", | ||
| inputSchema: z.object({ | ||
| focus: z.string().describe("The project area to inspect"), | ||
| }), | ||
| execute: async (_ctx, { focus }) => ({ | ||
| focus, | ||
| teamSize: 3, | ||
| releaseWindowDays: 10, | ||
| constraints: ["No new service", "Keep the first release small"], | ||
| }), | ||
| }), | ||
| estimateEffort: createTool({ | ||
| description: | ||
| "Estimate implementation days from task count and complexity", | ||
| inputSchema: z.object({ | ||
| tasks: z.number().int().positive(), | ||
| complexity: z.enum(["low", "medium", "high"]), | ||
| }), | ||
| execute: async (_ctx, { tasks, complexity }) => ({ | ||
| days: Math.ceil(tasks * { low: 0.5, medium: 1, high: 2 }[complexity]), | ||
| }), | ||
| }), | ||
| }, | ||
| stopWhen: stepCountIs(5), | ||
| ...defaultConfig, | ||
| }); | ||
|
|
||
| const specialists = { | ||
| analyst: analystAgent, | ||
| creative: creativeAgent, | ||
| critic: criticAgent, | ||
| }; | ||
|
|
||
| /** Route a request with one LLM call, then invoke only the selected agent. */ | ||
| export const dynamicRouting = action({ | ||
| args: { prompt: v.string() }, | ||
| returns: resultValidator, | ||
| handler: async (ctx, { prompt }): Promise<ExampleResult> => { | ||
| const { | ||
| object: { route }, | ||
| } = await coordinatorAgent.generateObject(ctx, standaloneScope, { | ||
| prompt: `Choose the best specialist for this request: ${prompt}`, | ||
| schema: z.object({ | ||
| route: z.enum(["analyst", "creative", "critic"]), | ||
| }), | ||
| }); | ||
| const response = await specialists[route].generateText( | ||
| ctx, | ||
| standaloneScope, | ||
| { prompt }, | ||
| ); | ||
| return { | ||
| steps: [ | ||
| { agent: "Router", output: `Selected ${route}` }, | ||
| { agent: route, output: response.text }, | ||
| ], | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| /** Run independent specialists in parallel, then synthesize their reports. */ | ||
| export const fanOut = action({ | ||
| args: { prompt: v.string() }, | ||
| returns: resultValidator, | ||
| handler: async (ctx, { prompt }): Promise<ExampleResult> => { | ||
| const reports = await Promise.all( | ||
| Object.entries(specialists).map(async ([name, agent]) => ({ | ||
| agent: name, | ||
| output: (await agent.generateText(ctx, standaloneScope, { prompt })) | ||
| .text, | ||
| })), | ||
| ); | ||
| const combined = await coordinatorAgent.generateText(ctx, standaloneScope, { | ||
| prompt: `Combine these specialist reports into one answer to "${prompt}":\n\n${reports | ||
| .map(({ agent, output }) => `${agent}: ${output}`) | ||
| .join("\n\n")}`, | ||
| }); | ||
| return { | ||
| steps: [...reports, { agent: "coordinator", output: combined.text }], | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| /** Give agents distinct sequential responsibilities in one controlled flow. */ | ||
| export const orchestrate = action({ | ||
| args: { prompt: v.string() }, | ||
| returns: resultValidator, | ||
| handler: async (ctx, { prompt }): Promise<ExampleResult> => { | ||
| const plan = await coordinatorAgent.generateText(ctx, standaloneScope, { | ||
| prompt: `Create a short plan for answering: ${prompt}`, | ||
| }); | ||
| const analysis = await analystAgent.generateText(ctx, standaloneScope, { | ||
| prompt: `Execute this plan for "${prompt}":\n${plan.text}`, | ||
| }); | ||
| const critique = await criticAgent.generateText(ctx, standaloneScope, { | ||
| prompt: `Review this analysis and name the important corrections:\n${analysis.text}`, | ||
| }); | ||
| const final = await coordinatorAgent.generateText(ctx, standaloneScope, { | ||
| prompt: `Answer "${prompt}" using this analysis and critique.\n\nAnalysis: ${analysis.text}\n\nCritique: ${critique.text}`, | ||
| }); | ||
| return { | ||
| steps: [ | ||
| { agent: "coordinator", output: plan.text }, | ||
| { agent: "analyst", output: analysis.text }, | ||
| { agent: "critic", output: critique.text }, | ||
| { agent: "coordinator", output: final.text }, | ||
| ], | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| /** Let the model alternate between reasoning and deterministic tool actions. */ | ||
| export const reasonAndAct = action({ | ||
| args: { prompt: v.string() }, | ||
| returns: resultValidator, | ||
| handler: async (ctx, { prompt }): Promise<ExampleResult> => { | ||
| const response = await reactAgent.generateText(ctx, standaloneScope, { | ||
| prompt, | ||
| }); | ||
| return { steps: [{ agent: "ReAct agent", output: response.text }] }; | ||
| }, | ||
| }); | ||
|
|
||
| /** Let several agents contribute to the same persistent conversation thread. */ | ||
| export const agentNetwork = action({ | ||
| args: { prompt: v.string() }, | ||
| returns: resultValidator, | ||
| handler: async (ctx, { prompt }): Promise<ExampleResult> => { | ||
| const { threadId } = await coordinatorAgent.createThread(ctx, { | ||
| userId: standaloneScope.userId, | ||
| title: `Agent network: ${prompt}`, | ||
| }); | ||
| const turns = [ | ||
| ["analyst", analystAgent, `Analyze this request: ${prompt}`], | ||
| [ | ||
| "creative", | ||
| creativeAgent, | ||
| "Read the earlier analysis in this thread and propose better options.", | ||
| ], | ||
| [ | ||
| "critic", | ||
| criticAgent, | ||
| "Review the earlier messages and identify the strongest option and its main risk.", | ||
| ], | ||
| [ | ||
| "coordinator", | ||
| coordinatorAgent, | ||
| "Use the full discussion in this thread to give the final answer.", | ||
| ], | ||
| ] as const; | ||
| const steps: ExampleResult["steps"] = []; | ||
| for (const [agentName, agent, turnPrompt] of turns) { | ||
| const response = await agent.generateText( | ||
| ctx, | ||
| { threadId }, | ||
| { prompt: turnPrompt }, | ||
| ); | ||
| steps.push({ agent: agentName, output: response.text }); | ||
| } | ||
| return { steps }; | ||
| }, | ||
| }); | ||
|
|
||
| const workflow = new WorkflowManager(components.workflow); | ||
| const revisionRequested = defineEvent({ | ||
| name: "revisionRequested", | ||
| validator: v.string(), | ||
| }); | ||
|
|
||
| export const writeForReview = coordinatorAgent.asTextAction({}); | ||
|
|
||
| /** Pause durably until feedback arrives, then resume from the recorded step. */ | ||
| export const reviewWorkflow = workflow.define({ | ||
| args: { prompt: v.string() }, | ||
| returns: v.string(), | ||
| handler: async (step, { prompt }): Promise<string> => { | ||
| const { text: draft } = await step.runAction( | ||
| internal.workflows.coordination.writeForReview, | ||
| { | ||
| userId: standaloneScope.userId, | ||
| prompt: `Write a short draft for: ${prompt}`, | ||
| }, | ||
| { name: "writeDraft", retry: true }, | ||
| ); | ||
| const feedback = await step.awaitEvent(revisionRequested); | ||
| const { text: revision } = await step.runAction( | ||
| internal.workflows.coordination.writeForReview, | ||
| { | ||
| userId: standaloneScope.userId, | ||
| prompt: `Revise this draft using the feedback.\n\nDraft: ${draft}\n\nFeedback: ${feedback}`, | ||
| }, | ||
| { name: "reviseDraft", retry: true }, | ||
| ); | ||
| return revision; | ||
| }, | ||
| }); | ||
|
|
||
| export const startReviewWorkflow = mutation({ | ||
| args: { prompt: v.string() }, | ||
| returns: vWorkflowId, | ||
| handler: (ctx, { prompt }): Promise<WorkflowId> => | ||
| workflow.start( | ||
| ctx, | ||
| internal.workflows.coordination.reviewWorkflow, | ||
| { prompt }, | ||
| { startAsync: true }, | ||
| ), | ||
| }); | ||
|
|
||
| export const resumeReviewWorkflow = mutation({ | ||
| args: { workflowId: vWorkflowId, feedback: v.string() }, | ||
| returns: v.null(), | ||
| handler: async (ctx, { workflowId, feedback }) => { | ||
| await workflow.sendEvent(ctx, { | ||
| ...revisionRequested, | ||
| workflowId, | ||
| value: feedback, | ||
| }); | ||
| return null; | ||
| }, | ||
| }); | ||
|
|
||
| export const reviewWorkflowStatus = query({ | ||
| args: { workflowId: vWorkflowId }, | ||
| returns: v.union( | ||
| v.object({ | ||
| state: v.union(v.literal("running"), v.literal("waiting")), | ||
| }), | ||
| v.object({ state: v.literal("completed"), result: v.string() }), | ||
| v.object({ state: v.literal("failed"), error: v.string() }), | ||
| v.object({ state: v.literal("canceled") }), | ||
| ), | ||
| handler: async (ctx, { workflowId }) => { | ||
| const status = await workflow.status(ctx, workflowId); | ||
| switch (status.type) { | ||
| case "inProgress": | ||
| return { | ||
| state: status.running.some((step) => step.kind === "event") | ||
| ? ("waiting" as const) | ||
| : ("running" as const), | ||
| }; | ||
| case "completed": | ||
| if (typeof status.result !== "string") { | ||
| throw new Error("Review workflow returned a non-string result"); | ||
| } | ||
| return { state: "completed" as const, result: status.result }; | ||
| case "failed": | ||
| return { state: "failed" as const, error: status.error }; | ||
| case "canceled": | ||
| return { state: "canceled" as const }; | ||
| default: | ||
| return status satisfies never; | ||
| } | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: get-convex/agent
Length of output: 6241
🏁 Script executed:
Repository: get-convex/agent
Length of output: 50373
🏁 Script executed:
Repository: get-convex/agent
Length of output: 5728
🏁 Script executed:
Repository: get-convex/agent
Length of output: 5210
Use a test-only mock model for
orchestrate.defaultConfigselects a live Anthropic, OpenAI, or Groq model when the corresponding API key exists. The test has no model override, so it can make four live network calls.🤖 Prompt for AI Agents