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:
147
akode-rs/ext/guardian-v3/src/subscription_tools.rs
Normal file
147
akode-rs/ext/guardian-v3/src/subscription_tools.rs
Normal file
@@ -0,0 +1,147 @@
|
||||
//! Self-only subscription tools over the captured reviewer catalog.
|
||||
//! Shared schemas are narrowed to channel names; replies never reveal a later live post ID.
|
||||
|
||||
use crate::ReviewerSubscriptions;
|
||||
use akode_message_board::MAX_TOOL_ARGUMENT_KIB;
|
||||
use akode_message_board::MAX_TOOL_RESPONSE_BYTES;
|
||||
use akode_message_board::MessageBoardTool;
|
||||
use akode_message_board::SubscriptionChange;
|
||||
use akode_message_board::SubscriptionRequest;
|
||||
use akode_message_board::SubscriptionState;
|
||||
use akode_message_board::SubscriptionTarget;
|
||||
use akode_tools::FunctionCallError;
|
||||
use akode_tools::JsonToolOutput;
|
||||
use akode_tools::ResponsesApiNamespace;
|
||||
use akode_tools::ResponsesApiNamespaceTool;
|
||||
use akode_tools::ToolCall;
|
||||
use akode_tools::ToolExecutor;
|
||||
use akode_tools::ToolExecutorFuture;
|
||||
use akode_tools::ToolName;
|
||||
use akode_tools::ToolSpec;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Arguments {
|
||||
channel_name: String,
|
||||
}
|
||||
|
||||
struct SubscriptionTool {
|
||||
subscriptions: Arc<ReviewerSubscriptions>,
|
||||
change: SubscriptionChange,
|
||||
name: ToolName,
|
||||
spec: ToolSpec,
|
||||
}
|
||||
|
||||
pub(crate) fn tools(
|
||||
subscriptions: &Arc<ReviewerSubscriptions>,
|
||||
namespace: Option<&str>,
|
||||
namespace_description: &str,
|
||||
) -> Vec<Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>> {
|
||||
[
|
||||
(MessageBoardTool::Subscribe, SubscriptionChange::Subscribe),
|
||||
(MessageBoardTool::Unsubscribe, SubscriptionChange::Unsubscribe),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|(tool, change)| {
|
||||
let ToolSpec::Function(mut function) = tool.spec() else {
|
||||
unreachable!("shared subscription specs are functions")
|
||||
};
|
||||
function
|
||||
.parameters
|
||||
.properties
|
||||
.get_or_insert_default()
|
||||
.retain(|name, _| name == "channel_name");
|
||||
function.parameters.required = Some(vec!["channel_name".into()].into_boxed_slice());
|
||||
function.description = match change {
|
||||
SubscriptionChange::Subscribe => "Subscribe yourself to an evidence channel in the captured catalog. This changes future evidence selection, without reading posts or authorizing any action.",
|
||||
SubscriptionChange::Unsubscribe => "Unsubscribe yourself from an evidence channel in the captured catalog. Evidence already admitted to the reviewer conversation is retained.",
|
||||
}
|
||||
.into();
|
||||
let name = ToolName::new(namespace.map(str::to_owned), function.name.clone());
|
||||
let spec = match namespace {
|
||||
Some(namespace) => ToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: namespace.into(),
|
||||
description: namespace_description.into(),
|
||||
tools: vec![ResponsesApiNamespaceTool::Function(function)],
|
||||
}),
|
||||
None => ToolSpec::Function(function),
|
||||
};
|
||||
Arc::new(SubscriptionTool {
|
||||
subscriptions: Arc::clone(subscriptions),
|
||||
change,
|
||||
name,
|
||||
spec,
|
||||
}) as Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl<'call> ToolExecutor<ToolCall<'call>> for SubscriptionTool {
|
||||
fn tool_name(&self) -> ToolName {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
fn spec(&self) -> ToolSpec {
|
||||
self.spec.clone()
|
||||
}
|
||||
|
||||
fn handle<'a>(&'a self, call: ToolCall<'call>) -> ToolExecutorFuture<'a>
|
||||
where
|
||||
'call: 'a,
|
||||
{
|
||||
Box::pin(async move {
|
||||
let raw = call.function_arguments()?;
|
||||
if raw.len() > MAX_TOOL_ARGUMENT_KIB * 1024 {
|
||||
return Err(FunctionCallError::RespondToModel(format!(
|
||||
"subscription arguments exceed {MAX_TOOL_ARGUMENT_KIB} KiB"
|
||||
)));
|
||||
}
|
||||
let arguments: Arguments = serde_json::from_str(raw)
|
||||
.map_err(|error| FunctionCallError::RespondToModel(error.to_string()))?;
|
||||
let subscriptions = &self.subscriptions;
|
||||
let channel = subscriptions
|
||||
.view
|
||||
.channels()
|
||||
.iter()
|
||||
.find(|channel| channel.channel_name == arguments.channel_name)
|
||||
.ok_or_else(|| {
|
||||
FunctionCallError::RespondToModel(
|
||||
"channel is outside the captured catalog".into(),
|
||||
)
|
||||
})?;
|
||||
let response = serde_json::to_value(SubscriptionState {
|
||||
channel_name: channel.channel_name.clone(),
|
||||
thread_id: None,
|
||||
target_agent: subscriptions.path.clone(),
|
||||
enabled: self.change == SubscriptionChange::Subscribe,
|
||||
last_message_id: channel.last_message_id,
|
||||
})
|
||||
.map_err(|error| FunctionCallError::Fatal(error.to_string()))?;
|
||||
if response.to_string().len() > call.response_byte_budget(MAX_TOOL_RESPONSE_BYTES) {
|
||||
return Err(FunctionCallError::RespondToModel(
|
||||
"subscription response exceeds the tool-output budget".into(),
|
||||
));
|
||||
}
|
||||
// The ordinary backend operation owns atomic membership updates. Its live
|
||||
// last_message_id is deliberately not returned through this captured view.
|
||||
subscriptions
|
||||
.board
|
||||
.set_subscription(
|
||||
subscriptions.caller,
|
||||
SubscriptionRequest {
|
||||
target: SubscriptionTarget::Channel(arguments.channel_name),
|
||||
target_agent: None,
|
||||
change: self.change,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(|error| FunctionCallError::RespondToModel(error.to_string()))?;
|
||||
Ok(
|
||||
Box::new(JsonToolOutput::new(response).with_external_context())
|
||||
as Box<dyn akode_tools::ToolOutput>,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user