@blazediff/agent 0.11.2 → 0.12.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 +595 -321
- package/dist/index.d.mts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +356 -93
- package/dist/index.mjs +357 -94
- package/package.json +1 -1
- package/skills/blazediff/JUDGING.md +1 -1
- package/skills/blazediff/SKILL.md +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -58,7 +58,13 @@ interface Judge {
|
|
|
58
58
|
*/
|
|
59
59
|
warmup?(): Promise<void>;
|
|
60
60
|
}
|
|
61
|
-
type JudgeBackend = "none" | "
|
|
61
|
+
type JudgeBackend = "none" | "agent" | "local";
|
|
62
|
+
/**
|
|
63
|
+
* Which model the `local` judge reads regions with. The ONNX model runs
|
|
64
|
+
* in-process anywhere; the MLX one is served by a managed Moondream Station and
|
|
65
|
+
* needs Apple Silicon, and is ~5x faster there.
|
|
66
|
+
*/
|
|
67
|
+
type LocalModel = "moondream-2-2b-onnx" | "moondream-3-9b-mlx";
|
|
62
68
|
|
|
63
69
|
declare const STABILITY_HOOKS_VERSION = 1;
|
|
64
70
|
/**
|
|
@@ -350,9 +356,13 @@ interface JudgmentRequest {
|
|
|
350
356
|
createdAt: string;
|
|
351
357
|
}
|
|
352
358
|
|
|
353
|
-
declare function resolveJudge(backend: JudgeBackend): Judge;
|
|
359
|
+
declare function resolveJudge(backend: JudgeBackend, model?: LocalModel): Judge;
|
|
354
360
|
|
|
355
361
|
interface JudgmentInterrupt {
|
|
362
|
+
/**
|
|
363
|
+
* Wire value predates the `host` → `agent` rename and is persisted in
|
|
364
|
+
* checkpoints, so renaming it would strand runs suspended by older versions.
|
|
365
|
+
*/
|
|
356
366
|
kind: "host-judgment-required";
|
|
357
367
|
entryId: string;
|
|
358
368
|
url: string;
|
|
@@ -399,6 +409,8 @@ interface RunOptions {
|
|
|
399
409
|
emitDiffPng?: boolean;
|
|
400
410
|
junitPath?: string;
|
|
401
411
|
judge?: JudgeBackend;
|
|
412
|
+
/** Vision model for `judge: "local"`; ignored by the other backends. */
|
|
413
|
+
model?: LocalModel;
|
|
402
414
|
threadId?: string;
|
|
403
415
|
onEvent?: (event: RunEvent) => void;
|
|
404
416
|
resume?: ResumeMap;
|
package/dist/index.d.ts
CHANGED
|
@@ -58,7 +58,13 @@ interface Judge {
|
|
|
58
58
|
*/
|
|
59
59
|
warmup?(): Promise<void>;
|
|
60
60
|
}
|
|
61
|
-
type JudgeBackend = "none" | "
|
|
61
|
+
type JudgeBackend = "none" | "agent" | "local";
|
|
62
|
+
/**
|
|
63
|
+
* Which model the `local` judge reads regions with. The ONNX model runs
|
|
64
|
+
* in-process anywhere; the MLX one is served by a managed Moondream Station and
|
|
65
|
+
* needs Apple Silicon, and is ~5x faster there.
|
|
66
|
+
*/
|
|
67
|
+
type LocalModel = "moondream-2-2b-onnx" | "moondream-3-9b-mlx";
|
|
62
68
|
|
|
63
69
|
declare const STABILITY_HOOKS_VERSION = 1;
|
|
64
70
|
/**
|
|
@@ -350,9 +356,13 @@ interface JudgmentRequest {
|
|
|
350
356
|
createdAt: string;
|
|
351
357
|
}
|
|
352
358
|
|
|
353
|
-
declare function resolveJudge(backend: JudgeBackend): Judge;
|
|
359
|
+
declare function resolveJudge(backend: JudgeBackend, model?: LocalModel): Judge;
|
|
354
360
|
|
|
355
361
|
interface JudgmentInterrupt {
|
|
362
|
+
/**
|
|
363
|
+
* Wire value predates the `host` → `agent` rename and is persisted in
|
|
364
|
+
* checkpoints, so renaming it would strand runs suspended by older versions.
|
|
365
|
+
*/
|
|
356
366
|
kind: "host-judgment-required";
|
|
357
367
|
entryId: string;
|
|
358
368
|
url: string;
|
|
@@ -399,6 +409,8 @@ interface RunOptions {
|
|
|
399
409
|
emitDiffPng?: boolean;
|
|
400
410
|
junitPath?: string;
|
|
401
411
|
judge?: JudgeBackend;
|
|
412
|
+
/** Vision model for `judge: "local"`; ignored by the other backends. */
|
|
413
|
+
model?: LocalModel;
|
|
402
414
|
threadId?: string;
|
|
403
415
|
onEvent?: (event: RunEvent) => void;
|
|
404
416
|
resume?: ResumeMap;
|
package/dist/index.js
CHANGED
|
@@ -14,6 +14,8 @@ var util = require('util');
|
|
|
14
14
|
var langgraph = require('@langchain/langgraph');
|
|
15
15
|
var sharp = require('sharp');
|
|
16
16
|
var pc = require('picocolors');
|
|
17
|
+
require('net');
|
|
18
|
+
var treeKill = require('tree-kill');
|
|
17
19
|
var langgraphCheckpoint = require('@langchain/langgraph-checkpoint');
|
|
18
20
|
|
|
19
21
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
@@ -41,12 +43,14 @@ var path__default = /*#__PURE__*/_interopDefault(path);
|
|
|
41
43
|
var util__namespace = /*#__PURE__*/_interopNamespace(util);
|
|
42
44
|
var sharp__default = /*#__PURE__*/_interopDefault(sharp);
|
|
43
45
|
var pc__default = /*#__PURE__*/_interopDefault(pc);
|
|
46
|
+
var treeKill__default = /*#__PURE__*/_interopDefault(treeKill);
|
|
44
47
|
|
|
45
48
|
// src/browser/capture.ts
|
|
46
49
|
var DEFAULT_VIEWPORT = { width: 1280, height: 800 };
|
|
47
50
|
var DEFAULT_WAIT_FOR = ["networkidle", "fonts"];
|
|
48
51
|
var DEFAULT_FULL_PAGE = true;
|
|
49
52
|
var DEFAULT_THRESHOLD = 0.1;
|
|
53
|
+
var DEFAULT_LOCAL_MODEL = "moondream-2-2b-onnx";
|
|
50
54
|
var MIN_AUTO_CONCURRENCY = 2;
|
|
51
55
|
var MAX_AUTO_CONCURRENCY = 8;
|
|
52
56
|
function cpuCores() {
|
|
@@ -904,9 +908,9 @@ function extractInternalLinks(base, target, hrefs) {
|
|
|
904
908
|
try {
|
|
905
909
|
const u = new URL(href, target);
|
|
906
910
|
if (u.origin !== base.origin) continue;
|
|
907
|
-
const
|
|
908
|
-
if (
|
|
909
|
-
out.push(
|
|
911
|
+
const path23 = u.pathname + u.search;
|
|
912
|
+
if (path23.startsWith("/api/")) continue;
|
|
913
|
+
out.push(path23);
|
|
910
914
|
} catch {
|
|
911
915
|
}
|
|
912
916
|
}
|
|
@@ -922,10 +926,10 @@ async function crawlRoutes(opts) {
|
|
|
922
926
|
const queue = [{ url: "/", depth: 0 }];
|
|
923
927
|
const discovered = [];
|
|
924
928
|
const recorded = /* @__PURE__ */ new Set();
|
|
925
|
-
const record = (
|
|
926
|
-
if (recorded.has(
|
|
927
|
-
recorded.add(
|
|
928
|
-
discovered.push({ url:
|
|
929
|
+
const record = (path23) => {
|
|
930
|
+
if (recorded.has(path23)) return;
|
|
931
|
+
recorded.add(path23);
|
|
932
|
+
discovered.push({ url: path23, source: "crawl" });
|
|
929
933
|
};
|
|
930
934
|
const handle = await acquireStableContext(CRAWL_VIEWPORT);
|
|
931
935
|
const fetchOne = async () => {
|
|
@@ -986,8 +990,8 @@ var DEFAULT_SAMPLING = {
|
|
|
986
990
|
threshold: DEFAULT_SAMPLE_THRESHOLD,
|
|
987
991
|
samples: DEFAULT_SAMPLES_PER_TEMPLATE
|
|
988
992
|
};
|
|
989
|
-
function parentPath(
|
|
990
|
-
const [pathPart] =
|
|
993
|
+
function parentPath(path23) {
|
|
994
|
+
const [pathPart] = path23.split("?", 2);
|
|
991
995
|
const idx = pathPart.lastIndexOf("/");
|
|
992
996
|
return idx <= 0 ? "/" : pathPart.slice(0, idx);
|
|
993
997
|
}
|
|
@@ -1299,7 +1303,7 @@ async function prepareTiles(opts) {
|
|
|
1299
1303
|
};
|
|
1300
1304
|
}
|
|
1301
1305
|
|
|
1302
|
-
// src/judge/
|
|
1306
|
+
// src/judge/agent.ts
|
|
1303
1307
|
async function tryPrepareTiles(input, entryDir) {
|
|
1304
1308
|
if (!input.regions || input.regions.length === 0) {
|
|
1305
1309
|
return null;
|
|
@@ -1320,8 +1324,8 @@ async function tryPrepareTiles(input, entryDir) {
|
|
|
1320
1324
|
return null;
|
|
1321
1325
|
}
|
|
1322
1326
|
}
|
|
1323
|
-
var
|
|
1324
|
-
name: "
|
|
1327
|
+
var agentJudge = {
|
|
1328
|
+
name: "agent",
|
|
1325
1329
|
async judge(input, cwd) {
|
|
1326
1330
|
input.onJudgingStart?.();
|
|
1327
1331
|
const p = paths(cwd);
|
|
@@ -1428,7 +1432,7 @@ function createLoadProgress(label, opts) {
|
|
|
1428
1432
|
clearInterval(heartbeat);
|
|
1429
1433
|
heartbeat = void 0;
|
|
1430
1434
|
};
|
|
1431
|
-
const
|
|
1435
|
+
const start2 = () => {
|
|
1432
1436
|
if (started) return;
|
|
1433
1437
|
started = true;
|
|
1434
1438
|
load.startedAt = Date.now();
|
|
@@ -1458,7 +1462,7 @@ function createLoadProgress(label, opts) {
|
|
|
1458
1462
|
};
|
|
1459
1463
|
const finish = (status) => {
|
|
1460
1464
|
if (finished) return;
|
|
1461
|
-
|
|
1465
|
+
start2();
|
|
1462
1466
|
finished = true;
|
|
1463
1467
|
stopHeartbeat();
|
|
1464
1468
|
if (interactive) {
|
|
@@ -1474,9 +1478,9 @@ function createLoadProgress(label, opts) {
|
|
|
1474
1478
|
);
|
|
1475
1479
|
};
|
|
1476
1480
|
return {
|
|
1477
|
-
start,
|
|
1481
|
+
start: start2,
|
|
1478
1482
|
onProgress(event) {
|
|
1479
|
-
|
|
1483
|
+
start2();
|
|
1480
1484
|
if (!event.file) return;
|
|
1481
1485
|
if (event.status === "done") {
|
|
1482
1486
|
const file = files.get(event.file);
|
|
@@ -1789,71 +1793,6 @@ function describeRegionChange(changeType, baseRead, actualRead) {
|
|
|
1789
1793
|
return `${kind} with no readable text change${updated}`;
|
|
1790
1794
|
}
|
|
1791
1795
|
|
|
1792
|
-
// src/judge/vision.ts
|
|
1793
|
-
var MODEL_ID2 = "Xenova/moondream2";
|
|
1794
|
-
var PATCH_SIZE = 14;
|
|
1795
|
-
var MAX_NEW_TOKENS2 = 64;
|
|
1796
|
-
var ANSWER_MARKER = "Answer:";
|
|
1797
|
-
async function defaultFactory2() {
|
|
1798
|
-
const { mod, tokenizer, processor, model } = await loadTransformersModel(
|
|
1799
|
-
"vision",
|
|
1800
|
-
async (mod2, progress_callback) => {
|
|
1801
|
-
const [tokenizer2, processor2, model2] = await Promise.all([
|
|
1802
|
-
mod2.AutoTokenizer.from_pretrained(MODEL_ID2, { progress_callback }),
|
|
1803
|
-
mod2.AutoProcessor.from_pretrained(MODEL_ID2, { progress_callback }),
|
|
1804
|
-
mod2.Moondream1ForConditionalGeneration.from_pretrained(MODEL_ID2, {
|
|
1805
|
-
dtype: {
|
|
1806
|
-
embed_tokens: "fp32",
|
|
1807
|
-
vision_encoder: "q8",
|
|
1808
|
-
decoder_model_merged: "q4"
|
|
1809
|
-
},
|
|
1810
|
-
device: "cpu",
|
|
1811
|
-
progress_callback
|
|
1812
|
-
})
|
|
1813
|
-
]);
|
|
1814
|
-
return { mod: mod2, tokenizer: tokenizer2, processor: processor2, model: model2 };
|
|
1815
|
-
}
|
|
1816
|
-
);
|
|
1817
|
-
return {
|
|
1818
|
-
async describe(imagePath, question) {
|
|
1819
|
-
const image = await mod.RawImage.read(imagePath);
|
|
1820
|
-
const vision = await processor(image);
|
|
1821
|
-
const [, , height, width] = vision.pixel_values.dims;
|
|
1822
|
-
const numImageTokens = Math.floor(height / PATCH_SIZE) * Math.floor(width / PATCH_SIZE);
|
|
1823
|
-
const prompt = `${"<image>".repeat(numImageTokens)}
|
|
1824
|
-
|
|
1825
|
-
Question: ${question}
|
|
1826
|
-
|
|
1827
|
-
${ANSWER_MARKER}`;
|
|
1828
|
-
const textInputs = tokenizer(prompt);
|
|
1829
|
-
const output = await model.generate({
|
|
1830
|
-
...textInputs,
|
|
1831
|
-
...vision,
|
|
1832
|
-
do_sample: false,
|
|
1833
|
-
max_new_tokens: MAX_NEW_TOKENS2,
|
|
1834
|
-
// Moondream loops the same sentence on composite tiles without these.
|
|
1835
|
-
repetition_penalty: 1.3,
|
|
1836
|
-
no_repeat_ngram_size: 3
|
|
1837
|
-
});
|
|
1838
|
-
const decoded = tokenizer.batch_decode(output, {
|
|
1839
|
-
skip_special_tokens: true
|
|
1840
|
-
});
|
|
1841
|
-
const raw = Array.isArray(decoded) ? String(decoded[0] ?? "") : String(decoded);
|
|
1842
|
-
const idx = raw.lastIndexOf(ANSWER_MARKER);
|
|
1843
|
-
return (idx >= 0 ? raw.slice(idx + ANSWER_MARKER.length) : raw).trim();
|
|
1844
|
-
}
|
|
1845
|
-
};
|
|
1846
|
-
}
|
|
1847
|
-
function createVisionRunnerHolder(factory = defaultFactory2) {
|
|
1848
|
-
let runnerPromise;
|
|
1849
|
-
return {
|
|
1850
|
-
get() {
|
|
1851
|
-
if (!runnerPromise) runnerPromise = factory();
|
|
1852
|
-
return runnerPromise;
|
|
1853
|
-
}
|
|
1854
|
-
};
|
|
1855
|
-
}
|
|
1856
|
-
|
|
1857
1796
|
// src/judge/local.ts
|
|
1858
1797
|
var READ_PROMPT = [
|
|
1859
1798
|
"Read the text in this image. Reply with only the exact words and numbers you",
|
|
@@ -1948,8 +1887,8 @@ function classifyFailure(err) {
|
|
|
1948
1887
|
return "read";
|
|
1949
1888
|
return "internal";
|
|
1950
1889
|
}
|
|
1951
|
-
function createLocalJudge(deps
|
|
1952
|
-
const vision = deps.vision
|
|
1890
|
+
function createLocalJudge(deps) {
|
|
1891
|
+
const vision = deps.vision;
|
|
1953
1892
|
const classifier = deps.classifier ?? createClassifierRunnerHolder();
|
|
1954
1893
|
const visionSemaphore = createSemaphore(1);
|
|
1955
1894
|
const classifierSemaphore = createSemaphore(1);
|
|
@@ -1983,7 +1922,7 @@ function createLocalJudge(deps = {}) {
|
|
|
1983
1922
|
return (await v.describe(input.actualPath, DESCRIBE_PROMPT)).trim();
|
|
1984
1923
|
}
|
|
1985
1924
|
return {
|
|
1986
|
-
name: "local",
|
|
1925
|
+
name: deps.name ?? "local",
|
|
1987
1926
|
// Stream both models' weights up front (concurrently) so the multi-second
|
|
1988
1927
|
// load is a single visible phase before judging, not a silent stall on the
|
|
1989
1928
|
// first test. Both are cached promises, so the judge reuses them after.
|
|
@@ -2038,7 +1977,329 @@ ${answer}`.trim()
|
|
|
2038
1977
|
}
|
|
2039
1978
|
};
|
|
2040
1979
|
}
|
|
2041
|
-
|
|
1980
|
+
|
|
1981
|
+
// src/judge/vision.ts
|
|
1982
|
+
function createVisionRunnerHolder(factory) {
|
|
1983
|
+
let runnerPromise;
|
|
1984
|
+
return {
|
|
1985
|
+
get() {
|
|
1986
|
+
if (!runnerPromise) runnerPromise = factory();
|
|
1987
|
+
return runnerPromise;
|
|
1988
|
+
}
|
|
1989
|
+
};
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1992
|
+
// src/judge/local-moondream-2-2b-onnx.ts
|
|
1993
|
+
var MODEL_ID2 = "Xenova/moondream2";
|
|
1994
|
+
var PATCH_SIZE = 14;
|
|
1995
|
+
var MAX_NEW_TOKENS2 = 64;
|
|
1996
|
+
var ANSWER_MARKER = "Answer:";
|
|
1997
|
+
async function createOnnxVisionRunner() {
|
|
1998
|
+
const { mod, tokenizer, processor, model } = await loadTransformersModel(
|
|
1999
|
+
"vision",
|
|
2000
|
+
async (mod2, progress_callback) => {
|
|
2001
|
+
const [tokenizer2, processor2, model2] = await Promise.all([
|
|
2002
|
+
mod2.AutoTokenizer.from_pretrained(MODEL_ID2, { progress_callback }),
|
|
2003
|
+
mod2.AutoProcessor.from_pretrained(MODEL_ID2, { progress_callback }),
|
|
2004
|
+
mod2.Moondream1ForConditionalGeneration.from_pretrained(MODEL_ID2, {
|
|
2005
|
+
dtype: {
|
|
2006
|
+
embed_tokens: "fp32",
|
|
2007
|
+
vision_encoder: "q8",
|
|
2008
|
+
decoder_model_merged: "q4"
|
|
2009
|
+
},
|
|
2010
|
+
device: "cpu",
|
|
2011
|
+
progress_callback
|
|
2012
|
+
})
|
|
2013
|
+
]);
|
|
2014
|
+
return { mod: mod2, tokenizer: tokenizer2, processor: processor2, model: model2 };
|
|
2015
|
+
}
|
|
2016
|
+
);
|
|
2017
|
+
return {
|
|
2018
|
+
async describe(imagePath, question) {
|
|
2019
|
+
const image = await mod.RawImage.read(imagePath);
|
|
2020
|
+
const vision = await processor(image);
|
|
2021
|
+
const [, , height, width] = vision.pixel_values.dims;
|
|
2022
|
+
const numImageTokens = Math.floor(height / PATCH_SIZE) * Math.floor(width / PATCH_SIZE);
|
|
2023
|
+
const prompt = `${"<image>".repeat(numImageTokens)}
|
|
2024
|
+
|
|
2025
|
+
Question: ${question}
|
|
2026
|
+
|
|
2027
|
+
${ANSWER_MARKER}`;
|
|
2028
|
+
const textInputs = tokenizer(prompt);
|
|
2029
|
+
const output = await model.generate({
|
|
2030
|
+
...textInputs,
|
|
2031
|
+
...vision,
|
|
2032
|
+
do_sample: false,
|
|
2033
|
+
max_new_tokens: MAX_NEW_TOKENS2,
|
|
2034
|
+
// Moondream loops the same sentence on composite tiles without these.
|
|
2035
|
+
repetition_penalty: 1.3,
|
|
2036
|
+
no_repeat_ngram_size: 3
|
|
2037
|
+
});
|
|
2038
|
+
const decoded = tokenizer.batch_decode(output, {
|
|
2039
|
+
skip_special_tokens: true
|
|
2040
|
+
});
|
|
2041
|
+
const raw = Array.isArray(decoded) ? String(decoded[0] ?? "") : String(decoded);
|
|
2042
|
+
const idx = raw.lastIndexOf(ANSWER_MARKER);
|
|
2043
|
+
return (idx >= 0 ? raw.slice(idx + ANSWER_MARKER.length) : raw).trim();
|
|
2044
|
+
}
|
|
2045
|
+
};
|
|
2046
|
+
}
|
|
2047
|
+
function createOnnxLocalJudge() {
|
|
2048
|
+
return createLocalJudge({
|
|
2049
|
+
vision: createVisionRunnerHolder(createOnnxVisionRunner),
|
|
2050
|
+
name: "local:moondream-2-2b-onnx"
|
|
2051
|
+
});
|
|
2052
|
+
}
|
|
2053
|
+
var onnxLocalJudge = createOnnxLocalJudge();
|
|
2054
|
+
util.promisify(child_process.execFile);
|
|
2055
|
+
async function stopProcess(pid) {
|
|
2056
|
+
if (!pid) return;
|
|
2057
|
+
await new Promise((resolve) => {
|
|
2058
|
+
treeKill__default.default(pid, "SIGTERM", (err) => {
|
|
2059
|
+
if (err) {
|
|
2060
|
+
treeKill__default.default(pid, "SIGKILL", () => resolve());
|
|
2061
|
+
return;
|
|
2062
|
+
}
|
|
2063
|
+
resolve();
|
|
2064
|
+
});
|
|
2065
|
+
});
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
// src/judge/station.ts
|
|
2069
|
+
var execFileP2 = util.promisify(child_process.execFile);
|
|
2070
|
+
var PORT = 2020;
|
|
2071
|
+
var BASE_URL = `http://127.0.0.1:${PORT}`;
|
|
2072
|
+
var COMMAND = "moondream-station";
|
|
2073
|
+
var MODEL = "moondream-3-preview-mlx-quantized";
|
|
2074
|
+
var MIN_INFERENCE_TIMEOUT_S = 900;
|
|
2075
|
+
var READY_TIMEOUT_MS = 12e4;
|
|
2076
|
+
var SETTINGS_TIMEOUT_MS = 3e4;
|
|
2077
|
+
var MODEL_TIMEOUT_MS = 9e5;
|
|
2078
|
+
var POLL_INTERVAL_MS = 500;
|
|
2079
|
+
var OUTPUT_TAIL_LINES = 15;
|
|
2080
|
+
var WARMUP_IMAGE = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAADElEQVR42mP4//8/AAX+Av4zEpUUAAAAAElFTkSuQmCC";
|
|
2081
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
2082
|
+
var note = (message) => {
|
|
2083
|
+
process.stderr.write(`${dim("[blazediff]")} ${message}
|
|
2084
|
+
`);
|
|
2085
|
+
};
|
|
2086
|
+
async function readStats() {
|
|
2087
|
+
try {
|
|
2088
|
+
const response = await fetch(`${BASE_URL}/v1/stats`, {
|
|
2089
|
+
signal: AbortSignal.timeout(2e3)
|
|
2090
|
+
});
|
|
2091
|
+
return response.ok ? await response.json() : null;
|
|
2092
|
+
} catch {
|
|
2093
|
+
return null;
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
async function waitForStats(predicate, timeoutMs, what, tail) {
|
|
2097
|
+
const deadline = Date.now() + timeoutMs;
|
|
2098
|
+
while (Date.now() < deadline) {
|
|
2099
|
+
const stats = await readStats();
|
|
2100
|
+
if (stats && predicate(stats)) return stats;
|
|
2101
|
+
await sleep(POLL_INTERVAL_MS);
|
|
2102
|
+
}
|
|
2103
|
+
const context = tail.length > 0 ? `
|
|
2104
|
+
${tail.join("\n")}` : "";
|
|
2105
|
+
throw new Error(`moondream station: timed out waiting for ${what}${context}`);
|
|
2106
|
+
}
|
|
2107
|
+
async function commandExists(command) {
|
|
2108
|
+
try {
|
|
2109
|
+
await execFileP2(process.platform === "win32" ? "where" : "which", [
|
|
2110
|
+
command
|
|
2111
|
+
]);
|
|
2112
|
+
return true;
|
|
2113
|
+
} catch {
|
|
2114
|
+
return false;
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
function ensureApplePlatform() {
|
|
2118
|
+
if (process.platform === "darwin" && process.arch === "arm64") return;
|
|
2119
|
+
throw new Error(
|
|
2120
|
+
"the moondream judge needs Apple Silicon (moondream station's Metal build). Use --judge local for the in-process ONNX judge."
|
|
2121
|
+
);
|
|
2122
|
+
}
|
|
2123
|
+
async function ensureInstalled() {
|
|
2124
|
+
if (await commandExists(COMMAND)) return;
|
|
2125
|
+
if (!await commandExists("uv")) {
|
|
2126
|
+
throw new Error(
|
|
2127
|
+
`moondream station is not installed. Install uv (https://docs.astral.sh/uv) and retry, or install it yourself: pipx install ${COMMAND}`
|
|
2128
|
+
);
|
|
2129
|
+
}
|
|
2130
|
+
note(`installing ${bold(COMMAND)} ${dim("(one time)")}`);
|
|
2131
|
+
await execFileP2("uv", ["tool", "install", COMMAND]);
|
|
2132
|
+
}
|
|
2133
|
+
function launch() {
|
|
2134
|
+
const tail = [];
|
|
2135
|
+
const child = child_process.spawn(COMMAND, {
|
|
2136
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
2137
|
+
detached: true,
|
|
2138
|
+
env: { ...process.env, NO_COLOR: "1" }
|
|
2139
|
+
});
|
|
2140
|
+
const remember = (chunk) => {
|
|
2141
|
+
for (const line of chunk.toString().split("\n")) {
|
|
2142
|
+
const text = line.trim();
|
|
2143
|
+
if (!text) continue;
|
|
2144
|
+
tail.push(text);
|
|
2145
|
+
if (tail.length > OUTPUT_TAIL_LINES) tail.shift();
|
|
2146
|
+
}
|
|
2147
|
+
};
|
|
2148
|
+
child.stdout?.on("data", remember);
|
|
2149
|
+
child.stderr?.on("data", remember);
|
|
2150
|
+
child.once("error", (error) => tail.push(error.message));
|
|
2151
|
+
child.stdin?.write("\n");
|
|
2152
|
+
return { child, tail };
|
|
2153
|
+
}
|
|
2154
|
+
var send = (child, command) => {
|
|
2155
|
+
child.stdin?.write(`${command}
|
|
2156
|
+
`);
|
|
2157
|
+
};
|
|
2158
|
+
async function ensureTimeout(child, tail) {
|
|
2159
|
+
const stats = await readStats();
|
|
2160
|
+
if ((stats?.default_timeout ?? 0) >= MIN_INFERENCE_TIMEOUT_S) return;
|
|
2161
|
+
send(child, `settings set inference_timeout ${MIN_INFERENCE_TIMEOUT_S}`);
|
|
2162
|
+
await waitForStats(
|
|
2163
|
+
(s) => (s.default_timeout ?? 0) >= MIN_INFERENCE_TIMEOUT_S,
|
|
2164
|
+
SETTINGS_TIMEOUT_MS,
|
|
2165
|
+
"the inference timeout to apply",
|
|
2166
|
+
tail
|
|
2167
|
+
);
|
|
2168
|
+
}
|
|
2169
|
+
async function ensureModel(child, active, tail) {
|
|
2170
|
+
if (active === MODEL) return;
|
|
2171
|
+
note(
|
|
2172
|
+
`switching moondream station to ${bold(MODEL)} ${dim("(first run downloads weights)")}`
|
|
2173
|
+
);
|
|
2174
|
+
send(child, `models switch ${MODEL}`);
|
|
2175
|
+
send(child, "y");
|
|
2176
|
+
await waitForStats(
|
|
2177
|
+
(s) => s.model === MODEL && s.status === "running",
|
|
2178
|
+
MODEL_TIMEOUT_MS,
|
|
2179
|
+
`moondream station to load ${MODEL}`,
|
|
2180
|
+
tail
|
|
2181
|
+
);
|
|
2182
|
+
}
|
|
2183
|
+
async function warmModel(endpoint) {
|
|
2184
|
+
const response = await fetch(`${endpoint}/query`, {
|
|
2185
|
+
method: "POST",
|
|
2186
|
+
headers: { "Content-Type": "application/json" },
|
|
2187
|
+
body: JSON.stringify({
|
|
2188
|
+
image_url: WARMUP_IMAGE,
|
|
2189
|
+
question: "Read the text in this image.",
|
|
2190
|
+
stream: false
|
|
2191
|
+
}),
|
|
2192
|
+
signal: AbortSignal.timeout(MODEL_TIMEOUT_MS)
|
|
2193
|
+
});
|
|
2194
|
+
const payload = await response.json();
|
|
2195
|
+
if (payload.error) throw new Error(`moondream station: ${payload.error}`);
|
|
2196
|
+
}
|
|
2197
|
+
function killOnExit(child) {
|
|
2198
|
+
const kill = () => {
|
|
2199
|
+
if (!child.pid) return;
|
|
2200
|
+
try {
|
|
2201
|
+
process.kill(-child.pid, "SIGKILL");
|
|
2202
|
+
} catch {
|
|
2203
|
+
}
|
|
2204
|
+
};
|
|
2205
|
+
process.once("exit", kill);
|
|
2206
|
+
process.once("SIGINT", () => {
|
|
2207
|
+
kill();
|
|
2208
|
+
process.exit(130);
|
|
2209
|
+
});
|
|
2210
|
+
process.once("SIGTERM", () => {
|
|
2211
|
+
kill();
|
|
2212
|
+
process.exit(143);
|
|
2213
|
+
});
|
|
2214
|
+
}
|
|
2215
|
+
async function start() {
|
|
2216
|
+
const endpoint = `${BASE_URL}/v1`;
|
|
2217
|
+
const running = await readStats();
|
|
2218
|
+
if (running) {
|
|
2219
|
+
await warmModel(endpoint);
|
|
2220
|
+
return {
|
|
2221
|
+
endpoint,
|
|
2222
|
+
model: running.model ?? "unknown",
|
|
2223
|
+
attached: true,
|
|
2224
|
+
stop: async () => {
|
|
2225
|
+
}
|
|
2226
|
+
};
|
|
2227
|
+
}
|
|
2228
|
+
ensureApplePlatform();
|
|
2229
|
+
await ensureInstalled();
|
|
2230
|
+
const { child, tail } = launch();
|
|
2231
|
+
try {
|
|
2232
|
+
const stats = await waitForStats(
|
|
2233
|
+
() => true,
|
|
2234
|
+
READY_TIMEOUT_MS,
|
|
2235
|
+
"moondream station to start",
|
|
2236
|
+
tail
|
|
2237
|
+
);
|
|
2238
|
+
await ensureTimeout(child, tail);
|
|
2239
|
+
await ensureModel(child, stats.model, tail);
|
|
2240
|
+
await warmModel(endpoint);
|
|
2241
|
+
killOnExit(child);
|
|
2242
|
+
note(`moondream station ready ${pc__default.default.green("\u2713")} ${dim(endpoint)}`);
|
|
2243
|
+
return {
|
|
2244
|
+
endpoint,
|
|
2245
|
+
model: MODEL,
|
|
2246
|
+
attached: false,
|
|
2247
|
+
stop: async () => {
|
|
2248
|
+
if (child.pid) await stopProcess(child.pid);
|
|
2249
|
+
}
|
|
2250
|
+
};
|
|
2251
|
+
} catch (error) {
|
|
2252
|
+
if (child.pid) await stopProcess(child.pid);
|
|
2253
|
+
throw error;
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
var station;
|
|
2257
|
+
function startStation() {
|
|
2258
|
+
station ?? (station = start());
|
|
2259
|
+
return station;
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
// src/judge/local-moondream-3-9b-mlx.ts
|
|
2263
|
+
var REQUEST_TIMEOUT_MS = 12e4;
|
|
2264
|
+
async function toDataUrl(imagePath) {
|
|
2265
|
+
const bytes = await promises.readFile(imagePath);
|
|
2266
|
+
return `data:image/png;base64,${bytes.toString("base64")}`;
|
|
2267
|
+
}
|
|
2268
|
+
function createStationVisionRunner(endpoint) {
|
|
2269
|
+
const queryUrl = `${endpoint.replace(/\/+$/, "")}/query`;
|
|
2270
|
+
return {
|
|
2271
|
+
async describe(imagePath, question) {
|
|
2272
|
+
const response = await fetch(queryUrl, {
|
|
2273
|
+
method: "POST",
|
|
2274
|
+
headers: { "Content-Type": "application/json" },
|
|
2275
|
+
body: JSON.stringify({
|
|
2276
|
+
image_url: await toDataUrl(imagePath),
|
|
2277
|
+
question,
|
|
2278
|
+
stream: false
|
|
2279
|
+
}),
|
|
2280
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
2281
|
+
});
|
|
2282
|
+
if (!response.ok) {
|
|
2283
|
+
throw new Error(
|
|
2284
|
+
`moondream station returned ${response.status} for ${queryUrl}`
|
|
2285
|
+
);
|
|
2286
|
+
}
|
|
2287
|
+
const payload = await response.json();
|
|
2288
|
+
if (payload.error) {
|
|
2289
|
+
throw new Error(`moondream station: ${payload.error}`);
|
|
2290
|
+
}
|
|
2291
|
+
return (payload.answer ?? "").trim();
|
|
2292
|
+
}
|
|
2293
|
+
};
|
|
2294
|
+
}
|
|
2295
|
+
function createMlxLocalJudge() {
|
|
2296
|
+
const vision = createVisionRunnerHolder(async () => {
|
|
2297
|
+
const station2 = await startStation();
|
|
2298
|
+
return createStationVisionRunner(station2.endpoint);
|
|
2299
|
+
});
|
|
2300
|
+
return createLocalJudge({ vision, name: "local:moondream-3-9b-mlx" });
|
|
2301
|
+
}
|
|
2302
|
+
var mlxLocalJudge = createMlxLocalJudge();
|
|
2042
2303
|
|
|
2043
2304
|
// src/judge/none.ts
|
|
2044
2305
|
var noneJudge = {
|
|
@@ -2260,7 +2521,7 @@ async function applyJudgments(opts = {}) {
|
|
|
2260
2521
|
});
|
|
2261
2522
|
return { report, applied, missing, invalid };
|
|
2262
2523
|
}
|
|
2263
|
-
var
|
|
2524
|
+
var AGENT_INSTRUCTIONS = [
|
|
2264
2525
|
"The visual-regression heuristic could not classify this diff confidently.",
|
|
2265
2526
|
"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).",
|
|
2266
2527
|
"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).",
|
|
@@ -2308,7 +2569,7 @@ function buildRequest(result, entry, cwd, tiles) {
|
|
|
2308
2569
|
},
|
|
2309
2570
|
signature: signatureOf(result),
|
|
2310
2571
|
message: result.message,
|
|
2311
|
-
instructions: isAmbiguous ?
|
|
2572
|
+
instructions: isAmbiguous ? AGENT_INSTRUCTIONS : void 0,
|
|
2312
2573
|
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2313
2574
|
};
|
|
2314
2575
|
}
|
|
@@ -2399,14 +2660,14 @@ async function writeJudgments(opts) {
|
|
|
2399
2660
|
}
|
|
2400
2661
|
|
|
2401
2662
|
// src/judge/index.ts
|
|
2402
|
-
function resolveJudge(backend) {
|
|
2663
|
+
function resolveJudge(backend, model = DEFAULT_LOCAL_MODEL) {
|
|
2403
2664
|
switch (backend) {
|
|
2404
2665
|
case "none":
|
|
2405
2666
|
return noneJudge;
|
|
2406
|
-
case "
|
|
2407
|
-
return
|
|
2667
|
+
case "agent":
|
|
2668
|
+
return agentJudge;
|
|
2408
2669
|
case "local":
|
|
2409
|
-
return
|
|
2670
|
+
return model === "moondream-3-9b-mlx" ? mlxLocalJudge : onnxLocalJudge;
|
|
2410
2671
|
}
|
|
2411
2672
|
}
|
|
2412
2673
|
function escapeXml(value) {
|
|
@@ -3158,7 +3419,7 @@ async function judgeNode(state) {
|
|
|
3158
3419
|
emitEvent({ type: "result", result: initial });
|
|
3159
3420
|
return { results: [initial] };
|
|
3160
3421
|
}
|
|
3161
|
-
const judge = resolveJudge(options.judge);
|
|
3422
|
+
const judge = resolveJudge(options.judge, options.model);
|
|
3162
3423
|
const output = await judge.judge(
|
|
3163
3424
|
{
|
|
3164
3425
|
entry,
|
|
@@ -3273,7 +3534,8 @@ async function dispatchNode(state) {
|
|
|
3273
3534
|
(item) => item.output.captureOutputPath
|
|
3274
3535
|
).length;
|
|
3275
3536
|
emitEvent({ type: "capture-complete", captured, total });
|
|
3276
|
-
if (state.options)
|
|
3537
|
+
if (state.options)
|
|
3538
|
+
await resolveJudge(state.options.judge, state.options.model).warmup?.();
|
|
3277
3539
|
return {};
|
|
3278
3540
|
}
|
|
3279
3541
|
function buildGraph(captureSemaphore, diffSemaphore, checkpointer, abortController) {
|
|
@@ -3390,6 +3652,7 @@ async function runGraph(opts) {
|
|
|
3390
3652
|
concurrency,
|
|
3391
3653
|
emitDiffPng: opts.emitDiffPng ?? true,
|
|
3392
3654
|
judge: opts.judge ?? "none",
|
|
3655
|
+
model: opts.model ?? DEFAULT_LOCAL_MODEL,
|
|
3393
3656
|
baselinesDir
|
|
3394
3657
|
}
|
|
3395
3658
|
};
|