From 05f5e069ea7136ff17478c6fa74656c416850ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20L=C3=A9cuyer?= Date: Tue, 17 Nov 2026 14:54:29 +0100 Subject: [PATCH 01/13] feat(hub): publish a folder's AI agents with their project A saved AI agent is an `ai_agent` resource, and the Hub bundle shipped every resource as an empty stub: a project built around an agent reached the Hub with the agent's tools and without the agent. - The publish screen lists a folder's agents as items. An agent is bundled like the one-step flow a run makes of it, so the scripts its tools run come along and its `$res:` references (provider, tools' fixed inputs) are relocated by the same code as a flow's. Its deploy identity and sharing switches stay here. - Agents are synced as a set after the items (`POST /hub/agents`). - Its demo is a conversation: "Record conversation" chats with the agent in a conversation of its own, one run per message, and saves each turn with its run (`POST /hub/projects/{slug}/agent_recording`). - Installing a project recreates its agents after the resource stubs they point at. Co-Authored-By: Claude Fable 5.2 --- backend/windmill-api/src/hub_publish.rs | 52 ++++ .../components/WorkspaceDeployLayout.svelte | 4 +- .../src/lib/components/recording/types.ts | 28 ++ .../AgentRecordSession.svelte | 137 +++++++++ .../workspaceSettings/DeployToHub.svelte | 69 ++++- .../workspaceSettings/deployToHubItems.ts | 12 +- .../deployToHubSession.svelte.ts | 270 +++++++++++++++++- .../workspaceSettings/projectBundle.test.ts | 138 +++++++++ .../workspaceSettings/projectBundle.ts | 62 +++- .../workspaceSettings/projectInstall.ts | 30 +- .../src/lib/importWizard/execution.svelte.ts | 4 +- 11 files changed, 784 insertions(+), 22 deletions(-) create mode 100644 frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte diff --git a/backend/windmill-api/src/hub_publish.rs b/backend/windmill-api/src/hub_publish.rs index 64154c3764060..e04e97f0d1dbe 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)) @@ -485,6 +490,53 @@ 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. +// `recording: null` clears it. +#[derive(Deserialize, Serialize)] +struct AgentRecordingBody { + path: String, + recording: Option, +} + +async fn publish_agent_recording( + ctx: HubPublishCtx, + Path((_workspace, slug)): Path<(String, ProjectSlug)>, + Json(body): Json, +) -> Result { + ctx.post(&format!("/projects/{}/agent_recording", slug), &body) + .await +} + #[derive(Deserialize, Serialize)] struct PublishTriggerBody { path: String, diff --git a/frontend/src/lib/components/WorkspaceDeployLayout.svelte b/frontend/src/lib/components/WorkspaceDeployLayout.svelte index 1cd1f858f2632..086889ad1e2a4 100644 --- a/frontend/src/lib/components/WorkspaceDeployLayout.svelte +++ b/frontend/src/lib/components/WorkspaceDeployLayout.svelte @@ -16,7 +16,9 @@ interface DeployableItem { key: string path: string - kind: Kind + /** `agent`: a saved AI agent listed as an item of its own (the Hub publish flow), + * rather than as the `ai_agent` resource it is stored as. */ + kind: Kind | 'agent' triggerKind?: string [key: string]: unknown } diff --git a/frontend/src/lib/components/recording/types.ts b/frontend/src/lib/components/recording/types.ts index dd85585768342..c36cbe8d84fe7 100644 --- a/frontend/src/lib/components/recording/types.ts +++ b/frontend/src/lib/components/recording/types.ts @@ -143,6 +143,34 @@ 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 and its answer. A turn is a run of the agent, which is a + * one-step flow: `recording` keeps that run whole, replayable like any flow's. */ +export type AgentTurn = { + user_message: string + answer: string + duration_ms: number + tool_calls: AgentToolCall[] + recording?: FlowRecording +} + +/** 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..b5ae86a072731 --- /dev/null +++ b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte @@ -0,0 +1,137 @@ + + +
+
+ {#if turns.length === 0 && pending === undefined} + + Ask the agent what a visitor would ask it. Two or three messages that show what it does make + a good demo. + + {/if} + {#each turns as turn, i (i)} +
+
+ {turn.user_message} +
+
+ {#if turn.tool_calls.length > 0} +
+ {#each turn.tool_calls as call, j (j)} + + {call.name} + + {/each} +
+ {/if} +
+
+ {turn.answer} +
+
+
{fmt(turn.duration_ms)}
+ {/each} + {#if pending !== undefined} +
+
+ {pending} +
+
+
+ The agent is answering… +
+ {/if} + {#if error} +
+ {error} +
+ {/if} +
+ +
+ + +
+ +
+ + {#if !onsave} + Push the bundle to the Hub first to save a conversation. + {:else if turns.length === 0} + Nothing to save yet. + {:else} + {turns.length} message{turns.length === 1 ? '' : 's'} — 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..02cfe0ba45d03 100644 --- a/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte +++ b/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte @@ -10,6 +10,7 @@ import { useDeployToHubSession, canRecord, + canRecordConversation, canRecordSession, sanitizeSlug, isValidSlug, @@ -21,6 +22,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, @@ -157,6 +159,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 +194,10 @@ appRecordDrawer?.openDrawer() } + function openAgentRecord(it: DeployItem) { + deployHub.session?.openAgentRecord(it) + agentRecordDrawer?.openDrawer() + } function openRecord(it: DeployItem) { recordDrawer?.openDrawer() void deployHub.session?.openRecord(it) @@ -896,6 +903,31 @@ {/if} {/if} + {#if s.phase === 'draft' && canRecordConversation(it)} + {#if it.rec === 'recorded'} + + Recorded + + + {:else} + No recording + + {/if} + {/if} {/snippet} {#snippet footer()} @@ -907,7 +939,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 +1109,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} +
+ s.sendAgentMessage(m)} + onsave={s.hubItemIds[s.agentTarget.key] + ? async () => { + if (await s.saveAgentRecording()) agentRecordDrawer?.closeDrawer() + } + : undefined} + /> +
+ {/key} + {/if} +
+
+
+ diff --git a/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts b/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts index c1cdea12ade78..a3dc1d4bf8388 100644 --- a/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts +++ b/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts @@ -7,16 +7,24 @@ 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' // `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..a576bf8de93c8 100644 --- a/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts +++ b/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts @@ -33,16 +33,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 +53,13 @@ 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, + AgentToolCall, + AgentTurn, + PipelineRecording, + RawAppRecording +} from '$lib/components/recording/types' import { TRIGGER_KINDS, listAllWorkspaceTriggers, @@ -67,6 +74,7 @@ export type Phase = 'predeploy' | 'draft' | 'under_review' | 'live' // Re-exported so the publish-flow components keep one import site. export { canRecord, + canRecordConversation, canRecordSession, mergeAppTableOrigin, type DeployItem, @@ -93,7 +101,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 @@ -363,7 +372,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') ) @@ -554,11 +565,16 @@ 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 the type + // lists none rather than failing the whole load. + this.#listAllPages((p) => + ResourceService.listResource({ workspace, resourceType: 'ai_agent', ...p }) + ).catch(() => []) ]) if (this.#disposed) return @@ -603,6 +619,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 +729,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 +894,24 @@ 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 + // What an agent is, not who runs it here: the deploy identity and the + // sharing switches belong to this workspace and never leave it. + const { + on_behalf_of: _onBehalfOf, + run_on_behalf_of: _runOnBehalfOf, + slack_anyone: _slackAnyone, + ...value + } = (r.value ?? {}) as Record + return { + kind: 'agent', + path: ref.path, + summary: ref.path.split('/').pop(), + description: r.description ?? undefined, + value + } } } catch (e: any) { return undefined @@ -1105,6 +1149,44 @@ 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) { + if (agents.length === 0) 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 +1392,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 +1408,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 +1838,136 @@ export class DeployToHubSession { }) } + // --- An agent's demo: a conversation, one run per message ----------------------- + + /** The agent being recorded, its conversation so far, and the turn in flight. */ + agentTarget = $state(undefined) + agentTurns = $state<(AgentTurn & { job_id: string })[]>([]) + agentPending = $state(undefined) + agentError = $state(undefined) + agentSaving = $state(false) + #agentMemoryId = '' + #agentTok = 0 + + openAgentRecord(it: DeployItem) { + this.#agentTok++ + this.agentTarget = it + this.agentTurns = [] + this.agentPending = undefined + this.agentError = undefined + // A conversation of its own, so the demo never continues someone's real chat. + this.#agentMemoryId = crypto.randomUUID() + } + + closeAgentRecord = () => { + this.#agentTok++ + this.agentTarget = undefined + this.agentPending = undefined + } + + /** Send one message: a run of the agent in the demo's conversation, waited for. */ + sendAgentMessage = async (text: string) => { + const it = this.agentTarget + const message = text.trim() + if (!it || !message || this.agentPending !== undefined) return + const tok = this.#agentTok + this.agentPending = message + this.agentError = undefined + const started = Date.now() + try { + const jobId = await JobService.runAgent({ + workspace: this.workspace, + path: it.path, + memoryId: this.#agentMemoryId, + requestBody: { user_message: message } + }) + const deadline = Date.now() + 15 * 60_000 + let interval = 500 + while (Date.now() < deadline) { + if (tok !== this.#agentTok) return + const r = await JobService.getCompletedJobResultMaybe({ + workspace: this.workspace, + id: jobId + }) + if (tok !== this.#agentTok) return + if (r.completed) { + if (!r.success) { + const failure = (r.result as any)?.error?.message ?? r.result + this.agentError = ( + typeof failure === 'string' ? failure : JSON.stringify(failure) + ).slice(0, 600) + return + } + this.agentTurns = [ + ...this.agentTurns, + { + user_message: message, + ...readAgentTurn(r.result), + duration_ms: Date.now() - started, + job_id: jobId + } + ] + return + } + await sleep(interval) + interval = Math.min(2000, interval * 1.5) + } + this.agentError = 'The agent did not answer within 15 minutes.' + } catch (e: any) { + if (tok === this.#agentTok) this.agentError = `Failed to run the agent: ${e?.message ?? e}` + } finally { + if (tok === this.#agentTok) this.agentPending = undefined + } + } + + /** Save the conversation as the agent's Hub recording. Each turn keeps its run, so the + * Hub can replay it step by step. Returns true on success. */ + async saveAgentRecording(): Promise { + const it = this.agentTarget + if (!it || this.agentTurns.length === 0 || this.agentSaving) return false + this.agentSaving = true + try { + const turns: AgentTurn[] = [] + for (const t of this.agentTurns) { + const { job_id, ...turn } = t + // A turn whose run cannot be read back still tells the conversation. + const run = await this.#buildAgentTurnRecording(it, job_id).catch(() => undefined) + turns.push({ ...turn, ...(run ? { recording: run } : {}) }) + } + 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'}`) + return true + } catch (e: any) { + sendUserToast(`Failed to save the conversation: ${e?.message ?? e}`, true) + return false + } finally { + this.agentSaving = false + } + } + + // A turn is a run of the agent as a one-step flow, materialized on the job. + async #buildAgentTurnRecording(it: DeployItem, jobId: string) { + const root = (await JobService.getJob({ workspace: this.workspace, id: jobId })) as any + if (!root.raw_flow) return undefined + return await buildFlowRecording(this.workspace, jobId, it.path, { + value: root.raw_flow, + schema: { type: 'object', properties: {}, required: [] }, + summary: it.summary ?? '' + }) + } + /** 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] @@ -2029,3 +2248,38 @@ export function useDeployToHubSession(args: { } } } + +/** The answer and the tools called, read off an agent run's result (`{ output, messages }`). + * A result of another shape is shown as it is rather than dropped. */ +function readAgentTurn(result: unknown): { answer: string; tool_calls: AgentToolCall[] } { + const r = result as { output?: unknown; messages?: any[] } | null + const text = (v: unknown) => + typeof v === 'string' ? v : v == null ? '' : JSON.stringify(v, null, 2) + const messages = Array.isArray(r?.messages) ? r.messages : [] + const results = new Map() + for (const m of messages) { + if (m?.role === 'tool' && typeof m.tool_call_id === 'string') { + results.set(m.tool_call_id, m.content) + } + } + const tool_calls: AgentToolCall[] = [] + for (const m of messages) { + if (m?.role !== 'assistant' || !Array.isArray(m.tool_calls)) continue + for (const c of m.tool_calls) { + const raw = c?.function?.arguments + let args: unknown = raw + if (typeof raw === 'string') { + try { + args = JSON.parse(raw) + } catch {} + } + tool_calls.push({ + name: c?.function?.name ?? 'tool', + args, + result: typeof c?.id === 'string' ? results.get(c.id) : undefined + }) + } + } + const output = r && typeof r === 'object' && 'output' in r ? r.output : result + return { answer: text(output), tool_calls } +} diff --git a/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts b/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts index e8c786e1c236d..c23566ab2e8f7 100644 --- a/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts +++ b/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts @@ -3,6 +3,8 @@ import { classifyPath, extractScriptRefs, extractFlowRefs, + extractAgentRefs, + rewriteAgentValue, extractAppRefs, buildPathMap, rewriteContent, @@ -926,3 +928,139 @@ 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') + }) +}) diff --git a/frontend/src/lib/components/workspaceSettings/projectBundle.ts b/frontend/src/lib/components/workspaceSettings/projectBundle.ts index e13924c755dec..d43051faa09eb 100644 --- a/frontend/src/lib/components/workspaceSettings/projectBundle.ts +++ b/frontend/src/lib/components/workspaceSettings/projectBundle.ts @@ -445,6 +445,41 @@ export function rewriteRawAppContent(content: string, map: Map): return JSON.stringify(rewriteAppValue(parsed, map)) } +// A saved agent (`ai_agent` resource value) reaches the rest of the workspace the way an AI +// agent step does: its tools run scripts by path, their static inputs and its provider hold +// `$res:` references. Viewing it as the one-step flow a run makes of it lets the flow +// extractor and rewriter do the work, so both stay the single definition of what a tool +// can reference. +function agentAsFlow(value: any): any { + return { + modules: [ + { + id: 'agent', + value: { + type: 'aiagent', + tools: Array.isArray(value?.tools) ? value.tools : [], + input_transforms: { provider: { type: 'static', value: value?.provider ?? null } } + } + } + ] + } +} + +export function extractAgentRefs(value: any): Ref[] { + return extractFlowRefs(agentAsFlow(value)) +} + +export function rewriteAgentValue(value: any, map: Map): any { + if (!value || typeof value !== 'object') return value + const step = rewriteFlowValue(agentAsFlow(value), map)?.modules?.[0]?.value + const out = { ...value } + if (Array.isArray(value.tools)) out.tools = step?.tools ?? value.tools + if (value.provider !== undefined) { + out.provider = step?.input_transforms?.provider?.value ?? value.provider + } + return out +} + // --------------------------------------------------------------------------- // Hub project export format (what /projects/{slug}/export returns) and its // retargeting into a destination folder. Kept here, next to the rewriters, @@ -466,6 +501,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 +524,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) @@ -555,6 +594,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 +622,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 +637,7 @@ export interface FetchedItem { description?: string /** scripts + raw_apps */ content?: string - /** flows + apps */ + /** flows + apps + agents */ value?: any /** scripts */ language?: string @@ -629,13 +678,15 @@ 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 [] } @@ -721,11 +772,16 @@ 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) })) ) diff --git a/frontend/src/lib/components/workspaceSettings/projectInstall.ts b/frontend/src/lib/components/workspaceSettings/projectInstall.ts index 6fd4bdd7282bf..55a837e74c54c 100644 --- a/frontend/src/lib/components/workspaceSettings/projectInstall.ts +++ b/frontend/src/lib/components/workspaceSettings/projectInstall.ts @@ -28,6 +28,7 @@ import { classifyPath, collectExportVarPaths, extractAppRefs, + extractAgentRefs, extractFlowRefs, extractRawAppRefs, extractScriptRefs, @@ -141,6 +142,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: 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 +289,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 +420,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) + ) } From 9c51a7abb90e22660848bde07f9114a966c52a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20L=C3=A9cuyer?= Date: Tue, 17 Nov 2026 15:21:56 +0100 Subject: [PATCH 02/13] feat(folders): count a folder's AI agents on their own The Folders table counted a saved agent among the resources, so a folder built around an agent showed nothing that said so. Agents get their own column, and the resources count is the credentials and other resources only. Co-Authored-By: Claude Fable 5.2 --- ...45d58e6fdc4dcf537cb5367c347b46e5c300e.json | 23 +++++++++++++++++++ ...1994d22596d4d869f54184a68ac4f0f9c174.json} | 4 ++-- backend/windmill-api-groups/src/folders.rs | 15 +++++++++++- backend/windmill-api/openapi.yaml | 4 ++++ .../src/lib/components/FolderUsageInfo.svelte | 2 +- .../(root)/(logged)/folders/+page.svelte | 3 ++- 6 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 backend/.sqlx/query-ae54be84233bb75de975221cfa845d58e6fdc4dcf537cb5367c347b46e5c300e.json rename backend/.sqlx/{query-8cc4fa257cb8053d27eb7a555d6e1b993e4a74b063eac632df8da24f6b87130e.json => query-fc9dd1be96a0a2c987724c2bb89e1994d22596d4d869f54184a68ac4f0f9c174.json} (70%) 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 3fb566cc1c3d6..9be773de5da51 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -24828,6 +24828,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: 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/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 _} - + From 38f9021cab16081e1b2ffb9a592c2486f41552eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20L=C3=A9cuyer?= Date: Tue, 17 Nov 2026 15:46:09 +0100 Subject: [PATCH 03/13] fix(hub): hand agents to the deploy layout as the resource rows it knows Widening the shared layout's item kind broke the workspace deploy screen, which passes that kind on. The layout already draws an agent as a resource row it is told about (isAgent): the publish screen now uses that, and the layout is unchanged. Co-Authored-By: Claude Fable 5.2 --- .../src/lib/components/WorkspaceDeployLayout.svelte | 4 +--- .../components/workspaceSettings/DeployToHub.svelte | 4 +++- .../components/workspaceSettings/deployToHubItems.ts | 12 +++++++++++- .../workspaceSettings/deployToHubSession.svelte.ts | 1 + 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/frontend/src/lib/components/WorkspaceDeployLayout.svelte b/frontend/src/lib/components/WorkspaceDeployLayout.svelte index 086889ad1e2a4..1cd1f858f2632 100644 --- a/frontend/src/lib/components/WorkspaceDeployLayout.svelte +++ b/frontend/src/lib/components/WorkspaceDeployLayout.svelte @@ -16,9 +16,7 @@ interface DeployableItem { key: string path: string - /** `agent`: a saved AI agent listed as an item of its own (the Hub publish flow), - * rather than as the `ai_agent` resource it is stored as. */ - kind: Kind | 'agent' + kind: Kind triggerKind?: string [key: string]: unknown } diff --git a/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte b/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte index 02cfe0ba45d03..5131566360d14 100644 --- a/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte +++ b/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte @@ -9,6 +9,7 @@ import { displayDate } from '$lib/utils' import { useDeployToHubSession, + asLayoutItem, canRecord, canRecordConversation, canRecordSession, @@ -331,7 +332,8 @@ {@const s = deployHub.session}
i.agent === true} nested folderDetail={subProjectNote} selectablePredicate={(i) => !s.isBlocked(i.path)} diff --git a/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts b/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts index a3dc1d4bf8388..35af1c4a2f7c9 100644 --- a/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts +++ b/frontend/src/lib/components/workspaceSettings/deployToHubItems.ts @@ -24,7 +24,17 @@ 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' +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 a576bf8de93c8..aa67a05ad45d9 100644 --- a/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts +++ b/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts @@ -73,6 +73,7 @@ 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, From ecd3479942cae789250af16b7264383e62439d1b Mon Sep 17 00:00:00 2001 From: Tristan Lecuyer Date: Tue, 17 Nov 2026 23:34:06 +0100 Subject: [PATCH 04/13] List the agents a flow or script uses in its Hub dependencies Co-Authored-By: Claude Opus 5.5 --- .../workspaceSettings/deployToHubSession.svelte.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts b/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts index aa67a05ad45d9..6ef7b4f973a3b 100644 --- a/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts +++ b/frontend/src/lib/components/workspaceSettings/deployToHubSession.svelte.ts @@ -16,6 +16,7 @@ import { classifyPath, extractScriptRefs, extractFlowRefs, + extractAgentRefs, extractAppRefs, extractTriggerConfigResourceRefs, extractVarRefsFromValue, @@ -462,7 +463,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 From fffa791eac029c0371460f6809c4cd6bb150273b Mon Sep 17 00:00:00 2001 From: Tristan Lecuyer Date: Wed, 18 Nov 2026 07:27:49 +0100 Subject: [PATCH 05/13] Export the read-only AI chat components from the package The Hub renders recorded agent conversations with the same components as the AI Sessions share page. Co-Authored-By: Claude Opus 5.5 --- frontend/package.json | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/frontend/package.json b/frontend/package.json index 66de709ffa1d6..5f7cb2acb162b 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -480,6 +480,37 @@ "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" } }, "files": [ @@ -651,6 +682,27 @@ ], "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" ] } }, From f823ea87ce39a95dbe327cb9a9ef105cc9e7b732 Mon Sep 17 00:00:00 2001 From: Tristan Lecuyer Date: Wed, 18 Nov 2026 15:09:12 +0100 Subject: [PATCH 06/13] Record only the current turn's tool calls; address review nits - readAgentTurn reads the messages after the turn's own user message: a run with memory returns the whole conversation, so turn N repeated the tools of turns 1..N-1. Covered by a two-turn test. - randomUUID from $lib/utils/uuid instead of crypto.randomUUID, which is missing on instances served over plain http. - The record drawer's message box uses TextInput instead of a raw textarea. Co-Authored-By: Claude Opus 5.5 --- .../AgentRecordSession.svelte | 31 +++++++------ .../deployToHubSession.svelte.ts | 11 +++-- .../workspaceSettings/readAgentTurn.test.ts | 43 +++++++++++++++++++ 3 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 frontend/src/lib/components/workspaceSettings/readAgentTurn.test.ts diff --git a/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte index b5ae86a072731..4dabd6e1088fe 100644 --- a/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte +++ b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte @@ -1,5 +1,6 @@ + +{#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/workspaceSettings/AgentRecordSession.svelte b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte index 3e79056d14012..51dad3d65760f 100644 --- a/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte +++ b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte @@ -1,8 +1,9 @@
@@ -52,44 +53,7 @@ a good demo. {/if} - {#each turns as turn, i (i)} -
-
- {turn.user_message} -
-
- {#if turn.tool_calls.length > 0} -
- {#each turn.tool_calls as call, j (j)} - - {#if call.name === 'web_search'}{:else}{/if}{call.name} - - {/each} -
- {/if} -
-
- {turn.answer} -
-
-
{fmt(turn.duration_ms)}
- {/each} - {#if pending !== undefined} -
-
- {pending} -
-
-
- The agent is answering… -
- {/if} + {#if error} {error} {/if} diff --git a/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte b/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte index 7a41dc6f940d4..8bde9a285a819 100644 --- a/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte +++ b/frontend/src/lib/components/workspaceSettings/DeployToHub.svelte @@ -1130,6 +1130,7 @@
+ const value = withoutAgentWorkspaceKeys(r.value) return { kind: 'agent', path: ref.path, @@ -2290,58 +2284,3 @@ export function useDeployToHubSession(args: { } } } - -/** The call a structured answer is given through: the answer itself, not a tool of the agent. */ -const STRUCTURED_OUTPUT_CALL = /^structured_output(_\d+)?$/ - -/** - * One turn, read off the result of its run: the answer, and what the agent called to get - * there, in order. Tool calls are the model's own (`assistant.tool_calls`, each with the - * `tool` message answering it); a web search is a capability of the provider rather than a - * call, so the worker records it as a `tool` message tagged `web_search`. - */ -export function readAgentTurn(result: unknown): { answer: string; tool_calls: AgentToolCall[] } { - const r = result as { output?: unknown; messages?: any[] } | null - const text = (v: unknown) => - typeof v === 'string' ? v : v == null ? '' : JSON.stringify(v, null, 2) - // With memory, the run returns the whole conversation: this 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 `agent_action`, and does not start a turn. - const all = Array.isArray(r?.messages) ? r.messages : [] - const lastUser = all - .map((m) => (m?.role === 'user' && !m.agent_action ? 'user' : '')) - .lastIndexOf('user') - const messages = lastUser >= 0 ? all.slice(lastUser + 1) : all - const results = new Map() - for (const m of messages) { - if (m?.role === 'tool' && typeof m.tool_call_id === 'string') { - results.set(m.tool_call_id, m.content) - } - } - const tool_calls: AgentToolCall[] = [] - for (const m of messages) { - if (m?.role === 'tool' && m.agent_action?.type === 'web_search') { - tool_calls.push({ name: 'web_search' }) - continue - } - if (m?.role !== 'assistant' || !Array.isArray(m.tool_calls)) continue - for (const c of m.tool_calls) { - const name = c?.function?.name ?? 'tool' - if (STRUCTURED_OUTPUT_CALL.test(name)) continue - const raw = c?.function?.arguments - let args: unknown = raw - if (typeof raw === 'string') { - try { - args = JSON.parse(raw) - } catch {} - } - tool_calls.push({ - name, - args, - result: typeof c?.id === 'string' ? results.get(c.id) : undefined - }) - } - } - const output = r && typeof r === 'object' && 'output' in r ? r.output : result - return { answer: text(output), tool_calls } -} diff --git a/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts b/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts index a657f16c57b5c..6e0d0b0df891c 100644 --- a/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts +++ b/frontend/src/lib/components/workspaceSettings/projectBundle.test.ts @@ -5,6 +5,7 @@ import { extractFlowRefs, extractAgentRefs, rewriteAgentValue, + withoutAgentWorkspaceKeys, extractAppRefs, buildPathMap, rewriteContent, @@ -1402,4 +1403,15 @@ describe('saved AI agents', () => { 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 a031051378a76..ccf4d570bdd96 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. */ @@ -78,64 +89,20 @@ function withRefPrefix(ref: string, next: string): string { return prefix + next } -/** The saved agent an `aiagent` step or tool is linked to, if any. */ -function linkedAgentPath(v: any): string | undefined { - return bareResourcePath(v?.agent) -} - -/** An MCP tool's server: a resource path, bare or as a `$res:` token. */ -function mcpResourcePath(tool: any): string | undefined { - const v = tool?.value - return v?.tool_type === 'mcp' ? bareResourcePath(v.resource_path) : undefined -} - /** A whole-string `$res:PATH` / `res://PATH` value (prefix in group 1, path in group 2). */ const RES_VALUE_RE = /^(\$res:|res:\/\/)(.+)$/ -/** - * Tool names a step enables are plain strings, and an MCP server goes by its bare resource - * path there: the list follows the server when it moves, or the worker, which matches names - * exactly, would silently drop it from the roster. - */ -function rewriteEnabledTools(inputTransforms: any, map: Map): void { - const enabled = inputTransforms?.enabled_tools - if (enabled?.type !== 'static' || !Array.isArray(enabled.value)) return - enabled.value = enabled.value.map((name: unknown) => - typeof name === 'string' ? (map.get(name) ?? name) : name - ) -} - -/** - * Every reference inside an agent's configuration: a saved agent's whole value, or the - * tool wiring (`tool_inputs`) a linked step lays over one. One walk serves both reading - * and rewriting (`resolve` is told each reference and may return its new path), so the two - * cannot disagree about what a reference is. - * - * Nothing here lists the fields a reference may sit in, on purpose. A saved agent is read - * unvalidated: the worker resolves its brain keys as arguments, whichever they are, its - * tools nest (an agent used as a tool, with tools of its own), and keys keep being added. - * So the whole value is walked, and a reference is recognised by its shape wherever it sits: - * - a string that is a `$res:` reference as a whole, the only form the worker resolves in - * an argument. A token inside a sentence (a prompt explaining the syntax) is prose. - * - `$res:` tokens anywhere in a rawscript tool's `content`, as in a flow's inline code - * - `{ type: 'script' | 'flow', path }`: a tool that runs a script or a flow - * - `{ type: 'aiagent', agent }`: a tool linked to another saved agent - * - `{ tool_type: 'mcp', resource_path }`: an MCP server - * - * The one place read differently is an input, where the worker reads one too: an entry of - * an `input_transforms` map, or of a tool's map under `tool_inputs`. There, a static - * `value` is an argument (only whole-string references count, so a JSON argument that - * looks like a tool is not one) and a JavaScript `expr` is code that is evaluated, never - * resolved. An object merely shaped like an input anywhere else is walked as configuration: - * the install check relies on this walk, and nothing may be hidden that way. - * `$var:` references are read by `extractVarRefsFromValue`, which walks every string. - */ 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 @@ -145,6 +112,10 @@ function walkAgentConfig(value: any, resolve: AgentRefResolver): any { 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' @@ -157,15 +128,14 @@ function walkAgentConfig(value: any, resolve: AgentRefResolver): any { else if (k === 'tool_inputs') child = 'toolInputs' out[k] = walk(v[k], child) } - // Shapes name things only where the worker reads configuration: not inside an - // argument, and not on the maps that merely hold inputs by name. 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') { - const linked = linkedAgentPath(v) + // 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) } @@ -185,31 +155,27 @@ function walkAgentConfig(value: any, resolve: AgentRefResolver): any { return walk(value, 'config') } -function collectAgentConfigRefs( - value: any, - add: (kind: RefKind, path: string) => void, - ctx: 'config' | 'toolInputs' = 'config' -): void { - walkAgentConfig(ctx === 'toolInputs' ? { tool_inputs: value } : value, (kind, path) => { +function collectAgentConfigRefs(value: any, add: (kind: RefKind, path: string) => void): void { + walkAgentConfig(value, (kind, path) => { add(kind, path) return undefined }) } -/** The rewrite matching `collectAgentConfigRefs`. Returns a copy. */ -function rewriteAgentConfig( - value: any, - map: Map, - ctx: 'config' | 'toolInputs' = 'config' -): any { - const resolve: AgentRefResolver = (_kind, path) => map.get(path) - if (ctx === 'toolInputs') return walkAgentConfig({ tool_inputs: value }, resolve).tool_inputs - const out = walkAgentConfig(value, resolve) - // A second pass, since it needs the tools' final paths: see `rewriteEnabledTools`. +/** Returns a rewritten copy. */ +function rewriteAgentConfig(value: any, map: Map): any { + const out = walkAgentConfig(value, (_kind, path) => map.get(path)) + // `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') { - if (v.type === 'aiagent') rewriteEnabledTools(v.input_transforms, map) + 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]) } } @@ -217,6 +183,20 @@ function rewriteAgentConfig( 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) @@ -245,18 +225,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') { - // A step or tool linked to a saved agent: an `ai_agent` resource, shipped whole. - const linked = linkedAgentPath(v) - if (linked) add('resource', linked) - // MCP tools are not modules, so the walk above never visits them. - for (const tool of Array.isArray(v.tools) ? v.tools : []) { - const mcp = mcpResourcePath(tool) - if (mcp) add('resource', mcp) - } - // What a linked step binds on the agent's own tools, keyed by tool id. - collectAgentConfigRefs(v.tool_inputs, add, 'toolInputs') - } + if (v.type === 'aiagent') collectAgentConfigRefs(agentStepConfig(v), add) if (typeof v.content === 'string') { for (const r of extractScriptRefs(v.content)) add('resource', r.path) } @@ -552,18 +521,15 @@ export function rewriteFlowValue(value: any, map: Map): any { v.path = map.get(v.path) } if (v.type === 'aiagent') { - const linked = linkedAgentPath(v) - if (linked && map.has(linked)) v.agent = withRefPrefix(v.agent, map.get(linked)!) - if (Array.isArray(v.tools)) { - for (const tool of v.tools) { - const mcp = mcpResourcePath(tool) - if (mcp && map.has(mcp)) { - tool.value.resource_path = withRefPrefix(tool.value.resource_path, map.get(mcp)!) - } - } + 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 (v.tool_inputs) v.tool_inputs = rewriteAgentConfig(v.tool_inputs, map, 'toolInputs') - rewriteEnabledTools(v.input_transforms, map) } if (typeof v.content === 'string') v.content = rewriteContent(v.content, map) const it = v.input_transforms @@ -625,8 +591,7 @@ export function rewriteRawAppContent(content: string, map: Map): return JSON.stringify(rewriteAppValue(parsed, map)) } -// A saved agent (`ai_agent` resource value). Its whole value is walked rather than a list of -// known fields: see `collectAgentConfigRefs`. +// A saved agent (`ai_agent` resource value), walked whole: see `walkAgentConfig`. export function extractAgentRefs(value: any): Ref[] { const out: Ref[] = [] const seen = new Set() diff --git a/frontend/src/lib/components/workspaceSettings/projectInstall.ts b/frontend/src/lib/components/workspaceSettings/projectInstall.ts index bc00edecd7cd1..23a6b197dc37e 100644 --- a/frontend/src/lib/components/workspaceSettings/projectInstall.ts +++ b/frontend/src/lib/components/workspaceSettings/projectInstall.ts @@ -29,6 +29,7 @@ import { collectExportVarPaths, extractAppRefs, extractAgentRefs, + withoutAgentWorkspaceKeys, extractFlowRefs, extractRawAppRefs, extractScriptRefs, @@ -152,7 +153,7 @@ function importAgent(workspace: string, a: ExportItem): Promise { requestBody: { path: a.path, resource_type: 'ai_agent', - value: a.value ?? {}, + value: withoutAgentWorkspaceKeys(a.value), description: a.description ?? a.summary ?? '' } }) diff --git a/frontend/src/lib/components/workspaceSettings/readAgentTurn.test.ts b/frontend/src/lib/components/workspaceSettings/readAgentTurn.test.ts deleted file mode 100644 index ec094dfd5d388..0000000000000 --- a/frontend/src/lib/components/workspaceSettings/readAgentTurn.test.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { describe, expect, it } from 'vitest' -import { readAgentTurn } from './deployToHubSession.svelte' - -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']) - }) -}) From 9a3ca7bbd7ab41cf5e9dd0765eeb4e2bea5f578e Mon Sep 17 00:00:00 2001 From: Tristan Lecuyer Date: Wed, 18 Nov 2026 19:00:16 +0100 Subject: [PATCH 11/13] Record an agent's demo in the agent's own chat The record drawer embeds FlowChat as the agent page's Chat tab does (new conversation, no sidebar), so the recorder uses the same composer, transcript, tool cards and approval/question handling. Saving reads each answered message off its completed run with readAgentTurn; failed or unfinished runs are left out. Co-Authored-By: Claude Opus 5.5 --- .../AgentRecordSession.svelte | 120 +++++--------- .../workspaceSettings/DeployToHub.svelte | 8 +- .../deployToHubSession.svelte.ts | 155 +++++------------- 3 files changed, 84 insertions(+), 199 deletions(-) diff --git a/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte index 51dad3d65760f..e460616a5e982 100644 --- a/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte +++ b/frontend/src/lib/components/workspaceSettings/AgentRecordSession.svelte @@ -1,109 +1,71 @@
-
- {#if turns.length === 0 && pending === undefined} - - Ask the agent what a visitor would ask it. Two or three messages that show what it does make - a good demo. - - {/if} - - {#if error} - {error} - {/if} -
- -
-
- { - if (e.key === 'Enter' && !e.shiftKey) { - e.preventDefault() - send() - } - } - }} - /> -
- +
+
{#if !onsave} Push the bundle to the Hub first to save a conversation. - {:else if turns.length === 0} - Nothing to save yet. + {:else if sent === 0} + Ask the agent what a visitor would ask it. Two or three messages make a good demo. {:else} - {turns.length} message{turns.length === 1 ? '' : 's'} — saved as the demo on the agent's Hub - page. + The answered messages are saved as the demo on the agent's Hub page. {/if}