defineAnswerCheckCatalog
KIT · function · Runtime value
Compile a checker catalog.
A checker may return a replacement value; it is threaded into the next checker, so the selected ids behave like an ordered chain rather than independent assertions.
Import
import { defineAnswerCheckCatalog } from '@jangts/agent-core/kits/answering';import { defineAnswerCheckCatalog } from '@jangts/agent-core/kits/answering';Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/answering, @jangts/agent-core/kits.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
App | — | — |
Call
defineAnswerCheckCatalog<Value, App>(definitions: readonly AnswerChecker<Value, App>[]): Readonly<{ check(request: { checkIds: readonly string[]; value: Value; }, context: OperationContext<App>): AnswerCheck<Value>; definitions: readonly Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }> | undefined; require(id: string): Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>; }>Compile a checker catalog.
A checker may return a replacement value; it is threaded into the next checker, so the selected ids behave like an ordered chain rather than independent assertions.
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
App | — | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
definitions | readonly AnswerChecker<Value, App>[] | Required | — |
Returns: Readonly<{ check(request: { checkIds: readonly string[]; value: Value; }, context: OperationContext<App>): AnswerCheck<Value>; definitions: readonly Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }> \| undefined; require(id: string): Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>; }>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
check | (request: { checkIds: readonly string[]; value: Value; }, context: OperationContext<App>) => AnswerCheck<Value> | Required | See the type definition and usage context. |
definitions | readonly Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>[] | Required | Read-only. Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | Required | Ids in the same order as definitions. |
values | () => IterableIterator<Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>> | Required | See the type definition and usage context. |
entries | () => IterableIterator<readonly [string, Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }>]> | Required | See the type definition and usage context. |
has | (id: string) => boolean | Required | See the type definition and usage context. |
get | (id: string) => Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }> | undefined | Required | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<{ id: string; check: (value: Value, context: OperationContext<App>) => CheckResult<Value>; }> | Required | Returns the definition or throws; use when the id is a required reference. |
Examples
- Answering catalog: tasks, source reads, and check references: One task definition ties together the input Schema, reader, output, and check, with real references verified at assembly.
Register one read task for explanatory material
Beginner · Node.js
import { Answering } from '@jangts/agent-core/kits/answering';
import { Presentation } from '@jangts/agent-core/kits/presentation';
const readers = Answering.defineAnswerReaderCatalog([{ id: 'manual', read: (input: string) => ({ question: input, source: 'Agent Core 允许普通对象节点。' }) }]);
const checks = Answering.defineAnswerCheckCatalog<string, {}>([]);
const outputs = Presentation.defineOutputCatalog([{ id: 'reply', kind: 'text', fields: [{ id: 'text', owner: 'generated' }] }]);
const tasks = Answering.defineAnswerTaskCatalog([
{ id: 'explain', description: '根据说明资料解释接口', readerId: 'manual', outputId: 'reply', goalIds: ['explain'], checkIds: [], metadata: {},
inputSchema: { jsonSchema: { type: 'string' }, parse(value: unknown) { if (typeof value !== 'string') throw new Error('需要问题'); return value; } } },
], { readers, checks, outputs });
console.log(tasks.read({ taskId: 'explain', input: '如何定义节点?' }, { app: {}, signal: new AbortController().signal }));import { Answering } from '@jangts/agent-core/kits/answering';
import { Presentation } from '@jangts/agent-core/kits/presentation';
const readers = Answering.defineAnswerReaderCatalog([{ id: 'manual', read: (input) => ({ question: input, source: 'Agent Core 允许普通对象节点。' }) }]);
const checks = Answering.defineAnswerCheckCatalog([]);
const outputs = Presentation.defineOutputCatalog([{ id: 'reply', kind: 'text', fields: [{ id: 'text', owner: 'generated' }] }]);
const tasks = Answering.defineAnswerTaskCatalog([
{ id: 'explain', description: '根据说明资料解释接口', readerId: 'manual', outputId: 'reply', goalIds: ['explain'], checkIds: [], metadata: {},
inputSchema: { jsonSchema: { type: 'string' }, parse(value) { if (typeof value !== 'string')
throw new Error('需要问题'); return value; } } },
], { readers, checks, outputs });
console.log(tasks.read({ taskId: 'explain', input: '如何定义节点?' }, { app: {}, signal: new AbortController().signal }));Add check references to the same task and check the generated result
Configuration · Node.js
import { Answering } from '@jangts/agent-core/kits/answering';
import { Presentation } from '@jangts/agent-core/kits/presentation';
const readers = Answering.defineAnswerReaderCatalog([{ id: 'manual', read: (input: string) => ({ question: input, source: 'Agent Core 允许普通对象节点。' }) }]);
const checks = Answering.defineAnswerCheckCatalog<string, {}>([
{ id: 'nonempty', check: value => ({ status: value.trim() ? 'passed' : 'failed', value: value.trim(), issues: value.trim() ? [] : [{ code: 'EMPTY', message: '答案为空' }] }) },
]);
const outputs = Presentation.defineOutputCatalog([{ id: 'reply', kind: 'text', fields: [{ id: 'text', owner: 'generated' }] }]);
const tasks = Answering.defineAnswerTaskCatalog([
{ id: 'explain', description: '根据说明资料解释接口', readerId: 'manual', outputId: 'reply', goalIds: ['explain'], checkIds: ['nonempty'], metadata: {},
inputSchema: { jsonSchema: { type: 'string' }, parse(value: unknown) { if (typeof value !== 'string') throw new Error('需要问题'); return value; } } },
], { readers, checks, outputs });
const context = { app: {}, signal: new AbortController().signal };
const evidence = await tasks.readAsync({ taskId: 'explain', input: '如何定义节点?' }, context);
console.log(evidence, checks.check({ checkIds: tasks.require('explain').checkIds, value: ' 普通对象即可。 ' }, context));import { Answering } from '@jangts/agent-core/kits/answering';
import { Presentation } from '@jangts/agent-core/kits/presentation';
const readers = Answering.defineAnswerReaderCatalog([{ id: 'manual', read: (input) => ({ question: input, source: 'Agent Core 允许普通对象节点。' }) }]);
const checks = Answering.defineAnswerCheckCatalog([
{ id: 'nonempty', check: value => ({ status: value.trim() ? 'passed' : 'failed', value: value.trim(), issues: value.trim() ? [] : [{ code: 'EMPTY', message: '答案为空' }] }) },
]);
const outputs = Presentation.defineOutputCatalog([{ id: 'reply', kind: 'text', fields: [{ id: 'text', owner: 'generated' }] }]);
const tasks = Answering.defineAnswerTaskCatalog([
{ id: 'explain', description: '根据说明资料解释接口', readerId: 'manual', outputId: 'reply', goalIds: ['explain'], checkIds: ['nonempty'], metadata: {},
inputSchema: { jsonSchema: { type: 'string' }, parse(value) { if (typeof value !== 'string')
throw new Error('需要问题'); return value; } } },
], { readers, checks, outputs });
const context = { app: {}, signal: new AbortController().signal };
const evidence = await tasks.readAsync({ taskId: 'explain', input: '如何定义节点?' }, context);
console.log(evidence, checks.check({ checkIds: tasks.require('explain').checkIds, value: ' 普通对象即可。 ' }, context));