@stav-ai/cli 0.1.7 → 0.1.10
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/README.md +20 -6
- package/dist/adapters/index.js +61 -20
- package/dist/adapters/pi-stav-provider.js +2 -1
- package/dist/agent/index.js +626 -0
- package/dist/agent/pairings.js +83 -0
- package/dist/agent/passkey.js +86 -0
- package/dist/agent/questions.js +232 -0
- package/dist/agent/relay.js +224 -0
- package/dist/agent/steps.js +131 -0
- package/dist/agent/wire.js +41 -0
- package/dist/app-names.js +54 -0
- package/dist/cli.js +41 -7
- package/dist/config.js +1 -0
- package/dist/http.js +2 -1
- package/dist/launch.js +27 -8
- package/dist/noise.js +225 -0
- package/dist/prompt.js +119 -0
- package/dist/stav.js +3 -3
- package/dist/store/index.js +10 -0
- package/dist/update-check.js +67 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/prompt.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** Questions in the terminal. Never asked when there is no terminal. */
|
|
2
2
|
import { createInterface } from 'node:readline/promises';
|
|
3
|
+
import { Writable } from 'node:stream';
|
|
4
|
+
import { StavError } from './errors.js';
|
|
3
5
|
import { terminalText } from './terminal-text.js';
|
|
4
6
|
export const terminal = {
|
|
5
7
|
interactive: Boolean(process.stdin.isTTY && process.stderr.isTTY),
|
|
@@ -17,3 +19,120 @@ export async function confirm(p, question) {
|
|
|
17
19
|
const answer = (await p.ask(`${question} [y/N] `)).toLowerCase();
|
|
18
20
|
return answer === 'y' || answer === 'yes';
|
|
19
21
|
}
|
|
22
|
+
/** After a line, this long without a key means that it was not the first of a paste. */
|
|
23
|
+
const PASTE_MS = 100;
|
|
24
|
+
/**
|
|
25
|
+
* A question on stdout, for what must not go to a stderr that is sent
|
|
26
|
+
* elsewhere. `hidden` shows nothing of what is typed. `signal` takes the
|
|
27
|
+
* question back. `freshMs` throws away what is typed until no key came for so
|
|
28
|
+
* long: it was meant for something else. `oneLine` refuses a paste of more
|
|
29
|
+
* lines, of which only the first would be the answer, and asks again.
|
|
30
|
+
*/
|
|
31
|
+
export function askHere(question, o = {}, io = { input: process.stdin, output: process.stdout }) {
|
|
32
|
+
let muted = o.freshMs !== undefined;
|
|
33
|
+
const output = new Writable({
|
|
34
|
+
write(chunk, _encoding, done) {
|
|
35
|
+
if (!muted)
|
|
36
|
+
io.output.write(chunk);
|
|
37
|
+
done();
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
// Without the width, an answer over more than one row is drawn wrong.
|
|
41
|
+
Object.defineProperty(output, 'columns', { get: () => io.output.columns });
|
|
42
|
+
const resized = () => output.emit('resize');
|
|
43
|
+
io.output.on('resize', resized);
|
|
44
|
+
const rl = createInterface({ input: io.input, output, terminal: true, historySize: 0 });
|
|
45
|
+
let answered = false;
|
|
46
|
+
/** Waits until no key came for `ms`. */
|
|
47
|
+
const quiet = (ms) => new Promise((resolve, reject) => {
|
|
48
|
+
const stop = () => {
|
|
49
|
+
clearTimeout(timer);
|
|
50
|
+
io.input.off('keypress', key);
|
|
51
|
+
o.signal?.removeEventListener('abort', aborted);
|
|
52
|
+
};
|
|
53
|
+
const over = () => {
|
|
54
|
+
stop();
|
|
55
|
+
resolve();
|
|
56
|
+
};
|
|
57
|
+
const aborted = () => {
|
|
58
|
+
stop();
|
|
59
|
+
reject(o.signal.reason);
|
|
60
|
+
};
|
|
61
|
+
let timer = setTimeout(over, ms);
|
|
62
|
+
const key = () => {
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
timer = setTimeout(over, ms);
|
|
65
|
+
};
|
|
66
|
+
io.input.on('keypress', key);
|
|
67
|
+
if (o.signal?.aborted)
|
|
68
|
+
aborted();
|
|
69
|
+
else
|
|
70
|
+
o.signal?.addEventListener('abort', aborted, { once: true });
|
|
71
|
+
});
|
|
72
|
+
/** Takes away a half line. Whole ones went to nobody. */
|
|
73
|
+
const clear = () => {
|
|
74
|
+
rl.write(null, { ctrl: true, name: 'e' });
|
|
75
|
+
rl.write(null, { ctrl: true, name: 'u' });
|
|
76
|
+
};
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
// Raw mode takes Ctrl-C as a key: give it back as the signal it would be.
|
|
79
|
+
// Whoever handles that ends the program, so the question never ends.
|
|
80
|
+
let interrupted = false;
|
|
81
|
+
rl.once('SIGINT', () => {
|
|
82
|
+
interrupted = true;
|
|
83
|
+
rl.close();
|
|
84
|
+
io.output.write('\n');
|
|
85
|
+
// A handler is called at once: the signal itself could come after the last thing to wait for.
|
|
86
|
+
if (!process.emit('SIGINT', 'SIGINT'))
|
|
87
|
+
process.kill(process.pid, 'SIGINT');
|
|
88
|
+
});
|
|
89
|
+
const fail = () => {
|
|
90
|
+
if (!interrupted)
|
|
91
|
+
reject(o.signal?.aborted ? o.signal.reason : new StavError('Cancelled.', 'cancelled'));
|
|
92
|
+
};
|
|
93
|
+
rl.once('close', fail);
|
|
94
|
+
const ask = async () => {
|
|
95
|
+
if (o.freshMs !== undefined) {
|
|
96
|
+
await quiet(o.freshMs);
|
|
97
|
+
clear();
|
|
98
|
+
muted = false;
|
|
99
|
+
}
|
|
100
|
+
for (;;) {
|
|
101
|
+
const pending = rl.question(terminalText(question), { signal: o.signal });
|
|
102
|
+
muted = o.hidden === true;
|
|
103
|
+
let lines = 0;
|
|
104
|
+
const count = () => {
|
|
105
|
+
lines++;
|
|
106
|
+
if (o.oneLine)
|
|
107
|
+
muted = true; // the rest of a paste
|
|
108
|
+
};
|
|
109
|
+
rl.on('line', count);
|
|
110
|
+
try {
|
|
111
|
+
const answer = await pending;
|
|
112
|
+
if (!o.oneLine)
|
|
113
|
+
return answer;
|
|
114
|
+
muted = true;
|
|
115
|
+
await quiet(o.pasteMs ?? PASTE_MS);
|
|
116
|
+
const more = lines > 0 || rl.line !== '';
|
|
117
|
+
clear();
|
|
118
|
+
muted = false;
|
|
119
|
+
if (!more)
|
|
120
|
+
return answer;
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
rl.off('line', count);
|
|
124
|
+
}
|
|
125
|
+
io.output.write('An answer is one line, and that was more. Nothing was sent.\n');
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
ask().then((answer) => {
|
|
129
|
+
answered = true;
|
|
130
|
+
resolve(answer.trim());
|
|
131
|
+
}, fail);
|
|
132
|
+
}).finally(() => {
|
|
133
|
+
io.output.off('resize', resized);
|
|
134
|
+
rl.close();
|
|
135
|
+
if (muted || !answered)
|
|
136
|
+
io.output.write('\n');
|
|
137
|
+
});
|
|
138
|
+
}
|
package/dist/stav.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** The Stav calls the CLI makes, typed. */
|
|
2
|
-
import { MODEL_RE } from './adapters/index.js';
|
|
2
|
+
import { isRouted, MODEL_RE } from './adapters/index.js';
|
|
3
3
|
import { StavError } from './errors.js';
|
|
4
4
|
import { request } from './http.js';
|
|
5
5
|
/**
|
|
@@ -78,11 +78,11 @@ export class Stav {
|
|
|
78
78
|
if (!Array.isArray(m.stav_surfaces) || !m.stav_surfaces.includes('chat'))
|
|
79
79
|
continue;
|
|
80
80
|
seen.add(m.id);
|
|
81
|
-
const router = m.id
|
|
81
|
+
const router = isRouted(m.id);
|
|
82
82
|
const n = m.context_length;
|
|
83
83
|
out.push({ id: m.id, contextWindow: !router && typeof n === 'number' && Number.isInteger(n) && n > 0 ? n : null });
|
|
84
84
|
}
|
|
85
|
-
const rank = (id) => (id
|
|
85
|
+
const rank = (id) => (!isRouted(id) ? 2 : id.startsWith('@') ? 1 : 0);
|
|
86
86
|
// Array.prototype.sort is stable: the gateway's order holds inside each group.
|
|
87
87
|
return out.sort((a, b) => rank(a.id) - rank(b.id));
|
|
88
88
|
}
|
package/dist/store/index.js
CHANGED
|
@@ -36,6 +36,16 @@ export class FileStore {
|
|
|
36
36
|
return {};
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
+
/** The file is there but cannot be read: every name in it would look missing. */
|
|
40
|
+
async damaged() {
|
|
41
|
+
try {
|
|
42
|
+
const parsed = JSON.parse(await fs.readFile(this.file, 'utf8'));
|
|
43
|
+
return !parsed || typeof parsed !== 'object';
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
return e.code !== 'ENOENT';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
39
49
|
async write(data) {
|
|
40
50
|
await fs.mkdir(path.dirname(this.file), { recursive: true, mode: 0o700 });
|
|
41
51
|
const tmp = `${this.file}.${process.pid}.tmp`;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "A new version is available": asks the npm registry for the newest version
|
|
3
|
+
* of this package, at most once a day, and says so before the command runs
|
|
4
|
+
* (DECIDE-APPS-V).
|
|
5
|
+
*
|
|
6
|
+
* The request is a plain GET of a public address: no key, no sign-in, no
|
|
7
|
+
* version and no machine details. It can never fail a command and holds one
|
|
8
|
+
* up for at most a second, once a day. Nothing from the answer is printed
|
|
9
|
+
* except a version number of the form x.y.z.
|
|
10
|
+
*/
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
import { configDir, FileStore } from './store/index.js';
|
|
14
|
+
import { compareVersions } from './version-compare.js';
|
|
15
|
+
import { VERSION } from './version.js';
|
|
16
|
+
export const DIST_TAGS_URL = 'https://registry.npmjs.org/-/package/@stav-ai/cli/dist-tags';
|
|
17
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
18
|
+
const WAIT_MS = 1000;
|
|
19
|
+
const RELEASE = /^[0-9]{1,5}\.[0-9]{1,5}\.[0-9]{1,5}$/;
|
|
20
|
+
/** How to update this copy: npx keeps its own, which a global install does not change. */
|
|
21
|
+
export function updateCommand(self = fileURLToPath(import.meta.url)) {
|
|
22
|
+
return self.split(/[\\/]/).includes('_npx') ? 'npx @stav-ai/cli@latest' : 'npm install -g @stav-ai/cli@latest';
|
|
23
|
+
}
|
|
24
|
+
async function newest(fetchFn) {
|
|
25
|
+
try {
|
|
26
|
+
const res = await fetchFn(DIST_TAGS_URL, { headers: { Accept: 'application/json' }, redirect: 'error', signal: AbortSignal.timeout(WAIT_MS) });
|
|
27
|
+
if (!res.ok)
|
|
28
|
+
return null;
|
|
29
|
+
const latest = (await res.json())?.latest;
|
|
30
|
+
return typeof latest === 'string' && RELEASE.test(latest) ? latest : null;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null; // Offline, a proxy, npm down: ask again tomorrow.
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** The lines to show, or none. Never throws. */
|
|
37
|
+
export async function updateNotice(d) {
|
|
38
|
+
if (!d.isTerminal || d.env.CI || d.env.STAV_NO_UPDATE_CHECK)
|
|
39
|
+
return [];
|
|
40
|
+
const now = (d.now ?? Date.now)();
|
|
41
|
+
const version = d.version ?? VERSION;
|
|
42
|
+
// A file of its own: config.json is read and written whole, and holds the chosen workspace.
|
|
43
|
+
const cache = new FileStore(path.join(configDir(d.env), 'update-check.json'));
|
|
44
|
+
const saved = await cache.readAll();
|
|
45
|
+
let latest = typeof saved.latest === 'string' && RELEASE.test(saved.latest) ? saved.latest : null;
|
|
46
|
+
const age = now - Number(saved.checked_at);
|
|
47
|
+
// A missing or broken time, or one in the future, counts as never asked.
|
|
48
|
+
if (!(age >= 0 && age < DAY_MS)) {
|
|
49
|
+
latest = (await newest(d.fetchFn)) ?? latest;
|
|
50
|
+
try {
|
|
51
|
+
// Saved after a failure too, so being offline costs one wait a day.
|
|
52
|
+
await cache.set('checked_at', String(now));
|
|
53
|
+
if (latest)
|
|
54
|
+
await cache.set('latest', latest);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// A read-only home: the check then runs again next time.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!latest || compareVersions(version, latest) >= 0)
|
|
61
|
+
return [];
|
|
62
|
+
return [
|
|
63
|
+
`A new version of the Stav CLI is available: ${latest} (you have ${version}).`,
|
|
64
|
+
`Update: ${d.command ?? updateCommand()}`,
|
|
65
|
+
'',
|
|
66
|
+
];
|
|
67
|
+
}
|
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.10';
|