billion-context-pi 0.1.34 → 0.1.35

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/dist/config.d.ts CHANGED
@@ -22,6 +22,12 @@ export interface AdapterConfig {
22
22
  * section. Default: true. Set `delegate: false` (adapter config or
23
23
  * ~/.pi/acp.json) to skip registering them. */
24
24
  delegate?: boolean;
25
+ /** How to display delegate usage in session totals.
26
+ * "merged" — delegate usage is added to main session totals (Pi default).
27
+ * "separate" — delegate usage is tracked separately, displayed in
28
+ * system notifications (excluded from main session totals).
29
+ * Default: "separate" */
30
+ displayUsage?: "merged" | "separate";
25
31
  /** Default timeout in seconds injected into the bash tool when the model
26
32
  * omits `timeout`. Pi has NO built-in default, so without this a command
27
33
  * that the model forgets to time out can hang for thousands of seconds.
@@ -1,3 +1,27 @@
1
+ export interface Usage {
2
+ input: number;
3
+ output: number;
4
+ cacheRead: number;
5
+ cacheWrite: number;
6
+ cacheWrite1h?: number;
7
+ reasoning?: number;
8
+ totalTokens: number;
9
+ cost: {
10
+ input: number;
11
+ output: number;
12
+ cacheRead: number;
13
+ cacheWrite: number;
14
+ total: number;
15
+ };
16
+ }
17
+ export interface UsageUpdateEvent {
18
+ kind: "usage-update";
19
+ usage: Usage;
20
+ }
21
+ export interface AgentSettledEvent {
22
+ kind: "agent-settled";
23
+ }
24
+ export declare function handleMessageEnd(event: Record<string, unknown>): UsageUpdateEvent | null;
1
25
  export interface ToolStartEvent {
2
26
  kind: "tool-start";
3
27
  toolName: string;
@@ -25,7 +49,7 @@ export interface ThinkingDeltaEvent {
25
49
  kind: "thinking-delta";
26
50
  delta: string;
27
51
  }
28
- export type ParsedEvent = ToolStartEvent | ToolUpdateEvent | ToolEndEvent | ReplyDeltaEvent | ReplyCompleteEvent | ThinkingDeltaEvent | ThinkingEndEvent | RetryStartEvent | RetryEndEvent;
52
+ export type ParsedEvent = ToolStartEvent | ToolUpdateEvent | ToolEndEvent | ReplyDeltaEvent | ReplyCompleteEvent | ThinkingDeltaEvent | ThinkingEndEvent | RetryStartEvent | RetryEndEvent | UsageUpdateEvent | AgentSettledEvent;
29
53
  export interface ThinkingEndEvent {
30
54
  kind: "thinking-end";
31
55
  }
@@ -37,10 +61,12 @@ export interface ThinkingEndEvent {
37
61
  export declare class ThinkingCollector {
38
62
  private readonly showThinking;
39
63
  private buf;
64
+ private usage;
40
65
  constructor(showThinking: boolean);
41
66
  push(delta: string): void;
42
- /** Return the segment line to write ("" when empty or disabled), resetting. */
67
+ process(ev: ParsedEvent): void;
43
68
  flush(): string;
69
+ getUsage(): Usage | undefined;
44
70
  }
