Skip to content

Components

A component is a class that does work. A decorator marks it, the container or a run builds it, and other classes take it through a field. A program does not build a component with new.

Here @Tool marks the class, and the @Use field takes another component:

@Tool({
name: "read_lines",
description: "Returns up to 200 lines of the file, starting at line `from`. The first line is 1.",
input: readLinesInput,
})
export class ReadLinesTool {
@Use(TextFile)
private readonly file!: TextFile;
public async execute(input: ReadLinesInput): Promise<string> {
const lines = await this.file.lines(input.from, input.count);
if (lines.length === 0) throw new ToolError(`the file has no line ${input.from}`);
return lines.join("\n");
}
}
Kind What it is
Capability Reaches outside the program: files, the network, another program. One that starts a program extends ProcessCapability; see Starting programs.
Service Logic with no outside reach, one instance for the program. It is a plain @asenajs/ioc component.
Run service Logic or state that belongs to one run.
Tool Something a model can call.
Harness A model with tools on one request, with a typed final answer.
Context The text a harness sends to the model. It is no component: the harness builds it with new.
Flow step One step of a workflow. It reads the state so far and adds fields to it.
Workflow A chain of flow steps over one state. With a command in its spec it is a command of a CLI.
CLI A command-line program whose commands are workflows.

How each one is written:

Kind Marked with Extends Registered as
Capability @Capability({ name, description, permission?, scope? }) CapabilityBase capability:<name>
Service @Service(name) from @asenajs/ioc nothing the name
Run service @RunService(name) nothing the name
Tool @Tool({ name, description, input }) nothing tool:<name>
Harness @Harness({ name, tools, permissions }) HarnessBase harness:<name>
Context @Context({ ...limits }) ContextBase not registered
Flow step @FlowStep({ name, description }) FlowStepBase flow-step:<name>
Workflow @Workflow({ name, description, steps, command? }) WorkflowBase workflow:<name>
CLI @CLI({ name, version, commands }) CliBase cli:<name>
  • In @Capability, permission is the word a run must be given before a tool gets the capability, and scope: "run" means it is built once per run.
  • A capability that extends another capability must declare the same permission as its base class. Another one throws when the class is defined.
  • Tools, harnesses and contexts are on their own page, and so are flow steps, workflows and the CLI.

Every component has one of two scopes.

Scope Who builds it How many Kinds
Program the container one for the program service, capability without scope, tool, harness, flow step, workflow, CLI
Run a run, the first time something in the run needs it one per run capability with scope: "run", run service

A component of the program serves every run, also runs that go on at the same time. So it keeps nothing of one run in its fields. What belongs to one run goes into the workflow state or into a run component.

Choose scope: "run" for a capability when:

  • it keeps state between calls, such as an open stream or what it already read, or
  • it reads a value of the run with @RunValue.

A class takes another through a field. There are three decorators for it.

Decorator From What the field gets
@Inject(Class) @asenajs/ioc A component of the program. @Inject("name") gives what is registered under a name.
@Use(Class) the kit A capability or a run component, in the run that is going on.
@RunValue(key) the kit For a run component: a value of its run. The values are the fields of the workflow’s input.

Two fields of a flow step:

@Inject(AnswerHarness)
private readonly harness!: AnswerHarness;
@Use(LineCount)
private readonly lineCount!: LineCount;

A field of a capability with scope: "run":

@RunValue("path")
private readonly path!: string;

All three are written the same way: readonly, without an initializer, declared with !. The type is the class, or for @RunValue the type of the value.

It depends on both sides.

The class that takes takes a component of the program with takes a run component with
Flow step @Inject @Use
Service, harness, workflow, CLI, capability without scope @Inject cannot
Run component @Inject; a capability without scope also with @Use @Use
Tool @Use, for a capability that declares a permission @Use, for a capability that declares a permission

The rules behind the table:

  • A run component is never taken with @Inject and never resolved from the container. Both throw, with an error that says to take it with @Use. A component of the program that injects one makes HarnessApplication.create throw.
  • Only a tool, a flow step or a run component declares @Use. A @Use field reads only while the run is on: inside a tool call, a flow step’s execute, or a run component of the run. Read anywhere else, it throws and names the class and the field.
  • A tool that a harness lists takes only classes marked with @Capability, and only capabilities that declare a permission. Anything else stops the start. A tool that needs a run service reaches it through a capability with scope: "run" that takes it.
  • No cycles. Run components that take each other with @Use in a cycle are an error that names the classes.
  • The type of a @RunValue field is not checked against the workflow’s input. Keep the two in step by hand.
  • An injected field is not there yet inside the constructor. Work that needs one belongs in a method marked with @OnStart() from @asenajs/ioc.

A value the container cannot build is registered by name: the environment, the working directory, a clock.

  1. Give it to HarnessApplication.create under instances:

    const app = await HarnessApplication.create({
    components: [NotesCli, SummarizeCommand, ModelStep, SummarizeStep, PrintStep, SummaryHarness, ReadLinesTool, TextFile],
    instances: { "notes:env": process.env },
    });
  2. Inject it with the name:

    @Inject("notes:env")
    private readonly env!: Env;

