Synaptic.Workflow (synaptic v0.3.0-alpha.13)

View Source

DSL entry point for defining Synaptic workflows.

Summary

Functions

Declares an asynchronous fire-and-forget workflow step. The block receives the accumulated context and executes like a normal step, but the workflow immediately continues to the next step instead of waiting for this one to finish. Results are merged back into the context once the step completes.

Marks that the workflow definition has declared its terminal point. In the MVP the macro only exists to nudge authors so that tests reflect the full lifecycle.

Produces the compiled workflow definition for the provided module.

Routes with a native Jev Choice over {description, target_step} branches. The block supplies state, just as with llm_router/4. At least two branches are required. Include an explicit catch-all branch when appropriate.

Declares an LLM-driven routing step. The block receives the accumulated context and returns a map or string that becomes the prompt input used to choose the next step from the provided branches.

Declares a parallel workflow step. The block must return a list of anonymous functions that receive the workflow context (map). Each function runs concurrently and must return {:ok, map} or {:error, term}. The step succeeds only when all parallel tasks succeed, and their maps are merged into the accumulated context.

Marks a code block as a side effect (e.g., database mutations, external API calls). When skip_side_effects: true is set in test configuration, the side effect is skipped and returns a default value instead.

Declares a ordered workflow step. The block receives context (map) accumulated from every previous step and must return {:ok, map}, {:error, term}, or suspend_for_human/2.

Convenience helper returned from steps to pause execution and wait for a human payload to resume the workflow.

Functions

async_step(name, opts \\ [], list)

(macro)

Declares an asynchronous fire-and-forget workflow step. The block receives the accumulated context and executes like a normal step, but the workflow immediately continues to the next step instead of waiting for this one to finish. Results are merged back into the context once the step completes.

commit()

(macro)

Marks that the workflow definition has declared its terminal point. In the MVP the macro only exists to nudge authors so that tests reflect the full lifecycle.

definition(module)

@spec definition(
  atom()
  | %{:__synaptic_definition__ => any(), optional(any()) => any()}
) :: any()

Produces the compiled workflow definition for the provided module.

jev_router(name, branches, opts \\ [], list)

(macro)

Routes with a native Jev Choice over {description, target_step} branches. The block supplies state, just as with llm_router/4. At least two branches are required. Include an explicit catch-all branch when appropriate.

:min_confidence defaults to 0.6 and must be tuned on your data. Below it, :fallback selects a declared step (for example a human-review step). Without a fallback the step returns {:error, {:jev_low_confidence, result}}. Transport errors remain errors; they do not select the uncertainty fallback.

The full Synaptic.Jev.Result is stored under :result_key (default :jev_result), including the selected target and fallback decision in result.metadata.routing. :prompt overrides the Choice instructions. Other client options are documented in Synaptic.Jev.

llm_router(name, branches, opts \\ [], list)

(macro)

Declares an LLM-driven routing step. The block receives the accumulated context and returns a map or string that becomes the prompt input used to choose the next step from the provided branches.

parallel_step(name, opts \\ [], list)

(macro)

Declares a parallel workflow step. The block must return a list of anonymous functions that receive the workflow context (map). Each function runs concurrently and must return {:ok, map} or {:error, term}. The step succeeds only when all parallel tasks succeed, and their maps are merged into the accumulated context.

side_effect(opts \\ [], list)

(macro)

Marks a code block as a side effect (e.g., database mutations, external API calls). When skip_side_effects: true is set in test configuration, the side effect is skipped and returns a default value instead.

Options

  • :default - Value to return when side effect is skipped (default: :ok)
  • :name - Optional identifier for the side effect, used in Telemetry metadata

Examples

step :save_user do
  side_effect do
    Database.insert(context.user)
  end

  {:ok, %{user_saved: true}}
end

step :send_email do
  side_effect default: {:ok, :sent}, name: :welcome_email do
    EmailService.send(context.user.email, "Welcome!")
  end

  {:ok, %{email_sent: true}}
end

step(name, opts \\ [], list)

(macro)

Declares a ordered workflow step. The block receives context (map) accumulated from every previous step and must return {:ok, map}, {:error, term}, or suspend_for_human/2.

suspend_for_human(message, metadata \\ %{}, context_updates \\ %{})

Convenience helper returned from steps to pause execution and wait for a human payload to resume the workflow.