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.

2.3 KB
Raw Blame
// resolveModel — turn a `provider/model` ref + a tenant into a ready-to-call descriptor.
// Pure & offline: no network. (An {info:true} option once attached models.dev metadata;
// no call site ever passed it, so `info` was permanently null. Deleted 2026-08-10.)
//
// SECURITY (host-gate, hermes' key-selection fix): the key is fetched by PROVIDER slug and
// paired only with THAT provider's baseUrl. Provider A's key can never be sent to provider B.

import { PROVIDERS } from './registry.js'

/**
 * @typedef {object} ResolvedModel
 * @property {string} ref
 * @property {string} provider
 * @property {string} model
 * @property {string} baseUrl
 * @property {string} apiKey
 * @property {'openai'|'anthropic'} protocol
 */

/** Split on the FIRST '/', so model ids may contain '/' (OpenRouter style). */
export function parseRef(ref) {
  const i = ref.indexOf('/')
  if (i < 1 || i === ref.length - 1) throw new Error(`bad model ref "${ref}", expected "provider/model"`)
  return { provider: ref.slice(0, i), model: ref.slice(i + 1) }
}

/**
 * @param {string} ref "provider/model"
 * @param {{tenantId?:string, store:import('./store.js').CredentialStore, baseUrl?:string, protocol?:'openai'|'anthropic'}} opts
 * @returns {Promise<ResolvedModel>}
 */
export async function resolveModel(ref, { tenantId, store, baseUrl, protocol } = {}) {
  const { provider, model } = parseRef(ref)
  const conn = PROVIDERS[provider]
  if (!conn && !baseUrl) throw new Error(`unknown provider "${provider}" — add it to registry.js or pass opts.baseUrl`)
  if (!store) throw new Error('resolveModel needs a CredentialStore (opts.store)')

  const apiKey = await store.getKey(tenantId, provider)
  if (!apiKey) throw new Error(`no API key for provider="${provider}" tenant="${tenantId ?? 'default'}" — user must BYOK`)

  return {
    ref, provider, model,
    // host-gate: known providers are PINNED to their registry endpoint so a caller-supplied
    // baseUrl can never redirect a provider's key to another host. Override only honored for
    // unknown providers (conn undefined), which require baseUrl to resolve at all.
    baseUrl: conn ? conn.baseUrl : baseUrl,
    apiKey,
    // known providers keep their registry protocol; a custom (conn-less) provider
    // uses the caller's declared protocol, defaulting to openai-compatible.
    protocol: conn?.protocol || protocol || 'openai',
  }
}