Skip to content

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.

2.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

export type DictationEntryMode = "live" | "recording-file";

function isLoopbackHostname(hostname: string): boolean {
  const normalized = hostname
    .trim()
    .toLowerCase()
    .replace(/^\[|\]$/g, "");
  return (
    normalized === "localhost" ||
    normalized.endsWith(".localhost") ||
    normalized === "127.0.0.1" ||
    normalized === "::1"
  );
}

/** Only insecure remote HTTP (no page mic) falls back; else live path keeps its errors visible. */
export function dictationEntryMode({
  isSecureContext,
  protocol,
  hostname,
}: {
  isSecureContext: boolean;
  protocol: string;
  hostname: string;
}): DictationEntryMode {
  if (isSecureContext || protocol !== "http:" || isLoopbackHostname(hostname)) {
    return "live";
  }
  return "recording-file";
}

export function currentDictationEntryMode(): DictationEntryMode {
  if (typeof window === "undefined") return "live";
  return dictationEntryMode({
    isSecureContext: window.isSecureContext,
    protocol: window.location.protocol,
    hostname: window.location.hostname,
  });
}

export function insecureDictationGuidance(mode: DictationEntryMode): string {
  return mode === "recording-file"
    ? "Press Dictate to choose a recording on this connection, or open this Unsloth server through HTTPS to dictate live."
    : "Open Unsloth at http://127.0.0.1 (localhost) or over HTTPS to dictate.";
}

export type RecordingPickerPlatform = "android" | "ios" | "other";

/** Touch check because iPadOS can identify as Macintosh. */
export function recordingPickerPlatform({
  userAgent,
  platform = "",
  maxTouchPoints = 0,
}: {
  userAgent: string;
  platform?: string;
  maxTouchPoints?: number;
}): RecordingPickerPlatform {
  if (/android/i.test(userAgent)) return "android";
  if (
    /iphone|ipad|ipod/i.test(userAgent) ||
    (/^mac/i.test(platform) && maxTouchPoints > 1)
  ) {
    return "ios";
  }
  return "other";
}

export function currentRecordingPickerPlatform(): RecordingPickerPlatform {
  if (typeof navigator === "undefined") return "other";
  return recordingPickerPlatform({
    userAgent: navigator.userAgent,
    platform: navigator.platform,
    maxTouchPoints: navigator.maxTouchPoints,
  });
}