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:
531
akode-rs/ext/goal/src/tool.rs
Normal file
531
akode-rs/ext/goal/src/tool.rs
Normal file
@@ -0,0 +1,531 @@
|
||||
use akode_core::ThreadManager;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Weak;
|
||||
|
||||
use akode_extension_api::FunctionCallError;
|
||||
use akode_extension_api::JsonToolOutput;
|
||||
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_protocol::ThreadId;
|
||||
use akode_protocol::protocol::ThreadGoal;
|
||||
use akode_protocol::protocol::ThreadGoalStatus;
|
||||
use akode_protocol::protocol::validate_thread_goal_objective;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::accounting::BudgetLimitedGoalDisposition;
|
||||
use crate::accounting::GoalAccountingState;
|
||||
use crate::events::GoalEventEmitter;
|
||||
use crate::metrics::GoalMetrics;
|
||||
use crate::runtime::GoalRuntimeHandle;
|
||||
use crate::spec::CREATE_GOAL_TOOL_NAME;
|
||||
use crate::spec::GET_GOAL_TOOL_NAME;
|
||||
use crate::spec::UPDATE_GOAL_TOOL_NAME;
|
||||
use crate::spec::create_create_goal_tool;
|
||||
use crate::spec::create_get_goal_tool;
|
||||
use crate::spec::create_update_goal_tool;
|
||||
use crate::steering::GOAL_OBJECTIVE_CONTEXT;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct GoalToolExecutor {
|
||||
pub(crate) execution_allowed: bool,
|
||||
kind: GoalToolKind,
|
||||
thread_id: ThreadId,
|
||||
state_db: Arc<akode_state::StateRuntime>,
|
||||
accounting_state: Arc<GoalAccountingState>,
|
||||
event_emitter: GoalEventEmitter,
|
||||
metrics: GoalMetrics,
|
||||
max_goal_token_budget: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum GoalToolKind {
|
||||
Get,
|
||||
Create(Weak<ThreadManager>),
|
||||
Update,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub struct CreateGoalRequest {
|
||||
pub objective: String,
|
||||
pub token_budget: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
struct UpdateGoalArgs {
|
||||
status: ThreadGoalStatus,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct GoalToolResponse {
|
||||
goal: Option<ThreadGoal>,
|
||||
remaining_tokens: Option<i64>,
|
||||
completion_budget_report: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum CompletionBudgetReport {
|
||||
Include,
|
||||
Omit,
|
||||
}
|
||||
|
||||
impl GoalToolExecutor {
|
||||
pub(crate) fn get(
|
||||
thread_id: ThreadId,
|
||||
state_db: Arc<akode_state::StateRuntime>,
|
||||
accounting_state: Arc<GoalAccountingState>,
|
||||
event_emitter: GoalEventEmitter,
|
||||
metrics: GoalMetrics,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: GoalToolKind::Get,
|
||||
execution_allowed: true,
|
||||
thread_id,
|
||||
state_db,
|
||||
accounting_state,
|
||||
event_emitter,
|
||||
metrics,
|
||||
max_goal_token_budget: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(
|
||||
runtime: &GoalRuntimeHandle,
|
||||
thread_manager: Weak<ThreadManager>,
|
||||
state_db: Arc<akode_state::StateRuntime>,
|
||||
event_emitter: GoalEventEmitter,
|
||||
metrics: GoalMetrics,
|
||||
max_goal_token_budget: Option<i64>,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: GoalToolKind::Create(thread_manager),
|
||||
execution_allowed: true,
|
||||
thread_id: runtime.thread_id(),
|
||||
state_db,
|
||||
accounting_state: runtime.accounting_state(),
|
||||
event_emitter,
|
||||
metrics,
|
||||
max_goal_token_budget,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update(
|
||||
thread_id: ThreadId,
|
||||
state_db: Arc<akode_state::StateRuntime>,
|
||||
accounting_state: Arc<GoalAccountingState>,
|
||||
event_emitter: GoalEventEmitter,
|
||||
metrics: GoalMetrics,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: GoalToolKind::Update,
|
||||
execution_allowed: true,
|
||||
thread_id,
|
||||
state_db,
|
||||
accounting_state,
|
||||
event_emitter,
|
||||
metrics,
|
||||
max_goal_token_budget: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'call> ToolExecutor<ToolCall<'call>> for GoalToolExecutor {
|
||||
fn tool_name(&self) -> ToolName {
|
||||
ToolName::plain(match self.kind {
|
||||
GoalToolKind::Get => GET_GOAL_TOOL_NAME,
|
||||
GoalToolKind::Create(_) => CREATE_GOAL_TOOL_NAME,
|
||||
GoalToolKind::Update => UPDATE_GOAL_TOOL_NAME,
|
||||
})
|
||||
}
|
||||
|
||||
fn supports_parallel_tool_calls(&self) -> bool {
|
||||
matches!(self.kind, GoalToolKind::Get)
|
||||
}
|
||||
|
||||
fn spec(&self) -> ToolSpec {
|
||||
match self.kind {
|
||||
GoalToolKind::Get => create_get_goal_tool(),
|
||||
GoalToolKind::Create(_) => create_create_goal_tool(),
|
||||
GoalToolKind::Update => create_update_goal_tool(),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle<'a>(
|
||||
&'a self,
|
||||
invocation: ToolCall<'call>,
|
||||
) -> akode_extension_api::ToolExecutorFuture<'a>
|
||||
where
|
||||
'call: 'a,
|
||||
{
|
||||
Box::pin(async move {
|
||||
if !self.execution_allowed {
|
||||
return Err(FunctionCallError::RespondToModel(
|
||||
"Goal tools require a persistent thread.".to_string(),
|
||||
));
|
||||
}
|
||||
match &self.kind {
|
||||
GoalToolKind::Get => self.handle_get(invocation).await,
|
||||
GoalToolKind::Create(thread_manager) => {
|
||||
self.handle_create(invocation, thread_manager).await
|
||||
}
|
||||
GoalToolKind::Update => self.handle_update(invocation).await,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl GoalToolExecutor {
|
||||
async fn handle_get(
|
||||
&self,
|
||||
invocation: ToolCall<'_>,
|
||||
) -> Result<Box<dyn ToolOutput>, FunctionCallError> {
|
||||
let _ = invocation.function_arguments()?;
|
||||
let goal = self
|
||||
.state_db
|
||||
.thread_goals()
|
||||
.get_thread_goal(self.thread_id)
|
||||
.await
|
||||
.map(|goal| goal.map(protocol_goal_from_state))
|
||||
.map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to read goal: {err}"))
|
||||
})?;
|
||||
goal_response(goal, CompletionBudgetReport::Omit)
|
||||
}
|
||||
|
||||
async fn handle_create(
|
||||
&self,
|
||||
invocation: ToolCall<'_>,
|
||||
thread_manager: &Weak<ThreadManager>,
|
||||
) -> Result<Box<dyn ToolOutput>, FunctionCallError> {
|
||||
let mut request: CreateGoalRequest = parse_arguments(invocation.function_arguments()?)?;
|
||||
request.objective = request.objective.trim().to_string();
|
||||
validate_thread_goal_objective(&request.objective)
|
||||
.map_err(FunctionCallError::RespondToModel)?;
|
||||
request.token_budget = request.token_budget.or(self.max_goal_token_budget);
|
||||
validate_goal_budget(request.token_budget, self.max_goal_token_budget)
|
||||
.map_err(FunctionCallError::RespondToModel)?;
|
||||
|
||||
let goal = self
|
||||
.state_db
|
||||
.thread_goals()
|
||||
.insert_thread_goal(
|
||||
self.thread_id,
|
||||
request.objective.as_str(),
|
||||
akode_state::ThreadGoalStatus::Active,
|
||||
request.token_budget,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| FunctionCallError::RespondToModel(format!("failed to create goal: {err}")))?
|
||||
.ok_or_else(|| {
|
||||
FunctionCallError::RespondToModel(
|
||||
"cannot create a new goal because this thread has an unfinished goal; complete the existing goal first"
|
||||
.to_string(),
|
||||
)
|
||||
})?;
|
||||
fill_empty_thread_preview_if_possible(self.state_db.as_ref(), self.thread_id, &goal).await;
|
||||
let turn_id = self
|
||||
.accounting_state
|
||||
.mark_current_turn_goal_active(goal.goal_id.clone());
|
||||
if let Some(manager) = thread_manager.upgrade()
|
||||
&& let Ok(thread) = manager.get_thread(self.thread_id).await
|
||||
{
|
||||
thread
|
||||
.inject_untrusted_context_for_turn(
|
||||
&invocation.turn_id,
|
||||
GOAL_OBJECTIVE_CONTEXT,
|
||||
goal.objective.clone(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
self.metrics.record_created();
|
||||
let goal = protocol_goal_from_state(goal);
|
||||
self.emit_goal_updated_from_tool_call(&invocation, turn_id, goal.clone());
|
||||
goal_response(Some(goal), CompletionBudgetReport::Omit)
|
||||
}
|
||||
|
||||
async fn handle_update(
|
||||
&self,
|
||||
invocation: ToolCall<'_>,
|
||||
) -> Result<Box<dyn ToolOutput>, FunctionCallError> {
|
||||
let args: UpdateGoalArgs = parse_arguments(invocation.function_arguments()?)?;
|
||||
if !matches!(
|
||||
args.status,
|
||||
ThreadGoalStatus::Complete | ThreadGoalStatus::Blocked | ThreadGoalStatus::Paused
|
||||
) {
|
||||
return Err(FunctionCallError::RespondToModel(
|
||||
"update_goal can only mark the existing goal complete, blocked, or paused at the user's explicit request; resume, budget-limited, and usage-limited status changes are controlled by the user or system"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
self.account_active_goal_progress(
|
||||
match args.status {
|
||||
ThreadGoalStatus::Complete => akode_state::GoalAccountingMode::ActiveOrComplete,
|
||||
ThreadGoalStatus::Blocked | ThreadGoalStatus::Paused => {
|
||||
akode_state::GoalAccountingMode::ActiveOrStopped
|
||||
}
|
||||
ThreadGoalStatus::Active
|
||||
| ThreadGoalStatus::UsageLimited
|
||||
| ThreadGoalStatus::BudgetLimited => unreachable!("status validated above"),
|
||||
},
|
||||
invocation.call_id.as_str(),
|
||||
BudgetLimitedGoalDisposition::ClearActive,
|
||||
)
|
||||
.await?;
|
||||
let previous_status = self
|
||||
.current_goal_status_for_metrics(/*expected_goal_id*/ None)
|
||||
.await?;
|
||||
let goal = self
|
||||
.state_db
|
||||
.thread_goals()
|
||||
.update_thread_goal(
|
||||
self.thread_id,
|
||||
akode_state::GoalUpdate {
|
||||
objective: None,
|
||||
status: Some(state_status_from_protocol(args.status)),
|
||||
token_budget: None,
|
||||
expected_goal_id: None,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to update goal: {err}"))
|
||||
})?
|
||||
.ok_or_else(|| {
|
||||
FunctionCallError::RespondToModel(
|
||||
"cannot update goal because this thread has no goal".to_string(),
|
||||
)
|
||||
})?;
|
||||
self.metrics
|
||||
.record_terminal_if_status_changed(previous_status, &goal);
|
||||
let goal = protocol_goal_from_state(goal);
|
||||
let turn_id = self.accounting_state.clear_current_turn_goal();
|
||||
self.emit_goal_updated_from_tool_call(&invocation, turn_id, goal.clone());
|
||||
goal_response(
|
||||
Some(goal),
|
||||
if args.status == ThreadGoalStatus::Complete {
|
||||
CompletionBudgetReport::Include
|
||||
} else {
|
||||
CompletionBudgetReport::Omit
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn emit_goal_updated_from_tool_call(
|
||||
&self,
|
||||
invocation: &ToolCall<'_>,
|
||||
turn_id: Option<String>,
|
||||
goal: ThreadGoal,
|
||||
) {
|
||||
self.event_emitter
|
||||
.thread_goal_updated(invocation.call_id.clone(), turn_id, goal);
|
||||
}
|
||||
|
||||
async fn account_active_goal_progress(
|
||||
&self,
|
||||
mode: akode_state::GoalAccountingMode,
|
||||
event_id: &str,
|
||||
budget_limited_goal_disposition: BudgetLimitedGoalDisposition,
|
||||
) -> Result<Option<ThreadGoal>, FunctionCallError> {
|
||||
let Some(turn_id) = self.accounting_state.current_turn_id() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let _accounting_permit = self
|
||||
.accounting_state
|
||||
.progress_accounting_permit()
|
||||
.await
|
||||
.map_err(|err| {
|
||||
FunctionCallError::Fatal(format!(
|
||||
"goal progress accounting semaphore closed: {err}"
|
||||
))
|
||||
})?;
|
||||
let Some(snapshot) = self.accounting_state.progress_snapshot(turn_id.as_str()) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let previous_status = self
|
||||
.current_goal_status_for_metrics(Some(snapshot.expected_goal_id.as_str()))
|
||||
.await?;
|
||||
let outcome = self
|
||||
.state_db
|
||||
.thread_goals()
|
||||
.account_thread_goal_usage(
|
||||
self.thread_id,
|
||||
snapshot.time_delta_seconds,
|
||||
snapshot.token_delta,
|
||||
mode,
|
||||
Some(snapshot.expected_goal_id.as_str()),
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!("failed to account goal progress: {err}"))
|
||||
})?;
|
||||
Ok(match outcome {
|
||||
akode_state::GoalAccountingOutcome::Updated(goal) => {
|
||||
self.metrics
|
||||
.record_terminal_if_status_changed(previous_status, &goal);
|
||||
self.accounting_state.mark_progress_accounted_for_status(
|
||||
turn_id.as_str(),
|
||||
&snapshot,
|
||||
goal.status,
|
||||
budget_limited_goal_disposition,
|
||||
);
|
||||
let goal = protocol_goal_from_state(goal);
|
||||
self.event_emitter.thread_goal_updated(
|
||||
event_id.to_string(),
|
||||
Some(turn_id),
|
||||
goal.clone(),
|
||||
);
|
||||
Some(goal)
|
||||
}
|
||||
akode_state::GoalAccountingOutcome::Unchanged(_) => None,
|
||||
})
|
||||
}
|
||||
|
||||
async fn current_goal_status_for_metrics(
|
||||
&self,
|
||||
expected_goal_id: Option<&str>,
|
||||
) -> Result<Option<akode_state::ThreadGoalStatus>, FunctionCallError> {
|
||||
let goal = self
|
||||
.state_db
|
||||
.thread_goals()
|
||||
.get_thread_goal(self.thread_id)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"failed to read goal metrics status: {err}"
|
||||
))
|
||||
})?;
|
||||
Ok(goal.and_then(|goal| {
|
||||
expected_goal_id
|
||||
.is_none_or(|expected_goal_id| goal.goal_id == expected_goal_id)
|
||||
.then_some(goal.status)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_arguments<T>(arguments: &str) -> Result<T, FunctionCallError>
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
{
|
||||
serde_json::from_str(arguments)
|
||||
.map_err(|err| FunctionCallError::RespondToModel(err.to_string()))
|
||||
}
|
||||
|
||||
pub(crate) fn validate_goal_budget(
|
||||
value: Option<i64>,
|
||||
max_goal_token_budget: Option<i64>,
|
||||
) -> Result<(), String> {
|
||||
if let Some(value) = value
|
||||
&& value <= 0
|
||||
{
|
||||
return Err("goal budgets must be positive when provided".to_string());
|
||||
}
|
||||
if let Some(value) = value
|
||||
&& let Some(max_goal_token_budget) = max_goal_token_budget
|
||||
&& value > max_goal_token_budget
|
||||
{
|
||||
return Err(format!(
|
||||
"goal token budget {value} exceeds the maximum allowed goal token budget of {max_goal_token_budget}"
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn goal_response(
|
||||
goal: Option<ThreadGoal>,
|
||||
completion_budget_report: CompletionBudgetReport,
|
||||
) -> Result<Box<dyn ToolOutput>, FunctionCallError> {
|
||||
let value = serde_json::to_value(GoalToolResponse::new(goal, completion_budget_report))
|
||||
.map_err(|err| FunctionCallError::Fatal(err.to_string()))?;
|
||||
Ok(Box::new(JsonToolOutput::new(value)))
|
||||
}
|
||||
|
||||
impl GoalToolResponse {
|
||||
fn new(goal: Option<ThreadGoal>, report_mode: CompletionBudgetReport) -> Self {
|
||||
let remaining_tokens = goal.as_ref().and_then(|goal| {
|
||||
goal.token_budget
|
||||
.map(|budget| (budget - goal.tokens_used).max(0))
|
||||
});
|
||||
let completion_budget_report = match report_mode {
|
||||
CompletionBudgetReport::Include => goal
|
||||
.as_ref()
|
||||
.filter(|goal| goal.status == ThreadGoalStatus::Complete)
|
||||
.and_then(completion_budget_report),
|
||||
CompletionBudgetReport::Omit => None,
|
||||
};
|
||||
Self {
|
||||
goal,
|
||||
remaining_tokens,
|
||||
completion_budget_report,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn fill_empty_thread_preview_if_possible(
|
||||
state_db: &akode_state::StateRuntime,
|
||||
thread_id: ThreadId,
|
||||
goal: &akode_state::ThreadGoal,
|
||||
) {
|
||||
if let Err(err) = state_db
|
||||
.set_thread_preview_if_empty(thread_id, goal.objective.as_str())
|
||||
.await
|
||||
{
|
||||
tracing::warn!(
|
||||
"failed to set empty thread preview from goal objective for {thread_id}: {err}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn protocol_goal_from_state(goal: akode_state::ThreadGoal) -> ThreadGoal {
|
||||
ThreadGoal {
|
||||
thread_id: goal.thread_id,
|
||||
objective: goal.objective,
|
||||
status: protocol_status_from_state(goal.status),
|
||||
token_budget: goal.token_budget,
|
||||
tokens_used: goal.tokens_used,
|
||||
time_used_seconds: goal.time_used_seconds,
|
||||
created_at: goal.created_at.timestamp(),
|
||||
updated_at: goal.updated_at.timestamp(),
|
||||
}
|
||||
}
|
||||
|
||||
fn protocol_status_from_state(status: akode_state::ThreadGoalStatus) -> ThreadGoalStatus {
|
||||
match status {
|
||||
akode_state::ThreadGoalStatus::Active => ThreadGoalStatus::Active,
|
||||
akode_state::ThreadGoalStatus::Paused => ThreadGoalStatus::Paused,
|
||||
akode_state::ThreadGoalStatus::Blocked => ThreadGoalStatus::Blocked,
|
||||
akode_state::ThreadGoalStatus::UsageLimited => ThreadGoalStatus::UsageLimited,
|
||||
akode_state::ThreadGoalStatus::BudgetLimited => ThreadGoalStatus::BudgetLimited,
|
||||
akode_state::ThreadGoalStatus::Complete => ThreadGoalStatus::Complete,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn state_status_from_protocol(
|
||||
status: ThreadGoalStatus,
|
||||
) -> akode_state::ThreadGoalStatus {
|
||||
match status {
|
||||
ThreadGoalStatus::Active => akode_state::ThreadGoalStatus::Active,
|
||||
ThreadGoalStatus::Paused => akode_state::ThreadGoalStatus::Paused,
|
||||
ThreadGoalStatus::Blocked => akode_state::ThreadGoalStatus::Blocked,
|
||||
ThreadGoalStatus::UsageLimited => akode_state::ThreadGoalStatus::UsageLimited,
|
||||
ThreadGoalStatus::BudgetLimited => akode_state::ThreadGoalStatus::BudgetLimited,
|
||||
ThreadGoalStatus::Complete => akode_state::ThreadGoalStatus::Complete,
|
||||
}
|
||||
}
|
||||
|
||||
fn completion_budget_report(goal: &ThreadGoal) -> Option<String> {
|
||||
if goal.token_budget.is_none() && goal.time_used_seconds <= 0 {
|
||||
None
|
||||
} else {
|
||||
Some(
|
||||
"Goal achieved. Report final usage from this tool result's structured goal fields. If `goal.tokenBudget` is present, include token usage from `goal.tokensUsed` and `goal.tokenBudget`. If `goal.timeUsedSeconds` is greater than 0, summarize elapsed time in a concise, human-friendly form appropriate to the response language."
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user