@phantomcodes/cli 1.4.1 → 1.4.3
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 +99 -133
- package/dist/agents/command.d.mts +36 -0
- package/dist/agents/command.mjs +22 -10
- package/dist/agents/proxy.d.mts +44 -0
- package/dist/agents/proxy.mjs +4 -2
- package/dist/cli/app.d.mts +25 -0
- package/dist/cli/app.mjs +2 -2
- package/dist/cli/commands.d.mts +23 -0
- package/dist/cli/commands.mjs +106 -54
- package/dist/cli/help.d.mts +26 -0
- package/dist/cli/help.mjs +10 -6
- package/dist/cli/run.d.mts +2 -0
- package/dist/cli/screens.d.mts +171 -0
- package/dist/cli/screens.mjs +3 -3
- package/dist/cli/start.d.mts +34 -0
- package/dist/core/brand.d.mts +12 -0
- package/dist/core/errors.d.mts +27 -0
- package/dist/core/free.d.mts +13 -0
- package/dist/core/helpers.d.mts +75 -0
- package/dist/core/helpers.mjs +27 -2
- package/dist/core/motion.d.mts +47 -0
- package/dist/core/node-trust.d.mts +12 -0
- package/dist/core/pick.d.mts +86 -0
- package/dist/core/request.d.mts +64 -0
- package/dist/core/request.mjs +78 -29
- package/dist/core/secrets.d.mts +33 -0
- package/dist/core/shapes.d.mts +13 -0
- package/dist/core/state.d.mts +3 -0
- package/dist/core/style.d.mts +49 -0
- package/dist/core/version.d.mts +2 -0
- package/dist/core/version.mjs +1 -1
- package/dist/keys/command.d.mts +16 -0
- package/dist/keys/saved.d.mts +90 -0
- package/dist/keys/saved.mjs +6 -2
- package/dist/models/choice.d.mts +7 -0
- package/dist/models/choice.mjs +1 -1
- package/dist/models/command.d.mts +8 -0
- package/dist/models/list.d.mts +39 -0
- package/dist/phantom.d.mts +45 -0
- package/dist/receipts/command.d.mts +7 -0
- package/dist/receipts/receipts.d.mts +67 -0
- package/dist/receipts/receipts.mjs +15 -11
- package/dist/setup/agents.d.mts +149 -0
- package/dist/setup/agents.mjs +50 -37
- package/dist/setup/apply.d.mts +71 -0
- package/dist/setup/apply.mjs +7 -2
- package/dist/setup/command.d.mts +12 -0
- package/dist/setup/command.mjs +32 -5
- package/dist/setup/editors.d.mts +101 -0
- package/dist/setup/editors.mjs +6 -4
- package/dist/setup/legacy.d.mts +13 -0
- package/dist/setup/plan.d.mts +104 -0
- package/dist/setup/plan.mjs +5 -3
- package/dist/setup/screen.d.mts +30 -0
- package/dist/setup/screen.mjs +2 -2
- package/dist/setup/shared.d.mts +141 -0
- package/dist/setup/shared.mjs +8 -2
- package/dist/setup/was.d.mts +42 -0
- package/dist/setup/was.mjs +91 -0
- package/dist/spend/command.d.mts +5 -0
- package/dist/spend/lineup.d.mts +54 -0
- package/dist/spend/lineup.mjs +3 -2
- package/dist/spend/report.d.mts +105 -0
- package/dist/spend/report.mjs +2 -2
- package/dist/spend/routing.d.mts +72 -0
- package/dist/workspace/command.d.mts +59 -0
- package/dist/workspace/command.mjs +106 -52
- package/package.json +7 -7
- package/skills/phantom-ai/SKILL.md +40 -66
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { Env } from '../core/state.mts';
|
|
2
|
+
import { type SetupAgent, type ProviderResult } from './shared.mts';
|
|
3
|
+
import type { JsonWas } from './was.mts';
|
|
4
|
+
/** One file setup changes, with its text before and after. */
|
|
5
|
+
export type SetupFile = {
|
|
6
|
+
/** The agent it is for, or `shell` for the startup file. */
|
|
7
|
+
agent: SetupAgent | 'shell';
|
|
8
|
+
/** The file. */
|
|
9
|
+
file: string;
|
|
10
|
+
/** `create`, `update` or `remove`. */
|
|
11
|
+
action: 'create' | 'update' | 'remove';
|
|
12
|
+
/** Its text now. null when it does not exist. */
|
|
13
|
+
before: string | null;
|
|
14
|
+
/** Its text after. null removes it. */
|
|
15
|
+
after: string | null;
|
|
16
|
+
/** True for the skill: phantom's own file, so it gets no backup and no diff. */
|
|
17
|
+
skill?: boolean;
|
|
18
|
+
/** The file's mode, for a file that holds a key. */
|
|
19
|
+
mode?: number;
|
|
20
|
+
/** The record of JSON keys setup wrote, to save once the file is written. null forgets it. */
|
|
21
|
+
was?: JsonWas | null;
|
|
22
|
+
};
|
|
23
|
+
/** What {@link planSetup} works out, for {@link applySetup} to write. */
|
|
24
|
+
export type SetupPlan = {
|
|
25
|
+
/** One entry per agent, as {@link SetupResult} reports it. */
|
|
26
|
+
agents: SetupResult['agents'];
|
|
27
|
+
/** The files to change. */
|
|
28
|
+
files: SetupFile[];
|
|
29
|
+
/** What was left out, and why. */
|
|
30
|
+
skipped: Array<{
|
|
31
|
+
/** The agent, or `shell` for the startup file. */
|
|
32
|
+
agent: SetupAgent | 'shell';
|
|
33
|
+
/** Why. */
|
|
34
|
+
reason: string;
|
|
35
|
+
}>;
|
|
36
|
+
/** Things to tell the user. */
|
|
37
|
+
notes: string[];
|
|
38
|
+
};
|
|
39
|
+
/** What {@link setupAgents} did. */
|
|
40
|
+
export type SetupResult = {
|
|
41
|
+
/** One entry per agent set up. */
|
|
42
|
+
agents: Array<{
|
|
43
|
+
/** The agent. */
|
|
44
|
+
agent: SetupAgent;
|
|
45
|
+
/** The path of the skill. null for an agent without skills. */
|
|
46
|
+
skill: string | null;
|
|
47
|
+
/** What `--provider` (or plain `phantom setup`) changed in the agent's settings, for an agent that supports it. */
|
|
48
|
+
provider?: ProviderResult;
|
|
49
|
+
}>;
|
|
50
|
+
/** Each file written, or with `--dry-run` each file that would be. */
|
|
51
|
+
files: Array<{
|
|
52
|
+
/** The agent it is for, or `shell`. */
|
|
53
|
+
agent: SetupAgent | 'shell';
|
|
54
|
+
/** The file. */
|
|
55
|
+
file: string;
|
|
56
|
+
/** `create`, `update` or `remove`. */
|
|
57
|
+
action: SetupFile['action'];
|
|
58
|
+
/** The copy of the file from before phantom changed it. */
|
|
59
|
+
backup?: string;
|
|
60
|
+
/** The change as a line diff, with keys masked. Left out for the skill. */
|
|
61
|
+
diff?: string;
|
|
62
|
+
}>;
|
|
63
|
+
/** What was left alone, and why, such as a config file that does not parse. */
|
|
64
|
+
skipped?: SetupPlan['skipped'];
|
|
65
|
+
/** Things to know, such as setting up without a key. */
|
|
66
|
+
notes?: string[];
|
|
67
|
+
/** True with `--dry-run`: nothing was written. */
|
|
68
|
+
dry_run?: boolean;
|
|
69
|
+
/** False when the user said no at the prompt: nothing was written. */
|
|
70
|
+
applied?: boolean;
|
|
71
|
+
/** False when only part of setup could run (no key yet, so the agents weren't pointed at Phantom AI). */
|
|
72
|
+
complete?: boolean;
|
|
73
|
+
};
|
|
74
|
+
/** Options for {@link planSetup} and {@link setupAgents}. */
|
|
75
|
+
export type SetupOptions = {
|
|
76
|
+
/** One agent, or `all` (the default): every agent whose folder is found. */
|
|
77
|
+
agent?: string;
|
|
78
|
+
/** With `all`, only these of the agents found: the ones ticked in setup's checklist. */
|
|
79
|
+
only?: readonly SetupAgent[];
|
|
80
|
+
/** Point the agents' own model calls at Phantom AI, or `off` to undo that. */
|
|
81
|
+
provider?: boolean | 'off';
|
|
82
|
+
/** The model to start each agent on, instead of {@link SETUP_MODEL}. */
|
|
83
|
+
model?: string;
|
|
84
|
+
/**
|
|
85
|
+
* False leaves the shell startup file alone (`--no-shell`, or "Your shell"
|
|
86
|
+
* unticked): setup then lists the lines to add by hand, so agents that read
|
|
87
|
+
* the key from the environment can still find it.
|
|
88
|
+
*/
|
|
89
|
+
shell?: boolean;
|
|
90
|
+
/** Plain `phantom setup`: with no key yet, install the skills and say so, rather than refuse. */
|
|
91
|
+
guided?: boolean;
|
|
92
|
+
/** Whether a program is on PATH. Defaults to checking for real. */
|
|
93
|
+
onPath?: (bin: string) => boolean;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Works out what setup would change, and writes nothing: the skill for each
|
|
97
|
+
* agent ({@link SETUP_AGENTS}), each agent's model
|
|
98
|
+
* settings with `provider` ({@link SetupAgentSpec.provider}), and the shell
|
|
99
|
+
* block ({@link shellBlock}) for agents that read the key from the
|
|
100
|
+
* environment. A config file setup can't edit safely is left out and listed
|
|
101
|
+
* in `skipped`.
|
|
102
|
+
* @throws {@link CliError} when no agent is found, the agent is unknown, the model is not a plain id, `provider` is set without an agent that supports it, or there is no key for `provider`.
|
|
103
|
+
*/
|
|
104
|
+
export declare function planSetup(env: Env, opts?: SetupOptions): SetupPlan;
|
package/dist/setup/plan.mjs
CHANGED
|
@@ -65,19 +65,21 @@ export function planSetup(env, opts = {}) {
|
|
|
65
65
|
const files = [];
|
|
66
66
|
const skipped = [];
|
|
67
67
|
const agents = [];
|
|
68
|
-
const add = (agent, file, after, skill = false, mode) => {
|
|
68
|
+
const add = (agent, file, after, skill = false, mode, was) => {
|
|
69
69
|
const known = files.find((f) => f.file === file);
|
|
70
70
|
if (known) {
|
|
71
71
|
known.after = after;
|
|
72
72
|
if (mode)
|
|
73
73
|
known.mode = mode;
|
|
74
|
+
if (was !== undefined)
|
|
75
|
+
known.was = was;
|
|
74
76
|
known.action = known.before === null ? 'create' : after === null ? 'remove' : 'update';
|
|
75
77
|
return;
|
|
76
78
|
}
|
|
77
79
|
const before = readText(file);
|
|
78
80
|
if (before === after)
|
|
79
81
|
return;
|
|
80
|
-
files.push({ agent, file, action: before === null ? 'create' : after === null ? 'remove' : 'update', before, after, ...(skill ? { skill } : {}), ...(mode ? { mode } : {}) });
|
|
82
|
+
files.push({ agent, file, action: before === null ? 'create' : after === null ? 'remove' : 'update', before, after, ...(skill ? { skill } : {}), ...(mode ? { mode } : {}), ...(was !== undefined ? { was } : {}) });
|
|
81
83
|
};
|
|
82
84
|
const skillText = readFileSync(skillSource(), 'utf-8');
|
|
83
85
|
const shellOn = new Map();
|
|
@@ -92,7 +94,7 @@ export function planSetup(env, opts = {}) {
|
|
|
92
94
|
try {
|
|
93
95
|
const p = spec.provider(ctx, provider === 'off');
|
|
94
96
|
for (const e of p.edits)
|
|
95
|
-
add(agent, e.file, e.after, false, e.mode);
|
|
97
|
+
add(agent, e.file, e.after, false, e.mode, e.was);
|
|
96
98
|
result = p.result;
|
|
97
99
|
if (spec.shellEnv) {
|
|
98
100
|
if (provider === 'off')
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Env } from '../core/state.mts';
|
|
2
|
+
import type { SetupResult } from './plan.mts';
|
|
3
|
+
/** Which moment the screen shows. */
|
|
4
|
+
export type ScreenMode =
|
|
5
|
+
/** Before "Make these changes?": what setup is about to do. */
|
|
6
|
+
'ask'
|
|
7
|
+
/** `--dry-run`: what setup would do; nothing was written. */
|
|
8
|
+
| 'dry'
|
|
9
|
+
/** The user said no at the prompt. */
|
|
10
|
+
| 'declined'
|
|
11
|
+
/** The changes are made. */
|
|
12
|
+
| 'done';
|
|
13
|
+
export type ScreenOptions = {
|
|
14
|
+
/** The environment, for the home folder (shown as ~) and the terminal's colors. */
|
|
15
|
+
env?: Env;
|
|
16
|
+
/** Print color, and the ghost. Off unless asked for, so tests and pipes get plain text. */
|
|
17
|
+
color?: boolean;
|
|
18
|
+
/** Show every line each file gains or loses (`--diff`). */
|
|
19
|
+
diff?: boolean;
|
|
20
|
+
/** Which moment to show. Worked out from the result when left out. */
|
|
21
|
+
mode?: ScreenMode;
|
|
22
|
+
/** The terminal's width, so long lines wrap at a word with an indent. 100 when unknown. */
|
|
23
|
+
columns?: number;
|
|
24
|
+
/** Say how to see every line. Off when a picker offers that as a choice instead. */
|
|
25
|
+
hint?: boolean;
|
|
26
|
+
};
|
|
27
|
+
/** `text` broken at spaces into lines of at most `width` characters; a single longer word stays whole. */
|
|
28
|
+
export declare function wrapWords(text: string, width: number): string[];
|
|
29
|
+
/** `phantom setup`'s screen for one result. See {@link ScreenMode} for the moments it shows. */
|
|
30
|
+
export declare function setupScreen(r: SetupResult, opts?: ScreenOptions): string;
|
package/dist/setup/screen.mjs
CHANGED
|
@@ -149,7 +149,7 @@ export function setupScreen(r, opts = {}) {
|
|
|
149
149
|
lines.push(` ${p.dim('Undo:')} phantom keeps a copy of each file it changes, and ${p.cyan('phantom setup --restore')} puts them back.`);
|
|
150
150
|
}
|
|
151
151
|
if (skills)
|
|
152
|
-
lines.push(` ${p.dim('
|
|
152
|
+
lines.push(` ${p.dim('Skill:')} a file that teaches the agent the phantom commands.`);
|
|
153
153
|
// Most people only see the 'ask' screen, so its hint is the plain command they just ran, plus --diff.
|
|
154
154
|
if (!opts.diff && opts.hint !== false && r.files.some((f) => f.diff)) {
|
|
155
155
|
lines.push('', mode === 'ask' ? ` To see every line first, answer n and run: ${p.cyan('phantom setup --diff')}` : ` To see every line, add ${p.cyan('--diff')}.`);
|
|
@@ -221,7 +221,7 @@ function doneSummary(agent, a, files, skipped, p) {
|
|
|
221
221
|
return 'loads your Phantom AI key in new terminals.';
|
|
222
222
|
const prov = a?.provider;
|
|
223
223
|
if (prov?.manual?.length)
|
|
224
|
-
return p.yellow('not finished:
|
|
224
|
+
return p.yellow('not finished: do these steps by hand.');
|
|
225
225
|
if (prov && !prov.on)
|
|
226
226
|
return !files ? p.dim('already off: nothing to remove.') : agent === 'claude' ? 'back on Anthropic.' : 'Phantom AI settings removed.';
|
|
227
227
|
if (prov?.on) {
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { Env } from '../core/state.mts';
|
|
2
|
+
import type { JsonWas } from './was.mts';
|
|
3
|
+
/** True when `bin` is a program on PATH. */
|
|
4
|
+
export declare function onPath(bin: string): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* `phantom setup` installs the phantom-ai skill where each agent looks for skills,
|
|
7
|
+
* and points each agent's own model calls at Phantom AI. Every agent here
|
|
8
|
+
* reads the same SKILL.md format, so one file serves all of them.
|
|
9
|
+
*
|
|
10
|
+
* Nothing is written straight away. {@link planSetup} works out each file's
|
|
11
|
+
* text before and after, so `phantom setup` can show the change as a diff and ask
|
|
12
|
+
* first, and {@link applySetup} writes it, keeping a copy of each file it
|
|
13
|
+
* edits beside it as `<file>.phantom.bak`. Lines phantom adds to a TOML, YAML or shell
|
|
14
|
+
* file end with `# phantom setup`, so `--provider off` removes exactly those.
|
|
15
|
+
*
|
|
16
|
+
* The key never goes into an agent's config. Claude Code runs `phantom key show`
|
|
17
|
+
* through `apiKeyHelper`. Agents that read an environment variable get
|
|
18
|
+
* PHANTOM_API_KEY from a block phantom adds to the shell's startup file, which
|
|
19
|
+
* reads the key from the macOS Keychain (or `phantom key show`) each time a shell
|
|
20
|
+
* starts ({@link shellBlock}). Cursor and Cline keep their model settings on
|
|
21
|
+
* their own settings screens, so setup prints the steps.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* What {@link planSetup} hands each agent's functions in {@link SETUP_AGENTS}.
|
|
25
|
+
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
26
|
+
*/
|
|
27
|
+
export type SetupContext = {
|
|
28
|
+
/** The environment phantom runs in. */
|
|
29
|
+
env: Env;
|
|
30
|
+
/** The home folder agents are found in. */
|
|
31
|
+
home: string;
|
|
32
|
+
/** Whether a program is on PATH. Defaults to checking for real. */
|
|
33
|
+
onPath?: (bin: string) => boolean;
|
|
34
|
+
/** The model `--model` asked for, if any. */
|
|
35
|
+
model?: string;
|
|
36
|
+
/** The shell startup file the key block goes in ({@link shellRcFile}). */
|
|
37
|
+
shellRc: string;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* One file a setup step changes.
|
|
41
|
+
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
42
|
+
*/
|
|
43
|
+
export type SetupEdit = {
|
|
44
|
+
/** The file. */
|
|
45
|
+
file: string;
|
|
46
|
+
/** Its text after the change. null removes the file. */
|
|
47
|
+
after: string | null;
|
|
48
|
+
/** The file's mode, for a file that holds a key. */
|
|
49
|
+
mode?: number;
|
|
50
|
+
/** What setup recorded about the JSON keys it wrote, for `--provider off` to put back ({@link JsonWas}). null forgets the record. */
|
|
51
|
+
was?: JsonWas | null;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* What {@link SetupAgentSpec.provider} returns.
|
|
55
|
+
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
56
|
+
*/
|
|
57
|
+
export type ProviderPlan = {
|
|
58
|
+
/** The files to write. Empty for an agent set up on its settings screen or by the shell block alone. */
|
|
59
|
+
edits: SetupEdit[];
|
|
60
|
+
/** What to report. */
|
|
61
|
+
result: ProviderResult;
|
|
62
|
+
};
|
|
63
|
+
/** An agent name `phantom setup --agent` takes, other than `all`. */
|
|
64
|
+
export type SetupAgent = 'pi' | 'codex' | 'claude' | 'cursor' | 'opencode' | 'continue' | 'goose' | 'aider' | 'copilot' | 'cline' | 'gemini' | 'crush' | 'qwen' | 'kilo' | 'kimi' | 'droid' | 'vibe' | 'zed' | 'vscode' | 'jetbrains' | 'warp' | 'trae';
|
|
65
|
+
/**
|
|
66
|
+
* One agent `phantom setup` knows: where to find it, where its skill goes, and what else setup can do for it.
|
|
67
|
+
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
68
|
+
*/
|
|
69
|
+
export type SetupAgentSpec = {
|
|
70
|
+
/** The agent's name in messages, such as `Claude Code`. */
|
|
71
|
+
label: string;
|
|
72
|
+
/** The folder (or file) in the home directory whose presence means the agent is installed. */
|
|
73
|
+
home: string;
|
|
74
|
+
/** Whether the agent is installed, for one found somewhere other than {@link SetupAgentSpec.home}, such as `$CODEX_HOME`. */
|
|
75
|
+
detect?: (env: Env, home: string) => boolean;
|
|
76
|
+
/** The folder in the home directory the skill goes in. Left out for agents without skills. */
|
|
77
|
+
skills?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Points the agent's own model calls at Phantom AI, or with `off` removes
|
|
80
|
+
* what phantom wrote. Throws a {@link CliError} with code `setup_unreadable` for
|
|
81
|
+
* a config file it can't edit safely, and setup skips that agent.
|
|
82
|
+
*/
|
|
83
|
+
provider?: (ctx: SetupContext, off: boolean) => ProviderPlan;
|
|
84
|
+
/**
|
|
85
|
+
* Variables the agent reads, set in the shell block next to
|
|
86
|
+
* PHANTOM_API_KEY. A value may use `$PHANTOM_API_KEY`. Present (even empty)
|
|
87
|
+
* for every agent that reads the key from the environment.
|
|
88
|
+
*/
|
|
89
|
+
shellEnv?: (ctx: SetupContext) => Record<string, string>;
|
|
90
|
+
};
|
|
91
|
+
/** The model setup starts each agent on when `--model` is not given. */
|
|
92
|
+
export declare const SETUP_MODEL = "phantom-1";
|
|
93
|
+
/** The coding agents Phantom AI serves at their own base path. */
|
|
94
|
+
export type AgentPath = 'codex' | 'claude-code' | 'cursor' | 'coding-agent' | 'gemini-cli';
|
|
95
|
+
/**
|
|
96
|
+
* The address Phantom AI serves a coding agent at, from PHANTOM_BASE_URL
|
|
97
|
+
* without its `/v1`: `/claude-code` (Claude Code adds `/v1` itself),
|
|
98
|
+
* `/gemini-cli` (Gemini CLI adds Google's `/v1beta`), or `/codex/v1`,
|
|
99
|
+
* `/cursor/v1` and `/coding-agent/v1` for the others.
|
|
100
|
+
* @throws {@link CliError} when PHANTOM_BASE_URL is not https ({@link httpsOnly}).
|
|
101
|
+
*/
|
|
102
|
+
export declare function agentBaseUrl(env: Env, agent: AgentPath): string;
|
|
103
|
+
/** The end of every line phantom adds to a TOML, YAML or shell file. */
|
|
104
|
+
export declare const SETUP_MARK = "# phantom setup";
|
|
105
|
+
/** The start of a line phantom turned into a comment because it replaced it. `--provider off` puts it back. */
|
|
106
|
+
export declare const SETUP_WAS = "# phantom setup was: ";
|
|
107
|
+
/** True for a line setup added: it ends with {@link SETUP_MARK}, or the mark pai wrote. */
|
|
108
|
+
export declare function setupMarked(line: string): boolean;
|
|
109
|
+
/** The line without the setup mark on its end. */
|
|
110
|
+
export declare function stripMark(line: string): string;
|
|
111
|
+
/** A line setup turned into a comment, put back as it was. Other lines come back unchanged. */
|
|
112
|
+
export declare function unwas(line: string): string;
|
|
113
|
+
/** A file's text, or null when it does not exist. Other read errors pass through. */
|
|
114
|
+
export declare function readText(file: string): string | null;
|
|
115
|
+
/** Refuses a file setup can't edit safely. Setup skips the agent and says why. */
|
|
116
|
+
export declare function unreadable(file: string, why: string): never;
|
|
117
|
+
/** How to run this phantom from outside this shell: `phantom` if it's installed, else this node and script by absolute path. */
|
|
118
|
+
export declare function phantomCommand(onPathFn: (bin: string) => boolean): string;
|
|
119
|
+
/** `$XDG_CONFIG_HOME` when it is an absolute path, else `~/.config`. */
|
|
120
|
+
export declare function configHome(env: Env, home: string): string;
|
|
121
|
+
/** A writer's `off` result for a file it edits by removing its own lines or entries. */
|
|
122
|
+
export declare function offResult(label: string, file: string, text: string | null, after: string | null, forgetJsonWas?: boolean): ProviderPlan;
|
|
123
|
+
/** What {@link claudeProvider} changed, and what each agent's provider step reports. */
|
|
124
|
+
export type ProviderResult = {
|
|
125
|
+
/** The agent's settings file, or the shell startup file for an agent set up by environment variables alone. */
|
|
126
|
+
settings: string;
|
|
127
|
+
/** True when the agent now runs on Phantom AI; false after `--provider off`, and for an agent set up on its settings screen. */
|
|
128
|
+
on: boolean;
|
|
129
|
+
/** The base URL the agent now calls. */
|
|
130
|
+
base_url?: string;
|
|
131
|
+
/** The model the agent now starts with, if phantom set one. */
|
|
132
|
+
model?: string;
|
|
133
|
+
/** Things the user should know, such as an ANTHROPIC_AUTH_TOKEN in the shell that would win over the Phantom key. */
|
|
134
|
+
warnings: string[];
|
|
135
|
+
/** Steps to follow by hand, for an agent whose model settings live on its settings screen. */
|
|
136
|
+
manual?: string[];
|
|
137
|
+
};
|
|
138
|
+
/** A model as Claude Code names it: `phantom-1:zdr` is `claude-code/phantom-1:zdr`, and one that has the prefix keeps one. */
|
|
139
|
+
export declare function claudeModelName(model: string): string;
|
|
140
|
+
/** The context window Phantom AI's catalog gives phantom-1. Goose and Aider size requests by it. */
|
|
141
|
+
export declare const SETUP_CONTEXT_WINDOW = 1000000;
|
package/dist/setup/shared.mjs
CHANGED
|
@@ -71,12 +71,18 @@ export function configHome(env, home) {
|
|
|
71
71
|
return env.XDG_CONFIG_HOME && path.isAbsolute(env.XDG_CONFIG_HOME) ? env.XDG_CONFIG_HOME : path.join(home, '.config');
|
|
72
72
|
}
|
|
73
73
|
/** A writer's `off` result for a file it edits by removing its own lines or entries. */
|
|
74
|
-
export function offResult(label, file, text, after) {
|
|
74
|
+
export function offResult(label, file, text, after, forgetJsonWas = false) {
|
|
75
75
|
const changed = text !== null && after !== text;
|
|
76
76
|
return {
|
|
77
|
-
edits: changed ? [{ file, after: after || null }] : [],
|
|
77
|
+
edits: changed ? [{ file, after: after || null, ...(forgetJsonWas ? { was: null } : {}) }] : [],
|
|
78
78
|
result: { settings: file, on: false, warnings: changed ? [] : [`${label} was not set to Phantom AI by phantom; nothing changed`] },
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
|
+
/** The prefix Claude Code's `/model` picker needs to keep a model: it drops ids without "claude" in them. The server strips it again. */
|
|
82
|
+
const CLAUDE_CODE_PREFIX = 'claude-code/';
|
|
83
|
+
/** A model as Claude Code names it: `phantom-1:zdr` is `claude-code/phantom-1:zdr`, and one that has the prefix keeps one. */
|
|
84
|
+
export function claudeModelName(model) {
|
|
85
|
+
return CLAUDE_CODE_PREFIX + (model.startsWith(CLAUDE_CODE_PREFIX) ? model.slice(CLAUDE_CODE_PREFIX.length) : model);
|
|
86
|
+
}
|
|
81
87
|
/** The context window Phantom AI's catalog gives phantom-1. Goose and Aider size requests by it. */
|
|
82
88
|
export const SETUP_CONTEXT_WINDOW = 1_000_000;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type Env } from '../core/state.mts';
|
|
2
|
+
import { type JsonChange } from './editors.mts';
|
|
3
|
+
/**
|
|
4
|
+
* What `phantom setup` replaced in a JSON settings file, so `--provider off` can put it back.
|
|
5
|
+
*
|
|
6
|
+
* TOML and YAML files keep this in the file itself: the replaced line stays as a `# phantom setup was:`
|
|
7
|
+
* comment. JSON has no comments, and Claude Code and the other agents read these files strictly, so
|
|
8
|
+
* the record lives in `setup-json-was.json` in the state folder instead (mode 600, because a replaced
|
|
9
|
+
* value can be a key the user had in the file).
|
|
10
|
+
*/
|
|
11
|
+
/** One key setup wrote in a JSON file. */
|
|
12
|
+
export type JsonWasEntry = {
|
|
13
|
+
/** The keys from the top of the file down to the value, such as `['env', 'ANTHROPIC_MODEL']`. */
|
|
14
|
+
path: string[];
|
|
15
|
+
/** True when the key held a value before setup wrote it. */
|
|
16
|
+
had: boolean;
|
|
17
|
+
/** The value before setup wrote the key. Missing when `had` is false. */
|
|
18
|
+
was?: unknown;
|
|
19
|
+
/** What setup wrote. Off puts the old value back only while the key still holds this. */
|
|
20
|
+
wrote: unknown;
|
|
21
|
+
};
|
|
22
|
+
/** Every key setup wrote in one file. */
|
|
23
|
+
export type JsonWas = JsonWasEntry[];
|
|
24
|
+
/** What setup recorded for `file`, or an empty list. */
|
|
25
|
+
export declare function readJsonWas(env: Env, file: string): JsonWas;
|
|
26
|
+
/** Saves what setup recorded for `file`. null forgets it. */
|
|
27
|
+
export declare function writeJsonWas(env: Env, file: string, entries: JsonWas | null): void;
|
|
28
|
+
/**
|
|
29
|
+
* The record after setup writes `changes` to a file whose parsed content is `settings`. Each key remembers
|
|
30
|
+
* the value that was there. When setup runs again over its own value, the older record stays, so the
|
|
31
|
+
* user's first value survives any number of runs.
|
|
32
|
+
* @param previous What setup recorded for this file before.
|
|
33
|
+
* @param tracked The paths to record. Other changes are not remembered (a key setup adds to a list, say).
|
|
34
|
+
*/
|
|
35
|
+
export declare function trackJsonChanges(settings: Record<string, unknown>, previous: JsonWas, changes: JsonChange[], tracked: string[][]): JsonWas;
|
|
36
|
+
/**
|
|
37
|
+
* The changes for `--provider off`. `removals` are what an agent's own off step would do (it knows which
|
|
38
|
+
* values look like phantom's); this swaps in the record: a key still holding what setup wrote gets its old
|
|
39
|
+
* value back, or goes if setup added it, and a key the user changed since is left alone. A container
|
|
40
|
+
* that a removal of a key setup added leaves empty goes too, and so does each one in `tidy` that ends up empty.
|
|
41
|
+
*/
|
|
42
|
+
export declare function restoreJsonChanges(settings: Record<string, unknown>, record: JsonWas, removals: JsonChange[], tidy?: string[][]): JsonChange[];
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
3
|
+
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { stateDir } from "../core/state.mjs";
|
|
5
|
+
import { jsonGet, jsonSet } from "./editors.mjs";
|
|
6
|
+
const recordFile = (env) => path.join(stateDir(env), 'setup-json-was.json');
|
|
7
|
+
function readAll(env) {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(readFileSync(recordFile(env), 'utf-8'));
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return {};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** What setup recorded for `file`, or an empty list. */
|
|
16
|
+
export function readJsonWas(env, file) {
|
|
17
|
+
return readAll(env)[file] ?? [];
|
|
18
|
+
}
|
|
19
|
+
/** Saves what setup recorded for `file`. null forgets it. */
|
|
20
|
+
export function writeJsonWas(env, file, entries) {
|
|
21
|
+
const all = readAll(env);
|
|
22
|
+
if (entries === null || entries.length === 0)
|
|
23
|
+
delete all[file];
|
|
24
|
+
else
|
|
25
|
+
all[file] = entries;
|
|
26
|
+
mkdirSync(stateDir(env), { recursive: true, mode: 0o700 });
|
|
27
|
+
writeFileSync(recordFile(env), JSON.stringify(all, null, 2) + '\n', { mode: 0o600 });
|
|
28
|
+
chmodSync(recordFile(env), 0o600);
|
|
29
|
+
}
|
|
30
|
+
const same = (a, b) => a.length === b.length && a.every((k, i) => k === b[i]);
|
|
31
|
+
const startsWith = (path, prefix) => prefix.length <= path.length && prefix.every((k, i) => k === path[i]);
|
|
32
|
+
/**
|
|
33
|
+
* The record after setup writes `changes` to a file whose parsed content is `settings`. Each key remembers
|
|
34
|
+
* the value that was there. When setup runs again over its own value, the older record stays, so the
|
|
35
|
+
* user's first value survives any number of runs.
|
|
36
|
+
* @param previous What setup recorded for this file before.
|
|
37
|
+
* @param tracked The paths to record. Other changes are not remembered (a key setup adds to a list, say).
|
|
38
|
+
*/
|
|
39
|
+
export function trackJsonChanges(settings, previous, changes, tracked) {
|
|
40
|
+
const next = [...previous];
|
|
41
|
+
for (const [path, value] of changes) {
|
|
42
|
+
if (value === undefined || !tracked.some((t) => same(t, path)))
|
|
43
|
+
continue;
|
|
44
|
+
const current = jsonGet(settings, path);
|
|
45
|
+
const at = next.findIndex((e) => same(e.path, path));
|
|
46
|
+
// Setup's own value from an earlier run: the record of what came before it still holds.
|
|
47
|
+
if (at !== -1 && isDeepStrictEqual(current, next[at].wrote)) {
|
|
48
|
+
next[at] = { ...next[at], wrote: value };
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const entry = { path, had: current !== undefined, ...(current !== undefined ? { was: current } : {}), wrote: value };
|
|
52
|
+
if (at === -1)
|
|
53
|
+
next.push(entry);
|
|
54
|
+
else
|
|
55
|
+
next[at] = entry;
|
|
56
|
+
}
|
|
57
|
+
return next;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The changes for `--provider off`. `removals` are what an agent's own off step would do (it knows which
|
|
61
|
+
* values look like phantom's); this swaps in the record: a key still holding what setup wrote gets its old
|
|
62
|
+
* value back, or goes if setup added it, and a key the user changed since is left alone. A container
|
|
63
|
+
* that a removal of a key setup added leaves empty goes too, and so does each one in `tidy` that ends up empty.
|
|
64
|
+
*/
|
|
65
|
+
export function restoreJsonChanges(settings, record, removals, tidy = []) {
|
|
66
|
+
const restores = [];
|
|
67
|
+
for (const e of record) {
|
|
68
|
+
if (isDeepStrictEqual(jsonGet(settings, e.path), e.wrote))
|
|
69
|
+
restores.push([e.path, e.had ? e.was : undefined]);
|
|
70
|
+
}
|
|
71
|
+
// A removal at a tracked key, or above one, is the record's to decide.
|
|
72
|
+
const rest = removals.filter(([p]) => !record.some((e) => startsWith(e.path, p)));
|
|
73
|
+
const changes = [...restores, ...rest];
|
|
74
|
+
// Remove what setup created and left empty, starting from the nearest container.
|
|
75
|
+
const after = structuredClone(settings);
|
|
76
|
+
for (const [p, v] of changes)
|
|
77
|
+
jsonSet(after, p, v);
|
|
78
|
+
// The containers to check, nearest first: where a removed key lived, and each one asked for in `tidy`.
|
|
79
|
+
const containers = [...restores.filter(([, v]) => v === undefined).map(([p]) => p.slice(0, -1)), ...tidy].filter((c) => c.length > 0);
|
|
80
|
+
for (const container of containers) {
|
|
81
|
+
for (let depth = container.length; depth >= 1; depth--) {
|
|
82
|
+
const parent = container.slice(0, depth);
|
|
83
|
+
const node = jsonGet(after, parent);
|
|
84
|
+
if (!node || typeof node !== 'object' || Object.keys(node).length > 0)
|
|
85
|
+
break;
|
|
86
|
+
jsonSet(after, parent, undefined);
|
|
87
|
+
changes.push([parent, undefined]);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return changes;
|
|
91
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type CommandInput } from '../core/helpers.mts';
|
|
2
|
+
/** `phantom route`: the key's route policy, its rules, and a dry run. Run by `run()`. */
|
|
3
|
+
export declare function routeCommand(input: CommandInput): Promise<number>;
|
|
4
|
+
/** `phantom lineup`: the model phantom-1 runs in each tier, and a fallback for each. Run by `run()`. */
|
|
5
|
+
export declare function lineupCommand(input: CommandInput): Promise<number>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A key's lineup decides which model `phantom-1` runs in each tier. The server
|
|
3
|
+
* checks every pick against the tier's choices
|
|
4
|
+
* (routers/phantom-1/runtime/src/lineup.ts in the Phantom repo), so the CLI
|
|
5
|
+
* passes model ids through.
|
|
6
|
+
*/
|
|
7
|
+
/** The three tiers `phantom-1` picks between, cheapest first. */
|
|
8
|
+
export declare const LINEUP_TIERS: readonly ["economy", "standard", "frontier"];
|
|
9
|
+
/** One of {@link LINEUP_TIERS}. */
|
|
10
|
+
export type LineupTier = (typeof LINEUP_TIERS)[number];
|
|
11
|
+
/** A model id for each tier that has one. */
|
|
12
|
+
export type LineupPicks = Partial<Record<LineupTier, string>>;
|
|
13
|
+
/** A key's lineup as the server stores it. A tier left out runs its default. */
|
|
14
|
+
export type Lineup = LineupPicks & {
|
|
15
|
+
/** The model to try when the tier's model fails. A tier left out keeps the usual fallback. */
|
|
16
|
+
fallbacks?: LineupPicks;
|
|
17
|
+
};
|
|
18
|
+
/** One model a tier can run, with its price per million tokens. */
|
|
19
|
+
export type LineupChoice = {
|
|
20
|
+
/** Model id. */
|
|
21
|
+
model: string;
|
|
22
|
+
/** Display name. */
|
|
23
|
+
name: string;
|
|
24
|
+
/** USD per million input tokens. null when the catalog doesn't list the model right now. */
|
|
25
|
+
input_per_m: number | null;
|
|
26
|
+
/** USD per million output tokens. null when the catalog doesn't list the model right now. */
|
|
27
|
+
output_per_m: number | null;
|
|
28
|
+
/** Context window in tokens. */
|
|
29
|
+
context: number | null;
|
|
30
|
+
/** True for the tier's default. */
|
|
31
|
+
default: boolean;
|
|
32
|
+
};
|
|
33
|
+
/** The model a tier runs. */
|
|
34
|
+
export type LineupTierModel = {
|
|
35
|
+
/** Model id. */
|
|
36
|
+
model: string;
|
|
37
|
+
/** Display name. */
|
|
38
|
+
name: string;
|
|
39
|
+
};
|
|
40
|
+
/** The answer from the /key/lineup calls. */
|
|
41
|
+
export type LineupResult = {
|
|
42
|
+
/** What the key changed from the defaults. null means every tier runs its default. */
|
|
43
|
+
lineup: Lineup | null;
|
|
44
|
+
/** The model each tier runs now. */
|
|
45
|
+
tiers: Record<LineupTier, LineupTierModel>;
|
|
46
|
+
/** Each tier's choices, default first. Only GET sends them. */
|
|
47
|
+
choices?: Record<LineupTier, LineupChoice[]>;
|
|
48
|
+
};
|
|
49
|
+
/** The key's lineup, the model each tier runs, and each tier's choices (GET /key/lineup). */
|
|
50
|
+
export declare function getLineup(apiKey: string, baseUrl?: string): Promise<LineupResult>;
|
|
51
|
+
/** Replaces the key's whole lineup (PUT /key/lineup). The server checks it before saving. */
|
|
52
|
+
export declare function putLineup(apiKey: string, lineup: Lineup, baseUrl?: string): Promise<LineupResult>;
|
|
53
|
+
/** Puts every tier back to its default (DELETE /key/lineup). */
|
|
54
|
+
export declare function resetLineup(apiKey: string, baseUrl?: string): Promise<LineupResult>;
|
package/dist/spend/lineup.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { request } from "../core/request.mjs";
|
|
2
2
|
/**
|
|
3
3
|
* A key's lineup decides which model `phantom-1` runs in each tier. The server
|
|
4
|
-
* checks every pick against the tier's choices
|
|
5
|
-
* repo), so the CLI
|
|
4
|
+
* checks every pick against the tier's choices
|
|
5
|
+
* (routers/phantom-1/runtime/src/lineup.ts in the Phantom repo), so the CLI
|
|
6
|
+
* passes model ids through.
|
|
6
7
|
*/
|
|
7
8
|
/** The three tiers `phantom-1` picks between, cheapest first. */
|
|
8
9
|
export const LINEUP_TIERS = ['economy', 'standard', 'frontier'];
|