Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/dim-agent-capabilities/src/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use tokio_util::sync::CancellationToken;
use uuid::Uuid;

mod face;
pub(crate) use face::deserialize_name;
pub use face::{FsHost, FsPermissions, FsRemove, OpenRead};

/// Read-only reader lifetime observations for filesystem flow tests.
Expand Down
2 changes: 1 addition & 1 deletion crates/dim-agent-capabilities/src/file_storage/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum EntryName {
Native(OsString),
}

fn deserialize_name<'de, D: serde::Deserializer<'de>>(
pub(crate) fn deserialize_name<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<OsString, D::Error> {
Ok(match EntryName::deserialize(deserializer)? {
Expand Down
6 changes: 6 additions & 0 deletions crates/dim-agent-capabilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ fn register_names_in(catalog: &mut dim_agent_runtime::names::ServiceCatalog) {
register::<context_usage::ContextUsage>(catalog, branch);
register::<context_preparation::ConfigurationPublisher>(catalog, branch);
register::<tool_executor::ToolExecutor>(catalog, branch);
catalog.register_shared_instance(name_of::<tool_executor::ToolExecutor>(), BRANCH_GROUP);
register::<tool_binding::ToolBinding>(catalog, branch);
catalog.register_shared_instance(name_of::<tool_binding::ToolBinding>(), BRANCH_GROUP);
register::<permission::Permission>(catalog, branch);
register::<approval::Approval>(catalog, branch);
catalog.register_shared_instance("skillsBranch", BRANCH_GROUP);
Expand All @@ -121,6 +123,10 @@ fn register_names_in(catalog: &mut dim_agent_runtime::names::ServiceCatalog) {
register::<dyn exec_stop::ExecStop>(catalog, branch);
register::<wait::Wait>(catalog, branch);
register::<workspace_tools::file_reads::FileReads>(catalog, branch);
catalog.register_shared_instance(
name_of::<workspace_tools::file_reads::FileReads>(),
BRANCH_GROUP,
);
catalog.register_shared_instance("branchLine", BRANCH_GROUP);
register::<host_platform::HostPlatform>(catalog, None);
register::<file_storage::FileStorage>(catalog, None);
Expand Down
14 changes: 14 additions & 0 deletions crates/dim-agent-capabilities/src/tool_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//! configuration, pins the source sets its prefix may still declare, and
//! fixes each request's handlers when Loop binds it, so a call always runs
//! the implementation of the version its request declared.
mod face;

use crate::{
context_preparation::ToolSetAdoption,
host::{ModelSettings, ToolExecutionContext},
Expand Down Expand Up @@ -210,6 +212,9 @@ impl ToolBinding {
/// Moves a request's snapshot to its budget recovery request.
pub fn transfer(&self, from: &str, to: &str) -> Result<(), String> {
let mut state = self.state.lock().unwrap();
if from != to && state.snapshots.contains_key(to) && state.snapshots.contains_key(from) {
return Err(format!("request {to} is already bound"));
}
let snapshot = state
.snapshots
.remove(from)
Expand Down Expand Up @@ -399,6 +404,7 @@ fn register(
}

pub struct ToolBindingPlugin {
instance: rutis::host::InstanceId,
binding: Key<ToolBinding>,
registry: Key<ToolRegistry>,
prompt: Key<Prompt>,
Expand All @@ -410,6 +416,7 @@ pub struct ToolBindingPlugin {
impl ToolBindingPlugin {
pub fn new(names: &Names<'_>) -> std::result::Result<Self, Error> {
Ok(Self {
instance: names.instance(dim_agent_runtime::BRANCH_GROUP)?,
binding: names.key()?,
registry: names.key()?,
prompt: names.key()?,
Expand Down Expand Up @@ -532,6 +539,13 @@ impl DynPlugin for ToolBindingPlugin {
tasks.wait().await;
Ok(())
})?;
ctx.provide_at::<dyn rutis_bridge::session::HostDispatch>(
rutis_bridge::session::host_key_in(
&dim_agent_runtime::names::name_of::<ToolBinding>(),
self.instance,
),
binding.clone(),
)?;
ctx.provide_at(self.binding.clone(), binding)?;
Ok(Effect::Done)
})
Expand Down
82 changes: 82 additions & 0 deletions crates/dim-agent-capabilities/src/tool_binding/face.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use super::ToolBinding;
use crate::port_faces::{request, unknown_method};
use dim_agent_types::{ToolDefinition, ToolSourceVersion};
use rutis_bridge::session::{Error, HostDispatch, Reply, Value, arg};
use serde::Deserialize;

const NAME: &str = "toolBinding";

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct Request {
request_id: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct Bind {
request_id: String,
through: u64,
tools: Vec<ToolDefinition>,
tool_sources: Vec<ToolSourceVersion>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BindCurrent {
request_id: String,
declared: Option<Vec<ToolDefinition>>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Transfer {
from: String,
to: String,
}

impl HostDispatch for ToolBinding {
fn methods(&self) -> Option<serde_json::Value> {
Some(serde_json::json!({
"bind": "sync", "bindCurrent": "sync", "definitions": "sync",
"transfer": "sync", "release": "sync"
}))
}

fn invoke(&self, method: &str, args: Value) -> Reply {
match method {
"bind" => {
let data: Bind = request(NAME, args)?;
self.bind(
&data.request_id,
data.through,
&data.tools,
&data.tool_sources,
)
.map_err(Error::value)?;
Ok(Value::Undefined)
}
"bindCurrent" => {
let data: BindCurrent = request(NAME, args)?;
arg(&self
.bind_current(&data.request_id, data.declared.as_deref())
.map_err(Error::value)?)
}
"definitions" => {
let data: Request = request(NAME, args)?;
arg(&self.definitions(&data.request_id).map_err(Error::value)?)
}
"transfer" => {
let data: Transfer = request(NAME, args)?;
self.transfer(&data.from, &data.to).map_err(Error::value)?;
Ok(Value::Undefined)
}
"release" => {
let data: Request = request(NAME, args)?;
self.release(&data.request_id);
Ok(Value::Undefined)
}
_ => Err(unknown_method(NAME, method)),
}
}
}
Loading