mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Refactor history and notes tool registration (#61747)
## What changed Split tool construction into `history_tools` and `notes_tools`, with `history_notes_tools` combining both. Each builder accepts an optional backend and reads its catalog messages directly from the step's `ExtensionData`. Extract a shared contributor registration helper and simplify the HTTP-backed extension to use these helpers. Update the bundled tool contract snapshot test to use the revised builder signature. GitOrigin-RevId: c9804b457ffc5d3634d7e58973a9f0f5028ca6e9
This commit is contained in:
committed by
copyberry
parent
5a6bbd29a4
commit
710bee73a8
@@ -1,3 +1,5 @@
|
||||
//! Installs the HTTP-backed history and notes contributor.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_analytics::AnalyticsEventsClient;
|
||||
@@ -14,18 +16,15 @@ use codex_extension_api::PromptFragment;
|
||||
use codex_extension_api::PromptSlot;
|
||||
use codex_extension_api::ThreadLifecycleContributor;
|
||||
use codex_extension_api::ThreadStartInput;
|
||||
use codex_extension_api::ToolCall;
|
||||
use codex_extension_api::ToolContributor;
|
||||
use codex_extension_api::ToolExecutor;
|
||||
use codex_login::AuthManager;
|
||||
use codex_protocol::AgentPath;
|
||||
use codex_protocol::openai_models::HistoryToolMessages;
|
||||
use codex_protocol::openai_models::NotesToolMessages;
|
||||
use codex_utils_output_truncation::TruncationPolicy;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::backend::HttpBackend;
|
||||
use crate::backend::HttpContext;
|
||||
use crate::tools::Executor;
|
||||
use crate::tools::history_notes_tools;
|
||||
|
||||
// Bound model context even when note paths are unusually long.
|
||||
@@ -148,53 +147,36 @@ impl ContextContributor for HistoryNotesExtension {
|
||||
}
|
||||
|
||||
impl ToolContributor for HistoryNotesExtension {
|
||||
fn tools(
|
||||
&self,
|
||||
_session_store: &ExtensionData,
|
||||
thread_store: &ExtensionData,
|
||||
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
|
||||
self.tools_with_messages(
|
||||
thread_store,
|
||||
/*history_messages*/ None,
|
||||
/*notes_messages*/ None,
|
||||
)
|
||||
fn tools(&self, _session: &ExtensionData, thread: &ExtensionData) -> Vec<Executor> {
|
||||
history_notes_tools(thread.get::<HttpBackend>().as_deref(), /*step*/ None)
|
||||
}
|
||||
|
||||
fn tools_for_step(
|
||||
&self,
|
||||
_session_store: &ExtensionData,
|
||||
thread_store: &ExtensionData,
|
||||
step_store: &ExtensionData,
|
||||
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
|
||||
let history_messages = step_store.get::<HistoryToolMessages>();
|
||||
let notes_messages = step_store.get::<NotesToolMessages>();
|
||||
self.tools_with_messages(
|
||||
thread_store,
|
||||
history_messages.as_deref(),
|
||||
notes_messages.as_deref(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl HistoryNotesExtension {
|
||||
fn tools_with_messages(
|
||||
&self,
|
||||
thread_store: &ExtensionData,
|
||||
history_messages: Option<&HistoryToolMessages>,
|
||||
notes_messages: Option<&NotesToolMessages>,
|
||||
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
|
||||
let Some(backend) = thread_store.get::<HttpBackend>() else {
|
||||
return Vec::new();
|
||||
};
|
||||
history_notes_tools((*backend).clone(), history_messages, notes_messages)
|
||||
_session: &ExtensionData,
|
||||
thread: &ExtensionData,
|
||||
step: &ExtensionData,
|
||||
) -> Vec<Executor> {
|
||||
history_notes_tools(thread.get::<HttpBackend>().as_deref(), Some(step))
|
||||
}
|
||||
}
|
||||
|
||||
/// Installs the standalone history and notes tools.
|
||||
pub fn install(registry: &mut ExtensionRegistryBuilder<Config>, auth_manager: Arc<AuthManager>) {
|
||||
let extension = Arc::new(HistoryNotesExtension { auth_manager });
|
||||
registry.thread_lifecycle_contributor(extension.clone());
|
||||
registry.config_contributor(extension.clone());
|
||||
registry.prompt_contributor(extension.clone());
|
||||
registry.tool_contributor(extension);
|
||||
register(registry, HistoryNotesExtension { auth_manager });
|
||||
}
|
||||
|
||||
pub(crate) fn register<C>(registry: &mut ExtensionRegistryBuilder<Config>, contributor: C)
|
||||
where
|
||||
C: ThreadLifecycleContributor<Config>
|
||||
+ ConfigContributor<Config>
|
||||
+ ContextContributor
|
||||
+ ToolContributor
|
||||
+ 'static,
|
||||
{
|
||||
let contributor = Arc::new(contributor);
|
||||
registry.thread_lifecycle_contributor(contributor.clone());
|
||||
registry.config_contributor(contributor.clone());
|
||||
registry.prompt_contributor(contributor.clone());
|
||||
registry.tool_contributor(contributor);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use crate::requests::NotesListFilesByPrefix;
|
||||
use crate::requests::NotesReadFile;
|
||||
use crate::requests::NotesSearchContents;
|
||||
use crate::requests::NotesWriteFile;
|
||||
use codex_extension_api::ExtensionData;
|
||||
use codex_extension_api::FunctionCallError;
|
||||
use codex_extension_api::ResponsesApiTool;
|
||||
use codex_extension_api::ToolCall;
|
||||
@@ -47,11 +48,11 @@ use serde_json::json;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
|
||||
type Executor = Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>;
|
||||
pub(crate) type Executor = Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>;
|
||||
|
||||
// Keep the catalog key, tool name, request type and HTTP route in one registration.
|
||||
macro_rules! register_tools {
|
||||
($( $catalog:ident: $messages:ty => $namespace:ident {
|
||||
($( $catalog:ident: $register:ident($messages:ty) => $namespace:ident {
|
||||
$( $name:ident: $request:ty $( [parallel = $parallel:literal] )? ),+ $(,)?
|
||||
} ),+ $(,)?) => {
|
||||
$($(impl Request for $request {
|
||||
@@ -60,33 +61,44 @@ macro_rules! register_tools {
|
||||
$(const PARALLEL: bool = $parallel;)?
|
||||
})+)+
|
||||
|
||||
$(pub(crate) fn $register<B>(backend: Option<&B>, step: Option<&ExtensionData>) -> Vec<Executor>
|
||||
where
|
||||
$(B: Handle<$request>,)+
|
||||
{
|
||||
let Some(backend) = backend else { return Vec::new() };
|
||||
let $catalog = step.and_then(ExtensionData::get::<$messages>);
|
||||
vec![$(Arc::new(HistoryNotesTool::<$request, B> {
|
||||
request: PhantomData,
|
||||
backend: backend.clone(),
|
||||
spec: tool_spec::<$request, <B as Handle<$request>>::Output>(
|
||||
$catalog.as_deref().and_then(|messages| messages.namespace_description.as_deref()),
|
||||
$catalog.as_deref().and_then(|messages| messages.$name.as_ref()),
|
||||
),
|
||||
}) as Executor,)+]
|
||||
})+
|
||||
|
||||
pub(crate) fn history_notes_tools<B>(
|
||||
backend: B,
|
||||
$($catalog: Option<&$messages>,)+
|
||||
backend: Option<&B>,
|
||||
step: Option<&ExtensionData>,
|
||||
) -> Vec<Executor>
|
||||
where
|
||||
$($(B: Handle<$request>,)+)+
|
||||
{
|
||||
vec![$($(Arc::new(HistoryNotesTool::<$request, B> {
|
||||
request: PhantomData,
|
||||
backend: backend.clone(),
|
||||
spec: tool_spec::<$request, <B as Handle<$request>>::Output>(
|
||||
$catalog.and_then(|messages| messages.namespace_description.as_deref()),
|
||||
$catalog.and_then(|messages| messages.$name.as_ref()),
|
||||
),
|
||||
}) as Executor,)+)+]
|
||||
let mut tools = Vec::new();
|
||||
$(tools.extend($register(backend, step));)+
|
||||
tools
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
register_tools! {
|
||||
history: HistoryToolMessages => HISTORY {
|
||||
history: history_tools(HistoryToolMessages) => HISTORY {
|
||||
list_windows: HistoryListWindows,
|
||||
list_items: HistoryListItems,
|
||||
read_item: HistoryReadItem,
|
||||
search_contents: HistorySearchContents,
|
||||
},
|
||||
notes: NotesToolMessages => NOTES {
|
||||
notes: notes_tools(NotesToolMessages) => NOTES {
|
||||
list_files_by_prefix: NotesListFilesByPrefix,
|
||||
read_file: NotesReadFile,
|
||||
search_contents: NotesSearchContents,
|
||||
|
||||
@@ -204,7 +204,7 @@ fn bundled_tool_contracts() {
|
||||
current_agent_name: "/root".to_owned(),
|
||||
}),
|
||||
);
|
||||
let specs: Vec<_> = history_notes_tools(backend, /*history*/ None, /*notes*/ None)
|
||||
let specs: Vec<_> = history_notes_tools(Some(&backend), /*step*/ None)
|
||||
.iter()
|
||||
.map(|tool| tool.spec())
|
||||
.collect();
|
||||
|
||||
Reference in New Issue
Block a user