flowchart LR
C[classify] -- question --> A[answer]
C -- statement --> N[note]
- Attach extra ports with
Component.Port.O(name).attach(this).
- Override
_transform($i, $o) to choose the output port at run time.
- A graph with two terminal components needs an explicit endpoint.
The program
examples/routing/index.tsimport { Component, Pipeline } from '@astack-tech/core';
import type { Port } from '@astack-tech/core';
class Classifier extends Component {
constructor() {
super({});
Component.Port.O('question').attach(this);
Component.Port.O('statement').attach(this);
}
override _transform($i: Port, $o: Port): void {
$i('in').receive((text: string) => {
$o(text.trim().endsWith('?') ? 'question' : 'statement').send(text);
});
}
}
class Answer extends Component {
constructor() {
super({});
}
override run(text: unknown): string {
return `Answering: ${String(text)}`;
}
}
class Note extends Component {
constructor() {
super({});
}
override run(text: unknown): string {
return `Noted: ${String(text)}`;
}
}
const pipeline = new Pipeline();
pipeline.addComponent('classify', new Classifier());
pipeline.addComponent('answer', new Answer());
pipeline.addComponent('note', new Note());
pipeline.connect('classify.question', 'answer.in');
pipeline.connect('classify.statement', 'note.in');
console.log(await pipeline.run<string>('classify.in', 'What is AStack?', 'answer.out'));
console.log(await pipeline.run<string>('classify.in', 'AStack is a framework.', 'note.out'));
Run it
pnpm install && pnpm build
pnpm --filter @astack-examples/routing start
CONCEPTRoutingSend data down different branches with named output ports.View the folder on GitHub