/api/v1/sessions/{id}/context 接口用于获取供模型推理和上下文组装使用的会话上下文。
返回结果会同时包含最近归档摘要、较早归档的轻量摘要列表、当前未归档消息以及对应的 Token 统计信息,适合 Agent 在推理前拼装上下文。
完成 API 鉴权说明 页面的 API Key 获取后,可调用本接口获取组装后的会话上下文。
/api/v1/sessions/{id}/context
统一资源标识符。
GET
客户端对服务器请求的操作类型。
参数 | 值 | 说明 |
|---|---|---|
Authorization |
| 鉴权 |
X-OpenViking-Agent |
| Agent ID |
参数 | 类型 | 位置 | 必选 | 默认值 | 备注 |
|---|---|---|---|---|---|
id | string | Path | 是 | -- | 会话 ID |
token_budget | int | Query | 否 | 128000 | 在当前 active messages 之后,分配给归档上下文组装结果的 Token 预算。 |
字段 | 参数说明 |
|---|---|
status | 请求状态,成功为 |
result | 成功时返回的数据对象 |
result.latest_archive_overview | 最新一条已完成归档的 overview 文本。若没有可用 completed archive,或 overview 超出预算,则可能为空字符串。 |
result.pre_archive_abstracts | 更早归档的轻量摘要列表,每项包含 |
result.messages | 当前会话中未归档和 live 消息列表 |
result.estimatedTokens | 本次返回上下文的估算 Token 数 |
result.stats | 本次组装过程的统计信息 |
result.stats.totalArchives | 总归档数 |
result.stats.includedArchives | 被纳入结果的归档数 |
result.stats.droppedArchives | 因预算不足被丢弃的归档数 |
result.stats.failedArchives | 读取失败的归档数 |
result.stats.activeTokens | 当前活动消息消耗的 Token 数 |
result.stats.archiveTokens | 归档内容消耗的 Token 数 |
error | 失败时返回的错误对象 |
error.code | 错误码字符串 |
error.message | 可读的错误描述 |
error.code | 说明 |
|---|---|
UNAUTHENTICATED | 缺少 API Key 或 API Key 无效 |
PERMISSION_DENIED | API Key 权限不足 |
NOT_FOUND | 会话不存在 |
INVALID_ARGUMENT |
|
curl -X GET `https://xxx/api/v1/sessions/a1b2c3d4/context?token_budget=128000` \ -H "Authorization: Bearer {api_key}" \ -H "X-OpenViking-Agent: {agent_id}"
执行成功返回:
{ "status": "ok", "result": { "latest_archive_overview": "# Session Summary\\n\\n**Overview**: User discussed deployment and auth setup.", "pre_archive_abstracts": [ { "archive_id": "archive_002", "abstract": "用户讨论了部署和鉴权配置。" }, { "archive_id": "archive_001", "abstract": "用户之前讨论了仓库初始化和鉴权配置。" } ], "messages": [ { "id": "msg_pending_1", "role": "user", "parts": [ { "type": "text", "text": "Pending user message" } ], "created_at": "2026-03-24T09:10:11Z" }, { "id": "msg_live_1", "role": "assistant", "parts": [ { "type": "text", "text": "Current live message" } ], "created_at": "2026-03-24T09:10:20Z" } ], "estimatedTokens": 160, "stats": { "totalArchives": 2, "includedArchives": 2, "droppedArchives": 0, "failedArchives": 0, "activeTokens": 98, "archiveTokens": 62 } } }