feat(js/plugins/middleware): implement agents middleware#5252
feat(js/plugins/middleware): implement agents middleware#5252pavelgj wants to merge 15 commits intopj/session-flow-take-2from
Conversation
Upgrade postcss from 8.4.31 to 8.5.12 across all workspaces and add new markdown/MDX-related dependencies (remark, rehype, unified, etc.) to the lockfile.
Introduce a new "Branching (Variants)" agent that generates multiple name suggestions and lets users pick one. Adds the nameAgent backend flow, Express API routes, a BranchingChat page with variant card UI, and navigation/routing integration in the test app.
Replaced the translatorAgent with a more comprehensive writerAgent in the agents test application. The new writerPrompt utilizes multiple input variables (tone, format, audience) rather than a single language parameter. This update better demonstrates how defineSessionFlowFromPrompt handles multiple prompt input variables. It showcases how a client can supply variables during initialization to dynamically reshape the agent's behavior at runtime without modifying server code. Related Express routes, console logs, and test flows have been updated to reflect the new agent and prompt names.
… apps Export AgentInit, AgentInput, AgentOutput, and AgentStreamChunk types from the genkit/beta entry point. Replace `as any` casts in the agents test app with proper generic type parameters for runFlow/streamFlow, improving type safety across BackgroundAgent and BankingInterrupt pages.
Add a new custom-state-agent (task tracker) to the agents test app, including Express API routes, a React TaskTrackerPage component with a chat + task sidebar UI, and associated styles for progress bars, task lists, and state visualization.
…lify workspaceAgent - Rename `simple-agent` to `custom-agent` and update all imports, routes, and log messages accordingly - Refactor `workspace-builder.ts` to use `defineAgent` instead of `defineCustomAgent`, leveraging the standard agent API for model calls, tool dispatch, streaming, and message management - Extract `emitArtifact` tool to module scope using `ai.defineTool`
There was a problem hiding this comment.
Code Review
This pull request introduces a new 'agents' middleware to the Genkit framework, enabling sub-agent delegation by injecting a delegation tool. The implementation includes the middleware logic, updated documentation, tests, and necessary dependency updates. The review feedback highlights a discrepancy between the documentation and the implementation regarding the method used to run sub-agents, suggests adopting stricter Zod validation for agent names and task descriptions, and advises aligning error handling with repository standards.
|
|
||
| export const AgentsOptionsSchema = z.object({ | ||
| agents: z | ||
| .array(z.string()) |
There was a problem hiding this comment.
The code correctly throws an error if the agents array is empty. To make this validation more declarative and fail earlier, you can enforce this constraint directly in the Zod schema using .min(1). This would also allow you to remove the manual check on lines 72-76.
.array(z.string())
.min(1)References
- Stricter validation is preferable to maintain consistency between different API versions (e.g., v1 and v2).
| .enum(agentNames as [string, ...string[]]) | ||
| .describe('The name of the sub-agent to call.'), | ||
| task: z | ||
| .string() |
There was a problem hiding this comment.
It is a good practice to ensure that the task delegated to a sub-agent is not an empty string. You can enforce this by adding .min(1) to the Zod schema.
| .string() | |
| .string().min(1) |
References
- Stricter validation is preferable to maintain consistency between different API versions (e.g., v1 and v2).
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Parent PR: #5251