创建聊天
在用户和 AI 代理之间创建聊天会话。
端点
POST https://api.xmz.ai/v1/agentrix/chats
认证
需要在授权头中提供 API 密钥:
Authorization: Bearer YOUR_API_KEY
重要提示
- 幂等性 :使用相同的
type和members调用,如果已存在,则返回现有的聊天 - 永久性 :聊天 ID 永远不会过期,并且可以无限期地重用
- 前提条件 : 在创建任务之前,需要先创建聊天
请求体
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
| type | 字符串 | 没有 | 聊天类型: direct 或 group (默认: direct) |
| members | array | 是 | 成员对象数组 |
成员对象
| 字段 | 类型 | 必需 | 描述 |
|---|---|---|---|
| memberCode | 字符串 | 是 | 代理 ID(当 type='agent' 时)或用户 ID(当 type='human' 时) |
| type | 字符串 | 是 | 成员类型:agent 或 human |
请求体架构
{
"type": "direct",
"members": [
{
"memberCode": "agent-codex-v1",
"type": "agent"
}
]
}
响应
成功响应(201 创建)
返回创建或现有的聊天会话:
{
"id": "chat-xxx",
"owner": "user-xxx",
"type": "direct",
"title": null,
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-15T10:30:00Z",
"defaultMachineId": null,
"defaultCloudId": null,
"defaultRepositoryId": null,
"defaultBaseBranch": null,
"members": [
{
"id": "member-xxx",
"chatId": "chat-xxx",
"memberCode": "agent-codex-v1",
"type": "agent",
"role": "agent",
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-15T10:30:00Z"
}
]
}
响应字段
| 字段 | 类型 | 描述 |
|---|---|---|
| id | 字符串 | 聊天标识符(创建任务时使用此作为 chatId) |
| owner | 字符串 | 聊天所有者(群聊的用户 ID,直接聊天的成员 Codes 排序) |
| type | 字符串 | 聊天类型:direct 或 group |
| title | string | null |
| createdAt | 字符串 | ISO 8601 创建时间戳 |
| updatedAt | 字符串 | ISO 8601 最后更新时间戳 |
| defaultMachineId | string | null |
| defaultCloudId | string | null |
| defaultRepositoryId | string | null |
| defaultBaseBranch | string | null |
| members | 数组 | 聊天成员数组 |
成员对象字段
| 字段 | 类型 | 描述 |
|---|---|---|
| id | 字符串 | 成员标识符 |
| chatId | 字符串 | 聊天标识符 |
| memberCode | 字符串 | 代理 ID 或用户 ID |
| type | 字符串 | 成员类型:agent 或 human |
| role | 字符串 | 成员角色 |
| createdAt | 字符串 | ISO 8601 时间戳 |
| updatedAt | 字符串 | ISO 8601 时间戳 |
错误响应
400 错误请求
{
"code": "INVALID_REQUEST",
"error": "validation_error",
"message": "Invalid request parameters"
}
401 未授权
{
"code": "UNAUTHORIZED",
"error": "unauthorized",
"message": "Invalid or missing API key"
}
示例
创建与 AI 代理的直接聊天:
curl -X POST https://api.xmz.ai/v1/agentrix/chats \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "direct",
"members": [
{
"memberCode": "agent-codex-v1",
"type": "agent"
}
]
}'
工作流
- 创建聊天 :使用所需的代理调用此端点
- 保存聊天 ID: 存储返回的
id字段(这是您的chatId) - 重用聊天 ID: 使用相同的
chatId进行多个任务 - 创建任务 : 创建任务时传递
chatId(参见 Create Task)
聊天类型
直接聊天
与代理进行一对一对话时,请使用 type: "direct"。这是 API 使用中最常见的类型。
{
"type": "direct",
"members": [
{
"memberCode": "agent-codex-v1",
"type": "agent"
}
]
}
群组聊天
对于多成员对话(多个代理或用户),请使用 type: "group"。
{
"type": "group",
"members": [
{
"memberCode": "agent-codex-v1",
"type": "agent"
},
{
"memberCode": "agent-claude-v1",
"type": "agent"
}
]
}
幂等性
多次使用相同的 type 和 members 调用此端点将返回现有的聊天,而不是创建重复的聊天。这确保您可以安全地调用端点,而不用担心创建具有相同代理的多个聊天。
示例 :如果您使用相同的代理调用端点两次,两次调用都将返回相同的 chatId。
相关端点
- Create Task - 使用聊天 ID 创建任务
- 列出聊天 - 查看所有聊天 (GET
/v1/chats) - 获取聊天 - 检索聊天详情 (GET
/v1/chats/:chatId) - 添加成员 - 向聊天中添加成员 (POST
/v1/chats/:chatId/members) - 移除成员 - 从聊天中移除成员 (DELETE
/v1/chats/:chatId/members)