管理员上下文 — TypeScript SDK
控制客户上下文记录开关、查询记录列表并导出正文。
概览
管理员 Context 资源控制终端客户的上下文记录,并导出已记录条目。可开启或关闭记录,按日期范围查询条目,以及获取单条记录的输入输出数据。
通过 admin.context 访问。
可用操作
| 方法 | 说明 |
|---|---|
toggle() | 开启或关闭客户的上下文记录 |
list() | 查询日期范围内的上下文记录 |
getBody() | 获取单条记录的输入输出数据 |
toggle
开启或关闭客户的上下文记录。
使用示例
const ok = await admin.context.toggle(42, true);
console.log(ok); // true
// Turn it back off
await admin.context.toggle(42, false);
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| userId | number | 是 | -- | 目标客户 ID(位置参数) |
| enabled | boolean | 是 | -- | true 开启记录,false 关闭记录(位置参数) |
响应
返回 boolean,成功时为 true。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2007 | PermissionError | 管理员缺少上下文管理权限 |
3001 | AuthenticationError | 会话过期且刷新失败 |
list
查询客户的上下文记录,可按日期范围、追踪 ID 或 API 密钥 ID 筛选。
使用示例
const result = await admin.context.list({
userId: 42,
startDate: '2026-07-01',
endDate: '2026-07-14',
});
console.log(result.totalEntries, result.totalBytes, result.truncated);
for (const entry of result.entries) {
console.log(entry);
}
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| userId | number | 是 | -- | 目标客户 ID |
| startDate | string | 否 | -- | 范围开始日期(YYYY-MM-DD) |
| endDate | string | 否 | -- | 范围结束日期(YYYY-MM-DD) |
| traceIdFilter | string | 否 | -- | 按追踪 ID 筛选 |
| apiKeyId | number | 否 | -- | 按 API 密钥 ID 筛选 |
| maxTraces | number | 否 | -- | 最多返回的追踪记录数 |
响应
返回包含 entries、totalEntries、totalBytes、truncated 的对象。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2007 | PermissionError | 管理员缺少上下文管理权限 |
3001 | AuthenticationError | 会话过期且刷新失败 |
getBody
获取单条上下文记录正文,即输入输出数据。
使用示例
const body = await admin.context.getBody({
uid: 42,
partitionDate: '2026-07-14',
traceId: 'trace-abc',
visibility: 'PLATFORM',
});
console.log(body.inputJson);
console.log(body.outputJsonl);
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| uid | number | 是 | -- | 目标客户 ID |
| partitionDate | string | 是 | -- | 记录分区日期(YYYY-MM-DD) |
| traceId | string | 是 | -- | 记录的追踪 ID |
| visibility | string | 是 | -- | 记录可见范围 |
响应
返回包含 traceId、uid、visibility、inputJson、outputJsonl 的对象。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2007 | PermissionError | 管理员缺少上下文管理权限 |
3001 | AuthenticationError | 会话过期且刷新失败 |
| 服务器 4xx | WltError | 没有匹配指定 traceId / partitionDate 的记录 |