diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index d8415e9a..1e94bb11 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -45,6 +45,7 @@ remembers doctrine; the branch remembers the blade. - Codex account auth route handling remains in vendored Codex but now lives in `codex_message_processor/auth_routes.rs`: API-key login, ChatGPT browser/device-code/external-token login, logout, auth status, account, rate limits, and add-credits nudge handling. This preserves Codex ownership of subscription auth while cutting the root processor to about 8,094 lines. - One-off command execution and command stream write/resize/terminate route handling now live in `codex_message_processor/command_routes.rs`; `CommandExecManager` remains the lifecycle owner, while the root processor is about 7,800 lines. - Git diff-to-origin, fuzzy file search/session routes, feedback upload, and Windows sandbox setup now live in `codex_message_processor/utility_routes.rs`; the root processor is about 7,397 lines. +- Realtime conversation routes now live in `codex_message_processor/realtime_routes.rs`, and review-start orchestration now lives in `codex_message_processor/review_routes.rs`; the root processor is about 6,938 lines. - The May 2026 foundation migration in `notes/epiphany-ideal-architecture-rebuild-plan.md` is complete enough to defend. Read `notes/epiphany-architectural-teardown.md` before touching Epiphany control-plane code, but do not treat it as an open-ended excuse to keep shaving the altar. The rebuild moved scene projection, freshness derivation, pressure policy, targeted context shards, bounded graph traversal, job/progress view derivation, planning view derivation, reorientation resume/regather verdict policy, pure CRRC recommendation policy, fixed-lane coordinator decision policy, role-board projection policy, role/reorient result interpretation, role self-persistence review policy, and role/reorient acceptance bundle policy into `epiphany-core`; typed acceptance receipts replaced summary-string identity for live accept paths, runtime links are dual-written on launch, and result read-back now prefers runtime links. - Read `notes/epiphany-ideal-architecture-rebuild-plan.md` immediately after the teardown. It defines the smallest coherent replacement architecture: durable `EpiphanyState`, runtime-spine-owned `RuntimeState`, separate role memory/heartbeat state, `epiphany-core` as policy owner, app-server as adapter, Aquarium as reflector, typed acceptance receipts, and derived view lenses instead of a protocol verb zoo. - Do not continue Aquarium UI, bridge, Face, or dogfood expansion until the teardown has a source-grounded cleanup slice plan. Epiphany is the foundation; patches on patches are not a purification rite, they are how the altar becomes load-bearing garbage. diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index e51256b2..f8c62512 100644 Binary files a/state/ledgers.msgpack and b/state/ledgers.msgpack differ diff --git a/state/map.yaml b/state/map.yaml index e4365012..ef07f7ff 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -286,9 +286,10 @@ rejected_paths: current_status: summary: The Codex starvation pass has five typing receipts, fifty-two processor/foundation cuts, a real physical extraction in `epiphany-codex-bridge`, a root `epiphany-state-model` crate for durable Epiphany state, a source-grounded Codex auth spine inventory, the native `epiphany-openai-adapter` document boundary, a buildable Codex auth/model wrapper in `epiphany-openai-codex-spine`, the first native runtime caller in `epiphany-openai-runtime`, native heartbeat launch planning/job opening in `epiphany-core`, native state-update contract/validation/application in `epiphany-core`, native exact retrieval path matching in `epiphany-core`, and the route-facing mutation/view/coordinator projection services in `epiphany-codex-bridge`. `codex-protocol` no longer defines the durable `Epiphany*` state structs; it re-exports the native model for legacy rollout/API compatibility while `epiphany-core` imports the native model directly. `epiphany-core` now has no direct vendored Codex dependency: pressure derivation accepts native `EpiphanyTokenUsageSnapshot`, Codex rollout replay moved to `codex-core::epiphany_rollout`, and exact retrieval search uses Epiphany-owned workspace walking/path scoring instead of `codex-file-search`. The bridge owns Codex-facing Epiphany adapters for scene, jobs, context/planning/graph query, retrieval/indexing projection, result projection, runtime-spine result adaptation, launch/prompt doctrine, mutation document mechanics, mutation service update/promote/role-accept/reorient-accept/role-launch/reorient-launch/job-launch/job-interrupt application, live-state projection, pressure/pre-compaction rendering, freshness/reorientation projection, CRRC/coordinator/role-board projection, watcher invalidation adaptation, full `thread/epiphany/view` response construction, freshness/context/graph-query response construction, role/reorient result response construction, distill/propose patch-response construction, coordinator status/view construction, and acceptance/evidence signal helpers. Vendored app-server no longer retains root Epiphany modules; `codex_message_processor.rs` still contains route dispatch, JSON-RPC response shaping, watcher registration/snapshotting calls, and notification emission, but its root no longer imports the evacuated Epiphany policy/protocol registry or owns the watcher invalidation manager. Codex product surfaces for apps, skills, plugins, marketplace, and external agent config import are cut from the live app-server/protocol path: list routes return empty lists only where compatibility shells still exist, mutation/detail routes report disabled where they survive, `externalAgentConfig/*` routes and import-completed notification are gone, helper/import modules and stale implementation-shape tests were deleted, runtime feature enablement no longer supports `apps` or `plugins`, config writes no longer emit plugin-toggle analytics, plugin/skill cache invalidation on config writes is gone, plugin startup warmups and app-list refreshes were removed, and app-server no longer directly depends on `codex-plugin`, `codex-chatgpt`, or `codex-core-plugins`. `Config::to_mcp_config` now keeps user-declared MCP servers but no longer accepts `PluginsManager`, folds plugin MCP servers into the runtime, enables Codex apps, enables skill MCP dependency install, or advertises plugin capability summaries; `TurnContext::apps_enabled()` now returns false so model turns cannot awaken Codex Apps through ChatGPT auth or feature flags; session startup and per-turn context no longer load skills, turns receive an empty `SkillLoadOutcome`, internal `Op::ListSkills` is gone from `codex-protocol` / `codex-core` / TUI command routing, the Codex skill watcher has been collapsed to a no-op marker with no file watcher, skill-root registration, cache-clear task, or `SkillsUpdateAvailable` session listener, model turns no longer run skill mention collection, skill env-var prompts, skill MCP dependency installation, skill instruction injection, or implicit skill invocation analytics from shell tools, and the turn prompt/router path no longer loads plugin/app capability summaries, injects available-skill/available-plugin/plugin instruction fragments, parses plugin/app mentions, records app/plugin invocation telemetry, or filters MCP tools through Codex app connectors. Skill mutation/list/notification is gone from the app-server edge too: `skills/list`, `skills/config/write`, `skills/changed`, `Op::ListSkills`, `EventMsg::ListSkillsResponse`, `EventMsg::SkillsUpdateAvailable`, the app-server handlers, TUI slash/startup/list/manage-skill surfaces, generated schema entries, core skill-list suite, and fake live-reload/developer-message tests were deleted. Skill mention plumbing is also gone from the live TUI/protocol edge: no `UserInput::Skill` variant, app-server schema branch, TUI skill popup, `skills_all` cache, composer `set_skill_mentions`, skill mention insertion, `skill://` / `SKILL.md` history decoding, or stale skill popup snapshot remains. The `codex-core-skills` crate, core `skills` re-export shim, no-op `skills_watcher`, session `SkillsManager` service, and per-turn `TurnSkillsContext` are deleted; core/protocol no longer keep legacy `` or `` prompt compatibility, memory usage classification, snapshot normalization, or cleanup paths. Remaining product-shaped skill residue is also cut from memory templates, app-server docs/tests/warnings, TUI tooltips/placeholders/warnings/comments, app-server v1 docs, agent-tool schema text, stale `UserInput::Skill` test arms, and obsolete feature flags/schema entries. App-instruction prompt compatibility is cut too: no `APPS_INSTRUCTIONS` protocol constants, no `` snapshot canonicalization, and MCP fixture tests now use neutral `demo_mcp` / `calendar_mcp` names instead of `codex_apps` / `/api/codex/apps` branding. The TUI app connector product path is also deleted: no `/apps` slash command, app-link view, connector AppEvent variants, app connector cache/popup/refresh loop, app mention insertion, `app://` mention codec support, stale app-popup tests, or TUI browser-opening shim remains. The `app/list` route and `app/list/updated` notification are gone from app-server protocol, generated schemas, app-server dispatch, test helpers, and docs; `AppInfo`/app-list metadata schema structs are deleted; the orphaned `codex-connectors` crate and `core::connectors` shim are deleted from the workspace; Codex app analytics no longer tracks `codex_app_mentioned` / `codex_app_used` facts, events, reducer arms, client APIs, or dedupe cache state; `[apps.*]`, `Feature::Apps`, `include_apps_instructions`, app config/read schemas, app managed requirements, app prompt tests, and app file-MCP tests are gone; `Feature::Plugins`, `Feature::RemotePlugin`, `plugin://` structured mentions, plugin instruction tags, and stale plugin popup snapshots are gone too; `[plugins]` / `[marketplaces]`, `PluginConfig`, `MarketplaceConfig`, marketplace edit helpers, stale plugin/external-agent README route claims, and generated schema entries are removed from config; and the bundled `codex-skills` sample-skill crate plus embedded imagegen/openai-docs/plugin-creator/skill-creator/skill-installer payloads are deleted from the workspace. The vendored `vendor/codex/sdk` subtree is deleted as non-spine app-server client/release scaffolding, including stale generated Python SDK route types for removed product surfaces; the root pnpm workspace no longer lists `sdk/typescript`. Live MCP tool approval is named `McpToolApproval`, not `AppToolApproval`, and remains under MCP server config; live structured user-input mentions are MCP-shaped only. `codex_message_processor.rs` is about 9,482 lines; root `message_processor.rs` is about 1,292 lines after the external-agent/config-product cuts; `epiphany_read_routes.rs` is about 579 lines after the full view and read-response extractions; `epiphany_mutation_routes.rs` is about 694 lines after route-load/notification helper cuts and product-surface starvation; the old vendored `epiphany_state_helpers.rs` rollout-state loader is deleted and its remaining function lives in `epiphany-codex-bridge::state`; and `codex_thread.rs` is about 1,425 lines after state-update law moved native. `epiphany-openai-adapter` defines CultCache `DatabaseEntry` request/event/receipt/auth-status documents with no Codex app-server or generic JSON dependency. `epiphany-openai-codex-spine` owns the native OpenAI model transport path: it exposes `default_codex_home` and `auth_manager`, projects `epiphany-openai-auth-spine` `AuthManager` / `CodexAuth` into typed `EpiphanyOpenAiAdapterStatus`, builds a local serializable OpenAI Responses request body instead of Codex protocol request/item/config types, opens HTTP Responses streams through `codex-client`, parses Responses SSE frames into typed stream events and receipts, and no longer directly or transitively depends on Codex's Responses client, model-provider stack, `codex-protocol`, `codex-api`, `codex-model-provider`, or `codex-model-provider-info`; it intentionally reaches retained vendored `codex-login` only through `epiphany-openai-auth-spine`. Auth identity, credential storage, and ChatGPT token refresh remain owned by the Codex-compatible auth organ; external bearer command auth and agent identity remain retained Codex auth capabilities to expose only if a typed Epiphany surface needs them. `epiphany-openai-runtime` no longer depends directly on `codex-login`; it writes OpenAI status/request/stream-event/receipt records plus runtime sessions/jobs/results/events into `epiphany-core::runtime_spine_cache`, can emit the assistant text artifact for legacy result consumers, is now the default executor for startup-only repo birth specialists, and now has `run-worker` to load a durable `EpiphanyRuntimeWorkerLaunchRequest`, call the typed OpenAI runtime through the spine, persist typed `EpiphanyRuntimeRoleWorkerResult` / `EpiphanyRuntimeReorientWorkerResult` documents, and complete the original heartbeat/specialist runtime job without Codex worker execution; `codex exec` survives only as explicit fallback in repo-birth startup. `epiphany-mvp-coordinator` now invokes `epiphany-openai-runtime run-worker` for launched runtime job ids and no longer creates a second coordinator-owned shadow runtime job/result as a compensator. `epiphany-core::plan_runtime_spine_heartbeat_launch` validates launch contracts/conflicts and projects durable job bindings/runtime links; `epiphany-core::open_runtime_spine_heartbeat_job` owns the initialize-runtime/ensure-session/create-job sequence and now persists `EpiphanyRuntimeWorkerLaunchRequest` CultCache records keyed by runtime job id, with indexed kind/contract fields plus MessagePack-encoded typed launch documents instead of discarding work intent after queue receipt creation; `roleResult` and `reorientResult` now require typed worker result documents; generic job summaries are lifecycle receipts, not reviewable findings; `epiphany-core::EpiphanyStateUpdate`, `epiphany_state_update_validation_errors`, and `apply_epiphany_state_update` own state-update document shape and mutation law while `codex-core` calls them for compatibility. Vendored `CodexThread::epiphany_launch_job` is now mostly revision check, persistence validation, launch request handoff, and rollout/session writeback for the compatibility route. Vendored `codex-core` no longer has `epiphany_distillation`, `epiphany_promotion`, `epiphany_proposal`, or `epiphany_retrieval` husk modules; it re-exports native surfaces from `epiphany-core` directly, and now owns only the real Codex rollout adapter because it is a host seam. `epiphany-runtime-spine` advertises OpenAI adapter status/model-request/stream-event/receipt CultNet schemas and mutation contracts, so the native caller now has a real storage route instead of only JSON-RPC. MCP itself is correctly JSON at its protocol edge; the target is typed Epiphany intent/result/receipt wrappers around MCP, not replacing MCP JSON. The current OpenAI request path no longer pulls `codex-api`; `codex-login` remains deliberately sealed behind `epiphany-openai-auth-spine`, and the next spine decision is another Epiphany-in-vendor evacuation surface unless a concrete typed auth-status/runtime need requires exposing external bearer or agent-identity auth. latest_cut: > - Utility route handling moved out of root `codex_message_processor.rs` into - `codex_message_processor/utility_routes.rs`. Git diff-to-origin, fuzzy - file search/session routes, feedback upload, and Windows sandbox setup are - now split from thread/auth/MCP/Epiphany processing; the root processor is - about 7,397 lines after the cut. + Realtime and review route handling moved out of root + `codex_message_processor.rs` into + `codex_message_processor/realtime_routes.rs` and + `codex_message_processor/review_routes.rs`. Voice transport and + review-turn orchestration are no longer inline with thread/auth/MCP/Epiphany + processing; the root processor is about 6,938 lines after the cut. next_action: Continue the Codex starvation campaign at the next Epiphany-in-vendor evacuation surface; keep vendored Codex code as host routing/lifecycle glue only, and do not cut the retained `codex-login` auth organ unless replacing it with an explicitly permitted OpenAI path rather than a lookalike. diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs index 63801878..14f35ba2 100644 --- a/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs @@ -4,6 +4,8 @@ mod epiphany_automation; mod epiphany_mutation_routes; mod epiphany_read_routes; mod mcp_routes; +mod realtime_routes; +mod review_routes; use self::auth_routes::ActiveLogin; pub(crate) use self::epiphany_automation::maybe_run_epiphany_coordinator_automation_for_turn_boundary; @@ -5027,467 +5029,6 @@ impl CodexMessageProcessor { } } - async fn prepare_realtime_conversation_thread( - &self, - request_id: ConnectionRequestId, - thread_id: &str, - ) -> Option<(ThreadId, Arc)> { - let (thread_id, thread) = match self.load_thread(thread_id).await { - Ok(v) => v, - Err(error) => { - self.outgoing.send_error(request_id, error).await; - return None; - } - }; - - match self - .ensure_conversation_listener( - thread_id, - request_id.connection_id, - /*raw_events_enabled*/ false, - ApiVersion::V2, - ) - .await - { - Ok(EnsureConversationListenerResult::Attached) => {} - Ok(EnsureConversationListenerResult::ConnectionClosed) => { - return None; - } - Err(error) => { - self.outgoing.send_error(request_id, error).await; - return None; - } - } - - if !thread.enabled(Feature::RealtimeConversation) { - self.send_invalid_request_error( - request_id, - format!("thread {thread_id} does not support realtime conversation"), - ) - .await; - return None; - } - - Some((thread_id, thread)) - } - - async fn thread_realtime_start( - &self, - request_id: ConnectionRequestId, - params: ThreadRealtimeStartParams, - ) { - let Some((_, thread)) = self - .prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id) - .await - else { - return; - }; - - let submit = self - .submit_core_op( - &request_id, - thread.as_ref(), - Op::RealtimeConversationStart(ConversationStartParams { - output_modality: params.output_modality, - prompt: params.prompt, - session_id: params.session_id, - transport: params.transport.map(|transport| match transport { - ThreadRealtimeStartTransport::Websocket => { - ConversationStartTransport::Websocket - } - ThreadRealtimeStartTransport::Webrtc { sdp } => { - ConversationStartTransport::Webrtc { sdp } - } - }), - voice: params.voice, - }), - ) - .await; - - match submit { - Ok(_) => { - self.outgoing - .send_response(request_id, ThreadRealtimeStartResponse::default()) - .await; - } - Err(err) => { - self.send_internal_error( - request_id, - format!("failed to start realtime conversation: {err}"), - ) - .await; - } - } - } - - async fn thread_realtime_append_audio( - &self, - request_id: ConnectionRequestId, - params: ThreadRealtimeAppendAudioParams, - ) { - let Some((_, thread)) = self - .prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id) - .await - else { - return; - }; - - let submit = self - .submit_core_op( - &request_id, - thread.as_ref(), - Op::RealtimeConversationAudio(ConversationAudioParams { - frame: params.audio.into(), - }), - ) - .await; - - match submit { - Ok(_) => { - self.outgoing - .send_response(request_id, ThreadRealtimeAppendAudioResponse::default()) - .await; - } - Err(err) => { - self.send_internal_error( - request_id, - format!("failed to append realtime conversation audio: {err}"), - ) - .await; - } - } - } - - async fn thread_realtime_append_text( - &self, - request_id: ConnectionRequestId, - params: ThreadRealtimeAppendTextParams, - ) { - let Some((_, thread)) = self - .prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id) - .await - else { - return; - }; - - let submit = self - .submit_core_op( - &request_id, - thread.as_ref(), - Op::RealtimeConversationText(ConversationTextParams { text: params.text }), - ) - .await; - - match submit { - Ok(_) => { - self.outgoing - .send_response(request_id, ThreadRealtimeAppendTextResponse::default()) - .await; - } - Err(err) => { - self.send_internal_error( - request_id, - format!("failed to append realtime conversation text: {err}"), - ) - .await; - } - } - } - - async fn thread_realtime_stop( - &self, - request_id: ConnectionRequestId, - params: ThreadRealtimeStopParams, - ) { - let Some((_, thread)) = self - .prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id) - .await - else { - return; - }; - - let submit = self - .submit_core_op(&request_id, thread.as_ref(), Op::RealtimeConversationClose) - .await; - - match submit { - Ok(_) => { - self.outgoing - .send_response(request_id, ThreadRealtimeStopResponse::default()) - .await; - } - Err(err) => { - self.send_internal_error( - request_id, - format!("failed to stop realtime conversation: {err}"), - ) - .await; - } - } - } - - async fn thread_realtime_list_voices( - &self, - request_id: ConnectionRequestId, - _params: ThreadRealtimeListVoicesParams, - ) { - self.outgoing - .send_response( - request_id, - ThreadRealtimeListVoicesResponse { - voices: RealtimeVoicesList::builtin(), - }, - ) - .await; - } - - fn build_review_turn(turn_id: String, display_text: &str) -> Turn { - let items = if display_text.is_empty() { - Vec::new() - } else { - vec![ThreadItem::UserMessage { - id: turn_id.clone(), - content: vec![V2UserInput::Text { - text: display_text.to_string(), - // Review prompt display text is synthesized; no UI element ranges to preserve. - text_elements: Vec::new(), - }], - }] - }; - - Turn { - id: turn_id, - items, - error: None, - status: TurnStatus::InProgress, - started_at: None, - completed_at: None, - duration_ms: None, - } - } - - async fn emit_review_started( - &self, - request_id: &ConnectionRequestId, - turn: Turn, - review_thread_id: String, - ) { - let response = ReviewStartResponse { - turn, - review_thread_id, - }; - self.outgoing - .send_response(request_id.clone(), response) - .await; - } - - async fn start_inline_review( - &self, - request_id: &ConnectionRequestId, - parent_thread: Arc, - review_request: ReviewRequest, - display_text: &str, - parent_thread_id: String, - ) -> std::result::Result<(), JSONRPCErrorError> { - let turn_id = self - .submit_core_op( - request_id, - parent_thread.as_ref(), - Op::Review { review_request }, - ) - .await; - - match turn_id { - Ok(turn_id) => { - let turn = Self::build_review_turn(turn_id, display_text); - self.emit_review_started(request_id, turn, parent_thread_id) - .await; - Ok(()) - } - Err(err) => Err(JSONRPCErrorError { - code: INTERNAL_ERROR_CODE, - message: format!("failed to start review: {err}"), - data: None, - }), - } - } - - async fn start_detached_review( - &self, - request_id: &ConnectionRequestId, - parent_thread_id: ThreadId, - parent_thread: Arc, - review_request: ReviewRequest, - display_text: &str, - ) -> std::result::Result<(), JSONRPCErrorError> { - let rollout_path = if let Some(path) = parent_thread.rollout_path() { - path - } else { - find_thread_path_by_id_str(&self.config.codex_home, &parent_thread_id.to_string()) - .await - .map_err(|err| JSONRPCErrorError { - code: INTERNAL_ERROR_CODE, - message: format!("failed to locate thread id {parent_thread_id}: {err}"), - data: None, - })? - .ok_or_else(|| JSONRPCErrorError { - code: INVALID_REQUEST_ERROR_CODE, - message: format!("no rollout found for thread id {parent_thread_id}"), - data: None, - })? - }; - - let mut config = self.config.as_ref().clone(); - if let Some(review_model) = &config.review_model { - config.model = Some(review_model.clone()); - } - - let NewThread { - thread_id, - thread: review_thread, - session_configured, - .. - } = self - .thread_manager - .fork_thread( - ForkSnapshot::Interrupted, - config, - rollout_path, - /*persist_extended_history*/ false, - self.request_trace_context(request_id).await, - ) - .await - .map_err(|err| JSONRPCErrorError { - code: INTERNAL_ERROR_CODE, - message: format!("error creating detached review thread: {err}"), - data: None, - })?; - - Self::log_listener_attach_result( - self.ensure_conversation_listener( - thread_id, - request_id.connection_id, - /*raw_events_enabled*/ false, - ApiVersion::V2, - ) - .await, - thread_id, - request_id.connection_id, - "review thread", - ); - - let fallback_provider = self.config.model_provider_id.as_str(); - if let Some(rollout_path) = review_thread.rollout_path() { - match read_summary_from_rollout(rollout_path.as_path(), fallback_provider).await { - Ok(summary) => { - let mut thread = summary_to_thread(summary, &self.config.cwd); - thread.epiphany_state = - live_thread_epiphany_state(review_thread.as_ref()).await; - self.thread_watch_manager - .upsert_thread_silently(thread.clone()) - .await; - thread.status = resolve_thread_status( - self.thread_watch_manager - .loaded_status_for_thread(&thread.id) - .await, - /*has_in_progress_turn*/ false, - ); - let notif = ThreadStartedNotification { thread }; - self.outgoing - .send_server_notification(ServerNotification::ThreadStarted(notif)) - .await; - } - Err(err) => { - tracing::warn!( - "failed to load summary for review thread {}: {}", - session_configured.session_id, - err - ); - } - } - } else { - tracing::warn!( - "review thread {} has no rollout path", - session_configured.session_id - ); - } - - let turn_id = self - .submit_core_op( - request_id, - review_thread.as_ref(), - Op::Review { review_request }, - ) - .await - .map_err(|err| JSONRPCErrorError { - code: INTERNAL_ERROR_CODE, - message: format!("failed to start detached review turn: {err}"), - data: None, - })?; - - let turn = Self::build_review_turn(turn_id, display_text); - let review_thread_id = thread_id.to_string(); - self.emit_review_started(request_id, turn, review_thread_id) - .await; - - Ok(()) - } - - async fn review_start(&self, request_id: ConnectionRequestId, params: ReviewStartParams) { - let ReviewStartParams { - thread_id, - target, - delivery, - } = params; - let (parent_thread_id, parent_thread) = match self.load_thread(&thread_id).await { - Ok(v) => v, - Err(error) => { - self.outgoing.send_error(request_id, error).await; - return; - } - }; - - let (review_request, display_text) = match Self::review_request_from_target(target) { - Ok(value) => value, - Err(err) => { - self.outgoing.send_error(request_id, err).await; - return; - } - }; - - let delivery = delivery.unwrap_or(ApiReviewDelivery::Inline).to_core(); - match delivery { - CoreReviewDelivery::Inline => { - if let Err(err) = self - .start_inline_review( - &request_id, - parent_thread, - review_request, - display_text.as_str(), - thread_id.clone(), - ) - .await - { - self.outgoing.send_error(request_id, err).await; - } - } - CoreReviewDelivery::Detached => { - if let Err(err) = self - .start_detached_review( - &request_id, - parent_thread_id, - parent_thread, - review_request, - display_text.as_str(), - ) - .await - { - self.outgoing.send_error(request_id, err).await; - } - } - } - } - async fn turn_interrupt(&self, request_id: ConnectionRequestId, params: TurnInterruptParams) { let TurnInterruptParams { thread_id, turn_id } = params; let is_startup_interrupt = turn_id.is_empty(); diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor/realtime_routes.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor/realtime_routes.rs new file mode 100644 index 00000000..c6fb5360 --- /dev/null +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor/realtime_routes.rs @@ -0,0 +1,217 @@ +use super::*; + +impl CodexMessageProcessor { + async fn prepare_realtime_conversation_thread( + &self, + request_id: ConnectionRequestId, + thread_id: &str, + ) -> Option<(ThreadId, Arc)> { + let (thread_id, thread) = match self.load_thread(thread_id).await { + Ok(v) => v, + Err(error) => { + self.outgoing.send_error(request_id, error).await; + return None; + } + }; + + match self + .ensure_conversation_listener( + thread_id, + request_id.connection_id, + /*raw_events_enabled*/ false, + ApiVersion::V2, + ) + .await + { + Ok(EnsureConversationListenerResult::Attached) => {} + Ok(EnsureConversationListenerResult::ConnectionClosed) => { + return None; + } + Err(error) => { + self.outgoing.send_error(request_id, error).await; + return None; + } + } + + if !thread.enabled(Feature::RealtimeConversation) { + self.send_invalid_request_error( + request_id, + format!("thread {thread_id} does not support realtime conversation"), + ) + .await; + return None; + } + + Some((thread_id, thread)) + } + + pub(super) async fn thread_realtime_start( + &self, + request_id: ConnectionRequestId, + params: ThreadRealtimeStartParams, + ) { + let Some((_, thread)) = self + .prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id) + .await + else { + return; + }; + + let submit = self + .submit_core_op( + &request_id, + thread.as_ref(), + Op::RealtimeConversationStart(ConversationStartParams { + output_modality: params.output_modality, + prompt: params.prompt, + session_id: params.session_id, + transport: params.transport.map(|transport| match transport { + ThreadRealtimeStartTransport::Websocket => { + ConversationStartTransport::Websocket + } + ThreadRealtimeStartTransport::Webrtc { sdp } => { + ConversationStartTransport::Webrtc { sdp } + } + }), + voice: params.voice, + }), + ) + .await; + + match submit { + Ok(_) => { + self.outgoing + .send_response(request_id, ThreadRealtimeStartResponse::default()) + .await; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to start realtime conversation: {err}"), + ) + .await; + } + } + } + + pub(super) async fn thread_realtime_append_audio( + &self, + request_id: ConnectionRequestId, + params: ThreadRealtimeAppendAudioParams, + ) { + let Some((_, thread)) = self + .prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id) + .await + else { + return; + }; + + let submit = self + .submit_core_op( + &request_id, + thread.as_ref(), + Op::RealtimeConversationAudio(ConversationAudioParams { + frame: params.audio.into(), + }), + ) + .await; + + match submit { + Ok(_) => { + self.outgoing + .send_response(request_id, ThreadRealtimeAppendAudioResponse::default()) + .await; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to append realtime conversation audio: {err}"), + ) + .await; + } + } + } + + pub(super) async fn thread_realtime_append_text( + &self, + request_id: ConnectionRequestId, + params: ThreadRealtimeAppendTextParams, + ) { + let Some((_, thread)) = self + .prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id) + .await + else { + return; + }; + + let submit = self + .submit_core_op( + &request_id, + thread.as_ref(), + Op::RealtimeConversationText(ConversationTextParams { text: params.text }), + ) + .await; + + match submit { + Ok(_) => { + self.outgoing + .send_response(request_id, ThreadRealtimeAppendTextResponse::default()) + .await; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to append realtime conversation text: {err}"), + ) + .await; + } + } + } + + pub(super) async fn thread_realtime_stop( + &self, + request_id: ConnectionRequestId, + params: ThreadRealtimeStopParams, + ) { + let Some((_, thread)) = self + .prepare_realtime_conversation_thread(request_id.clone(), ¶ms.thread_id) + .await + else { + return; + }; + + let submit = self + .submit_core_op(&request_id, thread.as_ref(), Op::RealtimeConversationClose) + .await; + + match submit { + Ok(_) => { + self.outgoing + .send_response(request_id, ThreadRealtimeStopResponse::default()) + .await; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to stop realtime conversation: {err}"), + ) + .await; + } + } + } + + pub(super) async fn thread_realtime_list_voices( + &self, + request_id: ConnectionRequestId, + _params: ThreadRealtimeListVoicesParams, + ) { + self.outgoing + .send_response( + request_id, + ThreadRealtimeListVoicesResponse { + voices: RealtimeVoicesList::builtin(), + }, + ) + .await; + } +} diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor/review_routes.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor/review_routes.rs new file mode 100644 index 00000000..cc29a360 --- /dev/null +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor/review_routes.rs @@ -0,0 +1,254 @@ +use super::*; + +impl CodexMessageProcessor { + fn build_review_turn(turn_id: String, display_text: &str) -> Turn { + let items = if display_text.is_empty() { + Vec::new() + } else { + vec![ThreadItem::UserMessage { + id: turn_id.clone(), + content: vec![V2UserInput::Text { + text: display_text.to_string(), + // Review prompt display text is synthesized; no UI element ranges to preserve. + text_elements: Vec::new(), + }], + }] + }; + + Turn { + id: turn_id, + items, + error: None, + status: TurnStatus::InProgress, + started_at: None, + completed_at: None, + duration_ms: None, + } + } + + async fn emit_review_started( + &self, + request_id: &ConnectionRequestId, + turn: Turn, + review_thread_id: String, + ) { + let response = ReviewStartResponse { + turn, + review_thread_id, + }; + self.outgoing + .send_response(request_id.clone(), response) + .await; + } + + async fn start_inline_review( + &self, + request_id: &ConnectionRequestId, + parent_thread: Arc, + review_request: ReviewRequest, + display_text: &str, + parent_thread_id: String, + ) -> std::result::Result<(), JSONRPCErrorError> { + let turn_id = self + .submit_core_op( + request_id, + parent_thread.as_ref(), + Op::Review { review_request }, + ) + .await; + + match turn_id { + Ok(turn_id) => { + let turn = Self::build_review_turn(turn_id, display_text); + self.emit_review_started(request_id, turn, parent_thread_id) + .await; + Ok(()) + } + Err(err) => Err(JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to start review: {err}"), + data: None, + }), + } + } + + async fn start_detached_review( + &self, + request_id: &ConnectionRequestId, + parent_thread_id: ThreadId, + parent_thread: Arc, + review_request: ReviewRequest, + display_text: &str, + ) -> std::result::Result<(), JSONRPCErrorError> { + let rollout_path = if let Some(path) = parent_thread.rollout_path() { + path + } else { + find_thread_path_by_id_str(&self.config.codex_home, &parent_thread_id.to_string()) + .await + .map_err(|err| JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to locate thread id {parent_thread_id}: {err}"), + data: None, + })? + .ok_or_else(|| JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("no rollout found for thread id {parent_thread_id}"), + data: None, + })? + }; + + let mut config = self.config.as_ref().clone(); + if let Some(review_model) = &config.review_model { + config.model = Some(review_model.clone()); + } + + let NewThread { + thread_id, + thread: review_thread, + session_configured, + .. + } = self + .thread_manager + .fork_thread( + ForkSnapshot::Interrupted, + config, + rollout_path, + /*persist_extended_history*/ false, + self.request_trace_context(request_id).await, + ) + .await + .map_err(|err| JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("error creating detached review thread: {err}"), + data: None, + })?; + + Self::log_listener_attach_result( + self.ensure_conversation_listener( + thread_id, + request_id.connection_id, + /*raw_events_enabled*/ false, + ApiVersion::V2, + ) + .await, + thread_id, + request_id.connection_id, + "review thread", + ); + + let fallback_provider = self.config.model_provider_id.as_str(); + if let Some(rollout_path) = review_thread.rollout_path() { + match read_summary_from_rollout(rollout_path.as_path(), fallback_provider).await { + Ok(summary) => { + let mut thread = summary_to_thread(summary, &self.config.cwd); + thread.epiphany_state = + live_thread_epiphany_state(review_thread.as_ref()).await; + self.thread_watch_manager + .upsert_thread_silently(thread.clone()) + .await; + thread.status = resolve_thread_status( + self.thread_watch_manager + .loaded_status_for_thread(&thread.id) + .await, + /*has_in_progress_turn*/ false, + ); + let notif = ThreadStartedNotification { thread }; + self.outgoing + .send_server_notification(ServerNotification::ThreadStarted(notif)) + .await; + } + Err(err) => { + tracing::warn!( + "failed to load summary for review thread {}: {}", + session_configured.session_id, + err + ); + } + } + } else { + tracing::warn!( + "review thread {} has no rollout path", + session_configured.session_id + ); + } + + let turn_id = self + .submit_core_op( + request_id, + review_thread.as_ref(), + Op::Review { review_request }, + ) + .await + .map_err(|err| JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to start detached review turn: {err}"), + data: None, + })?; + + let turn = Self::build_review_turn(turn_id, display_text); + let review_thread_id = thread_id.to_string(); + self.emit_review_started(request_id, turn, review_thread_id) + .await; + + Ok(()) + } + + pub(super) async fn review_start( + &self, + request_id: ConnectionRequestId, + params: ReviewStartParams, + ) { + let ReviewStartParams { + thread_id, + target, + delivery, + } = params; + let (parent_thread_id, parent_thread) = match self.load_thread(&thread_id).await { + Ok(v) => v, + Err(error) => { + self.outgoing.send_error(request_id, error).await; + return; + } + }; + + let (review_request, display_text) = match Self::review_request_from_target(target) { + Ok(value) => value, + Err(err) => { + self.outgoing.send_error(request_id, err).await; + return; + } + }; + + let delivery = delivery.unwrap_or(ApiReviewDelivery::Inline).to_core(); + match delivery { + CoreReviewDelivery::Inline => { + if let Err(err) = self + .start_inline_review( + &request_id, + parent_thread, + review_request, + display_text.as_str(), + thread_id.clone(), + ) + .await + { + self.outgoing.send_error(request_id, err).await; + } + } + CoreReviewDelivery::Detached => { + if let Err(err) = self + .start_detached_review( + &request_id, + parent_thread_id, + parent_thread, + review_request, + display_text.as_str(), + ) + .await + { + self.outgoing.send_error(request_id, err).await; + } + } + } + } +}