turno-cli 0.0.0-stage → 0.1.1
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/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/NOTICE +18 -0
- package/README.md +335 -2
- package/dist/accounts/audit.js +54 -0
- package/dist/accounts/builtin.js +214 -0
- package/dist/accounts/delegate.js +185 -0
- package/dist/accounts/registry.js +190 -0
- package/dist/agents/index.js +101 -0
- package/dist/agents/snippets.js +64 -0
- package/dist/cli/args.js +47 -0
- package/dist/cli/commands/accounts.js +216 -0
- package/dist/cli/commands/auto.js +44 -0
- package/dist/cli/commands/available.js +18 -0
- package/dist/cli/commands/browser.js +175 -0
- package/dist/cli/commands/complete.js +112 -0
- package/dist/cli/commands/completion.js +57 -0
- package/dist/cli/commands/config.js +168 -0
- package/dist/cli/commands/doctor.js +324 -0
- package/dist/cli/commands/exhausted.js +40 -0
- package/dist/cli/commands/handoff.js +118 -0
- package/dist/cli/commands/hook.js +63 -0
- package/dist/cli/commands/identity.js +114 -0
- package/dist/cli/commands/limit.js +102 -0
- package/dist/cli/commands/mcp.js +350 -0
- package/dist/cli/commands/mcpd.js +161 -0
- package/dist/cli/commands/migrate.js +176 -0
- package/dist/cli/commands/open.js +60 -0
- package/dist/cli/commands/profile.js +259 -0
- package/dist/cli/commands/resolve.js +23 -0
- package/dist/cli/commands/run.js +15 -0
- package/dist/cli/commands/secret.js +123 -0
- package/dist/cli/commands/session.js +36 -0
- package/dist/cli/commands/sessions.js +27 -0
- package/dist/cli/commands/setup.js +224 -0
- package/dist/cli/commands/status.js +101 -0
- package/dist/cli/commands/statusline.js +275 -0
- package/dist/cli/commands/ui.js +276 -0
- package/dist/cli/commands/undo.js +67 -0
- package/dist/cli/commands/update-check.js +22 -0
- package/dist/cli/commands/upgrade.js +162 -0
- package/dist/cli/commands/usage.js +167 -0
- package/dist/cli/commands/version.js +27 -0
- package/dist/cli/commands/which.js +28 -0
- package/dist/cli/group.js +50 -0
- package/dist/cli/io.js +22 -0
- package/dist/cli/launch.js +98 -0
- package/dist/cli/launchers.js +200 -0
- package/dist/cli/main.js +94 -0
- package/dist/cli/registry.js +77 -0
- package/dist/core/config-edit.js +42 -0
- package/dist/core/config.js +137 -0
- package/dist/core/exhausted.js +30 -0
- package/dist/core/journal.js +185 -0
- package/dist/core/manifest.js +177 -0
- package/dist/core/paths.js +54 -0
- package/dist/core/profiles.js +61 -0
- package/dist/core/ratelimit.js +39 -0
- package/dist/core/resolve.js +78 -0
- package/dist/core/secrets.js +44 -0
- package/dist/core/sessions.js +173 -0
- package/dist/core/toml-edit.js +152 -0
- package/dist/core/update.js +102 -0
- package/dist/core/version.js +109 -0
- package/dist/mcpd/cache.js +41 -0
- package/dist/mcpd/connectors.js +63 -0
- package/dist/mcpd/daemon.js +163 -0
- package/dist/mcpd/gateway.js +308 -0
- package/dist/mcpd/importer.js +160 -0
- package/dist/mcpd/instances.js +303 -0
- package/dist/mcpd/jsonrpc.js +19 -0
- package/dist/mcpd/launchd.js +86 -0
- package/dist/mcpd/peerpid.js +25 -0
- package/dist/mcpd/sync.js +208 -0
- package/dist/mcpd/upstream.js +232 -0
- package/dist/statusline/cost.js +135 -0
- package/dist/statusline/render.js +246 -0
- package/dist/ui/cmap.js +57 -0
- package/dist/ui/fonts.js +107 -0
- package/dist/ui/settings.js +69 -0
- package/dist/ui/terminal-id.js +30 -0
- package/dist/ui/terminals.js +163 -0
- package/package.json +69 -3
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { LATEST_PROTOCOL_VERSION } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import { ErrorCode, isNotification, isRequest, isResponse, rpcError } from "./jsonrpc.js";
|
|
3
|
+
/**
|
|
4
|
+
* One running MCP server, spoken to over a raw transport. Request ids and progress tokens
|
|
5
|
+
* are rewritten so several clients can share it without their messages crossing.
|
|
6
|
+
*/
|
|
7
|
+
export class Upstream {
|
|
8
|
+
opts;
|
|
9
|
+
transport;
|
|
10
|
+
nextId = 1;
|
|
11
|
+
pending = new Map();
|
|
12
|
+
progress = new Map();
|
|
13
|
+
closed = false;
|
|
14
|
+
init;
|
|
15
|
+
lastActivity = Date.now();
|
|
16
|
+
startedAt = Date.now();
|
|
17
|
+
constructor(opts) {
|
|
18
|
+
this.opts = opts;
|
|
19
|
+
}
|
|
20
|
+
get label() {
|
|
21
|
+
return this.opts.label;
|
|
22
|
+
}
|
|
23
|
+
get pid() {
|
|
24
|
+
return this.opts.pid?.() ?? undefined;
|
|
25
|
+
}
|
|
26
|
+
get inflight() {
|
|
27
|
+
return this.pending.size;
|
|
28
|
+
}
|
|
29
|
+
get isClosed() {
|
|
30
|
+
return this.closed;
|
|
31
|
+
}
|
|
32
|
+
async start() {
|
|
33
|
+
const t = await this.opts.connect();
|
|
34
|
+
this.transport = t;
|
|
35
|
+
t.onmessage = (m) => this.onMessage(m);
|
|
36
|
+
t.onclose = () => this.handleClose("transport closed");
|
|
37
|
+
t.onerror = () => {
|
|
38
|
+
/* errors surface as failed requests or close */
|
|
39
|
+
};
|
|
40
|
+
await t.start();
|
|
41
|
+
const res = await this.call("initialize", {
|
|
42
|
+
protocolVersion: LATEST_PROTOCOL_VERSION,
|
|
43
|
+
capabilities: { roots: { listChanged: true }, elicitation: { form: {}, url: {} } },
|
|
44
|
+
clientInfo: { name: "turno-mcpd", version: "0.1.0" },
|
|
45
|
+
}, undefined, undefined, this.opts.initTimeoutMs ?? 60_000);
|
|
46
|
+
if ("error" in res) {
|
|
47
|
+
await this.close();
|
|
48
|
+
throw new Error(`${this.label}: initialize failed: ${res.error.message}`);
|
|
49
|
+
}
|
|
50
|
+
const r = res.result;
|
|
51
|
+
this.init = {
|
|
52
|
+
protocolVersion: String(r.protocolVersion ?? LATEST_PROTOCOL_VERSION),
|
|
53
|
+
serverInfo: r.serverInfo ?? { name: this.label, version: "0" },
|
|
54
|
+
capabilities: r.capabilities ?? {},
|
|
55
|
+
...(typeof r.instructions === "string" ? { instructions: r.instructions } : {}),
|
|
56
|
+
};
|
|
57
|
+
await this.send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
58
|
+
return this.init;
|
|
59
|
+
}
|
|
60
|
+
/** Send a request upstream on behalf of a client and resolve with its outcome. */
|
|
61
|
+
call(method, params, origin, originId, timeoutMs) {
|
|
62
|
+
if (this.closed)
|
|
63
|
+
return Promise.resolve(rpcError(ErrorCode.Unavailable, `${this.label} is not running`));
|
|
64
|
+
const id = `u${this.nextId++}`;
|
|
65
|
+
let outParams = params;
|
|
66
|
+
const meta = params?._meta;
|
|
67
|
+
if (origin && meta && meta.progressToken !== undefined) {
|
|
68
|
+
const token = `p${id}`;
|
|
69
|
+
this.progress.set(token, { origin, token: meta.progressToken, ...(originId !== undefined ? { originId } : {}) });
|
|
70
|
+
outParams = { ...params, _meta: { ...meta, progressToken: token } };
|
|
71
|
+
}
|
|
72
|
+
this.lastActivity = Date.now();
|
|
73
|
+
return new Promise((resolve) => {
|
|
74
|
+
const p = { resolve, ...(origin ? { origin } : {}), ...(originId !== undefined ? { originId } : {}) };
|
|
75
|
+
if (timeoutMs) {
|
|
76
|
+
p.timer = setTimeout(() => {
|
|
77
|
+
this.pending.delete(id);
|
|
78
|
+
resolve(rpcError(ErrorCode.Unavailable, `${this.label}: ${method} timed out`));
|
|
79
|
+
}, timeoutMs);
|
|
80
|
+
}
|
|
81
|
+
this.pending.set(id, p);
|
|
82
|
+
this.send({ jsonrpc: "2.0", id, method, ...(outParams ? { params: outParams } : {}) }).catch((err) => {
|
|
83
|
+
this.settle(id, rpcError(ErrorCode.Unavailable, `${this.label}: ${err.message}`));
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/** Forward a client notification (cancelled, roots/list_changed, ...). */
|
|
88
|
+
notify(method, params, origin) {
|
|
89
|
+
if (this.closed)
|
|
90
|
+
return;
|
|
91
|
+
let out = params;
|
|
92
|
+
if (method === "notifications/cancelled" && params?.requestId !== undefined) {
|
|
93
|
+
const upstreamId = this.findUpstreamId(params.requestId, origin?.id);
|
|
94
|
+
if (!upstreamId)
|
|
95
|
+
return;
|
|
96
|
+
out = { ...params, requestId: upstreamId };
|
|
97
|
+
}
|
|
98
|
+
this.lastActivity = Date.now();
|
|
99
|
+
void this.send({ jsonrpc: "2.0", method, ...(out ? { params: out } : {}) }).catch(() => { });
|
|
100
|
+
}
|
|
101
|
+
/** Drop everything a client had in flight (it went away). */
|
|
102
|
+
forgetClient(client) {
|
|
103
|
+
for (const [token, p] of this.progress)
|
|
104
|
+
if (p.origin === client)
|
|
105
|
+
this.progress.delete(token);
|
|
106
|
+
}
|
|
107
|
+
/** Fetch every page of a list method. */
|
|
108
|
+
async listAll(method, key) {
|
|
109
|
+
const items = [];
|
|
110
|
+
let cursor;
|
|
111
|
+
for (let page = 0; page < 100; page++) {
|
|
112
|
+
const res = await this.call(method, cursor ? { cursor } : undefined, undefined, undefined, 60_000);
|
|
113
|
+
if ("error" in res)
|
|
114
|
+
throw new Error(`${this.label}: ${method}: ${res.error.message}`);
|
|
115
|
+
items.push(...(res.result[key] ?? []));
|
|
116
|
+
cursor = res.result.nextCursor;
|
|
117
|
+
if (!cursor)
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
return items;
|
|
121
|
+
}
|
|
122
|
+
async close() {
|
|
123
|
+
if (this.closed)
|
|
124
|
+
return;
|
|
125
|
+
this.closed = true;
|
|
126
|
+
for (const [id, p] of this.pending) {
|
|
127
|
+
if (p.timer)
|
|
128
|
+
clearTimeout(p.timer);
|
|
129
|
+
p.resolve(rpcError(ErrorCode.Unavailable, `${this.label} stopped`));
|
|
130
|
+
this.pending.delete(id);
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
await this.transport?.close();
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
/* already gone */
|
|
137
|
+
}
|
|
138
|
+
this.opts.onClose?.("closed");
|
|
139
|
+
}
|
|
140
|
+
findUpstreamId(clientId, originId) {
|
|
141
|
+
for (const [id, p] of this.pending) {
|
|
142
|
+
if (p.originId === clientId && (!originId || p.origin?.id === originId))
|
|
143
|
+
return id;
|
|
144
|
+
}
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
async send(m) {
|
|
148
|
+
if (!this.transport)
|
|
149
|
+
throw new Error("not started");
|
|
150
|
+
await this.transport.send(m);
|
|
151
|
+
}
|
|
152
|
+
settle(id, o) {
|
|
153
|
+
const p = this.pending.get(id);
|
|
154
|
+
if (!p)
|
|
155
|
+
return;
|
|
156
|
+
this.pending.delete(id);
|
|
157
|
+
this.progress.delete(`p${id}`);
|
|
158
|
+
if (p.timer)
|
|
159
|
+
clearTimeout(p.timer);
|
|
160
|
+
p.resolve(o);
|
|
161
|
+
}
|
|
162
|
+
handleClose(reason) {
|
|
163
|
+
if (this.closed)
|
|
164
|
+
return;
|
|
165
|
+
this.closed = true;
|
|
166
|
+
for (const [id, p] of this.pending) {
|
|
167
|
+
if (p.timer)
|
|
168
|
+
clearTimeout(p.timer);
|
|
169
|
+
p.resolve(rpcError(ErrorCode.Unavailable, `${this.label} exited`));
|
|
170
|
+
this.pending.delete(id);
|
|
171
|
+
}
|
|
172
|
+
this.opts.onClose?.(reason);
|
|
173
|
+
}
|
|
174
|
+
/** The client whose request is in flight, most recent first. */
|
|
175
|
+
activeOrigin() {
|
|
176
|
+
const all = [...this.pending.values()].filter((p) => p.origin);
|
|
177
|
+
const last = all[all.length - 1];
|
|
178
|
+
return last?.origin ? { origin: last.origin, ...(last.originId !== undefined ? { originId: last.originId } : {}) } : undefined;
|
|
179
|
+
}
|
|
180
|
+
onMessage(m) {
|
|
181
|
+
this.lastActivity = Date.now();
|
|
182
|
+
if (isResponse(m)) {
|
|
183
|
+
this.settle(m.id, "error" in m && m.error ? { error: m.error } : { result: m.result ?? {} });
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (isRequest(m)) {
|
|
187
|
+
void this.relayRequest(m);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (isNotification(m))
|
|
191
|
+
this.relayNotification(m);
|
|
192
|
+
}
|
|
193
|
+
async relayRequest(m) {
|
|
194
|
+
const id = m.id;
|
|
195
|
+
const method = m.method;
|
|
196
|
+
let outcome;
|
|
197
|
+
if (method === "ping") {
|
|
198
|
+
outcome = { result: {} };
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const target = this.activeOrigin();
|
|
202
|
+
outcome = target
|
|
203
|
+
? await target.origin.request(method, m.params, target.originId)
|
|
204
|
+
: rpcError(ErrorCode.MethodNotFound, `no client session is waiting on ${this.label}`);
|
|
205
|
+
}
|
|
206
|
+
const reply = "error" in outcome ? { jsonrpc: "2.0", id, error: outcome.error } : { jsonrpc: "2.0", id, result: outcome.result };
|
|
207
|
+
await this.send(reply).catch(() => { });
|
|
208
|
+
}
|
|
209
|
+
relayNotification(m) {
|
|
210
|
+
const method = m.method;
|
|
211
|
+
if (method === "notifications/progress") {
|
|
212
|
+
const token = String(m.params?.progressToken);
|
|
213
|
+
const p = this.progress.get(token);
|
|
214
|
+
if (p)
|
|
215
|
+
p.origin.notify(method, { ...m.params, progressToken: p.token }, p.originId);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (method === "notifications/cancelled") {
|
|
219
|
+
// The server gave up on a request it sent us; nothing to undo on our side.
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (method === "notifications/message") {
|
|
223
|
+
const target = this.activeOrigin();
|
|
224
|
+
if (target) {
|
|
225
|
+
target.origin.notify(method, m.params, target.originId);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
this.opts.onBroadcast?.(method, m.params);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=upstream.js.map
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, closeSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
/** $/MTok: input, output, cache write 5 m, cache write 1 h, cache read. Longest prefix wins. */
|
|
4
|
+
export const PRICES = {
|
|
5
|
+
"claude-fable-5-2": [10, 50, 12.5, 20, 0.25],
|
|
6
|
+
"claude-fable-5-1": [10, 50, 12.5, 20, 0.25],
|
|
7
|
+
"claude-mythos-5-1": [10, 50, 12.5, 20, 0.25],
|
|
8
|
+
"claude-fable-5": [10, 50, 12.5, 20, 1.0],
|
|
9
|
+
"claude-opus-5-5": [4, 20, 5, 8, 0.2],
|
|
10
|
+
"claude-opus-5": [5, 25, 6.25, 10, 0.5],
|
|
11
|
+
"claude-opus-4-8": [5, 25, 6.25, 10, 0.5],
|
|
12
|
+
"claude-opus-4-7": [5, 25, 6.25, 10, 0.5],
|
|
13
|
+
"claude-opus-4-6": [5, 25, 6.25, 10, 0.5],
|
|
14
|
+
"claude-sonnet-5-5": [2, 10, 2.5, 4, 0.2],
|
|
15
|
+
"claude-sonnet-5": [2, 10, 2.5, 4, 0.2],
|
|
16
|
+
"claude-sonnet-4-6": [3, 15, 3.75, 6, 0.3],
|
|
17
|
+
"claude-haiku-5-5": [1, 5, 1.25, 2, 0.1],
|
|
18
|
+
"claude-haiku-4-5": [1, 5, 1.25, 2, 0.1],
|
|
19
|
+
};
|
|
20
|
+
const KEYS = Object.keys(PRICES).sort((a, b) => b.length - a.length);
|
|
21
|
+
export function priceFor(model) {
|
|
22
|
+
if (!model)
|
|
23
|
+
return undefined;
|
|
24
|
+
for (const k of KEYS)
|
|
25
|
+
if (model.startsWith(k))
|
|
26
|
+
return PRICES[k];
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
/** API cost in USD of one assistant usage record. */
|
|
30
|
+
export function usageCost(model, u) {
|
|
31
|
+
const p = priceFor(model);
|
|
32
|
+
if (!p)
|
|
33
|
+
return 0;
|
|
34
|
+
const w1h = u.cache_creation?.ephemeral_1h_input_tokens ?? 0;
|
|
35
|
+
const w5m = u.cache_creation?.ephemeral_5m_input_tokens ?? Math.max(0, (u.cache_creation_input_tokens ?? 0) - w1h);
|
|
36
|
+
const usd = ((u.input_tokens ?? 0) * p[0] + (u.output_tokens ?? 0) * p[1] + w5m * p[2] + w1h * p[3] + (u.cache_read_input_tokens ?? 0) * p[4]) / 1e6;
|
|
37
|
+
return usd * (u.speed === "fast" ? 2 : 1);
|
|
38
|
+
}
|
|
39
|
+
function readJson(file) {
|
|
40
|
+
try {
|
|
41
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function writeJson(file, data) {
|
|
48
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
49
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
50
|
+
writeFileSync(tmp, JSON.stringify(data));
|
|
51
|
+
renameSync(tmp, file);
|
|
52
|
+
}
|
|
53
|
+
function real(p) {
|
|
54
|
+
try {
|
|
55
|
+
return realpathSync(p);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return p;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Read only complete new lines of a transcript and add their cost. */
|
|
62
|
+
function scan(path, st) {
|
|
63
|
+
let size;
|
|
64
|
+
try {
|
|
65
|
+
size = statSync(path).size;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (size < st.offset)
|
|
71
|
+
Object.assign(st, { offset: 0, usd: 0, recent: [] });
|
|
72
|
+
if (size === st.offset)
|
|
73
|
+
return;
|
|
74
|
+
const fd = openSync(path, "r");
|
|
75
|
+
try {
|
|
76
|
+
const buf = Buffer.alloc(size - st.offset);
|
|
77
|
+
readSync(fd, buf, 0, buf.length, st.offset);
|
|
78
|
+
let start = 0;
|
|
79
|
+
for (;;) {
|
|
80
|
+
const nl = buf.indexOf(10, start);
|
|
81
|
+
if (nl < 0)
|
|
82
|
+
break; // partial line: read it next time
|
|
83
|
+
const line = buf.subarray(start, nl);
|
|
84
|
+
st.offset += nl + 1 - start;
|
|
85
|
+
start = nl + 1;
|
|
86
|
+
if (line.indexOf('"usage"') < 0)
|
|
87
|
+
continue;
|
|
88
|
+
let o;
|
|
89
|
+
try {
|
|
90
|
+
o = JSON.parse(line.toString("utf8"));
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const m = o.message;
|
|
96
|
+
if (o.type !== "assistant" || !m?.usage)
|
|
97
|
+
continue;
|
|
98
|
+
const key = `${m.id}|${o.requestId}`; // every block of one response repeats its usage
|
|
99
|
+
if (st.recent.includes(key))
|
|
100
|
+
continue;
|
|
101
|
+
st.recent.push(key);
|
|
102
|
+
if (st.recent.length > 64)
|
|
103
|
+
st.recent.splice(0, st.recent.length - 64);
|
|
104
|
+
st.usd += usageCost(m.model, m.usage);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
closeSync(fd);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** Incremental cost of a conversation: the main transcript plus its subagents. */
|
|
112
|
+
export function computeCost(transcript, out, now = Date.now() / 1000) {
|
|
113
|
+
const prev = readJson(out);
|
|
114
|
+
const files = prev?.files ?? {};
|
|
115
|
+
const main = real(transcript);
|
|
116
|
+
const stem = main.endsWith(".jsonl") ? main.slice(0, -6) : main;
|
|
117
|
+
const subDir = join(stem, "subagents");
|
|
118
|
+
const subs = existsSync(subDir)
|
|
119
|
+
? readdirSync(subDir)
|
|
120
|
+
.filter((f) => f.endsWith(".jsonl"))
|
|
121
|
+
.map((f) => real(join(subDir, f)))
|
|
122
|
+
.sort()
|
|
123
|
+
: [];
|
|
124
|
+
for (const p of [main, ...subs]) {
|
|
125
|
+
files[p] ??= { offset: 0, usd: 0, recent: [] };
|
|
126
|
+
scan(p, files[p]);
|
|
127
|
+
}
|
|
128
|
+
const mainUsd = files[main]?.usd ?? 0;
|
|
129
|
+
const total = Object.values(files).reduce((a, f) => a + f.usd, 0);
|
|
130
|
+
const state = { files, main: mainUsd, sub: total - mainUsd, total, updated_at: now };
|
|
131
|
+
writeJson(out, state);
|
|
132
|
+
rmSync(`${out}.lock`, { force: true });
|
|
133
|
+
return state;
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=cost.js.map
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-row status line for Claude Code. Pure rendering: everything it shows comes in through
|
|
3
|
+
* RenderInput, which `collect()` in ../cli/commands/statusline.ts fills from disk and git.
|
|
4
|
+
*
|
|
5
|
+
* 1. account: profile and plan, 5 h and weekly usage of THAT account, context, cache,
|
|
6
|
+
* turns left, and a warning when a fallback or the wrong account is in use
|
|
7
|
+
* 2. session: project and worktree, branch, edits, model and effort, and real cost
|
|
8
|
+
*/
|
|
9
|
+
// 256-color palette: light text for neutral data and one hue per kind of data.
|
|
10
|
+
const TEXT = 252;
|
|
11
|
+
const MUTED = 246;
|
|
12
|
+
const SEP = 240;
|
|
13
|
+
const PROJECT = 75;
|
|
14
|
+
const WORKTREE = 110;
|
|
15
|
+
const BRANCH = 80;
|
|
16
|
+
const MODEL = 183;
|
|
17
|
+
const EFFORT = 213;
|
|
18
|
+
const OK = 114;
|
|
19
|
+
const WARN = 221;
|
|
20
|
+
const BAD = 203;
|
|
21
|
+
const WARM = 215;
|
|
22
|
+
const COLD = 117;
|
|
23
|
+
const COST = 150;
|
|
24
|
+
/** Pill backgrounds for profiles without a configured color, assigned in config order. */
|
|
25
|
+
export const PILL_DEFAULTS = [25, 28, 91, 30, 136, 124];
|
|
26
|
+
export const NAMED_COLORS = {
|
|
27
|
+
blue: 25,
|
|
28
|
+
green: 28,
|
|
29
|
+
violet: 91,
|
|
30
|
+
purple: 91,
|
|
31
|
+
cyan: 30,
|
|
32
|
+
yellow: 136,
|
|
33
|
+
red: 124,
|
|
34
|
+
orange: 166,
|
|
35
|
+
gray: 240,
|
|
36
|
+
// Spanish names accepted for configs migrated from the original tool
|
|
37
|
+
azul: 25,
|
|
38
|
+
verde: 28,
|
|
39
|
+
violeta: 91,
|
|
40
|
+
morado: 91,
|
|
41
|
+
cian: 30,
|
|
42
|
+
amarillo: 136,
|
|
43
|
+
rojo: 124,
|
|
44
|
+
};
|
|
45
|
+
const cp = (n) => String.fromCodePoint(n);
|
|
46
|
+
export const NERD = {
|
|
47
|
+
acct: cp(0xf2bd),
|
|
48
|
+
h5: cp(0xf252),
|
|
49
|
+
d7: cp(0xf073),
|
|
50
|
+
rst: cp(0xf0450),
|
|
51
|
+
batt: cp(0xf242),
|
|
52
|
+
swap: cp(0xf0ec),
|
|
53
|
+
warn: cp(0xf071),
|
|
54
|
+
ban: cp(0xf05e),
|
|
55
|
+
ok: cp(0xf058),
|
|
56
|
+
proj: cp(0xf401),
|
|
57
|
+
wt: cp(0xf402),
|
|
58
|
+
br: cp(0xe725),
|
|
59
|
+
model: cp(0xf2db),
|
|
60
|
+
brain: cp(0xf09d1),
|
|
61
|
+
ctx: cp(0xf0e4),
|
|
62
|
+
fire: cp(0xf0238),
|
|
63
|
+
snow: cp(0xf0717),
|
|
64
|
+
diff: cp(0xf440),
|
|
65
|
+
cost: cp(0xf155),
|
|
66
|
+
left: cp(0xe0b6),
|
|
67
|
+
right: cp(0xe0b4),
|
|
68
|
+
};
|
|
69
|
+
export const UNICODE = {
|
|
70
|
+
acct: "◉",
|
|
71
|
+
h5: "⧗",
|
|
72
|
+
d7: "▦",
|
|
73
|
+
rst: "↺",
|
|
74
|
+
batt: "▮",
|
|
75
|
+
swap: "⇄",
|
|
76
|
+
warn: "⚠",
|
|
77
|
+
ban: "⊘",
|
|
78
|
+
ok: "✓",
|
|
79
|
+
proj: "▣",
|
|
80
|
+
wt: "⑂",
|
|
81
|
+
br: "⎇",
|
|
82
|
+
model: "◈",
|
|
83
|
+
brain: "✦",
|
|
84
|
+
ctx: "◔",
|
|
85
|
+
fire: "♨",
|
|
86
|
+
snow: "❄",
|
|
87
|
+
diff: "±",
|
|
88
|
+
cost: "",
|
|
89
|
+
left: "",
|
|
90
|
+
right: "",
|
|
91
|
+
};
|
|
92
|
+
export function fg(n, text, bold = false) {
|
|
93
|
+
return text ? `\x1b[${bold ? "1;" : ""}38;5;${n}m${text}\x1b[0m` : "";
|
|
94
|
+
}
|
|
95
|
+
export function tokens(n) {
|
|
96
|
+
if (n === undefined || n === null)
|
|
97
|
+
return "";
|
|
98
|
+
if (n >= 1e6)
|
|
99
|
+
return `${(n / 1e6).toFixed(1).replace(/\.0$/, "")}M`;
|
|
100
|
+
return `${Math.round(n / 1e3)}k`;
|
|
101
|
+
}
|
|
102
|
+
export function rel(ts, now) {
|
|
103
|
+
const d = Math.trunc(ts - now);
|
|
104
|
+
if (d <= 0)
|
|
105
|
+
return "";
|
|
106
|
+
const h = Math.floor(d / 3600);
|
|
107
|
+
const m = Math.floor((d % 3600) / 60);
|
|
108
|
+
if (h >= 24)
|
|
109
|
+
return `${Math.floor(h / 24)}d${h % 24}h`;
|
|
110
|
+
return h ? `${h}h${String(m).padStart(2, "0")}m` : `${m}m`;
|
|
111
|
+
}
|
|
112
|
+
function level(pct) {
|
|
113
|
+
return pct < 50 ? OK : pct < 80 ? WARN : BAD;
|
|
114
|
+
}
|
|
115
|
+
function meter(pct, cells) {
|
|
116
|
+
const filled = Math.min(cells, Math.round((pct * cells) / 100));
|
|
117
|
+
return "▰".repeat(filled) + "▱".repeat(cells - filled);
|
|
118
|
+
}
|
|
119
|
+
function money(usd) {
|
|
120
|
+
return usd >= 100 ? `$${Math.round(usd).toLocaleString("en-US")}` : `$${usd.toFixed(2)}`;
|
|
121
|
+
}
|
|
122
|
+
function defaultWhen(ts) {
|
|
123
|
+
const d = new Date(ts * 1000);
|
|
124
|
+
const day = d.toLocaleDateString("en-US", { weekday: "short" });
|
|
125
|
+
return `${day} ${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
|
|
126
|
+
}
|
|
127
|
+
export function render(i) {
|
|
128
|
+
const I = i.nerd ? NERD : UNICODE;
|
|
129
|
+
const gap = i.nerd ? " " : " ";
|
|
130
|
+
const div = fg(SEP, " │ ");
|
|
131
|
+
const ic = (name, color) => (I[name] ? fg(color, `${I[name]}${gap}`) : "");
|
|
132
|
+
const when = i.when ?? defaultWhen;
|
|
133
|
+
const { data, now, compact } = i;
|
|
134
|
+
// Row 1: account
|
|
135
|
+
const row1 = [];
|
|
136
|
+
if (i.account) {
|
|
137
|
+
const label = `${I.acct}${gap}${i.account}${i.plan ? ` · ${i.plan}` : ""}`;
|
|
138
|
+
const body = `\x1b[48;5;${i.pill};1;38;5;255m ${label} \x1b[0m`;
|
|
139
|
+
row1.push(i.nerd ? fg(i.pill, I.left) + body + fg(i.pill, I.right) : body);
|
|
140
|
+
for (const [icon, w] of [
|
|
141
|
+
["h5", i.limits.five_hour],
|
|
142
|
+
["d7", i.limits.seven_day],
|
|
143
|
+
]) {
|
|
144
|
+
if (!w || w.used_percentage === undefined || w.used_percentage === null)
|
|
145
|
+
continue;
|
|
146
|
+
let pct = Math.round(w.used_percentage);
|
|
147
|
+
const reset = w.resets_at;
|
|
148
|
+
if (reset && reset <= now)
|
|
149
|
+
pct = 0; // the window already reset
|
|
150
|
+
let seg = ic(icon, MUTED) + (compact ? "" : `${fg(level(pct), meter(pct, 8))} `) + fg(level(pct), `${pct}%`);
|
|
151
|
+
if (reset && rel(reset, now) && !compact)
|
|
152
|
+
seg += fg(MUTED, ` ${I.rst}${gap}${rel(reset, now)}`);
|
|
153
|
+
row1.push(seg);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const cw = data.context_window ?? {};
|
|
157
|
+
const ctx = Math.round(cw.used_percentage ?? 0);
|
|
158
|
+
const cells = compact ? 5 : 10;
|
|
159
|
+
const filled = Math.min(cells, Math.floor((ctx * cells) / 100));
|
|
160
|
+
let ctxSeg = ic("ctx", level(ctx)) + fg(level(ctx), `[${"#".repeat(filled)}${"-".repeat(cells - filled)}] ${ctx}%`);
|
|
161
|
+
if (cw.total_input_tokens && cw.context_window_size && !compact) {
|
|
162
|
+
ctxSeg += fg(TEXT, ` ${tokens(cw.total_input_tokens)}/${tokens(cw.context_window_size)}`);
|
|
163
|
+
}
|
|
164
|
+
row1.push(ctxSeg);
|
|
165
|
+
const pc = data.prompt_cache ?? {};
|
|
166
|
+
if (pc.caching_observed) {
|
|
167
|
+
if (pc.warm && pc.expires_at && rel(pc.expires_at, now))
|
|
168
|
+
row1.push(ic("fire", WARM) + fg(WARM, rel(pc.expires_at, now)));
|
|
169
|
+
else if (pc.recache_tokens_if_cold)
|
|
170
|
+
row1.push(ic("snow", COLD) + fg(COLD, `+${tokens(pc.recache_tokens_if_cold)}`));
|
|
171
|
+
}
|
|
172
|
+
if (i.account) {
|
|
173
|
+
const t = i.turnsLeft;
|
|
174
|
+
if (t !== undefined && !compact) {
|
|
175
|
+
row1.push(ic("batt", t < 10 ? BAD : OK) + fg(t < 10 ? BAD : OK, `~${Math.min(t, 99)}${t > 99 ? "+" : ""} turns`));
|
|
176
|
+
}
|
|
177
|
+
const { chosen, assigned } = i;
|
|
178
|
+
if (chosen && i.account !== chosen) {
|
|
179
|
+
if (i.exhaustedMine)
|
|
180
|
+
row1.push(ic("warn", BAD) + fg(BAD, `exhausted · project uses ${chosen}`, true));
|
|
181
|
+
else if (chosen === assigned)
|
|
182
|
+
row1.push(ic("ok", OK) + fg(OK, `${chosen} is back`));
|
|
183
|
+
else
|
|
184
|
+
row1.push(ic("warn", WARN) + fg(WARN, `project uses ${chosen}`, true));
|
|
185
|
+
}
|
|
186
|
+
else if (chosen && chosen !== assigned) {
|
|
187
|
+
const text = compact
|
|
188
|
+
? `for ${assigned}`
|
|
189
|
+
: `fallback for ${assigned}${i.exhaustedAssigned ? ` until ${when(i.exhaustedAssigned)}` : ""}`;
|
|
190
|
+
row1.push(ic("swap", WARN) + fg(WARN, text));
|
|
191
|
+
}
|
|
192
|
+
else if (i.exhaustedMine) {
|
|
193
|
+
row1.push(ic("ban", BAD) + fg(BAD, `exhausted until ${when(i.exhaustedMine)}`, true));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Row 2: session
|
|
197
|
+
const ws = data.workspace ?? {};
|
|
198
|
+
const cwd = ws.current_dir ?? data.cwd ?? "";
|
|
199
|
+
const base = (p) => p.replace(/\/+$/, "").split("/").pop() ?? "";
|
|
200
|
+
const project = ws.repo?.name || base(ws.project_dir ?? cwd) || "?";
|
|
201
|
+
let first = ic("proj", PROJECT) + fg(PROJECT, project, true);
|
|
202
|
+
const wt = ws.git_worktree ? base(cwd) : ""; // git's own worktree name can be an internal id
|
|
203
|
+
if (wt && wt !== project && !compact)
|
|
204
|
+
first += ` ${ic("wt", WORKTREE)}${fg(WORKTREE, wt)}`;
|
|
205
|
+
const row2 = [first];
|
|
206
|
+
if (i.branch) {
|
|
207
|
+
const b = compact && i.branch.length > 24 ? `${i.branch.slice(0, 23)}…` : i.branch;
|
|
208
|
+
row2.push(ic("br", BRANCH) + fg(BRANCH, b));
|
|
209
|
+
}
|
|
210
|
+
const c = data.cost ?? {};
|
|
211
|
+
const added = c.total_lines_added ?? 0;
|
|
212
|
+
const removed = c.total_lines_removed ?? 0;
|
|
213
|
+
if ((added || removed) && !compact)
|
|
214
|
+
row2.push(`${ic("diff", MUTED)}${fg(OK, `+${added}`)} ${fg(BAD, `−${removed}`)}`);
|
|
215
|
+
const model = (data.model?.display_name ?? "Claude").replace(/\s*\(.*?\)/g, "");
|
|
216
|
+
const effort = data.effort?.level;
|
|
217
|
+
row2.push(ic("model", MODEL) + fg(MODEL, model) + (effort ? ` ${ic("brain", EFFORT)}${fg(EFFORT, effort)}` : ""));
|
|
218
|
+
if (i.cost) {
|
|
219
|
+
let seg = ic("cost", COST) + fg(COST, money(i.cost.total), true);
|
|
220
|
+
if (i.cost.sub >= 0.01 && !compact)
|
|
221
|
+
seg += fg(MUTED, ` (sub $${Math.round(i.cost.sub).toLocaleString("en-US")})`);
|
|
222
|
+
row2.push(seg);
|
|
223
|
+
}
|
|
224
|
+
else if (c.total_cost_usd !== undefined && c.total_cost_usd !== null) {
|
|
225
|
+
row2.push(ic("cost", COST) + fg(COST, `$${c.total_cost_usd.toFixed(2)}`) + fg(MUTED, "…"));
|
|
226
|
+
}
|
|
227
|
+
return `${row1.join(div)}\n${row2.join(div)}\n`;
|
|
228
|
+
}
|
|
229
|
+
/** Turns left in the 5 h window, from how much the percentage rose over this session's last turns. */
|
|
230
|
+
export function turnsLeft(history) {
|
|
231
|
+
const done = history.length >= 3 ? history.slice(0, -1) : history;
|
|
232
|
+
const deltas = [];
|
|
233
|
+
for (let k = 1; k < done.length; k++) {
|
|
234
|
+
const a = done[k - 1];
|
|
235
|
+
const b = done[k];
|
|
236
|
+
if (a[2] && b[2] && Math.abs(a[2] - b[2]) < 120 && b[1] > a[1])
|
|
237
|
+
deltas.push(b[1] - a[1]);
|
|
238
|
+
}
|
|
239
|
+
if (deltas.length < 2)
|
|
240
|
+
return undefined;
|
|
241
|
+
const last = deltas.slice(-5);
|
|
242
|
+
const avg = last.reduce((s, d) => s + d, 0) / last.length;
|
|
243
|
+
const pct = history[history.length - 1]?.[1] ?? 0;
|
|
244
|
+
return avg > 0.05 ? Math.trunc((100 - pct) / avg) : undefined;
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=render.js.map
|
package/dist/ui/cmap.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The code points a TrueType/OpenType font maps, read from its `cmap` table. Enough of the
|
|
3
|
+
* format to answer "does this font draw these icons": formats 4 (BMP) and 12 (full range).
|
|
4
|
+
*/
|
|
5
|
+
export function codePoints(font) {
|
|
6
|
+
const out = new Set();
|
|
7
|
+
const tables = font.readUInt16BE(4);
|
|
8
|
+
let cmap = -1;
|
|
9
|
+
for (let i = 0; i < tables; i++) {
|
|
10
|
+
const rec = 12 + i * 16;
|
|
11
|
+
if (font.toString("latin1", rec, rec + 4) === "cmap")
|
|
12
|
+
cmap = font.readUInt32BE(rec + 8);
|
|
13
|
+
}
|
|
14
|
+
if (cmap < 0)
|
|
15
|
+
return out;
|
|
16
|
+
const subtables = font.readUInt16BE(cmap + 2);
|
|
17
|
+
for (let i = 0; i < subtables; i++) {
|
|
18
|
+
const rec = cmap + 4 + i * 8;
|
|
19
|
+
const platform = font.readUInt16BE(rec);
|
|
20
|
+
const encoding = font.readUInt16BE(rec + 2);
|
|
21
|
+
const unicode = platform === 0 || (platform === 3 && (encoding === 1 || encoding === 10));
|
|
22
|
+
if (!unicode)
|
|
23
|
+
continue;
|
|
24
|
+
const at = cmap + font.readUInt32BE(rec + 4);
|
|
25
|
+
const format = font.readUInt16BE(at);
|
|
26
|
+
if (format === 12) {
|
|
27
|
+
const groups = font.readUInt32BE(at + 12);
|
|
28
|
+
for (let g = 0; g < groups; g++) {
|
|
29
|
+
const start = font.readUInt32BE(at + 16 + g * 12);
|
|
30
|
+
const end = font.readUInt32BE(at + 20 + g * 12);
|
|
31
|
+
for (let c = start; c <= end; c++)
|
|
32
|
+
out.add(c);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else if (format === 4) {
|
|
36
|
+
const segs = font.readUInt16BE(at + 6) / 2;
|
|
37
|
+
const ends = at + 14;
|
|
38
|
+
const starts = ends + segs * 2 + 2;
|
|
39
|
+
const deltas = starts + segs * 2;
|
|
40
|
+
const offsets = deltas + segs * 2;
|
|
41
|
+
for (let s = 0; s < segs; s++) {
|
|
42
|
+
const end = font.readUInt16BE(ends + s * 2);
|
|
43
|
+
const start = font.readUInt16BE(starts + s * 2);
|
|
44
|
+
const delta = font.readInt16BE(deltas + s * 2);
|
|
45
|
+
const offAt = offsets + s * 2;
|
|
46
|
+
const off = font.readUInt16BE(offAt);
|
|
47
|
+
for (let c = start; c <= end && c !== 0xffff; c++) {
|
|
48
|
+
const glyph = off === 0 ? (c + delta) & 0xffff : font.readUInt16BE(offAt + off + (c - start) * 2);
|
|
49
|
+
if (glyph !== 0)
|
|
50
|
+
out.add(c);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=cmap.js.map
|