Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

1.7 KB
Raw Blame
// Pure parse of a pasted custom-provider JSON config → the add-provider payload.
// Accepts common field aliases (base_url/endpoint, api_key/key, slug/name) and
// models as array or delimited string. Throws on bad JSON or missing essentials;
// the caller surfaces the message. No React — importable + testable in isolation.
// Ported from models-rahmanef-com; the /api/config route re-validates server-side.
export type CustomConfig = { name: string; baseURL: string; apiKey: string; protocol?: string; models?: string[] };

export const parseModelList = (s: string): string[] => s.split(/[\n,]/).map((m) => m.trim()).filter(Boolean);

/** Toggle one discovered id in a delimited model list, keeping the text field the
 * single source of truth. Returns the new list string (comma-space joined). */
export function toggleModelInList(current: string, id: string): string {
  const target = id.trim();
  if (!target) return current;
  const ids = parseModelList(current);
  const next = ids.includes(target) ? ids.filter((m) => m !== target) : [...ids, target];
  return next.join(", ");
}

export function parseCustomProviderConfig(json: string): CustomConfig {
  const o = JSON.parse(json);
  const name = o.name ?? o.slug,
    baseURL = o.baseURL ?? o.base_url ?? o.endpoint,
    apiKey = o.apiKey ?? o.api_key ?? o.key;
  if (!name || !baseURL || !apiKey) throw new Error("JSON needs name, baseURL and apiKey.");
  const models = Array.isArray(o.models)
    ? o.models.map((m: unknown) => String(m))
    : typeof o.models === "string"
      ? parseModelList(o.models)
      : undefined;
  return { name: String(name), baseURL: String(baseURL), apiKey: String(apiKey), protocol: o.protocol ? String(o.protocol) : undefined, models };
}