Delivery
KIT · constant · Runtime value
Import
ts
import { Delivery } from '@jangts/agent-core/kits/delivery';js
import { Delivery } from '@jangts/agent-core/kits/delivery';Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/delivery, @jangts/agent-core/kits.
Definition
Member index
| Member | Purpose |
|---|---|
defineDelivery | See the overload below for full inputs and outputs. |
Member details
defineDelivery
ts
Delivery.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.
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
State | — | — |
Result | — | — |
StageId | string | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
options | DeliveryOptions<Input, State, Result, StageId> | Required | — |
Fields of options
| Field | Type | Required | Description |
|---|---|---|---|
createState | (input: Input, context: ExecutionContext) => State | Required | Read-only. Creates the mutable per-run state; must return synchronously. |
stages | readonly DeliveryStage<Input, State, Result, StageId>[] | Required | Read-only. Stages in execution order; ids must be unique. |
beforeStage | ((input: { stageId: StageId; state: State; }, context: OperationContext<Input>) => void) | undefined | Optional | Read-only. Observes every stage before it runs, including stages without a handler. |
missingResult | () => Error | Required | Read-only. Builds the error thrown when every stage returned continue. |
Returns: Readonly<{ stages: readonly { id: StageId; }[]; deliver(input: Input, execution: ExecutionContext): Result; }>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
stages | readonly { id: StageId; }[] | Required | See the type definition and usage context. |
deliver | (input: Input, execution: ExecutionContext) => Result | Required | 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. |
Examples
- Delivery: sequential delivery with an explicit end: Each delivery stage explicitly continues or completes; once a stage completes, the run ends immediately.
Deliver a piece of text that is already confirmed
Beginner · 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 }));Add a validation stage while keeping the same deliver
Configuration · 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 }));