@alexeiled/claude-router 1.1.1 → 1.3.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/lib/config.mjs CHANGED
@@ -69,24 +69,75 @@ export const DEFAULTS = {
69
69
  // inert until a user adds one in router.json; a Claude Code turn starts near 100k tokens.
70
70
  cashCapUsd: 2,
71
71
  },
72
- jev: { endpoint: 'https://api.typesafe.ai/v1/systemone', model: 'jev-1.13.0', timeoutMs: 1500 },
72
+ // The active entry of `classifiers`. Each entry speaks the same typed-questions API. `keyOption` names the plugin
73
+ // option that holds the bearer key; a `{name}` in `endpoint` is filled from the plugin option of that name. Both
74
+ // must be in CLASSIFIER_OPTIONS. Cloudflare wraps the answer in `result`; the parser reads both shapes.
75
+ classifier: 'jev',
76
+ classifiers: {
77
+ jev: {
78
+ label: 'Jev',
79
+ endpoint: 'https://api.typesafe.ai/v1/systemone',
80
+ model: 'jev-1.13.0',
81
+ keyOption: 'typesafe_api_key',
82
+ timeoutMs: 1500,
83
+ },
84
+ clef: {
85
+ label: 'Clef',
86
+ endpoint: 'https://api.cloudflare.com/client/v4/accounts/{cloudflare_account_id}/ai/run/@cf/cloudflare/clef',
87
+ model: 'clef',
88
+ keyOption: 'cloudflare_api_token',
89
+ timeoutMs: 3000,
90
+ },
91
+ 'clef-flash': {
92
+ label: 'Clef Flash',
93
+ endpoint:
94
+ 'https://api.cloudflare.com/client/v4/accounts/{cloudflare_account_id}/ai/run/@cf/cloudflare/clef-flash',
95
+ model: 'clef-flash',
96
+ keyOption: 'cloudflare_api_token',
97
+ timeoutMs: 3000,
98
+ },
99
+ },
73
100
  context: { recentTurns: 6, maxTextChars: 1200 },
74
101
  };
75
102
 
76
- const API_KEY_VAR = 'TYPESAFE_API_KEY';
77
-
78
- // Keys a route or a model entry may carry. The other closed sections take their key sets from DEFAULTS.
103
+ // Keys a route, a model or a classifier entry may carry. The other closed sections take their key sets from DEFAULTS.
79
104
  const ROUTE_KEYS = ['model', 'effort'];
80
105
  const MODEL_KEYS = ['id', 'input', 'output', 'cacheRead', 'contextWindow', 'billing', 'efforts'];
106
+ const CLASSIFIER_KEYS = ['label', 'endpoint', 'model', 'keyOption', 'timeoutMs'];
107
+ // The plugin options a classifier can read, the userConfig fields of plugin.json, with what each holds as the pane
108
+ // names it. The engine lets a Mod read only declared options and literally named environment variables, so a
109
+ // classifier cannot name a setting of its own.
110
+ export const CLASSIFIER_OPTIONS = {
111
+ typesafe_api_key: 'API key',
112
+ cloudflare_api_token: 'API token',
113
+ cloudflare_account_id: 'account ID',
114
+ };
115
+ const OPTION_NAMES = Object.keys(CLASSIFIER_OPTIONS);
81
116
  // A JSON file can hold these as own keys; merged into a plain object they reach the prototype chain.
82
117
  const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
118
+ export const MIGRATION_HINT =
119
+ 'run node scripts/migrate-config.mjs /path/to/router.json from the router plugin directory';
83
120
 
