feat(ai): add plugin recommendations and agent context
This commit is contained in:
43 files changed
+1210
-118
No files matched your search
@@ -55,16 +55,16 @@ async fn run() -> Result<(), String> {
|
||||
|
||||
// Nothing reaches the model before the user opts the plugin in, and
|
||||
// nothing for a connection that is not open.
|
||||
if !plugin_tools::discover_plugin_tools(&state, None).await.is_empty() {
|
||||
if !plugin_tools::discover_plugin_tools(&state, None, None).await.is_empty() {
|
||||
return Err("tools were exposed before the plugin was enabled for the AI".to_string());
|
||||
}
|
||||
state.storage.set_ai_plugin_tool_plugin_enabled(PLUGIN_ID, true).await?;
|
||||
if !plugin_tools::discover_plugin_tools(&state, None).await.is_empty() {
|
||||
if !plugin_tools::discover_plugin_tools(&state, None, None).await.is_empty() {
|
||||
return Err("tools were exposed without an open plugin connection".to_string());
|
||||
}
|
||||
|
||||
state.get_or_create_pool(CONNECTION_ID, None).await?;
|
||||
let tools = plugin_tools::discover_plugin_tools(&state, None).await;
|
||||
let tools = plugin_tools::discover_plugin_tools(&state, None, None).await;
|
||||
let definitions = tools.definitions();
|
||||
let names = definitions.iter().map(|definition| definition.name.as_ref()).collect::<Vec<_>>();
|
||||
if names != ["hello__hello_greet", "hello__hello_set_greeting"] {
|
||||
|
||||
@@ -297,7 +297,10 @@ async fn run_agent_loop_inner(
|
||||
// Plugin tools join agent runs only; ask mode keeps its database-only
|
||||
// read tools.
|
||||
let plugin_tool_set = if is_agent_mode {
|
||||
plugin_tools::discover_plugin_tools(&agent_ctx.state, agent_ctx.host_runtime.as_ref()).await
|
||||
let bound_plugin_connection =
|
||||
(agent_ctx.db_type == DatabaseType::Plugin).then_some(agent_ctx.connection_id.as_str());
|
||||
plugin_tools::discover_plugin_tools(&agent_ctx.state, agent_ctx.host_runtime.as_ref(), bound_plugin_connection)
|
||||
.await
|
||||
} else {
|
||||
PluginToolSet::default()
|
||||
};
|
||||
|
||||
@@ -254,6 +254,7 @@ impl PluginToolBinding {
|
||||
pub async fn discover_plugin_tools(
|
||||
state: &Arc<AppState>,
|
||||
host_runtime: Option<&tokio::runtime::Handle>,
|
||||
connection_id: Option<&str>,
|
||||
) -> PluginToolSet {
|
||||
let enabled = match state.storage.load_ai_plugin_tool_plugin_ids().await {
|
||||
Ok(ids) if !ids.is_empty() => ids.into_iter().collect::<HashSet<_>>(),
|
||||
@@ -271,7 +272,8 @@ pub async fn discover_plugin_tools(
|
||||
return PluginToolSet::default();
|
||||
}
|
||||
};
|
||||
let connections = open_plugin_connections(state, &plugin_names).await;
|
||||
let mut connections = open_plugin_connections(state, &plugin_names).await;
|
||||
restrict_connections(&mut connections, connection_id);
|
||||
if connections.is_empty() {
|
||||
return PluginToolSet::default();
|
||||
}
|
||||
@@ -357,6 +359,12 @@ async fn open_plugin_connections(state: &AppState, plugins: &HashMap<String, Str
|
||||
connections
|
||||
}
|
||||
|
||||
fn restrict_connections(connections: &mut Vec<OpenPluginConnection>, connection_id: Option<&str>) {
|
||||
if let Some(connection_id) = connection_id {
|
||||
connections.retain(|connection| connection.connection_id == connection_id);
|
||||
}
|
||||
}
|
||||
|
||||
/// The lifecycle payload of `connection_id` if it is still open, with a fresh
|
||||
/// operation id for this call.
|
||||
async fn current_lifecycle(state: &AppState, plugin_id: &str, connection_id: &str) -> Option<Value> {
|
||||
@@ -950,6 +958,16 @@ mod tests {
|
||||
])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bound_plugin_agent_sees_only_its_connection() {
|
||||
let mut connections =
|
||||
vec![connection("k1", "cluster-a", "io.dbx.kafka"), connection("k2", "cluster-b", "io.dbx.kafka")];
|
||||
restrict_connections(&mut connections, Some("k2"));
|
||||
assert_eq!(connections.iter().map(|item| item.connection_id.as_str()).collect::<Vec<_>>(), ["k2"]);
|
||||
restrict_connections(&mut connections, None);
|
||||
assert_eq!(connections.len(), 1);
|
||||
}
|
||||
|
||||
fn kafka_listing() -> Value {
|
||||
json!({
|
||||
"tools": [
|
||||
|
||||
Reference in new issue
Block a user