Skip to content

Quick Start ​

One agent, memory repositories, the console channel and one manually dispatched run that answers where it came from. This is the same code as the example in the @chamvi/core README, which the test suite runs against a scripted model.

ts
import type { LanguageModel } from 'ai';
import { z } from 'zod';
import {
    AgentRevisionService,
    ArtifactService,
    CheckpointService,
    EffectService,
    Engine,
    IdempotencyService,
    LockService,
    RunService,
    StateService,
    StepService,
    consoleChannel,
    defineEffectTool,
    defineModelAgent,
    systemClock,
} from '@chamvi/core';
import {
    MemoryAgentRevisionRepository,
    MemoryArtifactRepository,
    MemoryCheckpointRepository,
    MemoryEffectRepository,
    MemoryIdempotencyRepository,
    MemoryLockRepository,
    MemoryRegistry,
    MemoryRunRepository,
    MemoryStateDocumentRepository,
    MemoryStateRepository,
    MemoryStepRepository,
} from '@chamvi/store-memory';

export async function triageIssue(model: LanguageModel) {
    const labels: string[] = [];

    const triage = defineModelAgent({
        name: 'triage',
        model,
        instructions: 'Label the issue in the trigger payload as bug, feature or question.',
        budget: { maxSteps: 5 },
        tools: {
            // the result is recorded per (run, tool, input): a resumed run reuses it
            addLabel: defineEffectTool({
                description: 'Adds a label to the issue.',
                inputSchema: z.object({ label: z.enum(['bug', 'feature', 'question']) }),
                execute: async ({ label }, { context }) => {
                    labels.push(label);
                    // answers where the run came from (here: the console), once per message
                    await context.channels.findOne()?.emit({ body: `Labelled as ${label}.` });

                    return { added: label };
                },
            }),
        },
    });

    // runs, steps, checkpoints, effects, artifacts and state in memory, gone with the process: @chamvi/app keeps them in PostgreSQL
    const registry = new MemoryRegistry();
    const runs = new MemoryRunRepository({ registry });
    const locks = new MemoryLockRepository({ registry });
    const engine = new Engine({
        runService: new RunService({ repository: runs, clock: systemClock }),
        stepService: new StepService({ repository: new MemoryStepRepository({ registry }) }),
        checkpointService: new CheckpointService({ repository: new MemoryCheckpointRepository({ registry }) }),
        effectService: new EffectService({ repository: new MemoryEffectRepository({ registry }) }),
        agentRevisionService: new AgentRevisionService({ repository: new MemoryAgentRevisionRepository({ registry }) }),
        artifactService: new ArtifactService({ repository: new MemoryArtifactRepository({ registry }) }),
        stateService: new StateService({
            repository: new MemoryStateRepository({ registry }),
            documentRepository: new MemoryStateDocumentRepository({ registry }),
        }),
        lockService: new LockService({ repository: locks, clock: systemClock }),
        idempotencyService: new IdempotencyService({ repository: new MemoryIdempotencyRepository({ registry }), clock: systemClock }),
        agents: [triage],
        // logs every message a tool sends through it
        channels: [consoleChannel()],
    });

    await engine.start();

    const { runId } = await engine.dispatch({
        agent: 'triage',
        trigger: 'manual',
        channel: 'console',
        // where the run came from on that channel; ctx.channels.findOne() answers there
        address: 'issue-1',
        payload: { title: 'App crashes on start' },
    });

    await engine.idle();
    await engine.stop();

    return { run: await engine.getRun(runId), labels };
}

Pass any AI SDK LanguageModel, for example anthropic(...) from @ai-sdk/anthropic. The function resolves with the settled run and the labels the tool added; the console channel logged Labelled as bug. once.

What happened ​

  1. defineModelAgent() checked the name and filled in the default budget. The tools factory runs once per execution and returns an AI SDK ToolSet.
  2. new Engine() validated the setup: unique agent and channel names, subscriptions to known channels.
  3. engine.start() recovered abandoned runs (there were none), started the channels and started polling.
  4. engine.dispatch() stored a queued run. The envelope (channel, subscription id and payload) became the run's first user message; address is where the run came from on that channel.
  5. The engine claimed the run, set it running and drove an AI SDK ToolLoopAgent. After every step it appended the steps and saved a checkpoint to the store.
  6. The model called addLabel. Because it is an effect tool, its result was recorded under an effect key, so a resumed run would reuse it instead of labelling twice. Inside it, context.channels.findOne() answered on the run's channel and address; that emit is recorded too, so it is sent once.
  7. The model answered with text, and the run settled completed. Its output is the text of the last answer (an agent with structured output gets the parsed value instead).
  8. engine.idle() resolved once nothing was running, and await engine.stop() stopped the poll and the channels.

Where to go next ​