defineDelivery
KIT · function · 运行时值
Compile a delivery pipeline from its declared stages.
Stages are registered once, in declaration order, and must have unique non-blank ids. A stage without a handle is a named hook only, so beforeStage can observe it.
导入
ts
import { defineDelivery } from '@jangts/agent-core/kits/delivery';js
import { defineDelivery } from '@jangts/agent-core/kits/delivery';其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/delivery、@jangts/agent-core/kits。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | — |
State | — | — |
Result | — | — |
StageId | string | — |
调用
ts
defineDelivery<Input, State, Result, StageId extends string>(options: DeliveryOptions<Input, State, Result, StageId>): Readonly<{ stages: readonly { id: StageId; }[]; deliver(input: Input, execution: ExecutionContext): Result; }>Compile a delivery pipeline from its declared stages.
Stages are registered once, in declaration order, and must have unique non-blank ids. A stage without a handle is a named hook only, so beforeStage can observe it.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | — |
State | — | — |
Result | — | — |
StageId | string | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | DeliveryOptions<Input, State, Result, StageId> | 必需 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
createState | (input: Input, context: ExecutionContext) => State | 必需 | 只读。Creates the mutable per-run state; must return synchronously. |
stages | readonly DeliveryStage<Input, State, Result, StageId>[] | 必需 | 只读。Stages in execution order; ids must be unique. |
beforeStage | ((input: { stageId: StageId; state: State; }, context: OperationContext<Input>) => void) | undefined | 可省略 | 只读。Observes every stage before it runs, including stages without a handler. |
missingResult | () => Error | 必需 | 只读。Builds the error thrown when every stage returned continue. |
返回: Readonly<{ stages: readonly { id: StageId; }[]; deliver(input: Input, execution: ExecutionContext): Result; }>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
stages | readonly { id: StageId; }[] | 必需 | 见类型定义及调用场景。 |
deliver | (input: Input, execution: ExecutionContext) => Result | 必需 | Build the state and run the stages in order. The first complete outcome ends the pipeline and returns its value; if none completes, missingResult supplies the error. |
使用示例
- Delivery:顺序交付与显式结束:每个交付阶段明确 continue 或 complete;一个阶段完成后立即结束。
交付一段已经确认的文本
入门 · Node.js
ts
import { Delivery } from '@jangts/agent-core/kits/delivery';
const delivery = Delivery.defineDelivery({
createState: (input: string) => ({ text: input }),
stages: [{ id: 'publish', handle: (state: { text: string }) => ({ type: 'complete' as const, value: state.text }) }],
missingResult: () => new Error('没有可交付结果'),
});
console.log(delivery.deliver('已完成的答复', { signal: new AbortController().signal }));js
import { Delivery } from '@jangts/agent-core/kits/delivery';
const delivery = Delivery.defineDelivery({
createState: (input) => ({ text: input }),
stages: [{ id: 'publish', handle: (state) => ({ type: 'complete', value: state.text }) }],
missingResult: () => new Error('没有可交付结果'),
});
console.log(delivery.deliver('已完成的答复', { signal: new AbortController().signal }));增加校验阶段,保留同一个 deliver
配置 · Node.js
ts
import { Delivery } from '@jangts/agent-core/kits/delivery';
const delivery = Delivery.defineDelivery({
createState: (input: string) => ({ text: input.trim() }),
stages: [
{ id: 'check', handle(state: { text: string }) {
if (!state.text) throw new Error('答复为空');
return { type: 'continue' as const };
} },
{ id: 'publish', handle: (state: { text: string }) => ({ type: 'complete' as const, value: state.text }) },
],
beforeStage: ({ stageId }: { stageId: string }) => { console.log('阶段', stageId); },
missingResult: () => new Error('没有可交付结果'),
});
console.log(delivery.deliver(' 已完成的答复 ', { signal: new AbortController().signal }));js
import { Delivery } from '@jangts/agent-core/kits/delivery';
const delivery = Delivery.defineDelivery({
createState: (input) => ({ text: input.trim() }),
stages: [
{ id: 'check', handle(state) {
if (!state.text)
throw new Error('答复为空');
return { type: 'continue' };
} },
{ id: 'publish', handle: (state) => ({ type: 'complete', value: state.text }) },
],
beforeStage: ({ stageId }) => { console.log('阶段', stageId); },
missingResult: () => new Error('没有可交付结果'),
});
console.log(delivery.deliver(' 已完成的答复 ', { signal: new AbortController().signal }));