Add Workflow-backed AI orchestration #979
Opentannerlinsleywants to merge 7 commits into
main fromtaren/workflow-orchestration-rfcopened View on GitHubjherr approved these changes
tombeckenham reviewed
@@ -0,0 +86,21 @@86+export const article = createWorkflow({87+ id: 'article',88+ input: z.object({ topic: z.string() }),89+})90+ .middleware([agentMiddleware()])91+ .handler(async (ctx) => {92+ const draft = await ctx.ai.agent('draft', writer, {93+ topic: ctx.input.topic,94+ })95+96+ const review = await ctx.approve({ title: 'Publish this article?' })97+ return { draft, approved: review.approved }98+ })99+```100+101+`ctx.ai` is the package-owned middleware namespace. Workflow core does not need102+to reserve every official integration name. A conflicting `ai` extension is a103+composition error visible in TypeScript, while Workflow's own fields remain104+reserved by core.105+106+The first argument to `ctx.ai.agent()` is the stable Workflow step ID. It is nottombeckenham reviewed
@@ -0,0 +92,21 @@92+ const draft = await ctx.ai.agent('draft', writer, {93+ topic: ctx.input.topic,94+ })95+96+ const review = await ctx.approve({ title: 'Publish this article?' })97+ return { draft, approved: review.approved }98+ })99+```100+101+`ctx.ai` is the package-owned middleware namespace. Workflow core does not need102+to reserve every official integration name. A conflicting `ai` extension is a103+composition error visible in TypeScript, while Workflow's own fields remain104+reserved by core.105+106+The first argument to `ctx.ai.agent()` is the stable Workflow step ID. It is not107+an arbitrary batch position. Reordering code across deployed workflow versions108+still follows Workflow's normal versioning rules.109+110+## Agent execution semantics111+112+An agent call is one Workflow step.tombeckenham reviewed
@@ -0,0 +197,21 @@197+198+TanStack AI persistence should own:199+200+threads and messages,201+tool-call and artifact projections,202+interrupt records,203+user-visible run metadata,204+the mapping between AI run IDs and Workflow run IDs.205+206+That persistence layer can consume the same AG-UI projection used by clients.207+It must tolerate repeated delivery because transports and recovered steps can208+produce duplicate attempts.209+210+## Child workflows211+212+True child workflows are deferred. Wrapping a child run inside `ctx.step()` is213+not sufficient: cancellation, parent-child status, signals, leases, and replay214+all need first-class runtime semantics.215+216+Composition in the first release uses normal helper functions and agent calls217+inside one Workflow definition.
Applied a fix from CI37fc82f
tanstack-source reviewed
@@ -0,0 +49,21 @@49+ }50+51+ return validateOutput(definition, result)52+ } finally {53+ cleanup()54+ }55+}56+57+async function drainAgentStream(58+ agentName: string,59+ stream: AsyncIterable<StreamChunk>,60+ emit: (chunk: StreamChunk) => void,61+): Promise<unknown> {62+ let text = ''63+ let structuredOutput: unknown | typeof NO_STRUCTURED_OUTPUT =64+ NO_STRUCTURED_OUTPUT65+66+ for await (const chunk of stream) {67+ if (chunk.type === 'RUN_STARTED' || chunk.type === 'RUN_FINISHED') {68+ continue69+ }@@ -0,0 +9,21 @@9+}) => {10+ const response = await request.post('/api/workflow-orchestration')11+ expect(response.ok()).toBeTruthy()12+13+ const chunks = parseSse(await response.text())14+ const types = chunks.map((chunk) => chunk.type)15+16+ expect(types).toEqual([17+ 'RUN_STARTED',18+ 'STEP_STARTED',19+ 'TEXT_MESSAGE_START',20+ 'TEXT_MESSAGE_CONTENT',21+ 'TEXT_MESSAGE_END',22+ 'STEP_FINISHED',23+ 'RUN_FINISHED',24+ ])25+ expect(chunks[1]).toMatchObject({26+ stepName: 'writer',27+ stepId: 'draft',28+ stepType: 'agent',29+ })
Applied a fix from CI9cf748b
Sign in to join this conversation.
🤖 Nx Cloud AI Fix Eligible
Important
Draft PR not reviewed#
Draft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share