ReasonerContext

Context object passed to reasoner functions with AI, memory, and workflow access

The ReasonerContext is passed to every reasoner function, providing access to AI capabilities, memory, workflow tracking, and execution metadata.

Basic Usage

agent.reasoner('analyze', async (ctx) => {
  // Access input data
  const { text } = ctx.input;

  // Use AI with the context
  const analysis = await ctx.ai(`Analyze: ${text}`);

  // Access memory
  await ctx.memory.set('last_analysis', analysis);

  // Call other agents
  const enriched = await ctx.call('enrichment-agent.enrich', { data: analysis });

  return { analysis, enriched };
});

Properties

Input & Metadata

Prop

Type

DID Properties

Prop

Type

Service Access

Prop

Type

Methods

ctx.ai()

Call the LLM with optional structured output.

// Simple text generation
const response = await ctx.ai('Explain quantum computing');

// With system prompt
const response = await ctx.ai('Analyze this text', {
  system: 'You are a sentiment analyzer.'
});

// Structured output with Zod
import { z } from 'zod';

const SentimentSchema = z.object({
  sentiment: z.enum(['positive', 'negative', 'neutral']),
  confidence: z.number(),
  keywords: z.array(z.string())
});

const result = await ctx.ai('I love this product!', {
  schema: SentimentSchema
});
// result is typed as { sentiment, confidence, keywords }

Options:

Prop

Type

ctx.aiStream()

Stream responses from the LLM.

const stream = await ctx.aiStream('Write a long story');

for await (const chunk of stream) {
  process.stdout.write(chunk);
}

ctx.call()

Execute a reasoner or skill on another agent.

// Call another agent's reasoner
const result = await ctx.call('sentiment-agent.analyze', {
  text: 'Great product!'
});

// Call a skill
const formatted = await ctx.call('formatter-agent.format_json', {
  data: result
});

Cross-agent calls automatically propagate execution context, building workflow DAGs.

ctx.discover()

Discover capabilities of other agents.

const discovery = await ctx.discover({
  tags: ['analysis'],
  includeDescriptions: true
});

for (const agent of discovery.json?.capabilities || []) {
  console.log(`${agent.agentId}: ${agent.reasoners.length} reasoners`);
}

Complete Example

import { z } from 'zod';

const TicketAnalysis = z.object({
  category: z.string(),
  priority: z.enum(['low', 'medium', 'high', 'critical']),
  sentiment: z.string(),
  suggestedAction: z.string()
});

agent.reasoner('analyze_ticket', async (ctx) => {
  const { ticketId, content } = ctx.input;

  // Track progress
  await ctx.workflow.progress(10, { status: 'analyzing' });

  // Get customer history from memory
  const history = await ctx.memory.session(ticketId).get('history') || [];

  // Analyze with AI
  const analysis = await ctx.ai(
    `Analyze this support ticket:\n${content}\n\nCustomer history: ${JSON.stringify(history)}`,
    {
      system: 'You are a support ticket analyzer.',
      schema: TicketAnalysis
    }
  );

  await ctx.workflow.progress(50, { status: 'enriching' });

  // Enrich with external data
  const enriched = await ctx.call('data-agent.get_customer_info', {
    ticketId
  });

  // Store analysis
  await ctx.memory.set(`ticket_${ticketId}_analysis`, {
    ...analysis,
    enrichedData: enriched,
    analyzedAt: new Date().toISOString()
  });

  // Generate credential for audit trail
  await ctx.did.generateCredential({
    inputData: { ticketId },
    outputData: analysis
  });

  await ctx.workflow.progress(100, { status: 'completed' });

  return {
    ticketId,
    analysis,
    executionId: ctx.executionId
  };
});