@blazediff/agent 0.11.2 → 0.13.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/README.md +20 -6
- package/dist/cli.js +597 -322
- package/dist/index.d.mts +16 -2
- package/dist/index.d.ts +16 -2
- package/dist/index.js +356 -93
- package/dist/index.mjs +357 -94
- package/dist/review/client/assets/index-COrZjnbT.js +9 -0
- package/dist/review/client/assets/index-t4NNOCuF.css +2 -0
- package/dist/review/client/index.html +3 -3
- package/package.json +10 -6
- package/skills/blazediff/JUDGING.md +1 -1
- package/skills/blazediff/SKILL.md +1 -1
- package/dist/review/client/assets/index-9wONQNrk.js +0 -8
- package/dist/review/client/assets/index-Bod0vbl9.css +0 -1
- package/dist/review/client/assets/logo-DtDpqlir.png +0 -0
package/dist/index.mjs
CHANGED
|
@@ -4,14 +4,17 @@ import { availableParallelism, cpus } from 'os';
|
|
|
4
4
|
import { existsSync, readFileSync } from 'fs';
|
|
5
5
|
import { pathToFileURL } from 'url';
|
|
6
6
|
import { chromium } from 'playwright';
|
|
7
|
-
import { spawn } from 'child_process';
|
|
7
|
+
import { execFile, spawn } from 'child_process';
|
|
8
8
|
import { createRequire } from 'module';
|
|
9
9
|
import { createHash } from 'crypto';
|
|
10
10
|
import { interpret } from '@blazediff/interpret-native';
|
|
11
11
|
import * as util from 'util';
|
|
12
|
+
import { promisify } from 'util';
|
|
12
13
|
import { Annotation, Command, StateGraph, START, Send, END, interrupt } from '@langchain/langgraph';
|
|
13
14
|
import sharp from 'sharp';
|
|
14
15
|
import pc from 'picocolors';
|
|
16
|
+
import 'net';
|
|
17
|
+
import treeKill from 'tree-kill';
|
|
15
18
|
import { BaseCheckpointSaver, TASKS, maxChannelVersion, getCheckpointId, copyCheckpoint, WRITES_IDX_MAP } from '@langchain/langgraph-checkpoint';
|
|
16
19
|
|
|
17
20
|
// src/browser/capture.ts
|
|
@@ -19,6 +22,7 @@ var DEFAULT_VIEWPORT = { width: 1280, height: 800 };
|
|
|
19
22
|
var DEFAULT_WAIT_FOR = ["networkidle", "fonts"];
|
|
20
23
|
var DEFAULT_FULL_PAGE = true;
|
|
21
24
|
var DEFAULT_THRESHOLD = 0.1;
|
|
25
|
+
var DEFAULT_LOCAL_MODEL = "moondream-2-2b-onnx";
|
|
22
26
|
var MIN_AUTO_CONCURRENCY = 2;
|
|
23
27
|
var MAX_AUTO_CONCURRENCY = 8;
|
|
24
28
|
function cpuCores() {
|
|
@@ -876,9 +880,9 @@ function extractInternalLinks(base, target, hrefs) {
|
|
|
876
880
|
try {
|
|
877
881
|
const u = new URL(href, target);
|
|
878
882
|
if (u.origin !== base.origin) continue;
|
|
879
|
-
const
|
|
880
|
-
if (
|
|
881
|
-
out.push(
|
|
883
|
+
const path23 = u.pathname + u.search;
|
|
884
|
+
if (path23.startsWith("/api/")) continue;
|
|
885
|
+
out.push(path23);
|
|
882
886
|
} catch {
|
|
883
887
|
}
|
|
884
888
|
}
|
|
@@ -894,10 +898,10 @@ async function crawlRoutes(opts) {
|
|
|
894
898
|
const queue = [{ url: "/", depth: 0 }];
|
|
895
899
|
const discovered = [];
|
|
896
900
|
const recorded = /* @__PURE__ */ new Set();
|
|
897
|
-
const record = (
|
|
898
|
-
if (recorded.has(
|
|
899
|
-
recorded.add(
|
|
900
|
-
discovered.push({ url:
|
|
901
|
+
const record = (path23) => {
|
|
902
|
+
if (recorded.has(path23)) return;
|
|
903
|
+
recorded.add(path23);
|
|
904
|
+
discovered.push({ url: path23, source: "crawl" });
|
|
901
905
|
};
|
|
902
906
|
const handle = await acquireStableContext(CRAWL_VIEWPORT);
|
|
903
907
|
const fetchOne = async () => {
|
|
@@ -958,8 +962,8 @@ var DEFAULT_SAMPLING = {
|
|
|
958
962
|
threshold: DEFAULT_SAMPLE_THRESHOLD,
|
|
959
963
|
samples: DEFAULT_SAMPLES_PER_TEMPLATE
|
|
960
964
|
};
|
|
961
|
-
function parentPath(
|
|
962
|
-
const [pathPart] =
|
|
965
|
+
function parentPath(path23) {
|
|
966
|
+
const [pathPart] = path23.split("?", 2);
|
|
963
967
|
const idx = pathPart.lastIndexOf("/");
|
|
964
968
|
return idx <= 0 ? "/" : pathPart.slice(0, idx);
|
|
965
969
|
}
|
|
@@ -1271,7 +1275,7 @@ async function prepareTiles(opts) {
|
|
|
1271
1275
|
};
|
|
1272
1276
|
}
|
|
1273
1277
|
|
|
1274
|
-
// src/judge/
|
|
1278
|
+
// src/judge/agent.ts
|
|
1275
1279
|
async function tryPrepareTiles(input, entryDir) {
|
|
1276
1280
|
if (!input.regions || input.regions.length === 0) {
|
|
1277
1281
|
return null;
|
|
@@ -1292,8 +1296,8 @@ async function tryPrepareTiles(input, entryDir) {
|
|
|
1292
1296
|
return null;
|
|
1293
1297
|
}
|
|
1294
1298
|
}
|
|
1295
|
-
var
|
|
1296
|
-
name: "
|
|
1299
|
+
var agentJudge = {
|
|
1300
|
+
name: "agent",
|
|
1297
1301
|
async judge(input, cwd) {
|
|
1298
1302
|
input.onJudgingStart?.();
|
|
1299
1303
|
const p = paths(cwd);
|
|
@@ -1400,7 +1404,7 @@ function createLoadProgress(label, opts) {
|
|
|
1400
1404
|
clearInterval(heartbeat);
|
|
1401
1405
|
heartbeat = void 0;
|
|
1402
1406
|
};
|
|
1403
|
-
const
|
|
1407
|
+
const start2 = () => {
|
|
1404
1408
|
if (started) return;
|
|
1405
1409
|
started = true;
|
|
1406
1410
|
load.startedAt = Date.now();
|
|
@@ -1430,7 +1434,7 @@ function createLoadProgress(label, opts) {
|
|
|
1430
1434
|
};
|
|
1431
1435
|
const finish = (status) => {
|
|
1432
1436
|
if (finished) return;
|
|
1433
|
-
|
|
1437
|
+
start2();
|
|
1434
1438
|
finished = true;
|
|
1435
1439
|
stopHeartbeat();
|
|
1436
1440
|
if (interactive) {
|
|
@@ -1446,9 +1450,9 @@ function createLoadProgress(label, opts) {
|
|
|
1446
1450
|
);
|
|
1447
1451
|
};
|
|
1448
1452
|
return {
|
|
1449
|
-
start,
|
|
1453
|
+
start: start2,
|
|
1450
1454
|
onProgress(event) {
|
|
1451
|
-
|
|
1455
|
+
start2();
|
|
1452
1456
|
if (!event.file) return;
|
|
1453
1457
|
if (event.status === "done") {
|
|
1454
1458
|
const file = files.get(event.file);
|
|
@@ -1761,71 +1765,6 @@ function describeRegionChange(changeType, baseRead, actualRead) {
|
|
|
1761
1765
|
return `${kind} with no readable text change${updated}`;
|
|
1762
1766
|
}
|
|
1763
1767
|
|
|
1764
|
-
// src/judge/vision.ts
|
|
1765
|
-
var MODEL_ID2 = "Xenova/moondream2";
|
|
1766
|
-
var PATCH_SIZE = 14;
|
|
1767
|
-
var MAX_NEW_TOKENS2 = 64;
|
|
1768
|
-
var ANSWER_MARKER = "Answer:";
|
|
1769
|
-
async function defaultFactory2() {
|
|
1770
|
-
const { mod, tokenizer, processor, model } = await loadTransformersModel(
|
|
1771
|
-
"vision",
|
|
1772
|
-
async (mod2, progress_callback) => {
|
|
1773
|
-
const [tokenizer2, processor2, model2] = await Promise.all([
|
|
1774
|
-
mod2.AutoTokenizer.from_pretrained(MODEL_ID2, { progress_callback }),
|
|
1775
|
-
mod2.AutoProcessor.from_pretrained(MODEL_ID2, { progress_callback }),
|
|
1776
|
-
mod2.Moondream1ForConditionalGeneration.from_pretrained(MODEL_ID2, {
|
|
1777
|
-
dtype: {
|
|
1778
|
-
embed_tokens: "fp32",
|
|
1779
|
-
vision_encoder: "q8",
|
|
1780
|
-
decoder_model_merged: "q4"
|
|
1781
|
-
},
|
|
1782
|
-
device: "cpu",
|
|
1783
|
-
progress_callback
|
|
1784
|
-
})
|
|
1785
|
-
]);
|
|
1786
|
-
return { mod: mod2, tokenizer: tokenizer2, processor: processor2, model: model2 };
|
|
1787
|
-
}
|
|
1788
|
-
);
|
|
1789
|
-
return {
|
|
1790
|
-
async describe(imagePath, question) {
|
|
1791
|
-
const image = await mod.RawImage.read(imagePath);
|
|
1792
|
-
const vision = await processor(image);
|
|
1793
|
-
const [, , height, width] = vision.pixel_values.dims;
|
|
1794
|
-
const numImageTokens = Math.floor(height / PATCH_SIZE) * Math.floor(width / PATCH_SIZE);
|
|
1795
|
-
const prompt = `${"<image>".repeat(numImageTokens)}
|
|
1796
|
-
|
|
1797
|
-
Question: ${question}
|
|
1798
|
-
|
|
1799
|
-
${ANSWER_MARKER}`;
|
|
1800
|
-
const textInputs = tokenizer(prompt);
|
|
1801
|
-
const output = await model.generate({
|
|
1802
|
-
...textInputs,
|
|
1803
|
-
...vision,
|
|
1804
|
-
do_sample: false,
|
|
1805
|
-
max_new_tokens: MAX_NEW_TOKENS2,
|
|
1806
|
-
// Moondream loops the same sentence on composite tiles without these.
|
|
1807
|
-
repetition_penalty: 1.3,
|
|
1808
|
-
no_repeat_ngram_size: 3
|
|
1809
|
-
});
|
|
1810
|
-
const decoded = tokenizer.batch_decode(output, {
|
|
1811
|
-
skip_special_tokens: true
|
|
1812
|
-
});
|
|
1813
|
-
const raw = Array.isArray(decoded) ? String(decoded[0] ?? "") : String(decoded);
|
|
1814
|
-
const idx = raw.lastIndexOf(ANSWER_MARKER);
|
|
1815
|
-
return (idx >= 0 ? raw.slice(idx + ANSWER_MARKER.length) : raw).trim();
|
|
1816
|
-
}
|
|
1817
|
-
};
|
|
1818
|
-
}
|
|
1819
|
-
function createVisionRunnerHolder(factory = defaultFactory2) {
|
|
1820
|
-
let runnerPromise;
|
|
1821
|
-
return {
|
|
1822
|
-
get() {
|
|
1823
|
-
if (!runnerPromise) runnerPromise = factory();
|
|
1824
|
-
return runnerPromise;
|
|
1825
|
-
}
|
|
1826
|
-
};
|
|
1827
|
-
}
|
|
1828
|
-
|
|
1829
1768
|
// src/judge/local.ts
|
|
1830
1769
|
var READ_PROMPT = [
|
|
1831
1770
|
"Read the text in this image. Reply with only the exact words and numbers you",
|
|
@@ -1920,8 +1859,8 @@ function classifyFailure(err) {
|
|
|
1920
1859
|
return "read";
|
|
1921
1860
|
return "internal";
|
|
1922
1861
|
}
|
|
1923
|
-
function createLocalJudge(deps
|
|
1924
|
-
const vision = deps.vision
|
|
1862
|
+
function createLocalJudge(deps) {
|
|
1863
|
+
const vision = deps.vision;
|
|
1925
1864
|
const classifier = deps.classifier ?? createClassifierRunnerHolder();
|
|
1926
1865
|
const visionSemaphore = createSemaphore(1);
|
|
1927
1866
|
const classifierSemaphore = createSemaphore(1);
|
|
@@ -1955,7 +1894,7 @@ function createLocalJudge(deps = {}) {
|
|
|
1955
1894
|
return (await v.describe(input.actualPath, DESCRIBE_PROMPT)).trim();
|
|
1956
1895
|
}
|
|
1957
1896
|
return {
|
|
1958
|
-
name: "local",
|
|
1897
|
+
name: deps.name ?? "local",
|
|
1959
1898
|
// Stream both models' weights up front (concurrently) so the multi-second
|
|
1960
1899
|
// load is a single visible phase before judging, not a silent stall on the
|
|
1961
1900
|
// first test. Both are cached promises, so the judge reuses them after.
|
|
@@ -2010,7 +1949,329 @@ ${answer}`.trim()
|
|
|
2010
1949
|
}
|
|
2011
1950
|
};
|
|
2012
1951
|
}
|
|
2013
|
-
|
|
1952
|
+
|
|
1953
|
+
// src/judge/vision.ts
|
|
1954
|
+
function createVisionRunnerHolder(factory) {
|
|
1955
|
+
let runnerPromise;
|
|
1956
|
+
return {
|
|
1957
|
+
get() {
|
|
1958
|
+
if (!runnerPromise) runnerPromise = factory();
|
|
1959
|
+
return runnerPromise;
|
|
1960
|
+
}
|
|
1961
|
+
};
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
// src/judge/local-moondream-2-2b-onnx.ts
|
|
1965
|
+
var MODEL_ID2 = "Xenova/moondream2";
|
|
1966
|
+
var PATCH_SIZE = 14;
|
|
1967
|
+
var MAX_NEW_TOKENS2 = 64;
|
|
1968
|
+
var ANSWER_MARKER = "Answer:";
|
|
1969
|
+
async function createOnnxVisionRunner() {
|
|
1970
|
+
const { mod, tokenizer, processor, model } = await loadTransformersModel(
|
|
1971
|
+
"vision",
|
|
1972
|
+
async (mod2, progress_callback) => {
|
|
1973
|
+
const [tokenizer2, processor2, model2] = await Promise.all([
|
|
1974
|
+
mod2.AutoTokenizer.from_pretrained(MODEL_ID2, { progress_callback }),
|
|
1975
|
+
mod2.AutoProcessor.from_pretrained(MODEL_ID2, { progress_callback }),
|
|
1976
|
+
mod2.Moondream1ForConditionalGeneration.from_pretrained(MODEL_ID2, {
|
|
1977
|
+
dtype: {
|
|
1978
|
+
embed_tokens: "fp32",
|
|
1979
|
+
vision_encoder: "q8",
|
|
1980
|
+
decoder_model_merged: "q4"
|
|
1981
|
+
},
|
|
1982
|
+
device: "cpu",
|
|
1983
|
+
progress_callback
|
|
1984
|
+
})
|
|
1985
|
+
]);
|
|
1986
|
+
return { mod: mod2, tokenizer: tokenizer2, processor: processor2, model: model2 };
|
|
1987
|
+
}
|
|
1988
|
+
);
|
|
1989
|
+
return {
|
|
1990
|
+
async describe(imagePath, question) {
|
|
1991
|
+
const image = await mod.RawImage.read(imagePath);
|
|
1992
|
+
const vision = await processor(image);
|
|
1993
|
+
const [, , height, width] = vision.pixel_values.dims;
|
|
1994
|
+
const numImageTokens = Math.floor(height / PATCH_SIZE) * Math.floor(width / PATCH_SIZE);
|
|
1995
|
+
const prompt = `${"<image>".repeat(numImageTokens)}
|
|
1996
|
+
|
|
1997
|
+
Question: ${question}
|
|
1998
|
+
|
|
1999
|
+
${ANSWER_MARKER}`;
|
|
2000
|
+
const textInputs = tokenizer(prompt);
|
|
2001
|
+
const output = await model.generate({
|
|
2002
|
+
...textInputs,
|
|
2003
|
+
...vision,
|
|
2004
|
+
do_sample: false,
|
|
2005
|
+
max_new_tokens: MAX_NEW_TOKENS2,
|
|
2006
|
+
// Moondream loops the same sentence on composite tiles without these.
|
|
2007
|
+
repetition_penalty: 1.3,
|
|
2008
|
+
no_repeat_ngram_size: 3
|
|
2009
|
+
});
|
|
2010
|
+
const decoded = tokenizer.batch_decode(output, {
|
|
2011
|
+
skip_special_tokens: true
|
|
2012
|
+
});
|
|
2013
|
+
const raw = Array.isArray(decoded) ? String(decoded[0] ?? "") : String(decoded);
|
|
2014
|
+
const idx = raw.lastIndexOf(ANSWER_MARKER);
|
|
2015
|
+
return (idx >= 0 ? raw.slice(idx + ANSWER_MARKER.length) : raw).trim();
|
|
2016
|
+
}
|
|
2017
|
+
};
|
|
2018
|
+
}
|
|
2019
|
+
function createOnnxLocalJudge() {
|
|
2020
|
+
return createLocalJudge({
|
|
2021
|
+
vision: createVisionRunnerHolder(createOnnxVisionRunner),
|
|
2022
|
+
name: "local:moondream-2-2b-onnx"
|
|
2023
|
+
});
|
|
2024
|
+
}
|
|
2025
|
+
var onnxLocalJudge = createOnnxLocalJudge();
|
|
2026
|
+
promisify(execFile);
|
|
2027
|
+
async function stopProcess(pid) {
|
|
2028
|
+
if (!pid) return;
|
|
2029
|
+
await new Promise((resolve) => {
|
|
2030
|
+
treeKill(pid, "SIGTERM", (err) => {
|
|
2031
|
+
if (err) {
|
|
2032
|
+
treeKill(pid, "SIGKILL", () => resolve());
|
|
2033
|
+
return;
|
|
2034
|
+
}
|
|
2035
|
+
resolve();
|
|
2036
|
+
});
|
|
2037
|
+
});
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
// src/judge/station.ts
|
|
2041
|
+
var execFileP2 = promisify(execFile);
|
|
2042
|
+
var PORT = 2020;
|
|
2043
|
+
var BASE_URL = `http://127.0.0.1:${PORT}`;
|
|
2044
|
+
var COMMAND = "moondream-station";
|
|
2045
|
+
var MODEL = "moondream-3-preview-mlx-quantized";
|
|
2046
|
+
var MIN_INFERENCE_TIMEOUT_S = 900;
|
|
2047
|
+
var READY_TIMEOUT_MS = 12e4;
|
|
2048
|
+
var SETTINGS_TIMEOUT_MS = 3e4;
|
|
2049
|
+
var MODEL_TIMEOUT_MS = 9e5;
|
|
2050
|
+
var POLL_INTERVAL_MS = 500;
|
|
2051
|
+
var OUTPUT_TAIL_LINES = 15;
|
|
2052
|
+
var WARMUP_IMAGE = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAADElEQVR42mP4//8/AAX+Av4zEpUUAAAAAElFTkSuQmCC";
|
|
2053
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
2054
|
+
var note = (message) => {
|
|
2055
|
+
process.stderr.write(`${dim("[blazediff]")} ${message}
|
|
2056
|
+
`);
|
|
2057
|
+
};
|
|
2058
|
+
async function readStats() {
|
|
2059
|
+
try {
|
|
2060
|
+
const response = await fetch(`${BASE_URL}/v1/stats`, {
|
|
2061
|
+
signal: AbortSignal.timeout(2e3)
|
|
2062
|
+
});
|
|
2063
|
+
return response.ok ? await response.json() : null;
|
|
2064
|
+
} catch {
|
|
2065
|
+
return null;
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
async function waitForStats(predicate, timeoutMs, what, tail) {
|
|
2069
|
+
const deadline = Date.now() + timeoutMs;
|
|
2070
|
+
while (Date.now() < deadline) {
|
|
2071
|
+
const stats = await readStats();
|
|
2072
|
+
if (stats && predicate(stats)) return stats;
|
|
2073
|
+
await sleep(POLL_INTERVAL_MS);
|
|
2074
|
+
}
|
|
2075
|
+
const context = tail.length > 0 ? `
|
|
2076
|
+
${tail.join("\n")}` : "";
|
|
2077
|
+
throw new Error(`moondream station: timed out waiting for ${what}${context}`);
|
|
2078
|
+
}
|
|
2079
|
+
async function commandExists(command) {
|
|
2080
|
+
try {
|
|
2081
|
+
await execFileP2(process.platform === "win32" ? "where" : "which", [
|
|
2082
|
+
command
|
|
2083
|
+
]);
|
|
2084
|
+
return true;
|
|
2085
|
+
} catch {
|
|
2086
|
+
return false;
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
function ensureApplePlatform() {
|
|
2090
|
+
if (process.platform === "darwin" && process.arch === "arm64") return;
|
|
2091
|
+
throw new Error(
|
|
2092
|
+
"the moondream judge needs Apple Silicon (moondream station's Metal build). Use --judge local for the in-process ONNX judge."
|
|
2093
|
+
);
|
|
2094
|
+
}
|
|
2095
|
+
async function ensureInstalled() {
|
|
2096
|
+
if (await commandExists(COMMAND)) return;
|
|
2097
|
+
if (!await commandExists("uv")) {
|
|
2098
|
+
throw new Error(
|
|
2099
|
+
`moondream station is not installed. Install uv (https://docs.astral.sh/uv) and retry, or install it yourself: pipx install ${COMMAND}`
|
|
2100
|
+
);
|
|
2101
|
+
}
|
|
2102
|
+
note(`installing ${bold(COMMAND)} ${dim("(one time)")}`);
|
|
2103
|
+
await execFileP2("uv", ["tool", "install", COMMAND]);
|
|
2104
|
+
}
|
|
2105
|
+
function launch() {
|
|
2106
|
+
const tail = [];
|
|
2107
|
+
const child = spawn(COMMAND, {
|
|
2108
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
2109
|
+
detached: true,
|
|
2110
|
+
env: { ...process.env, NO_COLOR: "1" }
|
|
2111
|
+
});
|
|
2112
|
+
const remember = (chunk) => {
|
|
2113
|
+
for (const line of chunk.toString().split("\n")) {
|
|
2114
|
+
const text = line.trim();
|
|
2115
|
+
if (!text) continue;
|
|
2116
|
+
tail.push(text);
|
|
2117
|
+
if (tail.length > OUTPUT_TAIL_LINES) tail.shift();
|
|
2118
|
+
}
|
|
2119
|
+
};
|
|
2120
|
+
child.stdout?.on("data", remember);
|
|
2121
|
+
child.stderr?.on("data", remember);
|
|
2122
|
+
child.once("error", (error) => tail.push(error.message));
|
|
2123
|
+
child.stdin?.write("\n");
|
|
2124
|
+
return { child, tail };
|
|
2125
|
+
}
|
|
2126
|
+
var send = (child, command) => {
|
|
2127
|
+
child.stdin?.write(`${command}
|
|
2128
|
+
`);
|
|
2129
|
+
};
|
|
2130
|
+
async function ensureTimeout(child, tail) {
|
|
2131
|
+
const stats = await readStats();
|
|
2132
|
+
if ((stats?.default_timeout ?? 0) >= MIN_INFERENCE_TIMEOUT_S) return;
|
|
2133
|
+
send(child, `settings set inference_timeout ${MIN_INFERENCE_TIMEOUT_S}`);
|
|
2134
|
+
await waitForStats(
|
|
2135
|
+
(s) => (s.default_timeout ?? 0) >= MIN_INFERENCE_TIMEOUT_S,
|
|
2136
|
+
SETTINGS_TIMEOUT_MS,
|
|
2137
|
+
"the inference timeout to apply",
|
|
2138
|
+
tail
|
|
2139
|
+
);
|
|
2140
|
+
}
|
|
2141
|
+
async function ensureModel(child, active, tail) {
|
|
2142
|
+
if (active === MODEL) return;
|
|
2143
|
+
note(
|
|
2144
|
+
`switching moondream station to ${bold(MODEL)} ${dim("(first run downloads weights)")}`
|
|
2145
|
+
);
|
|
2146
|
+
send(child, `models switch ${MODEL}`);
|
|
2147
|
+
send(child, "y");
|
|
2148
|
+
await waitForStats(
|
|
2149
|
+
(s) => s.model === MODEL && s.status === "running",
|
|
2150
|
+
MODEL_TIMEOUT_MS,
|
|
2151
|
+
`moondream station to load ${MODEL}`,
|
|
2152
|
+
tail
|
|
2153
|
+
);
|
|
2154
|
+
}
|
|
2155
|
+
async function warmModel(endpoint) {
|
|
2156
|
+
const response = await fetch(`${endpoint}/query`, {
|
|
2157
|
+
method: "POST",
|
|
2158
|
+
headers: { "Content-Type": "application/json" },
|
|
2159
|
+
body: JSON.stringify({
|
|
2160
|
+
image_url: WARMUP_IMAGE,
|
|
2161
|
+
question: "Read the text in this image.",
|
|
2162
|
+
stream: false
|
|
2163
|
+
}),
|
|
2164
|
+
signal: AbortSignal.timeout(MODEL_TIMEOUT_MS)
|
|
2165
|
+
});
|
|
2166
|
+
const payload = await response.json();
|
|
2167
|
+
if (payload.error) throw new Error(`moondream station: ${payload.error}`);
|
|
2168
|
+
}
|
|
2169
|
+
function killOnExit(child) {
|
|
2170
|
+
const kill = () => {
|
|
2171
|
+
if (!child.pid) return;
|
|
2172
|
+
try {
|
|
2173
|
+
process.kill(-child.pid, "SIGKILL");
|
|
2174
|
+
} catch {
|
|
2175
|
+
}
|
|
2176
|
+
};
|
|
2177
|
+
process.once("exit", kill);
|
|
2178
|
+
process.once("SIGINT", () => {
|
|
2179
|
+
kill();
|
|
2180
|
+
process.exit(130);
|
|
2181
|
+
});
|
|
2182
|
+
process.once("SIGTERM", () => {
|
|
2183
|
+
kill();
|
|
2184
|
+
process.exit(143);
|
|
2185
|
+
});
|
|
2186
|
+
}
|
|
2187
|
+
async function start() {
|
|
2188
|
+
const endpoint = `${BASE_URL}/v1`;
|
|
2189
|
+
const running = await readStats();
|
|
2190
|
+
if (running) {
|
|
2191
|
+
await warmModel(endpoint);
|
|
2192
|
+
return {
|
|
2193
|
+
endpoint,
|
|
2194
|
+
model: running.model ?? "unknown",
|
|
2195
|
+
attached: true,
|
|
2196
|
+
stop: async () => {
|
|
2197
|
+
}
|
|
2198
|
+
};
|
|
2199
|
+
}
|
|
2200
|
+
ensureApplePlatform();
|
|
2201
|
+
await ensureInstalled();
|
|
2202
|
+
const { child, tail } = launch();
|
|
2203
|
+
try {
|
|
2204
|
+
const stats = await waitForStats(
|
|
2205
|
+
() => true,
|
|
2206
|
+
READY_TIMEOUT_MS,
|
|
2207
|
+
"moondream station to start",
|
|
2208
|
+
tail
|
|
2209
|
+
);
|
|
2210
|
+
await ensureTimeout(child, tail);
|
|
2211
|
+
await ensureModel(child, stats.model, tail);
|
|
2212
|
+
await warmModel(endpoint);
|
|
2213
|
+
killOnExit(child);
|
|
2214
|
+
note(`moondream station ready ${pc.green("\u2713")} ${dim(endpoint)}`);
|
|
2215
|
+
return {
|
|
2216
|
+
endpoint,
|
|
2217
|
+
model: MODEL,
|
|
2218
|
+
attached: false,
|
|
2219
|
+
stop: async () => {
|
|
2220
|
+
if (child.pid) await stopProcess(child.pid);
|
|
2221
|
+
}
|
|
2222
|
+
};
|
|
2223
|
+
} catch (error) {
|
|
2224
|
+
if (child.pid) await stopProcess(child.pid);
|
|
2225
|
+
throw error;
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
var station;
|
|
2229
|
+
function startStation() {
|
|
2230
|
+
station ?? (station = start());
|
|
2231
|
+
return station;
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
// src/judge/local-moondream-3-9b-mlx.ts
|
|
2235
|
+
var REQUEST_TIMEOUT_MS = 12e4;
|
|
2236
|
+
async function toDataUrl(imagePath) {
|
|
2237
|
+
const bytes = await readFile(imagePath);
|
|
2238
|
+
return `data:image/png;base64,${bytes.toString("base64")}`;
|
|
2239
|
+
}
|
|
2240
|
+
function createStationVisionRunner(endpoint) {
|
|
2241
|
+
const queryUrl = `${endpoint.replace(/\/+$/, "")}/query`;
|
|
2242
|
+
return {
|
|
2243
|
+
async describe(imagePath, question) {
|
|
2244
|
+
const response = await fetch(queryUrl, {
|
|
2245
|
+
method: "POST",
|
|
2246
|
+
headers: { "Content-Type": "application/json" },
|
|
2247
|
+
body: JSON.stringify({
|
|
2248
|
+
image_url: await toDataUrl(imagePath),
|
|
2249
|
+
question,
|
|
2250
|
+
stream: false
|
|
2251
|
+
}),
|
|
2252
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
2253
|
+
});
|
|
2254
|
+
if (!response.ok) {
|
|
2255
|
+
throw new Error(
|
|
2256
|
+
`moondream station returned ${response.status} for ${queryUrl}`
|
|
2257
|
+
);
|
|
2258
|
+
}
|
|
2259
|
+
const payload = await response.json();
|
|
2260
|
+
if (payload.error) {
|
|
2261
|
+
throw new Error(`moondream station: ${payload.error}`);
|
|
2262
|
+
}
|
|
2263
|
+
return (payload.answer ?? "").trim();
|
|
2264
|
+
}
|
|
2265
|
+
};
|
|
2266
|
+
}
|
|
2267
|
+
function createMlxLocalJudge() {
|
|
2268
|
+
const vision = createVisionRunnerHolder(async () => {
|
|
2269
|
+
const station2 = await startStation();
|
|
2270
|
+
return createStationVisionRunner(station2.endpoint);
|
|
2271
|
+
});
|
|
2272
|
+
return createLocalJudge({ vision, name: "local:moondream-3-9b-mlx" });
|
|
2273
|
+
}
|
|
2274
|
+
var mlxLocalJudge = createMlxLocalJudge();
|
|
2014
2275
|
|
|
2015
2276
|
// src/judge/none.ts
|
|
2016
2277
|
var noneJudge = {
|
|
@@ -2232,7 +2493,7 @@ async function applyJudgments(opts = {}) {
|
|
|
2232
2493
|
});
|
|
2233
2494
|
return { report, applied, missing, invalid };
|
|
2234
2495
|
}
|
|
2235
|
-
var
|
|
2496
|
+
var AGENT_INSTRUCTIONS = [
|
|
2236
2497
|
"The visual-regression heuristic could not classify this diff confidently.",
|
|
2237
2498
|
"Read `locator.png` AND `regions.png` in parallel - issue both Read calls in a single tool batch. locator.png is a small thumbnail of the diff with every change region outlined in red; regions.png is a vertical stack of [baseline | actual] pairs, one row per change region at native resolution. Row order matches the `regions[]` array (top = largest by pixelCount).",
|
|
2238
2499
|
"Decide from the tile pairs. Only open the full diff / baseline / actual PNGs if the composite is itself ambiguous (e.g., a change clearly continues outside the cropped region).",
|
|
@@ -2280,7 +2541,7 @@ function buildRequest(result, entry, cwd, tiles) {
|
|
|
2280
2541
|
},
|
|
2281
2542
|
signature: signatureOf(result),
|
|
2282
2543
|
message: result.message,
|
|
2283
|
-
instructions: isAmbiguous ?
|
|
2544
|
+
instructions: isAmbiguous ? AGENT_INSTRUCTIONS : void 0,
|
|
2284
2545
|
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2285
2546
|
};
|
|
2286
2547
|
}
|
|
@@ -2371,14 +2632,14 @@ async function writeJudgments(opts) {
|
|
|
2371
2632
|
}
|
|
2372
2633
|
|
|
2373
2634
|
// src/judge/index.ts
|
|
2374
|
-
function resolveJudge(backend) {
|
|
2635
|
+
function resolveJudge(backend, model = DEFAULT_LOCAL_MODEL) {
|
|
2375
2636
|
switch (backend) {
|
|
2376
2637
|
case "none":
|
|
2377
2638
|
return noneJudge;
|
|
2378
|
-
case "
|
|
2379
|
-
return
|
|
2639
|
+
case "agent":
|
|
2640
|
+
return agentJudge;
|
|
2380
2641
|
case "local":
|
|
2381
|
-
return
|
|
2642
|
+
return model === "moondream-3-9b-mlx" ? mlxLocalJudge : onnxLocalJudge;
|
|
2382
2643
|
}
|
|
2383
2644
|
}
|
|
2384
2645
|
function escapeXml(value) {
|
|
@@ -3130,7 +3391,7 @@ async function judgeNode(state) {
|
|
|
3130
3391
|
emitEvent({ type: "result", result: initial });
|
|
3131
3392
|
return { results: [initial] };
|
|
3132
3393
|
}
|
|
3133
|
-
const judge = resolveJudge(options.judge);
|
|
3394
|
+
const judge = resolveJudge(options.judge, options.model);
|
|
3134
3395
|
const output = await judge.judge(
|
|
3135
3396
|
{
|
|
3136
3397
|
entry,
|
|
@@ -3245,7 +3506,8 @@ async function dispatchNode(state) {
|
|
|
3245
3506
|
(item) => item.output.captureOutputPath
|
|
3246
3507
|
).length;
|
|
3247
3508
|
emitEvent({ type: "capture-complete", captured, total });
|
|
3248
|
-
if (state.options)
|
|
3509
|
+
if (state.options)
|
|
3510
|
+
await resolveJudge(state.options.judge, state.options.model).warmup?.();
|
|
3249
3511
|
return {};
|
|
3250
3512
|
}
|
|
3251
3513
|
function buildGraph(captureSemaphore, diffSemaphore, checkpointer, abortController) {
|
|
@@ -3362,6 +3624,7 @@ async function runGraph(opts) {
|
|
|
3362
3624
|
concurrency,
|
|
3363
3625
|
emitDiffPng: opts.emitDiffPng ?? true,
|
|
3364
3626
|
judge: opts.judge ?? "none",
|
|
3627
|
+
model: opts.model ?? DEFAULT_LOCAL_MODEL,
|
|
3365
3628
|
baselinesDir
|
|
3366
3629
|
}
|
|
3367
3630
|
};
|