Skip to content

Users(用户卡)

用户卡代表参与对话的用户角色。一个账号下面可以有多个用户卡,每个会话在创建时需要绑定一个用户卡。

什么时候需要看这页

  • 你要创建新的用户卡
  • 你要查看或修改已有用户卡
  • 你要删除不再需要的用户卡

一个简单例子

bash
# 创建一个用户卡
curl -X POST http://localhost:3000/users \
  -H 'Content-Type: application/json' \
  -d '{
    "snapshot": {
      "name": "Player",
      "description": "A brave adventurer."
    }
  }'

# 列出所有活跃用户卡
curl "http://localhost:3000/users?status=active"

先理解几个词

这里的意思
snapshot用户卡的完整快照,包括名字和描述
user这里指用户卡,不是账号

User 对象

字段类型说明
idstring用户卡 ID
namestring用户名称
statusstringactive / disabled / deleted
snapshotobject用户快照(name, description 等)
revisioninteger用户资源版本号,用于并发写入 CAS
created_atinteger创建时间
updated_atinteger更新时间

创建用户卡

http
POST /users

请求体

字段类型必填说明
snapshotobject用户快照,必须包含 name
json
{
  "snapshot": {
    "name": "Player",
    "description": "A brave adventurer."
  }
}

响应 201

返回 { "data": User }

错误

状态码code说明
400validation_error请求体校验失败
409user_conflict同账号下用户名称冲突
503resource_busySQLite busy / locked 重试耗尽

列出用户卡

http
GET /users

查询参数

参数类型说明
statusstring按状态过滤
include_deletedboolean是否包含已删除(默认 false
keywordstring按名称搜索
sort_bystringupdated_at(默认)/ created_at / name
sort_orderstringasc / desc
limitinteger每页条数,默认 50
offsetinteger偏移量,默认 0

获取用户卡详情

http
GET /users/:id

更新用户卡

http
PATCH /users/:id

至少提供一个字段。可更新:snapshotstatus

可选字段:expected_revision。传入后,服务端会按 CAS 方式校验用户当前 revision;不匹配时返回 user_revision_conflict

json
{
  "expected_revision": 3,
  "status": "disabled"
}

错误

状态码code说明
400validation_error请求体为空或字段校验失败
404not_found用户不存在,或已处于 deleted 状态
409user_revision_conflict / user_conflictexpected_revision 过期,或同账号下用户名称冲突
503resource_busySQLite busy / locked 重试耗尽

软删除用户卡

http
DELETE /users/:id

将用户卡状态设为 deleted

可选请求体:{ "expected_revision": 3 }

响应 200

json
{ "data": { "id": "usr_001", "deleted": true, "revision": 4 } }

错误

状态码code说明
400validation_error请求体校验失败
404not_found用户不存在,或已处于 deleted 状态
409user_revision_conflictexpected_revision 过期
503resource_busySQLite busy / locked 重试耗尽

批量更新用户状态

http
PATCH /users/batch/status

批量更新用户卡状态。每次最多 100 条,不允许重复 ID。目标状态仅限 activedisabled。已删除(status: deleted)的用户卡视为 not_found

请求体

字段类型必填说明
idsstring[]用户卡 ID 数组,1-100 条,不允许重复
statusstring目标状态:active / disabled

请求示例

json
{
  "ids": ["usr_001", "usr_002", "usr_missing"],
  "status": "disabled"
}

响应 200

json
{
  "data": {
    "results": [
      { "index": 0, "id": "usr_001", "action": "updated" },
      { "index": 1, "id": "usr_002", "action": "updated" },
      { "index": 2, "id": "usr_missing", "action": "not_found" }
    ],
    "meta": { "total": 3, "updated": 2, "not_found": 1, "status": "disabled" }
  }
}

错误

状态码code说明
400validation_error请求体校验失败、ids 为空或超过 100 条、存在重复 ID
503resource_busySQLite busy / locked 重试耗尽

批量删除用户

http
POST /users/batch/delete

批量软删除用户卡(将状态设为 deleted)。每次最多 100 条,不允许重复 ID。已处于 deleted 状态的用户卡视为 not_found

请求体

字段类型必填说明
idsstring[]用户卡 ID 数组,1-100 条,不允许重复

请求示例

json
{
  "ids": ["usr_001", "usr_missing"]
}

响应 200

json
{
  "data": {
    "results": [
      { "index": 0, "id": "usr_001", "action": "deleted" },
      { "index": 1, "id": "usr_missing", "action": "not_found" }
    ],
    "meta": { "total": 2, "deleted": 1, "not_found": 1 }
  }
}

错误

状态码code说明
400validation_error请求体校验失败、ids 为空或超过 100 条、存在重复 ID
503resource_busySQLite busy / locked 重试耗尽

并发错误码

错误码说明
user_conflict用户重名冲突
user_revision_conflictexpected_revision 过期,或同一 revision 上的写入已被其他请求抢先提交
resource_busySQLite busy / locked 重试耗尽