import { 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 => {
if (typeof text !== 'string') {
throw new TypeError('Classifier expects a 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('classify.in', 'What is AStack?', 'answer.out'));
console.log(await pipeline.run('classify.in', 'AStack is a framework.', 'note.out'));