diff --git a/packages/config-yaml/src/index.ts b/packages/config-yaml/src/index.ts index 0a70b1c01..18a7098fb 100644 --- a/packages/config-yaml/src/index.ts +++ b/packages/config-yaml/src/index.ts @@ -8,6 +8,7 @@ export * from "./load/merge.js"; export * from "./load/proxySecretResolution.js"; export * from "./load/typeGuards.js"; export * from "./load/unroll.js"; +export * from "./modelName.js"; export * from "./registryClient.js"; export * from "./schemas/data/index.js"; export * from "./schemas/index.js"; diff --git a/packages/config-yaml/src/modelName.test.ts b/packages/config-yaml/src/modelName.test.ts new file mode 100644 index 000000000..626d4b379 --- /dev/null +++ b/packages/config-yaml/src/modelName.test.ts @@ -0,0 +1,43 @@ +import { parseProxyModelName } from "./modelName.js"; + +describe("parseProxyModelName", () => { + it("parses a basic model name correctly", () => { + const result = parseProxyModelName("owner/package/provider/model"); + expect(result).toEqual({ + ownerSlug: "owner", + packageSlug: "package", + provider: "provider", + model: "model", + }); + }); + + it("handles model names with slashes in the model part", () => { + const result = parseProxyModelName( + "owner/package/provider/model/with/slashes", + ); + expect(result).toEqual({ + ownerSlug: "owner", + packageSlug: "package", + provider: "provider", + model: "model/with/slashes", + }); + }); + + it("throws an error for invalid model formats (missing provider)", () => { + expect(() => { + parseProxyModelName("owner/package"); + }).toThrow("Invalid model format"); + }); + + it("throws an error for invalid model formats (missing model)", () => { + expect(() => { + parseProxyModelName("owner/package/provider"); + }).toThrow("Invalid model format"); + }); + + it("throws an error for empty string", () => { + expect(() => { + parseProxyModelName(""); + }).toThrow("Invalid model format"); + }); +}); diff --git a/packages/config-yaml/src/modelName.ts b/packages/config-yaml/src/modelName.ts new file mode 100644 index 000000000..5c15f790b --- /dev/null +++ b/packages/config-yaml/src/modelName.ts @@ -0,0 +1,19 @@ +export interface ProxyModelName { + ownerSlug: string; + packageSlug: string; + provider: string; + model: string; +} + +export function parseProxyModelName(modelName: string): ProxyModelName { + const parts = modelName.split("/"); + + const [ownerSlug, packageSlug, provider, ...modelParts] = parts; + const model = modelParts.join("/"); + + if (!provider || !model) { + throw new Error("Invalid model format"); + } + + return { provider, model, ownerSlug, packageSlug }; +}