-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcmux.ts
More file actions
183 lines (146 loc) · 4.87 KB
/
cmux.ts
File metadata and controls
183 lines (146 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* cmux — Push pi agent state into the cmux sidebar.
*
* Hooks into pi lifecycle events and fires cmux CLI commands to update
* sidebar status keys, progress, and notifications. Fire-and-forget —
* errors are silently ignored so cmux issues never affect pi.
*
* No-op when CMUX_SOCKET_PATH is not set (i.e. not running inside cmux).
*
* Source: https://github.com/HazAT/pi-config/tree/main/extensions/cmux
*/
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
const CMUX_SOCKET = process.env.CMUX_SOCKET_PATH;
const GREEN = "#22C55E";
const AMBER = "#F59E0B";
const PURPLE = "#8B5CF6";
const BLUE = "#3B82F6";
const GRAY = "#6B7280";
/** Status keys owned by this extension — cleared on shutdown. */
const STATUS_KEYS = [
"pi_state",
"pi_model",
"pi_thinking",
"pi_tokens",
"pi_cost",
"pi_tool",
];
/** Format a token count into a compact human-readable string. */
function formatTokens(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
if (n >= 10_000) return `${Math.round(n / 1000)}k`;
if (n >= 1_000) return `${(n / 1000).toFixed(1)}k`;
return String(n);
}
/** Format a cost value as a dollar string. */
function formatCost(n: number): string {
return `$${n.toFixed(2)}`;
}
/** Strip common prefixes/suffixes from model IDs for display. */
function shortModel(id: string): string {
return id.replace(/^claude-/, "").replace(/-\d{8}$/, "");
}
/** cmux sidebar integration — pushes agent state to cmux when running inside it. */
export default function cmuxExtension(pi: ExtensionAPI): void {
if (!CMUX_SOCKET) return;
let sessionCost = 0;
let hasUI = false;
function run(...args: string[]): void {
if (!hasUI) return;
pi.exec("cmux", args, { timeout: 2000 }).catch(() => {});
}
function setStatus(key: string, value: string, icon: string, color: string): void {
run("set-status", key, value, "--icon", icon, "--color", color);
}
function clearStatus(key: string): void {
run("clear-status", key);
}
/** Update the token-count status if context usage is available. */
function syncTokenStatus(ctx: ExtensionContext): void {
const usage = ctx.getContextUsage();
if (usage && usage.tokens != null && usage.tokens > 0) {
setStatus("pi_tokens", formatTokens(usage.tokens), "number", BLUE);
}
}
// --- Session lifecycle ---
pi.on("session_start", async (_event, ctx) => {
hasUI = ctx.hasUI;
if (!hasUI) return;
// Reconstruct session cost from existing entries
sessionCost = 0;
for (const entry of ctx.sessionManager.getBranch()) {
if (
entry.type === "message" &&
entry.message.role === "assistant" &&
(entry.message as any).usage?.cost?.total
) {
sessionCost += (entry.message as any).usage.cost.total;
}
}
setStatus("pi_state", "Idle", "checkmark.circle", GREEN);
if (ctx.model?.id) {
setStatus("pi_model", shortModel(ctx.model.id), "brain", PURPLE);
}
const thinking = pi.getThinkingLevel();
if (thinking && thinking !== "off") {
setStatus("pi_thinking", thinking, "sparkles", AMBER);
}
if (sessionCost > 0) {
setStatus("pi_cost", formatCost(sessionCost), "dollarsign.circle", GREEN);
}
syncTokenStatus(ctx);
});
pi.on("session_shutdown", async (_event, ctx) => {
if (!ctx.hasUI) return;
for (const key of STATUS_KEYS) {
clearStatus(key);
}
});
// --- Agent working state ---
pi.on("agent_start", async (_event, ctx) => {
if (!ctx.hasUI) return;
setStatus("pi_state", "Working", "arrow.circlepath", AMBER);
});
pi.on("agent_end", async (_event, ctx) => {
if (!ctx.hasUI) return;
setStatus("pi_state", "Idle", "checkmark.circle", GREEN);
clearStatus("pi_tool");
syncTokenStatus(ctx);
if (sessionCost > 0) {
setStatus("pi_cost", formatCost(sessionCost), "dollarsign.circle", GREEN);
}
// Notify user — empty body triggers blue ring + tab highlight
run("notify", "--title", "Needs attention");
});
// --- Turn tracking (tokens + cost) ---
pi.on("turn_end", async (event, ctx) => {
if (!ctx.hasUI) return;
const msg = event.message;
if (msg?.role === "assistant" && (msg as any).usage?.cost?.total) {
sessionCost += (msg as any).usage.cost.total;
setStatus("pi_cost", formatCost(sessionCost), "dollarsign.circle", GREEN);
}
syncTokenStatus(ctx);
});
// --- Model / thinking changes ---
pi.on("model_select", async (event, ctx) => {
if (!ctx.hasUI) return;
setStatus("pi_model", shortModel(event.model.id), "brain", PURPLE);
const thinking = pi.getThinkingLevel();
setStatus(
"pi_thinking",
thinking === "off" ? "off" : thinking,
"sparkles",
thinking === "off" ? GRAY : AMBER,
);
});
// --- Tool execution tracking ---
pi.on("tool_execution_start", async (event, ctx) => {
if (!ctx.hasUI) return;
setStatus("pi_tool", event.toolName, "wrench", GRAY);
});
pi.on("tool_execution_end", async (_event, ctx) => {
if (!ctx.hasUI) return;
clearStatus("pi_tool");
});
}