"notes:env" is a name the program chose, and Env is the program’s own type, Readonly<Record<string, string | undefined>>.

Any value except null and undefined can be an instance. The container never starts or stops it.

The kit has two values of its own. A program gives them as options of HarnessApplication.create, never under instances.

Name Type Option What it is
KitNames.IO KitInstances[KitNames.IO], an Io io Where the CLI writes: out(line) and err(line).
KitNames.PERMISSION_GROUPS KitInstances[KitNames.PERMISSION_GROUPS], a PermissionGroupSet permissionGroups The permission groups of the program.

Print through the io, so a test can capture the lines:

@Inject(KitNames.IO)
private readonly io!: KitInstances[KitNames.IO];

HarnessApplication is a program on the kit: an @asenajs/ioc container that the kit sets up.

answer-app.ts

import { HarnessApplication, PermissionGroupSet } from "@kerpetensh/harness-kit";
import { AnswerHarness } from "./AnswerHarness";
import { AnswerStep } from "./AnswerStep";
import { AnswerWorkflow } from "./AnswerWorkflow";
import { LineCount } from "./LineCount";
import { LookupTool } from "./LookupTool";
import { TextFile } from "./TextFile";
import { WebSearch } from "./WebSearch";
export function createApp(): Promise<HarnessApplication> {
return HarnessApplication.create({
components: [TextFile, WebSearch, LineCount, LookupTool, AnswerHarness, AnswerStep, AnswerWorkflow],
permissionGroups: new PermissionGroupSet({
offline: ["files:read"],
online: ["files:read", "web:search"],
}),
});
}

The options of HarnessApplication.create:

Option What it is
components The program’s own classes. Every marked class the program uses is listed here. An empty list throws.
io Where the CLI writes. Left out, the standard output and the standard error of the process.
permissionGroups The program’s permission groups. Left out, there is only the built-in group all, which gives every permission.
instances The named values, by the name they are injected under.
overrides Replaces what is registered under a name, as a test double does.
logger The logger of the container.

What an application does:

Member What it does
HarnessApplication.create(options) Builds the container. Nothing is started yet.
app.start() Runs the start hooks, and with them the checks below. A check that fails throws its own error.
app.resolve(ClassOrName) Gives a component of the program. A run component throws here.
app.stop() Runs the stop hooks and gives the number of them that failed.
app.container The @asenajs/ioc container. A run opened by hand takes it.

The kit registers four parts itself: RunGuard, HarnessRegistry, WorkflowRegistry and CapabilityRegistry. A program that lists one of them among its components, or gives an instance under one of the KitNames, is refused.

app.start() checks that the parts fit. A mistake stops the start with an error that names the classes:

  • A harness lists in permissions every permission its tools use, and none that no tool uses.
  • A harness lists only classes marked with @Tool, no two with the same name, and no two harnesses have the same name.
  • A tool takes only capabilities, and only ones that declare a permission.
  • A permission group lists only permissions that some harness lists.
  • Every tool a harness lists, every flow step a workflow lists and every command a CLI lists is registered in the container.
  • Every class a tool, a flow step or a run component takes with @Use is registered in the container.
  • A method marked with @OnFlowStart is on a run component.
  • A workflow a CLI lists has a command, no two commands of a CLI have the same name, and no workflow is listed by two CLIs.
Mistakes about Come as
Harnesses, tools, workflows and flow steps A WiringError. Its problems holds one line for each mistake it found
A CLI A plain Error

A wiring mistake stops every call of the program, help and version included, because the container does not start.

Each decorator keeps the spec it was given, and a function reads it back from the class: capabilitySpec, toolSpec, harnessSpec, flowStepSpec, workflowSpec, cliSpec and contextSpec. A class that is not marked throws an UnmarkedClassError.

Two of the kit’s parts tell what a started program holds. Take them with @Inject or app.resolve.

Part What it gives
CapabilityRegistry.specs The spec of every registered capability, sorted by name. It builds no capability, so one with scope: "run" is listed like every other.
HarnessRegistry.harnesses The class of every registered harness, sorted by harness name.

Kerpeten prints its kerpeten capabilities list from these two.

Do not give a subclass a field or a method with the name of a private member of its base class.

  • TypeScript’s private is checked by the compiler only. At run time such a member replaces the private member, and the base class then calls the wrong one.
  • tsc reports the clash (TS2415). Bun runs TypeScript without checking types, so a program that is never type-checked fails in a way that is hard to read.
  • Run tsc --noEmit on the program to catch a clash.

The other members are the API of the base class. Implement or override them as the pages of the kinds say, and do not use their names for anything else.

Base class Private members Public and protected members
CapabilityBase none capabilityName
ProcessCapability finish, text, spawn processSettings, collect, stream
FlowStepBase none spec, name, run, command, execute, end
WorkflowBase container, permissionGroups, parts, program, chain spec, name, command, finish, bind, expose, commandInput, run, execute, group, grant, admit, rescue
HarnessBase tools spec, output, finalizeRequest, instructions, context, skippedToolCall, bind, toolbox, run
ContextBase nonce spec, text, notes, sections, dataBlock
CliBase container, commands spec, io, bindCommands, usage, run