mirror of
https://github.com/openai/codex.git
synced 2026-11-29 00:00:54 +00:00
fix(extensions): complete turn tool migration
This commit is contained in:
Generated
+1
-1
@@ -2619,6 +2619,7 @@ dependencies = [
|
||||
"codex-sandboxing",
|
||||
"codex-shell-command",
|
||||
"codex-shell-escalation",
|
||||
"codex-skill-search-extension",
|
||||
"codex-state",
|
||||
"codex-terminal-detection",
|
||||
"codex-test-binary-support",
|
||||
@@ -4619,7 +4620,6 @@ dependencies = [
|
||||
"codex-model-provider-info",
|
||||
"codex-models-manager",
|
||||
"codex-protocol",
|
||||
"codex-skill-search-extension",
|
||||
"codex-utils-absolute-path",
|
||||
"codex-utils-cargo-bin",
|
||||
"codex-utils-path-uri",
|
||||
|
||||
@@ -136,6 +136,7 @@ assert_matches = { workspace = true }
|
||||
codex-image-generation-extension = { workspace = true }
|
||||
codex-home = { workspace = true }
|
||||
codex-otel = { workspace = true }
|
||||
codex-skill-search-extension = { workspace = true }
|
||||
codex-test-binary-support = { workspace = true }
|
||||
codex-utils-cargo-bin = { workspace = true }
|
||||
codex-web-search-extension = { workspace = true }
|
||||
|
||||
@@ -105,6 +105,7 @@ pub(super) async fn spawn_review_thread(
|
||||
extension_data.insert(HostLoadedSkills::new(
|
||||
parent_turn_context.turn_skills.outcome.clone(),
|
||||
));
|
||||
|
||||
let review_turn_context = TurnContext {
|
||||
sub_id: review_turn_id.clone(),
|
||||
trace_id: current_span_trace_id(),
|
||||
|
||||
@@ -27,7 +27,6 @@ codex-login = { workspace = true }
|
||||
codex-model-provider-info = { workspace = true }
|
||||
codex-models-manager = { workspace = true }
|
||||
codex-protocol = { workspace = true }
|
||||
codex-skill-search-extension = { workspace = true }
|
||||
codex-utils-absolute-path = { workspace = true }
|
||||
codex-utils-cargo-bin = { workspace = true }
|
||||
codex-utils-path-uri = { workspace = true }
|
||||
|
||||
@@ -25,7 +25,6 @@ use codex_exec_server::CreateDirectoryOptions;
|
||||
use codex_exec_server::ExecutorFileSystem;
|
||||
use codex_exec_server::RemoveOptions;
|
||||
use codex_extension_api::ExtensionRegistry;
|
||||
use codex_extension_api::ExtensionRegistryBuilder;
|
||||
use codex_extension_api::LoadUserInstructionsFuture;
|
||||
use codex_extension_api::UserInstructionsProvider;
|
||||
use codex_extension_api::empty_extension_registry;
|
||||
@@ -361,13 +360,6 @@ impl TestCodexBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_skill_search_extension(mut self) -> Self {
|
||||
let mut builder = ExtensionRegistryBuilder::<Config>::new();
|
||||
codex_skill_search_extension::install(&mut builder);
|
||||
self.extensions = Arc::new(builder.build());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_windows_cmd_shell(self) -> Self {
|
||||
if cfg!(windows) {
|
||||
self.with_user_shell(get_shell_by_model_provided_path(&PathBuf::from("cmd.exe")))
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
use anyhow::Result;
|
||||
use codex_exec_server::CreateDirectoryOptions;
|
||||
use codex_exec_server::ExecutorFileSystem;
|
||||
use codex_extension_api::ExtensionRegistryBuilder;
|
||||
use codex_features::Feature;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
@@ -89,6 +90,12 @@ async fn submit_user_turn(test: &TestCodex, cwd: AbsolutePathBuf, prompt: &str)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_codex_with_skill_search() -> core_test_support::test_codex::TestCodexBuilder {
|
||||
let mut extensions = ExtensionRegistryBuilder::new();
|
||||
codex_skill_search_extension::install(&mut extensions);
|
||||
test_codex().with_extensions(Arc::new(extensions.build()))
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn user_turn_includes_skill_instructions() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
@@ -194,8 +201,7 @@ async fn skill_search_tool_is_visible_and_returns_matching_repo_skill() -> Resul
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let search_call_id = "skill-search-call";
|
||||
let mut builder = test_codex()
|
||||
.with_skill_search_extension()
|
||||
let mut builder = test_codex_with_skill_search()
|
||||
.with_config(|config| {
|
||||
config
|
||||
.features
|
||||
@@ -271,9 +277,8 @@ async fn disabled_skill_search_keeps_static_instructions_and_hides_tool() -> Res
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let mut builder = test_codex()
|
||||
.with_skill_search_extension()
|
||||
.with_workspace_setup(move |cwd, fs| async move {
|
||||
let mut builder =
|
||||
test_codex_with_skill_search().with_workspace_setup(move |cwd, fs| async move {
|
||||
write_repo_skill(
|
||||
cwd,
|
||||
fs,
|
||||
@@ -321,8 +326,7 @@ async fn skill_search_excludes_skills_that_disallow_implicit_invocation() -> Res
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let search_call_id = "disallowed-skill-search-call";
|
||||
let mut builder = test_codex()
|
||||
.with_skill_search_extension()
|
||||
let mut builder = test_codex_with_skill_search()
|
||||
.with_config(|config| {
|
||||
config
|
||||
.features
|
||||
@@ -338,6 +342,14 @@ async fn skill_search_excludes_skills_that_disallow_implicit_invocation() -> Res
|
||||
"Use this skill only when explicitly invoked.",
|
||||
)
|
||||
.await?;
|
||||
write_repo_skill(
|
||||
cwd.clone(),
|
||||
Arc::clone(&fs),
|
||||
"public-demo",
|
||||
"Find private demo workflows",
|
||||
"Use this skill for public demo workflows.",
|
||||
)
|
||||
.await?;
|
||||
let metadata_dir = cwd
|
||||
.join(".agents")
|
||||
.join("skills")
|
||||
@@ -388,10 +400,11 @@ async fn skill_search_excludes_skills_that_disallow_implicit_invocation() -> Res
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert_eq!(
|
||||
mock.function_call_output_text(search_call_id).as_deref(),
|
||||
Some("")
|
||||
);
|
||||
let output = mock
|
||||
.function_call_output_text(search_call_id)
|
||||
.expect("skill_search should return output");
|
||||
assert!(output.contains("public-demo"));
|
||||
assert!(!output.contains("private-demo"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -402,8 +415,7 @@ async fn skill_search_uses_host_loaded_skills_from_each_turn() -> Result<()> {
|
||||
let server = start_mock_server().await;
|
||||
let first_call_id = "first-turn-skill-search-call";
|
||||
let second_call_id = "second-turn-skill-search-call";
|
||||
let mut builder = test_codex()
|
||||
.with_skill_search_extension()
|
||||
let mut builder = test_codex_with_skill_search()
|
||||
.with_config(|config| {
|
||||
config
|
||||
.features
|
||||
|
||||
@@ -1139,7 +1139,9 @@ async fn installed_tools_with_start(
|
||||
registry
|
||||
.tool_contributors()
|
||||
.iter()
|
||||
.flat_map(|contributor| contributor.tools(&session_store, &thread_store))
|
||||
.flat_map(|contributor| {
|
||||
contributor.tools(&session_store, &thread_store, &ExtensionData::new("turn-1"))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -1197,10 +1199,13 @@ impl GoalExtensionHarness {
|
||||
}
|
||||
|
||||
fn tools(&self) -> Vec<Arc<dyn ToolExecutor<ToolCall>>> {
|
||||
let turn_store = ExtensionData::new("turn-1");
|
||||
self.registry
|
||||
.tool_contributors()
|
||||
.iter()
|
||||
.flat_map(|contributor| contributor.tools(&self.session_store, &self.thread_store))
|
||||
.flat_map(|contributor| {
|
||||
contributor.tools(&self.session_store, &self.thread_store, &turn_store)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,11 @@ fn tools_are_not_contributed_when_dedicated_tools_disabled() {
|
||||
|
||||
assert!(
|
||||
extension
|
||||
.tools(&ExtensionData::new("session"), &thread_store)
|
||||
.tools(
|
||||
&ExtensionData::new("session"),
|
||||
&thread_store,
|
||||
&ExtensionData::new("turn"),
|
||||
)
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
@@ -90,6 +94,7 @@ fn tools_are_not_contributed_when_dedicated_tools_disabled() {
|
||||
fn tools_are_contributed_when_enabled_with_dedicated_tools() {
|
||||
let extension = MemoriesExtension::default();
|
||||
let thread_store = ExtensionData::new("thread");
|
||||
let turn_store = ExtensionData::new("turn");
|
||||
thread_store.insert(MemoriesExtensionConfig {
|
||||
enabled: true,
|
||||
dedicated_tools: true,
|
||||
@@ -97,11 +102,7 @@ fn tools_are_contributed_when_enabled_with_dedicated_tools() {
|
||||
});
|
||||
|
||||
let tool_names = extension
|
||||
.tools(
|
||||
&ExtensionData::new("session"),
|
||||
&thread_store,
|
||||
&ExtensionData::new("turn"),
|
||||
)
|
||||
.tools(&ExtensionData::new("session"), &thread_store, &turn_store)
|
||||
.into_iter()
|
||||
.map(|tool| tool.tool_name())
|
||||
.collect::<Vec<_>>();
|
||||
@@ -123,6 +124,7 @@ fn install_registers_dedicated_tool_contributor() {
|
||||
crate::install(&mut builder, /*metrics_client*/ None);
|
||||
let registry = builder.build();
|
||||
let thread_store = ExtensionData::new("thread");
|
||||
let turn_store = ExtensionData::new("turn");
|
||||
thread_store.insert(MemoriesExtensionConfig {
|
||||
enabled: true,
|
||||
dedicated_tools: true,
|
||||
@@ -132,7 +134,9 @@ fn install_registers_dedicated_tool_contributor() {
|
||||
let tool_names = registry
|
||||
.tool_contributors()
|
||||
.iter()
|
||||
.flat_map(|contributor| contributor.tools(&ExtensionData::new("session"), &thread_store))
|
||||
.flat_map(|contributor| {
|
||||
contributor.tools(&ExtensionData::new("session"), &thread_store, &turn_store)
|
||||
})
|
||||
.map(|tool| tool.tool_name())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ use codex_core_skills::HostLoadedSkills;
|
||||
use codex_extension_api::ConfigContributor;
|
||||
use codex_extension_api::ContextContributor;
|
||||
use codex_extension_api::ExtensionData;
|
||||
use codex_extension_api::ExtensionFuture;
|
||||
use codex_extension_api::ExtensionRegistryBuilder;
|
||||
use codex_extension_api::PromptFragment;
|
||||
use codex_extension_api::ThreadLifecycleContributor;
|
||||
@@ -39,7 +40,7 @@ impl ContextContributor for SkillSearchExtension {
|
||||
&'a self,
|
||||
_session_store: &'a ExtensionData,
|
||||
thread_store: &'a ExtensionData,
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<PromptFragment>> + Send + 'a>> {
|
||||
) -> ExtensionFuture<'a, Vec<PromptFragment>> {
|
||||
Box::pin(async move {
|
||||
let Some(config) = thread_store.get::<SkillSearchExtensionConfig>() else {
|
||||
return Vec::new();
|
||||
@@ -112,56 +113,5 @@ pub fn install(registry: &mut ExtensionRegistryBuilder<Config>) {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use codex_extension_api::PromptSlot;
|
||||
use codex_extension_api::ToolContributor;
|
||||
use codex_extension_api::ToolName;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::*;
|
||||
use crate::tool::SKILL_SEARCH_TOOL_NAME;
|
||||
|
||||
#[tokio::test]
|
||||
async fn prompt_contribution_is_gated_by_feature_config() {
|
||||
let extension = SkillSearchExtension;
|
||||
let session_store = ExtensionData::new("session");
|
||||
let thread_store = ExtensionData::new("thread");
|
||||
|
||||
assert!(
|
||||
extension
|
||||
.contribute(&session_store, &thread_store)
|
||||
.await
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
thread_store.insert(SkillSearchExtensionConfig { enabled: true });
|
||||
let fragments = extension.contribute(&session_store, &thread_store).await;
|
||||
|
||||
assert_eq!(fragments.len(), 1);
|
||||
assert_eq!(fragments[0].slot(), PromptSlot::DeveloperPolicy);
|
||||
assert!(fragments[0].text().contains(SKILL_SEARCH_TOOL_NAME));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_contribution_is_gated_by_feature_config() {
|
||||
let extension = SkillSearchExtension;
|
||||
let session_store = ExtensionData::new("session");
|
||||
let thread_store = ExtensionData::new("thread");
|
||||
let turn_store = ExtensionData::new("turn");
|
||||
|
||||
assert!(
|
||||
extension
|
||||
.tools(&session_store, &thread_store, &turn_store)
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
thread_store.insert(SkillSearchExtensionConfig { enabled: true });
|
||||
let tool_names = extension
|
||||
.tools(&session_store, &thread_store, &turn_store)
|
||||
.into_iter()
|
||||
.map(|tool| tool.tool_name())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(tool_names, vec![ToolName::plain(SKILL_SEARCH_TOOL_NAME)]);
|
||||
}
|
||||
}
|
||||
#[path = "extension_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
use codex_extension_api::PromptSlot;
|
||||
use codex_extension_api::ToolContributor;
|
||||
use codex_extension_api::ToolName;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::*;
|
||||
use crate::tool::SKILL_SEARCH_TOOL_NAME;
|
||||
|
||||
#[tokio::test]
|
||||
async fn prompt_contribution_is_gated_by_feature_config() {
|
||||
let extension = SkillSearchExtension;
|
||||
let session_store = ExtensionData::new("session");
|
||||
let thread_store = ExtensionData::new("thread");
|
||||
|
||||
assert!(
|
||||
extension
|
||||
.contribute(&session_store, &thread_store)
|
||||
.await
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
thread_store.insert(SkillSearchExtensionConfig { enabled: true });
|
||||
let fragments = extension.contribute(&session_store, &thread_store).await;
|
||||
|
||||
assert_eq!(fragments.len(), 1);
|
||||
assert_eq!(fragments[0].slot(), PromptSlot::DeveloperPolicy);
|
||||
assert!(fragments[0].text().contains(SKILL_SEARCH_TOOL_NAME));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_contribution_is_gated_by_feature_config() {
|
||||
let extension = SkillSearchExtension;
|
||||
let session_store = ExtensionData::new("session");
|
||||
let thread_store = ExtensionData::new("thread");
|
||||
let turn_store = ExtensionData::new("turn");
|
||||
|
||||
assert!(
|
||||
extension
|
||||
.tools(&session_store, &thread_store, &turn_store)
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
thread_store.insert(SkillSearchExtensionConfig { enabled: true });
|
||||
let tool_names = extension
|
||||
.tools(&session_store, &thread_store, &turn_store)
|
||||
.into_iter()
|
||||
.map(|tool| tool.tool_name())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(tool_names, vec![ToolName::plain(SKILL_SEARCH_TOOL_NAME)]);
|
||||
}
|
||||
@@ -4,7 +4,7 @@ use bm25::Document;
|
||||
use bm25::Language;
|
||||
use bm25::SearchEngine;
|
||||
use bm25::SearchEngineBuilder;
|
||||
use codex_core::skills::SkillMetadata;
|
||||
use codex_core_skills::SkillMetadata;
|
||||
use codex_extension_api::FunctionCallError;
|
||||
use codex_extension_api::ResponsesApiTool;
|
||||
use codex_extension_api::ToolCall;
|
||||
@@ -115,7 +115,7 @@ impl ToolExecutor<ToolCall> for SkillSearchTool {
|
||||
BTreeMap::from([
|
||||
(
|
||||
"limit".to_string(),
|
||||
JsonSchema::number(Some(format!(
|
||||
JsonSchema::integer(Some(format!(
|
||||
"Maximum number of skills to return (defaults to {DEFAULT_SKILL_SEARCH_LIMIT} and is capped at {MAX_SKILL_SEARCH_LIMIT})."
|
||||
))),
|
||||
),
|
||||
@@ -170,7 +170,6 @@ fn parse_args(call: &ToolCall) -> Result<SkillSearchArgs, FunctionCallError> {
|
||||
serde_json::from_value(value).map_err(|err| FunctionCallError::RespondToModel(err.to_string()))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct PlainTextToolOutput {
|
||||
text: String,
|
||||
}
|
||||
@@ -196,141 +195,5 @@ impl ToolOutput for PlainTextToolOutput {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_core::skills::SkillPolicy;
|
||||
use codex_extension_api::ConversationHistory;
|
||||
use codex_extension_api::NoopTurnItemEmitter;
|
||||
use codex_protocol::models::FunctionCallOutputBody;
|
||||
use codex_protocol::protocol::SkillScope;
|
||||
use codex_protocol::protocol::TruncationPolicy;
|
||||
use codex_tools::ToolPayload;
|
||||
use codex_utils_absolute_path::test_support::PathBufExt;
|
||||
use codex_utils_absolute_path::test_support::test_path_buf;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn skill(name: &str, description: &str, path: &str) -> SkillMetadata {
|
||||
SkillMetadata {
|
||||
name: name.to_string(),
|
||||
description: description.to_string(),
|
||||
short_description: None,
|
||||
interface: None,
|
||||
dependencies: None,
|
||||
policy: Some(SkillPolicy {
|
||||
allow_implicit_invocation: Some(true),
|
||||
products: Vec::new(),
|
||||
}),
|
||||
path_to_skills_md: test_path_buf(path).abs(),
|
||||
scope: SkillScope::User,
|
||||
plugin_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn call(arguments: serde_json::Value) -> ToolCall {
|
||||
ToolCall {
|
||||
turn_id: "turn-skill-search".to_string(),
|
||||
call_id: "call-skill-search".to_string(),
|
||||
tool_name: ToolName::plain(SKILL_SEARCH_TOOL_NAME),
|
||||
model: "gpt-test".to_string(),
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: arguments.to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn output_text(tool: &SkillSearchTool, arguments: serde_json::Value) -> String {
|
||||
let output = tool
|
||||
.handle(call(arguments))
|
||||
.await
|
||||
.expect("skill_search should return output");
|
||||
let response = output.to_response_item(
|
||||
"call-skill-search",
|
||||
&ToolPayload::Function {
|
||||
arguments: "{}".to_string(),
|
||||
},
|
||||
);
|
||||
let ResponseInputItem::FunctionCallOutput { output, .. } = response else {
|
||||
panic!("expected function output");
|
||||
};
|
||||
let FunctionCallOutputBody::Text(text) = output.body else {
|
||||
panic!("expected text output");
|
||||
};
|
||||
text
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn search_returns_plain_text_skill_lines() {
|
||||
let tool = SkillSearchTool::new(vec![
|
||||
skill("slides", "Build presentation decks", "/tmp/slides/SKILL.md"),
|
||||
skill("sheets", "Analyze spreadsheet data", "/tmp/sheets/SKILL.md"),
|
||||
]);
|
||||
let expected_path = test_path_buf("/tmp/slides/SKILL.md")
|
||||
.abs()
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/");
|
||||
|
||||
assert_eq!(
|
||||
output_text(&tool, json!({ "query": "presentation deck" })).await,
|
||||
format!("- slides: Build presentation decks (file: {expected_path})")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn search_returns_empty_text_when_no_skill_matches() {
|
||||
let tool = SkillSearchTool::new(vec![skill(
|
||||
"slides",
|
||||
"Build presentation decks",
|
||||
"/tmp/slides/SKILL.md",
|
||||
)]);
|
||||
|
||||
assert_eq!(output_text(&tool, json!({ "query": "quantum" })).await, "");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn search_defaults_to_eight_and_caps_requested_limit_at_sixteen() {
|
||||
let skills = (0..20)
|
||||
.map(|index| {
|
||||
skill(
|
||||
&format!("common-{index}"),
|
||||
"Common workflow",
|
||||
&format!("/tmp/common-{index}/SKILL.md"),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let tool = SkillSearchTool::new(skills);
|
||||
|
||||
let default_output = output_text(&tool, json!({ "query": "common workflow" })).await;
|
||||
let capped_output =
|
||||
output_text(&tool, json!({ "query": "common workflow", "limit": 100 })).await;
|
||||
|
||||
assert_eq!(default_output.lines().count(), DEFAULT_SKILL_SEARCH_LIMIT);
|
||||
assert_eq!(capped_output.lines().count(), MAX_SKILL_SEARCH_LIMIT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn search_rejects_empty_query_and_zero_limit() {
|
||||
let tool = SkillSearchTool::new(Vec::new());
|
||||
|
||||
let empty_query = tool
|
||||
.handle(call(json!({ "query": " " })))
|
||||
.await
|
||||
.err()
|
||||
.expect("empty query should fail");
|
||||
let zero_limit = tool
|
||||
.handle(call(json!({ "query": "docs", "limit": 0 })))
|
||||
.await
|
||||
.err()
|
||||
.expect("zero limit should fail");
|
||||
|
||||
assert_eq!(empty_query.to_string(), "query must not be empty");
|
||||
assert_eq!(zero_limit.to_string(), "limit must be greater than zero");
|
||||
}
|
||||
}
|
||||
#[path = "tool_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_core_skills::SkillPolicy;
|
||||
use codex_extension_api::ConversationHistory;
|
||||
use codex_extension_api::NoopTurnItemEmitter;
|
||||
use codex_protocol::models::FunctionCallOutputBody;
|
||||
use codex_protocol::protocol::SkillScope;
|
||||
use codex_protocol::protocol::TruncationPolicy;
|
||||
use codex_tools::ToolPayload;
|
||||
use codex_utils_absolute_path::test_support::PathBufExt;
|
||||
use codex_utils_absolute_path::test_support::test_path_buf;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn skill(name: &str, description: &str, path: &str) -> SkillMetadata {
|
||||
SkillMetadata {
|
||||
name: name.to_string(),
|
||||
description: description.to_string(),
|
||||
short_description: None,
|
||||
interface: None,
|
||||
dependencies: None,
|
||||
policy: Some(SkillPolicy {
|
||||
allow_implicit_invocation: Some(true),
|
||||
products: Vec::new(),
|
||||
}),
|
||||
path_to_skills_md: test_path_buf(path).abs(),
|
||||
scope: SkillScope::User,
|
||||
plugin_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn call(arguments: serde_json::Value) -> ToolCall {
|
||||
ToolCall {
|
||||
turn_id: "turn-skill-search".to_string(),
|
||||
call_id: "call-skill-search".to_string(),
|
||||
tool_name: ToolName::plain(SKILL_SEARCH_TOOL_NAME),
|
||||
model: "gpt-test".to_string(),
|
||||
truncation_policy: TruncationPolicy::Bytes(1024),
|
||||
conversation_history: ConversationHistory::default(),
|
||||
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
|
||||
environments: Vec::new(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: arguments.to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn output_text(tool: &SkillSearchTool, arguments: serde_json::Value) -> String {
|
||||
let output = tool
|
||||
.handle(call(arguments))
|
||||
.await
|
||||
.expect("skill_search should return output");
|
||||
let response = output.to_response_item(
|
||||
"call-skill-search",
|
||||
&ToolPayload::Function {
|
||||
arguments: "{}".to_string(),
|
||||
},
|
||||
);
|
||||
let ResponseInputItem::FunctionCallOutput { output, .. } = response else {
|
||||
panic!("expected function output");
|
||||
};
|
||||
let FunctionCallOutputBody::Text(text) = output.body else {
|
||||
panic!("expected text output");
|
||||
};
|
||||
text
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn search_returns_plain_text_skill_lines() {
|
||||
let tool = SkillSearchTool::new(vec![
|
||||
skill("slides", "Build presentation decks", "/tmp/slides/SKILL.md"),
|
||||
skill("sheets", "Analyze spreadsheet data", "/tmp/sheets/SKILL.md"),
|
||||
]);
|
||||
let expected_path = test_path_buf("/tmp/slides/SKILL.md")
|
||||
.abs()
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/");
|
||||
|
||||
assert_eq!(
|
||||
output_text(&tool, json!({ "query": "presentation deck" })).await,
|
||||
format!("- slides: Build presentation decks (file: {expected_path})")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn search_returns_empty_text_when_no_skill_matches() {
|
||||
let tool = SkillSearchTool::new(vec![skill(
|
||||
"slides",
|
||||
"Build presentation decks",
|
||||
"/tmp/slides/SKILL.md",
|
||||
)]);
|
||||
|
||||
assert_eq!(output_text(&tool, json!({ "query": "quantum" })).await, "");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn search_defaults_to_eight_and_caps_requested_limit_at_sixteen() {
|
||||
let skills = (0..20)
|
||||
.map(|index| {
|
||||
skill(
|
||||
&format!("common-{index}"),
|
||||
"Common workflow",
|
||||
&format!("/tmp/common-{index}/SKILL.md"),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let tool = SkillSearchTool::new(skills);
|
||||
|
||||
let default_output = output_text(&tool, json!({ "query": "common workflow" })).await;
|
||||
let capped_output =
|
||||
output_text(&tool, json!({ "query": "common workflow", "limit": 100 })).await;
|
||||
|
||||
assert_eq!(default_output.lines().count(), DEFAULT_SKILL_SEARCH_LIMIT);
|
||||
assert_eq!(capped_output.lines().count(), MAX_SKILL_SEARCH_LIMIT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn search_rejects_empty_query_and_zero_limit() {
|
||||
let tool = SkillSearchTool::new(Vec::new());
|
||||
|
||||
let empty_query = tool
|
||||
.handle(call(json!({ "query": " " })))
|
||||
.await
|
||||
.err()
|
||||
.expect("empty query should fail");
|
||||
let zero_limit = tool
|
||||
.handle(call(json!({ "query": "docs", "limit": 0 })))
|
||||
.await
|
||||
.err()
|
||||
.expect("zero limit should fail");
|
||||
|
||||
assert_eq!(empty_query.to_string(), "query must not be empty");
|
||||
assert_eq!(zero_limit.to_string(), "limit must be greater than zero");
|
||||
}
|
||||
@@ -164,6 +164,7 @@ mod tests {
|
||||
let registry = builder.build();
|
||||
let session_store = ExtensionData::new("session");
|
||||
let thread_store = ExtensionData::new("11111111-1111-4111-8111-111111111111");
|
||||
let turn_store = ExtensionData::new("turn");
|
||||
thread_store.insert(WebSearchExtensionConfig {
|
||||
available: true,
|
||||
provider: ModelProviderInfo::create_openai_provider(/*base_url*/ None),
|
||||
@@ -173,7 +174,7 @@ mod tests {
|
||||
let tool_names = registry
|
||||
.tool_contributors()
|
||||
.iter()
|
||||
.flat_map(|contributor| contributor.tools(&session_store, &thread_store))
|
||||
.flat_map(|contributor| contributor.tools(&session_store, &thread_store, &turn_store))
|
||||
.map(|tool| (tool.tool_name(), tool.supports_parallel_tool_calls()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user