@oribos/model-router 0.0.0-stage → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # @oribos/model-router
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Route `'provider/model'` strings to model instances — the model-routing capability package. A
4
+ parse, a catalog, and the provider packages you already installed; the core keeps no registry
5
+ and no magic strings:
6
+
7
+ ```ts
8
+ import { Agent } from '@oribos/core/agent';
9
+ import { createModelRouter } from '@oribos/model-router';
10
+ import { groq } from '@ai-sdk/groq';
11
+
12
+ const route = createModelRouter({
13
+ catalog, // a snapshot of https://models.dev/api.json pastes as-is
14
+ providers: { groq: (id) => groq(id) }, // your installed provider packages
15
+ });
16
+
17
+ const agent = new Agent({ name: 'assistant', model: route('groq/llama-3.3-70b-versatile') });
18
+ ```
19
+
20
+ ## How it works
21
+
22
+ - **Parse.** `parseModelString` splits on the **first** slash: model ids may themselves contain
23
+ slashes (deepinfra-style ids, and groq-hosted ids like `openai/gpt-oss-120b`), so only the
24
+ first segment is the provider. Anything without that shape is refused — the string stays this
25
+ package's vocabulary, never the core's.
26
+ - **Resolve.** `createModelRouter({ catalog, providers })` checks the parsed string against the
27
+ catalog — the provider must be carried, the model id must be listed — then hands the id to your
28
+ factory and validates the result through the core's model contract (`assertModel`). What the
29
+ catalog adds over bare parsing is **existence**: unknown providers, unlisted models, and
30
+ providers whose package you haven't registered are refused at resolution time with actionable
31
+ errors — including the provider's `npm` package from the catalog when a factory is missing.
32
+ - **Pins kept.** No registry anywhere — the catalog and the factories are caller-brought data,
33
+ nothing registers and no router exists until you build one. The catalog is a vendored-shape
34
+ type (`ModelCatalog`); a models.dev snapshot pastes as-is, extra fields pass through. Provider
35
+ package dependencies travel with you, never with this package or the core. Unconfigured, the
36
+ package costs nothing and adds nothing to the core.
37
+
38
+ See `docs/architecture/model.md` (「Provider 生态」) and `examples/model-router` (a committed
39
+ catalog snapshot cut, scripted offline provider stubs).
@@ -0,0 +1,110 @@
1
+ /**
2
+ * `@oribos/model-router` — the model-routing capability package: `'provider/model'` strings
3
+ * against a models.dev-style catalog (`docs/architecture/model.md` 篇级字符串路由行,ADR-0017
4
+ * 追赶面中层;#185).
5
+ *
6
+ * The carrying seam is data, not the core: the core's "no registry, no magic string" pins stay
7
+ * untouched (ADR-0004) — this package owns the parse and the catalog, and both are the caller's
8
+ * to bring. `parseModelString` splits a routing string on its first slash (model ids may
9
+ * themselves contain slashes — deepinfra-style ids like `meta-llama/llama-4-scout`); a router
10
+ * built with `createModelRouter` checks the string against a catalog pasted in as-is (a snapshot
11
+ * of `https://models.dev/api.json` satisfies the shape) and hands the model id to the factory
12
+ * you registered for that provider — your installed AI SDK provider package, its dependency
13
+ * staying yours. What the catalog adds over bare parsing is existence: unknown providers and
14
+ * unlisted models are refused at resolution time with actionable errors, including the
15
+ * provider's `npm` package from the catalog when a provider is known but no factory is
16
+ * registered for it.
17
+ *
18
+ * Resolution validates through the core's model contract (`assertModel`) — a factory result that
19
+ * is not a language model instance of the supported specification version surfaces as the core's
20
+ * own contract error, never as a mid-run surprise. Zero cost when unconfigured: the package adds
21
+ * nothing to the core, and a router that is never built never runs.
22
+ *
23
+ * Package face: [`parseModelString`], [`ParsedModelString`], [`InvalidModelStringError`],
24
+ * [`createModelRouter`], [`ModelRouter`], [`ModelRouterConfig`], [`ModelCatalog`],
25
+ * [`CatalogProviderEntry`], [`CatalogModelEntry`], [`ModelProviderFactory`],
26
+ * [`UnknownModelProviderError`], [`UnknownModelError`], [`ModelProviderMissingError`].
27
+ */
28
+ import type { Model } from '@oribos/core/model';
29
+ /** A routing string split into its provider and model halves. */
30
+ export interface ParsedModelString {
31
+ /** The provider id — everything before the first `/`. */
32
+ readonly providerId: string;
33
+ /** The model id — everything after the first `/`, slashes included. */
34
+ readonly modelId: string;
35
+ }
36
+ /** `parseModelString` on a value that is not a `'provider/model'` string. */
37
+ export declare class InvalidModelStringError extends Error {
38
+ constructor(value: string);
39
+ }
40
+ /**
41
+ * Splits a `'provider/model'` routing string on its **first** slash: model ids may themselves
42
+ * contain slashes (deepinfra-style ids like `meta-llama/llama-4-scout`), so only the first
43
+ * segment is the provider. Pure — no catalog, no providers, just the parse.
44
+ */
45
+ export declare function parseModelString(value: string): ParsedModelString;
46
+ /** One model's catalog entry. Only the record key is load-bearing; the rest is pasted through. */
47
+ export interface CatalogModelEntry {
48
+ /** The model's display name, if the catalog carries one. */
49
+ readonly name?: string | undefined;
50
+ /** The model's window limits, if the catalog carries them. */
51
+ readonly limit?: {
52
+ readonly context?: number;
53
+ readonly output?: number;
54
+ } | undefined;
55
+ /** models.dev snapshots carry more fields than routing consumes; they paste through untouched. */
56
+ readonly [key: string]: unknown;
57
+ }
58
+ /**
59
+ * One provider's catalog entry: which npm package serves it, and which model ids exist under it.
60
+ * A snapshot of `https://models.dev/api.json` satisfies this shape as-is.
61
+ */
62
+ export interface CatalogProviderEntry {
63
+ /** The AI SDK provider package that serves this provider — surfaced in missing-factory errors. */
64
+ readonly npm?: string | undefined;
65
+ /** The provider's display name, if the catalog carries one. */
66
+ readonly name?: string | undefined;
67
+ /** The provider's model ids — the existence check routing resolves against. */
68
+ readonly models: Readonly<Record<string, CatalogModelEntry>>;
69
+ /** models.dev snapshots carry more fields than routing consumes; they paste through untouched. */
70
+ readonly [key: string]: unknown;
71
+ }
72
+ /** The catalog: provider id → provider entry. Data the caller brings (e.g. a models.dev cut). */
73
+ export type ModelCatalog = Readonly<Record<string, CatalogProviderEntry>>;
74
+ /**
75
+ * The seam your installed provider package rides: called with the model id exactly as the
76
+ * catalog keys it, returns the language model instance (`(id) => groq(id)` for `@ai-sdk/groq`).
77
+ * The provider package's dependency travels with you, never with this package or the core.
78
+ */
79
+ export type ModelProviderFactory = (modelId: string) => unknown;
80
+ /** `createModelRouter` config: the catalog (data) and your factories (the installed packages). */
81
+ export interface ModelRouterConfig {
82
+ /** Provider id → provider entry; a models.dev snapshot pastes as-is. */
83
+ readonly catalog: ModelCatalog;
84
+ /** Provider id → factory from your installed AI SDK provider package. */
85
+ readonly providers: Readonly<Record<string, ModelProviderFactory>>;
86
+ }
87
+ /** A router: a routing string in, a contract-validated model instance out. */
88
+ export type ModelRouter = (value: string) => Model;
89
+ /** Routing a string whose provider id the catalog does not carry. */
90
+ export declare class UnknownModelProviderError extends Error {
91
+ constructor(value: string, catalog: ModelCatalog);
92
+ }
93
+ /** Routing a string whose provider the catalog carries but whose model id it does not list. */
94
+ export declare class UnknownModelError extends Error {
95
+ constructor(value: string, providerId: string, provider: CatalogProviderEntry);
96
+ }
97
+ /** Routing a provider the catalog carries but no factory is registered for. */
98
+ export declare class ModelProviderMissingError extends Error {
99
+ constructor(providerId: string, provider: CatalogProviderEntry);
100
+ }
101
+ /**
102
+ * Builds a router from a catalog and your installed provider packages: `'groq/llama-3.3-70b'` in,
103
+ * the model instance `groq('llama-3.3-70b')` out — after the catalog has vouched that both the
104
+ * provider and the model id exist. The result is validated through the core's model contract, so
105
+ * a factory that does not return a language model of the supported specification version fails
106
+ * here, at resolution time. Everything — the catalog, the factories — is caller-brought; nothing
107
+ * registers anywhere, and no router exists until you build one.
108
+ */
109
+ export declare function createModelRouter(config: ModelRouterConfig): ModelRouter;
110
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAGH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAIhD,iEAAiE;AACjE,MAAM,WAAW,iBAAiB;IAChC,yDAAyD;IACzD,QAAQ,CAAC,UAAU,EAAE,MAAM,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AChC,wEAAwE;IACxE,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAC/B,yEAAyE;IACzE,QAAQ,CAAC,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC,CAAC;CACpE;AAED,8EAA8E;AAC9E,MAAM,MAAM,WAAW,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,KAAK,CAAC;AAEnD,qEAAqE;AACrE,qBAAa,yBAA0B,SAAQ,KAAK;IAClD,YAAY,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAO/C;CACF;AAED,+FAA+F;AAC/F,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,YAAY,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAO5E;CACF;AAED,+EAA+E;AAC/E,qBAAa,yBAA0B,SAAQ,KAAK;IAClD,YAAY,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAO7D;CACF;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,GAAG,WAAW,CAaxE"}
package/dist/index.js ADDED
@@ -0,0 +1,105 @@
1
+ /**
2
+ * `@oribos/model-router` — the model-routing capability package: `'provider/model'` strings
3
+ * against a models.dev-style catalog (`docs/architecture/model.md` 篇级字符串路由行,ADR-0017
4
+ * 追赶面中层;#185).
5
+ *
6
+ * The carrying seam is data, not the core: the core's "no registry, no magic string" pins stay
7
+ * untouched (ADR-0004) — this package owns the parse and the catalog, and both are the caller's
8
+ * to bring. `parseModelString` splits a routing string on its first slash (model ids may
9
+ * themselves contain slashes — deepinfra-style ids like `meta-llama/llama-4-scout`); a router
10
+ * built with `createModelRouter` checks the string against a catalog pasted in as-is (a snapshot
11
+ * of `https://models.dev/api.json` satisfies the shape) and hands the model id to the factory
12
+ * you registered for that provider — your installed AI SDK provider package, its dependency
13
+ * staying yours. What the catalog adds over bare parsing is existence: unknown providers and
14
+ * unlisted models are refused at resolution time with actionable errors, including the
15
+ * provider's `npm` package from the catalog when a provider is known but no factory is
16
+ * registered for it.
17
+ *
18
+ * Resolution validates through the core's model contract (`assertModel`) — a factory result that
19
+ * is not a language model instance of the supported specification version surfaces as the core's
20
+ * own contract error, never as a mid-run surprise. Zero cost when unconfigured: the package adds
21
+ * nothing to the core, and a router that is never built never runs.
22
+ *
23
+ * Package face: [`parseModelString`], [`ParsedModelString`], [`InvalidModelStringError`],
24
+ * [`createModelRouter`], [`ModelRouter`], [`ModelRouterConfig`], [`ModelCatalog`],
25
+ * [`CatalogProviderEntry`], [`CatalogModelEntry`], [`ModelProviderFactory`],
26
+ * [`UnknownModelProviderError`], [`UnknownModelError`], [`ModelProviderMissingError`].
27
+ */
28
+ import { assertModel } from '@oribos/core/model';
29
+ /** `parseModelString` on a value that is not a `'provider/model'` string. */
30
+ export class InvalidModelStringError extends Error {
31
+ constructor(value) {
32
+ super(`not a 'provider/model' string: ${JSON.stringify(value)} — expected '<provider>/<model>', ` +
33
+ "split on the first '/' (the model id may itself contain slashes)");
34
+ this.name = 'InvalidModelStringError';
35
+ }
36
+ }
37
+ /**
38
+ * Splits a `'provider/model'` routing string on its **first** slash: model ids may themselves
39
+ * contain slashes (deepinfra-style ids like `meta-llama/llama-4-scout`), so only the first
40
+ * segment is the provider. Pure — no catalog, no providers, just the parse.
41
+ */
42
+ export function parseModelString(value) {
43
+ const slash = value.indexOf('/');
44
+ const providerId = slash === -1 ? '' : value.slice(0, slash);
45
+ const modelId = slash === -1 ? '' : value.slice(slash + 1);
46
+ if (providerId === '' ||
47
+ modelId === '' ||
48
+ providerId.trim() !== providerId ||
49
+ modelId.trim() !== modelId) {
50
+ throw new InvalidModelStringError(value);
51
+ }
52
+ return { providerId, modelId };
53
+ }
54
+ /** Routing a string whose provider id the catalog does not carry. */
55
+ export class UnknownModelProviderError extends Error {
56
+ constructor(value, catalog) {
57
+ super(`unknown provider in ${JSON.stringify(value)} — the catalog carries ` +
58
+ `${Object.keys(catalog).length} provider(s); cut a wider catalog (a models.dev snapshot ` +
59
+ 'pastes as-is) or fix the provider id');
60
+ this.name = 'UnknownModelProviderError';
61
+ }
62
+ }
63
+ /** Routing a string whose provider the catalog carries but whose model id it does not list. */
64
+ export class UnknownModelError extends Error {
65
+ constructor(value, providerId, provider) {
66
+ super(`unknown model in ${JSON.stringify(value)} — the catalog lists ` +
67
+ `${Object.keys(provider.models).length} model(s) for '${providerId}'; cut a wider ` +
68
+ 'catalog or fix the model id');
69
+ this.name = 'UnknownModelError';
70
+ }
71
+ }
72
+ /** Routing a provider the catalog carries but no factory is registered for. */
73
+ export class ModelProviderMissingError extends Error {
74
+ constructor(providerId, provider) {
75
+ const via = typeof provider.npm === 'string' ? ` (the catalog names its package: ${provider.npm})` : '';
76
+ super(`no provider factory registered for '${providerId}'${via} — install the provider package ` +
77
+ `and register it: createModelRouter({ catalog, providers: { '${providerId}': (id) => … } })`);
78
+ this.name = 'ModelProviderMissingError';
79
+ }
80
+ }
81
+ /**
82
+ * Builds a router from a catalog and your installed provider packages: `'groq/llama-3.3-70b'` in,
83
+ * the model instance `groq('llama-3.3-70b')` out — after the catalog has vouched that both the
84
+ * provider and the model id exist. The result is validated through the core's model contract, so
85
+ * a factory that does not return a language model of the supported specification version fails
86
+ * here, at resolution time. Everything — the catalog, the factories — is caller-brought; nothing
87
+ * registers anywhere, and no router exists until you build one.
88
+ */
89
+ export function createModelRouter(config) {
90
+ const { catalog, providers } = config;
91
+ return (value) => {
92
+ const { providerId, modelId } = parseModelString(value);
93
+ const provider = catalog[providerId];
94
+ if (provider === undefined)
95
+ throw new UnknownModelProviderError(value, catalog);
96
+ if (provider.models[modelId] === undefined) {
97
+ throw new UnknownModelError(value, providerId, provider);
98
+ }
99
+ const factory = providers[providerId];
100
+ if (factory === undefined)
101
+ throw new ModelProviderMissingError(providerId, provider);
102
+ return assertModel(factory(modelId));
103
+ };
104
+ }
105
+ //# sourceMappingURL=index.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AAE,MAAM,IAAI,yBAAyB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAChF,IAAI,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,SAAS,EAAE,CAAC;YAC3C,MAAM,IAAI,iBAAiB,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;QAC3D,CAAC;QACD,MAAM,OAAO,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC;QACtC,IAAI,OAAO,KAAK,SAAS;YAAE,MAAM,IAAI,yBAAyB,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;QACrF,OAAO,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IACvC,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@oribos/model-router",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.9.0",
4
+ "description": "Oribos model-router capability package — route 'provider/model' strings to model instances against a models.dev-style catalog you paste in. Parse and resolve only; the provider packages and the catalog stay yours, and the core keeps no registry and no magic strings.",
5
+ "keywords": [
6
+ "ai-agents",
7
+ "llm",
8
+ "typescript",
9
+ "agent-framework",
10
+ "model-router",
11
+ "models-dev"
12
+ ],
13
+ "license": "Apache-2.0",
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "engines": {
17
+ "node": ">=22.13.0"
18
+ },
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/0xnicholas/oribos-framework.git",
31
+ "directory": "packages/model-router"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "peerDependencies": {
37
+ "@oribos/core": "^0.9.0"
38
+ },
39
+ "devDependencies": {
40
+ "@oribos/testing": "0.0.0",
41
+ "@oribos/core": "0.9.0"
42
+ },
43
+ "scripts": {
44
+ "build": "tsc -p tsconfig.build.json",
45
+ "typecheck": "tsc -p tsconfig.json --noEmit",
46
+ "check:dist": "node ../../scripts/check-dist.mjs",
47
+ "check:export-surface": "node ../../scripts/check-export-surface.mjs",
48
+ "check:runtime-deps": "node ../../scripts/check-runtime-deps.mjs",
49
+ "check:byte-budget": "node ../../scripts/check-byte-budget.mjs",
50
+ "byte-budget:update": "node ../../scripts/check-byte-budget.mjs --update",
51
+ "check:deps-budget": "node ../../scripts/check-deps-budget.mjs",
52
+ "deps-budget:update": "node ../../scripts/check-deps-budget.mjs --update"
53
+ }
6
54
  }