@theokit/sdk 4.12.2 → 4.13.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/CHANGELOG.md +6 -0
- package/dist/cron.cjs +164 -78
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.js +164 -78
- package/dist/cron.js.map +1 -1
- package/dist/eval.cjs +164 -78
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +164 -78
- package/dist/eval.js.map +1 -1
- package/dist/index.cjs +178 -92
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +178 -92
- package/dist/index.js.map +1 -1
- package/dist/internal/providers/catalog-loader.d.ts +8 -0
- package/dist/internal/providers/catalog-schema.d.ts +53 -0
- package/dist/internal/providers/catalog-source-models-dev.d.ts +33 -0
- package/dist/models.cjs +332 -257
- package/dist/models.cjs.map +1 -1
- package/dist/models.d.cts +2 -0
- package/dist/models.d.ts +2 -0
- package/dist/models.js +330 -258
- package/dist/models.js.map +1 -1
- package/dist/provider-catalog.json +571 -5
- package/package.json +1 -1
package/dist/models.js
CHANGED
|
@@ -1,3 +1,116 @@
|
|
|
1
|
+
import { statSync, readFileSync, unlinkSync, mkdirSync, writeFileSync, renameSync } from 'fs';
|
|
2
|
+
import { dirname, join } from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { createHash, randomBytes } from 'crypto';
|
|
6
|
+
import { homedir } from 'os';
|
|
7
|
+
|
|
8
|
+
// src/internal/providers/catalog-loader.ts
|
|
9
|
+
var MODALITIES = ["text", "audio", "image", "video", "pdf"];
|
|
10
|
+
var costSchema = z.object({
|
|
11
|
+
/** USD per 1M tokens (models.dev convention). */
|
|
12
|
+
input: z.number().nonnegative(),
|
|
13
|
+
output: z.number().nonnegative(),
|
|
14
|
+
cache_read: z.number().nonnegative().optional(),
|
|
15
|
+
cache_write: z.number().nonnegative().optional()
|
|
16
|
+
}).loose();
|
|
17
|
+
var limitSchema = z.object({
|
|
18
|
+
context: z.number().positive(),
|
|
19
|
+
input: z.number().positive().optional(),
|
|
20
|
+
output: z.number().positive().optional()
|
|
21
|
+
}).loose();
|
|
22
|
+
var modalitiesSchema = z.object({
|
|
23
|
+
input: z.array(z.enum(MODALITIES)).optional(),
|
|
24
|
+
output: z.array(z.enum(MODALITIES)).optional()
|
|
25
|
+
}).loose();
|
|
26
|
+
var catalogModelSchema = z.object({
|
|
27
|
+
name: z.string().optional(),
|
|
28
|
+
release_date: z.string().optional(),
|
|
29
|
+
attachment: z.boolean().optional(),
|
|
30
|
+
reasoning: z.boolean().optional(),
|
|
31
|
+
temperature: z.boolean().optional(),
|
|
32
|
+
tool_call: z.boolean().optional(),
|
|
33
|
+
/** theokit extension — maps to ModelCapabilities.supportsStructuredOutput. */
|
|
34
|
+
structured_output: z.boolean().optional(),
|
|
35
|
+
/** theokit extension — maps to ModelCapabilities.supportsCacheControl. */
|
|
36
|
+
cache_control: z.boolean().optional(),
|
|
37
|
+
cost: costSchema.optional(),
|
|
38
|
+
limit: limitSchema.optional(),
|
|
39
|
+
modalities: modalitiesSchema.optional(),
|
|
40
|
+
status: z.enum(["alpha", "beta", "deprecated"]).optional()
|
|
41
|
+
}).loose();
|
|
42
|
+
|
|
43
|
+
// src/internal/providers/registry.ts
|
|
44
|
+
var REGISTRY = /* @__PURE__ */ new Map();
|
|
45
|
+
var ALIASES = /* @__PURE__ */ new Map();
|
|
46
|
+
function getProviderProfile(name) {
|
|
47
|
+
const canonical = ALIASES.get(name) ?? name;
|
|
48
|
+
return REGISTRY.get(canonical);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/internal/providers/catalog-loader.ts
|
|
52
|
+
var __dirname_resolved = dirname(fileURLToPath(import.meta.url));
|
|
53
|
+
var modelInfoIndex = /* @__PURE__ */ new Map();
|
|
54
|
+
function getCatalogModelInfo(key) {
|
|
55
|
+
ensureModelIndexLoaded();
|
|
56
|
+
return modelInfoIndex.get(key);
|
|
57
|
+
}
|
|
58
|
+
function patchModelInfo(key, model) {
|
|
59
|
+
ensureModelIndexLoaded();
|
|
60
|
+
modelInfoIndex.set(key, model);
|
|
61
|
+
}
|
|
62
|
+
var _modelIndexLoaded = false;
|
|
63
|
+
function ensureModelIndexLoaded() {
|
|
64
|
+
if (_modelIndexLoaded) return;
|
|
65
|
+
_modelIndexLoaded = true;
|
|
66
|
+
const catalog = loadProviderCatalog();
|
|
67
|
+
for (const entry of Object.values(catalog)) {
|
|
68
|
+
indexEntryModels(entry);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function indexEntryModels(entry) {
|
|
72
|
+
if (entry.models === void 0 || typeof entry.models !== "object") return;
|
|
73
|
+
for (const [modelId, raw] of Object.entries(entry.models)) {
|
|
74
|
+
const parsed = catalogModelSchema.safeParse(raw);
|
|
75
|
+
if (!parsed.success) {
|
|
76
|
+
process.stderr.write(
|
|
77
|
+
`[theokit-sdk] WARN: Skipping malformed catalog model "${entry.id}/${modelId}": ${parsed.error.issues[0]?.message ?? "invalid"}
|
|
78
|
+
`
|
|
79
|
+
);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
modelInfoIndex.set(`${entry.id}/${modelId}`, parsed.data);
|
|
83
|
+
for (const alias of entry.aliases ?? []) {
|
|
84
|
+
const key = `${alias}/${modelId}`;
|
|
85
|
+
if (!modelInfoIndex.has(key)) modelInfoIndex.set(key, parsed.data);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function validateEntry(raw) {
|
|
90
|
+
if (typeof raw.id !== "string" || typeof raw.displayName !== "string" || typeof raw.apiMode !== "string" || typeof raw.authType !== "string" || typeof raw.baseUrl !== "string" || !Array.isArray(raw.envVars) || !Array.isArray(raw.fallbackModels) || raw.capabilities == null || typeof raw.capabilities !== "object") {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return raw;
|
|
94
|
+
}
|
|
95
|
+
function loadProviderCatalog(opts) {
|
|
96
|
+
const catalogPath = join(__dirname_resolved, "provider-catalog.json");
|
|
97
|
+
const rawText = readFileSync(catalogPath, "utf-8");
|
|
98
|
+
let entries = JSON.parse(rawText);
|
|
99
|
+
const result = {};
|
|
100
|
+
for (const raw of entries) {
|
|
101
|
+
const validated = validateEntry(raw);
|
|
102
|
+
if (validated === null) {
|
|
103
|
+
process.stderr.write(
|
|
104
|
+
`[theokit-sdk] WARN: Skipping malformed catalog entry: ${JSON.stringify(raw).slice(0, 100)}
|
|
105
|
+
`
|
|
106
|
+
);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
result[validated.id] = validated;
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
|
|
1
114
|
// src/internal/llm/model-capabilities.ts
|
|
2
115
|
var CONSERVATIVE_DEFAULTS = {
|
|
3
116
|
supportsVision: false,
|
|
@@ -7,268 +120,25 @@ var CONSERVATIVE_DEFAULTS = {
|
|
|
7
120
|
maxContextTokens: 4096,
|
|
8
121
|
maxOutputTokens: 4096
|
|
9
122
|
};
|
|
10
|
-
var EXACT = /* @__PURE__ */ new Map([
|
|
11
|
-
// OpenAI family
|
|
12
|
-
[
|
|
13
|
-
"openai/gpt-4o",
|
|
14
|
-
{
|
|
15
|
-
supportsVision: true,
|
|
16
|
-
supportsStructuredOutput: true,
|
|
17
|
-
supportsToolUse: true,
|
|
18
|
-
supportsCacheControl: false,
|
|
19
|
-
maxContextTokens: 128e3,
|
|
20
|
-
maxOutputTokens: 16384
|
|
21
|
-
}
|
|
22
|
-
],
|
|
23
|
-
[
|
|
24
|
-
"openai/gpt-4o-mini",
|
|
25
|
-
{
|
|
26
|
-
supportsVision: true,
|
|
27
|
-
supportsStructuredOutput: true,
|
|
28
|
-
supportsToolUse: true,
|
|
29
|
-
supportsCacheControl: false,
|
|
30
|
-
maxContextTokens: 128e3,
|
|
31
|
-
maxOutputTokens: 16384
|
|
32
|
-
}
|
|
33
|
-
],
|
|
34
|
-
[
|
|
35
|
-
"openai/gpt-4-turbo",
|
|
36
|
-
{
|
|
37
|
-
supportsVision: true,
|
|
38
|
-
supportsStructuredOutput: false,
|
|
39
|
-
supportsToolUse: true,
|
|
40
|
-
supportsCacheControl: false,
|
|
41
|
-
maxContextTokens: 128e3,
|
|
42
|
-
maxOutputTokens: 4096
|
|
43
|
-
}
|
|
44
|
-
],
|
|
45
|
-
[
|
|
46
|
-
"openai/o1",
|
|
47
|
-
{
|
|
48
|
-
supportsVision: false,
|
|
49
|
-
supportsStructuredOutput: true,
|
|
50
|
-
supportsToolUse: true,
|
|
51
|
-
supportsCacheControl: false,
|
|
52
|
-
maxContextTokens: 2e5,
|
|
53
|
-
maxOutputTokens: 1e5
|
|
54
|
-
}
|
|
55
|
-
],
|
|
56
|
-
[
|
|
57
|
-
"openai/o3",
|
|
58
|
-
{
|
|
59
|
-
supportsVision: false,
|
|
60
|
-
supportsStructuredOutput: true,
|
|
61
|
-
supportsToolUse: true,
|
|
62
|
-
supportsCacheControl: false,
|
|
63
|
-
maxContextTokens: 2e5,
|
|
64
|
-
maxOutputTokens: 1e5
|
|
65
|
-
}
|
|
66
|
-
],
|
|
67
|
-
[
|
|
68
|
-
// GPT-4.1 — 1M-context flagship; multimodal + structured output (RADAR #92.a).
|
|
69
|
-
"openai/gpt-4.1",
|
|
70
|
-
{
|
|
71
|
-
supportsVision: true,
|
|
72
|
-
supportsStructuredOutput: true,
|
|
73
|
-
supportsToolUse: true,
|
|
74
|
-
supportsCacheControl: false,
|
|
75
|
-
maxContextTokens: 1047576,
|
|
76
|
-
maxOutputTokens: 32768
|
|
77
|
-
}
|
|
78
|
-
],
|
|
79
|
-
// Anthropic family
|
|
80
|
-
[
|
|
81
|
-
"anthropic/claude-opus-4",
|
|
82
|
-
{
|
|
83
|
-
supportsVision: true,
|
|
84
|
-
supportsStructuredOutput: false,
|
|
85
|
-
supportsToolUse: true,
|
|
86
|
-
supportsCacheControl: true,
|
|
87
|
-
maxContextTokens: 2e5,
|
|
88
|
-
maxOutputTokens: 32e3
|
|
89
|
-
}
|
|
90
|
-
],
|
|
91
|
-
[
|
|
92
|
-
"anthropic/claude-sonnet-4",
|
|
93
|
-
{
|
|
94
|
-
supportsVision: true,
|
|
95
|
-
supportsStructuredOutput: false,
|
|
96
|
-
supportsToolUse: true,
|
|
97
|
-
supportsCacheControl: true,
|
|
98
|
-
maxContextTokens: 2e5,
|
|
99
|
-
maxOutputTokens: 16e3
|
|
100
|
-
}
|
|
101
|
-
],
|
|
102
|
-
[
|
|
103
|
-
"anthropic/claude-3-5-sonnet",
|
|
104
|
-
{
|
|
105
|
-
supportsVision: true,
|
|
106
|
-
supportsStructuredOutput: false,
|
|
107
|
-
supportsToolUse: true,
|
|
108
|
-
supportsCacheControl: true,
|
|
109
|
-
maxContextTokens: 2e5,
|
|
110
|
-
maxOutputTokens: 8192
|
|
111
|
-
}
|
|
112
|
-
],
|
|
113
|
-
[
|
|
114
|
-
"anthropic/claude-3-5-sonnet-latest",
|
|
115
|
-
{
|
|
116
|
-
supportsVision: true,
|
|
117
|
-
supportsStructuredOutput: false,
|
|
118
|
-
supportsToolUse: true,
|
|
119
|
-
supportsCacheControl: true,
|
|
120
|
-
maxContextTokens: 2e5,
|
|
121
|
-
maxOutputTokens: 8192
|
|
122
|
-
}
|
|
123
|
-
],
|
|
124
|
-
[
|
|
125
|
-
"anthropic/claude-3-5-haiku-latest",
|
|
126
|
-
{
|
|
127
|
-
supportsVision: false,
|
|
128
|
-
supportsStructuredOutput: false,
|
|
129
|
-
supportsToolUse: true,
|
|
130
|
-
supportsCacheControl: true,
|
|
131
|
-
maxContextTokens: 2e5,
|
|
132
|
-
maxOutputTokens: 8192
|
|
133
|
-
}
|
|
134
|
-
],
|
|
135
|
-
[
|
|
136
|
-
"anthropic/claude-3-haiku",
|
|
137
|
-
{
|
|
138
|
-
supportsVision: true,
|
|
139
|
-
supportsStructuredOutput: false,
|
|
140
|
-
supportsToolUse: true,
|
|
141
|
-
supportsCacheControl: true,
|
|
142
|
-
maxContextTokens: 2e5,
|
|
143
|
-
maxOutputTokens: 4096
|
|
144
|
-
}
|
|
145
|
-
],
|
|
146
|
-
[
|
|
147
|
-
"anthropic/claude-3-opus",
|
|
148
|
-
{
|
|
149
|
-
supportsVision: true,
|
|
150
|
-
supportsStructuredOutput: false,
|
|
151
|
-
supportsToolUse: true,
|
|
152
|
-
supportsCacheControl: true,
|
|
153
|
-
maxContextTokens: 2e5,
|
|
154
|
-
maxOutputTokens: 4096
|
|
155
|
-
}
|
|
156
|
-
],
|
|
157
|
-
// Dot-form OpenRouter slugs theocode uses (RADAR #92.a). These are the same
|
|
158
|
-
// models as their dash-form siblings above; capability parity is intentional.
|
|
159
|
-
// Without these entries the dotted slugs fall through to the 4096 default
|
|
160
|
-
// (`anthropic/claude-3.5-sonnet` ≠ `anthropic/claude-3-5-sonnet`).
|
|
161
|
-
[
|
|
162
|
-
"anthropic/claude-opus-4.1",
|
|
163
|
-
{
|
|
164
|
-
supportsVision: true,
|
|
165
|
-
supportsStructuredOutput: false,
|
|
166
|
-
supportsToolUse: true,
|
|
167
|
-
supportsCacheControl: true,
|
|
168
|
-
maxContextTokens: 2e5,
|
|
169
|
-
maxOutputTokens: 32e3
|
|
170
|
-
}
|
|
171
|
-
],
|
|
172
|
-
[
|
|
173
|
-
"anthropic/claude-sonnet-4.5",
|
|
174
|
-
{
|
|
175
|
-
supportsVision: true,
|
|
176
|
-
supportsStructuredOutput: false,
|
|
177
|
-
supportsToolUse: true,
|
|
178
|
-
supportsCacheControl: true,
|
|
179
|
-
maxContextTokens: 2e5,
|
|
180
|
-
maxOutputTokens: 16e3
|
|
181
|
-
}
|
|
182
|
-
],
|
|
183
|
-
[
|
|
184
|
-
"anthropic/claude-3.5-sonnet",
|
|
185
|
-
{
|
|
186
|
-
supportsVision: true,
|
|
187
|
-
supportsStructuredOutput: false,
|
|
188
|
-
supportsToolUse: true,
|
|
189
|
-
supportsCacheControl: true,
|
|
190
|
-
maxContextTokens: 2e5,
|
|
191
|
-
maxOutputTokens: 8192
|
|
192
|
-
}
|
|
193
|
-
],
|
|
194
|
-
// Cheap OpenRouter slugs (RADAR #92.a) — previously fell to the 4096
|
|
195
|
-
// CONSERVATIVE default. toolUse on; vision/structuredOutput only for Gemini.
|
|
196
|
-
[
|
|
197
|
-
"qwen/qwen3-coder-30b-a3b-instruct",
|
|
198
|
-
{
|
|
199
|
-
supportsVision: false,
|
|
200
|
-
supportsStructuredOutput: false,
|
|
201
|
-
supportsToolUse: true,
|
|
202
|
-
supportsCacheControl: false,
|
|
203
|
-
maxContextTokens: 16e4,
|
|
204
|
-
maxOutputTokens: 8e3
|
|
205
|
-
}
|
|
206
|
-
],
|
|
207
|
-
[
|
|
208
|
-
"deepseek/deepseek-v4-flash",
|
|
209
|
-
{
|
|
210
|
-
supportsVision: false,
|
|
211
|
-
supportsStructuredOutput: false,
|
|
212
|
-
supportsToolUse: true,
|
|
213
|
-
supportsCacheControl: false,
|
|
214
|
-
maxContextTokens: 1048576,
|
|
215
|
-
maxOutputTokens: 8e3
|
|
216
|
-
}
|
|
217
|
-
],
|
|
218
|
-
[
|
|
219
|
-
"deepseek/deepseek-v3.2",
|
|
220
|
-
{
|
|
221
|
-
supportsVision: false,
|
|
222
|
-
supportsStructuredOutput: false,
|
|
223
|
-
supportsToolUse: true,
|
|
224
|
-
supportsCacheControl: false,
|
|
225
|
-
maxContextTokens: 131072,
|
|
226
|
-
maxOutputTokens: 8e3
|
|
227
|
-
}
|
|
228
|
-
],
|
|
229
|
-
[
|
|
230
|
-
"z-ai/glm-4.7-flash",
|
|
231
|
-
{
|
|
232
|
-
supportsVision: false,
|
|
233
|
-
supportsStructuredOutput: false,
|
|
234
|
-
supportsToolUse: true,
|
|
235
|
-
supportsCacheControl: false,
|
|
236
|
-
maxContextTokens: 202752,
|
|
237
|
-
maxOutputTokens: 8e3
|
|
238
|
-
}
|
|
239
|
-
],
|
|
240
|
-
[
|
|
241
|
-
"google/gemini-2.5-flash-lite",
|
|
242
|
-
{
|
|
243
|
-
supportsVision: true,
|
|
244
|
-
supportsStructuredOutput: true,
|
|
245
|
-
supportsToolUse: true,
|
|
246
|
-
supportsCacheControl: false,
|
|
247
|
-
maxContextTokens: 1048576,
|
|
248
|
-
maxOutputTokens: 8e3
|
|
249
|
-
}
|
|
250
|
-
],
|
|
251
|
-
[
|
|
252
|
-
"google/gemini-2.5-pro",
|
|
253
|
-
{
|
|
254
|
-
supportsVision: true,
|
|
255
|
-
supportsStructuredOutput: true,
|
|
256
|
-
supportsToolUse: true,
|
|
257
|
-
supportsCacheControl: false,
|
|
258
|
-
maxContextTokens: 1048576,
|
|
259
|
-
maxOutputTokens: 8e3
|
|
260
|
-
}
|
|
261
|
-
]
|
|
262
|
-
]);
|
|
263
123
|
var ROUTING_PREFIXES = ["openrouter/", "vertex/", "bedrock/"];
|
|
124
|
+
function capsFromCatalog(m) {
|
|
125
|
+
return {
|
|
126
|
+
supportsVision: m.modalities?.input?.includes("image") ?? m.attachment ?? false,
|
|
127
|
+
supportsStructuredOutput: m.structured_output ?? false,
|
|
128
|
+
supportsToolUse: m.tool_call ?? false,
|
|
129
|
+
supportsCacheControl: m.cache_control ?? false,
|
|
130
|
+
maxContextTokens: m.limit?.context ?? CONSERVATIVE_DEFAULTS.maxContextTokens,
|
|
131
|
+
maxOutputTokens: m.limit?.output ?? CONSERVATIVE_DEFAULTS.maxOutputTokens
|
|
132
|
+
};
|
|
133
|
+
}
|
|
264
134
|
function resolveModelCapabilities(modelId) {
|
|
265
135
|
const bare = stripVariantSuffix(stripRoutingPrefix(modelId));
|
|
266
|
-
const
|
|
267
|
-
if (
|
|
136
|
+
const fromIndex = getCatalogModelInfo(bare);
|
|
137
|
+
if (fromIndex !== void 0) return capsFromCatalog(fromIndex);
|
|
268
138
|
const withVendor = inferVendorPrefix(bare);
|
|
269
139
|
if (withVendor !== bare) {
|
|
270
|
-
const vendored =
|
|
271
|
-
if (vendored !== void 0) return vendored;
|
|
140
|
+
const vendored = getCatalogModelInfo(withVendor);
|
|
141
|
+
if (vendored !== void 0) return capsFromCatalog(vendored);
|
|
272
142
|
}
|
|
273
143
|
return CONSERVATIVE_DEFAULTS;
|
|
274
144
|
}
|
|
@@ -340,6 +210,208 @@ function toModelOption(modelId) {
|
|
|
340
210
|
};
|
|
341
211
|
}
|
|
342
212
|
|
|
343
|
-
|
|
213
|
+
// src/errors.ts
|
|
214
|
+
var TheokitAgentError = class extends Error {
|
|
215
|
+
name = "TheokitAgentError";
|
|
216
|
+
isRetryable;
|
|
217
|
+
code;
|
|
218
|
+
protoErrorCode;
|
|
219
|
+
metadata;
|
|
220
|
+
constructor(message, options = {}) {
|
|
221
|
+
super(message, options.cause !== void 0 ? { cause: options.cause } : void 0);
|
|
222
|
+
this.isRetryable = options.isRetryable ?? false;
|
|
223
|
+
if (options.code !== void 0) this.code = options.code;
|
|
224
|
+
if (options.protoErrorCode !== void 0) this.protoErrorCode = options.protoErrorCode;
|
|
225
|
+
if (options.metadata !== void 0) this.metadata = options.metadata;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
var ConfigurationError = class extends TheokitAgentError {
|
|
229
|
+
name = "ConfigurationError";
|
|
230
|
+
constructor(message, options = {}) {
|
|
231
|
+
super(message, { ...options, isRetryable: false });
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
function isTransientError(err) {
|
|
235
|
+
return err instanceof TheokitAgentError && err.isRetryable === true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/internal/runtime/retry/with-retry.ts
|
|
239
|
+
function defaultSleep(ms, signal) {
|
|
240
|
+
return new Promise((resolve, reject) => {
|
|
241
|
+
if (signal?.aborted) {
|
|
242
|
+
reject(signal.reason instanceof Error ? signal.reason : new Error("withRetry: aborted"));
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const timer = setTimeout(() => {
|
|
246
|
+
signal?.removeEventListener("abort", onAbort);
|
|
247
|
+
resolve();
|
|
248
|
+
}, ms);
|
|
249
|
+
function onAbort() {
|
|
250
|
+
clearTimeout(timer);
|
|
251
|
+
reject(signal?.reason instanceof Error ? signal.reason : new Error("withRetry: aborted"));
|
|
252
|
+
}
|
|
253
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
function resolveRetryOptions(options) {
|
|
257
|
+
const retries = options?.retries ?? 3;
|
|
258
|
+
if (!Number.isInteger(retries) || retries < 0) {
|
|
259
|
+
throw new ConfigurationError(
|
|
260
|
+
`withRetry: retries must be a non-negative integer, got ${retries}`,
|
|
261
|
+
{ code: "invalid_retry_config" }
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
return {
|
|
265
|
+
retries,
|
|
266
|
+
isRetryable: options?.isRetryable ?? isTransientError,
|
|
267
|
+
initialDelayMs: options?.initialDelayMs ?? 100,
|
|
268
|
+
maxDelayMs: options?.maxDelayMs ?? 3e4,
|
|
269
|
+
backoffMultiplier: options?.backoffMultiplier ?? 2,
|
|
270
|
+
rng: options?.rng ?? Math.random,
|
|
271
|
+
sleep: options?.sleep ?? defaultSleep,
|
|
272
|
+
signal: options?.signal
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function backoffMs(cfg, attempt) {
|
|
276
|
+
const ceiling = Math.min(cfg.maxDelayMs, cfg.initialDelayMs * cfg.backoffMultiplier ** attempt);
|
|
277
|
+
return Math.floor(cfg.rng() * ceiling);
|
|
278
|
+
}
|
|
279
|
+
async function withRetry(fn, options) {
|
|
280
|
+
const cfg = resolveRetryOptions(options);
|
|
281
|
+
let attempt = 0;
|
|
282
|
+
for (; ; ) {
|
|
283
|
+
try {
|
|
284
|
+
return await fn();
|
|
285
|
+
} catch (err) {
|
|
286
|
+
if (attempt >= cfg.retries || !cfg.isRetryable(err)) throw err;
|
|
287
|
+
await cfg.sleep(backoffMs(cfg, attempt), cfg.signal);
|
|
288
|
+
attempt += 1;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/retry.ts
|
|
294
|
+
var Retry = class {
|
|
295
|
+
constructor() {
|
|
296
|
+
}
|
|
297
|
+
static create(fn, options) {
|
|
298
|
+
return withRetry(fn, options);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// src/internal/providers/catalog-source-models-dev.ts
|
|
303
|
+
var DEFAULT_URL = "https://models.dev/api.json";
|
|
304
|
+
var TTL_MS = 60 * 60 * 1e3;
|
|
305
|
+
var FETCH_TIMEOUT_MS = 1e4;
|
|
306
|
+
function cachePathFor(url) {
|
|
307
|
+
const dir = join(homedir(), ".theokit", "cache", "models-dev");
|
|
308
|
+
if (url === DEFAULT_URL) return join(dir, "api.json");
|
|
309
|
+
const hash = createHash("sha256").update(url).digest("hex").slice(0, 12);
|
|
310
|
+
return join(dir, `api-${hash}.json`);
|
|
311
|
+
}
|
|
312
|
+
function writeCacheAtomic(path, body) {
|
|
313
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
314
|
+
const tmp = `${path}.tmp-${randomBytes(6).toString("hex")}`;
|
|
315
|
+
try {
|
|
316
|
+
writeFileSync(tmp, body);
|
|
317
|
+
renameSync(tmp, path);
|
|
318
|
+
} catch (err) {
|
|
319
|
+
try {
|
|
320
|
+
unlinkSync(tmp);
|
|
321
|
+
} catch {
|
|
322
|
+
}
|
|
323
|
+
throw err;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function patchIndexFromApiJson(raw) {
|
|
327
|
+
if (typeof raw !== "object" || raw === null) return 0;
|
|
328
|
+
let patched = 0;
|
|
329
|
+
for (const [providerId, provider] of Object.entries(raw)) {
|
|
330
|
+
const models = provider?.models;
|
|
331
|
+
if (models === void 0 || typeof models !== "object") continue;
|
|
332
|
+
const profile = getProviderProfile(providerId);
|
|
333
|
+
if (profile === void 0) continue;
|
|
334
|
+
for (const [modelId, rawModel] of Object.entries(models)) {
|
|
335
|
+
const parsed = catalogModelSchema.safeParse(rawModel);
|
|
336
|
+
if (!parsed.success) continue;
|
|
337
|
+
patchModelInfo(`${profile.name}/${modelId}`, parsed.data);
|
|
338
|
+
patched++;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return patched;
|
|
342
|
+
}
|
|
343
|
+
function loadCacheIntoIndex(url = DEFAULT_URL) {
|
|
344
|
+
const path = cachePathFor(url);
|
|
345
|
+
let body;
|
|
346
|
+
try {
|
|
347
|
+
body = readFileSync(path, "utf-8");
|
|
348
|
+
} catch {
|
|
349
|
+
return 0;
|
|
350
|
+
}
|
|
351
|
+
try {
|
|
352
|
+
return patchIndexFromApiJson(JSON.parse(body));
|
|
353
|
+
} catch {
|
|
354
|
+
try {
|
|
355
|
+
unlinkSync(path);
|
|
356
|
+
} catch {
|
|
357
|
+
}
|
|
358
|
+
process.stderr.write(`[theokit-sdk] WARN: corrupt models-dev cache deleted (${path})
|
|
359
|
+
`);
|
|
360
|
+
return 0;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
async function refreshModelCatalog(opts = {}) {
|
|
364
|
+
if (process.env.THEOKIT_DISABLE_MODELS_FETCH !== void 0) {
|
|
365
|
+
return { source: "skipped", models: 0 };
|
|
366
|
+
}
|
|
367
|
+
const url = opts.url ?? process.env.THEOKIT_MODELS_URL ?? DEFAULT_URL;
|
|
368
|
+
const path = cachePathFor(url);
|
|
369
|
+
const now = opts.deps?.now ?? (() => Date.now());
|
|
370
|
+
if (opts.force !== true) {
|
|
371
|
+
try {
|
|
372
|
+
const age = now() - statSync(path).mtimeMs;
|
|
373
|
+
if (age < TTL_MS) {
|
|
374
|
+
return { source: "cache", models: loadCacheIntoIndex(url) };
|
|
375
|
+
}
|
|
376
|
+
} catch {
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const fetchImpl = opts.deps?.fetch ?? fetch;
|
|
380
|
+
let body;
|
|
381
|
+
try {
|
|
382
|
+
const res = await Retry.create(
|
|
383
|
+
async () => {
|
|
384
|
+
const r = await fetchImpl(url, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) });
|
|
385
|
+
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
|
386
|
+
return r;
|
|
387
|
+
},
|
|
388
|
+
// 2 transient retries with backoff (OpenCode does the same); every error here is worth one more try —
|
|
389
|
+
// the whole call is already fail-closed at the caller.
|
|
390
|
+
{ retries: 2, isRetryable: () => true, initialDelayMs: 200 }
|
|
391
|
+
);
|
|
392
|
+
body = await res.text();
|
|
393
|
+
JSON.parse(body);
|
|
394
|
+
} catch (err) {
|
|
395
|
+
process.stderr.write(
|
|
396
|
+
`[theokit-sdk] WARN: models-dev refresh failed (${err.message}) \u2014 serving existing data
|
|
397
|
+
`
|
|
398
|
+
);
|
|
399
|
+
return { source: "cache", models: loadCacheIntoIndex(url) };
|
|
400
|
+
}
|
|
401
|
+
try {
|
|
402
|
+
writeCacheAtomic(path, body);
|
|
403
|
+
} catch (err) {
|
|
404
|
+
process.stderr.write(
|
|
405
|
+
`[theokit-sdk] WARN: models-dev cache write failed (${err.message})
|
|
406
|
+
`
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
return { source: "network", models: patchIndexFromApiJson(JSON.parse(body)) };
|
|
410
|
+
}
|
|
411
|
+
function getModelInfo(modelId) {
|
|
412
|
+
return getCatalogModelInfo(modelId);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export { getModelInfo, humanizeModelName, parseModelId, refreshModelCatalog, resolveModelCapabilities, toModelOption };
|
|
344
416
|
//# sourceMappingURL=models.js.map
|
|
345
417
|
//# sourceMappingURL=models.js.map
|