Skip to content
中文

AnswerFlowDefinition ​

KIT · interface · Type contract

Static handlers and limits; signal, diagnostics and App dependencies arrive at run.

Import ​

ts
import type { AnswerFlowDefinition } from '@jangts/agent-core/kits/answering';
js
/** @typedef {import('@jangts/agent-core/kits/answering').AnswerFlowDefinition} AnswerFlowDefinition */

Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/answering, @jangts/agent-core/kits.

Definition ​

ts
interface AnswerFlowDefinition<Data, Value, App>

Type parameters ​

Type parameterConstraintDefault type
Data——
Value——
App—undefined

Field structure ​

FieldTypeRequiredDescription
maxItemsnumberRequiredRead-only. Maximum items in one batch; must be a positive integer.
maxRepairsnumberRequiredRead-only. Repair rounds; the flow runs at most maxRepairs + 1 rounds in total.
reviewPolicy"required" | "off" | "optional" | undefinedOptionalRead-only. Review policy; defaults to optional, required blocks on an unresolved review.
canInvoke(context: OperationContext<App>) => booleanRequiredRequest-level budget guard; must return boolean synchronously.
generate(input: { readonly items: readonly AnswerItem<Data>[]; readonly repairs?: ReadonlyMap<string, readonly AnswerIssue[]>; readonly previous?: ReadonlyMap<string, Value>; }, context: OperationContext<App>) => Promise<ReadonlyMap<string, Value>>RequiredProduces values for the items that still need one. repairs carries the issues of a failed attempt and previous its rejected values.
check(input: { readonly item: AnswerItem<Data>; readonly value: Value; }, context: OperationContext<App>) => AnswerCheck<Value>RequiredLocal check for one generated value; must return synchronously.
review((input: { readonly items: readonly { item: AnswerItem<Data>; value: Value; }[]; }, context: OperationContext<App>) => Promise<ReadonlyMap<string, AnswerCheck<Value>>>) | undefinedOptionalBatch review; returns one check per reviewed item and must cover the submitted items.
fallback(input: { readonly item: AnswerItem<Data>; readonly issues: readonly AnswerIssue[]; }, context: OperationContext<App>) => RecoveryResult<Value>RequiredLast resort for a failed item; handled supplies a replacement value.
observe((input: { readonly stageId: string; readonly results: readonly AnswerResult<Data, Value>[]; }, context: OperationContext<App>) => MaybePromise<void>) | undefinedOptionalObserves a stage outcome; failures must not affect the flow result.

Member index ​

MemberPurpose
canInvokeRequest-level budget guard; must return boolean synchronously.
generateProduces values for the items that still need one. repairs carries the issues of a failed attempt and previous its rejected values.
checkLocal check for one generated value; must return synchronously.
fallbackLast resort for a failed item; handled supplies a replacement value.

Member details ​

canInvoke ​

Request-level budget guard; must return boolean synchronously.

ts
AnswerFlowDefinition.canInvoke(context: OperationContext<App>): boolean

Request-level budget guard; must return boolean synchronously.

ParameterTypePassingDescription
contextOperationContext<App>Required—
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: boolean.

generate ​

Produces values for the items that still need one.

repairs carries the issues of a failed attempt and previous its rejected values.

ts
AnswerFlowDefinition.generate(input: { readonly items: readonly AnswerItem<Data>[]; readonly repairs?: ReadonlyMap<string, readonly AnswerIssue[]>; readonly previous?: ReadonlyMap<string, Value>; }, context: OperationContext<App>): Promise<ReadonlyMap<string, Value>>

Produces values for the items that still need one.

repairs carries the issues of a failed attempt and previous its rejected values.

ParameterTypePassingDescription
input{ readonly items: readonly AnswerItem<Data>[]; readonly repairs?: ReadonlyMap<string, readonly AnswerIssue[]>; readonly previous?: ReadonlyMap<string, Value>; }Required—
contextOperationContext<App>Required—
Fields of input
FieldTypeRequiredDescription
itemsreadonly AnswerItem<Data>[]RequiredRead-only. See the type definition and usage context.
repairsReadonlyMap<string, readonly AnswerIssue[]> | undefinedOptionalRead-only. See the type definition and usage context.
previousReadonlyMap<string, Value> | undefinedOptionalRead-only. See the type definition and usage context.
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: Promise<ReadonlyMap<string, Value>>.

Awaited value: ReadonlyMap<string, Value>.

check ​

Local check for one generated value; must return synchronously.

ts
AnswerFlowDefinition.check(input: { readonly item: AnswerItem<Data>; readonly value: Value; }, context: OperationContext<App>): AnswerCheck<Value>

Local check for one generated value; must return synchronously.

ParameterTypePassingDescription
input{ readonly item: AnswerItem<Data>; readonly value: Value; }Required—
contextOperationContext<App>Required—
Fields of input
FieldTypeRequiredDescription
itemAnswerItem<Data>RequiredRead-only. See the type definition and usage context.
valueValueRequiredRead-only. See the type definition and usage context.
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: AnswerCheck<Value>.

Return value structure
FieldTypeRequiredDescription
status"unavailable" | "passed" | "failed" | "not-run"RequiredRead-only. See the type definition and usage context.
issuesreadonly Issue[]RequiredRead-only. Problems found; expected to be empty when the status is passed.
valueValue | undefinedOptionalRead-only. Checked or produced value; present only when the check yields one.

