@howaboua/pi-codex-web-run 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -2
- package/README.md +5 -5
- package/UPSTREAM_SYNC.md +2 -2
- package/biome.json +1 -1
- package/index.ts +46 -3
- package/package.json +17 -10
- package/src/codex-runtime/config.ts +5 -6
- package/src/execute.ts +1 -1
- package/src/request.ts +8 -1
- package/upstream/codex-api-endpoint/search.rs +0 -318
- package/upstream/codex-api-endpoint/search_types.rs +0 -305
- package/upstream/web-search/extension.rs +0 -220
- package/upstream/web-search/history.rs +0 -205
- package/upstream/web-search/lib.rs +0 -7
- package/upstream/web-search/output.rs +0 -74
- package/upstream/web-search/schema.rs +0 -36
- package/upstream/web-search/tool.rs +0 -332
- package/upstream/web-search/web_run_description.md +0 -105
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
use codex_api::SearchInput;
|
|
2
|
-
use codex_core::parse_turn_item;
|
|
3
|
-
use codex_protocol::items::TurnItem;
|
|
4
|
-
use codex_protocol::models::ContentItem;
|
|
5
|
-
use codex_protocol::models::ResponseItem;
|
|
6
|
-
use codex_protocol::models::plaintext_agent_message_content;
|
|
7
|
-
use codex_tools::retain_tail_from_last_n_user_messages;
|
|
8
|
-
use codex_tools::truncate_assistant_output_text_to_token_budget;
|
|
9
|
-
|
|
10
|
-
const ASSISTANT_CONTEXT_TOKEN_LIMIT: usize = 1_000;
|
|
11
|
-
const ASSISTANT_ROLE: &str = "assistant";
|
|
12
|
-
const USER_ROLE: &str = "user";
|
|
13
|
-
|
|
14
|
-
/// Builds the conversation tail for standalone web search.
|
|
15
|
-
///
|
|
16
|
-
/// The tail keeps the previous user text message, up to 1k tokens of assistant
|
|
17
|
-
/// text that followed it, and the current user text message.
|
|
18
|
-
pub(crate) fn recent_input(items: &[ResponseItem]) -> Option<SearchInput> {
|
|
19
|
-
let mut messages = Vec::new();
|
|
20
|
-
for item in items {
|
|
21
|
-
push_visible_message(&mut messages, item);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
retain_tail_from_last_n_user_messages(&mut messages, /*user_message_count*/ 2);
|
|
25
|
-
truncate_assistant_output_text_to_token_budget(&mut messages, ASSISTANT_CONTEXT_TOKEN_LIMIT);
|
|
26
|
-
(!messages.is_empty()).then_some(SearchInput::Items(messages))
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
fn push_visible_message(messages: &mut Vec<ResponseItem>, item: &ResponseItem) {
|
|
30
|
-
match item {
|
|
31
|
-
ResponseItem::Message { role, .. } if role == ASSISTANT_ROLE => {
|
|
32
|
-
let mut message = item.clone();
|
|
33
|
-
message.set_id(/*new_id*/ None);
|
|
34
|
-
messages.push(message);
|
|
35
|
-
}
|
|
36
|
-
ResponseItem::AgentMessage {
|
|
37
|
-
author,
|
|
38
|
-
content,
|
|
39
|
-
internal_chat_message_metadata_passthrough: metadata,
|
|
40
|
-
..
|
|
41
|
-
} => {
|
|
42
|
-
if let Some(text) = plaintext_agent_message_content(content) {
|
|
43
|
-
messages.push(ResponseItem::Message {
|
|
44
|
-
id: None,
|
|
45
|
-
role: ASSISTANT_ROLE.to_string(),
|
|
46
|
-
content: vec![ContentItem::OutputText {
|
|
47
|
-
text: format!("Agent message from {author}:\n{text}"),
|
|
48
|
-
}],
|
|
49
|
-
phase: None,
|
|
50
|
-
internal_chat_message_metadata_passthrough: metadata.clone(),
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
ResponseItem::Message {
|
|
55
|
-
id: _,
|
|
56
|
-
role,
|
|
57
|
-
content,
|
|
58
|
-
phase,
|
|
59
|
-
internal_chat_message_metadata_passthrough: metadata,
|
|
60
|
-
} if role == USER_ROLE
|
|
61
|
-
&& matches!(parse_turn_item(item), Some(TurnItem::UserMessage(_))) =>
|
|
62
|
-
{
|
|
63
|
-
let content = content
|
|
64
|
-
.iter()
|
|
65
|
-
.filter(|item| matches!(item, ContentItem::InputText { .. }))
|
|
66
|
-
.cloned()
|
|
67
|
-
.collect::<Vec<_>>();
|
|
68
|
-
if !content.is_empty() {
|
|
69
|
-
messages.push(ResponseItem::Message {
|
|
70
|
-
id: None,
|
|
71
|
-
role: role.clone(),
|
|
72
|
-
content,
|
|
73
|
-
phase: phase.clone(),
|
|
74
|
-
internal_chat_message_metadata_passthrough: metadata.clone(),
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
_ => {}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
#[cfg(test)]
|
|
83
|
-
mod tests {
|
|
84
|
-
use codex_api::SearchInput;
|
|
85
|
-
use codex_protocol::ResponseItemId;
|
|
86
|
-
use codex_protocol::models::ContentItem;
|
|
87
|
-
use codex_protocol::models::ResponseItem;
|
|
88
|
-
use pretty_assertions::assert_eq;
|
|
89
|
-
|
|
90
|
-
use super::ASSISTANT_ROLE;
|
|
91
|
-
use super::USER_ROLE;
|
|
92
|
-
use super::recent_input;
|
|
93
|
-
|
|
94
|
-
fn message(role: &str, text: &str) -> ResponseItem {
|
|
95
|
-
ResponseItem::Message {
|
|
96
|
-
id: None,
|
|
97
|
-
role: role.to_string(),
|
|
98
|
-
content: vec![if role == ASSISTANT_ROLE {
|
|
99
|
-
ContentItem::OutputText {
|
|
100
|
-
text: text.to_string(),
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
ContentItem::InputText {
|
|
104
|
-
text: text.to_string(),
|
|
105
|
-
}
|
|
106
|
-
}],
|
|
107
|
-
phase: None,
|
|
108
|
-
internal_chat_message_metadata_passthrough: None,
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
#[test]
|
|
113
|
-
fn keeps_current_user_and_previous_visible_turn() {
|
|
114
|
-
let mut previous_user = message(USER_ROLE, "previous user");
|
|
115
|
-
previous_user.set_id(Some(ResponseItemId::with_suffix("msg", "previous_user")));
|
|
116
|
-
let mut previous_assistant = message(ASSISTANT_ROLE, "previous assistant");
|
|
117
|
-
previous_assistant.set_id(Some(ResponseItemId::with_suffix(
|
|
118
|
-
"msg",
|
|
119
|
-
"previous_assistant",
|
|
120
|
-
)));
|
|
121
|
-
let items = vec![
|
|
122
|
-
message("system", "system"),
|
|
123
|
-
message(USER_ROLE, "old user"),
|
|
124
|
-
message(ASSISTANT_ROLE, "old assistant"),
|
|
125
|
-
previous_user,
|
|
126
|
-
ResponseItem::FunctionCall {
|
|
127
|
-
id: None,
|
|
128
|
-
name: "tool".to_string(),
|
|
129
|
-
namespace: None,
|
|
130
|
-
arguments: "{}".to_string(),
|
|
131
|
-
call_id: "call-1".to_string(),
|
|
132
|
-
encrypted_function_args: None,
|
|
133
|
-
internal_chat_message_metadata_passthrough: None,
|
|
134
|
-
},
|
|
135
|
-
previous_assistant,
|
|
136
|
-
message("developer", "developer"),
|
|
137
|
-
message(USER_ROLE, "current user"),
|
|
138
|
-
message(ASSISTANT_ROLE, "current commentary"),
|
|
139
|
-
];
|
|
140
|
-
|
|
141
|
-
assert_eq!(
|
|
142
|
-
recent_input(&items),
|
|
143
|
-
Some(SearchInput::Items(vec![
|
|
144
|
-
message(USER_ROLE, "previous user"),
|
|
145
|
-
message(ASSISTANT_ROLE, "previous assistant"),
|
|
146
|
-
message(USER_ROLE, "current user"),
|
|
147
|
-
]))
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
#[test]
|
|
152
|
-
fn keeps_only_text_from_recent_user_messages() {
|
|
153
|
-
let previous_user = ResponseItem::Message {
|
|
154
|
-
id: None,
|
|
155
|
-
role: USER_ROLE.to_string(),
|
|
156
|
-
content: vec![
|
|
157
|
-
ContentItem::InputText {
|
|
158
|
-
text: "previous user".to_string(),
|
|
159
|
-
},
|
|
160
|
-
ContentItem::InputImage {
|
|
161
|
-
image_url: "data:image/png;base64,image".to_string(),
|
|
162
|
-
detail: None,
|
|
163
|
-
},
|
|
164
|
-
],
|
|
165
|
-
phase: None,
|
|
166
|
-
internal_chat_message_metadata_passthrough: None,
|
|
167
|
-
};
|
|
168
|
-
let items = vec![
|
|
169
|
-
previous_user,
|
|
170
|
-
message(ASSISTANT_ROLE, "previous assistant"),
|
|
171
|
-
message(USER_ROLE, "current user"),
|
|
172
|
-
];
|
|
173
|
-
|
|
174
|
-
assert_eq!(
|
|
175
|
-
recent_input(&items),
|
|
176
|
-
Some(SearchInput::Items(vec![
|
|
177
|
-
message(USER_ROLE, "previous user"),
|
|
178
|
-
message(ASSISTANT_ROLE, "previous assistant"),
|
|
179
|
-
message(USER_ROLE, "current user"),
|
|
180
|
-
]))
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
#[test]
|
|
185
|
-
fn ignores_contextual_user_messages_when_selecting_recent_turns() {
|
|
186
|
-
let items = vec![
|
|
187
|
-
message(USER_ROLE, "previous user"),
|
|
188
|
-
message(ASSISTANT_ROLE, "previous assistant"),
|
|
189
|
-
message(
|
|
190
|
-
USER_ROLE,
|
|
191
|
-
"<environment_context>\n<cwd>/tmp</cwd>\n</environment_context>",
|
|
192
|
-
),
|
|
193
|
-
message(USER_ROLE, "current user"),
|
|
194
|
-
];
|
|
195
|
-
|
|
196
|
-
assert_eq!(
|
|
197
|
-
recent_input(&items),
|
|
198
|
-
Some(SearchInput::Items(vec![
|
|
199
|
-
message(USER_ROLE, "previous user"),
|
|
200
|
-
message(ASSISTANT_ROLE, "previous assistant"),
|
|
201
|
-
message(USER_ROLE, "current user"),
|
|
202
|
-
]))
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
use codex_extension_api::ToolOutput;
|
|
2
|
-
use codex_extension_api::ToolPayload;
|
|
3
|
-
use codex_protocol::models::FunctionCallOutputContentItem;
|
|
4
|
-
use codex_protocol::models::FunctionCallOutputPayload;
|
|
5
|
-
use codex_protocol::models::ResponseInputItem;
|
|
6
|
-
|
|
7
|
-
pub(crate) struct SearchOutput {
|
|
8
|
-
output: String,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
impl SearchOutput {
|
|
12
|
-
pub(crate) fn new(output: String) -> Self {
|
|
13
|
-
Self { output }
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
impl ToolOutput for SearchOutput {
|
|
18
|
-
fn log_preview(&self) -> String {
|
|
19
|
-
"[standalone web search output]".to_string()
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
fn success_for_logging(&self) -> bool {
|
|
23
|
-
true
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
fn contains_external_context(&self) -> bool {
|
|
27
|
-
true
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
fn to_response_item(&self, call_id: &str, _payload: &ToolPayload) -> ResponseInputItem {
|
|
31
|
-
ResponseInputItem::FunctionCallOutput {
|
|
32
|
-
call_id: call_id.to_string(),
|
|
33
|
-
output: FunctionCallOutputPayload::from_content_items(vec![
|
|
34
|
-
FunctionCallOutputContentItem::InputText {
|
|
35
|
-
text: self.output.clone(),
|
|
36
|
-
},
|
|
37
|
-
]),
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
#[cfg(test)]
|
|
43
|
-
mod tests {
|
|
44
|
-
use codex_extension_api::ToolPayload;
|
|
45
|
-
use codex_protocol::models::FunctionCallOutputContentItem;
|
|
46
|
-
use codex_protocol::models::FunctionCallOutputPayload;
|
|
47
|
-
use codex_protocol::models::ResponseInputItem;
|
|
48
|
-
use pretty_assertions::assert_eq;
|
|
49
|
-
|
|
50
|
-
use super::SearchOutput;
|
|
51
|
-
use super::ToolOutput;
|
|
52
|
-
|
|
53
|
-
#[test]
|
|
54
|
-
fn emits_plaintext_function_call_output() {
|
|
55
|
-
let output = SearchOutput::new("search output".to_string());
|
|
56
|
-
|
|
57
|
-
assert_eq!(
|
|
58
|
-
output.to_response_item(
|
|
59
|
-
"call-1",
|
|
60
|
-
&ToolPayload::Function {
|
|
61
|
-
arguments: "{}".to_string(),
|
|
62
|
-
},
|
|
63
|
-
),
|
|
64
|
-
ResponseInputItem::FunctionCallOutput {
|
|
65
|
-
call_id: "call-1".to_string(),
|
|
66
|
-
output: FunctionCallOutputPayload::from_content_items(vec![
|
|
67
|
-
FunctionCallOutputContentItem::InputText {
|
|
68
|
-
text: "search output".to_string(),
|
|
69
|
-
},
|
|
70
|
-
]),
|
|
71
|
-
}
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
use codex_api::SearchCommands;
|
|
2
|
-
use schemars::r#gen::SchemaSettings;
|
|
3
|
-
use serde_json::Map;
|
|
4
|
-
use serde_json::Value;
|
|
5
|
-
|
|
6
|
-
pub(crate) fn commands_schema() -> Value {
|
|
7
|
-
let schema = SchemaSettings::draft2019_09()
|
|
8
|
-
.with(|settings| {
|
|
9
|
-
settings.inline_subschemas = true;
|
|
10
|
-
settings.option_add_null_type = false;
|
|
11
|
-
})
|
|
12
|
-
.into_generator()
|
|
13
|
-
.into_root_schema_for::<SearchCommands>();
|
|
14
|
-
let schema = match serde_json::to_value(schema) {
|
|
15
|
-
Ok(schema) => schema,
|
|
16
|
-
Err(err) => panic!("search commands schema should serialize: {err}"),
|
|
17
|
-
};
|
|
18
|
-
let Value::Object(mut schema) = schema else {
|
|
19
|
-
unreachable!("search commands schema must be an object");
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
let mut tool_schema = Map::new();
|
|
23
|
-
for key in [
|
|
24
|
-
"properties",
|
|
25
|
-
"required",
|
|
26
|
-
"type",
|
|
27
|
-
"additionalProperties",
|
|
28
|
-
"$defs",
|
|
29
|
-
"definitions",
|
|
30
|
-
] {
|
|
31
|
-
if let Some(value) = schema.remove(key) {
|
|
32
|
-
tool_schema.insert(key.to_string(), value);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
Value::Object(tool_schema)
|
|
36
|
-
}
|
|
@@ -1,332 +0,0 @@
|
|
|
1
|
-
use codex_api::ReqwestTransport;
|
|
2
|
-
use codex_api::SearchClient;
|
|
3
|
-
use codex_api::SearchCommands;
|
|
4
|
-
use codex_api::SearchQuery;
|
|
5
|
-
use codex_api::SearchRequest;
|
|
6
|
-
use codex_api::SearchSettings;
|
|
7
|
-
use codex_core::X_CODEX_TURN_METADATA_HEADER;
|
|
8
|
-
use codex_core::web_search_action_detail;
|
|
9
|
-
use codex_extension_api::ExtensionTurnItem;
|
|
10
|
-
use codex_extension_api::FunctionCallError;
|
|
11
|
-
use codex_extension_api::ResponsesApiTool;
|
|
12
|
-
use codex_extension_api::ToolCall;
|
|
13
|
-
use codex_extension_api::ToolExecutor;
|
|
14
|
-
use codex_extension_api::ToolName;
|
|
15
|
-
use codex_extension_api::ToolOutput;
|
|
16
|
-
use codex_extension_api::ToolSpec;
|
|
17
|
-
use codex_extension_api::parse_tool_input_schema_without_compaction;
|
|
18
|
-
use codex_extension_items::ExtensionItem;
|
|
19
|
-
use codex_extension_items::web_search::WebSearchAction;
|
|
20
|
-
use codex_extension_items::web_search::WebSearchItem;
|
|
21
|
-
use codex_login::default_client::add_originator_header;
|
|
22
|
-
use codex_login::default_client::create_client;
|
|
23
|
-
use codex_model_provider::SharedModelProvider;
|
|
24
|
-
use codex_protocol::models::WebSearchAction as CoreWebSearchAction;
|
|
25
|
-
use codex_protocol::protocol::EventMsg;
|
|
26
|
-
use codex_protocol::protocol::WebSearchBeginEvent;
|
|
27
|
-
use codex_protocol::protocol::WebSearchEndEvent;
|
|
28
|
-
use codex_tools::ResponsesApiNamespace;
|
|
29
|
-
use codex_tools::ResponsesApiNamespaceTool;
|
|
30
|
-
use codex_tools::ToolExposure;
|
|
31
|
-
use codex_tools::default_namespace_description;
|
|
32
|
-
use http::HeaderMap;
|
|
33
|
-
use http::HeaderValue;
|
|
34
|
-
use url::Url;
|
|
35
|
-
|
|
36
|
-
use crate::history::recent_input;
|
|
37
|
-
use crate::output::SearchOutput;
|
|
38
|
-
use crate::schema::commands_schema;
|
|
39
|
-
|
|
40
|
-
pub(crate) const WEB_NAMESPACE: &str = "web";
|
|
41
|
-
pub(crate) const RUN_TOOL_NAME: &str = "run";
|
|
42
|
-
const WEB_RUN_DESCRIPTION: &str = include_str!("../web_run_description.md");
|
|
43
|
-
const RESULTS_PAYLOAD_BYTES_METRIC: &str = "codex.web_search.results.payload_bytes";
|
|
44
|
-
|
|
45
|
-
pub(crate) struct WebSearchTool {
|
|
46
|
-
pub(crate) session_id: String,
|
|
47
|
-
pub(crate) provider: SharedModelProvider,
|
|
48
|
-
pub(crate) settings: SearchSettings,
|
|
49
|
-
pub(crate) originator: Option<String>,
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
impl ToolExecutor<ToolCall> for WebSearchTool {
|
|
53
|
-
fn tool_name(&self) -> ToolName {
|
|
54
|
-
ToolName::namespaced(WEB_NAMESPACE, RUN_TOOL_NAME)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
fn spec(&self) -> ToolSpec {
|
|
58
|
-
// parse schema without compaction that removes field metadata/descriptions to match hosted tool definition
|
|
59
|
-
let parameters = match parse_tool_input_schema_without_compaction(&commands_schema()) {
|
|
60
|
-
Ok(parameters) => parameters,
|
|
61
|
-
Err(err) => panic!("search command schema should parse: {err}"),
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
ToolSpec::Namespace(ResponsesApiNamespace {
|
|
65
|
-
name: WEB_NAMESPACE.to_string(),
|
|
66
|
-
description: default_namespace_description(WEB_NAMESPACE),
|
|
67
|
-
tools: vec![ResponsesApiNamespaceTool::Function(ResponsesApiTool {
|
|
68
|
-
name: RUN_TOOL_NAME.to_string(),
|
|
69
|
-
description: WEB_RUN_DESCRIPTION.to_string(),
|
|
70
|
-
strict: false,
|
|
71
|
-
parameters,
|
|
72
|
-
output_schema: None,
|
|
73
|
-
defer_loading: None,
|
|
74
|
-
})],
|
|
75
|
-
})
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
fn exposure(&self) -> ToolExposure {
|
|
79
|
-
ToolExposure::Direct
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
fn supports_parallel_tool_calls(&self) -> bool {
|
|
83
|
-
true
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
fn handle(&self, call: ToolCall) -> codex_extension_api::ToolExecutorFuture<'_> {
|
|
87
|
-
Box::pin(self.handle_call(call))
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
impl WebSearchTool {
|
|
92
|
-
async fn handle_call(&self, call: ToolCall) -> Result<Box<dyn ToolOutput>, FunctionCallError> {
|
|
93
|
-
let commands = parse_commands(&call)?;
|
|
94
|
-
let command_action = command_action(&commands);
|
|
95
|
-
let provider = self
|
|
96
|
-
.provider
|
|
97
|
-
.api_provider()
|
|
98
|
-
.await
|
|
99
|
-
.map_err(|err| FunctionCallError::Fatal(err.to_string()))?;
|
|
100
|
-
let auth = self
|
|
101
|
-
.provider
|
|
102
|
-
.api_auth()
|
|
103
|
-
.await
|
|
104
|
-
.map_err(|err| FunctionCallError::Fatal(err.to_string()))?;
|
|
105
|
-
let client = SearchClient::new(
|
|
106
|
-
ReqwestTransport::from_http_client(create_client()),
|
|
107
|
-
provider,
|
|
108
|
-
auth,
|
|
109
|
-
);
|
|
110
|
-
let request = SearchRequest {
|
|
111
|
-
id: self.session_id.clone(),
|
|
112
|
-
model: call.model.clone(),
|
|
113
|
-
reasoning: None,
|
|
114
|
-
input: recent_input(call.conversation_history.items()),
|
|
115
|
-
commands: Some(commands),
|
|
116
|
-
settings: Some(self.settings.clone()),
|
|
117
|
-
max_output_tokens: Some(
|
|
118
|
-
u64::try_from(call.truncation_policy.token_budget()).unwrap_or(u64::MAX),
|
|
119
|
-
),
|
|
120
|
-
};
|
|
121
|
-
let extra_headers = search_request_headers(
|
|
122
|
-
self.originator.as_deref(),
|
|
123
|
-
call.codex_turn_metadata.as_deref(),
|
|
124
|
-
);
|
|
125
|
-
call.turn_item_emitter
|
|
126
|
-
.emit_started(extension_turn_item(
|
|
127
|
-
WebSearchItem {
|
|
128
|
-
id: call.call_id.clone(),
|
|
129
|
-
query: String::new(),
|
|
130
|
-
action: None,
|
|
131
|
-
results: None,
|
|
132
|
-
},
|
|
133
|
-
EventMsg::WebSearchBegin(WebSearchBeginEvent {
|
|
134
|
-
call_id: call.call_id.clone(),
|
|
135
|
-
}),
|
|
136
|
-
))
|
|
137
|
-
.await;
|
|
138
|
-
let response = client
|
|
139
|
-
.search(&request, extra_headers)
|
|
140
|
-
.await
|
|
141
|
-
.map_err(|err| FunctionCallError::Fatal(err.to_string()))?;
|
|
142
|
-
let output = response.output;
|
|
143
|
-
let results = response.results;
|
|
144
|
-
if let Some(results) = results.as_ref()
|
|
145
|
-
&& let Some(metrics) = codex_otel::global()
|
|
146
|
-
&& let Ok(payload) = serde_json::to_vec(results)
|
|
147
|
-
{
|
|
148
|
-
let payload_bytes = i64::try_from(payload.len()).unwrap_or(i64::MAX);
|
|
149
|
-
let _ = metrics.histogram(RESULTS_PAYLOAD_BYTES_METRIC, payload_bytes, &[]);
|
|
150
|
-
}
|
|
151
|
-
let legacy_action = match &command_action {
|
|
152
|
-
WebSearchAction::Search { query, queries } => CoreWebSearchAction::Search {
|
|
153
|
-
query: query.clone(),
|
|
154
|
-
queries: queries.clone(),
|
|
155
|
-
},
|
|
156
|
-
WebSearchAction::OpenPage { url } => CoreWebSearchAction::OpenPage { url: url.clone() },
|
|
157
|
-
WebSearchAction::FindInPage { url, pattern } => CoreWebSearchAction::FindInPage {
|
|
158
|
-
url: url.clone(),
|
|
159
|
-
pattern: pattern.clone(),
|
|
160
|
-
},
|
|
161
|
-
WebSearchAction::Other => CoreWebSearchAction::Other,
|
|
162
|
-
};
|
|
163
|
-
let query = web_search_action_detail(&legacy_action);
|
|
164
|
-
call.turn_item_emitter
|
|
165
|
-
.emit_completed(extension_turn_item(
|
|
166
|
-
WebSearchItem {
|
|
167
|
-
id: call.call_id.clone(),
|
|
168
|
-
query: query.clone(),
|
|
169
|
-
action: Some(command_action),
|
|
170
|
-
results: results.clone(),
|
|
171
|
-
},
|
|
172
|
-
EventMsg::WebSearchEnd(WebSearchEndEvent {
|
|
173
|
-
call_id: call.call_id.clone(),
|
|
174
|
-
query,
|
|
175
|
-
action: legacy_action,
|
|
176
|
-
results,
|
|
177
|
-
}),
|
|
178
|
-
))
|
|
179
|
-
.await;
|
|
180
|
-
|
|
181
|
-
Ok(Box::new(SearchOutput::new(output)))
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
fn search_request_headers(originator: Option<&str>, turn_metadata: Option<&str>) -> HeaderMap {
|
|
186
|
-
let mut headers = HeaderMap::new();
|
|
187
|
-
if let Some(turn_metadata) = turn_metadata
|
|
188
|
-
&& let Ok(header_value) = HeaderValue::from_str(turn_metadata)
|
|
189
|
-
{
|
|
190
|
-
headers.insert(X_CODEX_TURN_METADATA_HEADER, header_value);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if let Some(originator) = originator {
|
|
194
|
-
add_originator_header(&mut headers, originator);
|
|
195
|
-
}
|
|
196
|
-
headers
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
fn parse_commands(call: &ToolCall) -> Result<SearchCommands, FunctionCallError> {
|
|
200
|
-
let arguments = call.function_arguments()?;
|
|
201
|
-
if arguments.trim().is_empty() {
|
|
202
|
-
return Ok(SearchCommands::default());
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
serde_json::from_str(arguments)
|
|
206
|
-
.map_err(|err| FunctionCallError::RespondToModel(err.to_string()))
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
fn command_action(commands: &SearchCommands) -> WebSearchAction {
|
|
210
|
-
commands
|
|
211
|
-
.search_query
|
|
212
|
-
.as_deref()
|
|
213
|
-
.and_then(query_action)
|
|
214
|
-
.or_else(|| commands.image_query.as_deref().and_then(query_action))
|
|
215
|
-
.or_else(|| {
|
|
216
|
-
commands
|
|
217
|
-
.open
|
|
218
|
-
.as_deref()
|
|
219
|
-
.and_then(|operations| operations.first())
|
|
220
|
-
.and_then(|operation| {
|
|
221
|
-
literal_url(&operation.ref_id)
|
|
222
|
-
.map(|url| WebSearchAction::OpenPage { url: Some(url) })
|
|
223
|
-
})
|
|
224
|
-
})
|
|
225
|
-
.or_else(|| {
|
|
226
|
-
commands
|
|
227
|
-
.find
|
|
228
|
-
.as_deref()
|
|
229
|
-
.and_then(|operations| operations.first())
|
|
230
|
-
.map(|operation| WebSearchAction::FindInPage {
|
|
231
|
-
url: literal_url(&operation.ref_id),
|
|
232
|
-
pattern: Some(operation.pattern.clone()),
|
|
233
|
-
})
|
|
234
|
-
})
|
|
235
|
-
.unwrap_or(WebSearchAction::Other)
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
fn query_action(queries: &[SearchQuery]) -> Option<WebSearchAction> {
|
|
239
|
-
match queries {
|
|
240
|
-
[] => None,
|
|
241
|
-
[query] => Some(WebSearchAction::Search {
|
|
242
|
-
query: Some(query.q.clone()),
|
|
243
|
-
queries: None,
|
|
244
|
-
}),
|
|
245
|
-
queries => Some(WebSearchAction::Search {
|
|
246
|
-
query: None,
|
|
247
|
-
queries: Some(queries.iter().map(|query| query.q.clone()).collect()),
|
|
248
|
-
}),
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
fn literal_url(ref_id: &str) -> Option<String> {
|
|
253
|
-
Url::parse(ref_id).is_ok().then(|| ref_id.to_string())
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
fn extension_turn_item(item: WebSearchItem, legacy_event: EventMsg) -> ExtensionTurnItem {
|
|
257
|
-
ExtensionTurnItem {
|
|
258
|
-
item: ExtensionItem::WebSearch(item),
|
|
259
|
-
legacy_events: vec![legacy_event],
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
#[cfg(test)]
|
|
264
|
-
mod tests {
|
|
265
|
-
use codex_api::SearchCommands;
|
|
266
|
-
use codex_extension_items::web_search::WebSearchAction;
|
|
267
|
-
use pretty_assertions::assert_eq;
|
|
268
|
-
|
|
269
|
-
use super::command_action;
|
|
270
|
-
use super::search_request_headers;
|
|
271
|
-
use codex_core::X_CODEX_TURN_METADATA_HEADER;
|
|
272
|
-
|
|
273
|
-
#[test]
|
|
274
|
-
fn search_request_headers_forward_thread_originator_and_turn_metadata() {
|
|
275
|
-
let headers = search_request_headers(Some("chatgpt_cca"), Some("turn-metadata"));
|
|
276
|
-
assert_eq!(
|
|
277
|
-
headers
|
|
278
|
-
.get("originator")
|
|
279
|
-
.and_then(|value| value.to_str().ok()),
|
|
280
|
-
Some("chatgpt_cca")
|
|
281
|
-
);
|
|
282
|
-
assert_eq!(
|
|
283
|
-
headers
|
|
284
|
-
.get(X_CODEX_TURN_METADATA_HEADER)
|
|
285
|
-
.and_then(|value| value.to_str().ok()),
|
|
286
|
-
Some("turn-metadata")
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
#[test]
|
|
291
|
-
fn command_action_reports_queries_and_navigation_detail() {
|
|
292
|
-
let cases = [
|
|
293
|
-
(
|
|
294
|
-
r#"{"image_query":[{"q":"waterfalls"},{"q":"mountains"}]}"#,
|
|
295
|
-
WebSearchAction::Search {
|
|
296
|
-
query: None,
|
|
297
|
-
queries: Some(vec!["waterfalls".to_string(), "mountains".to_string()]),
|
|
298
|
-
},
|
|
299
|
-
),
|
|
300
|
-
(
|
|
301
|
-
r#"{"open":[{"ref_id":"https://example.com/docs"}]}"#,
|
|
302
|
-
WebSearchAction::OpenPage {
|
|
303
|
-
url: Some("https://example.com/docs".to_string()),
|
|
304
|
-
},
|
|
305
|
-
),
|
|
306
|
-
(
|
|
307
|
-
r#"{"find":[{"ref_id":"https://example.com/docs","pattern":"install"}]}"#,
|
|
308
|
-
WebSearchAction::FindInPage {
|
|
309
|
-
url: Some("https://example.com/docs".to_string()),
|
|
310
|
-
pattern: Some("install".to_string()),
|
|
311
|
-
},
|
|
312
|
-
),
|
|
313
|
-
(
|
|
314
|
-
r#"{"find":[{"ref_id":"turn0search0","pattern":"install"}]}"#,
|
|
315
|
-
WebSearchAction::FindInPage {
|
|
316
|
-
url: None,
|
|
317
|
-
pattern: Some("install".to_string()),
|
|
318
|
-
},
|
|
319
|
-
),
|
|
320
|
-
(
|
|
321
|
-
r#"{"open":[{"ref_id":"turn0search0"}]}"#,
|
|
322
|
-
WebSearchAction::Other,
|
|
323
|
-
),
|
|
324
|
-
];
|
|
325
|
-
|
|
326
|
-
for (arguments, expected) in cases {
|
|
327
|
-
let commands: SearchCommands =
|
|
328
|
-
serde_json::from_str(arguments).expect("valid search command arguments");
|
|
329
|
-
assert_eq!(command_action(&commands), expected);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|