offgrid-ai 0.16.3 → 0.17.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.
@@ -1,363 +0,0 @@
1
- // ── Run benchmark via Pi SDK (no subprocess, no NDJSON parsing) ────────────────
2
-
3
- import { readFile } from "node:fs/promises";
4
- import { join, relative, basename } from "node:path";
5
- import { Agent } from "@earendil-works/pi-agent-core";
6
- import { streamSimple } from "@earendil-works/pi-ai/compat";
7
- import { createCodingTools } from "@earendil-works/pi-coding-agent";
8
- import { pc, formatBytes } from "../ui.mjs";
9
- import { piApiModelId, modelReasoning, modelCompat } from "../harness-pi.mjs";
10
-
11
- const C = {
12
- thinking: pc.magenta,
13
- text: pc.green,
14
- tool: pc.yellow,
15
- success: pc.green,
16
- warning: pc.yellow,
17
- error: pc.red,
18
- info: pc.cyan,
19
- dim: pc.dim,
20
- };
21
-
22
- export async function runBenchmarkInPi(profile, runDirectory, { signal } = {}) {
23
- const model = buildModel(profile);
24
- const tools = createCodingTools(runDirectory);
25
- const systemPrompt = buildSystemPrompt(runDirectory);
26
- const promptText = await readFile(join(runDirectory, "prompt.md"), "utf8");
27
-
28
- const runResult = {
29
- model: `${profile.providerId}/${piApiModelId(profile)}`,
30
- exitCode: 0,
31
- wallClockMs: null,
32
- agentTurns: 0,
33
- promptTokens: 0,
34
- completionTokens: 0,
35
- totalTokens: 0,
36
- cacheRead: 0,
37
- cacheWrite: 0,
38
- toolCalls: 0,
39
- toolResults: 0,
40
- perTurn: [],
41
- rawResponseLines: [],
42
- error: null,
43
- };
44
-
45
- const runStartMs = Date.now();
46
- let currentTurnStartMs = null;
47
- let lastTurnEndMs = null;
48
- let turnToolCalls = 0;
49
- let responseBuffer = "";
50
- const verbose = Boolean(process.env.OFFGRID_BENCHMARK_VERBOSE);
51
- const toolArgsByCallId = new Map();
52
-
53
- // ── Status line state ────────────────────────────────────────────────────
54
- let statusBytes = 0;
55
- let streamedText = false;
56
- let execTimer = null;
57
- let execStartedAt = null;
58
-
59
- function clearStatusLine() {
60
- if (process.stdout.isTTY) process.stdout.write("\r\x1b[K");
61
- }
62
-
63
- function printStatusLine(text) {
64
- if (process.stdout.isTTY) process.stdout.write(`\r\x1b[K${text}`);
65
- }
66
-
67
- function stopExecTimer() {
68
- if (execTimer) { clearInterval(execTimer); execTimer = null; }
69
- clearStatusLine();
70
- }
71
-
72
- function startExecTimer(toolName) {
73
- stopExecTimer();
74
- execStartedAt = Date.now();
75
- if (!process.stdout.isTTY) return;
76
- const update = () => {
77
- const elapsed = Math.floor((Date.now() - execStartedAt) / 1000);
78
- printStatusLine(C.dim(`running ${toolName}… ${elapsed}s`));
79
- };
80
- update();
81
- execTimer = setInterval(update, 1000);
82
- }
83
-
84
- const agent = new Agent({
85
- initialState: {
86
- systemPrompt,
87
- model,
88
- thinkingLevel: profile.reasoning ? "low" : "off",
89
- tools,
90
- },
91
- streamFn: async (mdl, ctx, opts) =>
92
- streamSimple(mdl, ctx, { ...opts, apiKey: "none", timeoutMs: 2147483647 }),
93
- });
94
-
95
- // ── Event handler: render + collect metrics ──────────────────────────────
96
-
97
- agent.subscribe((event) => {
98
- try {
99
- handleEvent(event);
100
- } catch (err) {
101
- console.error(C.error(`[renderer error] ${err.message}`));
102
- }
103
- });
104
-
105
- function handleEvent(event) {
106
- switch (event.type) {
107
- case "turn_start": {
108
- stopExecTimer();
109
- runResult.agentTurns += 1;
110
- currentTurnStartMs = lastTurnEndMs ?? runStartMs;
111
- turnToolCalls = 0;
112
- console.log("");
113
- console.log(C.info(`Turn ${runResult.agentTurns}`));
114
- break;
115
- }
116
-
117
- case "message_update": {
118
- const evt = event.assistantMessageEvent;
119
- if (!evt) break;
120
- const sub = String(evt.type ?? "").replace(/_/gu, "");
121
- if (sub === "thinkingstart") {
122
- statusBytes = 0;
123
- } else if (sub === "thinkingdelta") {
124
- statusBytes += Buffer.byteLength(evt.delta || "", "utf8");
125
- const tokens = Math.max(1, Math.ceil(statusBytes / 4));
126
- printStatusLine(C.dim(`thinking… ${formatBytes(statusBytes)} (~${formatTokens(tokens)} tokens)`));
127
- if (verbose) process.stdout.write(C.thinking(evt.delta || ""));
128
- } else if (sub === "textstart") {
129
- clearStatusLine();
130
- statusBytes = 0;
131
- } else if (sub === "textdelta") {
132
- process.stdout.write(evt.delta || "");
133
- responseBuffer += evt.delta || "";
134
- streamedText = true;
135
- } else if (sub === "toolcallstart") {
136
- clearStatusLine();
137
- }
138
- break;
139
- }
140
-
141
- case "message_end": {
142
- if (streamedText) {
143
- console.log("");
144
- streamedText = false;
145
- }
146
- if (event.message?.role === "assistant") {
147
- for (const item of event.message.content ?? []) {
148
- if (item.type === "toolCall") {
149
- runResult.toolCalls += 1;
150
- turnToolCalls += 1;
151
- responseBuffer += `\n[toolCall] ${item.name}\n`;
152
- }
153
- }
154
- if (responseBuffer) {
155
- runResult.rawResponseLines.push(responseBuffer);
156
- responseBuffer = "";
157
- }
158
- }
159
- break;
160
- }
161
-
162
- case "tool_execution_start": {
163
- clearStatusLine();
164
- toolArgsByCallId.set(event.toolCallId, event.args);
165
- console.log(C.tool(formatToolStart(event.toolName, event.args, runDirectory)));
166
- startExecTimer(event.toolName);
167
- break;
168
- }
169
-
170
- case "tool_execution_end": {
171
- stopExecTimer();
172
- const { toolName, result, isError, toolCallId } = event;
173
- const args = toolArgsByCallId.get(toolCallId) ?? {};
174
- const marker = isError ? C.error("✗") : C.success("✓");
175
- console.log(`${marker} ${toolSummary(toolName, result, isError, args, runDirectory)}`);
176
- runResult.toolResults += 1;
177
- break;
178
- }
179
-
180
- case "turn_end": {
181
- stopExecTimer();
182
- clearStatusLine();
183
- const msg = event.message;
184
- const isFailure = msg?.role === "assistant" && (msg.stopReason === "error" || msg.stopReason === "aborted");
185
- const usage = !isFailure ? msg?.usage : null;
186
- if (usage) {
187
- runResult.promptTokens += usage.input ?? 0;
188
- runResult.completionTokens += usage.output ?? 0;
189
- runResult.cacheRead += usage.cacheRead ?? 0;
190
- runResult.cacheWrite += usage.cacheWrite ?? 0;
191
- }
192
- const turnEndMs = Date.now();
193
- const wallClockMs = currentTurnStartMs ? turnEndMs - currentTurnStartMs : null;
194
- runResult.perTurn.push({
195
- turn: runResult.agentTurns,
196
- inputTokens: usage?.input ?? 0,
197
- outputTokens: usage?.output ?? 0,
198
- cacheRead: usage?.cacheRead ?? 0,
199
- cacheWrite: usage?.cacheWrite ?? 0,
200
- wallClockMs,
201
- toolCalls: turnToolCalls,
202
- });
203
- lastTurnEndMs = turnEndMs;
204
- const tokStr = usage ? ` · ${formatTokens(usage.output ?? 0)} tokens` : "";
205
- console.log(C.success(`✓ turn ${runResult.agentTurns}${tokStr}`));
206
- break;
207
- }
208
-
209
- case "agent_end": {
210
- if (responseBuffer) {
211
- runResult.rawResponseLines.push(responseBuffer);
212
- responseBuffer = "";
213
- }
214
- break;
215
- }
216
- }
217
- }
218
-
219
- // ── Wire abort signal ────────────────────────────────────────────────────
220
-
221
- let cancelled = false;
222
- const abortListener = () => {
223
- cancelled = true;
224
- agent.abort();
225
- };
226
- if (signal) signal.addEventListener("abort", abortListener, { once: true });
227
-
228
- // ── Run ───────────────────────────────────────────────────────────────────
229
-
230
- try {
231
- console.log(C.info("Pi benchmark started"));
232
- console.log(C.dim(` Model ${model.provider}/${model.id}`));
233
- await agent.prompt(promptText);
234
- } catch (err) {
235
- if (!cancelled) {
236
- runResult.error = { message: err.message };
237
- }
238
- } finally {
239
- if (signal) signal.removeEventListener("abort", abortListener);
240
- }
241
-
242
- if (cancelled) {
243
- runResult.error = { message: "Cancelled by user" };
244
- }
245
-
246
- if (!runResult.error && agent.state.errorMessage) {
247
- runResult.error = { message: agent.state.errorMessage };
248
- }
249
-
250
- runResult.wallClockMs = Date.now() - runStartMs;
251
- runResult.totalTokens = runResult.promptTokens + runResult.completionTokens;
252
-
253
- console.log(C.info("Pi benchmark finished"));
254
- return runResult;
255
- }
256
-
257
- // ── Model construction ──────────────────────────────────────────────────────
258
-
259
- function buildModel(profile) {
260
- const reasoning = modelReasoning(profile) ?? false;
261
- const compat = modelCompat(profile);
262
-
263
- return {
264
- id: piApiModelId(profile),
265
- name: profile.label,
266
- api: "openai-completions",
267
- provider: profile.providerId,
268
- baseUrl: profile.baseUrl,
269
- reasoning,
270
- input: profile.mmprojPath || profile.capabilities?.vision ? ["text", "image"] : ["text"],
271
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
272
- contextWindow: profile.flags?.ctxSize ?? 32768,
273
- maxTokens: 16384,
274
- compat: {
275
- supportsDeveloperRole: false,
276
- supportsReasoningEffort: false,
277
- maxTokensField: "max_tokens",
278
- ...(compat ?? {}),
279
- },
280
- };
281
- }
282
-
283
- // ── System prompt ───────────────────────────────────────────────────────────
284
-
285
- function buildSystemPrompt(cwd) {
286
- const now = new Date();
287
- const date = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
288
- return `You are an expert coding assistant. You help users by reading files, executing commands, editing code, and writing new files.
289
-
290
- Available tools:
291
- - read: Read file contents (supports text and images)
292
- - bash: Execute shell commands
293
- - edit: Apply targeted text replacements to files
294
- - write: Write content to files (creates or overwrites)
295
-
296
- Guidelines:
297
- - Be concise in your responses
298
- - Show file paths clearly when working with files
299
- - Use the write tool to create files — do not return file contents as chat text
300
- - Use bash to run commands and verify your work
301
-
302
- Current date: ${date}
303
- Current working directory: ${cwd.replace(/\\/gu, "/")}`;
304
- }
305
-
306
- // ── Rendering helpers ───────────────────────────────────────────────────────
307
-
308
- function formatToolStart(toolName, args, cwd) {
309
- if (toolName === "read") return `→ read ${relPath(args.path, cwd)}`;
310
- if (toolName === "write") {
311
- const size = args.content ? ` · ${formatBytes(Buffer.byteLength(String(args.content), "utf8"))}` : "";
312
- return `→ write ${relPath(args.path, cwd)}${size}`;
313
- }
314
- if (toolName === "edit") {
315
- const count = Array.isArray(args.edits) ? args.edits.length : 0;
316
- return `→ edit ${relPath(args.path, cwd)}${count > 0 ? ` · ${count} replacement${count === 1 ? "" : "s"}` : ""}`;
317
- }
318
- if (toolName === "bash") return `→ run ${truncateOneLine(args.command ?? "")}`;
319
- return `→ ${toolName}`;
320
- }
321
-
322
- function toolSummary(toolName, result, isError, args, cwd) {
323
- const text = toolResultText(result);
324
- if (isError) return `${toolName} failed · ${firstLine(text)}`;
325
- if (toolName === "write") {
326
- const m = String(text).match(/Successfully wrote\s+([0-9,]+)\s+bytes/iu);
327
- const size = m ? ` · ${formatBytes(Number(m[1].replace(/,/gu, "")))}` : "";
328
- return `wrote ${relPath(args.path, cwd)}${size}`;
329
- }
330
- if (toolName === "read") return `read ${relPath(args.path, cwd)}${text ? ` · ${formatBytes(Buffer.byteLength(text, "utf8"))}` : ""}`;
331
- if (toolName === "edit") return `edited ${relPath(args.path, cwd)}`;
332
- if (toolName === "bash") return firstLine(text) || "command completed";
333
- return `${toolName}${text ? ` · ${firstLine(text)}` : ""}`;
334
- }
335
-
336
- function toolResultText(result) {
337
- const content = result?.content;
338
- if (typeof content === "string") return content;
339
- if (!Array.isArray(content)) return "";
340
- return content.map((c) => c?.text ?? "").filter(Boolean).join("\n");
341
- }
342
-
343
- function firstLine(text) {
344
- return String(text ?? "").split(/\r?\n/u).map((s) => s.trim()).find(Boolean) ?? "no details";
345
- }
346
-
347
- function relPath(path, cwd) {
348
- if (!path) return "unknown";
349
- const r = relative(cwd, String(path));
350
- if (r && !r.startsWith("..") && r !== ".") return r;
351
- return basename(String(path)) || String(path);
352
- }
353
-
354
- function truncateOneLine(value, max = 80) {
355
- const text = String(value ?? "").replace(/\s+/gu, " ").trim();
356
- return text.length > max ? `${text.slice(0, Math.max(1, max - 1))}…` : text;
357
- }
358
-
359
- function formatTokens(n) {
360
- if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
361
- if (n >= 1_000) return `${Math.round(n / 1_000)}k`;
362
- return String(Math.round(n));
363
- }
@@ -1,46 +0,0 @@
1
- // ── Shared utilities (matches local-llm-visual-benchmark) ──────────────────
2
-
3
- import { createHash } from "node:crypto";
4
- import { readdir, readFile } from "node:fs/promises";
5
- import { join } from "node:path";
6
-
7
- export function slugModelId(modelId, maxLength = 80) {
8
- const hash = createHash("sha256").update(modelId).digest("hex").slice(0, 10);
9
- const normalized = modelId.normalize("NFKD").replace(/[\u0300-\u036f]/gu, "").toLowerCase();
10
- const slug = normalized.replace(/[^a-z0-9]+/gu, "-").replace(/^-+|-+$/gu, "").replace(/-{2,}/gu, "-");
11
- if (slug.length > 0 && slug.length <= maxLength && slug === normalized) return slug;
12
- const baseMaxLength = Math.max(1, maxLength - 11);
13
- const base = slug.slice(0, baseMaxLength).replace(/^-+|-+$/gu, "") || "model";
14
- return `${base}-${hash}`;
15
- }
16
-
17
- export function createRunId(date = new Date()) {
18
- return date.toISOString().replace(/:/gu, "-").replace(/\./gu, "-");
19
- }
20
-
21
- export async function loadBenchmarks(benchDir) {
22
- const entries = await readdir(benchDir);
23
- const markdownFiles = entries.filter((f) => f.endsWith(".md")).sort();
24
- const benchmarks = [];
25
- for (const filename of markdownFiles) {
26
- const raw = await readFile(join(benchDir, filename), "utf8");
27
- const match = raw.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
28
- const frontmatter = match ? match[1] : "";
29
- const content = match ? match[2].trim() : raw.trim();
30
- let id = filename.replace(/\.md$/u, "");
31
- let title = id;
32
- let description = "";
33
- for (const line of frontmatter.split("\n")) {
34
- const kv = line.match(/^(\w+):\s*(.+)$/);
35
- if (kv) {
36
- const [, key, val] = kv;
37
- if (key === "id") id = val.trim();
38
- if (key === "title") title = val.trim();
39
- if (key === "description") description = val.trim();
40
- }
41
- }
42
- const kind = id === "ab-test-analysis" ? "data-science" : "visual";
43
- benchmarks.push({ id, title, description, prompt: content, kind });
44
- }
45
- return benchmarks;
46
- }
package/src/benchmark.mjs DELETED
@@ -1,12 +0,0 @@
1
- // ── Benchmark module (thin facade) ──────────────────────────────────────────
2
- // Submodules handle the actual logic. This file re-exports for backward compatibility.
3
-
4
- export { slugModelId, createRunId, loadBenchmarks } from "./benchmark/shared.mjs";
5
- export { findBenchmarkRepo, linkBenchmarkRepo } from "./benchmark/repo.mjs";
6
- export { prepareBenchmarkRun } from "./benchmark/prepare.mjs";
7
- export { runBenchmarkInPi } from "./benchmark/sdk-runner.mjs";
8
- export { queryServerMetrics } from "./benchmark/metrics.mjs";
9
- // unloadModelFromServer now lives in src/process.mjs (managed-server counterpart to stopProfile).
10
- export { unloadModelFromServer } from "./process.mjs";
11
- export { finalizeBenchmarkRun, renderBenchmarkSummary } from "./benchmark/finalize.mjs";
12
- export { benchmarkForProfile, benchmarkFlow } from "./benchmark/flow.mjs";
@@ -1,4 +0,0 @@
1
- export async function benchmarkCommand() {
2
- const { benchmarkFlow } = await import("../benchmark.mjs");
3
- return await benchmarkFlow();
4
- }