From 3e6735bc6fdf46daeb4652d43c9b36f0df623612 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 24 Nov 2026 03:00:46 -0800 Subject: [PATCH] fix(models): accept local ollama models in fallback rows like the model field Editing validation accepted an installed Ollama model (an unclaimed Ollama-shaped name where Ollama is reachable) in a block's model field but still rejected the same name in its fallback rows, although the fallback picker offers it and execution runs it on Ollama. Writing back a workflow saved with such a fallback through the workflow-state API failed outright, and editing or re-sending the rows failed through mothership edits and workflow-as-code. Both sites now ask one isRunnableModelId predicate. Also tidies follow-ups from the same changes: the MODEL_OPTION_SOURCES note no longer cites bare Ollama names, the browser-only Ollama check in blocks/utils is renamed so it no longer shares a name with the server's isOllamaAvailable, the model combobox uses DEFAULT_BLOCK_MODEL instead of its own copy, and two comments are reflowed. --- .../components/combobox/combobox.tsx | 6 +-- apps/sim/blocks/utils.ts | 10 ++--- apps/sim/lib/model-router/resolve.ts | 15 +++---- .../mothership/request/session/run-replay.ts | 6 +-- .../editing/validation-models.test.ts | 39 +++++++++++++++++++ .../lib/workflows/editing/validation.test.ts | 8 +++- apps/sim/lib/workflows/editing/validation.ts | 35 ++++++++++------- 7 files changed, 85 insertions(+), 34 deletions(-) diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/combobox/combobox.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/combobox/combobox.tsx index 2b6deebe620..ad1b752586f 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/combobox/combobox.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/combobox/combobox.tsx @@ -19,13 +19,13 @@ import { useAccessibleReferencePrefixes } from '@/app/workspace/[workspaceId]/w/ import type { SubBlockConfig } from '@/blocks/types' import { useDebounce } from '@/hooks/use-debounce' import { usePermissionConfig } from '@/hooks/use-permission-config' +import { DEFAULT_BLOCK_MODEL } from '@/providers/default-model' import { useWorkflowRegistry } from '@/stores/workflows/registry/store' import { useSubBlockStore } from '@/stores/workflows/subblock/store' /** * Constants for ComboBox component behavior */ -const DEFAULT_MODEL = 'claude-sonnet-5-5' const ZOOM_FACTOR_BASE = 0.96 const MIN_ZOOM = 0.1 const MAX_ZOOM = 1 @@ -308,7 +308,7 @@ export const ComboBox = memo(function ComboBox({ /** * Determines the default option value to use. - * Priority: explicit defaultValue > DEFAULT_MODEL for model field > first option + * Priority: explicit defaultValue > DEFAULT_BLOCK_MODEL for model field > first option */ const defaultOptionValue = useMemo(() => { if (defaultValue !== undefined) { @@ -322,7 +322,7 @@ export const ComboBox = memo(function ComboBox({ if (subBlockId === 'model') { const defaultModelOption = evaluatedOptions.find( - (opt) => getOptionValue(opt) === DEFAULT_MODEL + (opt) => getOptionValue(opt) === DEFAULT_BLOCK_MODEL ) if (defaultModelOption) { return getOptionValue(defaultModelOption) diff --git a/apps/sim/blocks/utils.ts b/apps/sim/blocks/utils.ts index d8b856a11b0..4761f882323 100644 --- a/apps/sim/blocks/utils.ts +++ b/apps/sim/blocks/utils.ts @@ -265,8 +265,8 @@ export function getAgentModelOptions() { * catalog, and whether each offers Auto (so accepts sim-auto on hosted Sim). * * {@link getAgentModelOptions} is deliberately absent: the agent's model field also takes - * bare Ollama names, free-typed ids (Bedrock inference profiles, fine-tunes) and retired - * ids, so validation passes its values through unchanged. + * free-typed ids (Bedrock inference profiles, fine-tunes) and retired ids, so validation + * passes its values through unchanged. */ export const MODEL_OPTION_SOURCES: ReadonlyMap = new Map([ [getModelOptions, { auto: false }], @@ -352,12 +352,12 @@ function getProviderFromStore(model: string): string | null { } /** - * Whether an Ollama instance is available. `isOllamaConfigured` reads the + * Whether this browser knows of an Ollama instance. `isOllamaConfigured` reads the * server-only `OLLAMA_URL` env var, which is always undefined in the browser — * there the providers store (populated from the server's model list, which is * non-empty only when Ollama is configured) is the signal. */ -function isOllamaAvailable(): boolean { +function hasOllamaInstance(): boolean { if (isOllamaConfigured) return true return useProvidersStore.getState().providers.ollama.models.length > 0 } @@ -417,7 +417,7 @@ export function shouldRequireApiKeyForModel(model: string): boolean { return false if (storeProvider) return true - if (isOllamaAvailable()) { + if (hasOllamaInstance()) { if (normalizedModel.includes('/')) return true if (normalizedModel in getBaseModelProviders()) return true return false diff --git a/apps/sim/lib/model-router/resolve.ts b/apps/sim/lib/model-router/resolve.ts index 6ca74caad39..b087e5c073e 100644 --- a/apps/sim/lib/model-router/resolve.ts +++ b/apps/sim/lib/model-router/resolve.ts @@ -248,12 +248,13 @@ async function callModelRouter( } /** - * Resolves the sim-auto pseudo-model to a concrete model for one block - * execution. Like any model check it throws when the workspace's permission group - * denies sim-auto itself or its permission config cannot be loaded. Otherwise it never fails - * the workflow: any error, timeout, non-hosted deployment, or fully unavailable - * pool column falls back to `fallbackModel` (the block's standard default). Callers must supply the same already-projected - * model-facing signals they pass to their provider boundary; this router never rescans them. + * Resolves the sim-auto pseudo-model to a concrete model for one block execution. Like any + * model check it throws when the workspace's permission group denies sim-auto itself or its + * permission config cannot be loaded. Otherwise it never fails the workflow: any error, + * timeout, non-hosted deployment, or fully unavailable pool column falls back to + * `fallbackModel` (the block's standard default). Callers must supply the same + * already-projected model-facing signals they pass to their provider boundary; this router + * never rescans them. */ export async function resolveAutoModel(args: { ctx: ExecutionContext @@ -262,7 +263,7 @@ export async function resolveAutoModel(args: { fallbackModel: string }): Promise { const { ctx, blockId, signals, fallbackModel } = args - // Before any fallback: a denied sim-auto must not run as whichever pool or default model is allowed. + // Before any fallback: a denied sim-auto must not run as whichever model is allowed. await validateModelProvider(ctx.userId, ctx.workspaceId ?? undefined, SIM_AUTO_MODEL_ID, ctx) const fallback: AutoRoutingResult = { model: fallbackModel, diff --git a/apps/sim/lib/mothership/request/session/run-replay.ts b/apps/sim/lib/mothership/request/session/run-replay.ts index 8b0af5982aa..582e18e61c2 100644 --- a/apps/sim/lib/mothership/request/session/run-replay.ts +++ b/apps/sim/lib/mothership/request/session/run-replay.ts @@ -98,9 +98,9 @@ export async function openRunReplay(params: { } /** - * The end a `replay_end` frame reports: its reason, `closed` for a frame this build - * cannot parse (an unknown reason, a missing length), or `null` for any other frame. It is worker-to-Sim control, never a - * stream event, whatever reason it carries. + * The end a `replay_end` frame reports: its reason, `closed` for a frame this build cannot + * parse (an unknown reason, a missing length), or `null` for any other frame. It is + * worker-to-Sim control, never a stream event, whatever reason it carries. */ function replayEnd(value: unknown): RunReplayEnd | null { if (!isRecordLike(value) || value.type !== MothershipStreamV1EventType.run) return null diff --git a/apps/sim/lib/workflows/editing/validation-models.test.ts b/apps/sim/lib/workflows/editing/validation-models.test.ts index f7ffe08c9c9..1b864ccbd12 100644 --- a/apps/sim/lib/workflows/editing/validation-models.test.ts +++ b/apps/sim/lib/workflows/editing/validation-models.test.ts @@ -147,3 +147,42 @@ describe('model ids no provider declares', () => { expect(errors[0]?.error).toContain('Unknown model id') }) }) + +describe('fallback model rows', () => { + function validateFallback(blockType: string, model: string) { + const result = validateInputsForBlock( + blockType, + { fallbackModels: [{ id: 'row-1', model }] }, + `${blockType}-1` + ) + return { errors: result.errors, rows: result.validInputs.fallbackModels } + } + + it.each(['agent', 'router_v2', 'evaluator'])( + 'accepts an installed Ollama model on %s where Ollama is reachable, as the model field does', + (blockType) => { + const { errors, rows } = validateFallback(blockType, 'mistral:latest') + expect(errors).toEqual([]) + expect(rows).toEqual([expect.objectContaining({ model: 'mistral:latest' })]) + } + ) + + it('rejects it on hosted Sim with no Ollama', () => { + setEnvFlags({ isHosted: true }) + const { errors } = validateFallback('agent', 'mistral:latest') + expect(errors[0]?.error).toContain('unknown model id "mistral:latest"') + }) + + it('rejects it when the Ollama provider is blacklisted', () => { + envFlagsMockFns.getBlacklistedProvidersFromEnv.mockReturnValue(['ollama']) + const { errors } = validateFallback('agent', 'mistral:latest') + expect(errors[0]?.error).toContain('unknown model id "mistral:latest"') + }) + + it('still rejects sim-auto and free text', () => { + expect(validateFallback('agent', 'sim-auto').errors[0]?.error).toContain('sim-auto') + expect(validateFallback('agent', 'best available model').errors[0]?.error).toContain( + 'unknown model id' + ) + }) +}) diff --git a/apps/sim/lib/workflows/editing/validation.test.ts b/apps/sim/lib/workflows/editing/validation.test.ts index 6838c8beb74..3620f9b5cc9 100644 --- a/apps/sim/lib/workflows/editing/validation.test.ts +++ b/apps/sim/lib/workflows/editing/validation.test.ts @@ -505,7 +505,13 @@ describe('validateInputsForBlock', () => { it('refuses sim-auto, evaluation models, unknown models, missing models, and non-arrays', () => { expect(validate([{ model: 'sim-auto' }]).valid).toBe(false) expect(validate([{ model: 'jev-latest' }]).valid).toBe(false) - expect(validate([{ model: 'definitely-not-a-model-9000' }]).valid).toBe(false) + // An unclaimed Ollama-shaped name is a possible local model wherever Ollama is reachable. + setEnvFlags({ isHosted: true }) + try { + expect(validate([{ model: 'definitely-not-a-model-9000' }]).valid).toBe(false) + } finally { + setEnvFlags({ isHosted: false }) + } expect(validate([{ apiKey: '{{KEY}}' }]).valid).toBe(false) expect(validate({ model: 'claude-sonnet-5' }).valid).toBe(false) }) diff --git a/apps/sim/lib/workflows/editing/validation.ts b/apps/sim/lib/workflows/editing/validation.ts index ad75b75ae51..7df6f8bae2a 100644 --- a/apps/sim/lib/workflows/editing/validation.ts +++ b/apps/sim/lib/workflows/editing/validation.ts @@ -560,7 +560,7 @@ function validateFallbackModelEntry(item: any, index: number): string | null { if (isEvaluationModel(model)) { return `${where}: ${model} is an evaluation model and cannot serve as a chat fallback` } - if (!isKnownModelId(model) && !isCustomModelId(model)) { + if (!isRunnableModelId(model)) { const suggestions = suggestModelIdsForUnknownModel(model) const suggestionText = suggestions.length > 0 ? ` Valid options include: ${suggestions.join(', ')}.` : '' @@ -585,6 +585,23 @@ function validateFallbackModelEntry(item: any, index: number): string | null { return null } +/** + * Whether Sim can run `model`: a catalogued or namespaced id, or a local Ollama name. An id no + * provider claims runs on Ollama (`getProviderFromModel`'s default), so wherever an Ollama server + * is reachable an Ollama-shaped name may be a locally installed model. The server cannot list + * what is installed, so a mistyped name of that shape fails at execution instead. sim-auto is + * never one: it never reaches a provider as itself. + */ +function isRunnableModelId(model: string): boolean { + if (isKnownModelId(model) || isCustomModelId(model)) return true + return ( + !isAutoModel(model) && + OLLAMA_MODEL_NAME.test(model) && + findProviderFromModel(model) === null && + isOllamaAvailable() + ) +} + /** A file object as the file-upload editor stores it: at least its name and serve path. */ function isStoredFile(value: unknown): boolean { return isPlainRecord(value) && Boolean(value.name) && Boolean(value.path) @@ -1001,22 +1018,10 @@ export function validateValueForSubBlockType( } // sim-auto is valid only where the source offers Auto, which it does on hosted Sim // only; writes of it anywhere else are rejected as unknown. - const isAuto = isAutoModel(trimmed) - if (isAuto && modelSource.auto && isHostedDeployment) { + if (isAutoModel(trimmed) && modelSource.auto && isHostedDeployment) { return { valid: true, value: trimmed.toLowerCase() } } - // An id no provider claims runs on Ollama (`getProviderFromModel`'s default), so - // wherever an Ollama server is reachable an Ollama-shaped name may be a locally - // installed model. The server cannot list what is installed, so a mistyped name of - // that shape fails at execution instead. sim-auto is never one: it never reaches a - // provider as itself. - const isUnknown = !isKnownModelId(trimmed) && !isCustomModelId(trimmed) - const mayBeLocalModel = - !isAuto && - OLLAMA_MODEL_NAME.test(trimmed) && - findProviderFromModel(trimmed) === null && - isOllamaAvailable() - if (trimmed !== '' && isUnknown && !mayBeLocalModel) { + if (trimmed !== '' && !isRunnableModelId(trimmed)) { const suggestions = suggestModelIdsForUnknownModel(trimmed) const suggestionText = suggestions.length > 0 ? ` Valid options include: ${suggestions.join(', ')}.` : ''