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: title a chat with the connection that answered it, not the local model (#9106)
Long Yixing1bfd1237297d
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex eb76ff72009..c57f07ec266 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -127,8 +127,6 @@ import type { MessageTiming, ToolCallMessagePart } from "@assistant-ui/core";import type { ChatModelAdapter } from "@assistant-ui/react";import { parsePartialJsonObject } from "assistant-stream/utils";import {- getExternalProviderApiKey,- isCustomProviderType,isExternalModelId,isPromptCacheTtl,loadExternalProviders,@@ -138,7 +136,6 @@ import {providerModelTakesMcpImages,supportsProviderPromptCacheTtl,supportsProviderPromptCaching,- toExternalBackendProviderType,} from "../external-providers";import {@@ -170,6 +167,11 @@ import {import { buildResearchInferenceRequest } from "../research-inference-request";import { pickFriendlyContainerName } from "../lib/friendly-names";+import {+ buildExternalRoutingFields,+ type ExternalRoutingUnavailableReason,+ resolveExternalRouting,+} from "../utils/chat-title";import {reasoningCapsFromLoad,resolveInferenceCheckpointId,@@ -191,7 +193,6 @@ import {getExternalReasoningCapabilities,providerSupportsPreserveThinking,getProviderCapabilities,- isGeminiCustomOpenAICompatBase,providerHostsCodeExecution,providerSupportsBuiltinCodeExecution,providerSupportsBuiltinImageGeneration,@@ -337,10 +338,7 @@ import {createOpenAIContainer,listOpenAIContainers,} from "./openai-containers";-import {- encryptProviderApiKey,- isProviderKeyRotationError,-} from "./providers-api";+import { isProviderKeyRotationError } from "./providers-api";import {beginExternalResearchFollow,ingestResearchUpdate,@@ -377,6 +375,28 @@ import {// Small models (<=9B) answer from memory, so "auto" forces retrieval for them.const AUTOINJECT_AUTO_MAX_SIZE_B = 9;+const EXTERNAL_ROUTING_REFUSALS: Record<+ ExternalRoutingUnavailableReason,+ { title: string; description: string; error: string }+> = {+ "connections-disabled": {+ title: "Connections are disabled.",+ description:+ "Turn on Enable connections in Settings → Connections to use hosted models.",+ error: "Connections disabled.",+ },+ "connection-missing": {+ title: "Connection not found.",+ description: "Open Settings → Connections and add it again.",+ error: "Connection not found.",+ },+ "missing-api-key": {+ title: "Missing API key for selected connection.",+ description: "Open Settings → Connections and set the API key again.",+ error: "Missing connection API key.",+ },+};+class ChatGenerationTerminalError extends Error {readonly generationStatus: "cancelled" | "failed";@@ -4817,22 +4837,15 @@ export function createOpenAIStreamAdapter(: false;const externalSelection = parseExternalModelId(params.checkpoint);const isExternalRequest = externalSelection !== null;- if (- isExternalRequest &&- !useExternalProvidersStore.getState().connectionsEnabled- ) {- toast.error("Connections are disabled.", {- description:- "Turn on Enable connections in Settings → Connections to use hosted models.",- });+ const externalRouting = resolveExternalRouting(params.checkpoint);+ if (externalRouting.kind === "unavailable") {+ const refusal = EXTERNAL_ROUTING_REFUSALS[externalRouting.reason];+ toast.error(refusal.title, { description: refusal.description });clearSelectedImageEditReference();- throw new Error("Connections disabled.");+ throw new Error(refusal.error);}- const externalProvider = isExternalRequest- ? loadExternalProviders().find(- (provider) => provider.id === externalSelection.providerId,- )- : null;+ const externalProvider =+ externalRouting.kind === "external" ? externalRouting.provider : null;const externalUsesStudioTools =providerModelSupportsStudioTools(@@ -4847,43 +4860,9 @@ export function createOpenAIStreamAdapter((model) => model.id === params.checkpoint,);const externalApiKey =- externalProvider && !externalProvider.hasApiKey- ? getExternalProviderApiKey(externalProvider.id).trim()- : "";-- if (isExternalRequest && !externalProvider) {- toast.error("Connection not found.", {- description: "Open Settings → Connections and add it again.",- });- clearSelectedImageEditReference();- throw new Error("Connection not found.");- }- // Local providers and custom Gemini bases allow an empty key.- const externalProviderIsCustom = externalProvider- ? isCustomProviderType(externalProvider.providerType)- : false;- const externalProviderIsGeminiCustomBase = Boolean(- externalProvider &&- externalProvider.providerType === "gemini" &&- isGeminiCustomOpenAICompatBase(externalProvider.baseUrl),- );- const externalProviderUsesOAuth =- externalProvider?.authKind === "chatgpt_oauth";-- if (- isExternalRequest &&- !externalApiKey &&- !externalProvider?.hasApiKey &&- !externalProviderUsesOAuth &&- !externalProviderIsCustom &&- !externalProviderIsGeminiCustomBase- ) {- toast.error("Missing API key for selected connection.", {- description: "Open Settings → Connections and set the API key again.",- });- clearSelectedImageEditReference();- throw new Error("Missing connection API key.");- }+ externalRouting.kind === "external" ? externalRouting.apiKey : "";+ const externalModelId =+ externalRouting.kind === "external" ? externalRouting.modelId : "";// Image-generation flag; computed first so Gemini image mode can suppress Search/Code.const imageGenerationEnabledForThisTurn = Boolean(@@ -6097,9 +6076,6 @@ export function createOpenAIStreamAdapter(}const { supportsPreserveThinking, preserveThinking } = runtime;- const externalBackendProviderType = toExternalBackendProviderType(- externalProvider?.providerType,- );const buildResponseDetails = (finishedAt: number,): ResponseDetailsMetadata => ({@@ -6438,19 +6414,14 @@ export function createOpenAIStreamAdapter(// Also on this body: a provider whose models run Studio tools can hand off too, and omitting// it makes arming research a no-op....(deepResearchArmed ? { deep_research_armed: true } : {}),- provider_id: externalProvider.id,- provider_type: externalBackendProviderType,- external_model: externalSelection.modelId,- ...(externalApiKey- ? {- encrypted_api_key: await encryptProviderApiKey(- externalApiKey,- forceRefreshPublicKey,- ),- }- : {}),- provider_base_url: externalProvider.baseUrl || null,- provider_api_type: externalProvider.apiType ?? "chat_completions",+ ...(await buildExternalRoutingFields(+ {+ provider: externalProvider,+ modelId: externalModelId,+ apiKey: externalApiKey,+ },+ { forceRefreshPublicKey },+ )),...(openaiCodeExecContainerId? {openai_code_exec_container_id: openaiCodeExecContainerId,diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsxindex 4fb1bd7db79..9a8cdfc3ce2 100644--- a/studio/frontend/src/features/chat/runtime-provider.tsx+++ b/studio/frontend/src/features/chat/runtime-provider.tsx@@ -47,6 +47,7 @@ import {} from "./api/chat-adapter";import {CHAT_HISTORY_UPDATED_EVENT,+ streamChatCompletions,uploadChatAttachmentOriginal,} from "./api/chat-api";import { selectCodeToolNames } from "./api/code-tool-placement";@@ -192,7 +193,13 @@ import {isChatThreadDeleted,markChatThreadDeleted,} from "./utils/chat-thread-tombstones";-import { fallbackTitleFromUserText } from "./utils/chat-title";+import {+ answeringCheckpoint,+ buildTitleRequest,+ fallbackTitleFromUserText,+ titleCheckpoint,+ titleFromStream,+} from "./utils/chat-title";import { syncExportedRepositoryToBackend } from "./utils/delete-thread-message";import { getImageInputUnavailableReason } from "./utils/image-input-support";import {@@ -233,15 +240,6 @@ const pendingRunStartReadyByMessageId = new Map<>();const pendingRunStartThreadIdsByMessageId = new Map<string, string[]>();-type TitleResponse = {- choices?: Array<{- finish_reason?: string | null;- message?: {- content?: string;- };- }>;-};-class PreStreamAwareAttachmentAdapter implements AttachmentAdapter {private readonly delegate: AttachmentAdapter;private readonly getThreadIds: () => Array<string | null | undefined>;@@ -987,11 +985,11 @@ function titleTextOf(m: ThreadMessage | undefined): string {}async function generateTitleWithModel(payload: {+ checkpoint: string;userText: string;assistantText?: string;}): Promise<string | null> {- const params = useChatRuntimeStore.getState().params;- if (!params.checkpoint) return null;+ if (!payload.checkpoint) return null;const user = clip(payload.userText, 256);const assistant = clip(payload.assistantText ?? "", 384);@@ -1000,62 +998,19 @@ async function generateTitleWithModel(payload: {parts.push(`Assistant: ${assistant}`);}- function normalizeTitle(raw: string): string | null {- let title = raw.split(/\r?\n/, 1)[0] ?? "";- title = title.replace(/^\s*title\s*:\s*/i, "");- title = title.replace(/[^\x20-\x7E]+/g, " ");- title = title.replace(/["'`]+/g, "");-- // Echo fail-safe: reject leading role labels before punctuation strips the ":".- if (/^\s*(user|assistant|base|lora)\s*:/i.test(title)) {- return null;- }-- title = title.replace(/[.!?:;,]+/g, " ");- title = title.replace(/\s+/g, " ").trim();-- const words = title.split(" ").filter(Boolean).slice(0, 6);- const joined = words.join(" ").trim();- if (!joined) return null;- return joined.length > 60 ? joined.slice(0, 60).trimEnd() : joined;+ try {+ // Inside the try: building this encrypts a browser key over the network.+ const request = await buildTitleRequest(+ payload.checkpoint,+ parts.join("\n"),+ );+ if (!request) return null;+ return await titleFromStream(+ streamChatCompletions(request, new AbortController().signal),+ );+ } catch {+ return null;}-- const response = await authFetch("/v1/chat/completions", {- method: "POST",- headers: { "Content-Type": "application/json" },- body: JSON.stringify({- model: params.checkpoint,- stream: false,- temperature: 0.2,- top_p: 0.9,- max_tokens: 24,- top_k: 20,- repetition_penalty: 1.0,- enable_thinking: false,- reasoning_effort: "none",- // Titling is a one-shot summarisation: never let it enter the tool loop. Omitting the field- // would inherit the server's tools-on default and put tool schemas in a 24-token prompt.- enable_tools: false,- messages: [- {- role: "system",- content:- "Write 1 concise chat title summarizing the conversation topic, not the user's exact wording. Use the assistant reply as context when provided. Rules: 2-6 words, no quotes, no punctuation, ASCII only, do not echo input. Output title only.",- },- { role: "user", content: parts.join("\n") },- ],- }),- });-- const body = (await response- .json()- .catch(() => null)) as TitleResponse | null;- if (!response.ok) return null;- const choice = body?.choices?.[0];- if (choice?.finish_reason === "length") return null;- const raw: string | undefined = choice?.message?.content;- if (!raw || /<\/?think>/i.test(raw)) return null;- return normalizeTitle(raw);}const inflightTitleByKey = new Set<string>();@@ -1885,6 +1840,9 @@ function createStudioDbAdapter();const userText = titleTextOf(firstUser) || defaultTitle;const assistantText = extractTextParts(firstAssistant);+ const answeredWith = answeringCheckpoint(+ firstAssistant?.metadata?.custom,+ );if (!autoTitle) {const title = fallbackTitleFromUserText(userText);@@ -1921,6 +1879,10 @@ function createStudioDbAdapter(try {const title =(await generateTitleWithModel({+ checkpoint: titleCheckpoint(+ answeredWith,+ useChatRuntimeStore.getState().params.checkpoint,+ ),userText,assistantText,})) || fallbackTitleFromUserText(userText);diff --git a/studio/frontend/src/features/chat/types/api.ts b/studio/frontend/src/features/chat/types/api.tsindex 4cae8f67425..e4d818129ac 100644--- a/studio/frontend/src/features/chat/types/api.ts+++ b/studio/frontend/src/features/chat/types/api.ts@@ -703,7 +703,8 @@ export interface OpenAIChatCompletionsRequest {export interface OpenAIChatDelta {role?: string;- content?: string | null;+ /** Magistral streams structured content parts: read through extractDeltaText. */+ content?: string | unknown[] | null;/** Streamed assistant tool calls. The Gemini and OpenAI Responses translators emit incremental* deltas so the chat-adapter can render tool cards as they arrive. */tool_calls?: OpenAIToolCallPart[];diff --git a/studio/frontend/src/features/chat/utils/chat-title.ts b/studio/frontend/src/features/chat/utils/chat-title.tsindex c2bd6d0cd25..da167e65ffe 100644--- a/studio/frontend/src/features/chat/utils/chat-title.ts+++ b/studio/frontend/src/features/chat/utils/chat-title.ts@@ -1,7 +1,32 @@// 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 {+ buildExternalModelId,+ parseExternalModelId,+} from "../external-providers";+import { encryptProviderApiKey } from "../api/providers-api";+import {+ type ExternalProviderConfig,+ getExternalProviderApiKey,+ isCustomProviderType,+ loadExternalProviders,+ toExternalBackendProviderType,+} from "../external-providers";+import {+ clampReasoningEffortToLevels,+ getExternalMinOutputTokens,+ getExternalReasoningCapabilities,+ getProviderCapabilities,+ isGeminiCustomOpenAICompatBase,+} from "../provider-capabilities";+import { useExternalProvidersStore } from "../stores/external-providers-store";import type { MessageRecord, ThreadRecord } from "../types";+import type {+ OpenAIChatChunk,+ OpenAIChatCompletionsRequest,+} from "../types/api";+import { extractDeltaText } from "./parse-assistant-content";/** Store the whole first line and let the sidebar clip it with CSS, so a wider one shows more.* Matches the rename input's maxLength: UTF-16 units, ellipsis included. */@@ -176,3 +201,253 @@ export function planLegacyTitleRepairs(}return repairs;}++// Routing lives here, not in the adapter, so node --test can load it (the provider is JSX).++/** The backend dispatches on provider_id / provider_type, never parsing the external:: model id. */+export interface ExternalRoutingFields {+ provider_id: string;+ provider_type: string;+ external_model: string;+ provider_base_url: string | null;+ provider_api_type: "chat_completions" | "responses";+ encrypted_api_key?: string;+}++export type ExternalRoutingUnavailableReason =+ | "connections-disabled"+ | "connection-missing"+ | "missing-api-key";++export interface ResolvedExternalConnection {+ provider: ExternalProviderConfig;+ modelId: string;+ /** Browser-held key, or "" when the backend holds one or none is needed. */+ apiKey: string;+}++export type ExternalRoutingTarget =+ | { kind: "local" }+ | { kind: "unavailable"; reason: ExternalRoutingUnavailableReason }+ | ({ kind: "external" } & ResolvedExternalConnection);++/** A request answering only some of these is served by the local model instead (#9045). */+export function resolveExternalRouting(+ checkpoint: string | null | undefined,+): ExternalRoutingTarget {+ const selection = parseExternalModelId(checkpoint);+ if (selection === null) return { kind: "local" };++ if (!useExternalProvidersStore.getState().connectionsEnabled) {+ return { kind: "unavailable", reason: "connections-disabled" };+ }++ const provider = loadExternalProviders().find(+ (c) => c.id === selection.providerId,+ );+ if (!provider) return { kind: "unavailable", reason: "connection-missing" };++ // Installation-saved key wins: the browser copy may be stale.+ const apiKey = provider.hasApiKey+ ? ""+ : getExternalProviderApiKey(provider.id).trim();+ const keyOptional =+ Boolean(provider.hasApiKey) ||+ provider.authKind === "chatgpt_oauth" ||+ isCustomProviderType(provider.providerType) ||+ (provider.providerType === "gemini" &&+ isGeminiCustomOpenAICompatBase(provider.baseUrl));+ if (!apiKey && !keyOptional)+ return { kind: "unavailable", reason: "missing-api-key" };++ return { kind: "external", provider, modelId: selection.modelId, apiKey };+}++/** Encrypted per attempt so a rotated-key retry can rebuild with forceRefreshPublicKey. */+export async function buildExternalRoutingFields(+ connection: ResolvedExternalConnection,+ options: { forceRefreshPublicKey?: boolean } = {},+): Promise<ExternalRoutingFields> {+ const { provider, modelId, apiKey } = connection;+ return {+ provider_id: provider.id,+ provider_type: toExternalBackendProviderType(provider.providerType),+ external_model: modelId,+ provider_base_url: provider.baseUrl || null,+ provider_api_type: provider.apiType ?? "chat_completions",+ ...(apiKey+ ? {+ encrypted_api_key: await encryptProviderApiKey(+ apiKey,+ options.forceRefreshPublicKey ?? false,+ ),+ }+ : {}),+ };+}++/** A deep research run's config is evidence only once it completed. */+export function answeringCheckpoint(custom: unknown): string {+ const meta = (custom ?? {}) as {+ responseDetails?: { modelId?: unknown };+ researchRun?: {+ status?: unknown;+ config?: { inferenceRequest?: Record<string, unknown> };+ };+ };+ const stamped = meta.responseDetails?.modelId;+ if (typeof stamped === "string" && stamped) return stamped;++ const run = meta.researchRun;+ if (run?.status !== "completed") return "";++ const inference = run.config?.inferenceRequest ?? {};+ const { providerId, providerType, externalModel } = inference;+ const routed = [providerId, providerType, externalModel].every(+ (field) => typeof field === "string" && field !== "",+ );+ if (routed)+ return buildExternalModelId(providerId as string, externalModel as string);+ const model = typeof inference.model === "string" ? inference.model : "";+ return parseExternalModelId(model) === null ? model : "";+}++/** Follows the connection that answered, not the live selection, so the excerpt never reaches an unused connection; local models follow the selection so an evicted one is not reloaded. */+export function titleCheckpoint(+ answeredWith: string,+ activeCheckpoint: string,+): string {+ if (parseExternalModelId(answeredWith) !== null) return answeredWith;+ return parseExternalModelId(activeCheckpoint) === null+ ? activeCheckpoint+ : "";+}++const VERBATIM_EFFORT_PROVIDER_TYPES = new Set(["openai", "openai_codex"]);++type TitleReasoningFields = Pick<+ OpenAIChatCompletionsRequest,+ "enable_thinking" | "reasoning_effort"+>;++function titleReasoningCaps(connection: ResolvedExternalConnection) {+ const { provider, modelId } = connection;+ return getExternalReasoningCapabilities(provider.providerType, modelId, {+ isReasoningProvider: provider.isReasoningModel === true,+ baseUrl: provider.baseUrl ?? null,+ apiType: provider.apiType,+ });+}++/** Responses route: either field becomes reasoning.effort (400 on non-reasoning models), sent verbatim, so omit or clamp. */+function titleReasoningFields(+ connection: ResolvedExternalConnection,+): TitleReasoningFields {+ const { provider } = connection;+ const caps = titleReasoningCaps(connection);+ const responsesRoute =+ VERBATIM_EFFORT_PROVIDER_TYPES.has(provider.providerType) ||+ provider.apiType === "responses";+ if (responsesRoute && !caps.supportsReasoning) return {};+ const clamp = responsesRoute && caps.reasoningStyle === "reasoning_effort";+ return {+ enable_thinking: false,+ reasoning_effort: clamp+ ? clampReasoningEffortToLevels("none", caps.reasoningEffortLevels)+ : "none",+ };+}++/** Reasoning that cannot be turned off counts toward the cap (Gemini 2.5 Pro forces 128), so it gets headroom. */+function titleMaxTokens(connection: ResolvedExternalConnection): number {+ const floor = getExternalMinOutputTokens(connection.provider.providerType);+ const caps = titleReasoningCaps(connection);+ const forcedReasoning = caps.supportsReasoning && !caps.supportsReasoningOff;+ return Math.max(24, floor, forcedReasoning ? 1024 : 0);+}++const TITLE_SYSTEM_PROMPT =+ "Write 1 concise chat title summarizing the conversation topic, not the user's exact wording. Use the assistant reply as context when provided. Rules: 2-6 words, no quotes, no punctuation, ASCII only, do not echo input. Output title only.";++export async function buildTitleRequest(+ checkpoint: string,+ prompt: string,+): Promise<OpenAIChatCompletionsRequest | null> {+ const routing = resolveExternalRouting(checkpoint);+ if (routing.kind === "unavailable") return null;++ // The backend forwards sampling fields verbatim, so gate them as the chat request does.+ const caps =+ routing.kind === "external"+ ? getProviderCapabilities(+ routing.provider.providerType,+ routing.provider.apiType,+ routing.modelId,+ routing.provider.baseUrl,+ )+ : null;+ const local = routing.kind === "local";++ return {+ model: checkpoint,+ // Required: the proxy answers SSE, so stream:false has no readable body.+ stream: true,+ ...(local || caps?.temperature !== false ? { temperature: 0.2 } : {}),+ ...(local || caps?.topP !== false ? { top_p: 0.9 } : {}),+ max_tokens: routing.kind === "external" ? titleMaxTokens(routing) : 24,+ ...(local || caps?.topK ? { top_k: 20 } : {}),+ ...(local || caps?.repetitionPenalty ? { repetition_penalty: 1.0 } : {}),+ ...(routing.kind === "external"+ ? titleReasoningFields(routing)+ : { enable_thinking: false, reasoning_effort: "none" as const }),+ // Else the server's tools-on default adds tool schemas.+ enable_tools: false,+ messages: [+ { role: "system", content: TITLE_SYSTEM_PROMPT },+ { role: "user", content: prompt },+ ],+ ...(routing.kind === "external"+ ? await buildExternalRoutingFields(routing)+ : {}),+ };+}++/** Truncated answers are discarded: hitting the token cap means it wrote something else. */+export async function titleFromStream(+ chunks: AsyncIterable<OpenAIChatChunk>,+): Promise<string | null> {+ let content = "";+ let finishReason: string | null = null;+ for await (const chunk of chunks) {+ const choice = chunk.choices?.[0];+ content += extractDeltaText(choice?.delta?.content).text;+ // A later usage chunk has no finish reason; it must not erase this one.+ finishReason = choice?.finish_reason ?? finishReason;+ }++ if (finishReason === "length") return null;+ // A model that cannot turn reasoning off streams its summary as a closed think block first.+ const visible = content.replace(/<think>[\s\S]*?<\/think>/gi, "");+ if (!visible || /<\/?think>/i.test(visible)) return null;+ return normalizeTitle(visible);+}++export function normalizeTitle(raw: string): string | null {+ let title = raw.split(/\r?\n/, 1)[0] ?? "";+ title = title.replace(/^\s*title\s*:\s*/i, "");+ title = title.replace(/[^\x20-\x7E]+/g, " ");+ title = title.replace(/["'`]+/g, "");++ // Echo fail-safe: reject leading role labels before punctuation strips the ":".+ if (/^\s*(user|assistant|base|lora)\s*:/i.test(title)) {+ return null;+ }++ title = title.replace(/[.!?:;,]+/g, " ");+ title = title.replace(/\s+/g, " ").trim();++ const words = title.split(" ").filter(Boolean).slice(0, 6);+ const joined = words.join(" ").trim();+ if (!joined) return null;+ return joined.length > 60 ? joined.slice(0, 60).trimEnd() : joined;+}diff --git a/studio/frontend/tests/chat-title-clip.test.ts b/studio/frontend/tests/chat-title-clip.test.tsindex db2b70312e4..77c37cd8c12 100644--- a/studio/frontend/tests/chat-title-clip.test.ts+++ b/studio/frontend/tests/chat-title-clip.test.ts@@ -1,18 +1,43 @@+// 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 { readFileSync } from "node:fs";import test from "node:test";-import type {- MessageRecord,- ThreadRecord,-} from "../src/features/chat/types.ts";import {+ installLocalStorageFake,+ registerBundlerResolver,+} from "./helpers/kit.ts";++// The runner cannot resolve the providers store / encryptor specifiers alone.+registerBundlerResolver();+const { store } = installLocalStorageFake();+Object.assign((globalThis.window as { location: object }).location, {+ href: "http://localhost/",+});++const {+ answeringCheckpoint,+ buildExternalRoutingFields,+ buildTitleRequest,fallbackTitleFromUserText,isLegacyClippedTitle,+ normalizeTitle,planLegacyTitleRepairs,+ resolveExternalRouting,selectLegacyRepairPage,threadsAwaitingImport,threadsMissingMessages,-} from "../src/features/chat/utils/chat-title.ts";+ titleCheckpoint,+ titleFromStream,+} = await import("../src/features/chat/utils/chat-title.ts");+const { useExternalProvidersStore } = await import(+ "../src/features/chat/stores/external-providers-store.ts"+);+type MessageRecord = import("../src/features/chat/types.ts").MessageRecord;+type ThreadRecord = import("../src/features/chat/types.ts").ThreadRecord;+type Chunk = import("../src/features/chat/types/api.ts").OpenAIChatChunk;const LONG ="Can you plot a Mandelbrot set and explain how the escape time algorithm works";@@ -296,3 +321,447 @@ test("an emptied chat is decided, one still importing is not", () => {"importing",]);});+++function stageConnection(overrides: Record<string, unknown> = {}): void {+ const base = {+ id: "conn-1",+ providerType: "llama_cpp",+ name: "c",+ baseUrl: "http://127.0.0.1:8080/v1",+ models: ["m"],+ createdAt: 1,+ updatedAt: 1,+ };+ store.set(+ "unsloth_chat_external_providers",+ JSON.stringify([{ ...base, ...overrides }]),+ );+}++function deltas(text: string, finishReason: string | null = "stop"): Chunk[] {+ const parts = [...text].map((ch) => ({+ choices: [{ delta: { content: ch } }],+ }));+ return [...parts, { choices: [{ delta: {}, finish_reason: finishReason }] }];+}++async function* iterate(chunks: Chunk[]): AsyncGenerator<Chunk> {+ for (const chunk of chunks) {+ yield chunk;+ }+}+const title = (chunks: Chunk[]) => titleFromStream(iterate(chunks));++// A throwaway 1024-bit RSA public key; only the encrypt path is under test.+const PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY-----+MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1n8QOqkDXkFEOC62kiqZcBCN3+l/DmD+0BGvjg8h1fFJD2Fla1ibcnmKb9Vok+PmR6jm1JX0yu8JHXPw1om01RwQWe+nehl2VzGfdEdNaRoKhW5oVsnnfmxlWJ/qWuV2rDK8DK/6UK9sC/duMkRWaRGdhyl+l+8/fuJc9JDRVzx7HwIDAQAB+-----END PUBLIC KEY-----`;++test.beforeEach(() => {+ store.clear();+ useExternalProvidersStore.getState().setConnectionsEnabled(true);+});++test("a title on a saved connection is routed to it, and streamed", async () => {+ stageConnection();+ const request = await buildTitleRequest(+ "external::conn-1::qwen3-30b",+ "User: hi",+ );++ assert.deepEqual(+ [+ request?.model,+ request?.provider_id,+ request?.provider_type,+ request?.external_model,+ request?.provider_base_url,+ request?.provider_api_type,+ request?.stream,+ ],+ [+ "external::conn-1::qwen3-30b",+ "conn-1",+ "llama_cpp",+ "qwen3-30b",+ "http://127.0.0.1:8080/v1",+ "chat_completions",+ true,+ ],+ );+ stageConnection({ apiType: "responses" });+ const responses = await buildTitleRequest("external::conn-1::m", "x");+ assert.equal(responses?.provider_api_type, "responses");+ stageConnection({ baseUrl: "", hasApiKey: true });+ store.set(+ "unsloth_chat_external_provider_keys",+ JSON.stringify({ "conn-1": "sk-stale" }),+ );+ const saved = await buildTitleRequest("external::conn-1::m", "x");+ assert.deepEqual(+ [saved?.provider_base_url, saved && "encrypted_api_key" in saved],+ [null, false],+ );+ globalThis.fetch = (async () => ({+ ok: true,+ json: async () => ({ public_key: PUBLIC_KEY_PEM }),+ })) as unknown as typeof fetch;+ stageConnection({ providerType: "openai", hasApiKey: false });+ store.set(+ "unsloth_chat_external_provider_keys",+ JSON.stringify({ "conn-1": "sk-browser-held" }),+ );+ const held = await buildTitleRequest("external::conn-1::gpt-5.4", "x");+ assert.notEqual(held?.encrypted_api_key, "sk-browser-held");+ // Envelope: version.wrappedKey.nonce.ciphertext; the RSA-1024 wrap is 128 bytes.+ assert.equal(atob((held?.encrypted_api_key ?? "").split(".")[1] ?? "").length, 128);+ const local = await buildTitleRequest(+ "unsloth/gemma-4-E2B-it-GGUF",+ "User: hi",+ );+ assert.deepEqual(+ [local?.model, local?.stream, local && "provider_id" in local],+ ["unsloth/gemma-4-E2B-it-GGUF", true, false],+ );+ assert.deepEqual(+ local?.messages?.map((m) => m.role),+ ["system", "user"],+ );+ assert.equal(local?.messages?.[1]?.content, "User: hi");+ assert.deepEqual(+ [+ local?.enable_tools,+ local?.enable_thinking,+ local?.reasoning_effort,+ local?.max_tokens,+ ],+ [false, false, "none", 24],+ );+ assert.match(+ String(local?.messages?.[0]?.content),+ /2-6 words, no quotes, no punctuation, ASCII only, do not echo input/,+ );+});++test("a connection that cannot serve the title yields no request at all", async () => {+ stageConnection({ providerType: "openai", hasApiKey: false });+ assert.equal(await buildTitleRequest("external::conn-1::gpt-5.4", "x"), null);+ assert.equal(await buildTitleRequest("external::gone::m", "x"), null);+ stageConnection({ providerType: "openai", hasApiKey: false });+ store.set(+ "unsloth_chat_external_provider_keys",+ JSON.stringify({ "conn-1": " " }),+ );+ assert.equal(await buildTitleRequest("external::conn-1::gpt-5.4", "x"), null);+ store.set("unsloth_chat_external_provider_keys", JSON.stringify({}));+ stageConnection({+ providerType: "openai",+ hasApiKey: false,+ authKind: "chatgpt_oauth",+ });+ assert.ok(await buildTitleRequest("external::conn-1::gpt-5.4", "x"));+ stageConnection({+ providerType: "gemini",+ hasApiKey: false,+ baseUrl: "https://gw.example/v1",+ });+ assert.ok(await buildTitleRequest("external::conn-1::gemini-2.5-pro", "x"));++ stageConnection();+ useExternalProvidersStore.getState().setConnectionsEnabled(false);+ assert.equal(+ await buildTitleRequest("external::conn-1::qwen3-30b", "x"),+ null,+ );+ assert.ok(await buildTitleRequest("unsloth/gemma-4-E2B-it-GGUF", "x"));+});++test("reasoning is asked off only where the connection's model allows it", async () => {+ async function effortFor(providerType: string, model: string) {+ stageConnection({ providerType, hasApiKey: true, models: [model] });+ return (await buildTitleRequest(`external::conn-1::${model}`, "x"))+ ?.reasoning_effort;+ }++ for (const [providerType, model, expected] of [+ ["openai", "gpt-5", "minimal"],+ ["openai", "o3", "low"],+ ["openai_codex", "gpt-5", "minimal"],+ // No reasoning at all: either field would reach OpenAI as reasoning.effort, a 400.+ ["openai", "gpt-4o", undefined],+ ["openai", "gpt-5-chat-latest", undefined],+ ["gemini", "gemini-2.5-pro", "none"],+ ["llama_cpp", "qwen3-30b", "none"],+ // Its own levels would floor "none" at "medium" if clamped like OpenAI.+ ["mistral", "magistral-medium-latest", "none"],+ ] as const) {+ assert.equal(+ await effortFor(providerType, model),+ expected,+ `${providerType}/${model}`,+ );+ }+});++test("what answered is read from the reply, whichever kind of turn it was", () => {+ const external = {+ providerId: "conn-a",+ providerType: "openai",+ externalModel: "gpt-5.5",+ };+ const research = (+ inferenceRequest: Record<string, unknown>,+ status = "completed",+ ) =>+ answeringCheckpoint({+ researchRun: { status, config: { inferenceRequest } },+ });+ const withRun = (modelId: string, status = "completed") =>+ answeringCheckpoint({+ responseDetails: { modelId },+ researchRun: { status, config: { inferenceRequest: external } },+ });++ assert.equal(+ answeringCheckpoint({+ responseDetails: { modelId: "external::conn-a::m" },+ }),+ "external::conn-a::m",+ );+ assert.equal(+ research({ model: "gpt-5.5", ...external }),+ "external::conn-a::gpt-5.5",+ );+ assert.equal(+ research({ model: "unsloth/gemma-4-E2B-it-GGUF" }),+ "unsloth/gemma-4-E2B-it-GGUF",+ );+ assert.equal(research({ model: "external::conn-a::m" }), "");+ for (const missing of [+ "providerId",+ "providerType",+ "externalModel",+ ] as const) {+ const partial: Record<string, unknown> = { model: "m", ...external };+ delete partial[missing];+ assert.equal(research(partial), "m", `missing ${missing}`);+ assert.equal(+ research({ ...partial, model: "external::conn-a::m" }),+ "",+ `missing ${missing}, external id`,+ );+ assert.equal(+ research({ model: "m", ...external, [missing]: "" }),+ "m",+ `blank ${missing}`,+ );+ }+ for (const status of ["running", "cancelled", "failed"]) {+ assert.equal(research({ model: "m", ...external }, status), "", status);+ }+ assert.equal(+ withRun("unsloth/gemma-4-E2B-it-GGUF"),+ "unsloth/gemma-4-E2B-it-GGUF",+ );+ assert.equal(withRun("external::conn-b::m"), "external::conn-b::m");+ assert.equal(withRun("external::conn-b::m", "failed"), "external::conn-b::m");+ assert.equal(withRun(""), "external::conn-a::gpt-5.5");+ assert.deepEqual(+ [+ answeringCheckpoint(undefined),+ answeringCheckpoint({ responseDetails: { modelId: 7 } }),+ ],+ ["", ""],+ );+});++test("a title is asked of the connection that answered, or of no connection", () => {+ const A = "external::conn-a::m";+ const B = "external::conn-b::m";+ const local = "unsloth/gemma-4-E2B-it-GGUF";+ for (const [answered, active, expected] of [+ [A, B, A],+ [A, local, A],+ [A, "", A],+ ["", B, ""],+ [local, B, ""],+ ["", local, local],+ [local, local, local],+ [local, "", ""],+ ["", "", ""],+ ] as const) {+ assert.equal(+ titleCheckpoint(answered, active),+ expected,+ `${answered || "-"} + ${active || "-"}`,+ );+ }+});++test("the title is assembled from the deltas, unless it was cut short or reasoned", async () => {+ const part = (text: string): Chunk => ({+ choices: [{ delta: { content: [{ type: "text", text }] } }],+ });++ assert.equal(+ await title(deltas('"Matrix Inversion" — Steps')),+ "Matrix Inversion Steps",+ );+ assert.equal(+ await title([part("Matrix Inversion"), part(" Steps"), ...deltas("")]),+ "Matrix Inversion Steps",+ );++ const cut = deltas("Inverting A Three By Three Matri", "length");+ assert.equal(await title(cut), null);+ assert.equal(+ await title([...cut, { choices: [] }, { choices: [{ delta: {} }] }]),+ null,+ );+ assert.equal(+ await title([+ {+ choices: [+ { delta: { content: [{ type: "thinking", thinking: "hmm" }] } },+ ],+ },+ ...deltas(""),+ ]),+ null,+ );+ // A closed reasoning summary (a model that cannot turn reasoning off) is dropped, the title kept.+ assert.equal(+ await title(deltas("<think>The user asks about gardens.</think>Spring Garden Plan")),+ "Spring Garden Plan",+ );+ for (const raw of [+ "<think>hmm</think>",+ "<THINK>hmm Ok",+ "hmm</ThInK> Ok",+ "",+ ]) {+ assert.equal(await title(deltas(raw)), null, raw);+ }+});++test("the answer is reduced to a short, plain, ASCII line", () => {+ for (const [raw, expected] of [+ [' "Matrix Inversion" — Steps ', "Matrix Inversion Steps"],+ [+ "Title: How To Invert A Three By Three Matrix Quickly",+ "How To Invert A Three By",+ ],+ ["Matrix Steps\nExplanation follows", "Matrix Steps"],+ ["`Matrix` 'Inversion' Steps", "Matrix Inversion Steps"],+ ["A.! B? C; D, E: F", "A B C D E F"],+ [" Title: Matrix Steps", "Matrix Steps"],+ ["Assistant: what is this", null],+ [" User: what is this", null],+ [`${"a".repeat(59)} b`, "a".repeat(59)],+ ["Base: what is this", null],+ ["Café — Résumé Matrix", "Caf R sum Matrix"],+ ["x".repeat(80), "x".repeat(60)],+ ["Inverting Title: A Matrix", "Inverting Title A Matrix"],+ ["Title : Matrix Steps", "Matrix Steps"],+ ["Asking User: what is", "Asking User what is"],+ [" ", null],+ ["User: what is this", null],+ ["LoRA: what is this", null],+ ] as const) {+ assert.equal(normalizeTitle(raw), expected, JSON.stringify(raw));+ }+});++test("a forced refresh re-fetches the public key instead of reusing the cached one", async () => {+ let fetched = 0;+ globalThis.fetch = (async () => {+ fetched += 1;+ return { ok: true, json: async () => ({ public_key: PUBLIC_KEY_PEM }) };+ }) as unknown as typeof fetch;+ stageConnection({ providerType: "openai", hasApiKey: false });+ store.set(+ "unsloth_chat_external_provider_keys",+ JSON.stringify({ "conn-1": "sk-browser-held" }),+ );+ const target = resolveExternalRouting("external::conn-1::gpt-5.4");+ if (target.kind !== "external") throw new Error("expected external");++ await buildExternalRoutingFields(target);+ const cached = fetched;+ await buildExternalRoutingFields(target);+ assert.equal(fetched, cached, "an ordinary build reuses the cached key");+ await buildExternalRoutingFields(target, { forceRefreshPublicKey: true });+ assert.equal(fetched, cached + 1, "a forced one refetches");+});++// Pinned as source text (JSX); gaps exclude braces so they cannot match past the block.+test("the title is wired to what answered, and built inside the fallback boundary", () => {+ const provider = readFileSync(+ new URL("../src/features/chat/runtime-provider.tsx", import.meta.url),+ "utf8",+ ).replace(/\s+/g, " ");+ for (const rule of [+ /const answeredWith = answeringCheckpoint\( firstAssistant\?\.metadata\?\.custom, \);/,+ /if \(!payload\.checkpoint\) return null;/,+ /checkpoint: titleCheckpoint\( answeredWith, useChatRuntimeStore\.getState\(\)\.params\.checkpoint, \),/,+ /try \{ [^{}]*?const request = await buildTitleRequest\( payload\.checkpoint, parts\.join\("\\n"\), \); if \(!request\) return null; [^{}]*?return await titleFromStream\([^{}]*?\); \} catch \{ [^{}]*?return null; \}/,+ ]) {+ assert.match(provider, rule);+ }+});++// The backend forwards these verbatim, so a strict chat-completions endpoint 400s on+// any the provider does not take, and the title is lost.+test("title sampling fields follow the provider's capabilities", async () => {+ const fields = (r: Awaited<ReturnType<typeof buildTitleRequest>>) =>+ ["temperature", "top_p", "top_k", "repetition_penalty"].filter(+ (k) => r !== null && k in r,+ );+ const local = await buildTitleRequest("unsloth/gemma-4-E2B-it-GGUF", "x");+ assert.deepEqual(fields(local), [+ "temperature",+ "top_p",+ "top_k",+ "repetition_penalty",+ ]);+ stageConnection({ providerType: "mistral", hasApiKey: true });+ const mistral = await buildTitleRequest("external::conn-1::mistral-small", "x");+ assert.deepEqual(fields(mistral), ["temperature", "top_p"]);+ stageConnection({ providerType: "openai", hasApiKey: true });+ const openai = await buildTitleRequest("external::conn-1::gpt-5.4", "x");+ assert.deepEqual(fields(openai), []);+});++test("a Responses custom connection still asks reasoning off", async () => {+ for (const [model, expected] of [+ ["gpt-5.4", "none"],+ ["gpt-5", "minimal"],+ ] as const) {+ stageConnection({ providerType: "custom", apiType: "responses", models: [model] });+ const request = await buildTitleRequest(`external::conn-1::${model}`, "x");+ assert.equal(request?.reasoning_effort, expected, model);+ }+});++test("the title budget is floored at the provider's minimum output", async () => {+ const local = await buildTitleRequest("unsloth/gemma-4-E2B-it-GGUF", "x");+ assert.equal(local?.max_tokens, 24);+ stageConnection({ providerType: "kimi", hasApiKey: true });+ const kimi = await buildTitleRequest("external::conn-1::kimi-k2-thinking", "x");+ assert.equal(kimi?.max_tokens, 16000);+ // Thinking that cannot be turned off counts toward the cap, so it gets headroom.+ for (const [providerType, model, expected] of [+ ["gemini", "gemini-2.5-pro", 1024],+ ["gemini", "gemini-2.5-flash", 64],+ ["openai", "o3", 1024],+ ["openai", "gpt-5.4", 64],+ ] as const) {+ stageConnection({ providerType, baseUrl: "", hasApiKey: true, models: [model] });+ const request = await buildTitleRequest(`external::conn-1::${model}`, "x");+ assert.equal(request?.max_tokens, expected, model);+ }+});diff --git a/studio/frontend/tests/pasted-text-attachment.test.ts b/studio/frontend/tests/pasted-text-attachment.test.tsindex f5da4d5da2c..bd94dadfe94 100644--- a/studio/frontend/tests/pasted-text-attachment.test.ts+++ b/studio/frontend/tests/pasted-text-attachment.test.ts@@ -4,7 +4,20 @@import assert from "node:assert/strict";import test from "node:test";-import { fallbackTitleFromUserText } from "../src/features/chat/utils/chat-title.ts";+import {+ installLocalStorageFake,+ registerBundlerResolver,+} from "./helpers/kit.ts";++// chat-title.ts reaches the providers store, so it must load through the resolver.+registerBundlerResolver();+installLocalStorageFake();+Object.assign((globalThis.window as { location: object }).location, {+ href: "http://localhost/",+});+const { fallbackTitleFromUserText } = await import(+ "../src/features/chat/utils/chat-title.ts"+);import {PASTED_TEXT_DEFAULT_MIN_CHARS,PASTED_TEXT_PREVIEW_MAX_CHARS,diff --git a/tests/studio/test_chat_title_generation.py b/tests/studio/test_chat_title_generation.pyindex b72635f7af0..99ad99b3160 100644--- a/tests/studio/test_chat_title_generation.py+++ b/tests/studio/test_chat_title_generation.py@@ -9,7 +9,9 @@ from pathlib import PathREPO = Path(__file__).resolve().parents[2]+# Pinned as source text: the frontend runner cannot import JSX.RUNTIME_TSX = REPO / "studio/frontend/src/features/chat/runtime-provider.tsx"+CHAT_ADAPTER_TS = REPO / "studio/frontend/src/features/chat/api/chat-adapter.ts"def _source_until(src: str, anchor: str, end_anchor: str) -> str:@@ -39,19 +41,6 @@ def _balanced_block(src: str, anchor: str) -> str:raise AssertionError(f"unbalanced block after {anchor!r}")-def test_title_model_prompt_targets_conversation_topic():- block = _source_until(- RUNTIME_TSX.read_text(encoding = "utf-8"),- "async function generateTitleWithModel",- "\nconst inflightTitleByKey",- )-- assert "conversation topic" in block- assert "not the user's exact wording" in block- assert "Use the assistant reply as context when provided" in block- assert "Rules: 2-6 words" in block--def test_title_model_payload_includes_optional_assistant_reply():block = _source_until(RUNTIME_TSX.read_text(encoding = "utf-8"),@@ -65,8 +54,6 @@ def test_title_model_payload_includes_optional_assistant_reply():assert "if (assistant)" in blockassert "parts.push(`Assistant: ${assistant}`);" in blockassert 'parts.join("\\n")' in block- assert "enable_thinking: false" in block- assert 'reasoning_effort: "none"' in blockdef test_generate_title_passes_first_assistant_reply_after_first_user():@@ -78,9 +65,6 @@ def test_generate_title_passes_first_assistant_reply_after_first_user():assert 'const firstUserIndex = messages.findIndex((m) => m.role === "user");' in blockassert 'm.role === "assistant" && i > firstUserIndex' in blockassert "const assistantText = extractTextParts(firstAssistant);" in block- assert "generateTitleWithModel({" in block- assert "userText," in block- assert "assistantText," in blockdef test_tool_call_only_first_assistant_still_uses_first_user_message():@@ -101,8 +85,9 @@ def test_tool_call_only_first_assistant_still_uses_first_user_message():title_block = " ".join(_balanced_block(source, "function titleTextOf").split())assert "const text = extractTextParts(m);" in title_blockassert (- "(await generateTitleWithModel({ userText, assistantText, })) || fallbackTitleFromUserText(userText);"- in generate_block+ "(await generateTitleWithModel({ checkpoint: titleCheckpoint( answeredWith, "+ "useChatRuntimeStore.getState().params.checkpoint, ), userText, assistantText, })) "+ "|| fallbackTitleFromUserText(userText);" in generate_block)@@ -117,31 +102,27 @@ def test_auto_title_disabled_uses_deterministic_user_text_fallback():assert "generateTitleWithModel" not in auto_title_off-def test_model_failure_still_falls_back_to_user_text():- source = RUNTIME_TSX.read_text(encoding = "utf-8")- model_block = _source_until(- source,- "async function generateTitleWithModel",- "\nconst inflightTitleByKey",- )- generate_block = _balanced_block(source, "async generateTitle(remoteId")-- assert "finish_reason?: string | null;" in source- assert 'if (choice?.finish_reason === "length") return null;' in model_block- assert r"if (!raw || /<\/?think>/i.test(raw)) return null;" in model_block- assert "})) || fallbackTitleFromUserText(userText);" in generate_block+def test_reply_stamps_the_checkpoint_its_request_captured():+ # The stamp must be the request's captured checkpoint, not the live selection.+ source = " ".join(CHAT_ADAPTER_TS.read_text(encoding = "utf-8").split())--def test_title_normalizer_still_enforces_output_constraints():- block = _source_until(- RUNTIME_TSX.read_text(encoding = "utf-8"),- "async function generateTitleWithModel",- "\nconst inflightTitleByKey",+ assert source.count("modelId: params.checkpoint,") == 2+ assert "modelId: useChatRuntimeStore.getState().params" not in source+ assert "const externalRouting = resolveExternalRouting(params.checkpoint);" in source+ assert (+ "...(await buildExternalRoutingFields( { provider: externalProvider, "+ "modelId: externalModelId, apiKey: externalApiKey, }, "+ "{ forceRefreshPublicKey }, ))" in source+ )+ assert (+ 'const externalProvider = externalRouting.kind === "external" '+ "? externalRouting.provider : null;" in source+ )+ assert (+ 'const externalApiKey = externalRouting.kind === "external" '+ '? externalRouting.apiKey : "";' in source+ )+ assert (+ 'const externalModelId = externalRouting.kind === "external" '+ '? externalRouting.modelId : "";' in source)-- assert r'replace(/[^\x20-\x7E]+/g, " ")' in block- assert 'replace(/["\'`]+/g, "")' in block- assert 'replace(/[.!?:;,]+/g, " ")' in block- assert 'title.split(" ").filter(Boolean).slice(0, 6)' in block- assert "joined.length > 60" in block- assert "return normalizeTitle(raw);" in block