diff --git a/frontend/scripts/fix-svelte-module-imports.js b/frontend/scripts/fix-svelte-module-imports.js index fe4fde4959..1a9e18bf8e 100644 --- a/frontend/scripts/fix-svelte-module-imports.js +++ b/frontend/scripts/fix-svelte-module-imports.js @@ -112,7 +112,13 @@ for (const file of walk(packageDir)) { return `${prefix}${quote}${relativeSpecifier(file, join(chatSdkDest, 'index.js'))}${quote}` } const target = resolve(dirname(file), specifier) - if (target !== packageDir && !target.startsWith(packageDir + sep)) { + // Test files ride along in the output but no consumer imports them, so an escaping + // import there (e.g. `../app.html?raw`) breaks nothing. + if ( + target !== packageDir && + !target.startsWith(packageDir + sep) && + !/\.test\.js$/.test(file) + ) { unresolvable.push(`${relative(packageDir, file)}: ${specifier}`) return match } diff --git a/frontend/src/lib/components/copilot/StepInputGen.svelte b/frontend/src/lib/components/copilot/StepInputGen.svelte index 5b912d4390..e143fb0cb8 100644 --- a/frontend/src/lib/components/copilot/StepInputGen.svelte +++ b/frontend/src/lib/components/copilot/StepInputGen.svelte @@ -19,6 +19,7 @@ import { AIBtnClasses } from './chat/AIButtonStyle' import { Check, Wand2 } from 'lucide-svelte' import { twMerge } from 'tailwind-merge' + import { newFlowInputOf, newFlowInputsInstruction } from './stepInputFlowInputs' let generatedContent = $state('') let loading = $state(false) @@ -40,7 +41,9 @@ let abortController = new AbortController() let newFlowInput = $state('') - const { flowStore, selectionManager } = getContext('FlowEditorContext') + const { flowStore, selectionManager, customUi } = + getContext('FlowEditorContext') + const canAddFlowInputs = customUi?.aiFlowInputs != false const { generatedExprs } = getContext('FlowCopilotContext') || {} function createFlowInput() { @@ -105,7 +108,7 @@ ${ ? 'Favor results and flow_input.iter.value over flow inputs.' : 'Favor results over flow inputs' } -If none of the available results are appropriate, are already used or are more appropriate for other inputs, you can also imagine new flow_input properties which we will create programmatically based on what you provide. +${newFlowInputsInstruction(canAddFlowInputs, 'reply with nothing')} Reply with the most probable answer, do not explain or discuss. Use javascript object dot notation to access the properties. Only return the expression without any wrapper.` @@ -119,16 +122,11 @@ Only return the expression without any wrapper.` abortController ) - if ( - pickableProperties && - generatedContent.startsWith('flow_input.') && - generatedContent.split('.')[1] && - !(generatedContent.split('.')[1] in pickableProperties.flow_input) - ) { - newFlowInput = generatedContent.split('.')[1] - } else { - newFlowInput = '' + const newInput = newFlowInputOf(generatedContent, pickableProperties) + if (newInput && !canAddFlowInputs) { + generatedContent = '' } + newFlowInput = (canAddFlowInputs && newInput) || '' } catch (err) { if (!abortController.signal.aborted) { sendUserToast('Could not generate step input: ' + err, true) diff --git a/frontend/src/lib/components/copilot/StepInputsGen.svelte b/frontend/src/lib/components/copilot/StepInputsGen.svelte index 2fd93866d3..ecde5cc5d6 100644 --- a/frontend/src/lib/components/copilot/StepInputsGen.svelte +++ b/frontend/src/lib/components/copilot/StepInputsGen.svelte @@ -20,6 +20,7 @@ import type { Flow } from '$lib/gen' import { twMerge } from 'tailwind-merge' import { AIBtnClasses } from './chat/AIButtonStyle' + import { newFlowInputOf, newFlowInputsInstruction } from './stepInputFlowInputs' let loading = $state(false) interface Props { @@ -30,7 +31,9 @@ let { pickableProperties = undefined, argNames = [], schema = undefined }: Props = $props() - const { flowStore, selectionManager } = getContext('FlowEditorContext') + const { flowStore, selectionManager, customUi } = + getContext('FlowEditorContext') + const canAddFlowInputs = customUi?.aiFlowInputs != false const { exprsToSet, stepInputsLoading, generatedExprs } = getContext('FlowCopilotContext') || {} @@ -81,7 +84,7 @@ ${ Here's a summary of the available data: ${YAML.stringify(availableData)} -If none of the available results are appropriate, are already used or are more appropriate for other inputs, you can also imagine new flow_input properties which we will create programmatically based on what you provide. +${newFlowInputsInstruction(canAddFlowInputs, 'leave that input out of your answer')} Reply with the most probable answer, do not explain or discuss. Use javascript object dot notation to access the properties. @@ -105,18 +108,14 @@ input_name2: expression2 const exprs = {} newFlowInputs = [] for (const [key, value] of parsedInputs) { - if (argNames.includes(key)) { + if (argNames.includes(key) && value) { + const newFlowInput = newFlowInputOf(value, pickableProperties) + if (newFlowInput && !canAddFlowInputs) continue exprs[key] = value - if ( - pickableProperties && - value.startsWith('flow_input.') && - value.split('.')[1] && - !(value.split('.')[1] in pickableProperties.flow_input) - ) { - newFlowInputs.push(value.split('.')[1]) - } + if (newFlowInput) newFlowInputs.push(newFlowInput) } } + parsedInputs = parsedInputs.filter(([key]) => key in exprs) generatedExprs?.set(exprs) } catch (err) { if (!abortController.signal.aborted) { diff --git a/frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts b/frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts index 3a20ae9009..17058e490f 100644 --- a/frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts +++ b/frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts @@ -333,7 +333,6 @@ export function supportsPlanMode(mode: AIMode): boolean { const navCapabilities = fromStore(roleCapabilities) - function isWorkspacePath(path: string | undefined): path is string { return path?.startsWith('f/') === true || path?.startsWith('u/') === true } @@ -3543,7 +3542,8 @@ export class AIChatManager implements ChatViewHost { pendingPrompt, this.flowAiChatHelpers!.getFlowAndSelectedId(), [], - this.flowAiChatHelpers!.inlineScriptSession + this.flowAiChatHelpers!.inlineScriptSession, + { flowInputsLocked: !this.flowAiChatHelpers!.canEditFlowInputs() } ) } else if (this.mode === AIMode.NAVIGATOR) { return prepareNavigatorUserMessage(pendingPrompt) @@ -4393,7 +4393,8 @@ export class AIChatManager implements ChatViewHost { oldInstructions, this.flowAiChatHelpers!.getFlowAndSelectedId(), oldSelectedContext, - this.flowAiChatHelpers!.inlineScriptSession + this.flowAiChatHelpers!.inlineScriptSession, + { flowInputsLocked: !this.flowAiChatHelpers!.canEditFlowInputs() } ) break case AIMode.NAVIGATOR: diff --git a/frontend/src/lib/components/copilot/chat/AIChatManager.test.ts b/frontend/src/lib/components/copilot/chat/AIChatManager.test.ts index 80f6b2da97..6a16487e7a 100644 --- a/frontend/src/lib/components/copilot/chat/AIChatManager.test.ts +++ b/frontend/src/lib/components/copilot/chat/AIChatManager.test.ts @@ -217,6 +217,7 @@ function createFlowHelpers({ setCode: vi.fn(), setFlowJson: vi.fn(), getFlowInputsSchema: vi.fn(), + canEditFlowInputs: () => true, updateExprsToSet: vi.fn(), acceptAllModuleActions, rejectAllModuleActions: vi.fn(), diff --git a/frontend/src/lib/components/copilot/chat/flow/FlowAIChat.svelte b/frontend/src/lib/components/copilot/chat/flow/FlowAIChat.svelte index 6cb398ee44..c2c1015b52 100644 --- a/frontend/src/lib/components/copilot/chat/flow/FlowAIChat.svelte +++ b/frontend/src/lib/components/copilot/chat/flow/FlowAIChat.svelte @@ -13,7 +13,11 @@ import { refreshStateStore } from '$lib/svelte5Utils.svelte' import type { FlowCopilotContext } from '../../flow' import type { ScriptLintResult } from '../shared' - import { applyFlowJsonUpdate, updateRawScriptModuleContent } from './helperUtils' + import { + applyFlowJsonUpdate, + isFlowSchemaChange, + updateRawScriptModuleContent + } from './helperUtils' import { findModuleInFlow } from '$lib/components/flows/flowTree' import { getEditorStoragePath } from '$lib/components/editorStoragePathContext' @@ -27,8 +31,15 @@ onTestFlow?: (conversationId?: string) => Promise } = $props() - const { flowStore, flowStateStore, selectionManager, currentEditor, previewArgs, opWorkspace } = - getContext('FlowEditorContext') + const { + flowStore, + flowStateStore, + selectionManager, + currentEditor, + previewArgs, + opWorkspace, + customUi + } = getContext('FlowEditorContext') const selectedId = $derived(selectionManager.getSelectedId()) const { exprsToSet } = getContext('FlowCopilotContext') ?? {} @@ -138,6 +149,7 @@ } await acceptPendingFlowEditsIfEnabled() }, + canEditFlowInputs: () => customUi?.aiFlowInputs != false, getFlowInputsSchema: async () => { const s = flowStore.val.schema ?? {} return { type: 'object', properties: {}, required: [], ...s } @@ -227,6 +239,11 @@ notes, settings }) => { + if (!flowHelpers.canEditFlowInputs() && isFlowSchemaChange(flowStore.val.schema, schema)) { + throw new Error( + 'The flow inputs (schema) are locked in this editor and cannot be added, changed or removed. Keep the schema exactly as it is: give steps static values or reference only the existing flow_input fields.' + ) + } try { if ( modules !== undefined || diff --git a/frontend/src/lib/components/copilot/chat/flow/core.ts b/frontend/src/lib/components/copilot/chat/flow/core.ts index 39be790f3e..9f20c2e684 100644 --- a/frontend/src/lib/components/copilot/chat/flow/core.ts +++ b/frontend/src/lib/components/copilot/chat/flow/core.ts @@ -120,6 +120,9 @@ export interface FlowAIChatHelpers { setCode: (id: string, code: string) => Promise setFlowJson: (update: FlowJsonUpdate) => Promise getFlowInputsSchema: () => Promise> + /** False when the host locked the flow inputs (`customUi.aiFlowInputs`): `setFlowJson` then + * refuses any schema change. */ + canEditFlowInputs: () => boolean /** Update exprsToSet store for InputTransformForm components (only if module is selected) */ updateExprsToSet: (id: string, inputTransforms: Record) => void @@ -1199,7 +1202,8 @@ export function prepareFlowUserMessage( instructions: string, flowAndSelectedId?: { flow: ExtendedOpenFlow; selectedId: string }, selectedContext: ContextElement[] = [], - inlineScriptSession?: InlineScriptSession + inlineScriptSession?: InlineScriptSession, + options: { flowInputsLocked?: boolean } = {} ): ChatCompletionUserMessageParam { const flow = flowAndSelectedId?.flow const selectedId = flowAndSelectedId?.selectedId @@ -1232,6 +1236,11 @@ ${selectedId}` flowContent += contextInstructions + if (options.flowInputsLocked) { + flowContent += `\n\n## FLOW INPUTS ARE LOCKED: +The flow inputs cannot be added, changed or removed in this editor: keep \`schema\` exactly as it is in the current flow JSON. Steps may reference only the existing \`flow_input\` fields; give any other value a step needs as a static value or a step result. If the request needs a new flow input, say that the flow inputs are locked instead of adding one.` + } + flowContent += `\n\n## INSTRUCTIONS: ${instructions}` diff --git a/frontend/src/lib/components/copilot/chat/flow/helperUtils.test.ts b/frontend/src/lib/components/copilot/chat/flow/helperUtils.test.ts index ae763155f7..68f96e86c2 100644 --- a/frontend/src/lib/components/copilot/chat/flow/helperUtils.test.ts +++ b/frontend/src/lib/components/copilot/chat/flow/helperUtils.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest' import type { FlowModule } from '$lib/gen' import { applyFlowJsonUpdate, + isFlowSchemaChange, updateRawScriptModuleContent, validateFlowGroups, validateFlowNotes @@ -627,3 +628,30 @@ describe('validateFlowNotes', () => { ]) }) }) + +describe('isFlowSchemaChange', () => { + const schema = { type: 'object', properties: { a: { type: 'string' } }, required: ['a'] } + + it('treats a resent schema as no change, whatever its key order', () => { + expect(isFlowSchemaChange(schema, undefined)).toBe(false) + expect(isFlowSchemaChange(schema, structuredClone(schema))).toBe(false) + expect( + isFlowSchemaChange(schema, { + required: ['a'], + properties: { a: { type: 'string' } }, + type: 'object' + }) + ).toBe(false) + expect(isFlowSchemaChange(undefined, null)).toBe(false) + }) + + it('flags an added, changed or removed input', () => { + const added = structuredClone(schema) + added.properties['b'] = { type: 'string' } + expect(isFlowSchemaChange(schema, added)).toBe(true) + expect(isFlowSchemaChange(schema, { ...schema, properties: { a: { type: 'number' } } })).toBe( + true + ) + expect(isFlowSchemaChange(schema, null)).toBe(true) + }) +}) diff --git a/frontend/src/lib/components/copilot/chat/flow/helperUtils.ts b/frontend/src/lib/components/copilot/chat/flow/helperUtils.ts index c5a7f7be22..681a6d8cf6 100644 --- a/frontend/src/lib/components/copilot/chat/flow/helperUtils.ts +++ b/frontend/src/lib/components/copilot/chat/flow/helperUtils.ts @@ -8,6 +8,7 @@ import { NoteColor } from '$lib/components/graph/noteColors' import type { InlineScriptSession } from './inlineScriptsUtils' +import { deepEqual } from 'fast-equals' import { FLOW_VALUE_SETTINGS_KEYS, type FlowValueSettings } from './flowValueSettings' /** Allowed note/group color names — matches the NoteColor palette the note and @@ -270,6 +271,19 @@ export function validateFlowNotes(rawNotes: unknown, moduleIds?: Set): F }) } +/** Whether an AI update's `schema` would change the flow inputs. `patch_flow_json` resends the + * schema it was shown, so an unchanged one (in any key order, or absent as `null`) is no change. */ +export function isFlowSchemaChange( + current: Record | null | undefined, + next: Record | null | undefined +): boolean { + if (next === undefined) return false + return !deepEqual( + JSON.parse(JSON.stringify(current ?? null)), + JSON.parse(JSON.stringify(next ?? null)) + ) +} + export function applyFlowJsonUpdate( flow: FlowLike, inlineScriptSession: InlineScriptSession, diff --git a/frontend/src/lib/components/copilot/stepInputFlowInputs.test.ts b/frontend/src/lib/components/copilot/stepInputFlowInputs.test.ts new file mode 100644 index 0000000000..a7728a4b9a --- /dev/null +++ b/frontend/src/lib/components/copilot/stepInputFlowInputs.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, it } from 'vitest' +import { newFlowInputOf } from './stepInputFlowInputs' + +describe('newFlowInputOf', () => { + const pickable = { flow_input: { name: 'x' }, priorIds: {} } as any + + it('names a flow input the flow does not have yet', () => { + expect(newFlowInputOf('flow_input.email', pickable)).toBe('email') + expect(newFlowInputOf('flow_input.email.domain', pickable)).toBe('email') + }) + + it('ignores existing flow inputs and other expressions', () => { + expect(newFlowInputOf('flow_input.name', pickable)).toBeUndefined() + expect(newFlowInputOf('results.a.email', pickable)).toBeUndefined() + expect(newFlowInputOf('"static"', pickable)).toBeUndefined() + expect(newFlowInputOf('flow_input.email', undefined)).toBeUndefined() + }) +}) diff --git a/frontend/src/lib/components/copilot/stepInputFlowInputs.ts b/frontend/src/lib/components/copilot/stepInputFlowInputs.ts new file mode 100644 index 0000000000..85b6d8cb4f --- /dev/null +++ b/frontend/src/lib/components/copilot/stepInputFlowInputs.ts @@ -0,0 +1,18 @@ +import type { PickableProperties } from '../flows/previousResults' + +/** The flow input an AI-suggested step input expression names when the flow has no such input + * yet, or undefined when it names none. */ +export function newFlowInputOf( + expr: string, + pickableProperties: PickableProperties | undefined +): string | undefined { + if (!pickableProperties || !expr.startsWith('flow_input.')) return undefined + const key = expr.split('.')[1] + return key && !(key in pickableProperties.flow_input) ? key : undefined +} + +export function newFlowInputsInstruction(canAddFlowInputs: boolean, omitHint: string): string { + return canAddFlowInputs + ? 'If none of the available results are appropriate, are already used or are more appropriate for other inputs, you can also imagine new flow_input properties which we will create programmatically based on what you provide.' + : `Only use the flow_input properties listed in the available data: new flow inputs cannot be created. If none of the available data is appropriate, ${omitHint}.` +} diff --git a/frontend/src/lib/components/custom_ui.ts b/frontend/src/lib/components/custom_ui.ts index 5b89d867ab..e69638f911 100644 --- a/frontend/src/lib/components/custom_ui.ts +++ b/frontend/src/lib/components/custom_ui.ts @@ -46,6 +46,10 @@ export type FlowBuilderWhitelabelCustomUi = { tagLabel?: string aiAgent?: boolean aiSandbox?: boolean + /** Set to false so no AI feature can add, change or remove the flow's inputs (its schema): the + * step input fill ("Fill inputs" and the per-input suggestion) only uses existing flow + * inputs, and the flow AI chat cannot edit the schema. */ + aiFlowInputs?: boolean suggestIntegration?: boolean suggestScript?: boolean // Default timeout (in seconds) prefilled when enabling a custom step timeout.