@stav-ai/cli 0.1.0 → 0.1.2
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 +18 -6
- package/dist/adapters/adapters.json +5 -0
- package/dist/adapters/deepseek-harness.patch.yml +25 -0
- package/dist/adapters/index.js +107 -5
- package/dist/auth/session.js +2 -1
- package/dist/cli.js +5 -2
- package/dist/http.js +1 -1
- package/dist/launch.js +61 -15
- package/dist/prompt.js +2 -1
- package/dist/stav.js +31 -0
- package/dist/terminal-text.js +19 -0
- package/dist/version.js +1 -1
- package/dist/workspace.js +2 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@ needs, and starts the app pointed at Stav.
|
|
|
7
7
|
```sh
|
|
8
8
|
npx @stav-ai/cli launch claude
|
|
9
9
|
npx @stav-ai/cli launch codex --model zai-org/glm-5-2
|
|
10
|
+
npx @stav-ai/cli launch dsh -- "run the tests"
|
|
10
11
|
```
|
|
11
12
|
|
|
12
13
|
Always use the scoped name `@stav-ai/cli`. The unscoped `stav` package on npm
|
|
@@ -23,22 +24,33 @@ is not ours.
|
|
|
23
24
|
| `stav apps` | The apps this CLI can launch |
|
|
24
25
|
| `stav launch <app> [--model <id>] [--workspace <slug>] [--yes] [-- <app arguments>]` | Start an app on Stav |
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
(`
|
|
27
|
+
It launches **Claude Code** (`claude`), **Codex** (`codex`) and
|
|
28
|
+
**DeepSeek Harness** (`dsh`). The app must already be installed.
|
|
28
29
|
|
|
29
30
|
## What `stav launch` does
|
|
30
31
|
|
|
31
32
|
1. Checks the app can be launched, and that your workspace has credits.
|
|
32
33
|
2. Shows what it will do, and asks once.
|
|
33
34
|
3. Makes an API key for the app, or uses the one it saved last time:
|
|
34
|
-
- a
|
|
35
|
-
|
|
35
|
+
- a personal key, under your workspace's key policy; if that policy
|
|
36
|
+
refuses it and you are an owner or admin, a workspace key instead;
|
|
36
37
|
- named for the app and you, marked with the app in your request log;
|
|
37
38
|
- with only the scope the app needs (`inference:messages` for Claude Code,
|
|
38
|
-
`inference:chat` for Codex).
|
|
39
|
+
`inference:chat` for Codex and DeepSeek Harness).
|
|
39
40
|
4. Starts the app with the Stav gateway and the key in its environment.
|
|
40
41
|
**No file is written.** Your `~/.claude` and `~/.codex/config.toml` are not
|
|
41
|
-
changed.
|
|
42
|
+
changed. For a model Claude Code does not know, the CLI also tells it the
|
|
43
|
+
model's context window (`CLAUDE_CODE_MAX_CONTEXT_TOKENS`, the largest
|
|
44
|
+
window Stav serves that model at for your workspace).
|
|
45
|
+
|
|
46
|
+
DeepSeek Harness reads its model providers from a file, so the CLI starts
|
|
47
|
+
its one-shot command `dsh --profile headless` with `--patch` and a file
|
|
48
|
+
that ships with the CLI. That file holds no key and no address: every
|
|
49
|
+
value comes from the environment. Your `~/.dsh` is not changed. Give the
|
|
50
|
+
task after `--`. The CLI also turns off DeepSeek Harness feedback uploads
|
|
51
|
+
(`DSH_TELEMETRY_MODE=DISABLED`). DeepSeek Harness runs the commands the
|
|
52
|
+
model writes without asking. Its own `SAFETY.md` says to run it in a
|
|
53
|
+
container or a VM.
|
|
42
54
|
|
|
43
55
|
The key is kept in your operating system's keychain, or in a file only you
|
|
44
56
|
can read when there is no keychain. Never in your project folder.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# The Stav provider for DeepSeek Harness, given to `dsh --patch` by the
|
|
2
|
+
# deepseek-harness adapter in index.ts. Shipped with the CLI and never
|
|
3
|
+
# written at run time: every value comes from the environment the CLI sets
|
|
4
|
+
# for the child, so the key is never in this file or in an argument (R-20).
|
|
5
|
+
#
|
|
6
|
+
# A --patch overlay is applied after the profile's own cordis.patch.yml and
|
|
7
|
+
# $DSH_HOME/cordis.patch.yml, and replaces each entry's config: for this run
|
|
8
|
+
# Stav is the only model provider and the default model. ~/.dsh is not
|
|
9
|
+
# changed. Checked with dsh 0.2.2-rc.5 (`--dump-config`, and a run).
|
|
10
|
+
- id: llm-pi-ai
|
|
11
|
+
config:
|
|
12
|
+
providers:
|
|
13
|
+
stav-cli:
|
|
14
|
+
displayName: Stav
|
|
15
|
+
apiKeyEnv: STAV_DSH_API_KEY
|
|
16
|
+
api: openai-completions
|
|
17
|
+
baseURL: !!js process.env.STAV_DSH_BASE_URL
|
|
18
|
+
models:
|
|
19
|
+
- id: !!js process.env.STAV_DSH_MODEL
|
|
20
|
+
contextWindow: !!js Number(process.env.STAV_DSH_CONTEXT_WINDOW)
|
|
21
|
+
maxTokens: !!js Number(process.env.STAV_DSH_MAX_TOKENS)
|
|
22
|
+
- id: agent-default-model
|
|
23
|
+
config:
|
|
24
|
+
provider: stav-cli
|
|
25
|
+
model: !!js process.env.STAV_DSH_MODEL
|
package/dist/adapters/index.js
CHANGED
|
@@ -4,8 +4,10 @@
|
|
|
4
4
|
* A catalogue row's recipe only picks one of these by name and may give a
|
|
5
5
|
* default model. Everything that runs on the person's machine — the program,
|
|
6
6
|
* its environment and its arguments — is here, reviewed and released with
|
|
7
|
-
* the CLI. No adapter writes a file
|
|
8
|
-
* environment and the command line
|
|
7
|
+
* the CLI. No adapter writes a file: Claude Code and Codex take everything
|
|
8
|
+
* from the environment and the command line, and DeepSeek Harness reads a
|
|
9
|
+
* fixed overlay file shipped with the CLI (deepseek-harness.patch.yml) whose
|
|
10
|
+
* values all come from the environment. The key goes only into the child's
|
|
9
11
|
* environment, never into its arguments or a file (R-20).
|
|
10
12
|
*
|
|
11
13
|
* adapters.json is byte-identical to the Stav API's
|
|
@@ -15,7 +17,25 @@
|
|
|
15
17
|
import { promises as fs } from 'node:fs';
|
|
16
18
|
import os from 'node:os';
|
|
17
19
|
import path from 'node:path';
|
|
20
|
+
import { fileURLToPath } from 'node:url';
|
|
18
21
|
import list from './adapters.json' with { type: 'json' };
|
|
22
|
+
/**
|
|
23
|
+
* A model Claude Code knows by name brings its own window, and for one of
|
|
24
|
+
* those CLAUDE_CODE_MAX_CONTEXT_TOKENS is ignored unless compaction is off.
|
|
25
|
+
* A bare claude-* id or an anthropic/claude-* id is left alone.
|
|
26
|
+
*/
|
|
27
|
+
function claudeKnows(model) {
|
|
28
|
+
return /^(anthropic\/)?claude-/.test(model);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Whether Claude Code should be told this model's window: a model it does
|
|
32
|
+
* not know, and not "auto" or an @workspace/router (the endpoint list then
|
|
33
|
+
* spans every model the Router may pick, and the largest is not the one
|
|
34
|
+
* served).
|
|
35
|
+
*/
|
|
36
|
+
function claudeNeedsWindow(model) {
|
|
37
|
+
return model !== null && model !== 'auto' && !model.startsWith('@') && !claudeKnows(model);
|
|
38
|
+
}
|
|
19
39
|
/** A model id as the gateway takes it; mirrors launch_recipes.MODEL_RE. */
|
|
20
40
|
export const MODEL_RE = /^[a-z0-9@][a-z0-9._/@:-]{0,127}$/;
|
|
21
41
|
const claudeCode = {
|
|
@@ -24,7 +44,8 @@ const claudeCode = {
|
|
|
24
44
|
displayName: 'Claude Code',
|
|
25
45
|
program: 'claude',
|
|
26
46
|
scopes: ['inference:messages'],
|
|
27
|
-
|
|
47
|
+
wantsContextWindow: claudeNeedsWindow,
|
|
48
|
+
plan({ gatewayUrl, key, model, contextWindow }) {
|
|
28
49
|
// Claude Code adds /v1 itself: the base URL is the origin. AUTH_TOKEN is
|
|
29
50
|
// sent as a Bearer with no approval prompt; API_KEY would ask once, and
|
|
30
51
|
// both together are ambiguous, so API_KEY is removed.
|
|
@@ -36,7 +57,22 @@ const claudeCode = {
|
|
|
36
57
|
env.ANTHROPIC_MODEL = model;
|
|
37
58
|
env.ANTHROPIC_SMALL_FAST_MODEL = model;
|
|
38
59
|
env.ANTHROPIC_DEFAULT_HAIKU_MODEL = model;
|
|
60
|
+
// Claude Code does not know a non-Claude model's window and assumes
|
|
61
|
+
// 200k. The real one lets it compact at the right point.
|
|
62
|
+
if (contextWindow && claudeNeedsWindow(model))
|
|
63
|
+
env.CLAUDE_CODE_MAX_CONTEXT_TOKENS = String(contextWindow);
|
|
39
64
|
}
|
|
65
|
+
// A window from the person's own shell (another gateway, another model)
|
|
66
|
+
// must never reach a Stav launch: with none from Stav, whatever the
|
|
67
|
+
// model, Claude Code uses its own figure.
|
|
68
|
+
const unsetWindow = !env.CLAUDE_CODE_MAX_CONTEXT_TOKENS ? ['CLAUDE_CODE_MAX_CONTEXT_TOKENS'] : [];
|
|
69
|
+
// The preview runs before the key exists, so the window is not known
|
|
70
|
+
// yet (contextWindow undefined): it is named, not given.
|
|
71
|
+
const describeWindow = env.CLAUDE_CODE_MAX_CONTEXT_TOKENS
|
|
72
|
+
? [`CLAUDE_CODE_MAX_CONTEXT_TOKENS=${env.CLAUDE_CODE_MAX_CONTEXT_TOKENS} (the model's context window in your workspace)`]
|
|
73
|
+
: contextWindow === undefined && claudeNeedsWindow(model)
|
|
74
|
+
? ["CLAUDE_CODE_MAX_CONTEXT_TOKENS=<the model's largest context window in your workspace, read from Stav; a long chat then sends more tokens per turn before it is compacted>"]
|
|
75
|
+
: [];
|
|
40
76
|
return {
|
|
41
77
|
program: 'claude',
|
|
42
78
|
args: [],
|
|
@@ -46,11 +82,13 @@ const claudeCode = {
|
|
|
46
82
|
'ANTHROPIC_API_KEY',
|
|
47
83
|
'CLAUDE_CODE_USE_BEDROCK', 'CLAUDE_CODE_USE_VERTEX', 'CLAUDE_CODE_USE_FOUNDRY',
|
|
48
84
|
'ANTHROPIC_BEDROCK_BASE_URL', 'ANTHROPIC_VERTEX_BASE_URL', 'ANTHROPIC_FOUNDRY_BASE_URL',
|
|
85
|
+
...unsetWindow,
|
|
49
86
|
],
|
|
50
87
|
describe: [
|
|
51
88
|
`ANTHROPIC_BASE_URL=${gatewayUrl}`,
|
|
52
89
|
'ANTHROPIC_AUTH_TOKEN=<the key, in the environment only>',
|
|
53
90
|
model ? `model ${model}` : "Claude Code's own default model",
|
|
91
|
+
...describeWindow,
|
|
54
92
|
],
|
|
55
93
|
};
|
|
56
94
|
},
|
|
@@ -117,9 +155,73 @@ const codex = {
|
|
|
117
155
|
};
|
|
118
156
|
},
|
|
119
157
|
};
|
|
120
|
-
|
|
158
|
+
/** The overlay DeepSeek Harness reads; built next to this module (npm run build copies it). */
|
|
159
|
+
export const DSH_PATCH_FILE = fileURLToPath(new URL('./deepseek-harness.patch.yml', import.meta.url));
|
|
160
|
+
/** dsh's own guess when nothing sizes a model (its defaultContextWindow). */
|
|
161
|
+
const DSH_DEFAULT_CONTEXT_WINDOW = 1_000_000;
|
|
162
|
+
/**
|
|
163
|
+
* The output cap dsh asks for on every call. Without one it asks for 128,000,
|
|
164
|
+
* which many models refuse; 32,000 is the value the app test proved.
|
|
165
|
+
*/
|
|
166
|
+
export const DSH_MAX_TOKENS = 32_000;
|
|
167
|
+
/** A named model, not "auto" or an @workspace/router. */
|
|
168
|
+
function namedModel(model) {
|
|
169
|
+
return model !== null && model !== 'auto' && !model.startsWith('@');
|
|
170
|
+
}
|
|
171
|
+
const deepseekHarness = {
|
|
172
|
+
name: 'deepseek-harness',
|
|
173
|
+
apps: ['deepseek-harness'],
|
|
174
|
+
displayName: 'DeepSeek Harness',
|
|
175
|
+
program: 'dsh',
|
|
176
|
+
scopes: ['inference:chat'],
|
|
177
|
+
wantsContextWindow: namedModel,
|
|
178
|
+
plan({ gatewayUrl, key, model, contextWindow }) {
|
|
179
|
+
// dsh reads model providers only from patch files. --patch adds the
|
|
180
|
+
// shipped overlay for this run; its values come from the STAV_DSH_*
|
|
181
|
+
// variables, a set no other tool uses, so a key dsh has stored or a
|
|
182
|
+
// ~/.dsh/.env cannot stand in for the Stav one. Only the one-shot
|
|
183
|
+
// headless profile was tested, so that is the one started. With no model,
|
|
184
|
+
// "auto" (the workspace's default Router): dsh's own default is
|
|
185
|
+
// DeepSeek's cloud, which must never get the run.
|
|
186
|
+
const base = `${gatewayUrl}/v1`;
|
|
187
|
+
const id = model ?? 'auto';
|
|
188
|
+
const window = contextWindow && namedModel(model) ? contextWindow : DSH_DEFAULT_CONTEXT_WINDOW;
|
|
189
|
+
// undefined: the preview, before the key exists (launch.ts passes no
|
|
190
|
+
// window); null: read, and Stav knows none.
|
|
191
|
+
const describeWindow = contextWindow && namedModel(model)
|
|
192
|
+
? `a context window of ${contextWindow} tokens (the model's in your workspace)`
|
|
193
|
+
: contextWindow === undefined && namedModel(model)
|
|
194
|
+
? "the model's largest context window in your workspace, read from Stav"
|
|
195
|
+
: `dsh's own context window guess (${DSH_DEFAULT_CONTEXT_WINDOW} tokens)`;
|
|
196
|
+
return {
|
|
197
|
+
program: 'dsh',
|
|
198
|
+
args: ['--profile', 'headless', '--patch', DSH_PATCH_FILE],
|
|
199
|
+
env: {
|
|
200
|
+
STAV_DSH_API_KEY: key,
|
|
201
|
+
STAV_DSH_BASE_URL: base,
|
|
202
|
+
STAV_DSH_MODEL: id,
|
|
203
|
+
STAV_DSH_CONTEXT_WINDOW: String(window),
|
|
204
|
+
STAV_DSH_MAX_TOKENS: String(DSH_MAX_TOKENS),
|
|
205
|
+
// Feedback would upload the whole session to DeepSeek, not to Stav.
|
|
206
|
+
DSH_TELEMETRY_MODE: 'DISABLED',
|
|
207
|
+
},
|
|
208
|
+
unset: [],
|
|
209
|
+
describe: [
|
|
210
|
+
`the one-shot command dsh --profile headless, with a "stav-cli" model provider at ${base} for this run only (your ~/.dsh is not changed)`,
|
|
211
|
+
'STAV_DSH_API_KEY=<the key, in the environment only>',
|
|
212
|
+
model ? `model ${model}` : 'model auto (your workspace\'s default Router)',
|
|
213
|
+
describeWindow,
|
|
214
|
+
`at most ${DSH_MAX_TOKENS} output tokens per call`,
|
|
215
|
+
'DSH_TELEMETRY_MODE=DISABLED (no feedback uploads to DeepSeek)',
|
|
216
|
+
"note: dsh runs the commands the model writes without asking; DeepSeek's own SAFETY.md says to run it in a container or VM",
|
|
217
|
+
'give the task after --, for example: stav launch deepseek-harness -- "run the tests"',
|
|
218
|
+
],
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
export const ADAPTERS = [claudeCode, codex, deepseekHarness];
|
|
121
223
|
/** Names a person may type for an app (DECIDE-APPS-P: `claude` → claude-code). */
|
|
122
|
-
export const ALIASES = { claude: 'claude-code' };
|
|
224
|
+
export const ALIASES = { claude: 'claude-code', dsh: 'deepseek-harness' };
|
|
123
225
|
export function resolveApp(name) {
|
|
124
226
|
const n = name.trim().toLowerCase();
|
|
125
227
|
return ALIASES[n] ?? n;
|
package/dist/auth/session.js
CHANGED
|
@@ -13,6 +13,7 @@ import http from 'node:http';
|
|
|
13
13
|
import { spawn } from 'node:child_process';
|
|
14
14
|
import { StavError } from '../errors.js';
|
|
15
15
|
import { USER_AGENT } from '../http.js';
|
|
16
|
+
import { terminalText } from '../terminal-text.js';
|
|
16
17
|
import { challengeOf, newState, newVerifier } from './pkce.js';
|
|
17
18
|
export const CALLBACK_PORTS = [53682, 53683, 53684];
|
|
18
19
|
const SESSION_KEY = 'session';
|
|
@@ -104,7 +105,7 @@ export const defaultIO = {
|
|
|
104
105
|
// The URL is printed too.
|
|
105
106
|
}
|
|
106
107
|
},
|
|
107
|
-
log: (line) => process.stderr.write(`${line}\n`),
|
|
108
|
+
log: (line) => process.stderr.write(`${terminalText(line)}\n`),
|
|
108
109
|
};
|
|
109
110
|
/** Text from a web page, safe to print: no control characters, bounded. */
|
|
110
111
|
export function printable(text) {
|
package/dist/cli.js
CHANGED
|
@@ -16,6 +16,7 @@ import { loadConfig } from './config.js';
|
|
|
16
16
|
import { StavError } from './errors.js';
|
|
17
17
|
import { launch, runChild, which } from './launch.js';
|
|
18
18
|
import { terminal } from './prompt.js';
|
|
19
|
+
import { terminalText } from './terminal-text.js';
|
|
19
20
|
import { Stav } from './stav.js';
|
|
20
21
|
import { forgetAppKeys, openStore, readSettings, writeSetting } from './store/index.js';
|
|
21
22
|
import { VERSION } from './version.js';
|
|
@@ -41,9 +42,11 @@ Environment:
|
|
|
41
42
|
Examples:
|
|
42
43
|
stav launch claude
|
|
43
44
|
stav launch codex --model zai-org/glm-5-2
|
|
45
|
+
stav launch dsh -- "run the tests"
|
|
44
46
|
`;
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
+
// Every line goes through terminalText: much of it is text from the Stav API.
|
|
48
|
+
const out = (line = '') => process.stdout.write(`${terminalText(line)}\n`);
|
|
49
|
+
const err = (line) => process.stderr.write(`${terminalText(line)}\n`);
|
|
47
50
|
async function main(argv) {
|
|
48
51
|
const dash = argv.indexOf('--');
|
|
49
52
|
const own = dash >= 0 ? argv.slice(0, dash) : argv;
|
package/dist/http.js
CHANGED
|
@@ -20,7 +20,7 @@ export async function request(fetchFn, url, opts = {}) {
|
|
|
20
20
|
method: opts.method ?? (opts.body !== undefined ? 'POST' : 'GET'),
|
|
21
21
|
headers,
|
|
22
22
|
body: opts.body !== undefined ? JSON.stringify(opts.body) : undefined,
|
|
23
|
-
signal: AbortSignal.timeout(30_000),
|
|
23
|
+
signal: AbortSignal.timeout(opts.timeoutMs ?? 30_000),
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
catch (err) {
|
package/dist/launch.js
CHANGED
|
@@ -20,6 +20,7 @@ import { chooseWorkspace } from './workspace.js';
|
|
|
20
20
|
export const INSTALL_HINT = {
|
|
21
21
|
claude: 'npm install -g @anthropic-ai/claude-code',
|
|
22
22
|
codex: 'npm install -g @openai/codex',
|
|
23
|
+
dsh: 'npm install -g @deepseek-ai/dsh',
|
|
23
24
|
};
|
|
24
25
|
export async function planLaunch(opts, d) {
|
|
25
26
|
if (d.platform === 'win32') {
|
|
@@ -78,10 +79,10 @@ export async function planLaunch(opts, d) {
|
|
|
78
79
|
return { adapter, slug, model, programPath, key: { secret: stored.secret, source: 'stored' }, workspace, rememberWorkspace, person, token };
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
|
-
const
|
|
82
|
+
const orWorkspaceKey = role === 'OWNER' || role === 'ADMIN';
|
|
82
83
|
return {
|
|
83
84
|
adapter, slug, model, programPath, workspace, rememberWorkspace, person, token,
|
|
84
|
-
key: { create: true,
|
|
85
|
+
key: { create: true, orWorkspaceKey, name: `${adapter.displayName} (${me.email})` },
|
|
85
86
|
};
|
|
86
87
|
}
|
|
87
88
|
function checkLaunchable(app, adapter, slug) {
|
|
@@ -127,25 +128,44 @@ async function explainRefusal(err, stav, token, teamId, workspaceName) {
|
|
|
127
128
|
};
|
|
128
129
|
return text[err.code] ? new StavError(text[err.code], err.code, err.status) : err;
|
|
129
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* The refusals of a personal key that come from the workspace's key policy.
|
|
133
|
+
* Each is a 403 that makes nothing, so trying a workspace key after it
|
|
134
|
+
* leaves no half-made key behind.
|
|
135
|
+
*/
|
|
136
|
+
export const POLICY_REFUSALS = new Map([
|
|
137
|
+
['user_keys_not_enabled', 'personal keys are turned off'],
|
|
138
|
+
['self_service_disabled', 'members cannot make their own personal keys'],
|
|
139
|
+
['approval_required', 'a personal key needs an approval'],
|
|
140
|
+
['max_user_keys_reached', 'you have the most personal keys it allows'],
|
|
141
|
+
['max_user_key_seats_reached', 'all its personal-key seats are used'],
|
|
142
|
+
]);
|
|
130
143
|
/** Make the key if the plan says so, store it, and return the secret. */
|
|
131
144
|
export async function settleKey(plan, d) {
|
|
132
145
|
if (!('create' in plan.key))
|
|
133
146
|
return plan.key.secret;
|
|
134
147
|
const stav = new Stav(d.cfg, d.fetchFn);
|
|
135
148
|
const body = { name: plan.key.name, scopes: [...plan.adapter.scopes], client_app: plan.slug };
|
|
149
|
+
const refused = async (err) => err instanceof StavError ? explainRefusal(err, stav, plan.token, plan.workspace.id, plan.workspace.name) : err;
|
|
136
150
|
let made;
|
|
151
|
+
let personal = true;
|
|
137
152
|
try {
|
|
138
|
-
made = plan.
|
|
139
|
-
? await stav.createTeamKey(plan.token, plan.workspace.id, body)
|
|
140
|
-
: await stav.createUserKey(plan.token, plan.workspace.id, body);
|
|
153
|
+
made = await stav.createUserKey(plan.token, plan.workspace.id, body);
|
|
141
154
|
}
|
|
142
155
|
catch (err) {
|
|
143
|
-
if (err instanceof StavError)
|
|
144
|
-
throw await
|
|
145
|
-
|
|
156
|
+
if (!plan.key.orWorkspaceKey || !(err instanceof StavError) || !POLICY_REFUSALS.has(err.code))
|
|
157
|
+
throw await refused(err);
|
|
158
|
+
d.log(`In ${plan.workspace.name}, ${POLICY_REFUSALS.get(err.code)}, so a workspace key is made instead.`);
|
|
159
|
+
personal = false;
|
|
160
|
+
try {
|
|
161
|
+
made = await stav.createTeamKey(plan.token, plan.workspace.id, body);
|
|
162
|
+
}
|
|
163
|
+
catch (err2) {
|
|
164
|
+
throw await refused(err2);
|
|
165
|
+
}
|
|
146
166
|
}
|
|
147
167
|
await saveAppKey(d.store, appKeyName(plan.workspace.id, plan.slug, plan.person), JSON.stringify({ id: made.id, secret: made.full_key }));
|
|
148
|
-
if (
|
|
168
|
+
if (personal && !(made.scopes && made.scopes.length)) {
|
|
149
169
|
// A Stav that does not yet set scopes on personal keys makes a key with
|
|
150
170
|
// full access: say so rather than claim it is scoped.
|
|
151
171
|
d.log(`Note: this personal key has full access; your workspace's Stav does not limit personal keys to ${plan.adapter.scopes.join(', ')} yet.`);
|
|
@@ -157,11 +177,13 @@ export function describePlan(plan, launch, appArgs, storeKind = 'keychain') {
|
|
|
157
177
|
if (plan.workspace)
|
|
158
178
|
lines.push(`Workspace: ${plan.workspace.name}`);
|
|
159
179
|
if ('create' in plan.key) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
180
|
+
// A Stav that does not yet limit personal keys makes this key with
|
|
181
|
+
// full access, so this is said before the question, not after.
|
|
182
|
+
lines.push(`Make a personal API key "${plan.key.name}" for ${plan.adapter.displayName}, asking for access to ${plan.adapter.scopes.join(', ')} only. If Stav cannot limit personal keys yet, the key has full access to your workspace.`);
|
|
183
|
+
if (plan.key.orWorkspaceKey) {
|
|
184
|
+
lines.push(`If ${plan.workspace.name} does not allow personal keys, make a workspace API key with the same name and access instead.`);
|
|
185
|
+
}
|
|
186
|
+
lines.push(storeKind === 'keychain'
|
|
165
187
|
? 'Keep it in your keychain (never in this folder), and use it next time.'
|
|
166
188
|
: 'Keep it in a file only you can read (no keychain here; never in this folder), and use it next time.');
|
|
167
189
|
}
|
|
@@ -194,7 +216,8 @@ export async function launch(opts, d) {
|
|
|
194
216
|
if (plan.workspace && plan.rememberWorkspace)
|
|
195
217
|
await d.saveSetting('workspace', plan.workspace.id);
|
|
196
218
|
const secret = await settleKey(plan, d);
|
|
197
|
-
const
|
|
219
|
+
const contextWindow = await readContextWindow(plan, secret, d);
|
|
220
|
+
const run = plan.adapter.plan({ gatewayUrl: d.cfg.gatewayUrl, key: secret, model: plan.model, contextWindow });
|
|
198
221
|
const env = { ...d.env };
|
|
199
222
|
for (const k of run.unset)
|
|
200
223
|
delete env[k];
|
|
@@ -202,6 +225,29 @@ export async function launch(opts, d) {
|
|
|
202
225
|
// An argv array and no shell: nothing in a value is ever run as a command.
|
|
203
226
|
return d.spawn(plan.programPath, [...run.args, ...opts.appArgs], { stdio: 'inherit', env, shell: false });
|
|
204
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* The model's context window for this workspace, when the adapter wants it.
|
|
230
|
+
* Never stops the launch: without it the app keeps its own assumption, and
|
|
231
|
+
* the person is told.
|
|
232
|
+
*/
|
|
233
|
+
async function readContextWindow(plan, key, d) {
|
|
234
|
+
if (!plan.model || !plan.adapter.wantsContextWindow?.(plan.model))
|
|
235
|
+
return null;
|
|
236
|
+
let n = null;
|
|
237
|
+
d.log(`Reading the context window of ${plan.model} from Stav...`);
|
|
238
|
+
try {
|
|
239
|
+
const stav = new Stav(d.cfg, d.fetchFn);
|
|
240
|
+
stav.onNote = d.log;
|
|
241
|
+
n = await stav.modelContext(key, plan.model);
|
|
242
|
+
}
|
|
243
|
+
catch (err) {
|
|
244
|
+
d.log(`Could not read the context window of ${plan.model} (${err instanceof Error ? err.message : String(err)}); ${plan.adapter.displayName} uses its own guess.`);
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
if (n === null)
|
|
248
|
+
d.log(`Stav does not know the context window of ${plan.model} for your workspace; ${plan.adapter.displayName} uses its own guess.`);
|
|
249
|
+
return n;
|
|
250
|
+
}
|
|
205
251
|
/** PATH lookup without a shell. */
|
|
206
252
|
export function which(program, env = process.env) {
|
|
207
253
|
for (const dir of (env.PATH ?? '').split(path.delimiter)) {
|
package/dist/prompt.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/** Questions in the terminal. Never asked when there is no terminal. */
|
|
2
2
|
import { createInterface } from 'node:readline/promises';
|
|
3
|
+
import { terminalText } from './terminal-text.js';
|
|
3
4
|
export const terminal = {
|
|
4
5
|
interactive: Boolean(process.stdin.isTTY && process.stderr.isTTY),
|
|
5
6
|
async ask(question) {
|
|
6
7
|
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
7
8
|
try {
|
|
8
|
-
return (await rl.question(question)).trim();
|
|
9
|
+
return (await rl.question(terminalText(question))).trim();
|
|
9
10
|
}
|
|
10
11
|
finally {
|
|
11
12
|
rl.close();
|
package/dist/stav.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
/** The Stav calls the CLI makes, typed. */
|
|
2
2
|
import { StavError } from './errors.js';
|
|
3
3
|
import { request } from './http.js';
|
|
4
|
+
/**
|
|
5
|
+
* The window is a nicety: each read gives up after this. The second read runs
|
|
6
|
+
* only when no endpoint states a window, so a slow gateway delays the start by
|
|
7
|
+
* at most twice this (10 s).
|
|
8
|
+
*/
|
|
9
|
+
const WINDOW_TIMEOUT_MS = 5_000;
|
|
4
10
|
export class Stav {
|
|
5
11
|
cfg;
|
|
6
12
|
fetchFn;
|
|
13
|
+
/** Where a note for the person goes (the launch sets it). */
|
|
14
|
+
onNote;
|
|
7
15
|
constructor(cfg, fetchFn) {
|
|
8
16
|
this.cfg = cfg;
|
|
9
17
|
this.fetchFn = fetchFn;
|
|
@@ -25,6 +33,29 @@ export class Stav {
|
|
|
25
33
|
app(slug) {
|
|
26
34
|
return request(this.fetchFn, `${this.cfg.gatewayUrl}/v1/public/apps/${encodeURIComponent(slug)}`, { nullOn: [404] });
|
|
27
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* The model's context window for the key's workspace: the LARGEST window
|
|
38
|
+
* any endpoint the workspace may use states, from the gateway's endpoint
|
|
39
|
+
* list (Roland 2026-11-27: the largest, not the smallest, so Claude Code
|
|
40
|
+
* is not held to one small endpoint). When no endpoint states one, the
|
|
41
|
+
* model's own figure (``context_length`` on the model answer). Null when
|
|
42
|
+
* the gateway knows neither or does not list the model.
|
|
43
|
+
*/
|
|
44
|
+
async modelContext(key, model) {
|
|
45
|
+
const path = model.split('/').map(encodeURIComponent).join('/');
|
|
46
|
+
const ok = (n) => typeof n === 'number' && Number.isInteger(n) && n > 0;
|
|
47
|
+
const list = await request(this.fetchFn, `${this.cfg.gatewayUrl}/v1/models/${path}/endpoints`, { token: key, nullOn: [401, 403, 404], timeoutMs: WINDOW_TIMEOUT_MS });
|
|
48
|
+
const known = (list?.data?.endpoints ?? []).map((e) => e.context_length).filter(ok);
|
|
49
|
+
if (known.length) {
|
|
50
|
+
const largest = Math.max(...known);
|
|
51
|
+
const smallest = Math.min(...known);
|
|
52
|
+
if (smallest < largest)
|
|
53
|
+
this.onNote?.(`${model} is served at ${largest} tokens of context, but at ${smallest} on one endpoint your workspace may use; a very long chat that lands there is refused.`);
|
|
54
|
+
return largest;
|
|
55
|
+
}
|
|
56
|
+
const m = await request(this.fetchFn, `${this.cfg.gatewayUrl}/v1/models/${path}`, { token: key, nullOn: [401, 403, 404], timeoutMs: WINDOW_TIMEOUT_MS });
|
|
57
|
+
return ok(m?.context_length) ? m.context_length : null;
|
|
58
|
+
}
|
|
28
59
|
/** Whether a key still works, and for which workspace. Null when it does not. */
|
|
29
60
|
checkKey(key) {
|
|
30
61
|
return request(this.fetchFn, `${this.cfg.gatewayUrl}/v1/auth/validate`, { token: key, nullOn: [401, 403] });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text safe to write to a terminal.
|
|
3
|
+
*
|
|
4
|
+
* Much of what the CLI prints comes from the Stav API: error messages, key
|
|
5
|
+
* names, workspace names, emails. An escape sequence in any of them would be
|
|
6
|
+
* run by the terminal: it could hide or rewrite lines of the plan shown
|
|
7
|
+
* before "Go ahead?", set the window title or write the clipboard. So every
|
|
8
|
+
* line passes through here on its way out.
|
|
9
|
+
*
|
|
10
|
+
* Kept: line feed and tab (the CLI's own help and lists use them).
|
|
11
|
+
* Replaced with a space: every other C0 and C1 control character (ESC, CR,
|
|
12
|
+
* BEL, NEL, CSI …), DEL, the Unicode line and paragraph separators, and the
|
|
13
|
+
* bidirectional marks, overrides and isolates, which can reorder what the
|
|
14
|
+
* reader sees.
|
|
15
|
+
*/
|
|
16
|
+
const UNSAFE = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u061c\u200e\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069]/g;
|
|
17
|
+
export function terminalText(text) {
|
|
18
|
+
return text.replace(UNSAFE, ' ');
|
|
19
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The CLI's own version. Kept equal to package.json by version.test.ts. */
|
|
2
|
-
export const VERSION = '0.1.
|
|
2
|
+
export const VERSION = '0.1.2';
|
package/dist/workspace.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* no header Stav silently uses the oldest membership.
|
|
5
5
|
*/
|
|
6
6
|
import { StavError } from './errors.js';
|
|
7
|
+
import { terminalText } from './terminal-text.js';
|
|
7
8
|
/**
|
|
8
9
|
* The chosen workspace: the one named (by id or slug), else the stored
|
|
9
10
|
* choice if still a membership, else the only one, else ask. Without a
|
|
@@ -27,7 +28,7 @@ export async function chooseWorkspace(all, named, stored, prompter) {
|
|
|
27
28
|
if (!prompter.interactive) {
|
|
28
29
|
throw new StavError(`You are in ${all.length} workspaces. Choose one with --workspace <slug> or stav workspace <slug>.`, 'workspace_required');
|
|
29
30
|
}
|
|
30
|
-
all.forEach((w, i) => process.stderr.write(` ${i + 1}. ${w.name}${w.slug ? ` (${w.slug})` : ''}, ${w.role.toLowerCase()}\n`));
|
|
31
|
+
all.forEach((w, i) => process.stderr.write(`${terminalText(` ${i + 1}. ${w.name}${w.slug ? ` (${w.slug})` : ''}, ${w.role.toLowerCase()}`)}\n`));
|
|
31
32
|
for (;;) {
|
|
32
33
|
const answer = await prompter.ask(`Which workspace? 1-${all.length}: `);
|
|
33
34
|
const n = Number.parseInt(answer, 10);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stav-ai/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
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
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"node": ">=22"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "tsc -p tsconfig.build.json",
|
|
18
|
+
"build": "tsc -p tsconfig.build.json && node --input-type=module -e \"import { copyFileSync } from 'node:fs'; copyFileSync('src/adapters/deepseek-harness.patch.yml', 'dist/adapters/deepseek-harness.patch.yml')\"",
|
|
19
19
|
"prepack": "npm run build",
|
|
20
20
|
"typecheck": "tsc --noEmit",
|
|
21
21
|
"test": "tsx --test 'src/**/*.test.ts'",
|