workflow

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2026 License: MIT Imports: 3 Imported by: 0

README

workflow

Composable building blocks for multi-step AI pipelines. Stdlib-only.

import "github.com/buckedunicorn/grok/workflow"

When to use

workflow is a higher-level alternative to runnable when every step in your pipeline is a string -> string transform (the common case for chat-completion chains). It trades the type-parameter generality of runnable for terser composition.

For mixed-type pipelines, prefer runnable.

Surface

Type / function Purpose
Step A function that transforms a string input into a string output
Chain(steps...) Sequential composition
Parallel(steps...) Concurrent execution; the combiner reduces results to a single string
Conditional(predicate, ifTrue, ifFalse) Branch on the input

Example

extract := workflow.Step(func(ctx context.Context, in string) (string, error) {
    comp, _ := client.Chat.Create(ctx, &chat.CreateRequest{
        Model:    "grok-4-1-fast-non-reasoning",
        Messages: []chat.Message{{Role: "user", Content: "Extract the main topic of: " + in}},
    })
    s, _ := comp.Choices[0].Message.Content.(string)
    return s, nil
})

summarise := workflow.Step(func(ctx context.Context, topic string) (string, error) {
    // ...
    return "summary of " + topic, nil
})

pipeline := workflow.Chain(extract, summarise)
out, err := pipeline.Run(ctx, longArticle)

Examples directory

Documentation

Overview

Package workflow provides composable building blocks for multi-step AI pipelines.

A Step is any function that transforms a string input into a string output. Steps compose via Chain (sequential), Parallel (concurrent), and Conditional.

result, err := workflow.Chain(ctx, userInput,
    llmSummarize,
    llmTranslate,
    workflow.Transform(strings.ToUpper),
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain(ctx context.Context, input string, steps ...Step) (string, error)

Chain runs steps sequentially, passing each output as the next step's input. Returns the output of the final step, or the first error encountered.

func Map

func Map(ctx context.Context, inputs []string, step Step) ([]string, error)

Map applies step to each element of inputs concurrently and returns results in the same order. All elements share a single errgroup; first error cancels all.

func Parallel

func Parallel(ctx context.Context, input string, steps ...Step) ([]string, error)

Parallel runs all steps concurrently with the same input. Returns all outputs in the same order as steps, or the first error that cancels all remaining steps.

Types

type Step

type Step func(ctx context.Context, input string) (string, error)

Step transforms a string input and returns a string output. It is the basic unit of a workflow pipeline.

func Conditional

func Conditional(condition func(string) bool, thenStep, elseStep Step) Step

Conditional returns a Step that calls thenStep when condition(input) is true, or elseStep otherwise. Pass nil for elseStep to pass input through unchanged.

func Retry

func Retry(maxAttempts int, step Step) Step

Retry wraps a Step, re-running it up to maxAttempts times on error.

func Transform

func Transform(fn func(string) string) Step

Transform wraps a pure string transformation as a Step.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL