@flow-state-dev/patterns/routed-specialists
Interfaces
RoutedSpecialistsConfig
Type Parameters
TOutputSchema
TOutputSchema extends ZodTypeAny = ZodTypeAny
Properties
collectionId?
collectionId?: string;
Override the TaskCollection id. Defaults to name. Useful when two
pattern instances share a flow and you want stable, predictable ids.
context?
context?: GeneratorSlot<any, any>;
Context slot for the default controller and synthesizer.
controller?
controller?: BlockDefinition<any, any, any, any>;
Controller block — reads workspace, returns
{ specialist, done, reasoning } (controllerOutputSchema).
Default: an LLM generator that knows about the registered
specialists and the iteration history (read from the collection).
controllerVisibility?
controllerVisibility?: ItemVisibility;
Item visibility for the default controller. Default: { client: true, history: false }.
initialState?
initialState?:
| Record<string, unknown>
| ((input) =>
| Record<string, unknown>
| Promise<Record<string, unknown>>);
Initial workspace state — static value or a function of the pattern
input. Applied via ctx.resources.workspace.setState once at start.
instructions?
instructions?: InstructionsSlot;
Overall instructions injected into the default controller and
synthesizer prompts. Ignored when controller or synthesizer
are overridden.
maxHistory?
maxHistory?: number;
Soft cap on the history window the default controller sees. When the
collection has more than maxHistory completed iterations, the
controller prompt only shows the most-recent N. The full collection
is preserved.
maxIterations?
maxIterations?: number;
Hard cap on controller iterations before the loop terminates. Default: 10.
model?
model?: string;
Model id for the default controller and synthesizer. Default: "openai/gpt-6-luna".
name
name: string;
Pattern instance name. Used as block-name prefix and the TaskCollection id.
outputSchema?
outputSchema?: TOutputSchema;
Output schema for the default synthesizer.
specialists
specialists: Record<string, BlockDefinition<any, any>>;
Specialist blocks keyed by name. Must have at least one entry. Each
specialist should declare resources: { workspace } to read/write the
shared resource; the dispatcher routes by task.assignee matching.
synthesizer?
synthesizer?: false | BlockDefinition<any, any, any, any>;
Final synthesis block — receives { workspace, iterations, history }
and returns the pattern's output. Pass false to skip synthesis and
return the raw { workspace, iterations, history } object.
synthesizerVisibility?
synthesizerVisibility?: ItemVisibility;
Item visibility for the default synthesizer. Default: { client: true, history: true }.
uses?
uses?: UsesSlot;
Capabilities to install on the default blocks (controller, synthesizer).
workspace
workspace: DefinedResource;
Shared workspace resource — created via createWorkspace(stateSchema).
Specialists read state via ctx.resources.workspace.state and contribute
by patching it.
Type Aliases
ControllerOutput
type ControllerOutput = z.infer<typeof controllerOutputSchema>;
RoutedSpecialistsControlState
type RoutedSpecialistsControlState = Omit<z.infer<typeof routedSpecialistsControlSchema>, "currentClaim"> & object;
Type Declaration
currentClaim?
currentClaim?: TaskClaimTicket;
Variables
controllerOutputSchema
const controllerOutputSchema: z.ZodObject<{
done: z.ZodBoolean;
reasoning: z.ZodString;
specialist: z.ZodNullable<z.ZodString>;
}, "strip", ZodTypeAny, {
done: boolean;
reasoning: string;
specialist: string | null;
}, {
done: boolean;
reasoning: string;
specialist: string | null;
}>;
Controller decision shape — what the controller block must return.
specialist names a registered specialist when done === false; it
is null when done === true.
routedSpecialistsControlSchema
const routedSpecialistsControlSchema: z.ZodObject<{
currentAttempt: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
currentClaim: z.ZodOptional<z.ZodObject<{
assignee: z.ZodOptional<z.ZodString>;
attempt: z.ZodNumber;
collectionId: z.ZodString;
createdAt: z.ZodNumber;
createdBy: z.ZodOptional<z.ZodString>;
followUpOf: z.ZodOptional<z.ZodString>;
incarnationId: z.ZodOptional<z.ZodString>;
partition: z.ZodOptional<z.ZodString>;
taskId: z.ZodString;
turn: z.ZodNumber;
}, "strip", ZodTypeAny, {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
}, {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
}>>;
currentSpecialist: z.ZodOptional<z.ZodString>;
currentTaskId: z.ZodOptional<z.ZodString>;
done: z.ZodDefault<z.ZodBoolean>;
iteration: z.ZodDefault<z.ZodNumber>;
tasks: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
}, "strip", ZodTypeAny, {
currentAttempt?: number | null;
currentClaim?: {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
};
currentSpecialist?: string;
currentTaskId?: string;
done: boolean;
iteration: number;
tasks: Record<string, unknown>;
}, {
currentAttempt?: number | null;
currentClaim?: {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
};
currentSpecialist?: string;
currentTaskId?: string;
done?: boolean;
iteration?: number;
tasks?: Record<string, unknown>;
}>;
Sequencer control state for a routedSpecialists instance.
tasks is the TaskCollection storage slot (per-iteration Task
records). iteration, currentSpecialist, and done are loop-
control flags written by the controller-record step.
currentClaim is the ownership ticket the record-iteration step's own
claim produced, carried so recordCompletion and recordError
can scope their write-back to that claim. It must be minted from the
claim's return value, not from createTask's: the claim is what
increments attempts, so the pre-claim task reads 0 where the
claimed one reads 1, and a ticket built from the wrong one would
decline every normal completion.
Optional, so a run resumed from a checkpoint taken before this field existed still completes its tasks.
currentAttempt is that earlier checkpoint's shape, read, never
set. Before the ticket, the step stored the claim's
attempts here and the write-back fenced on it. A legacy record has no
ticket and cannot rebuild one from its own state — the ticket also needs
the task's createdAt — so the write-back reads that one field off the
row and fences on the stored attempt instead (writeBackGuard in
index.ts). Its identity arm is vacuous, as the old guard never asked;
the attempt, status and lease arms are enforced. Declared so a parse
through this schema keeps it rather than stripping it; cleared by the
next iteration, which writes a real ticket.
Functions
createCheckLoop()
function createCheckLoop(name): BlockDefinition<ZodAny, ZodObject<{
continue: ZodBoolean;
}, "strip", ZodTypeAny, {
continue: boolean;
}, {
continue: boolean;
}>, any, {
continue: boolean;
}>;
Parameters
name
string
Returns
BlockDefinition<ZodAny, ZodObject<{
continue: ZodBoolean;
}, "strip", ZodTypeAny, {
continue: boolean;
}, {
continue: boolean;
}>, any, {
continue: boolean;
}>
createDispatchSpecialist()
function createDispatchSpecialist(name, specialists): BlockDefinition<ZodObject<{
done: ZodBoolean;
reasoning: ZodString;
specialist: ZodNullable<ZodString>;
}, "strip", ZodTypeAny, {
done: boolean;
reasoning: string;
specialist: string | null;
}, {
done: boolean;
reasoning: string;
specialist: string | null;
}>, ZodAny, {
done: boolean;
reasoning: string;
specialist: string | null;
}, any>;
Parameters
name
string
specialists
Record<string, BlockDefinition<any, any>>
Returns
BlockDefinition<ZodObject<{
done: ZodBoolean;
reasoning: ZodString;
specialist: ZodNullable<ZodString>;
}, "strip", ZodTypeAny, {
done: boolean;
reasoning: string;
specialist: string | null;
}, {
done: boolean;
reasoning: string;
specialist: string | null;
}>, ZodAny, {
done: boolean;
reasoning: string;
specialist: string | null;
}, any>
routedSpecialists()
function routedSpecialists<TOutputSchema>(config): SequencerDefinition<any, any, ZodObject<{
currentAttempt: ZodOptional<ZodNullable<ZodNumber>>;
currentClaim: ZodOptional<ZodObject<{
assignee: ZodOptional<ZodString>;
attempt: ZodNumber;
collectionId: ZodString;
createdAt: ZodNumber;
createdBy: ZodOptional<ZodString>;
followUpOf: ZodOptional<ZodString>;
incarnationId: ZodOptional<ZodString>;
partition: ZodOptional<ZodString>;
taskId: ZodString;
turn: ZodNumber;
}, "strip", ZodTypeAny, {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
}, {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
}>>;
currentSpecialist: ZodOptional<ZodString>;
currentTaskId: ZodOptional<ZodString>;
done: ZodDefault<ZodBoolean>;
iteration: ZodDefault<ZodNumber>;
tasks: ZodDefault<ZodRecord<ZodString, ZodUnknown>>;
}, "strip", ZodTypeAny, {
currentAttempt?: number | null;
currentClaim?: {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
};
currentSpecialist?: string;
currentTaskId?: string;
done: boolean;
iteration: number;
tasks: Record<string, unknown>;
}, {
currentAttempt?: number | null;
currentClaim?: {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
};
currentSpecialist?: string;
currentTaskId?: string;
done?: boolean;
iteration?: number;
tasks?: Record<string, unknown>;
}>, undefined>;
Creates a routedSpecialists block — a sequencer that loops:
controller picks specialist, specialist contributes to workspace,
controller picks again, repeat until convergence (or maxIterations).
Per-iteration records are stored in a sequencer-backed TaskCollection
so renderings (<Plan />, devtool) and post-hoc queries see the
decision sequence as first-class data.
Type Parameters
TOutputSchema
TOutputSchema extends ZodTypeAny = ZodTypeAny
Parameters
config
RoutedSpecialistsConfig<TOutputSchema>
Returns
SequencerDefinition<any, any, ZodObject<{
currentAttempt: ZodOptional<ZodNullable<ZodNumber>>;
currentClaim: ZodOptional<ZodObject<{
assignee: ZodOptional<ZodString>;
attempt: ZodNumber;
collectionId: ZodString;
createdAt: ZodNumber;
createdBy: ZodOptional<ZodString>;
followUpOf: ZodOptional<ZodString>;
incarnationId: ZodOptional<ZodString>;
partition: ZodOptional<ZodString>;
taskId: ZodString;
turn: ZodNumber;
}, "strip", ZodTypeAny, {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
}, {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
}>>;
currentSpecialist: ZodOptional<ZodString>;
currentTaskId: ZodOptional<ZodString>;
done: ZodDefault<ZodBoolean>;
iteration: ZodDefault<ZodNumber>;
tasks: ZodDefault<ZodRecord<ZodString, ZodUnknown>>;
}, "strip", ZodTypeAny, {
currentAttempt?: number | null;
currentClaim?: {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
};
currentSpecialist?: string;
currentTaskId?: string;
done: boolean;
iteration: number;
tasks: Record<string, unknown>;
}, {
currentAttempt?: number | null;
currentClaim?: {
assignee?: string;
attempt: number;
collectionId: string;
createdAt: number;
createdBy?: string;
followUpOf?: string;
incarnationId?: string;
partition?: string;
taskId: string;
turn: number;
};
currentSpecialist?: string;
currentTaskId?: string;
done?: boolean;
iteration?: number;
tasks?: Record<string, unknown>;
}>, undefined>
References
createWorkspace
Re-exports createWorkspace