fallback ​

Last resort for a failed item; handled supplies a replacement value.

ts
AnswerFlowDefinition.fallback(input: { readonly item: AnswerItem<Data>; readonly issues: readonly AnswerIssue[]; }, context: OperationContext<App>): RecoveryResult<Value>

Last resort for a failed item; handled supplies a replacement value.

ParameterTypePassingDescription
input{ readonly item: AnswerItem<Data>; readonly issues: readonly AnswerIssue[]; }Required—
contextOperationContext<App>Required—
Fields of input
FieldTypeRequiredDescription
itemAnswerItem<Data>RequiredRead-only. See the type definition and usage context.
issuesreadonly AnswerIssue[]RequiredRead-only. See the type definition and usage context.
Fields of context
FieldTypeRequiredDescription
appunknown extends App ? App : Readonly<App>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: RecoveryResult<Value>.

Optional shapes of Return value: choose one shape; required fields for each branch are listed separately.

Branch 1
FieldTypeRequiredDescription
handledfalseRequiredRead-only. See the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
handledtrueRequiredRead-only. See the type definition and usage context.
valueValueRequiredRead-only. See the type definition and usage context.

Examples ​

First handle one question that has a source ​

Beginner · Node.js

The offline example uses already-read body text directly to show the flow contract; when switching to model generation, the same generate port still applies.

ts
import { Answering } from '@jangts/agent-core/kits/answering';
const flow = Answering.defineAnswerFlow<{ source: string }, string, {}>({
  maxItems: 4, maxRepairs: 0, reviewPolicy: 'off', canInvoke: () => true,
  async generate({ items }) { return new Map(items.map(item => [item.id, item.data.source])); },
  check: ({ value }) => ({ status: value.trim() ? 'passed' : 'failed', issues: [] }),
  fallback: () => ({ handled: false }),
});
const result = await flow.run({ inputIds: ['question-1'], items: [{ id: 'explain', inputIds: ['question-1'], sourceStatus: 'ready', data: { source: '普通对象可以实现节点合同。' } }] }, { app: {}, signal: new AbortController().signal });
console.log(result[0].status, result[0].value);
js
import { Answering } from '@jangts/agent-core/kits/answering';
const flow = Answering.defineAnswerFlow({
    maxItems: 4, maxRepairs: 0, reviewPolicy: 'off', canInvoke: () => true,
    async generate({ items }) { return new Map(items.map(item => [item.id, item.data.source])); },
    check: ({ value }) => ({ status: value.trim() ? 'passed' : 'failed', issues: [] }),
    fallback: () => ({ handled: false }),
});
const result = await flow.run({ inputIds: ['question-1'], items: [{ id: 'explain', inputIds: ['question-1'], sourceStatus: 'ready', data: { source: '普通对象可以实现节点合同。' } }] }, { app: {}, signal: new AbortController().signal });
console.log(result[0].status, result[0].value);

Keep run unchanged and add review plus one local repair ​

Configuration · Node.js

The example triggers one real local repair with an empty draft; production review should rely on real evidence instead of substituting string equality for semantic review.

ts
import { Answering } from '@jangts/agent-core/kits/answering';
const flow = Answering.defineAnswerFlow<{ source: string }, string, {}>({
  maxItems: 4, maxRepairs: 1, reviewPolicy: 'required', canInvoke: () => true,
  async generate({ items, repairs }) { return new Map(items.map(item => [item.id, repairs?.has(item.id) ? item.data.source : ''])); },
  check: ({ value }) => ({ status: value.trim() ? 'passed' : 'failed', issues: value.trim() ? [] : [{ code: 'EMPTY', message: '需要非空回答' }] }),
  async review({ items }) { return new Map(items.map(({ item, value }) => [item.id, { status: value === item.data.source ? 'passed' as const : 'failed' as const, issues: [] }])); },
  fallback: () => ({ handled: true, value: '当前资料不足,暂时无法回答。' }),
});
const result = await flow.run({ inputIds: ['question-1'], items: [{ id: 'explain', inputIds: ['question-1'], sourceStatus: 'ready', data: { source: '普通对象可以实现节点合同。' } }] }, { app: {}, signal: new AbortController().signal });
console.log(result[0].status, result[0].checks);
js
import { Answering } from '@jangts/agent-core/kits/answering';
const flow = Answering.defineAnswerFlow({
    maxItems: 4, maxRepairs: 1, reviewPolicy: 'required', canInvoke: () => true,
    async generate({ items, repairs }) { return new Map(items.map(item => [item.id, repairs?.has(item.id) ? item.data.source : ''])); },
    check: ({ value }) => ({ status: value.trim() ? 'passed' : 'failed', issues: value.trim() ? [] : [{ code: 'EMPTY', message: '需要非空回答' }] }),
    async review({ items }) { return new Map(items.map(({ item, value }) => [item.id, { status: value === item.data.source ? 'passed' : 'failed', issues: [] }])); },
    fallback: () => ({ handled: true, value: '当前资料不足,暂时无法回答。' }),
});
const result = await flow.run({ inputIds: ['question-1'], items: [{ id: 'explain', inputIds: ['question-1'], sourceStatus: 'ready', data: { source: '普通对象可以实现节点合同。' } }] }, { app: {}, signal: new AbortController().signal });
console.log(result[0].status, result[0].checks);

API reference, usage examples and developer guides.