From 583937334869dbff821e22caa481932bd49fa968 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Mon, 31 Jul 2023 10:17:36 +0200 Subject: [PATCH] move inferArgs to be loaded JIT --- .../InlineScriptRunnableByPath.svelte | 3 +- .../mainInput/RunnableSelector.svelte | 3 +- frontend/src/lib/components/apps/utils.ts | 42 +------------------ frontend/src/lib/infer.ts | 40 +++++++++++++++++- 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/frontend/src/lib/components/apps/editor/inlineScriptsPanel/InlineScriptRunnableByPath.svelte b/frontend/src/lib/components/apps/editor/inlineScriptsPanel/InlineScriptRunnableByPath.svelte index 0b9e5212ee..e2474a510d 100644 --- a/frontend/src/lib/components/apps/editor/inlineScriptsPanel/InlineScriptRunnableByPath.svelte +++ b/frontend/src/lib/components/apps/editor/inlineScriptsPanel/InlineScriptRunnableByPath.svelte @@ -24,8 +24,7 @@ import { createEventDispatcher } from 'svelte' import { deepEqual } from 'fast-equals' import { computeFields } from './utils' - import { loadSchema } from '../../utils' - import { inferArgs } from '$lib/infer' + import { inferArgs, loadSchema } from '$lib/infer' import RunButton from './RunButton.svelte' import { getScriptByPath } from '$lib/scripts' import { sendUserToast } from '$lib/toast' diff --git a/frontend/src/lib/components/apps/editor/settingsPanel/mainInput/RunnableSelector.svelte b/frontend/src/lib/components/apps/editor/settingsPanel/mainInput/RunnableSelector.svelte index c6334f9925..b46bab9822 100644 --- a/frontend/src/lib/components/apps/editor/settingsPanel/mainInput/RunnableSelector.svelte +++ b/frontend/src/lib/components/apps/editor/settingsPanel/mainInput/RunnableSelector.svelte @@ -10,8 +10,9 @@ import type { AppViewerContext } from '$lib/components/apps/types' import { createEventDispatcher, getContext } from 'svelte' import type { Schema } from '$lib/common' - import { getAllScriptNames, loadSchema, schemaToInputsSpec } from '$lib/components/apps/utils' + import { getAllScriptNames, schemaToInputsSpec } from '$lib/components/apps/utils' import { defaultIfEmptyString, emptySchema } from '$lib/utils' + import { loadSchema } from '$lib/infer' type Tab = 'hubscripts' | 'workspacescripts' | 'workspaceflows' | 'inlinescripts' diff --git a/frontend/src/lib/components/apps/utils.ts b/frontend/src/lib/components/apps/utils.ts index 9e0c0715b3..f6858e3b7d 100644 --- a/frontend/src/lib/components/apps/utils.ts +++ b/frontend/src/lib/components/apps/utils.ts @@ -1,7 +1,5 @@ -import type { Schema, SupportedLanguage } from '$lib/common' -import { FlowService, ScriptService } from '$lib/gen' -import { inferArgs } from '$lib/infer' -import { emptySchema } from '$lib/utils' +import type { Schema } from '$lib/common' + import { twMerge } from 'tailwind-merge' import type { AppComponent } from './editor/component' import type { AppInput, InputType, ResultAppInput, StaticAppInput } from './inputType' @@ -39,42 +37,6 @@ export function allItems( return [...grid, ...Object.values(subgrids).flat()] } -export async function loadSchema( - workspace: string, - path: string, - runType: 'script' | 'flow' | 'hubscript' -): Promise<{ schema: Schema; summary: string | undefined }> { - if (runType === 'script') { - const script = await ScriptService.getScriptByPath({ - workspace, - path - }) - - return { schema: script.schema as any, summary: script.summary } - } else if (runType === 'flow') { - const flow = await FlowService.getFlowByPath({ - workspace, - path - }) - - return { schema: flow.schema as any, summary: flow.summary } - } else { - const script = await ScriptService.getHubScriptByPath({ - path - }) - if ( - script.schema == undefined || - Object.keys(script.schema).length == 0 || - typeof script.schema != 'object' - ) { - script.schema = emptySchema() - } - - await inferArgs(script.language as SupportedLanguage, script.content, script.schema) - return { schema: script.schema, summary: script.summary } - } -} - export function schemaToInputsSpec( schema: Schema, defaultUserInput: boolean diff --git a/frontend/src/lib/infer.ts b/frontend/src/lib/infer.ts index fe0ed97712..b84f232383 100644 --- a/frontend/src/lib/infer.ts +++ b/frontend/src/lib/infer.ts @@ -1,7 +1,7 @@ -import type { MainArgSignature } from '$lib/gen' +import { ScriptService, type MainArgSignature, FlowService } from '$lib/gen' import { get, writable } from 'svelte/store' import type { Schema, SchemaProperty, SupportedLanguage } from './common.js' -import { sortObject } from './utils.js' +import { emptySchema, sortObject } from './utils.js' import { tick } from 'svelte' import init, { parse_deno, @@ -183,3 +183,39 @@ function argSigToJsonSchemaType( oldS.format = undefined } } + +export async function loadSchema( + workspace: string, + path: string, + runType: 'script' | 'flow' | 'hubscript' +): Promise<{ schema: Schema; summary: string | undefined }> { + if (runType === 'script') { + const script = await ScriptService.getScriptByPath({ + workspace, + path + }) + + return { schema: script.schema as any, summary: script.summary } + } else if (runType === 'flow') { + const flow = await FlowService.getFlowByPath({ + workspace, + path + }) + + return { schema: flow.schema as any, summary: flow.summary } + } else { + const script = await ScriptService.getHubScriptByPath({ + path + }) + if ( + script.schema == undefined || + Object.keys(script.schema).length == 0 || + typeof script.schema != 'object' + ) { + script.schema = emptySchema() + } + + await inferArgs(script.language as SupportedLanguage, script.content, script.schema) + return { schema: script.schema, summary: script.summary } + } +}