DeliveryOptions
KIT · interface · 类型合同
Pipeline construction options.
导入
ts
import type { DeliveryOptions } from '@jangts/agent-core/kits/delivery';1
js
/** @typedef {import('@jangts/agent-core/kits/delivery').DeliveryOptions} DeliveryOptions */1
其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/delivery、@jangts/agent-core/kits。
定义
ts
interface DeliveryOptions<Input, State, Result, StageId>1
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | — |
State | — | — |
Result | — | — |
StageId | string | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
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. |
成员目录
| 成员 | 用途 |
|---|---|
createState | Creates the mutable per-run state; must return synchronously. |
missingResult | Builds the error thrown when every stage returned continue. |
成员详情
createState
Creates the mutable per-run state; must return synchronously.
ts
DeliveryOptions.createState(input: Input, context: ExecutionContext): State1
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Input | 必需 | — |
context | ExecutionContext | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
logger | SubFlowLog | undefined | 可省略 | 只读。Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | 可省略 | 只读。Diagnostic scope of the enclosing call, when recording is enabled. |
返回: State。
等待后的值: Awaited<State>。
missingResult
Builds the error thrown when every stage returned continue.
ts
DeliveryOptions.missingResult(): Error1
无调用参数。
返回: 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 }));1
2
3
4
5
6
7
2
3
4
5
6
7
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 }));1
2
3
4
5
6
7
2
3
4
5
6
7
增加校验阶段,保留同一个 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 }));1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }));1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15