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
defineModelAgent()checked the name and filled in the default budget. Thetoolsfactory runs once per execution and returns an AI SDKToolSet.new Engine()validated the setup: unique agent and channel names, subscriptions to known channels.engine.start()recovered abandoned runs (there were none), started the channels and started polling.engine.dispatch()stored aqueuedrun. The envelope (channel, subscription id and payload) became the run's first user message;addressis where the run came from on that channel.- The engine claimed the run, set it
runningand drove an AI SDKToolLoopAgent. After every step it appended the steps and saved a checkpoint to the store. - 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. - The model answered with text, and the run settled
completed. Itsoutputis the text of the last answer (an agent with structured output gets the parsed value instead). engine.idle()resolved once nothing was running, andawait engine.stop()stopped the poll and the channels.
Where to go next
- Your First Agent: the same agent as a folder on
@chamvi/app, run withchamvi dev, checked withchamvi start --check - Replace the memory repositories with durable ones before you rely on restarts. See Stores and repositories.
- Add a budget, approvals or state.
- Shut down with
drain()instead ofstop()in a real process.