Files
windmill/frontend/src/lib/components/RunForm.svelte
T
AlexRV12andClaude Opus 5 a6abf2c8a7 feat: run and test scripts from the AI chat through an argument form (#11001)
* fix: disable a dynamic input when its schema field is disabled

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* refactor: extract the run form's argument hygiene into job_args

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* feat: give Tabs an opt-in sliding selection indicator

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* refactor: share the chat's scroll-fade measurement

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* feat: give the chat a run-form contract and incremental job output

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* feat: run and test a script from the chat through an argument form

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* feat: carry a chat run's card and job across saves and reloads

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* feat: render a chat run as a tool call row with its form, logs and result

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* feat: open a pending run form in the sessions preview pane

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* test: benchmark running a deployed script from the chat

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ERed9zo2oJjpSczzMayaNh

* feat: offer a test run's dynamic options from the draft it previews

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs: say that a test run's dynselect helper executes on form display

* fix: send a schema default the model omitted when yolo skips the form

* fix: infer a test run's schema when the stored one declares no properties

* docs: tighten the note on the form's mount-time helper job

* fix: apply a nested schema default the bypass posture counts as answered

* fix: apply a declared default to a null value and an optional nested field

* fix: check required fields inside a supplied optional object before bypassing

* fix: read required args as own properties before bypassing the form

* fix: stop the turn from the run form's action row in the preview panel

* refactor: drop the run-form prediction and share its secret minting

* refactor: prefill a proposed secret instead of emptying the field

* docs: correct the comments the run-form prediction left behind

* fix: keep a proposed secret out of the chat's stored messages

* docs: say what a literal secret argument now does

* test: restore the copilotInfo export the aiStore mock omits

* docs: cut the run form's helper-script note to its constraints

* refactor: settle a run form from one entry and fetch a job's logs once

* fix: separate colliding secret paths, gate plan mode, keep polled logs

* fix: mint before the form opens, skip empty fields, show what ran

* revert: mint a run form's secrets at submit, not before it opens

* fix: settle a cancelled run card on the form's arguments, not the proposal

* fix: settle a stopped run form like a cancelled one, and keep an empty secret empty

* fix: snapshot a run's arguments before minting its secrets

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-09 12:31:58 +00:00

400 lines
11 KiB
Svelte

<script lang="ts">
import {
computeSharableHash as computeSharableHash,
defaultIfEmptyString,
emptyString,
truncateHash,
sendUserToast
} from '$lib/utils'
import type { Schema } from '$lib/common'
import { Badge, Button } from './common'
import SchemaForm from './SchemaForm.svelte'
import SharedBadge from './SharedBadge.svelte'
import TimeAgo from './TimeAgo.svelte'
import Popover from './meltComponents/Popover.svelte'
import { Calendar, Check, CornerDownLeft } from 'lucide-svelte'
import RunFormAdvancedPopup from './RunFormAdvancedPopup.svelte'
import { page } from '$app/state'
import { replaceState } from '$app/navigation'
import JsonInputs from '$lib/components/JsonInputs.svelte'
import { argsToJsonPayload } from '$lib/schema'
import { triggerableByAI } from '$lib/actions/triggerableByAI.svelte'
import InputSelectedBadge from './schema/InputSelectedBadge.svelte'
import { untrack } from 'svelte'
import { processSecretArgs } from './secretArgUtils'
import { enforceDisabledDefaults, resetKeysToast } from './job_args'
import PowerShellCommonParams from './PowerShellCommonParams.svelte'
let reloadArgs = $state(0)
let jsonEditor: JsonInputs | undefined = $state(undefined)
let schemaHeight = $state(0)
let showInputSelectedBadge = $state(false)
let savedPreviousArgs: Record<string, any> | undefined = $state(undefined)
let psCommonParams: Record<string, any> = $state({})
function extractPsCommonParams(allArgs: Record<string, any>): {
scriptArgs: Record<string, any>
commonParams: Record<string, any>
} {
const scriptArgs: Record<string, any> = {}
const commonParams: Record<string, any> = {}
for (const [k, v] of Object.entries(allArgs)) {
if (k.startsWith('_wm_ps_')) {
commonParams[k] = v
} else {
scriptArgs[k] = v
}
}
return { scriptArgs, commonParams }
}
export async function setArgs(nargs: Record<string, any>) {
const { scriptArgs, commonParams } = extractPsCommonParams(nargs)
args = scriptArgs
psCommonParams = commonParams
reloadArgs++
// `reloadArgs` only keys the form; the JSON editor reads its payload once, at mount.
syncJsonEditor()
}
export async function run(overrideScheduledForStr?: string | undefined | null) {
let processedArgs: Record<string, any>
const { args: withDefaults, resetKeys } = enforceDisabledDefaults(args ?? {}, runnable?.schema)
if (resetKeys.length > 0) {
sendUserToast(resetKeysToast(resetKeys))
}
try {
processedArgs = await processSecretArgs(withDefaults, runnable?.schema)
} catch (e) {
sendUserToast('Failed to process sensitive args: ' + e, true)
return
}
if (showPsCommonParams) {
for (const [k, v] of Object.entries(psCommonParams)) {
if (v !== undefined && v !== false && v !== '') {
processedArgs[k] = v
}
}
}
runAction(
overrideScheduledForStr === null ? undefined : (overrideScheduledForStr ?? scheduledForStr),
processedArgs,
invisible_to_owner,
overrideTag
)
}
interface Props {
runnable:
| {
summary?: string
schema?: Schema | any
description?: string
path?: string
is_template?: boolean
hash?: string
kind?: string
language?: string
can_write?: boolean
created_at?: string
created_by?: string
extra_perms?: Record<string, boolean>
}
| undefined
runAction: (
scheduledForStr: string | undefined,
args: Record<string, any>,
invisible_to_owner: boolean | undefined,
overrideTag: string | undefined
) => void
buttonText?: string
schedulable?: boolean
detailed?: boolean
autofocus?: boolean
loading?: boolean
noVariablePicker?: boolean
viewKeybinding?: boolean
scheduledForStr: string | undefined
invisible_to_owner: boolean | undefined
overrideTag: string | undefined
overrideTagNote?: string
args?: Record<string, any>
jsonView?: boolean
isValid?: boolean
}
let {
runnable,
runAction,
buttonText = 'Run',
schedulable = true,
detailed = true,
autofocus = false,
loading = false,
noVariablePicker = false,
viewKeybinding = false,
scheduledForStr = $bindable(),
invisible_to_owner = $bindable(),
overrideTag = $bindable(),
overrideTagNote = undefined,
args = $bindable(),
jsonView = false,
isValid = $bindable(true)
}: Props = $props()
let showPsCommonParams = $derived(
runnable?.language === 'powershell' && runnable?.schema?.['x-windmill-ps-cmd-binding'] === true
)
$effect.pre(() => {
if (args == undefined) {
args = {}
}
// Extract _wm_ps_* keys from args on initial load (e.g. "Run again" via URL hash)
if (args && Object.keys(args).some((k) => k.startsWith('_wm_ps_'))) {
const { scriptArgs, commonParams } = extractPsCommonParams(args)
args = scriptArgs
psCommonParams = commonParams
}
})
let debounced: number | undefined = undefined
function onArgsChange(args: any) {
try {
debounced && clearTimeout(debounced)
debounced = setTimeout(() => {
const nurl = new URL(window.location.href)
nurl.hash = computeSharableHash(args, overrideTag)
try {
replaceState(nurl.toString(), page.state)
} catch (e) {
console.error(e)
}
}, 200)
} catch (e) {
console.error('Impossible to set hash in args', e)
}
}
/** Rewrite the open JSON editor from the current args. Only for args replaced from outside
* the editor: entering the JSON view already starts from whatever `args` holds. */
export function syncJsonEditor() {
jsonEditor?.setCode(argsToJsonPayload(runnable?.schema, args))
}
$effect(() => {
overrideTag
Object.keys(args ?? {}).forEach((key) => {
args?.[key]
})
untrack(() => onArgsChange(args))
})
</script>
<!-- Standalone triggerable registration for the run form -->
<div
style="display: none"
use:triggerableByAI={{
id: `run-form-${runnable?.path ?? ''}`,
description: `Form to fill the inputs to run ${runnable?.summary && runnable?.summary.length > 0 ? runnable?.summary : runnable?.path}.
## Script description: ${runnable?.description ?? ''}.
## Schema used: ${JSON.stringify(runnable?.schema)}.
## Current args: ${JSON.stringify(args)}}`,
callback: (value) => {
savedPreviousArgs = args
setArgs(JSON.parse(value ?? '{}'))
showInputSelectedBadge = true
},
showAnimation: false
}}
></div>
{#snippet acceptButton()}
<Button
startIcon={{
icon: Check
}}
size="xs2"
btnClasses="border border-gray-200 dark:border-gray-600 !bg-surface text-primary"
on:click={() => {
showInputSelectedBadge = false
savedPreviousArgs = undefined
}}
>
Accept
</Button>
{/snippet}
{#if showInputSelectedBadge}
<InputSelectedBadge
inputSelected="ai"
labelColor="text-violet-800 dark:text-primary"
className="dark:!bg-violet-800 !bg-violet-200 !border-violet-200 dark:!border-violet-800"
{acceptButton}
onReject={() => {
setArgs(savedPreviousArgs ?? {})
savedPreviousArgs = undefined
showInputSelectedBadge = false
}}
/>
{/if}
<div class="max-w-3xl">
{#if detailed}
{#if runnable}
<div class="flex flex-row flex-wrap justify-between gap-4">
<div>
<div class="flex flex-col mb-2">
<h1 class="break-words py-2 mr-2">
{defaultIfEmptyString(runnable.summary, runnable.path ?? '')}
</h1>
{#if !emptyString(runnable.summary)}
<h2 class="font-bold pb-4">{runnable.path}</h2>
{/if}
<div class="flex items-center gap-2">
<span class="text-sm text-primary">
{#if runnable}
Edited <TimeAgo agoOnlyIfRecent date={runnable.created_at || ''} /> by {runnable.created_by ||
'unknown'}
{/if}
</span>
<Badge color="dark-gray">
{truncateHash(runnable?.hash ?? '')}
</Badge>
{#if runnable?.is_template}
<Badge color="blue">Template</Badge>
{/if}
{#if runnable && runnable.kind !== 'runnable'}
<Badge color="blue">
{runnable?.kind}
</Badge>
{/if}
<SharedBadge
canWrite={runnable.can_write ?? true}
extraPerms={runnable?.extra_perms ?? {}}
/>
</div>
</div>
</div>
</div>
{:else}
<h1
use:triggerableByAI={{
id: 'run-form-loading',
description: 'Run form is loading, should scan the page until this is gone'
}}>Loading...</h1
>
{/if}
{/if}
{#if runnable?.schema}
{#if jsonView}
<div
class="py-2"
style="height: {!schemaHeight || schemaHeight < 600 ? 600 : schemaHeight}px"
data-schema-picker
>
<JsonInputs
bind:this={jsonEditor}
on:select={(e) => {
if (e.detail) {
args = enforceDisabledDefaults(e.detail, runnable?.schema).args
}
}}
initialCode={argsToJsonPayload(runnable.schema, args)}
updateOnBlur={false}
placeholder={`Write args as JSON.<br/><br/>Example:<br/><br/>{<br/>&nbsp;&nbsp;"foo": "12"<br/>}`}
/>
</div>
{:else if !runnable.schema.properties || Object.keys(runnable.schema.properties).length === 0}
<div class="text-sm italic">{`This ${runnable.kind ?? 'runnable'} takes no arguments`}</div>
{:else}
{#key reloadArgs}
<div bind:clientHeight={schemaHeight}>
<SchemaForm
helperScript={{
source: 'deployed',
path: runnable.path!,
runnable_kind: runnable.hash ? 'script' : 'flow'
}}
prettifyHeader
{noVariablePicker}
{autofocus}
schema={runnable.schema}
bind:isValid
bind:args
/>
</div>
{/key}
{/if}
{:else}
<div class="text-xs text-primary">No arguments</div>
{/if}
{#if showPsCommonParams}
<div class="mt-4">
<PowerShellCommonParams bind:args={psCommonParams} />
</div>
{/if}
{#if schedulable}
<div class="flex gap-2 items-start flex-wrap justify-between mt-2 md:mt-6">
<div class="flex-row-reverse flex-wrap flex w-full gap-4">
<Button
id="run-form-run-button"
{loading}
variant="accent"
unifiedSize="md"
btnClasses="!inline-flex"
disabled={!isValid && !jsonView}
on:click={() => run()}
shortCut={{ Icon: CornerDownLeft, hide: !viewKeybinding }}
>
{scheduledForStr ? 'Schedule to run later' : buttonText}
</Button>
<div>
<Popover placement="bottom" closeButton usePointerDownOutside>
{#snippet trigger()}
<Button nonCaptureEvent startIcon={{ icon: Calendar }} unifiedSize="md" color="light">
Advanced
</Button>
{/snippet}
{#snippet content()}
<RunFormAdvancedPopup
bind:scheduledForStr
bind:invisible_to_owner
bind:overrideTag
{runnable}
/>
{/snippet}
</Popover>
</div>
</div>
{#if overrideTag}
<div class="flex-row-reverse flex w-full text-primary text-sm">
tag override: {overrideTag}
</div>
{:else if overrideTagNote}
<div class="flex-row-reverse flex w-full text-secondary text-xs">
{overrideTagNote}
</div>
{/if}
{#if invisible_to_owner}
<div class="flex-row-reverse flex w-full text-primary text-sm">
Job will be invisible to owner
</div>
{/if}
</div>
{:else}
<Button
btnClasses="!px-6 !py-1 w-full"
variant="accent"
disabled={!isValid && !jsonView}
on:click={() => run(null)}
shortCut={{ Icon: CornerDownLeft, hide: !viewKeybinding }}
>
{buttonText}
</Button>
{/if}
</div>