AnswerFlowDefinition
KIT · interface · 类型合同
Static handlers and limits; signal, diagnostics and App dependencies arrive at run.
导入
import type { AnswerFlowDefinition } from '@jangts/agent-core/kits/answering';/** @typedef {import('@jangts/agent-core/kits/answering').AnswerFlowDefinition} AnswerFlowDefinition */其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/answering、@jangts/agent-core/kits。
定义
interface AnswerFlowDefinition<Data, Value, App>泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Data | — | — |
Value | — | — |
App | — | undefined |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
maxItems | number | 必需 | 只读。Maximum items in one batch; must be a positive integer. |
maxRepairs | number | 必需 | 只读。Repair rounds; the flow runs at most maxRepairs + 1 rounds in total. |
reviewPolicy | "required" | "off" | "optional" | undefined | 可省略 | 只读。Review policy; defaults to optional, required blocks on an unresolved review. |
canInvoke | (context: OperationContext<App>) => boolean | 必需 | Request-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>> | 必需 | Produces 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> | 必需 | Local 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>>>) | undefined | 可省略 | Batch 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> | 必需 | Last 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>) | undefined | 可省略 | Observes a stage outcome; failures must not affect the flow result. |
成员目录
| 成员 | 用途 |
|---|---|
canInvoke | Request-level budget guard; must return boolean synchronously. |
generate | Produces values for the items that still need one. repairs carries the issues of a failed attempt and previous its rejected values. |
check | Local check for one generated value; must return synchronously. |
fallback | Last resort for a failed item; handled supplies a replacement value. |
成员详情
canInvoke
Request-level budget guard; must return boolean synchronously.
AnswerFlowDefinition.canInvoke(context: OperationContext<App>): booleanRequest-level budget guard; must return boolean synchronously.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
context | OperationContext<App> | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
app | unknown extends App ? App : Readonly<App> | 必需 | 只读。见类型定义及调用场景。 |
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. |
返回: boolean。
generate
Produces values for the items that still need one.
repairs carries the issues of a failed attempt and previous its rejected values.
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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | { readonly items: readonly AnswerItem<Data>[]; readonly repairs?: ReadonlyMap<string, readonly AnswerIssue[]>; readonly previous?: ReadonlyMap<string, Value>; } | 必需 | — |
context | OperationContext<App> | 必需 | — |
input 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
items | readonly AnswerItem<Data>[] | 必需 | 只读。见类型定义及调用场景。 |
repairs | ReadonlyMap<string, readonly AnswerIssue[]> | undefined | 可省略 | 只读。见类型定义及调用场景。 |
previous | ReadonlyMap<string, Value> | undefined | 可省略 | 只读。见类型定义及调用场景。 |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
app | unknown extends App ? App : Readonly<App> | 必需 | 只读。见类型定义及调用场景。 |
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. |
返回: Promise<ReadonlyMap<string, Value>>。
等待后的值: ReadonlyMap<string, Value>。
check
Local check for one generated value; must return synchronously.
AnswerFlowDefinition.check(input: { readonly item: AnswerItem<Data>; readonly value: Value; }, context: OperationContext<App>): AnswerCheck<Value>Local check for one generated value; must return synchronously.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | { readonly item: AnswerItem<Data>; readonly value: Value; } | 必需 | — |
context | OperationContext<App> | 必需 | — |
input 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
item | AnswerItem<Data> | 必需 | 只读。见类型定义及调用场景。 |
value | Value | 必需 | 只读。见类型定义及调用场景。 |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
app | unknown extends App ? App : Readonly<App> | 必需 | 只读。见类型定义及调用场景。 |
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. |
返回: AnswerCheck<Value>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
status | "unavailable" | "passed" | "failed" | "not-run" | 必需 | 只读。见类型定义及调用场景。 |
issues | readonly Issue[] | 必需 | 只读。Problems found; expected to be empty when the status is passed. |
value | Value | undefined | 可省略 | 只读。Checked or produced value; present only when the check yields one. |
fallback
Last resort for a failed item; handled supplies a replacement value.
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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | { readonly item: AnswerItem<Data>; readonly issues: readonly AnswerIssue[]; } | 必需 | — |
context | OperationContext<App> | 必需 | — |
input 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
item | AnswerItem<Data> | 必需 | 只读。见类型定义及调用场景。 |
issues | readonly AnswerIssue[] | 必需 | 只读。见类型定义及调用场景。 |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
app | unknown extends App ? App : Readonly<App> | 必需 | 只读。见类型定义及调用场景。 |
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. |
返回: RecoveryResult<Value>。
返回值的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
handled | false | 必需 | 只读。见类型定义及调用场景。 |
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
handled | true | 必需 | 只读。见类型定义及调用场景。 |
value | Value | 必需 | 只读。见类型定义及调用场景。 |
使用示例
- Answering Flow:逐项生成、复核与局部修正:对已拆分的问题批次保留覆盖关系,修复失败项,不重新生成已经通过的项。
先处理一个有来源的说明问题
入门 · Node.js
离线示例直接使用已读正文,展示流程合同;换成模型生成时仍用同一个 generate 端口。
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);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);保持 run 不变,增加复核和一次局部修正
配置 · Node.js
示例以空草稿触发一次真实局部修正;生产 review 应依据真实证据,不用字符串等号代替语义复核。
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);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);