Skip to content
English

AgentCachePort ​

CORE · interface · 类型合同

App-owned storage. Capacity, expiry, serialization and user isolation belong to the App.

导入 ​

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

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

定义 ​

ts
interface AgentCachePort

字段结构 ​

字段类型必需说明
supportsPersistenceboolean | undefined可省略只读。见类型定义及调用场景。
get(address: AgentCacheAddress) => Promise<unknown>必需见类型定义及调用场景。
has(address: AgentCacheAddress) => Promise<boolean>必需见类型定义及调用场景。
set(address: AgentCacheAddress, value: unknown) => Promise<void>必需见类型定义及调用场景。
delete(address: AgentCacheAddress) => Promise<boolean>必需见类型定义及调用场景。
clear(partition: Omit<AgentCacheAddress, "key">) => Promise<void>必需见类型定义及调用场景。

成员目录 ​

成员用途
get完整输入输出见下方重载。
has完整输入输出见下方重载。
set完整输入输出见下方重载。
delete完整输入输出见下方重载。
clear完整输入输出见下方重载。

成员详情 ​

get ​

ts
AgentCachePort.get(address: AgentCacheAddress): Promise<unknown>
参数类型传递说明
addressAgentCacheAddress必需—
address 的字段
字段类型必需说明
nodeIdstring必需只读。见类型定义及调用场景。
keystring必需只读。见类型定义及调用场景。
persistenceboolean必需只读。见类型定义及调用场景。
signalAbortSignal必需只读。见类型定义及调用场景。

返回: Promise<unknown>。

等待后的值: unknown。

has ​

ts
AgentCachePort.has(address: AgentCacheAddress): Promise<boolean>
参数类型传递说明
addressAgentCacheAddress必需—
address 的字段
字段类型必需说明
nodeIdstring必需只读。见类型定义及调用场景。
keystring必需只读。见类型定义及调用场景。
persistenceboolean必需只读。见类型定义及调用场景。
signalAbortSignal必需只读。见类型定义及调用场景。

返回: Promise<boolean>。

等待后的值: boolean。

set ​

ts
AgentCachePort.set(address: AgentCacheAddress, value: unknown): Promise<void>
参数类型传递说明
addressAgentCacheAddress必需—
valueunknown必需—
address 的字段
字段类型必需说明
nodeIdstring必需只读。见类型定义及调用场景。
keystring必需只读。见类型定义及调用场景。
persistenceboolean必需只读。见类型定义及调用场景。
signalAbortSignal必需只读。见类型定义及调用场景。

返回: Promise<void>。

等待后的值: void。

delete ​

ts
AgentCachePort.delete(address: AgentCacheAddress): Promise<boolean>
参数类型传递说明
addressAgentCacheAddress必需—
address 的字段
字段类型必需说明
nodeIdstring必需只读。见类型定义及调用场景。
keystring必需只读。见类型定义及调用场景。
persistenceboolean必需只读。见类型定义及调用场景。
signalAbortSignal必需只读。见类型定义及调用场景。

返回: Promise<boolean>。

等待后的值: boolean。

clear ​

ts
AgentCachePort.clear(partition: Omit<AgentCacheAddress, "key">): Promise<void>
参数类型传递说明
partitionOmit<AgentCacheAddress, "key">必需—
partition 的字段
字段类型必需说明
signalAbortSignal必需只读。见类型定义及调用场景。
nodeIdstring必需只读。见类型定义及调用场景。
persistenceboolean必需只读。见类型定义及调用场景。

返回: Promise<void>。

等待后的值: void。

使用示例 ​

一个普通步骤即可运行 ​

入门 · Node.js

ts
import { AgentCoreEngine } from '@jangts/agent-core/engine';

const agent = new AgentCoreEngine<string>();
const answer = await agent.run(() => ({ type: 'complete', value: '你好,Core' }));
console.log(answer);
js
import { AgentCoreEngine } from '@jangts/agent-core/engine';
const agent = new AgentCoreEngine();
const answer = await agent.run(() => ({ type: 'complete', value: '你好,Core' }));
console.log(answer);

增加显式 Workflow,仍然调用 run ​

配置 · Node.js

ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';

type State = { name: string; answer?: string };
const workflow = defineWorkflow<State, string>({
  id: 'greeting',
  nodes: [{
    id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
    run: state => ({ answer: '你好,' + state.name }),
    routes: [route.deliver('answer')],
  }],
});

const agent = new AgentCoreEngine({ workflow });
console.log(await agent.run({ name: 'Ivan' }));
console.log(await agent.run({ name: '访客' }));
js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({
    id: 'greeting',
    nodes: [{
            id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
            run: state => ({ answer: '你好,' + state.name }),
            routes: [route.deliver('answer')],
        }],
});
const agent = new AgentCoreEngine({ workflow });
console.log(await agent.run({ name: 'Ivan' }));
console.log(await agent.run({ name: '访客' }));

相关策略源码 ​

相关定义 ​

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