Skip to content

创建会话

接口说明

选择智能体后,调用此接口在服务端创建一条会话记录,并返回 conversationId

后续所有需要会话上下文的接口(流式聊天、灵感回复、图片生成)均需携带此 conversationId

使用时机:用户选择/切换智能体时调用一次,整个对话周期内复用同一个 conversationId;切换至新智能体时重新调用本接口获取新的 conversationId

基本信息

项目说明
接口路径POST /v1/chat/conversation
响应类型application/json
认证方式API Key + 请求签名(认证说明

请求体

json
{
  "agentId": "agent-uuid"
}
字段类型必填说明
agentIdstring智能体 ID,从智能体列表接口 /v1/agent/list 获取

签名示例(仅 agentId 一个字段):

待签名字符串 = "agentId=agent-uuid" + apiSecret
X-Signature = sha256(待签名字符串)

响应格式

成功响应

json
{
  "code": 10200,
  "success": true,
  "message": "OK",
  "data": {
    "conversationId": "a1b2c3d4e5f6..."
  },
  "traceId": "9fU0kfWy3E8YFGjS5S8bLPlAXG8xhdTH",
  "timestamp": "1773298769366"
}
字段类型说明
data.conversationIdstring会话 ID(UUID 格式去除连字符),后续所有聊天相关接口均需传入此值

错误响应

HTTP 状态码说明
401认证失败
400请求参数缺失或格式错误
500服务端内部错误

业务错误(HTTP 200):

message说明
agentId is required缺少 agentId 字段
Invalid API keyAPI Key 无效或已禁用
Too many requests, please try again later触发频率限制
API key usage limit reached达到总调用次数上限

客户端示例

javascript
async function createConversation(apiKey, apiSecret, userId, agentId) {
  const body = { agentId }
  const headers = await buildAuthHeaders(apiKey, apiSecret, userId, body)

  const response = await fetch('/v1/chat/conversation', {
    method: 'POST',
    headers,
    body: JSON.stringify(body),
  })

  const result = await response.json()
  if (!result.success) throw new Error('创建会话失败:' + result.message)
  return result.data.conversationId
}

使用时序

Client                         Sonaops Open API              PostgreSQL
  │                                   │                           │
  │  [用户选择智能体]                   │                           │
  │                                   │                           │
  │  POST /v1/chat/conversation       │                           │
  │  { "agentId": "agent-uuid" }      │                           │
  │──────────────────────────────────>│                           │
  │                                   │  INSERT open_chat_conversation
  │                                   │──────────────────────────>│
  │                                   │<──────────────────────────│
  │  { "data": { "conversationId":    │                           │
  │    "a1b2c3d4..." } }              │                           │
  │<──────────────────────────────────│                           │

conversationId 生命周期

时机操作
用户选择/切换智能体调用本接口创建新会话,获得新 conversationId
同一智能体持续对话复用同一 conversationId,无需重新创建
用户退出/页面刷新conversationId 可持久化(localStorage 等),继续使用已有会话

Released under the MIT License.