diff --git a/frontend/src/lib/components/ScriptEditor.svelte b/frontend/src/lib/components/ScriptEditor.svelte index eab47f8bbb..53662b5e9d 100644 --- a/frontend/src/lib/components/ScriptEditor.svelte +++ b/frontend/src/lib/components/ScriptEditor.svelte @@ -71,7 +71,9 @@ isDefault.push(k) } }) + await inferArgs(lang, code, schema) + schema = schema isDefault diff --git a/frontend/src/lib/components/flows/utils.ts b/frontend/src/lib/components/flows/utils.ts index bf261e7e91..db064a09d9 100644 --- a/frontend/src/lib/components/flows/utils.ts +++ b/frontend/src/lib/components/flows/utils.ts @@ -4,7 +4,7 @@ import { inferArgs } from '$lib/infer' import { loadSchema } from '$lib/scripts' import { workspaceStore } from '$lib/stores' import { emptySchema } from '$lib/utils' -import { get } from 'svelte/store' +import { get, writable } from 'svelte/store' import type { FlowModuleState, FlowState } from './flowState' export function cleanInputs(flow: Flow | any): Flow { @@ -74,11 +74,13 @@ export function selectedIdToModuleState(selectedId: string, flow: FlowState): Fl } } +const loadSchemaLastRun = writable<[string | undefined, Schema]>(undefined) export async function loadSchemaFromModule(module: FlowModule): Promise<{ input_transforms: Record schema: Schema }> { + const mod = module.value if (mod.type == 'rawscript' || mod.type === 'script') { diff --git a/frontend/src/lib/infer.ts b/frontend/src/lib/infer.ts index 1d9669e166..be333700c0 100644 --- a/frontend/src/lib/infer.ts +++ b/frontend/src/lib/infer.ts @@ -1,27 +1,37 @@ import { ScriptService, type MainArgSignature } from '$lib/gen' +import { get, writable } from 'svelte/store' import type { Schema, SchemaProperty } from './common.js' +const loadSchemaLastRun = writable<[string | undefined, MainArgSignature | undefined]>(undefined) + + export async function inferArgs( language: 'python3' | 'deno' | 'go', code: string, schema: Schema ): Promise { + let lastRun = get(loadSchemaLastRun) let inferedSchema: MainArgSignature - if (code == '') { code = ' ' } - if (language == 'python3') { - inferedSchema = await ScriptService.pythonToJsonschema({ - requestBody: code - }) - } else if (language == 'deno') { - inferedSchema = await ScriptService.denoToJsonschema({ - requestBody: code - }) - } else if (language == 'go') { - inferedSchema = await ScriptService.goToJsonschema({ - requestBody: code - }) + if (code == lastRun[0] && lastRun[1]) { + inferedSchema = lastRun[1] } else { - return + if (code == '') { code = ' ' } + if (language == 'python3') { + inferedSchema = await ScriptService.pythonToJsonschema({ + requestBody: code + }) + } else if (language == 'deno') { + inferedSchema = await ScriptService.denoToJsonschema({ + requestBody: code + }) + } else if (language == 'go') { + inferedSchema = await ScriptService.goToJsonschema({ + requestBody: code + }) + } else { + return + } + loadSchemaLastRun.set([code, inferedSchema]) } schema.required = []