45
71
  export interface RetryStartEvent {
46
72
  kind: "retry-start";
@@ -1,7 +1,44 @@
1
- import { type SpawnOptions } from "node:child_process";
1
+ import { type ChildProcess, type SpawnOptions } from "node:child_process";
2
2
  import { Type, type Static } from "typebox";
3
- import type { ExtensionAPI, ExtensionContext, ToolDefinition } from "@earendil-works/pi-coding-agent";
3
+ import type { AgentToolResult, ExtensionAPI, ExtensionContext, ToolDefinition } from "@earendil-works/pi-coding-agent";
4
+ import { type Usage } from "./delegate-events.js";
4
5
  export declare function delegateSpawnOptions(cwd: string, env: NodeJS.ProcessEnv): SpawnOptions;
6
+ type RunStatus = "running" | "completed" | "failed" | "cancelled";
7
+ interface DelegateRun {
8
+ runId: string;
9
+ agent: string;
10
+ task: string;
11
+ cwd: string;
12
+ startedAt: number;
13
+ finishedAt?: number;
14
+ status: RunStatus;
15
+ exitCode?: number | null;
16
+ child?: ChildProcess;
17
+ result?: {
18
+ code: number | null;
19
+ file: string;
20
+ body: string;
21
+ };
22
+ consumed?: boolean;
23
+ /** True once the close handler injected the result as a system
24
+ * notification (sendUserMessage succeeded). Lets a later wait() avoid
25
+ * re-delivering the same payload. */
26
+ injected?: boolean;
27
+ /** Watchdog reason string when the run was force-terminated ("no output for
28
+ * 5m", "30m limit"); surfaced in completion headers as "(timed out: ...)". */
29
+ timedOut?: string;
30
+ waiter?: () => void;
31
+ /** Accumulated LLM usage from the delegate (from message_end events). */
32
+ usage?: Usage;
33
+ /** True once a wait/cancel tool has returned usage — prevents double-count. */
34
+ usageReported?: boolean;
35
+ /** True once agent_settled fired; a watchdog kill after this is stuck teardown, not a timeout. */
36
+ agentSettled?: boolean;
37
+ }
38
+ export declare function addDelegateUsage(u: Usage): void;
39
+ export declare function getDelegateUsage(): Usage | undefined;
40
+ export declare function resetDelegateUsage(): void;
41
+ export declare function setDelegateDisplayUsage(mode: "merged" | "separate"): void;
5
42
  /** Snapshot of currently-running delegate runs, for the TUI status widget. */
6
43
  export declare function runningRunsSnapshot(): {
7
44
  runId: string;
@@ -36,6 +73,8 @@ export interface EventApplier {
36
73
  * never lost from the file. */
37
74
  export declare function makeEventApplier(opts: {
38
75
  showThinking: boolean;
76
+ onUsage?: (usage: Usage) => void;
77
+ onSettled?: () => void;
39
78
  }, writers: EventApplierWriters): EventApplier;
40
79
  declare const DelegateParams: Type.TObject<{
41
80
  agent: Type.TString;
@@ -53,6 +92,7 @@ declare const WaitParams: Type.TObject<{
53
92
  runId: Type.TString;
54
93
  timeout: Type.TOptional<Type.TInteger>;
55
94
  }>;
95
+ export declare function accumulateUsage(a: Usage | undefined, b: Usage): Usage;
56
96
  export declare function makeDelegateTool(pi: ExtensionAPI): ToolDefinition<typeof DelegateParams>;
57
97
  /** If the delegate already delivered its result via a system notification
58
98
  * (the close handler injected before this wait was called), return a short
@@ -66,6 +106,25 @@ export declare function injectedWaitMessage(run: {
66
106
  file: string;
67
107
  };
68
108
  }, runId: string, remainingLine: string): string | null;
109
+ /** Build usage-aware return payload. Sets usageReported=true so subsequent
110
+ * waits on the same run skip usage. */
111
+ export declare function buildWaitResult(run: DelegateRun, content: string, mode?: "merged" | "separate", contentType?: "text"): {
112
+ details: undefined;
113
+ content: {
114
+ type: "text";
115
+ text: string;
116
+ }[];
117
+ usage?: AgentToolResult<unknown>["usage"];
118
+ };
119
+ /** Build usage-aware result for cancel tool. */
120
+ export declare function buildCancelResult(run: DelegateRun, content: string, mode?: "merged" | "separate"): {
121
+ details: undefined;
122
+ content: {
123
+ type: "text";
124
+ text: string;
125
+ }[];
126
+ usage?: AgentToolResult<unknown>["usage"];
127
+ };
69
128
  export declare function makeDelegateWaitTool(_pi: ExtensionAPI): ToolDefinition<typeof WaitParams>;
70
129
  export declare function makeDelegateCancelTool(_pi: ExtensionAPI): ToolDefinition<typeof CancelParams>;
71
130
  export declare function buildChildArgs(args: DelegateArgs, rolePrompt: string, ctx: ExtensionContext): Promise<{
@@ -74,4 +133,5 @@ export declare function buildChildArgs(args: DelegateArgs, rolePrompt: string, c
74
133
  isAsync: boolean;
75
134
  useJsonStream: boolean;
76
135
  }>;
136
+ export declare function injectResult(pi: ExtensionAPI, agent: string, runId: string, task: string, code: number | null, file: string, timedOut?: string, usage?: Usage, mode?: "merged" | "separate", usageAlreadyReported?: boolean): boolean;
77
137
  export {};
@@ -18,6 +18,17 @@ export interface WatchdogHandle {
18
18
  poke(): void;
19
19
  /** Stop all timers (call on finalize). */
20
20
  dispose(): void;
21
+ /**
22
+ * agent_settled has been received: the agent's full flow (prompt + continue
23
+ * loop + retries) is over and pi emits this exactly once in the finally of
24
+ * _runAgentPrompt, after which the process should exit within milliseconds.
25
+ * If it is still alive after graceMs, the process is stuck in teardown
26
+ * (e.g. a provider call not returning) — kill it via killByWatchdog. graceMs
27
+ * is symmetric with EOF_GRACE_MS (10s): normal exits are millisecond-level,
28
+ * so 10s only hits genuinely hung processes. Idempotent (no-op when already
29
+ * settled or a grace timer is pending); dispose() clears the timer.
30
+ */
31
+ settledGrace(graceMs: number, _killGraceMs: number, reason: string): void;
21
32
  }
22
33
  /**
23
34
  * Guarantees a hung child process gets killed. A stuck child holds its stdout
@@ -0,0 +1,8 @@
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+ /** Mirrors pi's footer.js formatTokens: lowercase k/M, thresholds <1000/<10000/<1e6/<1e7. */
3
+ export declare function formatCompactTokens(count: number): string;
4
+ export declare function initFooterStatus(ctx: ExtensionContext): void;
5
+ /** Refresh the footer delegate-usage line. Cheap: reads the accumulated total
6
+ * and no-ops when the rendered text is unchanged (called on a 500ms tick). */
7
+ export declare function updateFooterStatus(): void;
8
+ export declare function disposeFooterStatus(): void;