aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
136
akode-rs/core/src/model_search.rs
Normal file
136
akode-rs/core/src/model_search.rs
Normal file
@@ -0,0 +1,136 @@
|
||||
//! Resolve one search provider for the sampling step, preserving the host's search policy.
|
||||
//!
|
||||
//! The ChatGPT-hosted search connector is not available in aKode, so a model that prefers it
|
||||
//! has search disabled. Standalone (native) search and client-provided search MCP remain.
|
||||
|
||||
use akode_protocol::config_types::WebSearchMode;
|
||||
use akode_protocol::openai_models::WebSearchProvider;
|
||||
use serde_json::Map;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::session::step_settings::ResolvedStepSettings;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::turn_metadata::ExecutionMetadata;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum ModelSearch {
|
||||
ClientMcp,
|
||||
Native { mode: WebSearchMode },
|
||||
Disabled,
|
||||
}
|
||||
|
||||
fn object(value: Option<&Value>) -> Option<Map<String, Value>> {
|
||||
match value {
|
||||
None => Some(Map::new()),
|
||||
Some(Value::Object(map)) => Some(map.clone()),
|
||||
Some(Value::String(encoded)) => serde_json::from_str(encoded).ok(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::config::Config {
|
||||
// Read the scalar override from the highest enabled configuration layer. Hosts can
|
||||
// supply it through normal session flags without extending the shared Config struct.
|
||||
pub(crate) fn client_search_provider(&self) -> Option<WebSearchProvider> {
|
||||
self.config_layer_stack
|
||||
.layers_high_to_low()
|
||||
.find_map(|layer| layer.config.get("tools")?.get("client_search_provider"))?
|
||||
.clone()
|
||||
.try_into()
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub(crate) fn requires_native_search(&self) -> bool {
|
||||
let standalone = akode_features::Feature::StandaloneWebSearch;
|
||||
if !self.features.enabled(standalone) {
|
||||
return false;
|
||||
}
|
||||
let mut features = self.features.clone();
|
||||
features.disable(standalone).is_err() || features.enabled(standalone)
|
||||
}
|
||||
}
|
||||
|
||||
fn requested_mode(context: &Map<String, Value>, default: WebSearchMode) -> Option<WebSearchMode> {
|
||||
context.get("mode").map_or(Some(default), |value| {
|
||||
serde_json::from_value(value.clone()).ok()
|
||||
})
|
||||
}
|
||||
|
||||
// These host metadata fields do not select connectors. Preserve the installation ID
|
||||
// when narrowing discovery, and validate the search context separately for native.
|
||||
fn unrestricted_connectors(request: &Map<String, Value>) -> bool {
|
||||
request.keys().all(|key| {
|
||||
matches!(
|
||||
key.as_str(),
|
||||
"openai/search_context" | "openai/codex_installation_id"
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn native_search_mode(turn: &TurnContext, request: &Map<String, Value>) -> Option<WebSearchMode> {
|
||||
if !unrestricted_connectors(request) {
|
||||
return None;
|
||||
}
|
||||
let context = object(request.get("openai/search_context"))?;
|
||||
let configured = turn.config.web_search_mode.value();
|
||||
let mut mode = configured.restrict_to(requested_mode(&context, configured)?);
|
||||
// A host domain allowlist covers opens and clicks. Native filters only the query.
|
||||
for (key, value) in &context {
|
||||
match key.as_str() {
|
||||
"mode" | "telemetry_attributes" | "model_supported_tools" => {}
|
||||
"is_web_access_restricted" => {
|
||||
if value.as_bool()? {
|
||||
mode = mode.restrict_to(WebSearchMode::Cached);
|
||||
}
|
||||
}
|
||||
"session_persistent" if value.as_bool()? || turn.config.ephemeral => {}
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
turn.config.web_search_mode.can_set(&mode).ok()?;
|
||||
(mode != WebSearchMode::Disabled).then_some(mode)
|
||||
}
|
||||
|
||||
pub(crate) fn resolve(turn: &TurnContext, settings: &ResolvedStepSettings) -> Option<ModelSearch> {
|
||||
let native_required = turn.config.requires_native_search();
|
||||
let provider = if native_required {
|
||||
WebSearchProvider::Standalone
|
||||
} else if let Some(provider) = settings.model_info.web_search_provider {
|
||||
provider
|
||||
} else {
|
||||
match turn.config.client_search_provider()? {
|
||||
WebSearchProvider::SearchMcp => return Some(ModelSearch::ClientMcp),
|
||||
WebSearchProvider::Standalone => WebSearchProvider::Standalone,
|
||||
}
|
||||
};
|
||||
let observe = |decision, reason| {
|
||||
turn.session_telemetry.counter(
|
||||
"codex.model_search.discovery",
|
||||
/*inc*/ 1,
|
||||
&[("reason", reason)],
|
||||
);
|
||||
Some(decision)
|
||||
};
|
||||
let turn_meta = turn
|
||||
.turn_metadata_state
|
||||
.current_meta_value_for_mcp_request(ExecutionMetadata::from_settings(settings))
|
||||
.unwrap_or_else(|| json!({}));
|
||||
let Some(request) = object(turn_meta.get("mcp_request_meta")) else {
|
||||
return observe(ModelSearch::Disabled, "invalid_request_metadata");
|
||||
};
|
||||
let native_mode = native_search_mode(turn, &request);
|
||||
if native_required {
|
||||
return match native_mode {
|
||||
Some(mode) => observe(ModelSearch::Native { mode }, "native_required"),
|
||||
None => observe(ModelSearch::Disabled, "fallback_restricted"),
|
||||
};
|
||||
}
|
||||
match provider {
|
||||
WebSearchProvider::Standalone => {
|
||||
Some(native_mode.map_or(ModelSearch::Disabled, |mode| ModelSearch::Native { mode }))
|
||||
}
|
||||
// The hosted search connector is never connected in aKode.
|
||||
WebSearchProvider::SearchMcp => observe(ModelSearch::Disabled, "connector_unavailable"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user