翻篇收录说明
翻篇网页快照

Workflow Utilities | AI SDK

https://ai-sdk.dev/docs/ai-sdk-harnesses/workflow-utilities

最近抓取:2026-12-04 03:03:22(北京时间)

最近内容变化:2026-11-30 23:37:06(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Run HarnessAgent turns as durable Workflow DevKit workflows.

正文摘录

AI SDK HarnessesMenuWorkflow UtilitiesCopy for LLMView MarkdownAsk AI about this page@ai-sdk/workflow-harness provides helpers for running HarnessAgent turns inside a workflow. The package provides a serializable state machine and runners for time-sliced and semantic agent step turns. You call the appropriate runner from your own 'use workflow' and 'use step' functions. The core harness and workflow files are framework-independent. The HTTP handlers shown below use Next.js as one example; adapt them to your runtime and Workflow SDK integration. If you use Next.js, ensure you have configured your project for Workflow before following the examples. Installation pnpmnpmyarnbun$ pnpm add @ai-sdk/workflow-harness workflow In addition to the workflow specific packages, install the core harness package, a harness adapter, and a sandbox adapter as shown in HarnessAgent. Configuring the Harness Agent The agent can be configured in the usual way, for the most part. When using semantic agent steps, set stopWhen to isStepCount(1) so one call to stream() completes one agent step. Omit it when using time slices. harness-workflow/agent.tsimport { HarnessAgent } from '@ai-sdk/harness/agent'; import { claudeCode } from '@ai-sdk/harness-claude-code'; import { isStepCount } from 'ai'; export const agent = new HarnessAgent({ harness: claudeCode, instructions: 'You are a helpful coding assistant.', /* * Only needed for semantic agent-step workflows. * Omit this for time-sliced workflows. */ stopWhen: isStepCount(1), }); Using Semantic Agent Steps Semantic agent steps persist the harness turn after each agent step. Configure the shared agent with the highlighted stopWhen option shown above, then call runHarnessAgentStep() from a Workflow step. Defining the Agent Step Keep the Workflow step in its own module and import the agent dynamically inside the step body. This keeps the agent, sandbox adapter, and other Node.js dependencies out of the workflow bundle. harness-workflow/agent-step.tsimport { runHarnessAgentStep, type HarnessWorkflowState, } from '@ai-sdk/workflow-harness'; export async function agentStep( state: HarnessWorkflowState, ): Promise<HarnessWorkflowState> { 'use step'; const { agent } = await import('./agent'); const { createVercelNetworkSandboxSession, resumeVercelNetworkSandboxSession, } = await import('@ai-sdk/sandbox-vercel'); const sandboxId = `harness-${state.sessionId}`; const isFirstStep = state.resumeFrom == null && state.continueFrom == null; const sandboxSession = isFirstStep ? await createVercelNetworkSandboxSession({ sandboxId, runtime: 'node24', ports: [4000], template: await agent.getSandboxTemplate(), }) : await resumeVercelNetworkSandboxSession({ sandboxId }); return runHarnessAgentStep({ agent, state, sandboxSession, }); } Defining the Semantic Agent Step Workflow Create the workflow state and keep scheduling agentStep() while the agent has more work. harness-workflow/workflow.tsimport { agentStep } from './agent-step'; import { createHarnessWorkflowState, finalizeHarnessWorkflow, type HarnessWorkflowInput, } from '@ai-sdk/workflow-harness'; export async function agentWorkflow(input: { messages: NonNullable<HarnessWorkflowInput['messages']>; sessionId: string; }) { 'use workflow'; let state = createHarnessWorkflowState(input); do { state = await agentStep(state); } while (state.status === 'ready_for_next_step'); return finalizeHarnessWorkflow(state); } This example covers the workflow execution foundation only. For multi-turn conversations, you must persist the harness session's resumeFrom state between workflow runs. See Resume Persistence. Each ready_for_next_step result carries continueFrom, which lets the next Workflow step continue the same unfinished turn. When the turn finishes, finalizeHarnessWorkflow() returns its result or throws if the workflow failed. Starting the Semantic Agent Step Workflow Start the workflow from server-side code. This Next.js route converts the AI SDK UI messages, starts agentWorkflow(), and returns the workflow's AI SDK UI message stream. Passing the converted messages lets HarnessAgent distinguish a new user turn from tool approval and tool result continuations. app/api/harness-workflow/route.tsimport { agentWorkflow } from '../../../harness-workflow/workflow'; import { convertToModelMessages, createUIMessageStreamResponse, type UIMessage, type UIMessageChunk, } from 'ai'; import { start } from 'workflow/api'; export async function POST(request: Request) { const body: { id?: string; messages: UIMessage[]; } = await request.json(); if (!body.id) { return new Response('Missing chat ID', { status: 400 }); } const messages = await convertToModelMessages(body.messages); const run = await start(agentWorkflow, [ { messages, sessionId: body.id, }, ]); return createUIMessageStreamResponse({ stream: run.readable as ReadableStream<UIMessageChunk>, }); } The workflow state sessionId identifies the harness session. The step derives sandboxId from it before first creating the sandbox and uses that same ID to reattach on later steps. The state does not serialize a live sandbox. Keep agent.ts, agent-step.ts, workflow.ts, and the route in separate modules so the workflow bundle does not include Node-heavy agent, sandbox, or framework dependencies. Using Time Slices Time slices persist a long-running harness turn at wall-clock boundaries. Omit the highlighted stopWhen option from the shared agent, then call runHarnessAgentTimeSlice() from a Workflow step. It uses a 750-second budget by default; pass timeSliceSeconds to choose a different budget. Defining the Time Slice Step As with semantic agent steps, keep the Workflow step in its own module and harness-workflow/time-slice-step.tsimport { runHarnessAgentTimeSlice, type HarnessWorkflowState, } from '@ai-sdk/workflow-harness'; export async function timeSliceStep( state: HarnessWorkflowState, ): Promise<HarnessWorkflowState> { 'use step'; const { agent } = await import('./agent'); const { createVercelNetworkSandboxSession, resumeVercelNetworkSandboxSession, } = await import('@ai-sdk/sandbox-vercel'); const sandboxId = `harness-${state.sessionId}`; const isFirstStep = state.resumeFrom == null && state.continueFrom == null; const sandboxSession = isFirstStep ? await createVercelNetworkSandboxSession({ sandboxId, runtime: 'node24', ports: [4000], template: await agent.getSandboxTemplate(), }) : await resumeVercelNetworkSandboxSession({ sandboxId }); return runHarnessAgentTimeSlice({ agent, state, sandboxSession, }); } Defining the Time-Sliced Workflow Create the workflow state and keep scheduling timeSliceStep() while the agent has more work. harness-workflow/workflow.tsimport { timeSliceStep } from './time-slice-step'; import { createHarnessWorkflowState, finalizeHarnessWorkflow, type HarnessWorkflowInput, } from '@ai-sdk/workflow-harness'; export async function timeSliceWorkflow(input: { messages: NonNullable<HarnessWorkflowInput['messages']>; sessionId: string; }) { 'use workflow'; let state = createHarnessWorkflowState(input); do { state = await timeSliceStep(state); } while (state.status === 'ready_for_next_step'); return finalizeHarnessWorkflow(state); } This example covers the workflow execution foundation only. For multi-turn conversations, you must persist the harness session's resumeFrom state between workflow runs. See Resume Persistence. Each ready_for_next_step result carries continueFrom, which lets the next Workflow step continue the same unfinished turn. When the turn finishes, finalizeHarnessWorkflow() returns its result or throws if the workflow failed. Starting the Time-Sliced Workflow Start the workflow from server-side code. As with semantic agent steps, pass the converted messages so HarnessAgent can distinguish a new user turn from tool approval and tool result continuations. app/api/harness-workflow/route.tsimport { timeSliceWorkflow } from '../../../harness-workflow/workflow'; import { convertToModelMessages, createUIMessageStreamResponse, type UIMessage, type UIMessageChunk, } from 'ai'; import { start } from 'workflow/api'; export async function POST(request: Request) { const body: { id?: string; messages: UIMessage[]; } = await request.json(); if (!body.id) { return new Response('Missing chat ID', { status: 400 }); } const messages = await convertToModelMessages(body.messages); const run = await start(timeSliceWorkflow, [ { messages, sessionId: body.id, }, ]); return createUIMessageStreamResponse({ stream: run.readable as ReadableStream<UIMessageChunk>, }); } If you use Next.js, wrap your config with withWorkflow() so the Workflow SDK transforms 'use workflow' and 'use step' modules. Other frameworks have their own Workflow SDK setup. next.config.jsimport { withWorkflow } from 'workflow/next'; const nextConfig = {}; export default withWorkflow(nextConfig); The workflow state sessionId identifies the harness session. The step derives sandboxId from it; otherwise, you must persist sandboxSession.id separately to reattach across steps. Keep agent.ts, time-slice-step.ts, workflow.ts, and the route in separate modules so the workflow bundle does not include Node-heavy agent, sandbox, or framework dependencies. destroyOnFinish destroys the harness session, but never the supplied sandbox. The caller can explicitly call sandboxSession.destroy() on the network session when no later step needs to reattach. Never destroy it at a time-slice boundary where a subsequent step must resume it. Resume Persistence Workflow automatically persists the HarnessWorkflowState returned by each step, including continueFrom, for the duration of the current workflow run. To continue the native harness session across separate user-turn workflow runs, persist the opaque resumeFrom state by sessionId. For sandbox bridge adapters, resumeFrom and continueFrom contain the private key that authorizes reconnecting to the running bridge. Workflow stores this key with the step state, so treat your Workflow storage and any resume storage as sensit