refactor(model-core): host snapshot fetcher, suggestion parser, and context-limit resolver
Move three pure helpers from src/shared/ into @oh-my-opencode/model-core so the package can stand alone without depending on plugin internals:
- buildModelCapabilitiesSnapshotFromModelsDev + fetchModelCapabilitiesSnapshot (models.dev normalization)
- parseModelSuggestion (cross-provider ProviderModelNotFoundError suggestion extraction)
- resolveActualContextLimit (Anthropic GA 1M context override)
Split provider-model-id-transform into two variants exposed by model-core:
- transformModelForProvider keeps the runtime dash to dot Anthropic rewrite used by the SDK
- transformModelForProviderDisplay preserves hyphenated Anthropic IDs so the installer writes registry-compatible model strings, fixing the ProviderModelNotFoundError fresh installs hit when the dotted form leaks into the config
src/shared/* and src/cli/provider-model-id-transform.ts collapse to re-export shims that point at the new core modules. Stale src/shared/{known-variants,model-capability-aliases,model-capability-guardrails,model-capability-heuristics}.ts re-export files plus the duplicated context-limit-resolver test are removed in favor of the canonical model-core copies.
Tests: bun test packages/model-core src/shared/model-capabilities-cache.test.ts src/cli/provider-model-id-transform.test.ts
This commit is contained in:
@@ -1,162 +1,20 @@
|
||||
import * as dataPath from "./data-path"
|
||||
import { createJsonFileCacheStore } from "./json-file-cache-store"
|
||||
import type { ModelCapabilitiesSnapshot, ModelCapabilitiesSnapshotEntry } from "./model-capabilities"
|
||||
import {
|
||||
MODELS_DEV_SOURCE_URL,
|
||||
buildModelCapabilitiesSnapshotFromModelsDev,
|
||||
fetchModelCapabilitiesSnapshot,
|
||||
} from "@oh-my-opencode/model-core"
|
||||
import type { ModelCapabilitiesSnapshot } from "./model-capabilities"
|
||||
|
||||
export {
|
||||
MODELS_DEV_SOURCE_URL,
|
||||
buildModelCapabilitiesSnapshotFromModelsDev,
|
||||
fetchModelCapabilitiesSnapshot,
|
||||
}
|
||||
|
||||
export const MODELS_DEV_SOURCE_URL = "https://models.dev/api.json"
|
||||
const MODEL_CAPABILITIES_CACHE_FILE = "model-capabilities.json"
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function readBoolean(value: unknown): boolean | undefined {
|
||||
return typeof value === "boolean" ? value : undefined
|
||||
}
|
||||
|
||||
function readNumber(value: unknown): number | undefined {
|
||||
return typeof value === "number" ? value : undefined
|
||||
}
|
||||
|
||||
function readString(value: unknown): string | undefined {
|
||||
return typeof value === "string" ? value : undefined
|
||||
}
|
||||
|
||||
function readStringArray(value: unknown): string[] | undefined {
|
||||
if (!Array.isArray(value)) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const result = value.filter((item): item is string => typeof item === "string")
|
||||
return result.length > 0 ? result : undefined
|
||||
}
|
||||
|
||||
function normalizeSnapshotEntry(rawModelID: string, rawModel: unknown): ModelCapabilitiesSnapshotEntry | undefined {
|
||||
if (!isRecord(rawModel)) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const id = readString(rawModel.id) ?? rawModelID
|
||||
const family = readString(rawModel.family)
|
||||
const reasoning = readBoolean(rawModel.reasoning)
|
||||
const temperature = readBoolean(rawModel.temperature)
|
||||
const toolCall = readBoolean(rawModel.tool_call)
|
||||
|
||||
const rawModalities = isRecord(rawModel.modalities) ? rawModel.modalities : undefined
|
||||
const modalitiesInput = readStringArray(rawModalities?.input)
|
||||
const modalitiesOutput = readStringArray(rawModalities?.output)
|
||||
const modalities = modalitiesInput || modalitiesOutput
|
||||
? {
|
||||
...(modalitiesInput ? { input: modalitiesInput } : {}),
|
||||
...(modalitiesOutput ? { output: modalitiesOutput } : {}),
|
||||
}
|
||||
: undefined
|
||||
|
||||
const rawLimit = isRecord(rawModel.limit) ? rawModel.limit : undefined
|
||||
const limitContext = readNumber(rawLimit?.context)
|
||||
const limitInput = readNumber(rawLimit?.input)
|
||||
const limitOutput = readNumber(rawLimit?.output)
|
||||
const limit = limitContext !== undefined || limitInput !== undefined || limitOutput !== undefined
|
||||
? {
|
||||
...(limitContext !== undefined ? { context: limitContext } : {}),
|
||||
...(limitInput !== undefined ? { input: limitInput } : {}),
|
||||
...(limitOutput !== undefined ? { output: limitOutput } : {}),
|
||||
}
|
||||
: undefined
|
||||
|
||||
return {
|
||||
id,
|
||||
...(family ? { family } : {}),
|
||||
...(reasoning !== undefined ? { reasoning } : {}),
|
||||
...(temperature !== undefined ? { temperature } : {}),
|
||||
...(toolCall !== undefined ? { toolCall } : {}),
|
||||
...(modalities ? { modalities } : {}),
|
||||
...(limit ? { limit } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
function mergeSnapshotEntries(
|
||||
existing: ModelCapabilitiesSnapshotEntry | undefined,
|
||||
incoming: ModelCapabilitiesSnapshotEntry,
|
||||
): ModelCapabilitiesSnapshotEntry {
|
||||
if (!existing) {
|
||||
return incoming
|
||||
}
|
||||
|
||||
const mergedModalities = existing.modalities || incoming.modalities
|
||||
? {
|
||||
...existing.modalities,
|
||||
...incoming.modalities,
|
||||
}
|
||||
: undefined
|
||||
const mergedLimit = existing.limit || incoming.limit
|
||||
? {
|
||||
...existing.limit,
|
||||
...incoming.limit,
|
||||
}
|
||||
: undefined
|
||||
|
||||
return {
|
||||
...existing,
|
||||
...incoming,
|
||||
...(mergedModalities ? { modalities: mergedModalities } : {}),
|
||||
...(mergedLimit ? { limit: mergedLimit } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
export function buildModelCapabilitiesSnapshotFromModelsDev(raw: unknown): ModelCapabilitiesSnapshot {
|
||||
const models: Record<string, ModelCapabilitiesSnapshotEntry> = {}
|
||||
const providers = isRecord(raw) ? raw : {}
|
||||
|
||||
for (const providerValue of Object.values(providers)) {
|
||||
if (!isRecord(providerValue)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const providerModels = providerValue.models
|
||||
if (!isRecord(providerModels)) {
|
||||
continue
|
||||
}
|
||||
|
||||
for (const [rawModelID, rawModel] of Object.entries(providerModels)) {
|
||||
const normalizedEntry = normalizeSnapshotEntry(rawModelID, rawModel)
|
||||
if (!normalizedEntry) {
|
||||
continue
|
||||
}
|
||||
|
||||
models[normalizedEntry.id.toLowerCase()] = mergeSnapshotEntries(
|
||||
models[normalizedEntry.id.toLowerCase()],
|
||||
normalizedEntry,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
generatedAt: new Date().toISOString(),
|
||||
sourceUrl: MODELS_DEV_SOURCE_URL,
|
||||
models,
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchModelCapabilitiesSnapshot(args: {
|
||||
sourceUrl?: string
|
||||
fetchImpl?: typeof fetch
|
||||
} = {}): Promise<ModelCapabilitiesSnapshot> {
|
||||
const sourceUrl = args.sourceUrl ?? MODELS_DEV_SOURCE_URL
|
||||
const fetchImpl = args.fetchImpl ?? fetch
|
||||
const response = await fetchImpl(sourceUrl)
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`models.dev fetch failed with ${response.status}`)
|
||||
}
|
||||
|
||||
const raw = await response.json()
|
||||
const snapshot = buildModelCapabilitiesSnapshotFromModelsDev(raw)
|
||||
return {
|
||||
...snapshot,
|
||||
sourceUrl,
|
||||
}
|
||||
}
|
||||
|
||||
export function createModelCapabilitiesCacheStore(
|
||||
getCacheDir: () => string = dataPath.getOmoOpenCodeCacheDir,
|
||||
) {
|
||||
@@ -186,7 +44,7 @@ export function createModelCapabilitiesCacheStore(
|
||||
|
||||
async function refreshModelCapabilitiesCache(args: {
|
||||
sourceUrl?: string
|
||||
fetchImpl?: typeof fetch
|
||||
fetchImpl?: (input: string) => Promise<Response>
|
||||
} = {}): Promise<ModelCapabilitiesSnapshot> {
|
||||
const snapshot = await fetchModelCapabilitiesSnapshot(args)
|
||||
writeModelCapabilitiesCache(snapshot)
|
||||
|
||||
Reference in New Issue
Block a user