@wierdbytes/pi-wierdr 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/README.md +224 -2
- package/agents/agent-types.ts +154 -0
- package/agents/child.ts +224 -0
- package/agents/client.ts +76 -0
- package/agents/format.ts +76 -0
- package/agents/hc.ts +209 -0
- package/agents/index.ts +182 -0
- package/agents/launch.ts +117 -0
- package/agents/manager.ts +545 -0
- package/agents/models.ts +71 -0
- package/agents/prepare.ts +86 -0
- package/agents/render.ts +201 -0
- package/agents/rpc.ts +110 -0
- package/agents/runs.ts +122 -0
- package/agents/settings.ts +52 -0
- package/agents/tools.ts +217 -0
- package/herdr/bash-tool.ts +100 -0
- package/herdr/bash.ts +427 -0
- package/herdr/cache.ts +144 -0
- package/herdr/hc.ts +126 -0
- package/herdr/index.ts +187 -0
- package/herdr/limits-publisher.ts +118 -0
- package/herdr/limits.ts +458 -0
- package/herdr/meta.ts +111 -0
- package/herdr/publisher.ts +99 -0
- package/herdr/settings.ts +25 -0
- package/herdr/subagents.ts +72 -0
- package/index.ts +23 -0
- package/package.json +68 -4
- package/settings.ts +35 -0
package/herdr/limits.ts
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subscription limits (Claude Pro/Max, ChatGPT plans for Codex) as pi reads
|
|
3
|
+
* them from provider responses, folded into one herdr-core document that
|
|
4
|
+
* every herdr client can show: the herdr app's mods, Wierdr, anything that
|
|
5
|
+
* talks to the core (`doc.get {name: "limits"}`, `doc` events).
|
|
6
|
+
*
|
|
7
|
+
* Providers report the current state of their usage windows on every
|
|
8
|
+
* response: Anthropic in `anthropic-ratelimit-unified-*` headers, Codex in
|
|
9
|
+
* `x-codex-*` headers (SSE) or a `codex.rate_limits` stream event
|
|
10
|
+
* (WebSocket). Parsing follows pi-langfuse's rate-limit scores, with the same
|
|
11
|
+
* account slugs, so both sources name an account alike.
|
|
12
|
+
*
|
|
13
|
+
* Pure functions, except the Codex account lookup (pi's auth.json). The
|
|
14
|
+
* writing lives in `limits-publisher.ts`.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { readFileSync, statSync } from "node:fs";
|
|
19
|
+
import { homedir } from "node:os";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
|
|
22
|
+
/** The herdr-core document the limits live in. */
|
|
23
|
+
export const LIMITS_DOC = "limits";
|
|
24
|
+
export const LIMITS_SCHEMA = 1;
|
|
25
|
+
|
|
26
|
+
export type LimitStatus = "ok" | "warning" | "exhausted";
|
|
27
|
+
|
|
28
|
+
export interface ReadingWindow {
|
|
29
|
+
/** Stable within the account: "5h", "7d", "7d_oi", or "<family>.<window>" for extra Codex limit families. */
|
|
30
|
+
id: string;
|
|
31
|
+
label: string;
|
|
32
|
+
windowSeconds?: number;
|
|
33
|
+
/** Share of the window used, in percent. Can exceed 100. */
|
|
34
|
+
usedPercent: number;
|
|
35
|
+
/** Unix seconds. */
|
|
36
|
+
resetsAt?: number;
|
|
37
|
+
status?: LimitStatus;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface LimitReading {
|
|
41
|
+
provider: "claude" | "codex";
|
|
42
|
+
source: "header" | "stream";
|
|
43
|
+
/** The provider's own account id (Anthropic organization, ChatGPT account), when the response names it. */
|
|
44
|
+
accountId?: string;
|
|
45
|
+
plan?: string;
|
|
46
|
+
/** The window that limits the account right now (Anthropic's representative claim). */
|
|
47
|
+
binding?: string;
|
|
48
|
+
status?: LimitStatus;
|
|
49
|
+
windows: ReadingWindow[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ============================================
|
|
53
|
+
// Parsing
|
|
54
|
+
// ============================================
|
|
55
|
+
|
|
56
|
+
const num = (value: unknown): number | undefined => {
|
|
57
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : undefined;
|
|
58
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
59
|
+
const n = Number(value);
|
|
60
|
+
return Number.isFinite(n) ? n : undefined;
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const round2 = (n: number) => Math.round(n * 100) / 100;
|
|
66
|
+
|
|
67
|
+
const unifiedStatus = (value: unknown): LimitStatus | undefined =>
|
|
68
|
+
value === "allowed" ? "ok" : value === "allowed_warning" ? "warning" : value === "rejected" ? "exhausted" : undefined;
|
|
69
|
+
|
|
70
|
+
/** "5h" → 18000, "7d" → 604800, "7d_oi" → 604800. */
|
|
71
|
+
function claimSeconds(claim: string): number | undefined {
|
|
72
|
+
const m = /^(\d+)([mhd])/.exec(claim);
|
|
73
|
+
if (!m) return undefined;
|
|
74
|
+
return Number(m[1]) * (m[2] === "m" ? 60 : m[2] === "h" ? 3_600 : 86_400);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function durationLabel(seconds: number): string {
|
|
78
|
+
if (seconds % 86_400 === 0) return `${seconds / 86_400} day${seconds === 86_400 ? "" : "s"}`;
|
|
79
|
+
if (seconds % 3_600 === 0) return `${seconds / 3_600} hour${seconds === 3_600 ? "" : "s"}`;
|
|
80
|
+
return `${Math.round(seconds / 60)} minutes`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function durationId(seconds: number): string {
|
|
84
|
+
if (seconds % 86_400 === 0) return `${seconds / 86_400}d`;
|
|
85
|
+
if (seconds % 3_600 === 0) return `${seconds / 3_600}h`;
|
|
86
|
+
return `${Math.round(seconds / 60)}m`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const CLAUDE_PREFIX = "anthropic-ratelimit-unified-";
|
|
90
|
+
const CLAUDE_LABELS: Record<string, string> = {
|
|
91
|
+
"5h": "5 hours",
|
|
92
|
+
"7d": "7 days",
|
|
93
|
+
"7d_oi": "7 days + extra",
|
|
94
|
+
"7d_opus": "Opus",
|
|
95
|
+
"7d_sonnet": "Sonnet",
|
|
96
|
+
};
|
|
97
|
+
// representative-claim names the binding window by its long name.
|
|
98
|
+
const CLAUDE_CLAIMS: Record<string, string> = {
|
|
99
|
+
five_hour: "5h",
|
|
100
|
+
seven_day: "7d",
|
|
101
|
+
seven_day_overage_included: "7d_oi",
|
|
102
|
+
seven_day_opus: "7d_opus",
|
|
103
|
+
seven_day_sonnet: "7d_sonnet",
|
|
104
|
+
overage: "overage",
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
function sortWindows(windows: ReadingWindow[]): ReadingWindow[] {
|
|
108
|
+
return windows.sort((a, b) => (a.windowSeconds ?? Infinity) - (b.windowSeconds ?? Infinity) || a.id.localeCompare(b.id));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function claudeFromHeaders(headers: Record<string, string>): LimitReading | null {
|
|
112
|
+
const windows: ReadingWindow[] = [];
|
|
113
|
+
for (const [key, raw] of Object.entries(headers)) {
|
|
114
|
+
if (!key.startsWith(CLAUDE_PREFIX) || !key.endsWith("-utilization")) continue;
|
|
115
|
+
const claim = key.slice(CLAUDE_PREFIX.length, -"-utilization".length);
|
|
116
|
+
// Windows only ("5h", "7d", "7d_oi"…): overage, grace and slow-mode budgets are not usage windows.
|
|
117
|
+
if (!/^\d+[mhd](_[a-z0-9_]+)?$/.test(claim)) continue;
|
|
118
|
+
const utilization = num(raw);
|
|
119
|
+
if (utilization === undefined) continue;
|
|
120
|
+
windows.push({
|
|
121
|
+
id: claim,
|
|
122
|
+
label: CLAUDE_LABELS[claim] ?? claim,
|
|
123
|
+
windowSeconds: claimSeconds(claim),
|
|
124
|
+
usedPercent: round2(utilization * 100),
|
|
125
|
+
resetsAt: num(headers[`${CLAUDE_PREFIX}${claim}-reset`]),
|
|
126
|
+
status: unifiedStatus(headers[`${CLAUDE_PREFIX}${claim}-status`]),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
if (!windows.length) return null;
|
|
130
|
+
const claim = headers[`${CLAUDE_PREFIX}representative-claim`];
|
|
131
|
+
return {
|
|
132
|
+
provider: "claude",
|
|
133
|
+
source: "header",
|
|
134
|
+
accountId: headers["anthropic-organization-id"] || undefined,
|
|
135
|
+
binding: claim ? (CLAUDE_CLAIMS[claim] ?? claim) : undefined,
|
|
136
|
+
status: unifiedStatus(headers[`${CLAUDE_PREFIX}status`]),
|
|
137
|
+
windows: sortWindows(windows),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
interface CodexWindowInput {
|
|
142
|
+
usedPercent?: number;
|
|
143
|
+
windowSeconds?: number;
|
|
144
|
+
resetsAt?: number;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function codexWindow(family: string, familyLabel: string | undefined, w: CodexWindowInput): ReadingWindow | null {
|
|
148
|
+
if (w.usedPercent === undefined) return null;
|
|
149
|
+
const seconds = w.windowSeconds;
|
|
150
|
+
const base = seconds ? durationId(seconds) : "window";
|
|
151
|
+
// The main Codex family keeps the plain ids ("5h", "7d") the other providers use.
|
|
152
|
+
const main = family === "codex";
|
|
153
|
+
const id = main ? base : `${family.replace(/[^a-z0-9_-]+/gi, "_")}.${base}`;
|
|
154
|
+
const label = main
|
|
155
|
+
? seconds
|
|
156
|
+
? durationLabel(seconds)
|
|
157
|
+
: "Window"
|
|
158
|
+
: `${familyLabel || family} ${seconds ? durationId(seconds) : ""}`.trim();
|
|
159
|
+
return {
|
|
160
|
+
id,
|
|
161
|
+
label,
|
|
162
|
+
windowSeconds: seconds,
|
|
163
|
+
usedPercent: round2(w.usedPercent),
|
|
164
|
+
resetsAt: w.resetsAt,
|
|
165
|
+
status: w.usedPercent >= 100 ? "exhausted" : undefined,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function codexFromHeaders(headers: Record<string, string>, nowS: number): LimitReading | null {
|
|
170
|
+
const windows: ReadingWindow[] = [];
|
|
171
|
+
for (const key of Object.keys(headers)) {
|
|
172
|
+
// x-<family>-<primary|secondary>-used-percent; the family is "codex" or another limit
|
|
173
|
+
// ("codex-secondary", "bengalfox"…) that names itself in x-<family>-limit-name.
|
|
174
|
+
const m = /^x-(.+)-(primary|secondary)-used-percent$/.exec(key);
|
|
175
|
+
if (!m) continue;
|
|
176
|
+
const [, family, slot] = m as unknown as [string, string, string];
|
|
177
|
+
const prefix = `x-${family}-${slot}`;
|
|
178
|
+
const minutes = num(headers[`${prefix}-window-minutes`]);
|
|
179
|
+
const after = num(headers[`${prefix}-reset-after-seconds`]);
|
|
180
|
+
const w = codexWindow(family, headers[`x-${family}-limit-name`], {
|
|
181
|
+
usedPercent: num(headers[key]),
|
|
182
|
+
windowSeconds: minutes ? minutes * 60 : undefined,
|
|
183
|
+
resetsAt: num(headers[`${prefix}-reset-at`]) ?? (after !== undefined ? Math.floor(nowS + after) : undefined),
|
|
184
|
+
});
|
|
185
|
+
if (w) windows.push(w);
|
|
186
|
+
}
|
|
187
|
+
if (!windows.length) return null;
|
|
188
|
+
return { provider: "codex", source: "header", windows: sortWindows(windows) };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function codexEventWindow(raw: unknown, nowS: number): CodexWindowInput | null {
|
|
192
|
+
if (!raw || typeof raw !== "object") return null;
|
|
193
|
+
const w = raw as Record<string, unknown>;
|
|
194
|
+
const minutes = num(w.window_minutes);
|
|
195
|
+
const seconds = num(w.limit_window_seconds);
|
|
196
|
+
const after = num(w.reset_after_seconds);
|
|
197
|
+
return {
|
|
198
|
+
usedPercent: num(w.used_percent),
|
|
199
|
+
windowSeconds: minutes ? minutes * 60 : seconds,
|
|
200
|
+
resetsAt: num(w.reset_at) ?? num(w.resets_at) ?? (after !== undefined ? Math.floor(nowS + after) : undefined),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function codexEventFamily(family: string, label: string | undefined, limits: unknown, nowS: number, out: ReadingWindow[]) {
|
|
205
|
+
if (!limits || typeof limits !== "object") return;
|
|
206
|
+
const l = limits as Record<string, unknown>;
|
|
207
|
+
for (const slot of [l.primary ?? l.primary_window, l.secondary ?? l.secondary_window]) {
|
|
208
|
+
const input = codexEventWindow(slot, nowS);
|
|
209
|
+
const w = input && codexWindow(family, label, input);
|
|
210
|
+
if (w) out.push(w);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function codexFromEvent(data: Record<string, unknown>, nowS: number): LimitReading | null {
|
|
215
|
+
const windows: ReadingWindow[] = [];
|
|
216
|
+
const main = data.rate_limits as Record<string, unknown> | null | undefined;
|
|
217
|
+
codexEventFamily("codex", undefined, main, nowS, windows);
|
|
218
|
+
codexEventFamily("code_review", "Code review", data.code_review_rate_limits, nowS, windows);
|
|
219
|
+
const extra = data.additional_rate_limits;
|
|
220
|
+
const items = Array.isArray(extra)
|
|
221
|
+
? extra
|
|
222
|
+
: extra && typeof extra === "object"
|
|
223
|
+
? Object.entries(extra as Record<string, unknown>).map(([k, v]) => ({ limit_id: k, ...(v as object) }))
|
|
224
|
+
: [];
|
|
225
|
+
for (const item of items) {
|
|
226
|
+
if (!item || typeof item !== "object") continue;
|
|
227
|
+
const it = item as Record<string, unknown>;
|
|
228
|
+
const family = String(it.metered_feature ?? it.limit_id ?? it.limit_name ?? "extra");
|
|
229
|
+
const label = typeof it.limit_name === "string" ? it.limit_name : undefined;
|
|
230
|
+
codexEventFamily(family, label, it.rate_limits ?? it.rate_limit ?? it, nowS, windows);
|
|
231
|
+
}
|
|
232
|
+
if (!windows.length) return null;
|
|
233
|
+
const blocked = main && (main.limit_reached === true || main.allowed === false);
|
|
234
|
+
return {
|
|
235
|
+
provider: "codex",
|
|
236
|
+
source: "stream",
|
|
237
|
+
plan: typeof data.plan_type === "string" ? data.plan_type : undefined,
|
|
238
|
+
status: blocked ? "exhausted" : main?.allowed === true ? "ok" : undefined,
|
|
239
|
+
windows: sortWindows(windows),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Limits in a provider response's headers (keys lower-case, as pi hands them over). */
|
|
244
|
+
export function readingFromHeaders(headers: unknown, nowS = Date.now() / 1_000): LimitReading | null {
|
|
245
|
+
if (!headers || typeof headers !== "object") return null;
|
|
246
|
+
const h = headers as Record<string, string>;
|
|
247
|
+
return claudeFromHeaders(h) ?? codexFromHeaders(h, nowS);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Limits in a provider stream event (`provider_stream_event.data`). */
|
|
251
|
+
export function readingFromStreamEvent(data: unknown, nowS = Date.now() / 1_000): LimitReading | null {
|
|
252
|
+
if (!data || typeof data !== "object") return null;
|
|
253
|
+
const d = data as Record<string, unknown>;
|
|
254
|
+
if (d.type === "codex.rate_limits") return codexFromEvent(d, nowS);
|
|
255
|
+
// The WebSocket transport forwards the response headers in this event.
|
|
256
|
+
if (d.type === "codex.response.metadata" && d.headers && typeof d.headers === "object") {
|
|
257
|
+
const headers = Object.fromEntries(
|
|
258
|
+
Object.entries(d.headers as Record<string, unknown>).map(([k, v]) => [k.toLowerCase(), String(v)]),
|
|
259
|
+
);
|
|
260
|
+
const reading = codexFromHeaders(headers, nowS);
|
|
261
|
+
return reading ? { ...reading, source: "stream" } : null;
|
|
262
|
+
}
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Cheap pre-check for the stream-event hook, which fires for every delta. */
|
|
267
|
+
export function isLimitStreamEvent(data: unknown): boolean {
|
|
268
|
+
const type = (data as { type?: unknown } | null | undefined)?.type;
|
|
269
|
+
return type === "codex.rate_limits" || type === "codex.response.metadata";
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ============================================
|
|
273
|
+
// Accounts
|
|
274
|
+
// ============================================
|
|
275
|
+
|
|
276
|
+
export interface LimitAccount {
|
|
277
|
+
/** Short, stable and not identifying: the first 8 hex digits of sha256(account id), or "main". */
|
|
278
|
+
slug: string;
|
|
279
|
+
/** The account's e-mail, when pi knows it (Codex). */
|
|
280
|
+
label?: string;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function accountSlug(accountId: string | undefined): string {
|
|
284
|
+
return accountId ? createHash("sha256").update(accountId).digest("hex").slice(0, 8) : "main";
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function agentDir(): string {
|
|
288
|
+
return process.env.PI_CODING_AGENT_DIR || join(homedir(), ".pi", "agent");
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let codexAccountCache: { mtimeMs: number; checkedAt: number; account?: { id?: string; label?: string } } | undefined;
|
|
292
|
+
|
|
293
|
+
function decodeJwtClaims(token: unknown): Record<string, unknown> | undefined {
|
|
294
|
+
if (typeof token !== "string") return undefined;
|
|
295
|
+
const part = token.split(".")[1];
|
|
296
|
+
if (!part) return undefined;
|
|
297
|
+
try {
|
|
298
|
+
return JSON.parse(Buffer.from(part, "base64url").toString("utf-8"));
|
|
299
|
+
} catch {
|
|
300
|
+
return undefined;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The ChatGPT account pi signs in to Codex with (auth.json), re-read when the file changes. */
|
|
305
|
+
export function codexAccountFromAuth(path = join(agentDir(), "auth.json"), now = Date.now()): { id?: string; label?: string } | undefined {
|
|
306
|
+
if (codexAccountCache && now - codexAccountCache.checkedAt < 60_000) return codexAccountCache.account;
|
|
307
|
+
try {
|
|
308
|
+
const mtimeMs = statSync(path).mtimeMs;
|
|
309
|
+
if (codexAccountCache && codexAccountCache.mtimeMs === mtimeMs) {
|
|
310
|
+
codexAccountCache.checkedAt = now;
|
|
311
|
+
return codexAccountCache.account;
|
|
312
|
+
}
|
|
313
|
+
const credential = (JSON.parse(readFileSync(path, "utf-8")) as Record<string, Record<string, unknown>>)["openai-codex"];
|
|
314
|
+
const claims = decodeJwtClaims(credential?.access);
|
|
315
|
+
const auth = claims?.["https://api.openai.com/auth"] as Record<string, unknown> | undefined;
|
|
316
|
+
const profile = claims?.["https://api.openai.com/profile"] as Record<string, unknown> | undefined;
|
|
317
|
+
const id =
|
|
318
|
+
typeof credential?.accountId === "string"
|
|
319
|
+
? credential.accountId
|
|
320
|
+
: typeof auth?.chatgpt_account_id === "string"
|
|
321
|
+
? auth.chatgpt_account_id
|
|
322
|
+
: undefined;
|
|
323
|
+
const email = typeof profile?.email === "string" ? profile.email : typeof claims?.email === "string" ? claims.email : undefined;
|
|
324
|
+
codexAccountCache = { mtimeMs, checkedAt: now, account: id || email ? { id, label: email } : undefined };
|
|
325
|
+
} catch {
|
|
326
|
+
codexAccountCache = { mtimeMs: -1, checkedAt: now, account: undefined };
|
|
327
|
+
}
|
|
328
|
+
return codexAccountCache.account;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function __resetCodexAccountForTest() {
|
|
332
|
+
codexAccountCache = undefined;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function resolveAccount(reading: LimitReading, codexAccount = codexAccountFromAuth): LimitAccount {
|
|
336
|
+
if (reading.provider === "codex") {
|
|
337
|
+
const account = codexAccount();
|
|
338
|
+
return { slug: accountSlug(reading.accountId ?? account?.id), label: account?.label };
|
|
339
|
+
}
|
|
340
|
+
return { slug: accountSlug(reading.accountId) };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// ============================================
|
|
344
|
+
// The document
|
|
345
|
+
// ============================================
|
|
346
|
+
|
|
347
|
+
/** One usage window as the document keeps it. Times are unix seconds. */
|
|
348
|
+
export interface DocWindow {
|
|
349
|
+
id: string;
|
|
350
|
+
label: string;
|
|
351
|
+
/** The window's length. */
|
|
352
|
+
window_s?: number;
|
|
353
|
+
/** Share used, in percent; can exceed 100. */
|
|
354
|
+
used: number;
|
|
355
|
+
resets_at?: number;
|
|
356
|
+
status?: LimitStatus;
|
|
357
|
+
/** When this window was last read. */
|
|
358
|
+
at: number;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export interface DocAccount {
|
|
362
|
+
provider: string;
|
|
363
|
+
/** `accountSlug`: same as pi-langfuse's `rl.<provider>.<account>.*` scores. */
|
|
364
|
+
account: string;
|
|
365
|
+
label?: string;
|
|
366
|
+
plan?: string;
|
|
367
|
+
status?: LimitStatus;
|
|
368
|
+
/** The window limiting the account right now. */
|
|
369
|
+
binding?: string;
|
|
370
|
+
/** Last reading of any window. */
|
|
371
|
+
at: number;
|
|
372
|
+
/** The machine that read it. */
|
|
373
|
+
host?: string;
|
|
374
|
+
source?: "header" | "stream";
|
|
375
|
+
windows: Record<string, DocWindow>;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* The `limits` document: one entry per account, keyed `<provider>.<account>`.
|
|
380
|
+
* Several pi processes write it; each folds its reading into what is there.
|
|
381
|
+
*/
|
|
382
|
+
export interface LimitsDoc {
|
|
383
|
+
schema: typeof LIMITS_SCHEMA;
|
|
384
|
+
/** Last write, unix seconds. */
|
|
385
|
+
updated_at: number;
|
|
386
|
+
accounts: Record<string, DocAccount>;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export const emptyDoc = (): LimitsDoc => ({ schema: LIMITS_SCHEMA, updated_at: 0, accounts: {} });
|
|
390
|
+
|
|
391
|
+
/** The document as stored, or an empty one when it is missing or not ours to read. */
|
|
392
|
+
export function asLimitsDoc(value: unknown): LimitsDoc {
|
|
393
|
+
const v = value as Partial<LimitsDoc> | null | undefined;
|
|
394
|
+
if (!v || typeof v !== "object" || v.schema !== LIMITS_SCHEMA || !v.accounts || typeof v.accounts !== "object") return emptyDoc();
|
|
395
|
+
return { schema: LIMITS_SCHEMA, updated_at: typeof v.updated_at === "number" ? v.updated_at : 0, accounts: { ...v.accounts } };
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Two readings of one window can disagree on the reset by a little (Codex derives it).
|
|
399
|
+
const SAME_WINDOW_S = 120;
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Folds window reading `b` into window `a`. Readings of parallel pi sessions
|
|
403
|
+
* land out of order, so within one window (same reset, give or take two
|
|
404
|
+
* minutes) the used share only grows; a later window replaces it; a reading of
|
|
405
|
+
* an earlier window is dropped. A reading without a reset (Anthropic: a window
|
|
406
|
+
* that hasn't started) replaces a window only once its reset has passed.
|
|
407
|
+
*/
|
|
408
|
+
export function foldWindow(a: DocWindow | undefined, b: DocWindow): DocWindow {
|
|
409
|
+
if (!a) return b;
|
|
410
|
+
if (a.resets_at !== undefined && b.resets_at !== undefined) {
|
|
411
|
+
if (b.resets_at > a.resets_at + SAME_WINDOW_S) return b;
|
|
412
|
+
if (b.resets_at < a.resets_at - SAME_WINDOW_S) return a;
|
|
413
|
+
return {
|
|
414
|
+
...b,
|
|
415
|
+
used: Math.max(a.used, b.used),
|
|
416
|
+
// The later reset of the two: Codex moves it while a window hasn't started.
|
|
417
|
+
resets_at: Math.max(a.resets_at, b.resets_at),
|
|
418
|
+
at: Math.max(a.at, b.at),
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
if (a.resets_at !== undefined && b.resets_at === undefined && b.at < a.resets_at) return a;
|
|
422
|
+
return b.at >= a.at ? b : a;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** The document key of an account. */
|
|
426
|
+
export const accountKey = (provider: string, slug: string) => `${provider}.${slug}`;
|
|
427
|
+
|
|
428
|
+
/** `doc` with `reading` folded in. Returns a new document; `doc` is left as it was. */
|
|
429
|
+
export function foldReading(doc: LimitsDoc, reading: LimitReading, account: LimitAccount, nowS: number, host?: string): LimitsDoc {
|
|
430
|
+
const key = accountKey(reading.provider, account.slug);
|
|
431
|
+
const prev = doc.accounts[key];
|
|
432
|
+
const windows: Record<string, DocWindow> = { ...(prev?.windows ?? {}) };
|
|
433
|
+
for (const w of reading.windows) {
|
|
434
|
+
const status = w.status ?? (reading.status === "exhausted" && reading.binding === w.id ? "exhausted" : reading.status ? "ok" : undefined);
|
|
435
|
+
const next: DocWindow = { id: w.id, label: w.label, used: w.usedPercent, at: nowS };
|
|
436
|
+
if (w.windowSeconds !== undefined) next.window_s = w.windowSeconds;
|
|
437
|
+
if (w.resetsAt !== undefined) next.resets_at = Math.floor(w.resetsAt);
|
|
438
|
+
if (status) next.status = status;
|
|
439
|
+
windows[w.id] = foldWindow(windows[w.id], next);
|
|
440
|
+
}
|
|
441
|
+
const latest = !prev || nowS >= prev.at;
|
|
442
|
+
const pick = <T>(mine: T | undefined, theirs: T | undefined) => (latest ? (mine ?? theirs) : (theirs ?? mine));
|
|
443
|
+
const next: DocAccount = { provider: reading.provider, account: account.slug, at: Math.max(prev?.at ?? 0, nowS), windows };
|
|
444
|
+
const label = pick(account.label, prev?.label);
|
|
445
|
+
const plan = pick(reading.plan, prev?.plan);
|
|
446
|
+
const status = latest ? reading.status : prev?.status;
|
|
447
|
+
const binding = latest ? reading.binding : prev?.binding;
|
|
448
|
+
const source = latest ? reading.source : prev?.source;
|
|
449
|
+
const at = latest ? host : prev?.host;
|
|
450
|
+
if (label) next.label = label;
|
|
451
|
+
if (plan) next.plan = plan;
|
|
452
|
+
if (status) next.status = status;
|
|
453
|
+
if (binding) next.binding = binding;
|
|
454
|
+
if (source) next.source = source;
|
|
455
|
+
if (at) next.host = at;
|
|
456
|
+
return { schema: LIMITS_SCHEMA, updated_at: Math.max(doc.updated_at, nowS), accounts: { ...doc.accounts, [key]: next } };
|
|
457
|
+
}
|
|
458
|
+
|
package/herdr/meta.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What pi tells herdr about itself, as terminal labels.
|
|
3
|
+
*
|
|
4
|
+
* OSC 7501 only carries a status word, an app name and a message, so the
|
|
5
|
+
* model, the reasoning effort and the context size travel as `agent.*`
|
|
6
|
+
* labels on the terminal instead (`hc label`). Herdr mods read them from
|
|
7
|
+
* `terminals()[i].labels` and re-render whenever they change.
|
|
8
|
+
*
|
|
9
|
+
* Pure functions only: the wiring lives in `index.ts`, the socket in `hc.ts`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Every label this extension owns. Removing all of them clears pi's trace. */
|
|
13
|
+
export const LABEL_KEYS = [
|
|
14
|
+
"agent.app",
|
|
15
|
+
"agent.pid",
|
|
16
|
+
"agent.model",
|
|
17
|
+
"agent.model_id",
|
|
18
|
+
"agent.provider",
|
|
19
|
+
"agent.effort",
|
|
20
|
+
"agent.ctx",
|
|
21
|
+
"agent.ctx_max",
|
|
22
|
+
"agent.subagents",
|
|
23
|
+
"agent.session",
|
|
24
|
+
"agent.cache_until",
|
|
25
|
+
"agent.cache_ttl",
|
|
26
|
+
] as const;
|
|
27
|
+
|
|
28
|
+
export type LabelKey = (typeof LABEL_KEYS)[number];
|
|
29
|
+
export type Labels = Partial<Record<LabelKey, string>>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Pi's own thinking levels; a provider map that only renames between them is
|
|
33
|
+
* not a display name. `max` is left out on purpose, as in the statusline: a
|
|
34
|
+
* model whose top level is `max` shows `max`.
|
|
35
|
+
*/
|
|
36
|
+
const STANDARD_LEVELS = new Set(["off", "minimal", "low", "medium", "high", "xhigh"]);
|
|
37
|
+
|
|
38
|
+
export interface MetaModel {
|
|
39
|
+
id?: string;
|
|
40
|
+
name?: string;
|
|
41
|
+
provider?: string;
|
|
42
|
+
reasoning?: boolean;
|
|
43
|
+
contextWindow?: number;
|
|
44
|
+
thinkingLevelMap?: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface MetaInputs {
|
|
48
|
+
pid: number;
|
|
49
|
+
model: MetaModel | undefined;
|
|
50
|
+
thinkingLevel: string | undefined;
|
|
51
|
+
context: { tokens: number | null; contextWindow: number } | undefined;
|
|
52
|
+
subagents: number;
|
|
53
|
+
/** The session's id; absent for an in-memory session (`--no-session`). */
|
|
54
|
+
session?: string;
|
|
55
|
+
/** The branch's prompt cache (`cache.ts`): its expiry (epoch ms) and lifetime; absent when unknown. */
|
|
56
|
+
cache?: { until: number; ttlMs: number };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The effort to show: pi's level, or the model's own word for it (`max`, a token budget). */
|
|
60
|
+
export function effortLabel(model: MetaModel | undefined, level: string | undefined): string | undefined {
|
|
61
|
+
if (!model?.reasoning || !level || level === "off") return undefined;
|
|
62
|
+
const mapped = model.thinkingLevelMap?.[level];
|
|
63
|
+
if (typeof mapped === "string" && mapped.length > 0 && !STANDARD_LEVELS.has(mapped)) return mapped;
|
|
64
|
+
return level;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The labels for this moment. A key left out means "not known now" and gets removed. */
|
|
68
|
+
export function collectLabels(inputs: MetaInputs): Labels {
|
|
69
|
+
const { model, context } = inputs;
|
|
70
|
+
const labels: Labels = { "agent.app": "pi", "agent.pid": String(inputs.pid) };
|
|
71
|
+
if (model) {
|
|
72
|
+
const name = model.name || model.id;
|
|
73
|
+
if (name) labels["agent.model"] = name;
|
|
74
|
+
if (model.id) labels["agent.model_id"] = model.id;
|
|
75
|
+
if (model.provider) labels["agent.provider"] = model.provider;
|
|
76
|
+
}
|
|
77
|
+
const effort = effortLabel(model, inputs.thinkingLevel);
|
|
78
|
+
if (effort) labels["agent.effort"] = effort;
|
|
79
|
+
if (context?.tokens != null && Number.isFinite(context.tokens)) labels["agent.ctx"] = String(Math.round(context.tokens));
|
|
80
|
+
const max = context?.contextWindow || model?.contextWindow;
|
|
81
|
+
if (max) labels["agent.ctx_max"] = String(Math.round(max));
|
|
82
|
+
if (inputs.subagents > 0) labels["agent.subagents"] = String(inputs.subagents);
|
|
83
|
+
if (inputs.session) labels["agent.session"] = inputs.session;
|
|
84
|
+
const cache = inputs.cache;
|
|
85
|
+
if (cache && Number.isFinite(cache.until) && cache.ttlMs > 0) {
|
|
86
|
+
labels["agent.cache_until"] = String(Math.round(cache.until));
|
|
87
|
+
labels["agent.cache_ttl"] = String(Math.round(cache.ttlMs));
|
|
88
|
+
}
|
|
89
|
+
return labels;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* What to send to go from `prev` (what the terminal has) to `next`: changed
|
|
94
|
+
* keys with their value, vanished keys as `""` (hc's "remove"). Empty when
|
|
95
|
+
* nothing changed, so callers can skip the round trip.
|
|
96
|
+
*/
|
|
97
|
+
export function diffLabels(prev: Labels, next: Labels): Record<string, string> {
|
|
98
|
+
const out: Record<string, string> = {};
|
|
99
|
+
for (const key of LABEL_KEYS) {
|
|
100
|
+
const before = prev[key];
|
|
101
|
+
const after = next[key];
|
|
102
|
+
if (after !== undefined && after !== before) out[key] = after;
|
|
103
|
+
else if (after === undefined && before !== undefined) out[key] = "";
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Every owned key set to `""`: removes them all. */
|
|
109
|
+
export function clearLabels(): Record<string, string> {
|
|
110
|
+
return Object.fromEntries(LABEL_KEYS.map((key) => [key, ""]));
|
|
111
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps the terminal's `agent.*` labels in step with pi, with as few socket
|
|
3
|
+
* round trips as possible: changes inside the debounce window collapse into
|
|
4
|
+
* one request, unchanged values are never sent, and only one request is in
|
|
5
|
+
* flight at a time. A failed write keeps the old picture, so the next
|
|
6
|
+
* publish retries the difference.
|
|
7
|
+
*
|
|
8
|
+
* A new publisher doesn't know what the terminal carries: pi rebuilds its
|
|
9
|
+
* extensions on `/new`, `/resume`, `/fork` and reload, and the old instance
|
|
10
|
+
* leaves its labels in place (no flicker). So the first write sends every
|
|
11
|
+
* owned key, removing whatever the previous session left behind (its cache
|
|
12
|
+
* expiry, its context size) instead of trusting an empty picture.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { clearLabels, diffLabels, type Labels } from "./meta.ts";
|
|
16
|
+
|
|
17
|
+
export type SendLabels = (labels: Record<string, string>) => Promise<boolean>;
|
|
18
|
+
|
|
19
|
+
export class LabelPublisher {
|
|
20
|
+
/** What the terminal is believed to carry; undefined until the first write lands. */
|
|
21
|
+
private applied: Labels | undefined;
|
|
22
|
+
/** What it should carry once the queue drains. */
|
|
23
|
+
private desired: Labels = {};
|
|
24
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
25
|
+
private draining: Promise<void> | undefined;
|
|
26
|
+
/** A flush came in while a write was out: go around once more. */
|
|
27
|
+
private again = false;
|
|
28
|
+
private stopped = false;
|
|
29
|
+
|
|
30
|
+
constructor(
|
|
31
|
+
private readonly send: SendLabels,
|
|
32
|
+
private readonly debounceMs = 150,
|
|
33
|
+
) {}
|
|
34
|
+
|
|
35
|
+
/** Schedule the terminal to carry exactly `labels`. */
|
|
36
|
+
publish(labels: Labels): void {
|
|
37
|
+
if (this.stopped) return;
|
|
38
|
+
this.desired = labels;
|
|
39
|
+
if (this.timer) return;
|
|
40
|
+
this.timer = setTimeout(() => {
|
|
41
|
+
this.timer = undefined;
|
|
42
|
+
void this.flush();
|
|
43
|
+
}, this.debounceMs);
|
|
44
|
+
this.timer.unref?.();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Send the pending difference now. Resolves once the terminal has it (or the write failed). */
|
|
48
|
+
flush(): Promise<void> {
|
|
49
|
+
if (this.timer) {
|
|
50
|
+
clearTimeout(this.timer);
|
|
51
|
+
this.timer = undefined;
|
|
52
|
+
}
|
|
53
|
+
if (this.draining) {
|
|
54
|
+
this.again = true;
|
|
55
|
+
return this.draining;
|
|
56
|
+
}
|
|
57
|
+
this.draining = this.drain().finally(() => {
|
|
58
|
+
this.draining = undefined;
|
|
59
|
+
});
|
|
60
|
+
return this.draining;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async drain(): Promise<void> {
|
|
64
|
+
do {
|
|
65
|
+
this.again = false;
|
|
66
|
+
if (this.stopped) return;
|
|
67
|
+
const target = this.desired;
|
|
68
|
+
const change = this.applied ? diffLabels(this.applied, target) : { ...clearLabels(), ...target };
|
|
69
|
+
if (Object.keys(change).length === 0) continue;
|
|
70
|
+
const ok = await this.send(change).catch(() => false);
|
|
71
|
+
if (!ok) return;
|
|
72
|
+
this.applied = target;
|
|
73
|
+
} while (this.again);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Stop publishing and remove every owned label (pi is quitting). */
|
|
77
|
+
async clear(): Promise<void> {
|
|
78
|
+
this.stopped = true;
|
|
79
|
+
if (this.timer) {
|
|
80
|
+
clearTimeout(this.timer);
|
|
81
|
+
this.timer = undefined;
|
|
82
|
+
}
|
|
83
|
+
if (this.draining) await this.draining;
|
|
84
|
+
await this.send(clearLabels()).catch(() => false);
|
|
85
|
+
this.applied = {};
|
|
86
|
+
this.desired = {};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Drop what is queued without touching the terminal: the session is being
|
|
91
|
+
* replaced and its successor republishes at its own start. Publishing
|
|
92
|
+
* again later works as before.
|
|
93
|
+
*/
|
|
94
|
+
cancel(): void {
|
|
95
|
+
if (this.timer) clearTimeout(this.timer);
|
|
96
|
+
this.timer = undefined;
|
|
97
|
+
this.desired = this.applied ?? this.desired;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The herdr side's key in `~/.pi/agent/@wierdbytes/pi-wierdr/settings.json`
|
|
3
|
+
* (`../settings.ts`). Every key is optional; a missing or broken file means
|
|
4
|
+
* the defaults.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { legacySettingsPath, readJson, settingsPath } from "../settings.ts";
|
|
8
|
+
|
|
9
|
+
export interface HerdrSettings {
|
|
10
|
+
/** Run bash commands (the model's and `!`) in herdr-core terminals under pi's. */
|
|
11
|
+
bash: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const DEFAULT_SETTINGS: HerdrSettings = { bash: true };
|
|
15
|
+
|
|
16
|
+
export function parseSettings(raw: unknown): HerdrSettings {
|
|
17
|
+
const o = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
|
|
18
|
+
return { bash: typeof o.bash === "boolean" ? o.bash : DEFAULT_SETTINGS.bash };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** pi-wierdr's file, or pi-herdr's while that one doesn't exist (the same keys at the top). */
|
|
22
|
+
export function loadSettings(path = settingsPath(), legacy = legacySettingsPath("pi-herdr")): HerdrSettings {
|
|
23
|
+
const raw = readJson(path);
|
|
24
|
+
return parseSettings(raw === undefined ? readJson(legacy) : raw);
|
|
25
|
+
}
|