Protocols
Protocols define the structure of reasoning flows. They specify the sequence of steps, conditions for branching, and how results are aggregated.
Protocol Definition
A protocol is a sequence of steps. Each step has a name, an action, and optional conditions. Steps can run sequentially, in parallel, or conditionally.
protocol.ts typescript
import { Protocol } from '@r-think/core';
const protocol = Protocol.define({
id: 'data-pipeline',
version: '1.0.0',
steps: [
{
name: 'validate',
action: validateInput,
description: 'Validate incoming data'
},
{
name: 'transform',
action: transformData,
dependsOn: ['validate']
},
{
name: 'enrich',
action: enrichData,
dependsOn: ['transform'],
condition: (ctx) => ctx.transform.success
},
{
name: 'emit',
action: emitResult,
dependsOn: ['transform', 'enrich']
}
],
retry: {
maxAttempts: 3,
backoff: 'exponential',
retryable: ['timeout', 'network']
}
}); Protocol Types
R-Think provides three built-in protocol types:
- Sequential - Steps run one after another. Use for ordered dependencies.
- Parallel - Independent steps run concurrently. Use for performance optimization.
- Conditional - Steps are selected based on runtime conditions. Use for branching logic.
Step Actions
Each step has an action function. The action receives the current context and returns a result. The result becomes available to downstream steps.
step.ts typescript
const step = {
name: 'assess',
action: async (ctx) => {
const score = await calculateScore(ctx.input);
return { score, threshold: 70 };
},
onSuccess: (result) => console.log('Step passed'),
onFailure: (error) => console.log('Step failed')
};