mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
## Why The memories extension has several distinct responsibilities: registering its prompt and tool contributors, enforcing local-memory filesystem boundaries, implementing list/read/search behavior, and wrapping that backend as extension tools. Those responsibilities were concentrated in `lib.rs`, `local.rs`, and the tool modules, which made follow-up work harder to review and risked growing files through unrelated edits. This PR reorganizes the crate so each responsibility has a narrower owner while preserving the same extension entrypoint and memory tool behavior. ## What Changed - Moved extension lifecycle, prompt, and tool registration into `src/extension.rs`, leaving `src/lib.rs` as the small crate entrypoint. - Split `LocalMemoriesBackend` helpers into `local/list.rs`, `local/path.rs`, `local/read.rs`, and `local/search.rs`. - Centralized tool names and limits at the crate level, and kept the backend and extension implementation crate-private. - Made `memory_list`, `memory_read`, and `memory_search` tool executors generic over `MemoriesBackend`, so tests can exercise the full executor path without depending on tool internals. - Consolidated and expanded memory extension tests in `src/tests.rs`, including read/search tool output coverage, multi-query search, windowed `all_within_lines`, and legacy `query` rejection. ## Testing - Not run locally.
85 lines
2.8 KiB
Rust
85 lines
2.8 KiB
Rust
use std::sync::Arc;
|
|
|
|
use codex_core::config::Config;
|
|
use codex_extension_api::ContextContributor;
|
|
use codex_extension_api::ExtensionData;
|
|
use codex_extension_api::ExtensionRegistryBuilder;
|
|
use codex_extension_api::PromptFragment;
|
|
use codex_extension_api::ThreadLifecycleContributor;
|
|
use codex_extension_api::ThreadStartInput;
|
|
use codex_extension_api::ToolContributor;
|
|
use codex_features::Feature;
|
|
use codex_memories_read::build_memory_tool_developer_instructions;
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
|
|
use crate::local::LocalMemoriesBackend;
|
|
use crate::tools;
|
|
|
|
/// Contributes Codex memory read-path prompt context and memory read tools.
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub(crate) struct MemoriesExtension;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub(crate) struct MemoriesExtensionConfig {
|
|
pub(crate) enabled: bool,
|
|
pub(crate) codex_home: AbsolutePathBuf,
|
|
}
|
|
|
|
impl ContextContributor for MemoriesExtension {
|
|
fn contribute<'a>(
|
|
&'a self,
|
|
_session_store: &'a ExtensionData,
|
|
thread_store: &'a ExtensionData,
|
|
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<PromptFragment>> + Send + 'a>> {
|
|
Box::pin(async move {
|
|
let Some(config) = thread_store.get::<MemoriesExtensionConfig>() else {
|
|
return Vec::new();
|
|
};
|
|
if !config.enabled {
|
|
return Vec::new();
|
|
}
|
|
|
|
build_memory_tool_developer_instructions(&config.codex_home)
|
|
.await
|
|
.map(PromptFragment::developer_policy)
|
|
.into_iter()
|
|
.collect()
|
|
})
|
|
}
|
|
}
|
|
|
|
impl ThreadLifecycleContributor<Config> for MemoriesExtension {
|
|
fn on_thread_start(&self, input: ThreadStartInput<'_, Config>) {
|
|
input.thread_store.insert(MemoriesExtensionConfig {
|
|
enabled: input.config.features.enabled(Feature::MemoryTool)
|
|
&& input.config.memories.use_memories,
|
|
codex_home: input.config.codex_home.clone(),
|
|
});
|
|
}
|
|
}
|
|
|
|
impl ToolContributor for MemoriesExtension {
|
|
fn tools(
|
|
&self,
|
|
_session_store: &ExtensionData,
|
|
thread_store: &ExtensionData,
|
|
) -> Vec<Arc<dyn codex_extension_api::ExtensionToolExecutor>> {
|
|
let Some(config) = thread_store.get::<MemoriesExtensionConfig>() else {
|
|
return Vec::new();
|
|
};
|
|
if !config.enabled {
|
|
return Vec::new();
|
|
}
|
|
|
|
tools::memory_tools(LocalMemoriesBackend::from_codex_home(&config.codex_home))
|
|
}
|
|
}
|
|
|
|
/// Installs the memories extension contributors into the extension registry.
|
|
pub fn install(registry: &mut ExtensionRegistryBuilder<Config>) {
|
|
let extension = Arc::new(MemoriesExtension);
|
|
registry.thread_lifecycle_contributor(extension.clone());
|
|
registry.prompt_contributor(extension.clone());
|
|
registry.tool_contributor(extension);
|
|
}
|