Skip to content

The agent that grows with you

10.5 KB
Raw Blame
/**
 * Provider + model dropdowns backed by the gateway's `model.options`
 * inventory, plus the bounded fetch that keeps a wedged bot socket from
 * spinning the picker forever.
 *
 * Shared by the advanced profile editor and the create dialog.
 */

import {
  Button,
  catalogProviderMatches,
  GlyphSpinner,
  Input,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
  useI18n,
  useQuery
} from '@hermes/plugin-sdk'
import { useState } from 'react'

import { labeled } from './dialog-parts'
import { useBots } from './i18n'
import { botRouteKey, requestForBot, resolveBotConnectionRoute } from './routing'
import { ID } from './shared'
import type { RosterRow } from './types'

// ── model picker (provider/model dropdowns via model.options) ───────────────

// #95279: the picker's catalog read rides the BOT's own socket — a lazily
// dialed second backend that can wedge (cold pool spawn, dropped remote hop)
// without the primary socket ever noticing. An unbounded RPC there left the
// query pending forever and the picker spinning ("never settles"). Bound every
// attempt: past the budget the query rejects and ModelPicker falls back to its
// free-text inputs instead of an eternal GlyphSpinner.
const MODEL_OPTIONS_SETTLE_MS = 20000

function boundedModelOptionsFetch<T>(fetch: Promise<T>, settleMs = MODEL_OPTIONS_SETTLE_MS): Promise<T> {
  // window.setTimeout (not bare setTimeout): bare vm test harnesses expose
  // timers only through the window shim. With no scheduler at all, degrade to
  // the old unbounded behavior rather than not fetching.
  const scope = typeof window === 'undefined' ? null : window

  if (!scope || typeof scope.setTimeout !== 'function') {
    return fetch
  }

  // `any`: the timer id is assigned synchronously by the executor below, but
  // it is still `null` on the declaration TypeScript sees from the closure,
  // and clearTimeout's signature takes `number | undefined`.
  let timerId: any = null

  const deadline = new Promise<never>((_, reject) => {
    timerId = scope.setTimeout(() => {
      reject(new Error(`model.options did not answer within ${Math.round(settleMs / 1000)}s (#95279 settle guard)`))
    }, settleMs)
  })

  return Promise.race([fetch, deadline]).finally(() => scope.clearTimeout(timerId))
}

/** One provider row of the gateway's `model.options` inventory. Entries in
 *  `models` are bare slugs on current gateways and objects on older ones. */
interface ModelProviderOption {
  aliases?: null | string[]
  models?: Array<string | { id?: string; name?: string }>
  name?: string
  slug: string
}
interface ModelOptionsResult {
  providers?: ModelProviderOption[]
}

function useModelOptions(bot: null | RosterRow = null) {
  // Hook body runs during render: an orphaned row must paint the picker
  // disabled/erroring, not throw into the pane's error boundary.
  const resolved = bot ? resolveBotConnectionRoute(bot) : null
  const route = resolved?.status === 'resolved' ? resolved.route : null
  const orphaned = resolved?.status === 'owner_removed'

  return useQuery<ModelOptionsResult>({
    queryKey: [ID, 'model-options', route ? botRouteKey(route) : 'active'],
    // No forced `refresh`: forcing a network read on EVERY mount bypassed the
    // staleTime cache, so each Bots view remount (tab re-front, dialog reopen,
    // pane visibility flip) knocked the picker back into its loading state and
    // discarded the user's staged selection mid-edit (#95279). The cached read
    // still refreshes per staleTime like every other surface's catalog.
    queryFn: () =>
      boundedModelOptionsFetch(
        requestForBot(bot, 'model.options', {
          include_unconfigured: true,
          explicit_only: false
        }) as Promise<ModelOptionsResult>
      ),
    enabled: !orphaned,
    staleTime: 120000,
    retry: false
  })
}

/**
 * Provider + model dropdowns from the gateway's configured inventory — the
 * same data the core model picker shows. `value = {provider, model}`;
 * onChange receives the merged patch.
 */
/** The two fields a profile pins for its model. */
interface ModelSelection {
  model: string
  provider: string
}
/** ModelPicker only ever emits the field(s) it just changed, so consumers can
 *  test membership with `in` and leave the rest of their state untouched. */
type ModelSelectionPatch = { model: string } | { model: string; provider: string } | { provider: string }
interface ModelPickerProps {
  bot?: null | RosterRow
  onChange: (patch: ModelSelectionPatch) => void
  placeholderModel?: string
  value: ModelSelection
}

