@qvac/registry-client 0.1.4
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 +202 -0
- package/README.md +291 -0
- package/bin/cli.js +208 -0
- package/index.d.ts +84 -0
- package/index.js +7 -0
- package/lib/client.js +322 -0
- package/lib/config.js +46 -0
- package/lib/logger.js +45 -0
- package/package.json +58 -0
- package/utils/env.js +12 -0
- package/utils/error.js +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
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 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 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 those 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
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# @tetherto/qvac-lib-registry-client
|
|
2
|
+
|
|
3
|
+
Read-only client library for querying the QVAC Registry. It replicates the registry HyperDB via Hyperswarm and provides APIs for searching and retrieving model information.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Establishes replication connection to the QVAC Registry swarm.
|
|
8
|
+
- Supports querying specific models by path, engine, name, and quantization.
|
|
9
|
+
- Downloads actual model files from Hyperblobs cores via blind peers.
|
|
10
|
+
- Provides indexed search methods for efficient lookups.
|
|
11
|
+
- Graceful disconnection from the swarm.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @tetherto/qvac-lib-registry-client
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Ensure the registry core key is available via environment variables or provided via options.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Basic Example
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
'use strict'
|
|
27
|
+
const { QVACRegistryClient } = require('@tetherto/qvac-lib-registry-client')
|
|
28
|
+
|
|
29
|
+
async function main () {
|
|
30
|
+
const client = new QVACRegistryClient({
|
|
31
|
+
registryCoreKey: process.env.QVAC_REGISTRY_CORE_KEY
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const models = await client.findModels({})
|
|
35
|
+
console.log('All models:', models)
|
|
36
|
+
|
|
37
|
+
if (models.length > 0) {
|
|
38
|
+
const model = await client.getModel(models[0].path, models[0].source)
|
|
39
|
+
console.log('Model:', model)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
await client.close()
|
|
43
|
+
}
|
|
44
|
+
main().catch(console.error)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Available Methods
|
|
48
|
+
|
|
49
|
+
#### Lifecycle
|
|
50
|
+
|
|
51
|
+
- `ready()`: Initialize client and connect to registry. Triggered from constructor asynchronously.
|
|
52
|
+
- `close()`: Close connection to registry and corestore.
|
|
53
|
+
|
|
54
|
+
#### Metadata Queries
|
|
55
|
+
|
|
56
|
+
- `getModel(path, source)`: Retrieves a specific model's metadata by path and source.
|
|
57
|
+
- `findModels(query)`: General search with filters. Supports path prefix queries for finding model shards.
|
|
58
|
+
- `findModelsByEngine(query)`: Searches models by engine (indexed).
|
|
59
|
+
- `findModelsByName(query)`: Searches models by name (indexed).
|
|
60
|
+
- `findModelsByQuantization(query)`: Searches models by quantization (indexed).
|
|
61
|
+
|
|
62
|
+
Query format for range queries:
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
// Exact match
|
|
66
|
+
const models = await client.findModelsByEngine({
|
|
67
|
+
gte: { engine: '@qvac/transcription-whispercpp' },
|
|
68
|
+
lte: { engine: '@qvac/transcription-whispercpp' }
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
// Prefix match (all models with path starting with 'hf/')
|
|
72
|
+
const hfModels = await client.findModels({
|
|
73
|
+
gte: { path: 'hf/' },
|
|
74
|
+
lte: { path: 'hf/\uffff' }
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### File Downloads
|
|
79
|
+
|
|
80
|
+
- `downloadModel(path, source, options)`: Downloads actual model file from Hyperblobs cores.
|
|
81
|
+
- `path`: Registry path of the model (e.g., 'hf/model.gguf')
|
|
82
|
+
- `source`: Source identifier (e.g., 'hf')
|
|
83
|
+
- `options`:
|
|
84
|
+
- `timeout`: Download timeout in ms (default: 30000)
|
|
85
|
+
- `outputFile`: Optional file path to save directly to disk
|
|
86
|
+
- Returns: `{ model: QVACModelEntry, artifact: QVACDownloadedArtifact }`
|
|
87
|
+
- If `outputFile` provided: `artifact = { path: '/path/to/file' }`
|
|
88
|
+
- Otherwise: `artifact = { stream: ReadableStream }`
|
|
89
|
+
|
|
90
|
+
Example - Download to file:
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
const result = await client.downloadModel('hf/ggml-tiny.bin', 'hf', {
|
|
94
|
+
outputFile: './downloaded/whisper-tiny.ggml',
|
|
95
|
+
timeout: 60000
|
|
96
|
+
})
|
|
97
|
+
console.log('Downloaded to:', result.artifact.path)
|
|
98
|
+
console.log('Model metadata:', result.model)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Example - Download as stream:
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
const result = await client.downloadModel('hf/ggml-tiny.bin', 'hf')
|
|
105
|
+
const fs = require('fs')
|
|
106
|
+
result.artifact.stream.pipe(fs.createWriteStream('./model.ggml'))
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## CLI
|
|
110
|
+
|
|
111
|
+
The package includes a CLI for querying and downloading models from the registry.
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
npx qvac-registry --help
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### List models
|
|
118
|
+
|
|
119
|
+
By default, `list` prints a compact table with path, source, quantization, and params:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
$ qvac-registry list
|
|
123
|
+
Found 195 model(s)
|
|
124
|
+
|
|
125
|
+
PATH SOURCE QUANT PARAMS
|
|
126
|
+
BSC-LT/salamandraTA-2B-instruct-GGUF/blob/60046856fcac87c47fb0c706e994e70f01eda62b/salamandrata_2b_inst_q4.gguf hf q4 2B
|
|
127
|
+
Qwen/Qwen3-8B-GGUF/blob/main/Qwen3-8B-Q4_K_M.gguf hf q4_k_m 8B
|
|
128
|
+
ggerganov/whisper.cpp/resolve/5359861c739e955e79d9a303bcbc70fb988958b1/ggml-tiny-q8_0.bin hf q8_0
|
|
129
|
+
...
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Use `--full` for detailed output per model:
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
$ qvac-registry list --full
|
|
136
|
+
Found 195 model(s)
|
|
137
|
+
|
|
138
|
+
Qwen/Qwen3-8B-GGUF/blob/main/Qwen3-8B-Q4_K_M.gguf
|
|
139
|
+
source: hf
|
|
140
|
+
engine: @qvac/llm-llamacpp
|
|
141
|
+
quantization: q4_k_m
|
|
142
|
+
params: 8B
|
|
143
|
+
size: 4.68 GB
|
|
144
|
+
license: Apache-2.0
|
|
145
|
+
sha256: d98cdcbd03e17ce47681435b5150e34c1417f50b5c0019dd560e4882c5745785
|
|
146
|
+
...
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Filter models
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# By engine
|
|
153
|
+
$ qvac-registry list --engine @qvac/transcription-whispercpp
|
|
154
|
+
|
|
155
|
+
# By quantization
|
|
156
|
+
$ qvac-registry list -q q4_k_m
|
|
157
|
+
|
|
158
|
+
# By name (case-sensitive prefix match on filename)
|
|
159
|
+
$ qvac-registry list -n Qwen3
|
|
160
|
+
|
|
161
|
+
# Combined
|
|
162
|
+
$ qvac-registry list -e @qvac/llm-llamacpp -q q4_k_m --full
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Get a specific model
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
$ qvac-registry get \
|
|
169
|
+
"ggerganov/whisper.cpp/resolve/5359861c739e955e79d9a303bcbc70fb988958b1/ggml-tiny-q8_0.bin" hf
|
|
170
|
+
|
|
171
|
+
ggerganov/whisper.cpp/resolve/5359861c739e955e79d9a303bcbc70fb988958b1/ggml-tiny-q8_0.bin
|
|
172
|
+
source: hf
|
|
173
|
+
engine: @qvac/transcription-whispercpp
|
|
174
|
+
quantization: q8_0
|
|
175
|
+
size: 41.52 MB
|
|
176
|
+
license: MIT
|
|
177
|
+
sha256: ...
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Download a model
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
$ qvac-registry download \
|
|
184
|
+
"ggerganov/whisper.cpp/resolve/5359861c739e955e79d9a303bcbc70fb988958b1/ggml-tiny-q8_0.bin" hf \
|
|
185
|
+
--output ./ggml-tiny-q8_0.bin
|
|
186
|
+
Downloading ... -> /absolute/path/ggml-tiny-q8_0.bin
|
|
187
|
+
Download complete: 41.52 MB
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### JSON output
|
|
191
|
+
|
|
192
|
+
All commands support `--json` for machine-readable output:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
$ qvac-registry list --engine @qvac/transcription-whispercpp --json | jq '.[0].path'
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Global flags
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
--key|-k [key] Registry core key (overrides QVAC_REGISTRY_CORE_KEY env)
|
|
202
|
+
--storage|-s [path] Client storage path
|
|
203
|
+
--verbose|-v Enable verbose/debug logging
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Examples
|
|
207
|
+
|
|
208
|
+
See the `examples/` folder for complete working examples:
|
|
209
|
+
|
|
210
|
+
- `example.js`: List models, query by engine/name/quantization, find shards
|
|
211
|
+
- `download-model.js`: Download a single model to disk
|
|
212
|
+
- `download-all-models.js`: Download all models in the registry
|
|
213
|
+
|
|
214
|
+
Run examples:
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
cd client
|
|
218
|
+
node examples/example.js
|
|
219
|
+
node examples/download-model.js
|
|
220
|
+
node examples/download-all-models.js
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Configuration
|
|
224
|
+
|
|
225
|
+
- `storage`: Path for local storage (defaults to temporary directory in `os.tmpdir()`, or `REGISTRY_STORAGE` env var if set).
|
|
226
|
+
- `registryCoreKey`: z-base-32 or hex string of the hypercore key (defaults to `QVAC_REGISTRY_CORE_KEY` env var).
|
|
227
|
+
- `logger`: Logger configuration object
|
|
228
|
+
- `level`: Log level ('debug', 'info', 'warn', 'error')
|
|
229
|
+
- `name`: Logger name for identification
|
|
230
|
+
|
|
231
|
+
### Storage Configuration
|
|
232
|
+
|
|
233
|
+
By default, the client uses a temporary directory created in `os.tmpdir()` with a unique name. Temporary storage is used for replicating registry metadata and is separate from downloaded model files. The temporary storage is not automatically cleaned up on exit and should be manually removed if needed. If you need persistent storage or want to control the location:
|
|
234
|
+
|
|
235
|
+
```javascript
|
|
236
|
+
const client = new QVACRegistryClient({
|
|
237
|
+
registryCoreKey: process.env.QVAC_REGISTRY_CORE_KEY,
|
|
238
|
+
storage: '/path/to/persistent/storage'
|
|
239
|
+
})
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Environment Variables
|
|
243
|
+
|
|
244
|
+
- `QVAC_REGISTRY_CORE_KEY`: Core key for accessing the registry hypercore
|
|
245
|
+
- `REGISTRY_STORAGE`: Path for local storage (optional, overrides temporary directory default)
|
|
246
|
+
|
|
247
|
+
## Error Handling
|
|
248
|
+
|
|
249
|
+
The client uses custom error codes in the range 19001-20000. All errors extend `QvacErrorRegistryClient`.
|
|
250
|
+
|
|
251
|
+
### Error Codes
|
|
252
|
+
|
|
253
|
+
| Code | Name | Description | When Thrown |
|
|
254
|
+
|------|------|-------------|-------------|
|
|
255
|
+
| 19001 | FAILED_TO_CONNECT | Connection to registry failed | Missing core key, network issues during initialization |
|
|
256
|
+
| 19002 | FAILED_TO_CLOSE | Failed to close registry cleanly | Resource cleanup errors during shutdown |
|
|
257
|
+
| 19003 | MODEL_NOT_FOUND | Model not found or invalid | Model doesn't exist or missing blob binding |
|
|
258
|
+
|
|
259
|
+
### Error Handling Example
|
|
260
|
+
|
|
261
|
+
```javascript
|
|
262
|
+
const { QVACRegistryClient } = require('@tetherto/qvac-lib-registry-client')
|
|
263
|
+
const { QvacErrorRegistryClient } = require('@tetherto/qvac-lib-registry-client/utils/error')
|
|
264
|
+
|
|
265
|
+
async function handleErrors () {
|
|
266
|
+
const client = new QVACRegistryClient({
|
|
267
|
+
registryCoreKey: process.env.QVAC_REGISTRY_CORE_KEY
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
try {
|
|
271
|
+
const model = await client.getModel('hf/model-name.Q4_K_M.gguf', 'hf')
|
|
272
|
+
if (!model) {
|
|
273
|
+
console.log('Model not found')
|
|
274
|
+
} else {
|
|
275
|
+
console.log('Model found:', model)
|
|
276
|
+
}
|
|
277
|
+
} catch (error) {
|
|
278
|
+
if (error instanceof QvacErrorRegistryClient) {
|
|
279
|
+
console.error(`Registry error [${error.code}]:`, error.message)
|
|
280
|
+
} else {
|
|
281
|
+
console.error('Unexpected error:', error)
|
|
282
|
+
}
|
|
283
|
+
} finally {
|
|
284
|
+
await client.close()
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## License
|
|
290
|
+
|
|
291
|
+
Apache-2.0
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
const { command, flag, arg, summary, header, footer, description } = require('paparam')
|
|
5
|
+
const { QVACRegistryClient } = require('../index')
|
|
6
|
+
const IdEnc = require('hypercore-id-encoding')
|
|
7
|
+
const path = require('path')
|
|
8
|
+
|
|
9
|
+
const VERSION = require('../package.json').version
|
|
10
|
+
|
|
11
|
+
// --- Helpers ---
|
|
12
|
+
|
|
13
|
+
function createClient (parentFlags) {
|
|
14
|
+
const opts = {}
|
|
15
|
+
if (parentFlags.key) opts.registryCoreKey = parentFlags.key
|
|
16
|
+
if (parentFlags.storage) opts.storage = parentFlags.storage
|
|
17
|
+
if (parentFlags.verbose) {
|
|
18
|
+
opts.logger = { level: 'debug', enabled: true }
|
|
19
|
+
} else {
|
|
20
|
+
opts.logger = { enabled: false }
|
|
21
|
+
}
|
|
22
|
+
return new QVACRegistryClient(opts)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getRootFlags (cmd) {
|
|
26
|
+
let current = cmd
|
|
27
|
+
while (current.parent) current = current.parent
|
|
28
|
+
return current.flags || {}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function formatSize (bytes) {
|
|
32
|
+
if (!bytes) return 'N/A'
|
|
33
|
+
if (bytes >= 1024 * 1024 * 1024) return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB'
|
|
34
|
+
if (bytes >= 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(2) + ' MB'
|
|
35
|
+
if (bytes >= 1024) return (bytes / 1024).toFixed(2) + ' KB'
|
|
36
|
+
return bytes + ' B'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function printModelCompact (model) {
|
|
40
|
+
const parts = [model.path, model.source]
|
|
41
|
+
if (model.quantization) parts.push(model.quantization)
|
|
42
|
+
if (model.params) parts.push(model.params)
|
|
43
|
+
console.log(parts.join('\t'))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function printModelFull (model, opts = {}) {
|
|
47
|
+
const blob = model.blobBinding || {}
|
|
48
|
+
const coreKey = blob.coreKey ? IdEnc.normalize(blob.coreKey) : 'N/A'
|
|
49
|
+
|
|
50
|
+
console.log(` ${model.path}`)
|
|
51
|
+
console.log(` source: ${model.source}`)
|
|
52
|
+
console.log(` engine: ${model.engine}`)
|
|
53
|
+
if (model.name) console.log(` name: ${model.name}`)
|
|
54
|
+
if (model.quantization) console.log(` quantization: ${model.quantization}`)
|
|
55
|
+
if (model.params) console.log(` params: ${model.params}`)
|
|
56
|
+
console.log(` size: ${formatSize(blob.byteLength || model.sizeBytes)}`)
|
|
57
|
+
if (model.licenseId || model.license) console.log(` license: ${model.licenseId || model.license}`)
|
|
58
|
+
if (blob.sha256 || model.sha256) console.log(` sha256: ${blob.sha256 || model.sha256}`)
|
|
59
|
+
if (opts.verbose && model.description) console.log(` description: ${model.description}`)
|
|
60
|
+
if (opts.verbose) console.log(` blob core: ${coreKey}`)
|
|
61
|
+
if (model.deprecated) console.log(' deprecated: true')
|
|
62
|
+
console.log()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function withClient (cmd, fn) {
|
|
66
|
+
const rootFlags = getRootFlags(cmd)
|
|
67
|
+
const client = createClient(rootFlags)
|
|
68
|
+
try {
|
|
69
|
+
await fn(client, rootFlags)
|
|
70
|
+
} finally {
|
|
71
|
+
await client.close()
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// --- Commands ---
|
|
76
|
+
|
|
77
|
+
const listCmd = command('list',
|
|
78
|
+
summary('List models from the registry'),
|
|
79
|
+
description('List all models or filter by name, engine, or quantization.'),
|
|
80
|
+
flag('--name|-n [name]', 'Filter by model name (partial match)'),
|
|
81
|
+
flag('--engine|-e [engine]', 'Filter by engine (exact match)'),
|
|
82
|
+
flag('--quantization|-q [quantization]', 'Filter by quantization (partial match)'),
|
|
83
|
+
flag('--full', 'Show full model details'),
|
|
84
|
+
flag('--include-deprecated', 'Include deprecated models'),
|
|
85
|
+
flag('--json', 'Output as JSON'),
|
|
86
|
+
async function (cmd) {
|
|
87
|
+
await withClient(cmd, async (client, rootFlags) => {
|
|
88
|
+
const { flags } = cmd
|
|
89
|
+
const params = {}
|
|
90
|
+
if (flags.name) params.name = flags.name
|
|
91
|
+
if (flags.engine) params.engine = flags.engine
|
|
92
|
+
if (flags.quantization) params.quantization = flags.quantization
|
|
93
|
+
if (flags.includeDeprecated) params.includeDeprecated = true
|
|
94
|
+
|
|
95
|
+
const hasFilters = flags.name || flags.engine || flags.quantization
|
|
96
|
+
let models
|
|
97
|
+
|
|
98
|
+
if (hasFilters) {
|
|
99
|
+
models = await client.findBy(params)
|
|
100
|
+
} else {
|
|
101
|
+
models = await client.findModels({}, { includeDeprecated: !!flags.includeDeprecated })
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (flags.json) {
|
|
105
|
+
const out = models.map(m => ({
|
|
106
|
+
...m,
|
|
107
|
+
blobBinding: m.blobBinding
|
|
108
|
+
? { ...m.blobBinding, coreKey: m.blobBinding.coreKey ? IdEnc.normalize(m.blobBinding.coreKey) : undefined }
|
|
109
|
+
: undefined
|
|
110
|
+
}))
|
|
111
|
+
console.log(JSON.stringify(out, null, 2))
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
console.log(`Found ${models.length} model(s)\n`)
|
|
116
|
+
|
|
117
|
+
if (flags.full) {
|
|
118
|
+
for (const m of models) {
|
|
119
|
+
printModelFull(m, { verbose: rootFlags.verbose })
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
console.log(['PATH', 'SOURCE', 'QUANT', 'PARAMS'].join('\t'))
|
|
123
|
+
for (const m of models) {
|
|
124
|
+
printModelCompact(m)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
const getCmd = command('get',
|
|
132
|
+
summary('Get a specific model by path and source'),
|
|
133
|
+
arg('<path>', 'Model path (e.g. hf/org/repo/file.gguf)'),
|
|
134
|
+
arg('<source>', 'Model source (e.g. hf, s3)'),
|
|
135
|
+
flag('--json', 'Output as JSON'),
|
|
136
|
+
async function (cmd) {
|
|
137
|
+
await withClient(cmd, async (client, rootFlags) => {
|
|
138
|
+
const modelPath = cmd.args.path
|
|
139
|
+
const source = cmd.args.source
|
|
140
|
+
|
|
141
|
+
const model = await client.getModel(modelPath, source)
|
|
142
|
+
if (!model) {
|
|
143
|
+
console.error(`Model not found: ${modelPath} (source: ${source})`)
|
|
144
|
+
process.exit(1)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (cmd.flags.json) {
|
|
148
|
+
const out = {
|
|
149
|
+
...model,
|
|
150
|
+
blobBinding: model.blobBinding
|
|
151
|
+
? { ...model.blobBinding, coreKey: model.blobBinding.coreKey ? IdEnc.normalize(model.blobBinding.coreKey) : undefined }
|
|
152
|
+
: undefined
|
|
153
|
+
}
|
|
154
|
+
console.log(JSON.stringify(out, null, 2))
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
printModelFull(model, { verbose: rootFlags.verbose })
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
const downloadCmd = command('download',
|
|
164
|
+
summary('Download a model from the registry'),
|
|
165
|
+
arg('<path>', 'Model path'),
|
|
166
|
+
arg('<source>', 'Model source (e.g. hf, s3)'),
|
|
167
|
+
flag('--output|-o <file>', 'Output file path (required)'),
|
|
168
|
+
flag('--timeout|-t [ms]', 'Download timeout in ms (default: 30000)'),
|
|
169
|
+
async function (cmd) {
|
|
170
|
+
await withClient(cmd, async (client) => {
|
|
171
|
+
const { flags } = cmd
|
|
172
|
+
const modelPath = cmd.args.path
|
|
173
|
+
const source = cmd.args.source
|
|
174
|
+
|
|
175
|
+
if (!flags.output) {
|
|
176
|
+
console.error('--output|-o is required')
|
|
177
|
+
process.exit(1)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const outputFile = path.resolve(flags.output)
|
|
181
|
+
const opts = { outputFile }
|
|
182
|
+
if (flags.timeout) opts.timeout = parseInt(flags.timeout, 10)
|
|
183
|
+
|
|
184
|
+
console.log(`Downloading ${modelPath} (${source}) -> ${outputFile}`)
|
|
185
|
+
|
|
186
|
+
const result = await client.downloadModel(modelPath, source, opts)
|
|
187
|
+
const size = result.model.blobBinding?.byteLength || 0
|
|
188
|
+
|
|
189
|
+
console.log(`Download complete: ${formatSize(size)}`)
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
// --- Root command ---
|
|
195
|
+
|
|
196
|
+
const cmd = command('qvac-registry',
|
|
197
|
+
header(`qvac-registry v${VERSION}`),
|
|
198
|
+
summary('QVAC Model Registry CLI'),
|
|
199
|
+
footer('Set QVAC_REGISTRY_CORE_KEY env var or use --key to specify the registry core key.'),
|
|
200
|
+
flag('--key|-k [key]', 'Registry core key (overrides QVAC_REGISTRY_CORE_KEY env)'),
|
|
201
|
+
flag('--storage|-s [path]', 'Client storage path'),
|
|
202
|
+
flag('--verbose|-v', 'Enable verbose/debug logging'),
|
|
203
|
+
listCmd,
|
|
204
|
+
getCmd,
|
|
205
|
+
downloadCmd
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
cmd.parse()
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export interface QVACBlobBinding {
|
|
2
|
+
coreKey: Buffer
|
|
3
|
+
blockOffset: number
|
|
4
|
+
blockLength: number
|
|
5
|
+
byteOffset: number
|
|
6
|
+
byteLength: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface QVACModelEntry {
|
|
10
|
+
path: string
|
|
11
|
+
source: string
|
|
12
|
+
engine: string
|
|
13
|
+
license: string
|
|
14
|
+
name: string
|
|
15
|
+
sizeBytes: number
|
|
16
|
+
sha256: string
|
|
17
|
+
quantization?: string
|
|
18
|
+
params?: string
|
|
19
|
+
description?: string
|
|
20
|
+
notes?: string
|
|
21
|
+
tags?: string[]
|
|
22
|
+
blobBinding: QVACBlobBinding
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface QVACDownloadedArtifactStream {
|
|
26
|
+
stream: NodeJS.ReadableStream
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface QVACDownloadedArtifactPath {
|
|
30
|
+
path: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type QVACDownloadedArtifact = QVACDownloadedArtifactStream | QVACDownloadedArtifactPath
|
|
34
|
+
|
|
35
|
+
export interface QVACDownloadResult {
|
|
36
|
+
model: QVACModelEntry
|
|
37
|
+
artifact: QVACDownloadedArtifact
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface QVACDownloadOptions {
|
|
41
|
+
timeout?: number
|
|
42
|
+
peerTimeout?: number
|
|
43
|
+
outputFile?: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface QVACRegistryClientOptions {
|
|
47
|
+
storage?: string
|
|
48
|
+
registryCoreKey?: string
|
|
49
|
+
logger?: any
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface QVACModelQuery {
|
|
53
|
+
gte?: Record<string, any>
|
|
54
|
+
lte?: Record<string, any>
|
|
55
|
+
gt?: Record<string, any>
|
|
56
|
+
lt?: Record<string, any>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface FindByParams {
|
|
60
|
+
/** Filter by name (partial match, case-insensitive) */
|
|
61
|
+
name?: string
|
|
62
|
+
/** Filter by engine (exact match) */
|
|
63
|
+
engine?: string
|
|
64
|
+
/** Filter by quantization (partial match, case-insensitive) */
|
|
65
|
+
quantization?: string
|
|
66
|
+
/** Include deprecated models (default: false) */
|
|
67
|
+
includeDeprecated?: boolean
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class QVACRegistryClient {
|
|
71
|
+
constructor (opts?: QVACRegistryClientOptions)
|
|
72
|
+
|
|
73
|
+
ready (): Promise<void>
|
|
74
|
+
close (): Promise<void>
|
|
75
|
+
|
|
76
|
+
getModel (path: string, source: string): Promise<QVACModelEntry | null>
|
|
77
|
+
downloadModel (path: string, source: string, options?: QVACDownloadOptions): Promise<QVACDownloadResult>
|
|
78
|
+
|
|
79
|
+
findBy (params?: FindByParams): Promise<QVACModelEntry[]>
|
|
80
|
+
findModels (query?: QVACModelQuery): Promise<QVACModelEntry[]>
|
|
81
|
+
findModelsByEngine (query?: QVACModelQuery): Promise<QVACModelEntry[]>
|
|
82
|
+
findModelsByName (query?: QVACModelQuery): Promise<QVACModelEntry[]>
|
|
83
|
+
findModelsByQuantization (query?: QVACModelQuery): Promise<QVACModelEntry[]>
|
|
84
|
+
}
|
package/index.js
ADDED
package/lib/client.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { QvacErrorRegistryClient, ERR_CODES } = require('../utils/error')
|
|
4
|
+
const RegistryConfig = require('./config')
|
|
5
|
+
const { RegistryDatabase } = require('@qvac/registry-schema')
|
|
6
|
+
const ReadyResource = require('ready-resource')
|
|
7
|
+
const Logger = require('./logger')
|
|
8
|
+
const Corestore = require('corestore')
|
|
9
|
+
const Hyperswarm = require('hyperswarm')
|
|
10
|
+
const Hyperblobs = require('hyperblobs')
|
|
11
|
+
const IdEnc = require('hypercore-id-encoding')
|
|
12
|
+
const path = require('path')
|
|
13
|
+
const fs = require('fs')
|
|
14
|
+
|
|
15
|
+
class QVACRegistryClient extends ReadyResource {
|
|
16
|
+
constructor (opts = {}) {
|
|
17
|
+
super()
|
|
18
|
+
|
|
19
|
+
this.logger = new Logger(opts.logger)
|
|
20
|
+
this.registryConfig = new RegistryConfig({ logger: this.logger })
|
|
21
|
+
|
|
22
|
+
this.db = null
|
|
23
|
+
this.corestore = null
|
|
24
|
+
this.hyperswarm = null
|
|
25
|
+
this._connectionHandler = null
|
|
26
|
+
|
|
27
|
+
this.storage = this.registryConfig.getRegistryStorage(opts.storage)
|
|
28
|
+
this.registryCoreKey = this.registryConfig.getRegistryCoreKey(opts.registryCoreKey)
|
|
29
|
+
|
|
30
|
+
this.logger.debug('Initializing QVAC Registry Client', {
|
|
31
|
+
mode: 'read',
|
|
32
|
+
registryCoreKey: opts.registryCoreKey ? '***' : 'not provided'
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
this.ready()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async _open () {
|
|
39
|
+
this.logger.debug('_open called')
|
|
40
|
+
|
|
41
|
+
if (!this.registryCoreKey) {
|
|
42
|
+
this.logger.error('Missing registry core key for read mode')
|
|
43
|
+
throw new QvacErrorRegistryClient({ code: ERR_CODES.FAILED_TO_CONNECT, adds: 'Missing registry core key. Set QVAC_REGISTRY_CORE_KEY environment variable.' })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.logger.debug('Creating corestore for open')
|
|
47
|
+
this.corestore = new Corestore(this.storage)
|
|
48
|
+
await this.corestore.ready()
|
|
49
|
+
|
|
50
|
+
this.logger.debug('Creating Hyperswarm for open')
|
|
51
|
+
this.hyperswarm = new Hyperswarm()
|
|
52
|
+
|
|
53
|
+
this._connectionHandler = (conn, peerInfo) => {
|
|
54
|
+
this.logger.debug('Client peer connected', { peer: IdEnc.normalize(peerInfo.publicKey) })
|
|
55
|
+
this.corestore.replicate(conn)
|
|
56
|
+
}
|
|
57
|
+
this.hyperswarm.on('connection', this._connectionHandler)
|
|
58
|
+
|
|
59
|
+
const viewKey = IdEnc.decode(this.registryCoreKey)
|
|
60
|
+
const viewCore = this.corestore.get({ key: viewKey })
|
|
61
|
+
await viewCore.ready()
|
|
62
|
+
|
|
63
|
+
this.logger.debug('Joining hyperswarm for registry view', {
|
|
64
|
+
viewKey: IdEnc.normalize(viewCore.key),
|
|
65
|
+
discoveryKey: IdEnc.normalize(viewCore.discoveryKey)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
this.hyperswarm.join(viewCore.discoveryKey, { client: true, server: false })
|
|
69
|
+
await this.hyperswarm.flush()
|
|
70
|
+
|
|
71
|
+
this.logger.debug('Waiting for view core sync')
|
|
72
|
+
await viewCore.update({ wait: true })
|
|
73
|
+
|
|
74
|
+
this.logger.debug('Creating RegistryDatabase from view core')
|
|
75
|
+
this.db = new RegistryDatabase(viewCore, { extension: false })
|
|
76
|
+
await this.db.ready()
|
|
77
|
+
|
|
78
|
+
this.logger.debug('QVACRegistryClient ready', {
|
|
79
|
+
length: viewCore.length
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async getModel (path, source) {
|
|
84
|
+
this._validateString(path, 'path')
|
|
85
|
+
this._validateString(source, 'source')
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
this.logger.info('Getting model', { path, source })
|
|
89
|
+
await this.ready()
|
|
90
|
+
|
|
91
|
+
const result = await this.db.getModel(path, source)
|
|
92
|
+
this.logger.debug('getModel result', { result })
|
|
93
|
+
return result ?? null
|
|
94
|
+
} catch (error) {
|
|
95
|
+
this.logger.error('Error getting model', error)
|
|
96
|
+
throw error
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async findModels (query = {}, opts = {}) {
|
|
101
|
+
await this.ready()
|
|
102
|
+
const { includeDeprecated = false } = opts
|
|
103
|
+
this.logger.debug('findModels called', { query, includeDeprecated })
|
|
104
|
+
|
|
105
|
+
let models = await this.db.findModelsByPath(query).toArray()
|
|
106
|
+
|
|
107
|
+
if (!includeDeprecated) {
|
|
108
|
+
models = models.filter(m => !m.deprecated)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return models
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async findModelsByEngine (query = {}) {
|
|
115
|
+
await this.ready()
|
|
116
|
+
this.logger.debug('findModelsByEngine called', { query })
|
|
117
|
+
return this.db.findModelsByEngine(query).toArray()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async findModelsByName (query = {}) {
|
|
121
|
+
await this.ready()
|
|
122
|
+
this.logger.debug('findModelsByName called', { query })
|
|
123
|
+
return this.db.findModelsByName(query).toArray()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async findModelsByQuantization (query = {}) {
|
|
127
|
+
await this.ready()
|
|
128
|
+
this.logger.debug('findModelsByQuantization called', { query })
|
|
129
|
+
return this.db.findModelsByQuantization(query).toArray()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Find models with optional filters.
|
|
134
|
+
* Uses the database's findBy method for efficient indexed queries.
|
|
135
|
+
* @param {Object} params - Filter parameters
|
|
136
|
+
* @param {string} [params.name] - Filter by name (partial match)
|
|
137
|
+
* @param {string} [params.engine] - Filter by engine (exact match)
|
|
138
|
+
* @param {string} [params.quantization] - Filter by quantization (partial match)
|
|
139
|
+
* @param {boolean} [params.includeDeprecated=false] - Include deprecated models
|
|
140
|
+
* @returns {Promise<Array>} Array of matching models
|
|
141
|
+
*/
|
|
142
|
+
async findBy (params = {}) {
|
|
143
|
+
await this.ready()
|
|
144
|
+
this.logger.debug('findBy called', { params })
|
|
145
|
+
return this.db.findBy(params)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
_validateString (value, name) {
|
|
149
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
150
|
+
throw new Error(`Invalid ${name}: ${value}`)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async _getBlobsCore (blobsCoreKey) {
|
|
155
|
+
const keyBuffer = Buffer.isBuffer(blobsCoreKey)
|
|
156
|
+
? blobsCoreKey
|
|
157
|
+
: Buffer.from(blobsCoreKey.data || blobsCoreKey, 'hex')
|
|
158
|
+
|
|
159
|
+
this.logger.debug('Creating hyperblobs core', {
|
|
160
|
+
blobCore: IdEnc.normalize(keyBuffer)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
const core = this.corestore.get({ key: keyBuffer })
|
|
164
|
+
await core.ready()
|
|
165
|
+
|
|
166
|
+
const blobs = new Hyperblobs(core)
|
|
167
|
+
await blobs.ready()
|
|
168
|
+
|
|
169
|
+
return { core, blobs }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async downloadModel (path, source, options = {}) {
|
|
173
|
+
this._validateString(path, 'path')
|
|
174
|
+
this._validateString(source, 'source')
|
|
175
|
+
|
|
176
|
+
if (options && typeof options !== 'object') {
|
|
177
|
+
throw new Error(`Invalid options: ${typeof options}`)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let core, blobs
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
this.logger.info('Downloading model', { path, source })
|
|
184
|
+
await this.ready()
|
|
185
|
+
|
|
186
|
+
const model = await this.getModel(path, source)
|
|
187
|
+
if (!model) {
|
|
188
|
+
throw new QvacErrorRegistryClient({ code: ERR_CODES.MODEL_NOT_FOUND, adds: `Model not found: ${path} (source: ${source})` })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!model.blobBinding || !model.blobBinding.coreKey) {
|
|
192
|
+
throw new QvacErrorRegistryClient({ code: ERR_CODES.MODEL_NOT_FOUND, adds: 'Model missing blob binding' })
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
this.logger.debug('Model metadata retrieved', { model })
|
|
196
|
+
|
|
197
|
+
const blobsCore = await this._getBlobsCore(model.blobBinding.coreKey)
|
|
198
|
+
core = blobsCore.core
|
|
199
|
+
blobs = blobsCore.blobs
|
|
200
|
+
|
|
201
|
+
this.logger.debug('Joining swarm for blobs core', {
|
|
202
|
+
discoveryKey: IdEnc.normalize(core.discoveryKey)
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
this.hyperswarm.join(core.discoveryKey, { client: true, server: false })
|
|
206
|
+
await this.hyperswarm.flush()
|
|
207
|
+
|
|
208
|
+
await core.update({ wait: true })
|
|
209
|
+
this.logger.debug('Blobs core updated')
|
|
210
|
+
|
|
211
|
+
let artifact
|
|
212
|
+
if (options.outputFile) {
|
|
213
|
+
await this._streamBlobToFile(blobs, model.blobBinding, options.outputFile, options)
|
|
214
|
+
artifact = { path: options.outputFile }
|
|
215
|
+
|
|
216
|
+
if (blobs) await blobs.close()
|
|
217
|
+
if (core) await core.close()
|
|
218
|
+
} else {
|
|
219
|
+
const stream = blobs.createReadStream(model.blobBinding, {
|
|
220
|
+
wait: true,
|
|
221
|
+
timeout: options.timeout || 30000
|
|
222
|
+
})
|
|
223
|
+
artifact = { stream }
|
|
224
|
+
|
|
225
|
+
const cleanup = async () => {
|
|
226
|
+
if (blobs) {
|
|
227
|
+
try {
|
|
228
|
+
await blobs.close()
|
|
229
|
+
} catch (cleanupError) {
|
|
230
|
+
this.logger.warn('Error closing blob instance', { error: cleanupError.message })
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (core) {
|
|
234
|
+
try {
|
|
235
|
+
await core.close()
|
|
236
|
+
} catch (cleanupError) {
|
|
237
|
+
this.logger.warn('Error closing blob core', { error: cleanupError.message })
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
this.logger.debug('Blob resources closed after stream end')
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
stream.on('close', cleanup)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
this.logger.info('Model downloaded successfully')
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
model,
|
|
250
|
+
artifact
|
|
251
|
+
}
|
|
252
|
+
} catch (error) {
|
|
253
|
+
this.logger.error('Error downloading model', error)
|
|
254
|
+
|
|
255
|
+
if (blobs) {
|
|
256
|
+
try {
|
|
257
|
+
await blobs.close()
|
|
258
|
+
} catch (cleanupError) {
|
|
259
|
+
this.logger.warn('Error closing blob instance on error', { error: cleanupError.message })
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (core) {
|
|
263
|
+
try {
|
|
264
|
+
await core.close()
|
|
265
|
+
} catch (cleanupError) {
|
|
266
|
+
this.logger.warn('Error closing blob core on error', { error: cleanupError.message })
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
throw error
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async _streamBlobToFile (blobs, blobPointer, filePath, options) {
|
|
275
|
+
const stream = blobs.createReadStream(blobPointer, {
|
|
276
|
+
wait: true,
|
|
277
|
+
timeout: options.timeout || 30000
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
const dir = path.dirname(filePath)
|
|
281
|
+
await fs.promises.mkdir(dir, { recursive: true })
|
|
282
|
+
|
|
283
|
+
const writeStream = fs.createWriteStream(filePath)
|
|
284
|
+
|
|
285
|
+
return new Promise((resolve, reject) => {
|
|
286
|
+
stream.pipe(writeStream)
|
|
287
|
+
writeStream.on('finish', resolve)
|
|
288
|
+
writeStream.on('error', reject)
|
|
289
|
+
stream.on('error', reject)
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async _close () {
|
|
294
|
+
this.logger.debug('_close called')
|
|
295
|
+
|
|
296
|
+
if (this.db) {
|
|
297
|
+
this.logger.debug('Closing database')
|
|
298
|
+
await this.db.close()
|
|
299
|
+
this.db = null
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (this.hyperswarm) {
|
|
303
|
+
if (this._connectionHandler) {
|
|
304
|
+
this.hyperswarm.off('connection', this._connectionHandler)
|
|
305
|
+
this._connectionHandler = null
|
|
306
|
+
}
|
|
307
|
+
this.logger.debug('Destroying hyperswarm')
|
|
308
|
+
await this.hyperswarm.destroy()
|
|
309
|
+
this.hyperswarm = null
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (this.corestore) {
|
|
313
|
+
this.logger.debug('Closing corestore')
|
|
314
|
+
await this.corestore.close()
|
|
315
|
+
this.corestore = null
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
this.logger.debug('QVACRegistryClient closed')
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
module.exports = QVACRegistryClient
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { getEnv } = require('../utils/env')
|
|
4
|
+
const { ENV_KEYS } = require('@qvac/registry-schema')
|
|
5
|
+
const Logger = require('./logger')
|
|
6
|
+
const os = require('os')
|
|
7
|
+
const path = require('path')
|
|
8
|
+
|
|
9
|
+
class RegistryConfig {
|
|
10
|
+
constructor (opts = {}) {
|
|
11
|
+
this.logger = new Logger(opts.logger)
|
|
12
|
+
this.logger.debug('RegistryConfig initialized', { opts })
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getRegistryStorage (providedPath) {
|
|
16
|
+
if (providedPath) {
|
|
17
|
+
this.logger.debug('getRegistryStorage called with providedPath', { providedPath })
|
|
18
|
+
return providedPath
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const envPath = getEnv(ENV_KEYS.REGISTRY_STORAGE)
|
|
22
|
+
const result = envPath || this._createTempStoragePath()
|
|
23
|
+
this.logger.debug('getRegistryStorage resolved', { envPath, result })
|
|
24
|
+
return result
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
_createTempStoragePath () {
|
|
28
|
+
const tmpBase = os.tmpdir()
|
|
29
|
+
const storageDir = path.join(tmpBase, `qvac-registry-client-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`)
|
|
30
|
+
this.logger.debug('Created temp storage path', { storageDir })
|
|
31
|
+
return storageDir
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getRegistryCoreKey (providedKey) {
|
|
35
|
+
if (providedKey) {
|
|
36
|
+
this.logger.debug('getRegistryCoreKey called with providedKey', { providedKey })
|
|
37
|
+
return providedKey
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const envKey = getEnv(ENV_KEYS.QVAC_REGISTRY_CORE_KEY)
|
|
41
|
+
this.logger.debug('getRegistryCoreKey loaded from env', { envKey })
|
|
42
|
+
return envKey || null
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = RegistryConfig
|
package/lib/logger.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
class Logger {
|
|
4
|
+
constructor (opts = {}) {
|
|
5
|
+
this.level = opts.level || 'info'
|
|
6
|
+
this.name = opts.name || 'QVACRegistryClient'
|
|
7
|
+
this.enabled = opts.enabled !== false
|
|
8
|
+
|
|
9
|
+
this.levels = {
|
|
10
|
+
debug: 0,
|
|
11
|
+
info: 1,
|
|
12
|
+
warn: 2,
|
|
13
|
+
error: 3
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_shouldLog (level) {
|
|
18
|
+
if (!this.enabled) return false
|
|
19
|
+
return this.levels[level] >= this.levels[this.level]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
_log (level, ...args) {
|
|
23
|
+
if (!this._shouldLog(level)) return
|
|
24
|
+
const prefix = `[${this.name}] [${level.toUpperCase()}]`
|
|
25
|
+
console[level](prefix, ...args)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
debug (...args) {
|
|
29
|
+
this._log('debug', ...args)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
info (...args) {
|
|
33
|
+
this._log('info', ...args)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
warn (...args) {
|
|
37
|
+
this._log('warn', ...args)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
error (...args) {
|
|
41
|
+
this._log('error', ...args)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = Logger
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@qvac/registry-client",
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "QVAC Registry client library for read-only queries via Hyperswarm",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"bin": {
|
|
9
|
+
"qvac-registry": "./bin/cli.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./index.js",
|
|
13
|
+
"./types": "./index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.d.ts",
|
|
18
|
+
"bin/",
|
|
19
|
+
"lib/",
|
|
20
|
+
"utils/",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test:unit": "brittle tests/unit/*.test.js",
|
|
26
|
+
"test:unit:bare": "brittle-bare tests/unit/*.test.js",
|
|
27
|
+
"test": "npm run test:unit && npm run test:integration",
|
|
28
|
+
"lint": "standard",
|
|
29
|
+
"lint:fix": "standard --fix",
|
|
30
|
+
"typecheck": "npx tsc --noEmit"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@qvac/error": "^0.1.0",
|
|
34
|
+
"@qvac/registry-schema": "^0.1.1",
|
|
35
|
+
"b4a": "^1.6.7",
|
|
36
|
+
"bare-fs": "^2.1.5",
|
|
37
|
+
"bare-os": "^2.2.0",
|
|
38
|
+
"bare-process": "^1.3.0",
|
|
39
|
+
"corestore": "^6.18.4",
|
|
40
|
+
"fs": "npm:bare-node-fs",
|
|
41
|
+
"hyperblobs": "^2.8.0",
|
|
42
|
+
"hypercore-id-encoding": "^1.3.0",
|
|
43
|
+
"hyperdb": "^4.16.1",
|
|
44
|
+
"hyperswarm": "^4.14.0",
|
|
45
|
+
"os": "npm:bare-node-os",
|
|
46
|
+
"paparam": "^1.10.0",
|
|
47
|
+
"path": "npm:bare-node-path",
|
|
48
|
+
"process": "npm:bare-node-process",
|
|
49
|
+
"ready-resource": "^1.0.1"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"brittle": "^3.4.0",
|
|
53
|
+
"dotenv": "^17.2.3",
|
|
54
|
+
"standard": "^17.1.0",
|
|
55
|
+
"test-tmp": "^1.4.0",
|
|
56
|
+
"typescript": "^5.9.3"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/utils/env.js
ADDED
package/utils/error.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { QvacErrorBase, addCodes } = require('@qvac/error')
|
|
4
|
+
const { name, version } = require('../package.json')
|
|
5
|
+
|
|
6
|
+
class QvacErrorRegistryClient extends QvacErrorBase { }
|
|
7
|
+
|
|
8
|
+
// This library has error code range from 19,001 to 20,000
|
|
9
|
+
const ERR_CODES = Object.freeze({
|
|
10
|
+
FAILED_TO_CONNECT: 19001,
|
|
11
|
+
FAILED_TO_CLOSE: 19002,
|
|
12
|
+
MODEL_NOT_FOUND: 19003
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
addCodes({
|
|
16
|
+
[ERR_CODES.FAILED_TO_CONNECT]: {
|
|
17
|
+
name: 'FAILED_TO_CONNECT',
|
|
18
|
+
message: (message) => `Failed to connect to registry, error: ${message}`
|
|
19
|
+
},
|
|
20
|
+
[ERR_CODES.FAILED_TO_CLOSE]: {
|
|
21
|
+
name: 'FAILED_TO_CLOSE',
|
|
22
|
+
message: (message) => `Failed to close registry, error: ${message}`
|
|
23
|
+
},
|
|
24
|
+
[ERR_CODES.MODEL_NOT_FOUND]: {
|
|
25
|
+
name: 'MODEL_NOT_FOUND',
|
|
26
|
+
message: (message) => `Model not found, error: ${message}`
|
|
27
|
+
}
|
|
28
|
+
}, {
|
|
29
|
+
name,
|
|
30
|
+
version
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
ERR_CODES,
|
|
35
|
+
QvacErrorRegistryClient
|
|
36
|
+
}
|