Class: Loop<TBot, TCustomState>
Defined in: packages/core/src/patterns/loop.ts:158
Processable Interface
The fundamental interface that all Baleybots primitives and patterns implement. This is what enables universal composability - any Processable can be used anywhere a Baleybot is expected.
Example
// All of these implement Processable:
const bot: Processable = Baleybot.create({ ... });
const chain: Processable = Pipeline.from().chain([...]).build();
const loop: Processable = new Loop(bot, config);
// With type inference from Zod schema:
const bot = Baleybot.create({
finalResponseSchema: z.object({ sentiment: z.string() })
});
const result = await bot.process('text'); // result is typed as { sentiment: string }
// With input/output type constraints:
const typedBot: Processable<string, { sentiment: string }> = bot;
Type Parameters
TBot
TBot extends Processable<unknown, unknown> = Processable<unknown, unknown>
The input type of the process method (default: unknown for flexibility)
TCustomState
TCustomState extends Record<string, unknown> = Record<string, unknown>
The output type of the process method (or a Zod schema to infer from)
Implements
Processable<unknown, {result:InferBotOutput<TBot>;state:TCustomState; }>
Constructors
Constructor
new Loop<
TBot,TCustomState>(bot,config):Loop<TBot,TCustomState>
Defined in: packages/core/src/patterns/loop.ts:173
Parameters
bot
TBot
config
LoopConfig<InferBotOutput<TBot>, TCustomState>
Returns
Loop<TBot, TCustomState>
Methods
getBotNames()
getBotNames():
string[]
Defined in: packages/core/src/patterns/loop.ts:362
Get all bot names in this processable (including nested processables)
Returns
string[]
Array of bot names that can emit events
Example
const pipeline = pipeline()
.step(chatBot)
.parallel({ sentiment: sentimentBot, tone: toneBot })
.build();
const botNames = pipeline.getBotNames();
// ['assistant', 'sentiment', 'tone']
Implementation of
getHistory()
getHistory():
InferBotOutput<TBot>[]
Defined in: packages/core/src/patterns/loop.ts:310
Get the full history of results from all iterations
Returns
InferBotOutput<TBot>[]
getId()
getId():
string
Defined in: packages/core/src/patterns/loop.ts:348
Get the unique ID of this processable instance
Each processable has a unique ID that persists for its lifetime. IDs are useful for:
- Filtering specific instances in subscriptions
- Tracking performance metrics per instance
- Debugging nested compositions
Returns
string
Unique ID string
Example
const bot1 = Baleybot.create({ name: 'analyzer' });
const bot2 = Baleybot.create({ name: 'analyzer' });
bot1.getId(); // 'baleybot-1-a3f891'
bot2.getId(); // 'baleybot-2-b4c2d3'
// Same semantic name, different IDs
bot1.getName(); // 'analyzer'
bot2.getName(); // 'analyzer'
Implementation of
getIterationCount()
getIterationCount():
number
Defined in: packages/core/src/patterns/loop.ts:317
Get the number of iterations that were executed
Returns
number
getName()
getName():
string
Defined in: packages/core/src/patterns/loop.ts:324
Get the name of this loop pattern
Returns
string
getSchema()
getSchema():
unknown
Defined in: packages/core/src/patterns/loop.ts:331
Get the output schema from the underlying bot
Returns
unknown
process()
process(
input,options?):Promise<{result:InferBotOutput<TBot>;state:TCustomState; }>
Defined in: packages/core/src/patterns/loop.ts:187
Process input and return structured output
You can use BOTH streaming events AND final result together:
- Streaming events: Provide
options.onTokento receive real-time events during execution - Final result: Returned when Promise resolves (complete parsed response)
Both work simultaneously - streaming events fire in real-time, then final result is returned.
Parameters
input
unknown
Input data (typed for safety)
options?
Optional callbacks for streaming, completion, errors
Returns
Promise<{ result: InferBotOutput<TBot>; state: TCustomState; }>
Promise resolving to structured output (inferred from TOutput if it's a Zod schema)
Implementation of
subscribeToAll()
subscribeToAll(
options?):Subscription
Defined in: packages/core/src/patterns/loop.ts:352
Subscribe to categorized events from this processable and all nested processables
Provides fine-grained control over event handling with categorized callbacks:
- onStreamEvent: Real-time content streaming (text_delta, structured_output_delta, tool_call_arguments_delta)
- onProgressUpdate: Status updates (tool_execution_start, tool_call_stream_start, tool_call_stream_complete, reasoning)
- onComplete: Emitted when each bot finishes processing with final output
- onError: Error events (error, tool_validation_error)
Callbacks now receive both bot ID and name for precise identification:
- botId: Unique instance identifier (e.g., 'baleybot-1-a3f891')
- botName: Semantic name (e.g., 'analyzer', 'router', 'processor')
Parameters
options?
Optional categorized callbacks and bot filter
omit?
string[]
onComplete?
(botId, botName, output) => void
onError?
(botId, botName, event) => void
onProgressUpdate?
(botId, botName, event) => void
onStreamEvent?
(botId, botName, event) => void
Returns
Subscription object with unsubscribe method
Examples
const subscription = pipeline.subscribeToAll({
onStreamEvent(botId, botName, event) {
// Real-time streaming content
if (event.type === 'text_delta') {
console.log(`[${botId}] ${botName}:`, event.content);
}
},
onProgressUpdate(botId, botName, event) {
// Status updates
if (event.type === 'tool_execution_start') {
console.log(`${botName} (${botId}) executing ${event.toolName}`);
}
},
onComplete(botId, botName, output) {
// Bot finished processing
console.log(`${botName} completed:`, output);
},
onError(botId, botName, event) {
// Errors
console.error(`${botName} error:`, event);
}
});
await pipeline.process('input');
subscription.unsubscribe();
const subscription = pipeline.subscribeToAll({
onStreamEvent(botId, botName, event) {
console.log(`${botName}:`, event);
},
omit: ['chat-step', 'processor-42-8a3f91'] // Filter by name or ID
});