diff --git a/backend/.sqlx/query-ae54be84233bb75de975221cfa845d58e6fdc4dcf537cb5367c347b46e5c300e.json b/backend/.sqlx/query-ae54be84233bb75de975221cfa845d58e6fdc4dcf537cb5367c347b46e5c300e.json new file mode 100644 index 0000000000000..e427d50ed3b5d --- /dev/null +++ b/backend/.sqlx/query-ae54be84233bb75de975221cfa845d58e6fdc4dcf537cb5367c347b46e5c300e.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT count(path) FROM resource WHERE path LIKE 'f/' || $1 || '%' AND resource_type IS DISTINCT FROM 'ai_agent' AND workspace_id = $2", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "count", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Text", + "Text" + ] + }, + "nullable": [ + null + ] + }, + "hash": "ae54be84233bb75de975221cfa845d58e6fdc4dcf537cb5367c347b46e5c300e" +} diff --git a/backend/.sqlx/query-8cc4fa257cb8053d27eb7a555d6e1b993e4a74b063eac632df8da24f6b87130e.json b/backend/.sqlx/query-fc9dd1be96a0a2c987724c2bb89e1994d22596d4d869f54184a68ac4f0f9c174.json similarity index 70% rename from backend/.sqlx/query-8cc4fa257cb8053d27eb7a555d6e1b993e4a74b063eac632df8da24f6b87130e.json rename to backend/.sqlx/query-fc9dd1be96a0a2c987724c2bb89e1994d22596d4d869f54184a68ac4f0f9c174.json index 3130dd7386e1e..ad6bd6c9260d6 100644 --- a/backend/.sqlx/query-8cc4fa257cb8053d27eb7a555d6e1b993e4a74b063eac632df8da24f6b87130e.json +++ b/backend/.sqlx/query-fc9dd1be96a0a2c987724c2bb89e1994d22596d4d869f54184a68ac4f0f9c174.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT count(path) FROM resource WHERE path LIKE 'f/' || $1 || '%' AND workspace_id = $2", + "query": "SELECT count(path) FROM resource WHERE path LIKE 'f/' || $1 || '%' AND resource_type = 'ai_agent' AND workspace_id = $2", "describe": { "columns": [ { @@ -19,5 +19,5 @@ null ] }, - "hash": "8cc4fa257cb8053d27eb7a555d6e1b993e4a74b063eac632df8da24f6b87130e" + "hash": "fc9dd1be96a0a2c987724c2bb89e1994d22596d4d869f54184a68ac4f0f9c174" } diff --git a/backend/windmill-api-groups/src/folders.rs b/backend/windmill-api-groups/src/folders.rs index c52b8a6316dff..ddf121b4d83c5 100644 --- a/backend/windmill-api-groups/src/folders.rs +++ b/backend/windmill-api-groups/src/folders.rs @@ -694,6 +694,7 @@ struct FolderUsage { pub flows: i64, pub apps: i64, pub resources: i64, + pub agents: i64, pub variables: i64, } async fn get_folder_usage( @@ -749,8 +750,19 @@ async fn get_folder_usage( .await? .unwrap_or(0); + // A saved AI agent is an `ai_agent` resource, but it is listed and used as an item of + // its own, so it is counted apart rather than among the credentials. + let agents = sqlx::query_scalar!( + "SELECT count(path) FROM resource WHERE path LIKE 'f/' || $1 || '%' AND resource_type = 'ai_agent' AND workspace_id = $2", + name, + w_id + ) + .fetch_one(&mut *tx) + .await? + .unwrap_or(0); + let resources = sqlx::query_scalar!( - "SELECT count(path) FROM resource WHERE path LIKE 'f/' || $1 || '%' AND workspace_id = $2", + "SELECT count(path) FROM resource WHERE path LIKE 'f/' || $1 || '%' AND resource_type IS DISTINCT FROM 'ai_agent' AND workspace_id = $2", name, w_id ) @@ -774,6 +786,7 @@ async fn get_folder_usage( schedules, apps: apps + raw_apps, resources, + agents, variables, })) } diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index d753709c80693..6cbe0b4966778 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -25045,6 +25045,10 @@ paths: type: number resources: type: number + description: resources other than AI agents + agents: + type: number + description: saved AI agents (resources of type `ai_agent`) variables: type: number schedules: @@ -25054,6 +25058,7 @@ paths: - flows - apps - resources + - agents - variables - schedules diff --git a/backend/windmill-api/src/hub_publish.rs b/backend/windmill-api/src/hub_publish.rs index 64154c3764060..37bc752b00792 100644 --- a/backend/windmill-api/src/hub_publish.rs +++ b/backend/windmill-api/src/hub_publish.rs @@ -39,6 +39,11 @@ pub fn workspaced_service() -> Router { ) .route("/resource_types", post(publish_resource_type)) .route("/resources", post(publish_resources)) + .route("/agents", post(publish_agents)) + .route( + "/projects/{slug}/agent_recording", + post(publish_agent_recording), + ) .route("/triggers", post(publish_triggers)) .route("/migrations", post(publish_migrations)) .route("/projects/{slug}/export", get(get_project_export)) @@ -384,10 +389,10 @@ struct ProjectLogoBody { logo: Option>, } -fn deserialize_explicit<'de, D: Deserializer<'de>>( +fn deserialize_explicit<'de, T: Deserialize<'de>, D: Deserializer<'de>>( d: D, -) -> Result>, D::Error> { - Option::::deserialize(d).map(Some) +) -> Result>, D::Error> { + Option::::deserialize(d).map(Some) } // Mirrors the Hub's own limits so an oversized/invalid payload is rejected @@ -485,6 +490,63 @@ async fn publish_resources( .await } +// A saved agent is an `ai_agent` resource whose value is the product: its prompt, model and +// tools. Unlike the stubs above it ships whole, since it holds no credential, only `$res:` +// references to the provider and to what its tools take, which travel as stubs. +#[derive(Deserialize, Serialize)] +struct PublishAgentBody { + path: String, + #[serde(skip_serializing_if = "Option::is_none")] + source_path: Option, + #[serde(skip_serializing_if = "Option::is_none")] + summary: Option, + #[serde(skip_serializing_if = "Option::is_none")] + description: Option, + value: serde_json::Value, +} + +// Full-set sync, as for triggers: the agents the folder holds now, absent ones removed. +#[derive(Deserialize, Serialize)] +struct PublishAgentsBody { + agents: Vec, + project_slug: ProjectSlug, +} + +async fn publish_agents( + ctx: HubPublishCtx, + Json(body): Json, +) -> Result { + ctx.post(&format!("/projects/{}/agents", body.project_slug), &body) + .await +} + +// The demo of an agent is a recorded conversation, kept on the agent's row of the draft. +// Double-Option, as for the logo: `recording: null` clears it, and a body without the key +// is refused rather than read as that. +#[derive(Deserialize, Serialize)] +struct AgentRecordingBody { + path: String, + #[serde(default, deserialize_with = "deserialize_explicit")] + recording: Option>, +} + +async fn publish_agent_recording( + ctx: HubPublishCtx, + Path((_workspace, slug)): Path<(String, ProjectSlug)>, + Json(body): Json, +) -> Result { + let Some(recording) = body.recording else { + return Err(Error::BadRequest( + "recording field is required: an object to set it, or null to clear it".to_string(), + )); + }; + ctx.post( + &format!("/projects/{}/agent_recording", slug), + &serde_json::json!({ "path": body.path, "recording": recording }), + ) + .await +} + #[derive(Deserialize, Serialize)] struct PublishTriggerBody { path: String, diff --git a/frontend/package.json b/frontend/package.json index 8e61b157d36c9..78c93d4a349d0 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -480,6 +480,46 @@ "types": "./package/components/common/badge/Badge.svelte.d.ts", "svelte": "./package/components/common/badge/Badge.svelte", "default": "./package/components/common/badge/Badge.svelte" + }, + "./components/copilot/chat/AIChatMessage.svelte": { + "types": "./package/components/copilot/chat/AIChatMessage.svelte.d.ts", + "svelte": "./package/components/copilot/chat/AIChatMessage.svelte", + "default": "./package/components/copilot/chat/AIChatMessage.svelte" + }, + "./components/copilot/chat/ToolGroupDisplay.svelte": { + "types": "./package/components/copilot/chat/ToolGroupDisplay.svelte.d.ts", + "svelte": "./package/components/copilot/chat/ToolGroupDisplay.svelte", + "default": "./package/components/copilot/chat/ToolGroupDisplay.svelte" + }, + "./components/copilot/chat/ChatTypingIndicator.svelte": { + "types": "./package/components/copilot/chat/ChatTypingIndicator.svelte.d.ts", + "svelte": "./package/components/copilot/chat/ChatTypingIndicator.svelte", + "default": "./package/components/copilot/chat/ChatTypingIndicator.svelte" + }, + "./components/copilot/chat/chatViewHost": { + "types": "./package/components/copilot/chat/chatViewHost.d.ts", + "default": "./package/components/copilot/chat/chatViewHost.js" + }, + "./components/copilot/chat/readOnlyChatViewHost.svelte": { + "types": "./package/components/copilot/chat/readOnlyChatViewHost.svelte.d.ts", + "default": "./package/components/copilot/chat/readOnlyChatViewHost.svelte.js" + }, + "./components/copilot/chat/toolGroups": { + "types": "./package/components/copilot/chat/toolGroups.d.ts", + "default": "./package/components/copilot/chat/toolGroups.js" + }, + "./components/copilot/chat/shared": { + "types": "./package/components/copilot/chat/shared.d.ts", + "default": "./package/components/copilot/chat/shared.js" + }, + "./components/recording/AgentRecordingTranscript.svelte": { + "types": "./package/components/recording/AgentRecordingTranscript.svelte.d.ts", + "svelte": "./package/components/recording/AgentRecordingTranscript.svelte", + "default": "./package/components/recording/AgentRecordingTranscript.svelte" + }, + "./components/recording/agentRecording": { + "types": "./package/components/recording/agentRecording.d.ts", + "default": "./package/components/recording/agentRecording.js" } }, "files": [ @@ -651,6 +691,33 @@ ], "components/common/badge/Badge.svelte": [ "./package/components/common/badge/Badge.svelte.d.ts" + ], + "components/copilot/chat/AIChatMessage.svelte": [ + "./package/components/copilot/chat/AIChatMessage.svelte.d.ts" + ], + "components/copilot/chat/ToolGroupDisplay.svelte": [ + "./package/components/copilot/chat/ToolGroupDisplay.svelte.d.ts" + ], + "components/copilot/chat/ChatTypingIndicator.svelte": [ + "./package/components/copilot/chat/ChatTypingIndicator.svelte.d.ts" + ], + "components/copilot/chat/chatViewHost": [ + "./package/components/copilot/chat/chatViewHost.d.ts" + ], + "components/copilot/chat/readOnlyChatViewHost.svelte": [ + "./package/components/copilot/chat/readOnlyChatViewHost.svelte.d.ts" + ], + "components/copilot/chat/toolGroups": [ + "./package/components/copilot/chat/toolGroups.d.ts" + ], + "components/copilot/chat/shared": [ + "./package/components/copilot/chat/shared.d.ts" + ], + "components/recording/AgentRecordingTranscript.svelte": [ + "./package/components/recording/AgentRecordingTranscript.svelte.d.ts" + ], + "components/recording/agentRecording": [ + "./package/components/recording/agentRecording.d.ts" ] } }, diff --git a/frontend/src/lib/components/FolderUsageInfo.svelte b/frontend/src/lib/components/FolderUsageInfo.svelte index cc706da9eddb5..6aa3c59196cc7 100644 --- a/frontend/src/lib/components/FolderUsageInfo.svelte +++ b/frontend/src/lib/components/FolderUsageInfo.svelte @@ -11,7 +11,7 @@ order?: any; } - let { name, tabular = false, order = ['scripts', 'flows', 'apps', 'schedules', 'variables', 'resources'] }: Props = $props(); + let { name, tabular = false, order = ['scripts', 'flows', 'apps', 'agents', 'schedules', 'variables', 'resources'] }: Props = $props(); let usage: Record = $state({}) diff --git a/frontend/src/lib/components/aiAgentResult.test.ts b/frontend/src/lib/components/aiAgentResult.test.ts index 6389295a7096b..174e9e2ce44dd 100644 --- a/frontend/src/lib/components/aiAgentResult.test.ts +++ b/frontend/src/lib/components/aiAgentResult.test.ts @@ -6,6 +6,7 @@ import { formatTokenCount, isAgentStream, parseAgentResult, + readAgentTurn, summarizeAgentResult } from './aiAgentResult' @@ -265,3 +266,97 @@ describe('coercing messages at the boundary', () => { ]) }) }) + +const call = (id: string, name: string, args: unknown) => ({ + id, + type: 'function', + function: { name, arguments: JSON.stringify(args) } +}) + +describe('readAgentTurn', () => { + it('reads the tools this turn called and its answer', () => { + const turn = readAgentTurn({ + output: 'Found it.', + messages: [ + { role: 'user', content: 'Where is my order?' }, + { role: 'assistant', tool_calls: [call('a', 'get_order', { id: 1 })] }, + { role: 'tool', tool_call_id: 'a', content: '{"status":"shipped"}' }, + { role: 'assistant', content: 'Found it.' } + ] + }) + expect(turn.answer).toBe('Found it.') + expect(turn.tool_calls).toEqual([ + { name: 'get_order', args: { id: 1 }, result: '{"status":"shipped"}' } + ]) + }) + + it('leaves out the earlier turns a run with memory returns', () => { + const turn = readAgentTurn({ + output: 'Refunded.', + messages: [ + { role: 'user', content: 'Where is my order?' }, + { role: 'assistant', tool_calls: [call('a', 'get_order', { id: 1 })] }, + { role: 'tool', tool_call_id: 'a', content: 'shipped' }, + { role: 'assistant', content: 'Found it.' }, + { role: 'user', content: 'Refund it.' }, + { role: 'assistant', tool_calls: [call('b', 'refund_order', { id: 1 })] }, + { role: 'tool', tool_call_id: 'b', content: 'ok' }, + { role: 'assistant', content: 'Refunded.' } + ] + }) + expect(turn.tool_calls.map((c) => c.name)).toEqual(['refund_order']) + }) +}) + +describe('readAgentTurn with background tools', () => { + it('keeps a background call whose result came back as a tagged user message', () => { + const turn = readAgentTurn({ + output: 'Rendered.', + messages: [ + { role: 'user', content: 'Render it.' }, + { role: 'assistant', tool_calls: [call('a', 'render', {})] }, + { role: 'tool', tool_call_id: 'a', content: 'Started in the background.' }, + { + role: 'user', + content: 'Background tool `render` (call a, job j) finished. Result:\nok', + agent_action: { type: 'tool_call', job_id: 'j', function_name: 'render', module_id: 'm' } + }, + { role: 'assistant', content: 'Rendered.' } + ] + }) + expect(turn.tool_calls.map((c) => c.name)).toEqual(['render']) + }) +}) + +describe('readAgentTurn with calls that are not tools of the agent', () => { + it('does not show the structured answer as a tool call', () => { + const turn = readAgentTurn({ + output: { city: 'Paris' }, + messages: [ + { role: 'user', content: 'Where?' }, + { role: 'assistant', tool_calls: [call('s', 'structured_output', { city: 'Paris' })] }, + { role: 'tool', tool_call_id: 's', content: 'Successfully ran structured_output tool' } + ] + }) + expect(turn.tool_calls).toEqual([]) + expect(JSON.parse(turn.answer)).toEqual({ city: 'Paris' }) + }) + + it('shows a web search, in the order it happened', () => { + const turn = readAgentTurn({ + output: 'Found.', + messages: [ + { role: 'user', content: 'Look it up, then check the order.' }, + { + role: 'tool', + content: 'Used websearch tool successfully', + agent_action: { type: 'web_search' } + }, + { role: 'assistant', tool_calls: [call('a', 'get_order', { id: 1 })] }, + { role: 'tool', tool_call_id: 'a', content: 'shipped' }, + { role: 'assistant', content: 'Found.' } + ] + }) + expect(turn.tool_calls.map((c) => c.name)).toEqual(['web_search', 'get_order']) + }) +}) diff --git a/frontend/src/lib/components/aiAgentResult.ts b/frontend/src/lib/components/aiAgentResult.ts index 0c4bf0377df6a..02bb488ebaf8f 100644 --- a/frontend/src/lib/components/aiAgentResult.ts +++ b/frontend/src/lib/components/aiAgentResult.ts @@ -1,4 +1,5 @@ import type { FlowStatusModule } from '$lib/gen' +import type { AgentToolCall } from '$lib/components/recording/types' import { parseStreamEvents, type AgentStreamEvent } from 'windmill-chat' /** The `agent_action` tag the worker puts on every message it records. */ @@ -155,6 +156,53 @@ function actionType(message: AgentMessage): string | undefined { return message.agent_action?.type } +/** The name the worker gives the structured-output tool: suffixed when an agent tool already has it. */ +export const STRUCTURED_OUTPUT_CALL = /^structured_output(_\d+)?$/ + +/** + * One turn of a conversation, read off the result of its run: the answer, and what the agent + * called to get there, in order. A web search is a capability of the provider, not a call of + * the model's: the worker records it as a `tool` message tagged `web_search`. + */ +export function readAgentTurn(result: unknown): { answer: string; tool_calls: AgentToolCall[] } { + const text = (v: unknown) => + typeof v === 'string' ? v : v == null ? '' : JSON.stringify(v, null, 2) + const parsed = parseAgentResult(result) + if (!parsed) return { answer: text(result), tool_calls: [] } + // With memory, the run returns the whole conversation: the turn starts after its own + // message, the last `user` one. A background tool's result also comes back as a `user` + // message, tagged with its call's action, and does not start a turn. + const all = parsed.messages + const lastUser = all.findLastIndex((m) => m.role === 'user' && !m.agent_action) + const messages = lastUser >= 0 ? all.slice(lastUser + 1) : all + const results = new Map() + for (const m of messages) { + if (m.role === 'tool' && m.tool_call_id) results.set(m.tool_call_id, m.content) + } + const tool_calls: AgentToolCall[] = [] + for (const m of messages) { + if (m.role === 'tool' && actionType(m) === 'web_search') { + tool_calls.push({ name: 'web_search' }) + continue + } + if (m.role !== 'assistant') continue + for (const call of m.tool_calls ?? []) { + const name = call.function?.name ?? 'tool' + // The answer itself, given through a tool: not something the agent called. + if (STRUCTURED_OUTPUT_CALL.test(name)) continue + const raw = call.function?.arguments + let args: unknown = raw + if (raw !== undefined) { + try { + args = JSON.parse(raw) + } catch {} + } + tool_calls.push({ name, args, result: call.id ? results.get(call.id) : undefined }) + } + } + return { answer: text(parsed.output), tool_calls } +} + export function summarizeAgentResult(result: AgentResult): AgentResultSummary { let toolCalls = 0 let webSearches = 0 diff --git a/frontend/src/lib/components/flows/conversations/flowChatViewHost.svelte.ts b/frontend/src/lib/components/flows/conversations/flowChatViewHost.svelte.ts index 51b45cf247c1b..5c593e0c26896 100644 --- a/frontend/src/lib/components/flows/conversations/flowChatViewHost.svelte.ts +++ b/frontend/src/lib/components/flows/conversations/flowChatViewHost.svelte.ts @@ -1,3 +1,4 @@ +import { STRUCTURED_OUTPUT_CALL } from '$lib/components/aiAgentResult' import { untrack } from 'svelte' import type { AttachmentUpload, @@ -251,9 +252,6 @@ function builtInWebSearch(message: ChatMessage): WebSearchResult | undefined { } } -/** The name the worker gives the structured-output tool: suffixed when an agent tool already has it. */ -const STRUCTURED_OUTPUT_CALL = /^structured_output(_\d+)?$/ - /** * `busy`: the latest turn is still running, so a failed tool call is not yet its outcome. * `stopped`: ids of the user messages of the turns the reader stopped, as message id or row id. diff --git a/frontend/src/lib/components/recording/AgentRecordingTranscript.svelte b/frontend/src/lib/components/recording/AgentRecordingTranscript.svelte new file mode 100644 index 0000000000000..940a7d756c01e --- /dev/null +++ b/frontend/src/lib/components/recording/AgentRecordingTranscript.svelte @@ -0,0 +1,94 @@ + + + +{#snippet messageRow(message: DisplayMessage, messageIndex: number, inGroup: boolean)} + +{/snippet} +{#snippet groupRow(message: DisplayMessage, messageIndex: number)} + {@render messageRow(message, messageIndex, true)} +{/snippet} + +
+ {#each chatItems as item (chatItemKey(item))} + {#if item.kind === 'group'} +
+
+ +
+
+ {:else} + {@render messageRow(item.message, item.index, false)} + {/if} + {/each} + {#if waiting} +
+ +
+ {/if} +
diff --git a/frontend/src/lib/components/recording/agentRecording.ts b/frontend/src/lib/components/recording/agentRecording.ts new file mode 100644 index 0000000000000..43ba87b3fea99 --- /dev/null +++ b/frontend/src/lib/components/recording/agentRecording.ts @@ -0,0 +1,28 @@ +import type { DisplayMessage } from '$lib/components/copilot/chat/shared' +import type { AgentTurn } from './types' + +/** + * A recorded conversation as the AI chat draws one: each turn's message, the tools the agent + * called, then its answer. The recorder and the Hub page both draw from this, so what is + * recorded is what a visitor reads, tool arguments and results included. + */ +export function agentRecordingMessages(turns: AgentTurn[]): DisplayMessage[] { + const out: DisplayMessage[] = [] + turns.forEach((turn, i) => { + out.push({ role: 'user', index: out.length, content: turn.user_message }) + ;(turn.tool_calls ?? []).forEach((call, j) => + out.push({ + role: 'tool', + tool_call_id: `${i}-${j}`, + content: call.name, + toolName: call.name, + parameters: call.args, + result: call.result, + // As the agent chat does: the row opens on what the tool got and returned. + showDetails: call.args != null || call.result != null + }) + ) + out.push({ role: 'assistant', content: turn.answer }) + }) + return out +} diff --git a/frontend/src/lib/components/recording/types.ts b/frontend/src/lib/components/recording/types.ts index dd85585768342..9c110d6623fd9 100644 --- a/frontend/src/lib/components/recording/types.ts +++ b/frontend/src/lib/components/recording/types.ts @@ -143,6 +143,33 @@ export type RawAppRecording = { truncated?: boolean } +/** One tool an agent called during a turn, read off the turn's result. */ +export type AgentToolCall = { + name: string + args?: unknown + result?: unknown +} + +/** One message to an agent, what it called, and its answer. The run behind a turn is not + * kept: it names who ran it and holds the agent as its workspace stores it. */ +export type AgentTurn = { + user_message: string + answer: string + duration_ms: number + tool_calls: AgentToolCall[] +} + +/** A recorded conversation with a saved AI agent (`ai_agent` resource): the demo of an + * agent on its Hub page. */ +export type AgentRecording = { + version: 1 + type: 'agent' + recorded_at: string + agent_path: string + total_duration_ms: number + turns: AgentTurn[] +} + /** Shape needed by JobLoader replay — a map of job ID to its replay stream */ export interface ActiveReplayData { jobs: Record diff --git a/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte new file mode 100644 index 0000000000000..34d98c4841be4 --- /dev/null +++ b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte @@ -0,0 +1,82 @@ + + +
+ {#if forgets} + + Each message is answered without the ones before it, but the Hub replays the recording as one + conversation. Record a single message, or turn on managed memory on the agent first. + + {/if} +
+ +
+ +
+ + {#if !onsave} + Push the bundle to the Hub first to save a conversation. + {:else if sent === 0} + Ask the agent what a visitor would ask it. Two or three messages make a good demo. + {:else} + The answered messages are saved as the demo on the agent's Hub page. + {/if} + + +
+
diff --git a/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte b/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte index a8019751853a4..68b91f34635db 100644 --- a/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte +++ b/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte @@ -9,7 +9,9 @@ import { displayDate } from '$lib/utils' import { useDeployToHubSession, + asLayoutItem, canRecord, + canRecordConversation, canRecordSession, sanitizeSlug, isValidSlug, @@ -21,6 +23,7 @@ import MigrationSqlEditor from './MigrationSqlEditor.svelte' import PipelineRecordingReplay from '$lib/components/recording/PipelineRecordingReplay.svelte' import RawAppRecordSession from './RawAppRecordSession.svelte' + import AgentRecordSession from './AgentRecordSession.svelte' import AssetGraphCanvas from '$lib/components/assets/AssetGraph/AssetGraphCanvas.svelte' import { ArrowLeft, @@ -35,6 +38,7 @@ FolderTree, Image as ImageIcon, Info, + Bot, LayoutDashboard, Loader2, Play, @@ -157,6 +161,7 @@ let recordDrawer = $state() let appRecordDrawer = $state() let appRecordTarget = $state(undefined) + let agentRecordDrawer = $state() let pipelinePreviewDrawer = $state() // Inline pipeline graph above the item list, collapsed by default so the // selection list stays the first thing in view. @@ -191,6 +196,10 @@ appRecordDrawer?.openDrawer() } + function openAgentRecord(it: DeployItem) { + deployHub.session?.openAgentRecord(it) + agentRecordDrawer?.openDrawer() + } function openRecord(it: DeployItem) { recordDrawer?.openDrawer() void deployHub.session?.openRecord(it) @@ -324,7 +333,8 @@ {@const s = deployHub.session}
i.agent === true} nested folderDetail={subProjectNote} selectablePredicate={(i) => !s.isBlocked(i.path)} @@ -896,6 +906,31 @@ {/if} {/if} + {#if s.phase === 'draft' && canRecordConversation(it)} + {#if it.rec === 'recorded'} + + Recorded + + + {:else} + No recording + + {/if} + {/if} {/snippet} {#snippet footer()} @@ -907,7 +942,7 @@ {:else if s.phase === 'draft'} {#if s.allRecorded} - All scripts and flows have a recording — best chance of approval and featuring. + Every item has a recording — best chance of approval and featuring. {:else} {s.recordableItems.filter((i) => i.rec === 'recorded').length} of {s .recordableItems.length} @@ -1077,6 +1112,41 @@ + + agentRecordDrawer?.closeDrawer()} + > +
+ + Chat with the agent the way a visitor would. Each message runs it for real, in a + conversation of its own; the exchange and the tools it calls are replayed on the Hub + page. + + {#if s.agentTarget} + {#key s.agentTarget.key} +
+ { + if (await s.saveAgentRecording()) agentRecordDrawer?.closeDrawer() + } + : undefined} + /> +
+ {/key} + {/if} +
+
+
+ @@ -1188,6 +1258,8 @@ {:else if u.kind === 'flow'} + {:else if u.kind === 'agent'} + {:else} {/if} diff --git a/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts b/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts index c1cdea12ade78..35af1c4a2f7c9 100644 --- a/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts +++ b/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts @@ -7,16 +7,34 @@ import type { Kind } from '$lib/utils_deployable' export type RecStatus = 'none' | 'recorded' +/** A saved AI agent is an `ai_agent` resource, but it is published and recorded as an item + * of its own, so it has a kind of its own here. */ +export type DeployKind = Kind | 'agent' + export interface DeployItem { key: string path: string - kind: Kind + kind: DeployKind summary?: string rec: RecStatus [k: string]: unknown } -export const canRecord = (k: Kind) => k === 'script' || k === 'flow' +export const canRecord = (k: DeployKind) => k === 'script' || k === 'flow' + +// An agent has no single run to capture: its demo is a conversation, recorded turn by turn +// in its own drawer and replayed on the Hub page. +export const canRecordConversation = (it: DeployItem): boolean => + it.kind === 'agent' || it.agent === true + +/** + * The publish list is the workspace-deploy layout, which knows agents as resource rows it is + * told about (`isAgent`). An agent item is handed to it that way, keeping its key, so + * selection and status still address the item itself. + */ +export function asLayoutItem(it: DeployItem): DeployItem & { kind: Kind } { + return it.kind === 'agent' ? { ...it, kind: 'resource', agent: true } : { ...it, kind: it.kind } +} // `s3_object` is a built-in file-picker format, never a resource type (it is not // on the Hub's type list), so it is excluded even when there is no catalog to diff --git a/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts b/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts index 8352fb7597a38..0aeee741101c8 100644 --- a/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts +++ b/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts @@ -9,6 +9,8 @@ import { ScheduleService } from '$lib/gen' import { sendUserToast } from '$lib/toast' +import { keepsManagedMemory } from '$lib/components/flows/agentFormFields' +import { readAgentTurn } from '$lib/components/aiAgentResult' import { sleep, emptySchema } from '$lib/utils' import { buildProjectBundle, @@ -16,6 +18,8 @@ import { classifyPath, extractScriptRefs, extractFlowRefs, + extractAgentRefs, + withoutAgentWorkspaceKeys, extractAppRefs, extractTriggerConfigResourceRefs, extractVarRefsFromValue, @@ -33,16 +37,17 @@ import { generateDatatableMigrations, type GeneratedMigration } from './projectMigrations' -import type { Kind } from '$lib/utils_deployable' import { canRecord, + canRecordConversation, canRecordSession, commonFolderPath, folderSubPaths, inputResourceTypes, mergeAppTableOrigin, HIDDEN_RESOURCE_TYPES, - type DeployItem + type DeployItem, + type DeployKind } from './deployToHubItems' import type { AssetGraphResponse } from '$lib/components/assets/AssetGraph/types' import { @@ -52,7 +57,12 @@ import { } from '$lib/components/assets/AssetGraph/cascadeRun' import { capturePipelineRecording } from '$lib/components/recording/pipelineRecording.svelte' import { buildFlowRecording, buildScriptRecording } from '$lib/components/recording/runRecording' -import type { PipelineRecording, RawAppRecording } from '$lib/components/recording/types' +import type { + AgentRecording, + AgentTurn, + PipelineRecording, + RawAppRecording +} from '$lib/components/recording/types' import { TRIGGER_KINDS, listAllWorkspaceTriggers, @@ -66,7 +76,9 @@ import { export type Phase = 'predeploy' | 'draft' | 'under_review' | 'live' // Re-exported so the publish-flow components keep one import site. export { + asLayoutItem, canRecord, + canRecordConversation, canRecordSession, mergeAppTableOrigin, type DeployItem, @@ -93,7 +105,8 @@ const ITEM_KIND_ROUTE: Record = { script: 'scripts/get', flow: 'flows/get', app: 'apps/get', - raw_app: 'apps_raw/get' + raw_app: 'apps_raw/get', + agent: 'agents/get' } // Prune a folder's asset graph to a set of scripts so a pipeline recording only @@ -293,6 +306,8 @@ export class DeployToHubSession { // Invalidate any in-flight pipeline cascade poll so it stops on the next // tick instead of polling to the timeout against a discarded session. this.#pipelineRunTok++ + this.#agentTok++ + this.#cancelAgentRun() } load() { @@ -363,7 +378,9 @@ export class DeployToHubSession { ) // A raw app's recorded session counts towards the project's recordings just as // a script's captured run does — both are what a visitor replays. - recordableItems = $derived(this.items.filter((i) => canRecord(i.kind) || canRecordSession(i))) + recordableItems = $derived( + this.items.filter((i) => canRecord(i.kind) || canRecordSession(i) || canRecordConversation(i)) + ) allRecorded = $derived( this.recordableItems.length > 0 && this.recordableItems.every((i) => i.rec === 'recorded') ) @@ -450,7 +467,10 @@ export class DeployToHubSession { ? extractFlowRefs(it.value).filter((r) => r.kind === 'resource') : it.kind === 'app' ? extractAppRefs(it.value) - : extractScriptRefs(it.content ?? '') + : it.kind === 'agent' + ? // Its provider and its tools' fixed inputs: what a fork has to fill. + extractAgentRefs(it.value).filter((r) => r.kind === 'resource') + : extractScriptRefs(it.content ?? '') for (const r of refs) { const stub = stubByNewPath.get(r.path) if (!stub || HIDDEN_RESOURCE_TYPES.has(stub.resource_type)) continue @@ -554,11 +574,17 @@ export class DeployToHubSession { const workspace = this.workspace this.loading = true try { - const [apps, rawApps, flows, scripts] = await Promise.all([ + const [apps, rawApps, flows, scripts, agents] = await Promise.all([ this.#listAllPages((p) => AppService.listApps({ workspace, ...p })), this.#listAllPages((p) => RawAppService.listRawApps({ workspace, ...p })), this.#listAllPages((p) => FlowService.listFlows({ workspace, ...p })), - this.#listAllPages((p) => ScriptService.listScripts({ workspace, ...p })) + this.#listAllPages((p) => ScriptService.listScripts({ workspace, ...p })), + // Saved AI agents are `ai_agent` resources; a workspace without any lists none. + // A failure fails the load like the other lists: agents sync as a full set, so + // publishing without them would remove them from the Hub. + this.#listAllPages((p) => + ResourceService.listResource({ workspace, resourceType: 'ai_agent', ...p }) + ) ]) if (this.#disposed) return @@ -603,6 +629,16 @@ export class DeployToHubSession { rec: 'none' }) } + for (const a of agents) { + next.push({ + key: `agent:${a.path}`, + path: a.path, + kind: 'agent', + // Its name, as the Agents page lists it: the description is a paragraph. + summary: a.path.split('/').pop(), + rec: 'none' + }) + } if (this.#disposed) return this.workspaceItems = next } catch (e: any) { @@ -703,7 +739,7 @@ export class DeployToHubSession { return { key, path: wpath, - kind: it.kind as Kind, + kind: it.kind as DeployKind, summary: it.summary ?? undefined, rec: it.has_recording ? 'recorded' : 'none' } satisfies DeployItem @@ -868,6 +904,17 @@ export class DeployToHubSession { }) if (!r.ok) return undefined return { kind: 'raw_app', path: ref.path, content: await r.text() } + } else if (ref.kind === 'agent') { + const r = await ResourceService.getResource({ workspace, path: ref.path }) + if (r.resource_type !== 'ai_agent') return undefined + const value = withoutAgentWorkspaceKeys(r.value) + return { + kind: 'agent', + path: ref.path, + summary: ref.path.split('/').pop(), + description: r.description ?? undefined, + value + } } } catch (e: any) { return undefined @@ -921,7 +968,7 @@ export class DeployToHubSession { body: JSON.stringify(body) }) const text = await res.text() - if (!res.ok) throw new Error(text) + if (!res.ok) throw Object.assign(new Error(text), { status: res.status }) try { return JSON.parse(text) } catch { @@ -1105,6 +1152,46 @@ export class DeployToHubSession { } } + // Full-set sync, like triggers: always sent, so an agent removed from the folder (or + // unticked) leaves the Hub too. A Hub too old to know agents answers 404; that only + // counts as a failure when there was an agent to publish. + async #pushAgents(slug: string, agents: BundledItem[]): Promise { + const keys = agents.map((a) => `agent:${a.path}`) + const mark = (status: 'loading' | 'deployed' | 'failed', error?: string) => { + const next = { ...this.deploymentStatus } + for (const k of keys) next[k] = error ? { status, error } : { status } + this.deploymentStatus = next + } + if (agents.length) mark('loading') + try { + const resp = await this.#postHub('/hub/agents', { + agents: agents.map((a) => ({ + path: a.newPath, + source_path: a.path, + summary: a.summary || a.newPath.split('/').pop(), + description: a.description ?? undefined, + value: a.value ?? {} + })), + project_slug: slug + }) + const ids = { ...this.hubItemIds } + for (const a of agents) { + const id = resp?.ids?.[a.newPath] + if (typeof id === 'number') ids[`agent:${a.path}`] = id + } + this.hubItemIds = ids + if (agents.length) mark('deployed') + return 0 + } catch (e: any) { + // A Hub without the route has no agents to clear. Any other failure of the empty + // sync is real: agents removed here would stay on the Hub's draft. + if (agents.length === 0 && e?.status === 404) return 0 + mark('failed', e?.message ?? String(e)) + sendUserToast(`Agent sync failed: ${e?.message ?? e}`, true) + return 1 + } + } + // Handler runnables (trigger error handlers, schedule on_* handlers) ship // with the bundle like the primary runnables do; hub refs stay external. #triggerHandlerSeed(triggers: WorkspaceTrigger[], slug: string): ItemRef[] { @@ -1310,6 +1397,8 @@ export class DeployToHubSession { // Stop writing item status / Hub IDs once the session is replaced — // continuing would publish into a project the user has moved away from. if (this.#disposed) return + // Agents go as one set once the scripts their tools run have landed. + if (it.kind === 'agent') continue const key = `${it.kind}:${it.path}` this.deploymentStatus = { ...this.deploymentStatus, [key]: { status: 'loading' } } try { @@ -1324,6 +1413,11 @@ export class DeployToHubSession { } } if (this.#disposed) return + failures += await this.#pushAgents( + slug, + bundle.items.filter((i) => i.kind === 'agent') + ) + if (this.#disposed) return try { await this.#pushTriggers(slug, resourcePathMap, triggersSnapshot) } catch (e: any) { @@ -1749,6 +1843,131 @@ export class DeployToHubSession { }) } + // --- An agent's demo: a conversation, one run per message ----------------------- + + /** The agent being recorded, and the runs its conversation has made so far. */ + agentTarget = $state(undefined) + agentRuns = $state([]) + agentSaving = $state(false) + /** The agent keeps no managed memory: each message would be answered without the ones + * before it, and the recording would replay as a conversation that never happened. */ + agentForgets = $state(false) + #agentTok = 0 + + openAgentRecord(it: DeployItem) { + const tok = ++this.#agentTok + this.#cancelAgentRun() + this.agentTarget = it + this.agentRuns = [] + this.agentForgets = false + void ResourceService.getResourceValue({ workspace: this.workspace, path: it.path }) + .then((value: any) => { + if (tok === this.#agentTok) this.agentForgets = !keepsManagedMemory(value?.memory) + }) + .catch(() => {}) + } + + closeAgentRecord = () => { + this.#agentTok++ + this.#cancelAgentRun() + this.agentTarget = undefined + } + + // The chat stops following a run when it unmounts but does not stop the run, which would + // go on calling real tools. Only the last one can still be running: the chat sends one + // message at a time. Cancelling a finished run is refused, which is fine. + #cancelAgentRun(id = this.agentRuns.at(-1)) { + if (!id) return + void JobService.cancelQueuedJob({ + workspace: this.workspace, + id, + requestBody: { reason: 'The Hub recording it was answering was closed' } + }).catch(() => {}) + } + + /** One message of the recording, run as the agent's own chat runs one. The conversation is a + * new one, so the demo never continues someone's real chat; it stays in the recorder's chats. */ + runAgentTurn = async ( + userMessage: string, + conversationId: string, + inputs?: Record + ): Promise => { + const it = this.agentTarget + if (!it) return undefined + const tok = this.#agentTok + const jobId = await JobService.runAgent({ + workspace: this.workspace, + path: it.path, + memoryId: conversationId, + requestBody: { ...(inputs ?? {}), user_message: userMessage } + }) + // Closed while the run was being created: it was not there to cancel then. + if (tok !== this.#agentTok) this.#cancelAgentRun(jobId) + else this.agentRuns = [...this.agentRuns, jobId] + return jobId + } + + /** Save the conversation as the agent's Hub recording: each answered message, the tools it + * called and the answer, read off its run. Not the runs themselves, which name who ran them + * and hold the agent as this workspace stores it. True when saved for the agent still open. */ + async saveAgentRecording(): Promise { + const it = this.agentTarget + if (!it || this.agentRuns.length === 0 || this.agentSaving) return false + const tok = this.#agentTok + this.agentSaving = true + try { + const turns: AgentTurn[] = [] + let failed = 0 + for (const id of this.agentRuns) { + const job = await JobService.getCompletedJob({ workspace: this.workspace, id }).catch( + () => undefined + ) + // Saving now would silently leave the message being answered out of the demo. + if (!job) { + sendUserToast('Wait for the agent to finish answering before saving.', true) + return false + } + // A message that failed or was cancelled is not part of the demo. + if (!job.success) { + failed++ + continue + } + turns.push({ + user_message: String((job.args as any)?.user_message ?? ''), + ...readAgentTurn(job.result), + duration_ms: job.duration_ms + }) + } + if (turns.length === 0) { + sendUserToast('No answered message to save yet.', true) + return false + } + const recording: AgentRecording = { + version: 1, + type: 'agent', + recorded_at: new Date().toISOString(), + agent_path: it.path, + total_duration_ms: turns.reduce((n, t) => n + t.duration_ms, 0), + turns + } + await this.#postHub(`/hub/projects/${encodeURIComponent(this.hubSlug)}/agent_recording`, { + path: it.path, + recording + }) + this.#patchItem(it.key, { rec: 'recorded' }) + sendUserToast( + `Conversation saved — ${turns.length} message${turns.length === 1 ? '' : 's'}` + + (failed ? `, ${failed} failed left out` : '') + ) + return tok === this.#agentTok + } catch (e: any) { + sendUserToast(`Failed to save the conversation: ${e?.message ?? e}`, true) + return false + } finally { + this.agentSaving = false + } + } + /** Save a recorded raw-app session as that app's Hub recording. */ async saveAppRecording(it: DeployItem, recording: RawAppRecording): Promise { const hubId = this.hubItemIds[it.key] diff --git a/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts b/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts index e8c786e1c236d..6e0d0b0df891c 100644 --- a/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts +++ b/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts @@ -3,6 +3,9 @@ import { classifyPath, extractScriptRefs, extractFlowRefs, + extractAgentRefs, + rewriteAgentValue, + withoutAgentWorkspaceKeys, extractAppRefs, buildPathMap, rewriteContent, @@ -926,3 +929,489 @@ describe('textHoldsBarePath', () => { expect(textHoldsBarePath(text, P)).toBe(expected) }) }) + +describe('saved AI agents', () => { + // An `ai_agent` resource value: a provider, a script tool with a fixed resource input, an + // inline tool reading a resource by token. + const value = { + provider: { kind: 'anthropic', model: 'claude', resource: '$res:u/admin/claude' }, + system_prompt: 'You make videos. Never touch u/admin/helper yourself.', + memory: { kind: 'compaction' }, + tools: [ + { + id: 'a', + summary: 'make_video', + value: { + tool_type: 'flowmodule', + type: 'script', + path: 'u/admin/helper', + input_transforms: { + gh: { type: 'static', value: '$res:f/shared/api' }, + name: { type: 'ai' } + } + } + }, + { + id: 'b', + summary: 'lookup', + value: { + tool_type: 'flowmodule', + type: 'rawscript', + language: 'bun', + content: 'const db = "$res:u/admin/pg"', + input_transforms: {} + } + } + ] + } + + it('finds the scripts its tools run and every resource it reaches', () => { + expect(extractAgentRefs(value)).toEqual( + expect.arrayContaining([ + { kind: 'script', path: 'u/admin/helper' }, + { kind: 'resource', path: 'f/shared/api' }, + { kind: 'resource', path: 'u/admin/pg' }, + { kind: 'resource', path: 'u/admin/claude' } + ]) + ) + }) + + it('relocates tools, inputs and provider, and leaves the prompt alone', () => { + const map = new Map([ + ['u/admin/helper', 'f/proj/helper'], + ['f/shared/api', 'f/proj/api'], + ['u/admin/pg', 'f/proj/pg'], + ['u/admin/claude', 'f/proj/claude'] + ]) + const out = rewriteAgentValue(value, map) + expect(out.provider.resource).toBe('$res:f/proj/claude') + expect(out.tools[0].value.path).toBe('f/proj/helper') + expect(out.tools[0].value.input_transforms.gh.value).toBe('$res:f/proj/api') + expect(out.tools[1].value.content).toContain('$res:f/proj/pg') + expect(out.system_prompt).toBe(value.system_prompt) + expect(out.memory).toEqual(value.memory) + }) + + it('ships whole in a bundle: its script comes along, and it is not also a stub', async () => { + const agent: FetchedItem = { kind: 'agent', path: 'u/admin/video_agent', value } + const helper: FetchedItem = { + kind: 'script', + path: 'u/admin/helper', + language: 'bun', + content: 'export async function main(){}' + } + // An app naming the agent would reference it as a resource: it must stay an item. + const flow: FetchedItem = { + kind: 'flow', + path: 'f/proj/uses_agent', + value: { + modules: [ + { + id: 'x', + value: { + type: 'rawscript', + content: 'const a = "$res:u/admin/video_agent"', + input_transforms: {} + } + } + ] + } + } + const bundle = await buildProjectBundle( + [ + { kind: 'agent', path: 'u/admin/video_agent' }, + { kind: 'flow', path: 'f/proj/uses_agent' } + ], + 'proj', + { + fetchItem: async (ref: ItemRef) => + ref.kind === 'agent' ? agent : ref.kind === 'flow' ? flow : helper, + resolveResourceType: async (path: string) => + path === 'u/admin/video_agent' ? 'ai_agent' : 'some_type' + } + ) + expect(bundle.items.map((i) => `${i.kind}:${i.newPath}`).sort()).toEqual([ + 'agent:f/proj/video_agent', + 'flow:f/proj/uses_agent', + 'script:f/proj/helper' + ]) + expect(bundle.resourceStubs.map((r) => r.resource_type)).not.toContain('ai_agent') + const shipped = bundle.items.find((i) => i.kind === 'agent')! + expect(shipped.value.tools[0].value.path).toBe('f/proj/helper') + expect(shipped.value.provider.resource).toBe('$res:f/proj/claude') + }) + + it('follows a retargeted export into another folder', () => { + const exp: ProjectExport = { + project: { slug: 'proj', name: 'P', summary: '', readme: null }, + scripts: [{ path: 'f/proj/helper', content: '' }], + flows: [], + apps: [], + resources: [{ path: 'f/proj/claude', resource_type: 'anthropic' }], + triggers: [], + agents: [ + { + path: 'f/proj/video_agent', + value: { + provider: { kind: 'anthropic', resource: '$res:f/proj/claude' }, + tools: [{ id: 'a', value: { type: 'script', path: 'f/proj/helper' } }] + } + } + ] + } + const out = retargetProjectExport(exp, 'proj', 'mine') + expect(out.agents?.[0].path).toBe('f/mine/video_agent') + expect(out.agents?.[0].value.provider.resource).toBe('$res:f/mine/claude') + expect(out.agents?.[0].value.tools[0].value.path).toBe('f/mine/helper') + }) + + it('ships and rewrites the resource an MCP tool connects to', () => { + const value = { + provider: { kind: 'anthropic', resource: '$res:f/proj/claude' }, + tools: [ + { id: 'm', value: { tool_type: 'mcp', resource_path: '$res:f/proj/mcp' } }, + { id: 'n', value: { tool_type: 'mcp', resource_path: 'f/proj/other_mcp' } } + ] + } + expect(extractAgentRefs(value)).toEqual( + expect.arrayContaining([ + { kind: 'resource', path: 'f/proj/mcp' }, + { kind: 'resource', path: 'f/proj/other_mcp' } + ]) + ) + const map = new Map([ + ['f/proj/mcp', 'f/mine/mcp'], + ['f/proj/other_mcp', 'f/mine/other_mcp'] + ]) + const out = rewriteAgentValue(value, map) + expect(out.tools[0].value.resource_path).toBe('$res:f/mine/mcp') + expect(out.tools[1].value.resource_path).toBe('f/mine/other_mcp') + }) + + it('relocates a variable only an agent uses', () => { + const exp: ProjectExport = { + project: { slug: 'proj', name: 'P', summary: '', readme: null }, + scripts: [{ path: 'f/proj/helper', content: '' }], + flows: [], + apps: [], + resources: [], + triggers: [], + agents: [ + { + path: 'f/proj/video_agent', + value: { + tools: [ + { + id: 'a', + value: { + type: 'script', + path: 'f/proj/helper', + input_transforms: { region: { type: 'static', value: '$var:f/proj/region' } } + } + } + ] + } + } + ] + } + expect(collectExportVarPaths(exp)).toContain('f/proj/region') + const out = retargetProjectExport(exp, 'proj', 'mine') + expect(out.agents?.[0].value.tools[0].value.input_transforms.region.value).toBe( + '$var:f/mine/region' + ) + }) + + it('ships and rewrites the MCP server of a nested agent tool', () => { + const value = { + tools: [ + { + id: 'inner', + value: { + tool_type: 'flowmodule', + type: 'aiagent', + input_transforms: {}, + tools: [{ id: 'm', value: { tool_type: 'mcp', resource_path: '$res:f/proj/mcp' } }] + } + } + ] + } + expect(extractAgentRefs(value)).toContainEqual({ kind: 'resource', path: 'f/proj/mcp' }) + const out = rewriteAgentValue(value, new Map([['f/proj/mcp', 'f/mine/mcp']])) + expect(out.tools[0].value.tools[0].value.resource_path).toBe('$res:f/mine/mcp') + }) + + it('relocates the saved agent a tool is linked to', () => { + const exp: ProjectExport = { + project: { slug: 'proj', name: 'P', summary: '', readme: null }, + scripts: [], + flows: [], + apps: [], + resources: [], + triggers: [], + agents: [ + { + path: 'f/proj/parent', + value: { + tools: [ + { + id: 'child', + value: { + tool_type: 'flowmodule', + type: 'aiagent', + agent: '$res:f/proj/child', + input_transforms: {} + } + } + ] + } + }, + { path: 'f/proj/child', value: { tools: [] } } + ] + } + const out = retargetProjectExport(exp, 'proj', 'mine') + expect(out.agents?.[0].value.tools[0].value.agent).toBe('$res:f/mine/child') + }) + + it('ships a linked agent from outside the folder whole, not as a stub', async () => { + const parent: FetchedItem = { + kind: 'agent', + path: 'f/proj/parent', + value: { + tools: [ + { + id: 'child', + value: { + tool_type: 'flowmodule', + type: 'aiagent', + agent: 'u/admin/child', + input_transforms: {} + } + } + ] + } + } + const child: FetchedItem = { kind: 'agent', path: 'u/admin/child', value: { tools: [] } } + const bundle = await buildProjectBundle([{ kind: 'agent', path: 'f/proj/parent' }], 'proj', { + fetchItem: async (ref: ItemRef) => (ref.path === 'u/admin/child' ? child : parent), + resolveResourceType: async (path: string) => + path === 'u/admin/child' ? 'ai_agent' : 'some_type' + }) + expect(bundle.items.map((i) => `${i.kind}:${i.newPath}`).sort()).toEqual([ + 'agent:f/proj/child', + 'agent:f/proj/parent' + ]) + expect(bundle.resourceStubs).toEqual([]) + const shipped = bundle.items.find((i) => i.newPath === 'f/proj/parent')! + expect(shipped.value.tools[0].value.agent).toBe('f/proj/child') + }) + + // A saved agent is read unvalidated, so no list of fields is kept: these pin the places + // reviews found one by one, and one nobody has named yet. + it('finds and rewrites a reference wherever it sits in the value', () => { + const value = { + provider: { kind: 'anthropic', resource: '$res:f/proj/claude' }, + output_schema: '$res:f/proj/answer_schema', + memory: { kind: 'auto', store: { resource: 'res://f/proj/store' } }, + some_future_key: [{ deep: { ref: '$res:f/proj/later' } }], + tools: [ + { + id: 'child', + value: { + tool_type: 'flowmodule', + type: 'aiagent', + agent: 'f/proj/child', + input_transforms: {}, + tool_inputs: { + query: { db: { type: 'static', value: { conn: '$res:f/proj/db' } } } + } + } + }, + { + id: 'code', + value: { + type: 'rawscript', + content: 'const k = "$res:f/proj/inline"', + input_transforms: {} + } + } + ] + } + const resources = extractAgentRefs(value) + .filter((r) => r.kind === 'resource') + .map((r) => r.path) + .sort() + expect(resources).toEqual([ + 'f/proj/answer_schema', + 'f/proj/child', + 'f/proj/claude', + 'f/proj/db', + 'f/proj/inline', + 'f/proj/later', + 'f/proj/store' + ]) + + const map = new Map(resources.map((p) => [p, p.replace('f/proj/', 'f/mine/')])) + const out = rewriteAgentValue(value, map) + expect(JSON.stringify(out)).not.toContain('f/proj/') + expect(out.output_schema).toBe('$res:f/mine/answer_schema') + expect(out.memory.store.resource).toBe('res://f/mine/store') + expect(out.some_future_key[0].deep.ref).toBe('$res:f/mine/later') + expect(out.tools[0].value.agent).toBe('f/mine/child') + expect(out.tools[0].value.tool_inputs.query.db.value.conn).toBe('$res:f/mine/db') + expect(out.tools[1].value.content).toBe('const k = "$res:f/mine/inline"') + // The source value is left as it was. + expect(value.output_schema).toBe('$res:f/proj/answer_schema') + }) + + it('reads prose and data as what they are, not as references', () => { + const value = { + // The worker resolves a value that is a reference as a whole, never a token in a + // sentence: a prompt explaining the syntax must not block the publish. + system_prompt: 'Pass resources as $res:u/user/name strings, e.g. $res:f/team/pg.', + memory: { messages: [{ role: 'user', content: 'use $res:u/someone/else' }] }, + tools: [ + { + id: 't', + value: { + type: 'script', + path: 'f/proj/helper', + input_transforms: { + // A JSON argument that looks like a tool is an argument. + arg: { type: 'static', value: { type: 'flow', path: 'u/other/thing' } }, + // Evaluated, not resolved. + x: { type: 'javascript', expr: '"$res:f/proj/not_a_ref"' } + } + } + } + ] + } + expect(extractAgentRefs(value)).toEqual([{ kind: 'script', path: 'f/proj/helper' }]) + const out = rewriteAgentValue(value, new Map([['u/other/thing', 'f/mine/thing']])) + expect(out).toEqual(value) + }) + + it('hides nothing beside a javascript or static input', () => { + // The install check walks the same way: keys set next to `expr` or `value` to make an + // object look like an input must still be read. + const value = { + type: 'javascript', + expr: '', + provider: { kind: 'openai', resource: '$res:u/admin/prod_openai' }, + tools: [ + { + id: 't', + type: 'static', + value: { tool_type: 'flowmodule', type: 'script', path: 'u/admin/danger' } + }, + { id: 'm', value: { tool_type: 'mcp', resource_path: 'u/admin/mcp' } } + ] + } + const paths = extractAgentRefs(value).map((r) => r.path) + expect(paths.sort()).toEqual(['u/admin/danger', 'u/admin/mcp', 'u/admin/prod_openai']) + }) + + it('keeps an MCP server enabled under its new path', () => { + const flow = { + modules: [ + { + id: 'a', + value: { + type: 'aiagent', + tools: [{ id: 'm', value: { tool_type: 'mcp', resource_path: '$res:u/me/mcp' } }], + input_transforms: { + enabled_tools: { type: 'static', value: ['u/me/mcp', 'lookup'] } + } + } + } + ] + } + const map = new Map([['u/me/mcp', 'f/proj/mcp']]) + const out = rewriteFlowValue(flow, map) + expect(out.modules[0].value.tools[0].value.resource_path).toBe('$res:f/proj/mcp') + expect(out.modules[0].value.input_transforms.enabled_tools.value).toEqual([ + 'f/proj/mcp', + 'lookup' + ]) + // The same on an agent used as a tool by a saved agent. + const nested = rewriteAgentValue( + { tools: [{ id: 'inner', value: flow.modules[0].value }] }, + map + ) + expect(nested.tools[0].value.input_transforms.enabled_tools.value).toEqual([ + 'f/proj/mcp', + 'lookup' + ]) + }) + + it('ships an agent a trigger already named whole, not as a stub', async () => { + const flow: FetchedItem = { + kind: 'flow', + path: 'f/proj/uses_agent', + value: { + modules: [ + { id: 'a', value: { type: 'aiagent', agent: 'u/me/agent', input_transforms: {} } } + ] + } + } + const agent: FetchedItem = { kind: 'agent', path: 'u/me/agent', value: { tools: [] } } + const bundle = await buildProjectBundle( + [{ kind: 'flow', path: 'f/proj/uses_agent' }], + 'proj', + { + fetchItem: async (ref: ItemRef) => (ref.kind === 'agent' ? agent : flow), + resolveResourceType: async () => 'ai_agent' + }, + ['u/me/agent'] + ) + expect(bundle.items.map((i) => `${i.kind}:${i.newPath}`).sort()).toEqual([ + 'agent:f/proj/agent', + 'flow:f/proj/uses_agent' + ]) + expect(bundle.resourceStubs).toEqual([]) + }) + + it('follows the tool wiring a linked agent step holds in a flow', () => { + const flow = { + modules: [ + { + id: 'a', + value: { + type: 'aiagent', + agent: '$res:f/proj/helpdesk', + input_transforms: {}, + tools: [], + tool_inputs: { + lookup: { db: { type: 'static', value: '$res:f/proj/db' } } + } + } + } + ] + } + expect(extractFlowRefs(flow)).toEqual( + expect.arrayContaining([ + { kind: 'resource', path: 'f/proj/helpdesk' }, + { kind: 'resource', path: 'f/proj/db' } + ]) + ) + const out = rewriteFlowValue( + flow, + new Map([ + ['f/proj/helpdesk', 'f/mine/helpdesk'], + ['f/proj/db', 'f/mine/db'] + ]) + ) + expect(out.modules[0].value.agent).toBe('$res:f/mine/helpdesk') + expect(out.modules[0].value.tool_inputs.lookup.db.value).toBe('$res:f/mine/db') + }) + + it('drops who an agent runs as, whoever wrote the value', () => { + const out = withoutAgentWorkspaceKeys({ + system_prompt: 'x', + on_behalf_of: 'u/someone', + run_on_behalf_of: true, + slack_anyone: true + }) + expect(out).toEqual({ system_prompt: 'x' }) + expect(withoutAgentWorkspaceKeys(undefined)).toEqual({}) + }) +}) diff --git a/frontend/src/lib/components/workspaceSettings/projectBundle.ts b/frontend/src/lib/components/workspaceSettings/projectBundle.ts index e13924c755dec..0805b9933122f 100644 --- a/frontend/src/lib/components/workspaceSettings/projectBundle.ts +++ b/frontend/src/lib/components/workspaceSettings/projectBundle.ts @@ -9,6 +9,17 @@ import { isRunnableByPath } from '$lib/components/apps/inputType' export type RefKind = 'resource' | 'script' | 'flow' +// Who an agent runs as and who may use it belong to the workspace it is deployed in: an +// export must not carry them, and an install must not trust one that does, or the agent +// would run its tools as whoever installed it for anyone who can read it. +const AGENT_WORKSPACE_KEYS = ['on_behalf_of', 'run_on_behalf_of', 'slack_anyone'] + +export function withoutAgentWorkspaceKeys(value: unknown): Record { + const out = { ...((value ?? {}) as Record) } + for (const key of AGENT_WORKSPACE_KEYS) delete out[key] + return out +} + export interface Ref { kind: RefKind /** Bare path, without the `$res:` / `res://` prefix for resources. */ @@ -66,12 +77,137 @@ export function extractScriptRefs(content: string): Ref[] { return out } +/** A resource path written bare or as a `$res:` / `res://` token, without its prefix. */ +function bareResourcePath(ref: unknown): string | undefined { + if (typeof ref !== 'string') return undefined + return ref.replace(/^\$res:/, '').replace(/^res:\/\//, '') || undefined +} + +/** `next` written the way `ref` was: bare, `$res:` or `res://`. */ +function withRefPrefix(ref: string, next: string): string { + const prefix = /^(\$res:|res:\/\/)/.exec(ref)?.[1] ?? '' + return prefix + next +} + +/** A whole-string `$res:PATH` / `res://PATH` value (prefix in group 1, path in group 2). */ +const RES_VALUE_RE = /^(\$res:|res:\/\/)(.+)$/ + +type AgentRefResolver = (kind: RefKind, path: string) => string | undefined +type AgentWalkContext = 'config' | 'inputs' | 'toolInputs' | 'input' | 'data' + +// The single definition of what an agent's configuration references, for reading +// (`resolve` returns nothing) and rewriting (it returns the new path) alike. No list of +// fields: a saved agent is read unvalidated and tools nest, so shapes are matched at any depth. +// The install containment check relies on this walk seeing everything the worker acts on. +function walkAgentConfig(value: any, resolve: AgentRefResolver): any { + const walk = (v: any, ctx: AgentWalkContext): any => { + if (typeof v === 'string') { + // Whole-string only, as the worker resolves arguments: a token in prose is inert. + const m = RES_VALUE_RE.exec(v) + const next = m ? resolve('resource', m[2]) : undefined + return next !== undefined ? m![1] + next : v + } + if (Array.isArray(v)) return v.map((x) => walk(x, ctx === 'data' ? 'data' : 'config')) + if (!v || typeof v !== 'object') return v + + const out: Record = {} + for (const k of Object.keys(v)) { + // Only an entry of `input_transforms` (or of a tool's map under `tool_inputs`) is an + // input: its static `value` is argument data and its JavaScript `expr` is never + // resolved. The same keys anywhere else are configuration, or a crafted export + // could hide a tool beside them. + let child: AgentWalkContext = 'config' + if (ctx === 'data') child = 'data' + else if (ctx === 'inputs') child = 'input' + else if (ctx === 'toolInputs') child = 'inputs' + else if (ctx === 'input' && v.type === 'static' && k === 'value') child = 'data' + else if (ctx === 'input' && v.type === 'javascript' && k === 'expr') { + out[k] = v[k] + continue + } else if (k === 'input_transforms') child = 'inputs' + else if (k === 'tool_inputs') child = 'toolInputs' + out[k] = walk(v[k], child) + } + if (ctx !== 'config' && ctx !== 'input') return out + + if ((v.type === 'script' || v.type === 'flow') && typeof v.path === 'string') { + out.path = resolve(v.type, v.path) ?? v.path + } + if (v.type === 'aiagent') { + // Linked to a saved agent, by bare path or `$res:`. + const linked = bareResourcePath(v.agent) + const next = linked ? resolve('resource', linked) : undefined + if (next !== undefined) out.agent = withRefPrefix(v.agent, next) + } + if (v.tool_type === 'mcp') { + const mcp = bareResourcePath(v.resource_path) + const next = mcp ? resolve('resource', mcp) : undefined + if (next !== undefined) out.resource_path = withRefPrefix(v.resource_path, next) + } + if (v.type === 'rawscript' && typeof v.content === 'string') { + out.content = v.content.replace(RES_TOKEN_RE, (whole: string, path: string) => { + const next = resolve('resource', path) + return next !== undefined ? `$res:${next}` : whole + }) + } + return out + } + return walk(value, 'config') +} + +function collectAgentConfigRefs(value: any, add: (kind: RefKind, path: string) => void): void { + walkAgentConfig(value, (kind, path) => { + add(kind, path) + return undefined + }) +} + +/** Returns a rewritten copy. */ +function rewriteAgentConfig(value: any, map: Map, only?: RefKind): any { + const out = walkAgentConfig(value, (kind, path) => + only === undefined || kind === only ? map.get(path) : undefined + ) + // `enabled_tools` names an MCP server by its bare resource path and the worker matches + // names exactly: the list must follow the server, or it silently leaves the roster. + const fixEnabled = (v: any) => { + if (Array.isArray(v)) v.forEach(fixEnabled) + else if (v && typeof v === 'object') { + const enabled = v.type === 'aiagent' ? v.input_transforms?.enabled_tools : undefined + if (enabled?.type === 'static' && Array.isArray(enabled.value)) { + enabled.value = enabled.value.map((name: unknown) => + typeof name === 'string' ? (map.get(name) ?? name) : name + ) + } + for (const k of Object.keys(v)) fixEnabled(v[k]) + } + } + fixEnabled(out) + return out +} + +// What `walkAgentConfig` must read on an `aiagent` flow step. Its module tools are left +// out (`null` keeps the indexes): the flow walk visits those as modules. +function agentStepConfig(v: any): any { + return { + type: 'aiagent', + agent: v.agent, + tools: (Array.isArray(v.tools) ? v.tools : []).map((t: any) => + t?.value?.tool_type === 'mcp' ? t : null + ), + tool_inputs: v.tool_inputs, + input_transforms: { enabled_tools: v.input_transforms?.enabled_tools } + } +} + /** * References inside a flow value: * - inline rawscript code with `$res:` (resource) * - static step inputs whose value is a `$res:` literal (resource) * - `type: script` steps that reference a script by path (script) * - `type: flow` steps that reference a sub-flow by path (flow) + * - on an `aiagent` step or tool: the saved agent it is linked to (resource, shipped whole + * as an agent), its MCP servers (resource), and the tool wiring a linked step lays over + * the agent's tools (`tool_inputs`) */ export function extractFlowRefs(value: any): Ref[] { const out: Ref[] = [] @@ -91,6 +227,7 @@ export function extractFlowRefs(value: any): Ref[] { if (!v || typeof v !== 'object') continue if (v.type === 'script' && typeof v.path === 'string') add('script', v.path) if (v.type === 'flow' && typeof v.path === 'string') add('flow', v.path) + if (v.type === 'aiagent') collectAgentConfigRefs(agentStepConfig(v), add) if (typeof v.content === 'string') { for (const r of extractScriptRefs(v.content)) add('resource', r.path) } @@ -385,6 +522,17 @@ export function rewriteFlowValue(value: any, map: Map): any { ) { v.path = map.get(v.path) } + if (v.type === 'aiagent') { + const next = rewriteAgentConfig(agentStepConfig(v), map) + if (v.agent !== undefined) v.agent = next.agent + next.tools.forEach((tool: any, i: number) => { + if (tool) v.tools[i] = tool + }) + if (v.tool_inputs !== undefined) v.tool_inputs = next.tool_inputs + if (v.input_transforms?.enabled_tools !== undefined) { + v.input_transforms.enabled_tools = next.input_transforms.enabled_tools + } + } if (typeof v.content === 'string') v.content = rewriteContent(v.content, map) const it = v.input_transforms if (it && typeof it === 'object') { @@ -445,6 +593,32 @@ export function rewriteRawAppContent(content: string, map: Map): return JSON.stringify(rewriteAppValue(parsed, map)) } +// A saved agent (`ai_agent` resource value), walked whole: see `walkAgentConfig`. +export function extractAgentRefs(value: any): Ref[] { + const out: Ref[] = [] + const seen = new Set() + collectAgentConfigRefs(value, (kind, path) => { + const key = `${kind}:${path}` + if (!seen.has(key)) { + seen.add(key) + out.push({ kind, path }) + } + }) + return out +} + +// Moves resource references only. Scripts, flows and resources share a namespace, so a map +// holding one resource path must not repoint a tool that runs a script sharing that path. +export function rewriteAgentResourceRefs(value: any, map: Map): any { + if (!value || typeof value !== 'object') return value + return rewriteAgentConfig(value, map, 'resource') +} + +export function rewriteAgentValue(value: any, map: Map): any { + if (!value || typeof value !== 'object') return value + return rewriteAgentConfig(value, map) +} + // --------------------------------------------------------------------------- // Hub project export format (what /projects/{slug}/export returns) and its // retargeting into a destination folder. Kept here, next to the rewriters, @@ -466,6 +640,9 @@ export interface ProjectExport { resources: ExportItem[] triggers: ExportItem[] migrations?: ProjectMigration[] + /** AI agents the project ships: `ai_agent` resources published with their value. + * Absent from exports made before agents were published. */ + agents?: ExportItem[] } // Map bundled paths `f//...` -> `f//...`. Only enumerated @@ -486,6 +663,7 @@ export function buildRetargetMap( for (const f of bundle.flows) add(f.path) for (const a of bundle.apps) add(a.path) for (const r of bundle.resources) add(r.path) + for (const a of bundle.agents ?? []) add(a.path) for (const t of bundle.triggers) { add(t.path) add(t.runnable_path) @@ -497,8 +675,8 @@ export function buildRetargetMap( return map } -// Internal-or-external variable paths referenced by the export's flows, apps and -// triggers. Scripts carry no variable args. Raw apps hold their structure in the +// Internal-or-external variable paths referenced by the export's flows, apps, +// triggers and agents. Scripts carry no variable args. Raw apps hold their structure in the // `value.raw` JSON string. export function collectExportVarPaths(bundle: ProjectExport): string[] { const out = new Set() @@ -508,6 +686,7 @@ export function collectExportVarPaths(bundle: ProjectExport): string[] { for (const f of bundle.flows) collect(f.value) for (const a of bundle.apps) collect(a.app_type === 'raw' ? safeParseRaw(a.value?.raw) : a.value) for (const t of bundle.triggers) collect(t.config) + for (const a of bundle.agents ?? []) collect(a.value) return [...out] } @@ -555,6 +734,15 @@ export function retargetProjectExport( : rewriteVarRefsInValue(rewriteAppValue(a.value, map), map) })), resources: bundle.resources.map((r) => ({ ...r, path: remap(r.path) })), + ...(bundle.agents + ? { + agents: bundle.agents.map((a) => ({ + ...a, + path: remap(a.path), + value: rewriteVarRefsInValue(rewriteAgentValue(a.value, map), map) + })) + } + : {}), triggers: bundle.triggers.map((t) => ({ ...t, path: remap(t.path), @@ -574,7 +762,8 @@ function rewriteRawVarRefs(raw: string, map: Map): string { return JSON.stringify(rewriteVarRefsInValue(parsed, map)) } -export type ItemKind = 'script' | 'flow' | 'app' | 'raw_app' +// `agent` is a saved AI agent: an `ai_agent` resource, published whole rather than as a stub. +export type ItemKind = 'script' | 'flow' | 'app' | 'raw_app' | 'agent' export interface ItemRef { kind: ItemKind @@ -588,7 +777,7 @@ export interface FetchedItem { description?: string /** scripts + raw_apps */ content?: string - /** flows + apps */ + /** flows + apps + agents */ value?: any /** scripts */ language?: string @@ -629,19 +818,23 @@ function refsForFetched(item: FetchedItem): Ref[] { if (item.kind === 'flow') return extractFlowRefs(item.value) if (item.kind === 'app') return extractAppRefs(item.value) if (item.kind === 'raw_app') return extractRawAppRefs(item.content ?? '') + if (item.kind === 'agent') return extractAgentRefs(item.value) return [] } // Whole-string `$var:`/`$jsonvar:` paths an item resolves at runtime. Scripts carry // no variable args; raw apps hold their structure in the `content` JSON string. function varRefsForFetched(item: FetchedItem): string[] { - if (item.kind === 'flow' || item.kind === 'app') return extractVarRefsFromValue(item.value) + if (item.kind === 'flow' || item.kind === 'app' || item.kind === 'agent') + return extractVarRefsFromValue(item.value) if (item.kind === 'raw_app') return extractVarRefsFromValue(safeParseRaw(item.content)) return [] } // Walks the transitive closure: scripts referenced by path are pulled in -// recursively, resources become empty stubs, hub refs stay external. +// recursively, resources become empty stubs, hub refs stay external. A referenced +// `ai_agent` resource is the exception: it is pulled in as an agent, whole, with what +// its own tools reference. export async function buildProjectBundle( seed: ItemRef[], slug: string, @@ -666,6 +859,30 @@ export async function buildProjectBundle( // Key by `${kind}:${path}`, not bare path: a script and flow can share a path, // and keying by path alone would silently drop one. const refKey = (kind: string, path: string) => `${kind}:${path}` + // Resource types, looked up once: a referenced `ai_agent` resource is an agent the + // project ships whole (with what its tools run), not an empty stub. + const types = new Map>() + const typeOf = (path: string) => { + if (!types.has(path)) types.set(path, deps.resolveResourceType(path)) + return types.get(path)! + } + // Agents among `paths`, as items to fetch. Asked once per path, whoever named it first: + // a trigger's resource is checked like one an item references. + const promoted = new Set() + const agentsAmong = async (paths: string[]): Promise => { + const fresh = [...new Set(paths)].filter((p) => !promoted.has(p)) + for (const p of fresh) promoted.add(p) + const found = await Promise.all(fresh.map(typeOf)) + const out: ItemRef[] = [] + fresh.forEach((path, i) => { + const key = refKey('agent', path) + if (found[i] === 'ai_agent' && !queued.has(key)) { + queued.add(key) + out.push({ kind: 'agent', path }) + } + }) + return out + } // Refs at the same BFS depth are independent: fetch each level concurrently. let level: ItemRef[] = [] @@ -676,11 +893,13 @@ export async function buildProjectBundle( level.push(s) } } + level.push(...(await agentsAmong([...resourcePaths]))) while (level.length > 0) { const results = await Promise.all( level.map(async (ref) => ({ ref, item: await deps.fetchItem(ref) })) ) const next: ItemRef[] = [] + const newResources: string[] = [] for (const { ref, item } of results) { if (!item) { unresolved.push(ref.path) @@ -690,6 +909,7 @@ export async function buildProjectBundle( for (const r of refsForFetched(item)) { if (classifyPath(r.path, slug) === 'hub') continue if (r.kind === 'resource') { + newResources.push(r.path) resourcePaths.add(r.path) } else if (r.kind === 'script' || r.kind === 'flow') { const key = refKey(r.kind, r.path) @@ -704,6 +924,7 @@ export async function buildProjectBundle( // materializes them as placeholders). Variables are never hub-hosted. for (const p of varRefsForFetched(item)) varPaths.add(p) } + next.push(...(await agentsAmong(newResources))) level = next } @@ -721,13 +942,18 @@ export async function buildProjectBundle( rewritten.value = rewriteVarRefsInValue(rewriteFlowValue(it.value, map), map) } else if (it.kind === 'app') { rewritten.value = rewriteVarRefsInValue(rewriteAppValue(it.value, map), map) + } else if (it.kind === 'agent') { + rewritten.value = rewriteVarRefsInValue(rewriteAgentValue(it.value, map), map) } return rewritten }) const resourceStubs: ResourceStub[] = [] + // An agent the bundle ships is published whole: an app or a flow step naming it must not + // also turn it into an empty stub at the same path. + for (const it of fetchedItems) if (it.kind === 'agent') resourcePaths.delete(it.path) const resolved = await Promise.all( - [...resourcePaths].map(async (path) => ({ path, type: await deps.resolveResourceType(path) })) + [...resourcePaths].map(async (path) => ({ path, type: await typeOf(path) })) ) for (const { path, type } of resolved) { if (!type) { diff --git a/frontend/src/lib/components/workspaceSettings/projectInstall.ts b/frontend/src/lib/components/workspaceSettings/projectInstall.ts index 6fd4bdd7282bf..23a6b197dc37e 100644 --- a/frontend/src/lib/components/workspaceSettings/projectInstall.ts +++ b/frontend/src/lib/components/workspaceSettings/projectInstall.ts @@ -28,6 +28,8 @@ import { classifyPath, collectExportVarPaths, extractAppRefs, + extractAgentRefs, + withoutAgentWorkspaceKeys, extractFlowRefs, extractRawAppRefs, extractScriptRefs, @@ -68,7 +70,7 @@ export function refContainmentViolation(refs: Ref[], folder: string): string | u } // `$var:`/`$jsonvar:` references (in flow static inputs, flow_env, app runnable -// inputs, trigger config) are resolved at runtime under the imported runnable's +// inputs, trigger config, an agent's value) are resolved at runtime under the imported runnable's // permissions and are never hub-hosted. Retargeting relocates a project's own refs // into the target folder; anything still outside it points at another namespace, so // reject those. Takes the parsed value so inline code carrying a literal is ignored. @@ -141,6 +143,22 @@ function importResourceStub(workspace: string, r: ExportItem): Promise }) } +// An agent is the one resource a project ships whole: its prompt, model and tools are the +// product, and it holds no credential, only `$res:` references to stubs created above. +// Never overwrites: a path collision is reported as a failed item, like a stub's. +function importAgent(workspace: string, a: ExportItem): Promise { + return ResourceService.createResource({ + workspace, + updateIfExists: false, + requestBody: { + path: a.path, + resource_type: 'ai_agent', + value: withoutAgentWorkspaceKeys(a.value), + description: a.description ?? a.summary ?? '' + } + }) +} + // Variables hold secrets/config, so their values are never shipped. Create an empty // secret placeholder for a project variable the importer must fill, mirroring the // resource stubs. Conflict-safe: an already-present variable (the importer filled it, @@ -272,12 +290,7 @@ export async function applyOneMigration( * called `f/cal/sync`. Flattening them to `trigger` would let whichever exists answer for * the other. */ -export type ImportedKind = - | 'script' - | 'flow' - | 'app' - | 'resource' - | `trigger:${string}` +export type ImportedKind = 'script' | 'flow' | 'app' | 'resource' | `trigger:${string}` /** * The key `alreadyPresent` is built and read with. Kind and path together, because the kinds @@ -408,6 +421,12 @@ export async function installProject(args: { if (present('resource', r.path)) continue await checked(r.path, () => importResourceStub(workspace, r)) } + // After the stubs: an agent's provider and its tools' inputs point at them. + for (const a of proj.agents ?? []) { + if (halted()) return + if (present('resource', a.path)) continue + await checkedItem(a.path, extractAgentRefs(a.value), a.value, () => importAgent(workspace, a)) + } // Placeholders for the project's internal `$var:`/`$jsonvar:` refs (retargeted // into this folder). External refs are rejected per-item, so only stub in-folder // ones; guard again in case an out-of-folder ref slipped through retargeting. diff --git a/frontend/src/lib/importWizard/execution.svelte.ts b/frontend/src/lib/importWizard/execution.svelte.ts index 12952525b5886..1240b5a831c6e 100644 --- a/frontend/src/lib/importWizard/execution.svelte.ts +++ b/frontend/src/lib/importWizard/execution.svelte.ts @@ -539,5 +539,7 @@ export class ImportExecution { } function itemCount(e: ProjectExport): number { - return e.scripts.length + e.flows.length + e.apps.length + e.resources.length + return ( + e.scripts.length + e.flows.length + e.apps.length + e.resources.length + (e.agents?.length ?? 0) + ) } diff --git a/frontend/src/lib/importWizard/retargetDeployed.test.ts b/frontend/src/lib/importWizard/retargetDeployed.test.ts index adf7a4c795b6c..64d02b000f30f 100644 --- a/frontend/src/lib/importWizard/retargetDeployed.test.ts +++ b/frontend/src/lib/importWizard/retargetDeployed.test.ts @@ -19,7 +19,9 @@ const state = vi.hoisted(() => ({ updatedApps: [] as any[], updatedFlows: [] as any[], flows: [] as any[], - updatedTriggers: [] as any[] + updatedTriggers: [] as any[], + agents: [] as any[], + updatedAgents: [] as any[] })) vi.mock('$lib/gen', () => ({ @@ -42,7 +44,12 @@ vi.mock('$lib/gen', () => ({ }, ScheduleService: { updateSchedule: vi.fn() }, ResourceService: { - deleteResource: vi.fn(async ({ path }: any) => state.deletedResources.push(path)) + deleteResource: vi.fn(async ({ path }: any) => state.deletedResources.push(path)), + listResource: vi.fn(async () => state.agents.map((a: any) => ({ path: a.path }))), + getResourceValue: vi.fn( + async ({ path }: any) => state.agents.find((a: any) => a.path === path)?.value + ), + updateResource: vi.fn(async (p: any) => state.updatedAgents.push(p)) } })) @@ -118,6 +125,8 @@ describe('applyRetarget', () => { state.flows = [] state.updatedFlows = [] state.updatedTriggers = [] + state.agents = [] + state.updatedAgents = [] }) // `updateAppRaw` refuses without a bundle, and the browser cannot rebuild one. The bundle @@ -375,6 +384,80 @@ describe('applyRetarget', () => { expect(outcome.stubDeleted).toBe(false) expect(state.deletedResources).toEqual([]) }) + it("moves an imported agent's provider and MCP server, keeping who it runs as", async () => { + state.apps = [] + state.agents = [ + { + path: 'f/proj/helpdesk', + value: { + on_behalf_of: 'u/service', + provider: { kind: 'smtp', resource: `$res:${FROM}` }, + tools: [ + { id: 'm', value: { tool_type: 'mcp', resource_path: FROM } }, + // A script sharing the path is not the resource being moved. + { id: 's', value: { type: 'script', path: FROM, input_transforms: {} } } + ] + } + } + ] + const out = await run() + const sent = state.updatedAgents[0].requestBody + expect(sent.preserve_on_behalf_of).toBe(true) + expect(sent.value.on_behalf_of).toBe('u/service') + expect(sent.value.provider.resource).toBe(`$res:${TO}`) + expect(sent.value.tools[0].value.resource_path).toBe(TO) + expect(sent.value.tools[1].value.path).toBe(FROM) + expect(out.rewritten.map((r) => r.kind)).toEqual(['agent']) + // The script tool still names the path, which no rewrite here may touch. + expect(out.stubDeleted).toBe(false) + }) + + it("keeps a nested agent's MCP server enabled under the resource it moved to", async () => { + state.apps = [] + state.agents = [ + { + path: 'f/proj/helpdesk', + value: { + tools: [ + { + id: 'inner', + value: { + tool_type: 'flowmodule', + type: 'aiagent', + tools: [{ id: 'm', value: { tool_type: 'mcp', resource_path: FROM } }], + input_transforms: { enabled_tools: { type: 'static', value: [FROM, 'lookup'] } } + } + } + ] + } + } + ] + const out = await run() + const inner = state.updatedAgents[0].requestBody.value.tools[0].value + expect(inner.tools[0].value.resource_path).toBe(TO) + expect(inner.input_transforms.enabled_tools.value).toEqual([TO, 'lookup']) + expect(out.stubDeleted).toBe(true) + }) + + it('drops the stub once the only agent reading it has moved', async () => { + state.apps = [] + state.agents = [ + { path: 'f/proj/helpdesk', value: { provider: { resource: `$res:${FROM}` }, tools: [] } } + ] + const out = await run() + expect(out.stubDeleted).toBe(true) + expect(state.deletedResources).toEqual([FROM]) + }) + + it('keeps the stub for an agent outside the project that reads it', async () => { + state.apps = [] + state.agents = [ + { path: 'u/someone/agent', value: { provider: { resource: `$res:${FROM}` }, tools: [] } } + ] + const out = await run() + expect(state.updatedAgents).toEqual([]) + expect(out.stubDeleted).toBe(false) + }) }) describe('seesWholeWorkspace', () => { diff --git a/frontend/src/lib/importWizard/retargetDeployed.ts b/frontend/src/lib/importWizard/retargetDeployed.ts index b33a69a667e7a..c490048836b5d 100644 --- a/frontend/src/lib/importWizard/retargetDeployed.ts +++ b/frontend/src/lib/importWizard/retargetDeployed.ts @@ -26,6 +26,8 @@ import { AppService, FlowService, ResourceService, ScheduleService, ScriptService } from '$lib/gen' import { + extractAgentRefs, + rewriteAgentResourceRefs, rewriteContent, rewriteTriggerConfig, referencesResourcePath, @@ -42,7 +44,7 @@ import { updateRawAppPolicy } from '$lib/sharedUtils' import type { App } from '$lib/components/apps/types' import { apiErrorMessage as errorMessage } from '$lib/utils' -export type ReferrerKind = 'script' | 'flow' | 'app' | 'trigger' +export type ReferrerKind = 'script' | 'flow' | 'app' | 'trigger' | 'agent' export interface Referrer { kind: ReferrerKind @@ -190,6 +192,22 @@ function restoreRunnableRefs(rewritten: any, row: any): any { return out } +/** + * A saved agent (`ai_agent` resource) reads a resource as a `$res:` value or, for an MCP + * server, as a bare path: both are moved by `rewriteAgentResourceRefs`. The path left + * anywhere in the value after that is a mention no rewrite reaches. + */ +function agentReadsResource(value: unknown, path: string): boolean { + return extractAgentRefs(value).some((r) => r.kind === 'resource' && r.path === path) +} +function agentNamesPathUnreachably(value: unknown, path: string): boolean { + const moved = rewriteAgentResourceRefs(value, new Map([[path, '']])) + return namesPathUnreachably(moved, path) +} + +/** What the resource listing caps at when no pagination is passed (`DEFAULT_PER_PAGE`). */ +const AGENT_LIST_LIMIT = 1000 + /** A reference this run will not move, recorded so the stub outlives it. */ const OUTSIDE_PROJECT = 'reads this resource from outside the project' const UNSEEN_BY_CALLER = 'holds items this account is not shown' @@ -200,7 +218,8 @@ const UNREACHABLE_REFERENCE = 'names the resource path outside a $res: reference * could not account for. * * The `listSearch*` endpoints return each item's content in one call per kind, so this is - * three calls plus one per trigger kind rather than one per item. + * three calls plus one per trigger kind rather than one per item. Agents are the exception: + * their listing has no value, so each is read. */ export async function planRetarget( workspace: string, @@ -287,6 +306,39 @@ export async function planRetarget( referrers.push({ kind: 'app', path: a.path!, ...gapped }) } + // The listing carries no value, so each agent is read. One that cannot be listed or read + // may reference the stub unseen. + let agents: Array<{ path: string }> = [] + try { + agents = await ResourceService.listResource({ workspace, resourceType: 'ai_agent' }) + if (agents.length >= AGENT_LIST_LIMIT) { + gaps.push({ path: 'AI agents', reason: 'could not all be listed' }) + } + } catch { + gaps.push({ path: 'AI agents', reason: 'could not be listed' }) + } + for (const a of agents) { + let value: unknown + try { + value = await ResourceService.getResourceValue({ workspace, path: a.path }) + } catch { + gaps.push({ path: a.path, reason: 'could not be read' }) + continue + } + const reads = agentReadsResource(value, from) + const bare = agentNamesPathUnreachably(value, from) + if (!reads && !bare) continue + if (!inFolder(a.path, folder)) { + gaps.push({ path: a.path, reason: OUTSIDE_PROJECT }) + continue + } + if (bare) { + gaps.push({ path: a.path, reason: UNREACHABLE_REFERENCE }) + if (!reads) continue + } + referrers.push({ kind: 'agent', path: a.path, ...(bare ? { gapped: true as const } : {}) }) + } + for (const kind of WORKSPACE_TRIGGER_KINDS) { const def = TRIGGER_KINDS[kind] // No `eeOnly` skip. That reads a client-side store, which is empty on an EE instance @@ -363,6 +415,7 @@ export async function applyRetarget(args: { if (r.kind === 'script') moved = await rewriteScript(workspace, r, map) else if (r.kind === 'flow') moved = await rewriteFlow(workspace, r, map) else if (r.kind === 'app') moved = await rewriteApp(workspace, r, map) + else if (r.kind === 'agent') moved = await rewriteAgent(workspace, r, map) else moved = await rewriteTrigger(workspace, r, map) } catch (e: any) { return { rewritten, gaps, stubDeleted: false, error: errorMessage(e) } @@ -475,6 +528,30 @@ async function rewriteFlow( return true } +/** + * An agent's run identity is `value.on_behalf_of`, which the backend replaces with the + * writer's on every write unless told to keep it: the value goes back whole, with the flag. + */ +async function rewriteAgent( + workspace: string, + r: Referrer, + map: Map +): Promise { + const path = r.path + const value = await ResourceService.getResourceValue({ workspace, path }) + if (!r.gapped) + for (const from of map.keys()) + if (agentNamesPathUnreachably(value, from)) return UNREACHABLE_REFERENCE + const next = rewriteAgentResourceRefs(value, map) + if (JSON.stringify(next) === JSON.stringify(value ?? null)) return true + await ResourceService.updateResource({ + workspace, + path, + requestBody: { ...PRESERVE_DEPLOYED_IDENTITY, value: next } + }) + return true +} + /** * The deployed policy is recomputed, not rebuilt. Recomputing is required: `triggerables_v2` * is keyed by `:rawscript/`, and rewriting an inline diff --git a/frontend/src/routes/(root)/(logged)/folders/+page.svelte b/frontend/src/routes/(root)/(logged)/folders/+page.svelte index 0d2b5841e12e7..5c4823df01ab0 100644 --- a/frontend/src/routes/(root)/(logged)/folders/+page.svelte +++ b/frontend/src/routes/(root)/(logged)/folders/+page.svelte @@ -165,6 +165,7 @@ Scripts Flows Apps + Agents Schedules Variables Resources @@ -176,7 +177,7 @@ {#if folders === undefined} {#each new Array(4) as _} - +