Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: share one edge index for Recipe Studio node connection status (#15942)
Daniel Han3bee6d8626de
diff --git a/studio/frontend/src/features/recipe-studio/components/recipe-graph-node.tsx b/studio/frontend/src/features/recipe-studio/components/recipe-graph-node.tsxindex 8bc0f9cd972..7c063755a8e 100644--- a/studio/frontend/src/features/recipe-studio/components/recipe-graph-node.tsx+++ b/studio/frontend/src/features/recipe-studio/components/recipe-graph-node.tsx@@ -31,12 +31,13 @@ import {Position,useUpdateNodeInternals,} from "@xyflow/react";-import { type ReactElement, memo, useEffect, useMemo } from "react";+import { type ReactElement, memo, useEffect } from "react";import {MAX_NODE_WIDTH,MAX_NOTE_NODE_WIDTH,MIN_NODE_WIDTH,} from "../constants";+import { useLocalProviderNames } from "../hooks/use-local-provider-names";import { useNodeConnectionStatus } from "../hooks/use-node-connection-status";import { useRecipeStudioStore } from "../stores/recipe-studio";import type {@@ -363,16 +364,7 @@ function RecipeGraphNodeBase({const config = useRecipeStudioStore((state) => state.configs[id]);const openConfig = useRecipeStudioStore((state) => state.openConfig);const updateConfig = useRecipeStudioStore((state) => state.updateConfig);- const allConfigs = useRecipeStudioStore((state) => state.configs);- const localProviderNames = useMemo(() => {- const names = new Set<string>();- for (const cfg of Object.values(allConfigs)) {- if (cfg.kind === "model_provider" && cfg.is_local === true) {- names.add(cfg.name);- }- }- return names;- }, [allConfigs]);+ const localProviderNames = useLocalProviderNames();const llmAuxVisible = useRecipeStudioStore((state) => state.llmAuxVisibility[id] ?? false,);diff --git a/studio/frontend/src/features/recipe-studio/hooks/use-local-provider-names.ts b/studio/frontend/src/features/recipe-studio/hooks/use-local-provider-names.tsnew file mode 100644index 00000000000..406bb898d00--- /dev/null+++ b/studio/frontend/src/features/recipe-studio/hooks/use-local-provider-names.ts@@ -0,0 +1,14 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import { useMemo } from "react";+import { useRecipeStudioStore } from "../stores/recipe-studio";+import { localProviderNamesKey } from "../utils/node-connection-status";++/** The same Set until the local provider names change, so typing in a node re-renders no other node for it. */+export function useLocalProviderNames(): Set<string> {+ const key = useRecipeStudioStore((state) =>+ localProviderNamesKey(state.configs),+ );+ return useMemo(() => new Set<string>(JSON.parse(key) as string[]), [key]);+}diff --git a/studio/frontend/src/features/recipe-studio/hooks/use-node-connection-status.ts b/studio/frontend/src/features/recipe-studio/hooks/use-node-connection-status.tsindex a547a09d33c..b4c9bb1dc19 100644--- a/studio/frontend/src/features/recipe-studio/hooks/use-node-connection-status.ts+++ b/studio/frontend/src/features/recipe-studio/hooks/use-node-connection-status.ts@@ -1,51 +1,19 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-import { useMemo } from "react";import { useRecipeStudioStore } from "../stores/recipe-studio";-import { INFRA_NODE_KINDS, type NodeConfig } from "../types";--type ConnectionStatus = {- isDisconnected: boolean;- /** No incoming data edge (infra edges only). */- missingDataInput: boolean;-};+import type { NodeConfig } from "../types";+import {+ type ConnectionStatus,+ nodeConnectionStatus,+} from "../utils/node-connection-status";export function useNodeConnectionStatus(nodeId: string,config: NodeConfig | undefined,): ConnectionStatus {- const edges = useRecipeStudioStore((state) => state.edges);- const configs = useRecipeStudioStore((state) => state.configs);-- return useMemo(() => {- const empty: ConnectionStatus = {- isDisconnected: false,- missingDataInput: false,- };-- if (!config || config.kind === "markdown_note") {- return empty;- }-- const nodeEdges = edges.filter(- (e) => e.source === nodeId || e.target === nodeId,- );- const isDisconnected = nodeEdges.length === 0;-- let missingDataInput = false;- if (config.kind === "llm" && !isDisconnected) {- const hasDataEdge = nodeEdges.some((e) => {- const otherId = e.source === nodeId ? e.target : e.source;- const otherConfig = configs[otherId];- return otherConfig && !INFRA_NODE_KINDS.has(otherConfig.kind);- });- missingDataInput = !hasDataEdge;- }-- return {- isDisconnected,- missingDataInput,- };- }, [nodeId, config, edges, configs]);+ // Returns one of three shared objects, so a node re-renders only when its own status changes.+ return useRecipeStudioStore((state) =>+ nodeConnectionStatus(nodeId, config, state.edges, state.configs),+ );}diff --git a/studio/frontend/src/features/recipe-studio/utils/node-connection-status.ts b/studio/frontend/src/features/recipe-studio/utils/node-connection-status.tsnew file mode 100644index 00000000000..9d843fb8076--- /dev/null+++ b/studio/frontend/src/features/recipe-studio/utils/node-connection-status.ts@@ -0,0 +1,97 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import type { Edge } from "@xyflow/react";+import { INFRA_NODE_KINDS, type NodeConfig } from "../types";++export type ConnectionStatus = {+ isDisconnected: boolean;+ /** No incoming data edge (infra edges only). */+ missingDataInput: boolean;+};++const CONNECTED: ConnectionStatus = {+ isDisconnected: false,+ missingDataInput: false,+};+const DISCONNECTED: ConnectionStatus = {+ isDisconnected: true,+ missingDataInput: false,+};+const MISSING_DATA_INPUT: ConnectionStatus = {+ isDisconnected: false,+ missingDataInput: true,+};++type Adjacency = Map<string, string[]>;++// One index per edges array, shared by every node: the store replaces the array on each change.+const adjacencyByEdges = new WeakMap<readonly Edge[], Adjacency>();++export function edgeAdjacency(edges: readonly Edge[]): Adjacency {+ const cached = adjacencyByEdges.get(edges);+ if (cached) {+ return cached;+ }+ const index: Adjacency = new Map();+ const add = (nodeId: string, otherId: string) => {+ const others = index.get(nodeId);+ if (others) {+ others.push(otherId);+ } else {+ index.set(nodeId, [otherId]);+ }+ };+ for (const edge of edges) {+ add(edge.source, edge.target);+ add(edge.target, edge.source);+ }+ adjacencyByEdges.set(edges, index);+ return index;+}++export function nodeConnectionStatus(+ nodeId: string,+ config: NodeConfig | undefined,+ edges: readonly Edge[],+ configs: Record<string, NodeConfig>,+): ConnectionStatus {+ if (!config || config.kind === "markdown_note") {+ return CONNECTED;+ }+ const others = edgeAdjacency(edges).get(nodeId);+ if (!others) {+ return DISCONNECTED;+ }+ if (config.kind !== "llm") {+ return CONNECTED;+ }+ const hasDataEdge = others.some((otherId) => {+ const otherConfig = configs[otherId];+ return otherConfig && !INFRA_NODE_KINDS.has(otherConfig.kind);+ });+ return hasDataEdge ? CONNECTED : MISSING_DATA_INPUT;+}++// One key per configs object, shared by every node: the store replaces the object on each change.+const localProviderKeyByConfigs = new WeakMap<+ Record<string, NodeConfig>,+ string+>();++export function localProviderNamesKey(+ configs: Record<string, NodeConfig>,+): string {+ let key = localProviderKeyByConfigs.get(configs);+ if (key === undefined) {+ const names: string[] = [];+ for (const cfg of Object.values(configs)) {+ if (cfg.kind === "model_provider" && cfg.is_local === true) {+ names.push(cfg.name);+ }+ }+ key = JSON.stringify(names);+ localProviderKeyByConfigs.set(configs, key);+ }+ return key;+}diff --git a/studio/frontend/tests/recipe-node-connection-status.test.ts b/studio/frontend/tests/recipe-node-connection-status.test.tsnew file mode 100644index 00000000000..65c3678d25b--- /dev/null+++ b/studio/frontend/tests/recipe-node-connection-status.test.ts@@ -0,0 +1,163 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";++import { registerBundlerResolver } from "./helpers/kit.ts";++registerBundlerResolver();++const { edgeAdjacency, localProviderNamesKey, nodeConnectionStatus } =+ await import("../src/features/recipe-studio/utils/node-connection-status.ts");+const { INFRA_NODE_KINDS } = await import(+ "../src/features/recipe-studio/types/index.ts"+);++type Edge = { id: string; source: string; target: string };+type Config = { id: string; kind: string; name: string; is_local?: boolean };+type Status = { isDisconnected: boolean; missingDataInput: boolean };++// The per-node edge scan this replaces, kept verbatim as the oracle.+function scanEdges(+ nodeId: string,+ config: Config | undefined,+ edges: Edge[],+ configs: Record<string, Config>,+): Status {+ if (!config || config.kind === "markdown_note") {+ return { isDisconnected: false, missingDataInput: false };+ }+ const nodeEdges = edges.filter(+ (e) => e.source === nodeId || e.target === nodeId,+ );+ const isDisconnected = nodeEdges.length === 0;+ let missingDataInput = false;+ if (config.kind === "llm" && !isDisconnected) {+ const hasDataEdge = nodeEdges.some((e) => {+ const otherId = e.source === nodeId ? e.target : e.source;+ const otherConfig = configs[otherId];+ return otherConfig && !INFRA_NODE_KINDS.has(otherConfig.kind);+ });+ missingDataInput = !hasDataEdge;+ }+ return { isDisconnected, missingDataInput };+}++function rng(seed: number): () => number {+ let s = seed >>> 0;+ return () => {+ s = (Math.imul(s ^ (s >>> 15), 0x2c1b3c6d) + 0x9e3779b9) >>> 0;+ return s / 2 ** 32;+ };+}++const KINDS = [+ "llm",+ "sampler",+ "expression",+ "seed",+ "validator",+ "markdown_note",+ "model_provider",+ "model_config",+ "tool_config",+];++function randomGraph(seed: number) {+ const r = rng(seed);+ const pick = <T>(xs: T[]): T => xs[Math.floor(r() * xs.length)] as T;+ const ids = Array.from(+ { length: 2 + Math.floor(r() * 30) },+ (_, i) => `n${i}`,+ );+ ids.push("__proto__", "constructor", "toString");+ const configs: Record<string, Config> = {};+ for (const id of ids) {+ if (r() < 0.85 && id !== "__proto__")+ configs[id] = { id, kind: pick(KINDS), name: id };+ }+ const edges: Edge[] = [];+ const count = Math.floor(r() * ids.length * 2);+ for (let i = 0; i < count; i++) {+ const source = pick(ids);+ const target = r() < 0.08 ? source : pick(ids);+ edges.push({ id: `e${i}`, source, target });+ }+ return { ids: [...ids, "missing"], configs, edges };+}++test("every node's status matches the per-node edge scan on random graphs", () => {+ for (let seed = 1; seed <= 400; seed++) {+ const { ids, configs, edges } = randomGraph(seed);+ for (const id of ids) {+ for (const config of [+ configs[id],+ undefined,+ { id, kind: "llm", name: id },+ ]) {+ assert.deepEqual(+ {+ ...nodeConnectionStatus(+ id,+ config as never,+ edges as never,+ configs as never,+ ),+ },+ scanEdges(id, config, edges, configs),+ `seed ${seed}, node ${id}, kind ${config?.kind}`,+ );+ }+ }+ }+});++test("the adjacency index is built once per edges array and a status is a shared value", () => {+ const edges: Edge[] = [{ id: "a", source: "x", target: "y" }];+ assert.equal(edgeAdjacency(edges as never), edgeAdjacency(edges as never));+ const configs = {+ x: { id: "x", kind: "llm", name: "x" },+ y: { id: "y", kind: "sampler", name: "y" },+ };+ // Configs change per keystroke; an unchanged status must keep its identity or every node re-renders.+ const before = nodeConnectionStatus(+ "x",+ configs.x as never,+ edges as never,+ configs as never,+ );+ const typed = { ...configs, y: { ...configs.y } };+ assert.equal(+ nodeConnectionStatus("x", typed.x as never, edges as never, typed as never),+ before,+ );+});++test("the local provider key changes only with the local provider names", () => {+ const base = {+ a: { id: "a", kind: "model_provider", name: "remote" },+ b: { id: "b", kind: "model_provider", name: "lp", is_local: true },+ c: { id: "c", kind: "llm", name: "q", prompt: "hi" },+ };+ const key = localProviderNamesKey(base as never);+ assert.equal(key, JSON.stringify(["lp"]));+ assert.equal(+ localProviderNamesKey({+ ...base,+ c: { ...base.c, prompt: "hi there" },+ } as never),+ key,+ );+ assert.equal(+ localProviderNamesKey({+ ...base,+ a: { ...base.a, is_local: true },+ } as never),+ JSON.stringify(["remote", "lp"]),+ );+ assert.equal(+ localProviderNamesKey({ ...base, b: { ...base.b, name: "lp2" } } as never),+ JSON.stringify(["lp2"]),+ );+});