Commit Graph
100 Commits
Author SHA1 Message Date
RainbowBird 0e4dc69ec0 fix(analytics): normalize PostHog event semantics (#2041) 2026-07-10 13:54:23 +08:00
RainbowBird 0642ede8d1 feat(analytics): expand PostHog coverage and forward server business facts (#2038)
## What

Closes the three structural gaps in AIRI's product analytics: the signup
surface had zero instrumentation, the payment funnel had no terminator
in PostHog, and SPA route changes emitted no pageviews. Also adds
semantic events for character cards, desktop-only features, and
data-maintenance actions.

## User paths

- User signs up / logs in / verifies email / resets password / links
OAuth / deletes account → each step now emits a PostHog event from
`apps/ui-server-auth` (previously fully uninstrumented), with
`identify()` wired on session load so anonymous funnel events merge into
the user person.
- User pays via Stripe → webhook writes `product_events` as before, and
the product-events service now forwards `payment_completed` (plus signup
and subscription lifecycle facts) to PostHog via posthog-node
`captureImmediate`, keyed by the Better Auth user id → the
`checkout_started → payment_completed` funnel closes end-to-end.
Per-request LLM/TTS volume is explicitly not forwarded.
- User navigates between routes in any surface (web / desktop / pocket /
docs) → `$pageview` + `$pageleave` fire per route change via the
posthog-js `defaults: '2025-05-24'` preset in the shared
`posthog.config.ts`.
- User creates / imports / duplicates / edits a ccv3 card, switches
stage background, runs destructive data actions (export / import / clear
chats, reset providers, wipe app data), or uses desktop differentiators
(Spotlight send, widget windows, in-app updater, MCP server management,
pairing QR) → dedicated low-cardinality events.

## Notable decisions

- Server forwarding defaults on: `POSTHOG_PROJECT_KEY` defaults to the
shared browser-safe phc_* project key; set it to an empty string to
disable. Postgres `product_events` remains the source of truth.
- Cross-surface events (`oauth_callback_failed`, account lifecycle)
share one stage vocabulary exported from stage-ui so the two emitters
cannot drift silently.
- Events captured right before full-page navigation use `sendBeacon` so
they survive the redirect (checkout, OAuth consent handoff, login
redirect).
- Removed dead wrappers (`trackSignup`, `trackFirstModelSelected`,
`trackModelChanged`) that duplicated live event streams under second
names.

## How tested

- `pnpm -F @proj-airi/server exec vitest run
src/services/domain/product-events.test.ts` — 6 passed, covering the
forwarding allowlist, the `user_signed_up → signup_completed` mapping,
non-forwarded per-request actions, and a throwing sink not failing the
webhook path nor losing the DB row.
- stage-ui suites (`use-analytics`, `use-linked-accounts`, exports
contract) — 22 passed, including new account/card/data/desktop event
assertions.
- Real transport smoke: posthog-node `captureImmediate` against
`us.i.posthog.com` with the production key resolved in 1380ms (one
`server_forwarding_smoke_test` event left in the project; filter by
event name).
- Browser-tested pageviews: `VITE_ENABLE_POSTHOG=true` dev build, two
`history.pushState` route changes each produced a `$pageview` with
`$pathname`, `navigation_type: pushState`, previous-page dwell time, and
the `surface` super property; batched POST to `us.i.posthog.com/e/`
returned 200. Note: posthog-js drops events from automated browsers
(`navigator.webdriver`) by default — the verification session bypassed
the bot filter locally; production config is untouched.
- Typecheck and lint pass for server, stage-ui, stage-pages, stage-web,
stage-tamagotchi, ui-server-auth.

Full verification record:
`apps/server/docs/ai-context/verifications/posthog-forwarding-and-pageview.md`

## Follow-ups (not in this PR)

- Bot channel usage stats (Discord / Telegram) once they route through
server-runtime counters.
- Main-process desktop events (tray menu, global shortcut fire) need
renderer relay plumbing.
- Confirm `payment_completed` arrives in PostHog after the first real
Stripe payment post-deploy.

https://claude.ai/code/session_01Q1yGavkQ1P41YhTWE4XKex
2026-07-08 15:33:39 +08:00
RainbowBird 689f02ac4f refactor(posthog): unify project key for all AIRI surfaces and streamline configuration 2026-07-08 00:05:26 +08:00
RainbowBird fb9461e00c fix(turbo): add missing dependency for build task 2026-07-02 02:42:57 +08:00
RainbowBird 63532367a9 chore: remove admin dashboard 2026-07-02 02:22:16 +08:00
RainbowBird c7de38b121 refactor(server): split capability alias admin routes 2026-07-02 01:04:29 +08:00
RainbowBird 281614d396 fix(server): harden provider catalog gating 2026-07-01 23:08:14 +08:00
RainbowBird ed8c93c77b feat(server): apply official llm alias routing policy 2026-07-01 22:34:57 +08:00
RainbowBird 14fb183723 fix(server): gate streaming tts requests 2026-07-01 22:34:57 +08:00
RainbowBird 28f93c2c5d fix(server): expose official voice preview urls 2026-07-01 22:34:56 +08:00
RainbowBird b2d7442949 feat(server): enforce official asr aliases 2026-07-01 22:34:56 +08:00
RainbowBird feec4da1fa feat(server): generate official tts voice previews 2026-07-01 22:34:56 +08:00
RainbowBird 87baf622c9 feat(server): add official provider catalog management 2026-07-01 22:34:56 +08:00
RainbowBird 3edbe4eb99 feat(tests): add createMockStripeCustomer function and update tests to use it 2026-07-01 20:28:18 +08:00
RainbowBird e1a2c51573 feat(speech): integrate Voice Pack model into speech catalog and update related tests 2026-07-01 20:18:13 +08:00
RainbowBird af7de96042 feat(analytics): add p1 behavior events 2026-07-01 20:16:09 +08:00
RainbowBird 0daac71435 feat(analytics): add p0 operations events 2026-07-01 20:16:06 +08:00
RainbowBird 27255001bd refactor: streamline speech module by removing voice pack dependencies
- Removed voice pack related imports and functionality from speech.vue and Stage.vue.
- Simplified speech input handling by eliminating voice pack parameters in speech store.
- Updated tests to reflect changes in voice pack handling and speech input resolution.
- Adjusted Airi card store to remove voice pack binding logic, focusing on speech configuration updates.
- Enhanced voice pack list item structure for clarity and maintainability.
2026-07-01 19:23:33 +08:00
RainbowBird c2aee68513 feat: add upstreamVoiceId to voice packs and related services
- Introduced upstreamVoiceId field in voice pack schema and database.
- Updated voice pack service to handle upstreamVoiceId in CRUD operations.
- Modified API routes and tests to accommodate upstreamVoiceId.
- Enhanced UI components to include upstreamVoiceId in forms and displays.
- Adjusted speech processing logic to utilize upstreamVoiceId where applicable.
- Updated related tests to ensure proper functionality with new field.
2026-07-01 16:07:33 +08:00
RainbowBird 1f1ef49eed feat(speech): refactor voice pack handling and remove synthetic model references 2026-07-01 02:47:31 +08:00
RainbowBird 4f80affcd3 feat(admin-dashboard): add support for Bedrock and OpenAI-compatible LLM slices in admin router config
- Implemented `buildBedrockSlice` function to handle multi-kilobyte Bedrock bearer tokens.
- Enhanced `createAdminRouterConfigService` to classify Bedrock and OpenAI-compatible LLM upstreams by baseURL.
- Added new interfaces for `AdminRouterBedrockSlice` and `AdminRouterOpenAICompatibleSlice`.
- Updated router config form to support Bedrock and OpenAI-compatible slices.
- Created tests for Bedrock and OpenAI-compatible slice compilation and behavior.
- Modified UI components to accommodate new slice types and improve user experience.
- Ensured proper normalization of API server URLs to HTTPS when necessary.
2026-07-01 02:14:12 +08:00
RainbowBird f9f20b88ff feat(api-environment): add API environment selection component and related functionality 2026-07-01 01:08:21 +08:00
RainbowBird 71482d3859 feat(speech): implement Voice Pack model option and integrate into settings 2026-07-01 00:45:08 +08:00
RainbowBird de9a2bac7f feat(stage-tamagotchi): add assistant label to ChatHistory component 2026-06-29 03:38:45 +08:00
RainbowBird cc6fe4e6e3 feat(stage-ui): add function to find recommended voice from available voices 2026-06-28 17:25:01 +08:00
RainbowBird 8036501fb0 feat(server): add default TTS model handling and update related tests 2026-06-28 16:55:09 +08:00
RainbowBird 51a44c1f54 fix: disable SSML when not support 2026-06-14 05:03:53 +08:00
RainbowBird c3f2beba47 feat(admin-ui): enhance LLM router page to support Streaming TTS and ASR providers 2026-06-14 04:45:36 +08:00
RainbowBird 3215687e98 feat(server): add Aliyun NLS ASR gateway and official transcription provider (#1970) 2026-06-14 04:22:07 +08:00
RainbowBird 8518c65aa4 feat(auth): enhance trusted admin redirect handling and add local patterns 2026-06-11 22:26:39 +08:00
RainbowBird 2786393aa5 feat(admin-ui): enhance dark mode support across UI components
- Updated various components in the llm-router to include dark mode styles, ensuring better visibility and aesthetics in dark theme.
- Added tests for API URL helpers to validate sign-in URL construction and redirection logic.
- Improved the sign-in flow to handle trusted redirects for standalone admin origins.
- Enhanced CSS styles for dark mode, including adjustments to backgrounds, borders, and text colors for better contrast and readability.
- Refactored input components to maintain consistent styling in both light and dark modes.
2026-06-11 21:59:08 +08:00
RainbowBird 8e572ad93c feat(server): add stepfun tts provider
Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Claude (via Claude Code)
2026-06-11 00:23:04 +08:00
RainbowBird 5bf6deab89 feat(admin-ui): load and edit router config from forms
Add a form-first LLM/TTS router config editor and a redacted admin config snapshot so operators can inspect configKV state before applying changes.

Preserve existing encrypted key entries when loaded slices are submitted without new plaintext keys.

Signed-off-by: RainbowBird <git@luoling.moe>
Commit-Message-Assisted-by: Claude (via Claude Code)
2026-06-11 00:14:34 +08:00
RainbowBird 16f9aaa4e3 feat(auth): enhance OAuth linking flow with account selection and error handling
- Added `prompt: 'select_account'` for Google and GitHub providers to force account selection during OAuth linking.
- Introduced localized error messages for account linking issues in multiple languages.
- Implemented a function to resolve OAuth error codes to specific i18n keys for better user feedback.
- Updated account settings page to display relevant error messages based on OAuth linking status.
2026-06-10 16:42:13 +08:00
RainbowBird a331d3a42a feat(auth): update AUTH_UI_URL to point to accounts.airi.build 2026-06-09 18:01:34 +08:00
RainbowBird 7fa7f8a072 feat(admin-ui): update admin UI URLs to remove trailing slashes and adjust redirects 2026-06-09 17:46:58 +08:00
RainbowBird 2ac4e90d56 feat(auth): add account linking support for different email providers 2026-06-09 17:46:51 +08:00
RainbowBird 11b820a77f feat(admin-ui): implement standalone admin UI with Cloudflare Pages deployment 2026-06-09 17:09:06 +08:00
RainbowBird 57ba232432 feat(auth): migrate auth ui to unique domain 2026-06-09 00:15:34 +08:00
RainbowBird 19e5020fd3 test: resolve type 2026-06-07 20:47:40 +08:00
RainbowBird 9382a12137 chore: cleanup and lint fix 2026-06-07 20:31:22 +08:00
RainbowBird 4de2aef746 chore(server): type issues 2026-06-07 19:53:44 +08:00
RainbowBird 007fae6810 chore: update package dependencies and remove specific catalog references
- Removed specific catalog references for '@unocss/core', 'es-toolkit', 'nanoid', and 'valibot' in pnpm-workspace.yaml and various service package.json files.
- Updated versions for '@vueuse/core', 'es-toolkit', 'nanoid', 'reka-ui', and 'valibot' to their latest compatible versions.
- Cleaned up unused catalog entries in pnpm-workspace.yaml.
2026-06-07 19:45:28 +08:00
RainbowBird eae942702c feat(speech): enhance voice pack handling and add computed properties for UI logic 2026-06-07 01:56:04 +08:00
RainbowBird 87e9495667 feat(admin): refine voice pack editor
Move Voice Pack creation and editing to dedicated admin routes, add catalog-backed free-text fields, and wire test audio generation through the public speech API.

Document the mock-API browser verification workflow so future local UI checks can avoid auth and tooling dead ends.

Signed-off-by: RainbowBird <git@luoling.moe>
2026-06-06 02:22:54 +08:00
RainbowBird 30d57e7370 refactor(stage): infer conversation analytics surface
Centralize conversation event surface detection inside useAnalytics and keep chat UI call sites focused on business fields.

Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Codex
2026-06-06 01:58:13 +08:00
RainbowBird c6b5906708 chore: ignore playwright-mcp 2026-06-06 01:44:52 +08:00
RainbowBird 4708e462d2 feat(stage): track conversation product events
Add PostHog product events for manual TTS stop clicks and conversation controls across Web, mobile, and Electron chat surfaces. Document the events in metrics ownership and cover the analytics API with Vitest.

Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Codex
2026-06-06 01:37:59 +08:00
RainbowBird a3dc9c24b0 style(stage): refine mobile TTS stop button
Match the mobile manual TTS stop control to the subtler Web chat button treatment by removing the bright filled circle and using neutral icon styling with primary hover feedback.

Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Codex
2026-06-06 00:58:27 +08:00
RainbowBird bc9baa4e3e fix(stage): cover manual TTS stop across layouts
Add the stop-speaking control to the mobile shared input layout and the Electron Tamagotchi chat input so Web/shared, mobile, and Electron all expose the same manual TTS interruption path.

Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Codex
2026-06-06 00:53:34 +08:00
RainbowBird 7549d4e9fb feat(docker): add build step for deps in Dockerfile 2026-06-06 00:41:06 +08:00
RainbowBird a3e1209446 feat(stage): add manual TTS interrupt button
Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Codex
2026-06-06 00:22:11 +08:00
RainbowBird abb6d16b67 feat(server): track blocked TTS preflight 2026-06-06 00:22:11 +08:00
RainbowBird 37d12fd2dd fix(docker): add git installation to Dockerfile for build dependencies 2026-06-05 23:08:29 +08:00
RainbowBird 3a16224e72 refactor(docker): consolidate Dockerfile logic and remove unused files 2026-06-05 22:49:36 +08:00
RainbowBird e6fa39ed4b feat(server): voice pack and tts routing (#1905)
Track per-app TTS concurrency in Redis, route capped upstreams by
available pool capacity, and surface pool saturation metrics. Document
the Voice Pack plan so the remaining backend and card-binding work has
an explicit implementation map.
2026-06-05 22:39:30 +08:00
RainbowBird df141e1429 docs: update README 2026-06-05 17:02:34 +08:00
RainbowBird cf5ed0766b fix(server): make analytics best-effort
Remove server-side PostHog wiring so request handlers no longer wait on third-party analytics delivery.

Keep auth activity and product event writes best-effort so degraded analytics do not fail session creation.

Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Codex (via Codex)
2026-06-05 16:27:10 +08:00
RainbowBird f72c7b9c72 feat(server): add product analytics dashboard row 2026-06-03 23:24:45 +08:00
RainbowBird 7ac69db4ef feat(server): add product analytics events (#1941) 2026-06-03 23:03:33 +08:00
RainbowBird 41e8cd7626 chore(eventa): use upstream hono adapter
Upgrade @moeru/eventa to v1.0.0-beta.8 through the pnpm catalog and remove the local Hono adapter copy.

Keep AIRI on business-level Eventa usage while adapter contract tests live upstream in @moeru/eventa.

Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Claude (via Claude Code)
2026-06-03 17:12:13 +08:00
RainbowBird 7841df28f8 build(deps): migrate workspace dependencies to catalogs
Move direct external dependency versions into pnpm catalogs and update workspace packages to reference catalog entries.

Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Codex
2026-06-03 17:12:13 +08:00
RainbowBird e4acc78e25 refactor(server): enhance validation schemas for auth and audio processing 2026-06-03 13:48:19 +08:00
RainbowBird a795806e81 refactor(server): split auth route helpers 2026-06-03 13:48:19 +08:00
RainbowBird f5b051d969 refactor(server): split chat ws runtime 2026-06-03 13:48:19 +08:00
RainbowBird baf4f1fa51 refactor(server): split audio speech ws session 2026-06-03 13:48:18 +08:00
RainbowBird ea1c658c8d refactor(server): split stripe route operations 2026-06-03 13:48:18 +08:00
RainbowBird 03d2dc05db docs(agents): add server gateway refactor skill 2026-06-03 13:48:18 +08:00
RainbowBirdandNeko 6f6fe01b3e refactor(server): openai route gateway
Co-authored-by: Neko <neko@ayaka.moe>
2026-06-03 00:21:25 +08:00
RainbowBird ee3ae8fc31 refactor(server): tts migrate to unspeech 2026-06-02 17:34:08 +08:00
RainbowBird c64e48c233 refactor(server): nanoid 2026-06-02 15:40:05 +08:00
RainbowBird 1936a22c95 refactor(server): split openai v1 route pipeline
Separate OpenAI-compatible route wiring from chat, speech, catalog, billing, and telemetry pipeline code.

Signed-off-by: RainbowBird <git@luoling.moe>

Commit-Message-Assisted-by: Claude (via Claude Code)
2026-06-02 15:39:25 +08:00
RainbowBird 03018a6a1e refactor(server): use es-toolkit clamp for query bounds (#1923) 2026-06-02 15:00:37 +08:00
RainbowBird f728f4d639 feat(server): add testing-only bearer token bypass for authentication 2026-06-01 18:16:21 +08:00
RainbowBird f2897f7663 feat(server): total user metrics 2026-05-30 21:20:36 +08:00
RainbowBird b5b6e4fb23 chore(server): forbidden vi.mock and vi.hoist 2026-05-30 20:18:50 +08:00
RainbowBird 42436bca53 refactor(server): langfuse upstream model 2026-05-30 20:13:53 +08:00
RainbowBird 6e871253e7 feat(server): langfuse 2026-05-30 19:35:37 +08:00
RainbowBird dc1037f349 feat(server): user metrics 2026-05-30 00:37:51 +08:00
RainbowBird 95915923e1 feat(server): enhance TTS configuration with support for default voices 2026-05-28 16:59:08 +08:00
RainbowBird 6abd813cfd chore(server): tracing undici 2026-05-27 18:00:39 +08:00
RainbowBird c4824f7bd1 fix(server): span tracing 2026-05-27 16:43:33 +08:00
RainbowBird 0ac9b709a2 refactor(server): integrate better-auth admin plugin (#1873) 2026-05-26 02:04:25 +08:00
RainbowBird 7e96386ec0 fix(server): unblock FLUX checkout from Electron desktop renderer
The packaged Electron renderer loads from file://, so its Stripe checkout
and portal requests carry no Referer and an opaque/absent Origin.
resolveTrustedRequestOrigin() returned undefined and the routes threw
INVALID_ORIGIN ("Missing trusted request origin"), blocking FLUX purchases
on desktop. Web and mobile were unaffected because they send a trusted web
origin. CORS was not the blocker: file:// requests already reach the API
(balance, providers, characters all work), so only the checkout-specific
redirect-base dependency failed.

Stripe success_url/cancel_url/return_url must be http(s) URLs, so file://
can never be a valid redirect base. Replace the trusted-origin requirement
with resolveCheckoutRedirectBase(), which prefers the request's trusted
origin (web/mobile return to where they started) and falls back to a
configured canonical web app URL (WEB_APP_URL, default https://airi.moeru.ai)
for origin-less clients like the desktop app.

On the client, the desktop app opens checkout in the system browser via
window.open (routed through setWindowOpenHandler -> shell.openExternal)
instead of navigating the file:// window to Stripe, and refreshes the FLUX
balance on window focus since it never receives the success_url redirect.
2026-05-26 01:56:47 +08:00
RainbowBird 6f63ce96e9 feat(server): server-controlled default streaming TTS model
The streaming speech provider settings page was seeding its model
picker with a hardcoded `volcengine/seed-tts-2.0`, which contradicts
the just-landed "streaming model catalog comes from the server"
contract. Now the default also comes from configKV.

- `UNSPEECH_UPSTREAM.streaming` gains `defaultModel?: string`.
- `GET /api/v1/audio/models/streaming` response gains `default: string
  | null` reading that field.
- admin slice apply: `streaming.defaultModel` survives key/URL
  rotation alongside `streaming.models`.
- `providerOfficialSpeechStreaming.listModels` stashes the value in a
  module-scope ref, exposed via `getDefaultStreamingModel()`.
- Streaming speech settings page reads the helper instead of the
  hardcoded id; falls back to the first server-returned model if the
  operator hasn't curated a default. `handleGenerateSpeech` no longer
  has a `seed-tts-2.0` backstop — a missing backend prefix in the
  selected model id now throws instead of silently defaulting.
2026-05-24 21:13:52 +08:00
RainbowBird be2c355b0e refactor(server): merge STREAMING_TTS_UPSTREAM + UNSPEECH_REST_BASE_URL into UNSPEECH_UPSTREAM
One unspeech deployment = one configKV entry. The split into two keys
modelled a hypothetical split deployment (operators pointing REST and
WS at different unspeech instances) that nobody actually runs, and the
explanatory comments justifying the split were exactly the migration-
narrative anti-pattern we just banned from source.

Schema:

  UNSPEECH_UPSTREAM = {
    restBaseURL: string,                  // required, http(s)://host:port
    streaming?: {                         // optional, only when ws TTS is on
      baseURL: string,                    // ws(s)://host:port/...
      keys: [{ id, ciphertext }],
      adapterParams: {},
      models?: [{ id, name?, description? }],
    },
  }

Admin slice surface flattens to one `kind: 'unspeech'`:

  { kind: 'unspeech', restBaseURL,
    streaming?: { upstreamURL, plaintextKey, keyEntryId? } }

Read-site changes:
- routeTts + listTtsVoices read UNSPEECH_UPSTREAM.restBaseURL via
  getOrThrow; absent entry → 503 CONFIG_NOT_SET.
- audio-speech-ws dials UNSPEECH_UPSTREAM.streaming and 1008-closes
  with streaming_tts_not_configured when the subtree is absent.
- handleListStreamingVoices reads .restBaseURL directly (no more
  ws→http scheme swap) and 503s on missing streaming subtree.
- handleListStreamingTTSModels reads .streaming.models.
- config-sync-subscriber listens for UNSPEECH_UPSTREAM invalidations
  instead of UNSPEECH_REST_BASE_URL.

Drive-by: tighten ConfigKVService.getOrThrow return to
`Exclude<ConfigDefinitions[K], undefined>` so call sites stop needing
non-null assertions on optional schema entries — the runtime already
throws, the type now reflects it.

Migration: operators repost a single admin slice to rewrite the merged
entry. Old STREAMING_TTS_UPSTREAM and UNSPEECH_REST_BASE_URL rows
become dead data and can be deleted from configKV; nothing reads them.
2026-05-19 23:28:42 +08:00
RainbowBird 615e0441e8 refactor(server): proxy TTS through unspeech, drop implicit fallbacks
Frontend voice picker silently lost Azure voices because /audio/voices
was called without ?model=, so the server fell back to DEFAULT_TTS_MODEL
and only ever returned one model's catalog. /audio/models also hardcoded
a single `auto` alias, so the client could never request a specific
model id even if it wanted to. On top of that, the Azure adapter shipped
only 2 hand-typed voices in voices/azure.json — the rest of Microsoft's
catalog was unreachable.

Fixed in one pass:

- Drop voices/*.json. Every getVoiceCatalog now calls unspeech REST
  (backend=microsoft|alibaba|volcengine). Azure proxies Microsoft's live
  voices/list; cosyvoice + volcengine use unspeech's embedded catalogs.
- Drop direct upstream provider calls from send(). All three adapters
  POST to <UNSPEECH_REST_BASE_URL>/v1/audio/speech with the openai-shaped
  body unspeech expects (model: `<backend>/<id>`). Azure SSML still built
  on our side so <prosody rate> speed survives — unspeech accepts
  pre-built <speak> documents.
- Fail-fast on every voices endpoint: empty ?model= → 400 MISSING_MODEL
  (no implicit DEFAULT_TTS_MODEL fallback), only `auto` resolves;
  streaming upstream missing → 503; unspeech non-2xx / network err → 502;
  malformed body → 502 with grepable message. No silent `{voices: []}`.
- /audio/models lists every LLM_ROUTER_CONFIG.tts.models key + `auto`.
- Frontend providerOfficialSpeech.listVoices passes ?model= and throws
  on upstream failure instead of returning [].
- New UNSPEECH_REST_BASE_URL configKV entry (no default, missing → 503).
  config-sync subscriber invalidates the voice cache when this key OR
  LLM_ROUTER_CONFIG changes.
- Router voice catalog Redis cache covers all providers with per-provider
  TTL (azure 6h live, alibaba/volcengine 24h since unspeech embeds them
  at build time).

Pending billing-SKU verification: DashScope cosyvoice now goes through
unspeech's WS-internal alibaba backend (was REST `/SpeechSynthesizer`).
Functional output equivalent — confirm bill after deploy, revert via
git history if WS pricing is materially higher.
2026-05-19 23:14:16 +08:00
RainbowBird 6b0788dd60 feat(server): expose streaming TTS model catalog from config
Streaming TTS model list was hardcoded in the official provider with two
Volcengine ids. This moves it server-side so operators control the
catalog without a client release.

Catalog lives on `STREAMING_TTS_UPSTREAM.models` (same configKV entry as
the ws upstream + key envelope) rather than a separate kv key — connection
target, keys, and surfaced ids are one deployment decision, splitting
them risks drift on rotation. `LLM_ROUTER_CONFIG.tts.models` is the
router/fallback domain for HTTP TTS; streaming is single-ws and has no
router semantics, so it gets its own `streamingTtsUpstreamSchema`
instead of reusing `ttsUpstreamSchema`.

New `GET /api/v1/audio/models/streaming` returns the configured list;
empty when upstream or models is unset (UI renders "no models" instead
of 5xx). Admin slice apply preserves existing `models` across key/connection
rotation so admin POSTs that only carry upstream+keys do not wipe the
catalog. Frontend `providerOfficialSpeechStreaming.listModels` fetches
the endpoint and throws on upstream errors (no silent empty array).
2026-05-19 23:14:16 +08:00
RainbowBird 201beae5ac fix(server): import 2026-05-18 23:48:11 +08:00
RainbowBird 4ed7315860 chore(server): finish test layout cleanup and tuck oidc under auth
- services/domain/llm-router and services/domain/user-deletion still had
  colocated *.test.ts; moved them into the local tests/ subdir so every
  service module follows the same layout as billing/, admin/*/, etc.
- routes/oidc/ wasn't an independent route group — it's wholly consumed
  by routes/auth/index.ts (electron-callback relay + OIDC bearer token
  helper). Moved to routes/auth/oidc/ to reflect that ownership.
- Fixed the relative imports in src/libs/tests/{auth,env,request-auth,
  ws-auth}.test.ts. They were moved to libs/tests/ in the previous
  commit without updating the './<source>' specifiers; typecheck only
  caught it once the rewriter ran across the tree.

pnpm typecheck passes; eslint matches the main baseline.
2026-05-18 23:39:14 +08:00
RainbowBird 812b2db4ab chore(server): drop flux_grant_batch schema and tables
Why
- Code, routes, service, worker, tests, and ai-context references for the
  legacy flux_grant_batch flow were removed in the previous commit. The
  schema file and the corresponding production tables were intentionally
  left for a separate DDL-only PR (this one) so the destructive change is
  easy to time and roll back.

What
- Delete src/schemas/flux-grant-batch.ts.
- Drop the re-export line in src/schemas/index.ts.
- drizzle-kit generate produced drizzle/0011_open_unus.sql:
    DROP TABLE flux_grant_batch CASCADE;
    DROP TABLE flux_grant_batch_recipient CASCADE;
  CASCADE removes the 6 associated indexes in one shot.
- docs/ai-context/architecture-overview.md updated: the dead-code
  reminder now points at the migration and explains the rollback story.

Also rolls in a pre-existing local move that was sitting uncommitted:
src/libs/{auth,env,request-auth,ws-auth}.test.ts → src/libs/tests/...
(aligning with the libs/tests/eventa-hono-adapter.test.ts placement that
was already on HEAD).

Deployment
- pnpm typecheck: passes.
- DO NOT run pnpm db:push on prod from this branch automatically. The
  drop is intentionally a separate operator action that requires picking
  a deploy window where no instance is still on an older image that
  could try to read flux_grant_batch. Until 0011 is applied to prod the
  table sits as an orphaned shell — safe to leave indefinitely.
2026-05-18 23:39:14 +08:00
RainbowBird c627bce9c9 refactor(server): split services into domain/adapter layers, drop dead code
Why
- src/services/ was an unordered mix of single-file services and module
  directories with no shared classification axis, plus several long-dead
  admin batch helpers that survived the move to the simpler synchronous
  admin-flux-grants flow.

What
- services/ now has two top-level layers:
    domain/   — DB state + business rules (billing, characters, chats,
                flux, flux-transaction, llm-router, providers, request-log,
                stripe, user-deletion, admin/{flux-grants,router-config})
    adapters/ — thin wrappers over external SDKs / infra (config-kv, email,
                posthog, tts/)
- admin/* moved under domain/admin/ with consistent plural names
  (flux-grants, router-config).
- tts-adapters/ collapsed to adapters/tts/ (no redundant -adapters suffix
  once nested under adapters/).
- 63 src files + scripts/e2e-llm-router.ts + tests/verifications/_harness.ts
  had relative imports rewritten; git mv preserves blame.
- apps/server/CLAUDE.md and docs/ai-context/*.md updated to match new paths.

Dead code removed
- services/admin-flux-grant-batches/ (service + worker + tests, 1090 LOC) —
  superseded by admin-flux-grants and never wired into app.ts.
- routes/admin/flux-grant-batches/ — same.
- utils/redis-compressed.ts + test — zero production call sites.
- llm-router/index.ts re-exports trimmed from 26 to 6; only symbols with
  external consumers are kept.

Intentionally kept
- schemas/flux-grant-batch.ts and its schemas/index.ts export remain so the
  drizzle-kit generate diff stays empty. Removing them is a separate PR
  that owns the drop-table migration for flux_grant_batch /
  flux_grant_batch_recipient.

Verification
- pnpm -F @proj-airi/server typecheck: passes.
- pnpm exec eslint apps/server: 49 errors, identical to main baseline
  (all are pre-existing node/prefer-global/buffer in envelope-crypto and
  scripts/e2e-llm-router; untouched by this change).
- Vitest passes per-file; the 6 mockDB hook timeouts under full-parallel
  run are the known pushSchema-per-worker infra cost, not a regression.
2026-05-18 23:36:45 +08:00
RainbowBird 45fb765df7 fix(server): ws conn 401 2026-05-18 23:36:08 +08:00
RainbowBird 8ef5844928 refactor(server): drop seed-router-config / seed-streaming-tts scripts
The two seed scripts are fully superseded by the new admin endpoint
`POST /api/admin/config/router` — same encryption, same configKV
writes, same `configkv:invalidate` publish, plus auth + audit + body
limits. Keeping both code paths created a drift risk on the AAD label
and the merge semantics.

Doc + test fallout:
- `e2e-llm-router.ts` now points readers to the admin endpoint for
  the prerequisite seed step.
- `docs/ai-context/verifications/llm-router.md` and
  `streaming-tts.md` get curl-based seed instructions; the 2026-05-15
  llm-router evidence stays intact with a note that the script it
  used has since been removed.
- The U9 follow-up entry in `llm-router.md` flips from "not shipped"
  to "partially shipped" — ETag + HMAC publish are still deferred,
  so the `config_write` / `config_invalid_hmac` Grafana panels stay
  parked.
- Self-edit on the admin route + `app.ts` docstrings to drop the
  earlier "scripts stay as break-glass" wording.
2026-05-18 23:36:07 +08:00
RainbowBird c241677cc0 fix(server): update DEFAULT_TTS_VOICES structure for model-specific recommendations 2026-05-18 23:36:07 +08:00
RainbowBird c1b88701db feat(server): admin endpoint for seeding/patching LLM router config
Replaces routine use of `scripts/seed-router-config.ts` and
`scripts/seed-streaming-tts.ts` with `POST /api/admin/config/router`.
Operators can now patch one provider at a time without shelling into
the Railway runner; the seed scripts stay as break-glass tools for
cold-boot and disaster recovery.

The endpoint accepts a discriminated-union slice list (openrouter /
azure / dashscope-cosyvoice / streaming-tts), envelope-encrypts
plaintext keys in-process (never echoed back), and supports
merge/reset modes plus dryRun. Writes go through the existing
configKV + Redis `configkv:invalidate` channel so multi-instance
deployments pick up changes within the pub/sub propagation window.

Guarded by the existing `authGuard + adminGuard` pair
(`ADMIN_EMAILS` allowlist + verified email).
2026-05-18 23:36:07 +08:00
RainbowBird efe0f8ac19 refactor(server): streaming tts support model switch 2026-05-18 23:35:38 +08:00
RainbowBird 2854e0accc fix(server): tts streaming issue 2026-05-18 23:35:37 +08:00
RainbowBird ba9247fb47 feat(server,stage-ui): bidirectional streaming TTS + audio path refactor
Why:
- Add a real bidirectional streaming TTS path: raw LLM tokens are
  forwarded to the upstream model (Volcengine v3 via the unspeech ws
  bridge) without client-side segmentation, so the model owns sentence
  splitting and audio chunks play as they arrive.
- Move audio endpoints out of /api/v1/openai/. `/audio/voices`,
  `/audio/models`, `/audio/voices/streaming` are not real OpenAI public
  APIs, and the streaming TTS surface has nothing to do with OpenAI —
  keeping them under /openai/ mislabelled the contract.
- Introduce `capabilities.speech.transport` on ProviderDefinition so
  future streaming providers (ElevenLabs / Cartesia / OpenAI Realtime)
  opt in without touching Stage.vue or the session factory.
- Unify Stage.vue's TTS path through a single StageTtsSession so the
  chat-orchestrator hooks no longer branch on provider id.

What:
- apps/server: new ws proxy /api/v1/audio/speech/ws bridges client ↔
  unspeech with auth, pre-flight flux check, billing from upstream
  session.finished.usage, OTel spans.
- apps/server: audio routes moved from /api/v1/openai/audio/* to
  /api/v1/audio/* (hard cutover; 404 sentinel tests added).
- apps/server: new /api/v1/audio/voices/streaming proxy reads voices
  from unspeech /api/voices?provider=volcengine.
- apps/server: new STREAMING_TTS_UPSTREAM configKV entry +
  scripts/seed-streaming-tts.ts.
- stage-ui: new libs/speech/streaming-pipeline.ts opens one ws per LLM
  intent (appendText / finish / cancel + onSentence / onError / onDone).
- stage-ui: new libs/speech/tts-session.ts — StageTtsSession interface
  with segmenter and streaming adapters; factory dispatches by
  capabilities.speech.transport instead of hard-coded provider id.
- stage-ui: providerOfficialSpeechStreaming with capabilities.speech =
  { transport: 'bidirectional-ws' }; settings page with model/voice
  picker + ws-based preview.
- stage-ui: Stage.vue chat hooks collapsed to a single currentSession;
  hot-swap watcher cancels mid-session on provider/voice/model change;
  unmount cancels and drains playback.

Tests:
- 9 streaming-pipeline tests (happy path / buffered / error / cancel /
  truncation)
- 11 tts-session tests (factory branch coverage + adapter contracts)
- 4 audio-speech-ws route tests (forwarding / billing / pre-flight /
  config-missing)
- 3 legacy-path 404 sentinels in v1 route tests
- Verification doc updated to reflect automated coverage.
2026-05-18 23:34:35 +08:00
RainbowBird a78eaaa4a9 test(server): integration test 2026-05-18 23:33:25 +08:00