Skip to content
English

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——
StageIdstring—

调用 ​

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——
StageIdstring—
参数类型传递说明
optionsDeliveryOptions<Input, State, Result, StageId>必需—
options 的字段
字段类型必需说明
createState(input: Input, context: ExecutionContext) => State必需只读。Creates the mutable per-run state; must return synchronously.
stagesreadonly 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; }>。

返回值结构
字段类型必需说明
stagesreadonly { 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.

使用示例 ​

交付一段已经确认的文本 ​

入门 · 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 }));

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。