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:
94
akode-rs/ext/memories/src/tools/ad_hoc_note.rs
Normal file
94
akode-rs/ext/memories/src/tools/ad_hoc_note.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
use akode_extension_api::JsonToolOutput;
|
||||
use akode_extension_api::ToolCall;
|
||||
use akode_extension_api::ToolExecutor;
|
||||
use akode_extension_api::ToolName;
|
||||
use akode_extension_api::ToolSpec;
|
||||
use akode_otel::MetricsClient;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::ADD_AD_HOC_NOTE_TOOL_NAME;
|
||||
use crate::backend::AddAdHocMemoryNoteRequest;
|
||||
use crate::backend::AddAdHocMemoryNoteResponse;
|
||||
use crate::backend::MemoriesBackend;
|
||||
use crate::metrics::record_tool_call;
|
||||
|
||||
use super::backend_error_to_function_call;
|
||||
use super::memory_function_tool;
|
||||
use super::memory_tool_name;
|
||||
use super::parse_args;
|
||||
|
||||
#[derive(Deserialize, JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct AddAdHocNoteArgs {
|
||||
/// Name of the note file to create, in
|
||||
/// YYYY-MM-DDTHH-MM-SS-<slug>.md format. The slug must use only lowercase
|
||||
/// ASCII letters, digits, and hyphens.
|
||||
#[schemars(
|
||||
length(min = 24, max = 128),
|
||||
regex(pattern = r"^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-[a-z0-9][a-z0-9-]{0,79}\.md$")
|
||||
)]
|
||||
filename: String,
|
||||
/// Verbatim Markdown note to append to the ad-hoc memory notes.
|
||||
#[schemars(length(min = 1))]
|
||||
note: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct AddAdHocNoteTool<B> {
|
||||
pub(super) backend: B,
|
||||
pub(super) metrics_client: Option<MetricsClient>,
|
||||
}
|
||||
|
||||
impl<'call, B> ToolExecutor<ToolCall<'call>> for AddAdHocNoteTool<B>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
fn tool_name(&self) -> ToolName {
|
||||
memory_tool_name(ADD_AD_HOC_NOTE_TOOL_NAME)
|
||||
}
|
||||
|
||||
fn spec(&self) -> ToolSpec {
|
||||
memory_function_tool::<AddAdHocNoteArgs, AddAdHocMemoryNoteResponse>(
|
||||
ADD_AD_HOC_NOTE_TOOL_NAME,
|
||||
"Create one append-only ad-hoc memory note after the user explicitly asks Codex to remember, forget, or update something.",
|
||||
)
|
||||
}
|
||||
|
||||
fn handle<'a>(&'a self, call: ToolCall<'call>) -> akode_extension_api::ToolExecutorFuture<'a>
|
||||
where
|
||||
'call: 'a,
|
||||
{
|
||||
Box::pin(self.handle_call(call))
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> AddAdHocNoteTool<B>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
async fn handle_call(
|
||||
&self,
|
||||
call: ToolCall<'_>,
|
||||
) -> Result<Box<dyn akode_extension_api::ToolOutput>, akode_extension_api::FunctionCallError>
|
||||
{
|
||||
let backend = self.backend.clone();
|
||||
let args: AddAdHocNoteArgs = parse_args(&call)?;
|
||||
let response = backend
|
||||
.add_ad_hoc_note(AddAdHocMemoryNoteRequest {
|
||||
filename: args.filename,
|
||||
note: args.note,
|
||||
})
|
||||
.await;
|
||||
record_tool_call(
|
||||
self.metrics_client.as_ref(),
|
||||
ADD_AD_HOC_NOTE_TOOL_NAME,
|
||||
"ad_hoc_notes",
|
||||
response.is_ok(),
|
||||
"not_applicable",
|
||||
);
|
||||
let response = response.map_err(backend_error_to_function_call)?;
|
||||
Ok(Box::new(JsonToolOutput::new(json!(response))))
|
||||
}
|
||||
}
|
||||
102
akode-rs/ext/memories/src/tools/list.rs
Normal file
102
akode-rs/ext/memories/src/tools/list.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
use akode_extension_api::JsonToolOutput;
|
||||
use akode_extension_api::ToolCall;
|
||||
use akode_extension_api::ToolExecutor;
|
||||
use akode_extension_api::ToolName;
|
||||
use akode_extension_api::ToolSpec;
|
||||
use akode_otel::MetricsClient;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::DEFAULT_LIST_MAX_RESULTS;
|
||||
use crate::LIST_TOOL_NAME;
|
||||
use crate::MAX_LIST_RESULTS;
|
||||
use crate::backend::ListMemoriesRequest;
|
||||
use crate::backend::ListMemoriesResponse;
|
||||
use crate::backend::MemoriesBackend;
|
||||
use crate::metrics::record_tool_call;
|
||||
use crate::metrics::scope_from_optional_path;
|
||||
use crate::metrics::truncated_tag;
|
||||
|
||||
use super::backend_error_to_function_call;
|
||||
use super::clamp_max_results;
|
||||
use super::memory_function_tool;
|
||||
use super::memory_tool_name;
|
||||
use super::parse_args;
|
||||
|
||||
#[derive(Deserialize, JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ListArgs {
|
||||
path: Option<String>,
|
||||
cursor: Option<String>,
|
||||
#[schemars(range(min = 1))]
|
||||
max_results: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct ListTool<B> {
|
||||
pub(super) backend: B,
|
||||
pub(super) metrics_client: Option<MetricsClient>,
|
||||
}
|
||||
|
||||
impl<'call, B> ToolExecutor<ToolCall<'call>> for ListTool<B>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
fn tool_name(&self) -> ToolName {
|
||||
memory_tool_name(LIST_TOOL_NAME)
|
||||
}
|
||||
|
||||
fn supports_parallel_tool_calls(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn spec(&self) -> ToolSpec {
|
||||
memory_function_tool::<ListArgs, ListMemoriesResponse>(
|
||||
LIST_TOOL_NAME,
|
||||
"List immediate files and directories under a path in the Codex memories store.",
|
||||
)
|
||||
}
|
||||
|
||||
fn handle<'a>(&'a self, call: ToolCall<'call>) -> akode_extension_api::ToolExecutorFuture<'a>
|
||||
where
|
||||
'call: 'a,
|
||||
{
|
||||
Box::pin(self.handle_call(call))
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> ListTool<B>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
async fn handle_call(
|
||||
&self,
|
||||
call: ToolCall<'_>,
|
||||
) -> Result<Box<dyn akode_extension_api::ToolOutput>, akode_extension_api::FunctionCallError>
|
||||
{
|
||||
let backend = self.backend.clone();
|
||||
let args: ListArgs = parse_args(&call)?;
|
||||
let scope = scope_from_optional_path(args.path.as_deref(), "root");
|
||||
let response = backend
|
||||
.list(ListMemoriesRequest {
|
||||
path: args.path,
|
||||
cursor: args.cursor,
|
||||
max_results: clamp_max_results(
|
||||
args.max_results,
|
||||
DEFAULT_LIST_MAX_RESULTS,
|
||||
MAX_LIST_RESULTS,
|
||||
),
|
||||
})
|
||||
.await;
|
||||
record_tool_call(
|
||||
self.metrics_client.as_ref(),
|
||||
LIST_TOOL_NAME,
|
||||
scope,
|
||||
response.is_ok(),
|
||||
truncated_tag(response.as_ref().ok().map(|response| response.truncated)),
|
||||
);
|
||||
let response = response.map_err(backend_error_to_function_call)?;
|
||||
Ok(Box::new(JsonToolOutput::new(json!(response))))
|
||||
}
|
||||
}
|
||||
113
akode-rs/ext/memories/src/tools/mod.rs
Normal file
113
akode-rs/ext/memories/src/tools/mod.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use akode_extension_api::FunctionCallError;
|
||||
use akode_extension_api::ResponsesApiTool;
|
||||
use akode_extension_api::ToolCall;
|
||||
use akode_extension_api::ToolExecutor;
|
||||
use akode_extension_api::ToolName;
|
||||
use akode_extension_api::ToolSpec;
|
||||
use akode_extension_api::parse_tool_input_schema;
|
||||
use akode_otel::MetricsClient;
|
||||
use akode_tools::ResponsesApiNamespace;
|
||||
use akode_tools::ResponsesApiNamespaceTool;
|
||||
use akode_tools::default_namespace_description;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::MEMORY_TOOLS_NAMESPACE;
|
||||
use crate::backend::MemoriesBackend;
|
||||
use crate::backend::MemoriesBackendError;
|
||||
use crate::schema;
|
||||
|
||||
mod ad_hoc_note;
|
||||
mod list;
|
||||
mod read;
|
||||
mod search;
|
||||
|
||||
pub(crate) fn memory_tools<B>(
|
||||
backend: B,
|
||||
metrics_client: Option<MetricsClient>,
|
||||
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
vec![
|
||||
Arc::new(ad_hoc_note::AddAdHocNoteTool {
|
||||
backend: backend.clone(),
|
||||
metrics_client: metrics_client.clone(),
|
||||
}),
|
||||
Arc::new(list::ListTool {
|
||||
backend: backend.clone(),
|
||||
metrics_client: metrics_client.clone(),
|
||||
}),
|
||||
Arc::new(read::ReadTool {
|
||||
backend: backend.clone(),
|
||||
metrics_client: metrics_client.clone(),
|
||||
}),
|
||||
Arc::new(search::SearchTool {
|
||||
backend,
|
||||
metrics_client,
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
pub(super) fn memory_tool_name(name: &str) -> ToolName {
|
||||
ToolName::namespaced(MEMORY_TOOLS_NAMESPACE, name)
|
||||
}
|
||||
|
||||
pub(super) fn memory_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: MEMORY_TOOLS_NAMESPACE.to_string(),
|
||||
description: default_namespace_description(MEMORY_TOOLS_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 clamp_max_results(requested: Option<usize>, default: usize, max: usize) -> usize {
|
||||
requested.unwrap_or(default).clamp(1, max)
|
||||
}
|
||||
|
||||
fn backend_error_to_function_call(err: MemoriesBackendError) -> FunctionCallError {
|
||||
match err {
|
||||
MemoriesBackendError::InvalidPath { .. }
|
||||
| MemoriesBackendError::InvalidCursor { .. }
|
||||
| MemoriesBackendError::InvalidFilename { .. }
|
||||
| MemoriesBackendError::NotFound { .. }
|
||||
| MemoriesBackendError::InvalidLineOffset
|
||||
| MemoriesBackendError::InvalidMaxLines
|
||||
| MemoriesBackendError::LineOffsetExceedsFileLength
|
||||
| MemoriesBackendError::NotFile { .. }
|
||||
| MemoriesBackendError::EmptyQuery
|
||||
| MemoriesBackendError::EmptyAdHocNote
|
||||
| MemoriesBackendError::AdHocNoteAlreadyExists { .. }
|
||||
| MemoriesBackendError::InvalidMatchWindow => {
|
||||
FunctionCallError::RespondToModel(err.to_string())
|
||||
}
|
||||
MemoriesBackendError::Io(_) => FunctionCallError::Fatal(err.to_string()),
|
||||
}
|
||||
}
|
||||
99
akode-rs/ext/memories/src/tools/read.rs
Normal file
99
akode-rs/ext/memories/src/tools/read.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
use akode_extension_api::JsonToolOutput;
|
||||
use akode_extension_api::ToolCall;
|
||||
use akode_extension_api::ToolExecutor;
|
||||
use akode_extension_api::ToolName;
|
||||
use akode_extension_api::ToolSpec;
|
||||
use akode_otel::MetricsClient;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::DEFAULT_READ_MAX_TOKENS;
|
||||
use crate::READ_TOOL_NAME;
|
||||
use crate::backend::MemoriesBackend;
|
||||
use crate::backend::ReadMemoryRequest;
|
||||
use crate::backend::ReadMemoryResponse;
|
||||
use crate::metrics::record_tool_call;
|
||||
use crate::metrics::scope_from_path;
|
||||
use crate::metrics::truncated_tag;
|
||||
|
||||
use super::backend_error_to_function_call;
|
||||
use super::memory_function_tool;
|
||||
use super::memory_tool_name;
|
||||
use super::parse_args;
|
||||
|
||||
#[derive(Deserialize, JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ReadArgs {
|
||||
path: String,
|
||||
#[schemars(range(min = 1))]
|
||||
line_offset: Option<usize>,
|
||||
#[schemars(range(min = 1))]
|
||||
max_lines: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct ReadTool<B> {
|
||||
pub(super) backend: B,
|
||||
pub(super) metrics_client: Option<MetricsClient>,
|
||||
}
|
||||
|
||||
impl<'call, B> ToolExecutor<ToolCall<'call>> for ReadTool<B>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
fn tool_name(&self) -> ToolName {
|
||||
memory_tool_name(READ_TOOL_NAME)
|
||||
}
|
||||
|
||||
fn supports_parallel_tool_calls(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn spec(&self) -> ToolSpec {
|
||||
memory_function_tool::<ReadArgs, ReadMemoryResponse>(
|
||||
READ_TOOL_NAME,
|
||||
"Read a Codex memory file by relative path, optionally starting at a 1-indexed line offset and limiting the number of lines returned.",
|
||||
)
|
||||
}
|
||||
|
||||
fn handle<'a>(&'a self, call: ToolCall<'call>) -> akode_extension_api::ToolExecutorFuture<'a>
|
||||
where
|
||||
'call: 'a,
|
||||
{
|
||||
Box::pin(self.handle_call(call))
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> ReadTool<B>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
async fn handle_call(
|
||||
&self,
|
||||
call: ToolCall<'_>,
|
||||
) -> Result<Box<dyn akode_extension_api::ToolOutput>, akode_extension_api::FunctionCallError>
|
||||
{
|
||||
let backend = self.backend.clone();
|
||||
let args: ReadArgs = parse_args(&call)?;
|
||||
let path = args.path;
|
||||
let scope = scope_from_path(path.as_str());
|
||||
let response = backend
|
||||
.read(ReadMemoryRequest {
|
||||
path: path.clone(),
|
||||
line_offset: args.line_offset.unwrap_or(1),
|
||||
max_lines: args.max_lines,
|
||||
max_tokens: DEFAULT_READ_MAX_TOKENS,
|
||||
})
|
||||
.await;
|
||||
record_tool_call(
|
||||
self.metrics_client.as_ref(),
|
||||
READ_TOOL_NAME,
|
||||
scope,
|
||||
response.is_ok(),
|
||||
truncated_tag(response.as_ref().ok().map(|response| response.truncated)),
|
||||
);
|
||||
let response = response.map_err(backend_error_to_function_call)?;
|
||||
Ok(Box::new(JsonToolOutput::new(json!(response))))
|
||||
}
|
||||
}
|
||||
119
akode-rs/ext/memories/src/tools/search.rs
Normal file
119
akode-rs/ext/memories/src/tools/search.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use akode_extension_api::JsonToolOutput;
|
||||
use akode_extension_api::ToolCall;
|
||||
use akode_extension_api::ToolExecutor;
|
||||
use akode_extension_api::ToolName;
|
||||
use akode_extension_api::ToolSpec;
|
||||
use akode_otel::MetricsClient;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::DEFAULT_SEARCH_MAX_RESULTS;
|
||||
use crate::MAX_SEARCH_RESULTS;
|
||||
use crate::SEARCH_TOOL_NAME;
|
||||
use crate::backend::MemoriesBackend;
|
||||
use crate::backend::SearchMatchMode;
|
||||
use crate::backend::SearchMemoriesRequest;
|
||||
use crate::backend::SearchMemoriesResponse;
|
||||
use crate::metrics::record_tool_call;
|
||||
use crate::metrics::scope_from_optional_path;
|
||||
use crate::metrics::truncated_tag;
|
||||
|
||||
use super::backend_error_to_function_call;
|
||||
use super::clamp_max_results;
|
||||
use super::memory_function_tool;
|
||||
use super::memory_tool_name;
|
||||
use super::parse_args;
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct SearchArgs {
|
||||
#[schemars(length(min = 1))]
|
||||
queries: Vec<String>,
|
||||
match_mode: Option<SearchMatchMode>,
|
||||
path: Option<String>,
|
||||
cursor: Option<String>,
|
||||
#[schemars(range(min = 0))]
|
||||
context_lines: Option<usize>,
|
||||
case_sensitive: Option<bool>,
|
||||
normalized: Option<bool>,
|
||||
#[schemars(range(min = 1))]
|
||||
max_results: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct SearchTool<B> {
|
||||
pub(super) backend: B,
|
||||
pub(super) metrics_client: Option<MetricsClient>,
|
||||
}
|
||||
|
||||
impl<'call, B> ToolExecutor<ToolCall<'call>> for SearchTool<B>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
fn tool_name(&self) -> ToolName {
|
||||
memory_tool_name(SEARCH_TOOL_NAME)
|
||||
}
|
||||
|
||||
fn supports_parallel_tool_calls(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn spec(&self) -> ToolSpec {
|
||||
memory_function_tool::<SearchArgs, SearchMemoriesResponse>(
|
||||
SEARCH_TOOL_NAME,
|
||||
"Search Codex memory files for substring matches, optionally normalizing separators or requiring all query substrings on the same line or within a line window.",
|
||||
)
|
||||
}
|
||||
|
||||
fn handle<'a>(&'a self, call: ToolCall<'call>) -> akode_extension_api::ToolExecutorFuture<'a>
|
||||
where
|
||||
'call: 'a,
|
||||
{
|
||||
Box::pin(self.handle_call(call))
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> SearchTool<B>
|
||||
where
|
||||
B: MemoriesBackend,
|
||||
{
|
||||
async fn handle_call(
|
||||
&self,
|
||||
call: ToolCall<'_>,
|
||||
) -> Result<Box<dyn akode_extension_api::ToolOutput>, akode_extension_api::FunctionCallError>
|
||||
{
|
||||
let backend = self.backend.clone();
|
||||
let args: SearchArgs = parse_args(&call)?;
|
||||
let scope = scope_from_optional_path(args.path.as_deref(), "all");
|
||||
let response = backend.search(args.into_request()).await;
|
||||
record_tool_call(
|
||||
self.metrics_client.as_ref(),
|
||||
SEARCH_TOOL_NAME,
|
||||
scope,
|
||||
response.is_ok(),
|
||||
truncated_tag(response.as_ref().ok().map(|response| response.truncated)),
|
||||
);
|
||||
let response = response.map_err(backend_error_to_function_call)?;
|
||||
Ok(Box::new(JsonToolOutput::new(json!(response))))
|
||||
}
|
||||
}
|
||||
|
||||
impl SearchArgs {
|
||||
fn into_request(self) -> SearchMemoriesRequest {
|
||||
SearchMemoriesRequest {
|
||||
queries: self.queries,
|
||||
match_mode: self.match_mode.unwrap_or(SearchMatchMode::Any),
|
||||
path: self.path,
|
||||
cursor: self.cursor,
|
||||
context_lines: self.context_lines.unwrap_or(0),
|
||||
case_sensitive: self.case_sensitive.unwrap_or(true),
|
||||
normalized: self.normalized.unwrap_or(false),
|
||||
max_results: clamp_max_results(
|
||||
self.max_results,
|
||||
DEFAULT_SEARCH_MAX_RESULTS,
|
||||
MAX_SEARCH_RESULTS,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user