HTTP · JSON · SSE

Alice 接口文档Alice API

Alice 是一个持续存在的叙事角色。这组接口让任何网页或脚本以访客身份和她对话:申请访客码、发送消息、实时接收回复。

Alice is a persistent narrative character. These endpoints let any web page or script talk to her as a visitor: get a visitor code, send messages and receive replies in real time.

BASEhttps://api.ignat.ai/alice/api
  1. 申请访客码POST /visitor 拿到 visitorId 和 visitorKey。
  2. Get a codePOST /visitor returns a visitorId and visitorKey.
  3. 订阅回复GET /events 打开事件流,回复从这里推送过来。
  4. SubscribeOpen the GET /events stream; replies are pushed there.
  5. 发送消息POST /message 立即返回 202,Alice 稍后通过事件流回复。
  6. SendPOST /message returns 202 at once; Alice answers later through the stream.

回复是异步的:Alice 可能马上回、过一会儿回、分几条回,也可能不回。不要在 /message 的响应里等回复。

Replies are asynchronous: Alice may answer right away, later, in several messages, or not at all. Never wait for a reply in the /message response.

通用约定Conventions

  • POST 请求体必须是 JSON,请求头 Content-Type: application/json,大小不超过 8 KB。
  • 允许任意来源跨域调用(Access-Control-Allow-Origin: *)。接口不用 cookie,也不需要。
  • 所有错误都返回 JSON:{"error": "..."},部分错误另带 code 字段,见错误。
  • 时间均为 ISO 8601 UTC 格式。
  • POST bodies must be JSON with Content-Type: application/json, at most 8 KB.
  • Any origin may call the API (Access-Control-Allow-Origin: *). No cookies are used or needed.
  • Every error is JSON: {"error": "..."}, some with an extra code field; see Errors.
  • Timestamps are ISO 8601 in UTC.

身份与凭证Identity & credentials

每个访客有一个独立身份,Alice 对不同访客的记忆互相隔离。身份分两种:

Each visitor has its own identity, and Alice keeps memories of different visitors apart. There are two kinds:

正式访客Visitor临时访客(guest)Guest
身份号IDv_ + 8 位chars, 如e.g. v_7KQ2MX9Pg_ + 22 位随机字符random chars
密码Key8 位 visitorKey,只在申请时返回一次8-char visitorKey, returned only once无,身份号本身就是凭证None; the ID itself is the credential
名字Name申请时必填,可改名Required, can be changed固定为“访客”+ g_ 后的前 6 位Fixed: “访客” + the 6 chars after g_
有效期Lifetime长期有效Long-lived闲置 60 分钟后作废Expires after 60 min idle

v_ 码和密码方便手动输入:不区分大小写,不含 0、O、1、I、L。凭证放置位置:

v_ codes and keys are made to be typed: case-insensitive, without 0, O, 1, I or L. Where the credentials go:

接口EndpointvisitorIdvisitorKey
POST /message, POST /name请求体body请求体body
GET /history查询参数query请求头 X-Visitor-Keyheader X-Visitor-Key
GET /events查询参数query查询参数(EventSource 不能带请求头)query (EventSource cannot send headers)

身份号或密码错误统一返回 401,不会提示是哪一个错了。同一身份号每小时最多输错 10 次,之后返回 429。所有访客都只有公开权限,凭证只用来区分访客。

A wrong ID or key always returns the same 401, so codes cannot be probed. Each code allows 10 failed attempts per hour, then 429. Every visitor has public access only; credentials just tell visitors apart.

快速上手Quick start

curl

shell
# 1. Get a visitor code
curl -X POST https://api.ignat.ai/alice/api/visitor \
  -H 'Content-Type: application/json' \
  -d '{"name": "Ada"}'
# → {"visitorId":"v_7KQ2MX9P","visitorKey":"H4TW8NRC","name":"Ada","renamesLeft":3}

# 2. Listen for replies (keep this running in another terminal)
curl -N 'https://api.ignat.ai/alice/api/events?visitorId=v_7KQ2MX9P&visitorKey=H4TW8NRC'

# 3. Say hello
curl -X POST https://api.ignat.ai/alice/api/message \
  -H 'Content-Type: application/json' \
  -d '{"visitorId":"v_7KQ2MX9P","visitorKey":"H4TW8NRC","text":"Hi, Alice"}'

JavaScript

browser
const API = 'https://api.ignat.ai/alice/api'
const post = (path, body) => fetch(API + path, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(body),
}).then(r => r.json())

const { visitorId, visitorKey } = await post('/visitor', { name: 'Ada' })
// Save both: the key is never shown again.

const events = new EventSource(
  `${API}/events?visitorId=${visitorId}&visitorKey=${visitorKey}`)
