Skip to content
English

Tool ​

KIT · interface · 类型合同

A registered tool: model metadata plus one optional lifecycle block.

导入 ​

ts
import type { Tool } from '@jangts/agent-core/kits/tools';
js
/** @typedef {import('@jangts/agent-core/kits/tools').Tool} Tool */

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/kits/tools、@jangts/agent-core/kits。

定义 ​

ts
interface Tool<Types>

继承或实现: ErasedTool、ModelTool<unknown, unknown, Types['context']>。

泛型参数 ​

类型参数约束默认类型
TypesToolRuntimeTypes—

字段结构 ​

字段类型必需说明
goalIdsreadonly string[] | "all"必需只读。Goal ids this tool serves, or 'all' when it is always available.
descriptionstring必需只读。见类型定义及调用场景。
audiencestring必需只读。Audience this tool is described to; validated against the registry policy.
effectstring必需只读。Declared effect class, for example read-only or mutating.
delegatestring | undefined可省略只读。Delegate id this tool answers, when a node delegates execution by name.
lifecycleLifecycleHooks<Types, LifecycleProtocol> | undefined可省略只读。Lifecycle hooks this tool declares for a request.
idstring必需只读。见类型定义及调用场景。
kindstring必需只读。见类型定义及调用场景。
debugSteps((result: unknown, context: NodeReportingContext) => void) | undefined可省略只读。见类型定义及调用场景。
parseInput(value: unknown) => unknown必需见类型定义及调用场景。
parseOutput(value: unknown) => unknown必需见类型定义及调用场景。
execute(input: unknown, context: ToolExecutionContext) => unknown必需见类型定义及调用场景。
inputSchemaSchemaAdapter<unknown>必需只读。Schema describing the arguments; must be an object schema.
activeToolCondition<Types["context"]> | undefined可省略只读。Omitted means active. Authorization remains a separate execution boundary.
asIntentAsIntent<Types["context"]> | undefined可省略只读。Omitted means this tool is not advertised by withIntent().
resolve(input: unknown, context: Readonly<{ stageId: string; }>) => unknown必需Converts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection.

成员目录 ​

成员用途
parseInput完整输入输出见下方重载。
parseOutput完整输入输出见下方重载。
execute完整输入输出见下方重载。
resolveConverts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection.

成员详情 ​

parseInput ​

ts
Tool.parseInput(value: unknown): unknown
参数类型传递说明
valueunknown必需—

返回: unknown。

parseOutput ​

ts
Tool.parseOutput(value: unknown): unknown
参数类型传递说明
valueunknown必需—

返回: unknown。

execute ​

ts
Tool.execute(input: unknown, context: ToolExecutionContext): unknown
参数类型传递说明
inputunknown必需—
contextToolExecutionContext必需—
context 的字段
字段类型必需说明
loggerScopedSubFlowLogger | undefined可省略只读。Delegate bound by Core to this actual invocation.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of the invocation, when recording is enabled.
mode"sync" | "async"必需只读。Declared execution discipline; mirrors whether the registry accepted an async tool.
invocationIdstring必需只读。Opaque invocation id shared by receipt, events and diagnostics.
signalAbortSignal必需只读。Non-optional cancellation for this invocation.
report(update: import("./execution/workflow.js").WorkflowNodeReport) => void必需Reports progress for the active node; Core supplies the execution identity.
createSubFlowLogger() => SubFlowLogger必需Bound to this context and owned by runtime; repeated calls in one visit return the same instance.

返回: unknown。

resolve ​

Converts validated arguments into the value handed back to the caller.

Runs inside the model call, so it must return synchronously; throwing rejects the selection.

ts
Tool.resolve(input: unknown, context: Readonly<{ stageId: string; }>): unknown

Converts validated arguments into the value handed back to the caller.

Runs inside the model call, so it must return synchronously; throwing rejects the selection.

参数类型传递说明
inputunknown必需—
contextReadonly<{ stageId: string; }>必需—
context 的字段
字段类型必需说明
stageIdstring必需见类型定义及调用场景。

返回: unknown。

使用示例 ​

类拥有配置与私有辅助方法,不手写 new ​

入门 · TypeScript 标准装饰器

ts
import { AgentCoreEngine, defineWorkflow } from '@jangts/agent-core';
import type { WorkflowNode } from '@jangts/agent-core';
import { node, nodePool } from '@jangts/agent-core/support/decorators';
import type { Node } from '@jangts/agent-core/support/decorators';
type State = { text: string; answer?: string };
@node<State, string>('greet', { ns: 'greeting' })
class Greeting implements Node<State, string> {
  readonly description = '生成问候';
  readonly routes = [{ complete: (state: Readonly<State>) => state.answer! }];
  #prefix = '你好,';
  run(state: Readonly<State>) { return { answer: this._format(state.text) }; }
  private _format(text: string) { return this.#prefix + text.trim(); }
}
const flow = defineWorkflow<State, string>({ id: 'greeting', nodes: nodePool.getAll('greeting') });
console.log(await new AgentCoreEngine({ workflow: flow }).run({ text: '访客' }));
js
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { AgentCoreEngine, defineWorkflow } from '@jangts/agent-core';
import { node, nodePool } from '@jangts/agent-core/support/decorators';
let Greeting = class Greeting {
    description = '生成问候';
    routes = [{ complete: (state) => state.answer }];
    #prefix = '你好,';
    run(state) { return { answer: this._format(state.text) }; }
    _format(text) { return this.#prefix + text.trim(); }
};
Greeting = __decorate([
    node('greet', { ns: 'greeting' })
], Greeting);
const flow = defineWorkflow({ id: 'greeting', nodes: nodePool.getAll('greeting') });
console.log(await new AgentCoreEngine({ workflow: flow }).run({ text: '访客' }));

保留服务方法名,自动绑定原对象 ​

配置 · TypeScript 标准装饰器

ts
import type { WorkflowNode } from '@jangts/agent-core';
import { node, nodePool } from '@jangts/agent-core/support/decorators';
type State = { text: string; answer?: string };
class GreetingService {
  constructor(private readonly prefix: string) {}
  @node<State, string>('reply', { ns: 'service',
    description: '调用现有服务',
    routes: [{ complete: (state: Readonly<State>) => state.answer! }],
  })
  answer(state: Readonly<State>) { return { answer: this.prefix + state.text }; }
}
const service = new GreetingService('你好,');
const definition = nodePool.get<WorkflowNode<State, string>>('reply', 'service')!;
console.log(definition === nodePool.get('reply', 'service'));
js
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { node, nodePool } from '@jangts/agent-core/support/decorators';
class GreetingService {
    prefix;
    constructor(prefix) {
        this.prefix = prefix;
    }
    answer(state) { return { answer: this.prefix + state.text }; }
}
__decorate([
    node('reply', { ns: 'service',
        description: '调用现有服务',
        routes: [{ complete: (state) => state.answer }],
    })
], GreetingService.prototype, "answer", null);
const service = new GreetingService('你好,');
const definition = nodePool.get('reply', 'service');
console.log(definition === nodePool.get('reply', 'service'));

相关定义 ​

接口参考、使用示例与开发指南。