npm.io
1.0.6 • Published yesterday

@ai-sdk/workflow-harness

Licence
Apache-2.0
Version
1.0.6
Deps
1
Size
86 kB
Vulns
0
Weekly
623
Stars
25.2K

@ai-sdk/workflow-harness

Run an AI SDK HarnessAgent (Claude Code, Codex, Pi) as a durable workflow using the Workflow DevKit.

A long agent turn is sliced into short, time-boxed steps so it survives a Fluid Compute function recycle (~800s). Between slices the agent is frozen non-destructively — the sandbox keeps running and the next slice reattaches to the in-flight turn (attach) — and a serializable state object is persisted as the durable step return value.

This package ships plain helpers + a serializable state machine; you own the thin 'use workflow' / 'use step' wrappers (the Workflow DevKit compiles those directives in your app).

Keep the Workflow DevKit entrypoints separate from the agent definition. The workflow module should import only workflow-safe code plus step modules. The step module should dynamically import the agent inside the 'use step' body so the agent, sandbox provider, and other Node-heavy dependencies stay out of the compiled workflow bundle.

agent.ts:

import { HarnessAgent } from '@ai-sdk/harness/agent';
import { claudeCode } from '@ai-sdk/harness-claude-code';
import { createVercelSandbox } from '@ai-sdk/sandbox-vercel';

export const agent = new HarnessAgent({
  harness: claudeCode,
  sandbox: createVercelSandbox({ runtime: 'node24', ports: [4000] }),
});

run-slice-step.ts:

import {
  runHarnessAgentSlice,
  type HarnessWorkflowState,
} from '@ai-sdk/workflow-harness';

export async function runSlice(
  state: HarnessWorkflowState,
): Promise<HarnessWorkflowState> {
  'use step';

  const { agent } = await import('./agent');
  return runHarnessAgentSlice({ agent, state });
}

workflow.ts:

import {
  createHarnessWorkflowState,
  finalizeHarnessWorkflow,
  type HarnessWorkflowInput,
} from '@ai-sdk/workflow-harness';
import { runSlice } from './run-slice-step';

export async function codingWorkflow(input: {
  prompt: HarnessWorkflowInput['prompt'];
  sessionId: string;
}) {
  'use workflow';

  let state = createHarnessWorkflowState(input);
  while (state.status === 'running' || state.status === 'timed_out') {
    state = await runSlice(state);
  }
  return finalizeHarnessWorkflow(state);
}

route.ts (Next.js example):

import { start } from 'workflow/api';
import { codingWorkflow } from './workflow';

export async function POST(request: Request) {
  const body = (await request.json()) as {
    prompt: string;
    sessionId: string;
  };
  const run = await start(codingWorkflow, [body]);

  return new Response(run.readable);
}

Keywords