84
- export function loadConfig({ env = {}, userFile = null } = {}) {
121
+ export function loadConfig({ userFile = null } = {}) {
85
122
  if (userFile !== null) checkShape(userFile);
86
123
  const config = merge(DEFAULTS, userFile ?? {});
87
124
  validate(config);
88
- const apiKey = env[API_KEY_VAR]?.trim() || null;
89
- return { ...config, apiKey };
125
+ return config;
126
+ }
127
+
128
+ export function activeClassifier(config) {
129
+ return config.classifiers[config.classifier];
130
+ }
131
+
132
+ // The `{name}` placeholders of an endpoint, in order.
133
+ export function endpointSettings(endpoint) {
134
+ return [...endpoint.matchAll(/\{([a-z][a-z0-9_]*)\}/g)].map((match) => match[1]);
135
+ }
136
+
137
+ // The router.json `file` with `id` as the active classifier; the default is written as no key at all.
138
+ export function withClassifier(file, id) {
139
+ const { classifier: _, ...rest } = file;
140
+ return id === DEFAULTS.classifier ? rest : { ...rest, classifier: id };
90
141
  }
91
142
 
92
143
  export const sameRoute = (a, b) => a.model === b.model && (a.effort ?? null) === (b.effort ?? null);
@@ -138,9 +189,8 @@ export function withRoutes(file, draft) {
138
189
  };
139
190
  }
140
191
 
141
- // The pane's tuning controls and the router.json fields they stand for.
192
+ // The pane's policy controls and the router.json fields they stand for. The classifier deadline saves on its own.
142
193
  const TUNING_FIELDS = {
143
- timeoutMs: ['jev', 'timeoutMs'],
144
194
  downgradeVotes: ['policy', 'downgradeVotes'],
145
195
  horizon: ['policy', 'downgradeHorizonTurns'],
146
196
  cashCapUsd: ['policy', 'cashCapUsd'],
@@ -152,14 +202,32 @@ export function tuningOf(config) {
152
202
  );
153
203
  }
154
204
 
155
- // The router.json `file` with only the tuning values the person changed in `draft` against `saved`.
205
+ // The router.json `file` with only the tuning values the person changed in `draft` against `saved`. A value equal to
206
+ // the default is written as no key, and a section left empty is dropped, as for routes and deadlines.
156
207
  export function withTuning(file, draft, saved) {
157
208
  let next = file;
158
- for (const [key, [section, field]] of Object.entries(TUNING_FIELDS))
159
- if (draft[key] !== saved[key]) next = { ...next, [section]: { ...next[section], [field]: draft[key] } };
209
+ for (const [key, [section, field]] of Object.entries(TUNING_FIELDS)) {
210
+ if (draft[key] === saved[key]) continue;
211
+ const { [field]: _, ...kept } = next[section] ?? {};
212
+ const entry = draft[key] === DEFAULTS[section][field] ? kept : { ...kept, [field]: draft[key] };
213
+ const { [section]: __, ...rest } = next;
214
+ next = Object.keys(entry).length ? { ...rest, [section]: entry } : rest;
215
+ }
160
216
  return next;
161
217
  }
162
218
 
219
+ // The router.json `file` with classifier `id` given `timeoutMs`. A built-in classifier's default deadline is written
220
+ // as no override, and an entry or section left empty is dropped, so a later change of the default still reaches it.
221
+ export function withClassifierTimeout(file, id, timeoutMs) {
222
+ const { classifiers = {}, ...rest } = file;
223
+ const { [id]: current = {}, ...others } = classifiers;
224
+ const { timeoutMs: _, ...entry } = current;
225
+ const isDefault = Object.hasOwn(DEFAULTS.classifiers, id) && DEFAULTS.classifiers[id].timeoutMs === timeoutMs;
226
+ const kept = isDefault ? entry : { ...entry, timeoutMs };
227
+ const next = Object.keys(kept).length ? { ...others, [id]: kept } : others;
228
+ return Object.keys(next).length ? { ...rest, classifiers: next } : rest;
229
+ }
230
+
163
231
  export function rank(tier) {
164
232
  return TIERS.indexOf(tier);
165
233
  }
@@ -176,23 +244,23 @@ function merge(base, override) {
176
244
  return out;
177
245
  }
178
246
 
