AsIntent
CORE · type · 类型合同
Demand-discovery metadata; it has no identity independent of its tool.
导入
ts
import type { AsIntent } from '@jangts/agent-core';js
/** @typedef {import('@jangts/agent-core').AsIntent} AsIntent */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs。
定义
类型: AsIntent<Context>
类型表达式
ts
type AsIntent<Context> = true | string | ((context: ToolContext<Context>) => boolean) | IntentOptions<Context>;泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Context | — | — |
联合分支结构
AsIntent的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
string
分支 2
true
分支 3
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
description | string | undefined | 可省略 | 只读。Optional demand-facing description; otherwise the tool description is reused. |
when | ((context: ToolContext<Context>) => boolean) | undefined | 可省略 | 只读。Synchronous participation predicate. |
delegates | readonly string[] | undefined | 可省略 | 只读。Allowed continuation delegates, not an execution sequence. |
分支 4
(context: ToolContext<Context>) => boolean
使用示例
- 模型 Tool 与传输:完整校验后才得到类型化选择:Tool 定义提供 Schema 与 resolver;传输将 wire 输出翻译为统一的模型选择合同。
- 装饰器:处理类与已有实例方法:类装饰代办定义实例化,方法装饰绑定原对象;输出仍走原 define、构造和 bind。
声明两个可选择 Tool
入门 · Node.js
ts
import { ModelToolRegistry } from '@jangts/agent-core';
const inputSchema = { jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value: unknown) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new Error('需要空对象');
return {};
} };
const tools = new ModelToolRegistry([
{ id: 'greet', description: '回答问候', inputSchema, resolve: () => 'greet' },
{ id: 'status', description: '查询状态', inputSchema, resolve: () => 'status' },
]).seal();
console.log(tools.resolve({ toolId: 'status', input: {} }, { stageId: 'recognize' }));js
import { ModelToolRegistry } from '@jangts/agent-core';
const inputSchema = { jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
throw new Error('需要空对象');
return {};
} };
const tools = new ModelToolRegistry([
{ id: 'greet', description: '回答问候', inputSchema, resolve: () => 'greet' },
{ id: 'status', description: '查询状态', inputSchema, resolve: () => 'status' },
]).seal();
console.log(tools.resolve({ toolId: 'status', input: {} }, { stageId: 'recognize' }));仅向一个阶段投影允许的子集
配置 · Node.js
ts
import { ModelToolRegistry, Support } from '@jangts/agent-core';
const schema = Support.defineSchema({ jsonSchema: { type: 'object', properties: {} },
parse(value: unknown) { if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('需要对象'); return {}; } });
const registry = new ModelToolRegistry([
{ id: 'greet', description: '问候', inputSchema: schema, resolve: () => 'greet' },
{ id: 'status', description: '状态', inputSchema: schema, resolve: () => 'status' },
]).seal();
const readOnly = registry.select(['status']);
const transport = Support.createModelTransport({ id: 'recognize', tools: readOnly });
console.log(transport.id, readOnly.ids());js
import { ModelToolRegistry, Support } from '@jangts/agent-core';
const schema = Support.defineSchema({ jsonSchema: { type: 'object', properties: {} },
parse(value) { if (!value || typeof value !== 'object' || Array.isArray(value))
throw new TypeError('需要对象'); return {}; } });
const registry = new ModelToolRegistry([
{ id: 'greet', description: '问候', inputSchema: schema, resolve: () => 'greet' },
{ id: 'status', description: '状态', inputSchema: schema, resolve: () => 'status' },
]).seal();
const readOnly = registry.select(['status']);
const transport = Support.createModelTransport({ id: 'recognize', tools: readOnly });
console.log(transport.id, readOnly.ids());