Answering Flow: item-by-item generation, review, and local repair
Preserve coverage relationships over a split question batch and repair failing items without regenerating the ones that passed.
run({inputIds,items},context) returns AnswerResult[]. Each item contains id, inputIds, data, and sourceStatus, all decided by the App after reading sources. validateAnswerBatch requires every original inputId to be covered by at least one item and rejects unknown inputs, duplicate task IDs, and limit violations.
The flow uses the App's generate, synchronous check, optional asynchronous review, synchronous fallback, and canInvoke. maxItems and maxRepairs are configured explicitly; reviewPolicy defaults to optional and can also be required or off. canInvoke only checks invocation conditions; real model calls must still go through the budget of the Runner the App has bound. generate returns a Map keyed by item.id; a foreign ID is rejected.
Only locally failing items are repaired; a known review failure is not let through because a later review is unavailable. The final status is accepted, fallback, or blocked, and fallback text is not evidence that a check passed. Sources with unavailable/conflict do not enter normal generation. The Kit uses Workflow internally and does not establish a new Core execution contract.
Progressive examples for the same business case
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.
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);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.
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);First validate multi-question coverage to avoid missing inputs
Extension · Node.js
import { Answering } from '@jangts/agent-core/kits/answering';
const items = [
{ id: 'define', inputIds: ['q1'], sourceStatus: 'ready' as const, data: { source: '定义节点' } },
{ id: 'run', inputIds: ['q2'], sourceStatus: 'unavailable' as const, data: { source: '' } },
];
Answering.validateAnswerBatch(['q1', 'q2'], items, 4);
console.log('两个问题均有明确处理项,包括资料不可用的项。');import { Answering } from '@jangts/agent-core/kits/answering';
const items = [
{ id: 'define', inputIds: ['q1'], sourceStatus: 'ready', data: { source: '定义节点' } },
{ id: 'run', inputIds: ['q2'], sourceStatus: 'unavailable', data: { source: '' } },
];
Answering.validateAnswerBatch(['q1', 'q2'], items, 4);
console.log('两个问题均有明确处理项,包括资料不可用的项。');