mirror of
https://github.com/moeru-ai/airi.git
synced 2026-11-29 10:56:17 +00:00
## Summary
Add an iMessage channel integration powered by the Photon API
(`integrations/photon-bot`) and a matching **iMessage** module settings
page in Stage UI.
- `integrations/photon-bot` runs as a standalone process, listens to the
Photon Socket.IO event stream, and forwards each inbound iMessage text
to AIRI as `input:text` bound to `photon:<spaceId>:<agentUserId>`.
- The bot reads the character reply from `output:gen-ai:chat:message`,
strips stage control tokens (`<|ACT …|>`, `<|DELAY …|>`, and malformed
`<ACT …|>`), and sends the plain text back through the Photon REST API
with an idempotency key.
- Stage UI adds an **iMessage** card and settings page (`Settings →
Modules → iMessage`) that sends `enabled`, `projectId`, and `apiKey` to
the `photon` module over `module:configure`.
## Context
- Request: Add a Photon-backed iMessage bot on the same integration path
as `discord-bot`, with a Stage UI settings page so users can configure
and toggle it without restarting the bot.
- Scenario: A user connects a Photon project with a shared iMessage line
to a local AIRI instance and exchanges texts with the character over
iMessage.
- Scope: Turn-based plain text only. Attachments, stickers, voice
messages, and `group` payloads are ignored. Inline reply quoting and
tapback sentences are implemented in the bot, while Photon Beta does not
yet deliver `parentMessageId` on shared-line replies or iMessage tapback
events. Settings backup schema changes are kept out of scope.
## Change map
| Module | Before | After | Description |
| --- | --- | --- | --- |
| `integrations/photon-bot` | Absent | New standalone module
(`PhotonStream`, `PhotonAdapter`, text formatters, Valibot config,
README, unit tests) | Holds the Photon stream, persists the stream
cursor per project, sends read receipts and typing indicators, backs off
on 401/403, and applies live `module:configure` updates. |
| `packages/stage-ui` & `packages/stage-pages` | No iMessage / Photon
module | `usePhotonStore`, `MessagingPhoton` component,
`/settings/modules/messaging-photon` route, module list entry, and
data-maintenance reset hook | Lets users toggle iMessage integration
immediately, edit Photon project ID and API key, save live changes, and
reset credentials. |
| `packages/i18n` | No `messaging-photon` strings | English
`settings.pages.modules.messaging-photon.*` keys in `en/settings.yaml` |
Provides English UI copy only. Crowdin manages other locales. |
| `README.md`, `docs/README.zh-CN.md` | The feature list names Telegram
and Discord chat | The list also names iMessage chat | Tells readers
about the new channel. Other locales keep their own text. |
## Architecture and behavior
```mermaid
sequenceDiagram
participant P as Photon API
participant S as PhotonStream
participant A as PhotonAdapter
participant C as AIRI Server Channel
participant U as Stage UI (iMessage page)
U->>C: ui:configure (enabled, projectId, apiKey)
C->>A: module:configure
A->>S: reconnect with new credentials (or close when disabled)
P->>S: delivery (message.received, seq)
S->>A: onDelivery(event, seq)
A->>P: markRead + setTyping(true)
A->>C: input:text (required delivery, binding photon:spaceId:agentUserId)
alt no consumer registered
C-->>A: error (no consumer registered)
A->>C: retry after 3s
else reply received
C-->>A: output:gen-ai:chat:message
A->>P: setTyping(false)
A->>P: sendText (idempotency-key: airi-reply:<messageId>)
A->>A: save { cursor: seq, projectId } to state file
end
```
## Boundaries and risks
| Invariant or boundary | Failure mode | Protection | Evidence or gap |
| --- | --- | --- | --- |
| Session isolation | Inbound iMessage joins the owner's private AIRI
chat session | Bind every `input:text` to
`photon:<spaceId>:<agentUserId>` and mask handles to last four digits |
Covered in `airi-adapter.test.ts` and `reply-text.test.ts` |
| Duplicate delivery on reconnect | Replay sends the same iMessage reply
twice | Persist cursor after `sendText` and pass `idempotency-key:
airi-reply:<messageId>` | Covered in `airi-adapter.test.ts` |
| Rejected API key (401 / 403) | Fast Socket.IO reconnect loop floods
logs and asks AIRI for unsent replies | Disconnect on 401/403 handshake
or HTTP refusal, log once, back off exponentially (30s to 10m), and
retry immediately on `module:configure` | Covered in
`airi-adapter.test.ts` |
| Project switch | Old project cursor skips events on a new project |
Store `projectId` beside `cursor` and reset cursor to `0` when
`projectId` changes | Covered in `airi-adapter.test.ts` |
| Inline replies and tapbacks | Photon Beta omits `parentMessageId` and
tapback events on shared lines | Documented in
`integrations/photon-bot/README.md`; quote lookup falls back to `"a
message"` | Unit-tested against documented OpenAPI shapes; live Photon
Beta delivery not yet available |
| UI settings state | Enabling without credentials or leaving stale
credentials after reset | Switch requires non-empty trimmed credentials,
applies immediately, and `resetState()` clears values and notifies
`photon` | Covered in
`packages/stage-ui/src/stores/modules/photon.test.ts` |
## Verification
- Manual happy-path check: Tested end-to-end on a shared Photon iMessage
line (inbound text, typing indicator, AIRI reply, outbound delivery, and
live settings toggle).
- Automated checks on `7f42b385a` (macOS arm64, Node 26.13.0, pnpm
11.24.0). The later commits change only `README.md`,
`docs/README.zh-CN.md`, and one lockfile metadata line.
- `pnpm -F @proj-airi/photon-bot typecheck`: pass.
- `pnpm -F @proj-airi/stage-ui typecheck`: pass.
- `pnpm -F @proj-airi/stage-pages typecheck`: pass.
- `pnpm typecheck`: pass.
- `pnpm lint`: pass.
- `NODE_OPTIONS=--no-experimental-webstorage TURBO_SCM_BASE=32ffe96dc
pnpm exec turbo run test:run --affected`: `photon-bot` 33/33,
`stage-pages` 95/95, `stage-ui` 2245/2246. CI uses the same
`NODE_OPTIONS`.
- The one `stage-ui` failure is `keyframe-editor.browser.test.ts > drags
a keyframe and commits the drag once`. This PR does not change that
file. It passes 3 of 3 runs alone on this head and on the merge base.
- CI on `e7177d8bf`: all checks pass.
- Visual captures: Before is the merge base `
|
||
|---|---|---|
| .. | ||
| discord-bot | ||
| minecraft | ||
| photon-bot | ||
| satori-bot | ||
| telegram-bot | ||
| twitter-services | ||
| vscode | ||