feat: add custom model names and option descriptors (#9807)
This commit is contained in:
1 parent
dd7bc147f7
commit
5a433244d0
19 files changed
+1423
-150
No files matched your search
@@ -10,6 +10,8 @@ import {
|
||||
getModelSelectionBooleanOptionValue,
|
||||
getModelSelectionStringOptionValue,
|
||||
getProviderOptionDescriptors,
|
||||
readCustomModelEntries,
|
||||
toCustomModelSetting,
|
||||
getProviderOptionBooleanSelectionValue,
|
||||
getProviderOptionStringSelectionValue,
|
||||
} from "./model.ts";
|
||||
@@ -192,3 +194,53 @@ describe("applyClaudePromptEffortPrefix", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readCustomModelEntries", () => {
|
||||
const capabilities: ModelCapabilities = {
|
||||
optionDescriptors: [
|
||||
{
|
||||
id: "effort",
|
||||
label: "Reasoning",
|
||||
type: "select",
|
||||
options: [{ id: "high", label: "High", isDefault: true }],
|
||||
currentValue: "high",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
it("resolves bare slugs and entries, trimming and deduplicating on slug", () => {
|
||||
expect(
|
||||
readCustomModelEntries([
|
||||
" bare ",
|
||||
{ slug: "named", name: " Named ", capabilities },
|
||||
"bare",
|
||||
{ slug: "named", name: "Second" },
|
||||
"",
|
||||
{ name: "no slug" },
|
||||
42,
|
||||
]),
|
||||
).toEqual([
|
||||
{ slug: "bare", name: "bare", capabilities: null },
|
||||
{ slug: "named", name: "Named", capabilities },
|
||||
]);
|
||||
});
|
||||
|
||||
it("drops unparseable capabilities but keeps the entry", () => {
|
||||
expect(
|
||||
readCustomModelEntries([{ slug: "x", capabilities: { optionDescriptors: "nope" } }]),
|
||||
).toEqual([{ slug: "x", name: "x", capabilities: null }]);
|
||||
expect(readCustomModelEntries("not a list")).toEqual([]);
|
||||
});
|
||||
|
||||
it("writes the compact stored shape back", () => {
|
||||
expect(toCustomModelSetting({ slug: "x", name: "x", capabilities: null })).toBe("x");
|
||||
expect(
|
||||
toCustomModelSetting({ slug: "x", name: "x", capabilities: { optionDescriptors: [] } }),
|
||||
).toBe("x");
|
||||
expect(toCustomModelSetting({ slug: "x", name: "X", capabilities })).toEqual({
|
||||
slug: "x",
|
||||
name: "X",
|
||||
capabilities,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,12 +1,15 @@
|
||||
import {
|
||||
type CustomModelSetting,
|
||||
MODEL_SLUG_ALIASES_BY_PROVIDER,
|
||||
type ModelCapabilities,
|
||||
ModelCapabilities,
|
||||
type ModelSelection,
|
||||
ProviderDriverKind,
|
||||
ProviderInstanceId,
|
||||
type ProviderOptionDescriptor,
|
||||
type ProviderOptionSelection,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
const DEFAULT_PROVIDER_DRIVER_KIND = ProviderDriverKind.make("codex");
|
||||
|
||||
@@ -247,6 +250,76 @@ export function normalizeCustomModelSlug(model: string | null | undefined): stri
|
||||
return model.trim() || null;
|
||||
}
|
||||
|
||||
/** A custom model setting with its optional fields resolved. */
|
||||
export interface CustomModelDefinition {
|
||||
readonly slug: string;
|
||||
readonly name: string;
|
||||
readonly capabilities: ModelCapabilities | null;
|
||||
}
|
||||
|
||||
const decodeCustomModelCapabilities = Schema.decodeUnknownOption(ModelCapabilities);
|
||||
|
||||
/**
|
||||
* Read a `customModels` setting into resolved definitions. Accepts the typed
|
||||
* union as well as the opaque `providerInstances[id].config` blob clients see,
|
||||
* so it tolerates bare slugs, malformed rows, and unparseable capabilities
|
||||
* (dropped rather than failing the whole list). Slugs are trimmed and
|
||||
* deduplicated, first occurrence wins; `name` falls back to the slug.
|
||||
*/
|
||||
export function readCustomModelEntries(value: unknown): CustomModelDefinition[] {
|
||||
if (!Array.isArray(value)) return [];
|
||||
const entries: CustomModelDefinition[] = [];
|
||||
const seen = new Set<string>();
|
||||
for (const raw of value) {
|
||||
const record =
|
||||
typeof raw === "string"
|
||||
? { slug: raw }
|
||||
: raw !== null && typeof raw === "object"
|
||||
? (raw as { slug?: unknown; name?: unknown; capabilities?: unknown })
|
||||
: null;
|
||||
if (!record) continue;
|
||||
const slug = normalizeCustomModelSlug(typeof record.slug === "string" ? record.slug : null);
|
||||
if (!slug || seen.has(slug)) continue;
|
||||
seen.add(slug);
|
||||
const name =
|
||||
(typeof record.name === "string" ? normalizeCustomModelSlug(record.name) : null) ?? slug;
|
||||
const capabilities =
|
||||
record.capabilities === undefined || record.capabilities === null
|
||||
? null
|
||||
: Option.getOrNull(decodeCustomModelCapabilities(record.capabilities));
|
||||
entries.push({
|
||||
slug,
|
||||
name,
|
||||
capabilities: capabilities
|
||||
? createModelCapabilities({ optionDescriptors: capabilities.optionDescriptors ?? [] })
|
||||
: null,
|
||||
});
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/** Slugs of a `customModels` setting, in stored order. */
|
||||
export function readCustomModelSlugs(value: unknown): string[] {
|
||||
return readCustomModelEntries(value).map((entry) => entry.slug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a definition back to the compact stored shape: a bare slug when it
|
||||
* carries nothing custom, otherwise an entry with only the set fields.
|
||||
*/
|
||||
export function toCustomModelSetting(entry: CustomModelDefinition): CustomModelSetting {
|
||||
const descriptors = entry.capabilities?.optionDescriptors ?? [];
|
||||
const name = entry.name !== entry.slug ? entry.name : undefined;
|
||||
if (!name && descriptors.length === 0) return entry.slug;
|
||||
return {
|
||||
slug: entry.slug,
|
||||
...(name ? { name } : {}),
|
||||
...(descriptors.length > 0
|
||||
? { capabilities: createModelCapabilities({ optionDescriptors: descriptors }) }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveSelectableModel(
|
||||
provider: ProviderDriverKind,
|
||||
value: string | null | undefined,
|
||||
|
||||
Reference in new issue
Block a user