events.addEventListener('message', e => console.log(JSON.parse(e.data).text))
events.addEventListener('typing', e => console.log('typing:', JSON.parse(e.data).active))

await post('/message', { visitorId, visitorKey, text: 'Hi, Alice', clientId: crypto.randomUUID() })

POST/visitor

申请一个新的访客身份。名字会先经过审查,不合格就不发码。

Creates a new visitor identity. The name is reviewed first; no code is issued if it is refused.

请求体Body

字段Field类型Type说明Description
namestring1–24 个字符。申请正式访客时必填;临时访客不能带1–24 characters. Required for a visitor; not allowed for a guest
guest optionalbooleantrue 时申请临时访客码,只发 g_ 码不发密码true issues a guest code with no key

响应 201Response 201

json
{ "visitorId": "v_7KQ2MX9P", "visitorKey": "H4TW8NRC", "name": "Ada", "renamesLeft": 3 }

// guest: true
{ "visitorId": "g_Q3v…", "name": "访客Q3v…", "renamesLeft": 0 }

可能的错误Errors

  • 400 缺少名字或名字过长;临时访客带了名字时 code: "guest_no_name"
  • 422 name_rejected:名字未通过审查,reason 字段说明原因
  • 503 name_review_unavailable:审查暂时无法进行,稍后再试
  • 429 visitor_limit:这个 IP 申请的新身份太多(正式与临时合计,名字被拒的尝试也算);visitor_busy:全站这一小时的新身份已满。已有身份不受影响,按 Retry-After 等待
  • 400 name missing or too long; code: "guest_no_name" when a guest sends one
  • 422 name_rejected: the name failed review; reason explains why
  • 503 name_review_unavailable: review cannot run right now, try later
  • 429 visitor_limit: too many new identities from this IP (visitors and guests together, refused names included); visitor_busy: the site has issued its new identities for this hour. Existing identities are unaffected; wait Retry-After

从未发过消息的身份闲置 24 小时后会被清理。

Identities that never send a message are removed after 24 hours idle.

POST/message

发一条消息给 Alice。接收成功就返回 202;回复稍后通过 /events 推送,也会写进 /history。

Sends Alice a message. Returns 202 once accepted; replies arrive later on /events and are also stored in /history.

请求体Body

字段Field类型Type说明Description
visitorIdstring身份号Visitor ID
visitorKeystring密码;临时访客不需要Key; not needed for guests
textstring1–2000 个字符1–2000 characters
clientId optionalstring客户端生成的唯一编号(字母、数字、_、-,最长 64)。网络重试时带同一个值,不会重复发送Client-made unique ID (letters, digits, _, -, up to 64). Retrying with the same value never sends twice

响应 202Response 202

json
{ "id": "1042", "visitorId": "v_7KQ2MX9P" }

可能的错误Errors

  • 400 文本为空或超长
  • 401 缺少或错误的凭证;临时访客码过期时为 Guest code expired
  • 429 发送太快;ip_daily_limit:这个 IP 今天发得太多(换身份也算在一起);或 daily_limit:今天的对话额度用完了,reply 字段是一句可以直接展示的话,Retry-After 是距离重置的秒数
  • 503 Alice 暂时无法接收,消息不会保存
  • 400 empty or overlong text
  • 401 missing or wrong credentials; Guest code expired for an expired guest code
  • 429 sending too fast; ip_daily_limit: this IP has sent too much today (across all identities); or daily_limit: today's chat quota is used up. reply holds a line you can show as-is, and Retry-After gives seconds until reset
  • 503 Alice cannot take messages right now; the message is not stored

GET/events

Server-Sent Events 事件流,用浏览器的 EventSource 或 curl -N 打开。断线后客户端会在 5 秒后自动重连。

A Server-Sent Events stream; open it with the browser's EventSource or curl -N. Clients reconnect automatically 5 seconds after a drop.

查询参数Query

visitorId, visitorKey

事件Events

事件Event数据Data说明Description
message{id, role, text, at}Alice 的一条消息,role 为 "alice"One message from Alice; role is "alice"
typing{active}是否正在输入;最长 90 秒后自动变为 falseWhether Alice is typing; turns false after 90 s at most
event-stream
retry: 5000

event: typing
data: {"active":true}

event: message
data: {"id":"1043","role":"alice","text":"Hi. Who's this?","at":"2026-10-19T11:02:31.000Z"}

: ping

每 25 秒发送一行 : ping 注释保持连接。同一访客最多同时开 3 条流,超出时关闭最早的一条。

A : ping comment every 25 seconds keeps the connection open. Each visitor may hold 3 streams at once; opening another closes the oldest.

GET/history

读取最近 200 条聊天记录(从旧到新)。用来恢复对话,或补上断线期间错过的消息。

