安装
AStack 需要 Node.js 20 或更高版本。
npm install @astack-tech/core @astack-tech/components
需要模型提供方时再安装 @astack-tech/integrations,需要工具时再安装 @astack-tech/tools。
编写组件
组件继承 Component。重写 run() 即可让它可以独立运行。
import { Component } from '@astack-tech/core';
class Uppercase extends Component {
constructor() {
super({});
}
override run(text: unknown): string {
return String(text).toUpperCase();
}
}
console.log(new Uppercase().run('hello')); // HELLO
连接组件
在管道中注册组件,把输出端口连接到输入端口,然后运行。
import { Pipeline } from '@astack-tech/core';
const pipeline = new Pipeline();
pipeline.addComponent('trim', new Trim());
pipeline.addComponent('upper', new Uppercase());
pipeline.connect('trim.out', 'upper.in');
const result = await pipeline.run<string>('trim.in', ' hello '); // 'HELLO'
每个组件默认都有一个 in 端口和一个 out 端口。run() 把输入发送到触发端口(trim.in),并以到达终点的值作为结果。这里的终点是自动推断的:upper 是唯一一个没有下游连接的组件。