Skip to main content

defineWorkflow

Use workflow.defineWorkflow<Input, Output>() to define a business workflow. It returns the definition object unchanged; an executor, CLI, Desktop, or Server runtime performs the actual execution.

Signature

workflow.defineWorkflow<Input, Output>(
workflow: WorkflowDefinition<Input, Output>,
): WorkflowDefinition<Input, Output>

Parameters

ParameterTypeDescription
workflowWorkflowDefinition<Input, Output>An executable workflow definition containing its name and main execution function.

WorkflowDefinition fields

FieldTypeDescription
namestringA stable workflow name used by Server, Desktop, logs, and structural analysis.
run(input, context) => OutputThe main execution function. It usually calls workflow.runNode or workflow.runStreamNode.

run arguments

NameDescription
inputBusiness input. In a Workflow project it comes from the selected entrypoint's createInput(context); in a Conversation project it comes from executor.createInput(context).
contextThe runtime-provided WorkflowContext, including providers, KV, conversation, files, metadata, node hooks, and an abort signal.

Return values

ValueTypeDescription
Workflow definitionWorkflowDefinition<Input, Output>The original definition object, for an executor to reference.
run resultOutputRead by the runtime during execution. Project output payloads must extend WorkflowPayload.

Execution flow

PhaseInputResult
ExecutorCLI arguments, form values, or a Server payloadInput
Workflow runInput and WorkflowContextOutput
Runtime reportOutputStandard execution report and final payload

Example

import type { WorkflowPayload } from "workflow-code";

interface Input {
message: string;
}

interface Output extends WorkflowPayload {
items: Array<{
id: string;
title: string;
content: unknown;
contentType?: "markdown" | "text" | "json" | "audio";
collapsed?: boolean;
}>;
}

export const exampleWorkflow = workflow.defineWorkflow<Input, Output>({
name: "example",
async run(input, context) {
const parsed = await workflow.runNode(inputNode, input, context);
return workflow.runNode(outputNode, parsed, context);
},
});

Notes

RuleDescription
Do not import runtime APIs from "workflow-code"The platform injects the global workflow object into business workflows.
Output must extend WorkflowPayloadOn success, return errCode: 0 and errMessage: "".
The runtime wraps exceptionsUnknown errors thrown from run are converted to WorkflowError and included in the execution report.