Repository navigation
Enable configured prompt hooks in Codex #24634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,6 +272,7 @@ pub struct ModelClient { | |
| pub struct ModelClientSession { | ||
| client: ModelClient, | ||
| websocket_session: WebsocketSession, | ||
| cache_websocket_session_on_drop: bool, | ||
| /// Turn state for sticky routing. | ||
| /// | ||
| /// This is an `OnceLock` that stores the turn state value received from the server | ||
|
|
@@ -480,6 +481,18 @@ impl ModelClient { | |
| ModelClientSession { | ||
| client: self.clone(), | ||
| websocket_session: self.take_cached_websocket_session(), | ||
| cache_websocket_session_on_drop: true, | ||
| turn_state: Arc::new(OnceLock::new()), | ||
| } | ||
| } | ||
|
|
||
| /// Creates a fresh streaming session that does not read or write the session's cached | ||
| /// WebSocket state. | ||
| pub(crate) fn new_isolated_session(&self) -> ModelClientSession { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A normal If a hook used that path, it could replace the continuation state for the real transcript. Starting fresh here and not caching on drop keeps hook inference from disturbing the next main turn. |
||
| ModelClientSession { | ||
| client: self.clone(), | ||
| websocket_session: WebsocketSession::default(), | ||
| cache_websocket_session_on_drop: false, | ||
| turn_state: Arc::new(OnceLock::new()), | ||
| } | ||
| } | ||
|
|
@@ -1107,9 +1120,11 @@ impl ModelClient { | |
|
|
||
| impl Drop for ModelClientSession { | ||
| fn drop(&mut self) { | ||
| let websocket_session = std::mem::take(&mut self.websocket_session); | ||
| self.client | ||
| .store_cached_websocket_session(websocket_session); | ||
| if self.cache_websocket_session_on_drop { | ||
| let websocket_session = std::mem::take(&mut self.websocket_session); | ||
| self.client | ||
| .store_cached_websocket_session(websocket_session); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use codex_hooks::PromptHookRequest; | ||
| use codex_hooks::PromptHookRunner; | ||
| use codex_models_manager::ModelsManagerConfig; | ||
| use codex_models_manager::manager::SharedModelsManager; | ||
| use codex_otel::SessionTelemetry; | ||
| use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig; | ||
| use codex_protocol::models::BaseInstructions; | ||
| use codex_protocol::models::ContentItem; | ||
| use codex_protocol::models::ResponseItem; | ||
| use codex_rollout_trace::InferenceTraceContext; | ||
| use futures::StreamExt; | ||
| use serde_json::json; | ||
|
|
||
| use crate::client::ModelClient; | ||
| use crate::client_common::Prompt; | ||
| use crate::client_common::ResponseEvent; | ||
| use crate::config::Config; | ||
| use crate::responses_metadata::CodexResponsesMetadata; | ||
| use crate::stream_events_utils::raw_assistant_output_text_from_item; | ||
|
|
||
| const PROMPT_HOOK_BASE_INSTRUCTIONS: &str = r#"You evaluate a Codex prompt hook. | ||
|
|
||
| The user message contains: | ||
| 1. The hook author's instructions. | ||
| 2. The hook input JSON. | ||
|
|
||
| Decide whether the hook input satisfies the hook author's instructions. | ||
|
|
||
| Return only JSON: | ||
| {"ok": true} | ||
| or | ||
| {"ok": false, "reason": "concise actionable reason"} | ||
|
|
||
| Use ok:false only when the hook criteria fail. Do not answer the user's task. Do not include Markdown or extra text."#; | ||
|
|
||
| pub(crate) fn build_prompt_hook_runner( | ||
| model_client: ModelClient, | ||
| models_manager: SharedModelsManager, | ||
| config: &Config, | ||
| session_telemetry: SessionTelemetry, | ||
| service_tier: Option<String>, | ||
| responses_metadata: CodexResponsesMetadata, | ||
| ) -> PromptHookRunner { | ||
| let models_manager_config = config.to_models_manager_config(); | ||
| PromptHookRunner::new(move |request| { | ||
| let model_client = model_client.clone(); | ||
| let models_manager = Arc::clone(&models_manager); | ||
| let models_manager_config = models_manager_config.clone(); | ||
| let session_telemetry = session_telemetry.clone(); | ||
| let service_tier = service_tier.clone(); | ||
| let responses_metadata = responses_metadata.clone(); | ||
| async move { | ||
| run_prompt_hook( | ||
| model_client, | ||
| models_manager, | ||
| models_manager_config, | ||
| session_telemetry, | ||
| service_tier, | ||
| responses_metadata, | ||
| request, | ||
| ) | ||
| .await | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /// Run the hook as an isolated model request: resolve the configured model, | ||
| /// send the rendered hook prompt as the only user message, disable all | ||
| /// tools and reasoning summaries, and constrain the final answer to the | ||
| /// small `{ ok, reason }` contract that `codex-hooks` maps back into existing | ||
| /// hook output semantics. | ||
| async fn run_prompt_hook( | ||
| model_client: ModelClient, | ||
| models_manager: SharedModelsManager, | ||
| models_manager_config: ModelsManagerConfig, | ||
| session_telemetry: SessionTelemetry, | ||
| service_tier: Option<String>, | ||
| responses_metadata: CodexResponsesMetadata, | ||
| request: PromptHookRequest, | ||
| ) -> anyhow::Result<String> { | ||
| let model_info = models_manager | ||
| .get_model_info(request.model.as_str(), &models_manager_config) | ||
| .await; | ||
| let prompt = Prompt { | ||
| input: vec![ResponseItem::Message { | ||
| id: None, | ||
| role: "user".to_string(), | ||
| content: vec![ContentItem::InputText { | ||
| text: request.prompt, | ||
|
abhinav-oai marked this conversation as resolved.
|
||
| }], | ||
| phase: None, | ||
| internal_chat_message_metadata_passthrough: None, | ||
| }], | ||
| tools: Vec::new(), | ||
| parallel_tool_calls: false, | ||
| base_instructions: BaseInstructions { | ||
| text: PROMPT_HOOK_BASE_INSTRUCTIONS.to_string(), | ||
| }, | ||
| output_schema: Some(prompt_hook_output_schema()), | ||
| output_schema_strict: false, | ||
| }; | ||
|
|
||
| let disabled_trace = InferenceTraceContext::disabled(); | ||
| let mut client_session = model_client.new_isolated_session(); | ||
| let mut stream = client_session | ||
| .stream( | ||
| &prompt, | ||
| &model_info, | ||
| &session_telemetry, | ||
| /*effort*/ None, | ||
| ReasoningSummaryConfig::None, | ||
| service_tier, | ||
| &responses_metadata, | ||
| &disabled_trace, | ||
| ) | ||
| .await?; | ||
| let mut delta_text = String::new(); | ||
| let mut item_texts = Vec::new(); | ||
| while let Some(event) = stream.next().await { | ||
| match event? { | ||
| ResponseEvent::OutputItemDone(item) => { | ||
| if let Some(text) = raw_assistant_output_text_from_item(&item) { | ||
| item_texts.push(text); | ||
| } | ||
| } | ||
| ResponseEvent::OutputTextDelta(delta) => delta_text.push_str(delta.as_str()), | ||
| ResponseEvent::Completed { .. } => break, | ||
| ResponseEvent::Created | ||
| | ResponseEvent::SafetyBuffering(_) | ||
| | ResponseEvent::TurnModerationMetadata(_) | ||
| | ResponseEvent::ReasoningSummaryDone { .. } | ||
| | ResponseEvent::OutputItemAdded(_) | ||
| | ResponseEvent::ServerModel(_) | ||
| | ResponseEvent::ModelVerifications(_) | ||
| | ResponseEvent::ServerReasoningIncluded(_) | ||
| | ResponseEvent::ToolCallInputDelta { .. } | ||
| | ResponseEvent::ReasoningSummaryDelta { .. } | ||
| | ResponseEvent::ReasoningContentDelta { .. } | ||
| | ResponseEvent::ReasoningSummaryPartAdded { .. } | ||
| | ResponseEvent::RateLimits(_) | ||
| | ResponseEvent::ModelsEtag(_) => {} | ||
| } | ||
| } | ||
|
|
||
| if item_texts.is_empty() { | ||
| Ok(delta_text) | ||
| } else { | ||
| Ok(item_texts.join("")) | ||
| } | ||
| } | ||
|
|
||
| fn prompt_hook_output_schema() -> serde_json::Value { | ||
| json!({ | ||
| "type": "object", | ||
| "properties": { | ||
| "ok": { | ||
| "type": "boolean" | ||
| }, | ||
| "reason": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": ["ok"], | ||
| "additionalProperties": false | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.