defineAnswerTaskCatalog
KIT · function · 运行时值
Compile the answer task catalog from its declarations and shared ports.
Task ids, schemas, readers, outputs and checks have one compiled authority: a task can only reference a reader or check that the ports actually registered, and metadata is cloned and frozen so later mutation by the caller cannot change a registered task.
导入
import { defineAnswerTaskCatalog } from '@jangts/agent-core/kits/answering';import { defineAnswerTaskCatalog } from '@jangts/agent-core/kits/answering';其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/answering、@jangts/agent-core/kits。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | — |
Evidence | — | — |
App | — | — |
Data | — | unknown |
调用
defineAnswerTaskCatalog<Input, Evidence, App, Data = unknown>(definitions: readonly AnswerTaskDefinition<Input, Data>[], ports: AnswerTaskPorts<Input, Evidence, App, Data>): Readonly<{ describe: () => { inputJsonSchema: Readonly<Record<string, unknown>>; id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; metadata: Data; }[]; read(request: { taskId: string; input: unknown; }, context: OperationContext<App>): Evidence; readAsync(request: { taskId: string; input: unknown; }, context: OperationContext<App>): Promise<Evidence>; definitions: readonly Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }> | undefined; require(id: string): Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>; }>Compile the answer task catalog from its declarations and shared ports.
Task ids, schemas, readers, outputs and checks have one compiled authority: a task can only reference a reader or check that the ports actually registered, and metadata is cloned and frozen so later mutation by the caller cannot change a registered task.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | — |
Evidence | — | — |
App | — | — |
Data | — | unknown |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definitions | readonly AnswerTaskDefinition<Input, Data>[] | 必需 | — |
ports | AnswerTaskPorts<Input, Evidence, App, Data> | 必需 | — |
ports 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
readers | ReadonlyCatalog<AnswerReader<Input, Evidence, App, Data>> | 必需 | 只读。见类型定义及调用场景。 |
outputs | ReadonlyCatalog<{ readonly id: string; }> | 必需 | 只读。见类型定义及调用场景。 |
checks | ReadonlyCatalog<{ readonly id: string; }> | 必需 | 只读。见类型定义及调用场景。 |
assertDefinition | ((task: Readonly<AnswerTaskDefinition<Input, Data>>) => void) | undefined | 可省略 | Extra App assertion over a raw definition, before it is compiled. |
返回: Readonly<{ describe: () => { inputJsonSchema: Readonly<Record<string, unknown>>; id: string; description: string; readerId: string; outputId: string; scopeId: string \| undefined; goalIds: readonly any[]; checkIds: readonly any[]; metadata: Data; }[]; read(request: { taskId: string; input: unknown; }, context: OperationContext<App>): Evidence; readAsync(request: { taskId: string; input: unknown; }, context: OperationContext<App>): Promise<Evidence>; definitions: readonly Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string \| undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string \| undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string \| undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string \| undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }> \| undefined; require(id: string): Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string \| undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>; }>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
describe | () => { inputJsonSchema: Readonly<Record<string, unknown>>; id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; metadata: Data; }[] | 必需 | Task metadata with the JSON schema flattened for model or admin presentation. |
read | (request: { taskId: string; input: unknown; }, context: OperationContext<App>) => Evidence | 必需 | Read evidence through a task's registered reader, synchronously. |
readAsync | (request: { taskId: string; input: unknown; }, context: OperationContext<App>) => Promise<Evidence> | 必需 | Read evidence through a task's registered reader, awaiting an async reader. |
definitions | readonly Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>[] | 必需 | 只读。Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | 必需 | Ids in the same order as definitions. |
values | () => IterableIterator<Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>> | 必需 | 见类型定义及调用场景。 |
entries | () => IterableIterator<readonly [string, Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }>]> | 必需 | 见类型定义及调用场景。 |
has | (id: string) => boolean | 必需 | 见类型定义及调用场景。 |
get | (id: string) => Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }> | undefined | 必需 | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<{ id: string; description: string; readerId: string; outputId: string; scopeId: string | undefined; goalIds: readonly any[]; checkIds: readonly any[]; inputSchema: SchemaAdapter<Input>; metadata: Data; }> | 必需 | Returns the definition or throws; use when the id is a required reference. |
使用示例
- Answering 目录:任务、资料读取与检查引用:同一任务定义关联输入 Schema、reader、output 和 check,装配时验证真实引用。
为说明资料登记一个读取任务
入门 · 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 }));相同任务增加检查引用并检查生成结果
配置 · 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));