Compare commits

...
Author SHA1 Message Date
Bailey Dixon cec05ceec2 Merge pull request #322 from Codename-11/dev
release(android): android-v1.8.1
2026-08-09 23:46:51 -04:00
Bailey Dixon 0e30699fff release(android): android-v1.8.1 2026-08-09 23:20:15 -04:00
Bailey Dixon d4041e4528 chore: merge main back after android-v1.8.0 2026-08-09 23:15:39 -04:00
Bailey Dixon c5ae402cd7 fix(android): align gateway and transcript contracts 2026-08-09 23:08:26 -04:00
Bailey Dixon 56eb213bbf Merge pull request #321 from Codename-11/dev
release(android): android-v1.8.0
2026-08-09 22:53:38 -04:00
Bailey Dixon 3165ebb8ce release(android): android-v1.8.0 2026-08-09 22:03:44 -04:00
Bailey Dixon a2457a39d4 Revert "Merge branch 'feature/hermes-management-profile-detail' into dev"
This reverts commit b481f1f337, reversing
changes made to 7b61ed4758.
2026-08-09 21:35:33 -04:00
Bailey Dixon c2799082b0 Revert "Merge branch 'feature/hermes-management-detail-surfaces' into dev"
This reverts commit 2a467bc03f, reversing
changes made to 44293030c9.
2026-08-09 21:35:33 -04:00
Bailey Dixon 2a467bc03f Merge branch 'feature/hermes-management-detail-surfaces' into dev 2026-08-09 21:13:35 -04:00
Bailey Dixon a30d3dc6ed feat(android): refine Hermes management surfaces 2026-08-09 21:13:29 -04:00
Bailey Dixon 44293030c9 fix(android): contain appearance header in viewport 2026-08-09 20:50:53 -04:00
Bailey Dixon 42ac8c5589 feat(marketing): refresh product screenshots 2026-08-09 20:08:42 -04:00
Bailey Dixon b481f1f337 Merge branch 'feature/hermes-management-profile-detail' into dev 2026-08-09 19:59:56 -04:00
Bailey Dixon 62c4017eeb feat(android): refine Hermes profile management detail 2026-08-09 19:59:40 -04:00
Bailey Dixon 7b61ed4758 Merge branch 'fix/hermes-management-detail-ui' into dev 2026-08-09 19:22:30 -04:00
Bailey Dixon 21259230b2 fix(android): restore Hermes management hub 2026-08-09 19:22:20 -04:00
Bailey Dixon 7ef1e93544 fix(android): reconcile profile shelf chat status 2026-08-09 19:13:34 -04:00
Bailey Dixon 61a50dc968 Merge branch 'feature/hermes-management-ui' into dev 2026-08-09 19:13:16 -04:00
Bailey Dixon 18b9ed005b chore(marketing): refresh production UI screenshots 2026-08-09 19:13:04 -04:00
Bailey Dixon c597187fa3 Merge branch 'feature/android-profile-shelf' into dev
# Conflicts:
#	CHANGELOG.md
#	DEVLOG.md
#	app/src/main/kotlin/com/hermesandroid/relay/ui/components/ConnectionInfoSheet.kt
#	app/src/main/kotlin/com/hermesandroid/relay/ui/components/MessageBubble.kt
#	app/src/main/kotlin/com/hermesandroid/relay/ui/screens/ChatScreen.kt
#	app/src/main/res/values-b+pt+BR/strings.xml
#	app/src/main/res/values-b+zh+Hans/strings.xml
#	app/src/main/res/values-de/strings.xml
#	app/src/main/res/values-es/strings.xml
#	app/src/main/res/values-ja/strings.xml
#	app/src/main/res/values-ru/strings.xml
2026-08-09 19:11:50 -04:00
Bailey Dixon c97f3a3359 feat(android): streamline Hermes management UI 2026-08-09 19:10:45 -04:00
Bailey Dixon 2d9ccd4712 Revert "Merge branch 'feature/agent-profile-defaults' into dev"
This reverts commit 93643be844, reversing
changes made to 1132fdf576.
2026-08-09 18:37:16 -04:00
Bailey Dixon 93643be844 Merge branch 'feature/agent-profile-defaults' into dev 2026-08-09 18:18:00 -04:00
Bailey Dixon b82ee95b8f feat(android): add profile defaults editor 2026-08-09 18:17:53 -04:00
Bailey Dixon 1132fdf576 fix(android): stabilize passport scrolling and model layout 2026-08-09 18:17:31 -04:00
Bailey Dixon 04c69cd15f fix(android): preserve gateway model during api catalog loads 2026-08-09 18:05:16 -04:00
Bailey Dixon b4edcad1e7 fix(android): keep passport catalog refresh read only 2026-08-09 17:54:08 -04:00
Bailey Dixon bef9579917 fix(android): keep passport controls session scoped 2026-08-09 17:11:24 -04:00
Bailey Dixon c84e037b77 fix(android): stop message narration safely 2026-08-09 16:38:27 -04:00
Bailey Dixon 3ad33691cb fix(android): recover persisted tool activity and chat speech 2026-08-09 16:21:40 -04:00
Bailey Dixon bb0a810798 fix(android): protect chat identity from pets 2026-08-09 16:09:09 -04:00
Bailey Dixon b675c1498c Merge branch 'feature/android-clean-activity' into dev 2026-08-09 15:46:02 -04:00
Bailey Dixon 6da1ad1a99 Merge branch 'fix/android-avatar-pet-regressions' into dev 2026-08-09 15:46:01 -04:00
Bailey Dixon 380b9e918d feat(android): clean up transcript activity 2026-08-09 15:44:05 -04:00
Bailey Dixon d531b4d377 fix(android): restore chat and pet interactions 2026-08-09 15:36:57 -04:00
Bailey Dixon 8cba61d2d8 Merge branch 'fix/android-chat-input-polish' into dev 2026-08-09 13:30:59 -04:00
Bailey Dixon 4106fd4b78 fix(android): polish chat message presentation 2026-08-09 13:30:53 -04:00
Bailey Dixon 47426db290 Merge branch 'feature/appearance-customization' into dev 2026-08-09 13:21:01 -04:00
Bailey Dixon b7945b072f feat(android): enhance appearance and visual assets 2026-08-09 13:20:53 -04:00
Bailey Dixon 3aead772a0 Merge branch 'fix/android-chat-input-polish' into dev 2026-08-09 12:41:22 -04:00
Bailey Dixon ccf94e1d9d feat(android): refine chat experience 2026-08-09 12:41:17 -04:00
Bailey Dixon 6b6edc1322 Merge branch 'fix/android-pet-ui-awareness' into dev 2026-08-09 12:31:28 -04:00
Bailey Dixon 2644b94fa9 fix(android): keep floating pets clear of UI 2026-08-09 12:31:14 -04:00
Bailey Dixon 440e3d5bb9 fix(android): unbox thinking status 2026-08-08 22:40:07 -04:00
Bailey Dixon 97158bc861 fix(android): stabilize server-default shelf avatar 2026-08-08 22:35:32 -04:00
Bailey Dixon d5a313ab5d feat(android): refine profile and chat handoffs 2026-08-08 20:27:01 -04:00
Bailey Dixon 3033e331b0 chore: backmerge Android 1.7.1 and Server 1.6.1 releases 2026-08-08 20:06:51 -04:00
Bailey Dixon ca98f2ae97 Merge pull request #317 from Codename-11/dev
Release Android 1.7.1 and Server 1.6.1
2026-08-08 19:44:56 -04:00
Bailey Dixon 07bbb16671 feat(android): add profile shelf 2026-08-08 19:25:20 -04:00
Bailey Dixon ee08187c67 release(server): server-v1.6.1 2026-08-08 19:15:32 -04:00
Bailey Dixon 7ccde11a2d release(android): android-v1.7.1 2026-08-08 19:15:31 -04:00
Bailey Dixon 2378e6ab9f Merge branch 'fix/android-stream-tail-follow' into dev 2026-08-08 18:37:07 -04:00
Bailey Dixon 9045550317 fix(android): follow growing streamed replies 2026-08-08 18:37:01 -04:00
Bailey Dixon 7074920625 test(android): reconcile post-release issue coverage 2026-08-08 18:01:50 -04:00
Bailey Dixon cac8a517b4 Merge branch 'fix/android-nous-hosted-onboarding' into dev 2026-08-08 16:26:38 -04:00
Bailey Dixon c27019cdbb fix(connections): stabilize hosted Hermes onboarding 2026-08-08 16:26:33 -04:00
Bailey Dixon d0b060fe12 Merge branch 'fix/android-session-owned-queue' into dev
# Conflicts:
#	CHANGELOG.md
#	DEVLOG.md
#	app/src/main/kotlin/com/hermesandroid/relay/viewmodel/ChatViewModel.kt
2026-08-08 13:48:09 -04:00
Bailey Dixon 098cc82711 Merge branch 'fix/android-agent-passport-safety-sheet' into dev
# Conflicts:
#	CHANGELOG.md
#	DEVLOG.md
2026-08-08 13:30:55 -04:00
Bailey Dixon 98a311afb5 fix(android): keep queued messages with origin session 2026-08-08 13:29:21 -04:00
Bailey Dixon cb1560fff3 fix(android): improve agent passport safety controls 2026-08-08 13:27:32 -04:00
Bailey Dixon 76bb9f8dcb Merge branch 'fix/issue-316-markdown-completion' into dev
# Conflicts:
#	CHANGELOG.md
#	DEVLOG.md
2026-08-08 12:39:54 -04:00
Bailey Dixon 1eb0393dd9 fix(android): render markdown when replies complete 2026-08-08 12:38:13 -04:00
Bailey Dixon 96e6d29c9e Merge branch 'fix/android-approval-explicit-consent' into dev 2026-08-08 12:34:55 -04:00
Bailey Dixon f9bf4e384f fix(android): require explicit approval decisions 2026-08-08 12:34:46 -04:00
Bailey Dixon 615119b100 Merge branch 'fix/android-session-pin-archive' into dev
# Conflicts:
#	CHANGELOG.md
#	docs/decisions.md
2026-08-08 09:24:40 -04:00
Bailey Dixon cfe86b6cc3 fix(android): persist session pin and archive state 2026-08-08 08:56:55 -04:00
Bailey Dixon 677914a51d Merge branch 'fix/android-active-tool-card-expansion' into dev 2026-08-08 08:47:13 -04:00
Bailey Dixon 8b3adf10c6 fix(android): preserve live tool card expansion 2026-08-08 08:47:07 -04:00
Bailey Dixon 5cc8e7187b Merge branch 'fix/android-model-inventory-identity' into dev 2026-08-08 08:42:55 -04:00
Bailey Dixon ece46cae24 fix(android): normalize model inventory identities 2026-08-08 08:42:47 -04:00
Bailey Dixon c4d3934cfe chore: backmerge Android 1.7.0 and Server 1.6.0 releases 2026-08-06 21:59:49 -04:00
Bailey Dixon 545d238fd2 Merge pull request #311 from Codename-11/dev
release: Android 1.7.0 and Server 1.6.0
2026-08-06 21:38:49 -04:00
Bailey Dixon 9b36a34e2a release(android): android-v1.7.0 2026-08-06 21:01:01 -04:00
Bailey Dixon 7c35a51aed release(server): server-v1.6.0 2026-08-06 21:00:52 -04:00
Bailey Dixon 474147cb62 test(server): align session TTL route expectations 2026-08-06 20:59:59 -04:00
Bailey Dixon 0bd246586a chore: backmerge released main into dev 2026-08-06 20:16:54 -04:00
Bailey Dixon fc6aaffc11 fix(android): keep restored chats bottom-pinned 2026-08-05 22:08:55 -04:00
Bailey Dixon 197b23f344 feat: resolve Codex effort levels dynamically 2026-08-05 21:28:43 -04:00
Bailey Dixon 0862b4c346 Merge branch 'fix/android-provider-effort-levels' into dev 2026-08-05 21:09:51 -04:00
Bailey Dixon 9c7bc46c7c feat(android): explain reasoning effort compatibility 2026-08-05 21:09:37 -04:00
Bailey Dixon 9554eab757 Merge fix/android-provider-effort-levels into dev 2026-08-05 20:38:32 -04:00
Bailey Dixon 3cacc7d6d6 Merge dev into fix/android-provider-effort-levels
# Conflicts:
#	DEVLOG.md
#	app/src/main/kotlin/com/hermesandroid/relay/viewmodel/ChatViewModel.kt
2026-08-05 20:37:14 -04:00
Bailey Dixon 311888a9fb fix(android): settle chat viewport and pet grounding 2026-08-05 20:35:22 -04:00
Bailey Dixon 96dfe472ef feat: resolve reasoning efforts through relay 2026-08-05 20:34:56 -04:00
Bailey Dixon 7d6a0215ba fix(android): stabilize chat drawer interactions 2026-08-05 19:55:55 -04:00
Bailey Dixon 51ec55e08c fix(android): restore developer settings imports 2026-08-05 19:23:31 -04:00
Bailey Dixon 4edcd18398 Merge fix/standard-voice-speech into dev
# Conflicts:
#	app/src/main/kotlin/com/hermesandroid/relay/viewmodel/VoiceViewModel.kt
2026-08-05 19:19:27 -04:00
Bailey Dixon 3f50a94d03 Merge fix/developer-options-data into dev
# Conflicts:
#	app/src/main/kotlin/com/hermesandroid/relay/data/FeatureFlags.kt
#	app/src/main/kotlin/com/hermesandroid/relay/ui/components/ActiveConnectionSections.kt
#	app/src/main/kotlin/com/hermesandroid/relay/ui/screens/DeveloperSettingsScreen.kt
#	app/src/test/kotlin/com/hermesandroid/relay/data/FeatureFlagsTest.kt
#	docs/localization-status.json
2026-08-05 19:18:59 -04:00
Bailey Dixon 94d2231a80 Merge chore/backmerge-main-after-android-1.6.0 into dev 2026-08-05 19:17:30 -04:00
Bailey Dixon 5ac798d5b3 Merge feature/local-dev-work-20260805 into dev 2026-08-05 19:17:17 -04:00
Bailey Dixon ebe1a8ebc4 chore(android): streamline local dev loop 2026-08-05 19:16:36 -04:00
Bailey Dixon fc3a1c169b fix(android): stabilize pet and model sync feedback 2026-08-05 19:08:27 -04:00
Bailey Dixon 6eb2c8bd9e fix(android): smooth active session glow loop 2026-08-05 18:53:31 -04:00
Bailey Dixon cbe323cf6d Merge fix/android-chat-tail-settle into dev 2026-08-05 18:35:50 -04:00
Bailey Dixon 5401e8f274 fix(android): keep completed chat tail stable 2026-08-05 17:58:09 -04:00
Bailey Dixon 0b62afa6c3 Merge feature/android-session-activity-ui into dev 2026-08-05 16:54:50 -04:00
Bailey Dixon 02801dda70 feat(android): surface active session states 2026-08-05 16:52:56 -04:00
Bailey Dixon 39011d5600 Merge pull request #307 from Codename-11/fix/android-focus-pointer-input
fix(android): restore focus voice controls
2026-08-05 16:21:32 -04:00
Bailey Dixon e50c644d09 fix(android): restore focus voice controls 2026-08-05 14:30:20 -04:00
Bailey Dixon 3ff4e7cf8e Merge pull request #304 from Codename-11/fix/android-actionable-diagnostics
fix(android): make connection diagnostics actionable
2026-08-05 10:52:31 -04:00
Bailey Dixon 683d9712d8 fix(android): make connection diagnostics actionable 2026-08-05 10:36:56 -04:00
Bailey Dixon 7ae4a3987e Merge pull request #301 from Codename-11/feature/android-reliability-support
feat(android): unify local reliability reporting
2026-08-04 21:44:52 -04:00
Bailey Dixon 9dcb280eff feat(android): unify local reliability reporting 2026-08-04 21:32:18 -04:00
Bailey Dixon a90067292a Merge pull request #300 from Codename-11/fix/android-chat-render-identity
fix(android): stabilize chat identity and issue labeling
2026-08-04 21:26:06 -04:00
Bailey Dixon 4447886e80 chore(ci): disable automatic issue labeling 2026-08-04 21:15:54 -04:00
Bailey Dixon 5d00885291 fix(android): enforce stable chat render identity 2026-08-04 20:50:41 -04:00
Bailey Dixon bbea5d1b73 Merge pull request #297 from Codename-11/dev
fix(release): repair Server 1.5.1 metadata
2026-08-03 22:25:29 -04:00
Bailey Dixon cce4b9b1d6 Merge pull request #296 from Codename-11/fix/server-1.5.1-release-heading
fix(release): align server 1.5.1 changelog heading
2026-08-03 22:23:19 -04:00
Bailey Dixon c3ff201ecc Merge pull request #294 from Codename-11/dev
release: server-v1.5.1 and android-v1.6.1
2026-08-03 22:20:48 -04:00
Bailey Dixon 9fdb11ae94 chore(android): script foreground service demo capture 2026-08-02 20:56:31 -04:00
Bailey Dixon fd35c9b2b6 docs(android): document Play foreground services 2026-08-02 19:47:52 -04:00
Bailey Dixon 9dc625ddd0 chore: backmerge Android 1.6.0 release 2026-08-02 19:46:48 -04:00
Bailey Dixon e5b25ab650 Merge pull request #284 from Codename-11/dev
release(android): android-v1.6.0
2026-08-02 19:28:01 -04:00
Bailey Dixon 0e6d64f987 fix(android): restore standard voice narration 2026-07-26 08:42:04 -04:00
Bailey Dixon e3bc816624 fix(android): repair developer options and data actions 2026-07-25 16:29:36 -04:00
285 changed files with 22554 additions and 3079 deletions
-65
View File
@@ -1,65 +0,0 @@
name: Issue Triage
on:
issues:
types: [opened]
workflow_dispatch:
inputs:
issue_number:
description: "Issue number to label again"
required: true
type: string
concurrency:
group: issue-triage-${{ github.event.issue.number || github.event.inputs.issue_number }}
cancel-in-progress: false
permissions:
contents: read
issues: write
jobs:
auto-label:
if: >
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'issues' && github.event.issue.user.type != 'Bot')
runs-on: ubuntu-latest
steps:
- name: Label from title prefix and issue area
uses: actions/github-script@v8
env:
ISSUE_NUMBER: ${{ github.event.issue.number || github.event.inputs.issue_number }}
with:
script: |
const issue_number = Number(process.env.ISSUE_NUMBER);
const { data: issue } = await github.rest.issues.get({
owner: context.repo.owner, repo: context.repo.repo, issue_number,
});
const title = (issue.title || '').toLowerCase();
const body = (issue.body || '').toLowerCase();
const haystack = `${title}\n${body}`;
const labels = [];
if (title.startsWith('[bug]')) labels.push('bug');
else if (title.startsWith('[feature]') || title.startsWith('[feat]')) labels.push('enhancement');
else if (title.startsWith('[docs]')) labels.push('documentation');
if (/\b(cli|desktop|terminal|daemon|pty|hermes-relay (install|binary|tray))\b/.test(haystack)) labels.push('area:cli');
else if (/\b(dashboard|plugin ui|react)\b/.test(haystack)) labels.push('area:dashboard');
else if (/\b(relay|plugin|aiohttp|python|pairing|voice (transcribe|synthesize)|bridge (endpoint|route))\b/.test(haystack)) labels.push('area:plugin');
else if (/\b(readme|user-?docs|documentation)\b/.test(haystack)) labels.push('area:docs');
else if (/\b(android|app|compose|apk|phone|samsung|gradle|chat|voice|notification|sphere|keystore)\b/.test(haystack)) labels.push('area:android');
if (!labels.length) {
core.info('No deterministic label matched; leaving the issue for maintainer triage.');
return;
}
try {
await github.rest.issues.addLabels({
owner: context.repo.owner, repo: context.repo.repo, issue_number, labels,
});
core.info(`Applied labels: ${labels.join(', ')}`);
} catch (error) {
core.warning(`Could not apply ${labels.join(', ')}: ${error.message}`);
}
+13
View File
@@ -191,6 +191,19 @@ jobs:
# app/build/outputs/bundle/sideloadRelease/hermes-relay-<version>-sideload-release.aab
run: ./gradlew bundleRelease assembleRelease
# The Play AAB carries its mapping for Play Console deobfuscation, but
# sideload issue reports need the exact mapping from this immutable build.
# Keep both variants as a workflow artifact (not a public release asset).
- name: Retain R8 mappings for retrace
uses: actions/upload-artifact@v7
with:
name: android-r8-mappings-${{ needs.validate.outputs.version }}-${{ github.sha }}
path: |
app/build/outputs/mapping/googlePlayRelease/mapping.txt
app/build/outputs/mapping/sideloadRelease/mapping.txt
if-no-files-found: error
retention-days: 90
- name: Scan release DEX for unsupported collection APIs
run: |
python3 scripts/check-android-collection-apis.py \
+72
View File
@@ -8,8 +8,80 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/), and this
### Fixed
- **Issue area labels require maintainer review.** The unreliable keyword-based auto-labeling workflow no longer assigns ownership from ambiguous issue text.
- **Windows-trusted certificates work in the desktop CLI.** The packaged Windows binary and newer Node runtimes add the Windows certificate store without dropping bundled or operator-supplied roots, while TLS verification and Relay certificate pinning remain enforced.
## [Android 1.8.1] - 2026-08-09
### Fixed
- **Android preserves complete long-session transcripts.** API-server and profile-scoped Dashboard history reads now use explicit bounded pagination, retain compatibility with older unpaginated responses, and keep edit, retry, sharing, and recovery anchors stable beyond Hermes' latest-500 default window.
- **Android follows authoritative Gateway turn contracts.** Submit rejections retain the server's message without silently falling through to SSE, event envelopes reconcile consistently, and edit-and-regenerate requests send the required truncation confirmation.
## [Android 1.8.0] - 2026-08-09
### Added
- **Android chat keeps work in context and makes live turns easier to read.** Draft text, edits, quotes, and attachments stay with their connection, profile, and session; conversation search and prompt-turn navigation jump by stable message identity; message actions reveal smoothly on tap; quoted replies use linked previews without placing markup in the composer; assistant replies retain their compact high-contrast bubbles; and pending attachments support preview, removal, and accessible reordering.
- **Android reasoning and tool activity use a quieter transcript.** Live thinking opens as an inline disclosure and settles to a collapsed Thought row, while consecutive routine reads, searches, commands, browser actions, and device actions share one live activity ticker or concise completed summary. Approvals, failures, generated media, file changes, output risks, and delegated work keep their own visible lifecycle surfaces even when ordinary tool progress is hidden.
- **Android Profile Shelf makes agent switching immediate without mixing conversations.** The Chat header expands a compact, accessible shelf with ordered profile avatars, a subtle Server-default home badge on the resolved identity, last-session restoration, display hiding, lock controls, and one full switcher shared with Agent Passport.
- **Android accepts shared text as a new Chat draft.** Hermes Relay now appears in the system sharesheet for text, opens the active profile in a fresh conversation, and fills the composer for review without sending automatically.
### Changed
- **Android appearance controls are more expressive and easier to preview.** Theme presets, accent and shape customization, imported Sphere skins, and custom pet creation share one live-preview workflow while preserving separate agent, background, and companion identities.
### Fixed
- **Android restores complete Gateway activity and makes settled replies speakable.** Successful Gateway turns reconcile structured persisted tool calls even when an upstream server omits live tool lifecycle events, and a configured voice can read a completed assistant reply from its message actions without requiring Voice Mode. While that narration is active, the same message actions expose Stop without cancelling an unrelated chat turn.
- **Android chat matches standard keyboard, scrolling, and photo behavior.** Sentence capitalization is enabled, physical Enter can send or insert a newline according to a device-level setting, Ctrl/Command+Enter always submits, directional keys stay with the text caret, expanded thinking and tool content retains bottom-follow until the user scrolls away, and portrait attachments honor their EXIF orientation in previews and message viewers.
- **Android distinguishes live-turn corrections from queued follow-ups.** The composer names its current action with visible text and accessible state, successful gateway redirects show a correction lifecycle marker, and attachment-bearing follow-ups always enter the session-owned queue because the upstream redirect operation is text-only.
- **Android visibly explains quiet startup work without an empty chat bubble.** The full-size thinking animation now sits directly in the conversation lane with a stable reviewable status until the first answer text arrives, while recovery keeps its explicit reconnecting state.
- **Android keeps pets and screen chrome inside safe interaction bounds.** Floating companions avoid agent identity rows and controls during scrolling, remain touchable for their menu, and settings headers respect edge-to-edge system insets.
## [Android 1.7.1] - 2026-08-08
### Fixed
- **Android chat follows a growing live reply.** Bottom-owned conversations now observe each replacement of the streaming message list, keeping newly added lines visible while preserving the reader's position after a manual scroll away.
- **Hosted Hermes onboarding completes through the official Dashboard sign-in path.** Android recognizes hosted account addresses, uses the system-browser native PKCE flow, and resumes the verified Dashboard session after its loopback callback.
- **Live Android tool cards remain expandable while a run is active.** Streaming Gateway updates preserve stable card identity and merge tool arguments and result previews into the existing row, so details can be opened before the session finishes.
- **Completed Android replies format Markdown immediately.** Live assistant text keeps its stable plain renderer only while incomplete, then the same owned row transitions to rich code blocks, lists, emphasis, and links without leaving or reopening the session.
- **Android approval cards require an explicit labeled decision.** Reading or scrolling a guarded command, navigating away, backgrounding, recomposition, later turn activity, and card dismissal cannot submit or locally resolve it; pending requests remain bound to their owning profile and session until an explicit response or authoritative upstream expiry.
- **Android Agent Passport controls are readable and easy to dismiss.** Safety and speed choices use full-width accessible targets with plain-language selected-state explanations, while a persistent close action and boundary-aware downward swipe make the sheet reliably dismissible without stealing nested content scrolling.
- **Android queued messages stay with their originating chat.** Follow-ups now retain their exact connection, profile, session, run, route, attachments, and voice context across concurrent Gateway session switches instead of following whichever session is visible when a run finishes.
- **Android model pickers reject duplicate catalog identities before rendering.** Repeated provider/model rows from cached or refreshed inventories are merged at the provider boundary, while identical model IDs under different providers remain distinct choices with provider-aware reasoning capabilities.
- **Android session pins and archives survive app restarts.** The session drawer now reads and updates the owning Hermes profile's durable session metadata, rolls failed changes back, and makes unpinned stars clearly distinct in light theme.
## [1.6.1] - 2026-08-08
### Fixed
- **The Dashboard plugin hands hosted Hermes connections to Android reliably.** Mobile setup exposes the canonical Dashboard address and keeps dialog focus handling contained, so system-browser authentication can return to the correct connection without disrupting the Dashboard.
## [Android 1.7.0] - 2026-08-06
### Added
- **Android exposes provider-aware reasoning controls.** The effort drawer consumes exact upstream or optional Relay capability metadata for each provider/model identity, while unmodified or older Hermes installations retain a fail-soft standard fallback including `max` and `ultra`.
- **Android support information is local, redacted, and reviewable.** Fatal crashes and handled failures share a bounded on-device record, Diagnostics can copy or share the exact reviewed text, and nothing is uploaded automatically.
### Fixed
- **Android chat chrome follows its active interaction state.** Opening the session drawer dismisses the composer keyboard, refreshed sessions keep their newest row visible, and floating pets wait for measured chat terrain, sit flush on supported rails, and treat the complete scroll-to-bottom control as forbidden space.
- **Android pets and optional model discovery initialize quietly.** Floating companions wait for a measured overlay before taking their home position, and background API model-inventory failures retain actionable local diagnostics without interrupting chat with a generic notice.
- **Android chat and Voice stay precisely bottom-pinned through replies, restores, and layout changes.** The active tail keeps its stable live renderer until another row takes ownership, restored sessions follow late composer and message measurement without overriding a reader, and bottom-owned transcripts settle to the exact list boundary after replies and keyboard animations instead of leaving a small hidden remainder.
- **Android Focus voice controls remain responsive.** The modal click-through guard now sits behind the voice UI instead of consuming pointer events from the mic, close, expansion, and panel controls.
- **Android diagnostics explain what failed and what to try next.** Relay, route, WebSocket, and API checks distinguish the saved route from the redacted request they actually attempted, name the operation, and provide targeted guidance for connection, DNS, timeout, TLS, authentication, rate-limit, and server failures.
- **Android chat and Voice keep one render identity through recovery.** Checkpoint restore, streamed callbacks, server-ID adoption, and replay now resolve the same owned transcript row before publication, preventing recurring Compose duplicate-key crashes.
- **Android crash reports retain actionable release context.** Reports identify the Android surface, avoid exposing hosts and credentials, migrate earlier local crash records, and release automation retains exact Play and sideload R8 mappings for retrace.
## [1.6.0] - 2026-08-06
### Added
- **Relay supplies exact provider/model reasoning capabilities when providers expose them.** The bounded, profile-aware overlay resolves dynamic catalogs for OpenAI Codex, Copilot, LM Studio, and Ollama Cloud, keeps provider credentials on the host, and leaves unknown or unavailable catalogs on the advisory fallback.
## [Android 1.6.1] - 2026-08-03
### Fixed
+32 -7
View File
@@ -17,17 +17,41 @@ That's it — no extra setup or credentials required for a debug build.
Helper scripts for common development tasks:
```bash
scripts/dev.bat build # Build debug APK
scripts/dev.bat build # Build the sideload debug APK
scripts/dev.bat compile # Compile sideload Kotlin only
scripts/dev.bat test-one "com.hermesandroid.relay.SomeTest" # Run one test class
scripts/dev.bat install-fast # Build arm64 only + install + launch
scripts/dev.bat release # Build signed release APK
scripts/dev.bat bundle # Build release AAB for Google Play
scripts/dev.bat run # Build + install + launch + logcat
scripts/dev.bat test # Run unit tests
scripts/dev.bat run # Build sideload + install + launch + logcat
scripts/dev.bat test # Run sideload debug unit tests
scripts/dev.bat version # Show current version
scripts/dev.bat relay # Start relay server (dev, no TLS)
```
Linux/macOS equivalent lives at `scripts/dev.sh`.
### Fast Android iteration
Gradle's daemon, local build cache, configuration cache, and parallel task
execution are enabled for repeat local builds. Keep the same Gradle JVM
configuration between invocations and do not add `--no-daemon` to normal dev
commands; a different heap or Java home starts a separate daemon and discards
the warm-process benefit.
Use the narrowest command that proves the change:
1. `scripts/dev.bat compile` for a Kotlin compile check.
2. `scripts/dev.bat test-one "<fully-qualified-class-or-pattern>"` for a focused regression.
3. `scripts/dev.bat install-fast` when the result must run on the connected
arm64 phone. This passes `-Phermes.devAbi=arm64-v8a`, avoiding the x86,
x86_64, and armeabi-v7a native libraries in the local APK.
4. `scripts/dev.bat prepush` before pushing Android work.
`install-fast` is intentionally phone-specific. Use `install` for a universal
sideload debug APK or when the target ABI is not arm64. Release builds remain
universal and are unaffected unless `-Phermes.devAbi` is explicitly supplied.
## Repository Structure
```
@@ -51,12 +75,12 @@ The legacy `relay_server/` directory is a thin compatibility shim around `plugin
| Component | Stack |
|-----------|-------|
| **Android App** | Kotlin 2.0, Jetpack Compose, Material 3, OkHttp |
| **Android App** | Kotlin 2.4, Jetpack Compose, Material 3, OkHttp |
| **Relay Server** | Python 3.11+, aiohttp |
| **Serialization** | kotlinx.serialization |
| **Build** | AGP 9, Gradle 8.13, JVM toolchain 17 |
| **Build** | AGP 9.3.1, Gradle 9.6.1, JVM toolchain 17 |
| **CI/CD** | GitHub Actions (lint, build, test, signed APK artifacts) |
| **Min SDK** | 26 (Android 8.0) / Target SDK 35 |
| **Min SDK** | 26 (Android 8.0) / Target SDK 36 |
## Running the Relay Locally
@@ -174,7 +198,8 @@ Release notes (`RELEASE_NOTES.md`, `app/src/main/assets/whats_new.txt`, `docs/pl
CI in one cached Gradle invocation. Run it before pushing Android PR updates
to catch common hosted failures without waiting for another full Actions
cycle; hosted CI remains the exhaustive all-variant gate.
- **Android unit tests:** `scripts/dev.bat test` (runs JUnit + MockK + Compose testing)
- **Focused Android unit test:** `scripts/dev.bat test-one "<fully-qualified-class-or-pattern>"`
- **Android unit tests:** `scripts/dev.bat test` (runs the sideload debug JUnit + MockK + Compose suite)
- **Python tests:** `python -m unittest plugin.tests.test_<name>` from the repo root with the hermes-agent venv active. `pytest` works too but the pre-existing `conftest.py` imports a module that isn't always installed — `unittest` avoids that entirely.
CI is split into path-filtered workflows: `.github/workflows/ci-android.yml` (lint + build + test on app/Gradle changes), `.github/workflows/ci-server.yml` (syntax check + focused server tests on plugin/Python changes), and `.github/workflows/ci-desktop.yml` (desktop type/build/smoke checks). They run on pushes to `main` and `dev` and on PRs targeting either when their paths are touched.
+349
View File
@@ -1,5 +1,354 @@
# Hermes-Relay — Dev Log
## 2026-08-09 — Gateway activity recovery and chat speech
Successful Android Gateway turns now reconcile against their profile-owned,
structured session history. This recovers persisted tool calls when an upstream
Gateway completes a turn without emitting live tool lifecycle events; assistant
prose is never inspected for inferred activity, and the recovered calls continue
through the existing Off, Compact, and Detailed display policy.
The Speak message action now follows configured voice readiness and idle output
state instead of active Voice Mode or presentation style. A settled assistant
reply can therefore be read aloud directly from chat, while live or provider
playback still prevents overlapping output. One-shot message narration owns its
completion state outside Voice Mode and replaces Speak with Stop while active;
stopping drains only that narration pipeline and does not cancel a chat turn
started while the response was playing.
## 2026-08-09 — Quiet reasoning and grouped tool activity
Android Chat now treats reasoning and routine tools as transcript scaffolding.
Visible live reasoning opens automatically without a tinted card, then collapses
to a quiet Thought disclosure when it settles unless the reader has explicitly
chosen its state. Empty reasoning remains absent and the existing first-token
status continues to own the waiting state.
Top-level routine calls retain their source order but render as consecutive
activity runs. A live run keeps one summary and one latest-activity ticker in a
stable footprint; a settled run becomes one collapsed summary that can disclose
the original identity-preserving tool rows. File edits, approval/question tools,
generated media, failures, output-risk findings, and delegated work split runs
and retain independent surfaces. Off hides only ordinary activity runs, Compact
uses compact disclosed rows, and Detailed preserves the full per-tool detail
surface on demand. Expansion still yields bottom-follow ownership, and each run
registers its measured bounds as Chat pet terrain.
## 2026-08-09 — Android chat experience and attachment polish
The Android composer now declares sentence capitalization and a Send IME
action. A device-level setting chooses whether unmodified physical Enter sends
or inserts a newline; Shift+Enter remains a newline and Ctrl/Command+Enter
always submits. Every submit route converges on the existing live-turn owner,
so the current gateway state still decides whether content steers the active
turn or queues behind it. Directional focus traversal is cancelled while the
editor owns focus so hardware arrow keys continue to move the caret and
selection.
Conversation bottom-follow now remains owned when thinking or tool details
expand. Only an actual drag ending above the bottom yields that ownership, and
a chat first measured while the IME is already visible now captures the same
resize-follow state as a keyboard opened after composition.
Bitmap-backed attachment surfaces now apply EXIF rotation and reflection before
display. Attachment reads and Base64 conversion also leave the UI thread, so
selecting a larger photo no longer performs the full ingestion path inside the
activity-result callback.
Composer drafts now belong to the stable connection, profile, and session
identity. Text, edit context, and pending attachments restore when returning to
a chat without persisting attachment bytes. Pending attachments expose bounded,
orientation-aware previews plus explicit remove and reorder controls.
Conversation overflow now opens transcript search with previous/next matches
and a prompt-turn rail, both keyed to the same stable UI identity as the message
list. Assistant prose retains the compact bubble and subtle edge treatment that
keeps it legible above the animated chat background. Tapping a message reveals
the existing copy, quote, speak, and edit actions with reduced-motion-aware
expansion and accessible targets.
Quotes are composer-owned structured references instead of raw blockquote text.
The composer and sent message render a linked, highlighted author preview; the
transport remains ordinary Markdown so unmodified Desktop and TUI clients show
a readable quoted reply. Thinking and top-level tools continue to use their
independent compact thought bubbles and configured compact or full tool cards,
without an aggregate completion card. The composer also names Correction and
Queue states with visible labels. Gateway redirects remain text-only:
follow-ups with attachments are forced through the existing destination-owned
queue so files cannot be left behind by a correction request.
## 2026-08-08 — Standalone Android thinking status
Blank streaming assistant rows now present the full-size working animation
directly in the conversation lane above the visible `Still working…` label,
without painting an empty assistant bubble around the status. The first answer
token replaces that standalone state with the normal response bubble, while
recovery retains the distinct `Reconnecting to your answer…` wording. The
status owns one stable TalkBack description and suppresses animated child
nodes, avoiding repeated announcements without claiming measurable progress.
## 2026-08-08 — Android text-share draft handoff
The shared Android manifest now advertises a `text/*` `ACTION_SEND` target for
both app flavors. `MainActivity` accepts only non-blank single-item text shares
and places them in a process-local, identity-fenced handoff that survives cold
Compose initialization. Once the configured chat context settles, the app root
navigates to Chat and delegates draft creation and composer prefill to
`ChatViewModel`.
The ViewModel reuses the existing new-chat lifecycle, preserving Gateway
background-turn reconciliation and the active connection/profile/transport
namespace. Composer prefills use a one-consumer conflated channel so an intent
received before Chat composition is delivered once. Shared text is never
routed through message sending; the user must review and submit it explicitly.
## 2026-08-08 — Android Profile Shelf and profile-context identity
Chat profile selection now lives in a collapsible shelf directly below the top
app bar. The header toggles the shelf, the active capsule opens Agent Passport,
inactive 48 dp avatars switch context, and a pinned overflow opens the same full
switcher used by Passport. Saved ordering and hidden state drive both surfaces;
the selected hidden profile remains disclosed, while a one-identity shelf stays
out of the layout. Long-press actions expose inspection, Passport, profile lock,
and hiding without adding activity or presence claims.
The shelf uses the chat surface instead of a second elevated toolbar. A neutral
active capsule, 36 dp avatar artwork inside 48 dp targets, compact spacing, and
a contained overflow affordance keep the row visually subordinate to Chat. When
Server default resolves to a concrete profile, that profile's avatar remains
the identity and a small home badge discloses its default routing role.
Local avatar lookup remains keyed to the Server-default presentation identity,
so an image customized while that row is selected appears consistently in both
the Chat header and shelf rather than being re-keyed to whichever explicit
profile is currently active.
The Server-default sentinel is now distinct from a profile literally named
`default`. Profile selection restores the last compatible connection/profile/
transport session, otherwise leaves a fresh draft. Gateway turns detach and
reconcile in their original session, live SSE turns keep switching disabled,
and every profile transition clears session-scoped model, provider, personality,
reasoning, approval, Fast, and YOLO state before the destination session seeds
its own values. Server sticky-default state is never written.
## 2026-08-08 — Streaming reply tail follow
Android's conversation-bottom follower now reads the current immutable message
list from Compose state inside its long-lived layout observer. Each streamed
replacement can therefore advance the viewport as the active bubble gains
lines, while dragging or scrolling away still releases bottom ownership.
## 2026-08-08 — Settled live replies transition to Markdown
Android now releases the live plain-text renderer immediately after an
assistant row settles. The transition retains the row's stable UI identity,
commits the final live frame before replacing its selectable text topology,
and anchors the same bottom-owned row during the Markdown remeasure. Readers
who scrolled away retain their viewport, while completed code fences, lists,
emphasis, links, and interrupted partial replies no longer require session
navigation before rich rendering appears.
## 2026-08-08 — Explicit-consent ownership for Android approvals
Android no longer treats unrelated Gateway activity or a terminal display event
as proof that an approval was resolved. Approval cards remain pending through
scrolling, recomposition, navigation, and background restoration, and retain
their exact connection, profile, and session ownership. Only a labeled action
that successfully reaches `approval.respond`, an authoritative upstream expiry,
or an explicit interrupt can retire the local request; the interrupt path stays
visibly denied because upstream force-denies it.
## 2026-08-08 — Agent Passport control and dismissal accessibility
The Android Agent Passport keeps its title and explicit close action outside
the nested content scroller. Material bottom-sheet gestures are enabled again,
so a downward gesture scrolls long content toward its top boundary before the
sheet receives the gesture and dismisses; backdrop and Back dismissal retain
the same callback.
Safety and speed choices now sit below their labels instead of competing for a
narrow horizontal column. Every segment provides at least a 48 dp target,
allows two-line labels, exposes radio-selection semantics, and states the
meaning of the current approval, chat override, or processing-tier choice in
plain language. The layout remains scrollable on compact heights and at larger
font scales without moving the close action off-screen.
## 2026-08-08 — Session-owned Android send queues
Android follow-up queues now capture the composing connection/profile context,
stored session, configured transport, originating run, attachments, and voice
context as one immutable destination. Queue presentation is filtered to the
visible session, while completion eligibility is tied to the exact run and live
Gateway generation that owned the queue. A completion from another session or
an older live generation cannot dispatch through the current composer route.
In-flight checkpoints retain bounded queued text across process restoration.
Attachment bytes are not copied into Preferences DataStore; an attachment queue
that cannot be restored is rejected with a visible review-and-resend notice.
Connection replacement and session deletion cancel their owned queues, while
switching among concurrent Gateway sessions preserves each session's queue.
## 2026-08-07 — Provider-owned model inventory identity
Android now normalizes Gateway and API model inventories before publishing
them to picker consumers. Repeated provider rows merge by canonical provider
slug, repeated exact model IDs collapse within that provider, and capability
metadata follows the same provider/model identity. Models intentionally offered
by different providers remain distinct choices, even when their display labels
and model IDs match.
The searchable picker groups and keys rows by provider slug plus exact model ID
instead of provider display text. Cached loads, dynamic refreshes, API aliases,
and Manage inventory use the same idempotent identity rule, preventing duplicate
catalog data from reaching keyed Compose lists without hiding valid routes.
## 2026-08-05 — Restored chat bottom ownership and effort fallback clarity
Opening an existing Android session now retains exact bottom ownership through
late, non-streaming layout changes. Composer capability controls, status rows,
and restored message content can finish measuring after history first reaches
the footer; a session-scoped geometry observer corrects those changes without
using a fixed delay. New-message following remains governed by the smooth
auto-scroll setting, while a real drag, IME ownership, and the Voice dock keep
their existing anchors.
The advisory effort drawer now states that Hermes does not advertise exact
levels for the selected model before explaining why standard options are shown.
The wording is consistent across all shipped Android locales.
## 2026-08-05 — Provider-aware reasoning effort discovery
The optional Relay plugin now exposes a bearer-protected, profile-aware model
capability overlay without requiring changes to upstream Hermes. Android merges
that overlay with the standard `model.options` inventory using exact provider
and model identities, while older or unpaired Relay installations continue with
the canonical advisory fallback.
Dynamic LM Studio, Ollama Cloud, and Copilot discovery is bounded by a shared
network limiter, cached by profile, endpoint, model, and credential fingerprint,
and fenced across refresh generations. Neither credentials nor internal cache
scope are returned to clients. Composer controls, Agent Passport, session
creation, and asynchronous server reconciliation share the same capability
resolver so a displayed effort cannot silently differ from the value sent.
## 2026-08-05 — Chat drawer and companion terrain ownership
The Chat screen now clears composer focus when the session drawer commits to
opening, dismissing the IME without continuously clearing focus from drawer
search or rename fields. Drawer refreshes override keyed list anchoring only
when the leading session identity changes, keeping the newest row visible after
activity-based reordering.
Floating companions wait for Chat's measured composer rail before publishing
their first roaming position. Their collision footprint contains both the
pointer target and rendered sprite, and the complete scroll-to-bottom control
envelope is an obstacle rather than a landing perch. Supported rails add no
visual lift, and the floating-only renderer aligns each frame's opaque bottom
edge to its canvas baseline so transparent atlas padding cannot make pets hover;
centered previews and message avatars remain unchanged.
## 2026-08-05 — Measured pet placement and passive model sync
The floating pet now remains unpublished until the app-level overlay has a
positive measured viewport. Its initial home coordinate is therefore derived
from the real safe bounds instead of the zero-size pre-measure bounds that
collapsed to the top-left corner.
API provider inventory remains an optional background catalog on Gateway-led
connections. A timeout, refusal, or unavailable optional route no longer emits
a global chat notice during initialization, reconnection, or connection-sheet
refresh. The failure is retained as a contextual warning in local Diagnostics,
including the operation, endpoint role, redacted stack trace, preserved network
cause, and targeted troubleshooting guidance. Cached and Gateway-owned model
options remain unchanged.
## 2026-08-05 — Stable chat-tail completion
Chat and Voice now treat the active streamed reply as the owner of its live
renderer until a different row becomes the conversation tail. Stream
completion retains the existing Compose subtree and list anchor; the full
Markdown renderer is deferred until the row is no longer active or the session
is revisited.
The last-in-group timestamp occupies its final geometry from the first
streaming frame and is only revealed at completion. Measured positive growth
during an active stream continues to follow the bottom without replacing the
logical anchor. Once completion layout stabilizes, a bottom-owned transcript
settles to the exact LazyColumn boundary; proximity slop is reserved for
retaining follow intent during motion and cannot define the final position. The
visible footer supplies the exact remaining distance so rounding or adjacent
layout changes cannot leave a residual forward range.
IME expansion participates in that same viewport owner. A transcript already
at the bottom advances by the measured viewport-height loss throughout the
keyboard animation, then settles exactly after inset updates stop on both open
and close. A transcript being read above the bottom preserves its existing
anchor, and a real drag cancels keyboard follow immediately. Host-side coverage
verifies renderer ownership, unchanged bubble height, exact footer settling,
keyboard arming, viewport loss, completion/IME settlement ownership, and
history-reading behavior.
## 2026-08-05 — Focus voice input boundary repair
The Focus voice presentation remains modal without installing a consuming
pointer handler on the full overlay ancestor. Its click-through guard is now a
behind-content sibling: empty-space gestures cannot reach the chat or drawer,
while the mic, close, expand/collapse, and panel controls receive their full
pointer sequence.
Host-side Compose coverage injects real touch events instead of invoking
semantic click actions. It verifies both child callback delivery and the modal
background boundary so the two requirements cannot regress independently.
## 2026-08-05 — Actionable Android connection diagnostics
Android diagnostic entries now separate the configured route from the exact
request operation and path used to test it. Relay health checks identify the
HTTP `/health` probe that precedes a WebSocket connection, route selection
records its Dashboard, API, or Relay probe, and WebSocket and API checks name
their handshake or authentication stage.
Known network and HTTP failure classes attach a bounded next step for refused
listeners, DNS, routing, timeouts, TLS, credentials, rate limits, missing
routes, and server failures. The activity list, status timeline, detail dialog,
copy text, and GitHub issue prefill all carry the same context. Public issue
text preserves protocol and request paths while redacting hosts, credentials,
queries, and user information.
## 2026-08-04 — Android transcript identity ownership
ChatHandler now owns one render identity for every published transcript row.
Checkpoint recovery and all streamed message mutations resolve both the mutable
server/domain ID and the stable UI identity, so history adoption cannot leave a
stale client reference that appends a second row. The publication boundary also
coalesces repeated render identities before Chat or Voice can observe them,
while keeping the first transcript position and latest state.
Focused coverage composes history reconciliation with checkpoint restore,
exercises stale post-adoption callbacks, and runs deterministic transition
sequences across restore, replay, deltas, thinking, and usage updates. Voice's
temporary transcript row now occupies an auxiliary key namespace disjoint from
real message rows.
## 2026-08-04 — Android reliability and support foundation
Android fatal capture and centrally classified handled failures now converge on
a versioned, allowlisted reliability record. Reports are redacted before local
persistence, capped at 20 records with 14-day retention, written atomically,
and correlated only with random app/report identifiers. Expected cancellation
and permission denial remain non-reportable. The pre-existing one-file crash
format migrates locally on first launch.
Crash recovery leads with the recovery outcome and no-upload guarantee, then
requires an explicit review before copy, share, or GitHub actions. Diagnostics
adds an offline support-information review using the same exact redacted text.
Android issue prefills now request the Android area while repository-wide issue
ownership remains maintainer-reviewed, and the release workflow retains both
variant R8 mappings for deterministic retrace.
The architecture audit defers an ANR watchdog, richer allowlisted breadcrumbs,
hashed product correlation, and OOM emergency writing until their lifecycle,
privacy, and false-positive behavior can be validated on devices.
## 2026-08-02 — Android Russian localization
Android now ships complete Russian catalogs for the main and sideload builds.
+4 -4
View File
@@ -1,8 +1,8 @@
# Hermes-Relay-Server v__VERSION__
**Release Date:** August 3, 2026
**Release Date:** August 8, 2026
This patch makes intentional re-pairing repair the existing device record instead of accumulating duplicate Relay sessions.
This patch makes the optional Dashboard plugin's Android setup handoff reliable for hosted Hermes connections.
Standard chat, session history, and Vanilla Hermes voice remain upstream-owned and do not require this plugin.
@@ -10,8 +10,8 @@ Standard chat, session history, and Vanilla Hermes voice remain upstream-owned a
### Fixed
- **Re-pairing replaces stale credentials for the same device.** After the host approves a new pair, Relay revokes older sessions and refresh credentials that belong to that device before issuing the replacement.
- **Existing and unrelated sessions remain operator-controlled.** The Dashboard and `/relay revoke <token-prefix>` continue to provide explicit cleanup without treating optional Relay pairing as a requirement.
- **Canonical hosted-Hermes setup handoff.** The Dashboard plugin supplies the verified Dashboard address Android needs to continue through the official system-browser authentication flow.
- **Contained dialog focus behavior.** Mobile setup dialogs retain their own focus and keyboard handling without disrupting the surrounding Dashboard.
## Install / update
+10 -7
View File
@@ -138,7 +138,7 @@ Full server setup, TLS, and systemd details: [docs/relay-server.md](docs/relay-s
<table>
<tr>
<td align="center" width="25%"><img src="assets/screenshots/01_startup.png" alt="Cold start" width="100%"><br><sub><b>Cold start</b></sub></td>
<td align="center" width="25%"><img src="assets/screenshots/01_voice_conversation.png" alt="Voice controls in chat" width="100%"><br><sub><b>Voice in chat</b></sub></td>
<td align="center" width="25%"><img src="assets/screenshots/02_chat.png" alt="Streaming chat" width="100%"><br><sub><b>Streaming chat</b></sub></td>
<td align="center" width="25%"><img src="assets/screenshots/03_voice.png" alt="Hands-free voice" width="100%"><br><sub><b>Hands-free voice</b></sub></td>
<td align="center" width="25%"><img src="assets/screenshots/04_sessions.png" alt="Session history" width="100%"><br><sub><b>Session history</b></sub></td>
@@ -267,11 +267,14 @@ Already installed? The same recipe is auto-loaded as a Hermes skill — invoke `
```bash
# Android: open the repo root in Android Studio, wait for Gradle sync, Run (Shift+F10).
scripts/dev.bat build # Build debug APK
scripts/dev.bat build # Build sideload debug APK
scripts/dev.bat compile # Compile sideload Kotlin only
scripts/dev.bat test-one "com.hermesandroid.relay.SomeTest" # Focused unit test
scripts/dev.bat install-fast # arm64 phone build + install + launch
scripts/dev.bat release # Build signed release APK
scripts/dev.bat bundle # Build release AAB for Google Play
scripts/dev.bat run # Build + install + launch + logcat
scripts/dev.bat test # Run unit tests
scripts/dev.bat run # Build sideload + install + launch + logcat
scripts/dev.bat test # Run sideload debug unit tests
scripts/dev.bat version # Show current version
scripts/dev.bat relay # Start the relay server (dev, no TLS)
```
@@ -280,13 +283,13 @@ scripts/dev.bat relay # Start the relay server (dev, no TLS)
| Component | Stack |
|-----------|-------|
| **Android app** | Kotlin 2.0, Jetpack Compose, Material 3, OkHttp |
| **Android app** | Kotlin 2.4, Jetpack Compose, Material 3, OkHttp |
| **Hermes-Relay CLI** | TypeScript, Bun-compiled native binary, Node ≥21 (source/dev), zero runtime deps |
| **Server / plugin** | Python 3.11+, aiohttp |
| **Serialization** | kotlinx.serialization (Android) |
| **Build** | AGP 9, Gradle 8.13, JVM toolchain 17 (Android); `tsc` + `bun build --compile` (CLI) |
| **Build** | AGP 9.3.1, Gradle 9.6.1, JVM toolchain 17 (Android); `tsc` + `bun build --compile` (CLI) |
| **CI/CD** | GitHub Actions — lint, build, test, APK artifact, CLI binaries per platform |
| **Min SDK** | 26 (Android 8.0) · Target SDK 35 |
| **Min SDK** | 26 (Android 8.0) · Target SDK 36 |
<details>
<summary><b>Repository structure</b></summary>
+13 -1
View File
@@ -494,6 +494,14 @@ the new app version and a higher `appVersionCode`.
in `app/build.gradle.kts`. Never rename the sideload APK — the
in-app update checker matches assets by `.apk` + `sideload` in the
name, and user-docs verify steps cite the filename.
The release workflow also retains
`app/build/outputs/mapping/{googlePlayRelease,sideloadRelease}/mapping.txt`
for 90 days in the `android-r8-mappings-<version>-<sha>` workflow
artifact. It is intentionally not a GitHub Release asset. To symbolicate an
in-app or sideload report, download the artifact for the exact version/SHA and
run Android's retrace tool with the matching flavor mapping:
`retrace <mapping.txt> <obfuscated-trace.txt>`. Play reports can additionally
use the mapping bundled into the uploaded AAB through Play Console.
- `app/src/main/assets/whats_new.txt` — in-app "What's New" content
shown in the settings/about screen. Update with the version number
and a brief feature summary. Gets stale silently if forgotten
@@ -509,7 +517,11 @@ the new app version and a higher `appVersionCode`.
the version reference and the "Release Notes" section that gets
pasted into the Play Console "What's new" field. Keep the Play
"What's new" within **500 characters** and framed around the
release's themes, not a feature dump.
release's themes, not a feature dump. Compare its **Foreground service
permissions** section with the merged `googlePlayRelease` manifest and
complete Play Console declarations for every declared service type before
approval; the Publisher API can upload a draft and still reject promotion
when an App content declaration is missing.
#### Scrub for public distribution
+17 -13
View File
@@ -1,10 +1,10 @@
# Hermes-Relay-Android v1.6.1
# Hermes-Relay-Android v1.8.1
**Release Date:** August 3, 2026
**Release Date:** August 9, 2026
## Download
> Installing on your phone? Download `hermes-relay-1.6.1-sideload-release.apk` and tap it for the full feature set, or install the conservative build from [Google Play](https://play.google.com/store/apps/details?id=com.axiomlabs.hermesrelay).
> Installing on your phone? Download `hermes-relay-1.8.1-sideload-release.apk` and tap it for the full feature set, or install the conservative build from [Google Play](https://play.google.com/store/apps/details?id=com.axiomlabs.hermesrelay).
The `.aab` file is a Play Console upload bundle and cannot be installed by tapping it on a phone.
@@ -12,19 +12,23 @@ Verify the download against `SHA256SUMS.txt`. See the [sideload guide](https://h
## Summary
This patch clarifies optional Relay recovery, restores the full session drawer, and fixes several chat and Voice regressions.
This patch keeps long Hermes conversations complete and aligns Android's
Gateway behavior with current upstream turn contracts.
## Fixed
- Optional Relay failures stay within Relay-only surfaces, use consistent status labels, and only request re-pairing when the stored Relay credential actually needs it.
- Foreground recovery reconnects immediately after ordinary backoff, while retained credentials are described as stored details rather than an active in-memory session.
- Session history loads its 200-row drawer window through upstream-compatible 100-row pages instead of failing with HTTP 422.
- Selecting text remains stable when a streamed response changes from live text to rendered Markdown.
- Manual Voice recording waits for barge-in microphone teardown and gives a useful recovery message when the microphone is unavailable.
- New-chat coaching yields while Voice owns the composer, so it no longer covers the expanding Voice drawer.
- Complete transcript reads page explicitly across both API-server and
profile-scoped Dashboard routes, so sessions beyond Hermes' latest-500
default retain stable history, sharing, retry, edit, and recovery anchors.
- Gateway submit rejections preserve the authoritative server message without
silently falling through to SSE.
- Gateway event envelopes reconcile consistently, and edit-and-regenerate
requests send the required truncation confirmation.
## Install / Verify
- App version: **1.6.1** (versionCode **38**).
- Standard Chat and Vanilla Hermes voice continue to work against unmodified upstream Hermes.
- Same-device Relay re-pair replacement requires the optional Server 1.5.1 plugin; Dashboard and `/relay revoke <token-prefix>` remain available for explicit cleanup.
- App version: **1.8.1** (versionCode **42**).
- Standard Chat, sessions, Manage, and Vanilla Hermes voice continue to work
against unmodified upstream Hermes.
- The optional Relay plugin is not required for standard Android chat or hosted
Dashboard authentication.
+17 -1
View File
@@ -24,6 +24,22 @@ multi-session protocol before treating `lifecycle=session` as an isolation claim
---
## Split fast Android unit tests from resource and screenshot tests
The quick-loop commands now narrow execution to the sideload debug variant and
support one-class filtering, but all `:app` unit tests still share one Android
test variant. That variant includes merged Android resources, gives every test
worker a 2 GiB heap, runs on JDK 21, and enables Roborazzi recording because a
small subset of Robolectric/screenshot tests requires those settings.
Create a separate resource/screenshot test lane so pure state, parser, routing,
and formatting tests can run as ordinary JVM tests without Android resource
packaging. Keep golden-image recording explicit rather than applying it to all
unit tests, preserve a CI task that runs both lanes, and benchmark cold plus
warm focused-test latency before adopting the split.
---
## Verify Android native dashboard sign-in on device
Android now selects Custom Tab + PKCE for HTTPS gateways that advertise
@@ -1258,7 +1274,7 @@ Follow-ups:
profile/skill-aware empty-state chips and the ~40-flow recomposition hotspot at
the top of `ChatScreen`.
- **Pet hot-load + in-app add/remove (shipped 2026-06-20).** Pets now live-refresh: an `avatarsRefreshTick` keys the avatar `produceState` in `RelayApp`, and Appearance re-scans `pets/` on open and after in-app import/delete — no app restart. Appearance gained "Add a pet" (SAF `.zip` import via `PetImporter`, zip-slip/zip-bomb guarded + validated through `toAvatar`) and an "Installed pets" list with per-pet remove (`PetLoader.deletePet`, confirm dialog, Sphere fallback). Remaining:
- **Sphere-skin parity.** Skins are still process-scoped + `adb push` only — the live tick and the importer cover pets, not skins. Extend the tick to `loadUserSkins` and add a `.json` skin import if hot-loading/adding skins in-app is wanted.
- **Sphere-skin parity (shipped 2026-08-09).** Appearance now imports a bounded, validated declarative `.json` skin through the system picker, hot-refreshes the shared sphere registry, and selects the imported skin without an app restart.
- `**adb push` into `Android/data` hangs on Samsung scoped storage.** Confirmed: pushing a pet pack to `/sdcard/Android/data/<pkg>/files/pets/` stalls (no bytes written) although `adb shell ls` of the dir works. In-app `.zip` import is the supported path; `/sdcard/Download` pushes fine. Consider softening `docs/pet-spec.md` + user-docs to lead with in-app import over adb.
- **On-device import/delete smoke.** Import `/sdcard/Download/lucy.zip` via Add a pet → confirm Lucy appears, selects, and animates all states; then remove it and confirm the avatar falls back to the Sphere.
- **Pet behavior model — richer state association (spec'd 2026-06-19, `docs/pet-spec.md` "Agent states &amp; pet behavior").** Shipped: the honesty clamp (declared reactivity ∩ `PET_RENDERER_CAPABILITIES`), the friendly `writing` alias, the `**working`/tool-use overlay** (pet-local sub-state from `toolCallBurst`; opt-in `working` clip drives both the swap and the Tools badge), the **one-shot reaction layer** (`greet`/`wake` on appear, `done`/`celebrate` on turn-finish — opt-in, play-once-then-revert, transition-derived; `ONE_SHOT_MAX_MS` backstop), and `**intensity` modulation** (opt-in `reactive.intensity` → live playback speedup ≤1.6× via `rememberUpdatedState`; un-clamps the Activity badge). Voice · Tools · Activity reactivity is now complete. Remaining:
+21
View File
@@ -7,6 +7,15 @@ plugins {
alias(libs.plugins.play.publisher)
}
val supportedHermesDevAbis = setOf("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
val hermesDevAbi = providers.gradleProperty("hermes.devAbi").orNull
hermesDevAbi?.let { requestedAbi ->
require(requestedAbi in supportedHermesDevAbis) {
"Unsupported hermes.devAbi '$requestedAbi'. Expected one of: " +
supportedHermesDevAbis.sorted().joinToString()
}
}
// Rename output artifacts to include the app version. AGP respects
// `archivesName` for both APK (assemble*) and AAB (bundle*) outputs, so
// this single line produces `hermes-relay-<version>-<flavor>-<buildType>`
@@ -43,6 +52,17 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
// Optional local-only fast path for device iteration. Native voice/VAD
// dependencies make the universal sideload APK very large, while a
// connected phone needs only its own ABI. Release and normal debug
// builds remain universal unless the developer explicitly supplies
// -Phermes.devAbi=<abi>.
hermesDevAbi?.let { requestedAbi ->
ndk {
abiFilters += requestedAbi
}
}
// Feature flags — DEV_MODE enables all experimental features in debug builds
buildConfigField("boolean", "DEV_MODE", "false")
}
@@ -300,6 +320,7 @@ dependencies {
// Coil 3 — async image loading for generated images in chat
implementation(libs.coil.compose)
implementation(libs.coil.network.okhttp)
implementation(libs.exifinterface)
// QR Code scanning (ML Kit + CameraX)
implementation(libs.mlkit.barcode)
@@ -121,12 +121,15 @@ class OnboardingFlowTest {
}
@Test
fun connectPage_showsNearbyFirst() {
fun connectPage_recommendsGeneralSetupQr() {
setOnboardingContent()
navigateToPage(4)
composeTestRule
.onNodeWithText("Enter address instead")
.onNodeWithText("Scan Hermes setup QR")
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Recommended")
.assertIsDisplayed()
}
@@ -135,7 +138,7 @@ class OnboardingFlowTest {
setOnboardingContent()
navigateToPage(4)
composeTestRule.onNodeWithText("Enter address instead").performClick()
composeTestRule.onNodeWithText("Server or VPS").performClick()
composeTestRule.waitForIdle()
composeTestRule
@@ -148,7 +151,7 @@ class OnboardingFlowTest {
setOnboardingContent()
navigateToPage(4)
composeTestRule.onNodeWithText("Enter address instead").performClick()
composeTestRule.onNodeWithText("Server or VPS").performClick()
composeTestRule.waitForIdle()
composeTestRule
@@ -156,12 +159,28 @@ class OnboardingFlowTest {
.assertIsDisplayed()
}
@Test
fun cloudSetup_requestsTheHostedDashboardAddress() {
setOnboardingContent()
navigateToPage(4)
composeTestRule.onNodeWithText("Nous-hosted Hermes").performClick()
composeTestRule.waitForIdle()
composeTestRule
.onNodeWithText("Connect to Nous-hosted Hermes")
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Use the complete HTTPS address shown for your hosted agent.")
.assertIsDisplayed()
}
@Test
fun connectPage_keepsPairingOptional() {
setOnboardingContent()
navigateToPage(4)
composeTestRule.onNodeWithText("Other connection methods").performClick()
composeTestRule.onNodeWithText("Advanced").performClick()
composeTestRule.waitForIdle()
composeTestRule
Binary file not shown.

Before

Width:  |  Height:  |  Size: 128 KiB

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 166 KiB

After

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 112 KiB

After

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 134 KiB

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 KiB

After

Width:  |  Height:  |  Size: 232 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 222 KiB

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

After

Width:  |  Height:  |  Size: 109 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 166 KiB

After

Width:  |  Height:  |  Size: 180 KiB

@@ -1 +1 @@
Optional Relay recovery now stays in the right surfaces with clear status labels and immediate foreground retry. Session history again loads its full window through upstream-compatible paging. Streamed text selection, Voice microphone handoff, and the expanding Voice drawer are also more reliable.
Long sessions now retain complete history beyond Hermes' latest-500 default, keeping edit, retry, sharing, and recovery anchors stable. Gateway submit rejections preserve the server's message without unintended SSE fallback, while event envelopes and edit-and-regenerate requests follow current upstream contracts.
+25 -4
View File
@@ -48,6 +48,25 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- User-mediated text handoff. The app opens a fresh Chat draft
and fills the composer; it never sends from an external intent. -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
<!-- The loopback native-PKCE result page uses this fixed, tokenless
link only to bring the installed flavor back to the foreground.
MainActivity intentionally does not interpret the URI as an auth
callback or navigation command. -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="${applicationId}"
android:host="return" />
</intent-filter>
<!-- Some Android OEM assistant pickers enumerate ACTION_ASSIST
activities in addition to VoiceInteractionService providers. -->
<intent-filter>
@@ -101,8 +120,8 @@
<!-- Protects user-started active turns automatically; the optional
"Persistent connection" setting extends the same foreground
protection to idle/background connectivity (and relay-paired
device features). In main so BOTH flavors ship it. specialUse
needs a Play Console foreground-service declaration at submission. -->
device features). In main so BOTH flavors ship it. Every Play
foreground-service type needs its matching App content declaration. -->
<service
android:name=".network.upstream.GatewayKeepAliveService"
android:exported="false"
@@ -113,7 +132,8 @@
</service>
<!-- Experimental, explicitly user-started on-device wake-word listener.
Audio remains local and the service is never boot/restart started. -->
Audio remains local and the service is never boot/restart started.
The Play build's microphone type needs an App content declaration. -->
<service
android:name=".wake.WakeWordForegroundService"
android:exported="false"
@@ -122,7 +142,8 @@
<!-- User-started protection for voice capture from the system overlay.
The service does not own AudioRecord; it keeps foreground-only
microphone app-ops available while Hermes is behind another app. -->
microphone app-ops available while Hermes is behind another app.
Include this use case in the Play microphone declaration. -->
<service
android:name=".voice.VoiceOverlayForegroundService"
android:exported="false"
+113
View File
@@ -1,5 +1,118 @@
{
"versions": [
{
"version": "1.8.1",
"title": "Complete, reliable transcripts",
"date": "2026-08-09",
"sections": [
{
"header": "Keep long sessions complete",
"bullets": [
"Android pages explicitly through complete API-server and profile-scoped Dashboard history instead of silently stopping at Hermes' latest-500 default.",
"Sharing, retry, edit, and recovery retain stable transcript anchors while bounded safety limits keep unusually large reads controlled."
]
},
{
"header": "Follow Gateway truth",
"bullets": [
"Authoritative submit rejections preserve the server's message without an unintended SSE fallback.",
"Gateway event envelopes and edit-and-regenerate truncation confirmation now follow current upstream contracts."
]
}
]
},
{
"version": "1.8.0",
"title": "Conversations with more context",
"date": "2026-08-09",
"sections": [
{
"header": "Keep the whole turn together",
"bullets": [
"Quote, edit, search, and attach or reorder files without losing the active connection, profile, or session.",
"Share text from another Android app into a fresh Chat draft for review before sending."
]
},
{
"header": "See the work without the clutter",
"bullets": [
"Live thinking settles into a compact Thought disclosure, while routine tool activity groups into concise runs.",
"Approvals, failures, generated media, file changes, risks, and delegated work remain clearly distinct."
]
},
{
"header": "Switch agents, not identities",
"bullets": [
"The Profile Shelf switches agents from Chat while restoring each profile's last session.",
"Agent Passport model and reasoning controls remain scoped to the active session instead of rewriting server defaults."
]
},
{
"header": "Make it yours",
"bullets": [
"Preview theme accents and shapes, Sphere skins, and pets in one Appearance workflow.",
"Message speech controls, pet touch targets, scrolling terrain, image rotation, and edge-to-edge settings layout are more reliable."
]
}
]
},
{
"version": "1.7.1",
"title": "Safer, steadier conversations",
"date": "2026-08-08",
"sections": [
{
"header": "Chat stays with you",
"bullets": [
"Growing streamed replies stay visible while you are at the bottom, and intentional scrollback remains undisturbed.",
"Completed replies render Markdown immediately while live tool details remain expandable."
]
},
{
"header": "Sessions keep their ownership",
"bullets": [
"Queued follow-ups retain their originating connection, profile, session, route, attachments, and voice context.",
"Session pins and archives persist across restarts, and duplicate model rows are reconciled before rendering."
]
},
{
"header": "Safer controls and setup",
"bullets": [
"Approval cards require an explicit labeled decision, and Agent Passport safety controls are easier to read and dismiss.",
"Hosted Hermes setup completes through the official Dashboard system-browser sign-in flow."
]
}
]
},
{
"version": "1.7.0",
"title": "Smarter controls, steadier sessions",
"date": "2026-08-06",
"sections": [
{
"header": "Model controls fit the model",
"bullets": [
"Reasoning effort choices follow the selected provider and model when an exact supported list is available.",
"Unmodified Hermes and setups without the optional Relay capability overlay keep a fail-soft standard choice list."
]
},
{
"header": "Active chats stay easy to follow",
"bullets": [
"The searchable session drawer shows which conversations are working or waiting for input.",
"Restored and completed chats remain bottom-pinned through late layout changes without overriding intentional scrollback.",
"Chat and Voice keep stable rows through recovery, and Focus Voice controls receive taps normally."
]
},
{
"header": "Support stays private and useful",
"bullets": [
"Review locally redacted support information before choosing to copy, share, or open GitHub; nothing uploads automatically.",
"Connection diagnostics identify the failed operation and offer targeted guidance without exposing hosts or credentials."
]
}
]
},
{
"version": "1.6.1",
"title": "Clearer recovery, steadier chat",
+5 -5
View File
@@ -1,6 +1,6 @@
v1.6.1 - Clearer recovery, steadier chat
v1.8.1 - Complete, reliable transcripts
* Keep optional Relay recovery scoped to Relay surfaces with clear status labels.
* Restore the 200-session drawer through upstream-compatible paging.
* Stabilize streamed text selection and Voice microphone handoff.
* Keep new-chat coaching clear of the expanding Voice drawer.
* Keep complete history in long sessions beyond Hermes' latest-500 default.
* Preserve stable edit, retry, sharing, and recovery anchors while paging history.
* Show authoritative Gateway rejection messages without an unintended fallback.
* Reconcile Gateway events and edit-and-regenerate requests with current upstream contracts.
@@ -25,6 +25,8 @@ import com.hermesandroid.relay.notifications.TurnCompleteNotifier
import com.hermesandroid.relay.notifications.InteractionRequestNotifier
import com.hermesandroid.relay.ui.RelayApp
import com.hermesandroid.relay.util.NavRouteRequest
import com.hermesandroid.relay.util.SharedTextRequest
import com.hermesandroid.relay.util.extractSharedText
import com.hermesandroid.relay.viewmodel.ConnectionViewModel
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.collect
@@ -127,6 +129,7 @@ class MainActivity : AppCompatActivity() {
// in RelayApp's NavRouteRequest collector — we just pump the request
// into the SharedFlow here.
consumeNavRouteIntent(intent)
consumeSharedTextIntent(intent)
val consumedAssistantActivation =
com.hermesandroid.relay.assistant.AssistantSessionProtocol.consumeActivation(
this,
@@ -153,6 +156,7 @@ class MainActivity : AppCompatActivity() {
// instead of onCreate. RelayApp's collector handles both cases.
setIntent(intent)
consumeNavRouteIntent(intent)
consumeSharedTextIntent(intent)
com.hermesandroid.relay.assistant.AssistantSessionProtocol.consumeActivation(this, intent)
// === END PHASE3-safety-rails-followup ===
}
@@ -163,6 +167,15 @@ class MainActivity : AppCompatActivity() {
NavRouteRequest.tryRequest(route)
}
private fun consumeSharedTextIntent(intent: Intent?) {
val sharedText = extractSharedText(
action = intent?.action,
mimeType = intent?.type,
text = intent?.getCharSequenceExtra(Intent.EXTRA_TEXT),
) ?: return
SharedTextRequest.tryRequest(sharedText)
}
private fun configureAssistantWindow(intent: Intent?) {
if (
intent?.getBooleanExtra(
@@ -3,10 +3,10 @@ package com.hermesandroid.relay.data
/**
* Shared profile/personality display and request identity helpers.
*
* A null profile name is the app's explicit "Server default" state. The
* relay also advertises the root Hermes config as a synthetic profile named
* "default"; for request/session identity that row is an alias of server
* default so it does not split chat, voice, or session scope.
* A null profile name is the app's explicit "Server default" state. It is
* intentionally distinct from a real profile whose name is literally
* `default`: the former follows the server's sticky default, while the latter
* explicitly addresses the root profile.
*/
object AgentDisplay {
const val SERVER_DEFAULT_PROFILE_KEY: String = "__server_default__"
@@ -16,17 +16,16 @@ object AgentDisplay {
"hermes agent",
)
// Only an EXPLICIT pick drives request/session identity. The advertised
// "default" profile is an alias for server default, so falling back to it
// here would split chat, voice, or session scope.
// Only an explicit pick drives request identity. Server default is the null
// selection; a named `default` profile is an ordinary explicit pick.
@Suppress("UNUSED_PARAMETER")
fun effectiveProfile(
selectedProfile: Profile?,
profiles: List<Profile>,
): Profile? = selectedProfile
// Display can use the synthetic default profile's metadata without making
// it a request/session override. Verbose SOUL summaries are filtered by
// Display can use the root default profile's metadata without making it a
// request/session override. Verbose SOUL summaries are filtered by
// profileDisplayName below, so this is safe for headers/cards.
fun effectiveDisplayProfile(
selectedProfile: Profile?,
@@ -39,7 +38,7 @@ object AgentDisplay {
?.let { activeName ->
profiles.firstOrNull { it.name.equals(activeName, ignoreCase = true) }
}
?: profiles.firstOrNull { isServerDefaultAlias(it.name) }
?: profiles.firstOrNull { it.name.equals("default", ignoreCase = true) }
}
// The NAME goes in the name slot. Non-default profiles use their profile
@@ -48,7 +47,7 @@ object AgentDisplay {
// verbose SOUL summary.
fun profileDisplayName(profile: Profile?): String? {
if (profile == null) return null
if (isServerDefaultAlias(profile.name)) {
if (profile.name.equals("default", ignoreCase = true)) {
return defaultProfileDisplayName(profile)
}
return when {
@@ -135,22 +134,18 @@ object AgentDisplay {
?.takeIf { it.isNotEmpty() }
?.takeUnless { it.lowercase() in GENERIC_MODEL_ALIASES }
fun isServerDefaultAlias(profileName: String?): Boolean =
profileName?.trim()?.equals("default", ignoreCase = true) == true
fun normalizeSelection(profile: Profile?): Profile? =
if (isServerDefaultAlias(profile?.name)) null else profile
fun normalizeSelection(profile: Profile?): Profile? = profile
fun profileRequestName(profileName: String?): String? =
profileName
?.trim()
?.takeIf { it.isNotEmpty() && !isServerDefaultAlias(it) }
?.takeIf { it.isNotEmpty() && it != SERVER_DEFAULT_PROFILE_KEY }
/**
* The profile name that owns chat sessions for the current UI selection.
*
* [selectedProfileName] is null (or the synthetic `default` alias) for the
* "Server default" row. That UI sentinel must remain distinct from the
* [selectedProfileName] is null for the "Server default" row. That UI
* sentinel must remain distinct from the
* server's sticky active profile: a dashboard launched under the root home
* may still report `active=victor`, in which case upstream Gateway and
* dashboard session calls must explicitly target `victor`. The resolved
@@ -0,0 +1,147 @@
package com.hermesandroid.relay.data
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
/**
* Immutable owner of one composer draft.
*
* Callers must supply stable ids rather than display labels. [sessionId] may be
* a server id or a stable client-generated id for a not-yet-created session.
* [draftId] separates the primary composer from any future named draft slot.
*/
data class ChatComposerDraftKey(
val connectionId: String,
val profileId: String,
val sessionId: String,
val draftId: String = PRIMARY_DRAFT_ID,
) {
init {
require(connectionId.isNotBlank()) { "connectionId must not be blank" }
require(profileId.isNotBlank()) { "profileId must not be blank" }
require(sessionId.isNotBlank()) { "sessionId must not be blank" }
require(draftId.isNotBlank()) { "draftId must not be blank" }
}
companion object {
const val PRIMARY_DRAFT_ID = "primary"
const val DEFAULT_PROFILE_ID = "default"
}
}
/** Message references associated with composer content. */
data class ChatComposerDraftContext(
val quotedMessageId: String? = null,
val editingMessageId: String? = null,
) {
internal fun normalized(): ChatComposerDraftContext = copy(
quotedMessageId = quotedMessageId?.takeIf(String::isNotBlank),
editingMessageId = editingMessageId?.takeIf(String::isNotBlank),
)
}
/**
* Complete restorable state for one composer.
*
* Selection offsets use the same start-inclusive/end-exclusive convention as
* Compose text fields. The store clamps them whenever the text changes so a
* restored selection can never address outside the restored string.
*/
data class ChatComposerDraft(
val text: String = "",
val selectionStart: Int = text.length,
val selectionEnd: Int = selectionStart,
val context: ChatComposerDraftContext = ChatComposerDraftContext(),
val attachments: List<Attachment> = emptyList(),
) {
val isEmpty: Boolean
get() = text.isEmpty() &&
context.quotedMessageId == null &&
context.editingMessageId == null &&
attachments.isEmpty()
internal fun normalized(): ChatComposerDraft {
val normalizedStart = selectionStart.coerceIn(0, text.length)
val normalizedEnd = selectionEnd.coerceIn(0, text.length)
return copy(
selectionStart = minOf(normalizedStart, normalizedEnd),
selectionEnd = maxOf(normalizedStart, normalizedEnd),
context = context.normalized(),
attachments = attachments.toList(),
)
}
}
/**
* Session-owned composer state.
*
* This store is deliberately memory-only: outbound [Attachment.content] can
* contain large Base64 payloads and must not enter Preferences DataStore. Keep
* one instance in the chat owner (normally its ViewModel) so drafts survive
* navigation and Activity recreation. Process death starts with empty drafts;
* a future durable implementation should persist URI grants, not attachment
* bytes.
*/
interface ChatComposerDraftStore {
fun observe(key: ChatComposerDraftKey): Flow<ChatComposerDraft>
fun snapshot(key: ChatComposerDraftKey): ChatComposerDraft
fun save(key: ChatComposerDraftKey, draft: ChatComposerDraft)
fun update(
key: ChatComposerDraftKey,
transform: (ChatComposerDraft) -> ChatComposerDraft,
)
fun remove(key: ChatComposerDraftKey)
fun removeSession(connectionId: String, profileId: String, sessionId: String)
fun clear()
}
class InMemoryChatComposerDraftStore : ChatComposerDraftStore {
private val drafts = MutableStateFlow<Map<ChatComposerDraftKey, ChatComposerDraft>>(emptyMap())
override fun observe(key: ChatComposerDraftKey): Flow<ChatComposerDraft> =
drafts
.map { it[key] ?: ChatComposerDraft() }
.distinctUntilChanged()
override fun snapshot(key: ChatComposerDraftKey): ChatComposerDraft =
drafts.value[key] ?: ChatComposerDraft()
@Synchronized
override fun save(key: ChatComposerDraftKey, draft: ChatComposerDraft) {
val normalized = draft.normalized()
drafts.value = if (normalized.isEmpty) {
drafts.value - key
} else {
drafts.value + (key to normalized)
}
}
@Synchronized
override fun update(
key: ChatComposerDraftKey,
transform: (ChatComposerDraft) -> ChatComposerDraft,
) {
save(key, transform(snapshot(key)))
}
@Synchronized
override fun remove(key: ChatComposerDraftKey) {
drafts.value = drafts.value - key
}
@Synchronized
override fun removeSession(connectionId: String, profileId: String, sessionId: String) {
drafts.value = drafts.value.filterKeys { key ->
key.connectionId != connectionId ||
key.profileId != profileId ||
key.sessionId != sessionId
}
}
@Synchronized
override fun clear() {
drafts.value = emptyMap()
}
}
@@ -0,0 +1,48 @@
package com.hermesandroid.relay.data
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
/** Phone-local behavior for a physical keyboard's unmodified Enter key. */
enum class PhysicalKeyboardEnterBehavior(val storedValue: String) {
SendMessage("send_message"),
InsertNewline("insert_newline"),
;
companion object {
fun fromStoredValue(value: String?): PhysicalKeyboardEnterBehavior =
entries.firstOrNull { it.storedValue == value } ?: SendMessage
}
}
/** Device-level chat input preferences shared by every Hermes profile. */
class ChatInputPreferencesRepository(
private val dataStore: DataStore<Preferences>,
) {
constructor(context: Context) : this(context.relayDataStore)
companion object {
internal val KEY_PHYSICAL_KEYBOARD_ENTER =
stringPreferencesKey("physical_keyboard_enter_behavior")
}
val physicalKeyboardEnterBehavior: Flow<PhysicalKeyboardEnterBehavior> = dataStore.data
.map { preferences ->
PhysicalKeyboardEnterBehavior.fromStoredValue(
preferences[KEY_PHYSICAL_KEYBOARD_ENTER],
)
}
.distinctUntilChanged()
suspend fun setPhysicalKeyboardEnterBehavior(behavior: PhysicalKeyboardEnterBehavior) {
dataStore.edit { preferences ->
preferences[KEY_PHYSICAL_KEYBOARD_ENTER] = behavior.storedValue
}
}
}
@@ -112,12 +112,11 @@ data class ChatMessage(
*/
val clientOnly: Boolean = false,
/**
* Delivery state for a message the user sends into an agent **Thread** over
* the relay proactive channel ([com.hermesandroid.relay.viewmodel.ChatViewModel]
* routes `source=phone` sessions here instead of the normal chat send).
* `SENDING` until the relay acks (`proactive.reply.ack`) → `DELIVERED`;
* `FAILED` on a send error. Null for ordinary chat messages — those render
* no status affix.
* Delivery state for a user-authored message. Agent **Thread** replies use
* `SENDING` until the relay acks (`proactive.reply.ack`) → `DELIVERED`,
* with `FAILED` on a send error. Ordinary chat may additionally use
* `QUEUED` and `STEERED` to make active-turn routing visible. Null keeps the
* legacy behavior of rendering no status affix.
*/
val deliveryStatus: MessageDeliveryStatus? = null,
/**
@@ -373,6 +372,12 @@ data class ToolCall(
val outputRiskFindings: List<String> = emptyList(),
/** Upstream removed sensitive spans before emitting the findings. */
val outputRiskRedacted: Boolean = false,
/**
* Stable UI identity retained when a generating placeholder adopts its
* gateway tool ID. This keeps per-card interaction state attached to the
* logical call across streaming reconciliation and list updates.
*/
val uiKey: String = id ?: "$name:$startedAt",
)
enum class MessageRole {
@@ -382,15 +387,17 @@ enum class MessageRole {
}
/**
* Delivery state of a user reply sent into an agent Thread over the relay
* proactive channel. Only set on Thread replies; ordinary chat messages leave
* it null and show no status affix.
* Delivery state of a user-authored message. Thread replies use the relay ack
* lifecycle; ordinary chat can additionally expose queue and steer outcomes.
* Null preserves the legacy behavior of rendering no status affix.
*
* - [SENDING] handed to the relay; awaiting the per-reply ack.
* - [QUEUED] held client-side until the active turn completes.
* - [STEERED] accepted as a correction to the active turn.
* - [DELIVERED] the relay acked (`proactive.reply.ack`) — buffered for the agent.
* - [FAILED] the send errored (e.g. relay disconnected).
*/
enum class MessageDeliveryStatus { SENDING, DELIVERED, FAILED }
enum class MessageDeliveryStatus { SENDING, QUEUED, STEERED, DELIVERED, FAILED }
data class ChatSession(
val sessionId: String,
@@ -409,6 +416,9 @@ data class ChatSession(
val source: String? = null,
/** Server reports a persisted session runtime/model binding. */
val hasModelConfig: Boolean = false,
/** Durable upstream session metadata, scoped by the owning connection/profile DB. */
val pinned: Boolean = false,
val archived: Boolean = false,
) {
val activityTimestamp: Long
get() = firstPositive(lastActivityAt, updatedAt, startedAt)
@@ -0,0 +1,66 @@
package com.hermesandroid.relay.data
import java.nio.charset.StandardCharsets
import java.util.Base64
/** Structured identity and preview for a quoted chat message. */
data class ChatQuoteReference(
val messageId: String,
val authorLabel: String,
val excerpt: String,
)
/** Parsed transport envelope: Android renders [reference] separately from [body]. */
data class ChatQuoteEnvelope(
val reference: ChatQuoteReference,
val body: String,
)
/**
* Serialize a structured quote as ordinary Markdown for unmodified Hermes clients.
* Android parses the same envelope back into a quote chip, while Desktop/TUI see
* a readable linked attribution instead of an Android-only marker.
*/
fun buildChatQuotedPrompt(body: String, reference: ChatQuoteReference?): String {
if (reference == null) return body
val encodedId = Base64.getUrlEncoder().withoutPadding().encodeToString(
reference.messageId.toByteArray(StandardCharsets.UTF_8),
)
val author = reference.authorLabel.normalizedQuoteText(MAX_AUTHOR_CHARS)
val excerpt = reference.excerpt.normalizedQuoteText(MAX_EXCERPT_CHARS)
if (encodedId.isBlank() || author.isBlank() || excerpt.isBlank()) return body
return "> **Replying to [@$author](hermes-message://$encodedId):** $excerpt\n\n$body"
}
/** Parse only the exact bounded envelope emitted by [buildChatQuotedPrompt]. */
fun parseChatQuotedPrompt(content: String): ChatQuoteEnvelope? {
val match = QUOTE_ENVELOPE.matchEntire(content) ?: return null
val author = match.groupValues[1]
val encodedId = match.groupValues[2]
val excerpt = match.groupValues[3]
val body = match.groupValues[4]
val messageId = runCatching {
String(Base64.getUrlDecoder().decode(encodedId), StandardCharsets.UTF_8)
}.getOrNull()?.takeIf { it.isNotBlank() && it.length <= MAX_MESSAGE_ID_CHARS } ?: return null
return ChatQuoteEnvelope(
reference = ChatQuoteReference(messageId, author, excerpt),
body = body,
)
}
private fun String.normalizedQuoteText(maxChars: Int): String =
replace(Regex("[\\p{Cc}\\s]+"), " ")
.replace("\\", "")
.replace("]", "")
.trim()
.take(maxChars)
private val QUOTE_ENVELOPE = Regex(
pattern = "^> \\*\\*Replying to \\[@([^]\\r\\n]{1,$MAX_AUTHOR_CHARS})]" +
"\\(hermes-message://([A-Za-z0-9_-]{1,512})\\):\\*\\* " +
"([^\\r\\n]{1,$MAX_EXCERPT_CHARS})\\n\\n([\\s\\S]*)$",
)
private const val MAX_AUTHOR_CHARS = 40
private const val MAX_EXCERPT_CHARS = 240
private const val MAX_MESSAGE_ID_CHARS = 512
@@ -32,6 +32,7 @@ data class ChatTurnCheckpoint(
val priorUserMessageCount: Int,
val baselineAssistantCount: Int,
val pendingAsk: ChatTurnAskCheckpoint? = null,
val queuedMessages: List<ChatQueuedMessageCheckpoint> = emptyList(),
val startedAt: Long,
val updatedAt: Long,
) {
@@ -41,6 +42,17 @@ data class ChatTurnCheckpoint(
}
}
@Serializable
data class ChatQueuedMessageCheckpoint(
val id: String,
val text: String,
val transport: String,
val ownerRunId: String,
val interfaceContextPrompt: String? = null,
/** Attachment bytes are intentionally not copied into Preferences DataStore. */
val hadAttachments: Boolean = false,
)
@Serializable
data class ChatTurnUserCheckpoint(
val id: String,
@@ -134,7 +134,7 @@ data class Connection(
* than to crash).
*/
fun extractDefaultLabel(apiServerUrl: String): String =
extractHost(apiServerUrl) ?: apiServerUrl
extractHost(apiServerUrl)?.let(::defaultLabelFromHost) ?: apiServerUrl
/** Preserve explicit labels while upgrading an auto-generated IP label to a discovered host name. */
fun chooseDiscoveredLabel(
@@ -160,7 +160,25 @@ data class Connection(
val primary = dashboardUrl?.trim()?.takeIf { it.isNotBlank() }
?: apiServerUrl.trim().takeIf { it.isNotBlank() }
?: relayUrl.trim()
return extractHost(primary) ?: primary
return extractHost(primary)?.let(::defaultLabelFromHost) ?: primary
}
/**
* Nous-hosted agent gateways use the stable
* `<slug>.agents.nousresearch.com` origin contract. The public Hermes
* status response deliberately carries no tenant/agent display name,
* so a URL-only connection uses that exact single-label slug as its
* least-surprising default. Portal's human-readable agent name is only
* available through its separately authenticated discovery API.
*
* Match the complete suffix and exactly one leading DNS label. This
* avoids shortening lookalike or operator-controlled hostnames.
*/
private fun defaultLabelFromHost(host: String): String {
val suffix = ".agents.nousresearch.com"
if (!host.endsWith(suffix, ignoreCase = true)) return host
val slug = host.dropLast(suffix.length)
return slug.takeIf { it.isNotBlank() && '.' !in it } ?: host
}
private fun extractHost(url: String): String? = try {
@@ -26,7 +26,6 @@ object ProfilePresentationPolicy {
fun availableKeys(profiles: List<Profile>): List<String> = buildList {
add(AgentDisplay.SERVER_DEFAULT_PROFILE_KEY)
profiles.asSequence()
.filterNot { AgentDisplay.isServerDefaultAlias(it.name) }
.map(Profile::name)
.distinct()
.forEach(::add)
@@ -49,6 +48,12 @@ object ProfilePresentationPolicy {
): List<String> = orderedKeys(profiles, presentation).filter { key ->
key == selectedKey || key !in presentation.hidden
}
fun shouldShowShelf(
profiles: List<Profile>,
presentation: ProfilePresentation,
selectedKey: String,
): Boolean = visibleKeys(profiles, presentation, selectedKey).size > 1
}
class ProfilePresentationStore(
@@ -0,0 +1,7 @@
package com.hermesandroid.relay.data
/** Live activity surfaced beside a session without conflating it with selection. */
enum class SessionActivityState {
Working,
NeedsInput,
}
@@ -3,6 +3,8 @@ package com.hermesandroid.relay.diagnostics
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import com.hermesandroid.relay.reliability.ReliabilityCenter
import com.hermesandroid.relay.reliability.ReliabilityRedactor
enum class DiagnosticCategory(val label: String) {
Api("API"),
@@ -25,7 +27,16 @@ data class DiagnosticLogEntry(
val severity: DiagnosticSeverity,
val title: String,
val detail: String? = null,
/** Human-readable action that produced this event, not merely its subsystem. */
val operation: String? = null,
val endpointRole: String? = null,
/** User/configuration-facing route before protocol/path normalization. */
val configuredUrl: String? = null,
/** Exact sanitized URL attempted on the wire, including the diagnostic path. */
val requestUrl: String? = null,
/** Concrete next troubleshooting step for failures with a known interpretation. */
val suggestion: String? = null,
/** Legacy single-URL field retained for diagnostics that have not needed split context. */
val url: String? = null,
val elapsedMs: Long? = null,
/**
@@ -34,7 +45,11 @@ data class DiagnosticLogEntry(
* the detail view shows this. Null for non-error / manually-recorded entries.
*/
val stacktrace: String? = null,
)
) {
/** Best route for mode inference and compact list rendering. */
val primaryUrl: String?
get() = configuredUrl ?: requestUrl ?: url
}
/**
* Current health of a single subsystem on the Diagnostics status timeline.
@@ -81,19 +96,29 @@ object DiagnosticsLog {
severity: DiagnosticSeverity = DiagnosticSeverity.Info,
title: String,
detail: String? = null,
operation: String? = null,
endpointRole: String? = null,
configuredUrl: String? = null,
requestUrl: String? = null,
suggestion: String? = null,
url: String? = null,
elapsedMs: Long? = null,
stacktrace: String? = null,
) {
val safeConfiguredUrl = sanitizeUrl(configuredUrl)
val safeRequestUrl = sanitizeUrl(requestUrl)
val entry = DiagnosticLogEntry(
timestampMs = System.currentTimeMillis(),
category = category,
severity = severity,
title = clean(title) ?: title.take(MAX_TEXT_LENGTH),
detail = clean(detail),
operation = clean(operation),
endpointRole = clean(endpointRole),
url = sanitizeUrl(url),
configuredUrl = safeConfiguredUrl,
requestUrl = safeRequestUrl,
suggestion = clean(suggestion),
url = if (safeConfiguredUrl == null && safeRequestUrl == null) sanitizeUrl(url) else null,
elapsedMs = elapsedMs,
stacktrace = redactTrace(stacktrace),
)
@@ -121,20 +146,40 @@ object DiagnosticsLog {
title: String,
detail: String? = null,
throwable: Throwable? = null,
operation: String? = null,
endpointRole: String? = null,
configuredUrl: String? = null,
requestUrl: String? = null,
suggestion: String? = null,
url: String? = null,
elapsedMs: Long? = null,
reliabilityContext: String? = null,
) {
record(
category = category,
severity = DiagnosticSeverity.Error,
title = title,
detail = detail ?: throwable?.message,
operation = operation,
endpointRole = endpointRole,
configuredUrl = configuredUrl,
requestUrl = requestUrl,
suggestion = suggestion,
url = url,
elapsedMs = elapsedMs,
stacktrace = throwable?.let { stackTraceText(it) },
)
if (throwable != null) {
runCatching {
ReliabilityCenter.recordHandled(
title = title,
detail = detail ?: throwable.message,
throwable = throwable,
context = reliabilityContext,
routeRole = endpointRole,
)
}
}
}
private fun stackTraceText(t: Throwable): String =
@@ -167,10 +212,8 @@ object DiagnosticsLog {
val prefix = noQuery.substring(0, schemeEnd + 3)
val rest = noQuery.substring(schemeEnd + 3)
val slash = rest.indexOf('/').let { if (it < 0) rest.length else it }
val authority = rest.substring(0, slash)
val path = rest.substring(slash)
val safeAuthority = authority.substringAfterLast('@')
prefix + safeAuthority + path
prefix + "[host]" + path
} else {
noQuery
}
@@ -197,7 +240,7 @@ object DiagnosticsLog {
*/
private fun redactTrace(value: String?): String? {
val trimmed = value?.trim()?.takeIf { it.isNotBlank() } ?: return null
val redacted = redact(trimmed)
val redacted = ReliabilityRedactor.redact(trimmed, MAX_TRACE_LENGTH)
return if (redacted.length > MAX_TRACE_LENGTH) {
redacted.take(MAX_TRACE_LENGTH) + "\n… (truncated)"
} else {
@@ -205,8 +248,5 @@ object DiagnosticsLog {
}
}
private fun redact(value: String): String =
value.replace(Regex("""(?i)(bearer|token|api[_-]?key|session[_-]?token)\s*[:=]\s*\S+""")) {
"${it.groupValues[1]}=[hidden]"
}
private fun redact(value: String): String = ReliabilityRedactor.redact(value, MAX_TRACE_LENGTH)
}
@@ -0,0 +1,42 @@
package com.hermesandroid.relay.diagnostics
import java.net.ConnectException
import java.net.NoRouteToHostException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.net.ssl.SSLException
/**
* Maps network failure classes to narrow, truthful next steps.
*
* These messages are diagnostic guidance, not recovery behavior: callers still
* own retries, routing, and authentication. Walking the cause chain preserves
* useful classification when OkHttp or a coroutine boundary wraps the socket
* exception in a higher-level failure.
*/
object NetworkDiagnosticGuidance {
fun forThrowable(throwable: Throwable, target: String): String? {
val causes = generateSequence(throwable as Throwable?) { it.cause }.take(12).toList()
return when {
causes.any { it is ConnectException } ->
"Verify $target is running and listening on the configured host and port."
causes.any { it is UnknownHostException } ->
"Verify the configured hostname resolves from this device."
causes.any { it is NoRouteToHostException } ->
"Verify the device has a network path to the configured host."
causes.any { it is SocketTimeoutException } ->
"Check network routing or firewall rules between this device and $target."
causes.any { it is SSLException } ->
"Verify the TLS scheme, certificate, and trust configuration for $target."
else -> null
}
}
fun forHttpStatus(statusCode: Int, target: String): String? = when (statusCode) {
401, 403 -> "Verify the configured $target credentials or pair the device again."
404 -> "Verify this URL points to the expected $target route and version."
429 -> "Wait for the server's backoff period before retrying."
in 500..599 -> "Check the $target server logs for the failing request."
else -> null
}
}
@@ -15,6 +15,7 @@ import com.hermesandroid.relay.data.PairingPreferences
import com.hermesandroid.relay.diagnostics.DiagnosticCategory
import com.hermesandroid.relay.diagnostics.DiagnosticSeverity
import com.hermesandroid.relay.diagnostics.DiagnosticsLog
import com.hermesandroid.relay.diagnostics.NetworkDiagnosticGuidance
import com.hermesandroid.relay.network.relay.models.Envelope
import com.hermesandroid.relay.network.shared.EndpointResolver
import com.hermesandroid.relay.network.shared.EndpointSurface
@@ -441,7 +442,9 @@ class ConnectionManager(
severity = DiagnosticSeverity.Error,
title = context?.getString(R.string.conn_diag_socket_blocked) ?: "Relay socket blocked",
detail = "ws:// is disabled",
url = url,
operation = "Open Relay WebSocket",
configuredUrl = url,
suggestion = "Use wss:// or explicitly allow plain ws:// for a trusted LAN or VPN.",
)
return
}
@@ -452,7 +455,9 @@ class ConnectionManager(
severity = DiagnosticSeverity.Error,
title = context?.getString(R.string.conn_diag_url_invalid) ?: "Relay socket URL invalid",
detail = "URL must start with ws:// or wss://",
url = url,
operation = "Open Relay WebSocket",
configuredUrl = url,
suggestion = "Edit or re-pair the Relay route with a ws:// or wss:// URL.",
)
return
}
@@ -488,14 +493,18 @@ class ConnectionManager(
category = DiagnosticCategory.Relay,
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.conn_diag_opening_insecure) ?: "Opening insecure relay socket",
url = normalized,
operation = "Open Relay WebSocket",
configuredUrl = url,
requestUrl = normalized,
)
} else {
DiagnosticsLog.record(
category = DiagnosticCategory.Relay,
severity = DiagnosticSeverity.Info,
title = context?.getString(R.string.conn_diag_opening_socket) ?: "Opening relay socket",
url = normalized,
operation = "Open Relay WebSocket",
configuredUrl = url,
requestUrl = normalized,
)
}
@@ -985,7 +994,9 @@ class ConnectionManager(
severity = DiagnosticSeverity.Error,
title = "Invalid relay URL",
detail = "The relay address could not be parsed; re-pair to refresh it.",
url = url,
operation = "Build Relay WebSocket request",
configuredUrl = url,
suggestion = "Edit or re-pair the Relay route to replace the invalid address.",
)
authenticated = false
_connectionState.value = ConnectionState.Disconnected
@@ -1018,7 +1029,8 @@ class ConnectionManager(
category = DiagnosticCategory.Relay,
severity = DiagnosticSeverity.Info,
title = context?.getString(R.string.conn_diag_connected) ?: "Relay socket connected",
url = url,
operation = "Relay WebSocket handshake",
requestUrl = url,
)
// TOFU: record the peer cert fingerprint if we don't have one
@@ -1079,7 +1091,9 @@ class ConnectionManager(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.conn_diag_closed) ?: "Relay socket closed",
detail = "code=$code reason=$reason",
url = url,
operation = "Relay WebSocket session",
requestUrl = url,
suggestion = if (code == 1000) null else "Check the Relay server logs for the matching close code and reason.",
)
authenticated = false
_connectionState.value = ConnectionState.Disconnected
@@ -1102,7 +1116,11 @@ class ConnectionManager(
t.message,
code?.let { "HTTP $it" },
).joinToString(": "),
url = url,
operation = "Relay WebSocket handshake",
requestUrl = url,
suggestion = code?.let {
NetworkDiagnosticGuidance.forHttpStatus(it, "Relay")
} ?: NetworkDiagnosticGuidance.forThrowable(t, "Relay"),
)
lastUpgradeResponseCode = code
if (response == null) {
@@ -7,10 +7,12 @@ import com.hermesandroid.relay.auth.PairedDeviceInfo
import com.hermesandroid.relay.diagnostics.DiagnosticCategory
import com.hermesandroid.relay.diagnostics.DiagnosticSeverity
import com.hermesandroid.relay.diagnostics.DiagnosticsLog
import com.hermesandroid.relay.diagnostics.NetworkDiagnosticGuidance
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.contentOrNull
@@ -59,6 +61,10 @@ class RelayHttpClient(
companion object {
private const val TAG = "RelayHttpClient"
const val MAX_MODEL_CAPABILITY_ROWS = 64
private const val MAX_MODEL_CAPABILITY_PROVIDER_CHARS = 128
private const val MAX_MODEL_CAPABILITY_MODEL_CHARS = 512
private const val MAX_MODEL_CAPABILITY_PROFILE_CHARS = 128
private val sessionsJson = Json {
ignoreUnknownKeys = true
isLenient = true
@@ -710,6 +716,37 @@ class RelayHttpClient(
@SerialName("age_seconds") val ageSeconds: Int? = null,
)
@Serializable
data class ModelCapabilityRequestRow(
val provider: String,
val model: String,
)
@Serializable
data class ModelCapabilitiesRequest(
@SerialName("schema_version") val schemaVersion: Int = 1,
val profile: String? = null,
val refresh: Boolean = false,
val models: List<ModelCapabilityRequestRow>,
)
@Serializable
data class ModelCapabilityRow(
val provider: String,
val model: String,
val reasoning: Boolean? = null,
@SerialName("reasoning_efforts") val reasoningEfforts: List<String> = emptyList(),
@SerialName("reasoning_efforts_exact") val reasoningEffortsExact: Boolean = false,
val source: String = "",
)
@Serializable
data class ModelCapabilitiesResponse(
@SerialName("schema_version") val schemaVersion: Int = 1,
@SerialName("contract_version") val contractVersion: String = "",
val capabilities: List<ModelCapabilityRow> = emptyList(),
)
/** Fetch the installed plugin/protocol/profile capability contract. */
suspend fun fetchRelayInfo(): Result<RelayInfo?> = withContext(Dispatchers.IO) {
val relayUrl = relayUrlProvider()?.trim().orEmpty()
@@ -743,6 +780,64 @@ class RelayHttpClient(
}
}
/** Optional provider/model reasoning overlay; 404 and no pairing are fail-soft. */
suspend fun fetchModelCapabilities(
models: List<ModelCapabilityRequestRow>,
profile: String? = null,
refresh: Boolean = false,
): Result<ModelCapabilitiesResponse?> = withContext(Dispatchers.IO) {
val boundedModels = models
.asSequence()
.map {
ModelCapabilityRequestRow(
it.provider.trim().take(MAX_MODEL_CAPABILITY_PROVIDER_CHARS),
it.model.trim().take(MAX_MODEL_CAPABILITY_MODEL_CHARS),
)
}
.filter { it.provider.isNotEmpty() && it.model.isNotEmpty() }
.distinct()
.take(MAX_MODEL_CAPABILITY_ROWS)
.toList()
if (boundedModels.isEmpty()) return@withContext Result.success(null)
val relayUrl = relayUrlProvider()?.trim().orEmpty()
val token = sessionTokenProvider()
if (relayUrl.isEmpty() || token.isNullOrBlank()) return@withContext Result.success(null)
val base = relayUrl
.replace(Regex("^wss://", RegexOption.IGNORE_CASE), "https://")
.replace(Regex("^ws://", RegexOption.IGNORE_CASE), "http://")
.trimEnd('/')
val url = runCatching { "$base/relay/model-capabilities".toHttpUrl() }.getOrElse {
return@withContext Result.success(null)
}
val payload = ModelCapabilitiesRequest(
profile = profile?.trim()?.take(MAX_MODEL_CAPABILITY_PROFILE_CHARS)
?.takeIf { it.isNotEmpty() },
refresh = refresh,
models = boundedModels,
)
val request = Request.Builder()
.url(url)
.post(sessionsJson.encodeToString(payload).toRequestBody("application/json".toMediaType()))
.header("Authorization", "Bearer $token")
.header("Accept", "application/json")
.build()
try {
okHttpClient.newBuilder().callTimeout(4, java.util.concurrent.TimeUnit.SECONDS).build()
.newCall(request).execute().use { response ->
if (response.code == 404) return@withContext Result.success(null)
if (!response.isSuccessful) return@withContext Result.failure(IOException("HTTP ${response.code}"))
val body = response.body?.string().orEmpty()
val parsed = body.takeIf { it.isNotBlank() }?.let {
sessionsJson.decodeFromString(ModelCapabilitiesResponse.serializer(), it)
}
if (parsed?.schemaVersion != 1) Result.success(null) else Result.success(parsed)
}
} catch (e: Exception) {
Log.w(TAG, "fetchModelCapabilities failed: ${e.message}")
Result.failure(e)
}
}
/**
* Ask the relay whether a newer plugin release is available — it compares its
* installed version against the latest `plugin-v*` GitHub release (cached an
@@ -1139,6 +1234,7 @@ class RelayHttpClient(
logSuccess: Boolean = true,
): Result<RelayHealth> = withContext(Dispatchers.IO) {
val trimmed = relayUrl.trim()
val operation = "Relay health probe before WebSocket connection"
if (trimmed.isEmpty()) {
return@withContext Result.failure(
IllegalArgumentException("Relay URL is empty")
@@ -1159,7 +1255,9 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Error,
title = context?.getString(R.string.http_diag_url_invalid) ?: "Relay URL invalid",
detail = e.message,
url = relayUrl,
operation = operation,
configuredUrl = relayUrl,
suggestion = "Enter a Relay URL beginning with ws:// or wss://.",
)
return@withContext Result.failure(
IOException("Invalid relay URL: ${e.message}")
@@ -1180,6 +1278,7 @@ class RelayHttpClient(
.get()
.header("Accept", "application/json")
.build()
val requestUrl = request.url.toString()
try {
fastClient.newCall(request).execute().use { response ->
@@ -1189,8 +1288,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.http_diag_health_failed) ?: "Relay health failed",
detail = "HTTP ${response.code}",
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = NetworkDiagnosticGuidance.forHttpStatus(response.code, "Relay"),
)
return@withContext Result.failure(
IOException("Relay responded HTTP ${response.code}")
@@ -1203,8 +1305,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.http_diag_health_failed) ?: "Relay health failed",
detail = "Empty response",
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = "Verify this route points to a Hermes-Relay server and inspect its logs.",
)
return@withContext Result.failure(
IOException("Relay returned an empty response")
@@ -1220,8 +1325,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.http_diag_health_failed) ?: "Relay health failed",
detail = "Non-JSON response",
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = "Verify this route points to a Hermes-Relay server rather than another HTTP service.",
)
return@withContext Result.failure(
IOException("Relay returned non-JSON: ${e.message ?: "parse error"}")
@@ -1234,8 +1342,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.http_diag_health_failed) ?: "Relay health failed",
detail = "status=${status ?: "missing"}",
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = "Check the Relay service health and server logs.",
)
return@withContext Result.failure(
IOException("Relay reports status=${status ?: "missing"} (expected 'ok')")
@@ -1248,8 +1359,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.http_diag_health_failed) ?: "Relay health failed",
detail = "Missing version field",
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = "Verify this route points to a current Hermes-Relay server.",
)
return@withContext Result.failure(
IOException("Response doesn't look like a hermes-relay — missing 'version' field")
@@ -1265,7 +1379,9 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Info,
title = context?.getString(R.string.http_diag_health_ok) ?: "Relay health ok",
detail = "version=$version clients=$clients sessions=$sessions",
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
)
}
@@ -1278,8 +1394,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.http_diag_health_timeout) ?: "Relay health timeout",
detail = "No HTTP response in 3s",
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = NetworkDiagnosticGuidance.forThrowable(e, "Relay"),
)
Result.failure(IOException("Relay is not responding (3s timeout)"))
} catch (e: java.net.ConnectException) {
@@ -1289,8 +1408,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Error,
title = context?.getString(R.string.http_diag_conn_refused) ?: "Relay connection refused",
detail = e.message,
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = NetworkDiagnosticGuidance.forThrowable(e, "Relay"),
)
Result.failure(IOException("Connection refused — is the relay running on this URL?"))
} catch (e: IOException) {
@@ -1300,8 +1422,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.http_diag_health_failed) ?: "Relay health failed",
detail = e.message ?: "Network error",
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = NetworkDiagnosticGuidance.forThrowable(e, "Relay"),
)
Result.failure(IOException("Network error: ${e.message ?: "unreachable"}"))
} catch (e: Exception) {
@@ -1311,8 +1436,11 @@ class RelayHttpClient(
severity = DiagnosticSeverity.Error,
title = context?.getString(R.string.http_diag_health_failed) ?: "Relay health failed",
detail = e.message ?: e.javaClass.simpleName,
url = httpBase,
operation = operation,
configuredUrl = trimmed,
requestUrl = requestUrl,
elapsedMs = System.currentTimeMillis() - startedAtMs,
suggestion = NetworkDiagnosticGuidance.forThrowable(e, "Relay"),
)
Result.failure(e)
}
@@ -9,6 +9,7 @@ import com.hermesandroid.relay.data.routeAuthority
import com.hermesandroid.relay.diagnostics.DiagnosticCategory
import com.hermesandroid.relay.diagnostics.DiagnosticSeverity
import com.hermesandroid.relay.diagnostics.DiagnosticsLog
import com.hermesandroid.relay.diagnostics.NetworkDiagnosticGuidance
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.async
@@ -353,6 +354,10 @@ class EndpointResolver(
surface: EndpointSurface,
): Boolean {
val startedAtMs = clock()
val operation = when (surface) {
EndpointSurface.Standard -> "Dashboard or API route health probe"
EndpointSurface.Relay -> "Relay route health probe"
}
val target = probeTarget(candidate, surface)
val url = target?.requestUrl?.toHttpUrlOrNull()
?: run {
@@ -362,8 +367,10 @@ class EndpointResolver(
severity = DiagnosticSeverity.Error,
title = context?.getString(R.string.endpoint_diag_probe_invalid) ?: "Endpoint probe invalid",
detail = "No valid Dashboard, API, or Relay URL",
operation = operation,
endpointRole = candidate.role,
url = candidate.primaryRouteUrl(),
configuredUrl = candidate.primaryRouteUrl(),
suggestion = "Edit or re-pair this route so it contains a valid service URL.",
)
recordOutcome(candidate, surface, reachable = false, detail = "Invalid route URL")
return false
@@ -397,9 +404,16 @@ class EndpointResolver(
severity = if (ok) DiagnosticSeverity.Info else DiagnosticSeverity.Warning,
title = probeTitle,
detail = if (ok) null else "HTTP ${resp.code}",
operation = operation,
endpointRole = candidate.role,
url = target.baseUrl,
configuredUrl = target.baseUrl,
requestUrl = target.requestUrl,
elapsedMs = clock() - startedAtMs,
suggestion = if (ok) {
null
} else {
NetworkDiagnosticGuidance.forHttpStatus(resp.code, surface.diagnosticTarget())
},
)
recordOutcome(
candidate,
@@ -415,9 +429,12 @@ class EndpointResolver(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.endpoint_diag_probe_timeout) ?: "Endpoint probe timeout",
detail = "No ${target.path} response in ${PROBE_TIMEOUT_MS}ms",
operation = operation,
endpointRole = candidate.role,
url = target.baseUrl,
configuredUrl = target.baseUrl,
requestUrl = target.requestUrl,
elapsedMs = clock() - startedAtMs,
suggestion = "Check network routing or firewall rules between this device and ${surface.diagnosticTarget()}.",
)
recordOutcome(candidate, surface, reachable = false, detail = PROBE_TIMEOUT_DETAIL)
false
@@ -428,9 +445,12 @@ class EndpointResolver(
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.endpoint_diag_probe_timeout) ?: "Endpoint probe timeout",
detail = "No ${target.path} response in ${PROBE_TIMEOUT_MS}ms",
operation = operation,
endpointRole = candidate.role,
url = target.baseUrl,
configuredUrl = target.baseUrl,
requestUrl = target.requestUrl,
elapsedMs = clock() - startedAtMs,
suggestion = "Check network routing or firewall rules between this device and ${surface.diagnosticTarget()}.",
)
recordOutcome(candidate, surface, reachable = false, detail = PROBE_TIMEOUT_DETAIL)
false
@@ -441,10 +461,13 @@ class EndpointResolver(
category = DiagnosticCategory.Endpoint,
severity = DiagnosticSeverity.Warning,
title = context?.getString(R.string.endpoint_diag_probe_failed) ?: "Endpoint probe failed",
detail = e.javaClass.simpleName,
detail = humanProbeFailure(e),
operation = operation,
endpointRole = candidate.role,
url = target.baseUrl,
configuredUrl = target.baseUrl,
requestUrl = target.requestUrl,
elapsedMs = clock() - startedAtMs,
suggestion = NetworkDiagnosticGuidance.forThrowable(e, surface.diagnosticTarget()),
)
recordOutcome(candidate, surface, reachable = false, detail = humanProbeFailure(e))
false
@@ -521,6 +544,11 @@ class EndpointResolver(
else -> e.javaClass.simpleName
}
private fun EndpointSurface.diagnosticTarget(): String = when (this) {
EndpointSurface.Standard -> "Dashboard or API server"
EndpointSurface.Relay -> "Relay"
}
/**
* Mark [candidate] unreachable without re-probing. Called from
* `ConnectionManager`'s `NetworkCallback.onLost` so the next resolve()
@@ -61,7 +61,7 @@ object HermesLanDiscovery {
.callTimeout(PROBE_TIMEOUT_MS * 2, TimeUnit.MILLISECONDS)
.build()
coroutineScope {
val results = coroutineScope {
val semaphore = Semaphore(MAX_CONCURRENT_PROBES)
hosts.map { host ->
async {
@@ -79,6 +79,8 @@ object HermesLanDiscovery {
.thenBy { it.host },
)
}
Log.d(TAG, "scan complete hosts=${hosts.size} matches=${results.size}")
results
}
private fun probeHost(
@@ -133,8 +135,7 @@ object HermesLanDiscovery {
val body = response.body.string().take(2_048)
expectedBody(body, contentType)
}
} catch (e: Exception) {
Log.d(TAG, "probe failed url=$url type=${e.javaClass.simpleName}")
} catch (_: Exception) {
false
}
}
@@ -218,8 +218,14 @@ class ChatHandler {
*/
private val activeAnnotationTools = mutableMapOf<String, String>()
private val _messages = MutableStateFlow<List<ChatMessage>>(emptyList())
val messages: StateFlow<List<ChatMessage>> = _messages.asStateFlow()
// All Chat and Voice transcript publications pass through this invariant
// boundary. A caller may address a row by its mutable server/domain id or
// its retained client uiKey, but Compose must never observe both aliases.
private val _messages = RenderedMessageState(emptyList())
val messages: StateFlow<List<ChatMessage>> = _messages.flow
private fun ChatMessage.matchesIdentity(reference: String): Boolean =
id == reference || uiKey == reference
/**
* Latest gateway `status.update` lifecycle line for the in-flight turn
@@ -324,8 +330,8 @@ class ChatHandler {
// message.started can replace that domain id with the server id while
// uiKey deliberately retains the client id. Resolve the current domain
// id before every event so the tail keeps mutating the same visible row.
val currentMessageId = _messages.value.findLast { message ->
message.id == messageId || message.uiKey == messageId
val currentMessageId = _messages.value.findLast {
it.matchesIdentity(messageId)
}?.id ?: messageId
when (envelope.event) {
@@ -392,7 +398,7 @@ class ChatHandler {
*/
fun updateDeliveryStatus(messageId: String, status: MessageDeliveryStatus) {
_messages.update { list ->
list.map { if (it.id == messageId) it.copy(deliveryStatus = status) else it }
list.map { if (it.matchesIdentity(messageId)) it.copy(deliveryStatus = status) else it }
}
}
@@ -400,7 +406,7 @@ class ChatHandler {
fun setBackgroundTask(messageId: String, task: BackgroundTaskState) {
_messages.update { list ->
list.map { message ->
if (message.id == messageId) message.copy(backgroundTask = task) else message
if (message.matchesIdentity(messageId)) message.copy(backgroundTask = task) else message
}
}
}
@@ -412,7 +418,7 @@ class ChatHandler {
) {
_messages.update { list ->
list.map { message ->
if (message.id == messageId && message.backgroundTask != null) {
if (message.matchesIdentity(messageId) && message.backgroundTask != null) {
message.copy(backgroundTask = transform(message.backgroundTask))
} else {
message
@@ -493,7 +499,7 @@ class ChatHandler {
*/
fun appendAskCardMessage(messageId: String, card: HermesCard) {
_messages.update { list ->
if (list.any { it.id == messageId }) return@update list
if (list.any { it.matchesIdentity(messageId) }) return@update list
val msg = ChatMessage(
id = messageId,
role = MessageRole.ASSISTANT,
@@ -515,7 +521,7 @@ class ChatHandler {
*/
fun truncateMessagesFrom(messageId: String) {
_messages.update { list ->
val idx = list.indexOfFirst { it.id == messageId }
val idx = list.indexOfFirst { it.matchesIdentity(messageId) }
if (idx < 0) list else list.take(idx)
}
}
@@ -523,7 +529,7 @@ class ChatHandler {
fun replaceMessageContent(messageId: String, content: String) {
_messages.update { messages ->
messages.map { message ->
if (message.id == messageId) {
if (message.matchesIdentity(messageId)) {
message.copy(content = content)
} else {
message
@@ -543,8 +549,9 @@ class ChatHandler {
content: String,
) {
_messages.update { messages ->
val interim = messages.firstOrNull { it.id == interimMessageId } ?: return@update messages
val current = messages.firstOrNull { it.id == currentMessageId }
val interim = messages.firstOrNull { it.matchesIdentity(interimMessageId) }
?: return@update messages
val current = messages.firstOrNull { it.matchesIdentity(currentMessageId) }
val mergedTools = (interim.toolCalls + current?.toolCalls.orEmpty())
.distinctBy { it.id ?: "${it.name}:${it.startedAt}" }
val merged = interim.copy(
@@ -563,14 +570,18 @@ class ChatHandler {
backgroundTask = current?.backgroundTask ?: interim.backgroundTask,
)
messages
.filterNot { it.id == currentMessageId && currentMessageId != interimMessageId }
.map { if (it.id == interimMessageId) merged else it }
.filterNot {
current != null &&
current.uiKey != interim.uiKey &&
it.matchesIdentity(currentMessageId)
}
.map { if (it.matchesIdentity(interimMessageId)) merged else it }
}
}
/** Remove a provisional client-side message that never became a real turn. */
fun removeMessage(messageId: String) {
_messages.update { messages -> messages.filterNot { it.id == messageId } }
_messages.update { messages -> messages.filterNot { it.matchesIdentity(messageId) } }
}
/**
@@ -752,7 +763,7 @@ class ChatHandler {
_messages.update { messages ->
var changed = false
val mapped = messages.map { msg ->
if (msg.id == messageId && msg.role == MessageRole.ASSISTANT) {
if (msg.matchesIdentity(messageId) && msg.role == MessageRole.ASSISTANT) {
changed = true
// A realtimeTurn trace is attached ONLY for provider-only
// (non-Hermes-backed) turns — Hermes-backed ones leave it
@@ -971,7 +982,12 @@ class ChatHandler {
val user = checkpoint.user
val assistant = checkpoint.assistant
val upstreamText = upstreamAssistantText.orEmpty()
val currentAssistant = _messages.value.lastOrNull { it.id == assistant.id }
// A history poll may already have adopted the server id while the
// checkpoint still names the original client identity. They are one
// logical row, resolved through either side of that alias.
val currentAssistant = _messages.value.lastOrNull {
it.matchesIdentity(assistant.id) && it.role == MessageRole.ASSISTANT
}
val restoredContent = listOf(
assistant.content,
upstreamText,
@@ -1053,7 +1069,8 @@ class ChatHandler {
?.takeIf { it.isNotEmpty() }
?: checkpointMoaReferences
val restoredAssistant = ChatMessage(
id = assistant.id,
id = currentAssistant?.id ?: assistant.id,
uiKey = currentAssistant?.uiKey ?: assistant.id,
role = MessageRole.ASSISTANT,
content = restoredContent,
timestamp = assistant.timestamp,
@@ -1080,10 +1097,12 @@ class ChatHandler {
activeAgentName = restoredAssistant.agentName ?: activeAgentName
_messages.update { current ->
val withoutOldAssistant = current.filterNot { it.id == assistant.id }
val withoutOldAssistant = current.filterNot {
it.matchesIdentity(assistant.id) && it.role == MessageRole.ASSISTANT
}
val users = withoutOldAssistant.filter { it.role == MessageRole.USER }
val positionalUser = users.getOrNull(checkpoint.priorUserMessageCount)
val hasUser = withoutOldAssistant.any { it.id == user.id } ||
val hasUser = withoutOldAssistant.any { it.matchesIdentity(user.id) } ||
positionalUser?.content?.trim() == user.content.trim()
val withUser = if (hasUser) {
withoutOldAssistant
@@ -1100,7 +1119,7 @@ class ChatHandler {
// those corrections as ordinary user bubbles before the assistant
// row. Consume matching already-present rows first so repeated
// resume/checkpoint passes cannot duplicate them.
val originalUserIndex = withUser.indexOfFirst { it.id == user.id }
val originalUserIndex = withUser.indexOfFirst { it.matchesIdentity(user.id) }
.takeIf { it >= 0 }
?: withUser.indexOfFirst {
it.role == MessageRole.USER && it.content.trim() == user.content.trim()
@@ -1213,7 +1232,7 @@ class ChatHandler {
fun replaceMessageId(oldId: String, newId: String) {
_messages.update { messages ->
messages.map { msg ->
if (msg.id == oldId && msg.content.isBlank() && msg.isStreaming) {
if (msg.matchesIdentity(oldId) && msg.content.isBlank() && msg.isStreaming) {
msg.copy(id = newId)
} else msg
}
@@ -1236,7 +1255,7 @@ class ChatHandler {
fun mutateMessage(messageId: String, transform: (ChatMessage) -> ChatMessage) {
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId) transform(msg) else msg
if (msg.matchesIdentity(messageId)) transform(msg) else msg
}
}
}
@@ -1890,6 +1909,37 @@ class ChatHandler {
}
}
/**
* Returns true when structured session history contains tool lifecycle
* state that the live transcript did not receive. This is a read-only
* preflight for Gateway completion reconciliation: healthy live turns keep
* their current StateFlow list and UI identity, while omitted upstream
* tool events opt into [loadMessageHistory]. Assistant prose is never
* inspected or interpreted here.
*/
fun hasMissingPersistedToolActivity(items: List<MessageItem>): Boolean {
val toolResults = items.filter { it.role == "tool" }
.associateBy { it.toolCallId }
val persistedCalls = coalesceRenderedHistoryItems(items)
.asSequence()
.filter { it.role == "assistant" }
.flatMap { parseToolCallsFromHistory(it.toolCalls, toolResults).asSequence() }
.toList()
if (persistedCalls.isEmpty()) return false
val localCalls = _messages.value.flatMap { it.toolCalls }
return persistedCalls.any { persisted ->
val local = persisted.id?.let { id -> localCalls.firstOrNull { it.id == id } }
?: localCalls.firstOrNull {
it.id == null && it.name == persisted.name && it.args == persisted.args
}
local == null ||
(persisted.isComplete && !local.isComplete) ||
(persisted.result != null && persisted.result != local.result) ||
(persisted.success != null && persisted.success != local.success)
}
}
// --- Session management ---
fun setSessionId(sessionId: String?) {
@@ -1943,6 +1993,8 @@ class ChatHandler {
// rows (default source). (ADR 12 — Threads surface, slice 1.)
source = item.source,
hasModelConfig = item.hasModelConfig,
pinned = item.pinned,
archived = item.archived,
)
}.sortedByDescending { it.activityTimestamp }
// Preserve the active session's optimistic row when the server list
@@ -1989,6 +2041,26 @@ class ChatHandler {
}
}
/** Apply an optimistic pin/archive mutation; a failed server write restores the copy. */
fun setSessionFlagsLocal(
sessionId: String,
pinned: Boolean? = null,
archived: Boolean? = null,
) {
_sessions.update { sessions ->
sessions.map { session ->
if (session.sessionId == sessionId) {
session.copy(
pinned = pinned ?: session.pinned,
archived = archived ?: session.archived,
)
} else {
session
}
}
}
}
/**
* Add a newly created session to the list.
*/
@@ -2024,12 +2096,12 @@ class ChatHandler {
_messages.update { messages ->
val existing = messages.findLast {
it.id == messageId && it.role == MessageRole.ASSISTANT
it.matchesIdentity(messageId) && it.role == MessageRole.ASSISTANT
}
if (existing != null) {
messages.map { msg ->
if (msg.id == messageId) {
if (msg.matchesIdentity(messageId)) {
msg.copy(content = msg.content + processedDelta)
} else {
msg
@@ -2262,7 +2334,7 @@ class ChatHandler {
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId && msg.role == MessageRole.ASSISTANT) {
if (msg.matchesIdentity(messageId) && msg.role == MessageRole.ASSISTANT) {
msg.copy(cards = msg.cards + card)
} else msg
}
@@ -2288,7 +2360,7 @@ class ChatHandler {
_messages.update { messages ->
messages.map { msg ->
if (msg.id != messageId || msg.role != MessageRole.ASSISTANT) return@map msg
if (!msg.matchesIdentity(messageId) || msg.role != MessageRole.ASSISTANT) return@map msg
var cleaned = msg.content
var changed = false
for (rawLine in msg.content.lines()) {
@@ -2355,7 +2427,7 @@ class ChatHandler {
private fun stripLineFromContent(messageId: String, line: String) {
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId && msg.role == MessageRole.ASSISTANT) {
if (msg.matchesIdentity(messageId) && msg.role == MessageRole.ASSISTANT) {
// Remove the line (with surrounding newlines) from content
val cleaned = msg.content
.replace("\n$line\n", "\n")
@@ -2492,7 +2564,7 @@ class ChatHandler {
// that raced with stripLineFromContent during streaming.
_messages.update { messages ->
messages.map { msg ->
if (msg.id != messageId || msg.role != MessageRole.ASSISTANT) return@map msg
if (!msg.matchesIdentity(messageId) || msg.role != MessageRole.ASSISTANT) return@map msg
var cleaned = msg.content
var changed = false
for (rawLine in msg.content.lines()) {
@@ -2546,7 +2618,7 @@ class ChatHandler {
private fun finalizeAnnotations(messageId: String) {
_messages.update { messages ->
messages.map { msg ->
if (msg.id != messageId || msg.role != MessageRole.ASSISTANT) return@map msg
if (!msg.matchesIdentity(messageId) || msg.role != MessageRole.ASSISTANT) return@map msg
val existingToolNames = msg.toolCalls.map { it.name }.toSet()
val newToolCalls = mutableListOf<ToolCall>()
@@ -2629,7 +2701,7 @@ class ChatHandler {
.take(4)
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId && msg.role == MessageRole.ASSISTANT) {
if (msg.matchesIdentity(messageId) && msg.role == MessageRole.ASSISTANT) {
msg.copy(badges = cleaned)
} else {
msg
@@ -2645,7 +2717,7 @@ class ChatHandler {
if (cleaned.isEmpty()) return
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId && msg.role == MessageRole.ASSISTANT) {
if (msg.matchesIdentity(messageId) && msg.role == MessageRole.ASSISTANT) {
msg.copy(badges = (msg.badges + cleaned).distinct().take(4))
} else {
msg
@@ -2679,11 +2751,11 @@ class ChatHandler {
)
_messages.update { messages ->
val target = messages.findLast {
it.id == messageId && it.role == MessageRole.ASSISTANT
it.matchesIdentity(messageId) && it.role == MessageRole.ASSISTANT
}
if (target != null) {
messages.map { msg ->
if (msg.id == messageId) {
if (msg.matchesIdentity(messageId)) {
msg.copy(toolCalls = msg.toolCalls + placeholder)
} else {
msg
@@ -2707,6 +2779,7 @@ class ChatHandler {
messageId: String,
toolCallId: String,
toolName: String,
argsPreview: String? = null,
runId: String? = null,
provenance: String? = null,
) {
@@ -2714,7 +2787,7 @@ class ChatHandler {
_messages.update { messages ->
val target = messages.findLast {
it.id == messageId && it.role == MessageRole.ASSISTANT
it.matchesIdentity(messageId) && it.role == MessageRole.ASSISTANT
}
if (target != null) {
// Adopt a pending "preparing" placeholder for this name
@@ -2728,12 +2801,13 @@ class ChatHandler {
.indexOfFirst { it.isGenerating && !it.isComplete && it.name.isEmpty() }
.takeIf { it >= 0 }
messages.map { msg ->
if (msg.id != messageId) return@map msg
if (!msg.matchesIdentity(messageId)) return@map msg
if (genIdx != null) {
val calls = msg.toolCalls.toMutableList()
calls[genIdx] = calls[genIdx].copy(
id = toolCallId,
name = toolName,
args = argsPreview ?: calls[genIdx].args,
isGenerating = false,
// Execution starts now — preparing time isn't runtime.
startedAt = System.currentTimeMillis(),
@@ -2746,7 +2820,7 @@ class ChatHandler {
toolCalls = msg.toolCalls + ToolCall(
id = toolCallId,
name = toolName,
args = null,
args = argsPreview,
result = null,
success = null,
isComplete = false,
@@ -2767,7 +2841,7 @@ class ChatHandler {
ToolCall(
id = toolCallId,
name = toolName,
args = null,
args = argsPreview,
result = null,
success = null,
isComplete = false,
@@ -2786,7 +2860,7 @@ class ChatHandler {
fun onMoaReference(messageId: String, event: GatewayMoaReference) {
_messages.update { messages ->
val targetIndex = messages.indexOfLast {
it.id == messageId && it.role == MessageRole.ASSISTANT
it.matchesIdentity(messageId) && it.role == MessageRole.ASSISTANT
}
if (targetIndex < 0) return@update messages
_isStreaming.value = true
@@ -2861,11 +2935,11 @@ class ChatHandler {
)
_messages.update { messages ->
val target = messages.findLast {
it.id == messageId && it.role == MessageRole.ASSISTANT
it.matchesIdentity(messageId) && it.role == MessageRole.ASSISTANT
}
if (target != null) {
messages.map { msg ->
if (msg.id != messageId) return@map msg
if (!msg.matchesIdentity(messageId)) return@map msg
val closed = msg.toolCalls.map { call ->
if (call.taskIndex == event.taskIndex && !call.isComplete) {
call.copy(
@@ -2900,7 +2974,7 @@ class ChatHandler {
val summaryId = "subagent-${event.taskIndex}-${syntheticToolSeq++}"
_messages.update { messages ->
messages.map { msg ->
if (msg.id != messageId || msg.role != MessageRole.ASSISTANT) return@map msg
if (!msg.matchesIdentity(messageId) || msg.role != MessageRole.ASSISTANT) return@map msg
val hasLaneCalls = msg.toolCalls.any { it.taskIndex == event.taskIndex }
val closed = msg.toolCalls.map { call ->
if (call.taskIndex == event.taskIndex && !call.isComplete) {
@@ -2951,14 +3025,14 @@ class ChatHandler {
// Snapshot the matching tool call's name BEFORE mutating — we need it
// to decide whether to emit a phone-action result bubble below.
val toolName = _messages.value
.firstOrNull { it.id == messageId && it.role == MessageRole.ASSISTANT }
.firstOrNull { it.matchesIdentity(messageId) && it.role == MessageRole.ASSISTANT }
?.toolCalls
?.firstOrNull { it.id == toolCallId }
?.name
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId && msg.role == MessageRole.ASSISTANT) {
if (msg.matchesIdentity(messageId) && msg.role == MessageRole.ASSISTANT) {
val updatedCalls = msg.toolCalls.map { call ->
if (call.id == toolCallId && !call.isComplete) {
call.copy(
@@ -2995,14 +3069,14 @@ class ChatHandler {
// Snapshot tool name before the update so the phone-action bubble
// can label the failure correctly.
val toolName = _messages.value
.firstOrNull { it.id == messageId && it.role == MessageRole.ASSISTANT }
.firstOrNull { it.matchesIdentity(messageId) && it.role == MessageRole.ASSISTANT }
?.toolCalls
?.firstOrNull { it.id == toolCallId }
?.name
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId && msg.role == MessageRole.ASSISTANT) {
if (msg.matchesIdentity(messageId) && msg.role == MessageRole.ASSISTANT) {
val updatedCalls = msg.toolCalls.map { call ->
if (call.id == toolCallId && !call.isComplete) {
call.copy(
@@ -3035,7 +3109,7 @@ class ChatHandler {
fun onToolOutputRisk(messageId: String, outputRisk: GatewayToolOutputRisk) {
_messages.update { messages ->
messages.map { msg ->
if (msg.id != messageId || msg.role != MessageRole.ASSISTANT) return@map msg
if (!msg.matchesIdentity(messageId) || msg.role != MessageRole.ASSISTANT) return@map msg
val updatedCalls = msg.toolCalls.map { call ->
if (call.id == outputRisk.toolCallId) {
call.copy(
@@ -3066,7 +3140,7 @@ class ChatHandler {
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId) {
if (msg.matchesIdentity(messageId)) {
msg.copy(isStreaming = false, isThinkingStreaming = false)
} else {
msg
@@ -3095,7 +3169,7 @@ class ChatHandler {
fun markStopped(messageId: String) {
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId && "Stopped" !in msg.badges) {
if (msg.matchesIdentity(messageId) && "Stopped" !in msg.badges) {
msg.copy(badges = msg.badges + "Stopped")
} else {
msg
@@ -3122,7 +3196,7 @@ class ChatHandler {
fun markError(messageId: String) {
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId && "Error" !in msg.badges) {
if (msg.matchesIdentity(messageId) && "Error" !in msg.badges) {
msg.copy(badges = msg.badges + "Error", clientOnly = true)
} else {
msg
@@ -3148,7 +3222,7 @@ class ChatHandler {
_messages.update { messages ->
messages
.filterNot { msg ->
msg.id == messageId &&
msg.matchesIdentity(messageId) &&
msg.role == MessageRole.ASSISTANT &&
msg.toolCalls.isEmpty() &&
msg.backgroundTask == null &&
@@ -3156,7 +3230,7 @@ class ChatHandler {
(msg.content.isBlank() || isIntentionalSilenceMarker(msg.content))
}
.map { msg ->
if (msg.id == messageId || msg.isStreaming) {
if (msg.matchesIdentity(messageId) || msg.isStreaming) {
msg.copy(isStreaming = false, isThinkingStreaming = false)
} else {
msg
@@ -3220,10 +3294,12 @@ class ChatHandler {
fun onThinkingDelta(messageId: String, delta: String) {
_isStreaming.value = true
_messages.update { messages ->
val existing = messages.findLast { it.id == messageId && it.role == MessageRole.ASSISTANT }
val existing = messages.findLast {
it.matchesIdentity(messageId) && it.role == MessageRole.ASSISTANT
}
if (existing != null) {
messages.map { msg ->
if (msg.id == messageId) {
if (msg.matchesIdentity(messageId)) {
msg.copy(
thinkingContent = msg.thinkingContent + delta,
isThinkingStreaming = true
@@ -3248,7 +3324,7 @@ class ChatHandler {
fun onUsageReceived(messageId: String, inputTokens: Int?, outputTokens: Int?, totalTokens: Int?, cost: Double?) {
_messages.update { messages ->
messages.map { msg ->
if (msg.id == messageId) {
if (msg.matchesIdentity(messageId)) {
msg.copy(
inputTokens = inputTokens,
outputTokens = outputTokens,
@@ -3281,7 +3357,7 @@ class ChatHandler {
)
_messages.update { messages ->
messages.map { msg ->
if (msg.id != messageId) return@map msg
if (!msg.matchesIdentity(messageId)) return@map msg
val alreadyDispatched = msg.cardDispatches.any {
it.cardKey == cardKey && it.actionValue == actionValue
}
@@ -763,12 +763,24 @@ class DashboardApiClient(
suspend fun getSessionMessages(
sessionId: String,
profile: String? = null,
mode: SessionMessageLoadMode = SessionMessageLoadMode.COMPLETE,
): Result<List<MessageItem>> = withContext(Dispatchers.IO) {
val name = profile?.trim().orEmpty()
val query = if (name.isNotBlank()) "?profile=${pathSegment(name)}" else ""
getJson("/api/sessions/${pathSegment(sessionId)}/messages$query").mapCatching { root ->
val parsed = json.decodeFromJsonElement(MessageListResponse.serializer(), root)
parsed.messages ?: parsed.data ?: parsed.items ?: emptyList()
loadSessionMessages(mode) { page ->
val query = buildList {
add("limit=${page.limit}")
add("offset=${page.offset}")
add("order=${page.order}")
if (name.isNotBlank()) add("profile=${pathSegment(name)}")
}.joinToString(prefix = "?", separator = "&")
getJson("/api/sessions/${pathSegment(sessionId)}/messages$query").mapCatching { root ->
val parsed = json.decodeFromJsonElement(MessageListResponse.serializer(), root)
SessionMessagePage(
messages = parsed.messages ?: parsed.data ?: parsed.items ?: emptyList(),
pagination = parsed.pagination,
payloadChars = root.toString().length,
)
}
}
}
@@ -832,6 +844,20 @@ class DashboardApiClient(
},
)
/** Persist the upstream keep flag that backs official-client session pins. */
suspend fun setSessionPinned(
sessionId: String,
pinned: Boolean,
profile: String? = null,
): Result<JsonObject> =
patchJsonObject(
"/api/sessions/${pathSegment(sessionId)}${profileQuery(profile)}",
buildJsonObject {
put("pinned", pinned)
profile?.trim()?.takeIf { it.isNotBlank() }?.let { put("profile", it) }
},
)
/**
* Dry-run a server-backed bulk session cleanup via the dashboard
* `POST /api/sessions/prune` (`dry_run: true`). Returns what WOULD be
@@ -260,6 +260,9 @@ class GatewayChatClient(
private val _serverProvider = MutableStateFlow<String?>(null)
val serverProvider: StateFlow<String?> = _serverProvider.asStateFlow()
private val _serverModelIdentity = MutableStateFlow<GatewayModelIdentity?>(null)
val serverModelIdentity: StateFlow<GatewayModelIdentity?> = _serverModelIdentity.asStateFlow()
/**
* Active reasoning EFFORT from `session.info` (string; "" when reasoning is
* disabled). The reasoning DISPLAY mode is NOT on session.info — it stays a
@@ -269,6 +272,10 @@ class GatewayChatClient(
private val _serverReasoningEffort = MutableStateFlow<String?>(null)
val serverReasoningEffort: StateFlow<String?> = _serverReasoningEffort.asStateFlow()
private val _serverReasoningIdentity = MutableStateFlow<GatewayReasoningIdentity?>(null)
val serverReasoningIdentity: StateFlow<GatewayReasoningIdentity?> =
_serverReasoningIdentity.asStateFlow()
/**
* Server-reported credential warning (upstream `session.info.credential_warning`)
* — present ONLY when the active provider's key is missing/invalid, absent
@@ -555,7 +562,11 @@ class GatewayChatClient(
buildJsonObject {
put("session_id", liveSessionId ?: error("no live session"))
put("text", text)
truncateBeforeUserOrdinal?.let { put("truncate_before_user_ordinal", it) }
truncateBeforeUserOrdinal?.let { ordinal ->
put("truncate_before_user_ordinal", ordinal)
put("confirm_truncate", true)
if (ordinal == 0) put("confirm_empty_truncate", true)
}
if (queuedFollowUp) put("queued", true)
},
// Long-running RPC, not a generic 15s ack — see the
@@ -581,7 +592,7 @@ class GatewayChatClient(
if (activeTurn === turn) activeTurn = null
turn.disarmWatchdog()
val submitError = submitted.exceptionOrNull()
if ((submitError as? GatewayRpcException)?.code == ACTIVE_SESSION_CAP_REJECTION) {
if (submitError.isAuthoritativePromptSubmitRejection()) {
// Authoritative policy rejection: the gateway received
// the prompt and deliberately refused to create the
// first turn. Falling back to SSE would bypass the cap
@@ -590,7 +601,7 @@ class GatewayChatClient(
// through the normal failed-turn callback instead.
turn.tracer.done("submit-rejected")
turn.callbacks.onError(
submitError.message ?: "Hermes rejected the new session",
submitError?.message ?: "Hermes rejected the new session",
)
return@launch
}
@@ -1115,41 +1126,59 @@ class GatewayChatClient(
)
/** Answer a [GatewayAsk.Kind.CLARIFY] ask. */
suspend fun respondClarify(requestId: String, answer: String): Result<GatewayAskResponse> =
rpc(
suspend fun respondClarify(requestId: String, answer: String): Result<GatewayAskResponse> {
val respondingTurn = activeTurn
return rpc(
"clarify.respond",
buildJsonObject {
put("request_id", requestId)
put("answer", answer)
},
).map { it.gatewayAskResponse() }
).map {
it.gatewayAskResponse().also {
respondingTurn?.acknowledgeInteraction(GatewayAskExpiry(GatewayAsk.Kind.CLARIFY, requestId))
}
}
}
/**
* Answer a [GatewayAsk.Kind.SUDO] ask. The password must NEVER be logged
* or persisted — it exists only inside this outbound frame.
*/
suspend fun respondSudo(requestId: String, password: String): Result<GatewayAskResponse> =
rpc(
suspend fun respondSudo(requestId: String, password: String): Result<GatewayAskResponse> {
val respondingTurn = activeTurn
return rpc(
"sudo.respond",
buildJsonObject {
put("request_id", requestId)
put("password", password)
},
).map { it.gatewayAskResponse() }
).map {
it.gatewayAskResponse().also {
respondingTurn?.acknowledgeInteraction(GatewayAskExpiry(GatewayAsk.Kind.SUDO, requestId))
}
}
}
/**
* Answer a [GatewayAsk.Kind.SECRET] ask. Empty [value] = skip (upstream
* returns `skipped: true` to the tool). The value must NEVER be logged
* or persisted — it exists only inside this outbound frame.
*/
suspend fun respondSecret(requestId: String, value: String): Result<GatewayAskResponse> =
rpc(
suspend fun respondSecret(requestId: String, value: String): Result<GatewayAskResponse> {
val respondingTurn = activeTurn
return rpc(
"secret.respond",
buildJsonObject {
put("request_id", requestId)
put("value", value)
},
).map { it.gatewayAskResponse() }
).map {
it.gatewayAskResponse().also {
respondingTurn?.acknowledgeInteraction(GatewayAskExpiry(GatewayAsk.Kind.SECRET, requestId))
}
}
}
/**
* Answer a [GatewayAsk.Kind.APPROVAL] ask — correlated by the live
@@ -1157,6 +1186,7 @@ class GatewayChatClient(
* resolves every pending approval on the session at once.
*/
suspend fun respondApproval(choice: String, all: Boolean = false): Result<GatewayAskResponse> {
val respondingTurn = activeTurn
val sid = liveSessionId
?: return Result.failure(GatewayRpcException("no live session"))
return rpc(
@@ -1166,7 +1196,11 @@ class GatewayChatClient(
put("choice", choice)
put("all", all)
},
).map { it.gatewayAskResponse() }
).map {
it.gatewayAskResponse().also {
respondingTurn?.acknowledgeInteraction(GatewayAskExpiry(GatewayAsk.Kind.APPROVAL, null))
}
}
}
/**
@@ -1419,23 +1453,12 @@ class GatewayChatClient(
if (refresh) put("refresh", true)
}
return rpc("model.options", params).map { result ->
val providers = (result["providers"] as? JsonArray).orEmpty().mapNotNull { el ->
val obj = el as? JsonObject ?: return@mapNotNull null
val slug = obj.stringField("slug") ?: return@mapNotNull null
GatewayModelProvider(
name = obj.stringField("name") ?: slug,
slug = slug,
models = (obj["models"] as? JsonArray).orEmpty()
.mapNotNull { (it as? JsonPrimitive)?.contentOrNull },
isCurrent = (obj["is_current"] as? JsonPrimitive)?.booleanOrNull ?: false,
warning = obj.stringField("warning"),
authenticated = (obj["authenticated"] as? JsonPrimitive)?.booleanOrNull ?: true,
unavailableModels = (obj["unavailable_models"] as? JsonArray).orEmpty()
.mapNotNull { (it as? JsonPrimitive)?.contentOrNull },
freeTier = (obj["free_tier"] as? JsonPrimitive)?.booleanOrNull ?: false,
totalModels = (obj["total_models"] as? JsonPrimitive)?.contentOrNull?.toIntOrNull() ?: 0,
)
}
val providers = normalizeGatewayModelProviders(
(result["providers"] as? JsonArray).orEmpty().mapNotNull { el ->
val obj = el as? JsonObject ?: return@mapNotNull null
parseGatewayModelProvider(obj)
},
)
GatewayModelOptions(
providers = providers,
currentModel = result.stringField("model") ?: "",
@@ -1784,12 +1807,23 @@ class GatewayChatClient(
_serverPersonality.value =
(info.stringField("personality") ?: "").ifBlank { "none" }
}
info.stringField("model")?.takeIf { it.isNotBlank() }?.let { _serverModel.value = it }
info.stringField("provider")?.takeIf { it.isNotBlank() }?.let { _serverProvider.value = it }
val model = info.stringField("model")?.takeIf { it.isNotBlank() }
val provider = info.stringField("provider")?.takeIf { it.isNotBlank() }
model?.let { _serverModel.value = it }
provider?.let { _serverProvider.value = it }
if (model != null && provider != null) {
_serverModelIdentity.value = GatewayModelIdentity(model = model, provider = provider)
}
// reasoning effort: ignore "" (reasoning disabled) so it can't clobber
// the chip; display mode is config.get-only, not here.
info.stringField("reasoning_effort")?.takeIf { it.isNotBlank() }
?.let { _serverReasoningEffort.value = it }
val reasoningEffort = info.stringField("reasoning_effort")?.takeIf { it.isNotBlank() }
reasoningEffort?.let { _serverReasoningEffort.value = it }
if (model != null && provider != null && reasoningEffort != null) {
_serverReasoningIdentity.value = GatewayReasoningIdentity(
identity = GatewayModelIdentity(model = model, provider = provider),
effort = reasoningEffort,
)
}
// credential_warning: present only when the provider key is missing/
// invalid. ABSENT means healthy — clear to null so it self-resolves.
_serverCredentialWarning.value =
@@ -2136,13 +2170,14 @@ class GatewayChatClient(
pendingAsk.kind == expiry.kind &&
(pendingAsk.kind == GatewayAsk.Kind.APPROVAL ||
pendingAsk.requestId == expiry.requestId)
val turnResumed = pendingAsk != null &&
GatewayEventMapper.isInteractionResumeEvent(type)
if (explicitlyExpired || turnResumed) {
// Ordinary turn activity is not a decision acknowledgement. It can
// be replayed or buffered. Only an authoritative expiry retires a
// detached ask; an explicit response is retired by its foreground VM.
if (explicitlyExpired) {
backgroundTurn.pendingAsk = null
callbackDispatcher {
backgroundInteractionListener?.invoke(
GatewayBackgroundInteractionEvent.Resolved(
GatewayBackgroundInteractionEvent.Expired(
storedSessionId = backgroundTurn.storedSessionId,
profile = backgroundTurn.profile,
ask = pendingAsk,
@@ -2158,6 +2193,7 @@ class GatewayChatClient(
unmatchedTurnCompleteListener?.invoke(
GatewayBackgroundTurnCompletion(
storedSessionId = backgroundTurn.storedSessionId,
liveSessionId = eventSessionId,
profile = backgroundTurn.profile,
expectedAssistantText = expectedText,
),
@@ -2232,6 +2268,7 @@ class GatewayChatClient(
unmatchedTurnCompleteListener?.invoke(
GatewayBackgroundTurnCompletion(
storedSessionId = storedId,
liveSessionId = eventSessionId,
profile = liveSessionProfile,
expectedAssistantText = expectedText,
),
@@ -2640,6 +2677,9 @@ class GatewayChatClient(
fun restoreInteraction(ask: GatewayAsk) {
mapper.restoreInteraction(ask)
}
fun acknowledgeInteraction(expiry: GatewayAskExpiry) {
mapper.acknowledgeInteraction(expiry)
}
private val deferredEventLock = Any()
private val deferredEvents = mutableListOf<Pair<String, JsonObject?>>()
private var eventsDeferred = deferEvents
@@ -2969,7 +3009,9 @@ class GatewayChatClient(
callbackDispatcher { callbacks.onInterimReconciled(text) }
},
onThinkingDelta = { v -> callbackDispatcher { callbacks.onThinkingDelta(v) } },
onToolCallStart = { a, b -> callbackDispatcher { callbacks.onToolCallStart(a, b) } },
onToolCallStart = { id, name, args ->
callbackDispatcher { callbacks.onToolCallStart(id, name, args) }
},
onToolCallDone = { a, b -> callbackDispatcher { callbacks.onToolCallDone(a, b) } },
onToolCallFailed = { a, b -> callbackDispatcher { callbacks.onToolCallFailed(a, b) } },
onToolOutputRisk = { v -> callbackDispatcher { callbacks.onToolOutputRisk(v) } },
@@ -2983,7 +3025,6 @@ class GatewayChatClient(
onMoaReference = { v -> callbackDispatcher { callbacks.onMoaReference(v) } },
onInteractionRequest = { v -> callbackDispatcher { callbacks.onInteractionRequest(v) } },
onInteractionExpired = { v -> callbackDispatcher { callbacks.onInteractionExpired(v) } },
onInteractionResolved = { v -> callbackDispatcher { callbacks.onInteractionResolved(v) } },
// MUST be wrapped like every other member: GatewayTurnCallbacks gives
// onStatusUpdate a default no-op, so omitting it here silently swallows
// EVERY gateway status line — the ❌ terminal-error lifecycle update
@@ -3047,7 +3088,13 @@ internal class GatewayConnectAttemptException(message: String) : Exception(messa
internal class GatewayRpcException(message: String, val code: Int? = null) : Exception(message)
private const val JSONRPC_METHOD_NOT_FOUND = -32601
private const val ACTIVE_SESSION_CAP_REJECTION = 4090
private val AUTHORITATIVE_PROMPT_SUBMIT_REJECTIONS = setOf(
4028, // first-turn truncate requires explicit empty-history confirmation
4029, // every destructive truncate requires explicit confirmation
4090, // active-session capacity policy
5070, // initial session persistence failed: storage full
5071, // other authoritative initial session persistence failure
)
private const val MAX_RECOVERED_CORRECTIONS = 32
private const val MAX_RECOVERED_CORRECTION_CHARS = 32_768
private const val PET_THUMB_DATA_PREFIX = "data:image/png;base64,"
@@ -3103,6 +3150,9 @@ private fun Throwable?.isMethodNotFound(): Boolean {
msg.contains("unknown method", ignoreCase = true)
}
private fun Throwable?.isAuthoritativePromptSubmitRejection(): Boolean =
(this as? GatewayRpcException)?.code in AUTHORITATIVE_PROMPT_SUBMIT_REJECTIONS
private fun Throwable?.isApprovalModeUnsupported(): Boolean {
val rpcError = this as? GatewayRpcException ?: return false
val message = rpcError.message.orEmpty()
@@ -40,6 +40,15 @@ class GatewayEventMapper(
if (!duplicate) callbacks.onInteractionRequest(ask)
}
/** Retire only the ask whose explicit respond RPC reached server truth. */
internal fun acknowledgeInteraction(expiry: GatewayAskExpiry) {
val pending = pendingInteraction ?: return
if (pending.matches(expiry)) {
pendingInteraction = null
drainDeferredTerminalEvent()
}
}
private var sawMessageStart = false
private var previousEventType: String? = null
private var sawTextDelta = false
@@ -50,6 +59,7 @@ class GatewayEventMapper(
private var compactionStatusActive = false
private var moaStatusActive = false
private var pendingInteraction: GatewayAsk? = null
private var deferredTerminalEvent: Pair<String, JsonObject?>? = null
/**
* `tool.complete` events match their `tool.start` by `tool_id`; when a
@@ -81,17 +91,17 @@ class GatewayEventMapper(
}
callbacks.onInteractionExpired(expiry)
previousEventType = type
if (pendingInteraction == null) drainDeferredTerminalEvent()
return
}
if (type in INTERACTION_RESUME_EVENTS) {
pendingInteraction?.let { ask ->
pendingInteraction = null
callbacks.onInteractionResolved(
GatewayAskExpiry(kind = ask.kind, requestId = ask.requestId),
)
}
if (pendingInteraction != null && type in TERMINAL_EVENTS) {
// A buffered/late terminal frame is not consent. Upstream blocks
// the turn on an interaction, so hold the first terminal until an
// explicit response acknowledgement or authoritative expiry.
if (deferredTerminalEvent == null) deferredTerminalEvent = type to payload
previousEventType = type
return
}
when (type) {
"reasoning.delta" -> {
val text = payload.string("text")
@@ -197,7 +207,10 @@ class GatewayEventMapper(
}
else -> syntheticToolId(name)
}
callbacks.onToolCallStart(toolId, name)
val argsPreview = payload.string("args_text")
?.takeIf { it.isNotBlank() }
?: payload.string("context")?.takeIf { it.isNotBlank() }
callbacks.onToolCallStart(toolId, name, argsPreview)
}
"tool.complete" -> {
@@ -210,7 +223,10 @@ class GatewayEventMapper(
if (!error.isNullOrEmpty()) {
callbacks.onToolCallFailed(toolId, error)
} else {
callbacks.onToolCallDone(toolId, payload.string("summary"))
val resultPreview = payload.string("result_text")
?.takeIf { it.isNotBlank() }
?: payload.string("summary")?.takeIf { it.isNotBlank() }
callbacks.onToolCallDone(toolId, resultPreview)
}
}
@@ -371,6 +387,12 @@ class GatewayEventMapper(
previousEventType = type
}
private fun drainDeferredTerminalEvent() {
val deferred = deferredTerminalEvent ?: return
deferredTerminalEvent = null
onEvent(deferred.first, deferred.second)
}
private fun syntheticToolId(name: String): String {
val id = "gateway-tool-$name-${syntheticToolCounter++}"
openSyntheticIdsByName.getOrPut(name) { ArrayDeque() }.addLast(id)
@@ -410,20 +432,7 @@ class GatewayEventMapper(
private const val MAX_MOA_LABEL_CHARS = 120
private const val MAX_MOA_REFERENCE_CHARS = 16_000
private val OUTPUT_RISK_LEVELS = setOf("low", "medium", "high", "critical")
private val INTERACTION_RESUME_EVENTS = setOf(
"reasoning.delta",
"thinking.delta",
"reasoning.available",
"message.delta",
"message.interim",
"message.start",
"tool.generating",
"tool.start",
"tool.complete",
"message.complete",
"error",
)
private val TERMINAL_EVENTS = setOf("message.complete", "error")
internal fun isFailedMoaReference(text: String): Boolean {
val normalized = text.trimStart().lowercase()
return normalized.startsWith("[failed:") || normalized.startsWith("[skipped:")
@@ -497,8 +506,6 @@ class GatewayEventMapper(
else -> null
}
fun isInteractionResumeEvent(type: String): Boolean = type in INTERACTION_RESUME_EVENTS
/**
* Hermes 2026-07-15 emits these operational wait lines through the
* legacy `thinking.delta` display callback. Match the deliberately
@@ -1,6 +1,11 @@
package com.hermesandroid.relay.network.upstream
import com.hermesandroid.relay.network.upstream.models.UsageInfo
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.contentOrNull
/**
* Shared types for the Gateway chat transport — upstream hermes-agent's
@@ -168,6 +173,7 @@ data class GatewaySessionRecovery(
/** A detached sibling turn reached its terminal event on the shared Gateway socket. */
data class GatewayBackgroundTurnCompletion(
val storedSessionId: String,
val liveSessionId: String,
val profile: String?,
val expectedAssistantText: String?,
)
@@ -184,7 +190,8 @@ sealed interface GatewayBackgroundInteractionEvent {
override val ask: GatewayAsk,
) : GatewayBackgroundInteractionEvent
data class Resolved(
/** An authoritative upstream `*.expire` event ended this request. */
data class Expired(
override val storedSessionId: String,
override val profile: String?,
override val ask: GatewayAsk,
@@ -347,8 +354,122 @@ data class GatewayModelProvider(
val unavailableModels: List<String> = emptyList(),
val freeTier: Boolean = false,
val totalModels: Int = 0,
/** Per-model capability rows keyed by the exact model id. */
val capabilities: Map<String, GatewayModelCapabilities> = emptyMap(),
)
/** Shared tolerant parser for the gateway RPC and API-server REST twins. */
internal fun parseGatewayModelProvider(obj: JsonObject): GatewayModelProvider? {
val slug = (obj["slug"] as? JsonPrimitive)?.contentOrNull
?.trim()?.takeIf { it.isNotEmpty() } ?: return null
val capabilities = (obj["capabilities"] as? JsonObject).orEmpty().mapNotNull { (model, raw) ->
val row = raw as? JsonObject ?: return@mapNotNull null
val effortsElement = row["reasoning_efforts"]
val efforts = if (effortsElement is JsonArray) {
effortsElement
.mapNotNull { (it as? JsonPrimitive)?.contentOrNull?.trim()?.takeIf(String::isNotEmpty) }
.distinct()
} else {
null
}
val modelId = model.trim().takeIf { it.isNotEmpty() } ?: return@mapNotNull null
modelId to GatewayModelCapabilities(
reasoning = (row["reasoning"] as? JsonPrimitive)?.booleanOrNull,
reasoningEfforts = efforts,
reasoningEffortsExact =
(row["reasoning_efforts_exact"] as? JsonPrimitive)?.booleanOrNull,
)
}.toMap()
return GatewayModelProvider(
name = (obj["name"] as? JsonPrimitive)?.contentOrNull ?: slug,
slug = slug,
models = (obj["models"] as? JsonArray).orEmpty()
.mapNotNull { (it as? JsonPrimitive)?.contentOrNull?.trim()?.takeIf(String::isNotEmpty) }
.distinct(),
isCurrent = (obj["is_current"] as? JsonPrimitive)?.booleanOrNull ?: false,
warning = (obj["warning"] as? JsonPrimitive)?.contentOrNull,
authenticated = (obj["authenticated"] as? JsonPrimitive)?.booleanOrNull ?: true,
unavailableModels = (obj["unavailable_models"] as? JsonArray).orEmpty()
.mapNotNull { (it as? JsonPrimitive)?.contentOrNull?.trim()?.takeIf(String::isNotEmpty) }
.distinct(),
freeTier = (obj["free_tier"] as? JsonPrimitive)?.booleanOrNull ?: false,
totalModels = (obj["total_models"] as? JsonPrimitive)?.contentOrNull?.toIntOrNull() ?: 0,
capabilities = capabilities,
)
}
/**
* Publish one coherent row per provider identity.
*
* Dynamic catalogs and compatibility payloads can repeat a provider row or a
* model inside that row. Provider slugs are case-insensitive upstream, while
* model ids remain exact request values. Merge only equal provider slugs so a
* model intentionally offered by two different providers stays selectable.
*/
internal fun normalizeGatewayModelProviders(
providers: List<GatewayModelProvider>,
): List<GatewayModelProvider> {
val normalized = linkedMapOf<String, GatewayModelProvider>()
providers.forEach { raw ->
val slug = raw.slug.trim()
if (slug.isEmpty()) return@forEach
val models = raw.models.map(String::trim).filter(String::isNotEmpty).distinct()
val unavailable = raw.unavailableModels
.map(String::trim)
.filter(String::isNotEmpty)
.distinct()
val capabilities = raw.capabilities.mapNotNull { (model, capability) ->
model.trim().takeIf(String::isNotEmpty)?.let { it to capability }
}.toMap()
val row = raw.copy(
name = raw.name.trim().ifEmpty { slug },
slug = slug,
models = models,
unavailableModels = unavailable,
totalModels = maxOf(raw.totalModels, models.size),
capabilities = capabilities,
)
val identity = slug.lowercase()
val existing = normalized[identity]
normalized[identity] = if (existing == null) {
row
} else {
val mergedModels = (existing.models + row.models).distinct()
existing.copy(
models = mergedModels,
isCurrent = existing.isCurrent || row.isCurrent,
warning = existing.warning ?: row.warning,
authenticated = existing.authenticated || row.authenticated,
unavailableModels = (existing.unavailableModels + row.unavailableModels).distinct(),
freeTier = existing.freeTier || row.freeTier,
totalModels = maxOf(existing.totalModels, row.totalModels, mergedModels.size),
capabilities = mergeGatewayModelCapabilities(existing.capabilities, row.capabilities),
)
}
}
return normalized.values.toList()
}
private fun mergeGatewayModelCapabilities(
existing: Map<String, GatewayModelCapabilities>,
incoming: Map<String, GatewayModelCapabilities>,
): Map<String, GatewayModelCapabilities> {
val merged = existing.toMutableMap()
incoming.forEach { (model, next) ->
val current = merged[model]
merged[model] = if (current == null) {
next
} else {
GatewayModelCapabilities(
reasoning = next.reasoning ?: current.reasoning,
reasoningEfforts = next.reasoningEfforts ?: current.reasoningEfforts,
reasoningEffortsExact = next.reasoningEffortsExact ?: current.reasoningEffortsExact,
)
}
}
return merged
}
data class GatewayMoaReference(
val index: Int?,
val count: Int?,
@@ -364,6 +485,15 @@ data class GatewayModelOptions(
val currentProvider: String,
)
/** Coherent model identity from a single `session.info` payload. */
data class GatewayModelIdentity(val model: String, val provider: String)
/** Model identity and effort observed together in one `session.info` payload. */
data class GatewayReasoningIdentity(
val identity: GatewayModelIdentity,
val effort: String,
)
/** Reject provider catalogs that completed after a profile/context switch. */
internal fun isCurrentModelOptionsResponse(
requestGeneration: Long,
@@ -373,6 +503,21 @@ internal fun isCurrentModelOptionsResponse(
): Boolean =
requestGeneration == currentGeneration && requestProfileKey == currentProfileKey
/**
* Selects the identity a model-options response may publish into chat UI state.
* Catalog-only requests populate picker choices without changing session identity.
*/
internal fun modelOptionsIdentityToPublish(
catalogOnly: Boolean,
hasLiveSession: Boolean,
sessionIdentity: GatewayModelIdentity?,
options: GatewayModelOptions,
): GatewayModelIdentity? = when {
catalogOnly -> null
hasLiveSession && sessionIdentity != null -> sessionIdentity
else -> GatewayModelIdentity(options.currentModel, options.currentProvider)
}
/**
* The explicit in-chat overrides to bind onto a gateway `session.create` as the
* new session's PER-SESSION overrides. Matches the upstream desktop client,
@@ -437,7 +582,7 @@ class GatewayTurnCallbacks(
*/
val onInterimReconciled: (text: String) -> Unit = { _ -> },
val onThinkingDelta: (String) -> Unit,
val onToolCallStart: (toolCallId: String, toolName: String) -> Unit,
val onToolCallStart: (toolCallId: String, toolName: String, argsPreview: String?) -> Unit,
val onToolCallDone: (toolCallId: String, resultPreview: String?) -> Unit,
val onToolCallFailed: (toolCallId: String, errorMsg: String?) -> Unit,
/** Attach deterministic output-risk metadata to the matching tool card. */
@@ -470,8 +615,6 @@ class GatewayTurnCallbacks(
val onInteractionRequest: (GatewayAsk) -> Unit,
/** Server declared a pending interaction expired; clear only the matching card. */
val onInteractionExpired: (GatewayAskExpiry) -> Unit,
/** The turn resumed after a pending interaction was resolved elsewhere. */
val onInteractionResolved: (GatewayAskExpiry) -> Unit = { _ -> },
/**
* Gateway `status.update` lifecycle line — model fallback, retries, and
* errors (often emoji-prefixed: 🔄 fallback, ⏳ retry, ❌ error). Default
@@ -19,6 +19,7 @@ import com.hermesandroid.relay.network.upstream.models.SkillListResponse
import com.hermesandroid.relay.network.upstream.models.UsageInfo
import com.hermesandroid.relay.util.TurnLatencyTracer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
import kotlinx.serialization.Serializable
@@ -27,6 +28,7 @@ import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.put
@@ -213,25 +215,12 @@ internal fun parseApiProviderModelOptionsBody(
val root = runCatching { json.parseToJsonElement(body) as? JsonObject }.getOrNull()
?: return null
val rows = root["providers"] as? JsonArray ?: return null
val providers = rows.mapNotNull { element ->
val obj = element as? JsonObject ?: return@mapNotNull null
val slug = (obj["slug"] as? JsonPrimitive)?.contentOrNull
?.trim()?.takeIf { it.isNotEmpty() } ?: return@mapNotNull null
GatewayModelProvider(
name = (obj["name"] as? JsonPrimitive)?.contentOrNull ?: slug,
slug = slug,
models = (obj["models"] as? JsonArray).orEmpty()
.mapNotNull { (it as? JsonPrimitive)?.contentOrNull },
isCurrent = (obj["is_current"] as? JsonPrimitive)?.booleanOrNull ?: false,
warning = (obj["warning"] as? JsonPrimitive)?.contentOrNull,
authenticated = (obj["authenticated"] as? JsonPrimitive)?.booleanOrNull ?: true,
unavailableModels = (obj["unavailable_models"] as? JsonArray).orEmpty()
.mapNotNull { (it as? JsonPrimitive)?.contentOrNull },
freeTier = (obj["free_tier"] as? JsonPrimitive)?.booleanOrNull ?: false,
totalModels = (obj["total_models"] as? JsonPrimitive)?.contentOrNull
?.toIntOrNull() ?: 0,
)
}
val providers = normalizeGatewayModelProviders(
rows.mapNotNull { element ->
val obj = element as? JsonObject ?: return@mapNotNull null
parseGatewayModelProvider(obj)
},
)
return ApiProviderModelOptions(
providers = providers,
currentModel = (root["model"] as? JsonPrimitive)?.contentOrNull.orEmpty(),
@@ -254,7 +243,8 @@ enum class ApiModelRoutingErrorCode {
class ApiModelRoutingException(
val code: ApiModelRoutingErrorCode,
message: String,
) : IOException(message)
cause: Throwable? = null,
) : IOException(message, cause)
sealed interface ApiModelSelectionAck {
data object ServerDefault : ApiModelSelectionAck
@@ -337,7 +327,7 @@ internal fun parseModelOptionsBody(json: Json, body: String): List<ApiModelOptio
root = (obj["root"] as? JsonPrimitive)?.contentOrNull,
parent = (obj["parent"] as? JsonPrimitive)?.contentOrNull,
)
}
}.distinctBy { it.id }
}
private const val STREAM_ERROR_BODY_LIMIT = 16L * 1024L
@@ -696,19 +686,62 @@ class HermesApiClient(
}
}
suspend fun getMessages(sessionId: String): List<MessageItem> = withContext(Dispatchers.IO) {
suspend fun setSessionPinned(sessionId: String, pinned: Boolean): Boolean =
patchSessionFlag(sessionId, "pinned", pinned)
suspend fun setSessionArchived(sessionId: String, archived: Boolean): Boolean =
patchSessionFlag(sessionId, "archived", archived)
private suspend fun patchSessionFlag(
sessionId: String,
field: String,
value: Boolean,
): Boolean = withContext(Dispatchers.IO) {
try {
val request = authRequest("$baseUrl/api/sessions/$sessionId/messages")
.get()
val reqBody = buildJsonObject { put(field, value) }.toString()
val request = authRequest("$baseUrl/api/sessions/$sessionId")
.patch(reqBody.toRequestBody(JSON_MEDIA))
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) return@withContext emptyList()
val body = response.body?.string() ?: return@withContext emptyList()
val parsed = json.decodeFromString<MessageListResponse>(body)
parsed.data ?: parsed.items ?: parsed.messages ?: emptyList()
if (!response.isSuccessful) {
Log.w(TAG, "Set session $field failed: HTTP ${response.code}")
}
response.isSuccessful
}
} catch (e: Exception) {
Log.w(TAG, "Failed to get messages: ${e.message}")
Log.w(TAG, "Failed to set session $field: ${e.message}")
false
}
}
suspend fun getMessages(
sessionId: String,
mode: SessionMessageLoadMode = SessionMessageLoadMode.COMPLETE,
): List<MessageItem> = withContext(Dispatchers.IO) {
loadSessionMessages(mode) { page ->
runCatching {
val url = "$baseUrl/api/sessions/$sessionId/messages".toHttpUrlOrNull()
?.newBuilder()
?.addQueryParameter("limit", page.limit.toString())
?.addQueryParameter("offset", page.offset.toString())
?.addQueryParameter("order", page.order)
?.build()
?: error("invalid session messages URL")
val request = authRequest(url.toString()).get().build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) error("HTTP ${response.code}")
val body = response.body?.string() ?: error("empty response body")
val parsed = json.decodeFromString<MessageListResponse>(body)
SessionMessagePage(
messages = parsed.data ?: parsed.items ?: parsed.messages ?: emptyList(),
pagination = parsed.pagination,
payloadChars = body.length,
)
}
}
}.getOrElse { error ->
if (error is CancellationException) throw error
Log.w(TAG, "Failed to get messages: ${error.message}")
emptyList()
}
}
@@ -815,6 +848,7 @@ class HermesApiClient(
ApiModelRoutingException(
ApiModelRoutingErrorCode.INVENTORY_UNAVAILABLE,
"Model inventory could not be loaded.",
e,
)
},
)
@@ -4,17 +4,28 @@ import android.content.Context
import com.hermesandroid.relay.auth.SessionTokenStore
import com.hermesandroid.relay.auth.SecureStoreCache
import com.hermesandroid.relay.auth.buildRawTokenStore
import java.io.EOFException
import java.io.IOException
import java.io.InterruptedIOException
import java.net.ConnectException
import java.net.InetAddress
import java.net.NoRouteToHostException
import java.net.SocketException
import java.net.UnknownHostException
import java.security.MessageDigest
import java.security.SecureRandom
import java.util.Collections
import java.util.IdentityHashMap
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
import javax.net.ssl.SSLException
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okhttp3.Authenticator
import okhttp3.Dns
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
@@ -28,6 +39,7 @@ import okio.ByteString.Companion.toByteString
private const val NATIVE_PKCE_FLOW = "native_pkce"
private const val CALLBACK_PATH = "/callback"
private const val TOKEN_KEY = "dashboard_native_tokens_json"
private const val NATIVE_AUTH_DNS_RETRY_BACKOFF_MILLIS = 75L
private val JSON_MEDIA = "application/json; charset=utf-8".toMediaType()
@Serializable
@@ -91,6 +103,8 @@ class NativeDashboardAuthClient(
baseUrl: String,
private val tokenStore: NativeDashboardTokenStore,
private val client: OkHttpClient = OkHttpClient.Builder()
.dns(RetryingNativeAuthDns())
.retryOnConnectionFailure(false)
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
@@ -271,12 +285,19 @@ class NativeDashboardAuthClient(
}
throw NativeDashboardAuthHttpException(response.code)
}
val body = response.body?.string().orEmpty()
val body = response.body.string()
runCatching { json.decodeFromString<NativeDashboardTokens>(body) }
.getOrElse { throw IOException("Dashboard token response was malformed", it) }
.getOrElse {
throw NativeDashboardTokenShapeException(
"Dashboard token response was malformed",
it,
)
}
.also {
if (it.accessToken.isBlank()) {
throw IOException("Dashboard token response did not include an access token")
throw NativeDashboardTokenShapeException(
"Dashboard token response did not include an access token",
)
}
}
}
@@ -285,7 +306,7 @@ class NativeDashboardAuthClient(
NativeTokenRefreshCoordinator.currentGeneration(tokenStore.coordinationKey) !=
expectedGeneration
) {
throw IOException("Dashboard sign-in is no longer active")
throw NativeDashboardInactiveAuthorizationException()
}
tokenStore.save(tokens)
}
@@ -312,6 +333,42 @@ class NativeDashboardAuthClient(
}
}
/**
* Retries only the name lookup that precedes a native-auth request. The HTTP
* call itself remains single-shot, so a one-time authorization code is never
* replayed after the server may have consumed it.
*/
internal class RetryingNativeAuthDns(
private val delegate: Dns = Dns.SYSTEM,
private val backoffMillis: Long = NATIVE_AUTH_DNS_RETRY_BACKOFF_MILLIS,
private val sleeper: (Long) -> Unit = { Thread.sleep(it) },
) : Dns {
override fun lookup(hostname: String): List<InetAddress> {
val firstFailure = try {
return delegate.lookup(hostname)
} catch (error: UnknownHostException) {
error
}
if (backoffMillis > 0L) {
try {
sleeper(backoffMillis)
} catch (interrupted: InterruptedException) {
Thread.currentThread().interrupt()
firstFailure.addSuppressed(interrupted)
throw firstFailure
}
}
return try {
delegate.lookup(hostname)
} catch (secondFailure: UnknownHostException) {
secondFailure.addSuppressed(firstFailure)
throw secondFailure
}
}
}
internal class NativeDashboardCallbackException(
message: String,
val retryable: Boolean = true,
@@ -430,10 +487,56 @@ class DashboardBearerAuth(
}
}
private class NativeDashboardAuthHttpException(
internal class NativeDashboardAuthHttpException(
val statusCode: Int,
) : IOException("Dashboard native authentication failed (HTTP $statusCode)")
internal class NativeDashboardTokenShapeException(
message: String,
cause: Throwable? = null,
) : IOException(message, cause)
internal class NativeDashboardInactiveAuthorizationException :
IOException("Dashboard sign-in is no longer active")
internal fun nativeDashboardSignInFailureStage(error: Throwable): String {
error.firstCauseOfType<NativeDashboardCallbackException>()?.let { return "callback_error" }
error.firstCauseOfType<NativeDashboardAuthHttpException>()?.let {
return "token_http_${it.statusCode}"
}
if (error.firstCauseOfType<NativeDashboardTokenShapeException>() != null) return "token_shape"
if (error.firstCauseOfType<NativeDashboardInactiveAuthorizationException>() != null) {
return "inactive_generation"
}
if (error.firstCauseOfType<InterruptedIOException>() != null) return "token_transport_timeout"
if (error.firstCauseOfType<UnknownHostException>() != null) return "token_transport_dns"
if (error.firstCauseOfType<ConnectException>() != null ||
error.firstCauseOfType<NoRouteToHostException>() != null
) {
return "token_transport_connect"
}
if (error.firstCauseOfType<SSLException>() != null) return "token_transport_tls"
if (error.firstCauseOfType<SocketException>() != null ||
error.firstCauseOfType<EOFException>() != null
) {
return "token_transport_socket"
}
return if (error.firstCauseOfType<IOException>() != null) "token_transport" else "token_store"
}
internal fun nativeDashboardSignInFailureDiagnostic(error: Throwable): String =
"dashboard_native_pkce_failed stage=${nativeDashboardSignInFailureStage(error)}"
private inline fun <reified T : Throwable> Throwable.firstCauseOfType(): T? {
val seen = Collections.newSetFromMap(IdentityHashMap<Throwable, Boolean>())
var current: Throwable? = this
while (current != null && seen.add(current)) {
if (current is T) return current
current = current.cause
}
return null
}
private object NativeTokenRefreshCoordinator {
private val locks = ConcurrentHashMap<String, Any>()
private val generations = ConcurrentHashMap<String, Long>()
@@ -1,5 +1,6 @@
package com.hermesandroid.relay.network.upstream
import com.hermesandroid.relay.BuildConfig
import java.io.IOException
import java.io.InputStream
import java.net.InetAddress
@@ -7,6 +8,7 @@ import java.net.InetSocketAddress
import java.net.ServerSocket
import java.net.Socket
import java.net.SocketTimeoutException
import java.security.MessageDigest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.currentCoroutineContext
@@ -14,12 +16,44 @@ import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import okio.ByteString.Companion.toByteString
private const val CALLBACK_PATH = "/callback"
private const val MAX_REQUEST_LINE_BYTES = 8 * 1024
private const val MAX_HEADER_BYTES = 16 * 1024
private const val ACCEPT_POLL_MILLIS = 500
internal const val DEFAULT_NATIVE_SIGN_IN_TIMEOUT_MILLIS = 2 * 60 * 1000L
internal val NATIVE_SIGN_IN_RETURN_URI = "${BuildConfig.APPLICATION_ID}://return"
private enum class CallbackPage(
val modifier: String,
val eyebrow: String,
val title: String,
val message: String,
val guidance: String,
) {
Success(
modifier = "success",
eyebrow = "Secure sign-in",
title = "Sign-in complete",
message = "Your secure session is ready in Hermes Relay.",
guidance = "Return to Hermes Relay to continue.",
),
Failure(
modifier = "failure",
eyebrow = "Secure sign-in",
title = "Sign-in needs another try",
message = "No session details were saved from this attempt.",
guidance = "Return to Hermes Relay and start sign-in again.",
),
Rejected(
modifier = "rejected",
eyebrow = "Protected callback",
title = "Callback not accepted",
message = "Hermes Relay ignored this request to protect your sign-in.",
guidance = "Return to the app and continue the sign-in already in progress.",
),
}
internal enum class DashboardRedirectAuthMode {
NativePkce,
@@ -119,7 +153,7 @@ class NativeDashboardSignInCoordinator(
writeResponse(
socket,
status = "403 Forbidden",
body = "This sign-in callback was not accepted.",
page = CallbackPage.Rejected,
)
return@use null
}
@@ -132,7 +166,7 @@ class NativeDashboardSignInCoordinator(
writeResponse(
socket,
status = "400 Bad Request",
body = "This sign-in callback was not accepted.",
page = CallbackPage.Rejected,
)
return@use null
}
@@ -145,7 +179,7 @@ class NativeDashboardSignInCoordinator(
writeResponse(
socket,
status = "200 OK",
body = "Sign-in complete. You can return to Hermes Relay.",
page = CallbackPage.Success,
)
}
} catch (error: NativeDashboardCallbackException) {
@@ -153,21 +187,21 @@ class NativeDashboardSignInCoordinator(
writeResponse(
socket,
status = "400 Bad Request",
body = "This sign-in callback was not accepted.",
page = CallbackPage.Rejected,
)
return@use null
}
writeResponse(
socket,
status = "400 Bad Request",
body = "Sign-in could not be completed. Return to Hermes Relay and try again.",
page = CallbackPage.Failure,
)
throw error
} catch (error: Exception) {
writeResponse(
socket,
status = "400 Bad Request",
body = "Sign-in could not be completed. Return to Hermes Relay and try again.",
page = CallbackPage.Failure,
)
throw error
}
@@ -237,17 +271,20 @@ class NativeDashboardSignInCoordinator(
throw IOException("Native sign-in callback line was too large")
}
private fun writeResponse(socket: Socket, status: String, body: String) {
val html = """
<!doctype html>
<html><head><meta name="viewport" content="width=device-width,initial-scale=1"></head>
<body><p>${escapeHtml(body)}</p></body></html>
""".trimIndent().toByteArray(Charsets.UTF_8)
private fun writeResponse(socket: Socket, status: String, page: CallbackPage) {
val html = callbackPageHtml(page).toByteArray(Charsets.UTF_8)
val headers = buildString {
append("HTTP/1.1 ").append(status).append("\r\n")
append("Content-Type: text/html; charset=utf-8\r\n")
append("Content-Length: ").append(html.size).append("\r\n")
append("Cache-Control: no-store\r\n")
append("Pragma: no-cache\r\n")
append("Expires: 0\r\n")
append("Content-Security-Policy: ").append(CALLBACK_CSP).append("\r\n")
append("Referrer-Policy: no-referrer\r\n")
append("X-Content-Type-Options: nosniff\r\n")
append("X-Frame-Options: DENY\r\n")
append("Permissions-Policy: camera=(), geolocation=(), microphone=(), payment=(), usb=()\r\n")
append("Connection: close\r\n\r\n")
}.toByteArray(Charsets.US_ASCII)
runCatching {
@@ -259,8 +296,63 @@ class NativeDashboardSignInCoordinator(
}
}
private fun escapeHtml(value: String): String =
value.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
private fun callbackPageHtml(page: CallbackPage): String = """
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<meta name="color-scheme" content="dark">
<title>${page.title} · Hermes Relay</title>
<style>$CALLBACK_STYLE</style>
</head>
<body>
<main class="shell">
<section class="card ${page.modifier}" aria-labelledby="page-title" aria-describedby="page-message page-guidance">
<div class="status-rail" aria-hidden="true"></div>
<p class="brand">Hermes Relay</p>
<p class="eyebrow">${page.eyebrow}</p>
<h1 id="page-title" tabindex="-1">${page.title}</h1>
<p id="page-message" class="message">${page.message}</p>
<p id="page-guidance" class="guidance">${page.guidance}</p>
<a class="return-link" href="$NATIVE_SIGN_IN_RETURN_URI">Return to Hermes Relay</a>
</section>
</main>
</body>
</html>
""".trimIndent()
private companion object {
private val CALLBACK_STYLE = """
:root{color-scheme:dark;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;background:#08090d;color:#f7f4ff}
*{box-sizing:border-box}
body{margin:0;min-height:100vh;min-height:100svh;background:#08090d}
.shell{min-height:100vh;min-height:100svh;display:grid;place-items:center;padding:max(24px,env(safe-area-inset-top)) max(20px,env(safe-area-inset-right)) max(24px,env(safe-area-inset-bottom)) max(20px,env(safe-area-inset-left))}
.card{position:relative;width:min(100%,460px);overflow:hidden;border:1px solid #2d2938;border-radius:18px;background:#111219;padding:32px 28px 28px;box-shadow:0 18px 48px rgba(0,0,0,.34);animation:card-in 220ms ease-out both}
.status-rail{position:absolute;inset:0 auto 0 0;width:4px;background:#9b6bf0}
.success .status-rail{background:#55d98b}.failure .status-rail{background:#ff7188}
.brand{margin:0 0 28px;color:#bba4f5;font-size:.78rem;font-weight:750;letter-spacing:.12em;text-transform:uppercase}
.eyebrow{margin:0 0 8px;color:#cac4d8;font-size:.9rem;font-weight:650}
h1{margin:0;color:#fff;font-size:clamp(1.8rem,8vw,2.45rem);font-weight:720;letter-spacing:-.035em;line-height:1.08;outline:none}
h1:focus-visible{outline:2px solid #9b6bf0;outline-offset:6px;border-radius:3px}
.message{margin:18px 0 0;color:#eeeaf7;font-size:1.04rem;line-height:1.6}
.guidance{margin:12px 0 0;color:#bdb7c9;font-size:.95rem;line-height:1.55}
.return-link{display:block;width:100%;margin-top:26px;border:1px solid #8c5ef0;border-radius:12px;background:#6b35e8;color:#fff;padding:13px 18px;font:inherit;font-weight:700;text-align:center;text-decoration:none;cursor:pointer}
.return-link:hover{border-color:#c4a8ff}.return-link:focus-visible{outline:3px solid #c4a8ff;outline-offset:3px}.return-link:active{background:#5d28d3}
@keyframes card-in{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:none}}
@media (prefers-reduced-motion:reduce){*,*::before,*::after{animation:none!important;scroll-behavior:auto!important;transition:none!important}}
@media (max-width:380px){.card{padding:28px 22px 24px;border-radius:16px}}
""".trimIndent()
private fun cspHash(value: String): String = MessageDigest.getInstance("SHA-256")
.digest(value.toByteArray(Charsets.UTF_8))
.toByteString()
.base64()
private val CALLBACK_CSP = buildString {
append("default-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'; ")
append("img-src 'none'; connect-src 'none'; object-src 'none'; script-src 'none'; ")
append("style-src 'sha256-").append(cspHash(CALLBACK_STYLE)).append("'")
}
}
}
@@ -0,0 +1,102 @@
package com.hermesandroid.relay.network.upstream
/** Canonical reasoning-effort values accepted by upstream Hermes. */
object ReasoningEfforts {
const val DEFAULT = "medium"
val canonical: List<String> =
listOf("none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra")
fun normalize(value: String?): String {
val normalized = value?.trim()?.lowercase().orEmpty()
return normalized.takeIf { it in canonical } ?: DEFAULT
}
}
/** Provider/model capability row advertised by upstream `model.options`. */
data class GatewayModelCapabilities(
/** Legacy capability flag. Null means the server did not advertise support either way. */
val reasoning: Boolean? = null,
/** Exact selectable values on newer servers. Null means use the canonical compatibility list. */
val reasoningEfforts: List<String>? = null,
/** Explicit false means the advertised list is advisory rather than selectable. */
val reasoningEffortsExact: Boolean? = null,
)
data class ReasoningEffortAvailability(
val supported: Boolean?,
val choices: List<String>,
val exact: Boolean,
) {
fun accepts(effort: String): Boolean = supported != false && (!exact || effort in choices)
}
/** Exact provider/model identity to which a confirmed effort belongs. */
data class ReasoningEffortIdentity(val provider: String, val model: String)
/**
* Resolve the active provider/model's advertised reasoning contract.
*
* Older servers expose either no capability entry or only `reasoning: true`;
* those receive the full canonical compatibility list. An explicit false
* disables the control. A `reasoning_efforts` array is authoritative.
*/
fun resolveReasoningEffortAvailability(
providers: List<GatewayModelProvider>,
provider: String?,
model: String?,
relayCapabilities: Map<ReasoningEffortIdentity, GatewayModelCapabilities> = emptyMap(),
): ReasoningEffortAvailability {
val normalizedProvider = provider?.trim().orEmpty()
val normalizedModel = model?.trim().orEmpty()
if (normalizedProvider.isEmpty() || normalizedModel.isEmpty()) {
return ReasoningEffortAvailability(
supported = null,
choices = ReasoningEfforts.canonical,
exact = false,
)
}
val providerRow = providers.firstOrNull {
it.slug.equals(normalizedProvider, ignoreCase = true)
}
val upstream = providerRow?.capabilities?.get(normalizedModel)
?: providerRow?.capabilities?.entries?.firstOrNull {
it.key.equals(normalizedModel, ignoreCase = true)
}?.value
val identity = ReasoningEffortIdentity(
provider = normalizedProvider.lowercase(),
model = normalizedModel,
)
val relay = relayCapabilities[identity]
fun exactAvailability(capabilities: GatewayModelCapabilities): ReasoningEffortAvailability {
val choices = capabilities.reasoningEfforts.orEmpty()
.map { it.trim().lowercase() }
.filter { it in ReasoningEfforts.canonical }
.distinct()
return ReasoningEffortAvailability(
supported = capabilities.reasoning ?: choices.isNotEmpty(),
choices = choices,
exact = true,
)
}
// Contract precedence: authoritative upstream, authoritative Relay overlay,
// explicit upstream suppression, then compatibility fallback.
if (upstream?.reasoningEfforts != null && upstream.reasoningEffortsExact == true) {
return exactAvailability(upstream)
}
if (relay?.reasoningEfforts != null && relay.reasoningEffortsExact == true) {
return exactAvailability(relay)
}
if (upstream?.reasoning == false) {
return ReasoningEffortAvailability(supported = false, choices = emptyList(), exact = false)
}
return ReasoningEffortAvailability(
supported = upstream?.reasoning,
choices = ReasoningEfforts.canonical,
exact = false,
)
}
@@ -0,0 +1,52 @@
package com.hermesandroid.relay.network.upstream
import com.hermesandroid.relay.data.ChatMessage
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
/**
* The publication boundary for Chat and Voice transcript rows.
*
* [ChatMessage.id] may change when the server adopts a client-created row,
* while [ChatMessage.uiKey] is that row's stable render identity. Every value
* emitted from this state therefore has exactly one row per render identity.
*/
internal class RenderedMessageState(initialValue: List<ChatMessage>) {
private val mutable = MutableStateFlow(normalizeRenderedMessages(initialValue))
val flow: StateFlow<List<ChatMessage>> = mutable.asStateFlow()
var value: List<ChatMessage>
get() = mutable.value
set(value) {
mutable.value = normalizeRenderedMessages(value)
}
fun update(transform: (List<ChatMessage>) -> List<ChatMessage>) {
mutable.update { current -> normalizeRenderedMessages(transform(current)) }
}
}
/**
* Coalesce aliases before they can escape to keyed Compose consumers.
*
* The first slot owns transcript position and the latest snapshot owns row
* state. This is the same ordering contract used for replayed server history.
*/
internal fun normalizeRenderedMessages(messages: List<ChatMessage>): List<ChatMessage> {
if (messages.size < 2) return messages
val firstSlotByUiKey = HashMap<String, Int>()
val normalized = ArrayList<ChatMessage>(messages.size)
for (message in messages) {
val existingSlot = firstSlotByUiKey[message.uiKey]
if (existingSlot == null) {
firstSlotByUiKey[message.uiKey] = normalized.size
normalized += message
} else {
normalized[existingSlot] = message
}
}
return if (normalized.size == messages.size) messages else normalized
}
@@ -0,0 +1,78 @@
package com.hermesandroid.relay.network.upstream
import com.hermesandroid.relay.network.upstream.models.MessageItem
import com.hermesandroid.relay.network.upstream.models.MessagePagination
import kotlinx.coroutines.CancellationException
/** Explicit transcript read intent for Hermes' bounded messages endpoint. */
enum class SessionMessageLoadMode {
/** One bounded newest-first window, returned in chronological order. */
LATEST,
/** Every page, oldest first, subject to Android memory safety bounds. */
COMPLETE,
}
internal const val SESSION_MESSAGE_PAGE_SIZE = 500
private const val MAX_COMPLETE_TRANSCRIPT_MESSAGES = 50_000
private const val MAX_COMPLETE_TRANSCRIPT_PAYLOAD_CHARS = 32_000_000
internal data class SessionMessagePageRequest(
val limit: Int = SESSION_MESSAGE_PAGE_SIZE,
val offset: Int = 0,
val order: String,
)
internal data class SessionMessagePage(
val messages: List<MessageItem>,
val pagination: MessagePagination?,
val payloadChars: Int,
)
internal class SessionTranscriptTooLargeException(message: String) : IllegalStateException(message)
/** Shared API-server/dashboard pagination contract. Legacy unpaginated envelopes remain valid. */
internal suspend fun loadSessionMessages(
mode: SessionMessageLoadMode,
fetchPage: suspend (SessionMessagePageRequest) -> Result<SessionMessagePage>,
): Result<List<MessageItem>> {
return try {
val order = if (mode == SessionMessageLoadMode.LATEST) "latest" else "oldest"
val collected = ArrayList<MessageItem>()
var offset = 0
var payloadChars = 0L
while (true) {
val request = SessionMessagePageRequest(offset = offset, order = order)
val page = fetchPage(request).getOrThrow()
payloadChars += page.payloadChars
if (payloadChars > MAX_COMPLETE_TRANSCRIPT_PAYLOAD_CHARS) {
throw SessionTranscriptTooLargeException(
"Session transcript exceeds Android's 32 MB safe-load limit",
)
}
if (collected.size + page.messages.size > MAX_COMPLETE_TRANSCRIPT_MESSAGES) {
throw SessionTranscriptTooLargeException(
"Session transcript exceeds Android's 50,000-message safe-load limit",
)
}
collected += page.messages
if (mode == SessionMessageLoadMode.LATEST) break
// Older Hermes returned one unpaginated complete envelope. Never issue
// a speculative second request against that contract.
val pagination = page.pagination ?: break
val returned = pagination.returned ?: page.messages.size
if (page.messages.isEmpty() || returned < request.limit || page.messages.size < request.limit) break
val nextOffset = offset + page.messages.size
if (nextOffset <= offset) break
offset = nextOffset
}
Result.success(collected)
} catch (error: CancellationException) {
throw error
} catch (error: Throwable) {
Result.failure(error)
}
}
@@ -165,6 +165,9 @@ data class SessionItem(
@SerialName("input_tokens") val inputTokens: Int? = null,
@SerialName("output_tokens") val outputTokens: Int? = null,
@SerialName("has_model_config") val hasModelConfig: Boolean = false,
/** Durable flags returned by current Dashboard and API-server session resources. */
val pinned: Boolean = false,
val archived: Boolean = false,
) {
val resolvedLastActivity: Double?
get() = lastActive ?: lastActivity ?: lastActivityAt ?: updatedAt
@@ -241,7 +244,16 @@ data class MessageListResponse(
val items: List<MessageItem>? = null,
val messages: List<MessageItem>? = null, // alternate key
val data: List<MessageItem>? = null, // upstream /api/sessions/{id}/messages list envelope
val total: Int? = null
val total: Int? = null,
val pagination: MessagePagination? = null,
)
@Serializable
data class MessagePagination(
val limit: Int? = null,
val offset: Int? = null,
val order: String? = null,
val returned: Int? = null,
)
@Serializable
@@ -0,0 +1,123 @@
package com.hermesandroid.relay.reliability
import android.content.Context
import android.os.Build
import com.hermesandroid.relay.BuildConfig
import java.io.PrintWriter
import java.io.StringWriter
import java.time.Instant
import java.util.concurrent.Executors
/**
* Android boundary for the local reliability store. Nothing in this object has
* a network path; writes stay in app-private storage until a user explicitly
* reviews and shares text through the UI.
*/
object ReliabilityCenter {
private val writer = Executors.newSingleThreadExecutor { runnable ->
Thread(runnable, "hermes-reliability-writer").apply { isDaemon = true }
}
private val appSessionId = ReliabilityReport.newId("app")
@Volatile
private var store: ReliabilityStore? = null
fun initialize(context: Context) {
if (store != null) return
synchronized(this) {
if (store == null) {
store = ReliabilityStore(
java.io.File(context.applicationContext.filesDir, "reliability/reports-v1.json"),
)
}
}
}
fun recordFatal(
context: Context,
throwable: Throwable,
threadName: String,
timeIso: String = Instant.now().toString(),
): ReliabilityReport {
initialize(context)
val summary = buildString {
append(throwable.javaClass.simpleName.ifBlank { "Unexpected crash" })
throwable.message?.takeIf { it.isNotBlank() }?.let { append(": ").append(it) }
}
val report = ReliabilityReport(
reportId = ReliabilityReport.newId(),
appSessionId = appSessionId,
timeIso = timeIso,
kind = ReliabilityKind.FatalCrash,
owner = ReliabilityOwner.Android,
severity = ReliabilitySeverity.Fatal,
summary = summary,
recovery = "The app restarted. Work already running on Hermes may still be active.",
reportRecommended = true,
technicalDetail = "Thread: $threadName\n${stackTraceText(throwable)}",
environment = environment(),
pendingReview = true,
)
// Fatal capture must complete before the platform terminates the process.
store?.append(report)
return report
}
fun recordHandled(
title: String,
detail: String?,
throwable: Throwable,
context: String?,
routeRole: String? = null,
) {
val target = store ?: return
val classification = ReliabilityClassifier.classify(throwable, context)
if (!classification.shouldPersist) return
val report = ReliabilityReport(
reportId = ReliabilityReport.newId(),
appSessionId = appSessionId,
timeIso = Instant.now().toString(),
kind = classification.kind,
owner = classification.owner,
severity = ReliabilitySeverity.Error,
summary = title,
recovery = detail ?: "The failure was handled; retry or review Diagnostics if it continues.",
reportRecommended = classification.reportRecommended,
technicalDetail = stackTraceText(throwable),
routeRole = routeRole,
environment = environment(),
)
writer.execute { runCatching { target.append(report) } }
}
fun reports(context: Context): List<ReliabilityReport> {
initialize(context)
return store?.readAll().orEmpty()
}
fun pendingCrash(context: Context): ReliabilityReport? =
reports(context).lastOrNull { it.kind == ReliabilityKind.FatalCrash && it.pendingReview }
fun markReviewed(context: Context, reportId: String) {
initialize(context)
store?.markReviewed(reportId)
}
fun import(context: Context, report: ReliabilityReport) {
initialize(context)
store?.append(report)
}
fun environment(): ReliabilityEnvironment = ReliabilityEnvironment(
versionName = BuildConfig.VERSION_NAME,
versionCode = BuildConfig.VERSION_CODE,
flavor = BuildConfig.FLAVOR,
manufacturer = Build.MANUFACTURER.orEmpty().ifBlank { "?" },
model = Build.MODEL.orEmpty().ifBlank { "?" },
androidRelease = Build.VERSION.RELEASE.orEmpty().ifBlank { "?" },
sdkInt = Build.VERSION.SDK_INT,
)
private fun stackTraceText(throwable: Throwable): String =
StringWriter().also { throwable.printStackTrace(PrintWriter(it)) }.toString().trim()
}
@@ -0,0 +1,361 @@
package com.hermesandroid.relay.reliability
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File
import java.time.Instant
import java.util.UUID
import java.util.concurrent.CancellationException
const val RELIABILITY_SCHEMA_VERSION = 1
@Serializable
enum class ReliabilityKind {
FatalCrash,
AnrSignal,
RecoverableProductError,
Connectivity,
Authentication,
RateLimit,
ServiceUnavailable,
ExpectedCancellation,
UserDenial,
}
@Serializable
enum class ReliabilityOwner(val label: String) {
Android("Android"),
Dashboard("Dashboard"),
Api("API"),
Relay("Relay"),
UpstreamGateway("Upstream Gateway"),
Voice("Voice"),
Unknown("Unknown"),
}
@Serializable
enum class ReliabilitySeverity { Info, Warning, Error, Fatal }
@Serializable
data class ReliabilityEnvironment(
val versionName: String,
val versionCode: Int,
val flavor: String,
val manufacturer: String,
val model: String,
val androidRelease: String,
val sdkInt: Int,
)
/**
* Allowlisted local reliability record. There are deliberately no fields for
* prompts, messages, profile names, product session IDs, URLs, media, or paths.
*/
@Serializable
data class ReliabilityReport(
val schemaVersion: Int = RELIABILITY_SCHEMA_VERSION,
val reportId: String,
val appSessionId: String,
val timeIso: String,
val kind: ReliabilityKind,
val owner: ReliabilityOwner,
val severity: ReliabilitySeverity,
val summary: String,
val recovery: String,
val reportRecommended: Boolean,
val technicalDetail: String? = null,
val routeRole: String? = null,
val environment: ReliabilityEnvironment,
val pendingReview: Boolean = false,
) {
fun shortTitle(): String = summary.lineSequence().firstOrNull().orEmpty().ifBlank {
kind.name
}.take(90)
fun versionLine(): String =
"${environment.versionName} (code ${environment.versionCode}) ${environment.flavor}"
fun environmentBlock(): String = buildString {
appendLine("- Hermes-Relay version/tag: ${environment.versionName} (code ${environment.versionCode})")
appendLine(
"- Install surface: " +
if (environment.flavor.equals("sideload", ignoreCase = true)) "sideload APK" else "Google Play",
)
appendLine(
"- Android device and OS: ${environment.manufacturer} ${environment.model} — " +
"Android ${environment.androidRelease} (SDK ${environment.sdkInt})",
)
append("- Connection mode: ${routeRole ?: "unknown"}")
}
/** Exact local review/copy/share payload. Redaction is repeated for legacy defense in depth. */
fun toPlainText(): String = ReliabilityRedactor.redact(
buildString {
appendLine("Hermes-Relay support information")
appendLine("Report: $reportId")
appendLine("Session: $appSessionId")
appendLine("Time: $timeIso")
appendLine("Type: ${kind.name}")
appendLine("Owner: ${owner.label}")
appendLine("App: ${versionLine()}")
appendLine(
"Device: ${environment.manufacturer} ${environment.model} — " +
"Android ${environment.androidRelease} (SDK ${environment.sdkInt})",
)
routeRole?.let { appendLine("Route: $it") }
appendLine()
appendLine("What happened: $summary")
appendLine("Recovery: $recovery")
technicalDetail?.let {
appendLine()
appendLine("Technical detail (redacted)")
append(it)
}
},
)
companion object {
fun newId(prefix: String = "rpt"): String =
"$prefix-${UUID.randomUUID().toString().replace("-", "").take(16)}"
}
}
/** Old `files/crash/last-crash.json` shape, retained only for one-way migration. */
@Serializable
data class LegacyCrashSnapshot(
val timeIso: String,
val versionName: String,
val versionCode: Int,
val flavor: String,
val manufacturer: String,
val model: String,
val androidRelease: String,
val sdkInt: Int,
val threadName: String,
val exceptionSummary: String,
val stackTrace: String,
)
fun migrateLegacyCrash(
old: LegacyCrashSnapshot,
reportId: String = ReliabilityReport.newId(),
appSessionId: String = ReliabilityReport.newId("legacy"),
): ReliabilityReport = ReliabilityReport(
reportId = reportId,
appSessionId = appSessionId,
timeIso = runCatching { Instant.parse(old.timeIso).toString() }.getOrDefault(old.timeIso),
kind = ReliabilityKind.FatalCrash,
owner = ReliabilityOwner.Android,
severity = ReliabilitySeverity.Fatal,
summary = ReliabilityRedactor.redact(old.exceptionSummary, 240),
recovery = "The app restarted. Work already running on Hermes may still be active.",
reportRecommended = true,
technicalDetail = ReliabilityRedactor.redact("Thread: ${old.threadName}\n${old.stackTrace}"),
environment = ReliabilityEnvironment(
old.versionName, old.versionCode, old.flavor, old.manufacturer, old.model,
old.androidRelease, old.sdkInt,
),
pendingReview = true,
)
data class ReliabilityClassification(
val kind: ReliabilityKind,
val owner: ReliabilityOwner,
val reportRecommended: Boolean,
val shouldPersist: Boolean,
)
object ReliabilityClassifier {
fun classify(throwable: Throwable, context: String? = null): ReliabilityClassification {
val message = throwable.message.orEmpty().lowercase()
val owner = ownerForContext(context)
return when {
throwable is CancellationException -> ReliabilityClassification(
ReliabilityKind.ExpectedCancellation, owner, reportRecommended = false, shouldPersist = false,
)
throwable is SecurityException && ("denied" in message || "permission" in message) ->
ReliabilityClassification(
ReliabilityKind.UserDenial, ReliabilityOwner.Android,
reportRecommended = false, shouldPersist = false,
)
"429" in message || "rate limit" in message || "too many requests" in message ->
ReliabilityClassification(
ReliabilityKind.RateLimit, owner, reportRecommended = false, shouldPersist = true,
)
"401" in message || "403" in message || "unauthorized" in message || "forbidden" in message ->
ReliabilityClassification(
ReliabilityKind.Authentication, owner, reportRecommended = false, shouldPersist = true,
)
throwable is java.net.UnknownHostException ||
throwable is java.net.ConnectException ||
throwable is java.net.SocketTimeoutException ||
"timeout" in message -> ReliabilityClassification(
ReliabilityKind.Connectivity, owner, reportRecommended = false, shouldPersist = true,
)
"503" in message || "service unavailable" in message || "gateway_draining" in message ->
ReliabilityClassification(
ReliabilityKind.ServiceUnavailable, owner,
reportRecommended = false, shouldPersist = true,
)
else -> ReliabilityClassification(
ReliabilityKind.RecoverableProductError, owner,
reportRecommended = true, shouldPersist = true,
)
}
}
fun ownerForContext(context: String?): ReliabilityOwner = when (context?.lowercase()) {
"dashboard", "manage", "dashboard_auth" -> ReliabilityOwner.Dashboard
"gateway", "gateway_chat", "upstream_gateway" -> ReliabilityOwner.UpstreamGateway
"transcribe", "synthesize", "voice_config", "record", "voice" -> ReliabilityOwner.Voice
"pair", "save_and_test", "media_fetch", "relay" -> ReliabilityOwner.Relay
"send_message", "load_sessions", "create_session", "api" -> ReliabilityOwner.Api
"android", "permission", "ui" -> ReliabilityOwner.Android
else -> ReliabilityOwner.Unknown
}
}
/** Local, deterministic redaction. It runs before persistence and again before export. */
object ReliabilityRedactor {
const val MAX_TECHNICAL_LENGTH = 8_000
private const val HIDDEN = "[hidden]"
private val secretAssignment = Regex(
"""(?i)\b(authorization|bearer|cookie|set-cookie|token|api[_-]?key|session[_-]?token|pairing[_-]?code|password|secret|oauth[_-]?code)\s*[:=]\s*((?:Bearer\s+)?[^\s,;]+)""",
)
private val sensitiveHeader = Regex("""(?im)^\s*(authorization|cookie|set-cookie)\s*:\s*.+$""")
private val standaloneBearer = Regex("""(?i)\bBearer\s+[A-Za-z0-9._~+/=-]+""")
private val sensitivePayload = Regex(
"""(?i)\b(prompt|message|content|transcript|reasoning|tool[_-]?(args|result)|profile[_-]?name)\s*[:=]\s*([^\r\n]+)""",
)
private val url = Regex("""(?i)\b(?:https?|wss?)://[^\s)\]}>,]+""")
private val ipv4 = Regex("""(?<![\w.])(?:\d{1,3}\.){3}\d{1,3}(?::\d+)?(?![\w.])""")
private val uuid = Regex("""(?i)\b[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b""")
private val email = Regex("""(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b""")
private val namedHost = Regex("""(?i)\b(host|hostname)\s*[:=]\s*[^\s,;]+""")
private val unresolvedHost = Regex("""(?i)(?:resolve|resolved|host)\s+[\"']([^\"']+)[\"']""")
private val windowsPath = Regex("""(?i)\b[A-Z]:\\(?:[^\s\\]+\\)+[^\s]+""")
private val unixPrivatePath = Regex("""(?i)(?:/home/|/Users/|/data/user/\d+/|/sdcard/)[^\s]+""")
fun redact(value: String, maxLength: Int = MAX_TECHNICAL_LENGTH): String {
var result = value
result = sensitiveHeader.replace(result) { "${it.groupValues[1]}: $HIDDEN" }
result = secretAssignment.replace(result) { "${it.groupValues[1]}=$HIDDEN" }
result = standaloneBearer.replace(result, "Bearer $HIDDEN")
result = sensitivePayload.replace(result) { "${it.groupValues[1]}=$HIDDEN" }
result = url.replace(result, "[url hidden]")
result = ipv4.replace(result, "[host hidden]")
result = uuid.replace(result, "[id hidden]")
result = email.replace(result, "[email hidden]")
result = namedHost.replace(result) { "${it.groupValues[1]}=$HIDDEN" }
result = unresolvedHost.replace(result, "host \"$HIDDEN\"")
result = windowsPath.replace(result, "[path hidden]")
result = unixPrivatePath.replace(result, "[path hidden]")
return if (result.length > maxLength) {
result.take(maxLength) + "\n… (truncated)"
} else {
result
}
}
}
@Serializable
private data class ReliabilityEnvelope(
val schemaVersion: Int = RELIABILITY_SCHEMA_VERSION,
val reports: List<ReliabilityReport> = emptyList(),
)
/** Pure file store so retention, bounds, and migration behavior are JVM-testable. */
class ReliabilityStore(
private val file: File,
private val maxReports: Int = 20,
private val retentionDays: Long = 14,
) {
private val json = Json { encodeDefaults = true; ignoreUnknownKeys = true }
private val lock = Any()
fun readAll(now: Instant = Instant.now()): List<ReliabilityReport> = synchronized(lock) {
val decoded = decode()
val retained = prune(decoded, now)
if (retained != decoded) write(retained)
retained
}
fun append(report: ReliabilityReport, now: Instant = Instant.now()) = synchronized(lock) {
write(prune(decode() + sanitize(report), now))
}
fun markReviewed(reportId: String, now: Instant = Instant.now()) = synchronized(lock) {
write(
prune(
decode().map { if (it.reportId == reportId) it.copy(pendingReview = false) else it },
now,
),
)
}
private fun sanitize(report: ReliabilityReport): ReliabilityReport = report.copy(
summary = ReliabilityRedactor.redact(report.summary, 240),
recovery = ReliabilityRedactor.redact(report.recovery, 240),
technicalDetail = report.technicalDetail?.let(ReliabilityRedactor::redact),
routeRole = report.routeRole?.let { ReliabilityRedactor.redact(it, 40) },
)
private fun prune(reports: List<ReliabilityReport>, now: Instant): List<ReliabilityReport> {
val cutoff = now.minusSeconds(retentionDays * 24 * 60 * 60)
return reports
.distinctBy { it.reportId }
.filter { report -> runCatching { Instant.parse(report.timeIso) >= cutoff }.getOrDefault(true) }
.sortedBy { it.timeIso }
.takeLast(maxReports.coerceAtLeast(1))
}
private fun decode(): List<ReliabilityReport> = runCatching {
if (!file.isFile) return emptyList()
json.decodeFromString<ReliabilityEnvelope>(file.readText()).reports
}.getOrDefault(emptyList())
private fun write(reports: List<ReliabilityReport>) {
file.parentFile?.mkdirs()
val temp = File(file.parentFile, "${file.name}.tmp")
temp.writeText(json.encodeToString(ReliabilityEnvelope(reports = reports)))
runCatching {
java.nio.file.Files.move(
temp.toPath(),
file.toPath(),
java.nio.file.StandardCopyOption.ATOMIC_MOVE,
java.nio.file.StandardCopyOption.REPLACE_EXISTING,
)
}.recoverCatching {
java.nio.file.Files.move(
temp.toPath(),
file.toPath(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING,
)
}.getOrThrow()
}
}
object SupportBundleBuilder {
const val MAX_REPORTS = 10
fun build(reports: List<ReliabilityReport>): String {
val selected = reports.sortedByDescending { it.timeIso }.take(MAX_REPORTS)
return ReliabilityRedactor.redact(
buildString {
appendLine("Hermes-Relay support bundle")
appendLine("Local-only export · review before sharing")
appendLine("Reports: ${selected.size}")
selected.forEachIndexed { index, report ->
appendLine()
appendLine("===== Report ${index + 1} =====")
append(report.toPlainText())
appendLine()
}
},
maxLength = 64_000,
)
}
}
@@ -176,10 +176,12 @@ internal class HermesRuntimeBinder(
chat.setDisplayProfileProvider { connection.effectiveDisplayProfile.value }
chat.setDisplayAliasProvider { connection.profileDisplayAlias.value }
chat.setProfileSessionLister { connection.listProfileScopedSessions() }
chat.setProfileMessageLoader(connection::loadProfileScopedMessages)
chat.setProfileMessageLoaderWithMode(connection::loadProfileScopedMessages)
chat.setDashboardConfigLoader { connection.loadActiveDashboardConfig() }
chat.profileSessionDeleter = connection::deleteProfileScopedSession
chat.profileSessionRenamer = connection::renameProfileScopedSession
chat.profileSessionPinner = connection::setProfileScopedSessionPinned
chat.profileSessionArchiver = connection::setProfileScopedSessionArchived
chat.onSessionChanged = connection::saveLastSessionId
chat.setDemoModeWiring(
isDemo = { connection.isDemoMode.value },
@@ -59,6 +59,7 @@ import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalWindowInfo
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
@@ -96,13 +97,16 @@ import com.hermesandroid.relay.ui.components.avatar.LocalPetPlaybackSpeed
import com.hermesandroid.relay.ui.components.avatar.LocalPetStabilize
import com.hermesandroid.relay.ui.components.avatar.PetLoader
import com.hermesandroid.relay.ui.components.avatar.SphereAvatar
import com.hermesandroid.relay.ui.components.avatar.resolveBackgroundAvatar
import com.hermesandroid.relay.ui.components.FloatingPetCompanion
import com.hermesandroid.relay.ui.components.shouldCompactFloatingPet
import com.hermesandroid.relay.ui.components.pet.LocalPetCompanionCoordinator
import com.hermesandroid.relay.ui.components.pet.LocalPetSafeAreaRegistry
import com.hermesandroid.relay.ui.components.pet.PetCompanionCoordinator
import com.hermesandroid.relay.ui.components.pet.PetInteractionLayer
import com.hermesandroid.relay.ui.components.pet.PetSafeAreaRegistry
import com.hermesandroid.relay.ui.components.pet.petPerchSurface
import com.hermesandroid.relay.ui.components.pet.platformModalOwnsPetLayer
import com.hermesandroid.relay.ui.components.ConnectionSwitcherSheet
import com.hermesandroid.relay.ui.components.ChatTransportStatusBadge
import com.hermesandroid.relay.ui.components.ChatTransportTier
@@ -135,6 +139,7 @@ import com.hermesandroid.relay.ui.onboarding.OnboardingScreen
import com.hermesandroid.relay.ui.screens.AboutScreen
import com.hermesandroid.relay.ui.screens.AnalyticsScreen
import com.hermesandroid.relay.ui.screens.AppearanceSettingsScreen
import com.hermesandroid.relay.ui.screens.CustomPetGuideScreen
import com.hermesandroid.relay.ui.screens.PetdexBrowseScreen
import com.hermesandroid.relay.ui.screens.BridgeCoreScreen
import com.hermesandroid.relay.ui.screens.DiagnosticsScreen
@@ -272,6 +277,20 @@ internal fun shouldShowConnectionFooter(
presentationMode: VoicePresentationMode,
): Boolean = !voiceMode || presentationMode == VoicePresentationMode.Conversation
/**
* Full-screen setup owns the whole window. Keeping connection-dependent app
* chrome composed behind it makes that chrome rebuild while Add connection
* swaps to its placeholder, visibly resizing the entering setup surface.
*/
internal fun shouldSuppressGlobalChrome(
onboardingCompleted: Boolean,
isDemoMode: Boolean,
currentRoute: String?,
): Boolean =
(!onboardingCompleted && !isDemoMode) ||
currentRoute == Screen.Onboarding.route ||
currentRoute == Screen.Pair.route
internal const val APP_STATUS_PET_WALK_REGION = "app-status-footer"
/**
@@ -451,6 +470,7 @@ sealed class Screen(
data object MediaSettings : Screen("settings/media", "Media", Icons.Filled.Settings)
data object AppearanceSettings : Screen("settings/appearance", "Appearance", Icons.Filled.Settings)
data object PetdexBrowse : Screen("settings/appearance/petdex", "Petdex", Icons.Filled.Settings)
data object CustomPetGuide : Screen("settings/appearance/custom-pet", "Create a pet", Icons.Filled.Settings)
data object Analytics : Screen("settings/analytics", "Analytics", Icons.Filled.Settings)
data object Diagnostics : Screen("settings/diagnostics", "Diagnostics", Icons.Filled.Settings)
data object DeveloperSettings : Screen("settings/developer", "Developer", Icons.Filled.Settings)
@@ -684,16 +704,20 @@ fun RelayApp() {
val appThemeId by connectionViewModel.appTheme.collectAsState()
val fontScale by connectionViewModel.fontScale.collectAsState()
val appFontId by connectionViewModel.appFont.collectAsState()
val appearanceAccent by connectionViewModel.appearanceAccent.collectAsState()
val appearanceShape by connectionViewModel.appearanceShape.collectAsState()
// Resolve the active sphere skin (built-in / adaptive / user-loaded) and
// publish it + the full available set so every MorphingSphere picks it up
// via LocalSphereSkin without per-call-site threading. Adaptive skins read
// the brand lazily inside MorphingSphere, so this can sit outside the theme.
val sphereSkinId by connectionViewModel.sphereSkin.collectAsState()
val appearanceAssetsRefreshTick by connectionViewModel.avatarsRefreshTick.collectAsState()
val sphereContext = androidx.compose.ui.platform.LocalContext.current
val availableSphereSkins by produceState(
initialValue = SphereRegistry.builtIns,
key1 = sphereContext,
key2 = appearanceAssetsRefreshTick,
) {
value = SphereRegistry.builtIns +
withContext(Dispatchers.IO) { SphereSkinLoader.loadUserSkins(sphereContext) }
@@ -706,14 +730,14 @@ fun RelayApp() {
)
}
// Ambient visualization and pet companionship are independent. Existing
// LocalAgentAvatar call sites keep rendering the sphere; a selected pet is
// published separately for the floating companion surface.
// Central/background visualization and pet companionship are independent.
// LocalAgentAvatar owns the central surfaces; LocalFloatingPet owns roaming.
val floatingPetId by connectionViewModel.floatingPet.collectAsState()
val backgroundAvatarId by connectionViewModel.backgroundAvatar.collectAsState()
// Re-scans the pets/ dir whenever the tick bumps (in-app import/delete, or the
// Appearance screen opening), so newly added/removed pets appear everywhere
// without an app restart.
val avatarsRefreshTick by connectionViewModel.avatarsRefreshTick.collectAsState()
val avatarsRefreshTick = appearanceAssetsRefreshTick
val availablePets by produceState(
initialValue = emptyList<AgentAvatar>(),
key1 = sphereContext,
@@ -724,6 +748,10 @@ fun RelayApp() {
val activeFloatingPet = remember(floatingPetId, availablePets) {
availablePets.firstOrNull { it.id == floatingPetId }
}
var floatingPetMenuExpanded by remember(activeFloatingPet?.id) { mutableStateOf(false) }
val activeBackgroundAvatar = remember(backgroundAvatarId, availablePets) {
resolveBackgroundAvatar(backgroundAvatarId, availablePets)
}
val petSpeed by connectionViewModel.petSpeed.collectAsState()
val petStabilize by connectionViewModel.petStabilize.collectAsState()
val petRoamingEnabled by connectionViewModel.petRoamingEnabled.collectAsState()
@@ -804,7 +832,7 @@ fun RelayApp() {
CompositionLocalProvider(
LocalSphereSkin provides activeSphereSkin,
LocalAvailableSphereSkins provides availableSphereSkins,
LocalAgentAvatar provides SphereAvatar,
LocalAgentAvatar provides activeBackgroundAvatar,
// Compatibility list for the existing Appearance picker during the
// transition; new companion UI consumes LocalAvailablePets.
LocalAvailableAvatars provides listOf(SphereAvatar) + availablePets,
@@ -817,11 +845,23 @@ fun RelayApp() {
LocalPetSafeAreaRegistry provides petSafeAreaRegistry,
LocalAgentIconPath provides agentIconPath,
) {
// Dialogs and modal sheets use their own focused window. Treat that
// focus handoff as an app-wide interaction layer so a dock-only pet on
// any route cannot remain visible beneath modal chrome.
PetInteractionLayer(
owner = "platform-modal-window",
active = platformModalOwnsPetLayer(
windowFocused = LocalWindowInfo.current.isWindowFocused,
petMenuExpanded = floatingPetMenuExpanded,
),
)
HermesRelayTheme(
appThemeId = appThemeId,
themePreference = themePreference,
fontScale = fontScale,
appFontId = appFontId,
accentHex = appearanceAccent,
shapeId = appearanceShape,
) {
// Surface a crash report from a previous session, if any. Renders a
// platform Dialog (own window) so tree position is z-order-agnostic;
@@ -907,8 +947,11 @@ fun RelayApp() {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
val isOnboarding = currentRoute == Screen.Onboarding.route
val suppressGlobalChrome = (!onboardingCompleted && !isDemoMode) || isOnboarding
val suppressGlobalChrome = shouldSuppressGlobalChrome(
onboardingCompleted = onboardingCompleted,
isDemoMode = isDemoMode,
currentRoute = currentRoute,
)
// Safety net: landing on a real connect surface (onboarding or the
// Connect/Pair wizard) while demo is still active — via the banner's
@@ -1034,6 +1077,22 @@ fun RelayApp() {
val gatewayCurrentModel by chatViewModel.gatewayCurrentModel.collectAsState()
val appReady by connectionViewModel.isReady.collectAsState()
val initialChatSettled by chatViewModel.initialChatSettled.collectAsState()
// Android sharesheet handoff: wait until the configured chat context is
// settled, then ask ChatViewModel to own the new draft and composer
// prefill. Navigation is presentation-only; no composable writes chat
// stores or sends the shared text.
LaunchedEffect(navController, onboardingCompleted, initialChatSettled) {
if (!onboardingCompleted || !initialChatSettled) return@LaunchedEffect
com.hermesandroid.relay.util.SharedTextRequest.pending.collect { request ->
request ?: return@collect
if (chatViewModel.openSharedTextDraft(request.text)) {
navController.navigate(Screen.Chat.route(openAgentSheet = false)) {
launchSingleTop = true
}
com.hermesandroid.relay.util.SharedTextRequest.consume(request.id)
}
}
}
// The SAME readiness signal ChatScreen renders its "Connect Standard
// Hermes" CTA from (chat client exists + reachable verdict). The gate
// must release on this — releasing on the resolver's earlier
@@ -1640,9 +1699,9 @@ fun RelayApp() {
) { backStackEntry ->
// Responsive bubble width based on screen width. The "Blend"
// chat look favors wider bubbles: on compact phones the cap is
// raised so long turns fill most of the row (binding on the
// available width minus the assistant avatar gutter) instead
// of wrapping early in a narrow column.
// raised so long turns fill most of the row instead of
// wrapping early in a narrow column. Assistant identity
// stays in the group header and does not reduce this cap.
val configuration = LocalConfiguration.current
val screenWidthDp = configuration.screenWidthDp.dp
val maxBubbleWidth = when {
@@ -2458,6 +2517,7 @@ fun RelayApp() {
connectionViewModel = connectionViewModel,
onBack = { navController.popBackStack() },
onBrowsePetdex = { navController.navigate(Screen.PetdexBrowse.route) },
onCreatePet = { navController.navigate(Screen.CustomPetGuide.route) },
)
}
composable(Screen.PetdexBrowse.route) {
@@ -2466,6 +2526,20 @@ fun RelayApp() {
onBack = { navController.popBackStack() },
)
}
composable(Screen.CustomPetGuide.route) {
CustomPetGuideScreen(
connectionViewModel = connectionViewModel,
onBack = { navController.popBackStack() },
onStartNewChat = { prompt ->
chatViewModel.createNewChat()
chatViewModel.stageComposerDraft(prompt)
navController.navigate(Screen.Chat.route(openAgentSheet = false)) {
popUpTo(Screen.Chat.route) { inclusive = false }
launchSingleTop = true
}
},
)
}
composable(Screen.Analytics.route) {
AnalyticsScreen(
connectionViewModel = connectionViewModel,
@@ -2653,6 +2727,7 @@ fun RelayApp() {
onOpenAppearance = {
navController.navigate(Screen.AppearanceSettings.route) { launchSingleTop = true }
},
onMenuExpandedChanged = { floatingPetMenuExpanded = it },
onExitTerrainDebug = {
petTerrainOverlayScope.launch {
FeatureFlags.setPetTerrainOverlayEnabled(sphereContext, false)
@@ -1,15 +1,20 @@
package com.hermesandroid.relay.ui.components
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.hermesandroid.relay.R
import java.io.File
/**
@@ -28,6 +33,15 @@ fun AgentAvatarFace(name: String, letterStyle: TextStyle, modifier: Modifier = M
contentScale = ContentScale.Crop,
modifier = modifier.fillMaxSize(),
)
} else if (name.equals("Hermes", ignoreCase = true)) {
Image(
painter = painterResource(R.drawable.splash_icon),
contentDescription = null,
contentScale = ContentScale.Fit,
modifier = modifier
.fillMaxSize()
.padding(2.dp),
)
} else {
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text(
@@ -332,7 +332,7 @@ private fun decodeGalleryBitmap(bytes: ByteArray): android.graphics.Bitmap? {
sample *= 2
}
val options = BitmapFactory.Options().apply { inSampleSize = sample }
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options)
return decodeOrientedBitmap(bytes, options)
}
private const val GALLERY_COLUMNS = 2
@@ -4,7 +4,6 @@ package com.hermesandroid.relay.ui.components
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.pdf.PdfRenderer
import android.media.audiofx.Visualizer
import android.net.Uri
@@ -697,7 +696,7 @@ private fun ImageBody(
val decoded = withContext(Dispatchers.IO) {
runCatching {
val bytes = attachmentBytes(context, attachment)
bytes?.let { BitmapFactory.decodeByteArray(it, 0, it.size)?.asImageBitmap() }
bytes?.let { decodeOrientedBitmap(it)?.asImageBitmap() }
}.getOrNull()
}
if (decoded != null) bitmap = decoded else failed = true
@@ -304,10 +304,8 @@ private fun DataUrlChatImage(image: ChatInlineImage, maxWidth: Dp) {
BitmapFactory.decodeByteArray(decoded.bytes, 0, decoded.bytes.size, bounds)
val sample = inlineImageSampleSize(bounds.outWidth, bounds.outHeight)
?: return@withInlineImageDecodeLock DataUrlImagePhase.Rejected
val bitmap = BitmapFactory.decodeByteArray(
val bitmap = decodeOrientedBitmap(
decoded.bytes,
0,
decoded.bytes.size,
BitmapFactory.Options().apply {
inSampleSize = sample
inPreferredConfig = android.graphics.Bitmap.Config.ARGB_8888
@@ -482,7 +480,7 @@ private fun RelayServerImage(
is ServerImageResult.Success -> {
val bytes = result.bytes
val bmp = runCatching {
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
decodeOrientedBitmap(bytes)
}.getOrNull()?.asImageBitmap()
if (bmp != null) {
putInlineImage(image.src, bmp, result.sensitive)
@@ -29,6 +29,8 @@ import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Send
import androidx.compose.material.icons.filled.Add
@@ -58,8 +60,18 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusProperties
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.R
@@ -84,6 +96,7 @@ enum class ChatInputTrailing { SEND, VOICE, STOP, STEER, QUEUE }
private val ChatComposerShape = RoundedCornerShape(18.dp)
private val ChatInputChipShape = RoundedCornerShape(12.dp)
internal const val CHAT_INPUT_FIELD_TEST_TAG = "chat-input-field"
data class ChatInputPickerOption(
val label: String,
@@ -167,13 +180,21 @@ fun ChatInputBar(
onModelOptionSelected: (ChatInputPickerOption) -> Unit = {},
onModelPickerClick: (() -> Unit)? = null,
effortControl: ChatInputPickerControl? = null,
onEffortOptionSelected: (ChatInputPickerOption) -> Unit = {},
onEffortPickerClick: (() -> Unit)? = null,
topContent: (@Composable () -> Unit)? = null,
topContentVisible: Boolean = topContent != null,
suppressVoiceTrailing: Boolean = false,
modifier: Modifier = Modifier,
surfaceModifier: Modifier = Modifier,
enabled: Boolean = true,
physicalEnterSends: Boolean = true,
) {
val canSubmit = enabled && trailing in setOf(
ChatInputTrailing.SEND,
ChatInputTrailing.STEER,
ChatInputTrailing.QUEUE,
)
// Keep the last caption around so the AnimatedVisibility exit doesn't
// flash an empty line while collapsing.
var lastCaption by remember { mutableStateOf<String?>(null) }
@@ -202,18 +223,57 @@ fun ChatInputBar(
}
Column(modifier = modifier.fillMaxWidth()) {
// Caption row — correct/queue hinting, single line, no buttons.
// Correction and queueing are materially different actions. Keep the
// explanation, but lead with a visible state pill so the distinction
// does not depend on the trailing icon or accent color alone.
AnimatedVisibility(visible = caption != null) {
Text(
text = caption ?: lastCaption.orEmpty(),
style = relayMetadataStyle(),
color = if (trailing == ChatInputTrailing.STEER) {
MaterialTheme.colorScheme.tertiary.copy(alpha = 0.9f)
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
modifier = Modifier.padding(horizontal = 20.dp, vertical = 2.dp),
)
val detail = caption ?: lastCaption.orEmpty()
val actionLabel = when (trailing) {
ChatInputTrailing.STEER -> stringResource(R.string.chat_input_steer_response)
ChatInputTrailing.QUEUE -> stringResource(R.string.chat_input_queue_message)
else -> null
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 2.dp)
.semantics {
liveRegion = LiveRegionMode.Polite
contentDescription = listOfNotNull(actionLabel, detail)
.joinToString(separator = ". ")
},
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
if (actionLabel != null) {
Text(
text = actionLabel,
style = MaterialTheme.typography.labelSmall,
color = if (trailing == ChatInputTrailing.STEER) {
MaterialTheme.colorScheme.onTertiaryContainer
} else {
MaterialTheme.colorScheme.onSecondaryContainer
},
modifier = Modifier
.clip(RoundedCornerShape(999.dp))
.background(
if (trailing == ChatInputTrailing.STEER) {
MaterialTheme.colorScheme.tertiaryContainer
} else {
MaterialTheme.colorScheme.secondaryContainer
},
)
.padding(horizontal = 8.dp, vertical = 3.dp),
)
}
Text(
text = detail,
style = relayMetadataStyle(),
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
}
// Voice hint pill — floats above the trailing button.
@@ -255,7 +315,8 @@ fun ChatInputBar(
border = BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant),
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 8.dp, vertical = 6.dp),
.padding(horizontal = 8.dp, vertical = 6.dp)
.then(surfaceModifier),
) {
Column {
if (topContent != null) {
@@ -288,9 +349,44 @@ fun ChatInputBar(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 34.dp)
.padding(horizontal = 8.dp, vertical = 4.dp),
.padding(horizontal = 8.dp, vertical = 4.dp)
// Keep directional keys inside the editor. Compose's
// BasicTextField owns normal caret/selection movement;
// cancelling focus traversal prevents a boundary arrow
// from jumping to a neighboring composer control.
.focusProperties {
left = FocusRequester.Cancel
right = FocusRequester.Cancel
up = FocusRequester.Cancel
down = FocusRequester.Cancel
}
.onPreviewKeyEvent { event ->
val native = event.nativeKeyEvent
val isEnter = native.keyCode == android.view.KeyEvent.KEYCODE_ENTER ||
native.keyCode == android.view.KeyEvent.KEYCODE_NUMPAD_ENTER
val isSubmitShortcut = native.isCtrlPressed || native.isMetaPressed
if (native.action != android.view.KeyEvent.ACTION_DOWN || !isEnter) {
false
} else if (isSubmitShortcut || (physicalEnterSends && !native.isShiftPressed)) {
if (canSubmit) onSend()
true
} else {
// Shift+Enter always inserts a newline. When
// Enter is configured for newlines, the plain
// key also stays owned by BasicTextField.
false
}
}
.testTag(CHAT_INPUT_FIELD_TEST_TAG),
maxLines = 5,
enabled = enabled,
keyboardOptions = KeyboardOptions(
capitalization = KeyboardCapitalization.Sentences,
imeAction = ImeAction.Send,
),
keyboardActions = KeyboardActions(
onSend = { if (canSubmit) onSend() },
),
textStyle = MaterialTheme.typography.bodyLarge.copy(
color = MaterialTheme.colorScheme.onSurface,
),
@@ -312,7 +408,7 @@ fun ChatInputBar(
Row(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 40.dp),
.heightIn(min = 48.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(4.dp),
) {
@@ -322,7 +418,7 @@ fun ChatInputBar(
Box {
Box(
modifier = Modifier
.size(38.dp)
.size(48.dp)
.clip(CircleShape)
.combinedClickable(
onClick = { attachMenuExpanded = true },
@@ -397,8 +493,9 @@ fun ChatInputBar(
if (effortControl != null) {
ChatInputPickerChip(
control = effortControl,
onSelect = onEffortOptionSelected,
onSelect = {},
modifier = Modifier.widthIn(max = 104.dp),
onClickOverride = onEffortPickerClick,
)
}
@@ -0,0 +1,122 @@
package com.hermesandroid.relay.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.R
import com.hermesandroid.relay.data.ChatQuoteReference
internal data class ChatQuoteReferenceColors(
val background: Color,
val author: Color,
val excerpt: Color,
val accent: Color,
)
internal fun chatQuoteReferenceColors(colorScheme: ColorScheme) = ChatQuoteReferenceColors(
background = colorScheme.surfaceContainerHigh,
author = colorScheme.onSurface,
excerpt = colorScheme.onSurfaceVariant,
accent = colorScheme.primary,
)
/** Attachment-like quote reference used in both the composer and sent bubbles. */
@Composable
fun ChatQuoteReferenceChip(
reference: ChatQuoteReference,
modifier: Modifier = Modifier,
onOpenOriginal: (() -> Unit)? = null,
onRemove: (() -> Unit)? = null,
) {
val openDescription = stringResource(R.string.chat_quote_open, reference.authorLabel)
val colors = chatQuoteReferenceColors(MaterialTheme.colorScheme)
Surface(
modifier = modifier.fillMaxWidth(),
shape = RoundedCornerShape(8.dp),
color = colors.background,
) {
Row(
modifier = Modifier
.then(
onOpenOriginal?.let { open ->
Modifier.clickable(
role = Role.Button,
onClickLabel = openDescription,
onClick = open,
)
} ?: Modifier,
)
.semantics { contentDescription = "$openDescription. ${reference.excerpt}" }
.padding(start = 7.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Box(
modifier = Modifier
.width(3.dp)
.height(32.dp)
.clip(RoundedCornerShape(99.dp))
.background(colors.accent),
)
Column(
modifier = Modifier
.weight(1f)
.padding(horizontal = 8.dp, vertical = 5.dp),
) {
Text(
text = "@${reference.authorLabel}",
style = MaterialTheme.typography.labelMedium,
fontWeight = FontWeight.Medium,
color = colors.author,
)
Text(
text = reference.excerpt,
style = MaterialTheme.typography.labelSmall,
color = colors.excerpt,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
)
}
if (onRemove != null) {
IconButton(
onClick = onRemove,
modifier = Modifier.size(48.dp),
) {
Icon(
imageVector = Icons.Filled.Close,
contentDescription = stringResource(R.string.chat_quote_remove),
tint = colors.excerpt,
modifier = Modifier.size(18.dp),
)
}
}
}
}
}
@@ -5,9 +5,11 @@ import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
@@ -15,6 +17,7 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
@@ -32,6 +35,7 @@ import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Bolt
import androidx.compose.material.icons.filled.ChatBubble
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Language
@@ -83,6 +87,7 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.ClipEntry
import androidx.compose.ui.platform.LocalClipboard
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
@@ -679,7 +684,6 @@ fun RelayInfoSheet(
// the Profile section footer spells out the override.
private enum class AgentPassportPicker {
Profile,
Personality,
Model,
Reasoning,
@@ -694,7 +698,6 @@ fun AgentInfoSheet(
onNavigateToConnections: () -> Unit,
onNavigateToProfileInspector: (String) -> Unit = {},
) {
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
val agentProfiles by connectionViewModel.agentProfiles.collectAsState()
val selectedProfile by connectionViewModel.selectedProfile.collectAsState()
val resolvedProfile by connectionViewModel.effectiveDisplayProfile.collectAsState()
@@ -707,12 +710,14 @@ fun AgentInfoSheet(
val personalityNames by chatViewModel.personalityNames.collectAsState()
val defaultPersonality by chatViewModel.defaultPersonality.collectAsState()
val selectedModel by chatViewModel.selectedModelOverride.collectAsState()
val selectedProvider by chatViewModel.selectedProviderOverride.collectAsState()
val serverModel by chatViewModel.serverModelName.collectAsState()
val gatewayModel by chatViewModel.gatewayCurrentModel.collectAsState()
val gatewayProvider by chatViewModel.gatewayCurrentProvider.collectAsState()
val modelProviders by chatViewModel.modelProviders.collectAsState()
val apiModelOptions by chatViewModel.apiModelOptions.collectAsState()
val modelOptionsRefreshing by chatViewModel.modelOptionsRefreshing.collectAsState()
val reasoningCapabilityRevision by chatViewModel.reasoningCapabilityRevision.collectAsState()
val selectedReasoning by chatViewModel.selectedReasoningEffort.collectAsState()
val approvalMode by chatViewModel.approvalMode.collectAsState()
val approvalCapability by chatViewModel.approvalModeCapability.collectAsState()
@@ -742,8 +747,21 @@ fun AgentInfoSheet(
connectionViewModel.connectionStore.activeConnectionId.collectAsState()
val authState by connectionViewModel.authState.collectAsState()
val reasoningAvailability = remember(
selectedModel,
selectedProvider,
gatewayModel,
gatewayProvider,
modelProviders,
apiModelOptions,
reasoningCapabilityRevision,
) {
chatViewModel.reasoningEffortAvailability()
}
var selectedTab by remember { mutableStateOf(0) }
var activePicker by remember { mutableStateOf<AgentPassportPicker?>(null) }
var showProfileSwitcher by remember { mutableStateOf(false) }
var showIdentityEditor by remember { mutableStateOf(false) }
var showProfileManager by remember { mutableStateOf(false) }
@@ -753,6 +771,20 @@ fun AgentInfoSheet(
val currentSession = remember(sessions, currentSessionId) {
sessions.firstOrNull { it.sessionId == currentSessionId }
}
val sessionModelState = resolveSessionModelUiState(
hasSession = currentSessionId != null,
pendingModel = selectedModel,
pendingProvider = selectedProvider,
gatewayModel = gatewayModel,
gatewayProvider = gatewayProvider,
persistedSessionModel = currentSession?.model,
profileDefaultModel = resolvedProfile?.model,
serverDefaultModel = serverModel,
)
val sessionPickerProvider = sessionModelState.pickerProvider
?: sessionModelState.pickerModel?.let { model ->
modelProviders.singleOrNull { model in it.models }?.slug
}
val agentName = AgentDisplay.agentName(
profile = resolvedProfile,
selectedPersonality = selectedPersonality,
@@ -764,25 +796,22 @@ fun AgentInfoSheet(
AgentDisplay.profileDisplayName(it) ?: it.name
} ?: stringResource(R.string.conn_info_server_default)
val serverDefaultLabel = stringResource(R.string.conn_info_server_default)
val modelLabel = AgentDisplay.displayModelName(selectedModel ?: gatewayModel)
?: AgentDisplay.displayModelName(resolvedProfile?.model)
?: AgentDisplay.displayModelName(serverModel)
val modelLabel = AgentDisplay.displayModelName(sessionModelState.model)
?: serverDefaultLabel
val serverDefaultModelLabel = AgentDisplay.displayModelName(serverModel)
?: AgentDisplay.displayModelName(resolvedProfile?.model)
val providerLabel = modelProviders
.firstOrNull { it.slug.equals(gatewayProvider, ignoreCase = true) }
.firstOrNull { it.slug.equals(sessionModelState.provider, ignoreCase = true) }
?.name
?.takeIf { it.isNotBlank() }
?: gatewayProvider.takeIf { it.isNotBlank() }
val sessionModelLabel = AgentDisplay.displayModelName(currentSession?.model)
?: modelLabel
val sessionProviderLabel = currentSession?.model
?: sessionModelState.provider
val sessionModelLabel = modelLabel
val sessionProviderLabel = sessionModelState.model
?.takeIf { it.isNotBlank() }
?.let { sessionModel ->
modelProviders.firstOrNull { sessionModel in it.models }?.name
}
?: providerLabel.takeIf { currentSession?.model.isNullOrBlank() }
?: providerLabel
val connected = chatReady
val resolvedPresence = resolvedProfile?.let {
ProfilePresenceResolver.resolve(it, connected)
@@ -832,6 +861,9 @@ fun AgentInfoSheet(
apiModelOptions,
agentProfiles,
selectedModel,
selectedProvider,
sessionModelState,
sessionPickerProvider,
modelLabel,
unavailableModelLabel,
modelNeedsSetupLabel,
@@ -852,7 +884,7 @@ fun AgentInfoSheet(
label = serverDefaultLabel,
value = null,
secondary = serverDefaultModelLabel,
selected = selectedModel == null,
selected = sessionModelState.inheritsProfileDefault,
),
)
modelProviders
@@ -872,7 +904,8 @@ fun AgentInfoSheet(
provider.warning ?: modelNeedsSetupLabel
else -> null
},
selected = selectedModel == model,
selected = sessionModelState.pickerModel == model &&
sessionPickerProvider.equals(provider.slug, ignoreCase = true),
enabled = !unavailable,
),
)
@@ -888,7 +921,7 @@ fun AgentInfoSheet(
value = model.id,
group = "Routes",
secondary = model.routeDetail,
selected = selectedModel == model.id,
selected = sessionModelState.pickerModel == model.id,
),
)
}
@@ -896,10 +929,10 @@ fun AgentInfoSheet(
}
LaunchedEffect(Unit) {
chatViewModel.refreshModelOptions()
chatViewModel.refreshModelOptions(catalogOnly = true)
chatViewModel.refreshApprovalMode()
chatViewModel.refreshPersonalities()
chatViewModel.refreshModels()
chatViewModel.refreshModels(catalogOnly = true)
connectionViewModel.refreshDashboardProfiles()
}
@@ -912,28 +945,10 @@ fun AgentInfoSheet(
showIdentityEditor = false
}
ModalBottomSheet(
onDismissRequest = onDismiss,
sheetState = sheetState,
sheetGesturesEnabled = false,
dragHandle = null,
AgentPassportSheetHost(
scrollState = passportScrollState,
onDismiss = onDismiss,
) {
Column(
modifier = Modifier
.fillMaxHeight(0.94f)
.verticalScroll(
state = passportScrollState,
overscrollEffect = null,
)
.padding(horizontal = 20.dp, vertical = 4.dp)
.navigationBarsPadding(),
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
Text(
text = stringResource(R.string.conn_info_agent_passport),
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold,
)
AgentPassportHeader(
agentName = agentName,
profileLabel = profileLabel,
@@ -943,10 +958,7 @@ fun AgentInfoSheet(
presence = resolvedPresence,
hasSoul = resolvedProfile?.hasSoul == true,
skillCount = resolvedProfile?.skillCount ?: 0,
transportLabel = transportFriendlyName(sessionTransport.type),
sessionLabel = currentSessionId?.take(8) ?: "—",
contextLabel = contextLabel,
onProfileClick = { activePicker = AgentPassportPicker.Profile },
onProfileClick = { showProfileSwitcher = true },
)
if (showIdentityEditor) {
@@ -980,7 +992,61 @@ fun AgentInfoSheet(
) {
Column {
Text(
text = stringResource(R.string.conn_info_active_configuration),
text = stringResource(R.string.conn_info_session_title),
modifier = Modifier.padding(
start = 18.dp,
top = 17.dp,
end = 18.dp,
bottom = 4.dp,
),
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold,
)
PassportConfigRow(
icon = Icons.Filled.ViewInAr,
title = stringResource(R.string.conn_info_model_title),
value = if (sessionModelState.inheritsProfileDefault) {
stringResource(R.string.conn_info_server_default_model, modelLabel)
} else {
modelLabel
},
expanded = false,
enabled = !isStreaming,
onClick = { activePicker = AgentPassportPicker.Model },
)
if (
reasoningAvailability.supported != false &&
reasoningAvailability.choices.isNotEmpty()
) {
PassportDivider()
PassportConfigRow(
icon = Icons.Filled.Psychology,
title = stringResource(R.string.chat_select_reasoning_effort),
value = selectedReasoning?.let { reasoningEffortLabel(it) }
?: serverDefaultLabel,
expanded = false,
enabled = gatewayControlsAvailable && !isStreaming,
onClick = { activePicker = AgentPassportPicker.Reasoning },
)
}
}
}
// Upstream personality changes persist to the active profile
// while also applying to the live session. Keep that control
// visibly separate from the session-only model/effort card so
// it cannot be mistaken for an ephemeral override.
Surface(
shape = RoundedCornerShape(22.dp),
color = MaterialTheme.colorScheme.surfaceContainer,
border = androidx.compose.foundation.BorderStroke(
1.dp,
MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.55f),
),
) {
Column {
Text(
text = stringResource(R.string.conn_info_profile),
modifier = Modifier.padding(
start = 18.dp,
top = 17.dp,
@@ -1001,28 +1067,6 @@ fun AgentInfoSheet(
enabled = !isStreaming,
onClick = { activePicker = AgentPassportPicker.Personality },
)
PassportDivider()
PassportConfigRow(
icon = Icons.Filled.ViewInAr,
title = stringResource(R.string.conn_info_model_title),
value = selectedModel ?: if (modelLabel == serverDefaultLabel) {
serverDefaultLabel
} else {
stringResource(R.string.conn_info_server_default_model, modelLabel)
},
expanded = false,
enabled = !isStreaming,
onClick = { activePicker = AgentPassportPicker.Model },
)
PassportDivider()
PassportConfigRow(
icon = Icons.Filled.Psychology,
title = stringResource(R.string.chat_select_reasoning_effort),
value = reasoningLabel(selectedReasoning),
expanded = false,
enabled = gatewayControlsAvailable && !isStreaming,
onClick = { activePicker = AgentPassportPicker.Reasoning },
)
}
}
@@ -1120,7 +1164,7 @@ fun AgentInfoSheet(
sessionProfileName = effectiveSessionProfileName,
modelLabel = sessionModelLabel,
providerLabel = sessionProviderLabel,
reasoningLabel = selectedReasoning?.let(::reasoningLabel)
reasoningLabel = selectedReasoning?.let { reasoningEffortLabel(it) }
?: stringResource(R.string.conn_info_server_default),
approvalMode = approvalMode,
approvalCapability = approvalCapability,
@@ -1144,53 +1188,9 @@ fun AgentInfoSheet(
)
}
}
}
}
when (activePicker) {
AgentPassportPicker.Profile -> {
val selectedProfileKey = AgentDisplay.profileSessionKey(selectedProfile?.name)
val visibleProfileKeys = ProfilePresentationPolicy.visibleKeys(
profiles = agentProfiles,
presentation = profilePresentation,
selectedKey = selectedProfileKey,
)
val options = visibleProfileKeys.mapNotNull { profileKey ->
if (profileKey == AgentDisplay.SERVER_DEFAULT_PROFILE_KEY) {
ChatInputPickerOption(
label = serverDefaultLabel,
value = null,
secondary = AgentDisplay.profileDisplayName(resolvedProfile),
selected = selectedProfile == null,
enabled = !isProfileLocked && profileSwitchEnabled,
)
} else {
agentProfiles.firstOrNull { it.name == profileKey }?.let { profile ->
ChatInputPickerOption(
label = AgentDisplay.profileDisplayName(profile)
?: profile.name.replaceFirstChar { it.uppercase() },
value = profile.name,
secondary = profile.model.takeIf { it.isNotBlank() },
selected = selectedProfile?.name == profile.name,
enabled = !isProfileLocked && profileSwitchEnabled,
)
}
}
}
OptionPickerSheet(
title = stringResource(R.string.conn_info_profile),
options = options,
onSelect = { option ->
val profile = option.value?.let { name ->
agentProfiles.firstOrNull { it.name == name }
}
connectionViewModel.selectProfile(profile)
chatViewModel.activateGatewayProfile(profile)
activePicker = null
},
onDismiss = { activePicker = null },
)
}
AgentPassportPicker.Personality -> OptionPickerSheet(
title = stringResource(R.string.conn_info_personality_title),
options = buildList {
@@ -1221,7 +1221,9 @@ fun AgentInfoSheet(
AgentPassportPicker.Model -> ModelPickerSheet(
options = passportModelOptions,
refreshing = modelOptionsRefreshing,
onRefresh = { chatViewModel.refreshModelOptions(refresh = true) },
onRefresh = {
chatViewModel.refreshModelOptions(refresh = true, catalogOnly = true)
},
onSelect = { option ->
activePicker = null
if (option.provider == null && apiModelOptions.any { it.id == option.value }) {
@@ -1232,32 +1234,66 @@ fun AgentInfoSheet(
},
onDismiss = { activePicker = null },
)
AgentPassportPicker.Reasoning -> OptionPickerSheet(
title = stringResource(R.string.chat_select_reasoning_effort),
options = listOf(
"none" to stringResource(R.string.chat_reasoning_none),
"minimal" to stringResource(R.string.chat_reasoning_minimal),
"low" to stringResource(R.string.chat_reasoning_low),
"medium" to stringResource(R.string.chat_reasoning_medium),
"high" to stringResource(R.string.chat_reasoning_high),
"xhigh" to "XHigh",
).map { (value, label) ->
AgentPassportPicker.Reasoning -> {
val reasoningOptions = reasoningAvailability.choices.map { value ->
ChatInputPickerOption(
label = label,
label = reasoningEffortLabel(value),
value = value,
selected = selectedReasoning == value,
enabled = gatewayControlsAvailable && !isStreaming,
enabled = reasoningAvailability.supported != false &&
gatewayControlsAvailable && !isStreaming,
)
},
onSelect = { option ->
option.value?.let(chatViewModel::selectReasoningEffort)
activePicker = null
},
onDismiss = { activePicker = null },
)
}
OptionPickerSheet(
title = stringResource(R.string.chat_select_reasoning_effort),
subtitle = when {
reasoningAvailability.exact &&
selectedReasoning != null &&
selectedReasoning !in reasoningAvailability.choices -> stringResource(
R.string.reasoning_effort_current_outside_supported,
reasoningEffortLabel(selectedReasoning),
reasoningOptions.joinToString { it.label },
)
!reasoningAvailability.exact ->
stringResource(R.string.reasoning_effort_standard_levels_notice)
else -> null
},
options = reasoningOptions,
onSelect = { option ->
option.value?.let(chatViewModel::selectReasoningEffort)
activePicker = null
},
onDismiss = { activePicker = null },
)
}
null -> Unit
}
if (showProfileSwitcher) {
ProfileSwitcherSheet(
connectionViewModel = connectionViewModel,
profiles = agentProfiles,
selectedProfile = selectedProfile,
resolvedProfile = resolvedProfile,
presentation = profilePresentation,
isProfileLocked = isProfileLocked,
switchEnabled = profileSwitchEnabled,
onSelect = { profile ->
if (AgentDisplay.profileSessionKey(profile?.name) !=
AgentDisplay.profileSessionKey(selectedProfile?.name)
) {
connectionViewModel.selectProfile(profile)
chatViewModel.activateGatewayProfile(profile)
}
},
onManageDisplay = {
showProfileSwitcher = false
showProfileManager = true
},
onDismiss = { showProfileSwitcher = false },
)
}
if (showProfileManager) {
ProfileDisplayManagerDialog(
profiles = agentProfiles,
@@ -1271,6 +1307,75 @@ fun AgentInfoSheet(
}
}
internal const val AGENT_PASSPORT_SHEET_TAG = "agentPassportSheet"
internal const val AGENT_PASSPORT_SCROLL_TAG = "agentPassportScroll"
internal const val AGENT_PASSPORT_CLOSE_TAG = "agentPassportClose"
@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun AgentPassportSheetHost(
scrollState: ScrollState,
onDismiss: () -> Unit,
content: @Composable ColumnScope.() -> Unit,
) {
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
ModalBottomSheet(
onDismissRequest = onDismiss,
sheetState = sheetState,
// A full-height sheet and its long verticalScroll child otherwise
// compete for the same drag at the content boundaries. On some devices
// that repeatedly settles/re-expands the sheet and produces a visible
// vibration at the bottom. Close and system Back remain explicit.
sheetGesturesEnabled = false,
) {
Column(
modifier = Modifier
.fillMaxHeight(0.94f)
.testTag(AGENT_PASSPORT_SHEET_TAG),
) {
// Keep the close action visible while compact or large-font content
// scrolls independently below it.
Row(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 56.dp)
.padding(start = 20.dp, end = 8.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
) {
Text(
text = stringResource(R.string.conn_info_agent_passport),
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold,
)
IconButton(
onClick = onDismiss,
modifier = Modifier.testTag(AGENT_PASSPORT_CLOSE_TAG),
) {
Icon(
imageVector = Icons.Filled.Close,
contentDescription = stringResource(R.string.common_close),
)
}
}
Column(
modifier = Modifier
.weight(1f)
.verticalScroll(
state = scrollState,
overscrollEffect = null,
)
.testTag(AGENT_PASSPORT_SCROLL_TAG)
.padding(horizontal = 20.dp, vertical = 4.dp)
.navigationBarsPadding(),
verticalArrangement = Arrangement.spacedBy(10.dp),
content = content,
)
}
}
}
@Composable
private fun AgentPassportIdentityEditor(
connectionViewModel: ConnectionViewModel,
@@ -1356,9 +1461,6 @@ private fun AgentPassportHeader(
presence: ProfilePresence?,
hasSoul: Boolean,
skillCount: Int,
transportLabel: String,
sessionLabel: String,
contextLabel: String,
onProfileClick: () -> Unit,
) {
val brand = LocalBrand.current
@@ -1376,18 +1478,9 @@ private fun AgentPassportHeader(
}
}
val identityMetadata = listOfNotNull(
providerLabel,
stringResource(R.string.conn_info_soul).takeIf { hasSoul },
stringResource(R.string.conn_info_skills_count, skillCount).takeIf { skillCount > 0 },
).joinToString(" · ")
val connectionValue = listOf(
transportLabel,
if (connected) {
stringResource(R.string.conn_info_connected)
} else {
stringResource(R.string.conn_info_disconnected)
},
).joinToString(" · ")
Surface(
shape = RoundedCornerShape(24.dp),
color = MaterialTheme.colorScheme.surfaceContainer,
@@ -1480,7 +1573,7 @@ private fun AgentPassportHeader(
) {
Column(modifier = Modifier.padding(horizontal = 14.dp, vertical = 10.dp)) {
Text(
text = stringResource(R.string.conn_info_active_profile).uppercase(),
text = stringResource(R.string.profile_shelf_switch_agent).uppercase(),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.primary,
)
@@ -1522,40 +1615,17 @@ private fun AgentPassportHeader(
}
}
HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f))
Row(modifier = Modifier.fillMaxWidth()) {
PassportTechnicalField(
label = stringResource(R.string.conn_info_model_title),
value = modelLabel,
modifier = Modifier.weight(1f),
)
PassportTechnicalDivider(height = 44.dp)
PassportTechnicalField(
label = stringResource(R.string.conn_info_connection),
value = connectionValue,
valueColor = if (connected) brand.green else MaterialTheme.colorScheme.error,
modifier = Modifier.weight(1f),
)
}
HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f))
Row(modifier = Modifier.fillMaxWidth()) {
PassportTechnicalField(
label = stringResource(R.string.conn_info_session_title),
value = sessionLabel,
modifier = Modifier.weight(1f),
)
PassportTechnicalDivider(height = 42.dp)
PassportTechnicalField(
label = stringResource(R.string.conn_info_context),
value = contextLabel,
modifier = Modifier.weight(1f),
)
PassportTechnicalDivider(height = 42.dp)
PassportTechnicalField(
label = stringResource(R.string.conn_info_transport),
value = transportLabel,
modifier = Modifier.weight(1f),
)
}
PassportTechnicalField(
label = stringResource(R.string.voice_test_label_provider),
value = providerLabel ?: "—",
modifier = Modifier.fillMaxWidth(),
)
PassportTechnicalField(
label = stringResource(R.string.conn_info_model_title),
value = modelLabel,
modifier = Modifier.fillMaxWidth(),
maxLines = 2,
)
}
}
}
@@ -1566,6 +1636,7 @@ private fun PassportTechnicalField(
value: String,
modifier: Modifier = Modifier,
valueColor: Color = MaterialTheme.colorScheme.onSurface,
maxLines: Int = 1,
) {
Column(
modifier = modifier.padding(horizontal = 8.dp),
@@ -1581,22 +1652,12 @@ private fun PassportTechnicalField(
text = value,
style = MaterialTheme.typography.labelLarge,
color = valueColor,
maxLines = 1,
maxLines = maxLines,
overflow = TextOverflow.Ellipsis,
)
}
}
@Composable
private fun PassportTechnicalDivider(height: Dp) {
Box(
modifier = Modifier
.height(height)
.widthIn(min = 1.dp, max = 1.dp)
.background(MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f)),
)
}
@Composable
private fun AgentPassportTabs(
selected: Int,
@@ -1722,8 +1783,17 @@ private fun PassportDivider() {
)
}
internal const val PASSPORT_APPROVAL_CONTROL_TAG = "passportApproval"
internal const val PASSPORT_CHAT_OVERRIDE_CONTROL_TAG = "passportChatOverride"
internal const val PASSPORT_FAST_CONTROL_TAG = "passportFast"
internal data class PassportSegmentOption(
val label: String,
val description: String,
)
@Composable
private fun AgentPassportSafetyCard(
internal fun AgentPassportSafetyCard(
approvalMode: GatewayApprovalMode?,
approvalCapability: GatewayApprovalModeCapability,
approvalWritable: Boolean,
@@ -1737,9 +1807,18 @@ private fun AgentPassportSafetyCard(
onFast: (Boolean) -> Unit,
) {
val approvalModes = listOf(
GatewayApprovalMode.Manual to stringResource(R.string.conn_info_approval_mode_manual),
GatewayApprovalMode.Smart to stringResource(R.string.conn_info_approval_mode_smart),
GatewayApprovalMode.Off to stringResource(R.string.conn_info_approval_mode_off),
GatewayApprovalMode.Manual to PassportSegmentOption(
label = stringResource(R.string.conn_info_approval_mode_manual),
description = stringResource(R.string.conn_info_approval_mode_manual_desc),
),
GatewayApprovalMode.Smart to PassportSegmentOption(
label = stringResource(R.string.conn_info_approval_mode_smart),
description = stringResource(R.string.conn_info_approval_mode_smart_desc),
),
GatewayApprovalMode.Off to PassportSegmentOption(
label = stringResource(R.string.conn_info_approval_mode_off),
description = stringResource(R.string.conn_info_approval_mode_off_desc),
),
)
Surface(
shape = RoundedCornerShape(22.dp),
@@ -1782,11 +1861,12 @@ private fun AgentPassportSafetyCard(
}
GatewayApprovalModeCapability.Supported -> {
PassportSegmentedControl(
labels = approvalModes.map { it.second },
options = approvalModes.map { it.second },
selected = approvalModes.indexOfFirst { it.first == approvalMode }
.takeIf { it >= 0 },
enabled = controlsAvailable && gatewayReady &&
approvalWritable && !isStreaming,
testTagPrefix = PASSPORT_APPROVAL_CONTROL_TAG,
onSelected = { index -> onApprovalMode(approvalModes[index].first) },
)
}
@@ -1804,15 +1884,26 @@ private fun AgentPassportSafetyCard(
},
) {
PassportSegmentedControl(
labels = listOf(
stringResource(R.string.conn_info_inherited),
stringResource(R.string.conn_info_bypassed),
options = listOf(
PassportSegmentOption(
label = stringResource(R.string.conn_info_inherited),
description = if (approvalMode == GatewayApprovalMode.Off) {
stringResource(R.string.conn_info_profile_already_bypasses)
} else {
stringResource(R.string.conn_info_chat_override_inherited_desc)
},
),
PassportSegmentOption(
label = stringResource(R.string.conn_info_bypassed),
description = stringResource(R.string.conn_info_yolo_mode_desc_ephemeral),
),
),
selected = if (
approvalMode != GatewayApprovalMode.Off && yoloEnabled == true
) 1 else 0,
enabled = controlsAvailable && gatewayReady && !isStreaming &&
approvalMode != GatewayApprovalMode.Off,
testTagPrefix = PASSPORT_CHAT_OVERRIDE_CONTROL_TAG,
onSelected = { onYolo(it == 1) },
)
}
@@ -1824,12 +1915,19 @@ private fun AgentPassportSafetyCard(
description = stringResource(R.string.conn_info_fast_mode_desc),
) {
PassportSegmentedControl(
labels = listOf(
stringResource(R.string.conn_info_fast),
stringResource(R.string.appearance_font_normal),
options = listOf(
PassportSegmentOption(
label = stringResource(R.string.conn_info_fast),
description = stringResource(R.string.conn_info_fast_mode_desc),
),
PassportSegmentOption(
label = stringResource(R.string.appearance_font_normal),
description = stringResource(R.string.conn_info_normal_tier_desc),
),
),
selected = if (fastEnabled == true) 0 else 1,
enabled = controlsAvailable && gatewayReady && !isStreaming,
testTagPrefix = PASSPORT_FAST_CONTROL_TAG,
onSelected = { onFast(it == 0) },
)
}
@@ -1844,91 +1942,119 @@ private fun PassportSafetyRow(
description: String,
control: @Composable () -> Unit,
) {
Row(
Column(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 7.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(10.dp),
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
Surface(
modifier = Modifier.size(38.dp),
shape = CircleShape,
color = MaterialTheme.colorScheme.surfaceContainerHighest,
contentColor = MaterialTheme.colorScheme.primary,
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(10.dp),
) {
Box(contentAlignment = Alignment.Center) {
Icon(icon, contentDescription = null, modifier = Modifier.size(20.dp))
Surface(
modifier = Modifier.size(40.dp),
shape = CircleShape,
color = MaterialTheme.colorScheme.surfaceContainerHighest,
contentColor = MaterialTheme.colorScheme.primary,
) {
Box(contentAlignment = Alignment.Center) {
Icon(icon, contentDescription = null, modifier = Modifier.size(20.dp))
}
}
Column(modifier = Modifier.weight(1f)) {
Text(
text = title,
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Medium,
)
Text(
text = description,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
Column(modifier = Modifier.weight(1.25f)) {
Text(
text = title,
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Medium,
)
Text(
text = description,
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 3,
overflow = TextOverflow.Ellipsis,
)
}
Box(modifier = Modifier.weight(1f)) {
control()
}
control()
}
}
@Composable
private fun PassportSegmentedControl(
labels: List<String>,
internal fun PassportSegmentedControl(
options: List<PassportSegmentOption>,
selected: Int?,
enabled: Boolean,
testTagPrefix: String,
onSelected: (Int) -> Unit,
) {
Row(
modifier = Modifier
.fillMaxWidth()
.selectableGroup()
.clip(RoundedCornerShape(14.dp))
.background(MaterialTheme.colorScheme.surfaceContainerHighest)
.padding(3.dp)
.alpha(if (enabled) 1f else 0.52f),
) {
labels.forEachIndexed { index, label ->
Surface(
modifier = Modifier
.weight(1f)
.selectable(
selected = selected == index,
enabled = enabled,
role = Role.RadioButton,
onClick = { onSelected(index) },
),
shape = RoundedCornerShape(11.dp),
color = if (selected == index) {
MaterialTheme.colorScheme.primary
} else {
Color.Transparent
},
contentColor = if (selected == index) {
MaterialTheme.colorScheme.onPrimary
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
) {
Text(
text = label,
modifier = Modifier.padding(horizontal = 4.dp, vertical = 8.dp),
textAlign = androidx.compose.ui.text.style.TextAlign.Center,
style = MaterialTheme.typography.labelMedium,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {
Row(
modifier = Modifier
.fillMaxWidth()
.selectableGroup()
.clip(RoundedCornerShape(14.dp))
.background(MaterialTheme.colorScheme.surfaceContainerHighest)
.padding(3.dp),
) {
options.forEachIndexed { index, option ->
Surface(
modifier = Modifier
.weight(1f)
.heightIn(min = 48.dp)
.testTag("$testTagPrefix-$index")
.selectable(
selected = selected == index,
enabled = enabled,
role = Role.RadioButton,
onClick = { onSelected(index) },
)
.semantics {
contentDescription = "${option.label}. ${option.description}"
},
shape = RoundedCornerShape(11.dp),
color = when {
selected != index -> Color.Transparent
enabled -> MaterialTheme.colorScheme.primary
else -> MaterialTheme.colorScheme.primaryContainer
},
contentColor = when {
selected == index && enabled -> MaterialTheme.colorScheme.onPrimary
selected == index -> MaterialTheme.colorScheme.onPrimaryContainer
enabled -> MaterialTheme.colorScheme.onSurfaceVariant
else -> MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f)
},
) {
Box(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 48.dp)
.padding(horizontal = 6.dp, vertical = 6.dp),
contentAlignment = Alignment.Center,
) {
Text(
text = option.label,
textAlign = androidx.compose.ui.text.style.TextAlign.Center,
style = MaterialTheme.typography.bodyMedium,
fontWeight = if (selected == index) {
FontWeight.SemiBold
} else {
FontWeight.Medium
},
maxLines = 2,
)
}
}
}
}
selected?.let(options::getOrNull)?.let { option ->
Text(
text = option.description,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.padding(horizontal = 4.dp),
)
}
}
}
@@ -2094,9 +2220,6 @@ private fun compactTokenCount(value: Int): String = when {
else -> value.toString()
}
private fun reasoningLabel(value: String?): String =
value?.replaceFirstChar { it.uppercase() } ?: "Medium"
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun LegacyAgentInfoSheet(
@@ -2143,7 +2266,7 @@ private fun LegacyAgentInfoSheet(
// Pull the gateway's curated provider/model list (model.options) when the
// sheet opens — the real switchable models, grouped by provider.
LaunchedEffect(Unit) { chatViewModel.refreshModelOptions() }
LaunchedEffect(Unit) { chatViewModel.refreshModelOptions(catalogOnly = true) }
LaunchedEffect(Unit) { chatViewModel.refreshApprovalMode() }
// Re-pull server-supplied personalities (list + default + active) on open so
// a server-side change shows without an app reload.
@@ -2151,7 +2274,7 @@ private fun LegacyAgentInfoSheet(
// Re-pull the SSE-fallback model list + skill catalog on open so server-side
// changes surface without an app reload (gateway model groups are covered by
// refreshModelOptions above; skills feed the command palette).
LaunchedEffect(Unit) { chatViewModel.refreshModels() }
LaunchedEffect(Unit) { chatViewModel.refreshModels(catalogOnly = true) }
LaunchedEffect(Unit) { chatViewModel.refreshSkills() }
// Pull the host's agent profiles from the dashboard so they appear in the
// Profile picker even on a dashboard-only (non-relay) connection.
@@ -2356,7 +2479,7 @@ private fun LegacyAgentInfoSheet(
// "default" so "Server default" vs "default" would be
// otherwise indistinguishable.
val serverDefaultProfile = agentProfiles
.firstOrNull { AgentDisplay.isServerDefaultAlias(it.name) }
.firstOrNull { it.name.equals("default", ignoreCase = true) }
val selectedKey = AgentDisplay.profileSessionKey(selectedProfile?.name)
val visibleProfileKeys = ProfilePresentationPolicy
.visibleKeys(agentProfiles, profilePresentation, selectedKey)
@@ -2379,8 +2502,7 @@ private fun LegacyAgentInfoSheet(
val lockedDisplayName = when {
lockedProfileName == null ->
stringResource(R.string.conn_info_server_default)
AgentDisplay.isServerDefaultAlias(lockedProfileName) ||
lockedProfileName == AgentDisplay.SERVER_DEFAULT_PROFILE_KEY ->
lockedProfileName == AgentDisplay.SERVER_DEFAULT_PROFILE_KEY ->
stringResource(R.string.conn_info_server_default)
else ->
agentProfiles
@@ -3468,7 +3590,7 @@ private fun LegacyAgentInfoSheet(
// --- AgentInfoSheet helpers ----------------------------------------------
@Composable
private fun ProfileDisplayManagerDialog(
internal fun ProfileDisplayManagerDialog(
profiles: List<Profile>,
presentation: ProfilePresentation,
selectedProfileName: String?,
@@ -10,7 +10,9 @@ import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
@@ -38,12 +40,15 @@ import androidx.compose.material.icons.automirrored.outlined.MenuBook
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.ChevronRight
import androidx.compose.material.icons.filled.Cloud
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.Dns
import androidx.compose.material.icons.filled.ErrorOutline
import androidx.compose.material.icons.filled.Keyboard
import androidx.compose.material.icons.filled.PhonelinkLock
import androidx.compose.material.icons.filled.QrCodeScanner
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Wifi
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.AlertDialog
@@ -76,7 +81,6 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalClipboard
import androidx.compose.ui.platform.LocalContext
@@ -201,6 +205,8 @@ fun ConnectionWizard(
val currentRelayUrl by connectionViewModel.relayUrl.collectAsState()
val currentDashboardUrl by connectionViewModel.effectiveDashboardUrl.collectAsState()
val activeConnection by connectionViewModel.activeConnection.collectAsState()
val accessibleMotion = rememberAccessibleMotionState()
val animateWizardTransitions = shouldAnimateWizardTransitions(accessibleMotion)
var step by remember { mutableStateOf(WizardStep.Nearby) }
var chosenMethod by remember { mutableStateOf(PairMethod.Standard) }
@@ -222,7 +228,7 @@ fun ConnectionWizard(
mutableStateOf<ConnectionViewModel.DashboardSetupResult?>(null)
}
var dashboardSuggestedHostname by remember { mutableStateOf<String?>(null) }
var connectionGuideExpanded by rememberSaveable { mutableStateOf(showSkip) }
var dashboardEntryIntent by rememberSaveable { mutableStateOf(DashboardEntryIntent.Server) }
var pendingDashboardDraft by remember { mutableStateOf<DashboardConnectionDraft?>(null) }
val relayScopedFlow = autoStart == "relay"
val pairingHome = if (relayScopedFlow) WizardStep.RelayChoice else WizardStep.Method
@@ -527,18 +533,27 @@ fun ConnectionWizard(
}
}
Column(
modifier = modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState())
.padding(horizontal = 20.dp, vertical = 16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
WizardStepIndicator(currentStep = step.indicatorIndex, method = chosenMethod)
if (!showQrScanner) {
Column(
modifier = modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState())
.padding(horizontal = 20.dp, vertical = 16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
if (step != WizardStep.Nearby) {
WizardStepIndicator(currentStep = step.indicatorIndex, method = chosenMethod)
}
AnimatedContent(
targetState = step,
transitionSpec = { fadeIn() togetherWith fadeOut() },
transitionSpec = {
if (animateWizardTransitions) {
fadeIn(tween(200)) togetherWith fadeOut(tween(180))
} else {
EnterTransition.None togetherWith ExitTransition.None
}
},
label = "wizard-step",
) { current ->
when (current) {
@@ -553,7 +568,14 @@ fun ConnectionWizard(
inspectDashboard(dashboardUrl, candidate.hostname)
}
},
onManual = {
onCloud = {
dashboardEntryIntent = DashboardEntryIntent.Cloud
dashboardProbeError = null
dashboardAddress = ""
step = WizardStep.DashboardManual
},
onManualServer = {
dashboardEntryIntent = DashboardEntryIntent.Server
dashboardProbeError = null
step = WizardStep.DashboardManual
},
@@ -569,11 +591,10 @@ fun ConnectionWizard(
onOtherWays = { step = WizardStep.Method },
onSkip = if (showSkip) onCancel else null,
onTryDemo = onTryDemo,
guideExpanded = connectionGuideExpanded,
onGuideExpandedChange = { connectionGuideExpanded = it },
)
WizardStep.DashboardManual -> DashboardManualStep(
intent = dashboardEntryIntent,
address = dashboardAddress,
onAddressChange = {
dashboardAddress = it
@@ -582,7 +603,7 @@ fun ConnectionWizard(
busy = dashboardProbeBusy,
error = dashboardProbeError,
onBack = { step = WizardStep.Nearby },
onSubmit = { inspectDashboard(dashboardAddress, null) },
onSubmit = { resolvedAddress -> inspectDashboard(resolvedAddress, null) },
)
WizardStep.DashboardFound -> dashboardProbeResult?.let { result ->
@@ -728,47 +749,65 @@ fun ConnectionWizard(
// role — otherwise Retry would drop the user's
// "Prefer" choice on every failure.
pendingPayload = reorderedPayload
// Pre-pair duplicate check — stops the flow
// on the Confirm screen if the scanned
// serverUrl already matches an existing
// connection. User can then re-pair that
// existing entry in place (see the
// DuplicateConnectionDialog at the bottom
// of this composable) or cancel out. If no
// duplicate, proceed to Verify as before.
val existing = if (relayScopedFlow) {
null
} else {
findDuplicateFor(
reorderedPayload.serverUrl,
reorderedPayload.relay?.url.orEmpty(),
reorderedPayload.dashboardUrl,
)
}
if (existing != null) {
duplicatePrompt = existing
} else if (reorderedPayload.relay == null) {
launchStandardConnect(
reorderedPayload.serverUrl,
reorderedPayload.key,
"",
reorderedPayload.dashboardUrl.orEmpty(),
reorderedPayload.endpoints,
)
} else {
wizardScope.launch {
connectionViewModel.ensureActiveConnectionForSetup(
apiServerUrl = reorderedPayload.serverUrl,
relayUrl = reorderedPayload.relay.url,
routeCandidates = reorderedPayload.endpoints,
val dispatch = setupQrDispatch(reorderedPayload)
when (dispatch) {
SetupQrDispatch.Dashboard -> {
// Dashboard-only setup belongs to the same
// upstream probe/auth owner as manual and LAN
// Dashboard setup. DashboardFound performs the
// existing duplicate check before persistence,
// and applyDashboardConnect launches native
// sign-in when the advertised status requires it.
// Move to the manual Dashboard surface before
// probing so a contract failure is visible and
// retryable instead of being hidden by Confirm.
dashboardEntryIntent = DashboardEntryIntent.Server
dashboardAddress = reorderedPayload.dashboardUrl.orEmpty()
step = WizardStep.DashboardManual
inspectDashboard(
reorderedPayload.dashboardUrl.orEmpty(),
null,
)
connectionViewModel.applyPairingPayload(
reorderedPayload,
ttlSeconds,
preserveStandardConfig = relayScopedFlow,
)
step = WizardStep.Verify
verifyAttempt += 1
}
SetupQrDispatch.StandardApi,
SetupQrDispatch.Relay -> {
// API and Relay payloads retain their existing
// duplicate/persistence behavior unchanged.
val existing = if (relayScopedFlow) {
null
} else {
findDuplicateFor(
reorderedPayload.serverUrl,
reorderedPayload.relay?.url.orEmpty(),
reorderedPayload.dashboardUrl,
)
}
if (existing != null) {
duplicatePrompt = existing
} else if (dispatch == SetupQrDispatch.StandardApi) {
launchStandardConnect(
reorderedPayload.serverUrl,
reorderedPayload.key,
"",
reorderedPayload.dashboardUrl.orEmpty(),
reorderedPayload.endpoints,
)
} else {
wizardScope.launch {
connectionViewModel.ensureActiveConnectionForSetup(
apiServerUrl = reorderedPayload.serverUrl,
relayUrl = reorderedPayload.relay?.url.orEmpty(),
routeCandidates = reorderedPayload.endpoints,
)
connectionViewModel.applyPairingPayload(
reorderedPayload,
ttlSeconds,
preserveStandardConfig = relayScopedFlow,
)
step = WizardStep.Verify
verifyAttempt += 1
}
}
}
}
},
@@ -839,6 +878,7 @@ fun ConnectionWizard(
}
}
}
}
if (showQrScanner) {
QrPairingScanner(
@@ -1096,6 +1136,44 @@ private enum class WizardStep {
private enum class PairMethod { Standard, Scan, EnterCode, ShowCode }
private enum class DashboardEntryIntent { Cloud, Server }
internal enum class SetupQrDispatch { Dashboard, StandardApi, Relay }
/** Keep setup QR routing aligned with the surface that owns its credentials. */
internal fun setupQrDispatch(payload: HermesPairingPayload): SetupQrDispatch = when {
payload.relay != null -> SetupQrDispatch.Relay
!payload.hasApiServer && !payload.dashboardUrl.isNullOrBlank() -> SetupQrDispatch.Dashboard
else -> SetupQrDispatch.StandardApi
}
/** Wizard motion follows the shared OS-animation and touch-exploration policy. */
internal fun shouldAnimateWizardTransitions(state: AccessibleMotionState): Boolean =
state.osAnimations && !state.touchExploration
private const val NOUS_CLOUD_HOST_SUFFIX = ".agents.nousresearch.com"
private val NOUS_CLOUD_SLUG = Regex("^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$")
internal fun resolveNousCloudDashboardAddress(value: String): String {
val trimmed = value.trim()
if (trimmed.contains("://")) return trimmed
val slug = trimmed.removeSuffix(NOUS_CLOUD_HOST_SUFFIX)
return if (NOUS_CLOUD_SLUG.matches(slug)) {
"https://$slug$NOUS_CLOUD_HOST_SUFFIX"
} else {
trimmed
}
}
internal fun isValidNousCloudSlug(value: String): Boolean =
NOUS_CLOUD_SLUG.matches(value.trim())
internal fun isValidNousCloudAddressInput(value: String): Boolean {
val trimmed = value.trim()
if (trimmed.contains("://")) return false
return isValidNousCloudSlug(trimmed.removeSuffix(NOUS_CLOUD_HOST_SUFFIX))
}
private data class StandardConnectionDraft(
val apiUrl: String,
val apiKey: String,
@@ -1201,19 +1279,50 @@ private fun NearbyHermesStep(
message: String?,
onSearchAgain: () -> Unit,
onSelect: (HermesLanDiscoveryResult) -> Unit,
onManual: () -> Unit,
onCloud: () -> Unit,
onManualServer: () -> Unit,
onPairRelayQr: () -> Unit,
onPairRelayCode: () -> Unit,
onOtherWays: () -> Unit,
onSkip: (() -> Unit)?,
onTryDemo: (() -> Unit)?,
guideExpanded: Boolean,
onGuideExpandedChange: (Boolean) -> Unit,
) {
NewNearbyHermesStep(
busy = busy,
setupReady = setupReady,
results = results,
message = message,
onSearchAgain = onSearchAgain,
onSelect = onSelect,
onCloud = onCloud,
onManualServer = onManualServer,
onScanQr = onPairRelayQr,
onAdvanced = onOtherWays,
onSkip = onSkip,
onTryDemo = onTryDemo,
)
}
@Composable
private fun NewNearbyHermesStep(
busy: Boolean,
setupReady: Boolean,
results: List<HermesLanDiscoveryResult>,
message: String?,
onSearchAgain: () -> Unit,
onSelect: (HermesLanDiscoveryResult) -> Unit,
onCloud: () -> Unit,
onManualServer: () -> Unit,
onScanQr: () -> Unit,
onAdvanced: () -> Unit,
onSkip: (() -> Unit)?,
onTryDemo: (() -> Unit)?,
) {
val context = LocalContext.current
Column(
modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(14.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Text(
text = stringResource(R.string.cw_connect_to_hermes),
@@ -1221,183 +1330,140 @@ private fun NearbyHermesStep(
modifier = Modifier.semantics { heading() },
)
Text(
text = stringResource(R.string.cw_nearby_description),
text = stringResource(R.string.cw_connection_chooser_description),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Surface(
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.72f),
shape = RoundedCornerShape(16.dp),
Card(
modifier = Modifier
.fillMaxWidth()
.animateContentSize(),
.clickable(enabled = setupReady, role = Role.Button, onClick = onScanQr),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.primaryContainer,
),
shape = RoundedCornerShape(18.dp),
) {
Column(
modifier = Modifier.fillMaxWidth(),
Row(
modifier = Modifier.padding(horizontal = 18.dp, vertical = 20.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(16.dp),
) {
Row(
modifier = Modifier
.fillMaxWidth()
.clickable(
role = Role.Button,
onClick = { onGuideExpandedChange(!guideExpanded) },
)
.padding(horizontal = 16.dp, vertical = 14.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(12.dp),
Icon(
imageVector = Icons.Filled.QrCodeScanner,
contentDescription = null,
tint = MaterialTheme.colorScheme.onPrimaryContainer,
modifier = Modifier.size(34.dp),
)
Column(
modifier = Modifier.weight(1f),
verticalArrangement = Arrangement.spacedBy(3.dp),
) {
Text(
text = stringResource(R.string.cw_before_connecting),
text = stringResource(R.string.cw_scan_setup_qr_title),
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold,
modifier = Modifier.weight(1f),
color = MaterialTheme.colorScheme.onPrimaryContainer,
)
Icon(
imageVector = Icons.Filled.ChevronRight,
contentDescription = null,
modifier = Modifier
.size(22.dp)
.rotate(if (guideExpanded) 90f else 0f),
Text(
text = stringResource(R.string.cw_recommended),
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onPrimaryContainer,
)
Text(
text = stringResource(R.string.cw_scan_setup_qr_subtitle),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.82f),
)
}
if (guideExpanded) {
Column(
modifier = Modifier.padding(start = 16.dp, end = 16.dp, bottom = 12.dp),
verticalArrangement = Arrangement.spacedBy(14.dp),
) {
ConnectionGuideStep(
number = "1",
title = stringResource(R.string.cw_connect_step_server_title),
body = stringResource(R.string.cw_connect_step_server_body),
)
ConnectionGuideStep(
number = "2",
title = stringResource(R.string.cw_connect_step_network_title),
body = stringResource(R.string.cw_connect_step_network_body),
)
ConnectionGuideStep(
number = "3",
title = stringResource(R.string.cw_connect_step_phone_title),
body = stringResource(R.string.cw_connect_step_phone_body),
)
TextButton(
onClick = { openExternalUrl(context, SetupGuideUrl) },
modifier = Modifier.align(Alignment.End),
) {
Icon(
imageVector = Icons.AutoMirrored.Outlined.MenuBook,
contentDescription = null,
modifier = Modifier.size(16.dp),
)
Spacer(Modifier.size(6.dp))
Text(stringResource(R.string.cw_setup_guide))
}
}
}
Icon(
imageVector = Icons.Filled.ChevronRight,
contentDescription = null,
tint = MaterialTheme.colorScheme.onPrimaryContainer,
)
}
}
if (busy) {
Surface(
color = MaterialTheme.colorScheme.surfaceVariant,
shape = RoundedCornerShape(12.dp),
modifier = Modifier.fillMaxWidth(),
) {
Row(
modifier = Modifier.padding(16.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(12.dp),
) {
CircularProgressIndicator(modifier = Modifier.size(22.dp), strokeWidth = 2.dp)
Column {
Text(
stringResource(
if (setupReady) R.string.cw_nearby_searching
else R.string.cw_preparing_connection,
),
style = MaterialTheme.typography.titleSmall,
)
Text(
stringResource(
if (setupReady) R.string.cw_nearby_searching_hint
else R.string.cw_preparing_connection_hint,
),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
}
}
if (results.isNotEmpty()) {
Text(
text = stringResource(R.string.cw_nearby_heading),
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.semantics { heading() },
)
results.forEach { candidate ->
NearbyHermesResult(candidate = candidate, onClick = { onSelect(candidate) })
}
}
message?.let {
Text(
text = it,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.semantics { liveRegion = LiveRegionMode.Polite },
)
Text(
text = stringResource(R.string.cw_nearby_empty_hint),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
if (!busy) {
OutlinedButton(
onClick = onSearchAgain,
modifier = Modifier.fillMaxWidth().heightIn(min = 48.dp),
) {
Icon(Icons.Filled.Refresh, contentDescription = null, modifier = Modifier.size(18.dp))
Spacer(Modifier.size(8.dp))
Text(stringResource(R.string.cw_nearby_search_again))
}
}
Button(
onClick = onManual,
enabled = setupReady,
modifier = Modifier.fillMaxWidth().heightIn(min = 48.dp),
) {
Text(stringResource(R.string.cw_nearby_enter_address))
}
HorizontalDivider(modifier = Modifier.padding(vertical = 2.dp))
Text(
text = stringResource(R.string.cw_relay_entry_title),
text = stringResource(R.string.cw_other_ways_to_connect),
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold,
modifier = Modifier.semantics { heading() },
)
Surface(
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.58f),
shape = RoundedCornerShape(16.dp),
modifier = Modifier.fillMaxWidth(),
) {
Column(modifier = Modifier.fillMaxWidth()) {
ConnectionChooserRow(
icon = Icons.Filled.Cloud,
title = stringResource(R.string.cw_cloud_title),
subtitle = stringResource(R.string.cw_cloud_subtitle),
onClick = onCloud,
enabled = setupReady,
)
HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant)
ConnectionChooserRow(
icon = Icons.Filled.Dns,
title = stringResource(R.string.cw_server_vps_title),
subtitle = stringResource(R.string.cw_server_vps_subtitle),
onClick = onManualServer,
enabled = setupReady,
)
if (results.isNotEmpty()) {
results.forEach { candidate ->
HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant)
ConnectionChooserRow(
icon = Icons.Filled.Wifi,
title = stringResource(R.string.cw_nearby_heading),
subtitle = candidate.dashboardUrl.orEmpty(),
onClick = { onSelect(candidate) },
enabled = setupReady,
status = stringResource(R.string.cw_ready),
)
}
} else {
HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant)
ConnectionChooserRow(
icon = Icons.Filled.Wifi,
title = stringResource(R.string.cw_nearby_heading),
subtitle = when {
busy -> stringResource(R.string.cw_nearby_searching)
!message.isNullOrBlank() -> message
else -> stringResource(R.string.cw_nearby_empty_hint)
},
onClick = onSearchAgain,
enabled = setupReady && !busy,
showProgress = busy,
)
}
}
}
Text(
text = stringResource(R.string.cw_relay_entry_desc),
text = stringResource(R.string.cw_relay_optional_note),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
MethodTile(
icon = Icons.Filled.QrCodeScanner,
title = stringResource(R.string.cw_relay_pair_qr),
subtitle = stringResource(R.string.cw_relay_pair_qr_desc),
onClick = onPairRelayQr,
enabled = setupReady,
)
TextButton(onClick = onPairRelayCode, enabled = setupReady, modifier = Modifier.fillMaxWidth()) {
Text(stringResource(R.string.cw_relay_enter_code))
}
TextButton(onClick = onOtherWays, enabled = setupReady, modifier = Modifier.fillMaxWidth()) {
Text(stringResource(R.string.cw_other_connection_methods))
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
) {
TextButton(onClick = { openExternalUrl(context, SetupGuideUrl) }) {
Icon(
imageVector = Icons.AutoMirrored.Outlined.MenuBook,
contentDescription = null,
modifier = Modifier.size(16.dp),
)
Spacer(Modifier.size(6.dp))
Text(stringResource(R.string.cw_setup_guide))
}
TextButton(onClick = onAdvanced, enabled = setupReady) {
Text(stringResource(R.string.cw_advanced))
}
}
if (onTryDemo != null) {
TextButton(onClick = onTryDemo, modifier = Modifier.fillMaxWidth()) {
@@ -1413,33 +1479,55 @@ private fun NearbyHermesStep(
}
@Composable
private fun ConnectionGuideStep(
number: String,
private fun ConnectionChooserRow(
icon: androidx.compose.ui.graphics.vector.ImageVector,
title: String,
body: String,
subtitle: String,
onClick: () -> Unit,
enabled: Boolean,
status: String? = null,
showProgress: Boolean = false,
) {
Row(
verticalAlignment = Alignment.Top,
horizontalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier
.fillMaxWidth()
.clickable(enabled = enabled, role = Role.Button, onClick = onClick)
.padding(horizontal = 16.dp, vertical = 14.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(14.dp),
) {
Surface(
color = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
shape = CircleShape,
modifier = Modifier.size(28.dp),
color = MaterialTheme.colorScheme.surface,
shape = RoundedCornerShape(12.dp),
) {
Box(contentAlignment = Alignment.Center) {
Text(number, style = MaterialTheme.typography.labelLarge, fontWeight = FontWeight.Bold)
}
Icon(
imageVector = icon,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(10.dp).size(24.dp),
)
}
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(2.dp)) {
Text(title, style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold)
Text(
body,
subtitle,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 2,
)
}
status?.let {
Text(
it,
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.primary,
)
}
if (showProgress) {
CircularProgressIndicator(modifier = Modifier.size(20.dp), strokeWidth = 2.dp)
} else {
Icon(Icons.Filled.ChevronRight, contentDescription = null)
}
}
}
@@ -1488,42 +1576,103 @@ private fun NearbyHermesResult(
@Composable
private fun DashboardManualStep(
intent: DashboardEntryIntent,
address: String,
onAddressChange: (String) -> Unit,
busy: Boolean,
error: String?,
onBack: () -> Unit,
onSubmit: () -> Unit,
onSubmit: (String) -> Unit,
) {
val context = LocalContext.current
val fieldError = optionalHttpUrlError(address, context)
var useCustomAddress by rememberSaveable(intent) { mutableStateOf(false) }
val cloudSlugMode = intent == DashboardEntryIntent.Cloud && !useCustomAddress
val fieldError = if (cloudSlugMode) {
when {
address.isBlank() -> null
!isValidNousCloudAddressInput(address) -> context.getString(R.string.cw_cloud_slug_error)
else -> null
}
} else {
optionalHttpUrlError(address, context)
}
val resolvedAddress = if (cloudSlugMode) resolveNousCloudDashboardAddress(address) else address.trim()
Column(modifier = Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(14.dp)) {
Text(
text = stringResource(R.string.cw_manual_hermes_title),
text = stringResource(
if (intent == DashboardEntryIntent.Cloud) R.string.cw_cloud_entry_title
else R.string.cw_manual_hermes_title,
),
style = MaterialTheme.typography.headlineSmall,
modifier = Modifier.semantics { heading() },
)
Text(
text = stringResource(R.string.cw_manual_hermes_description),
text = stringResource(
if (intent == DashboardEntryIntent.Cloud) R.string.cw_cloud_entry_description
else R.string.cw_manual_hermes_description,
),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
OutlinedTextField(
value = address,
onValueChange = onAddressChange,
label = { Text(stringResource(R.string.cw_hermes_address)) },
placeholder = { Text(stringResource(R.string.cw_hermes_address_placeholder)) },
onValueChange = { updated ->
if (cloudSlugMode && (updated.contains("://") || updated.contains('/'))) {
useCustomAddress = true
}
onAddressChange(updated)
},
label = {
Text(
stringResource(
if (cloudSlugMode) R.string.cw_cloud_agent_name
else R.string.cw_hermes_address,
)
)
},
placeholder = {
Text(
stringResource(
if (cloudSlugMode) R.string.cw_cloud_slug_placeholder
else if (intent == DashboardEntryIntent.Cloud) R.string.cw_cloud_address_placeholder
else R.string.cw_hermes_address_placeholder,
)
)
},
supportingText = {
Text(fieldError ?: stringResource(R.string.cw_hermes_address_hint))
Text(
fieldError ?: stringResource(
if (cloudSlugMode) R.string.cw_cloud_slug_hint
else if (intent == DashboardEntryIntent.Cloud) R.string.cw_cloud_address_hint
else R.string.cw_hermes_address_hint,
)
)
},
isError = fieldError != null || error != null,
singleLine = true,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Go, autoCorrectEnabled = false),
keyboardActions = KeyboardActions(onGo = {
if (address.isNotBlank() && fieldError == null && !busy) onSubmit()
if (address.isNotBlank() && fieldError == null && !busy) onSubmit(resolvedAddress)
}),
modifier = Modifier.fillMaxWidth(),
)
if (intent == DashboardEntryIntent.Cloud) {
TextButton(
onClick = {
useCustomAddress = !useCustomAddress
onAddressChange("")
},
enabled = !busy,
modifier = Modifier.align(Alignment.End),
) {
Text(
stringResource(
if (useCustomAddress) R.string.cw_cloud_use_nous_address
else R.string.cw_cloud_use_custom_address,
)
)
}
}
error?.let {
Text(it, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.error)
}
@@ -1532,7 +1681,7 @@ private fun DashboardManualStep(
Text(stringResource(R.string.cw_back))
}
Button(
onClick = onSubmit,
onClick = { onSubmit(resolvedAddress) },
enabled = address.isNotBlank() && fieldError == null && !busy,
modifier = Modifier.weight(1f).heightIn(min = 48.dp),
) {
@@ -3093,11 +3242,13 @@ private fun ConfirmStep(
modifier = Modifier.padding(14.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
LabeledLine(
label = stringResource(R.string.cw_label_api_server),
value = payload.serverUrl,
hint = stringResource(R.string.cw_hint_chat),
)
if (payload.hasApiServer) {
LabeledLine(
label = stringResource(R.string.cw_label_api_server),
value = payload.serverUrl,
hint = stringResource(R.string.cw_hint_chat),
)
}
if (relayUrl == null) {
LabeledLine(
label = stringResource(R.string.cw_label_dashboard),
@@ -3108,14 +3259,16 @@ private fun ConfirmStep(
?: stringResource(R.string.cw_value_derived),
hint = stringResource(R.string.cw_hint_manage),
)
LabeledLine(
label = stringResource(R.string.cw_label_api_key),
value = if (payload.key.isBlank()) {
stringResource(R.string.cw_value_not_included)
} else {
stringResource(R.string.cw_value_included)
},
)
if (payload.hasApiServer) {
LabeledLine(
label = stringResource(R.string.cw_label_api_key),
value = if (payload.key.isBlank()) {
stringResource(R.string.cw_value_not_included)
} else {
stringResource(R.string.cw_value_included)
},
)
}
}
if (relayUrl != null) {
LabeledLine(
@@ -46,7 +46,7 @@ import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.hermesandroid.relay.R
import com.hermesandroid.relay.util.CrashReport
import com.hermesandroid.relay.reliability.ReliabilityReport
import com.hermesandroid.relay.util.CrashReporter
import com.hermesandroid.relay.util.IssueReport
import kotlinx.coroutines.Dispatchers
@@ -57,16 +57,16 @@ import kotlinx.coroutines.withContext
* crashed). Render it inside the app theme so the dialog picks up Material
* colors — see RelayApp.
*
* Peeks the report on first composition (does NOT delete on read) and clears it
* only when the user acknowledges it (Dismiss/Report). A report the user merely
* Peeks the report on first composition (does not mark it reviewed on read) and
* acknowledges it only on Dismiss/Report. A report the user merely
* glanced at — or never reached because the app was backgrounded — therefore
* survives relaunches instead of being lost after one view; once acknowledged
* it's deleted and won't reappear.
* survives relaunches instead of being lost after one view. Once acknowledged,
* it remains in bounded Diagnostics history but does not interrupt startup again.
*/
@Composable
fun CrashReportGate() {
val context = LocalContext.current
var report by remember { mutableStateOf<CrashReport?>(null) }
var report by remember { mutableStateOf<ReliabilityReport?>(null) }
var checked by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
@@ -79,19 +79,26 @@ fun CrashReportGate() {
CrashReportDialog(
report = pending,
onDismiss = {
// Acknowledged (Dismiss/Report) → delete so it won't reappear.
// Acknowledged (Dismiss/Report) → retain as reviewed history without showing again.
// Copy does NOT route through here, so the report stays available
// across relaunches until the user actually dismisses or reports it.
CrashReporter.clearPending(context)
CrashReporter.clearPending(context, pending.reportId)
report = null
},
)
}
@Composable
private fun CrashReportDialog(report: CrashReport, onDismiss: () -> Unit) {
private fun CrashReportDialog(report: ReliabilityReport, onDismiss: () -> Unit) {
val context = LocalContext.current
val reportText = remember(report) { report.toPlainText() }
var showDetails by remember(report.reportId) { mutableStateOf(false) }
val copiedMessage = stringResource(R.string.crash_toast_copied)
val noShareMessage = stringResource(R.string.crash_toast_no_share)
val shareTitle = stringResource(R.string.crash_share_title)
val reportSubject = stringResource(R.string.crash_share_subject, report.shortTitle())
val openedMessage = stringResource(R.string.crash_toast_opened)
val noBrowserMessage = stringResource(R.string.crash_toast_no_browser)
Dialog(
onDismissRequest = onDismiss,
@@ -126,25 +133,34 @@ private fun CrashReportDialog(report: CrashReport, onDismiss: () -> Unit) {
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Spacer(Modifier.height(14.dp))
Box(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 120.dp, max = 300.dp)
.clip(RoundedCornerShape(12.dp))
.background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.45f)),
) {
SelectionContainer {
Text(
text = reportText,
fontFamily = FontFamily.Monospace,
fontSize = 11.sp,
lineHeight = 15.sp,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(12.dp),
)
Text(
text = stringResource(R.string.crash_privacy),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.padding(top = 6.dp),
)
if (showDetails) {
Spacer(Modifier.height(14.dp))
Box(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 120.dp, max = 300.dp)
.clip(RoundedCornerShape(12.dp))
.background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.45f)),
) {
SelectionContainer {
Text(
text = reportText,
fontFamily = FontFamily.Monospace,
fontSize = 11.sp,
lineHeight = 15.sp,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(12.dp),
)
}
}
}
@@ -157,45 +173,45 @@ private fun CrashReportDialog(report: CrashReport, onDismiss: () -> Unit) {
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
TextButton(onClick = onDismiss) { Text(stringResource(R.string.common_dismiss)) }
OutlinedButton(
onClick = {
IssueReport.copyToClipboard(context, reportText)
toast(context, "Crash report copied")
},
) { Text(stringResource(R.string.common_copy)) }
if (!showDetails) {
Button(onClick = { showDetails = true }) {
Text(stringResource(R.string.crash_review))
}
} else {
OutlinedButton(
onClick = {
IssueReport.copyToClipboard(context, reportText)
toast(context, copiedMessage)
},
) { Text(stringResource(R.string.common_copy)) }
// Universal, GitHub-free path: hand the full report to the
// system share sheet (email, chat apps, notes, Drive…). The
// user picks the destination, so nothing leaves the device
// until they choose to send it — same privacy posture as Copy.
OutlinedButton(
onClick = {
val shared = IssueReport.share(
context,
"Hermes-Relay crash report — ${report.shortTitle()}",
reportText,
chooserTitle = "Share crash report",
)
if (!shared) {
OutlinedButton(
onClick = {
val shared = IssueReport.share(
context,
reportSubject,
reportText,
chooserTitle = shareTitle,
)
if (!shared) {
IssueReport.copyToClipboard(context, reportText)
toast(context, noShareMessage)
}
onDismiss()
},
) { Text(stringResource(R.string.common_share)) }
Button(
onClick = {
IssueReport.copyToClipboard(context, reportText)
toast(context, "Report copied — no app found to share to")
}
onDismiss()
},
) { Text(stringResource(R.string.common_share)) }
Button(
onClick = {
// Copy the FULL report first; the URL only carries the
// head of the trace, so the user can paste the rest.
IssueReport.copyToClipboard(context, reportText)
val opened = IssueReport.openUrl(context, CrashReporter.buildGithubIssueUrl(report))
toast(
context,
if (opened) "Full report copied — paste into the issue if it's truncated"
else "Report copied — no browser found to open GitHub",
)
onDismiss()
},
) { Text(stringResource(R.string.common_report)) }
val opened = IssueReport.openUrl(context, CrashReporter.buildGithubIssueUrl(report))
toast(context, if (opened) openedMessage else noBrowserMessage)
onDismiss()
},
) { Text(stringResource(R.string.common_report)) }
}
}
}
}
@@ -106,9 +106,27 @@ fun DiagnosticDetailDialog(entry: DiagnosticLogEntry, onDismiss: () -> Unit) {
MetaRow("When", DateFormat.format("yyyy-MM-dd HH:mm:ss", entry.timestampMs).toString())
MetaRow("Severity", severityName)
MetaRow("Category", entry.category.label)
entry.operation?.let { MetaRow("Operation", it) }
entry.endpointRole?.let { MetaRow("Route", it) }
entry.url?.let { MetaRow("URL", it) }
entry.configuredUrl?.let { MetaRow("Configured URL", it) }
entry.requestUrl?.let { MetaRow("Request", it) }
if (entry.configuredUrl == null && entry.requestUrl == null) {
entry.url?.let { MetaRow("URL", it) }
}
entry.elapsedMs?.let { MetaRow("Elapsed", "${it}ms") }
entry.suggestion?.let { suggestion ->
Spacer(Modifier.height(10.dp))
Text(
text = "Suggested next step",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Text(
text = suggestion,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface,
)
}
Spacer(Modifier.height(14.dp))
val body = entry.stacktrace ?: entry.detail
@@ -269,9 +287,15 @@ private fun DiagnosticLogEntry.toPlainText(): String = buildString {
appendLine("Severity: ${severity.name}")
appendLine("Time: ${DateFormat.format("yyyy-MM-dd HH:mm:ss", timestampMs)}")
appendLine("App: ${BuildConfig.VERSION_NAME} (code ${BuildConfig.VERSION_CODE}) ${BuildConfig.FLAVOR}")
operation?.let { appendLine("Operation: $it") }
endpointRole?.let { appendLine("Route: $it") }
url?.let { appendLine("URL: $it") }
configuredUrl?.let { appendLine("Configured URL: $it") }
requestUrl?.let { appendLine("Request: $it") }
if (configuredUrl == null && requestUrl == null) {
url?.let { appendLine("URL: $it") }
}
elapsedMs?.let { appendLine("Elapsed: ${it}ms") }
suggestion?.let { appendLine("Suggested next step: $it") }
detail?.let {
appendLine()
appendLine("Detail:")
@@ -218,8 +218,10 @@ private fun severityColor(severity: DiagnosticSeverity): Color = when (severity)
private fun DiagnosticLogEntry.detailLine(): String? {
val pieces = listOfNotNull(
detail,
suggestion,
operation,
endpointRole?.let { "route=$it" },
url,
primaryUrl,
elapsedMs?.let { "${it}ms" },
)
return pieces.joinToString(" - ").takeIf { it.isNotBlank() }
@@ -18,6 +18,8 @@ import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
@@ -54,6 +56,7 @@ import com.hermesandroid.relay.data.PetBehaviorPreferences
import com.hermesandroid.relay.data.PetTemperament
import com.hermesandroid.relay.ui.components.avatar.AgentAvatar
import com.hermesandroid.relay.ui.components.avatar.AvatarRenderState
import com.hermesandroid.relay.ui.components.avatar.LocalPetGroundOpaqueBottom
import com.hermesandroid.relay.ui.components.avatar.PetLocomotion
import com.hermesandroid.relay.ui.components.pet.LocalPetSafeAreaRegistry
import com.hermesandroid.relay.ui.components.pet.PetLayoutDirection
@@ -106,6 +109,7 @@ import kotlin.math.roundToInt
import kotlin.math.sqrt
internal const val FLOATING_PET_COMPACT_HEIGHT_DP = 700
internal const val FLOATING_PET_SUPPORTED_RAIL_CLEARANCE_DP = 0f
internal const val CHAT_PET_WALK_REGION = "chat-composer-perch"
internal const val CHAT_PET_MESSAGE_PERCH_PREFIX = "chat-message-perch:"
internal const val CHAT_PET_ASSISTANT_MESSAGE_PERCH_PREFIX = "${CHAT_PET_MESSAGE_PERCH_PREFIX}assistant:"
@@ -220,6 +224,18 @@ internal fun floatingPetAcceptsPointerInput(
surfaceScrolling: Boolean,
): Boolean = positioned && !surfaceScrolling
/** The overlay's pre-measure zero size is not a valid coordinate space. */
internal fun shouldInitializeFloatingPet(
positioned: Boolean,
viewportWidth: Int,
viewportHeight: Int,
terrainReady: Boolean = true,
): Boolean = !positioned && viewportWidth > 0 && viewportHeight > 0 && terrainReady
/** Collision geometry must contain both the pointer target and rendered art. */
internal fun floatingPetCollisionSizePx(targetSizePx: Float, visualSizePx: Float): Float =
maxOf(targetSizePx, visualSizePx)
internal fun floatingPetRoamDelayMs(
hasMoved: Boolean,
roamIntervalMs: Long = PET_ROAM_REPEAT_DELAY_MS,
@@ -304,6 +320,31 @@ internal fun shouldDockFloatingPet(
roamingAllowed: Boolean,
): Boolean = !roamingEnabled || !roamingAllowed
/**
* Chat gives a deliberately paused companion one stable home: the live
* composer rail. Other routes retain the user's saved free-form edge home, and
* active roaming keeps its richer settled-chat habitat hierarchy.
*/
internal fun floatingPetHomePoint(
route: String?,
roamingEnabled: Boolean,
roamingAllowed: Boolean,
manualHomePoint: PetPoint,
roamingHomePoint: PetPoint?,
pausedChatDockPoint: PetPoint?,
): PetPoint = when {
route == "chat" && roamingAllowed && !roamingEnabled && pausedChatDockPoint != null -> {
pausedChatDockPoint
}
roamingEnabled && roamingAllowed -> roamingHomePoint ?: manualHomePoint
else -> manualHomePoint
}
internal fun floatingPetAllowsVerticalMoveActions(
route: String?,
roamingEnabled: Boolean,
): Boolean = route != "chat" || roamingEnabled
internal fun petRailSupportingPoint(
rails: Iterable<PetRoamingRail>,
point: PetPoint,
@@ -450,10 +491,19 @@ fun FloatingPetCompanion(
onResetPlacement: () -> Unit,
onHide: () -> Unit,
onOpenAppearance: () -> Unit,
onMenuExpandedChanged: (Boolean) -> Unit = {},
onExitTerrainDebug: () -> Unit = {},
modifier: Modifier = Modifier,
) {
var menuExpanded by remember(pet.id) { mutableStateOf(false) }
val latestOnMenuExpandedChanged by rememberUpdatedState(onMenuExpandedChanged)
fun setMenuExpanded(expanded: Boolean) {
menuExpanded = expanded
onMenuExpandedChanged(expanded)
}
DisposableEffect(pet.id) {
onDispose { latestOnMenuExpandedChanged(false) }
}
var dragging by remember(pet.id) { mutableStateOf(false) }
var draggedPoint by remember(pet.id) { mutableStateOf<PetPoint?>(null) }
var pendingDrop by remember(pet.id) { mutableStateOf<PendingPetDrop?>(null) }
@@ -490,12 +540,17 @@ fun FloatingPetCompanion(
val visualSize = dimensions.visualSizeDp.dp
val targetSizePx = with(density) { targetSize.toPx() }
val visualSizePx = with(density) { visualSize.toPx() }
val collisionSizePx = floatingPetCollisionSizePx(targetSizePx, visualSizePx)
val collisionSize = with(density) { collisionSizePx.toDp() }
val heldLiftPx = with(density) { 6.dp.toPx() }
val scrollReactionLiftPx = with(density) { PET_SCROLL_REACTION_LIFT_DP.dp.toPx() }
val maximumDirectHopPx = with(density) { PET_MAX_DIRECT_HOP_DP.dp.toPx() }
val maximumMessageHopPx = with(density) { PET_MAX_CLEAR_MESSAGE_HOP_DP.dp.toPx() }
val safeMarginPx = with(density) { 12.dp.toPx() }
val perchClearancePx = with(density) { 6.dp.toPx() }
// A supported collision box sits directly on its measured rail. Obstacle
// clearance remains part of [footprint]; adding it again here makes the
// visible pet hover above the surface.
val perchClearancePx = with(density) { FLOATING_PET_SUPPORTED_RAIL_CLEARANCE_DP.dp.toPx() }
val topClearancePx = with(density) { 76.dp.toPx() }
val bottomClearancePx = with(density) { (if (compact) 84.dp else 104.dp).toPx() }
val radius = targetSizePx / 2f
@@ -514,8 +569,8 @@ fun FloatingPetCompanion(
PetSafeBounds(left, top, right, bottom)
}
val registry = LocalPetSafeAreaRegistry.current
val footprint = remember(targetSizePx, safeMarginPx) {
PetFootprint(targetSizePx, targetSizePx, safeMarginPx / 2f)
val footprint = remember(collisionSizePx, safeMarginPx) {
PetFootprint(collisionSizePx, collisionSizePx, safeMarginPx / 2f)
}
// Bubble-edge hops are visual traversal, not persistent placement. Keep
// the larger accessible target for controls and viewport containment while
@@ -814,30 +869,47 @@ fun FloatingPetCompanion(
val requested = placement.sanitized().resolve(safeBounds, petLayoutDirection)
projectIntoSafeBounds(requested, safeBounds, registeredObstacles) ?: requested
}
val homeRail = remember(roamingRails, manualHomePoint, settledHabitat) {
val composerHomeRail = remember(composerRails, manualHomePoint) {
val distanceToHome: (PetRoamingRail) -> Float = { rail ->
rail.bounds.clamp(manualHomePoint).distanceSquaredTo(manualHomePoint)
}
composerRails.minByOrNull(distanceToHome)
}
val homeRail = remember(roamingRails, manualHomePoint, settledHabitat, composerHomeRail) {
val distanceToHome: (PetRoamingRail) -> Float = { rail ->
rail.bounds.clamp(manualHomePoint).distanceSquaredTo(manualHomePoint)
}
settledHabitat?.rail
?: roamingRails.filter { it.perchKey == CHAT_PET_WALK_REGION }.minByOrNull(distanceToHome)
?: composerHomeRail
?: roamingRails.minByOrNull(distanceToHome)
}
fun pointAtPlacementEdge(rail: PetSafeBounds): PetPoint {
val xAtEdge = if (
(placement.edge == PetLogicalEdge.Start) == (petLayoutDirection == PetLayoutDirection.Ltr)
) rail.left else rail.right
return PetPoint(xAtEdge, rail.top)
}
val roamingHomePoint = remember(placement.edge, homeRail, petLayoutDirection) {
homeRail?.bounds?.let { rail ->
homeRail?.bounds?.let(::pointAtPlacementEdge)
}
val pausedChatDockPoint = remember(placement.edge, composerHomeRail, petLayoutDirection) {
composerHomeRail?.bounds?.let { rail ->
val xAtEdge = if (
(placement.edge == PetLogicalEdge.Start) == (petLayoutDirection == PetLayoutDirection.Ltr)
) rail.left else rail.right
PetPoint(xAtEdge, rail.top)
}
}
// Enabling roaming explicitly docks onto the screen-owned safe rail. A
// manual drag or vertical accessibility action pauses roaming first, so the
// persisted free-form placement remains visible and authoritative.
val homePoint = if (roamingEnabled && roamingAllowed) {
roamingHomePoint ?: manualHomePoint
} else {
manualHomePoint
}
// Active roaming uses screen-owned terrain. Paused Chat deliberately uses
// the composer rail; other routes retain the persisted free-form edge home.
val homePoint = floatingPetHomePoint(
route = route,
roamingEnabled = roamingEnabled,
roamingAllowed = roamingAllowed,
manualHomePoint = manualHomePoint,
roamingHomePoint = roamingHomePoint,
pausedChatDockPoint = pausedChatDockPoint,
)
val heldProgress by animateFloatAsState(
targetValue = if (dragging) 1f else 0f,
@@ -1583,8 +1655,26 @@ fun FloatingPetCompanion(
return true
}
LaunchedEffect(pet.id, homePoint) {
if (!positioned) {
// Chat roaming owns a measured composer rail. Publishing before that rail
// exists lets initialization race route registration and can strand the
// pet on transient fallback geometry until direct manipulation.
val initialTerrainReady = route != "chat" || !roamingAllowed ||
composerRails.isNotEmpty()
LaunchedEffect(
pet.id,
homePoint,
viewportWidth,
viewportHeight,
initialTerrainReady,
) {
if (
shouldInitializeFloatingPet(
positioned = positioned,
viewportWidth = viewportWidth,
viewportHeight = viewportHeight,
terrainReady = initialTerrainReady,
)
) {
x.snapTo(homePoint.x)
y.snapTo(homePoint.y)
positioned = true
@@ -2211,11 +2301,14 @@ fun FloatingPetCompanion(
.offset {
val displayed = draggedPoint ?: PetPoint(x.value, y.value)
IntOffset(
(displayed.x - targetSizePx / 2f).roundToInt(),
(displayed.y - targetSizePx / 2f).roundToInt(),
(displayed.x - collisionSizePx / 2f).roundToInt(),
(displayed.y - collisionSizePx / 2f).roundToInt(),
)
}
.size(targetSize)
// The host, route footprint, and visible art share one center.
// A smaller pointer-only host shifts bottom-aligned art upward
// whenever the authored visual is larger than its touch target.
.size(collisionSize)
.alpha(if (positioned) 1f else 0f)
.graphicsLayer {
val scale = 1f + heldProgress * 0.10f
@@ -2309,31 +2402,43 @@ fun FloatingPetCompanion(
enabled = floatingPetAcceptsPointerInput(positioned, surfaceScrolling),
) {
tapReactionNonce += 1
menuExpanded = true
setMenuExpanded(true)
}
.semantics(mergeDescendants = true) {
role = Role.Button
contentDescription = companionDescription
stateDescription = stateLabel
customActions = listOf(
CustomAccessibilityAction(moveStartLabel) {
customActions = buildList {
add(CustomAccessibilityAction(moveStartLabel) {
onPlacementChanged(placement.copy(edge = PetLogicalEdge.Start)); true
},
CustomAccessibilityAction(moveEndLabel) {
})
add(CustomAccessibilityAction(moveEndLabel) {
onPlacementChanged(placement.copy(edge = PetLogicalEdge.End)); true
},
CustomAccessibilityAction(moveUpLabel) {
if (roamingEnabled) onRoamingEnabledChanged(false)
onPlacementChanged(placement.copy(verticalFraction = placement.verticalFraction - 0.15f)); true
},
CustomAccessibilityAction(moveDownLabel) {
if (roamingEnabled) onRoamingEnabledChanged(false)
onPlacementChanged(placement.copy(verticalFraction = placement.verticalFraction + 0.15f)); true
},
CustomAccessibilityAction(resetLabel) { onResetPlacement(); true },
CustomAccessibilityAction(appearanceLabel) { onOpenAppearance(); true },
CustomAccessibilityAction(hideLabel) { onHide(); true },
)
})
// A paused Chat pet has no meaningful vertical free-form
// coordinate: it is deliberately attached to the composer.
// Do not expose TalkBack actions that would appear to do
// nothing; start/end still choose the dock corner.
if (floatingPetAllowsVerticalMoveActions(route, roamingEnabled)) {
add(CustomAccessibilityAction(moveUpLabel) {
if (roamingEnabled) onRoamingEnabledChanged(false)
onPlacementChanged(
placement.copy(verticalFraction = placement.verticalFraction - 0.15f),
)
true
})
add(CustomAccessibilityAction(moveDownLabel) {
if (roamingEnabled) onRoamingEnabledChanged(false)
onPlacementChanged(
placement.copy(verticalFraction = placement.verticalFraction + 0.15f),
)
true
})
}
add(CustomAccessibilityAction(resetLabel) { onResetPlacement(); true })
add(CustomAccessibilityAction(appearanceLabel) { onOpenAppearance(); true })
add(CustomAccessibilityAction(hideLabel) { onHide(); true })
}
},
contentAlignment = Alignment.Center,
) {
@@ -2354,27 +2459,31 @@ fun FloatingPetCompanion(
),
)
key(pet.id) {
pet.Render(
state = state.copy(
petLocomotion = presentedPetLocomotion(
dragging = dragging,
dropping = pendingDrop != null,
agentState = state.state,
movement = locomotion,
CompositionLocalProvider(LocalPetGroundOpaqueBottom provides true) {
pet.Render(
state = state.copy(
petLocomotion = presentedPetLocomotion(
dragging = dragging,
dropping = pendingDrop != null,
agentState = state.state,
movement = locomotion,
),
paused = shouldPauseFloatingPet(
alreadyPaused = state.paused || !accessibleMotion.osAnimations ||
accessibleMotion.touchExploration,
animationEnabled = animationEnabled,
),
),
paused = shouldPauseFloatingPet(
alreadyPaused = state.paused || !accessibleMotion.osAnimations ||
accessibleMotion.touchExploration,
animationEnabled = animationEnabled,
),
),
modifier = Modifier.size(visualSize),
)
modifier = Modifier
.align(Alignment.BottomCenter)
.size(visualSize),
)
}
}
DropdownMenu(
expanded = menuExpanded,
onDismissRequest = { menuExpanded = false },
onDismissRequest = { setMenuExpanded(false) },
) {
DropdownMenuItem(
text = { Text("${pet.label} · $stateLabel", color = MaterialTheme.colorScheme.onSurfaceVariant) },
@@ -2391,28 +2500,28 @@ fun FloatingPetCompanion(
)
},
onClick = {
menuExpanded = false
setMenuExpanded(false)
onRoamingEnabledChanged(!roamingEnabled)
},
)
DropdownMenuItem(
text = { Text(resetLabel) },
onClick = {
menuExpanded = false
setMenuExpanded(false)
onResetPlacement()
},
)
DropdownMenuItem(
text = { Text(appearanceLabel) },
onClick = {
menuExpanded = false
setMenuExpanded(false)
onOpenAppearance()
},
)
DropdownMenuItem(
text = { Text(hideLabel) },
onClick = {
menuExpanded = false
setMenuExpanded(false)
onHide()
},
)
@@ -2,7 +2,6 @@ package com.hermesandroid.relay.ui.components
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.pdf.PdfRenderer
import android.media.MediaMetadataRetriever
import android.net.Uri
@@ -290,7 +289,7 @@ private fun ImageRender(
android.util.Base64.decode(attachment.content, android.util.Base64.DEFAULT)
else -> null
}
bytes?.let { BitmapFactory.decodeByteArray(it, 0, it.size)?.asImageBitmap() }
bytes?.let { decodeOrientedBitmap(it)?.asImageBitmap() }
}.getOrNull()
}
if (decoded != null) bitmap = decoded else decodeFailed = true
@@ -1,6 +1,11 @@
package com.hermesandroid.relay.ui.components
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
@@ -32,7 +37,12 @@ import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.VolumeUp
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.FormatQuote
import androidx.compose.material.icons.filled.Stop
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
@@ -40,7 +50,6 @@ import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
@@ -58,6 +67,7 @@ import androidx.compose.ui.platform.LocalLocale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
@@ -70,13 +80,16 @@ import com.hermesandroid.relay.data.HermesCardAction
import com.hermesandroid.relay.data.MediaSettingsRepository
import com.hermesandroid.relay.data.MessageDeliveryStatus
import com.hermesandroid.relay.data.MessageRole
import com.hermesandroid.relay.data.parseChatQuotedPrompt
import com.hermesandroid.relay.ui.components.pet.petObstacleSurface
import com.hermesandroid.relay.ui.components.pet.petPerchSurface
import com.hermesandroid.relay.ui.components.pet.petVisitTargetSurface
import com.hermesandroid.relay.ui.theme.leftEdgeGlow
import kotlinx.coroutines.delay
import java.text.SimpleDateFormat
import java.util.Date
internal const val CHAT_PET_IDENTITY_OBSTACLE_PREFIX = "chat-message-identity:"
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MessageBubble(
@@ -87,23 +100,27 @@ fun MessageBubble(
isFirstInGroup: Boolean = true,
isLastInGroup: Boolean = true,
/**
* Keeps a just-completed live tail on its stable Text layout. The owning
* list releases this once another row becomes the tail, allowing full
* Markdown to render without disturbing the visible bottom anchor.
* Keeps the current live tail on its stable Text layout while a final
* streaming frame commits. The owning list releases it immediately after
* completion so the same row transitions to full Markdown with its bottom
* anchor preserved.
*/
retainStreamingLayout: Boolean = false,
onCopyMessage: (String) -> Unit = {},
/**
* Quote this message into the input field. Null hides the Quote entry in
* the long-press menu, so legacy call sites keep the copy-only behavior.
* Select this message as a structured composer quote. Null hides Quote.
*/
onQuoteMessage: ((String) -> Unit)? = null,
onQuoteMessage: ((ChatMessage) -> Unit)? = null,
/** Navigate a rendered quote chip to its original message id. */
onNavigateToMessage: ((String) -> Unit)? = null,
/**
* Reads a completed assistant response through the active voice renderer.
* Null hides the entry; the owning screen uses that to limit the action to
* idle Conversation voice sessions.
*/
onSpeakMessage: ((String) -> Unit)? = null,
/** Stops a message-context narration currently owned by the voice pipeline. */
onStopSpeaking: (() -> Unit)? = null,
/**
* Invoked when the user taps a FAILED inbound attachment card.
* `attachmentIndex` is the position in [ChatMessage.attachments] so the
@@ -149,6 +166,7 @@ fun MessageBubble(
imageGenerationRotationIndex: Int = 0,
petVisitTargetKey: String? = null,
petPerchKey: String? = null,
animationEnabled: Boolean = true,
) {
val isUser = message.role == MessageRole.USER
val isSystem = message.role == MessageRole.SYSTEM
@@ -202,18 +220,19 @@ fun MessageBubble(
val alignment = if (isUser) Alignment.End else Alignment.Start
val locale = LocalLocale.current.platformLocale
val timeFormat = remember(locale) { SimpleDateFormat("h:mm a", locale) }
val a11yDescription = "${message.role.name.lowercase()} message: ${message.content.take(100)}"
val quoteEnvelope = remember(message.content) { parseChatQuotedPrompt(message.content) }
val visibleMessageContent = quoteEnvelope?.body ?: message.content
val isDarkTheme = LocalBrand.current.isDark
// Pull generated/inline image links (`![alt](src)`) out of assistant
// content so they render as real images (remote URLs via Coil) or a
// graceful inline notice — not the blank element the markdown renderer
// emits for an image link. User/system bubbles keep their raw content.
val (markdownBody, inlineImages) = remember(message.content, isUser, isSystem) {
val (markdownBody, inlineImages) = remember(visibleMessageContent, isUser, isSystem) {
if (isUser || isSystem) {
message.content to emptyList()
visibleMessageContent to emptyList()
} else {
extractChatInlineImages(message.content)
extractChatInlineImages(visibleMessageContent)
}
}
val showImageGeneration = shouldShowImageGenerationPlaceholder(
@@ -221,6 +240,26 @@ fun MessageBubble(
isStreaming = message.isStreaming,
hasMediaResult = message.attachments.isNotEmpty() || inlineImages.isNotEmpty(),
)
val streamingStatusLabel = if (
!isUser &&
!isSystem &&
message.isStreaming &&
message.content.isBlank() &&
!showImageGeneration
) {
if (recoveringAnswer) {
stringResource(R.string.msg_bubble_reconnecting)
} else {
stringResource(R.string.msg_bubble_still_working)
}
} else {
null
}
val a11yDescription = buildString {
append(message.role.name.lowercase())
append(" message: ")
append(streamingStatusLabel ?: visibleMessageContent.take(100))
}
val hasImageGenerationCall = remember(message.toolCalls) {
message.toolCalls.any {
it.name.trim().lowercase() == "image_generate"
@@ -249,52 +288,36 @@ fun MessageBubble(
val blurMode by blurRepo.blurMode.collectAsState(initial = BlurMode.FLAGGED)
CompositionLocalProvider(LocalMediaBlurMode provides blurMode) {
// The active profile image is sender identity, distinct from both the
// floating pet companion and the ambient Sphere. Reserve one stable gutter
// for an assistant run and render the identity only on its first message.
Row(
Column(
modifier = modifier.fillMaxWidth(),
verticalAlignment = Alignment.Top,
horizontalAlignment = alignment,
) {
if (!isUser && !isSystem) {
Box(
modifier = Modifier.width(40.dp),
contentAlignment = Alignment.TopCenter,
) {
if (shouldShowMessageGroupAvatar(
isUser = isUser,
isSystem = isSystem,
isFirstInGroup = isFirstInGroup,
agentName = message.agentName,
)
// Keep sender identity in the first-message label rather than a
// persistent leading column. Long responses and every follow-up in the
// group therefore retain the full bubble-width allowance.
if (!isUser && !isSystem && isFirstInGroup && !message.agentName.isNullOrBlank()) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(6.dp),
modifier = Modifier
.padding(bottom = 3.dp, start = 4.dp)
.petObstacleSurface(
key = "$CHAT_PET_IDENTITY_OBSTACLE_PREFIX${message.uiKey}",
routes = setOf("chat"),
),
) {
Surface(
// Decorative: the adjacent visible agent name already owns
// the identity announcement, avoiding duplicate TalkBack copy.
modifier = Modifier.size(32.dp),
modifier = Modifier.size(24.dp),
shape = CircleShape,
color = MaterialTheme.colorScheme.primary,
) {
AgentAvatarFace(
name = message.agentName.orEmpty(),
letterStyle = MaterialTheme.typography.labelMedium,
letterStyle = MaterialTheme.typography.labelSmall,
)
}
}
}
}
Column(
modifier = Modifier.weight(1f),
horizontalAlignment = alignment
) {
// Agent name label sits beside the first group identity avatar. Pet
// companions never enter this row.
if (!isUser && !isSystem && isFirstInGroup && !message.agentName.isNullOrBlank()) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(4.dp),
modifier = Modifier.padding(bottom = 2.dp, start = 4.dp),
) {
Text(
text = localizeAgentName(message.agentName),
style = MaterialTheme.typography.labelSmall,
@@ -329,7 +352,7 @@ fun MessageBubble(
}
// Thinking block (above the bubble, only for assistant messages)
if (!isUser && showThinking && message.thinkingContent.isNotEmpty()) {
if (!isUser && showThinking && message.thinkingContent.isNotBlank()) {
ThinkingBlock(
thinkingContent = message.thinkingContent,
isStreaming = message.isThinkingStreaming,
@@ -378,12 +401,22 @@ fun MessageBubble(
// carries only thinking and/or tool calls (both rendered OUTSIDE
// this Surface — the ThinkingBlock above, the tool pills as separate
// rows) would otherwise paint a bare timestamp-only chip between the
// Thought-process block and the tool pill. Keep the bubble while
// streaming (StreamingDots is the live "working" indicator) and
// whenever there are cards/attachments to render inside it.
// Thought-process block and the tool pill. The first-token working state
// is rendered directly in the conversation
// lane below, without an opaque bubble. Cards and attachments still own
// a normal bubble even when response prose has not arrived yet.
streamingStatusLabel?.let { streamingStatus ->
StandaloneStreamingStatus(
status = streamingStatus,
accessibilityDescription = a11yDescription,
textColor = textColor,
modifier = Modifier.padding(start = 12.dp, top = 4.dp, bottom = 6.dp),
)
}
val showBubble = isUser || isSystem ||
message.content.isNotBlank() ||
message.isStreaming ||
visibleMessageContent.isNotBlank() ||
quoteEnvelope != null ||
showImageGeneration ||
message.cards.isNotEmpty() ||
message.attachments.isNotEmpty() ||
@@ -398,19 +431,25 @@ fun MessageBubble(
modifier = Modifier
.padding(top = 8.dp, bottom = 8.dp, end = 6.dp)
.width(3.dp)
.height(if (message.content.isBlank()) 14.dp else 24.dp)
.height(if (visibleMessageContent.isBlank()) 14.dp else 24.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.tertiary.copy(alpha = 0.85f))
)
}
// Long-press opens a compact action menu when a quote handler is
// wired; with copy as the only action it stays a direct copy so the
// one-action case doesn't pay a menu tap.
Column(horizontalAlignment = alignment) {
// A normal tap reveals the compact action strip. Long-press preserves
// the existing overflow menu (or direct-copy shortcut when Copy is the
// only available action).
var showMessageActions by remember { mutableStateOf(false) }
var showInlineActions by remember(message.uiKey) { mutableStateOf(false) }
val haptic = LocalHapticFeedback.current
val accessibleMotion = rememberAccessibleMotionState()
val animateInlineActions = animationEnabled && accessibleMotion.osAnimations &&
!accessibleMotion.touchExploration
val showEditAction = onEditMessage != null && isUser
val showSpeakAction = shouldShowSpeakResponseAction(message, onSpeakMessage != null)
if (onQuoteMessage != null || showEditAction || showSpeakAction) {
val showStopSpeakingAction = shouldShowStopSpeakingAction(message, onStopSpeaking != null)
if (onQuoteMessage != null || showEditAction || showSpeakAction || showStopSpeakingAction) {
DropdownMenu(
expanded = showMessageActions,
onDismissRequest = { showMessageActions = false },
@@ -419,7 +458,7 @@ fun MessageBubble(
text = { Text(stringResource(R.string.msg_bubble_copy)) },
onClick = {
showMessageActions = false
onCopyMessage(message.content)
onCopyMessage(visibleMessageContent)
},
)
if (onQuoteMessage != null) {
@@ -427,7 +466,7 @@ fun MessageBubble(
text = { Text(stringResource(R.string.msg_bubble_quote)) },
onClick = {
showMessageActions = false
onQuoteMessage(message.content)
onQuoteMessage(message.copy(content = visibleMessageContent))
},
)
}
@@ -442,7 +481,22 @@ fun MessageBubble(
},
onClick = {
showMessageActions = false
onSpeakMessage?.invoke(message.content)
onSpeakMessage?.invoke(visibleMessageContent)
},
)
}
if (showStopSpeakingAction) {
DropdownMenuItem(
text = { Text(stringResource(R.string.msg_bubble_stop_speaking)) },
leadingIcon = {
Icon(
imageVector = Icons.Filled.Stop,
contentDescription = null,
)
},
onClick = {
showMessageActions = false
onStopSpeaking?.invoke()
},
)
}
@@ -491,16 +545,19 @@ fun MessageBubble(
} else Modifier
)
.combinedClickable(
onClick = {},
onClick = { showInlineActions = !showInlineActions },
onLongClick = {
// Buzz the instant the long-press registers — opening the
// action menu is the discoverability moment, so it gets the
// same tactile confirm every chat app fires.
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
if (onQuoteMessage != null || showEditAction || showSpeakAction) {
if (
onQuoteMessage != null || showEditAction || showSpeakAction ||
showStopSpeakingAction
) {
showMessageActions = true
} else {
onCopyMessage(message.content)
onCopyMessage(visibleMessageContent)
}
}
)
@@ -526,12 +583,23 @@ fun MessageBubble(
}
)
) {
Column(modifier = Modifier.padding(horizontal = 14.dp, vertical = 9.dp)) {
Column(
modifier = Modifier.padding(horizontal = 14.dp, vertical = 9.dp),
) {
quoteEnvelope?.let { envelope ->
ChatQuoteReferenceChip(
reference = envelope.reference,
onOpenOriginal = onNavigateToMessage?.let { navigate ->
{ navigate(envelope.reference.messageId) }
},
modifier = Modifier.padding(bottom = 7.dp),
)
}
val messageTextContent: @Composable () -> Unit = {
if (isUser || isSystem) {
// Plain text for user and system messages
Text(
text = message.content,
text = visibleMessageContent,
style = MaterialTheme.typography.bodyMedium.copy(
fontSize = 15.sp,
lineHeight = 21.sp,
@@ -539,11 +607,10 @@ fun MessageBubble(
color = textColor
)
} else {
// Keep one plain Text node stable while content grows
// and while this completed response remains the visible
// tail. Full Markdown renders once another row becomes
// the tail (or the session is revisited), where its
// remeasure cannot reset the active viewport.
// Keep one plain Text node stable only while content is
// incomplete (plus the final committed live frame).
// Completion releases this flag and selects the full
// Markdown tree on the same stable message row.
if (markdownBody.isNotEmpty()) {
StreamingMarkdownContent(
content = markdownBody,
@@ -558,7 +625,7 @@ fun MessageBubble(
// a live Text node becomes a Markdown tree, or settled
// Markdown content changes its node topology, so a handle
// cannot keep pointing at a removed selectable.
if (showSpeakAction) {
if (showSpeakAction || showStopSpeakingAction) {
DisableSelection { messageTextContent() }
} else {
key(
@@ -686,78 +753,25 @@ fun MessageBubble(
}
}
// Streaming indicator — only while awaiting the first token. Once
// text starts flowing, the growing reply is itself the progress
// signal, so the pulsing dots stop (Messenger/Telegram drop the
// typing bubble the moment content appears) instead of throbbing
// under the text for the whole turn.
if (
message.isStreaming &&
message.content.isBlank() &&
!showImageGeneration
) {
// After a few seconds with no content yet, escalate the bare
// dots to a labeled "Still working…" so a slow first token
// never reads as a hang on the SSE / sessions paths.
val awaitingFirstToken = message.content.isBlank()
var showStillWorking by remember(message.id) { mutableStateOf(false) }
LaunchedEffect(message.id, awaitingFirstToken) {
showStillWorking = false
if (awaitingFirstToken) {
delay(4_000)
showStillWorking = true
}
}
val thinkingIndicator = LocalThinkingIndicator.current
Row(verticalAlignment = Alignment.CenterVertically) {
when (thinkingIndicator.style) {
ThinkingIndicatorStyle.Matrix -> DotMatrixIndicator(
// Auto follows the bubble text color; accents
// come from the brand palette. The grid modulates
// its own alpha (idle dots ≈0.18, lit dots 1.0).
color = thinkingIndicator.color.toColor(autoColor = textColor),
pattern = thinkingIndicator.pattern,
animated = thinkingIndicator.animated,
modifier = Modifier.padding(top = 4.dp),
)
ThinkingIndicatorStyle.Dots -> StreamingDots(
color = textColor.copy(alpha = 0.6f),
modifier = Modifier.padding(top = 4.dp),
)
}
// During dropped-stream answer recovery the label shows
// immediately (the 4s escalation is for a slow first
// token; a recovery is already known to be slow).
if ((showStillWorking || recoveringAnswer) && awaitingFirstToken) {
Spacer(modifier = Modifier.width(8.dp))
Text(
text = if (recoveringAnswer) {
stringResource(R.string.msg_bubble_reconnecting)
} else {
stringResource(R.string.msg_bubble_still_working)
},
style = MaterialTheme.typography.labelSmall,
color = textColor.copy(alpha = 0.6f),
modifier = Modifier.padding(top = 4.dp),
)
}
}
}
// Timestamp — only on the LAST bubble of a same-author run so a
// burst of fragments doesn't stack three near-touching time labels.
// Grouping breaks on a >5min gap (ChatScreen), so every pause still
// surfaces its own time. Alpha floored at 0.6 for 11sp contrast.
// Keep the live bubble's footer structurally quiet. Showing a
// timestamp while text is still growing makes it chase every
// token and exaggerates any single-frame layout lag. Reveal it
// once the message settles into its final layout.
if (isLastInGroup && !message.isStreaming) {
// Reserve the footer from the first streaming frame so
// completion is a color-only transition and cannot resize the
// row. Hide the reserved timestamp from accessibility until it
// becomes visible.
if (isLastInGroup) {
Spacer(modifier = Modifier.height(2.dp))
Text(
text = timeFormat.format(Date(message.timestamp)),
style = MaterialTheme.typography.labelSmall,
color = textColor.copy(alpha = 0.6f)
color = textColor.copy(alpha = if (message.isStreaming) 0f else 0.6f),
modifier = if (message.isStreaming) {
Modifier.clearAndSetSemantics { }
} else {
Modifier
},
)
}
@@ -765,23 +779,17 @@ fun MessageBubble(
// message routed over the relay proactive channel). Null on every
// ordinary chat message, which render nothing here.
message.deliveryStatus?.takeIf { isUser }?.let { status ->
val label = stringResource(
when (status) {
MessageDeliveryStatus.SENDING -> R.string.msg_bubble_sending
MessageDeliveryStatus.DELIVERED -> R.string.msg_bubble_delivered
MessageDeliveryStatus.FAILED -> R.string.msg_bubble_not_sent
}
)
val alpha = when (status) {
MessageDeliveryStatus.SENDING -> 0.5f
MessageDeliveryStatus.DELIVERED -> 0.5f
MessageDeliveryStatus.FAILED -> 0.7f
}
Spacer(modifier = Modifier.height(2.dp))
Text(
text = label,
style = MaterialTheme.typography.labelSmall,
color = textColor.copy(alpha = alpha),
MessageDeliveryIndicator(
status = status,
text = MessageDeliveryIndicatorText(
sending = stringResource(R.string.msg_bubble_sending),
queued = stringResource(R.string.msg_bubble_queued),
steered = stringResource(R.string.msg_bubble_steered),
delivered = stringResource(R.string.msg_bubble_delivered),
failed = stringResource(R.string.msg_bubble_not_sent),
tapToRetry = stringResource(R.string.chat_retry),
),
)
}
@@ -795,13 +803,127 @@ fun MessageBubble(
}
}
}
val inlineActions: @Composable () -> Unit = {
MessageInlineActions(
showQuote = onQuoteMessage != null,
showSpeak = showSpeakAction,
showStopSpeaking = showStopSpeakingAction,
showEdit = showEditAction,
onCopy = {
showInlineActions = false
onCopyMessage(visibleMessageContent)
},
onQuote = {
showInlineActions = false
onQuoteMessage?.invoke(message.copy(content = visibleMessageContent))
},
onSpeak = {
showInlineActions = false
onSpeakMessage?.invoke(visibleMessageContent)
},
onStopSpeaking = {
showInlineActions = false
onStopSpeaking?.invoke()
},
onEdit = {
showInlineActions = false
onEditMessage?.invoke(message)
},
)
}
if (animateInlineActions) {
AnimatedVisibility(
visible = showInlineActions,
enter = fadeIn(tween(120)) + expandVertically(
animationSpec = tween(180, easing = LinearOutSlowInEasing),
expandFrom = Alignment.Top,
),
exit = fadeOut(tween(90)) + shrinkVertically(
animationSpec = tween(140),
shrinkTowards = Alignment.Top,
),
) {
inlineActions()
}
} else if (showInlineActions) {
inlineActions()
}
} // end Column (bubble + revealed actions)
} // end Row (bubble + optional leading accent bar)
} // end if (showBubble)
} // end content Column
} // end Row (avatar gutter + content)
} // end CompositionLocalProvider(LocalMediaBlurMode)
}
@Composable
private fun MessageInlineActions(
showQuote: Boolean,
showSpeak: Boolean,
showStopSpeaking: Boolean,
showEdit: Boolean,
onCopy: () -> Unit,
onQuote: () -> Unit,
onSpeak: () -> Unit,
onStopSpeaking: () -> Unit,
onEdit: () -> Unit,
) {
Surface(
shape = RoundedCornerShape(12.dp),
color = MaterialTheme.colorScheme.surfaceContainerHigh,
tonalElevation = 2.dp,
modifier = Modifier.padding(top = 2.dp),
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(horizontal = 2.dp),
) {
IconButton(onClick = onCopy, modifier = Modifier.size(48.dp)) {
Icon(
imageVector = Icons.Filled.ContentCopy,
contentDescription = stringResource(R.string.msg_bubble_copy),
modifier = Modifier.size(18.dp),
)
}
if (showQuote) {
IconButton(onClick = onQuote, modifier = Modifier.size(48.dp)) {
Icon(
imageVector = Icons.Filled.FormatQuote,
contentDescription = stringResource(R.string.msg_bubble_quote),
modifier = Modifier.size(18.dp),
)
}
}
if (showSpeak) {
IconButton(onClick = onSpeak, modifier = Modifier.size(48.dp)) {
Icon(
imageVector = Icons.AutoMirrored.Filled.VolumeUp,
contentDescription = stringResource(R.string.msg_bubble_speak_response),
modifier = Modifier.size(18.dp),
)
}
}
if (showStopSpeaking) {
IconButton(onClick = onStopSpeaking, modifier = Modifier.size(48.dp)) {
Icon(
imageVector = Icons.Filled.Stop,
contentDescription = stringResource(R.string.msg_bubble_stop_speaking),
modifier = Modifier.size(18.dp),
)
}
}
if (showEdit) {
IconButton(onClick = onEdit, modifier = Modifier.size(48.dp)) {
Icon(
imageVector = Icons.Filled.Edit,
contentDescription = stringResource(R.string.msg_bubble_edit),
modifier = Modifier.size(18.dp),
)
}
}
}
}
}
internal data class MessageSelectionTopologyKey(
val renderer: String,
val markdownBody: String?,
@@ -875,6 +997,15 @@ internal fun shouldShowSpeakResponseAction(
!message.isStreaming &&
message.content.isNotBlank()
internal fun shouldShowStopSpeakingAction(
message: ChatMessage,
handlerAvailable: Boolean,
): Boolean =
handlerAvailable &&
message.role == MessageRole.ASSISTANT &&
!message.isStreaming &&
message.content.isNotBlank()
internal fun shouldShowMessageGroupAvatar(
isUser: Boolean,
isSystem: Boolean,
@@ -912,6 +1043,42 @@ private fun MessagePathBadge(text: String, leadingIcon: ImageVector? = null) {
}
}
/** First-token progress rendered in the conversation lane, not inside a message bubble. */
@Composable
private fun StandaloneStreamingStatus(
status: String,
accessibilityDescription: String,
textColor: Color,
modifier: Modifier = Modifier,
) {
val thinkingIndicator = LocalThinkingIndicator.current
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(5.dp),
modifier = modifier.clearAndSetSemantics {
contentDescription = accessibilityDescription
},
) {
when (thinkingIndicator.style) {
ThinkingIndicatorStyle.Matrix -> DotMatrixIndicator(
color = thinkingIndicator.color.toColor(autoColor = textColor),
pattern = thinkingIndicator.pattern,
animated = thinkingIndicator.animated,
)
ThinkingIndicatorStyle.Dots -> StreamingDots(
color = textColor.copy(alpha = 0.6f),
)
}
Text(
text = status,
style = MaterialTheme.typography.labelSmall,
color = textColor.copy(alpha = 0.68f),
maxLines = 2,
overflow = TextOverflow.Ellipsis,
)
}
}
/**
* Three dots that animate opacity in sequence to indicate streaming is in progress.
*/
@@ -0,0 +1,167 @@
package com.hermesandroid.relay.ui.components
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Send
import androidx.compose.material.icons.filled.Bolt
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.ErrorOutline
import androidx.compose.material.icons.filled.Schedule
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.minimumInteractiveComponentSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.data.MessageDeliveryStatus
import com.hermesandroid.relay.ui.theme.relayMetadataStyle
/**
* Localized copy for [MessageDeliveryIndicator]. Keeping copy at the call site
* lets MessageBubble use Android string resources without coupling this small
* presentation component to a particular resource catalog.
*/
data class MessageDeliveryIndicatorText(
val sending: String,
val queued: String,
val steered: String,
val delivered: String,
val failed: String,
val tapToRetry: String,
)
/**
* Compact, accessible lifecycle feedback for a user-authored chat message.
*
* Every state has both a distinct icon and a visible text label, so meaning is
* never carried by color alone. A failed state becomes a button only when
* [onRetry] is supplied; its entire 48dp row is then the retry target.
*/
@Composable
fun MessageDeliveryIndicator(
status: MessageDeliveryStatus,
text: MessageDeliveryIndicatorText,
modifier: Modifier = Modifier,
failureMessage: String? = null,
onRetry: (() -> Unit)? = null,
) {
val retryAction = onRetry.takeIf { status == MessageDeliveryStatus.FAILED }
val retryable = retryAction != null
val line = messageDeliveryStatusLine(
status = status,
text = text,
failureMessage = failureMessage,
retryable = retryable,
)
val presentation = messageDeliveryPresentation(status)
val tint = when (presentation.colorRole) {
DeliveryColorRole.NEUTRAL -> MaterialTheme.colorScheme.onSurfaceVariant
DeliveryColorRole.ACCENT -> MaterialTheme.colorScheme.primary
DeliveryColorRole.QUEUED -> MaterialTheme.colorScheme.tertiary
DeliveryColorRole.ERROR -> MaterialTheme.colorScheme.error
}
Row(
modifier = modifier
.then(
if (retryable) {
Modifier
.minimumInteractiveComponentSize()
.clickable(
role = Role.Button,
onClickLabel = text.tapToRetry,
onClick = retryAction,
)
} else {
Modifier
},
)
.semantics(mergeDescendants = true) {
contentDescription = line
liveRegion = LiveRegionMode.Polite
}
.padding(horizontal = 2.dp, vertical = 2.dp),
horizontalArrangement = Arrangement.spacedBy(4.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
imageVector = presentation.icon,
contentDescription = null,
tint = tint,
modifier = Modifier.size(14.dp),
)
Text(
text = line,
style = relayMetadataStyle(),
color = tint,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
)
}
}
internal fun messageDeliveryStatusLine(
status: MessageDeliveryStatus,
text: MessageDeliveryIndicatorText,
failureMessage: String? = null,
retryable: Boolean = false,
): String {
val label = when (status) {
MessageDeliveryStatus.SENDING -> text.sending
MessageDeliveryStatus.QUEUED -> text.queued
MessageDeliveryStatus.STEERED -> text.steered
MessageDeliveryStatus.DELIVERED -> text.delivered
MessageDeliveryStatus.FAILED -> text.failed
}
if (status != MessageDeliveryStatus.FAILED) return label
return buildList {
add(label)
failureMessage?.trim()?.takeIf(String::isNotEmpty)?.let(::add)
if (retryable) add(text.tapToRetry)
}.joinToString(" · ")
}
private data class DeliveryPresentation(
val icon: ImageVector,
val colorRole: DeliveryColorRole,
)
private enum class DeliveryColorRole { NEUTRAL, ACCENT, QUEUED, ERROR }
private fun messageDeliveryPresentation(status: MessageDeliveryStatus): DeliveryPresentation =
when (status) {
MessageDeliveryStatus.SENDING -> DeliveryPresentation(
icon = Icons.AutoMirrored.Filled.Send,
colorRole = DeliveryColorRole.NEUTRAL,
)
MessageDeliveryStatus.QUEUED -> DeliveryPresentation(
icon = Icons.Filled.Schedule,
colorRole = DeliveryColorRole.QUEUED,
)
MessageDeliveryStatus.STEERED -> DeliveryPresentation(
icon = Icons.Filled.Bolt,
colorRole = DeliveryColorRole.ACCENT,
)
MessageDeliveryStatus.DELIVERED -> DeliveryPresentation(
icon = Icons.Filled.CheckCircle,
colorRole = DeliveryColorRole.ACCENT,
)
MessageDeliveryStatus.FAILED -> DeliveryPresentation(
icon = Icons.Filled.ErrorOutline,
colorRole = DeliveryColorRole.ERROR,
)
}
@@ -85,7 +85,12 @@ fun ModelPickerSheet(
// (the caller sorts is_current to the front).
val otherGroupLabel = stringResource(R.string.model_picker_other)
val grouped = remember(filtered, otherGroupLabel) {
filtered.groupBy { it.group?.takeIf { g -> g.isNotBlank() } ?: otherGroupLabel }
filtered.groupBy { option ->
ModelPickerGroupIdentity(
provider = option.provider?.trim()?.lowercase().orEmpty(),
label = option.group?.takeIf { it.isNotBlank() } ?: otherGroupLabel,
)
}
}
ModalBottomSheet(
@@ -180,10 +185,10 @@ fun ModelPickerSheet(
}
}
grouped.forEach { (provider, opts) ->
item(key = "header_$provider") {
grouped.forEach { (group, opts) ->
item(key = modelPickerHeaderKey(group)) {
Text(
text = provider,
text = group.label,
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(
@@ -191,7 +196,7 @@ fun ModelPickerSheet(
),
)
}
items(items = opts, key = { "$provider:${it.value}" }) { opt ->
items(items = opts, key = ::modelPickerOptionKey) { opt ->
ModelPickerRow(option = opt, onClick = { onSelect(opt) })
}
}
@@ -217,6 +222,18 @@ fun ModelPickerSheet(
}
}
internal data class ModelPickerGroupIdentity(
val provider: String,
val label: String,
)
internal fun modelPickerHeaderKey(group: ModelPickerGroupIdentity): String =
"provider:${group.provider}:${group.label}"
/** Exact domain identity; duplicate provider/model rows must be repaired upstream. */
internal fun modelPickerOptionKey(option: ChatInputPickerOption): String =
"model:${option.provider?.trim()?.lowercase().orEmpty()}:${option.value}"
/**
* Compact sibling of [ModelPickerSheet] for short, ungrouped control lists.
* Passport selectors use this so profile, personality, and reasoning choices
@@ -0,0 +1,64 @@
package com.hermesandroid.relay.ui.components
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import androidx.exifinterface.media.ExifInterface
import java.io.ByteArrayInputStream
internal data class ImageOrientationTransform(
val rotationDegrees: Float = 0f,
val flipHorizontal: Boolean = false,
)
internal fun imageOrientationTransform(orientation: Int): ImageOrientationTransform = when (orientation) {
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> ImageOrientationTransform(flipHorizontal = true)
ExifInterface.ORIENTATION_ROTATE_180 -> ImageOrientationTransform(rotationDegrees = 180f)
ExifInterface.ORIENTATION_FLIP_VERTICAL ->
ImageOrientationTransform(rotationDegrees = 180f, flipHorizontal = true)
ExifInterface.ORIENTATION_TRANSPOSE ->
ImageOrientationTransform(rotationDegrees = 90f, flipHorizontal = true)
ExifInterface.ORIENTATION_ROTATE_90 -> ImageOrientationTransform(rotationDegrees = 90f)
ExifInterface.ORIENTATION_TRANSVERSE ->
ImageOrientationTransform(rotationDegrees = -90f, flipHorizontal = true)
ExifInterface.ORIENTATION_ROTATE_270 -> ImageOrientationTransform(rotationDegrees = -90f)
else -> ImageOrientationTransform()
}
/**
* Decode image bytes for display and apply their EXIF orientation. BitmapFactory
* returns the stored pixel layout and otherwise leaves portrait phone photos
* sideways when the camera encoded rotation as metadata.
*/
internal fun decodeOrientedBitmap(
bytes: ByteArray,
options: BitmapFactory.Options? = null,
): Bitmap? {
if (bytes.isEmpty()) return null
val decoded = if (options == null) {
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
} else {
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options)
} ?: return null
val orientation = runCatching {
ByteArrayInputStream(bytes).use { stream ->
ExifInterface(stream).getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL,
)
}
}.getOrDefault(ExifInterface.ORIENTATION_NORMAL)
val transform = imageOrientationTransform(orientation)
if (transform.rotationDegrees == 0f && !transform.flipHorizontal) return decoded
val matrix = Matrix().apply {
if (transform.rotationDegrees != 0f) postRotate(transform.rotationDegrees)
if (transform.flipHorizontal) postScale(-1f, 1f)
}
return runCatching {
Bitmap.createBitmap(decoded, 0, 0, decoded.width, decoded.height, matrix, true)
}.getOrNull()?.also { oriented ->
if (oriented !== decoded) decoded.recycle()
} ?: decoded
}
@@ -0,0 +1,325 @@
package com.hermesandroid.relay.ui.components
import android.graphics.BitmapFactory
import android.util.Base64
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.ArrowForward
import androidx.compose.material.icons.filled.AttachFile
import androidx.compose.material.icons.filled.BrokenImage
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.R
import com.hermesandroid.relay.data.Attachment
import com.hermesandroid.relay.data.AttachmentRenderMode
import com.hermesandroid.relay.data.AttachmentState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
/**
* Pending files shown immediately above the chat composer.
*
* The owner keeps attachment state and handles the four stable integration
* callbacks. [onMove] receives the current and requested indices, allowing a
* ViewModel or session-owned draft store to perform one atomic reorder.
*/
@Composable
fun PendingAttachmentComposer(
attachments: List<Attachment>,
onPreview: (attachment: Attachment, index: Int) -> Unit,
onRemove: (index: Int) -> Unit,
onMove: (fromIndex: Int, toIndex: Int) -> Unit,
modifier: Modifier = Modifier,
imageDecoder: suspend (Attachment) -> ImageBitmap? = ::decodePendingAttachmentImage,
) {
if (attachments.isEmpty()) return
val attachmentLabel = stringResource(R.string.attachment_title)
Row(
modifier = modifier
.fillMaxWidth()
.horizontalScroll(rememberScrollState())
.semantics {
contentDescription = "${attachments.size} $attachmentLabel"
}
.testTag("pending-attachment-composer"),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
attachments.forEachIndexed { index, attachment ->
PendingAttachmentItem(
attachment = attachment,
index = index,
count = attachments.size,
onPreview = { onPreview(attachment, index) },
onRemove = { onRemove(index) },
onMoveLeft = { onMove(index, index - 1) },
onMoveRight = { onMove(index, index + 1) },
imageDecoder = imageDecoder,
)
}
}
}
@Composable
private fun PendingAttachmentItem(
attachment: Attachment,
index: Int,
count: Int,
onPreview: () -> Unit,
onRemove: () -> Unit,
onMoveLeft: () -> Unit,
onMoveRight: () -> Unit,
imageDecoder: suspend (Attachment) -> ImageBitmap?,
) {
var previewState by remember(attachment.content, attachment.cachedUri, attachment.state) {
mutableStateOf(initialPreviewState(attachment))
}
LaunchedEffect(attachment.content, attachment.cachedUri, attachment.state) {
if (attachment.isImage && attachment.state == AttachmentState.LOADED) {
previewState = PendingPreviewState.Loading
previewState = imageDecoder(attachment)?.let(PendingPreviewState::Ready)
?: PendingPreviewState.Failed
}
}
val name = attachment.fileName?.takeIf(String::isNotBlank)
?: stringResource(R.string.attachment_title)
val type = attachmentTypeLabel(attachment.renderMode)
val size = attachment.fileSize?.let { formatAttachmentSize(it) }
val status = when (previewState) {
PendingPreviewState.Loading -> stringResource(R.string.pending_attachment_status_loading)
is PendingPreviewState.Ready -> stringResource(R.string.pending_attachment_status_ready)
PendingPreviewState.Failed -> attachment.errorMessage?.takeIf(String::isNotBlank)
?: stringResource(R.string.pending_attachment_status_failed)
}
val summary = listOfNotNull(name, type, size, status).joinToString(", ")
val previewEnabled = previewState is PendingPreviewState.Ready
Surface(
modifier = Modifier
.width(216.dp)
.semantics { contentDescription = summary }
.clip(RoundedCornerShape(14.dp))
.clickable(
enabled = previewEnabled,
onClickLabel = stringResource(R.string.pending_attachment_preview_named, name),
role = Role.Button,
onClick = onPreview,
)
.testTag("pending-attachment-$index"),
shape = RoundedCornerShape(14.dp),
color = MaterialTheme.colorScheme.surfaceVariant,
) {
Column(modifier = Modifier.padding(8.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
PendingAttachmentThumbnail(previewState)
Spacer(Modifier.width(8.dp))
Column(modifier = Modifier.weight(1f)) {
Text(
text = name,
style = MaterialTheme.typography.labelLarge,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Text(
text = listOfNotNull(type, size).joinToString(" · "),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Text(
text = status,
style = MaterialTheme.typography.labelSmall,
color = if (previewState == PendingPreviewState.Failed) {
MaterialTheme.colorScheme.error
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
)
}
}
Spacer(Modifier.height(4.dp))
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End,
) {
IconButton(
onClick = onMoveLeft,
enabled = index > 0,
modifier = Modifier
.size(48.dp)
.testTag("pending-attachment-move-left-$index"),
) {
Icon(
Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = stringResource(R.string.pending_attachment_move_left, name),
)
}
IconButton(
onClick = onMoveRight,
enabled = index < count - 1,
modifier = Modifier
.size(48.dp)
.testTag("pending-attachment-move-right-$index"),
) {
Icon(
Icons.AutoMirrored.Filled.ArrowForward,
contentDescription = stringResource(R.string.pending_attachment_move_right, name),
)
}
IconButton(
onClick = onRemove,
modifier = Modifier
.size(48.dp)
.testTag("pending-attachment-remove-$index"),
) {
Icon(
Icons.Filled.Close,
contentDescription = stringResource(R.string.pending_attachment_remove, name),
)
}
}
}
}
}
@Composable
private fun PendingAttachmentThumbnail(state: PendingPreviewState) {
Box(
modifier = Modifier
.size(64.dp)
.clip(RoundedCornerShape(10.dp))
.background(MaterialTheme.colorScheme.surface),
contentAlignment = Alignment.Center,
) {
when (state) {
PendingPreviewState.Loading -> CircularProgressIndicator(
modifier = Modifier.size(24.dp),
strokeWidth = 2.dp,
)
is PendingPreviewState.Ready -> if (state.bitmap != null) {
Image(
bitmap = state.bitmap,
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier.size(64.dp),
)
} else {
Icon(Icons.Filled.AttachFile, contentDescription = null)
}
PendingPreviewState.Failed -> Icon(
Icons.Filled.BrokenImage,
contentDescription = null,
tint = MaterialTheme.colorScheme.error,
)
}
}
}
@Composable
private fun attachmentTypeLabel(mode: AttachmentRenderMode): String = stringResource(
when (mode) {
AttachmentRenderMode.IMAGE -> R.string.attachment_type_image
AttachmentRenderMode.VIDEO -> R.string.attachment_type_video
AttachmentRenderMode.AUDIO -> R.string.attachment_type_audio
AttachmentRenderMode.PDF -> R.string.attachment_type_pdf
AttachmentRenderMode.TEXT -> R.string.attachment_type_text
AttachmentRenderMode.GENERIC -> R.string.attachment_type_file
},
)
private fun initialPreviewState(attachment: Attachment): PendingPreviewState = when {
attachment.state == AttachmentState.FAILED -> PendingPreviewState.Failed
attachment.state == AttachmentState.LOADING -> PendingPreviewState.Loading
attachment.isImage -> PendingPreviewState.Loading
else -> PendingPreviewState.Ready(bitmap = null)
}
@Composable
internal fun formatAttachmentSize(bytes: Long): String {
val safeBytes = bytes.coerceAtLeast(0)
return when {
safeBytes >= 1024L * 1024L * 1024L -> stringResource(
R.string.media_bytes_gb,
safeBytes / (1024.0 * 1024.0 * 1024.0),
)
safeBytes >= 1024L * 1024L -> stringResource(
R.string.media_bytes_mb,
safeBytes / (1024.0 * 1024.0),
)
safeBytes >= 1024L -> stringResource(R.string.media_bytes_kb, safeBytes / 1024.0)
else -> stringResource(R.string.media_bytes_b, safeBytes)
}
}
private suspend fun decodePendingAttachmentImage(attachment: Attachment): ImageBitmap? =
withContext(Dispatchers.IO) {
runCatching {
val bytes = Base64.decode(attachment.content, Base64.DEFAULT)
if (bytes.isEmpty()) return@runCatching null
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return@runCatching null
var sample = 1
while (
bounds.outWidth / sample > PENDING_ATTACHMENT_THUMBNAIL_TARGET_PX ||
bounds.outHeight / sample > PENDING_ATTACHMENT_THUMBNAIL_TARGET_PX
) {
sample *= 2
}
decodeOrientedBitmap(
bytes,
BitmapFactory.Options().apply { inSampleSize = sample },
)?.asImageBitmap()
}.getOrNull()
}
private sealed interface PendingPreviewState {
data object Loading : PendingPreviewState
data class Ready(val bitmap: ImageBitmap?) : PendingPreviewState
data object Failed : PendingPreviewState
}
private const val PENDING_ATTACHMENT_THUMBNAIL_TARGET_PX = 256
@@ -0,0 +1,527 @@
package com.hermesandroid.relay.ui.components
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.MoreHoriz
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Tune
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.disabled
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.hermesandroid.relay.R
import com.hermesandroid.relay.data.AgentDisplay
import com.hermesandroid.relay.data.Profile
import com.hermesandroid.relay.data.ProfilePresentation
import com.hermesandroid.relay.data.ProfilePresentationPolicy
import com.hermesandroid.relay.viewmodel.ConnectionViewModel
import java.io.File
data class ProfileChoice(
val key: String,
val profile: Profile?,
) {
val isServerDefault: Boolean get() = key == AgentDisplay.SERVER_DEFAULT_PROFILE_KEY
}
object ProfileShelfPolicy {
fun choices(
profiles: List<Profile>,
presentation: ProfilePresentation,
selectedProfileName: String?,
): List<ProfileChoice> {
val selectedKey = AgentDisplay.profileSessionKey(selectedProfileName)
return ProfilePresentationPolicy
.visibleKeys(profiles, presentation, selectedKey)
.mapNotNull { key ->
if (key == AgentDisplay.SERVER_DEFAULT_PROFILE_KEY) {
ProfileChoice(key, null)
} else {
profiles.firstOrNull { it.name == key }?.let { ProfileChoice(key, it) }
}
}
}
fun canSwitch(isStreaming: Boolean, streamingEndpoint: String): Boolean =
!isStreaming || streamingEndpoint == "gateway"
fun isSelected(choice: ProfileChoice, selectedProfileName: String?): Boolean =
choice.key == AgentDisplay.profileSessionKey(selectedProfileName)
/** Local presentation assets follow the selected choice key, not the sticky profile it resolves to. */
fun iconProfileName(choice: ProfileChoice): String? = choice.profile?.name
}
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ProfileShelf(
connectionViewModel: ConnectionViewModel,
profiles: List<Profile>,
selectedProfile: Profile?,
resolvedProfile: Profile?,
presentation: ProfilePresentation,
activeDisplayName: String,
isProfileLocked: Boolean,
lockedProfileName: String?,
switchEnabled: Boolean,
onSelect: (Profile?) -> Unit,
onOpenPassport: () -> Unit,
onOpenSwitcher: () -> Unit,
onInspect: (String) -> Unit,
onLock: (Profile?) -> Unit,
onUnlock: () -> Unit,
onHide: (String?) -> Unit,
modifier: Modifier = Modifier,
) {
val choices = remember(profiles, presentation, selectedProfile?.name) {
ProfileShelfPolicy.choices(profiles, presentation, selectedProfile?.name)
}
if (choices.size <= 1) return
var actionChoice by remember { mutableStateOf<ProfileChoice?>(null) }
val lockedKey = lockedProfileName ?: AgentDisplay.SERVER_DEFAULT_PROFILE_KEY
Surface(
modifier = modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.surface,
tonalElevation = 0.dp,
) {
Column {
Row(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 55.dp)
.padding(start = 8.dp, end = 4.dp, top = 3.dp, bottom = 3.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Row(
modifier = Modifier
.weight(1f)
.horizontalScroll(rememberScrollState()),
horizontalArrangement = Arrangement.spacedBy(6.dp),
verticalAlignment = Alignment.CenterVertically,
) {
choices.forEach { choice ->
val selected = ProfileShelfPolicy.isSelected(choice, selectedProfile?.name)
val label = if (selected) {
activeDisplayName
} else {
profileChoiceLabel(choice, resolvedProfile)
}
if (selected) {
val openPassportDescription = stringResource(R.string.profile_shelf_open_passport)
Box(
modifier = Modifier
.heightIn(min = 48.dp)
.widthIn(max = 190.dp)
.combinedClickable(
role = Role.Button,
onClick = onOpenPassport,
onLongClick = { actionChoice = choice },
)
.semantics {
contentDescription = "$label. $openPassportDescription"
},
contentAlignment = Alignment.Center,
) {
Surface(
shape = RoundedCornerShape(22.dp),
color = MaterialTheme.colorScheme.surfaceContainerLow,
border = BorderStroke(
1.dp,
MaterialTheme.colorScheme.primary.copy(alpha = 0.72f),
),
) {
Row(
modifier = Modifier.padding(horizontal = 7.dp, vertical = 4.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(7.dp),
) {
ProfileChoiceAvatar(
connectionViewModel,
choice,
resolvedProfile,
label,
36,
)
Text(
text = label,
modifier = Modifier.weight(1f, fill = false),
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.SemiBold,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Icon(
Icons.AutoMirrored.Filled.KeyboardArrowRight,
contentDescription = null,
modifier = Modifier.size(18.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
}
} else {
val enabled = switchEnabled && !isProfileLocked
val switchToDescription = stringResource(R.string.profile_shelf_switch_to)
Box(
modifier = Modifier
.size(48.dp)
.clip(CircleShape)
.combinedClickable(
enabled = true,
role = Role.Button,
onClick = { if (enabled) onSelect(choice.profile) },
onLongClick = { actionChoice = choice },
)
.semantics {
if (!enabled) disabled()
contentDescription = if (enabled) {
"$label. $switchToDescription"
} else {
label
}
},
contentAlignment = Alignment.Center,
) {
ProfileChoiceAvatar(
connectionViewModel,
choice,
resolvedProfile,
label,
36,
)
}
}
}
}
IconButton(
onClick = onOpenSwitcher,
modifier = Modifier.size(48.dp),
) {
Surface(
modifier = Modifier.size(40.dp),
shape = CircleShape,
color = MaterialTheme.colorScheme.surfaceContainerLow,
border = BorderStroke(
1.dp,
MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.42f),
),
) {
Box(contentAlignment = Alignment.Center) {
Icon(
Icons.Filled.MoreHoriz,
contentDescription = stringResource(R.string.profile_shelf_all_profiles),
modifier = Modifier.size(20.dp),
)
}
}
}
}
HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.32f))
}
}
actionChoice?.let { choice ->
val selected = ProfileShelfPolicy.isSelected(choice, selectedProfile?.name)
val target = choice.profile ?: resolvedProfile
val label = profileChoiceLabel(choice, resolvedProfile)
ProfileShelfActionsDialog(
label = label,
canSwitch = switchEnabled && !isProfileLocked,
selected = selected,
canInspect = target != null,
lockedToChoice = isProfileLocked && lockedKey == choice.key,
onInspect = {
actionChoice = null
target?.name?.let(onInspect)
},
onPassport = {
actionChoice = null
if (!selected && switchEnabled && !isProfileLocked) onSelect(choice.profile)
onOpenPassport()
},
onToggleLock = {
actionChoice = null
if (isProfileLocked && lockedKey == choice.key) onUnlock() else onLock(choice.profile)
},
onHide = {
actionChoice = null
onHide(choice.profile?.name)
},
onDismiss = { actionChoice = null },
)
}
}
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
fun ProfileSwitcherSheet(
connectionViewModel: ConnectionViewModel,
profiles: List<Profile>,
selectedProfile: Profile?,
resolvedProfile: Profile?,
presentation: ProfilePresentation,
isProfileLocked: Boolean,
switchEnabled: Boolean,
onSelect: (Profile?) -> Unit,
onManageDisplay: () -> Unit,
onDismiss: () -> Unit,
) {
val choices = remember(profiles, presentation, selectedProfile?.name) {
ProfileShelfPolicy.choices(profiles, presentation, selectedProfile?.name)
}
ModalBottomSheet(onDismissRequest = onDismiss) {
Column(
modifier = Modifier
.fillMaxWidth()
.heightIn(max = 560.dp)
.verticalScroll(rememberScrollState())
.padding(bottom = 24.dp),
) {
Text(
text = stringResource(R.string.profile_shelf_switch_agent),
modifier = Modifier.padding(horizontal = 24.dp, vertical = 12.dp),
style = MaterialTheme.typography.headlineSmall,
fontWeight = FontWeight.SemiBold,
)
if (isProfileLocked) {
Text(
text = stringResource(R.string.profile_shelf_locked_hint),
modifier = Modifier.padding(horizontal = 24.dp, vertical = 4.dp),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
choices.forEach { choice ->
val selected = ProfileShelfPolicy.isSelected(choice, selectedProfile?.name)
val label = profileChoiceLabel(choice, resolvedProfile)
val model = choice.profile?.model?.takeIf { it.isNotBlank() }
ListItem(
modifier = Modifier.selectable(
selected = selected,
enabled = selected || (switchEnabled && !isProfileLocked),
role = Role.RadioButton,
onClick = {
if (!selected) onSelect(choice.profile)
onDismiss()
},
),
headlineContent = {
Text(label, maxLines = 1, overflow = TextOverflow.Ellipsis)
},
supportingContent = if (model != null) {
{ Text(model, maxLines = 1, overflow = TextOverflow.Ellipsis) }
} else {
null
},
leadingContent = {
ProfileChoiceAvatar(connectionViewModel, choice, resolvedProfile, label, 42)
},
trailingContent = if (selected) {
{ Icon(Icons.Filled.Check, contentDescription = null) }
} else {
null
},
)
}
HorizontalDivider(modifier = Modifier.padding(top = 8.dp))
TextButton(
onClick = onManageDisplay,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 12.dp, vertical = 4.dp),
) {
Icon(Icons.Filled.Tune, contentDescription = null)
Spacer(Modifier.size(8.dp))
Text(stringResource(R.string.conn_info_manage_profiles))
}
}
}
}
@Composable
private fun ProfileChoiceAvatar(
connectionViewModel: ConnectionViewModel,
choice: ProfileChoice,
resolvedProfile: Profile?,
label: String,
size: Int,
) {
val iconProfileName = ProfileShelfPolicy.iconProfileName(choice)
val iconPath by connectionViewModel.profileIconFlow(iconProfileName).collectAsState(initial = null)
Box(modifier = Modifier.size(size.dp)) {
Surface(
modifier = Modifier.fillMaxSize(),
shape = CircleShape,
color = MaterialTheme.colorScheme.primary,
border = BorderStroke(
1.dp,
MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.55f),
),
) {
when {
!iconPath.isNullOrBlank() -> AsyncImage(
model = File(iconPath.orEmpty()),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier.fillMaxSize(),
)
resolvedProfile == null && choice.isServerDefault -> Box(contentAlignment = Alignment.Center) {
Icon(
Icons.Filled.Home,
contentDescription = null,
modifier = Modifier.size((size * 0.48f).dp),
tint = MaterialTheme.colorScheme.onPrimary,
)
}
else -> Box(contentAlignment = Alignment.Center) {
Text(
text = label.trim().firstOrNull()?.uppercase() ?: "H",
color = MaterialTheme.colorScheme.onPrimary,
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.Bold,
)
}
}
}
if (choice.isServerDefault && resolvedProfile != null) {
Surface(
modifier = Modifier
.align(Alignment.BottomStart)
.size((size * 0.38f).dp),
shape = CircleShape,
color = MaterialTheme.colorScheme.surfaceContainerHighest,
border = BorderStroke(1.dp, MaterialTheme.colorScheme.surface),
) {
Box(contentAlignment = Alignment.Center) {
Icon(
Icons.Filled.Home,
contentDescription = null,
modifier = Modifier.size((size * 0.22f).dp),
tint = MaterialTheme.colorScheme.onSurface,
)
}
}
}
}
}
@Composable
private fun ProfileShelfActionsDialog(
label: String,
canSwitch: Boolean,
selected: Boolean,
canInspect: Boolean,
lockedToChoice: Boolean,
onInspect: () -> Unit,
onPassport: () -> Unit,
onToggleLock: () -> Unit,
onHide: () -> Unit,
onDismiss: () -> Unit,
) {
AlertDialog(
onDismissRequest = onDismiss,
title = { Text(label, maxLines = 1, overflow = TextOverflow.Ellipsis) },
text = {
Column {
ProfileActionRow(Icons.Filled.Visibility, stringResource(R.string.profile_shelf_inspect), canInspect, onInspect)
ProfileActionRow(
Icons.Filled.Person,
stringResource(R.string.conn_info_agent_passport),
selected || canSwitch,
onPassport,
)
ProfileActionRow(
Icons.Filled.Lock,
stringResource(if (lockedToChoice) R.string.settings_unlock else R.string.settings_profile_lock),
true,
onToggleLock,
)
ProfileActionRow(
Icons.Filled.VisibilityOff,
stringResource(R.string.profile_shelf_hide),
!selected,
onHide,
)
}
},
confirmButton = { TextButton(onClick = onDismiss) { Text(stringResource(R.string.chat_dismiss)) } },
)
}
@Composable
private fun ProfileActionRow(
icon: androidx.compose.ui.graphics.vector.ImageVector,
label: String,
enabled: Boolean,
onClick: () -> Unit,
) {
TextButton(onClick = onClick, enabled = enabled, modifier = Modifier.fillMaxWidth()) {
Icon(icon, contentDescription = null)
Spacer(Modifier.size(12.dp))
Text(label, modifier = Modifier.weight(1f))
}
}
@Composable
private fun profileChoiceLabel(choice: ProfileChoice, resolvedProfile: Profile?): String =
if (choice.isServerDefault) {
stringResource(R.string.conn_info_server_default)
} else {
choice.profile?.let(AgentDisplay::profileDisplayName)
?: choice.profile?.name?.replaceFirstChar { it.uppercase() }
?: resolvedProfile?.let(AgentDisplay::profileDisplayName)
?: stringResource(R.string.chat_agent_default)
}
@@ -2,6 +2,7 @@ package com.hermesandroid.relay.ui.components
import android.util.Log
import android.view.ViewGroup
import androidx.activity.compose.BackHandler
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.Preview
@@ -13,6 +14,7 @@ import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.snap
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
@@ -52,6 +54,11 @@ import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.paneTitle
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
@@ -63,6 +70,7 @@ import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.common.InputImage
import com.hermesandroid.relay.data.ApiEndpoint
import com.hermesandroid.relay.data.Connection
import com.hermesandroid.relay.data.DashboardEndpoint
import com.hermesandroid.relay.data.EndpointCandidate
import com.hermesandroid.relay.data.RelayEndpoint
@@ -80,6 +88,8 @@ import java.net.URI
import java.util.concurrent.Executors
import kotlin.math.max
internal fun scannerSettleDelayMs(motionEnabled: Boolean): Long = if (motionEnabled) 200L else 0L
/**
* Parsed result from a Hermes pairing QR code.
*
@@ -182,7 +192,11 @@ data class HermesPairingPayload(
) {
/** Build the full API server URL from host, port, and tls flag. */
val serverUrl: String
get() = "${if (tls) "https" else "http"}://$host:$port"
get() = if (host.isBlank()) "" else "${if (tls) "https" else "http"}://$host:$port"
/** Whether this setup payload explicitly configures the optional API server. */
val hasApiServer: Boolean
get() = host.isNotBlank()
}
/**
@@ -239,14 +253,17 @@ private val json = Json {
* Try to parse a scanned string as a Hermes connection QR payload.
*
* Accepts v1, v2, and v3 (or anything without a `hermes` field — we default
* to `1`). Returns null when the payload is not valid JSON, has no `host`
* field, or fails strict decoding.
* to `1`). Returns null when the payload is not valid JSON, configures neither
* a recognized API nor Dashboard surface, or fails strict decoding.
*
* For standard Hermes setup, also accepts generic API-only QRs:
* - a plain `http://host:8642` or `https://host:8642` URL
* - JSON with `api_url`, `apiUrl`, `server_url`, `serverUrl`, or `url`, plus
* optional `api_key`, `apiKey`, or `key`, and optional `dashboard_url` or
* `dashboardUrl`
* - JSON with only `dashboard_url` or `dashboardUrl`, containing a fully
* qualified HTTP(S) Dashboard/Gateway URL. This represents the upstream
* standard surface without inventing an API server or Relay endpoint.
*
* **Endpoint synthesis (ADR 24):** when the payload has no `endpoints`
* array (v1/v2 QRs), a single priority-0 [EndpointCandidate] is materialized
@@ -261,6 +278,7 @@ fun parseHermesPairingQr(raw: String): HermesPairingPayload? {
val trimmed = raw.trim()
return parseHermesRelayQr(trimmed)
?: parseGenericApiJsonQr(trimmed)
?: parseGenericDashboardJsonQr(trimmed)
?: parseGenericApiUrlQr(trimmed)
}
@@ -327,6 +345,38 @@ private fun parseGenericApiUrlQr(raw: String): HermesPairingPayload? {
return payloadFromApiUrl(raw, apiKey = "")
}
private fun parseGenericDashboardJsonQr(raw: String): HermesPairingPayload? {
return try {
val obj = json.decodeFromString<JsonObject>(raw)
// Do not silently downgrade a malformed API setup into Dashboard-only
// setup. When an API alias is present, parseGenericApiJsonQr owns the
// payload and must either accept or reject it as a whole.
val apiAliases = setOf("api_url", "apiUrl", "server_url", "serverUrl", "url")
if (obj.keys.any { it in apiAliases }) return null
val dashboardUrl = firstString(obj, "dashboard_url", "dashboardUrl")
?.trimEnd('/')
?: return null
val uri = runCatching { URI(dashboardUrl) }.getOrNull() ?: return null
if (uri.scheme?.lowercase() !in setOf("http", "https") || uri.host.isNullOrBlank()) {
return null
}
HermesPairingPayload(
dashboardUrl = dashboardUrl,
endpoints = listOf(
EndpointCandidate(
role = Connection.inferRouteRole(dashboardUrl),
priority = 0,
dashboard = DashboardEndpoint(dashboardUrl),
),
),
)
} catch (_: Exception) {
null
}
}
private fun firstString(obj: JsonObject, vararg names: String): String? {
return names.firstNotNullOfOrNull { name ->
obj[name]?.jsonPrimitive?.contentOrNull?.trim()?.takeIf { it.isNotBlank() }
@@ -547,6 +597,12 @@ fun QrPairingScanner(
) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
val motion = rememberAccessibleMotionState()
val motionEnabled = motion.osAnimations && !motion.touchExploration
val scannerPaneTitle = stringResource(
if (relayOnly) R.string.qr_scanner_relay_title else R.string.qr_scanner_title,
)
BackHandler(onBack = onDismiss)
// Pre-resolve strings used inside non-Composable contexts (CameraX
// listeners, exception handlers, etc. don't have a Composable scope).
val cameraErrorMsg = stringResource(R.string.qr_scanner_camera_error)
@@ -609,7 +665,8 @@ fun QrPairingScanner(
// ~450ms so the user perceives the lock-on, then forward to onPairingDetected.
LaunchedEffect(lockedPayload) {
val payload = lockedPayload ?: return@LaunchedEffect
kotlinx.coroutines.delay(450)
val settleDelayMs = scannerSettleDelayMs(motionEnabled)
if (settleDelayMs > 0) kotlinx.coroutines.delay(settleDelayMs)
onPairingDetected(payload)
}
@@ -617,6 +674,7 @@ fun QrPairingScanner(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.scrim.copy(alpha = 0.95f))
.semantics { paneTitle = scannerPaneTitle }
) {
Column(
modifier = Modifier.fillMaxSize(),
@@ -645,7 +703,7 @@ fun QrPairingScanner(
),
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onSurface,
modifier = Modifier.align(Alignment.Center)
modifier = Modifier.align(Alignment.Center).semantics { heading() }
)
}
@@ -796,6 +854,7 @@ fun QrPairingScanner(
ScannerCornersOverlay(
detected = detectedBox,
locked = lockedPayload != null,
motionEnabled = motionEnabled,
modifier = Modifier.fillMaxSize(),
)
}
@@ -833,6 +892,19 @@ fun QrPairingScanner(
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center
)
Text(
text = stringResource(
if (lockedPayload == null) R.string.qr_scanner_ready
else R.string.qr_scanner_found,
),
style = MaterialTheme.typography.labelMedium,
color = if (lockedPayload == null) {
MaterialTheme.colorScheme.onSurfaceVariant
} else {
MaterialTheme.colorScheme.primary
},
modifier = Modifier.semantics { liveRegion = LiveRegionMode.Polite },
)
}
}
}
@@ -905,6 +977,7 @@ private fun CameraUnavailableCard(
private fun ScannerCornersOverlay(
detected: ViewportRect?,
locked: Boolean,
motionEnabled: Boolean,
modifier: Modifier = Modifier,
) {
// Themed idle: two-tone gradient between primary (top-left/bottom-right)
@@ -922,22 +995,28 @@ private fun ScannerCornersOverlay(
// Slow breathing pulse on alpha when idle. Locked state stays solid +
// gets its own one-shot ramp so the green burst is unmistakable.
val infiniteTransition = rememberInfiniteTransition(label = "scan-corners")
val idlePulse by infiniteTransition.animateFloat(
initialValue = 0.45f,
targetValue = 1f,
animationSpec = infiniteRepeatable(
animation = tween(1400, easing = FastOutSlowInEasing),
repeatMode = RepeatMode.Reverse,
),
label = "idle-pulse",
)
val idlePulse = if (motionEnabled) {
val infiniteTransition = rememberInfiniteTransition(label = "scan-corners")
infiniteTransition.animateFloat(
initialValue = 0.45f,
targetValue = 1f,
animationSpec = infiniteRepeatable(
animation = tween(1400, easing = FastOutSlowInEasing),
repeatMode = RepeatMode.Reverse,
),
label = "idle-pulse",
).value
} else {
1f
}
// One-shot ramp that fires when `locked` flips true. Drives the
// outward scale pop on the corners + the green tint flash overlay.
val lockRamp by animateFloatAsState(
targetValue = if (locked) 1f else 0f,
animationSpec = if (locked) {
animationSpec = if (!motionEnabled) {
snap()
} else if (locked) {
spring(dampingRatio = 0.55f, stiffness = 220f)
} else {
tween(180)
@@ -973,14 +1052,15 @@ private fun ScannerCornersOverlay(
targetBottom = 0f
}
val springSpec = spring<Float>(
dampingRatio = 0.7f,
stiffness = 280f,
)
val animLeft by animateFloatAsState(targetLeft, springSpec, label = "snap-l")
val animTop by animateFloatAsState(targetTop, springSpec, label = "snap-t")
val animRight by animateFloatAsState(targetRight, springSpec, label = "snap-r")
val animBottom by animateFloatAsState(targetBottom, springSpec, label = "snap-b")
val positionSpec = if (motionEnabled) {
spring<Float>(dampingRatio = 0.7f, stiffness = 280f)
} else {
snap()
}
val animLeft by animateFloatAsState(targetLeft, positionSpec, label = "snap-l")
val animTop by animateFloatAsState(targetTop, positionSpec, label = "snap-t")
val animRight by animateFloatAsState(targetRight, positionSpec, label = "snap-r")
val animBottom by animateFloatAsState(targetBottom, positionSpec, label = "snap-b")
Canvas(
modifier = modifier.onSizeChanged { size = it }
@@ -1100,7 +1180,7 @@ private fun ScannerCornersOverlay(
// Lock flash — brief green tint over the entire viewport that fades
// out as lockRamp settles. Driven by the same spring as the corner
// pop so they read as one event.
if (lockRamp > 0f) {
if (motionEnabled && lockRamp > 0f) {
drawRect(
color = successCore.copy(alpha = 0.18f * lockRamp),
topLeft = Offset.Zero,
@@ -0,0 +1,20 @@
package com.hermesandroid.relay.ui.components
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.hermesandroid.relay.R
import com.hermesandroid.relay.network.upstream.ReasoningEfforts
/** One localized label source shared by the composer and Agent Passport. */
@Composable
fun reasoningEffortLabel(value: String?): String = when (ReasoningEfforts.normalize(value)) {
"none" -> stringResource(R.string.chat_reasoning_none)
"minimal" -> stringResource(R.string.chat_reasoning_minimal)
"low" -> stringResource(R.string.chat_reasoning_low)
"medium" -> stringResource(R.string.chat_reasoning_medium)
"high" -> stringResource(R.string.chat_reasoning_high)
"xhigh" -> stringResource(R.string.chat_reasoning_xhigh)
"max" -> stringResource(R.string.chat_reasoning_max)
"ultra" -> stringResource(R.string.chat_reasoning_ultra)
else -> error("ReasoningEfforts.normalize returned a non-canonical value")
}
@@ -1,7 +1,15 @@
package com.hermesandroid.relay.ui.components
import android.content.Context
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.SweepGradient
import androidx.compose.animation.Crossfade
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
@@ -33,6 +41,7 @@ import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.outlined.StarBorder
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
@@ -57,26 +66,52 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLocale
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.R
import com.hermesandroid.relay.data.ChatSession
import com.hermesandroid.relay.data.SessionActivityState
import com.hermesandroid.relay.ui.theme.RelayRefresh
import com.hermesandroid.relay.ui.theme.relayMetadataStyle
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
private enum class SessionDrawerFilter {
internal enum class SessionDrawerFilter {
All,
Threads,
Pinned,
Archive,
}
internal const val SESSION_DRAWER_LIST_TAG = "session-drawer-list"
internal const val UNPINNED_STAR_ALPHA = 0.45f
internal fun sessionPinIcon(pinned: Boolean) =
if (pinned) Icons.Filled.Star else Icons.Outlined.StarBorder
internal fun resolveSessionDrawerFilter(
filter: SessionDrawerFilter,
showThreads: Boolean,
archiveSupported: Boolean,
): SessionDrawerFilter = when {
filter == SessionDrawerFilter.Threads && !showThreads -> SessionDrawerFilter.All
filter == SessionDrawerFilter.Archive && !archiveSupported -> SessionDrawerFilter.All
else -> filter
}
@Composable
fun SessionDrawerContent(
sessions: List<ChatSession>,
@@ -85,12 +120,17 @@ fun SessionDrawerContent(
scopeSubtitle: String? = null,
isLoading: Boolean = false,
isOpen: Boolean = true,
activityStates: Map<String, SessionActivityState> = emptyMap(),
animationEnabled: Boolean = true,
autoTitlesSupported: Boolean = true,
archiveSupported: Boolean = true,
onRefresh: (() -> Unit)? = null,
onNewChat: () -> Unit,
onSelectSession: (String) -> Unit,
onDeleteSession: (String) -> Unit,
onRenameSession: (String, String) -> Unit,
onSetSessionPinned: (String, Boolean) -> Unit = { _, _ -> },
onSetSessionArchived: (String, Boolean) -> Unit = { _, _ -> },
onCopySessionId: ((String) -> Unit)? = null,
/**
* When true, the Threads affordance (header spool + filter chip) shows even with no
@@ -115,21 +155,15 @@ fun SessionDrawerContent(
var sourceFilterOpen by remember { mutableStateOf(false) }
var deleteDialogSession by remember { mutableStateOf<ChatSession?>(null) }
var query by remember { mutableStateOf("") }
var searchExpanded by remember { mutableStateOf(false) }
var filter by remember { mutableStateOf(SessionDrawerFilter.All) }
var pinnedSessionIds by remember { mutableStateOf<Set<String>>(emptySet()) }
var archivedSessionIds by remember { mutableStateOf<Set<String>>(emptySet()) }
val listState = rememberLazyListState()
var scrollToTopPending by remember { mutableStateOf(false) }
val trimmedQuery = query.trim()
// Threads affordance shows when the capability is active OR there's already at least one
// agent Thread (source=phone) in the list. If the filter is on Threads but they've
// vanished, fall back to All so the drawer never gets stuck on an empty hidden filter.
val showThreads = threadsCapabilityActive || sessions.any { isThreadSource(it.source) }
val activeFilter = if (filter == SessionDrawerFilter.Threads && !showThreads) {
SessionDrawerFilter.All
} else {
filter
}
val activeFilter = resolveSessionDrawerFilter(filter, showThreads, archiveSupported)
// External gateway sources present (discord/telegram/cron/…) for the source
// filter dropdown. Own chats (tui/api_server) + phone Threads aren't listed.
val presentSources = sessions
@@ -141,14 +175,13 @@ fun SessionDrawerContent(
.asSequence()
.filter { session ->
when (activeFilter) {
SessionDrawerFilter.All -> session.sessionId !in archivedSessionIds
SessionDrawerFilter.All -> !session.archived
SessionDrawerFilter.Threads ->
isThreadSource(session.source) &&
session.sessionId !in archivedSessionIds
!session.archived
SessionDrawerFilter.Pinned ->
session.sessionId in pinnedSessionIds &&
session.sessionId !in archivedSessionIds
SessionDrawerFilter.Archive -> session.sessionId in archivedSessionIds
session.pinned && !session.archived
SessionDrawerFilter.Archive -> session.archived
}
}
.filter { session ->
@@ -166,7 +199,7 @@ fun SessionDrawerContent(
session.model.orEmpty().contains(needle, ignoreCase = true)
}
.sortedWith(
compareByDescending<ChatSession> { it.sessionId in pinnedSessionIds }
compareByDescending<ChatSession> { it.pinned }
.thenByDescending { it.activityTimestamp }
.thenByDescending { it.startTimestamp }
.thenBy { it.title.orEmpty().lowercase(locale = Locale.ROOT) }
@@ -174,27 +207,12 @@ fun SessionDrawerContent(
.toList()
val topVisibleSessionId = visibleSessions.firstOrNull()?.sessionId
LaunchedEffect(isOpen) {
scrollToTopPending = isOpen
if (isOpen && visibleSessions.isNotEmpty()) {
listState.scrollToItem(0)
scrollToTopPending = false
}
}
LaunchedEffect(filter, trimmedQuery) {
if (isOpen && visibleSessions.isNotEmpty()) {
listState.scrollToItem(0)
scrollToTopPending = false
} else if (isOpen) {
scrollToTopPending = true
}
}
LaunchedEffect(isOpen, topVisibleSessionId, visibleSessions.size) {
if (isOpen && scrollToTopPending && visibleSessions.isNotEmpty()) {
listState.scrollToItem(0)
scrollToTopPending = false
LaunchedEffect(isOpen, activeFilter, trimmedQuery, topVisibleSessionId) {
if (isOpen && topVisibleSessionId != null) {
// A drawer-open refresh can reorder rows after the initial scroll.
// Override LazyColumn's normal key anchoring so the new leading
// session is visible instead of being retained above the viewport.
listState.requestScrollToItem(0)
}
}
@@ -298,6 +316,21 @@ fun SessionDrawerContent(
)
}
}
IconButton(
onClick = { searchExpanded = !searchExpanded },
modifier = Modifier.size(36.dp),
) {
Icon(
Icons.Filled.Search,
contentDescription = stringResource(R.string.drawer_search_sessions),
tint = if (searchExpanded || query.isNotBlank()) {
RelayRefresh.Relay
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
modifier = Modifier.size(20.dp),
)
}
// Manual re-pull: the server titles a session asynchronously after
// the first turn (and never pushes a rename), so a refresh is the
// way to pick up a title the auto-reconcile window missed.
@@ -335,24 +368,29 @@ fun SessionDrawerContent(
Text(stringResource(R.string.drawer_new_chat))
}
Spacer(modifier = Modifier.height(8.dp))
OutlinedTextField(
value = query,
onValueChange = { query = it },
modifier = Modifier.fillMaxWidth(),
singleLine = true,
leadingIcon = {
Icon(Icons.Filled.Search, contentDescription = null)
},
placeholder = { Text(stringResource(R.string.drawer_search_placeholder)) },
)
if (searchExpanded || query.isNotBlank()) {
Spacer(modifier = Modifier.height(8.dp))
OutlinedTextField(
value = query,
onValueChange = { query = it },
modifier = Modifier.fillMaxWidth(),
singleLine = true,
leadingIcon = {
Icon(Icons.Filled.Search, contentDescription = null)
},
placeholder = { Text(stringResource(R.string.drawer_search_placeholder)) },
)
}
Spacer(modifier = Modifier.height(8.dp))
Row(
modifier = Modifier.horizontalScroll(rememberScrollState()),
horizontalArrangement = Arrangement.spacedBy(6.dp),
) {
SessionDrawerFilter.entries
.filter { it != SessionDrawerFilter.Threads || showThreads }
.filter { item ->
(item != SessionDrawerFilter.Threads || showThreads) &&
(item != SessionDrawerFilter.Archive || archiveSupported)
}
.forEach { item ->
FilterChip(
selected = activeFilter == item,
@@ -462,27 +500,25 @@ fun SessionDrawerContent(
)
}
} else {
LazyColumn(state = listState) {
LazyColumn(
state = listState,
modifier = Modifier.testTag(SESSION_DRAWER_LIST_TAG),
) {
items(visibleSessions, key = { it.sessionId }) { session ->
SessionItem(
session = session,
isActive = session.sessionId == currentSessionId,
pinned = session.sessionId in pinnedSessionIds,
archived = session.sessionId in archivedSessionIds,
activityState = activityStates[session.sessionId],
animationEnabled = animationEnabled,
pinned = session.pinned,
archived = session.archived,
archiveSupported = archiveSupported,
onClick = { onSelectSession(session.sessionId) },
onTogglePinned = {
pinnedSessionIds = if (session.sessionId in pinnedSessionIds) {
pinnedSessionIds - session.sessionId
} else {
pinnedSessionIds + session.sessionId
}
onSetSessionPinned(session.sessionId, !session.pinned)
},
onToggleArchived = {
archivedSessionIds = if (session.sessionId in archivedSessionIds) {
archivedSessionIds - session.sessionId
} else {
archivedSessionIds + session.sessionId
}
onSetSessionArchived(session.sessionId, !session.archived)
},
onRename = { renameDialogSession = session },
onCopySessionId = { onCopySessionId?.invoke(session.sessionId) },
@@ -590,8 +626,11 @@ fun SessionDrawerContent(
private fun SessionItem(
session: ChatSession,
isActive: Boolean,
activityState: SessionActivityState?,
animationEnabled: Boolean,
pinned: Boolean,
archived: Boolean,
archiveSupported: Boolean,
onClick: () -> Unit,
onTogglePinned: () -> Unit,
onToggleArchived: () -> Unit,
@@ -603,6 +642,12 @@ private fun SessionItem(
val locale = LocalLocale.current.platformLocale
val context = LocalContext.current
val untitledLabel = stringResource(R.string.drawer_untitled)
val activityLabel = when (activityState) {
SessionActivityState.Working -> stringResource(R.string.drawer_activity_working)
SessionActivityState.NeedsInput -> stringResource(R.string.drawer_activity_needs_input)
null -> null
}
val motion = rememberAccessibleMotionState()
val backgroundColor = if (isActive) {
MaterialTheme.colorScheme.secondaryContainer
} else {
@@ -613,6 +658,13 @@ private fun SessionItem(
modifier = Modifier
.fillMaxWidth()
.background(backgroundColor)
.sessionActivityBorder(
state = activityState,
animated = animationEnabled && motion.osAnimations && !motion.touchExploration,
)
.semantics {
activityLabel?.let { stateDescription = it }
}
.clickable(onClick = onClick)
.padding(horizontal = 16.dp, vertical = 10.dp),
verticalAlignment = Alignment.CenterVertically
@@ -637,6 +689,17 @@ private fun SessionItem(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
if (activityState != null && activityLabel != null) {
SessionActivityIndicator(activityState, activityLabel)
}
if (pinned) {
Icon(
Icons.Filled.Star,
contentDescription = null,
tint = RelayRefresh.Amber,
modifier = Modifier.size(12.dp),
)
}
// Agent Thread tag — the clean spool + "Thread", so a source=phone
// conversation reads as its own lane in the unified session list (ADR 12).
if (isThreadSource(session.source)) {
@@ -687,18 +750,6 @@ private fun SessionItem(
}
}
IconButton(
onClick = onTogglePinned,
modifier = Modifier.size(36.dp),
) {
Icon(
Icons.Filled.Star,
contentDescription = if (pinned) stringResource(R.string.drawer_unpin_session) else stringResource(R.string.drawer_pin_session),
tint = if (pinned) RelayRefresh.Amber else MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.size(19.dp),
)
}
Box {
IconButton(
onClick = { menuOpen = true },
@@ -715,6 +766,32 @@ private fun SessionItem(
expanded = menuOpen,
onDismissRequest = { menuOpen = false },
) {
DropdownMenuItem(
text = {
Text(
if (pinned) {
stringResource(R.string.drawer_unpin_session)
} else {
stringResource(R.string.drawer_pin_session)
}
)
},
leadingIcon = {
Icon(
sessionPinIcon(pinned),
contentDescription = null,
tint = if (pinned) {
RelayRefresh.Amber
} else {
MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = UNPINNED_STAR_ALPHA)
},
)
},
onClick = {
menuOpen = false
onTogglePinned()
},
)
DropdownMenuItem(
text = { Text(stringResource(R.string.chat_copy_session_id)) },
leadingIcon = {
@@ -735,24 +812,26 @@ private fun SessionItem(
onRename()
},
)
DropdownMenuItem(
text = { Text(if (archived) stringResource(R.string.drawer_restore) else stringResource(R.string.drawer_archive)) },
leadingIcon = {
Icon(
Icons.Filled.Archive,
contentDescription = null,
tint = if (archived) {
RelayRefresh.Relay
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
)
},
onClick = {
menuOpen = false
onToggleArchived()
},
)
if (archiveSupported) {
DropdownMenuItem(
text = { Text(if (archived) stringResource(R.string.drawer_restore) else stringResource(R.string.drawer_archive)) },
leadingIcon = {
Icon(
Icons.Filled.Archive,
contentDescription = null,
tint = if (archived) {
RelayRefresh.Relay
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
)
},
onClick = {
menuOpen = false
onToggleArchived()
},
)
}
DropdownMenuItem(
text = {
Text(
@@ -777,6 +856,147 @@ private fun SessionItem(
}
}
@Composable
private fun SessionActivityIndicator(state: SessionActivityState, label: String) {
val color = when (state) {
SessionActivityState.Working -> RelayRefresh.Relay
SessionActivityState.NeedsInput -> RelayRefresh.Amber
}
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(4.dp),
) {
Box(
modifier = Modifier
.size(7.dp)
.clip(RoundedCornerShape(50))
.background(color),
)
Text(
text = label,
style = relayMetadataStyle(),
color = color,
maxLines = 1,
)
}
}
@Composable
private fun Modifier.sessionActivityBorder(
state: SessionActivityState?,
animated: Boolean,
): Modifier {
if (state == null) return this
val color = when (state) {
SessionActivityState.Working -> RelayRefresh.Relay
SessionActivityState.NeedsInput -> RelayRefresh.Amber
}
val shouldRotate = animated && state == SessionActivityState.Working
val phase = if (shouldRotate) {
val transition = rememberInfiniteTransition(label = "session-activity")
transition.animateFloat(
initialValue = 0f,
targetValue = 1f,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 2_230, easing = LinearEasing),
),
label = "session-activity-glow",
)
} else {
null
}
return drawWithCache {
val coreWidth = 1.25.dp.toPx()
val coreInset = coreWidth / 2f
val haloWidth = 5.dp.toPx()
val haloInset = haloWidth / 2f
val radius = 12.dp.toPx()
// Matching transparent endpoints make phase 0 and 1 identical, so the
// full shader rotation loops without the dash-pattern reset of the old ring.
val animatedShader = SweepGradient(
size.width / 2f,
size.height / 2f,
intArrayOf(
color.copy(alpha = 0f).toArgb(),
color.copy(alpha = 0f).toArgb(),
color.copy(alpha = 0.16f).toArgb(),
color.copy(alpha = 0.72f).toArgb(),
color.toArgb(),
color.copy(alpha = 0.34f).toArgb(),
color.copy(alpha = 0f).toArgb(),
),
floatArrayOf(0f, 0.52f, 0.66f, 0.78f, 0.84f, 0.92f, 1f),
)
val shaderMatrix = Matrix()
val haloPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeWidth = haloWidth
alpha = 88
shader = animatedShader
}
val middlePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeWidth = 2.75.dp.toPx()
alpha = 150
shader = animatedShader
}
val corePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeWidth = coreWidth
shader = animatedShader
}
val haloRect = RectF(
haloInset,
haloInset,
size.width - haloInset,
size.height - haloInset,
)
val middleInset = middlePaint.strokeWidth / 2f
val middleRect = RectF(
middleInset,
middleInset,
size.width - middleInset,
size.height - middleInset,
)
val coreRect = RectF(
coreInset,
coreInset,
size.width - coreInset,
size.height - coreInset,
)
onDrawWithContent {
drawContent()
if (phase != null) {
shaderMatrix.setRotate(
phase.value * 360f - 90f,
size.width / 2f,
size.height / 2f,
)
animatedShader.setLocalMatrix(shaderMatrix)
drawContext.canvas.nativeCanvas.apply {
drawRoundRect(haloRect, radius, radius, haloPaint)
drawRoundRect(middleRect, radius, radius, middlePaint)
drawRoundRect(coreRect, radius, radius, corePaint)
}
} else {
drawRoundRect(
brush = Brush.linearGradient(
listOf(color.copy(alpha = 0.72f), color.copy(alpha = 0.28f))
),
topLeft = androidx.compose.ui.geometry.Offset(coreInset, coreInset),
size = androidx.compose.ui.geometry.Size(
width = size.width - coreWidth,
height = size.height - coreWidth,
),
cornerRadius = CornerRadius(radius),
style = Stroke(width = coreWidth),
)
}
}
}
}
private fun sessionTimestampText(session: ChatSession, locale: Locale, context: Context): String? {
val timestamp = session.activityTimestamp
if (timestamp <= 0L) return null
@@ -0,0 +1,52 @@
package com.hermesandroid.relay.ui.components
/**
* Model identity shown by chat-scoped controls.
*
* A running/resumed session owns its runtime model. Profile and server values
* are defaults only, and may seed a fresh draft, but must never repaint an
* existing session merely because a picker or Agent Passport was opened.
*/
internal data class SessionModelUiState(
val model: String?,
val provider: String?,
val pickerModel: String?,
val pickerProvider: String?,
val inheritsProfileDefault: Boolean,
)
internal fun resolveSessionModelUiState(
hasSession: Boolean,
pendingModel: String?,
pendingProvider: String?,
gatewayModel: String?,
gatewayProvider: String?,
persistedSessionModel: String?,
profileDefaultModel: String?,
serverDefaultModel: String?,
): SessionModelUiState {
fun String?.present(): String? = this?.trim()?.takeIf(String::isNotEmpty)
val pending = pendingModel.present()
val gateway = gatewayModel.present()
val persisted = persistedSessionModel.present()
val profileDefault = profileDefaultModel.present()
val serverDefault = serverDefaultModel.present()
val effectiveModel = if (hasSession) {
pending ?: gateway ?: persisted ?: profileDefault ?: serverDefault
} else {
pending ?: profileDefault ?: gateway ?: serverDefault
}
val effectiveProvider = pendingProvider.present()
?: gatewayProvider.present()
return SessionModelUiState(
model = effectiveModel,
provider = effectiveProvider,
// A live session highlights what it is actually running. On a fresh
// draft, null remains the explicit "inherit profile default" state.
pickerModel = if (hasSession) effectiveModel else pending,
pickerProvider = if (hasSession) effectiveProvider else pendingProvider.present(),
inheritsProfileDefault = !hasSession && pending == null,
)
}
@@ -0,0 +1,96 @@
package com.hermesandroid.relay.ui.components
import android.content.Context
import android.net.Uri
import android.util.Log
import kotlinx.serialization.json.Json
import java.io.File
import java.io.InputStream
sealed interface SphereSkinImportResult {
data class Success(val id: String, val label: String) : SphereSkinImportResult
data class Failure(val reason: String) : SphereSkinImportResult
}
/** Imports one declarative sphere-skin JSON file into app-scoped local storage. */
object SphereSkinImporter {
private const val TAG = "SphereSkinImporter"
private const val MAX_BYTES = 256L * 1024L
private val json = Json {
ignoreUnknownKeys = true
isLenient = true
}
fun importUri(context: Context, uri: Uri): SphereSkinImportResult {
val input = try {
context.contentResolver.openInputStream(uri)
} catch (t: Throwable) {
Log.w(TAG, "openInputStream failed: ${t.message}")
null
} ?: return SphereSkinImportResult.Failure("Couldn't open that file.")
return input.use { importStream(it, SphereSkinLoader.userDir(context)) }
}
/** Pure import core used by tests: bounds, parses, validates, then swaps with rollback. */
internal fun importStream(input: InputStream, skinsDir: File): SphereSkinImportResult {
return try {
val bytes = readBounded(input)
?: return SphereSkinImportResult.Failure("That sphere file is too large.")
val spec = try {
json.decodeFromString(SphereSpec.serializer(), bytes.decodeToString())
} catch (_: Throwable) {
return SphereSkinImportResult.Failure("That sphere file isn't valid JSON.")
}
val resolved = spec.copy(id = spec.id.ifBlank { "custom-sphere" })
val skin = try {
resolved.toSkin()
} catch (t: Throwable) {
return SphereSkinImportResult.Failure(t.message ?: "That isn't a valid sphere skin.")
}
skinsDir.mkdirs()
val target = File(skinsDir, "${safeFileName(skin.id)}.json")
val staging = File(skinsDir, ".${target.name}.tmp")
val backup = File(skinsDir, ".${target.name}.bak")
staging.writeBytes(bytes)
if (backup.exists() && !target.exists()) backup.renameTo(target)
if (backup.exists() && target.exists()) backup.delete()
if (target.exists() && !target.renameTo(backup)) {
staging.delete()
return SphereSkinImportResult.Failure("Couldn't replace the existing sphere skin.")
}
if (!staging.renameTo(target)) {
staging.delete()
if (backup.exists()) backup.renameTo(target)
return SphereSkinImportResult.Failure("Couldn't save that sphere skin.")
}
backup.delete()
SphereSkinImportResult.Success(skin.id, skin.label)
} catch (t: Throwable) {
Log.w(TAG, "import failed: ${t.message}")
SphereSkinImportResult.Failure("Couldn't import that sphere skin.")
}
}
private fun readBounded(input: InputStream): ByteArray? {
val buffer = ByteArray(8 * 1024)
val output = java.io.ByteArrayOutputStream()
var total = 0L
while (true) {
val count = input.read(buffer)
if (count < 0) break
total += count
if (total > MAX_BYTES) return null
output.write(buffer, 0, count)
}
return output.toByteArray()
}
private fun safeFileName(id: String): String = id.trim()
.map { if (it.isLetterOrDigit() || it == '-' || it == '_' || it == '.') it else '-' }
.joinToString("")
.trim('.', '-')
.ifBlank { "custom-sphere" }
}
@@ -0,0 +1,130 @@
package com.hermesandroid.relay.ui.components
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.hermesandroid.relay.R
import com.hermesandroid.relay.reliability.ReliabilityReport
import com.hermesandroid.relay.reliability.SupportBundleBuilder
import com.hermesandroid.relay.util.IssueReport
data class SupportReviewState(
val reportCount: Int,
val text: String,
val shareEnabled: Boolean,
)
internal fun buildSupportReviewState(reports: List<ReliabilityReport>): SupportReviewState =
SupportReviewState(
reportCount = reports.takeLast(SupportBundleBuilder.MAX_REPORTS).size,
text = SupportBundleBuilder.build(reports),
shareEnabled = reports.isNotEmpty(),
)
/** Exact review surface for the local text handed to clipboard/share. */
@Composable
fun SupportBundleDialog(state: SupportReviewState, onDismiss: () -> Unit) {
val context = LocalContext.current
val copied = stringResource(R.string.support_bundle_copied)
val noShare = stringResource(R.string.support_bundle_no_share)
val chooser = stringResource(R.string.support_bundle_share_title)
Dialog(
onDismissRequest = onDismiss,
properties = DialogProperties(usePlatformDefaultWidth = false),
) {
Surface(
modifier = Modifier.fillMaxWidth(0.94f),
shape = RoundedCornerShape(24.dp),
tonalElevation = 6.dp,
) {
Column(modifier = Modifier.padding(20.dp)) {
Text(stringResource(R.string.support_bundle_title), style = MaterialTheme.typography.titleMedium)
Spacer(Modifier.height(6.dp))
Text(
stringResource(R.string.support_bundle_privacy, state.reportCount),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Spacer(Modifier.height(14.dp))
Box(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 160.dp, max = 420.dp)
.background(
MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.45f),
RoundedCornerShape(12.dp),
),
) {
SelectionContainer {
Text(
text = state.text,
fontFamily = FontFamily.Monospace,
fontSize = 11.sp,
lineHeight = 15.sp,
modifier = Modifier.verticalScroll(rememberScrollState()).padding(12.dp),
)
}
}
Spacer(Modifier.height(18.dp))
FlowRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End),
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
TextButton(onClick = onDismiss) { Text(stringResource(R.string.common_close)) }
OutlinedButton(
enabled = state.shareEnabled,
onClick = {
IssueReport.copyToClipboard(context, state.text)
Toast.makeText(context, copied, Toast.LENGTH_LONG).show()
},
) { Text(stringResource(R.string.common_copy)) }
Button(
enabled = state.shareEnabled,
onClick = {
if (!IssueReport.share(
context,
subject = chooser,
text = state.text,
chooserTitle = chooser,
)
) {
IssueReport.copyToClipboard(context, state.text)
Toast.makeText(context, noShare, Toast.LENGTH_LONG).show()
}
},
) { Text(stringResource(R.string.common_share)) }
}
}
}
}
}
@@ -15,12 +15,11 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ExpandLess
import androidx.compose.material.icons.filled.ExpandMore
import androidx.compose.material.icons.filled.Psychology
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -29,6 +28,9 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLocale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.semantics.semantics
import com.hermesandroid.relay.R
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.style.TextOverflow
@@ -49,7 +51,12 @@ fun ThinkingBlock(
/** Stable key when this interactive block participates in Chat pet terrain. */
petObstacleKey: String? = null,
) {
if (thinkingContent.isBlank()) return
var expanded by remember { mutableStateOf(isStreaming) }
var userToggled by remember { mutableStateOf(false) }
LaunchedEffect(isStreaming) {
if (!userToggled) expanded = isStreaming
}
val locale = LocalLocale.current.platformLocale
val timeLabel = timestamp?.let {
remember(it, locale) {
@@ -57,8 +64,13 @@ fun ThinkingBlock(
.format(java.util.Date(it))
}
}
val expansionState = if (expanded) {
stringResource(R.string.tool_progress_state_expanded)
} else {
stringResource(R.string.tool_progress_state_collapsed)
}
Card(
Column(
modifier = modifier
.then(
if (petObstacleKey != null) {
@@ -71,18 +83,20 @@ fun ThinkingBlock(
},
)
.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
)
) {
Column(modifier = Modifier.padding(8.dp)) {
// Header row
Row(
modifier = Modifier
.fillMaxWidth()
.clickable { expanded = !expanded },
verticalAlignment = Alignment.CenterVertically
) {
Row(
modifier = Modifier
.fillMaxWidth()
.clickable(role = Role.Button) {
userToggled = true
expanded = !expanded
}
.semantics {
stateDescription = expansionState
}
.padding(horizontal = 4.dp, vertical = 3.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = Icons.Filled.Psychology,
contentDescription = accessibilityLabel,
@@ -91,9 +105,13 @@ fun ThinkingBlock(
)
Spacer(modifier = Modifier.width(6.dp))
Text(
text = headerText ?: if (isStreaming) stringResource(R.string.thinking_thinking) else stringResource(R.string.thinking_title),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.tertiary
text = headerText ?: if (isStreaming) {
stringResource(R.string.thinking_thinking)
} else {
stringResource(R.string.thinking_settled)
},
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Spacer(modifier = Modifier.weight(1f))
if (!isStreaming && timeLabel != null) {
@@ -110,25 +128,23 @@ fun ThinkingBlock(
modifier = Modifier.size(16.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
// Collapsible content
AnimatedVisibility(
visible = expanded,
enter = expandVertically(),
exit = shrinkVertically()
) {
Text(
text = thinkingContent,
style = MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Default,
color = MaterialTheme.colorScheme.onSurfaceVariant
),
modifier = Modifier.padding(top = 4.dp),
maxLines = if (isStreaming) Int.MAX_VALUE else 50,
overflow = TextOverflow.Ellipsis
)
}
AnimatedVisibility(
visible = expanded,
enter = expandVertically(),
exit = shrinkVertically()
) {
Text(
text = thinkingContent,
style = MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Default,
color = MaterialTheme.colorScheme.onSurfaceVariant
),
modifier = Modifier.padding(start = 26.dp, top = 2.dp, end = 4.dp, bottom = 4.dp),
maxLines = if (isStreaming) Int.MAX_VALUE else 50,
overflow = TextOverflow.Ellipsis
)
}
}
}
@@ -0,0 +1,284 @@
package com.hermesandroid.relay.ui.components
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ExpandLess
import androidx.compose.material.icons.filled.ExpandMore
import androidx.compose.material.icons.filled.MoreHoriz
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalResources
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.R
import com.hermesandroid.relay.data.ToolCall
import com.hermesandroid.relay.ui.components.pet.petObstacleSurface
private val TOOL_ACTIVITY_PET_ROUTES = setOf("chat")
private val FILE_EDIT_TOOLS = setOf("edit_file", "write_file", "apply_patch", "patch")
private val ATTENTION_TOOLS = setOf(
"clarify",
"delegate_task",
"image_generate",
"approval",
"request_user_input",
"ask_user",
"sudo",
"secret",
)
internal sealed interface ToolTranscriptItem {
data class ActivityRun(val calls: List<ToolCall>) : ToolTranscriptItem
data class Standalone(val call: ToolCall) : ToolTranscriptItem
}
/** Off suppresses diagnostics, never an attention or deliverable surface. */
internal fun ToolTranscriptItem.isVisibleForToolDisplay(toolDisplay: String): Boolean =
this is ToolTranscriptItem.Standalone || toolDisplay != "off"
/**
* Preserve source order while folding only routine activity. Calls that are
* actionable, security-relevant, failed, or deliver a file/media result split
* the run and retain their own surface.
*/
internal fun groupTranscriptTools(calls: List<ToolCall>): List<ToolTranscriptItem> {
val result = mutableListOf<ToolTranscriptItem>()
val run = mutableListOf<ToolCall>()
fun flushRun() {
if (run.isNotEmpty()) {
result += ToolTranscriptItem.ActivityRun(run.toList())
run.clear()
}
}
calls.forEach { call ->
if (call.requiresStandaloneToolSurface()) {
flushRun()
result += ToolTranscriptItem.Standalone(call)
} else {
run += call
}
}
flushRun()
return result
}
internal fun ToolCall.requiresStandaloneToolSurface(): Boolean {
val normalized = name.trim().lowercase()
return success == false ||
!error.isNullOrBlank() ||
outputRisk != null ||
normalized in FILE_EDIT_TOOLS ||
normalized in ATTENTION_TOOLS
}
internal data class ToolActivityCounts(
val reads: Int = 0,
val searches: Int = 0,
val commands: Int = 0,
val browserActions: Int = 0,
val deviceActions: Int = 0,
val other: Int = 0,
)
internal fun countToolActivity(calls: List<ToolCall>): ToolActivityCounts {
var counts = ToolActivityCounts()
calls.forEach { call ->
val name = call.name.trim().lowercase()
counts = when {
name.contains("search") || name.contains("grep") || name.contains("find") ->
counts.copy(searches = counts.searches + 1)
name.contains("terminal") || name.contains("shell") || name.contains("exec") ||
name.contains("command") || name.contains("bash") || name.contains("powershell") ->
counts.copy(commands = counts.commands + 1)
name.contains("browser") || name.contains("chrome") || name.contains("playwright") ||
name.startsWith("web_") -> counts.copy(browserActions = counts.browserActions + 1)
name.contains("android") || name.contains("adb") || name.contains("device") ||
name.contains("tap") || name.contains("swipe") ->
counts.copy(deviceActions = counts.deviceActions + 1)
name.contains("read") || name.contains("list") || name.contains("glob") ||
name.contains("open") || name.contains("inspect") -> counts.copy(reads = counts.reads + 1)
else -> counts.copy(other = counts.other + 1)
}
}
return counts
}
@Composable
fun ToolActivityRun(
calls: List<ToolCall>,
live: Boolean,
detailed: Boolean,
modifier: Modifier = Modifier,
messageTimestamp: Long? = null,
petObstacleKey: String? = null,
onExpandedChange: (Boolean) -> Unit = {},
) {
if (calls.isEmpty()) return
var expanded by rememberSaveable(calls.first().uiKey) { mutableStateOf(false) }
val summary = toolActivitySummary(countToolActivity(calls), live)
val stateLabel = if (expanded) {
stringResource(R.string.tool_progress_cd_collapse)
} else {
stringResource(R.string.tool_progress_cd_expand)
}
val expansionState = if (expanded) {
stringResource(R.string.tool_progress_state_expanded)
} else {
stringResource(R.string.tool_progress_state_collapsed)
}
Column(
modifier = modifier
.then(
if (petObstacleKey != null) {
Modifier.petObstacleSurface(petObstacleKey, TOOL_ACTIVITY_PET_ROUTES)
} else {
Modifier
},
)
.fillMaxWidth(),
) {
Row(
modifier = Modifier
.fillMaxWidth()
.clickable(role = Role.Button) {
expanded = !expanded
onExpandedChange(expanded)
}
.semantics {
contentDescription = summary
stateDescription = expansionState
}
.padding(horizontal = 4.dp, vertical = 4.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
imageVector = Icons.Filled.MoreHoriz,
contentDescription = null,
modifier = Modifier.size(16.dp),
tint = if (live) MaterialTheme.colorScheme.tertiary
else MaterialTheme.colorScheme.onSurfaceVariant,
)
Spacer(modifier = Modifier.width(6.dp))
Text(
text = summary,
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
Icon(
imageVector = if (expanded) Icons.Filled.ExpandLess else Icons.Filled.ExpandMore,
contentDescription = stateLabel,
modifier = Modifier.size(16.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
if (live && !expanded) {
val latest = calls.last()
AnimatedContent(
targetState = latest.uiKey,
transitionSpec = {
(slideInVertically { it / 2 } + fadeIn()) togetherWith
(slideOutVertically { -it / 2 } + fadeOut())
},
label = "toolActivityTicker",
) { latestKey ->
val visible = calls.lastOrNull { it.uiKey == latestKey } ?: latest
Text(
text = toolActivityTickerLabel(visible),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.78f),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.padding(start = 26.dp, end = 4.dp, bottom = 4.dp),
)
}
}
AnimatedVisibility(
visible = expanded,
enter = expandVertically(),
exit = shrinkVertically(),
) {
Column(modifier = Modifier.padding(start = 22.dp, top = 2.dp)) {
calls.forEach { call ->
key(call.uiKey) {
if (detailed) {
ToolProgressCard(
toolCall = call,
messageTimestamp = messageTimestamp,
onExpandedChange = onExpandedChange,
)
} else {
CompactToolCall(toolCall = call)
}
Spacer(modifier = Modifier.height(4.dp))
}
}
}
}
}
}
@Composable
private fun toolActivitySummary(counts: ToolActivityCounts, live: Boolean): String {
val resources = LocalResources.current
val clauses = buildList {
fun addClause(count: Int, liveRes: Int, settledRes: Int) {
if (count > 0) add(resources.getQuantityString(if (live) liveRes else settledRes, count, count))
}
addClause(counts.reads, R.plurals.tool_run_reading, R.plurals.tool_run_read)
addClause(counts.searches, R.plurals.tool_run_searching, R.plurals.tool_run_searched)
addClause(counts.commands, R.plurals.tool_run_running_commands, R.plurals.tool_run_ran_commands)
addClause(counts.browserActions, R.plurals.tool_run_browsing, R.plurals.tool_run_browsed)
addClause(counts.deviceActions, R.plurals.tool_run_using_device, R.plurals.tool_run_used_device)
addClause(counts.other, R.plurals.tool_run_using_tools, R.plurals.tool_run_used_tools)
}
return clauses.joinToString(" · ")
}
@Composable
private fun toolActivityTickerLabel(call: ToolCall): String {
val name = localizeToolName(call.name).ifBlank { stringResource(R.string.tool_preparing) }
return when {
call.isGenerating && !call.isComplete -> stringResource(R.string.tool_run_preparing, name)
!call.isComplete -> stringResource(R.string.tool_run_running, name)
else -> stringResource(R.string.tool_run_finished, name)
}
}
@@ -40,7 +40,6 @@ import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -69,15 +68,20 @@ fun ToolProgressCard(
* message timestamp. Null hides the time.
*/
messageTimestamp: Long? = null,
/**
* Reports explicit user expansion so the owning transcript can yield its
* live-tail auto-follow while the user inspects arguments or results.
*/
onExpandedChange: (Boolean) -> Unit = {},
) {
// Preparing (tool.generating) starts collapsed — the args preview line
// under the header is the whole story until tool.start lands.
// rememberSaveable (keyed per tool call, namespaced by the message item's
// key in the chat LazyColumn) so a manual expand survives scroll-off /
// re-render instead of snapping shut every time the row recomposes.
var expanded by rememberSaveable(
key = "toolcard:" + (toolCall.id ?: "${toolCall.name}:${toolCall.startedAt}"),
) { mutableStateOf(!toolCall.isComplete && !toolCall.isGenerating) }
// Use the call identity as an input, not a custom saveable key: custom keys
// bypass positional scoping and can share state between nested lazy items.
// uiKey survives generating -> running ID adoption and ordinary copies.
var expanded by rememberSaveable(toolCall.uiKey) {
mutableStateOf(!toolCall.isComplete && !toolCall.isGenerating)
}
val isPreparing = toolCall.isGenerating && !toolCall.isComplete
val timeMillis = toolCall.completedAt ?: messageTimestamp
val locale = LocalLocale.current.platformLocale
@@ -88,11 +92,6 @@ fun ToolProgressCard(
}
}
// Auto-collapse when tool completes
LaunchedEffect(toolCall.isComplete) {
if (toolCall.isComplete) expanded = false
}
val statusIcon: ImageVector
val statusColor = when {
toolCall.isComplete && toolCall.success == true -> {
@@ -162,7 +161,10 @@ fun ToolProgressCard(
Row(
modifier = Modifier
.fillMaxWidth()
.clickable { expanded = !expanded },
.clickable {
expanded = !expanded
onExpandedChange(expanded)
},
verticalAlignment = Alignment.CenterVertically
) {
// Tool type icon
@@ -0,0 +1,368 @@
package com.hermesandroid.relay.ui.components
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.data.ChatMessage
import com.hermesandroid.relay.data.MessageRole
import com.hermesandroid.relay.ui.theme.LocalBrand
@Immutable
data class TranscriptMatch(
val messageUiKey: String,
val messageIndex: Int,
val start: Int,
val endExclusive: Int,
)
@Immutable
data class TranscriptTurnMarker(
val messageUiKey: String,
val messageIndex: Int,
val turnNumber: Int,
val label: String,
val matchCount: Int,
val containsActiveMatch: Boolean,
)
@Immutable
data class TranscriptNavigationModel(
val matches: List<TranscriptMatch>,
val turns: List<TranscriptTurnMarker>,
)
/**
* User-facing copy is injectable so the component remains reusable and its
* eventual ChatScreen integration can supply localized resources without
* coupling the pure navigation model to Android resources.
*/
@Immutable
data class TranscriptNavigatorStrings(
val searchPlaceholder: String = "Search conversation",
val previousMatch: String = "Previous match",
val nextMatch: String = "Next match",
val closeSearch: String = "Close search",
val noResults: String = "No results",
val resultCount: (current: Int, total: Int) -> String = { current, total -> "$current of $total" },
val resultCountDescription: (current: Int, total: Int) -> String = { current, total ->
if (total == 0) "No search results" else "Search result $current of $total"
},
val turnDescription: (turn: Int, label: String, matches: Int) -> String = { turn, label, matches ->
val suffix = if (matches == 1) ", 1 match" else if (matches > 1) ", $matches matches" else ""
"Jump to turn $turn: $label$suffix"
},
)
object TranscriptNavigatorTestTags {
const val Query = "transcript-search-query"
const val ResultCount = "transcript-search-result-count"
const val Previous = "transcript-search-previous"
const val Next = "transcript-search-next"
const val Close = "transcript-search-close"
const val TurnPrefix = "transcript-turn-"
}
/**
* Compact, self-contained find bar plus prompt rail for an active transcript.
* All jump callbacks use [ChatMessage.uiKey], the same stable identity used by
* Chat's LazyColumn, so a history reconciliation cannot invalidate a target.
*/
@Composable
fun TranscriptSearchNavigator(
messages: List<ChatMessage>,
onJumpToMessage: (messageUiKey: String) -> Unit,
onClose: () -> Unit,
modifier: Modifier = Modifier,
initialQuery: String = "",
strings: TranscriptNavigatorStrings = TranscriptNavigatorStrings(),
) {
val brand = LocalBrand.current
var query by rememberSaveable { mutableStateOf(initialQuery) }
var activeMatchIndex by rememberSaveable { mutableIntStateOf(0) }
val model = remember(messages, query, activeMatchIndex) {
buildTranscriptNavigationModel(messages, query, activeMatchIndex)
}
val normalizedActiveIndex = activeMatchIndex.coerceIn(0, (model.matches.size - 1).coerceAtLeast(0))
val focusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) {
runCatching { focusRequester.requestFocus() }
}
fun selectMatch(index: Int) {
if (model.matches.isEmpty()) return
val normalized = Math.floorMod(index, model.matches.size)
activeMatchIndex = normalized
onJumpToMessage(model.matches[normalized].messageUiKey)
}
Column(
modifier = modifier
.fillMaxWidth()
.background(brand.navy)
.border(1.dp, brand.line, RoundedCornerShape(14.dp))
.padding(horizontal = 8.dp, vertical = 6.dp),
verticalArrangement = Arrangement.spacedBy(2.dp),
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(2.dp),
) {
Row(
modifier = Modifier
.weight(1f)
.clip(RoundedCornerShape(10.dp))
.background(brand.surfaceLow)
.border(1.dp, brand.line, RoundedCornerShape(10.dp))
.padding(horizontal = 10.dp, vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Icon(
imageVector = Icons.Filled.Search,
contentDescription = null,
tint = brand.muted,
modifier = Modifier.size(18.dp),
)
BasicTextField(
value = query,
onValueChange = {
query = it
activeMatchIndex = 0
},
modifier = Modifier
.weight(1f)
.focusRequester(focusRequester)
.testTag(TranscriptNavigatorTestTags.Query),
singleLine = true,
textStyle = MaterialTheme.typography.bodyMedium.copy(color = brand.ink),
cursorBrush = SolidColor(brand.relay),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
keyboardActions = KeyboardActions(onSearch = { selectMatch(normalizedActiveIndex) }),
decorationBox = { inner ->
Box {
if (query.isEmpty()) {
Text(
text = strings.searchPlaceholder,
color = brand.dim,
style = MaterialTheme.typography.bodyMedium,
)
}
inner()
}
},
)
}
val current = if (model.matches.isEmpty()) 0 else normalizedActiveIndex + 1
Text(
text = if (model.matches.isEmpty() && query.isNotBlank()) {
strings.noResults
} else {
strings.resultCount(current, model.matches.size)
},
color = brand.muted,
style = MaterialTheme.typography.labelSmall,
maxLines = 1,
modifier = Modifier
.testTag(TranscriptNavigatorTestTags.ResultCount)
.semantics {
contentDescription = strings.resultCountDescription(current, model.matches.size)
},
)
IconButton(
onClick = { selectMatch(normalizedActiveIndex - 1) },
enabled = model.matches.isNotEmpty(),
modifier = Modifier.testTag(TranscriptNavigatorTestTags.Previous),
) {
Icon(Icons.Filled.KeyboardArrowUp, strings.previousMatch, tint = brand.ink)
}
IconButton(
onClick = { selectMatch(normalizedActiveIndex + 1) },
enabled = model.matches.isNotEmpty(),
modifier = Modifier.testTag(TranscriptNavigatorTestTags.Next),
) {
Icon(Icons.Filled.KeyboardArrowDown, strings.nextMatch, tint = brand.ink)
}
IconButton(
onClick = onClose,
modifier = Modifier.testTag(TranscriptNavigatorTestTags.Close),
) {
Icon(Icons.Filled.Close, strings.closeSearch, tint = brand.ink)
}
}
TranscriptTurnRail(
turns = model.turns,
onJumpToMessage = onJumpToMessage,
strings = strings,
)
}
}
@Composable
fun TranscriptTurnRail(
turns: List<TranscriptTurnMarker>,
onJumpToMessage: (messageUiKey: String) -> Unit,
modifier: Modifier = Modifier,
strings: TranscriptNavigatorStrings = TranscriptNavigatorStrings(),
) {
if (turns.isEmpty()) return
val brand = LocalBrand.current
Row(
modifier = modifier
.fillMaxWidth()
.horizontalScroll(rememberScrollState()),
verticalAlignment = Alignment.CenterVertically,
) {
turns.forEach { turn ->
IconButton(
onClick = { onJumpToMessage(turn.messageUiKey) },
modifier = Modifier
.testTag("${TranscriptNavigatorTestTags.TurnPrefix}${turn.messageUiKey}")
.semantics {
contentDescription = strings.turnDescription(
turn.turnNumber,
turn.label,
turn.matchCount,
)
},
) {
Box(
modifier = Modifier
.size(if (turn.containsActiveMatch) 12.dp else 8.dp)
.clip(RoundedCornerShape(50))
.background(
when {
turn.containsActiveMatch -> brand.relay
turn.matchCount > 0 -> brand.purple
else -> brand.dim
},
),
)
}
}
}
}
internal fun buildTranscriptNavigationModel(
messages: List<ChatMessage>,
query: String,
activeMatchIndex: Int = 0,
): TranscriptNavigationModel {
val needle = query.trim()
val matches = if (needle.isEmpty()) {
emptyList()
} else {
buildList {
messages.forEachIndexed { messageIndex, message ->
val text = message.searchableRenderedText()
var start = 0
while (start <= text.length - needle.length) {
val found = text.indexOf(needle, startIndex = start, ignoreCase = true)
if (found < 0) break
add(TranscriptMatch(message.uiKey, messageIndex, found, found + needle.length))
start = found + needle.length.coerceAtLeast(1)
}
}
}
}
val activeMatch = matches.getOrNull(activeMatchIndex.coerceIn(0, (matches.size - 1).coerceAtLeast(0)))
val userIndexes = messages.indices.filter { messages[it].role == MessageRole.USER }
val turns = userIndexes.mapIndexed { ordinal, messageIndex ->
val endExclusive = userIndexes.getOrNull(ordinal + 1) ?: messages.size
val turnMatches = matches.count { it.messageIndex in messageIndex until endExclusive }
val prompt = messages[messageIndex]
TranscriptTurnMarker(
messageUiKey = prompt.uiKey,
messageIndex = messageIndex,
turnNumber = ordinal + 1,
label = prompt.content.compactTranscriptLabel("Prompt ${ordinal + 1}"),
matchCount = turnMatches,
containsActiveMatch = activeMatch?.messageIndex?.let { it in messageIndex until endExclusive } == true,
)
}
return TranscriptNavigationModel(matches = matches, turns = turns)
}
internal fun ChatMessage.searchableRenderedText(): String = buildList {
add(content)
add(thinkingContent)
moaReferences.forEach {
add(it.label)
add(it.text)
}
toolCalls.forEach {
add(it.name)
add(it.taskLabel.orEmpty())
add(it.args.orEmpty())
add(it.result.orEmpty())
add(it.error.orEmpty())
}
attachments.forEach {
add(it.fileName.orEmpty())
add(it.errorMessage.orEmpty())
}
cards.forEach { card ->
add(card.title.orEmpty())
add(card.subtitle.orEmpty())
add(card.body.orEmpty())
card.fields.forEach {
add(it.label)
add(it.value)
}
card.actions.forEach { add(it.label) }
add(card.footer.orEmpty())
}
}.filter(String::isNotBlank).joinToString("\n")
private fun String.compactTranscriptLabel(fallback: String): String {
val compact = trim().replace(Regex("\\s+"), " ")
return when {
compact.isEmpty() -> fallback
compact.length <= 64 -> compact
else -> compact.take(61).trimEnd() + "…"
}
}
@@ -194,27 +194,29 @@ fun VoiceModeOverlay(
modifier = modifier
.fillMaxSize()
.background(if (focusMode) surface.copy(alpha = 0.96f) else Color.Transparent)
// Click-through fix: in focus mode the overlay is a true modal.
// Consume any pointer event a child (mic button, transcript,
// chips, pill) didn't handle so stray taps/swipes don't fall
// through to the chat + session drawer behind it. Children run on
// the same Main pass leaf-first, so this only catches the gaps.
// In Conversation the overlay is intentionally transparent and the
// chat stays interactive, so no scrim is installed.
.then(
if (focusMode) {
Modifier.pointerInput(Unit) {
) {
if (focusMode) {
// Focus is modal, but its click-through guard must be a sibling
// behind the controls. A consuming pointerInput on the root is an
// ancestor of every button and cancels their gesture detectors on
// later pointer passes, leaving ripples without firing callbacks.
// This scrim wins hit-testing only where no foreground control did,
// so blank-space gestures stay off the chat underneath while mic,
// close, pill, and expanded-panel controls remain interactive.
Box(
Modifier
.fillMaxSize()
.testTag("voiceFocusInputScrim")
.pointerInput(Unit) {
awaitPointerEventScope {
while (true) {
awaitPointerEvent().changes.forEach { it.consume() }
}
}
}
} else {
Modifier
}
},
)
) {
}
if (focusMode) {
VoiceSessionPill(
uiState = uiState,
@@ -427,7 +429,7 @@ fun VoiceModeOverlay(
)
}
if (pendingTranscriptText != null) {
item(key = "pending-voice-transcript") {
item(key = "aux:pending-voice-transcript") {
CompactTranscriptRow(
message = ChatMessage(
id = "pending-voice-transcript",
@@ -741,7 +743,8 @@ internal fun pendingVoiceTranscriptText(
return if (alreadyRendered) null else transcribed
}
internal fun voiceTranscriptItemKey(message: ChatMessage): String = message.uiKey
/** Message rows and Voice-only auxiliary rows occupy disjoint key namespaces. */
internal fun voiceTranscriptItemKey(message: ChatMessage): String = "message:${message.uiKey}"
/**
* Conversation-mode voice controls that live inside the chat composer.
@@ -104,8 +104,8 @@ interface AgentAvatar {
}
/**
* Legacy ambient-visualization seam. The app now provides [SphereAvatar] here;
* floating companions are published through [LocalFloatingPet].
* Central/ambient visualization seam. The app provides the independently saved
* Sphere or pet-format background here; floating companions use [LocalFloatingPet].
*/
val LocalAgentAvatar = staticCompositionLocalOf<AgentAvatar> { SphereAvatar }
@@ -117,14 +117,20 @@ val LocalAvailableAvatars = staticCompositionLocalOf<List<AgentAvatar>> { listOf
/**
* The optional floating pet companion. Unlike [LocalAgentAvatar], this does not
* replace the ambient sphere or the active profile's identity image. `null`
* means the user has not selected a companion.
* replace the independently selected central/background visualization or the
* active profile's identity image. `null` means no companion is selected.
*/
val LocalFloatingPet = staticCompositionLocalOf<AgentAvatar?> { null }
/** User-installed pets available to the companion picker (sphere excluded). */
val LocalAvailablePets = staticCompositionLocalOf<List<AgentAvatar>> { emptyList() }
/** Resolve a central visualization without ever coupling it to floating state. */
internal fun resolveBackgroundAvatar(
selectedId: String,
availablePets: List<AgentAvatar>,
): AgentAvatar = availablePets.firstOrNull { it.id == selectedId } ?: SphereAvatar
/** Ambient sphere/background visibility, independent of the motion setting. */
val LocalBackgroundVisualizationEnabled = staticCompositionLocalOf { true }
@@ -143,3 +149,11 @@ val LocalPetPlaybackSpeed = staticCompositionLocalOf { 1f }
* on; the sphere ignores it.
*/
val LocalPetStabilize = staticCompositionLocalOf { true }
/**
* Grounds decoded pet art on the bottom of its render canvas. The floating
* companion enables this at its host boundary so transparent atlas padding
* cannot make a supported pet appear to hover. Pickers, previews, and message
* avatars retain their centered rendering.
*/
val LocalPetGroundOpaqueBottom = staticCompositionLocalOf { false }
@@ -214,10 +214,10 @@ class PetAvatar(
private val decodedSheets = LinkedHashMap<String, DecodedSheet>(4, 0.75f, true)
private val decodedClips = LinkedHashMap<PetDecodeKey, PetFrames>(8, 0.75f, true)
private fun decode(clip: PetClip, stabilize: Boolean): PetFrames? {
val key = PetDecodeKey(clip, stabilize)
private fun decode(clip: PetClip, stabilize: Boolean, groundOpaqueBottom: Boolean): PetFrames? {
val key = PetDecodeKey(clip, stabilize, groundOpaqueBottom)
synchronized(decodedClips) { decodedClips[key] }?.let { return it }
val decoded = decodeClip(clip, stabilize, decodedSheets) ?: return null
val decoded = decodeClip(clip, stabilize, groundOpaqueBottom, decodedSheets) ?: return null
synchronized(decodedClips) {
decodedClips[key] = decoded
while (decodedClips.size > PET_DECODED_CLIP_CACHE_ENTRIES) {
@@ -363,18 +363,21 @@ class PetAvatar(
// character that floats/jumps cell-to-cell). Global Appearance toggle;
// flipping it selects a separately cached decode.
val stabilize = LocalPetStabilize.current
val groundOpaqueBottom = LocalPetGroundOpaqueBottom.current
// Decode the active clip off the main thread. Keep the last complete
// visual while a new state is loading so greet/done/locomotion swaps do
// not expose the Canvas as a blank frame.
var displayedClip by remember(id, stabilize) {
var displayedClip by remember(id, stabilize, groundOpaqueBottom) {
mutableStateOf<DisplayedPetClip?>(null)
}
LaunchedEffect(id, clip, stabilize, mirrorHorizontally) {
LaunchedEffect(id, clip, stabilize, groundOpaqueBottom, mirrorHorizontally) {
val decoded = if (clip == null) {
null
} else {
withContext(Dispatchers.IO) { runCatching { decode(clip, stabilize) }.getOrNull() }
withContext(Dispatchers.IO) {
runCatching { decode(clip, stabilize, groundOpaqueBottom) }.getOrNull()
}
?.let { DisplayedPetClip(it, mirrorHorizontally) }
}
displayedClip = retainPetFrameDuringDecode(
@@ -484,6 +487,7 @@ private data class DisplayedPetClip(
private data class PetDecodeKey(
val clip: PetClip,
val stabilize: Boolean,
val groundOpaqueBottom: Boolean,
)
/** One decoded atlas per selected pet; all of its row clips share these pixels. */
@@ -492,6 +496,7 @@ private data class DecodedSheet(val bitmap: Bitmap, val image: ImageBitmap)
private fun decodeClip(
clip: PetClip,
stabilize: Boolean,
groundOpaqueBottom: Boolean,
decodedSheets: MutableMap<String, DecodedSheet>,
): PetFrames? = when (clip) {
is FrameSequenceClip -> {
@@ -530,7 +535,13 @@ private fun decodeClip(
frameHeight = bitmaps.first().height,
frameCount = bitmaps.size,
fps = clip.fps,
centerOffsets = if (stabilize) bitmaps.map { bitmapRecenter(it) } else null,
centerOffsets = if (stabilize || groundOpaqueBottom) {
bitmaps.map { bitmap ->
bitmapContentAlignment(bitmap, stabilize, groundOpaqueBottom)
}
} else {
null
},
)
}
}
@@ -583,8 +594,17 @@ private fun decodeClip(
frameCount = count,
fps = clip.fps,
startFrame = clip.startFrame,
centerOffsets = if (stabilize) {
sheetRecenter(bmp, cols, clip.frameWidth, clip.frameHeight, clip.startFrame, count)
centerOffsets = if (stabilize || groundOpaqueBottom) {
sheetContentAlignment(
bmp,
cols,
clip.frameWidth,
clip.frameHeight,
clip.startFrame,
count,
stabilize,
groundOpaqueBottom,
)
} else {
null
},
@@ -593,33 +613,65 @@ private fun decodeClip(
}
}
/** Per-cell recenter offsets for a sprite sheet (see [contentRecenter]). */
private fun sheetRecenter(
/** Per-cell alignment offsets for a sprite sheet (see [contentAlignment]). */
private fun sheetContentAlignment(
bmp: Bitmap,
cols: Int,
fw: Int,
fh: Int,
startFrame: Int,
count: Int,
stabilize: Boolean,
groundOpaqueBottom: Boolean,
): List<IntOffset> {
val buf = IntArray(fw * fh)
return (0 until count).map { i ->
val cell = startFrame + i
contentRecenter(bmp, (cell % cols) * fw, (cell / cols) * fh, fw, fh, buf)
contentAlignment(
bmp,
(cell % cols) * fw,
(cell / cols) * fh,
fw,
fh,
buf,
stabilize,
groundOpaqueBottom,
)
}
}
/** Recenter offset for one standalone frame bitmap. */
private fun bitmapRecenter(bmp: Bitmap): IntOffset =
contentRecenter(bmp, 0, 0, bmp.width, bmp.height, IntArray(bmp.width * bmp.height))
/** Content alignment offset for one standalone frame bitmap. */
private fun bitmapContentAlignment(
bmp: Bitmap,
stabilize: Boolean,
groundOpaqueBottom: Boolean,
): IntOffset = contentAlignment(
bmp,
0,
0,
bmp.width,
bmp.height,
IntArray(bmp.width * bmp.height),
stabilize,
groundOpaqueBottom,
)
/**
* Scan the [w]×[h] region at ([x0],[y0]) of [bmp] for opaque pixels and return the
* offset (source px) that moves their bounding-box center to the region center —
* cancelling per-frame positional drift. [buf] (size ≥ [w]×[h]) is reused scratch.
* Returns [IntOffset.Zero] for a fully-transparent region.
* offset (source px) that horizontally stabilizes the content and, when asked,
* grounds its opaque bottom. [buf] (size ≥ [w]×[h]) is reused scratch. Returns
* [IntOffset.Zero] for a fully-transparent region.
*/
private fun contentRecenter(bmp: Bitmap, x0: Int, y0: Int, w: Int, h: Int, buf: IntArray): IntOffset {
private fun contentAlignment(
bmp: Bitmap,
x0: Int,
y0: Int,
w: Int,
h: Int,
buf: IntArray,
stabilize: Boolean,
groundOpaqueBottom: Boolean,
): IntOffset {
bmp.getPixels(buf, 0, w, x0, y0, w, h)
var minX = w
var minY = h
@@ -638,7 +690,44 @@ private fun contentRecenter(bmp: Bitmap, x0: Int, y0: Int, w: Int, h: Int, buf:
}
}
if (maxX < 0) return IntOffset.Zero
return IntOffset(w / 2 - (minX + maxX) / 2, h / 2 - (minY + maxY) / 2)
return petContentAlignmentOffset(
width = w,
height = h,
minX = minX,
minY = minY,
maxX = maxX,
maxY = maxY,
stabilize = stabilize,
groundOpaqueBottom = groundOpaqueBottom,
)
}
/** Pure alignment policy shared by atlas and standalone frames. */
internal fun petContentAlignmentOffset(
width: Int,
height: Int,
minX: Int,
minY: Int,
maxX: Int,
maxY: Int,
stabilize: Boolean,
groundOpaqueBottom: Boolean,
): IntOffset {
if (
width <= 0 || height <= 0 || minX < 0 || minY < 0 ||
maxX < minX || maxY < minY || maxX >= width || maxY >= height
) {
return IntOffset.Zero
}
val x = if (stabilize) width / 2 - (minX + maxX) / 2 else 0
val y = if (groundOpaqueBottom) {
height - 1 - maxY
} else if (stabilize) {
height / 2 - (minY + maxY) / 2
} else {
0
}
return IntOffset(x, y)
}
/** Draw frame [index] of [f], contain-fit + centered, scaled by [bounce]. */
@@ -12,6 +12,8 @@ import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import com.hermesandroid.relay.ui.components.SphereState
import com.hermesandroid.relay.ui.components.avatar.AvatarRenderState
@@ -100,6 +102,7 @@ class PetCompanionCoordinator {
private var renderState by mutableStateOf(AvatarRenderState(SphereState.Idle))
private var visitRequestState by mutableStateOf(PetVisitRequestState())
private val surfaces = mutableStateMapOf<String, PetCompanionSurface>()
private val interactionLayers = mutableStateMapOf<String, Boolean>()
val pendingVisitRequest: PetVisitRequest?
get() = visitRequestState.pending
@@ -141,20 +144,49 @@ class PetCompanionCoordinator {
surfaces.remove(owner)
}
/** App-wide UI ownership gate for drawers and other same-window overlays. */
fun publishInteractionLayer(owner: String, active: Boolean) {
require(owner.isNotBlank()) { "Pet interaction-layer owner must not be blank." }
if (active) interactionLayers[owner] = true else interactionLayers.remove(owner)
}
fun clearInteractionLayer(owner: String) {
interactionLayers.remove(owner)
}
fun activityFor(owner: String?): PetCompanionActivity {
val surface = owner?.let(surfaces::get)
return PetCompanionActivity(
renderState = renderState,
scrolling = surface?.scrolling == true,
hidden = surface?.hidden == true,
hidden = surface?.hidden == true || interactionLayers.values.any { it },
)
}
}
private data class PetCompanionSurface(val scrolling: Boolean, val hidden: Boolean)
/** A pet-owned popup may take window focus without becoming a competing modal. */
internal fun platformModalOwnsPetLayer(
windowFocused: Boolean,
petMenuExpanded: Boolean,
): Boolean = !windowFocused && !petMenuExpanded
val LocalPetCompanionCoordinator = staticCompositionLocalOf { PetCompanionCoordinator() }
/**
* Declares that a same-window interaction layer currently owns input. Platform
* dialogs and modal sheets are covered separately by root window-focus state.
*/
@Composable
fun PetInteractionLayer(owner: String, active: Boolean) {
val coordinator = LocalPetCompanionCoordinator.current
SideEffect { coordinator.publishInteractionLayer(owner, active) }
DisposableEffect(coordinator, owner) {
onDispose { coordinator.clearInteractionLayer(owner) }
}
}
/**
* Explicit UI surfaces measured by their owners. Their top edges become
* walkable perches, matching Hermes Desktop's live-DOM ledge model without
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -60,6 +60,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.R
import com.hermesandroid.relay.data.PhysicalKeyboardEnterBehavior
import com.hermesandroid.relay.network.upstream.GatewayAvailability
import com.hermesandroid.relay.network.upstream.ServerCapabilities
import com.hermesandroid.relay.ui.components.ChatTransportStatus
@@ -443,6 +444,48 @@ fun ChatSettingsScreen(
HorizontalDivider()
val physicalKeyboardEnterBehavior by
connectionViewModel.physicalKeyboardEnterBehavior.collectAsState()
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text(
text = stringResource(R.string.chat_settings_physical_keyboard_enter),
style = MaterialTheme.typography.bodyMedium,
)
Text(
text = stringResource(R.string.chat_settings_physical_keyboard_enter_desc),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
val enterBehaviors = PhysicalKeyboardEnterBehavior.entries
SingleChoiceSegmentedButtonRow(modifier = Modifier.fillMaxWidth()) {
enterBehaviors.forEachIndexed { index, behavior ->
SegmentedButton(
shape = SegmentedButtonDefaults.itemShape(
index = index,
count = enterBehaviors.size,
),
onClick = {
connectionViewModel.setPhysicalKeyboardEnterBehavior(behavior)
},
selected = behavior == physicalKeyboardEnterBehavior,
) {
Text(
text = stringResource(
if (behavior == PhysicalKeyboardEnterBehavior.SendMessage) {
R.string.chat_input_send_message
} else {
R.string.chat_settings_insert_newline
},
),
style = MaterialTheme.typography.labelMedium,
)
}
}
}
}
HorizontalDivider()
// Background chat-alert toggle. First enable on
// API 33+ runs the POST_NOTIFICATIONS request (the
// BridgeScreen master-toggle precedent); if the user
@@ -0,0 +1,249 @@
package com.hermesandroid.relay.ui.screens
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.AutoAwesome
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.R
import com.hermesandroid.relay.viewmodel.ConnectionViewModel
import kotlinx.coroutines.launch
internal enum class CustomPetKind { STATIC, ANIMATED }
internal fun buildCustomPetPrompt(
name: String,
description: String,
kind: CustomPetKind,
): String {
val safeName = name.trim().ifBlank { "My Hermes companion" }
val safeDescription = description.trim().ifBlank {
"a friendly companion that remains recognizable at small mobile sizes"
}
val output = if (kind == CustomPetKind.STATIC) {
"Create one transparent PNG or WebP image. Keep the subject centered with generous edge padding."
} else {
"Create a validated Hermes pet pack ZIP using pet.json plus transparent sprite sheets. " +
"Include idle, walk-left/right, thinking or working, writing, speaking, listening, greet, done, and error where practical. " +
"Frames must show real motion while keeping the character, scale, baseline, and anchor consistent. " +
"At minimum, pet.json must use schemaVersion 1, a stable id and label, and an idle state. " +
"A frame-list state is shaped like {\"frames\":[\"idle-01.png\"],\"fps\":8}; a sheet state is shaped like " +
"{\"sheet\":\"idle.png\",\"frameWidth\":256,\"frameHeight\":256,\"frameCount\":16,\"fps\":8}. " +
"All referenced files must remain inside the pack folder."
}
return """
Help me create a custom floating pet for the Hermes Relay Android app.
Pet name: $safeName
Character direction: $safeDescription
Format: ${if (kind == CustomPetKind.STATIC) "single static image" else "animated pet pack"}
$output
Follow the current Hermes Relay pet contract above. If the repository docs/pet-spec.md or user-docs/features/custom-avatars.md are available, use them as the final authority. Use image-generation tools only if they are available in this chat. Do not upload my reference image, generated art, or pet pack anywhere except through tools I explicitly approve. Work locally where possible.
Before returning anything, validate the manifest and every referenced file, reject path traversal, verify decoded image dimensions and safe margins, and check that animated frames differ visibly without drifting. Then show me a concise review summary and attach the final PNG/WebP or ZIP for me to inspect. Do not install, publish, or share it automatically. I will review it and import it from Appearance > Floating pet.
If you need a visual reference, ask me to attach it in this chat before generating the pet.
""".trimIndent()
}
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@Composable
fun CustomPetGuideScreen(
connectionViewModel: ConnectionViewModel,
onBack: () -> Unit,
onStartNewChat: (String) -> Unit,
) {
var name by remember { mutableStateOf("") }
var description by remember { mutableStateOf("") }
var kind by remember { mutableStateOf(CustomPetKind.ANIMATED) }
val prompt = remember(name, description, kind) { buildCustomPetPrompt(name, description, kind) }
val clipboard = LocalClipboardManager.current
val copiedMessage = stringResource(R.string.pet_creator_copied)
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
val importLauncher = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
uri?.let(connectionViewModel::importPet)
}
LaunchedEffect(connectionViewModel) {
connectionViewModel.avatarEvents.collect { snackbarHostState.showSnackbar(it) }
}
Scaffold(
topBar = {
TopAppBar(
title = { Text(stringResource(R.string.pet_creator_title)) },
navigationIcon = {
IconButton(onClick = onBack) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, stringResource(R.string.appearance_back))
}
},
colors = TopAppBarDefaults.topAppBarColors(containerColor = MaterialTheme.colorScheme.surface),
)
},
snackbarHost = { SnackbarHost(snackbarHostState) },
) { innerPadding ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.verticalScroll(rememberScrollState())
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Text(
text = stringResource(R.string.pet_creator_intro),
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
GuideCard(step = "1", title = stringResource(R.string.pet_creator_step_idea)) {
OutlinedTextField(
value = name,
onValueChange = { name = it },
modifier = Modifier.fillMaxWidth(),
label = { Text(stringResource(R.string.pet_creator_name)) },
singleLine = true,
)
OutlinedTextField(
value = description,
onValueChange = { description = it },
modifier = Modifier.fillMaxWidth(),
label = { Text(stringResource(R.string.pet_creator_description)) },
supportingText = { Text(stringResource(R.string.pet_creator_description_hint)) },
minLines = 3,
)
FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
FilterChip(
selected = kind == CustomPetKind.ANIMATED,
onClick = { kind = CustomPetKind.ANIMATED },
label = { Text(stringResource(R.string.pet_creator_animated)) },
)
FilterChip(
selected = kind == CustomPetKind.STATIC,
onClick = { kind = CustomPetKind.STATIC },
label = { Text(stringResource(R.string.pet_creator_static)) },
)
}
}
GuideCard(step = "2", title = stringResource(R.string.pet_creator_step_create)) {
Text(
text = stringResource(R.string.pet_creator_review_copy),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Card(
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
shape = RoundedCornerShape(12.dp),
) {
Text(
text = prompt,
modifier = Modifier.padding(14.dp),
style = MaterialTheme.typography.bodySmall,
)
}
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
OutlinedButton(
onClick = {
clipboard.setText(AnnotatedString(prompt))
scope.launch { snackbarHostState.showSnackbar(copiedMessage) }
},
modifier = Modifier.weight(1f),
) {
Icon(Icons.Filled.ContentCopy, null)
Text(stringResource(R.string.pet_creator_copy), modifier = Modifier.padding(start = 6.dp))
}
Button(
onClick = { onStartNewChat(prompt) },
modifier = Modifier.weight(1f),
) {
Icon(Icons.Filled.AutoAwesome, null)
Text(stringResource(R.string.pet_creator_new_chat), modifier = Modifier.padding(start = 6.dp))
}
}
}
GuideCard(step = "3", title = stringResource(R.string.pet_creator_step_review)) {
Text(
text = stringResource(R.string.pet_creator_review_desc),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
OutlinedButton(
onClick = {
importLauncher.launch(arrayOf("application/zip", "image/*", "application/octet-stream", "*/*"))
},
modifier = Modifier.fillMaxWidth(),
) {
Icon(Icons.Filled.Add, null)
Text(stringResource(R.string.pet_creator_import), modifier = Modifier.padding(start = 6.dp))
}
}
}
}
}
@Composable
private fun GuideCard(step: String, title: String, content: @Composable ColumnScope.() -> Unit) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surfaceVariant),
shape = RoundedCornerShape(16.dp),
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
Text(
text = "$step · $title",
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.primary,
)
content()
}
}
}
@@ -2833,7 +2833,7 @@ internal fun parseModelOptions(root: JsonObject): List<ModelProviderOption> {
.mapNotNull { (it as? JsonPrimitive)?.contentOrNull?.trim()?.takeIf(String::isNotBlank) }
.map { it.lowercase() }
.toSet()
return providers.mapNotNull { element ->
val parsed = providers.mapNotNull { element ->
val obj = element as? JsonObject ?: return@mapNotNull null
val id = obj.stringField("slug")
?: obj.stringField("id")
@@ -2860,10 +2860,25 @@ internal fun parseModelOptions(root: JsonObject): List<ModelProviderOption> {
// Absent hint field: a row with models is assumed usable; an empty
// row can only be an unconfigured skeleton, so grey it.
authenticated = obj.booleanField("authenticated") ?: models.isNotEmpty(),
models = models,
models = models.distinct(),
setupHint = obj.stringField("warning"),
)
}.sortedByDescending { it.authenticated }
}
val merged = linkedMapOf<String, ModelProviderOption>()
parsed.forEach { row ->
val identity = row.id.trim().lowercase()
val existing = merged[identity]
merged[identity] = if (existing == null) {
row.copy(id = row.id.trim())
} else {
existing.copy(
authenticated = existing.authenticated || row.authenticated,
models = (existing.models + row.models).distinct(),
setupHint = existing.setupHint ?: row.setupHint,
)
}
}
return merged.values.sortedByDescending { it.authenticated }
}
@Composable
@@ -1,5 +1,6 @@
package com.hermesandroid.relay.ui.screens
import android.util.Log
import android.webkit.CookieManager
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
@@ -46,6 +47,9 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.hermesandroid.relay.R
import com.hermesandroid.relay.diagnostics.DiagnosticCategory
import com.hermesandroid.relay.diagnostics.DiagnosticSeverity
import com.hermesandroid.relay.diagnostics.DiagnosticsLog
import com.hermesandroid.relay.ui.components.ConnectionSetupTimeline
import com.hermesandroid.relay.ui.components.ConnectionSetupTimelineStep
import com.hermesandroid.relay.network.upstream.DashboardApiClient
@@ -58,6 +62,8 @@ import com.hermesandroid.relay.network.upstream.NativeDashboardSignInCoordinator
import com.hermesandroid.relay.network.upstream.androidDashboardRedirectAuthMode
import com.hermesandroid.relay.network.upstream.importDashboardCookieHeader
import com.hermesandroid.relay.network.upstream.isNativeDashboardTransportEligible
import com.hermesandroid.relay.network.upstream.nativeDashboardSignInFailureDiagnostic
import com.hermesandroid.relay.network.upstream.nativeDashboardSignInFailureStage
import com.hermesandroid.relay.viewmodel.ConnectionViewModel
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
@@ -66,6 +72,8 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
private const val NATIVE_DASHBOARD_AUTH_LOG_TAG = "HermesNativeAuth"
/**
* Connection-level Dashboard authentication flow. It is deliberately outside
* Manage so onboarding, connection setup, Voice, Chat, and Manage can all use
@@ -246,8 +254,24 @@ fun DashboardSignInScreen(
} catch (cancelled: CancellationException) {
throw cancelled
} catch (error: Exception) {
actionMessage = error.message
?: resources.getString(R.string.dashboard_signin_failed)
val failureStage = nativeDashboardSignInFailureStage(error)
val failureDetail = nativeDashboardSignInFailureDiagnostic(error)
DiagnosticsLog.record(
category = DiagnosticCategory.Auth,
severity = DiagnosticSeverity.Error,
title = resources.getString(R.string.dashboard_signin_failed),
detail = failureDetail,
operation = "dashboard_native_pkce",
)
Log.w(NATIVE_DASHBOARD_AUTH_LOG_TAG, failureDetail)
actionMessage = nativeDashboardSignInActionMessage(
failureStage = failureStage,
errorMessage = error.message,
fallbackMessage = resources.getString(R.string.dashboard_signin_failed),
transportRetryMessage = resources.getString(
R.string.dashboard_native_signin_transport_retry,
),
)
actionIsError = true
} finally {
actionInFlight = false
@@ -338,6 +362,17 @@ fun DashboardSignInScreen(
}
}
internal fun nativeDashboardSignInActionMessage(
failureStage: String,
errorMessage: String?,
fallbackMessage: String,
transportRetryMessage: String,
): String = if (failureStage.startsWith("token_transport")) {
transportRetryMessage
} else {
errorMessage ?: fallbackMessage
}
@Composable
private fun DashboardAuthenticationComplete(onContinue: () -> Unit) {
Column(
@@ -3,6 +3,7 @@ package com.hermesandroid.relay.ui.screens
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
@@ -10,6 +11,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
@@ -24,6 +26,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
@@ -42,6 +45,13 @@ import com.hermesandroid.relay.network.upstream.ServerCapabilities
import com.hermesandroid.relay.ui.components.DiagnosticDetailDialog
import com.hermesandroid.relay.ui.components.DiagnosticsLogPanel
import com.hermesandroid.relay.ui.components.StatusCheckTimeline
import com.hermesandroid.relay.ui.components.SupportBundleDialog
import com.hermesandroid.relay.ui.components.SupportReviewState
import com.hermesandroid.relay.ui.components.buildSupportReviewState
import com.hermesandroid.relay.reliability.ReliabilityCenter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import com.hermesandroid.relay.viewmodel.ChatRuntimeStatus
import com.hermesandroid.relay.viewmodel.ChatTransportPath
import com.hermesandroid.relay.viewmodel.ChatTransportReadiness
@@ -118,6 +128,8 @@ fun DiagnosticsScreen(
// Tapping a check backed by a concrete log entry opens its full detail.
var selectedEntry by remember { mutableStateOf<DiagnosticLogEntry?>(null) }
var supportReview by remember { mutableStateOf<SupportReviewState?>(null) }
val scope = rememberCoroutineScope()
Scaffold(
topBar = {
@@ -232,6 +244,19 @@ fun DiagnosticsScreen(
},
)
OutlinedButton(
onClick = {
scope.launch {
supportReview = withContext(Dispatchers.IO) {
buildSupportReviewState(ReliabilityCenter.reports(context))
}
}
},
modifier = Modifier.fillMaxWidth(),
) {
Text(stringResource(R.string.support_bundle_review))
}
Text(
text = stringResource(R.string.diag_recent_diagnostics),
style = MaterialTheme.typography.titleMedium,
@@ -256,6 +281,9 @@ fun DiagnosticsScreen(
selectedEntry?.let { entry ->
DiagnosticDetailDialog(entry = entry, onDismiss = { selectedEntry = null })
}
supportReview?.let { state ->
SupportBundleDialog(state = state, onDismiss = { supportReview = null })
}
}
// -----------------------------------------------------------------------------
@@ -295,7 +323,7 @@ internal fun buildStatusChecks(
it.category == category && it.severity == DiagnosticSeverity.Error
}
fun DiagnosticLogEntry.message(): String = detail ?: title
fun DiagnosticLogEntry.message(): String = suggestion ?: detail ?: title
val checks = mutableListOf<StatusCheck>()
@@ -4,12 +4,15 @@ package com.hermesandroid.relay.ui.screens
import android.widget.Toast
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
@@ -17,9 +20,14 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.hermesandroid.relay.R
import com.hermesandroid.relay.ui.components.ConnectionWizard
import com.hermesandroid.relay.viewmodel.ConnectionViewModel
@@ -88,21 +96,47 @@ fun PairScreen(
Box(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.padding(innerPadding),
contentAlignment = Alignment.Center,
) {
ConnectionWizard(
connectionViewModel = connectionViewModel,
onComplete = {
Toast.makeText(context, context.getString(R.string.pair_connection_updated), Toast.LENGTH_SHORT).show()
onComplete()
},
onCancel = onCancel,
onManageSignIn = onManageSignIn,
showSkip = false,
autoStart = autoStart,
setupReady = setupReady,
onTryDemo = onTryDemo,
)
if (!setupReady) {
// Add connection navigates before its connection-scoped auth
// store is ready. Keep one stable surface on screen instead of
// composing a disabled wizard that is immediately invalidated
// by the placeholder switch's URL/auth/connection emissions.
Column(
modifier = Modifier.semantics {
liveRegion = LiveRegionMode.Polite
},
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
CircularProgressIndicator()
Text(
text = stringResource(R.string.cw_preparing_connection),
style = MaterialTheme.typography.titleMedium,
)
Text(
text = stringResource(R.string.cw_preparing_connection_hint),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
} else {
ConnectionWizard(
connectionViewModel = connectionViewModel,
onComplete = {
Toast.makeText(context, context.getString(R.string.pair_connection_updated), Toast.LENGTH_SHORT).show()
onComplete()
},
onCancel = onCancel,
onManageSignIn = onManageSignIn,
showSkip = false,
autoStart = autoStart,
setupReady = true,
onTryDemo = onTryDemo,
)
}
}
}
}
@@ -424,7 +424,6 @@ fun SettingsScreen(
val lockedDisplayName: String? = when {
!isProfileLocked -> null
lockedProfileName == null ||
AgentDisplay.isServerDefaultAlias(lockedProfileName) ||
lockedProfileName == AgentDisplay.SERVER_DEFAULT_PROFILE_KEY ->
serverDefaultLabel
else ->
@@ -1055,13 +1054,12 @@ private fun ProfileLockDialog(
onUnlock: () -> Unit,
onDismiss: () -> Unit,
) {
// Selectable rows: a synthetic "Server default" sentinel + the advertised
// profiles, minus the synthetic "default" alias (folded into Server default).
val selectableProfiles = profiles.filterNot { AgentDisplay.isServerDefaultAlias(it.name) }
// Selectable rows: a synthetic "Server default" sentinel + every advertised
// profile, including a profile literally named `default`.
val selectableProfiles = profiles
// Is the stored lock target Server default (sentinel / "default" alias / null)?
// Is the stored lock target Server default (sentinel / null)?
val lockedIsServerDefault = lockedProfileName == null ||
AgentDisplay.isServerDefaultAlias(lockedProfileName) ||
lockedProfileName == AgentDisplay.SERVER_DEFAULT_PROFILE_KEY
val lockedProfile = if (lockedIsServerDefault) {
null
@@ -0,0 +1,41 @@
package com.hermesandroid.relay.ui.theme
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Shapes
import androidx.compose.ui.unit.dp
enum class AppearanceShape(val id: String) {
SOFT("soft"),
BALANCED("balanced"),
SHARP("sharp");
companion object {
val DEFAULT = SOFT
fun fromId(id: String?): AppearanceShape = entries.firstOrNull { it.id == id } ?: DEFAULT
}
}
fun appearanceShapes(shapeId: String?): Shapes = when (AppearanceShape.fromId(shapeId)) {
AppearanceShape.SOFT -> Shapes(
extraSmall = RoundedCornerShape(8.dp),
small = RoundedCornerShape(12.dp),
medium = RoundedCornerShape(18.dp),
large = RoundedCornerShape(24.dp),
extraLarge = RoundedCornerShape(30.dp),
)
AppearanceShape.BALANCED -> Shapes(
extraSmall = RoundedCornerShape(5.dp),
small = RoundedCornerShape(8.dp),
medium = RoundedCornerShape(12.dp),
large = RoundedCornerShape(16.dp),
extraLarge = RoundedCornerShape(20.dp),
)
AppearanceShape.SHARP -> Shapes(
extraSmall = RoundedCornerShape(2.dp),
small = RoundedCornerShape(4.dp),
medium = RoundedCornerShape(6.dp),
large = RoundedCornerShape(8.dp),
extraLarge = RoundedCornerShape(10.dp),
)
}
@@ -7,6 +7,7 @@ import androidx.compose.runtime.Immutable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.lerp
import androidx.compose.ui.graphics.luminance
/**
* Theme-scoped brand token bundle.
@@ -79,6 +80,41 @@ val LocalBrand = staticCompositionLocalOf { BrandPalettes.HermesDark }
private fun Color.lighten(f: Float): Color = lerp(this, Color.White, f)
private fun Color.darken(f: Float): Color = lerp(this, Color.Black, f)
/** User-selectable accent seeds. Stored as stable RGB strings in DataStore. */
val AccentSwatches = listOf(
"#356CFF", "#00B5A0", "#62C51E", "#ED4E8B", "#FF4B00",
)
fun normalizeAccentHex(value: String?): String? {
val normalized = value?.trim()?.uppercase()?.let { if (it.startsWith("#")) it else "#$it" }
return normalized?.takeIf { it.matches(Regex("#[0-9A-F]{6}")) }
}
fun accentColor(value: String?): Color? = normalizeAccentHex(value)?.let {
Color(0xFF000000L or it.drop(1).toLong(16))
}
fun contrastRatio(foreground: Color, background: Color): Float {
val light = maxOf(foreground.luminance(), background.luminance())
val dark = minOf(foreground.luminance(), background.luminance())
return (light + 0.05f) / (dark + 0.05f)
}
fun readableContentColor(background: Color): Color =
listOf(Color.Black, Color.White).maxBy { contrastRatio(it, background) }
/** Apply one accent seed without changing surfaces or semantic status colors. */
fun BrandPalette.withAccent(accentHex: String?): BrandPalette {
val accent = accentColor(accentHex) ?: return this
return copy(
relay = if (isDark) accent.lighten(0.34f) else accent.darken(0.08f),
electric = accent,
electricMuted = if (isDark) accent.lighten(0.18f) else accent.lighten(0.12f),
purple = lerp(accent, purple, 0.38f),
cyan = lerp(accent, cyan, 0.45f),
)
}
/**
* Derive a complete Material 3 [ColorScheme] from the palette. Dark and light
* palettes use distinct mappings; the "text on a deep accent chip" slots use
@@ -87,15 +123,15 @@ private fun Color.darken(f: Float): Color = lerp(this, Color.Black, f)
fun BrandPalette.toColorScheme(): ColorScheme = if (isDark) {
darkColorScheme(
primary = relay,
onPrimary = background,
onPrimary = readableContentColor(relay),
primaryContainer = electric,
onPrimaryContainer = paper,
onPrimaryContainer = readableContentColor(electric),
secondary = purple,
onSecondary = paper,
onSecondary = readableContentColor(purple),
secondaryContainer = navy3,
onSecondaryContainer = paper,
tertiary = cyan,
onTertiary = background,
onTertiary = readableContentColor(cyan),
tertiaryContainer = purple.copy(alpha = 0.42f),
onTertiaryContainer = paper,
background = background,
@@ -119,15 +155,15 @@ fun BrandPalette.toColorScheme(): ColorScheme = if (isDark) {
} else {
lightColorScheme(
primary = electric,
onPrimary = Color.White,
onPrimary = readableContentColor(electric),
primaryContainer = relay.copy(alpha = 0.22f),
onPrimaryContainer = electric.darken(0.32f),
secondary = purple,
onSecondary = Color.White,
onSecondary = readableContentColor(purple),
secondaryContainer = purple.copy(alpha = 0.16f),
onSecondaryContainer = purple.darken(0.38f),
tertiary = cyan,
onTertiary = Color.White,
onTertiary = readableContentColor(cyan),
tertiaryContainer = cyan.copy(alpha = 0.16f),
onTertiaryContainer = cyan.darken(0.42f),
background = background,

Some files were not shown because too many files have changed in this diff Show More