Returns the latest 200 messages, oldest first. Use it to restore a chat or catch up on messages missed while offline.

shell
curl 'https://api.ignat.ai/alice/api/history?visitorId=v_7KQ2MX9P' \
  -H 'X-Visitor-Key: H4TW8NRC'
json
{
  "visitorId": "v_7KQ2MX9P",
  "name": "Ada",
  "renamesLeft": 3,
  "messages": [
    { "id": "1042", "role": "visitor", "text": "Hi, Alice", "at": "2026-10-19T11:02:20.000Z" },
    { "id": "1043", "role": "alice", "text": "Hi. Who's this?", "at": "2026-10-19T11:02:31.000Z" }
  ]
}

POST/name

修改正式访客的名字。新名字同样要经过审查;每个自然月最多成功修改 3 次。临时访客不能改名(403)。

Changes a visitor's name. The new name is reviewed too; at most 3 successful changes per calendar month. Guests cannot be renamed (403).

json
// request
{ "visitorId": "v_7KQ2MX9P", "visitorKey": "H4TW8NRC", "name": "Ada L." }
// 200
{ "name": "Ada L.", "renamesLeft": 2 }
  • 429 rename_limit:本月次数已用完
  • 409 同一身份的另一次改名还在进行中
  • 422 / 503 与 /visitor 相同
  • 429 rename_limit: no changes left this month
  • 409 another rename for this identity is in progress
  • 422 / 503 same as /visitor

错误Errors

错误响应的 error 是给人看的英文说明;需要程序判断时请用 HTTP 状态码和 code 字段。

error is a human-readable message. Programs should branch on the HTTP status and the code field.

状态码Status含义Meaning
400参数不对,或 JSON 无法解析Bad field or unparsable JSON
401缺少或错误的凭证,临时访客码已过期Missing or wrong credentials, or an expired guest code
403临时访客不能执行此操作Not allowed for guests
404 / 405路径不存在 / 请求方法不对Unknown path / wrong method
409同类操作正在进行Same operation already in progress
413 / 415请求体超过 8 KB / 不是 JSONBody over 8 KB / not JSON
422名字未通过审查Name refused by review
429频率、输错次数或每日额度超限,按 Retry-After(秒)等待Rate, failed-attempt or daily limit hit; wait Retry-After seconds
503服务暂时不可用Temporarily unavailable
code出现于Seen on附加字段Extra fields
guest_no_name/visitor 400, /name 403—
name_rejected/visitor, /name 422reason
name_review_unavailable/visitor, /name 503—
rename_limit/name 429renamesLeft
visitor_limit/visitor 429—
visitor_busy/visitor 429—
ip_daily_limit/message 429—
daily_limit/message 429reply

限制Limits

以下是当前的默认值,可能随时调整。超限时返回 429 和 Retry-After。

Current defaults; they may change. Exceeding one returns 429 with Retry-After.

按 IP 的限制里,IPv6 地址按 /64 网段算作同一个 IP。

For per-IP limits, IPv6 addresses in the same /64 count as one IP.

项目Limit默认值Default
每个访客发送Messages per visitor6 次/分钟,60 次/小时6 / min, 60 / hour
每个 IP 发送Messages per IP6 次/分钟,60 次/小时,40 次/天(滚动 24 小时,超出返回 ip_daily_limit)6 / min, 60 / hour, 40 / day (rolling 24 h; ip_daily_limit beyond that)
申请新身份New identities每个 IP 5 个/小时、10 个/天;全站 30 个/小时5 / hour and 10 / day per IP; 30 / hour site-wide
凭证输错Failed credentials每个码 10 次/小时,每个 IP 20 次/小时10 / hour per code, 20 / hour per IP
全站每日对话Site-wide daily turns有总量上限,用完返回 daily_limitCapped; returns daily_limit when used up
改名Renames3 次/自然月3 / calendar month
临时访客码Guest code闲置 60 分钟作废Expires after 60 min idle
消息长度 / 名字长度Text / name length2000 / 24 字符2000 / 24 characters

数据与隐私Data & privacy

  • 对话会被保存,站长可以查看。访客记录默认保存 30 天后删除。
  • 服务端只存密码的哈希值,丢失后无法找回,只能重新申请。
  • Alice 会记住与你的对话,但不会把你的内容告诉其他访客。
  • 请不要在对话中发送密码、证件号等敏感信息。
  • Chats are stored and the site owner can read them. Visitor records are deleted after 30 days by default.
  • Only a hash of the key is stored. A lost key cannot be recovered; get a new code instead.
  • Alice remembers your conversations but does not share them with other visitors.
  • Please do not send passwords, ID numbers or other sensitive data.