Skip to main content

Workflows

Toolpack SDK supports two workflow types for AI execution: Direct and Planned.

Direct Workflow (Default)

In direct mode, the AI responds immediately and executes tools as needed in a single flow:

const toolpack = await Toolpack.init({
provider: 'openai',
tools: true,
defaultMode: 'chat', // Chat mode uses direct workflow
});

// AI responds directly, executing tools inline
const stream = toolpack.stream({
messages: [{ role: 'user', content: 'What is 2 + 2?' }],
model: 'gpt-4o',
});

Planned Workflow (Agent Mode)

In planned mode, the AI first creates a plan, then executes it step by step:

const toolpack = await Toolpack.init({
provider: 'openai',
tools: true,
defaultMode: 'agent', // Agent mode uses planned workflow
});

// AI creates a plan, then executes each step
const stream = toolpack.stream({
messages: [{ role: 'user', content: 'Refactor the utils.ts file to use async/await' }],
model: 'gpt-4o',
});

for await (const chunk of stream) {
// Chunks include step information
if (chunk.workflowStep) {
console.log(`Step ${chunk.workflowStep.number}: ${chunk.workflowStep.description}`);
}
process.stdout.write(chunk.delta);
}

Workflow Events

Listen to workflow events for progress tracking:

const toolpack = await Toolpack.init({
provider: 'openai',
tools: true,
defaultMode: 'agent',
});

// Plan created
toolpack.on('workflow:plan_created', (plan) => {
console.log('Plan created:', plan.steps.map(s => s.description));
});

// Execution started
toolpack.on('workflow:started', (plan) => {
console.log('Execution started');
});

// Step events
toolpack.on('workflow:step_start', (step, plan) => {
console.log(`Starting: ${step.description}`);
});

toolpack.on('workflow:step_complete', (step, plan) => {
console.log(`Completed: ${step.description}`);
});

toolpack.on('workflow:step_failed', (step, error, plan) => {
console.log(`Failed: ${step.description}`, error.message);
});

// Progress updates
toolpack.on('workflow:progress', (progress) => {
console.log(`Progress: ${progress.percentage}% - ${progress.currentStepDescription}`);
});

// Completion
toolpack.on('workflow:completed', (plan, result) => {
console.log('Workflow completed:', result.metrics);
});

toolpack.on('workflow:failed', (plan, error) => {
console.log('Workflow failed:', error.message);
});

Workflow Configuration

Configure workflow behavior in custom modes:

const customMode: ModeConfig = {
name: 'careful-agent',
displayName: 'Careful Agent',
description: 'Plans carefully with approval',
systemPrompt: '...',
allowedToolCategories: [],
blockedToolCategories: [],
allowedTools: [],
blockedTools: [],
blockAllTools: false,

workflow: {
planning: {
enabled: true,
requireApproval: true, // Pause for user approval
maxSteps: 10,
},
steps: {
enabled: true,
retryOnFailure: true,
maxRetries: 3,
allowDynamicSteps: true,
maxTotalSteps: 20,
},
progress: {
enabled: true,
reportPercentage: true,
},
onFailure: {
strategy: 'ask_user', // 'abort' | 'skip' | 'ask_user' | 'try_alternative'
},
},
};

Planning Options

OptionTypeDefaultDescription
enabledbooleanfalseEnable planning phase
requireApprovalbooleanfalsePause for user approval before executing
planningPromptstring-Custom system prompt for plan generation
maxStepsnumber20Maximum steps allowed in a plan

Step Options

OptionTypeDefaultDescription
enabledbooleanfalseEnable step-by-step execution
retryOnFailurebooleantrueRetry failed steps
maxRetriesnumber3Maximum retry attempts per step
allowDynamicStepsbooleantrueAllow adding steps during execution
maxTotalStepsnumber50Maximum total steps including dynamic

Progress Options

OptionTypeDefaultDescription
enabledbooleantrueEmit progress events
reportPercentagebooleantrueInclude completion percentage

Failure Strategies

StrategyDescription
abortStop execution immediately
skipSkip the failed step and continue
ask_userPause and wait for user decision
try_alternativeLet AI try a different approach

Accessing the Workflow Executor

For advanced control:

const executor = toolpack.getWorkflowExecutor();

// Configure at runtime
executor.setConfig({
planning: { enabled: true },
steps: { enabled: true, maxRetries: 5 },
});