CLI 使用介绍

更新时间:
复制 MD 格式

tablestore-agent-cli 提供记忆库管理、记忆写入、长期记忆检索、短期记忆查询、请求审计和 Dashboard 能力,适用于本地调试、运维管理和自动化脚本。

安装

通过 npm 全局安装 CLI,要求 Node.js 18 或以上版本。使用记忆整理(memory dream)能力需 CLI 版本不低于 0.2.5。

npm install -g @tablestore/tablestore-agent-cli
tablestore-agent-cli version

配置

CLI 首次使用前需配置访问凭证、表格存储实例和记忆服务默认值。配置项写入本地配置文件,环境变量优先级高于配置文件。

配置访问凭证

tablestore-agent-cli configure set access_key_id '...'
tablestore-agent-cli configure set access_key_secret '...'
tablestore-agent-cli configure set region 'cn-beijing'

配置表格存储实例

tablestore-agent-cli configure set ots_endpoint 'https://<instance>.cn-beijing.ots.aliyuncs.com'
tablestore-agent-cli configure set ots_instance_name '<instance-name>'

未配置 ots_endpointots_instance_name 时,CLI 会在执行 doctor 或实际操作时自动创建并复用托管表格存储实例。

配置记忆服务默认值

tablestore-agent-cli configure set memory_store_name 'agent_memory'
tablestore-agent-cli configure set memory_app_id 'app-001'
tablestore-agent-cli configure set memory_tenant_id 'tenant-001'
tablestore-agent-cli configure set memory_agent_id 'agent-001'
tablestore-agent-cli configure set memory_run_id 'run-001'

配置默认值后,后续命令可省略对应参数。

配置文件与环境变量

配置文件路径:

~/.config/tablestore-agent-cli/config.toml

环境变量优先级高于配置文件,对应关系如下:

环境变量

配置项

TABLESTORE_ACCESS_KEY_ID

access_key_id

TABLESTORE_ACCESS_KEY_SECRET

access_key_secret

TABLESTORE_ENDPOINT

ots_endpoint

TABLESTORE_INSTANCE_NAME

ots_instance_name

TABLESTORE_REGION

region

TABLESTORE_MEMORY_STORE_NAME

memory_store_name

TABLESTORE_MEMORY_APP_ID

memory_app_id

TABLESTORE_MEMORY_TENANT_ID

memory_tenant_id

TABLESTORE_MEMORY_AGENT_ID

memory_agent_id

TABLESTORE_MEMORY_RUN_ID

memory_run_id

诊断

执行 doctor memory 检查访问凭证、表格存储实例配置和记忆服务连通性。任一检查项失败时命令返回非 0 退出码。

tablestore-agent-cli doctor memory

记忆库管理

记忆库(Memory Store)是长期记忆和短期记忆的存储单元,每个记忆库对应表格存储实例上的一组数据表与索引。

创建记忆库

tablestore-agent-cli memory create --store agent_memory --description "Agent 长期记忆库"

记忆库创建后会异步初始化二级索引,需等待约 1 分钟索引就绪后才能正常写入和检索。

列出记忆库

tablestore-agent-cli memory list
tablestore-agent-cli memory ls --limit 50 --next-token <token>

lslist 的别名。该命令单页返回,如需翻页继续读取,请使用响应中的 nextToken

查看记忆库

tablestore-agent-cli memory describe --store agent_memory
tablestore-agent-cli memory show --store agent_memory

showdescribe 的别名。

更新记忆库

tablestore-agent-cli memory update --store agent_memory --description "新的描述"

删除记忆库

tablestore-agent-cli memory delete --store agent_memory
tablestore-agent-cli memory rm --store agent_memory -y

rmdelete 的别名。TTY 环境下删除会提示确认,传入 -y 跳过确认。

写入记忆

写入记忆需指定完整 Scope,包含 app-idtenant-idagent-idrun-id 四段。写入命令的 Scope 不允许使用通配符 *

写入文本

通过 --text 直接传入预加工的文本:

tablestore-agent-cli memory add \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001 \
  --text "用户喜欢喝咖啡"

同步写入

默认写入为异步模式,文本由服务端在后台抽取记忆。需要写入完成后立即可检索时使用 --sync

tablestore-agent-cli memory add \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001 \
  --text "用户偏好简洁的回答风格" \
  --sync

通过消息文件写入

通过 --messages-file 传入对话消息列表,文件内容必须为 JSON 数组:

tablestore-agent-cli memory add \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001 \
  --messages-file ./messages.json

附加元数据

通过 --metadata 传入 JSON 字符串,附加业务自定义字段:

tablestore-agent-cli memory add \
  --store agent_memory \
  --app-id app-001 \
  --text "用户喜欢川菜" \
  --metadata '{"source":"chat","topic":"preference"}'

检索长期记忆

通过 memory search 进行语义检索。Scope 中 tenant-idagent-idrun-id 支持反引号包裹的通配符 *,跨范围聚合命中。

tablestore-agent-cli memory search \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id '*' \
  --run-id '*' \
  --query "用户喜欢什么食物" \
  --top-k 5

--top-k 默认值为 10,取值范围 1~50

关闭 Rerank:

