@phantomcodes/cli 1.2.7 → 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/README.md +24 -1
- package/dist/agents/command.mjs +9 -10
- package/dist/cli/app.mjs +2 -0
- package/dist/cli/commands.mjs +12 -0
- package/dist/cli/screens.mjs +55 -4
- package/dist/core/free.mjs +2 -0
- package/dist/core/version.mjs +1 -1
- package/dist/models/choice.mjs +27 -0
- package/dist/models/command.mjs +66 -0
- package/dist/models/list.mjs +39 -0
- package/dist/setup/command.mjs +52 -3
- package/dist/spend/command.mjs +21 -4
- package/dist/spend/routing.mjs +2 -0
- package/package.json +1 -1
- package/skills/phantom-ai/SKILL.md +12 -0
package/README.md
CHANGED
|
@@ -70,7 +70,9 @@ pi gets the agent skill.
|
|
|
70
70
|
|
|
71
71
|
Preview without changing files with `phantom setup --dry-run`. To leave your
|
|
72
72
|
shell startup file alone, untick "Your shell" in the checklist or pass
|
|
73
|
-
`--no-shell`; setup then prints the lines to add yourself.
|
|
73
|
+
`--no-shell`; setup then prints the lines to add yourself. Cap what the key can spend each
|
|
74
|
+
month with `phantom setup --limit 20` (`--limit none` removes it); at a terminal,
|
|
75
|
+
setup asks once when the key has no cap. The Phantom CLI keeps backups;
|
|
74
76
|
undo the changes with `phantom setup --restore`.
|
|
75
77
|
|
|
76
78
|
## Connect for one run
|
|
@@ -135,6 +137,25 @@ PHANTOM_KEY_NAME=coding phantom route test --table
|
|
|
135
137
|
Prefer a specific model? Select it in your agent. Policies that apply only to
|
|
136
138
|
`auto` leave explicit model choices alone.
|
|
137
139
|
|
|
140
|
+
Phantom has three models of its own. `phantom models` shows what each can do
|
|
141
|
+
(chat, images in, embeddings, rerank, images, video, speech, transcription) and
|
|
142
|
+
the model behind each job:
|
|
143
|
+
|
|
144
|
+
- `phantom-1` does every job, and picks economy, standard or frontier for each chat request.
|
|
145
|
+
- `phantom-1:free` is chat on free models at $0, with no card.
|
|
146
|
+
- `phantom-1:zdr` is chat (with images) on providers that keep none of your data.
|
|
147
|
+
|
|
148
|
+
At a terminal, `phantom models` asks which one your agents should use. To pick
|
|
149
|
+
without the menu:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
phantom models use phantom-1:zdr
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`phantom claude`, `phantom codex` and `phantom pi` start on the model you pick,
|
|
156
|
+
and so does the next `phantom setup`. Agents set up before you picked keep their
|
|
157
|
+
model until you run `phantom setup` again.
|
|
158
|
+
|
|
138
159
|
`phantom-1` picks a tier for each request: economy, standard or frontier. Your key's
|
|
139
160
|
lineup picks the model in each tier, and a fallback model to try when it fails:
|
|
140
161
|
|
|
@@ -180,11 +201,13 @@ it cancels. A named model, not `phantom-1`, still spends credit.
|
|
|
180
201
|
| --- | --- |
|
|
181
202
|
| Connect | `phantom login`, `phantom logout` |
|
|
182
203
|
| Connect existing agents | `phantom setup`, `phantom setup --dry-run`, `phantom setup --restore` |
|
|
204
|
+
| Providers that answer soonest | `phantom route set --sort fast` (`--sort default` for cheapest first) |
|
|
183
205
|
| Run inference | `phantom claude`, `phantom codex`, `phantom pi` |
|
|
184
206
|
| Fund and check spending | `phantom fund`, `phantom balance`, `phantom spend`, `phantom workspace status` |
|
|
185
207
|
| Manage workspace keys | `phantom keys create`, `phantom keys set`, `phantom keys rotate`, `phantom keys revoke` |
|
|
186
208
|
| Manage local copies | `phantom keys saved`, `phantom keys save`, `phantom keys rm` |
|
|
187
209
|
| Route models | `phantom route` |
|
|
210
|
+
| See phantom-1, phantom-1:free and phantom-1:zdr, and pick one | `phantom models`, `phantom models use <id>` |
|
|
188
211
|
| Pick phantom-1's models | `phantom lineup`, `phantom lineup set`, `phantom lineup reset` |
|
|
189
212
|
|
|
190
213
|
Put phantom flags before agent arguments; use `--` to separate them:
|
package/dist/agents/command.mjs
CHANGED
|
@@ -6,10 +6,9 @@ import { die, NO_KEY } from "../core/errors.mjs";
|
|
|
6
6
|
import { DEFAULT_BASE_URL, httpsOnly, request } from "../core/request.mjs";
|
|
7
7
|
import { keyId, readNamedKey, resolveApiKey } from "../keys/saved.mjs";
|
|
8
8
|
import { workspaceLogin } from "../workspace/command.mjs";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const CLAUDE_DEFAULT_MODEL = `claude-code/${DEFAULT_MODEL}`;
|
|
9
|
+
import { startModel } from "../models/choice.mjs";
|
|
10
|
+
/** The model as Claude Code names it: its picker keeps only ids that say "claude", so Phantom lists them as `claude-code/<id>`. */
|
|
11
|
+
const claudeName = (model) => `claude-code/${model}`;
|
|
13
12
|
/** The key's spend and balance, or null when the answer has no numbers, such as a sign-in page that came back with status 200. */
|
|
14
13
|
async function keyStatus(apiKey, baseUrl) {
|
|
15
14
|
const s = await request('GET', '/key/balance', apiKey, undefined, baseUrl);
|
|
@@ -28,9 +27,9 @@ export const AGENTS = {
|
|
|
28
27
|
// reads the token from the environment, so it stays off the command line.
|
|
29
28
|
// It runs with this phantom's own Node: the curl installer puts none on PATH.
|
|
30
29
|
const helper = `${JSON.stringify(process.execPath)} -p "process.env.PHANTOM_AGENT_TOKEN || ''"`;
|
|
31
|
-
// phantom-1 unless the user
|
|
32
|
-
// Claude Code ranks below --model and below a model in its settings file.
|
|
33
|
-
const model = hasModel(args) || env.ANTHROPIC_MODEL ? {} : { ANTHROPIC_MODEL:
|
|
30
|
+
// The model picked with `phantom models` (phantom-1 until then), unless the user names one. It goes in the
|
|
31
|
+
// environment, which Claude Code ranks below --model and below a model in its settings file.
|
|
32
|
+
const model = hasModel(args) || env.ANTHROPIC_MODEL ? {} : { ANTHROPIC_MODEL: claudeName(startModel(env)) };
|
|
34
33
|
const vars = { ANTHROPIC_BASE_URL: proxy, CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY: '1', ENABLE_TOOL_SEARCH: 'true' };
|
|
35
34
|
const { ANTHROPIC_AUTH_TOKEN: _auth, ...rest } = env;
|
|
36
35
|
return {
|
|
@@ -57,7 +56,7 @@ export const AGENTS = {
|
|
|
57
56
|
'model_provider="phantom_run"',
|
|
58
57
|
...provider.map(([k, v]) => `model_providers.phantom_run.${k}=${JSON.stringify(v)}`),
|
|
59
58
|
'features.api_key_model_discovery=true',
|
|
60
|
-
...(hasModel(args) ? [] : [`model=${JSON.stringify(
|
|
59
|
+
...(hasModel(args) ? [] : [`model=${JSON.stringify(startModel(env))}`]),
|
|
61
60
|
];
|
|
62
61
|
return { args: [...config.flatMap((c) => ['-c', c]), ...args], env: { ...env, OPENAI_BASE_URL: base, PHANTOM_AGENT_TOKEN: token } };
|
|
63
62
|
},
|
|
@@ -70,9 +69,9 @@ export const AGENTS = {
|
|
|
70
69
|
// for this run leaves the user's own files alone. Its key is read from
|
|
71
70
|
// PHANTOM_AGENT_TOKEN when pi calls, so the file holds no secret.
|
|
72
71
|
const file = path.join(tmp, 'phantom-provider.ts');
|
|
73
|
-
const model = { id:
|
|
72
|
+
const model = { id: startModel(env), name: 'Phantom AI router (phantom)', reasoning: true, input: ['text', 'image'], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: 1_000_000, maxTokens: 64_000 };
|
|
74
73
|
writeFileSync(file, `export default function (pi) {\n pi.registerProvider('phantom', ${JSON.stringify({ baseUrl: `${proxy}/v1`, api: 'openai-completions', apiKey: '$PHANTOM_AGENT_TOKEN', models: [model] })});\n}\n`, { mode: 0o600 });
|
|
75
|
-
return { args: ['-e', file, ...(hasModel(args) ? [] : ['--provider', 'phantom', '--model',
|
|
74
|
+
return { args: ['-e', file, ...(hasModel(args) ? [] : ['--provider', 'phantom', '--model', startModel(env)]), ...args], env: { ...env, PHANTOM_AGENT_TOKEN: token } };
|
|
76
75
|
},
|
|
77
76
|
},
|
|
78
77
|
};
|
package/dist/cli/app.mjs
CHANGED
|
@@ -32,6 +32,7 @@ export function mainMenu(s) {
|
|
|
32
32
|
{ label: 'Balance', hint: 'what this key can spend', value: { run: ['balance'] } },
|
|
33
33
|
{ label: 'Spend', hint: 'what you spent, and what phantom-1 saved', value: { run: ['spend'] } },
|
|
34
34
|
{ label: 'Add credit', hint: 'by card or crypto', value: { run: ['fund'] } },
|
|
35
|
+
{ label: 'Models', hint: 'phantom-1, free or zdr: what each can do, and pick one', value: { run: ['models'] } },
|
|
35
36
|
{ label: 'Choose models', hint: 'the model phantom-1 runs in each tier', value: { run: ['lineup', 'set'] } },
|
|
36
37
|
{ label: 'Keys', hint: 'list, new, limits, revoke', value: { open: 'keys' } },
|
|
37
38
|
{ label: 'Plan', hint: 'Pro or Max, instead of credit', value: { run: ['plan'] } },
|
|
@@ -78,6 +79,7 @@ export function nextSteps(ran) {
|
|
|
78
79
|
workspace: [['See my keys', ['keys']], ['Spend by key', ['spend', '--workspace']]],
|
|
79
80
|
keys: [['New key', ['keys', 'create']], ['Set a spending limit', ['keys', 'set']], ['Revoke a key', ['keys', 'revoke']]],
|
|
80
81
|
plan: [['Start Pro', ['plan', 'pro']], ['Start Max', ['plan', 'max']], ['Manage billing', ['plan', 'manage']]],
|
|
82
|
+
models: [['Put it in my agents', ['setup']], ['Choose the model in each tier', ['lineup', 'set']]],
|
|
81
83
|
route: [['See what a request would get', ['route', 'test']], ['Choose models', ['lineup', 'set']]],
|
|
82
84
|
'setup --restore': [['Sign out, and forget saved keys', ['logout', '--all']]],
|
|
83
85
|
login: [['Set up my agents', ['setup']]],
|
package/dist/cli/commands.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import { planCommand } from "../plan/command.mjs";
|
|
|
7
7
|
import { verifyCommand } from "../receipts/command.mjs";
|
|
8
8
|
import { setupCommand } from "../setup/command.mjs";
|
|
9
9
|
import { SETUP_MODEL } from "../setup/shared.mjs";
|
|
10
|
+
import { modelsCommand } from "../models/command.mjs";
|
|
10
11
|
import { SETUP_NAMES, orList } from "../setup/agents.mjs";
|
|
11
12
|
/** The short `--help`'s headings, in the order they print. */
|
|
12
13
|
export const HELP_GROUPS = ['Start', 'Run', 'Money', 'Keys', 'Routing', 'More'];
|
|
@@ -119,6 +120,9 @@ export const COMMANDS = [
|
|
|
119
120
|
name: 'route', handler: routeCommand,
|
|
120
121
|
group: 'Routing', summary: 'which models "auto" runs, and rules for picking',
|
|
121
122
|
help: ` route [get] show which model "auto" runs, and why
|
|
123
|
+
route set --sort fast|cheap|throughput|default
|
|
124
|
+
which providers serve this key's models first: fast
|
|
125
|
+
starts answering soonest; cheap is the default
|
|
122
126
|
route set --models a,b,c models for "auto", first is the default
|
|
123
127
|
[--applies-to auto|all] [--on-empty stop|cheapest] [--fallback]
|
|
124
128
|
[--stick-minutes n] keep a conversation's model this long (default 5)
|
|
@@ -142,6 +146,13 @@ export const COMMANDS = [
|
|
|
142
146
|
[--frontier-fallback <model>]
|
|
143
147
|
the model to try when a tier's model fails
|
|
144
148
|
lineup reset every tier back to its default`,
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: 'models', handler: modelsCommand, keyless: true,
|
|
152
|
+
group: 'Routing', summary: 'phantom-1, free or zdr: what each can do, and which your agents use',
|
|
153
|
+
help: ` models what phantom-1, phantom-1:free and phantom-1:zdr can do,
|
|
154
|
+
and the model behind each job; at a terminal, pick one
|
|
155
|
+
models use <id> start your agents on it, without the menu`,
|
|
145
156
|
},
|
|
146
157
|
{
|
|
147
158
|
name: 'logout', handler: logoutDispatch, keyless: true,
|
|
@@ -162,6 +173,7 @@ export const COMMANDS = [
|
|
|
162
173
|
[--diff] show every line each file gains or loses
|
|
163
174
|
[--no-shell] leave your shell startup file alone and list
|
|
164
175
|
the lines to add yourself
|
|
176
|
+
[--limit <USD|none>] cap what this key can spend each month
|
|
165
177
|
[--json] print JSON in a terminal too (a pipe always gets JSON)
|
|
166
178
|
[--model <id>] the model each agent starts on (default ${SETUP_MODEL})
|
|
167
179
|
setup --agent <name> install the skill for one agent, without asking:
|
package/dist/cli/screens.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { paint } from "../core/style.mjs";
|
|
2
|
+
import { JOBS } from "../models/list.mjs";
|
|
3
|
+
import { FREE_AGENT_REQUESTS_A_DAY, FREE_REQUESTS_A_DAY } from "../core/free.mjs";
|
|
2
4
|
/** The window's width: given, or the terminal's, or 100 for a pipe. */
|
|
3
5
|
const widthOf = (style) => style.columns ?? process.stdout.columns ?? 100;
|
|
4
6
|
/** How long a caption's rule is: 66, or less in a narrow window. */
|
|
@@ -178,12 +180,15 @@ export function routeScreen(r, style = {}) {
|
|
|
178
180
|
const w = ruleWidth(style);
|
|
179
181
|
const lines = ['', ...caption(p, w, 'ROUTING', p.dim('how model "auto" picks for this key'))];
|
|
180
182
|
const policy = r.route_policy;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
183
|
+
const row = (label, value) => ` ${p.dim(label.padEnd(14))}${value}`;
|
|
184
|
+
// Which providers serve first holds for every request, routed or not.
|
|
185
|
+
const sortWords = { cost: 'cheapest first', ttft: 'fastest to start answering first', tps: 'most tokens a second first' };
|
|
186
|
+
const providers = row('providers', `${sortWords[policy?.sort ?? 'cost']}${policy?.sort ? '' : p.dim(' (the default)')}${policy?.zdr ? ', zero data retention only' : ''}${policy?.no_training ? ', no training on prompts' : ''}`);
|
|
187
|
+
if (!policy || !policy.models.length) {
|
|
188
|
+
lines.push(' No route policy, so model "auto" runs phantom-1, which picks a model for each request.', '', providers);
|
|
189
|
+
lines.push(...(policy?.sort ? next(p, 'Back to cheapest first:', 'phantom route set --sort default') : next(p, 'Providers that start answering soonest:', 'phantom route set --sort fast')));
|
|
184
190
|
return lines.join('\n');
|
|
185
191
|
}
|
|
186
|
-
const row = (label, value) => ` ${p.dim(label.padEnd(14))}${value}`;
|
|
187
192
|
// One model a line, in the order "auto" tries them, so a long list doesn't wrap.
|
|
188
193
|
policy.models.forEach((m, k) => lines.push(row(k === 0 ? 'models' : '', `${p.dim(`${k + 1}.`)} ${k === 0 ? `${p.bold(m)} ${p.dim('the default')}` : m}`)));
|
|
189
194
|
lines.push(row('applies to', policy.applies_to === 'all' ? 'every request' : 'requests for model "auto"'));
|
|
@@ -191,6 +196,7 @@ export function routeScreen(r, style = {}) {
|
|
|
191
196
|
lines.push(row('a model fails', policy.fallback_on_error ? 'try the next one' : 'stop there'));
|
|
192
197
|
const stick = policy.stick_minutes ?? 5;
|
|
193
198
|
lines.push(row('conversations', stick ? `stay on their model for ${plural(stick, 'minute')}${policy.stick_by_prompt === false ? ' (by x-phantom-session only)' : ''}` : 'can change model every request'));
|
|
199
|
+
lines.push(providers);
|
|
194
200
|
const rules = policy.rules ?? [];
|
|
195
201
|
lines.push('', ...caption(p, w, 'RULES', p.dim(rules.length ? 'checked in order; the first that matches picks' : '')));
|
|
196
202
|
if (!rules.length)
|
|
@@ -347,3 +353,48 @@ export function keySavedScreen(name, style = {}) {
|
|
|
347
353
|
const w = ruleWidth(style);
|
|
348
354
|
return ['', done(p, `Key "${name}" saved on this computer.`), ...next(p, 'Run an agent on it:', `phantom codex --key ${name}`)].join('\n');
|
|
349
355
|
}
|
|
356
|
+
/** What each of Phantom's own models is for, in a few words. */
|
|
357
|
+
const MODEL_ABOUT = {
|
|
358
|
+
'phantom-1': 'every job; chat picks economy, standard or frontier for each request',
|
|
359
|
+
'phantom-1:free': `chat on free models at $0, no card; ${FREE_REQUESTS_A_DAY} requests a day, ${FREE_AGENT_REQUESTS_A_DAY} from an agent`,
|
|
360
|
+
'phantom-1:zdr': 'chat on providers that keep none of your data, at their prices',
|
|
361
|
+
};
|
|
362
|
+
/** A model's few words: {@link MODEL_ABOUT}, or the first sentence the server gives for a model this CLI doesn't know yet. */
|
|
363
|
+
const modelAbout = (m) => MODEL_ABOUT[m.id] ?? m.description.split('. ')[0];
|
|
364
|
+
/**
|
|
365
|
+
* `phantom models`: a table of what each of Phantom's own models can do, a
|
|
366
|
+
* column per model, a ✓ per job. Under it, the model behind each job, so you
|
|
367
|
+
* can see what runs before you pick.
|
|
368
|
+
*/
|
|
369
|
+
export function modelsScreen(v, style = {}) {
|
|
370
|
+
const p = paint(style.env ?? {}, Boolean(style.color));
|
|
371
|
+
const LABEL = 18;
|
|
372
|
+
const COLUMN = 17;
|
|
373
|
+
// Padding goes outside the color, so the row's trailing spaces can be trimmed.
|
|
374
|
+
const cell = (text, color) => color(text) + ' '.repeat(Math.max(0, COLUMN - text.length));
|
|
375
|
+
const row = (label, cells) => ` ${label.padEnd(LABEL)}${cells.join('')}`.trimEnd();
|
|
376
|
+
const lines = ['', ...caption(p, ruleWidth(style), 'MODELS', p.dim('one name each; your agents send it'))];
|
|
377
|
+
lines.push(row('', v.models.map((m) => cell(m.id, p.bold))));
|
|
378
|
+
lines.push(row('', v.models.map((m) => cell(m.id === v.using ? 'in use' : '', p.lime))));
|
|
379
|
+
for (const [job, label] of JOBS) {
|
|
380
|
+
lines.push(row(label, v.models.map((m) => (m.can.includes(job) ? cell('✓', p.lime) : cell('·', p.dim)))));
|
|
381
|
+
}
|
|
382
|
+
for (const m of v.models) {
|
|
383
|
+
lines.push('', ` ${p.bold(m.id)}${m.id === v.using ? ` ${p.lime('in use')}` : ''}`, ` ${p.dim(modelAbout(m))}`);
|
|
384
|
+
// Chat models in different tiers are named by tier; models in one tier are tried in order.
|
|
385
|
+
const oneTier = new Set(m.chat.map((c) => c.tier)).size === 1;
|
|
386
|
+
const idWidth = Math.max(0, ...m.chat.map((c) => c.id.length));
|
|
387
|
+
m.chat.forEach((c, i) => {
|
|
388
|
+
const label = oneTier ? (i === 0 ? 'chat' : 'then') : c.tier;
|
|
389
|
+
lines.push(` ${p.dim(label.padEnd(16))}${c.free ? `${c.id.padEnd(idWidth)} ${p.dim('$0')}` : c.id}`);
|
|
390
|
+
});
|
|
391
|
+
for (const [job, label] of JOBS) {
|
|
392
|
+
if (job === 'chat' || !m.can.includes(job))
|
|
393
|
+
continue;
|
|
394
|
+
lines.push(` ${p.dim(label.toLowerCase().padEnd(16))}${m.jobs[job] ?? p.dim('on the chat models above')}`);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const other = v.models.find((m) => m.id !== v.using)?.id ?? v.using;
|
|
398
|
+
lines.push(...next(p, 'Pick the one your agents use:', `phantom models use ${other}`));
|
|
399
|
+
return lines.join('\n');
|
|
400
|
+
}
|
package/dist/core/free.mjs
CHANGED
|
@@ -5,5 +5,7 @@
|
|
|
5
5
|
* repo's `lib/config.ts`; change both together.
|
|
6
6
|
*/
|
|
7
7
|
export const FREE_REQUESTS_A_DAY = 50;
|
|
8
|
+
/** The same, for requests a coding agent sends (`FREE_ROUTER_CAPS.perDayAgent` in the Phantom repo). */
|
|
9
|
+
export const FREE_AGENT_REQUESTS_A_DAY = 200;
|
|
8
10
|
/** The free offer in one line short enough for an 80-column terminal. */
|
|
9
11
|
export const FREE_LINE = `phantom-1 runs free models, ${FREE_REQUESTS_A_DAY} requests a day, with no card`;
|
package/dist/core/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** This release of phantom. `npm version` keeps it equal to the version in package.json. */
|
|
2
|
-
export const VERSION = '1.
|
|
2
|
+
export const VERSION = '1.3.0';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { stateDir } from "../core/state.mjs";
|
|
4
|
+
import { SETUP_MODEL } from "../setup/shared.mjs";
|
|
5
|
+
// The model you picked with `phantom models`. Setup writes it into each
|
|
6
|
+
// agent's settings, and `phantom claude`, `phantom codex` and `phantom pi`
|
|
7
|
+
// start on it. It lives in one plain file, `model`, in the state folder.
|
|
8
|
+
/** The file that holds the model you picked. */
|
|
9
|
+
const choiceFile = (env) => path.join(stateDir(env), 'model');
|
|
10
|
+
/** The model you picked with `phantom models`, or undefined when you never picked one. */
|
|
11
|
+
export function savedModel(env) {
|
|
12
|
+
try {
|
|
13
|
+
return readFileSync(choiceFile(env), 'utf-8').trim() || undefined;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** Saves the model your agents start on. */
|
|
20
|
+
export function saveModel(env, id) {
|
|
21
|
+
mkdirSync(stateDir(env), { recursive: true });
|
|
22
|
+
writeFileSync(choiceFile(env), `${id}\n`);
|
|
23
|
+
}
|
|
24
|
+
/** The model agents start on: the one you picked, or phantom-1. */
|
|
25
|
+
export function startModel(env) {
|
|
26
|
+
return savedModel(env) ?? SETUP_MODEL;
|
|
27
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { die } from "../core/errors.mjs";
|
|
2
|
+
import { out, pickIo, showsScreen } from "../core/helpers.mjs";
|
|
3
|
+
import { choose } from "../core/pick.mjs";
|
|
4
|
+
import { colorOn, paint } from "../core/style.mjs";
|
|
5
|
+
import { hideHints, modelsScreen } from "../cli/screens.mjs";
|
|
6
|
+
import { unknownSub } from "../cli/start.mjs";
|
|
7
|
+
import { isSetUp } from "../setup/apply.mjs";
|
|
8
|
+
import { saveModel, startModel } from "./choice.mjs";
|
|
9
|
+
import { getModels } from "./list.mjs";
|
|
10
|
+
/**
|
|
11
|
+
* `phantom models`: phantom-1, phantom-1:free and phantom-1:zdr side by side,
|
|
12
|
+
* with the jobs each can do and the model behind each job. At a terminal you
|
|
13
|
+
* pick one, and your agents start on it (`./choice.mts`).
|
|
14
|
+
* `phantom models use <id>` picks without the menu.
|
|
15
|
+
*/
|
|
16
|
+
export async function modelsCommand(input) {
|
|
17
|
+
const { io, env, subcommand, argv, baseUrl, apiKey } = input;
|
|
18
|
+
const table = showsScreen(input);
|
|
19
|
+
const style = { env, color: colorOn(env, io.stdoutIsTTY), columns: io.columns };
|
|
20
|
+
const models = await getModels(baseUrl, apiKey);
|
|
21
|
+
const ids = models.map((m) => m.id);
|
|
22
|
+
if (subcommand === 'use') {
|
|
23
|
+
const id = argv[2] && !argv[2].startsWith('--') ? argv[2] : undefined;
|
|
24
|
+
if (!id || !ids.includes(id))
|
|
25
|
+
die(`models use needs one of: ${ids.join(', ')}`, 'models_usage');
|
|
26
|
+
saveModel(env, id);
|
|
27
|
+
out({ using: id }, table, () => picked(input, id), io.stdout);
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
if (subcommand)
|
|
31
|
+
die(unknownSub('models', subcommand, ['use']), 'models_usage');
|
|
32
|
+
const view = { models, using: startModel(env) };
|
|
33
|
+
const pick = pickIo(input);
|
|
34
|
+
if (!pick) {
|
|
35
|
+
out(view, table, (d) => modelsScreen(d, style), io.stdout);
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
// At a terminal: the table, then a picker, instead of a command to type.
|
|
39
|
+
const hintsWere = hideHints(true);
|
|
40
|
+
let id;
|
|
41
|
+
try {
|
|
42
|
+
io.stdout(modelsScreen(view, style) + '\n\n');
|
|
43
|
+
const choices = models.map((m) => ({ label: m.id, hint: m.id === view.using ? 'what they use now' : m.name, value: m.id }));
|
|
44
|
+
id = await choose({ ...pick, esc: 'back' }, 'Which one should your agents use?', choices, Math.max(0, ids.indexOf(view.using)));
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
hideHints(hintsWere);
|
|
48
|
+
}
|
|
49
|
+
if (id === null)
|
|
50
|
+
return 0;
|
|
51
|
+
if (id === view.using) {
|
|
52
|
+
io.stdout(`\n Your agents already use ${id}.\n`);
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
saveModel(env, id);
|
|
56
|
+
io.stdout(`\n${picked(input, id)}\n`);
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
/** The lines after a pick: what changed, and how to put it in agents set up already. */
|
|
60
|
+
function picked(input, id) {
|
|
61
|
+
const p = paint(input.env, colorOn(input.env, input.io.stdoutIsTTY));
|
|
62
|
+
const lines = [` ${p.lime('✓')} Your agents start on ${id} from now on.`];
|
|
63
|
+
if (isSetUp(input.env))
|
|
64
|
+
lines.push(` ${p.dim('Agents set up already keep their model until you run')} ${p.cyan('phantom setup')}${p.dim('.')}`);
|
|
65
|
+
return lines.join('\n');
|
|
66
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { request } from "../core/request.mjs";
|
|
2
|
+
// Phantom's own models (phantom-1, phantom-1:free, phantom-1:zdr) and what
|
|
3
|
+
// each can do, read from the server, so a model Phantom adds or changes shows
|
|
4
|
+
// up with no new CLI release. `phantom models` shows them.
|
|
5
|
+
/** Each job a model can do, with its name on screen, in the order the table lists them. */
|
|
6
|
+
export const JOBS = [
|
|
7
|
+
['chat', 'Chat'],
|
|
8
|
+
['vision', 'Images in'],
|
|
9
|
+
['embedding', 'Embeddings'],
|
|
10
|
+
['rerank', 'Rerank'],
|
|
11
|
+
['image', 'Make images'],
|
|
12
|
+
['video', 'Make video'],
|
|
13
|
+
['speech', 'Text to speech'],
|
|
14
|
+
['transcription', 'Speech to text'],
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* Phantom's own models from the server. /v1/routers names each one's chat
|
|
18
|
+
* models and the model behind every other job; /v1/models says whether it
|
|
19
|
+
* takes images. Neither needs a key.
|
|
20
|
+
*/
|
|
21
|
+
export async function getModels(baseUrl, apiKey = '') {
|
|
22
|
+
const [routers, list] = await Promise.all([
|
|
23
|
+
request('GET', '/routers', apiKey, undefined, baseUrl),
|
|
24
|
+
request('GET', '/models', apiKey, undefined, baseUrl),
|
|
25
|
+
]);
|
|
26
|
+
const takesImages = new Set(list.data.filter((m) => m.modalities?.input?.includes('image')).map((m) => m.id));
|
|
27
|
+
return routers.data.map((r) => {
|
|
28
|
+
const can = JOBS.map(([job]) => job).filter((job) => {
|
|
29
|
+
if (job === 'chat')
|
|
30
|
+
return r.models.length > 0;
|
|
31
|
+
// phantom-1 names a model for images; phantom-1:zdr sends them to its chat models.
|
|
32
|
+
if (job === 'vision')
|
|
33
|
+
return Boolean(r.jobs.vision) || takesImages.has(r.id);
|
|
34
|
+
return Boolean(r.jobs[job]);
|
|
35
|
+
});
|
|
36
|
+
const chat = r.models.map((m) => ({ id: m.id, tier: m.tier, free: m.pricing.prompt === 0 && m.pricing.completion === 0 }));
|
|
37
|
+
return { id: r.id, name: r.name, description: r.description, chat, jobs: r.jobs, can };
|
|
38
|
+
});
|
|
39
|
+
}
|
package/dist/setup/command.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import { createInterface } from 'node:readline/promises';
|
|
|
4
4
|
import { die } from "../core/errors.mjs";
|
|
5
5
|
import { out, pickIo } from "../core/helpers.mjs";
|
|
6
6
|
import { BackToMenu, checklist, choose } from "../core/pick.mjs";
|
|
7
|
+
import { request } from "../core/request.mjs";
|
|
7
8
|
import { resolveApiKey } from "../keys/saved.mjs";
|
|
8
9
|
import { workspaceLogin } from "../workspace/command.mjs";
|
|
9
10
|
import { SETUP_AGENTS, SETUP_NAMES } from "./agents.mjs";
|
|
@@ -13,6 +14,7 @@ import { setupScreen } from "./screen.mjs";
|
|
|
13
14
|
import { colorOn, paint, unstyled } from "../core/style.mjs";
|
|
14
15
|
import { inApp } from "../cli/screens.mjs";
|
|
15
16
|
import { brandIntro, motionOn, play, reveal } from "../core/motion.mjs";
|
|
17
|
+
import { savedModel } from "../models/choice.mjs";
|
|
16
18
|
/** `--table` output for `setup`, and what a terminal shows: see {@link setupScreen}. */
|
|
17
19
|
export function tableSetup(d, opts = {}) {
|
|
18
20
|
return setupScreen(d, opts);
|
|
@@ -104,6 +106,8 @@ export async function setupCommand(input) {
|
|
|
104
106
|
// Plain `phantom setup` sets up everything it finds and asks first. Any of
|
|
105
107
|
// the older flags runs just what they name, without asking.
|
|
106
108
|
const guided = agent === undefined && provider === undefined;
|
|
109
|
+
// Checked before anything is written, so a typo in --limit changes nothing.
|
|
110
|
+
const limit = capFlag(flags['limit']);
|
|
107
111
|
// With no key yet, sign in first, as phantom claude does, so the agents
|
|
108
112
|
// get one in this run. Its words go to stderr; stdout stays setup's result.
|
|
109
113
|
if (guided && !dryRun && io.isTTY && !resolveApiKey(env)) {
|
|
@@ -117,7 +121,8 @@ export async function setupCommand(input) {
|
|
|
117
121
|
const options = {
|
|
118
122
|
agent,
|
|
119
123
|
provider: guided || provider === true ? true : provider === 'off' ? 'off' : false,
|
|
120
|
-
model:
|
|
124
|
+
// No --model: the one picked with `phantom models`, or phantom-1.
|
|
125
|
+
model: typeof model === 'string' ? model : savedModel(env),
|
|
121
126
|
guided,
|
|
122
127
|
shell: !flags['no-shell'],
|
|
123
128
|
};
|
|
@@ -196,7 +201,7 @@ export async function setupCommand(input) {
|
|
|
196
201
|
}
|
|
197
202
|
break;
|
|
198
203
|
}
|
|
199
|
-
return finish(applySetup(env, plan));
|
|
204
|
+
return capAfter(input, await finish(applySetup(env, plan)), limit, guided ? pick : null);
|
|
200
205
|
}
|
|
201
206
|
const ask = io.ask ??
|
|
202
207
|
(process.stdin.isTTY
|
|
@@ -219,5 +224,49 @@ export async function setupCommand(input) {
|
|
|
219
224
|
return 0;
|
|
220
225
|
}
|
|
221
226
|
}
|
|
222
|
-
return finish(applySetup(env, plan));
|
|
227
|
+
return capAfter(input, await finish(applySetup(env, plan)), limit, null);
|
|
228
|
+
}
|
|
229
|
+
/** `--limit`: dollars a month above zero, `none` to remove the cap, or undefined when it isn't given. */
|
|
230
|
+
function capFlag(value) {
|
|
231
|
+
if (value === undefined)
|
|
232
|
+
return undefined;
|
|
233
|
+
if (value === 'none')
|
|
234
|
+
return null;
|
|
235
|
+
const usd = Number(String(value).replace(/^\$/, ''));
|
|
236
|
+
if (typeof value !== 'string' || !Number.isFinite(usd) || usd <= 0)
|
|
237
|
+
die('--limit takes dollars a month above zero, such as 20, or none', 'setup_limit');
|
|
238
|
+
return usd;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* After setup has written its files: the key's monthly spend cap (PATCH
|
|
242
|
+
* /key/budget, a calendar month). `--limit` sets it; without one, setup at a
|
|
243
|
+
* terminal asks once, only when the key has no cap yet. Returns setup's exit code.
|
|
244
|
+
*/
|
|
245
|
+
async function capAfter(input, code, limit, pick) {
|
|
246
|
+
const { env, io, baseUrl } = input;
|
|
247
|
+
const apiKey = resolveApiKey(env);
|
|
248
|
+
if (!apiKey || (limit === undefined && !pick))
|
|
249
|
+
return code;
|
|
250
|
+
let cap = limit;
|
|
251
|
+
if (cap === undefined) {
|
|
252
|
+
const now = await request('GET', '/key/budget', apiKey, undefined, baseUrl).catch(() => null);
|
|
253
|
+
if (!now || now.budget_usd !== null)
|
|
254
|
+
return code;
|
|
255
|
+
io.stderr('\n');
|
|
256
|
+
const answer = await choose(pick, 'Cap what this key can spend each month?', [
|
|
257
|
+
{ label: 'No cap', hint: 'free use is never charged', value: 0 },
|
|
258
|
+
{ label: '$10 a month', value: 10 },
|
|
259
|
+
{ label: '$25 a month', value: 25 },
|
|
260
|
+
{ label: '$50 a month', value: 50 },
|
|
261
|
+
]);
|
|
262
|
+
if (!answer)
|
|
263
|
+
return code;
|
|
264
|
+
cap = answer;
|
|
265
|
+
}
|
|
266
|
+
await request('PATCH', '/key/budget', apiKey, cap === null ? { budget_usd: null } : { budget_usd: cap, period_days: null }, baseUrl);
|
|
267
|
+
const p = paint(env, colorOn(env, io.isTTY));
|
|
268
|
+
io.stderr(cap === null
|
|
269
|
+
? ` ${p.lime('✓')} This key has no monthly cap now.\n`
|
|
270
|
+
: ` ${p.lime('✓')} This key can spend at most $${cap} a month. Change it any time: ${p.cyan('phantom setup --limit <USD|none>')}\n`);
|
|
271
|
+
return code;
|
|
223
272
|
}
|
package/dist/spend/command.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { colorOn, paint } from "../core/style.mjs";
|
|
|
6
6
|
import { choose } from "../core/pick.mjs";
|
|
7
7
|
import { hideHints, lineupScreen, priceHint, routeScreen, routeTestScreen, TIER_ABOUT } from "../cli/screens.mjs";
|
|
8
8
|
import { getLineup, putLineup, resetLineup, LINEUP_TIERS } from "./lineup.mjs";
|
|
9
|
-
import { getRoute, putRoute, patchRoute, clearRoute, testRoute, parseCondition } from "./routing.mjs";
|
|
9
|
+
import { getRoute, putRoute, patchRoute, clearRoute, testRoute, parseCondition, SORT_WORDS } from "./routing.mjs";
|
|
10
10
|
/** `phantom route`: the key's route policy, its rules, and a dry run. Run by `run()`. */
|
|
11
11
|
export async function routeCommand(input) {
|
|
12
12
|
const { argv, io, apiKey, subcommand, flags, baseUrl, env } = input;
|
|
@@ -44,11 +44,28 @@ export async function routeCommand(input) {
|
|
|
44
44
|
fields.stick_minutes = stick;
|
|
45
45
|
if (flags['stick-by-prompt'] !== undefined)
|
|
46
46
|
fields.stick_by_prompt = flags['stick-by-prompt'] !== 'false';
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const sort = flags['sort'];
|
|
48
|
+
if (sort !== undefined && (typeof sort !== 'string' || !(sort in SORT_WORDS || sort === 'default'))) {
|
|
49
|
+
die('--sort takes fast (quickest to start answering), cheap (the default), throughput (most tokens a second) or default', 'route_usage');
|
|
50
|
+
}
|
|
51
|
+
if (typeof sort === 'string' && sort !== 'default')
|
|
52
|
+
fields.sort = SORT_WORDS[sort];
|
|
53
|
+
if (Object.keys(fields).length === 0 && sort !== 'default') {
|
|
54
|
+
die('route set needs --models a,b,c, --sort, --applies-to, --on-empty, --fallback, --stick-minutes, --stick-by-prompt, or --file policy.json');
|
|
49
55
|
}
|
|
50
56
|
const current = (await getRoute(apiKey, baseUrl)).route_policy;
|
|
51
|
-
|
|
57
|
+
let result;
|
|
58
|
+
if (sort === 'default') {
|
|
59
|
+
// Back to cheapest first: drop sort, and the whole policy when sort was all it held.
|
|
60
|
+
const { sort: _was, ...rest } = { ...(current ?? { models: [] }), ...fields };
|
|
61
|
+
void _was;
|
|
62
|
+
const empty = !rest.models.length && !rest.zdr && !rest.no_training && !(rest.rules ?? []).length;
|
|
63
|
+
result = !current ? { route_policy: null } : empty ? await clearRoute(apiKey, baseUrl) : await putRoute(apiKey, rest, baseUrl);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
// A key with no policy gets one of preferences only, which routes nothing, unless --models is given too.
|
|
67
|
+
result = current ? await patchRoute(apiKey, fields, baseUrl) : await putRoute(apiKey, { models: [], ...fields }, baseUrl);
|
|
68
|
+
}
|
|
52
69
|
out(result, table, tableRoute, io.stdout);
|
|
53
70
|
}
|
|
54
71
|
}
|
package/dist/spend/routing.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { die } from "../core/errors.mjs";
|
|
2
2
|
import { request } from "../core/request.mjs";
|
|
3
|
+
/** The words `phantom route set --sort` takes, and the server's name for each. The server's own names work too. */
|
|
4
|
+
export const SORT_WORDS = { cheap: 'cost', fast: 'ttft', throughput: 'tps', cost: 'cost', ttft: 'ttft', tps: 'tps' };
|
|
3
5
|
/** The key's route policy (GET /key/route). */
|
|
4
6
|
export function getRoute(apiKey, baseUrl) {
|
|
5
7
|
return request('GET', '/key/route', apiKey, undefined, baseUrl);
|
package/package.json
CHANGED
|
@@ -102,6 +102,18 @@ phantom route test --table
|
|
|
102
102
|
Routing affects `auto` requests. Use `PHANTOM_KEY_NAME=coding` to manage the same
|
|
103
103
|
key used by an agent with `--key coding`. Route tests are free.
|
|
104
104
|
|
|
105
|
+
## Pick phantom-1, phantom-1:free or phantom-1:zdr
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
phantom models
|
|
109
|
+
phantom models use phantom-1:zdr
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`phantom models` lists Phantom's three models with the jobs each can do and the
|
|
113
|
+
model behind each job. `phantom-1` does every job; `phantom-1:free` is chat at $0;
|
|
114
|
+
`phantom-1:zdr` is chat on providers that keep no data. `models use` sets the model
|
|
115
|
+
`phantom claude`, `phantom codex`, `phantom pi` and the next `phantom setup` start on.
|
|
116
|
+
|
|
105
117
|
## Pick phantom-1's models
|
|
106
118
|
|
|
107
119
|
```bash
|