Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.7 KB
Raw Blame
import type { CommitMessageModel } from './commit-message-agent-spec'
import { labelFromModelId } from './model-id-label'

// Use OMP’s only machine-readable listing; provider-grouped tables require brittle parsing.
export const OMP_MODEL_LIST_ARGS = ['models', '--json']

/** The outermost JSON value on stdout, or null when none parses. */
function parseJsonObject(stdout: string): unknown {
  const trimmed = stdout.trim()
  try {
    return JSON.parse(trimmed)
  } catch {
    // Update/extension notices can precede the outermost JSON object on stdout.
    const start = trimmed.indexOf('{')
    const end = trimmed.lastIndexOf('}')
    if (start === -1 || end <= start) {
      return null
    }
    try {
      return JSON.parse(trimmed.slice(start, end + 1))
    } catch {
      return null
    }
  }
}

/** OMP's `provider/model` selector, the id its listing and `--model` share. */
export function ompModelSelector(
  provider: string | undefined,
  model: string | undefined
): string | null {
  return provider && model ? `${provider}/${model}` : null
}

/** Parse provider/model selectors accepted by --model and /model; bare ids collide across providers. */
export function parseOmpModelList(stdout: string): CommitMessageModel[] {
  const parsed = parseJsonObject(stdout)
  if (!parsed || typeof parsed !== 'object' || !('models' in parsed)) {
    return []
  }
  const rows: unknown = parsed.models
  if (!Array.isArray(rows)) {
    return []
  }
  const byId = new Map<string, CommitMessageModel>()
  for (const row of rows) {
    const value: unknown = row
    if (!value || typeof value !== 'object' || Array.isArray(value)) {
      continue
    }
    const provider =
      'provider' in value && typeof value.provider === 'string' ? value.provider.trim() : ''
    const bareId = 'id' in value && typeof value.id === 'string' ? value.id.trim() : ''
    const selector =
      'selector' in value && typeof value.selector === 'string' ? value.selector.trim() : ''
    const id = selector || ompModelSelector(provider, bareId)
    if (!id || byId.has(id)) {
      continue
    }
    const name = 'name' in value && typeof value.name === 'string' ? value.name.trim() : ''
    const contextWindow =
      'contextWindow' in value && typeof value.contextWindow === 'number'
        ? value.contextWindow
        : null
    byId.set(id, {
      id,
      label: name || labelFromModelId(id),
      // Provider distinguishes identical model labels across accounts/providers.
      ...(provider ? { description: provider } : {}),
      ...(contextWindow !== null && Number.isFinite(contextWindow) && contextWindow > 0
        ? { contextWindowTokens: contextWindow }
        : {})
    })
  }
  return [...byId.values()]
}