@stav-ai/cli 0.1.4 → 0.1.5
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/dist/adapters/index.js +67 -7
- package/dist/adapters/pi-stav-provider.js +29 -13
- package/dist/launch.js +26 -2
- package/dist/stav.js +30 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/adapters/index.js
CHANGED
|
@@ -223,6 +223,53 @@ const deepseekHarness = {
|
|
|
223
223
|
};
|
|
224
224
|
},
|
|
225
225
|
};
|
|
226
|
+
/**
|
|
227
|
+
* The most bytes the model list may take in the one environment variable
|
|
228
|
+
* that carries it. Linux refuses a single variable over 128 KiB
|
|
229
|
+
* (MAX_ARG_STRLEN) and the program would not start; this leaves room for
|
|
230
|
+
* the rest of the value. macOS has no per-variable limit below that. Windows
|
|
231
|
+
* (32,767 characters per variable) is not a concern: `stav launch` refuses
|
|
232
|
+
* win32 before any adapter runs (checkLaunch in launch.ts, code
|
|
233
|
+
* `unsupported_platform`). Lower this before launches on Windows are allowed.
|
|
234
|
+
*/
|
|
235
|
+
export const MODEL_LIST_BUDGET = 96_000;
|
|
236
|
+
/**
|
|
237
|
+
* The models an app's picker offers: the launch's own model first, with its
|
|
238
|
+
* window, then every other one the key may use, in the order Stav gave them
|
|
239
|
+
* (auto, the Routers, the models). Models past ``budget`` bytes, as
|
|
240
|
+
* ``entryBytes`` counts them, are left out and counted in ``dropped``; the
|
|
241
|
+
* launch's model, ``auto`` and the Routers are always kept.
|
|
242
|
+
*/
|
|
243
|
+
export function pickerModels(ctx, defaultContext, entryBytes, budget = MODEL_LIST_BUDGET) {
|
|
244
|
+
const id = ctx.model ?? 'auto';
|
|
245
|
+
const first = { id, contextWindow: ctx.contextWindow && namedModel(ctx.model) ? ctx.contextWindow : defaultContext };
|
|
246
|
+
const out = [first];
|
|
247
|
+
let used = entryBytes(first);
|
|
248
|
+
let dropped = 0;
|
|
249
|
+
for (const m of ctx.models ?? []) {
|
|
250
|
+
if (m.id === id)
|
|
251
|
+
continue;
|
|
252
|
+
const entry = { id: m.id, contextWindow: m.contextWindow ?? defaultContext };
|
|
253
|
+
const n = entryBytes(entry);
|
|
254
|
+
if (namedModel(m.id) && used + n > budget) {
|
|
255
|
+
dropped += 1;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
used += n;
|
|
259
|
+
out.push(entry);
|
|
260
|
+
}
|
|
261
|
+
return { models: out, dropped };
|
|
262
|
+
}
|
|
263
|
+
/** The plan's line about the picker, for the preview and the run. */
|
|
264
|
+
function describePicker(models, dropped, ctx, picker) {
|
|
265
|
+
if (ctx.models === undefined)
|
|
266
|
+
return `every chat model and Router your key may use, read from Stav, to pick from with ${picker}`;
|
|
267
|
+
if (ctx.models === null)
|
|
268
|
+
return `only this model in ${picker}: the list of models could not be read from Stav`;
|
|
269
|
+
const more = models.length - 1;
|
|
270
|
+
const left = dropped ? ` (${dropped} more models left out: the list would not fit in one environment variable)` : '';
|
|
271
|
+
return `${more} more models and Routers your key may use, to pick from with ${picker}${left}`;
|
|
272
|
+
}
|
|
226
273
|
/** The provider id the kilo-code adapter gives Stav; one only the CLI uses, so a "stav" block a person made from the guide never merges into it. */
|
|
227
274
|
export const KILO_PROVIDER = 'stav-cli';
|
|
228
275
|
/**
|
|
@@ -244,7 +291,9 @@ const kiloCode = {
|
|
|
244
291
|
program: 'kilo',
|
|
245
292
|
scopes: ['inference:chat'],
|
|
246
293
|
wantsContextWindow: namedModel,
|
|
247
|
-
|
|
294
|
+
wantsModelList: true,
|
|
295
|
+
plan(ctx) {
|
|
296
|
+
const { gatewayUrl, key, model, contextWindow } = ctx;
|
|
248
297
|
// Kilo Code merges KILO_CONFIG_CONTENT after the global and the project
|
|
249
298
|
// config files, and resolves {env:} in it (checked with Kilo Code
|
|
250
299
|
// 7.8.40), so the whole provider is given in the environment and no file
|
|
@@ -257,7 +306,6 @@ const kiloCode = {
|
|
|
257
306
|
const base = `${gatewayUrl}/v1`;
|
|
258
307
|
const id = model ?? 'auto';
|
|
259
308
|
const ref = `${KILO_PROVIDER}/${id}`;
|
|
260
|
-
const window = contextWindow && namedModel(model) ? contextWindow : KILO_DEFAULT_CONTEXT;
|
|
261
309
|
// The config files merge deep, so a "stav-cli" block in the person's own
|
|
262
310
|
// config keeps every key this one does not set. The SDK and the address
|
|
263
311
|
// are therefore set at both levels Kilo Code reads them from (provider and
|
|
@@ -265,7 +313,12 @@ const kiloCode = {
|
|
|
265
313
|
// reach Stav, never anywhere else. Only a Kilo organisation's console
|
|
266
314
|
// config and MDM-managed config merge after KILO_CONFIG_CONTENT; those are
|
|
267
315
|
// set by the person's administrator and are meant to win.
|
|
316
|
+
// Every chat model, Router and auto the key may use is listed, so
|
|
317
|
+
// Kilo's model picker (/models) offers them all; the launch's model is
|
|
318
|
+
// the one selected.
|
|
268
319
|
const sdk = '@ai-sdk/openai-compatible';
|
|
320
|
+
const entry = (m) => ({ name: m.id, provider: { npm: sdk, api: base }, limit: { context: m.contextWindow ?? KILO_DEFAULT_CONTEXT, output: KILO_MAX_OUTPUT } });
|
|
321
|
+
const picker = pickerModels(ctx, KILO_DEFAULT_CONTEXT, (m) => JSON.stringify({ [m.id]: entry(m) }).length);
|
|
269
322
|
const config = {
|
|
270
323
|
model: ref,
|
|
271
324
|
small_model: ref,
|
|
@@ -276,9 +329,7 @@ const kiloCode = {
|
|
|
276
329
|
api: base,
|
|
277
330
|
name: 'Stav',
|
|
278
331
|
options: { apiKey: '{env:STAV_KILO_API_KEY}', baseURL: base },
|
|
279
|
-
models:
|
|
280
|
-
[id]: { name: id, provider: { npm: sdk, api: base }, limit: { context: window, output: KILO_MAX_OUTPUT } },
|
|
281
|
-
},
|
|
332
|
+
models: Object.fromEntries(picker.models.map((m) => [m.id, entry(m)])),
|
|
282
333
|
},
|
|
283
334
|
},
|
|
284
335
|
};
|
|
@@ -305,6 +356,7 @@ const kiloCode = {
|
|
|
305
356
|
'STAV_KILO_API_KEY=<the key, in the environment only>',
|
|
306
357
|
model ? `model ${model}` : "model auto (your workspace's default Router)",
|
|
307
358
|
describeWindow,
|
|
359
|
+
describePicker(picker.models, picker.dropped, ctx, '/models'),
|
|
308
360
|
`at most ${KILO_MAX_OUTPUT} output tokens per call`,
|
|
309
361
|
'KILO_TELEMETRY_LEVEL=off, KILO_DISABLE_SESSION_INGEST=1, KILO_DISABLE_SHARE=1 (no usage data or session uploads to Kilo)',
|
|
310
362
|
'note: Kilo Code runs commands and edits files. It asks before most shell commands, unless your Kilo config allows them (Kilo writes bash: allow into a config file that existed before its first start)',
|
|
@@ -400,7 +452,9 @@ const pi = {
|
|
|
400
452
|
program: 'pi',
|
|
401
453
|
scopes: ['inference:chat'],
|
|
402
454
|
wantsContextWindow: namedModel,
|
|
403
|
-
|
|
455
|
+
wantsModelList: true,
|
|
456
|
+
plan(ctx) {
|
|
457
|
+
const { gatewayUrl, key, model, contextWindow } = ctx;
|
|
404
458
|
// pi reads a custom provider's base URL only from models.json, so the
|
|
405
459
|
// shipped extension registers a "stav-cli" provider from the STAV_PI_*
|
|
406
460
|
// variables (checked with pi 1.7.0). --provider is always given: Stav
|
|
@@ -409,6 +463,10 @@ const pi = {
|
|
|
409
463
|
const base = `${gatewayUrl}/v1`;
|
|
410
464
|
const id = model ?? 'auto';
|
|
411
465
|
const window = contextWindow && namedModel(model) ? contextWindow : PI_DEFAULT_CONTEXT;
|
|
466
|
+
// Every chat model, Router and auto the key may use, for pi's model
|
|
467
|
+
// picker (/model), in STAV_PI_MODELS; the launch's model comes first.
|
|
468
|
+
const toEnv = (m) => ({ id: m.id, contextWindow: m.contextWindow ?? PI_DEFAULT_CONTEXT });
|
|
469
|
+
const picker = pickerModels(ctx, PI_DEFAULT_CONTEXT, (m) => JSON.stringify(toEnv(m)).length + 1);
|
|
412
470
|
// undefined: the preview, before the key exists; null: read, and Stav knows none.
|
|
413
471
|
const describeWindow = contextWindow && namedModel(model)
|
|
414
472
|
? `a context window of ${contextWindow} tokens (the model's in your workspace)`
|
|
@@ -424,6 +482,7 @@ const pi = {
|
|
|
424
482
|
STAV_PI_MODEL: id,
|
|
425
483
|
STAV_PI_CONTEXT_WINDOW: String(window),
|
|
426
484
|
STAV_PI_MAX_TOKENS: String(PI_MAX_TOKENS),
|
|
485
|
+
STAV_PI_MODELS: JSON.stringify(picker.models.map(toEnv)),
|
|
427
486
|
// No model catalogue refresh or update check at pi.dev.
|
|
428
487
|
PI_OFFLINE: '1',
|
|
429
488
|
},
|
|
@@ -433,9 +492,10 @@ const pi = {
|
|
|
433
492
|
'STAV_PI_API_KEY=<the key, in the environment only>',
|
|
434
493
|
model ? `model ${model}` : "model auto (your workspace's default Router)",
|
|
435
494
|
describeWindow,
|
|
495
|
+
describePicker(picker.models, picker.dropped, ctx, '/model'),
|
|
436
496
|
`at most ${PI_MAX_TOKENS} output tokens per call`,
|
|
437
497
|
'PI_OFFLINE=1 (no model catalogue refresh or update check at pi.dev)',
|
|
438
|
-
'note: pi runs the commands the model writes without asking; run it in a folder or container where that is safe',
|
|
498
|
+
'note: pi runs the commands the model writes without asking, and those commands can read STAV_PI_API_KEY from their environment; run it in a folder or container where that is safe',
|
|
439
499
|
'run one task without the chat screen after --, for example: stav launch pi -- -p "fix the failing test"',
|
|
440
500
|
],
|
|
441
501
|
};
|
|
@@ -8,25 +8,41 @@
|
|
|
8
8
|
// registered at startup is ready for --provider/--model on the same run, and
|
|
9
9
|
// ~/.pi/agent is not changed. Checked with pi 1.7.0.
|
|
10
10
|
|
|
11
|
+
// STAV_PI_MODELS is the list for pi's model picker (/model): every chat
|
|
12
|
+
// model, Router and auto the key may use, the launch's model first, each as
|
|
13
|
+
// {id, contextWindow}. Without it, or when it does not read, only
|
|
14
|
+
// STAV_PI_MODEL is offered.
|
|
15
|
+
function pickerModels(env) {
|
|
16
|
+
const one = [{ id: env.STAV_PI_MODEL, contextWindow: Number(env.STAV_PI_CONTEXT_WINDOW) }];
|
|
17
|
+
let list;
|
|
18
|
+
try {
|
|
19
|
+
list = JSON.parse(env.STAV_PI_MODELS ?? "");
|
|
20
|
+
} catch {
|
|
21
|
+
return one;
|
|
22
|
+
}
|
|
23
|
+
if (!Array.isArray(list)) return one;
|
|
24
|
+
const ok = list.filter(
|
|
25
|
+
(m) => m && typeof m.id === "string" && m.id && Number.isInteger(m.contextWindow) && m.contextWindow > 0,
|
|
26
|
+
);
|
|
27
|
+
return ok.length ? ok : one;
|
|
28
|
+
}
|
|
29
|
+
|
|
11
30
|
export default function stavProvider(pi) {
|
|
12
31
|
const env = process.env;
|
|
13
|
-
const model = env.STAV_PI_MODEL;
|
|
14
32
|
pi.registerProvider("stav-cli", {
|
|
15
33
|
name: "Stav",
|
|
16
34
|
baseUrl: env.STAV_PI_BASE_URL,
|
|
17
35
|
apiKey: "$STAV_PI_API_KEY",
|
|
18
36
|
api: "openai-completions",
|
|
19
|
-
models:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
],
|
|
37
|
+
models: pickerModels(env).map((m) => ({
|
|
38
|
+
type: "chat",
|
|
39
|
+
id: m.id,
|
|
40
|
+
name: m.id,
|
|
41
|
+
reasoning: false,
|
|
42
|
+
input: ["text"],
|
|
43
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
44
|
+
contextWindow: m.contextWindow,
|
|
45
|
+
maxTokens: Number(env.STAV_PI_MAX_TOKENS),
|
|
46
|
+
})),
|
|
31
47
|
});
|
|
32
48
|
}
|
package/dist/launch.js
CHANGED
|
@@ -257,8 +257,9 @@ export async function launch(opts, d) {
|
|
|
257
257
|
if (plan.workspace && plan.rememberWorkspace)
|
|
258
258
|
await d.saveSetting('workspace', plan.workspace.id);
|
|
259
259
|
const secret = await settleKey(plan, d);
|
|
260
|
-
|
|
261
|
-
const
|
|
260
|
+
// Both reads at once: each gives up after its own timeout.
|
|
261
|
+
const [contextWindow, models] = await Promise.all([readContextWindow(plan, secret, d), readModelList(plan, secret, d)]);
|
|
262
|
+
const run = plan.adapter.plan({ gatewayUrl: d.cfg.gatewayUrl, key: secret, model: plan.model, contextWindow, models });
|
|
262
263
|
const env = { ...d.env };
|
|
263
264
|
for (const k of run.unset)
|
|
264
265
|
delete env[k];
|
|
@@ -289,6 +290,29 @@ async function readContextWindow(plan, key, d) {
|
|
|
289
290
|
d.log(`Stav does not know the context window of ${plan.model} for your workspace; ${plan.adapter.displayName} starts without the window from Stav (see the plan above for what it uses).`);
|
|
290
291
|
return n;
|
|
291
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* Every chat model, Router and auto the key may use, when the adapter wants
|
|
295
|
+
* them for the app's model picker. Never stops the launch: without the list
|
|
296
|
+
* the app offers only the launch's model, and the person is told.
|
|
297
|
+
* ``undefined`` when the adapter does not want it.
|
|
298
|
+
*/
|
|
299
|
+
async function readModelList(plan, key, d) {
|
|
300
|
+
if (!plan.adapter.wantsModelList)
|
|
301
|
+
return undefined;
|
|
302
|
+
d.log('Reading the models and Routers your key may use from Stav...');
|
|
303
|
+
try {
|
|
304
|
+
const list = await new Stav(d.cfg, d.fetchFn).chatModels(key);
|
|
305
|
+
if (list === null)
|
|
306
|
+
d.log(`Stav did not list the models for this key; ${plan.adapter.displayName} offers only ${plan.model ?? 'auto'}.`);
|
|
307
|
+
else
|
|
308
|
+
d.log(`Stav listed ${list.length} models and Routers for this key.`);
|
|
309
|
+
return list;
|
|
310
|
+
}
|
|
311
|
+
catch (err) {
|
|
312
|
+
d.log(`Could not read the models your key may use (${err instanceof Error ? err.message : String(err)}); ${plan.adapter.displayName} offers only ${plan.model ?? 'auto'}.`);
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
292
316
|
/** PATH lookup without a shell. */
|
|
293
317
|
export function which(program, env = process.env) {
|
|
294
318
|
for (const dir of (env.PATH ?? '').split(path.delimiter)) {
|
package/dist/stav.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** The Stav calls the CLI makes, typed. */
|
|
2
|
+
import { MODEL_RE } from './adapters/index.js';
|
|
2
3
|
import { StavError } from './errors.js';
|
|
3
4
|
import { request } from './http.js';
|
|
4
5
|
/**
|
|
@@ -56,6 +57,35 @@ export class Stav {
|
|
|
56
57
|
const m = await request(this.fetchFn, `${this.cfg.gatewayUrl}/v1/models/${path}`, { token: key, nullOn: [401, 403, 404], timeoutMs: WINDOW_TIMEOUT_MS });
|
|
57
58
|
return ok(m?.context_length) ? m.context_length : null;
|
|
58
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Every chat model the key's workspace may use, with ``auto`` and the
|
|
62
|
+
* workspace's Routers, from the gateway's model list (the key's own
|
|
63
|
+
* policy). Order: ``auto``, then the Routers, then the models in the
|
|
64
|
+
* gateway's order. ``contextWindow`` is the list's ``context_length``:
|
|
65
|
+
* the smallest window any serving endpoint states, so it holds wherever
|
|
66
|
+
* a call lands; null for ``auto``, a Router, or a model with none stated.
|
|
67
|
+
* Null when the gateway refuses the key.
|
|
68
|
+
*/
|
|
69
|
+
async chatModels(key) {
|
|
70
|
+
const list = await request(this.fetchFn, `${this.cfg.gatewayUrl}/v1/models`, { token: key, nullOn: [401, 403, 404], timeoutMs: WINDOW_TIMEOUT_MS });
|
|
71
|
+
if (!list)
|
|
72
|
+
return null;
|
|
73
|
+
const out = [];
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
for (const m of list.data ?? []) {
|
|
76
|
+
if (typeof m.id !== 'string' || !MODEL_RE.test(m.id) || seen.has(m.id))
|
|
77
|
+
continue;
|
|
78
|
+
if (!Array.isArray(m.stav_surfaces) || !m.stav_surfaces.includes('chat'))
|
|
79
|
+
continue;
|
|
80
|
+
seen.add(m.id);
|
|
81
|
+
const router = m.id === 'auto' || m.id.startsWith('@');
|
|
82
|
+
const n = m.context_length;
|
|
83
|
+
out.push({ id: m.id, contextWindow: !router && typeof n === 'number' && Number.isInteger(n) && n > 0 ? n : null });
|
|
84
|
+
}
|
|
85
|
+
const rank = (id) => (id === 'auto' ? 0 : id.startsWith('@') ? 1 : 2);
|
|
86
|
+
// Array.prototype.sort is stable: the gateway's order holds inside each group.
|
|
87
|
+
return out.sort((a, b) => rank(a.id) - rank(b.id));
|
|
88
|
+
}
|
|
59
89
|
/** Whether a key still works, and for which workspace. Null when it does not. */
|
|
60
90
|
checkKey(key) {
|
|
61
91
|
return request(this.fetchFn, `${this.cfg.gatewayUrl}/v1/auth/validate`, { token: key, nullOn: [401, 403] });
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The CLI's own version. Kept equal to package.json by units.test.ts ("VERSION matches package.json"). */
|
|
2
|
-
export const VERSION = '0.1.
|
|
2
|
+
export const VERSION = '0.1.5';
|