@auxiora/model-registry 1.10.7
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 +191 -0
- package/package.json +10 -0
- package/src/db.ts +39 -0
- package/src/index.ts +6 -0
- package/src/registry.ts +181 -0
- package/src/types.ts +58 -0
- package/tests/registry.test.ts +239 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2026 Auxiora Contributors
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/package.json
ADDED
package/src/db.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
|
|
3
|
+
const SCHEMA_DDL = `
|
|
4
|
+
CREATE TABLE IF NOT EXISTS discovered_models (
|
|
5
|
+
id TEXT PRIMARY KEY,
|
|
6
|
+
provider_source TEXT NOT NULL,
|
|
7
|
+
model_id TEXT NOT NULL,
|
|
8
|
+
display_name TEXT NOT NULL,
|
|
9
|
+
context_length INTEGER DEFAULT 0,
|
|
10
|
+
supports_vision INTEGER DEFAULT 0,
|
|
11
|
+
supports_tools INTEGER DEFAULT 0,
|
|
12
|
+
supports_streaming INTEGER DEFAULT 1,
|
|
13
|
+
supports_image_gen INTEGER DEFAULT 0,
|
|
14
|
+
cost_per_1k_input REAL DEFAULT 0,
|
|
15
|
+
cost_per_1k_output REAL DEFAULT 0,
|
|
16
|
+
strengths TEXT DEFAULT '[]',
|
|
17
|
+
raw_metadata TEXT,
|
|
18
|
+
hf_model_card TEXT,
|
|
19
|
+
hf_downloads INTEGER,
|
|
20
|
+
hf_likes INTEGER,
|
|
21
|
+
hf_trending_score REAL,
|
|
22
|
+
hf_tags TEXT,
|
|
23
|
+
hf_benchmark_scores TEXT,
|
|
24
|
+
hf_inference_providers TEXT,
|
|
25
|
+
last_refreshed_at INTEGER NOT NULL,
|
|
26
|
+
created_at INTEGER NOT NULL,
|
|
27
|
+
enabled INTEGER DEFAULT 1
|
|
28
|
+
);
|
|
29
|
+
CREATE INDEX IF NOT EXISTS idx_models_source ON discovered_models(provider_source);
|
|
30
|
+
CREATE INDEX IF NOT EXISTS idx_models_enabled ON discovered_models(enabled);
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
export function openDatabase(dbPath: string): DatabaseSync {
|
|
34
|
+
const db = new DatabaseSync(dbPath);
|
|
35
|
+
db.exec('PRAGMA journal_mode=WAL');
|
|
36
|
+
db.exec('PRAGMA foreign_keys=ON');
|
|
37
|
+
db.exec(SCHEMA_DDL);
|
|
38
|
+
return db;
|
|
39
|
+
}
|
package/src/index.ts
ADDED
package/src/registry.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { openDatabase } from './db.js';
|
|
3
|
+
import type { DiscoveredModel, ModelCapabilitiesLike, ModelSearchOptions } from './types.js';
|
|
4
|
+
|
|
5
|
+
type Row = Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
function rowToModel(row: Row): DiscoveredModel {
|
|
8
|
+
return {
|
|
9
|
+
id: row.id as string,
|
|
10
|
+
providerSource: row.provider_source as string,
|
|
11
|
+
modelId: row.model_id as string,
|
|
12
|
+
displayName: row.display_name as string,
|
|
13
|
+
contextLength: row.context_length as number,
|
|
14
|
+
supportsVision: (row.supports_vision as number) === 1,
|
|
15
|
+
supportsTools: (row.supports_tools as number) === 1,
|
|
16
|
+
supportsStreaming: (row.supports_streaming as number) === 1,
|
|
17
|
+
supportsImageGen: (row.supports_image_gen as number) === 1,
|
|
18
|
+
costPer1kInput: row.cost_per_1k_input as number,
|
|
19
|
+
costPer1kOutput: row.cost_per_1k_output as number,
|
|
20
|
+
strengths: JSON.parse((row.strengths as string) || '[]') as string[],
|
|
21
|
+
rawMetadata: row.raw_metadata as string | undefined,
|
|
22
|
+
hfModelCard: row.hf_model_card as string | undefined,
|
|
23
|
+
hfDownloads: row.hf_downloads as number | undefined,
|
|
24
|
+
hfLikes: row.hf_likes as number | undefined,
|
|
25
|
+
hfTrendingScore: row.hf_trending_score as number | undefined,
|
|
26
|
+
hfTags: row.hf_tags ? JSON.parse(row.hf_tags as string) as string[] : undefined,
|
|
27
|
+
hfBenchmarkScores: row.hf_benchmark_scores
|
|
28
|
+
? JSON.parse(row.hf_benchmark_scores as string) as Record<string, number>
|
|
29
|
+
: undefined,
|
|
30
|
+
hfInferenceProviders: row.hf_inference_providers
|
|
31
|
+
? JSON.parse(row.hf_inference_providers as string) as string[]
|
|
32
|
+
: undefined,
|
|
33
|
+
lastRefreshedAt: row.last_refreshed_at as number,
|
|
34
|
+
createdAt: row.created_at as number,
|
|
35
|
+
enabled: (row.enabled as number) === 1,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class ModelRegistry {
|
|
40
|
+
private db: DatabaseSync;
|
|
41
|
+
|
|
42
|
+
constructor(dbPath: string) {
|
|
43
|
+
this.db = openDatabase(dbPath);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
upsertModels(models: DiscoveredModel[]): void {
|
|
47
|
+
const stmt = this.db.prepare(`
|
|
48
|
+
INSERT OR REPLACE INTO discovered_models (
|
|
49
|
+
id, provider_source, model_id, display_name, context_length,
|
|
50
|
+
supports_vision, supports_tools, supports_streaming, supports_image_gen,
|
|
51
|
+
cost_per_1k_input, cost_per_1k_output, strengths, raw_metadata,
|
|
52
|
+
hf_model_card, hf_downloads, hf_likes, hf_trending_score,
|
|
53
|
+
hf_tags, hf_benchmark_scores, hf_inference_providers,
|
|
54
|
+
last_refreshed_at, created_at, enabled
|
|
55
|
+
) VALUES (
|
|
56
|
+
?, ?, ?, ?, ?,
|
|
57
|
+
?, ?, ?, ?,
|
|
58
|
+
?, ?, ?, ?,
|
|
59
|
+
?, ?, ?, ?,
|
|
60
|
+
?, ?, ?,
|
|
61
|
+
?, ?, ?
|
|
62
|
+
)
|
|
63
|
+
`);
|
|
64
|
+
|
|
65
|
+
for (const m of models) {
|
|
66
|
+
stmt.run(
|
|
67
|
+
m.id, m.providerSource, m.modelId, m.displayName, m.contextLength,
|
|
68
|
+
m.supportsVision ? 1 : 0, m.supportsTools ? 1 : 0,
|
|
69
|
+
m.supportsStreaming ? 1 : 0, m.supportsImageGen ? 1 : 0,
|
|
70
|
+
m.costPer1kInput, m.costPer1kOutput,
|
|
71
|
+
JSON.stringify(m.strengths), m.rawMetadata ?? null,
|
|
72
|
+
m.hfModelCard ?? null, m.hfDownloads ?? null,
|
|
73
|
+
m.hfLikes ?? null, m.hfTrendingScore ?? null,
|
|
74
|
+
m.hfTags ? JSON.stringify(m.hfTags) : null,
|
|
75
|
+
m.hfBenchmarkScores ? JSON.stringify(m.hfBenchmarkScores) : null,
|
|
76
|
+
m.hfInferenceProviders ? JSON.stringify(m.hfInferenceProviders) : null,
|
|
77
|
+
m.lastRefreshedAt, m.createdAt,
|
|
78
|
+
m.enabled ? 1 : 0,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
getModels(options?: ModelSearchOptions): DiscoveredModel[] {
|
|
84
|
+
const conditions: string[] = [];
|
|
85
|
+
const params: unknown[] = [];
|
|
86
|
+
|
|
87
|
+
if (options?.source) {
|
|
88
|
+
conditions.push('provider_source = ?');
|
|
89
|
+
params.push(options.source);
|
|
90
|
+
}
|
|
91
|
+
if (options?.query) {
|
|
92
|
+
conditions.push('(display_name LIKE ? OR model_id LIKE ?)');
|
|
93
|
+
const q = `%${options.query}%`;
|
|
94
|
+
params.push(q, q);
|
|
95
|
+
}
|
|
96
|
+
if (options?.supportsVision !== undefined) {
|
|
97
|
+
conditions.push('supports_vision = ?');
|
|
98
|
+
params.push(options.supportsVision ? 1 : 0);
|
|
99
|
+
}
|
|
100
|
+
if (options?.supportsTools !== undefined) {
|
|
101
|
+
conditions.push('supports_tools = ?');
|
|
102
|
+
params.push(options.supportsTools ? 1 : 0);
|
|
103
|
+
}
|
|
104
|
+
if (options?.enabled !== undefined) {
|
|
105
|
+
conditions.push('enabled = ?');
|
|
106
|
+
params.push(options.enabled ? 1 : 0);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
110
|
+
const limit = options?.limit ?? 200;
|
|
111
|
+
const offset = options?.offset ?? 0;
|
|
112
|
+
|
|
113
|
+
const rows = this.db.prepare(
|
|
114
|
+
`SELECT * FROM discovered_models ${where} ORDER BY display_name ASC LIMIT ? OFFSET ?`,
|
|
115
|
+
).all(...params, limit, offset) as Row[];
|
|
116
|
+
|
|
117
|
+
return rows.map(rowToModel);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getModel(id: string): DiscoveredModel | undefined {
|
|
121
|
+
const row = this.db.prepare(
|
|
122
|
+
'SELECT * FROM discovered_models WHERE id = ?',
|
|
123
|
+
).get(id) as Row | undefined;
|
|
124
|
+
|
|
125
|
+
return row ? rowToModel(row) : undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Convert discovered models to the format ModelRouter expects.
|
|
130
|
+
* Returns Record<modelId, ModelCapabilities>.
|
|
131
|
+
*/
|
|
132
|
+
toModelCapabilities(source: string): Record<string, ModelCapabilitiesLike> {
|
|
133
|
+
const models = this.getModels({ source, enabled: true });
|
|
134
|
+
const result: Record<string, ModelCapabilitiesLike> = {};
|
|
135
|
+
|
|
136
|
+
for (const m of models) {
|
|
137
|
+
result[m.modelId] = {
|
|
138
|
+
maxContextTokens: m.contextLength,
|
|
139
|
+
supportsVision: m.supportsVision,
|
|
140
|
+
supportsTools: m.supportsTools,
|
|
141
|
+
supportsStreaming: m.supportsStreaming,
|
|
142
|
+
supportsImageGen: m.supportsImageGen,
|
|
143
|
+
costPer1kInput: m.costPer1kInput,
|
|
144
|
+
costPer1kOutput: m.costPer1kOutput,
|
|
145
|
+
strengths: m.strengths,
|
|
146
|
+
isLocal: false,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
search(query: string): DiscoveredModel[] {
|
|
154
|
+
return this.getModels({ query, enabled: true });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
getTrending(limit = 20): DiscoveredModel[] {
|
|
158
|
+
const rows = this.db.prepare(
|
|
159
|
+
`SELECT * FROM discovered_models
|
|
160
|
+
WHERE hf_trending_score IS NOT NULL AND enabled = 1
|
|
161
|
+
ORDER BY hf_trending_score DESC
|
|
162
|
+
LIMIT ?`,
|
|
163
|
+
).all(limit) as Row[];
|
|
164
|
+
|
|
165
|
+
return rows.map(rowToModel);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
pruneStale(maxAgeMs: number): number {
|
|
169
|
+
const cutoff = Date.now() - maxAgeMs;
|
|
170
|
+
const result = this.db.prepare(
|
|
171
|
+
'DELETE FROM discovered_models WHERE last_refreshed_at < ?',
|
|
172
|
+
).run(cutoff);
|
|
173
|
+
return result.changes;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
setEnabled(id: string, enabled: boolean): void {
|
|
177
|
+
this.db.prepare(
|
|
178
|
+
'UPDATE discovered_models SET enabled = ? WHERE id = ?',
|
|
179
|
+
).run(enabled ? 1 : 0, id);
|
|
180
|
+
}
|
|
181
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A model discovered from an external provider API (OpenRouter, HuggingFace, etc.).
|
|
3
|
+
*/
|
|
4
|
+
export interface DiscoveredModel {
|
|
5
|
+
/** Composite key: `${providerSource}:${modelId}` */
|
|
6
|
+
id: string;
|
|
7
|
+
/** Source provider that discovered this model */
|
|
8
|
+
providerSource: string;
|
|
9
|
+
/** Original model ID from the external API */
|
|
10
|
+
modelId: string;
|
|
11
|
+
displayName: string;
|
|
12
|
+
contextLength: number;
|
|
13
|
+
supportsVision: boolean;
|
|
14
|
+
supportsTools: boolean;
|
|
15
|
+
supportsStreaming: boolean;
|
|
16
|
+
supportsImageGen: boolean;
|
|
17
|
+
costPer1kInput: number;
|
|
18
|
+
costPer1kOutput: number;
|
|
19
|
+
strengths: string[];
|
|
20
|
+
rawMetadata?: string;
|
|
21
|
+
/** HuggingFace-specific fields */
|
|
22
|
+
hfModelCard?: string;
|
|
23
|
+
hfDownloads?: number;
|
|
24
|
+
hfLikes?: number;
|
|
25
|
+
hfTrendingScore?: number;
|
|
26
|
+
hfTags?: string[];
|
|
27
|
+
hfBenchmarkScores?: Record<string, number>;
|
|
28
|
+
hfInferenceProviders?: string[];
|
|
29
|
+
lastRefreshedAt: number;
|
|
30
|
+
createdAt: number;
|
|
31
|
+
enabled: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ModelSearchOptions {
|
|
35
|
+
source?: string;
|
|
36
|
+
query?: string;
|
|
37
|
+
supportsVision?: boolean;
|
|
38
|
+
supportsTools?: boolean;
|
|
39
|
+
enabled?: boolean;
|
|
40
|
+
limit?: number;
|
|
41
|
+
offset?: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Structural type matching ModelCapabilities from @auxiora/providers
|
|
46
|
+
* to avoid a hard dependency on that package.
|
|
47
|
+
*/
|
|
48
|
+
export interface ModelCapabilitiesLike {
|
|
49
|
+
maxContextTokens: number;
|
|
50
|
+
supportsVision: boolean;
|
|
51
|
+
supportsTools: boolean;
|
|
52
|
+
supportsStreaming: boolean;
|
|
53
|
+
supportsImageGen: boolean;
|
|
54
|
+
costPer1kInput: number;
|
|
55
|
+
costPer1kOutput: number;
|
|
56
|
+
strengths: string[];
|
|
57
|
+
isLocal: boolean;
|
|
58
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { ModelRegistry } from '../src/registry.js';
|
|
3
|
+
import type { DiscoveredModel } from '../src/types.js';
|
|
4
|
+
|
|
5
|
+
function makeModel(overrides: Partial<DiscoveredModel> = {}): DiscoveredModel {
|
|
6
|
+
const now = Date.now();
|
|
7
|
+
return {
|
|
8
|
+
id: 'openrouter:test/model-1',
|
|
9
|
+
providerSource: 'openrouter',
|
|
10
|
+
modelId: 'test/model-1',
|
|
11
|
+
displayName: 'Test Model 1',
|
|
12
|
+
contextLength: 128000,
|
|
13
|
+
supportsVision: true,
|
|
14
|
+
supportsTools: true,
|
|
15
|
+
supportsStreaming: true,
|
|
16
|
+
supportsImageGen: false,
|
|
17
|
+
costPer1kInput: 0.003,
|
|
18
|
+
costPer1kOutput: 0.015,
|
|
19
|
+
strengths: ['reasoning', 'code'],
|
|
20
|
+
lastRefreshedAt: now,
|
|
21
|
+
createdAt: now,
|
|
22
|
+
enabled: true,
|
|
23
|
+
...overrides,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('ModelRegistry', () => {
|
|
28
|
+
let registry: ModelRegistry;
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
registry = new ModelRegistry(':memory:');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('upsertModels', () => {
|
|
35
|
+
it('should insert models and retrieve them', () => {
|
|
36
|
+
const model = makeModel();
|
|
37
|
+
registry.upsertModels([model]);
|
|
38
|
+
|
|
39
|
+
const result = registry.getModel('openrouter:test/model-1');
|
|
40
|
+
expect(result).toBeDefined();
|
|
41
|
+
expect(result!.displayName).toBe('Test Model 1');
|
|
42
|
+
expect(result!.supportsVision).toBe(true);
|
|
43
|
+
expect(result!.strengths).toEqual(['reasoning', 'code']);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should upsert (update on conflict)', () => {
|
|
47
|
+
const model = makeModel();
|
|
48
|
+
registry.upsertModels([model]);
|
|
49
|
+
|
|
50
|
+
const updated = makeModel({ displayName: 'Updated Name', costPer1kInput: 0.005 });
|
|
51
|
+
registry.upsertModels([updated]);
|
|
52
|
+
|
|
53
|
+
const result = registry.getModel('openrouter:test/model-1');
|
|
54
|
+
expect(result!.displayName).toBe('Updated Name');
|
|
55
|
+
expect(result!.costPer1kInput).toBe(0.005);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should insert multiple models at once', () => {
|
|
59
|
+
const models = [
|
|
60
|
+
makeModel({ id: 'openrouter:a/model-a', modelId: 'a/model-a', displayName: 'Model A' }),
|
|
61
|
+
makeModel({ id: 'openrouter:b/model-b', modelId: 'b/model-b', displayName: 'Model B' }),
|
|
62
|
+
makeModel({ id: 'openrouter:c/model-c', modelId: 'c/model-c', displayName: 'Model C' }),
|
|
63
|
+
];
|
|
64
|
+
registry.upsertModels(models);
|
|
65
|
+
|
|
66
|
+
const all = registry.getModels();
|
|
67
|
+
expect(all).toHaveLength(3);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('getModels', () => {
|
|
72
|
+
beforeEach(() => {
|
|
73
|
+
registry.upsertModels([
|
|
74
|
+
makeModel({ id: 'openrouter:a/big', modelId: 'a/big', displayName: 'Big Model', providerSource: 'openrouter', supportsVision: true }),
|
|
75
|
+
makeModel({ id: 'openrouter:a/small', modelId: 'a/small', displayName: 'Small Model', providerSource: 'openrouter', supportsVision: false }),
|
|
76
|
+
makeModel({ id: 'huggingface:b/chat', modelId: 'b/chat', displayName: 'Chat Model', providerSource: 'huggingface', supportsVision: false }),
|
|
77
|
+
]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should filter by source', () => {
|
|
81
|
+
const result = registry.getModels({ source: 'openrouter' });
|
|
82
|
+
expect(result).toHaveLength(2);
|
|
83
|
+
expect(result.every(m => m.providerSource === 'openrouter')).toBe(true);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should filter by vision support', () => {
|
|
87
|
+
const result = registry.getModels({ supportsVision: true });
|
|
88
|
+
expect(result).toHaveLength(1);
|
|
89
|
+
expect(result[0]!.displayName).toBe('Big Model');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should search by query', () => {
|
|
93
|
+
const result = registry.getModels({ query: 'chat' });
|
|
94
|
+
expect(result).toHaveLength(1);
|
|
95
|
+
expect(result[0]!.displayName).toBe('Chat Model');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should support limit and offset', () => {
|
|
99
|
+
const page1 = registry.getModels({ limit: 2, offset: 0 });
|
|
100
|
+
expect(page1).toHaveLength(2);
|
|
101
|
+
|
|
102
|
+
const page2 = registry.getModels({ limit: 2, offset: 2 });
|
|
103
|
+
expect(page2).toHaveLength(1);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('getModel', () => {
|
|
108
|
+
it('should return undefined for non-existent model', () => {
|
|
109
|
+
const result = registry.getModel('nonexistent');
|
|
110
|
+
expect(result).toBeUndefined();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('toModelCapabilities', () => {
|
|
115
|
+
it('should convert to ModelCapabilities format', () => {
|
|
116
|
+
registry.upsertModels([
|
|
117
|
+
makeModel({ id: 'openrouter:a/model', modelId: 'a/model', providerSource: 'openrouter' }),
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
const caps = registry.toModelCapabilities('openrouter');
|
|
121
|
+
expect(caps['a/model']).toBeDefined();
|
|
122
|
+
expect(caps['a/model']!.maxContextTokens).toBe(128000);
|
|
123
|
+
expect(caps['a/model']!.supportsVision).toBe(true);
|
|
124
|
+
expect(caps['a/model']!.isLocal).toBe(false);
|
|
125
|
+
expect(caps['a/model']!.strengths).toEqual(['reasoning', 'code']);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should exclude disabled models', () => {
|
|
129
|
+
registry.upsertModels([
|
|
130
|
+
makeModel({ id: 'openrouter:a/enabled', modelId: 'a/enabled', enabled: true }),
|
|
131
|
+
makeModel({ id: 'openrouter:a/disabled', modelId: 'a/disabled', enabled: false }),
|
|
132
|
+
]);
|
|
133
|
+
|
|
134
|
+
const caps = registry.toModelCapabilities('openrouter');
|
|
135
|
+
expect(caps['a/enabled']).toBeDefined();
|
|
136
|
+
expect(caps['a/disabled']).toBeUndefined();
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('search', () => {
|
|
141
|
+
it('should search by display name and model ID', () => {
|
|
142
|
+
registry.upsertModels([
|
|
143
|
+
makeModel({ id: 'or:anthropic/claude-3-opus', modelId: 'anthropic/claude-3-opus', displayName: 'Claude 3 Opus' }),
|
|
144
|
+
makeModel({ id: 'or:meta/llama-3', modelId: 'meta/llama-3', displayName: 'Llama 3' }),
|
|
145
|
+
]);
|
|
146
|
+
|
|
147
|
+
const results = registry.search('claude');
|
|
148
|
+
expect(results).toHaveLength(1);
|
|
149
|
+
expect(results[0]!.displayName).toBe('Claude 3 Opus');
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('getTrending', () => {
|
|
154
|
+
it('should return models sorted by trending score', () => {
|
|
155
|
+
registry.upsertModels([
|
|
156
|
+
makeModel({ id: 'hf:a', modelId: 'a', providerSource: 'huggingface', hfTrendingScore: 0.8 }),
|
|
157
|
+
makeModel({ id: 'hf:b', modelId: 'b', providerSource: 'huggingface', hfTrendingScore: 0.95 }),
|
|
158
|
+
makeModel({ id: 'hf:c', modelId: 'c', providerSource: 'huggingface', hfTrendingScore: 0.5 }),
|
|
159
|
+
]);
|
|
160
|
+
|
|
161
|
+
const trending = registry.getTrending(2);
|
|
162
|
+
expect(trending).toHaveLength(2);
|
|
163
|
+
expect(trending[0]!.modelId).toBe('b');
|
|
164
|
+
expect(trending[1]!.modelId).toBe('a');
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should exclude models without trending score', () => {
|
|
168
|
+
registry.upsertModels([
|
|
169
|
+
makeModel({ id: 'or:x', modelId: 'x', providerSource: 'openrouter' }),
|
|
170
|
+
makeModel({ id: 'hf:y', modelId: 'y', providerSource: 'huggingface', hfTrendingScore: 0.5 }),
|
|
171
|
+
]);
|
|
172
|
+
|
|
173
|
+
const trending = registry.getTrending();
|
|
174
|
+
expect(trending).toHaveLength(1);
|
|
175
|
+
expect(trending[0]!.modelId).toBe('y');
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('pruneStale', () => {
|
|
180
|
+
it('should delete models older than maxAge', () => {
|
|
181
|
+
const old = Date.now() - 10 * 24 * 60 * 60 * 1000; // 10 days ago
|
|
182
|
+
const recent = Date.now();
|
|
183
|
+
|
|
184
|
+
registry.upsertModels([
|
|
185
|
+
makeModel({ id: 'or:old', modelId: 'old', lastRefreshedAt: old }),
|
|
186
|
+
makeModel({ id: 'or:new', modelId: 'new', lastRefreshedAt: recent }),
|
|
187
|
+
]);
|
|
188
|
+
|
|
189
|
+
const pruned = registry.pruneStale(7 * 24 * 60 * 60 * 1000); // 7 days
|
|
190
|
+
expect(pruned).toBe(1);
|
|
191
|
+
|
|
192
|
+
const remaining = registry.getModels();
|
|
193
|
+
expect(remaining).toHaveLength(1);
|
|
194
|
+
expect(remaining[0]!.modelId).toBe('new');
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe('setEnabled', () => {
|
|
199
|
+
it('should disable and re-enable a model', () => {
|
|
200
|
+
registry.upsertModels([makeModel()]);
|
|
201
|
+
|
|
202
|
+
registry.setEnabled('openrouter:test/model-1', false);
|
|
203
|
+
let model = registry.getModel('openrouter:test/model-1');
|
|
204
|
+
expect(model!.enabled).toBe(false);
|
|
205
|
+
|
|
206
|
+
registry.setEnabled('openrouter:test/model-1', true);
|
|
207
|
+
model = registry.getModel('openrouter:test/model-1');
|
|
208
|
+
expect(model!.enabled).toBe(true);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('HuggingFace-specific fields', () => {
|
|
213
|
+
it('should persist and retrieve HF metadata', () => {
|
|
214
|
+
registry.upsertModels([
|
|
215
|
+
makeModel({
|
|
216
|
+
id: 'hf:meta/llama',
|
|
217
|
+
modelId: 'meta/llama',
|
|
218
|
+
providerSource: 'huggingface',
|
|
219
|
+
hfModelCard: '# Llama\n\nA great model.',
|
|
220
|
+
hfDownloads: 1500000,
|
|
221
|
+
hfLikes: 5000,
|
|
222
|
+
hfTrendingScore: 0.92,
|
|
223
|
+
hfTags: ['transformers', 'pytorch', 'llama'],
|
|
224
|
+
hfBenchmarkScores: { mmlu: 0.82, humaneval: 0.71 },
|
|
225
|
+
hfInferenceProviders: ['together', 'fireworks-ai'],
|
|
226
|
+
}),
|
|
227
|
+
]);
|
|
228
|
+
|
|
229
|
+
const model = registry.getModel('hf:meta/llama')!;
|
|
230
|
+
expect(model.hfModelCard).toBe('# Llama\n\nA great model.');
|
|
231
|
+
expect(model.hfDownloads).toBe(1500000);
|
|
232
|
+
expect(model.hfLikes).toBe(5000);
|
|
233
|
+
expect(model.hfTrendingScore).toBe(0.92);
|
|
234
|
+
expect(model.hfTags).toEqual(['transformers', 'pytorch', 'llama']);
|
|
235
|
+
expect(model.hfBenchmarkScores).toEqual({ mmlu: 0.82, humaneval: 0.71 });
|
|
236
|
+
expect(model.hfInferenceProviders).toEqual(['together', 'fireworks-ai']);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
});
|