Files
windmill/frontend/src/lib/components/ModuleTest.svelte
T
hugocasaandClaude Opus 5 c297ed0052 feat: managed memory with an inherited or custom memory id per step (#11118)
* feat: split ai agent memory into agent policy, run memory id and step history

* fix: scope string memory ids to workspace and flow, keep nested tool history inputs

* chore: update sqlx cache for the flow context query

* docs: describe memory id scoping as collision-free rather than isolated

* chore: regenerate openflow json after merging main

* fix: offer no memory id for legacy manual memory, document linked history inputs

* fix: seed provided messages from legacy manual memory and hide its note once set

* fix: bypass memory when a provided messages expression evaluates to null

* fix: require a user message when provided messages are empty

* chore: keep the empty messages comment within the line width

* docs: name the history inputs wherever linked steps list their flow-local inputs

* docs: keep the memory storage path on one line

* feat: managed memory with an inherited or custom memory id per step

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

* fix: list a custom memory id in the test run form and name where an inherited one comes from

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

* fix: keep memory id out of the add-field menu and drop the memory id telemetry

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

* fix: keep legacy auto memory without an id working after an untouched redeploy

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

* fix: rename step messages to previous_messages and address review

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

* style: rewrap comments and docs lines lengthened by the previous_messages rename

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

* refactor: read agent memory as either a legacy shape or the current one

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

* fix: name the memory setting in ignored-input notes and keep conversions honest

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

* fix: keep a legacy memory count unset on open and read a cleared count as off

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: address review on cleared test history and zero-count memory

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: drop flow-local keys from a linked agent resource before interpolating it

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: keep a linked resource's own inputs as fallbacks and note ignored history on image runs

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: restore the linked agent draft tests and log ignored history on every image run

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: resolve the one-of variant from the value when the selected one leaves the list

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: treat zero-count managed memory as off when enabling chat mode and shorten comments

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: stop requiring user_message in the openflow agent contract when previous messages are the prompt

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 11:55:43 +02:00

315 lines
10 KiB
Svelte

<script lang="ts">
import {
ScriptService,
type AiAgent,
type FlowModule,
type InputTransform,
type JavascriptTransform,
type Job
} from '$lib/gen'
import { workspaceStore } from '$lib/stores'
import { getScriptByPath } from '$lib/scripts'
import { getContext, untrack } from 'svelte'
import type { FlowEditorContext } from './flows/types'
import JobLoader, { type Callbacks } from './JobLoader.svelte'
import { getStepHistoryLoaderContext } from './stepHistoryLoader.svelte'
import { loadSchemaFromModule } from './flows/flowInfers'
import {
inlineAgentDraft,
loadLinkedAgentDrafts,
normalizeAgentRef,
type LinkedAgentDraft
} from './flows/linkedAgentDrafts'
import { AGENT_FLOW_LOCAL_KEYS } from './flows/agentResourceUtils'
import { AGENT_HISTORY_KEYS } from './flows/agentFormFields'
import { sendUserToast } from '$lib/toast'
interface Props {
mod: FlowModule
testJob?: Job | undefined
testIsLoading?: boolean
noEditor?: boolean
scriptProgress?: any
onJobDone?: () => void
}
let {
mod,
testJob = $bindable(undefined),
testIsLoading = $bindable(false),
noEditor = false,
scriptProgress = $bindable(undefined),
onJobDone
}: Props = $props()
const {
flowStore,
flowStateStore,
pathStore,
stepsInputArgs,
previewArgs,
modulesTestStates,
devTempScriptRefs,
opWorkspace
} = getContext<FlowEditorContext>('FlowEditorContext')
let previewBase = $derived($pathStore ?? '')
// Acting workspace when the flow editor runs in an AI session; else the nav workspace.
let opWs = $derived(opWorkspace?.() ?? $workspaceStore)
let jobLoader: JobLoader | undefined = $state(undefined)
let jobProgressReset: () => void = () => {}
let stepHistoryLoader = getStepHistoryLoaderContext()
// Every explicit run re-evaluates the args with errors surfaced. The reactive evaluations
// that follow each flow edit stay quiet, so without this a failing expression is silently
// `undefined` in what the run is built from. Manually edited args are preserved across the
// refresh by `initializeFromSchema`.
export function runTestWithStepArgs() {
stepsInputArgs?.updateStepArgs(
mod.id,
flowStateStore.val,
flowStore?.val,
previewArgs?.val,
true
)
runTest(stepsInputArgs.getStepArgs(mod.id))
}
// A step's timeout is an InputTransform. Only a static numeric value can be applied
// to a single-step preview; dynamic expressions are evaluated server-side and only
// take effect when running the full flow.
function staticTimeout(timeout: FlowModule['timeout']): number | undefined {
if (timeout?.type === 'static' && typeof timeout.value === 'number') {
return timeout.value
}
return undefined
}
export async function runTest(args: any) {
// Not defined if JobProgressBar not loaded
if (jobProgressReset) jobProgressReset()
if (modulesTestStates.states[mod.id]) {
modulesTestStates.states[mod.id].cancel = async () => {
await jobLoader?.cancelJob()
modulesTestStates.states[mod.id].testJob = undefined
}
modulesTestStates.runTestCb?.(mod.id)
}
const val = mod.value
const timeout = staticTimeout(mod.timeout)
// let jobId: string | undefined = undefined
let callbacks: Callbacks = {
done: (x) => {
jobDone(x)
}
}
if (val.type == 'rawscript') {
await jobLoader?.runPreview(
// An empty base stays empty: `'' + '/' + id` is an absolute path, which
// `require_path_read_access_for_preview` rejects outright. A flow with no path yet
// previews unnamed instead.
val.path ?? (previewBase ? previewBase + '/' + mod.id : ''),
val.content,
val.language,
mod.id === 'preprocessor' ? { _ENTRYPOINT_OVERRIDE: 'preprocessor', ...args } : args,
flowStore?.val?.tag ?? val.tag,
undefined,
undefined,
callbacks,
previewBase,
undefined,
devTempScriptRefs?.(),
timeout
)
} else if (val.type == 'script') {
const script = val.hash
? await ScriptService.getScriptByHash({ workspace: opWs!, hash: val.hash })
: await getScriptByPath(val.path, opWs)
await jobLoader?.runPreview(
val.path,
script.content,
script.language,
mod.id === 'preprocessor' ? { _ENTRYPOINT_OVERRIDE: 'preprocessor', ...args } : args,
flowStore?.val?.tag ?? (val.tag_override ? val.tag_override : script.tag),
script.lock,
val.hash ?? script.hash,
callbacks,
previewBase,
undefined,
undefined,
timeout
)
} else if (val.type == 'flow') {
await jobLoader?.runFlowByPath(val.path, args, callbacks)
} else if (val.type == 'aiagent') {
const { schema } = await loadSchemaFromModule(mod, opWs)
// A linked step whose agent has an unsaved draft is tested as the draft, the same way the
// whole-flow preview and the agent editor's own test pane run it. `inlineAgentDraft`
// clears `agent` and moves the draft's brain and tools onto the step, so the branches
// below then treat it as a standalone agent.
let draft: LinkedAgentDraft | undefined
if (val.agent) {
const linked = normalizeAgentRef(val.agent)
try {
draft = (await loadLinkedAgentDrafts([linked], opWs)).get(linked)
} catch (err: any) {
// The load refuses when the agent's unsaved changes cannot be read, and this function's
// caller neither awaits nor catches: without this the rejection is unhandled and the
// button appears to do nothing, with the test already marked as started.
sendUserToast(`Could not run test: ${err?.body ?? err}`, true)
// Guarded like every other access to it here: the entry is only created for steps the
// panel is tracking, and this runs on a path where it may never have been.
if (modulesTestStates.states[mod.id]) {
modulesTestStates.states[mod.id].loading = false
}
return
}
}
const agentVal = draft ? inlineAgentDraft(val, draft.args) : val
// `args` spans the whole AI agent schema, so on a linked step it carries every brain key as
// undefined; overlaying those would shadow the draft's brain, so an inlined step takes only
// the inputs its form offers. A blank history input is unset, as on the step: an expression
// evaluating to nothing reads as an empty memory id, and the step's transform is stale.
const isBlank = (v: unknown) => v == undefined || v === '' || (Array.isArray(v) && !v.length)
const formKeys = (
draft ? (AGENT_FLOW_LOCAL_KEYS as readonly string[]) : Object.keys(args)
).filter(
(key) => !(AGENT_HISTORY_KEYS as readonly string[]).includes(key) || !isBlank(args[key])
)
const stepTransforms = Object.fromEntries(
Object.entries((agentVal.input_transforms ?? {}) as Record<string, InputTransform>).filter(
([key]) => !(AGENT_HISTORY_KEYS as readonly string[]).includes(key) || !isBlank(args[key])
)
)
// The test form only covers the schema it was given, and for a standalone agent that may be
// the flow-local one (the agent editor shows the brain in its own form, not here). Take the
// brain from the module as authored and let the form's own keys win over it, so an edit made
// in the form after the test panel mounted is what runs. A linked agent needs none of this:
// the server reads its brain from the resource.
const inputTransforms: { [key: string]: JavascriptTransform | InputTransform } = {
...(agentVal.agent ? {} : stepTransforms),
...Object.fromEntries(
formKeys.map((key) => [
key,
{
expr: `flow_input.${key}`,
type: 'javascript'
}
])
)
}
await jobLoader?.runFlowPreview(
args,
{
value: {
modules: [
{
id: mod.id,
// A linked step has no tools of its own: the resource's tools are resolved
// server-side from `agent`. `tool_inputs` goes in either way — a step forked
// for editing has no `agent` yet still carries the flow's bindings, which the
// runtime overlays, so the preview must test against them too.
value: {
type: 'aiagent',
...(agentVal.agent ? { agent: agentVal.agent } : { tools: agentVal.tools ?? [] }),
tool_inputs: agentVal.tool_inputs,
input_transforms: inputTransforms as AiAgent['input_transforms']
} as Extract<FlowModule['value'], { type: 'aiagent' }>
}
]
},
summary: '',
schema
},
callbacks,
previewBase
)
} else {
throw Error('Not supported module type')
}
}
function jobDone(testJob: Job & { result?: any }) {
if (testJob && !testJob.canceled && testJob.type == 'CompletedJob') {
if (flowStateStore.val[mod.id]) {
flowStateStore.val[mod.id] = {
...flowStateStore.val[mod.id],
previewResult: testJob.result,
previewSuccess: testJob.success,
previewJobId: testJob.id,
previewLogs: testJob['logs']
}
}
stepHistoryLoader?.resetInitial(mod.id)
}
if (modulesTestStates.states[mod.id]) {
modulesTestStates.states[mod.id].testJob = testJob
}
onJobDone?.()
}
export function cancelJob() {
modulesTestStates.states[mod.id]?.cancel?.()
}
$effect(() => {
// Update testIsLoading to read the state from parent components
testIsLoading = modulesTestStates.states?.[mod.id]?.loading ?? false
})
$effect(() => {
// Update testJob to read the state from parent components
testJob = modulesTestStates.states?.[mod.id]?.testJob
})
modulesTestStates.states[untrack(() => mod).id] = {
...(modulesTestStates.states?.[untrack(() => mod).id] ?? { loading: false }),
loading: testIsLoading,
testJob: testJob
}
</script>
<JobLoader
noCode={true}
toastError={noEditor}
workspaceOverride={opWs}
bind:scriptProgress
bind:this={jobLoader}
bind:isLoading={
() => modulesTestStates.states[mod.id]?.loading ?? false,
(v) => {
let newLoading = v ?? false
if (modulesTestStates.states && modulesTestStates.states?.[mod.id]?.loading !== newLoading) {
modulesTestStates.states[mod.id] = {
...(modulesTestStates.states?.[mod.id] ?? {}),
loading: newLoading,
hiddenInGraph: false
}
}
}
}
bind:job={
() => modulesTestStates.states[mod.id]?.testJob,
(v) => modulesTestStates.states[mod.id] && (modulesTestStates.states[mod.id].testJob = v)
}
loadPlaceholderJobOnStart={{
type: 'QueuedJob',
id: '',
running: false,
canceled: false,
job_kind: 'preview',
permissioned_as: '',
is_flow_step: false,
email: '',
visible_to_owner: true,
tag: ''
}}
/>