export function ModelPicker({ bot = null, value, onChange, placeholderModel }: ModelPickerProps) {
  const b = useBots()
  const { t } = useI18n()
  const { data, isLoading, error } = useModelOptions(bot)

  // Hooks are ALWAYS declared up front, before any conditional return.
  // Declaring them after a return trips React error #310.
  const NONE = '__default__'
  const CUSTOM = '__custom__'
  const providers = (data?.providers || []).filter(p => p && p.slug)

  const isKnown =
    !value.provider || value.provider === NONE || providers.some(p => catalogProviderMatches(p, value.provider))

  // The manual-entry latch is the USER's choice only. Seeding it from
  // `isKnown` froze whatever the catalog state was at first paint: on the
  // first open the async read had not resolved yet, so a configured provider
  // read as unknown and the picker latched into free text — the dropdown
  // only appeared on the second open, from the cached catalog. `null` means
  // "no user choice yet": derive from the LIVE catalog, so a provider the
  // loaded inventory knows flips to the dropdowns when data arrives, while
  // one it does not know still gets the free-text form.
  const [manualEntry, setManualEntry] = useState<boolean | null>(null)
  const useFreeText = manualEntry ?? !isKnown

  if (isLoading) {
    return (
      <div className="flex justify-center py-2">
        <GlyphSpinner className="text-(--ui-text-tertiary)" spinner="breathe" />
      </div>
    )
  }

  if (error || !providers.length) {
    // Fallback: free text (older gateway or empty inventory).
    return (
      <div className="grid grid-cols-2 gap-2.5">
        {labeled(
          t.settings.model.provider,
          <Input
            onChange={event =>
              onChange({
                provider: event.target.value
              })
            }
            placeholder="omnirouter / 9router / nous …"
            value={value.provider}
          />
        )}
        {labeled(
          t.settings.model.model,
          <Input
            onChange={event =>
              onChange({
                model: event.target.value
              })
            }
            placeholder="antigravity/gemini-3.6-flash-high"
            value={value.model}
          />
        )}
      </div>
    )
  }

  if (useFreeText) {
    return (
      <div className="flex flex-col gap-2">
        <div className="grid grid-cols-2 gap-2.5">
          {labeled(
            b.editor.providerCustom,
            <Input
              onChange={event =>
                onChange({
                  provider: event.target.value
                })
              }
              placeholder="omnirouter / inferx / 9router"
              value={value.provider}
            />
          )}
          {labeled(
            b.editor.modelCustom,
            <Input
              onChange={event =>
                onChange({
                  model: event.target.value
                })
              }
              placeholder="antigravity/gemini-3.6-flash-high"
              value={value.model}
            />
          )}
        </div>
        <Button
          className="h-6 self-start text-xs text-(--ui-text-tertiary)"
          onClick={() => setManualEntry(false)}
          size="sm"
          variant="ghost"
        >
          {b.editor.backToDropdowns}
        </Button>
      </div>
    )
  }

  const activeProvider = providers.find(p => catalogProviderMatches(p, value.provider)) || null

  const models = activeProvider
    ? (activeProvider.models || []).map(m => (typeof m === 'string' ? m : m.id || m.name || ''))
    : []

  return (
    <div className="grid grid-cols-[1fr_1.4fr] gap-2.5">
      {labeled(
        t.settings.model.provider,
        <Select
          onValueChange={v => {
            if (v === NONE) {
              onChange({
                provider: '',
                model: ''
              })
            } else if (v === CUSTOM) {
              setManualEntry(true)
            } else {
              const prov = providers.find(p => p.slug === v)
              const provModels = (prov?.models || []).map(m => (typeof m === 'string' ? m : m.id || m.name || ''))
              const first = provModels[0] || ''
              onChange({
                provider: v,
                model: prov && provModels.includes(value.model) ? value.model : first
              })
            }
          }}
          value={activeProvider?.slug || value.provider || NONE}
        >
          <SelectTrigger className="h-8 rounded-md">
            <SelectValue />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value={NONE}>{b.editor.inheritLaunch}</SelectItem>
            {providers.map(p => (
              <SelectItem key={p.slug} value={p.slug}>
                {p.name ? `${p.name} (${p.slug})` : p.slug}
              </SelectItem>
            ))}
            <SelectItem value={CUSTOM}>{b.editor.enterManually}</SelectItem>
          </SelectContent>
        </Select>
      )}
      {labeled(
        t.settings.model.model,
        activeProvider && models.length > 0 ? (
          <Select
            onValueChange={v =>
              onChange({
                model: v
              })
            }
            value={value.model || (models[0] ?? '')}
          >
            <SelectTrigger className="h-8 rounded-md">
              <SelectValue />
            </SelectTrigger>
            <SelectContent>
              {models.map(m => (
                <SelectItem key={m} value={m}>
                  {m}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        ) : (
          <Input
            onChange={event =>
              onChange({
                model: event.target.value
              })
            }
            placeholder={
              placeholderModel === undefined ? b.editor.gatewayDefault : placeholderModel || b.editor.modelNameExample
            }
            value={value.model}
          />
        )
      )}
    </div>
  )
}