Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

origin-key.ts

29 lines1.1 KB

1/**
2 * Bedrock's own version on a model id: `-v1:0`, `-v1`, `-1:0`, then any
3 * remaining `:N`. Only version 1 is read as Bedrock's: a maker's own
4 * `-v4` (`cohere.embed-v4:0`) or `-v2` (`luma.ray-v2:0`) stays in the id.
5 */
6export const BEDROCK_VERSION = /(?:-v1(?::\d+)?|-1:\d+|:\d+)$/
7​
8/**
9 * The key the name tier of `reconcileSameAs` (issue #719) matches rows by:
10 * lowercase, a leading `maker/` dropped when `maker` is a provider id or a
11 * claimed namespace (`prefixes`, lowercased), a Bedrock-form `maker.model`
12 * id dropped to its model with Bedrock's version taken off, then dots read
13 * as hyphens. Nothing here names a maker: the prefixes come from the
14 * providers and `provider_model_namespaces` tables.
15 */
16export function originNameKey(
17 rawId: string,
18 prefixes: ReadonlySet<string>,
19): string {
20 let id = rawId.toLowerCase()
21 const slash = id.indexOf('/')
22 const dotted = /^([a-z0-9-]+)\.(.+)$/.exec(id)
23 if (slash > 0 && prefixes.has(id.slice(0, slash))) {
24 id = id.slice(slash + 1)
25 } else if (dotted?.[1] && dotted[2] && prefixes.has(dotted[1])) {
26 id = dotted[2].replace(BEDROCK_VERSION, '')
27 }
28 return id.replaceAll('.', '-')