refactor(cli): parse variants from model refs
This commit is contained in:
parent
cccd013801
commit
67f2393f83
10 changed files with 79 additions and 34 deletions
|
|
@ -15,7 +15,27 @@ export const Ref = Schema.Struct({
|
|||
id: ID,
|
||||
providerID: Provider.ID,
|
||||
variant: VariantID.pipe(optional),
|
||||
}).annotate({ identifier: "Model.Ref" })
|
||||
})
|
||||
.annotate({ identifier: "Model.Ref" })
|
||||
.pipe(
|
||||
statics((schema) => ({
|
||||
parse: (input: string) => {
|
||||
const providerEnd = input.indexOf("/")
|
||||
if (providerEnd <= 0) throw new Error(`Invalid model reference: ${input}`)
|
||||
const providerID = input.slice(0, providerEnd)
|
||||
const variantStart = input.indexOf("#", providerEnd + 1)
|
||||
const id = input.slice(providerEnd + 1, variantStart === -1 ? undefined : variantStart)
|
||||
const variant = variantStart === -1 ? undefined : input.slice(variantStart + 1)
|
||||
if (!id || providerID.includes("#") || (variant !== undefined && (!variant || variant.includes("#"))))
|
||||
throw new Error(`Invalid model reference: ${input}`)
|
||||
return schema.make({
|
||||
providerID: Provider.ID.make(providerID),
|
||||
id: ID.make(id),
|
||||
...(variant ? { variant: VariantID.make(variant) } : {}),
|
||||
})
|
||||
},
|
||||
})),
|
||||
)
|
||||
export interface Ref extends Schema.Schema.Type<typeof Ref> {}
|
||||
|
||||
export const Family = Schema.String.pipe(Schema.brand("Model.Family"))
|
||||
|
|
|
|||
22
packages/schema/test/model.test.ts
Normal file
22
packages/schema/test/model.test.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { describe, expect, test } from "bun:test"
|
||||
import { Model } from "../src/model.js"
|
||||
|
||||
describe("Model.Ref", () => {
|
||||
test("parses model references with optional variants", () => {
|
||||
const variant = Model.Ref.parse("openrouter/openai/gpt-5#high")
|
||||
expect(String(variant.providerID)).toBe("openrouter")
|
||||
expect(String(variant.id)).toBe("openai/gpt-5")
|
||||
expect(String(variant.variant)).toBe("high")
|
||||
|
||||
const standard = Model.Ref.parse("anthropic/claude-sonnet")
|
||||
expect(String(standard.providerID)).toBe("anthropic")
|
||||
expect(String(standard.id)).toBe("claude-sonnet")
|
||||
expect(standard.variant).toBeUndefined()
|
||||
})
|
||||
|
||||
test("rejects malformed model references", () => {
|
||||
expect(() => Model.Ref.parse("gpt-5")).toThrow()
|
||||
expect(() => Model.Ref.parse("openai/gpt-5#")).toThrow()
|
||||
expect(() => Model.Ref.parse("openai/gpt-5#high#extra")).toThrow()
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue