workflow
Modules
Module workflow
ballerina/workflow Ballerina library
Overview
Workflows and activities are ordinary Ballerina functions:
@workflow:Workflow— A durable function that orchestrates a business process. The runtime checkpoints every step and replays recorded history to recover from failures.@workflow:Activity— A function that performs a single non-deterministic operation (API call, database query, email send). Once an activity completes, its result is recorded and never re-executed during replay.
import ballerina/workflow; type OrderRequest record {| string orderId; string item; |}; type OrderResult record {| string orderId; string status; |}; @workflow:Activity function checkInventory(string item) returns boolean|error { // Call external inventory API return true; } @workflow:Workflow function processOrder(workflow:Context ctx, OrderRequest request) returns OrderResult|error { boolean inStock = check ctx->callActivity(checkInventory, {"item": request.item}); if !inStock { return {orderId: request.orderId, status: "OUT_OF_STOCK"}; } return {orderId: request.orderId, status: "COMPLETED"}; }
Key Features
@workflow:Workflowand@workflow:Activityannotations for durable orchestration and non-deterministic operations- Automatic checkpointing and replay-based recovery from failures
- External data delivery to running workflow instances
Authoring guidelines
- Workflow functions must contain only orchestration logic (control flow and waiting for data). All business logic and non-deterministic operations - database calls, external API calls, I/O - belong in activity functions.
- Activities must be invoked through
ctx->callActivity(...)from within a workflow function. Calling an activity function directly is a compile-time error. - When calling an activity, pass its arguments as a record whose keys exactly match the activity function's parameter names - for example,
ctx->callActivity(checkInventory, {"item": request.item})forfunction checkInventory(string item). - When using the Ballerina Integrator tooling, place all
@workflow:Workflowand@workflow:Activityfunctions in the project'sfunctions.balfile. - Store the workflow ID returned by
workflow:run()so that laterworkflow:sendData()calls can be routed to the correct running instance.
Starting a Workflow
Use workflow:run() to start a workflow instance from any entry point — HTTP service, scheduled job, message consumer, or main:
string workflowId = check workflow:run(processOrder, {orderId: "ORD-001", item: "laptop"});
Receiving External Data
A workflow can pause and wait for external input — approvals, payment confirmations, user decisions — using future-based event records. Send data to a running workflow with workflow:sendData():
// In the workflow — wait for a human decision ApprovalDecision decision = check wait events.approval; // From outside — deliver the decision check workflow:sendData(processOrder, workflowId, "approval", {approverId: "mgr-1", approved: true});
Multi-Future Waits with ctx->await
Use ctx->await to wait for multiple futures at once, with optional quorum and timeout:
| Pattern | Example |
|---|---|
| Wait for all | ctx->await([f1, f2]) |
| Wait for any (first wins) | ctx->await([f1, f2], 1) |
| Quorum (N of M) | ctx->await([f1, f2, f3], 2) |
| With deadline | ctx->await([f1, f2], timeout = {hours: 48}) |
Error Handling
Activity errors are returned as plain Ballerina values. The workflow decides what happens next:
string|error result = ctx->callActivity(chargeCard, {"amount": input.amount}); if result is error { // retry with a different card, fall back, or compensate }
Enable automatic retries for transient failures:
string result = check ctx->callActivity(chargeCard, {"amount": input.amount}, retryPolicy = {maxRetries: 3});
Configuration
Add a Config.toml to your project. For local development with no server:
[ballerina.workflow] mode = "IN_MEMORY"
For production, connect to a Temporal server:
[ballerina.workflow] mode = "SELF_HOSTED" url = "temporal.mycompany.com:7233" namespace = "default" taskQueue = "my-task-queue"
See Configure the Module for every mode and field.
Documentation
| Guide | Description |
|---|---|
| Get Started | Write and run your first workflow |
| Key Concepts | Workflows, activities, external data, and timers |
| Write Workflow Functions | Signatures, determinism rules, and durable sleep |
| Write Activity Functions | Activity patterns and retry options |
| Handle Data | Waiting for external input and sending data |
| Handle Errors | Propagation, retry, fallback, and compensation |
| Configure the Module | Connection settings, TLS, and namespaces |
Examples
| Example | Description |
|---|---|
| Get Started | First workflow |
| Order Processing | HTTP-triggered workflow with result polling |
| Human in the Loop | Pause for a human approval |
| Wait for All | Dual authorization — both teams must approve |
| Alternative Wait | First responder wins (approval ladder) |
| Forward Recovery | Pause for corrected data and retry |
| Error Propagation | Fail the workflow on a critical error |
| Error Fallback | Fall back to a secondary activity |
| Error Compensation | Saga: undo committed steps on failure |
Functions
completeHumanTask
function completeHumanTask(string taskWorkflowId, anydata result, [string, string...]? callerRoles, string? userId) returns error?Completes a pending human task by sending the result back to the waiting workflow. Given
callerRoles, the caller must hold one of the task's userRoles; omitting them skips the check
and leaves enforcement to the caller.
Parameters
- taskWorkflowId string - Temporal workflow ID of the human task child workflow
- result anydata - The value to return to the workflow (must be compatible with the declared
T)
- callerRoles [string, string...]? (default ()) - Roles held by the caller; validated against the task's configured
userRoles
- userId string? (default ()) - The user ID of the person completing the task (used for auditing)
Return Type
- error? - An error if the task cannot be found, is already completed, or the caller is unauthorized
getPendingAgentEvents
function getPendingAgentEvents(string agentId) returns PendingAgentEvent[]|errorLists the data events a running durable agent has accepted but not yet answered. Use it after a
restart to rediscover in-flight turns and read their answers with DurableAgent.getDataResult
or waitForDataResult.
Parameters
- agentId string - Target agent (workflow) ID
Return Type
- PendingAgentEvent[]|error - The in-flight event turns (empty when the agent is idle), or an error
getWorkflowResult
Waits for a workflow to complete and returns its result.
Parameters
- workflowId string - The workflow ID
- timeoutSeconds int (default 30) - Maximum wait time in seconds
Return Type
- anydata|error - Result of the workflow as anydata, or an error
run
Starts a new workflow instance and returns its unique ID.
Parameters
- processFunction
function() ()- The workflow function (must have@Workflow)
- input anydata (default ()) - Optional input data for the workflow. Must match the workflow
function's declared input parameter type (any
anydatasubtype)
sendData
function sendData( function() () workflow, string workflowId, string dataName, anydata data) returns error?Sends data to a running workflow's events record.
Parameters
- workflow
function() ()- The workflow function (must have@Workflow)
- workflowId string - Target workflow ID (from
run)
- dataName string - Field name in the workflow's events record
- data anydata - The data payload
Return Type
- error? - An error if sending fails
Classes
workflow: DurableAgent
A durable AI agent declared as an object. Must be assigned to a module-level final variable:
its capabilities are registered at compile time from the constructor config, and the variable
name becomes the agent's stable identity.
Constructor
Declares the agent. Capabilities are fixed here and registered with the workflow runtime at module init by the compiler plugin.
init (*DurableAgentConfig config)- config *DurableAgentConfig - The agent's complete configuration
bindAgentName
function bindAgentName(string agentName)Binds the agent's stable identity — its module-level variable name — to this object. Called by the compiler-plugin-generated module-init code; not part of the public API surface. The first binding wins; later calls are ignored.
Parameters
- agentName string - The agent's module-level variable name
run
Starts the agent durably and returns the new instance ID — always the ID,
never the result (a durable agent may suspend for days on a human task, so
no caller thread is blocked). Outside a workflow this is a top-level start;
inside a @workflow:Workflow the agent runs as a Temporal child workflow.
Parameters
- query string - The user turn appended to the agent's system prompt
- input json (default ()) - Optional structured JSON payload for the run; must match the
agent's declared
inputType
sendData
Sends an event to a running instance on a declared channel and returns a correlation token
for reading that turn's response. The instance must be one this agent's run returned: the
channel and payload are checked against this declaration.
Parameters
- instanceId string - An instance ID this agent's
runreturned
- eventName string - A channel declared in the agent's
events
- data anydata - The payload; validated against the channel's declared
requesttype
getResult
Returns the final result of an instance if it has finished, without waiting.
While the instance is still working (e.g. suspended on a human task) a
workflow:AgentBusyError is returned — check back later, or use waitForResult.
Parameters
- instanceId string - The agent instance ID returned by
run
- T typedesc<anydata> (default <>) - Expected result type (inferred from context)
Return Type
- T|error - The result as
T, aworkflow:AgentBusyErrorwhile in progress, or an error
getDataResult
Returns the response for a specific sendData turn if it is ready, without
waiting. While the turn is unanswered a workflow:AgentBusyError is returned.
Parameters
- instanceId string - The agent instance ID returned by
run
- token string - The correlation token returned by
sendData
- T typedesc<anydata> (default <>) - Expected response type (inferred from context)
Return Type
- T|error - The turn's response as
T, aworkflow:AgentBusyErrorwhile unanswered, or an error
waitForResult
Waits until the instance finishes and returns its result. Inside a workflow this durably suspends the caller (no thread held); from a service it blocks but is resumable — if the caller crashes, calling again after restart resumes the wait, because the result lives in history.
Parameters
- instanceId string - The agent instance ID returned by
run
- T typedesc<anydata> (default <>) - Expected result type (inferred from context)
Return Type
- T|error - The result as
T, or an error
waitForDataResult
Waits for a specific sendData turn's response (same durability guarantees
as waitForResult).
Parameters
- instanceId string - The agent instance ID returned by
run
- token string - The correlation token returned by
sendData
- T typedesc<anydata> (default <>) - Expected response type (inferred from context)
Return Type
- T|error - The turn's response as
T, or an error
Clients
workflow: Context
Workflow execution context providing activity execution, durable sleep, deterministic time, and multi-future await APIs.
Constructor
Creates a workflow execution context wrapping the native context handle.
This constructor is called by the workflow runtime; do not instantiate Context directly.
init (handle nativeContext)- nativeContext handle - Native context handle from the workflow engine
callActivity
function callActivity( function() () activityFunction, map<anydata|object {}> args, typedesc<anydata> T, string? stepId, *CallActivityOptions options) returns T|errorExecutes an activity function. A completed activity is never re-executed on replay — its
recorded result is reused — but a failed attempt can run again under a retry policy, so keep
side effects idempotent. T comes from the assignment, so bind every call: () _ = check ...
when the activity returns only error?.
Parameters
- activityFunction
function() ()- The activity function (must have@Activity)
- args map<anydata|object {}> (default {}) - Arguments keyed by parameter name. A module-level
finalclient object may be passed where the activity declares one
- T typedesc<anydata> (default <>) - Expected return type (inferred from context)
- stepId string? (default ()) - Identity of this step within the workflow, matching a node of the descriptor
graph. A constant string; defaults to
<activity>#<ordinal>
- options *CallActivityOptions - How the invocation behaves:
approvalPolicygates the call behind a review;retryPolicyisNoRetry(default),AutoRetry, aReviewTaskDefinitionthat raises a review on failure, orRetryBeforeReview
Return Type
- T|error - The activity result as
T, or an error
await
function await(future<anydata>[] futures, Unsigned32 minCount, Duration? timeout, typedesc<anydata|error|(anydata|error)[]> T) returns TWaits for at least minCount data futures to complete. Results are a positional tuple
aligned to input order; use nilable members (T?) for partial waits.
Parameters
- futures future<anydata>[] - Data futures from the workflow's events record
- minCount Unsigned32 (default <int:Unsigned32>futures.length()) - Minimum completions required (default: all)
- timeout Duration? (default ()) - Maximum wait duration; returns an error on timeout
Return Type
- T - Positional tuple of values (
nilfor an incomplete position), or an error
awaitHumanTask
function awaitHumanTask(string taskName, map<json> taskInput, typedesc<anydata> T, string? stepId, *HumanTaskDefinition definition) returns T|HumanTaskErrorCreates a human task and blocks until a person completes it or the timeout elapses. The task
runs as a durable child workflow typed taskName, so it survives worker restarts.
Parameters
- taskName string - Identifies the task type; used as the child workflow type and ID
- taskInput map<json> - Read-only object shown beside the form;
{}when there is nothing to show
- T typedesc<anydata> (default <>) - Expected result type; drives form schema generation and runtime validation
- stepId string? (default ()) - Identity of this step within the workflow, as for
callActivity
- definition *HumanTaskDefinition - The task's
HumanTaskDefinition, as an included record (userRoles = "MANAGER")
Return Type
- T|HumanTaskError - The value the person submitted, or a
HumanTaskError— timed out, rejected, or failed to produce a result
runChildWorkflow
function runChildWorkflow( function() () childWorkflow, anydata input, string? stepId) returns string|errorStarts a child workflow and returns its instance ID without waiting. The child's lifecycle is
tied to this workflow, so in-flight children are cancelled when it closes. Read the result
later with getChildWorkflowResult or waitForChildWorkflow to fan out and gather.
Parameters
- childWorkflow
function() ()- The child workflow function (must have@Workflow)
- input anydata (default ()) - Optional input for the child workflow. Must match the child workflow
function's declared input parameter type (any
anydatasubtype)
- stepId string? (default ()) - Identity of this step within the workflow, as for
callActivity
getChildWorkflowResult
Returns a child workflow's result if it has already completed, without waiting. While the
child is still running this answers WorkflowBusyError — check back later, or use the
blocking waitForChildWorkflow.
Parameters
- childWorkflowId string - The child workflow instance ID returned by
runChildWorkflow
- T typedesc<anydata> (default <>) - Expected result type (inferred from context)
Return Type
- T|error - The child's result as
T, aworkflow:WorkflowBusyErrorwhile the child is still running, or an error if the child failed
waitForChildWorkflow
Waits durably until a child workflow started with runChildWorkflow completes and
returns its result. The wait is a durable suspend — no thread is held, and the wait
survives worker crashes and restarts (on replay the result is served from history).
Parameters
- childWorkflowId string - The child workflow instance ID returned by
runChildWorkflow
- T typedesc<anydata> (default <>) - Expected result type (inferred from context)
Return Type
- T|error - The child's result as
T, or an error if the child failed
callWorkflow
function callWorkflow( function() () childWorkflow, anydata input, typedesc<anydata> T, string? stepId) returns T|errorStarts a child workflow and durably waits for its result — runChildWorkflow followed by
waitForChildWorkflow in one call. The wait is a durable suspend, not a held thread.
Parameters
- childWorkflow
function() ()- The child workflow function (must have@Workflow)
- input anydata (default ()) - Optional input for the child workflow. Must match the child workflow
function's declared input parameter type (any
anydatasubtype)
- T typedesc<anydata> (default <>) - Expected result type (inferred from context)
- stepId string? (default ()) -
Return Type
- T|error - The child's result as
T, or an error if the child failed
sendDataToChildWorkflow
function sendDataToChildWorkflow(string childWorkflowId, string dataName, anydata data) returns error?Sends data to a running workflow instance's events record from inside a workflow — the
in-workflow counterpart of workflow:sendData, usually aimed at a child workflow.
Parameters
- childWorkflowId string - Target workflow instance ID (usually from
runChildWorkflow)
- dataName string - Field name in the target workflow's events record
- data anydata - The data payload
Return Type
- error? - An error if sending fails
sleep
Durable sleep that survives process crashes and restarts. Use instead of runtime:sleep.
Parameters
- duration Duration - The duration to sleep
- stepId string? (default ()) - Identity of this step within the workflow, as for
callActivity
Return Type
- error? - An error if the sleep fails, otherwise nil
currentTime
function currentTime() returns UtcReturns the deterministic workflow time. Use instead of time:utcNow() inside workflows.
Return Type
- Utc - The current workflow time as
time:Utc
isReplaying
function isReplaying() returns booleanChecks whether the workflow is recovering from a failure (re-executing recorded history).
Return Type
- boolean -
trueif recovering,falseon first execution
getWorkflowId
Get the unique workflow ID.
getWorkflowType
Get the workflow type name.
lastHumanTaskCompletion
function lastHumanTaskCompletion(string? taskName) returns HumanTaskCompletion?Who acted on the most recent human task this workflow created — or on the most recent one
with the given name. Lets a later task exclude or prefer that person. () before any task
has completed.
Parameters
- taskName string? (default ()) - A task name, or
()for the latest task of any name
Return Type
- HumanTaskCompletion? - The completion, or
()
lastReviewDecision
function lastReviewDecision(string? taskName) returns ReviewDecisionRecord?The decision reached by the most recent review this workflow raised — a gated call or a
failed activity — or by the most recent review of the given task name. () before any
review has been decided.
Parameters
- taskName string? (default ()) - A review task name, or
()for the latest review of any name
Return Type
- ReviewDecisionRecord? - The decision, or
()
Constants
workflow: NoApproval
No approval gate: the call runs as soon as it is made.
workflow: NoAutomaticRetry
DeprecatedDeprecated alias of NoRetry.
Deprecated
Use NoRetry instead.
workflow: NoRetry
No automatic retry by the engine. Errors from the activity are returned
directly to the caller. This is the default behaviour when no retryPolicy
is specified. Note that an AI agent may still decide to call the activity
again from its own reasoning — this policy only disables engine-driven
retries.
Enums
workflow: CompletionRole
Who completed a task, relative to its definition: someone it was assigned to, or one of its administrators stepping in.
Members
workflow: EventCardinality
How a declared event channel consumes its requests.
Members
workflow: Mode
Deployment mode for the workflow runtime.
Members
temporal server start-dev)Annotations
workflow: Activity
Marks a function as a workflow activity.
workflow: Workflow
Marks a function as a workflow.
Configurables
workflow: mode
Deployment mode for the workflow runtime.
LOCAL— Connects to a locally running server (e.g.,temporal server start-dev). This is the default mode.CLOUD— Managed cloud deployment. Requiresurl,namespace, and authentication (authApiKeyor mTLS certificate/key pair).SELF_HOSTED— Self-hosted server. Requiresurl; authentication is optional.IN_MEMORY— Lightweight in-memory engine with no external server. Workflows are not persisted and will be lost on restart. All other connection/scheduler fields are ignored in this mode.
workflow: url
Server URL for the workflow runtime.
For LOCAL mode, defaults to "localhost:7233".
For CLOUD mode, use the cloud endpoint (e.g., "
workflow: namespace
Workflow namespace.
For LOCAL and SELF_HOSTED modes, defaults to "default".
For CLOUD mode, use your cloud namespace (e.g., "
workflow: authApiKey
API key for bearer-token authentication. Required for CLOUD mode (unless mTLS is configured). Optional for SELF_HOSTED mode. Ignored in LOCAL and IN_MEMORY modes.
workflow: authMtlsCert
Path to the mTLS client certificate file (PEM format).
Used for CLOUD or SELF_HOSTED modes with mutual TLS authentication.
Must be provided together with authMtlsKey.
Ignored in LOCAL and IN_MEMORY modes.
workflow: authMtlsKey
Path to the mTLS client private key file (PEM format).
Used together with authMtlsCert for mutual TLS authentication.
Ignored in LOCAL and IN_MEMORY modes.
workflow: authCaCert
Path to the CA certificate file (PEM format) used to verify the server's TLS certificate. Set this when the Temporal server uses a certificate from a private or self-signed CA that is not in the JVM's default trust store. Applicable to CLOUD mode (API key over TLS) and SELF_HOSTED mode (TLS or mTLS). Ignored in LOCAL and IN_MEMORY modes.
workflow: taskQueue
Task queue name for workflow and activity polling. Each workflow program should use a unique task queue to avoid conflicts. Ignored in IN_MEMORY mode.
workflow: maxConcurrentWorkflows
Maximum number of concurrent workflow task executions. Controls how many workflow tasks the workflow scheduler processes in parallel. Must be a positive integer. Ignored in IN_MEMORY mode.
workflow: maxConcurrentActivities
Maximum number of concurrent activity executions. Controls how many activities the workflow scheduler processes in parallel. Must be a positive integer. Ignored in IN_MEMORY mode.
workflow: activityRetryInitialInterval
Initial delay (in seconds) before the first activity retry attempt.
Part of the default retry policy applied to all activities unless
overridden per-call via callActivity's retryPolicy (e.g., an AutoRetry
record setting maxRetries or retryDelay).
Must be a positive integer.
workflow: activityRetryBackoffCoefficient
Backoff multiplier applied to the activity retry interval after each attempt. For example, with an initial interval of 1s and coefficient of 2.0, retries occur at 1s, 2s, 4s, 8s, etc. Must be >= 1.0.
workflow: activityRetryMaximumInterval
Maximum delay (in seconds) between activity retries. Caps the exponential backoff to prevent excessively long waits. 0 means unset/no upper limit and is allowed. When explicitly set to a non-zero value it must be a positive integer (> 0).
workflow: activityRetryMaximumAttempts
Maximum number of activity retry attempts.
- 1 means no retries (execute once only, the default).
- 0 means unlimited retries.
- Any positive value sets the retry cap.
Records
workflow: ActivityDecl
An activity capability of a durable agent, with optional gating and retry config.
For the no-config case pass the bare @workflow:Activity function instead.
Fields
- activity
function() ()- The@workflow:Activityfunction
- name? string - Tool name advertised to the model; defaults to the function name
- description? string - Tool description advertised to the model; defaults to the function's doc comment
- bindings? map<anydata|object {}> - Fixed arguments partially applied to the activity (e.g. a
connection), hidden from the model: only the remaining data parameters appear in the tool's schema. Client objects are bound by referencing their module-levelfinalvariable
- approvalPolicy ApprovalPolicy(default NoApproval) - Whether a person approves each call before it runs, and who
- retryPolicy RetryPolicy(default NoRetry) - Retry behaviour on failure, as for
ctx->callActivity
workflow: AutoRetry
Automatic retry configuration. When the activity fails, it is automatically retried according to the configured backoff policy.
Fields
- maxRetries int(default 3) - Maximum retry attempts (default: 3)
- retryDelay decimal(default 1.0) - Initial delay in seconds before the first retry (default: 1.0)
- retryBackoff decimal(default 2.0) - Multiplier applied to delay after each retry (default: 2.0)
- maxRetryDelay? decimal - Cap on the delay between retries, in seconds
workflow: CallActivityOptions
How a Context.callActivity invocation behaves, passed as an included record
parameter. Deliberately an OPEN record so a future behaviour
option — an approval gate, a heartbeat policy, a per-call timeout — is a new field
here rather than a new parameter, and tooling derives its forms from this record.
The step identity (stepId) is NOT here: it is workflow mechanics, not invocation
behaviour, and stays a function parameter on every context operation.
Fields
- approvalPolicy ApprovalPolicy(default NoApproval) - Whether a person approves the call before it runs, and who
- retryPolicy RetryPolicy(default NoRetry) - Failure behaviour:
NoRetry(fail the workflow),AutoRetry(durable backoff retries), aReviewTaskDefinition(raise a review on failure so a person decides to rerun, rerun with edited input, or fail), orRetryBeforeReview(retry, then review)
workflow: DurableAgentConfig
The complete, self-declarative configuration of a durable agent. Capability kinds are separate fields so each renders as its own edge type in the diagram.
Fields
- systemPrompt SystemPrompt - The agent's identity: role + instructions (the per-run query
is appended as the user turn by
run)
- model ModelProvider - The model provider used for the agent's LLM calls
- inputType typedesc<json>?(default json) - The type of the structured JSON payload the agent's
run(and the management API start) accepts alongside the query.json(the default) accepts any JSON payload unvalidated; a narrower type — typically a record — declares the payload's shape and is validated against it at the call site and at run time;()declares a no-payload agent whose only input is the query text
- resultType typedesc<anydata>?(default ()) - When declared, the agent produces a typed final result: as the
reasoning loop concludes, one more durable model call converts the
conversation outcome into this type, and
waitForResult/getResultreturn it.()keeps the final text response
- activities (ActivityDecl|
function() ())[](default []) -@workflow:Activityfunctions, bare or asActivityDeclwhen gating/roles/bindings are needed
- tools (ToolDecl|ToolConfig|BaseToolKit|
function() ())[](default []) - AI tools:@ai:AgentToolfunctions,ai:ToolConfigs, toolkits, orToolDeclwhen gating is needed
- events map<EventConfig>(default {}) - Event channels keyed by channel name (
{chat: {request: string, MISSING[]response: string}}) MISSING[]
- humanTasks map<HumanTaskDefinition>(default {}) - Human task capabilities keyed by task name (
{signoff: {userRoles: MISSING[]"manager"}}) MISSING[]Capability names share one namespace across activities, tools, events, human tasks, and peers: a name claimed twice is rejected when the agent registers, so the program fails at startup
- peers PeerDecl[](default []) - Peer durable agents advertised as delegable tools
- maxIter int(default 16) - Hard cap on reasoning iterations per turn
- eventTimeout Duration?(default ()) - Maximum wait per event-channel wait (each chat turn, each event).
Omit to wait indefinitely — a conversation stays open as long as it
takes, and
maxEventWaitsremains the runaway backstop. On a timeout the model is told so it can wrap up gracefully
- maxEventWaits int(default MAX_EVENT_WAITS) - Cap on event waits per run — chat turns and events together. The backstop for a conversation nobody closes; raise it for a chat-like agent whose turns are many and short
workflow: Duration
A time duration, structurally identical to time:Duration. Declared in this module so
timeout fields render as first-class workflow forms without a cross-module type reference;
time:Duration values remain assignable.
Fields
- years int(default 0) - The duration in years
- months int(default 0) - The duration in months
- weeks int(default 0) - The duration in weeks
- days int(default 0) - The duration in days
- hours int(default 0) - The duration in hours
- minutes int(default 0) - The duration in minutes
- seconds decimal(default 0.0) - The duration in seconds
workflow: EventConfig
One event channel of a durable agent, declared in the mapping form of events
where the mapping key is the channel name — constant by construction, so the
name needs no separate validation. request/response capture both sides'
types and the channel's duplexity: a response type declares a duplex
(request-response) channel whose turn answers are read with
getDataResult/waitForDataResult; a nil response declares a one-way
channel — data flows in, no result is read back.
Fields
- request typedesc<anydata> - Type of the payload sent to the agent on this channel;
sendDatavalidates each payload against it
- response typedesc<anydata>?(default ()) - Type of the agent's reply for this channel;
()for one-way channels
- cardinality EventCardinality(default MULTI_EVENT) - Business cardinality of the channel: re-armed per turn
(
MULTI_EVENT, the default) or consumed once (SINGLE_EVENT)
workflow: HumanTaskCompletion
Who acted on a human task this workflow created, as recorded when the task closed.
Fields
- taskId string - Child workflow ID of the task instance
- taskName string - The qualified task name
- completedBy string?(default ()) - The user who completed or rejected it, when the completion recorded one
- completedAt string?(default ()) - ISO-8601 instant of the decision, when recorded
- identitySource string?(default ()) - Where that identity came from, when recorded
- completedAs CompletionRole(default AUDIENCE) -
workflow: HumanTaskDefinition
A human task: who may answer it and how it reads, plus the shapes it takes and returns.
The input supplied to the task is checked against taskInputType before the task is
created, whether a workflow passes it to awaitHumanTask or an agent supplies it.
Fields
- Fields Included from *ReviewTaskDefinition
- maxRetries never
- userRoles string|[string, string...]|()
- users string|[string, string...]
- excludedUsers string|[string, string...]
- excludedRoles string|[string, string...]
- administratorRoles string|[string, string...]
- administratorUsers string|[string, string...]
- title string|()
- description string|()
- timeout Duration|()
- anydata...
- taskInputType typedesc<map<json>>(default JsonObject) - Shape of the input shown to the decider
- resultType typedesc<anydata>(default anydata) - Shape of the answer. A workflow states this as
awaitHumanTask'sTinstead; an agent declares it here
workflow: HumanTaskRejectedDetail
Detail fields carried by a HumanTaskRejectedError.
Fields
- taskName string - The
taskNamevalue passed toawaitHumanTask
- taskWorkflowId string - Temporal child workflow ID of the rejected task instance
- reason string - The reason submitted with the rejection
- details map<json>?(default ()) - Structured data submitted with the rejection, or
()if none was given
- rejectedBy string?(default ()) - The user who rejected the task, when the rejection recorded one
workflow: HumanTaskTimeoutDetail
Detail fields carried by a HumanTaskTimeoutError.
Fields
- taskName string - The
taskNamevalue passed toawaitHumanTask
- taskWorkflowId string - Temporal child workflow ID of the timed-out task instance
- timedOutAfter string - Configured deadline as an ISO-8601 duration (e.g.
"PT24H")
- timedOutAt string - ISO-8601 timestamp at which the timeout was recorded
workflow: PeerDecl
A peer advertised to this agent's model as delegable tools: one to start the peer, and one per event the peer declares. The peer runs as a Temporal child workflow; its tool names take the peer's module-level variable name as prefix. A one-way event returns an acknowledgement, a duplex event or the run entry waits durably for the answer.
Fields
- agent DurableAgent|
function() ()- The peerworkflow:DurableAgent, or a@workflow:Workflowfunction (started fire-and-forget: a workflow has no events to answer on)
- description? string - What the peer does, for the model
- allowedEvents? string[] - The peer events to expose; omit for every declared event,
[]for the run entry only
workflow: PendingAgentEvent
A data-event turn a durable agent has accepted but not yet answered. Returned
by getPendingAgentEvents so callers can rediscover in-flight event turns
after a crash and fetch their answers via DurableAgent.getDataResult /
waitForDataResult.
Fields
- token string - The turn's correlation token (as returned by
DurableAgent.sendData)
- eventName string - The event channel the turn was sent on
workflow: RetryBeforeReview
Automatic retries first; when they are spent, a person decides. Carries both an
AutoRetry and a review's audience, so the review is raised only after the last
automatic attempt fails. maxRetries is required here and forbidden on
ReviewTaskDefinition, so a literal of either shape needs no cast.
Fields
- Fields Included from *AutoRetry
- userRoles string|[string, string...]|() - Role(s) permitted to answer this decision, or
()when onlyusersmay
- users string|[string, string...] - User id(s) permitted to answer it, whatever their roles
- excludedUsers string|[string, string...] - User id(s) that may not answer it, whatever their roles
- excludedRoles string|[string, string...] - Role(s) that may not answer it
- administratorRoles string|[string, string...] - Role(s) that administer it: they see it, may reassign it, move its deadline, fail it, or complete it — a completion by them is recorded as an administrator's
- administratorUsers string|[string, string...] - User id(s) that administer it, whatever their roles
- title string|() - Short summary shown in the inbox. Defaults to the task name
- description string|() - Additional context shown with the form or decision
- timeout Duration|() - Maximum time to wait. Omit to wait indefinitely
- anydata... -
- maxRetries int - Automatic attempts before the review is raised
workflow: ReviewDecisionRecord
The decision a review reached, as this workflow saw it.
Fields
- taskId string - Child workflow ID of the review instance
- taskName string - The review's qualified task name
- action string -
proceed,proceed-with-inputorreject
- feedback string?(default ()) - The reviewer's note, when one was given
- decidedBy string?(default ()) - The user who decided, when recorded
- decidedAt string?(default ()) - ISO-8601 instant of the decision, when recorded
- completedAs CompletionRole(default AUDIENCE) -
workflow: ReviewRejectedDetail
Detail fields carried by a ReviewRejectedError.
Fields
- taskName string - The review's qualified task name
- taskWorkflowId string - Child workflow ID of the review instance
- activityName string - The activity under review
- trigger string -
PRE_RUNfor an approval gate,ON_FAILUREfor a failed activity
- feedback string?(default ()) - The reviewer's note, when one was given
- rejectedBy string?(default ()) - The user who rejected it, when recorded
workflow: ReviewTaskDefinition
A review's whole definition: its audience and wording. A human task adds the shapes it
is checked against — see HumanTaskDefinition. maxRetries is forbidden so a
RetryBeforeReview literal is never mistaken for a plain review.
Fields
- userRoles string|[string, string...]|() - Role(s) permitted to answer this decision, or
()when onlyusersmay
- users string|[string, string...] - User id(s) permitted to answer it, whatever their roles
- excludedUsers string|[string, string...] - User id(s) that may not answer it, whatever their roles
- excludedRoles string|[string, string...] - Role(s) that may not answer it
- administratorRoles string|[string, string...] - Role(s) that administer it: they see it, may reassign it, move its deadline, fail it, or complete it — a completion by them is recorded as an administrator's
- administratorUsers string|[string, string...] - User id(s) that administer it, whatever their roles
- title string|() - Short summary shown in the inbox. Defaults to the task name
- description string|() - Additional context shown with the form or decision
- timeout Duration|() - Maximum time to wait. Omit to wait indefinitely
- anydata... -
- maxRetries? never - Never present; retries belong to
RetryBeforeReview
workflow: ReviewTimeoutDetail
Detail fields carried by a ReviewTimeoutError.
Fields
- taskName string - The review's qualified task name
- taskWorkflowId string - Child workflow ID of the review instance
- activityName string - The activity under review
- trigger string -
PRE_RUNfor an approval gate,ON_FAILUREfor a failed activity
- timedOutAfter string - Configured deadline as an ISO-8601 duration
- timedOutAt string - ISO-8601 timestamp at which the timeout was recorded
workflow: ToolDecl
An AI tool capability of a durable agent, with optional gating config. For the
no-config case pass the ai:ToolConfig/ai:BaseToolKit/@ai:AgentTool function
directly.
Fields
- tool BaseToolKit|ToolConfig|FunctionTool - The tool: an
@ai:AgentToolfunction, anai:ToolConfig, or a toolkit
- approvalPolicy ApprovalPolicy(default NoApproval) - Whether a person approves each call before it runs, and who
Errors
workflow: AgentBusyError
Returned by the non-blocking getResult/getDataResult reads when the agent
instance (or the specific turn) is still in progress — e.g. suspended on a human
task. Check back later, or use the blocking waitForResult/waitForDataResult
forms, which durably wait and are resumable across crashes.
workflow: HumanTaskError
Every failure awaitHumanTask can report: nobody acted in time
(HumanTaskTimeoutError), someone rejected the task (HumanTaskRejectedError), or the
task could not produce a result at all (HumanTaskFailedError).
workflow: HumanTaskFailedError
Returned by awaitHumanTask when the task neither completed nor closed with a reason
it can report — the task workflow failed, was terminated by an administrator, or the
submitted value did not match the expected result type.
workflow: HumanTaskRejectedError
Returned by awaitHumanTask when the task is rejected instead of completed. The reason and any
details submitted with the rejection are on the error detail, so a workflow can compensate on
what the rejecting user said.
workflow: HumanTaskTimeoutError
Returned by awaitHumanTask when no human acts within the configured deadline.
Catch the whole family with on fail workflow:HumanTaskError e and narrow with
if e is workflow:HumanTaskTimeoutError to run compensation logic for a timeout.
workflow: ReviewFailedError
Returned when a review ended without a usable decision — terminated, or its child failed.
workflow: ReviewRejectedError
Returned when a person rejects a review: a gated call is skipped, or a failed activity's failure stands. For a failed activity the original failure is the error's cause.
workflow: ReviewTaskError
Every failure a review can report.
workflow: ReviewTimeoutError
Returned when a review's deadline passes before a person decides. For a failed activity the original failure is the error's cause.
workflow: WorkflowBusyError
Returned by the non-blocking ctx->getChildWorkflowResult read when the child
workflow is still running (e.g. suspended on a human task). Check back later, or
use the blocking ctx->waitForChildWorkflow form, which durably suspends until
the child completes.
Union types
workflow: RetryPolicy
RetryPolicy
Failure behaviour of an activity call: fail at once (NoRetry), retry with backoff
(AutoRetry), raise a review (ReviewTaskDefinition), or retry then review
(RetryBeforeReview).
workflow: ApprovalPolicy
ApprovalPolicy
Whether a person must approve a call before it runs, and who. NoApproval runs the
call directly; a ReviewTaskDefinition raises a PRE_RUN review first.
Map types
workflow: JsonObject
JsonObject
Any JSON object.
Import
import ballerina/workflow;Metadata
Released date: 5 days ago
Version: 0.10.0
License: Apache-2.0
Compatibility
Platform: java21
Ballerina version: 2201.13.6
GraalVM compatible: No
Pull count
Total: 10910
Current verison: 418
Weekly downloads
Keywords
workflow
Type/Library
Contributors