add customUi.aiFlowInputs to stop the AI from adding flow inputs (#12192)

* feat: add customUi.aiFlowInputs to stop the AI from adding flow inputs

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

* fix: let test files escape the package root in the packaging guard

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

* test: pin the flow input lock's schema-change check and new-input detection

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-04 18:11:00 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 7850bd213d
commit ef36bbac43
12 changed files with 143 additions and 30 deletions
@@ -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
}
@@ -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>('FlowEditorContext')
const { flowStore, selectionManager, customUi } =
getContext<FlowEditorContext>('FlowEditorContext')
const canAddFlowInputs = customUi?.aiFlowInputs != false
const { generatedExprs } = getContext<FlowCopilotContext | undefined>('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)
@@ -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>('FlowEditorContext')
const { flowStore, selectionManager, customUi } =
getContext<FlowEditorContext>('FlowEditorContext')
const canAddFlowInputs = customUi?.aiFlowInputs != false
const { exprsToSet, stepInputsLoading, generatedExprs } =
getContext<FlowCopilotContext | undefined>('FlowCopilotContext') || {}
@@ -81,7 +84,7 @@ ${
Here's a summary of the available data:
<available>
${YAML.stringify(availableData)}</available>
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) {
@@ -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:
@@ -217,6 +217,7 @@ function createFlowHelpers({
setCode: vi.fn(),
setFlowJson: vi.fn(),
getFlowInputsSchema: vi.fn(),
canEditFlowInputs: () => true,
updateExprsToSet: vi.fn(),
acceptAllModuleActions,
rejectAllModuleActions: vi.fn(),
@@ -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<string | undefined>
} = $props()
const { flowStore, flowStateStore, selectionManager, currentEditor, previewArgs, opWorkspace } =
getContext<FlowEditorContext>('FlowEditorContext')
const {
flowStore,
flowStateStore,
selectionManager,
currentEditor,
previewArgs,
opWorkspace,
customUi
} = getContext<FlowEditorContext>('FlowEditorContext')
const selectedId = $derived(selectionManager.getSelectedId())
const { exprsToSet } = getContext<FlowCopilotContext | undefined>('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 ||
@@ -120,6 +120,9 @@ export interface FlowAIChatHelpers {
setCode: (id: string, code: string) => Promise<void>
setFlowJson: (update: FlowJsonUpdate) => Promise<FlowJsonUpdateResult>
getFlowInputsSchema: () => Promise<Record<string, any>>
/** 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<string, InputTransform>) => 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}`
@@ -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)
})
})
@@ -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<string>): 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<string, any> | null | undefined,
next: Record<string, any> | 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,
@@ -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()
})
})
@@ -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}.`
}
+4
View File
@@ -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.