421 lines
15 KiB
Rust
421 lines
15 KiB
Rust
use std::collections::hash_map::DefaultHasher;
|
|
use std::hash::Hash;
|
|
use std::hash::Hasher;
|
|
use std::sync::Arc;
|
|
|
|
use akode_extension_api::ExtensionData;
|
|
use akode_extension_api::ExtensionMetrics;
|
|
use akode_extension_api::FunctionCallError;
|
|
use akode_extension_api::JsonToolOutput;
|
|
use akode_extension_api::ResponsesApiTool;
|
|
use akode_extension_api::ToolCall;
|
|
use akode_extension_api::ToolExecutor;
|
|
use akode_extension_api::ToolName;
|
|
use akode_extension_api::ToolOutput;
|
|
use akode_extension_api::ToolSpec;
|
|
use akode_extension_api::parse_tool_input_schema;
|
|
use akode_otel::SessionTelemetry;
|
|
use akode_otel::SkillInvocationEvent;
|
|
use akode_otel::SkillInvocationType as InvocationType;
|
|
use akode_otel::sanitize_metric_tag_value;
|
|
use akode_tools::ResponsesApiNamespace;
|
|
use akode_tools::ResponsesApiNamespaceTool;
|
|
use akode_tools::default_namespace_description;
|
|
use schemars::JsonSchema;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use serde_json::Value;
|
|
|
|
use crate::aliases::SkillLocator;
|
|
use crate::catalog::SkillAuthority;
|
|
use crate::catalog::SkillCatalogEntry;
|
|
use crate::catalog::SkillSourceKind;
|
|
use crate::catalog::SkillSourceLocation;
|
|
use crate::sources::SkillProviders;
|
|
use crate::state::SkillsSessionState;
|
|
use crate::state::SkillsThreadState;
|
|
use crate::step_state::SkillToolAuthoritySelector;
|
|
use crate::step_state::SkillsStepState;
|
|
use crate::telemetry::ActiveSkillTurnMetrics;
|
|
use crate::telemetry::SkillTurnMetrics;
|
|
|
|
mod installations;
|
|
mod list;
|
|
mod read;
|
|
mod schema;
|
|
|
|
const SKILLS_NAMESPACE: &str = "skills";
|
|
const MAX_HANDLE_BYTES: usize = 2_048;
|
|
const MAX_SKILL_RESPONSE_BYTES: usize = 512 * 1024;
|
|
|
|
pub(crate) fn skill_tools(
|
|
providers: SkillProviders,
|
|
session_store: &ExtensionData,
|
|
thread_store: &ExtensionData,
|
|
step_store: &ExtensionData,
|
|
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
|
|
let Some(thread_state) = thread_store.get::<SkillsThreadState>() else {
|
|
return Vec::new();
|
|
};
|
|
let Some(step_state) = step_store.get::<SkillsStepState>() else {
|
|
return Vec::new();
|
|
};
|
|
if !step_state.tools_enabled {
|
|
return Vec::new();
|
|
}
|
|
let invocations = SkillInvocationRecorder::from_stores(session_store, thread_store);
|
|
let metrics = step_store
|
|
.get::<Arc<dyn ExtensionMetrics>>()
|
|
.map(|metrics| Arc::clone(metrics.as_ref()));
|
|
let context = SkillToolContext {
|
|
thread_id: thread_store.level_id().to_string(),
|
|
providers,
|
|
thread_state,
|
|
invocations,
|
|
step_state,
|
|
};
|
|
let mut tools: Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> = vec![
|
|
Arc::new(list::ListTool {
|
|
context: context.clone(),
|
|
}),
|
|
Arc::new(read::ReadTool {
|
|
context: context.clone(),
|
|
metrics: metrics.clone(),
|
|
}),
|
|
];
|
|
if context.step_state.discovery_v2 {
|
|
tools.push(Arc::new(installations::InstallationsTool {
|
|
context,
|
|
metrics,
|
|
}));
|
|
}
|
|
tools
|
|
}
|
|
|
|
/// Records skill invocations to local logs and local metrics only.
|
|
#[derive(Clone)]
|
|
pub(crate) struct SkillInvocationRecorder {
|
|
telemetry: Option<Arc<SessionTelemetry>>,
|
|
metrics: Option<Arc<dyn ExtensionMetrics>>,
|
|
turn_metrics: Option<Arc<SkillTurnMetrics>>,
|
|
}
|
|
|
|
impl SkillInvocationRecorder {
|
|
pub(crate) fn from_stores(
|
|
session_store: &ExtensionData,
|
|
thread_store: &ExtensionData,
|
|
) -> Option<Self> {
|
|
Some(Self {
|
|
telemetry: session_store.get::<SessionTelemetry>(),
|
|
metrics: session_store
|
|
.get::<SkillsSessionState>()
|
|
.and_then(|state| state.extension_metrics.clone()),
|
|
// Code-mode callbacks retain these tools after another turn becomes active.
|
|
turn_metrics: thread_store
|
|
.get_or_init(ActiveSkillTurnMetrics::default)
|
|
.0
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
.upgrade(),
|
|
})
|
|
}
|
|
|
|
pub(crate) fn record_skill_invocation(
|
|
&self,
|
|
skill: &SkillCatalogEntry,
|
|
source: &SkillSourceLocation,
|
|
model: String,
|
|
turn_id: String,
|
|
invocation_type: InvocationType,
|
|
) {
|
|
let invocation = skill.invocation(source, invocation_type);
|
|
if let Some(telemetry) = &self.telemetry {
|
|
telemetry
|
|
.as_ref()
|
|
.clone()
|
|
.with_model(&model, &model)
|
|
.skill_invocation(SkillInvocationEvent {
|
|
turn_id: &turn_id,
|
|
skill_name: &skill.name,
|
|
scope: skill.invocation_scope,
|
|
plugin_id: invocation.plugin_id.as_deref(),
|
|
invocation_type,
|
|
});
|
|
}
|
|
let turn_metrics = self
|
|
.turn_metrics
|
|
.as_ref()
|
|
.filter(|turn| turn.turn_id == turn_id);
|
|
if let Some(turn_metrics) = &turn_metrics {
|
|
turn_metrics.record_plugin(invocation.plugin_id.as_deref());
|
|
}
|
|
if let Some(metrics) = &self.metrics {
|
|
let skill_name_tag = sanitize_metric_tag_value(skill.name.as_str());
|
|
let plugin_id_tag = sanitize_metric_tag_value(
|
|
invocation.plugin_id.as_deref().unwrap_or("unattributed"),
|
|
);
|
|
let model_slug_tag = sanitize_metric_tag_value(model.as_str());
|
|
let reasoning_effort = turn_metrics
|
|
.as_ref()
|
|
.map(|turn| turn.reasoning_effort.as_str())
|
|
.unwrap_or("unknown");
|
|
let invoke_type = match invocation_type {
|
|
InvocationType::Explicit => "explicit",
|
|
InvocationType::Implicit => "implicit",
|
|
};
|
|
metrics.counter(
|
|
"codex.skill.injected",
|
|
/*inc*/ 1,
|
|
&[
|
|
("status", "ok"),
|
|
("skill", skill_name_tag.as_str()),
|
|
("invoke_type", invoke_type),
|
|
("plugin_id", plugin_id_tag.as_str()),
|
|
("model_slug", model_slug_tag.as_str()),
|
|
("reasoning_effort", reasoning_effort),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct SkillToolContext {
|
|
thread_id: String,
|
|
providers: SkillProviders,
|
|
thread_state: Arc<SkillsThreadState>,
|
|
invocations: Option<SkillInvocationRecorder>,
|
|
step_state: Arc<SkillsStepState>,
|
|
}
|
|
|
|
impl SkillToolContext {
|
|
fn matching_packages<'a>(
|
|
&'a self,
|
|
package: &SkillLocator,
|
|
) -> Result<Vec<(&'a SkillCatalogEntry, SkillSourceLocation)>, FunctionCallError> {
|
|
let catalog = &self.step_state.catalog.merged;
|
|
let matching_entries = |resolved_package: &str, alias_source: Option<&SkillSourceKind>| {
|
|
catalog
|
|
.entries
|
|
.iter()
|
|
.filter_map(|entry| {
|
|
entry.source_locations().find_map(|source| {
|
|
if source.package.0 != resolved_package
|
|
|| alias_source.is_some_and(|kind| *kind != source.authority.kind)
|
|
{
|
|
return None;
|
|
}
|
|
SkillToolAuthority::from_authority(&source.authority)?;
|
|
Some((entry, source))
|
|
})
|
|
})
|
|
.collect::<Vec<_>>()
|
|
};
|
|
let locator = package.as_str();
|
|
let exact = matching_entries(locator, /*alias_source*/ None);
|
|
if !exact.is_empty() || locator.starts_with("skill://") {
|
|
return Ok(exact);
|
|
}
|
|
if let Some(source) = package.alias_source() {
|
|
let resolved = self.step_state.catalog.aliases.resolve(package);
|
|
return Ok(resolved.map_or_else(Vec::new, |id| matching_entries(&id, Some(source))));
|
|
}
|
|
|
|
let named = catalog
|
|
.entries
|
|
.iter()
|
|
.filter(|entry| entry.is_model_visible())
|
|
.filter(|entry| entry.name == locator || list::listed_name(&entry.name) == locator)
|
|
.filter_map(|entry| {
|
|
entry.source_locations().find_map(|source| {
|
|
SkillToolAuthority::from_authority(&source.authority)?;
|
|
Some((entry, source))
|
|
})
|
|
})
|
|
.collect::<Vec<_>>();
|
|
// Separate environments can each have an installation of the same package.
|
|
if let Some(((_, first), rest)) = named.split_first()
|
|
&& rest.iter().any(|(_, source)| {
|
|
source.authority != first.authority || source.package != first.package
|
|
})
|
|
{
|
|
return Err(FunctionCallError::RespondToModel(
|
|
"skill name is ambiguous; use skills.list and pass the package for the intended skill"
|
|
.to_string(),
|
|
));
|
|
}
|
|
Ok(named)
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize, Hash, JsonSchema)]
|
|
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
|
|
enum InstallationSource {
|
|
Executor { environment_id: String },
|
|
}
|
|
|
|
impl InstallationSource {
|
|
fn validate(&self) -> Result<(), FunctionCallError> {
|
|
let Self::Executor { environment_id } = self;
|
|
validate_handle("environment_id", environment_id, MAX_HANDLE_BYTES)
|
|
}
|
|
|
|
fn matches(&self, source: &SkillSourceLocation) -> bool {
|
|
source.enabled
|
|
&& match self {
|
|
Self::Executor { environment_id } => {
|
|
source.authority.kind == SkillSourceKind::Executor
|
|
&& source
|
|
.main_prompt
|
|
.environment_path()
|
|
.is_some_and(|(id, _)| id == environment_id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, Hash, JsonSchema, PartialEq, Serialize)]
|
|
#[serde(untagged)]
|
|
#[schemars(deny_unknown_fields)]
|
|
enum InstallationLocation {
|
|
Executor {
|
|
environment_id: String,
|
|
skill_root: String,
|
|
},
|
|
}
|
|
|
|
impl InstallationLocation {
|
|
fn from_source(source: &SkillSourceLocation) -> Option<Self> {
|
|
if !source.enabled {
|
|
return None;
|
|
}
|
|
match &source.authority.kind {
|
|
SkillSourceKind::Executor => {
|
|
let (environment_id, path) = source.main_prompt.environment_path()?;
|
|
Some(Self::Executor {
|
|
environment_id: environment_id.to_string(),
|
|
skill_root: path.parent()?.inferred_native_path_string(),
|
|
})
|
|
}
|
|
SkillSourceKind::Host | SkillSourceKind::Custom(_) => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize, Eq, Hash, JsonSchema, PartialEq, Serialize)]
|
|
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
|
|
pub(crate) enum SkillToolAuthority {
|
|
Executor { id: String },
|
|
}
|
|
|
|
impl SkillToolAuthority {
|
|
fn selector(&self) -> SkillToolAuthoritySelector {
|
|
match self {
|
|
Self::Executor { .. } => SkillToolAuthoritySelector::Executor,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn from_authority(authority: &SkillAuthority) -> Option<Self> {
|
|
match &authority.kind {
|
|
SkillSourceKind::Executor => Some(Self::Executor {
|
|
id: authority.id.clone(),
|
|
}),
|
|
SkillSourceKind::Host | SkillSourceKind::Custom(_) => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn skill_tool_name(name: &str) -> ToolName {
|
|
ToolName::namespaced(SKILLS_NAMESPACE, name)
|
|
}
|
|
|
|
fn skill_function_tool<I: JsonSchema, O: JsonSchema>(name: &str, description: &str) -> ToolSpec {
|
|
let tool = ResponsesApiTool {
|
|
name: name.to_string(),
|
|
description: description.to_string(),
|
|
strict: false,
|
|
defer_loading: None,
|
|
parameters: parse_tool_input_schema(&schema::input_schema_for::<I>())
|
|
.unwrap_or_else(|err| panic!("generated input schema for {name} should parse: {err}")),
|
|
output_schema: Some(schema::output_schema_for::<O>().into()),
|
|
};
|
|
|
|
ToolSpec::Namespace(ResponsesApiNamespace {
|
|
name: SKILLS_NAMESPACE.to_string(),
|
|
description: default_namespace_description(SKILLS_NAMESPACE),
|
|
tools: vec![ResponsesApiNamespaceTool::Function(tool)],
|
|
})
|
|
}
|
|
|
|
fn parse_args<T: for<'de> Deserialize<'de>>(call: &ToolCall<'_>) -> Result<T, FunctionCallError> {
|
|
let arguments = call.function_arguments()?;
|
|
let value = if arguments.trim().is_empty() {
|
|
Value::Object(serde_json::Map::new())
|
|
} else {
|
|
serde_json::from_str(arguments)
|
|
.map_err(|err| FunctionCallError::RespondToModel(err.to_string()))?
|
|
};
|
|
serde_json::from_value(value).map_err(|err| FunctionCallError::RespondToModel(err.to_string()))
|
|
}
|
|
|
|
fn validate_handle(name: &str, value: &str, max_bytes: usize) -> Result<(), FunctionCallError> {
|
|
if is_bounded_handle(value, max_bytes) {
|
|
return Ok(());
|
|
}
|
|
|
|
Err(FunctionCallError::RespondToModel(format!(
|
|
"{name} must be non-empty, contain no control characters, and be at most {max_bytes} bytes"
|
|
)))
|
|
}
|
|
|
|
fn is_bounded_handle(value: &str, max_bytes: usize) -> bool {
|
|
!value.is_empty() && value.len() <= max_bytes && !value.chars().any(char::is_control)
|
|
}
|
|
|
|
fn pagination_cursor(value: &(impl Hash + ?Sized), offset: usize) -> String {
|
|
format!("{:016x}:{offset}", value_fingerprint(value))
|
|
}
|
|
|
|
fn parse_pagination_cursor(
|
|
cursor: Option<&str>,
|
|
value: &(impl Hash + ?Sized),
|
|
tool: &str,
|
|
) -> Result<usize, FunctionCallError> {
|
|
let Some(cursor) = cursor else {
|
|
return Ok(0);
|
|
};
|
|
let invalid = || FunctionCallError::RespondToModel(format!("{tool} cursor is invalid"));
|
|
let (fingerprint, offset) = cursor.split_once(':').ok_or_else(invalid)?;
|
|
if u64::from_str_radix(fingerprint, 16).ok() != Some(value_fingerprint(value)) {
|
|
return Err(FunctionCallError::RespondToModel(format!(
|
|
"{tool} cursor is stale; restart from the first page"
|
|
)));
|
|
}
|
|
offset.parse::<usize>().map_err(|_| invalid())
|
|
}
|
|
|
|
fn value_fingerprint(value: &(impl Hash + ?Sized)) -> u64 {
|
|
let mut hasher = DefaultHasher::new();
|
|
value.hash(&mut hasher);
|
|
hasher.finish()
|
|
}
|
|
|
|
fn serialized_len(value: &impl Serialize) -> Result<usize, FunctionCallError> {
|
|
serde_json::to_vec(value)
|
|
.map(|value| value.len())
|
|
.map_err(|err| FunctionCallError::Fatal(err.to_string()))
|
|
}
|
|
|
|
fn skill_json_output<T: Serialize>(
|
|
value: &T,
|
|
authority: SkillToolAuthoritySelector,
|
|
) -> Result<Box<dyn ToolOutput>, FunctionCallError> {
|
|
let value = serde_json::to_value(value).map_err(|err| {
|
|
FunctionCallError::Fatal(format!("failed to serialize tool output: {err}"))
|
|
})?;
|
|
let output = JsonToolOutput::new(value);
|
|
Ok(match authority {
|
|
SkillToolAuthoritySelector::Executor => Box::new(output),
|
|
})
|
|
}
|