tablestore-agent-cli memory search \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --query "用户偏好" \
  --disable-rerank

Rerank 默认启用,传入 --disable-rerank 关闭重排,结果仅按向量距离排序。

使用 Metadata 精确匹配过滤:

tablestore-agent-cli memory search \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --query "用户偏好" \
  --metadata '{"source":"chat"}'

响应 results 列表的每个元素包含命中的记忆单元 unit 与得分 scoreunit 内部字段使用 snake_case 命名,详见 API 接口介绍

管理长期记忆

针对单条长期记忆单元的查询、更新与删除操作。memory-idmemory add 响应或 list-units 命令返回。获取、更新、删除单条记忆时必须传入与写入时一致的完整 Scope(app-idtenant-idagent-idrun-id 四段全填,不允许使用通配符 *)。

列出记忆单元

tablestore-agent-cli memory list-units --store agent_memory --app-id app-001
tablestore-agent-cli memory ls-units --store agent_memory --app-id app-001 --limit 20 --next-token <token>

ls-unitslist-units 的别名。

获取单条记忆

tablestore-agent-cli memory get \
  --store agent_memory \
  --memory-id mem-001 \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001

catget 的别名。

更新单条记忆

tablestore-agent-cli memory update-unit \
  --store agent_memory \
  --memory-id mem-001 \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001 \
  --text "用户偏好咖啡,且喜欢简洁回答"

删除单条记忆

tablestore-agent-cli memory delete-unit \
  --store agent_memory \
  --memory-id mem-001 \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001

rm-unitdelete-unit 的别名。

查询短期记忆和审计

查询短期记忆

tablestore-agent-cli memory msg-list \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001

短期记忆查询要求 Scope 四级(app-idtenant-idagent-idrun-id)全部填写,不允许使用通配符 *ls-msgsmsg-list 的别名。

查询请求审计

--operation 过滤指定操作类型的请求审计记录,Scope 中 tenant-idagent-idrun-id 支持反引号包裹的通配符 *

tablestore-agent-cli memory req-list \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id '*' \
  --agent-id '*' \
  --run-id '*' \
  --operation AddMemories

ls-reqsreq-list 的别名。

列出 Scope

memory scope-list(别名 ls-scopes)列出记忆库中已存在的 Scope,便于发现某应用或租户下有哪些 Agent 和会话产生过记忆。Scope 中 tenant-idagent-idrun-id 支持通配符 *

tablestore-agent-cli memory scope-list \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id '*' \
  --agent-id '*' \
  --run-id '*'

添加 --all 自动翻页并在客户端去重,--max-pages 限制翻页上限(默认 20)。

跟踪异步任务

memory add 默认异步写入,可用返回的 requestId 跟踪记忆抽取任务的执行状态。

获取单个任务:

tablestore-agent-cli memory task get \
  --store agent_memory \
  --request-id <requestId>

列出任务,可按状态过滤(queuedrunningcompletedfailedneeds_reconcile):

tablestore-agent-cli memory task list \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001 \
  --status completed

showtask get 的别名,lstask list 的别名。首次写入后任务索引建立期间,task get 可能短暂返回冲突错误,稍后重试即可。

记忆整理(Dream)

memory dream 子命令用于创建和管理记忆整理任务,对已有记忆去重、合并、精炼,并可提取技能与画像。

创建整理任务

tablestore-agent-cli memory dream create \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --apply-mode proposal

常用选项:--apply-mode proposal|safe_auto--scope-output-mode preserve_scope|promote_scope--instructions <text>--confidence-thresholds--max-sessions--max-messages--max-memories--min-timestamp--max-timestamp。多 Scope 整理使用 --scopes-file <path>(与单 Scope 选项互斥)。

查询整理任务

tablestore-agent-cli memory dream get --store agent_memory --dream-id <dreamId>
tablestore-agent-cli memory dream list --store agent_memory --app-id app-001 --tenant-id user-001

查看与应用整理动作

tablestore-agent-cli memory dream actions \
  --store agent_memory \
  --dream-id <dreamId> \
  --status proposed
tablestore-agent-cli memory dream apply \
  --store agent_memory \
  --dream-id <dreamId> \
  --action-ids <actionId1>,<actionId2>

dream actions(别名 ls-actions)支持 --status--action--min-confidence--max-confidence--order-by 等过滤条件;dream apply 也可通过 --action-ids-file <path> 传入包含动作 ID 的 JSON 数组文件。

取消进行中的整理任务:

tablestore-agent-cli memory dream cancel \
  --store agent_memory \
  --dream-id <dreamId>

面向脚本的输出

在脚本或 Agent 中调用 CLI 时,使用 -q(或 --quiet)只输出响应中的 data 字段,便于管道处理:

tablestore-agent-cli -q memory search \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --query "用户喜欢什么饮品"

Dashboard

CLI 内置 Web Dashboard,用于可视化管理与调试。

tablestore-agent-cli dashboard start
tablestore-agent-cli dashboard start -p 9999
tablestore-agent-cli dashboard start --host 0.0.0.0

Dashboard 默认监听 127.0.0.1:3000,浏览器打开该地址即可访问。-p 自定义端口;--host 0.0.0.0 允许从其他机器访问。