@phantomcodes/cli 1.6.3 → 1.7.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.
Files changed (139) hide show
  1. package/README.md +138 -117
  2. package/bin/phantom.mjs +37 -2
  3. package/dist/agents/command.d.mts +8 -27
  4. package/dist/agents/command.mjs +323 -59
  5. package/dist/agents/login.d.mts +3 -0
  6. package/dist/agents/login.mjs +97 -0
  7. package/dist/agents/opencode-check.d.mts +15 -0
  8. package/dist/agents/opencode-check.mjs +193 -0
  9. package/dist/agents/phantom-key.d.mts +7 -0
  10. package/dist/agents/phantom-key.mjs +40 -0
  11. package/dist/agents/proxy.d.mts +26 -28
  12. package/dist/agents/proxy.mjs +225 -33
  13. package/dist/chatgpt/command.d.mts +2 -0
  14. package/dist/chatgpt/command.mjs +119 -0
  15. package/dist/chatgpt/session.d.mts +87 -0
  16. package/dist/chatgpt/session.mjs +253 -0
  17. package/dist/cli/accounts.d.mts +21 -0
  18. package/dist/cli/accounts.mjs +96 -0
  19. package/dist/cli/app.d.mts +4 -24
  20. package/dist/cli/app.mjs +224 -139
  21. package/dist/cli/commands.d.mts +1 -16
  22. package/dist/cli/commands.mjs +144 -239
  23. package/dist/cli/help.d.mts +0 -19
  24. package/dist/cli/help.mjs +30 -55
  25. package/dist/cli/run.mjs +35 -79
  26. package/dist/cli/start.d.mts +2 -31
  27. package/dist/cli/start.mjs +13 -116
  28. package/dist/core/brand.d.mts +0 -7
  29. package/dist/core/brand.mjs +0 -8
  30. package/dist/core/errors.d.mts +0 -16
  31. package/dist/core/errors.mjs +0 -16
  32. package/dist/core/helpers.d.mts +0 -46
  33. package/dist/core/helpers.mjs +1 -64
  34. package/dist/core/node-trust.d.mts +0 -5
  35. package/dist/core/node-trust.mjs +0 -6
  36. package/dist/core/pick.d.mts +7 -55
  37. package/dist/core/pick.mjs +49 -106
  38. package/dist/core/request.d.mts +0 -46
  39. package/dist/core/request.mjs +1 -60
  40. package/dist/core/secrets.d.mts +0 -4
  41. package/dist/core/secrets.mjs +0 -10
  42. package/dist/core/state.d.mts +3 -1
  43. package/dist/core/state.mjs +147 -4
  44. package/dist/core/style.d.mts +0 -27
  45. package/dist/core/style.mjs +0 -20
  46. package/dist/core/version.d.mts +1 -2
  47. package/dist/core/version.mjs +1 -2
  48. package/dist/local/accounts.d.mts +25 -0
  49. package/dist/local/accounts.mjs +40 -0
  50. package/dist/local/agent-headers.d.mts +7 -0
  51. package/dist/local/agent-headers.mjs +44 -0
  52. package/dist/local/chatgpt-plan.d.mts +147 -0
  53. package/dist/local/chatgpt-plan.mjs +384 -0
  54. package/dist/local/command.d.mts +24 -0
  55. package/dist/local/command.mjs +211 -0
  56. package/dist/local/config.d.mts +49 -0
  57. package/dist/local/config.mjs +168 -0
  58. package/dist/local/decide.d.mts +37 -0
  59. package/dist/local/decide.mjs +201 -0
  60. package/dist/local/failure-signs.d.mts +4 -0
  61. package/dist/local/failure-signs.mjs +99 -0
  62. package/dist/local/keys.d.mts +32 -0
  63. package/dist/local/keys.mjs +239 -0
  64. package/dist/local/models.d.mts +53 -0
  65. package/dist/local/models.mjs +117 -0
  66. package/dist/local/replay.d.mts +59 -0
  67. package/dist/local/replay.mjs +143 -0
  68. package/dist/local/report.d.mts +43 -0
  69. package/dist/local/report.mjs +153 -0
  70. package/dist/local/request-log.d.mts +53 -0
  71. package/dist/local/request-log.mjs +69 -0
  72. package/dist/local/retry-after.d.mts +3 -0
  73. package/dist/local/retry-after.mjs +28 -0
  74. package/dist/local/rust-router.d.mts +39 -0
  75. package/dist/local/rust-router.mjs +54 -0
  76. package/dist/local/server.d.mts +27 -0
  77. package/dist/local/server.mjs +449 -0
  78. package/dist/local/status-line.d.mts +17 -0
  79. package/dist/local/status-line.mjs +81 -0
  80. package/dist/local/tiers.d.mts +3 -0
  81. package/dist/local/tiers.mjs +137 -0
  82. package/dist/local/traces.d.mts +69 -0
  83. package/dist/local/traces.mjs +341 -0
  84. package/dist/phantom.d.mts +0 -37
  85. package/dist/phantom.mjs +0 -31
  86. package/package.json +15 -9
  87. package/skills/phantom-ai/SKILL.md +28 -97
  88. package/dist/cli/screens.d.mts +0 -177
  89. package/dist/cli/screens.mjs +0 -398
  90. package/dist/core/free.d.mts +0 -13
  91. package/dist/core/free.mjs +0 -14
  92. package/dist/core/motion.d.mts +0 -47
  93. package/dist/core/motion.mjs +0 -156
  94. package/dist/core/shapes.d.mts +0 -13
  95. package/dist/core/shapes.mjs +0 -1
  96. package/dist/keys/command.d.mts +0 -16
  97. package/dist/keys/command.mjs +0 -143
  98. package/dist/keys/saved.d.mts +0 -96
  99. package/dist/keys/saved.mjs +0 -169
  100. package/dist/models/choice.d.mts +0 -7
  101. package/dist/models/choice.mjs +0 -27
  102. package/dist/models/command.d.mts +0 -8
  103. package/dist/models/command.mjs +0 -66
  104. package/dist/models/list.d.mts +0 -39
  105. package/dist/models/list.mjs +0 -39
  106. package/dist/receipts/command.d.mts +0 -7
  107. package/dist/receipts/command.mjs +0 -41
  108. package/dist/receipts/receipts.d.mts +0 -76
  109. package/dist/receipts/receipts.mjs +0 -118
  110. package/dist/setup/agents.d.mts +0 -149
  111. package/dist/setup/agents.mjs +0 -886
  112. package/dist/setup/apply.d.mts +0 -85
  113. package/dist/setup/apply.mjs +0 -301
  114. package/dist/setup/command.d.mts +0 -12
  115. package/dist/setup/command.mjs +0 -307
  116. package/dist/setup/editors.d.mts +0 -108
  117. package/dist/setup/editors.mjs +0 -500
  118. package/dist/setup/legacy.d.mts +0 -13
  119. package/dist/setup/legacy.mjs +0 -36
  120. package/dist/setup/plan.d.mts +0 -104
  121. package/dist/setup/plan.mjs +0 -132
  122. package/dist/setup/screen.d.mts +0 -30
  123. package/dist/setup/screen.mjs +0 -260
  124. package/dist/setup/shared.d.mts +0 -141
  125. package/dist/setup/shared.mjs +0 -88
  126. package/dist/setup/was.d.mts +0 -42
  127. package/dist/setup/was.mjs +0 -91
  128. package/dist/spend/command.d.mts +0 -5
  129. package/dist/spend/command.mjs +0 -235
  130. package/dist/spend/lineup.d.mts +0 -61
  131. package/dist/spend/lineup.mjs +0 -39
  132. package/dist/spend/reason.d.mts +0 -5
  133. package/dist/spend/reason.mjs +0 -60
  134. package/dist/spend/report.d.mts +0 -105
  135. package/dist/spend/report.mjs +0 -68
  136. package/dist/spend/routing.d.mts +0 -74
  137. package/dist/spend/routing.mjs +0 -57
  138. package/dist/workspace/command.d.mts +0 -59
  139. package/dist/workspace/command.mjs +0 -871
