/api/v1/sessions/{id}/messages 接口用于向指定会话追加一条消息。
该接口支持两种写法:
content 传入纯文本内容的简单模式parts 传入结构化消息内容的 Parts 模式如果目标会话不存在,系统会在首次添加消息时自动创建会话。
完成 API 鉴权说明 页面的 API Key 获取后,可调用本接口写入消息。
/api/v1/sessions/{id}/messages
统一资源标识符。
POST
客户端对服务器请求的操作类型。
参数 | 值 | 说明 |
|---|---|---|
Content-Type |
| 请求消息类型 |
Authorization |
| 鉴权 |
X-OpenViking-Agent |
| Agent ID |
参数 | 类型 | 位置 | 必选 | 默认值 | 备注 |
|---|---|---|---|---|---|
id | string | Path | 是 | -- | 会话 ID |
role | string | Body | 是 | -- | 消息角色,取值通常为 |
content | string | Body | 条件必选 | -- | 纯文本消息内容,和 |
parts | array | Body | 条件必选 | -- | 结构化消息片段,和 |
created_at | string | Body | 否 | -- | ISO 8601 时间戳 |
parts 中常见片段类型包括:
text:文本片段context:上下文片段tool:工具调用片段字段 | 参数说明 |
|---|---|
status | 请求状态,成功为 |
result | 成功时返回的数据对象 |
result.session_id | 会话 ID |
result.message_count | 当前会话中的消息数量 |
error | 失败时返回的错误对象 |
error.code | 错误码字符串 |
error.message | 可读的错误描述 |
error.code | 说明 |
|---|---|
UNAUTHENTICATED | 缺少 API Key 或 API Key 无效 |
PERMISSION_DENIED | API Key 权限不足 |
INVALID_ARGUMENT | 未提供 |
curl -X POST `https://xxx/api/v1/sessions/a1b2c3d4/messages` \ -H "Authorization: Bearer {api_key}" \ -H "X-OpenViking-Agent: {agent_id}" \ -H "Content-Type: application/json" \ -d '{ "role": "user", "content": "How do I authenticate users?" }'
curl -X POST `https://xxx/api/v1/sessions/a1b2c3d4/messages` \ -H "Authorization: Bearer {api_key}" \ -H "X-OpenViking-Agent: {agent_id}" \ -H "Content-Type: application/json" \ -d '{ "role": "assistant", "parts": [ {"type": "text", "text": "Based on the authentication guide..."}, {"type": "context", "uri": "viking://resources/docs/auth/", "context_type": "resource", "abstract": "Auth guide"} ] }'
执行成功返回:
{ "status": "ok", "result": { "session_id": "a1b2c3d4", "message_count": 2 } }
curl -X POST http://localhost:1933/api/v1/sessions/a1b2c3d4/messages/batch \ -H "Authorization: Bearer {api_key}" \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "user", "content": "How do I authenticate users?"}, {"role": "assistant", "content": "You can use OAuth 2.0 for authentication."}, {"role": "user", "content": "Any specific recommendations?"} ] }'