import{CodeBlock as r,InstallCommand as a}from"@contractspec/lib.design-system";import t from"@contractspec/lib.ui-link";import{ChevronRight as c}from"lucide-react";import{jsx as e,jsxs as o}from"react/jsx-runtime";function d(){return o("div",{className:"space-y-8",children:[o("div",{className:"space-y-4",children:[e("h1",{className:"font-bold text-4xl",children:"Workflow Runtime Library"}),o("p",{className:"text-lg text-muted-foreground",children:["The ",e("code",{children:"@contractspec/lib.contracts-spec/workflow"})," library provides the core ",e("code",{children:"WorkflowRunner"})," for executing stateful, durable workflows."]})]}),o("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Installation"}),e(a,{package:"@contractspec/lib.contracts-spec"})]}),o("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"WorkflowRunner"}),e("p",{className:"text-muted-foreground",children:"The runner manages execution state, step transitions, retries, and compensation."}),e(r,{language:"typescript",code:`import { WorkflowRunner } from '@contractspec/lib.contracts-spec/workflow/runner';
import { InMemoryStateStore } from '@contractspec/lib.contracts-spec/workflow/adapters/memory-store';
import { WorkflowRegistry } from '@contractspec/lib.contracts-spec/workflow/spec';

const registry = new WorkflowRegistry();
registry.register(MyWorkflow);

const runner = new WorkflowRunner({
  registry,
  stateStore: new InMemoryStateStore(),
  opExecutor: async (op, input, ctx) => {
    // Execute operation using your adapter (REST, GraphQL, etc.)
    return await myAdapter.execute(op, input);
  },
});

// Start a workflow
const workflowId = await runner.start('my.workflow', 1, { userId: '123' });

// Execute next step (usually called by a worker or queue consumer)
await runner.executeStep(workflowId);`})]}),o("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"State Persistence"}),o("p",{className:"text-muted-foreground",children:["The runner relies on a ",e("code",{children:"StateStore"})," to persist workflow execution history. ContractSpec ships with:"]}),o("ul",{className:"list-disc space-y-2 pl-6 text-muted-foreground",children:[o("li",{children:[e("code",{children:"InMemoryStateStore"})," - for testing and development."]}),o("li",{children:[e("code",{children:"PrismaStateStore"})," - for production using Prisma ORM."]})]})]}),o("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Events"}),e("p",{className:"text-muted-foreground",children:"The runner emits events that you can subscribe to for monitoring:"}),o("ul",{className:"list-disc space-y-2 pl-6 text-muted-foreground",children:[e("li",{children:e("code",{children:"workflow.started"})}),e("li",{children:e("code",{children:"workflow.step_completed"})}),e("li",{children:e("code",{children:"workflow.step_failed"})}),e("li",{children:e("code",{children:"workflow.step_retrying"})}),e("li",{children:e("code",{children:"workflow.completed"})}),e("li",{children:e("code",{children:"workflow.cancelled"})}),e("li",{children:e("code",{children:"workflow.compensation_step_completed"})})]})]}),o("div",{className:"flex items-center gap-4 pt-4",children:[e(t,{href:"/docs/libraries",className:"btn-ghost",children:"Back to Libraries"}),o(t,{href:"/docs/libraries/data-views",className:"btn-primary",children:["Next: Data Views ",e(c,{size:16})]})]})]})}export{d as LibrariesWorkflowsPage};
