Skip to content

feat(mobile): add context-aware prompt optimization in composer - #2036

Merged
NanmiCoder merged 2 commits into
mainfrom
feat/mobile-prompt-optimize
Nov 26, 2026
Merged

NanmiCoder merged 2 commits into
mainfrom
feat/mobile-prompt-optimize

Conversation

@NanmiCoder

Copy link
Copy Markdown
Owner

Summary

Adds input-side context-aware prompt optimization to the mobile app (iOS, Android, and H5), bringing parity with the desktop implementation (#1965) and closing #2025:

  • Reuses the existing side-channel POST /api/sessions/:id/optimize-prompt (120 s timeout) and DELETE /api/sessions/:id/optimize-prompt/:requestId endpoints, working on idle, empty, and mid-turn (queueing) sessions without mutating session transcripts.
  • Adds a 44×44 wand action button (PromptOptimizeButton) in ChatComposer (shown only in existing sessions when draft text is non-empty and not a / command) and an inline status/restore/error bar (PromptOptimizeFeedback) with one-click Restore (15 s window) and clear Chinese error feedback.
  • Handles cancellation safely across typing, sending, voice dictation, external draft changes, screen blur (useFocusEffect), host/session switches, and unmounts via useLayoutEffect and per-run client capture.
  • Prevents data loss against pre-v0.8.0 desktop hosts: mobile probes read-only GET /api/sessions/:id/optimize-prompt before issuing the first DELETE (only sending DELETE when the v0.8.0+ 405 Unsupported prompt optimization route signature is confirmed), and the server adds an explicit if (subResource) 404 guard before /api/sessions/:id item handlers in src/server/api/sessions.ts.
  • Adds matching optimize-prompt routes and self-test assertions to mobile/scripts/fixture-server.ts.

Closes #2025

Feature Quality Contract

  • Changed surface: mobile, server
  • Tests added or updated:
    • mobile/src/core/promptOptimize.test.ts: unit tests for isOptimizablePrompt, API timeout/encoding, legacy pre-v0.8.0 GET-probe cancel protection, v0.8.0+ probe caching, error/abort mapping, oversized prompt guard, per-run client cancel targeting, edit/session-switch cancellation, and restore flow.
    • mobile/src/core/usePromptOptimize.test.ts: real React (react-reconciler) lifecycle tests covering controlled draft updates, synchronous useLayoutEffect external-edit & blocked guards, useFocusEffect blur cleanup, and context/client/unmount cleanup.
    • mobile/src/ui/ChatComposer.promptOptimize.test.ts: composer wiring tests for button visibility, mid-turn queueing, voice/text edit cancellation, inline status/restore/notice/error states, and disabled states.
    • src/server/__tests__/e2e/prompt-optimize.test.ts: regression assertion confirming DELETE /api/sessions/:id/unknown-subresource/:uuid returns 404 and preserves the session.
    • mobile/scripts/fixture-server.ts: added GET probe, POST optimize, and DELETE cancel assertions to --self-test.
  • Coverage evidence:
    • bun run check:coverage passed (5/5 suites passed, 0 failed).
  • E2E / live-model evidence:
    • Deterministic mock/fixture checks passed: bun run check:mobile (typecheck, lint, unit/component/hook tests, H5 export), bun run check:policy, bun run check:server, bun run check:chat-contract, bun run check:agent-flow, bun run check:agent-e2e (34 mock + 21 replay scenarios), bun test src/server/__tests__/e2e/prompt-optimize.test.ts, and bun mobile/scripts/fixture-server.ts --self-test.
    • Live-model smoke & physical iOS/Android device interaction: not run (no live provider quota used).
  • Known risk / rollback:
    • Mobile /new screen creates sessions lazily on send and therefore omits sessionId, so prompt optimization is available inside existing sessions (/chat/[id]).
    • Safe rollback via standard git revert of the squash commit; no persisted storage schema changes or migrations.

Verification

  • I ran the relevant local checks, or explained why they do not apply.
  • I added or updated same-area tests for every production behavior change.
  • I ran the checks selected by bun run check:impact; if I claim PR-ready/full validation, I also ran bun run verify.
  • New or changed executable production lines meet the changed-line coverage threshold, or the blocker/maintainer override is documented.
  • I attached or summarized the quality report path, JUnit/log artifact path, and pass/fail/skip counts.
  • I ran deterministic E2E/contract checks for cross-boundary changes. Live-model evidence is attached when a trusted maintainer ran it, otherwise it is explicitly marked not run.

Risk

  • This PR does not touch CLI core paths, is authored by a CODEOWNER, or has maintainer approval for allow-cli-core-change.
  • Production code changes include matching tests, or have maintainer approval for allow-missing-tests.
  • Coverage baseline/threshold changes have maintainer approval for allow-coverage-baseline-change.
  • Any quarantine-policy change has maintainer approval; deterministic provider/chat contract tests were not quarantined.
  • Provider/runtime changes are covered by offline mock/fixture contract tests; live smoke was run by a trusted maintainer or explicitly deferred.

Adds input-side context-aware prompt optimization for mobile (iOS, Android, and H5) using the existing /api/sessions/:id/optimize-prompt endpoint, with inline restore/cancel/error feedback, focus/edit/session-switch cancellation, legacy pre-v0.8.0 safe cancel probing, and a server-side 404 guard for unknown session sub-resources.

Closes #2025
@github-actions

Copy link
Copy Markdown

PR quality triage

Changed areas: area:server

CLI core policy: No CLI-core policy block detected.

Missing-test policy: No missing-test policy block detected.

Coverage baseline policy: No coverage-baseline policy block detected.

CLI core files:

  • none

Coverage policy files:

  • none

Required check plan:

  • See PR Quality / scope-plan; it is the single source of truth for selected jobs.

Test coverage signals:

  • No obvious missing-test signal from changed paths.

Risk notes:

  • No special risk notes from changed paths.

Hard merge gates come from the deterministic GitHub Actions contract lanes above.

@NanmiCoder
NanmiCoder merged commit 5ede0b0 into main Nov 26, 2026
19 checks passed
@NanmiCoder
NanmiCoder deleted the feat/mobile-prompt-optimize branch November 26, 2026 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[功能] 用户输入侧提示词优化

1 participant