@phren/cli 0.3.10 → 0.3.11

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.
@@ -29,6 +29,7 @@ import { ModelCatalog } from "./models.js";
29
29
  import { ModelSwitcher } from "./model-switch.js";
30
30
  import { SideQuestions } from "./side-questions.js";
31
31
  import { AccountUsageReader } from "./usage.js";
32
+ import { ResourceMonitor } from "./resources.js";
32
33
  import { createScheduleLauncher, Scheduler, scheduleRunsFile } from "./schedules.js";
33
34
  import { dailyCanaryDue, runCanary } from "./canary.js";
34
35
  import { defaultPhrenPath } from "../shared.js";
@@ -81,6 +82,7 @@ export async function serve(version) {
81
82
  const sideQuestions = new SideQuestions();
82
83
  const contextUsage = new WorkspaceContextUsage();
83
84
  const accountUsage = new AccountUsageReader();
85
+ const resources = new ResourceMonitor();
84
86
  const tabActivity = new TabActivityStore();
85
87
  const codexQuestions = new CodexQuestions();
86
88
  const scheduleStore = defaultPhrenPath();
@@ -139,9 +141,10 @@ export async function serve(version) {
139
141
  read: workspacesReader({ modules, info, agentHooks, journal, tabActivity, contextUsage }),
140
142
  info: () => ({ ...info, capabilities: info.capabilities }),
141
143
  renew: server => agentHooks.overview.renew(server),
144
+ resources: () => resources.read(),
142
145
  });
143
146
  const http = createServer(createRouteHandler({ version, modules, info, computerID, scheduleStore, scheduler, dispatches, agentHooks,
144
- journal, tabActivity, contextUsage, modelCatalog, modelSwitcher, sideQuestions, accountUsage, codexQuestions, launches, locatedDirectories,
147
+ journal, tabActivity, contextUsage, modelCatalog, modelSwitcher, sideQuestions, accountUsage, resources, codexQuestions, launches, locatedDirectories,
145
148
  fanoutMessages, canary, streams, returns }));
146
149
  http.requestTimeout = 20_000;
147
150
  http.headersTimeout = 10_000;
@@ -165,7 +168,7 @@ export async function serve(version) {
165
168
  ws.clients.delete(oldest);
166
169
  }
167
170
  if (overviewServer !== undefined) {
168
- overview(client, overviewServer, url.searchParams.get("watchApprovals") === "1");
171
+ overview(client, overviewServer, url.searchParams.get("watchApprovals") === "1", url.searchParams.get("resources") === "1");
169
172
  return;
170
173
  }
