DOCSComponents

Components

The Component class, its ports, and its two execution modes.

A component is a class that extends Component from @astack-tech/core.

Ports

Every component has two default ports: in (input) and out (output). Add more ports in the constructor:

import { Component } from '@astack-tech/core';

class Classifier extends Component {
  constructor() {
    super({});
    Component.Port.I('text').attach(this); // extra input port
    Component.Port.O('question').attach(this); // extra output port
    Component.Port.O('statement').attach(this);
  }
}

A port is addressed as componentName.portName inside a pipeline, for example classify.question.

Two execution modes

Standalone. run(input) processes one value and returns the result, or a promise of it. The default implementation returns the input unchanged.

Pipeline. When a component is added to a pipeline, the runtime calls _transform($i, $o). The default _transform calls run() for every value on in and sends the result to out. So a component that only overrides run() already works in a pipeline.

Override _transform when you need more than one input or output, or when the output port depends on the data:

import type { Port } from '@astack-tech/core';

override _transform($i: Port, $o: Port): void {
  $i('in').receive((text: string) => {
    $o(text.endsWith('?') ? 'question' : 'statement').send(text);
  });
}

$i(name).receive(callback) subscribes to an input port. $o(name).send(value) emits on an output port. A component can send zero, one or many values for each input.

Asynchronous work

receive callbacks are not awaited. Start the async work and send when it completes:

override _transform($i: Port, $o: Port): void {
  $i('in').receive((prompt: string) => {
    void model.generateCompletion(prompt).then(answer => {
      $o('out').send(answer);
    });
  });
}

Built-in components

Component Package Ports Purpose
TextSplitter components text → out Split text into chunks of at most chunkSize characters, with overlap.
ToolInvoker components in → out Run the tool calls found in a list of messages. See Tools.
Agent components in → out Run a model with tools in a loop. See Agents.
StreamingAgent components in → out Same loop, emitting progress events. See Streaming.
RLMAgent components in → out Answer questions over large contexts. See RLM.
Deepseek integrations prompt → completion, messages → message Call the DeepSeek API. See Model providers.

RUN IT

Examples for this page