@@ -1,68 +0,0 @@
1
- import { die } from "../core/errors.mjs";
2
- import { out, showsScreen } from "../core/helpers.mjs";
3
- import { colorOn } from "../core/style.mjs";
4
- import { barsGrowing, motionOn, play } from "../core/motion.mjs";
5
- import { spendScreen } from "../cli/screens.mjs";
6
- import { request } from "../core/request.mjs";
7
- import { workspaceGet } from "../workspace/command.mjs";
8
- /** The dates `--from` and `--to` ask for, as a query string. The server checks them. */
9
- function query(dates) {
10
- const params = new URLSearchParams();
11
- for (const [name, value] of Object.entries(dates)) {
12
- if (value === true)
13
- die(`--${name} requires a date as YYYY-MM-DD`);
14
- if (value !== undefined)
15
- params.set(name, value);
16
- }
17
- const text = params.toString();
18
- return text ? `?${text}` : '';
19
- }
20
- /** What the key spent (GET /key/spend). */
21
- export function getSpend(apiKey, dates = {}, baseUrl) {
22
- return request('GET', `/key/spend${query(dates)}`, apiKey, undefined, baseUrl);
23
- }
24
- const dollars = (n) => `$${n.toFixed(4)}`;
25
- const calls = (n) => `${n} ${n === 1 ? 'call' : 'calls'}`;
26
- /** `--table` output for `spend`. */
27
- export function tableSpend(d) {
28
- const s = d;
29
- const lines = [
30
- `range ${s.from} to ${s.to} (UTC)`,
31
- `spent ${dollars(s.total.spent_usd)} (${calls(s.total.calls)})`,
32
- ];
33
- if (!s.routing.available)
34
- lines.push(`phantom-1 saved unknown: ${s.routing.reason}`);
35
- else if (s.routing.calls === 0)
36
- lines.push('phantom-1 saved nothing yet: no calls went to phantom-1');
37
- else
38
- lines.push(`phantom-1 saved ${dollars(s.routing.saved_usd)} (${calls(s.routing.calls)} cost ${dollars(s.routing.spent_usd)}; on ${s.routing.baseline_model} they would cost ${dollars(s.routing.baseline_usd)})`);
39
- lines.push('', 'by key');
40
- for (const k of s.keys) {
41
- const saved = k.saved_usd === null ? '' : ` saved ${dollars(k.saved_usd)}`;
42
- lines.push(` ${k.name.padEnd(16)}${dollars(k.spent_usd)} ${calls(k.calls)}${saved}${k.active ? '' : ' (revoked or rotated)'}`);
43
- }
44
- lines.push('', 'by model');
45
- for (const m of s.models)
46
- lines.push(` ${m.model.padEnd(40)}${dollars(m.spent_usd)} ${calls(m.calls)}`);
47
- return lines.join('\n');
48
- }
49
- /**
50
- * `phantom spend`: what the key spent, by model and day, and what phantom-1
51
- * saved. `--workspace` shows every key in the workspace by name, with the
52
- * workspace connection `phantom login` saved. Run by `run()`.
53
- */
54
- export async function spendCommand(input) {
55
- const { io, apiKey, baseUrl, flags, env } = input;
56
- const dates = { from: flags['from'], to: flags['to'] };
57
- const result = flags['workspace']
58
- ? (await workspaceGet(input, `/spend${query(dates)}`))
59
- : await getSpend(apiKey, dates, baseUrl);
60
- const screen = showsScreen(input);
61
- // The bars grow in, the way the site's cost bars do; JSON, pipes and NO_COLOR get the result at once.
62
- if (screen && motionOn(env, io.stdoutIsTTY)) {
63
- await play(io.stdout, barsGrowing(spendScreen(result, { env, color: true }).split('\n')), { rows: process.stdout.rows, skipOnKey: true });
64
- return 0;
65
- }
66
- out(result, screen, (d) => spendScreen(d, { env, color: colorOn(env, io.stdoutIsTTY) }), io.stdout);
67
- return 0;
68
- }
@@ -1,74 +0,0 @@
1
- /**
2
- * A key's route policy decides which model `model: "auto"` runs. The server
3
- * validates it and owns the list of conditions and actions
4
- * (routers/phantom-1/runtime/src/route-policy.ts in the Phantom repo), so the
5
- * CLI passes it through as JSON.
6
- */
7
- /** One routing rule: when the condition in `if` holds, run the model `use` names. */
8
- export type RouteRule = {
9
- /** Exactly one condition, such as `{ pace: 'ahead' }` or `{ input_tokens_over: 50000 }`. */
10
- if: Record<string, string | number | boolean>;
11
- /** A model from the policy's `models`, or `cheapest`, `first` or `next`. */
12
- use: string;
13
- };
14
- /** A key's route policy, as the server stores and validates it. */
15
- export type RoutePolicy = {
16
- /** The models routing may pick from. The first is the default for `auto`. Empty: the policy holds preferences only (such as `sort`) and routes nothing. */
17
- models: string[];
18
- /** `auto` routes only requests for model `auto`; `all` routes every request. Default `auto`. */
19
- applies_to?: 'auto' | 'all';
20
- /** Rules checked in order. The first that matches picks the model. */
21
- rules?: RouteRule[];
22
- /** When the key's cap for the period is used up: `stop` refuses requests, `cheapest` keeps running on the cheapest model in `models`. Default `stop`. */
23
- on_empty?: 'stop' | 'cheapest';
24
- /** When true, the other models in `models` are tried if the picked one fails. Default false. */
25
- fallback_on_error?: boolean;
26
- /** Minutes a conversation stays on the model it started on after its last request, so its prompt cache stays warm. 0 turns it off. Default 5. */
27
- stick_minutes?: number;
28
- /** When true, a conversation without an x-phantom-session header is recognised by its opening messages. false: only the header counts. Default true. */
29
- stick_by_prompt?: boolean;
30
- /** Which providers serve this key's requests first: `cost` (cheapest, the default), `ttft` (quickest to start answering) or `tps` (most tokens a second). */
31
- sort?: ProviderSort;
32
- /** Only providers that delete prompts right away. */
33
- zdr?: boolean;
34
- /** Only providers that don't train on prompts. */
35
- no_training?: boolean;
36
- };
37
- /** The provider orders the server takes for `sort`. */
38
- export type ProviderSort = 'cost' | 'ttft' | 'tps';
39
- /** The words `phantom route set --sort` takes, and the server's name for each. The server's own names work too. */
40
- export declare const SORT_WORDS: Record<string, ProviderSort>;
41
- /** The answer from the /key/route calls. */
42
- export type RouteResult = {
43
- /** The key's policy. null means none, and model `auto` is refused. */
44
- route_policy: RoutePolicy | null;
45
- };
46
- /** The answer from POST /key/route/test. */
47
- export type RouteTestResult = {
48
- /** The model the request would run on. The server sends null when the key's cap is used up and the request would be refused. */
49
- model: string | null;
50
- /** What decided it: `<condition>:<value>` for a rule, or `default`, `requested`, `cache`, `on_empty` or `budget_exceeded`. The server sends null when the request is not routed and runs the model it named. */
51
- reason: string | null;
52
- /** True when the route policy picked the model, false when the request runs the model it named or is refused. */
53
- routed: boolean;
54
- /** Models tried next if this one fails, when `fallback_on_error` is on. */
55
- fallbacks?: string[];
56
- };
57
- /** The key's route policy (GET /key/route). */
58
- export declare function getRoute(apiKey: string, baseUrl?: string): Promise<RouteResult>;
59
- /** Replaces the key's route policy (PUT /key/route). The server checks it before saving. */
60
- export declare function putRoute(apiKey: string, policy: RoutePolicy, baseUrl?: string): Promise<RouteResult>;
61
- /** Changes some top-level fields of the route policy and keeps the rest (PATCH /key/route). A list sent here replaces the whole list. */
62
- export declare function patchRoute(apiKey: string, fields: Partial<RoutePolicy>, baseUrl?: string): Promise<RouteResult>;
63
- /** Removes the key's route policy (DELETE /key/route). */
64
- export declare function clearRoute(apiKey: string, baseUrl?: string): Promise<RouteResult>;
65
- /** Which model a request would run on now, and why (POST /key/route/test). Nothing is charged. A route policy picks without calling a model; `phantom-1`, `phantom-1:private`, or `auto` on a key with no policy can call the model that picks the tier. `phantom-1:free` never does. */
66
- export declare function testRoute(apiKey: string, opts: {
67
- model: string;
68
- messages?: unknown;
69
- tools?: unknown;
70
- }, baseUrl?: string): Promise<RouteTestResult>;
71
- /** The conditions a rule can test. The server owns this list; it is here so a typo is caught before a request is sent. */
72
- export declare const RULE_CONDITIONS: string[];
73
- /** `pace=ahead` or `input_tokens_over=50000` into one rule condition. */
74
- export declare function parseCondition(raw: string): Record<string, string | number | boolean>;
@@ -1,57 +0,0 @@
1
- import { die } from "../core/errors.mjs";
2
- import { PhantomApiError } from "../core/errors.mjs";
3
- import { request } from "../core/request.mjs";
4
- /** The words `phantom route set --sort` takes, and the server's name for each. The server's own names work too. */
5
- export const SORT_WORDS = { cheap: 'cost', fast: 'ttft', throughput: 'tps', cost: 'cost', ttft: 'ttft', tps: 'tps' };
6
- /** The key's route policy (GET /key/route). */
7
- export function getRoute(apiKey, baseUrl) {
8
- return request('GET', '/key/route', apiKey, undefined, baseUrl);
9
- }
10
- /**
11
- * The server's refusal of a policy reads `Invalid route policy: rules.2.if: Unrecognized key: "x"; ...`, with the
12
- * path of each field it checked. The paths mean nothing to a person typing flags, so they come off.
13
- */
14
- function plainPolicyError(err) {
15
- if (!(err instanceof PhantomApiError) || err.status !== 400 || !err.message.startsWith('Invalid route policy:'))
16
- return err;
17
- const problems = err.message
18
- .slice('Invalid route policy:'.length)
19
- .split(/;\s+/)
20
- .map((p) => p.trim().replace(/^[a-z_]+(?:\.[a-z0-9_]+)*:\s+(?=[A-Z])/, ''))
21
- .filter((p, k, all) => p && all.indexOf(p) === k);
22
- return new PhantomApiError(err.status, err.code, `Phantom AI did not accept that route policy. ${problems.join('; ')}`);
23
- }
24
- /** Replaces the key's route policy (PUT /key/route). The server checks it before saving. */
25
- export async function putRoute(apiKey, policy, baseUrl) {
26
- return request('PUT', '/key/route', apiKey, policy, baseUrl).catch((err) => {
27
- throw plainPolicyError(err);
28
- });
29
- }
30
- /** Changes some top-level fields of the route policy and keeps the rest (PATCH /key/route). A list sent here replaces the whole list. */
31
- export async function patchRoute(apiKey, fields, baseUrl) {
32
- return request('PATCH', '/key/route', apiKey, fields, baseUrl).catch((err) => {
33
- throw plainPolicyError(err);
34
- });
35
- }
36
- /** Removes the key's route policy (DELETE /key/route). */
37
- export function clearRoute(apiKey, baseUrl) {
38
- return request('DELETE', '/key/route', apiKey, undefined, baseUrl);
39
- }
40
- /** Which model a request would run on now, and why (POST /key/route/test). Nothing is charged. A route policy picks without calling a model; `phantom-1`, `phantom-1:private`, or `auto` on a key with no policy can call the model that picks the tier. `phantom-1:free` never does. */
41
- export function testRoute(apiKey, opts, baseUrl) {
42
- return request('POST', '/key/route/test', apiKey, opts, baseUrl);
43
- }
44
- /** The conditions a rule can test. The server owns this list; it is here so a typo is caught before a request is sent. */
45
- export const RULE_CONDITIONS = ['pace', 'budget_left_pct_below', 'days_left_below', 'has_tools', 'input_tokens_over', 'reasoning_requested'];
46
- /** `pace=ahead` or `input_tokens_over=50000` into one rule condition. */
47
- export function parseCondition(raw) {
48
- const eq = raw.indexOf('=');
49
- if (eq < 1)
50
- die(`Write a condition as name=value, for example pace=ahead`);
51
- const name = raw.slice(0, eq);
52
- if (!RULE_CONDITIONS.includes(name))
53
- die(`"${name}" is not a condition. Use one of: ${RULE_CONDITIONS.join(', ')}. For example --if pace=ahead.`, 'route_usage');
54
- const text = raw.slice(eq + 1);
55
- const value = text === 'true' ? true : text === 'false' ? false : text !== '' && Number.isFinite(Number(text)) ? Number(text) : text;
56
- return { [name]: value };
57
- }
@@ -1,59 +0,0 @@
1
- import { type Env } from '../core/state.mts';
2
- import { type CommandInput } from '../core/helpers.mts';
3
- export type Credentials = {
4
- version: 1;
5
- workspace_id: string;
6
- management_credential: string;
7
- base_url: string;
8
- recovery?: {
9
- version: 1;
10
- workspace_id: string;
11
- management_credential: string;
12
- };
13
- };
14
- /**
15
- * One call to the workspace API, with the limits every call shares ({@link fetchAnswer}): 30 seconds
16
- * unless `timeoutMs` says less, and a plain message for a web page, an empty answer or no connection.
17
- * @throws {@link PhantomApiError} when the API answers with an error status.
18
- */
19
- export declare function wire<T>(url: string, method: string, endpoint: string, body?: unknown, token?: string, timeoutMs?: number): Promise<T>;
20
- /** The workspace `phantom login` saved for this API address, or null when it saved none or saved one for another address. */
21
- export declare function savedWorkspace(input: CommandInput): Credentials | null;
22
- /** The management credential `phantom login` saved for this API address, or null when it saved none. */
23
- export declare function savedManagement(input: CommandInput): string | null;
24
- /** A read from the saved workspace, such as `/spend`, with its management credential. */
25
- export declare function workspaceGet(input: CommandInput, path: string): Promise<unknown>;
26
- /**
27
- * Device authorization for accounts or guests, saving keys locally without copying a secret through the browser.
28
- * With `inSetup`, `phantom setup` carries on once it is signed in, so it says that instead of printing the
29
- * saved key's details and the next commands.
30
- */
31
- export declare function workspaceLogin(input: CommandInput, opts?: {
32
- inSetup?: boolean;
33
- }): Promise<number>;
34
- /** Manage a workspace from the terminal: balance, keys, funding, recovery and revocation. */
35
- export declare function workspaceCommand(input: CommandInput): Promise<number>;
36
- /** Forget local management credentials and revoke server management access, leaving server-side keys and recovery files valid. True when there was a workspace sign-in to forget. */
37
- export declare function forgetWorkspace(env: Env, baseUrl?: string): Promise<boolean>;
38
- /**
39
- * `--amount` as dollars: `10`, `10.50` or `$10`. A bare `--amount` is
40
- * usually `--amount $5` after the shell has replaced `$5` with nothing, so
41
- * the message says to leave out the $.
42
- */
43
- export declare function dollarsFlag(flags: Record<string, string | true>): number | undefined;
44
- /** A workspace key as the view lists it: the parts used to find it by what people type. */
45
- type ListedKey = {
46
- id: string;
47
- name?: string;
48
- prefix?: string;
49
- spent_usd?: number;
50
- limit_usd?: number | null;
51
- /** False for a key turned off by a rotate or revoke. Missing counts as on. */
52
- is_active?: boolean;
53
- };
54
- /**
55
- * Finds one key by id, the start of its id, its name, or the start of the key itself. Refuses when none or more than one matches.
56
- * Keys that are turned off never match: after a rotate, the old key and the new one share a name.
57
- */
58
- export declare function findKey(allKeys: ListedKey[], typed: string): string;
59
- export {};