171
174
  if (url.pathname === "/v1/speech/transcribe") {
@@ -297,6 +297,11 @@ export function phrenHookContext(raw, includeSidechain = false) {
297
297
  * private reasoning redacted, queued phone messages, background task
298
298
  * notifications and compaction markers. */
299
299
  export function visibleClaudeEvent(raw, includeSidechain = false) {
300
+ // A follow-up the phone sent a Claude fan-out worker, written into its
301
+ // event log by the Hook before the resumed run.
302
+ if (raw.type === "phren/fanout-message" && typeof raw.text === "string") {
303
+ return { type: "user", timestamp: raw.timestamp, message: { role: "user", content: raw.text.slice(0, 65_536) } };
304
+ }
300
305
  // A queued phone message carries the same wrapper; unwrap before the
301
306
  // digest so enqueue and remove keep matching keys.
302
307
  if (raw.type === "queue-operation" && typeof raw.content === "string")
@@ -9,6 +9,8 @@ export interface UsageWindow {
9
9
  limitUSD?: number;
10
10
  usedTokens?: number;
11
11
  resetsAt?: string;
12
+ /** The service says this window is refusing requests now (OpenCode Go's `rate-limited`). */
13
+ limited?: boolean;
12
14
  asOf?: string;
13
15
  }
14
16
  export interface UsageSpend {
@@ -16,7 +18,7 @@ export interface UsageSpend {
16
18
  period: "rolling_7_days" | "rolling_30_days" | "calendar_week";
17
19
  }
18
20
  export interface AccountUsage {
19
- source: "codex" | "claude" | "opencode" | "opencode-go" | "openrouter";
21
+ source: "codex" | "claude" | "opencode" | "opencode-go" | "openrouter" | "copilot";
20
22
  windows: UsageWindow[];
21
23
  updatedAt?: string;
22
24
  message?: string;
@@ -39,26 +41,32 @@ export declare function openCodeFailure(error: unknown): string;
39
41
  export declare function readOpenRouterKey(): Promise<string | undefined>;
40
42
  /** OpenCode Go's key stays local and is sent only to the Go gateway. */
41
43
  export declare function readOpenCodeGoKey(): Promise<string | undefined>;
42
- type GoPeriod = "5h" | "7d" | "30d";
43
- type GoLimit = {
44
- limitUSD: number;
45
- resetsAt?: string;
46
- };
47
- type GoLimits = Map<string, Partial<Record<GoPeriod, GoLimit>>>;
48
- type GoTotals = Map<string, Record<GoPeriod, {
49
- usedUSD: number;
50
- usedTokens: number;
51
- }>>;
52
- /** Best-effort gateway discovery. All four requests are independent because
53
- * undocumented routes vary by OpenCode release and account. */
54
- export declare function fetchOpenCodeGoLimits(key: string, fetchImpl?: typeof fetch): Promise<GoLimits>;
55
- /** Sum only completed OpenCode Go fan-outs. Their ledger stays on the
56
- * computer and malformed jobs are ignored rather than becoming usage. */
57
- export declare function readOpenCodeGoLedger(root?: string, now?: Date): Promise<GoTotals>;
44
+ /**
45
+ * Go's own report of the plan, `GET /zen/go/v1/usage`:
46
+ * `{usage: {rolling|weekly|monthly: {status, percent, resetsAt}}}`. It is
47
+ * account-wide, so it already counts every computer's use, and it is what
48
+ * Go enforces: `status: "rate-limited"` is the window that refuses requests.
49
+ * It carries no dollar amounts, so none are shown.
50
+ */
51
+ export declare function openCodeGoPlan(value: unknown): UsageWindow[];
52
+ export declare function fetchOpenCodeGoPlan(key: string, fetchImpl?: typeof fetch): Promise<UsageWindow[]>;
53
+ export interface GoRefusals {
54
+ count: number;
55
+ first: string;
56
+ last: string;
57
+ models: string[];
58
+ }
59
+ /** "Go usage limit exceeded" refusals in OpenCode's own log over the last
60
+ * `withinMs`. The first read of a log covers at most its last 64 MB. */
61
+ export declare function readGoRefusals(now?: Date, withinMs?: number, root?: string): Promise<GoRefusals | undefined>;
62
+ export declare function openCodeGoUsage(windows: UsageWindow[], refusals: GoRefusals | undefined, now: Date, hasKey: boolean, planError?: boolean): AccountUsage;
63
+ /** The accounts one caller can read: the sources it names, and Go's plan
64
+ * windows only for a caller that asked for them (`goPlan=1`). */
65
+ export declare function usageForCaller(accounts: AccountUsage[], sources: Set<string>, goPlan: boolean): AccountUsage[];
58
66
  export declare function readOpenCodeGoUsage(now?: Date, options?: {
59
- root?: string;
60
67
  readKey?: () => Promise<string | undefined>;
61
68
  fetchImpl?: typeof fetch;
69
+ readRefusals?: (now: Date) => Promise<GoRefusals | undefined>;
62
70
  }): Promise<AccountUsage>;
63
71
  /** OpenRouter reports the current UTC calendar week's charged usage per key. */
64
72
  export declare function fetchOpenRouterUsage(key: string, fetchImpl?: typeof fetch, now?: Date): Promise<AccountUsage>;
@@ -95,6 +103,14 @@ export declare function readClaudeToken(execSecurity?: typeof execFile.__promisi
95
103
  export declare function fetchClaudeUsage(token: string, fetchImpl?: typeof fetch, now?: Date): Promise<AccountUsage>;
96
104
  /** Only initialize and read limits. Never create a thread, prompt, or login. */
97
105
  export declare function readCodexLimits(executable?: string): Promise<AccountUsage>;
106
+ /**
107
+ * GitHub Copilot's own quota report (`gh api /copilot_internal/user`): one
108
+ * window per limited quota (premium requests, usually) with the monthly
109
+ * reset. Unlimited quotas are named in the message, not drawn as 0%.
110
+ */
111
+ export declare function copilotUsage(value: unknown, now?: Date): AccountUsage;
112
+ /** Reads through the GitHub CLI's own sign-in; the token never reaches Phren. */
113
+ export declare function readCopilotUsage(now?: Date, run?: (file: string, args: string[]) => Promise<string>): Promise<AccountUsage>;
98
114
  export declare class AccountUsageReader {
99
115
  private readCodex;
100
116
  private now;
@@ -102,13 +118,14 @@ export declare class AccountUsageReader {
102
118
  private readOpenCode;
103
119
  private readOpenRouter;
104
120
  private readOpenCodeGo;
121
+ private readCopilot;
105
122
  private cached?;
106
123
  private pending?;
107
124
  private claudeCached?;
108
125
  private claudePending?;
109
126
  private spendingCached?;
110
127
  private spendingPending?;
111
- constructor(readCodex?: typeof readCodexLimits, now?: () => number, readClaudeLive?: (now: Date) => Promise<AccountUsage | undefined>, readOpenCode?: (now: Date) => Promise<AccountUsage>, readOpenRouter?: (now: Date) => Promise<AccountUsage | undefined>, readOpenCodeGo?: (now: Date) => Promise<AccountUsage>);
128
+ constructor(readCodex?: typeof readCodexLimits, now?: () => number, readClaudeLive?: (now: Date) => Promise<AccountUsage | undefined>, readOpenCode?: (now: Date) => Promise<AccountUsage>, readOpenRouter?: (now: Date) => Promise<AccountUsage | undefined>, readOpenCodeGo?: (now: Date) => Promise<AccountUsage>, readCopilot?: (now: Date) => Promise<AccountUsage>);
112
129
  read(): Promise<{
113
130
  accounts: AccountUsage[];
114
131
  }>;
@@ -121,4 +138,3 @@ export declare class AccountUsageReader {
121
138
  /** Wrap an existing status line, preserving its input, output and options. */
122
139
  export declare function usageStatusLine(current: unknown, program: string, remove: boolean): unknown;
123
140
  export declare function captureClaudeUsage(original: string): Promise<void>;
124
- export {};