diff --git a/backend/windmill-api/src/ai_session_tools/gen/tools.json b/backend/windmill-api/src/ai_session_tools/gen/tools.json index 6227f0c11d..81b779088f 100644 --- a/backend/windmill-api/src/ai_session_tools/gen/tools.json +++ b/backend/windmill-api/src/ai_session_tools/gen/tools.json @@ -1019,7 +1019,7 @@ "type": "object", "properties": { "path": { - "description": "Workspace path of the script to test.", + "description": "Workspace path of the script to test, or a hub/// path from search_hub_scripts to try a hub script.", "type": "string" }, "args": { diff --git a/backend/windmill-api/src/ai_session_tools/runs.rs b/backend/windmill-api/src/ai_session_tools/runs.rs index 99b6a653ec..c3df5f6008 100644 --- a/backend/windmill-api/src/ai_session_tools/runs.rs +++ b/backend/windmill-api/src/ai_session_tools/runs.rs @@ -8,7 +8,7 @@ use serde_json::{json, Map, Value}; use super::chat::{Effect, RunPhase}; use super::workspace::{ - api_message, get_draft, infer_script_schema, load_flow, load_script, path_enc, + api_message, get_draft, infer_script_schema, load_flow, load_script_to_run, path_enc, }; use super::{enc, js_head, js_len, js_tail, opt_str_arg, pretty, str_arg, truthy, Ctx, ToolResult}; @@ -943,7 +943,7 @@ fn form_keys(schema: Option<&Value>) -> Vec { } async fn resolve_test_script(ctx: &Ctx<'_>, path: &str) -> Result { - let script = load_script(ctx, path).await?; + let script = load_script_to_run(ctx, path).await?; Ok(Resolved { schema: script.schema, code: Some(script.content.clone()), @@ -1643,7 +1643,7 @@ async fn resolve_step(ctx: &Ctx<'_>, path: &str, step_id: &str) -> Result { - let loaded = load_script(ctx, &target).await?; + let loaded = load_script_to_run(ctx, &target).await?; (loaded.content, loaded.language, loaded.schema) } }; diff --git a/backend/windmill-api/src/ai_session_tools/workspace.rs b/backend/windmill-api/src/ai_session_tools/workspace.rs index 80e5da2041..f75146b92c 100644 --- a/backend/windmill-api/src/ai_session_tools/workspace.rs +++ b/backend/windmill-api/src/ai_session_tools/workspace.rs @@ -982,6 +982,30 @@ pub(super) struct LoadedScript { pub schema: Option, } +/// A hub script, by its `hub///` path. Hub scripts live outside the +/// workspace: they have no draft and no workspace row. +pub(super) async fn get_hub_script(ctx: &Ctx<'_>, path: &str) -> Result { + ctx.get(&format!("/scripts/hub/get_full/{}", path_enc(path))) + .await +} + +/// The script a test run executes: `load_script`, or a hub script as published. Only test +/// runs take a hub path: an edit would save a draft no run reads, and `run_script` stays +/// limited to deployed workspace scripts, which is all a read-and-run session may run. +pub(super) async fn load_script_to_run(ctx: &Ctx<'_>, path: &str) -> Result { + if !path.starts_with("hub/") { + return load_script(ctx, path).await; + } + let script = get_hub_script(ctx, path).await?; + let text = |key: &str| script.get(key).and_then(Value::as_str).map(String::from); + Ok(LoadedScript { + content: text("content").unwrap_or_default(), + language: text("language").unwrap_or_default(), + summary: text("summary"), + schema: non_null(script.get("schema")).cloned(), + }) +} + pub(super) async fn load_script(ctx: &Ctx<'_>, path: &str) -> Result { let text = |value: &Value, key: &str| value.get(key).and_then(Value::as_str).map(String::from); if let Some((item, _)) = get_draft_item(ctx, "script", path, None).await? { @@ -1664,9 +1688,7 @@ async fn read_workspace_item(ctx: &Ctx<'_>, args: &Value) -> ToolResult { let item = match item_type { "script" if hub => { - let script = ctx - .get(&format!("/scripts/hub/get_full/{}", path_enc(path))) - .await?; + let script = get_hub_script(ctx, path).await?; let mut row = script.clone(); row["path"] = json!(path); let mut item = with_value( diff --git a/frontend/src/lib/components/copilot/chat/global/core.ts b/frontend/src/lib/components/copilot/chat/global/core.ts index 9943367090..3a8882f8c5 100644 --- a/frontend/src/lib/components/copilot/chat/global/core.ts +++ b/frontend/src/lib/components/copilot/chat/global/core.ts @@ -853,7 +853,11 @@ function waitSecondsToDetachMs(waitSeconds: number | undefined): number | undefi } const testRunScriptSchema = z.object({ - path: z.string().describe('Workspace path of the script to test.'), + path: z + .string() + .describe( + 'Workspace path of the script to test, or a hub/// path from search_hub_scripts to try a hub script.' + ), args: testRunArgsSchema, background: backgroundArgSchema, wait_seconds: waitSecondsArgSchema