247
+ // The 0.8 gateway layout: these keys only existed there.
248
+ export const isGatewayLayout = (file) =>
249
+ Object.hasOwn(file, 'gateway') ||
250
+ Object.hasOwn(file, 'log') ||
251
+ Object.values(file.models ?? {}).some(
252
+ (model) => model && (Object.hasOwn(model, 'features') || Object.hasOwn(model, 'maxOutput')),
253
+ );
254
+
179
255
  // The raw file, before the merge: only known keys, and every section an object. Messages name the path, never the
180
256
  // value. An open map (model aliases, cache TTL labels) takes any key but the forbidden ones.
181
257
  function checkShape(file) {
182
- if (
183
- file &&
184
- typeof file === 'object' &&
185
- (Object.hasOwn(file, 'gateway') ||
186
- Object.hasOwn(file, 'log') ||
187
- Object.values(file.models ?? {}).some(
188
- (model) => model && (Object.hasOwn(model, 'features') || Object.hasOwn(model, 'maxOutput')),
189
- ))
190
- )
191
- throw new Error(
192
- 'router.json uses gateway settings; run node scripts/migrate-config.mjs /path/to/router.json from the router plugin directory',
193
- );
258
+ if (file && typeof file === 'object' && isGatewayLayout(file))
259
+ throw new Error(`router.json uses gateway settings; ${MIGRATION_HINT}`);
260
+ if (file && typeof file === 'object' && Object.hasOwn(file, 'jev'))
261
+ throw new Error(`router.json uses the 1.1 jev section; ${MIGRATION_HINT}`);
194
262
  checkObject(file, 'router.json', Object.keys(DEFAULTS));
195
- const { routes, models, cache, policy, jev, context } = file;
263
+ const { routes, models, cache, policy, classifiers, context } = file;
196
264
  if (routes !== undefined) {
197
265
  checkObject(routes, 'routes', TIERS);
198
266
  for (const [tier, route] of Object.entries(routes)) checkObject(route, `routes.${tier}`, ROUTE_KEYS);
@@ -207,7 +275,10 @@ function checkShape(file) {
207
275
  if (cache[map] !== undefined) checkObject(cache[map], `cache.${map}`);
208
276
  }
209
277
  if (policy !== undefined) checkObject(policy, 'policy', Object.keys(DEFAULTS.policy));
210
- if (jev !== undefined) checkObject(jev, 'jev', Object.keys(DEFAULTS.jev));
278
+ if (classifiers !== undefined) {
279
+ checkObject(classifiers, 'classifiers');
280
+ for (const [id, entry] of Object.entries(classifiers)) checkObject(entry, `classifiers.${id}`, CLASSIFIER_KEYS);
281
+ }
211
282
  if (context !== undefined) checkObject(context, 'context', Object.keys(DEFAULTS.context));
212
283
  }
213
284
 
@@ -260,16 +331,31 @@ function validate(config) {
260
331
  if (!isNumber(p[`${side}PivotUsd`], 0, { above: true })) throw new Error(`policy.${side}PivotUsd must be positive`);
261
332
  }
262
333
  if (!isNumber(p.cashCapUsd, 0)) throw new Error('policy.cashCapUsd must be a non-negative number');
263
- const { cache, jev, context } = config;
334
+ const { cache, context } = config;
264
335
  for (const map of ['writeMultiplier', 'ttlMs']) {
265
336
  for (const [ttl, value] of Object.entries(cache[map]))
266
337
  if (!isNumber(value, 0, { above: true })) throw new Error(`cache.${map}.${ttl} must be positive`);
267
338
  }
268
339
  if (!isNumber(cache.warmMarginMs, 0)) throw new Error('cache.warmMarginMs must be a non-negative number');
269
- if (!isNumber(jev.timeoutMs, 0, { above: true })) throw new Error('jev.timeoutMs must be positive');
270
- for (const field of ['endpoint', 'model']) {
271
- if (typeof jev[field] !== 'string' || !jev[field]) throw new Error(`jev.${field} is required`);
340
+ for (const [id, entry] of Object.entries(config.classifiers)) {
341
+ const at = `classifiers.${id}`;
342
+ for (const field of ['label', 'endpoint', 'model']) {
343
+ if (typeof entry[field] !== 'string' || !entry[field]) throw new Error(`${at}.${field} is required`);
344
+ }
345
+ // The bearer key travels with the request: plain http only to this machine, as the test stubs use.
346
+ if (!/^(https:\/\/|http:\/\/(127\.0\.0\.1|localhost|\[::1\])(:\d+)?\/)/.test(entry.endpoint))
347
+ throw new Error(`${at}.endpoint must be an https URL, or http on localhost`);
348
+ // Only `{name}` placeholders: a stray brace would reach the network as part of the URL.
349
+ if (/[{}]/.test(entry.endpoint.replace(/\{[a-z][a-z0-9_]*\}/g, '')))
350
+ throw new Error(`${at}.endpoint placeholders must look like {lower_snake_case}`);
351
+ if (endpointSettings(entry.endpoint).some((name) => !OPTION_NAMES.includes(name)))
352
+ throw new Error(`${at}.endpoint placeholders must be one of ${OPTION_NAMES.join(', ')}`);
353
+ if (!OPTION_NAMES.includes(entry.keyOption))
354
+ throw new Error(`${at}.keyOption must be one of ${OPTION_NAMES.join(', ')}`);
355
+ if (!isNumber(entry.timeoutMs, 0, { above: true })) throw new Error(`${at}.timeoutMs must be positive`);
272
356
  }
357
+ if (typeof config.classifier !== 'string' || !Object.hasOwn(config.classifiers, config.classifier))
358
+ throw new Error('classifier is not in classifiers');
273
359
  for (const field of ['recentTurns', 'maxTextChars']) {
274
360
  if (!isNumber(context[field], 1, { integer: true })) throw new Error(`context.${field} must be a positive integer`);
275
361
  }
@@ -1,5 +1,5 @@
1
- // Pure Jev wire contract: request body, answer parser and retry rules.
2
- import { TIERS } from './config.mjs';
1
+ // Pure classifier wire contract: request body, answer parser, credentials and retry rules.
2
+ import { activeClassifier, endpointSettings, TIERS } from './config.mjs';
3
3
 
4
4
  const CRITERIA = {
5
5
  micro: {
@@ -47,7 +47,7 @@ export function buildRequest(config, prompt, turns) {
47
47
  for (const tier of TIERS) criteria[tier] = { ...CRITERIA[tier], route: config.routes[tier] };
48
48
  criteria.uncertain = CRITERIA.uncertain;
49
49
  return {
50
- model: config.jev.model,
50
+ model: activeClassifier(config).model,
51
51
  state: { currentRequest: { text: prompt }, recentDialogue: turns },
52
52
  questions: {
53
53
  route: { type: 'choice', instructions: ROUTE_INSTRUCTIONS, criteria },
@@ -65,7 +65,26 @@ export function retryDelayMs(value, now) {
65
65
  return Number.isFinite(at) ? Math.max(0, at - now) : null;
66
66
  }
67
67
 
68
- export function parseAnswers(json) {
68
+ // The key and the filled endpoint of `classifier`, read through `lookup(name)`: a missing key wins over a missing
69
+ // endpoint setting, the order the person fixes them in.
70
+ export async function resolveCredentials(classifier, lookup) {
71
+ const apiKey = await lookup(classifier.keyOption);
72
+ let endpoint = classifier.endpoint;
73
+ for (const name of endpointSettings(classifier.endpoint)) {
74
+ const value = await lookup(name);
75
+ endpoint = value ? endpoint.replaceAll(`{${name}}`, encodeURIComponent(value)) : null;
76
+ if (!endpoint) break;
77
+ }
78
+ return {
79
+ apiKey: apiKey || null,
80
+ endpoint,
81
+ missing: !apiKey ? 'missing-key' : !endpoint ? 'missing-account' : null,
82
+ };
83
+ }
84
+
85
+ // Jev answers bare; Cloudflare's REST API wraps the same answer as `{ result, success, errors, messages }`.
86
+ export function parseAnswers(body) {
87
+ const json = body?.result && !body.answers ? body.result : body;
69
88
  const route = json?.answers?.route;
70
89
  if (
71
90
  route?.type !== 'choice' ||
@@ -73,9 +92,9 @@ export function parseAnswers(json) {
73
92
  typeof route.probabilities !== 'object' ||
74
93
  Array.isArray(route.probabilities)
75
94
  )
76
- throw new Error('jev malformed answer');
95
+ throw new Error('classifier malformed answer');
77
96
  const allowed = new Set([...TIERS, 'uncertain']);
78
- if (!allowed.has(route.choice)) throw new Error('jev unknown choice');
97
+ if (!allowed.has(route.choice)) throw new Error('classifier unknown choice');
79
98
  const probabilities = {};
80
99
  for (const key of allowed) probabilities[key] = clamp(route.probabilities[key]);
81
100
  const continuation = json.answers.continuation?.type === 'noul' ? clamp(json.answers.continuation.noul) : null;
@@ -1,5 +1,5 @@
1
- import { TIERS } from './config.mjs';
2
- import { CLASSIFIER, CLASSIFIER_ERRORS, GATEWAY_SETTINGS, SHORT_REASONS, usageMetrics } from './native-display.mjs';
1
+ import { activeClassifier, TIERS } from './config.mjs';
2
+ import { classifierLabel, classifierStatus, GATEWAY_SETTINGS, SHORT_REASONS, usageMetrics } from './native-display.mjs';
3
3
  import { modelName, TIER_COLOR } from './native-panel.mjs';
4
4
 
5
5
  const BARS = '▂▄▆█';
@@ -8,7 +8,17 @@ const SEPARATOR = ' · ';
8
8
  const ROUTER_BUTTON = 8;
9
9
  const STRIP = 20;
10
10
  // Classifier failures worth a warning; busy and cancelled are routine and pass silently.
11
- const WARNED = new Set(['missing-key', 'paused', 'timeout', 'auth', 'http', 'unreachable', 'policy', 'malformed']);
11
+ const WARNED = new Set([
12
+ 'missing-key',
13
+ 'missing-account',
14
+ 'paused',
15
+ 'timeout',
16
+ 'auth',
17
+ 'http',
18
+ 'unreachable',
19
+ 'policy',
20
+ 'malformed',
21
+ ]);
12
22
 
13
23
  const part = (text, style = {}) => ({ text, style });
14
24
  const button = (props) => ({ button: props });
@@ -57,12 +67,12 @@ function served(view) {
57
67
  return Boolean(actual && selected && actual !== selected && !actual.startsWith(`${selected}-`));
58
68
  }
59
69
 
60
- function supportText(estimate) {
70
+ function supportText(config, estimate) {
61
71
  if (!Number.isFinite(estimate?.threshold)) return null;
62
72
  const mass = Number.isFinite(estimate.upgradeMass) ? estimate.upgradeMass : estimate.downgradeMass;
63
73
  if (!Number.isFinite(mass)) return null;
64
74
  const pct = (n) => `${Math.round(n * 100)}%`;
65
- return `${CLASSIFIER} ${pct(mass)} ${mass >= estimate.threshold ? '≥' : '<'} ${pct(estimate.threshold)}`;
75
+ return `${classifierLabel(config)} ${pct(mass)} ${mass >= estimate.threshold ? '≥' : '<'} ${pct(estimate.threshold)}`;
66
76
  }
67
77
 
68
78
  export function bandSegments(config, view, usage, actions) {
@@ -92,7 +102,12 @@ export function bandSegments(config, view, usage, actions) {
92
102
  if (view.phase === 'choosing')
93
103
  return [
94
104
  segment(0, meter(view.tier, true), part(' choosing for this turn…', { dimColor: true })),
95
- segment(3, part(`${CLASSIFIER} · ${config.jev.timeoutMs / 1000} s deadline`, { dimColor: true })),
105
+ segment(
106
+ 3,
107
+ part(`${classifierLabel(config)} · ${activeClassifier(config).timeoutMs / 1000} s deadline`, {
108
+ dimColor: true,
109
+ }),
110
+ ),
96
111
  ];
97
112
  const pin = view.pendingPin
98
113
  ? segment(
@@ -113,19 +128,21 @@ export function bandSegments(config, view, usage, actions) {
113
128
  part(route(view)),
114
129
  served(view) ? part(' fallback', { color: 'yellow' }) : null,
115
130
  );
131
+ // The warning leads and the route yields: when the band is narrow, what to fix matters more than the model kept,
132
+ // and a long classifier label ("Clef Flash: no account ID") must not push the warning out.
116
133
  if (WARNED.has(view.error))
117
134
  return [
118
- main,
135
+ segment(0, part(`⚠ ${classifierStatus(config, view.error)}`, { color: 'yellow' })),
136
+ view.error === 'missing-key' || view.error === 'missing-account'
137
+ ? segment(0, button({ key: 'band-key', label: 'Set up', onPress: actions.key }))
138
+ : segment(2, button({ key: 'band-fix', label: 'Details', onPress: actions.open })),
139
+ { ...main, priority: 1 },
119
140
  pin,
120
- segment(0, part(`⚠ ${CLASSIFIER} ${CLASSIFIER_ERRORS[view.error]}`, { color: 'yellow' })),
121
141
  segment(3, part('keeping model', { color: 'yellow' })),
122
- view.error === 'missing-key'
123
- ? segment(1, button({ key: 'band-key', label: 'Set key', onPress: actions.key }))
124
- : segment(2, button({ key: 'band-fix', label: 'Details', onPress: actions.open })),
125
142
  ].filter(Boolean);
126
143
  const metrics = usageMetrics(config, view, usage);
127
144
  const percent = (bar) => part(bar.percent === null ? '?' : `${bar.percent}%`, { color: bar.color });
128
- const support = supportText(view.estimate);
145
+ const support = supportText(config, view.estimate);
129
146
  return [
130
147
  main,
131
148
  pin,
@@ -198,9 +215,9 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
198
215
  });
199
216
  }
200
217
 
201
- export function switchToast(previous, next, estimate) {
218
+ export function switchToast(config, previous, next, estimate) {
202
219
  const label = (decision) =>
203
220
  `${modelName(decision.model)}${decision.effort === null || decision.effort === undefined ? '' : ` · ${decision.effort}`}`;
204
- const support = supportText(estimate);
221
+ const support = supportText(config, estimate);
205
222
  return `${label(previous)} → ${label(next)} — ${SHORT_REASONS[next.reason]?.replace(/^\W+\s*/, '') ?? next.reason}${support ? ` (${support})` : ''}`;
206
223
  }
@@ -1,3 +1,4 @@
1
+ import { activeClassifier, CLASSIFIER_OPTIONS, endpointSettings } from './config.mjs';
1
2
  import { tierForModel } from './native-router.mjs';
2
3
 
3
4
  export const REASONS = {
@@ -20,10 +21,40 @@ export const REASONS = {
20
21
  pinned: 'one-turn model pin',
21
22
  };
22
23
 
23
- // The decision model behind the router. Named in the UI only where it explains a reading or a failure.
24
- export const CLASSIFIER = 'Jev';
24
+ // The active decision model behind the router. Named in the UI only where it explains a reading or a failure.
25
+ export const classifierLabel = (config) => activeClassifier(config).label;
26
+ // The credentials error of classifier `id` from the view, or null when its key and endpoint settings are all there.
27
+ // Not yet read counts as a missing key.
28
+ export const missingCredentials = (view, id) =>
29
+ view.credentials && Object.hasOwn(view.credentials, id) ? view.credentials[id] : 'missing-key';
30
+ // What classifier `id` lacks, named by the setting the person enters: "no API token", "no account ID".
31
+ export function missingText(config, id, missing) {
32
+ const entry = config.classifiers[id];
33
+ const option = missing === 'missing-key' ? entry.keyOption : endpointSettings(entry.endpoint)[0];
34
+ return `no ${CLASSIFIER_OPTIONS[option] ?? 'setting'}`;
35
+ }
36
+
37
+ // Every setting the configured classifiers read, grouped by classifier: "Jev: API key · Clef, Clef Flash: API token,
38
+ // account ID".
39
+ export function credentialsNeeded(config) {
40
+ const groups = new Map();
41
+ for (const entry of Object.values(config.classifiers)) {
42
+ const needs = [entry.keyOption, ...endpointSettings(entry.endpoint)].map((option) => CLASSIFIER_OPTIONS[option]);
43
+ const key = needs.join(', ');
44
+ groups.set(key, [...(groups.get(key) ?? []), entry.label]);
45
+ }
46
+ return [...groups].map(([needs, labels]) => `${labels.join(', ')}: ${needs}`).join(' · ');
47
+ }
48
+
49
+ // The active classifier with what went wrong: "Jev: no API key", "Clef timed out". Short: the band keeps it whole.
50
+ export function classifierStatus(config, error) {
51
+ const label = activeClassifier(config).label;
52
+ return error === 'missing-key' || error === 'missing-account'
53
+ ? `${label}: ${missingText(config, config.classifier, error)}`
54
+ : `${label} ${CLASSIFIER_ERRORS[error] ?? error}`;
55
+ }
56
+
25
57
  export const CLASSIFIER_ERRORS = {
26
- 'missing-key': 'key not set',
27
58
  paused: 'paused after failures',
28
59
  timeout: 'timed out',
29
60
  auth: 'rejected the key',
@@ -1,3 +1,4 @@
1
+ import { activeClassifier } from './config.mjs';
1
2
  import {
2
3
  buildRequest,
3
4
  isTransientStatus,
@@ -34,14 +35,15 @@ export class NativeJev {
34
35
  return { failures: this.failures, pausedUntil: this.pausedUntil };
35
36
  }
36
37
 
37
- async ask({ request, sleep, config, apiKey, prompt, turns, signal }) {
38
+ async ask({ request, sleep, config, apiKey, endpoint, prompt, turns, signal }) {
38
39
  if (!apiKey) return { advice: null, error: 'missing-key' };
40
+ if (!endpoint) return { advice: null, error: 'missing-account' };
39
41
  if (signal?.aborted) return { advice: null, error: 'cancelled' };
40
42
  if (this.now() < this.pausedUntil) return { advice: null, error: 'paused' };
41
43
  if (this.active || this.pending) return { advice: null, error: 'busy' };
42
44
  this.active = true;
43
45
  try {
44
- const advice = await this.complete({ request, sleep, config, apiKey, prompt, turns, signal });
46
+ const advice = await this.complete({ request, sleep, config, apiKey, endpoint, prompt, turns, signal });
45
47
  this.failures = 0;
46
48
  this.pausedUntil = 0;
47
49
  return { advice, error: null };
@@ -57,15 +59,15 @@ export class NativeJev {
57
59
  }
58
60
  }
59
61
 
60
- async complete({ request, sleep, config, apiKey, prompt, turns, signal }) {
61
- const deadline = this.now() + config.jev.timeoutMs;
62
+ async complete({ request, sleep, config, apiKey, endpoint, prompt, turns, signal }) {
63
+ const deadline = this.now() + activeClassifier(config).timeoutMs;
62
64
  const body = JSON.stringify(buildRequest(config, prompt, turns));
63
65
  for (let attempt = 0; attempt < 2; attempt += 1) {
64
66
  const remaining = deadline - this.now();
65
67
  if (signal?.aborted) throw new AdviceError('cancelled');
66
68
  if (remaining <= 0) throw new AdviceError('timeout');
67
69
  const call = Promise.resolve().then(() =>
68
- request(config.jev.endpoint, {
70
+ request(endpoint, {
69
71
  method: 'POST',
70
72
  headers: { authorization: `Bearer ${apiKey}`, 'content-type': 'application/json' },
71
73
  body,