@stav-ai/cli 0.0.0-stage → 0.1.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.
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Where the CLI keeps the sign-in and the app keys (DECIDE-APPS-K): the
3
+ * operating system's keychain, with a file only the user can read as the
4
+ * fallback (a Linux server with no secret service). Never in a project
5
+ * folder (R-20).
6
+ *
7
+ * Secrets are stored one entry each. Settings that are not secret (the
8
+ * chosen workspace) live in config.json beside the fallback file.
9
+ */
10
+ import { promises as fs } from 'node:fs';
11
+ import os from 'node:os';
12
+ import path from 'node:path';
13
+ export const SERVICE = 'stav-cli';
14
+ export function configDir(env = process.env, platform = process.platform) {
15
+ if (env.STAV_CONFIG_DIR)
16
+ return env.STAV_CONFIG_DIR;
17
+ if (platform === 'win32' && env.APPDATA)
18
+ return path.join(env.APPDATA, 'stav');
19
+ const base = env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config');
20
+ return path.join(base, 'stav');
21
+ }
22
+ /** A JSON file of name → value, mode 0600, in a 0700 directory. */
23
+ export class FileStore {
24
+ file;
25
+ kind = 'file';
26
+ constructor(file) {
27
+ this.file = file;
28
+ }
29
+ async readAll() {
30
+ try {
31
+ const raw = await fs.readFile(this.file, 'utf8');
32
+ const parsed = JSON.parse(raw);
33
+ return parsed && typeof parsed === 'object' ? parsed : {};
34
+ }
35
+ catch {
36
+ return {};
37
+ }
38
+ }
39
+ async write(data) {
40
+ await fs.mkdir(path.dirname(this.file), { recursive: true, mode: 0o700 });
41
+ const tmp = `${this.file}.${process.pid}.tmp`;
42
+ await fs.writeFile(tmp, JSON.stringify(data, null, 2), { mode: 0o600 });
43
+ await fs.rename(tmp, this.file);
44
+ await fs.chmod(this.file, 0o600).catch(() => undefined);
45
+ }
46
+ async get(name) {
47
+ return (await this.readAll())[name] ?? null;
48
+ }
49
+ async set(name, value) {
50
+ const data = await this.readAll();
51
+ data[name] = value;
52
+ await this.write(data);
53
+ }
54
+ async delete(name) {
55
+ const data = await this.readAll();
56
+ if (name in data) {
57
+ delete data[name];
58
+ await this.write(data);
59
+ }
60
+ }
61
+ }
62
+ /** The OS keychain through @napi-rs/keyring (an optional dependency). */
63
+ export class KeychainStore {
64
+ mod;
65
+ kind = 'keychain';
66
+ constructor(mod) {
67
+ this.mod = mod;
68
+ }
69
+ async get(name) {
70
+ return new this.mod.Entry(SERVICE, name).getPassword() ?? null;
71
+ }
72
+ async set(name, value) {
73
+ new this.mod.Entry(SERVICE, name).setPassword(value);
74
+ }
75
+ async delete(name) {
76
+ await new this.mod.Entry(SERVICE, name).deletePassword();
77
+ }
78
+ }
79
+ export class MemoryStore {
80
+ kind = 'memory';
81
+ data = new Map();
82
+ async get(name) {
83
+ return this.data.get(name) ?? null;
84
+ }
85
+ async set(name, value) {
86
+ this.data.set(name, value);
87
+ }
88
+ async delete(name) {
89
+ this.data.delete(name);
90
+ }
91
+ }
92
+ /**
93
+ * The keychain when it loads and answers, else the file. STAV_STORE=file
94
+ * forces the file (CI, containers).
95
+ */
96
+ export async function openStore(env = process.env) {
97
+ const file = new FileStore(path.join(configDir(env), 'credentials.json'));
98
+ if (env.STAV_STORE === 'file')
99
+ return file;
100
+ try {
101
+ const mod = await import('@napi-rs/keyring');
102
+ const store = new KeychainStore(mod);
103
+ await store.get('probe');
104
+ return store;
105
+ }
106
+ catch {
107
+ return file;
108
+ }
109
+ }
110
+ /** The app keys saved on this machine, one entry each, and an index of their names. */
111
+ const KEY_INDEX = 'keys';
112
+ export function appKeyName(team, slug, person) {
113
+ return `key:${team}:${slug}:${person.toLowerCase()}`;
114
+ }
115
+ async function keyIndex(store) {
116
+ try {
117
+ const raw = await store.get(KEY_INDEX);
118
+ const list = raw ? JSON.parse(raw) : [];
119
+ return Array.isArray(list) ? list.filter((x) => typeof x === 'string') : [];
120
+ }
121
+ catch {
122
+ return [];
123
+ }
124
+ }
125
+ export async function saveAppKey(store, name, value) {
126
+ await store.set(name, value);
127
+ const names = await keyIndex(store);
128
+ if (!names.includes(name))
129
+ await store.set(KEY_INDEX, JSON.stringify([...names, name]));
130
+ }
131
+ /** Remove every saved app key (all workspaces, all people on this account). */
132
+ export async function forgetAppKeys(store) {
133
+ const names = await keyIndex(store);
134
+ for (const n of names)
135
+ await store.delete(n);
136
+ await store.delete(KEY_INDEX);
137
+ return names.length;
138
+ }
139
+ /** Settings that are not secret. */
140
+ export async function readSettings(env = process.env) {
141
+ return new FileStore(path.join(configDir(env), 'config.json')).readAll();
142
+ }
143
+ export async function writeSetting(name, value, env = process.env) {
144
+ const s = new FileStore(path.join(configDir(env), 'config.json'));
145
+ if (value === null)
146
+ await s.delete(name);
147
+ else
148
+ await s.set(name, value);
149
+ }
@@ -0,0 +1,12 @@
1
+ /** x.y.z compare: negative when a < b. Anything unparseable sorts last. */
2
+ export function compareVersions(a, b) {
3
+ const parse = (v) => (/^[0-9]+\.[0-9]+\.[0-9]+$/.test(v) ? v.split('.').map(Number) : null);
4
+ const pa = parse(a);
5
+ const pb = parse(b);
6
+ if (!pa || !pb)
7
+ return pa ? -1 : pb ? 1 : 0;
8
+ for (let i = 0; i < 3; i++)
9
+ if (pa[i] !== pb[i])
10
+ return pa[i] - pb[i];
11
+ return 0;
12
+ }
@@ -0,0 +1,2 @@
1
+ /** The CLI's own version. Kept equal to package.json by version.test.ts. */
2
+ export const VERSION = '0.1.0';
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Which workspace (R-21): a person in more than one picks one, and the CLI
3
+ * never picks for them. Every /api call then sends X-Team-Id, because with
4
+ * no header Stav silently uses the oldest membership.
5
+ */
6
+ import { StavError } from './errors.js';
7
+ /**
8
+ * The chosen workspace: the one named (by id or slug), else the stored
9
+ * choice if still a membership, else the only one, else ask. Without a
10
+ * terminal and with more than one, it refuses and says how to choose.
11
+ */
12
+ export async function chooseWorkspace(all, named, stored, prompter) {
13
+ if (all.length === 0) {
14
+ throw new StavError('You are not in a Stav workspace yet. Create one at https://stav.ai/app, then run this again.', 'no_workspace');
15
+ }
16
+ if (named) {
17
+ const hit = all.find((w) => w.id === named || (w.slug && w.slug === named));
18
+ if (!hit)
19
+ throw new StavError(`You are not a member of a workspace "${named}". Run stav workspace to see yours.`, 'unknown_workspace');
20
+ return hit;
21
+ }
22
+ const kept = stored ? all.find((w) => w.id === stored) : undefined;
23
+ if (kept)
24
+ return kept;
25
+ if (all.length === 1)
26
+ return all[0];
27
+ if (!prompter.interactive) {
28
+ throw new StavError(`You are in ${all.length} workspaces. Choose one with --workspace <slug> or stav workspace <slug>.`, 'workspace_required');
29
+ }
30
+ all.forEach((w, i) => process.stderr.write(` ${i + 1}. ${w.name}${w.slug ? ` (${w.slug})` : ''}, ${w.role.toLowerCase()}\n`));
31
+ for (;;) {
32
+ const answer = await prompter.ask(`Which workspace? 1-${all.length}: `);
33
+ const n = Number.parseInt(answer, 10);
34
+ if (n >= 1 && n <= all.length && String(n) === answer)
35
+ return all[n - 1];
36
+ }
37
+ }
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@stav-ai/cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Run your coding agent on Stav: sign in, get a key for the app, and start it pointed at the Stav gateway.",
5
+ "type": "module",
6
+ "bin": {
7
+ "stav": "dist/cli.js"
8
+ },
9
+ "files": [
10
+ "dist",
11
+ "LICENSE",
12
+ "README.md"
13
+ ],
14
+ "engines": {
15
+ "node": ">=22"
16
+ },
17
+ "scripts": {
18
+ "build": "tsc -p tsconfig.build.json",
19
+ "prepack": "npm run build",
20
+ "typecheck": "tsc --noEmit",
21
+ "test": "tsx --test 'src/**/*.test.ts'",
22
+ "clean": "rm -rf dist"
23
+ },
24
+ "optionalDependencies": {
25
+ "@napi-rs/keyring": "2.1.2"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^22.19.0",
29
+ "tsx": "^4.23.15",
30
+ "typescript": "^7.0.2"
31
+ },
32
+ "keywords": [
33
+ "stav",
34
+ "cli",
35
+ "claude-code",
36
+ "codex",
37
+ "sovereign",
38
+ "inference"
39
+ ],
40
+ "homepage": "https://stav.ai/apps",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/stavhq/stav-cli.git"
44
+ },
45
+ "license": "Apache-2.0",
46
+ "publishConfig": {
47
+ "access": "public"
48
+ }
49
+ }