fascicle 0.3.3 → 0.4.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 +64 -0
- package/README.md +58 -5
- package/dist/bin/fascicle-viewer.js +9 -0
- package/dist/index.d.ts +153 -7
- package/dist/index.js +1227 -21
- package/dist/index.js.map +1 -1
- package/dist/static/viewer.html +374 -0
- package/package.json +8 -3
package/dist/index.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import { readFile, readdir } from "node:fs/promises";
|
|
4
|
-
import { join } from "node:path";
|
|
3
|
+
import { mkdir, open, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { appendFileSync, createReadStream, existsSync, mkdirSync, mkdtempSync, statSync, watch, writeFileSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
5
7
|
import { spawn } from "node:child_process";
|
|
6
8
|
import { generateText, stepCountIs, streamText, tool } from "ai";
|
|
9
|
+
import { createServer } from "node:http";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { parseArgs } from "node:util";
|
|
7
12
|
//#region packages/core/src/flow-schema.json
|
|
8
13
|
var flow_schema_default = {
|
|
9
14
|
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
@@ -1945,7 +1950,7 @@ const trajectory_event_schema = z.union([
|
|
|
1945
1950
|
]);
|
|
1946
1951
|
//#endregion
|
|
1947
1952
|
//#region packages/core/src/version.ts
|
|
1948
|
-
const version = "0.
|
|
1953
|
+
const version = "0.4.0";
|
|
1949
1954
|
//#endregion
|
|
1950
1955
|
//#region packages/core/src/index.ts
|
|
1951
1956
|
/**
|
|
@@ -1953,7 +1958,9 @@ const version = "0.3.3";
|
|
|
1953
1958
|
*
|
|
1954
1959
|
* Side-effect imports ensure every composer registers itself with the runner
|
|
1955
1960
|
* at module load. Importing anything from `@repo/core` populates the
|
|
1956
|
-
* dispatch table with
|
|
1961
|
+
* dispatch table with the core primitives. Built-in composites
|
|
1962
|
+
* (adversarial, ensemble, tournament, consensus) live in @repo/composites
|
|
1963
|
+
* and surface through the umbrella package.
|
|
1957
1964
|
*/
|
|
1958
1965
|
const flow_schema = flow_schema_default;
|
|
1959
1966
|
//#endregion
|
|
@@ -2592,8 +2599,613 @@ function tournament(config) {
|
|
|
2592
2599
|
return compose(config.name ?? "tournament", inner);
|
|
2593
2600
|
}
|
|
2594
2601
|
//#endregion
|
|
2602
|
+
//#region packages/observability/src/filesystem.ts
|
|
2603
|
+
/**
|
|
2604
|
+
* Filesystem JSONL trajectory logger.
|
|
2605
|
+
*
|
|
2606
|
+
* Appends one JSON object per line to a configured output file. `start_span`
|
|
2607
|
+
* and `end_span` each emit a line; `record` emits a line with the event as
|
|
2608
|
+
* written. Hierarchical span ids are tracked via an in-memory stack: when
|
|
2609
|
+
* `start_span` fires, the innermost still-open span's id is attached as
|
|
2610
|
+
* `parent_span_id` on the new span's record. This is best-effort for
|
|
2611
|
+
* concurrent children (two siblings spawned from the same parent will see
|
|
2612
|
+
* whichever opened most recently as their parent until proper async-context
|
|
2613
|
+
* propagation is wired). Two concurrent `run(...)` calls that pass distinct
|
|
2614
|
+
* logger instances (constructed with distinct `output_path`s) share nothing.
|
|
2615
|
+
*
|
|
2616
|
+
* Paths are accepted at construction; the logger never reads `process.env`.
|
|
2617
|
+
*/
|
|
2618
|
+
function filesystem_logger(options) {
|
|
2619
|
+
const { output_path } = options;
|
|
2620
|
+
mkdirSync(dirname(output_path), { recursive: true });
|
|
2621
|
+
const stack = [];
|
|
2622
|
+
const write_line = (event) => {
|
|
2623
|
+
appendFileSync(output_path, `${JSON.stringify(event)}\n`);
|
|
2624
|
+
};
|
|
2625
|
+
const start_span = (name, meta) => {
|
|
2626
|
+
const span_id = `${name}:${randomUUID().slice(0, 8)}`;
|
|
2627
|
+
const parent_span_id = stack.length > 0 ? stack[stack.length - 1] : void 0;
|
|
2628
|
+
const event = {
|
|
2629
|
+
kind: "span_start",
|
|
2630
|
+
span_id,
|
|
2631
|
+
name,
|
|
2632
|
+
...meta
|
|
2633
|
+
};
|
|
2634
|
+
if (parent_span_id !== void 0) event["parent_span_id"] = parent_span_id;
|
|
2635
|
+
write_line(event);
|
|
2636
|
+
stack.push(span_id);
|
|
2637
|
+
return span_id;
|
|
2638
|
+
};
|
|
2639
|
+
const end_span = (id, meta) => {
|
|
2640
|
+
write_line({
|
|
2641
|
+
kind: "span_end",
|
|
2642
|
+
span_id: id,
|
|
2643
|
+
...meta
|
|
2644
|
+
});
|
|
2645
|
+
const idx = stack.lastIndexOf(id);
|
|
2646
|
+
if (idx !== -1) stack.splice(idx, 1);
|
|
2647
|
+
};
|
|
2648
|
+
const record = (event) => {
|
|
2649
|
+
write_line({ ...event });
|
|
2650
|
+
};
|
|
2651
|
+
return {
|
|
2652
|
+
record,
|
|
2653
|
+
start_span,
|
|
2654
|
+
end_span
|
|
2655
|
+
};
|
|
2656
|
+
}
|
|
2657
|
+
//#endregion
|
|
2658
|
+
//#region packages/observability/src/http.ts
|
|
2659
|
+
/**
|
|
2660
|
+
* HTTP push trajectory logger.
|
|
2661
|
+
*
|
|
2662
|
+
* Conforms to `TrajectoryLogger`. POSTs each event as a single line of
|
|
2663
|
+
* newline-delimited JSON to a configured URL. Pairs naturally with the
|
|
2664
|
+
* fascicle viewer's `/api/ingest` endpoint, but is a generic adapter — the
|
|
2665
|
+
* receiver can be anything that accepts NDJSON over HTTP.
|
|
2666
|
+
*
|
|
2667
|
+
* Failure policy: drop on transport error and invoke the optional `on_error`
|
|
2668
|
+
* callback. Never throws, never blocks the user's flow on a dev tool being
|
|
2669
|
+
* up. The flow always wins; the viewer is best-effort.
|
|
2670
|
+
*
|
|
2671
|
+
* Hierarchical span ids are tracked in-process, mirroring `filesystem_logger`
|
|
2672
|
+
* so the wire format is identical.
|
|
2673
|
+
*/
|
|
2674
|
+
const default_fetch = async (url, init) => {
|
|
2675
|
+
const res = await fetch(url, init);
|
|
2676
|
+
return {
|
|
2677
|
+
ok: res.ok,
|
|
2678
|
+
status: res.status
|
|
2679
|
+
};
|
|
2680
|
+
};
|
|
2681
|
+
function http_logger(options) {
|
|
2682
|
+
const { url, on_error } = options;
|
|
2683
|
+
const send = options.fetch ?? default_fetch;
|
|
2684
|
+
const stack = [];
|
|
2685
|
+
const post = (event) => {
|
|
2686
|
+
send(url, {
|
|
2687
|
+
method: "POST",
|
|
2688
|
+
headers: { "content-type": "application/x-ndjson" },
|
|
2689
|
+
body: `${JSON.stringify(event)}\n`
|
|
2690
|
+
}).then((res) => {
|
|
2691
|
+
if (!res.ok && on_error) on_error(/* @__PURE__ */ new Error(`http_logger: POST ${url} -> ${res.status}`));
|
|
2692
|
+
}).catch((err) => {
|
|
2693
|
+
if (on_error) on_error(err);
|
|
2694
|
+
});
|
|
2695
|
+
};
|
|
2696
|
+
const start_span = (name, meta) => {
|
|
2697
|
+
const span_id = `${name}:${randomUUID().slice(0, 8)}`;
|
|
2698
|
+
const parent_span_id = stack.length > 0 ? stack[stack.length - 1] : void 0;
|
|
2699
|
+
const event = {
|
|
2700
|
+
kind: "span_start",
|
|
2701
|
+
span_id,
|
|
2702
|
+
name,
|
|
2703
|
+
...meta
|
|
2704
|
+
};
|
|
2705
|
+
if (parent_span_id !== void 0) event["parent_span_id"] = parent_span_id;
|
|
2706
|
+
post(event);
|
|
2707
|
+
stack.push(span_id);
|
|
2708
|
+
return span_id;
|
|
2709
|
+
};
|
|
2710
|
+
const end_span = (id, meta) => {
|
|
2711
|
+
post({
|
|
2712
|
+
kind: "span_end",
|
|
2713
|
+
span_id: id,
|
|
2714
|
+
...meta
|
|
2715
|
+
});
|
|
2716
|
+
const idx = stack.lastIndexOf(id);
|
|
2717
|
+
if (idx !== -1) stack.splice(idx, 1);
|
|
2718
|
+
};
|
|
2719
|
+
const record = (event) => {
|
|
2720
|
+
post({ ...event });
|
|
2721
|
+
};
|
|
2722
|
+
return {
|
|
2723
|
+
record,
|
|
2724
|
+
start_span,
|
|
2725
|
+
end_span
|
|
2726
|
+
};
|
|
2727
|
+
}
|
|
2728
|
+
//#endregion
|
|
2729
|
+
//#region packages/observability/src/tee.ts
|
|
2730
|
+
function tee_logger(...loggers) {
|
|
2731
|
+
if (loggers.length === 0) throw new Error("tee_logger: at least one logger required");
|
|
2732
|
+
const id_map = /* @__PURE__ */ new Map();
|
|
2733
|
+
const safe_each = (fn) => {
|
|
2734
|
+
for (let i = 0; i < loggers.length; i += 1) {
|
|
2735
|
+
const l = loggers[i];
|
|
2736
|
+
if (l === void 0) continue;
|
|
2737
|
+
try {
|
|
2738
|
+
fn(l, i);
|
|
2739
|
+
} catch {}
|
|
2740
|
+
}
|
|
2741
|
+
};
|
|
2742
|
+
return {
|
|
2743
|
+
record: (event) => {
|
|
2744
|
+
safe_each((l) => l.record(event));
|
|
2745
|
+
},
|
|
2746
|
+
start_span: (name, meta) => {
|
|
2747
|
+
const ids = [];
|
|
2748
|
+
safe_each((l) => {
|
|
2749
|
+
ids.push(l.start_span(name, meta));
|
|
2750
|
+
});
|
|
2751
|
+
const canonical = ids[0] ?? `${name}:tee`;
|
|
2752
|
+
if (loggers.length > 1) id_map.set(canonical, ids);
|
|
2753
|
+
return canonical;
|
|
2754
|
+
},
|
|
2755
|
+
end_span: (id, meta) => {
|
|
2756
|
+
const ids = id_map.get(id);
|
|
2757
|
+
if (ids === void 0) {
|
|
2758
|
+
safe_each((l) => l.end_span(id, meta));
|
|
2759
|
+
return;
|
|
2760
|
+
}
|
|
2761
|
+
id_map.delete(id);
|
|
2762
|
+
safe_each((l, idx) => l.end_span(ids[idx] ?? id, meta));
|
|
2763
|
+
}
|
|
2764
|
+
};
|
|
2765
|
+
}
|
|
2766
|
+
//#endregion
|
|
2767
|
+
//#region packages/composites/src/bench.ts
|
|
2768
|
+
/**
|
|
2769
|
+
* bench: run a flow against a fixture set, score each result, return a report.
|
|
2770
|
+
*
|
|
2771
|
+
* Online counterpart to `learn`: where `learn` reflects on past trajectories
|
|
2772
|
+
* after the fact, `bench` runs the flow live against fresh `cases`, judges
|
|
2773
|
+
* each output, and produces a `BenchReport` you can compare against a
|
|
2774
|
+
* committed baseline via `regression_compare`.
|
|
2775
|
+
*
|
|
2776
|
+
* Each case becomes one `run(flow, case.input, ...)`. Per-case observability:
|
|
2777
|
+
* - `trajectory_dir` writes `${dir}/${case.id}.jsonl` via filesystem_logger
|
|
2778
|
+
* - `live_url` POSTs each event via http_logger to a viewer's /api/ingest
|
|
2779
|
+
* - both can be combined; bench tees them with an internal cost tracker
|
|
2780
|
+
*
|
|
2781
|
+
* Cost is tracked per-case in-process by intercepting `kind: 'cost'` events
|
|
2782
|
+
* on the trajectory pipeline. The cost number matches what would be on disk
|
|
2783
|
+
* even when no trajectory_dir is set.
|
|
2784
|
+
*
|
|
2785
|
+
* Judges run as Steps after each case, with the same trajectory logger so
|
|
2786
|
+
* judge spans land in the case's trajectory file (or push). A judge that
|
|
2787
|
+
* throws or returns undefined abstains: the entry is omitted from
|
|
2788
|
+
* `case.scores` and the case does not contribute to that judge's mean.
|
|
2789
|
+
*/
|
|
2790
|
+
async function bench(flow, cases, judges, options = {}) {
|
|
2791
|
+
const run_id = options.run_id ?? `bench-${String(Date.now())}-${random_suffix()}`;
|
|
2792
|
+
const flow_name = describe_flow_name(flow);
|
|
2793
|
+
const concurrency = resolve_concurrency(options.concurrency, cases.length);
|
|
2794
|
+
const install_signal_handlers = options.install_signal_handlers ?? false;
|
|
2795
|
+
if (options.trajectory_dir !== void 0) await mkdir(options.trajectory_dir, { recursive: true });
|
|
2796
|
+
const judge_entries = Object.entries(judges);
|
|
2797
|
+
const run_one = async (bc) => {
|
|
2798
|
+
const trajectory_path = options.trajectory_dir === void 0 ? void 0 : join(options.trajectory_dir, `${bc.id}.jsonl`);
|
|
2799
|
+
const sinks = [];
|
|
2800
|
+
if (trajectory_path !== void 0) sinks.push(filesystem_logger({ output_path: trajectory_path }));
|
|
2801
|
+
if (options.live_url !== void 0) sinks.push(http_logger({ url: options.live_url }));
|
|
2802
|
+
const cost_tracker = create_cost_tracker();
|
|
2803
|
+
const case_logger = sinks.length === 0 ? cost_tracker.logger : tee_logger(...sinks, cost_tracker.logger);
|
|
2804
|
+
const start = Date.now();
|
|
2805
|
+
let output;
|
|
2806
|
+
let error;
|
|
2807
|
+
let ok = false;
|
|
2808
|
+
try {
|
|
2809
|
+
output = await run(flow, bc.input, {
|
|
2810
|
+
trajectory: case_logger,
|
|
2811
|
+
install_signal_handlers
|
|
2812
|
+
});
|
|
2813
|
+
ok = true;
|
|
2814
|
+
} catch (err) {
|
|
2815
|
+
error = err instanceof Error ? err.message : String(err);
|
|
2816
|
+
ok = false;
|
|
2817
|
+
}
|
|
2818
|
+
const scores = {};
|
|
2819
|
+
if (ok && output !== void 0) {
|
|
2820
|
+
const judge_input = bc.meta === void 0 ? {
|
|
2821
|
+
input: bc.input,
|
|
2822
|
+
output
|
|
2823
|
+
} : {
|
|
2824
|
+
input: bc.input,
|
|
2825
|
+
output,
|
|
2826
|
+
meta: bc.meta
|
|
2827
|
+
};
|
|
2828
|
+
for (const [name, judge] of judge_entries) {
|
|
2829
|
+
let raw;
|
|
2830
|
+
try {
|
|
2831
|
+
raw = await run(judge, judge_input, {
|
|
2832
|
+
trajectory: case_logger,
|
|
2833
|
+
install_signal_handlers
|
|
2834
|
+
});
|
|
2835
|
+
} catch {
|
|
2836
|
+
continue;
|
|
2837
|
+
}
|
|
2838
|
+
const normalized = normalize_score(raw);
|
|
2839
|
+
if (normalized === void 0) continue;
|
|
2840
|
+
scores[name] = normalized;
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
const duration_ms = Date.now() - start;
|
|
2844
|
+
const result = {
|
|
2845
|
+
case_id: bc.id,
|
|
2846
|
+
ok,
|
|
2847
|
+
...output !== void 0 ? { output } : {},
|
|
2848
|
+
...error !== void 0 ? { error } : {},
|
|
2849
|
+
scores,
|
|
2850
|
+
duration_ms,
|
|
2851
|
+
cost_usd: cost_tracker.total(),
|
|
2852
|
+
...trajectory_path !== void 0 ? { trajectory_path } : {}
|
|
2853
|
+
};
|
|
2854
|
+
if (options.on_case) options.on_case(result);
|
|
2855
|
+
return result;
|
|
2856
|
+
};
|
|
2857
|
+
const cases_out = await run_with_concurrency(cases, concurrency, run_one);
|
|
2858
|
+
return {
|
|
2859
|
+
flow_name,
|
|
2860
|
+
run_id,
|
|
2861
|
+
cases: cases_out,
|
|
2862
|
+
summary: summarize(cases_out, judge_entries.map(([n]) => n))
|
|
2863
|
+
};
|
|
2864
|
+
}
|
|
2865
|
+
function summarize(cases, judge_names) {
|
|
2866
|
+
const total_duration_ms = cases.reduce((acc, c) => acc + c.duration_ms, 0);
|
|
2867
|
+
const total_cost_usd = cases.reduce((acc, c) => acc + c.cost_usd, 0);
|
|
2868
|
+
const ok_count = cases.filter((c) => c.ok).length;
|
|
2869
|
+
const pass_rate = cases.length === 0 ? 0 : ok_count / cases.length;
|
|
2870
|
+
const mean_cost_usd = cases.length === 0 ? 0 : total_cost_usd / cases.length;
|
|
2871
|
+
const mean_scores = {};
|
|
2872
|
+
for (const name of judge_names) {
|
|
2873
|
+
let sum = 0;
|
|
2874
|
+
let count = 0;
|
|
2875
|
+
for (const c of cases) {
|
|
2876
|
+
const n = score_value_of(c.scores[name]);
|
|
2877
|
+
if (n === void 0) continue;
|
|
2878
|
+
sum += n;
|
|
2879
|
+
count += 1;
|
|
2880
|
+
}
|
|
2881
|
+
if (count > 0) mean_scores[name] = sum / count;
|
|
2882
|
+
}
|
|
2883
|
+
return {
|
|
2884
|
+
pass_rate,
|
|
2885
|
+
mean_scores,
|
|
2886
|
+
total_duration_ms,
|
|
2887
|
+
total_cost_usd,
|
|
2888
|
+
mean_cost_usd
|
|
2889
|
+
};
|
|
2890
|
+
}
|
|
2891
|
+
function normalize_score(raw) {
|
|
2892
|
+
if (raw === void 0 || raw === null) return void 0;
|
|
2893
|
+
if (typeof raw === "number") return Number.isFinite(raw) ? { score: raw } : void 0;
|
|
2894
|
+
if (typeof raw === "object" && "score" in raw) {
|
|
2895
|
+
const r = raw;
|
|
2896
|
+
if (typeof r.score !== "number" || !Number.isFinite(r.score)) return void 0;
|
|
2897
|
+
if (r.reason !== void 0 && typeof r.reason !== "string") return { score: r.score };
|
|
2898
|
+
return r.reason === void 0 ? { score: r.score } : {
|
|
2899
|
+
score: r.score,
|
|
2900
|
+
reason: r.reason
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2903
|
+
}
|
|
2904
|
+
function score_value_of(s) {
|
|
2905
|
+
if (s === void 0 || s === null) return void 0;
|
|
2906
|
+
if (typeof s === "number") return Number.isFinite(s) ? s : void 0;
|
|
2907
|
+
if (typeof s === "object" && "score" in s) {
|
|
2908
|
+
const raw = s["score"];
|
|
2909
|
+
return typeof raw === "number" && Number.isFinite(raw) ? raw : void 0;
|
|
2910
|
+
}
|
|
2911
|
+
}
|
|
2912
|
+
function create_cost_tracker() {
|
|
2913
|
+
let total = 0;
|
|
2914
|
+
return {
|
|
2915
|
+
total: () => total,
|
|
2916
|
+
logger: {
|
|
2917
|
+
record: (event) => {
|
|
2918
|
+
if (event.kind !== "cost") return;
|
|
2919
|
+
const value = event["total_usd"];
|
|
2920
|
+
if (typeof value === "number" && Number.isFinite(value)) total += value;
|
|
2921
|
+
},
|
|
2922
|
+
start_span: (name) => `cost-tracker:${name}`,
|
|
2923
|
+
end_span: () => {}
|
|
2924
|
+
}
|
|
2925
|
+
};
|
|
2926
|
+
}
|
|
2927
|
+
async function run_with_concurrency(items, limit, fn) {
|
|
2928
|
+
if (items.length === 0) return [];
|
|
2929
|
+
if (limit >= items.length) return Promise.all(items.map((it) => fn(it)));
|
|
2930
|
+
const results = Array.from({ length: items.length });
|
|
2931
|
+
let next = 0;
|
|
2932
|
+
const worker = async () => {
|
|
2933
|
+
while (true) {
|
|
2934
|
+
const idx = next;
|
|
2935
|
+
next += 1;
|
|
2936
|
+
if (idx >= items.length) return;
|
|
2937
|
+
const item = items[idx];
|
|
2938
|
+
if (item === void 0) continue;
|
|
2939
|
+
results[idx] = await fn(item);
|
|
2940
|
+
}
|
|
2941
|
+
};
|
|
2942
|
+
const workers = [];
|
|
2943
|
+
for (let i = 0; i < limit; i += 1) workers.push(worker());
|
|
2944
|
+
await Promise.all(workers);
|
|
2945
|
+
return results;
|
|
2946
|
+
}
|
|
2947
|
+
function resolve_concurrency(value, n_cases) {
|
|
2948
|
+
if (value === void 0) return Math.max(1, n_cases);
|
|
2949
|
+
if (!Number.isFinite(value) || value <= 0) return Math.max(1, n_cases);
|
|
2950
|
+
return Math.max(1, Math.floor(value));
|
|
2951
|
+
}
|
|
2952
|
+
function describe_flow_name(flow) {
|
|
2953
|
+
const display = flow.config?.["display_name"];
|
|
2954
|
+
if (typeof display === "string" && display.length > 0) return display;
|
|
2955
|
+
return flow.id ?? flow.kind ?? "flow";
|
|
2956
|
+
}
|
|
2957
|
+
function random_suffix() {
|
|
2958
|
+
return Math.random().toString(36).slice(2, 8);
|
|
2959
|
+
}
|
|
2960
|
+
//#endregion
|
|
2961
|
+
//#region packages/composites/src/judges.ts
|
|
2962
|
+
/**
|
|
2963
|
+
* Stock judges for `bench`.
|
|
2964
|
+
*
|
|
2965
|
+
* A `Judge<I, O, S>` is a `Step<{ input, output, meta }, S>` — i.e. just a
|
|
2966
|
+
* step that scores a flow's input/output/metadata triple. Returning
|
|
2967
|
+
* `undefined` (or throwing) means the judge abstains; bench records the
|
|
2968
|
+
* absence as a missing key in `case.scores` and excludes the case from that
|
|
2969
|
+
* judge's `mean_scores` denominator.
|
|
2970
|
+
*
|
|
2971
|
+
* judge_equals — strict equality vs. `meta.expected`
|
|
2972
|
+
* judge_with — wrap a user-supplied scoring function
|
|
2973
|
+
* judge_llm — prompt a model + parse the rubric score out of the reply
|
|
2974
|
+
*
|
|
2975
|
+
* `judge_llm` is engine-agnostic: callers pass their already-configured
|
|
2976
|
+
* model_call step (Step<string, string>), which keeps composites decoupled
|
|
2977
|
+
* from @repo/engine. The user-facing wiring lives in their own code.
|
|
2978
|
+
*/
|
|
2979
|
+
function is_record(v) {
|
|
2980
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
2981
|
+
}
|
|
2982
|
+
function deep_equal(a, b) {
|
|
2983
|
+
if (a === b) return true;
|
|
2984
|
+
if (a === null || b === null) return false;
|
|
2985
|
+
if (typeof a !== typeof b) return false;
|
|
2986
|
+
if (typeof a !== "object") return false;
|
|
2987
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
2988
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
2989
|
+
if (a.length !== b.length) return false;
|
|
2990
|
+
for (let i = 0; i < a.length; i += 1) if (!deep_equal(a[i], b[i])) return false;
|
|
2991
|
+
return true;
|
|
2992
|
+
}
|
|
2993
|
+
if (!is_record(a) || !is_record(b)) return false;
|
|
2994
|
+
const ak = Object.keys(a).toSorted();
|
|
2995
|
+
const bk = Object.keys(b).toSorted();
|
|
2996
|
+
if (ak.length !== bk.length) return false;
|
|
2997
|
+
for (let i = 0; i < ak.length; i += 1) {
|
|
2998
|
+
const ka = ak[i];
|
|
2999
|
+
const kb = bk[i];
|
|
3000
|
+
if (ka === void 0 || kb === void 0 || ka !== kb) return false;
|
|
3001
|
+
if (!deep_equal(a[ka], b[kb])) return false;
|
|
3002
|
+
}
|
|
3003
|
+
return true;
|
|
3004
|
+
}
|
|
3005
|
+
function judge_equals() {
|
|
3006
|
+
return step("judge_equals", (args) => {
|
|
3007
|
+
if (args.meta === void 0) return void 0;
|
|
3008
|
+
if (!("expected" in args.meta)) return void 0;
|
|
3009
|
+
const expected = args.meta["expected"];
|
|
3010
|
+
const matches = deep_equal(args.output, expected);
|
|
3011
|
+
return {
|
|
3012
|
+
score: matches ? 1 : 0,
|
|
3013
|
+
reason: matches ? "match" : "mismatch"
|
|
3014
|
+
};
|
|
3015
|
+
});
|
|
3016
|
+
}
|
|
3017
|
+
function judge_with(fn) {
|
|
3018
|
+
return step("judge_with", async (args) => {
|
|
3019
|
+
return normalize_score(await fn(args));
|
|
3020
|
+
});
|
|
3021
|
+
}
|
|
3022
|
+
function judge_llm(config) {
|
|
3023
|
+
const scale = config.scale ?? {
|
|
3024
|
+
min: 0,
|
|
3025
|
+
max: 1
|
|
3026
|
+
};
|
|
3027
|
+
const build_prompt = step("judge_llm_build_prompt", (args) => render_prompt(config.rubric, scale, args));
|
|
3028
|
+
const parse = step("judge_llm_parse", (reply) => parse_score(reply, scale));
|
|
3029
|
+
return compose("judge_llm", sequence([
|
|
3030
|
+
build_prompt,
|
|
3031
|
+
config.model,
|
|
3032
|
+
parse
|
|
3033
|
+
]));
|
|
3034
|
+
}
|
|
3035
|
+
function render_prompt(rubric, scale, args) {
|
|
3036
|
+
return [
|
|
3037
|
+
`You are an evaluator. Score the model output against the rubric below.`,
|
|
3038
|
+
``,
|
|
3039
|
+
`Rubric:`,
|
|
3040
|
+
rubric,
|
|
3041
|
+
``,
|
|
3042
|
+
`Return a single line of JSON with exactly two keys: {"score": <number ${String(scale.min)}..${String(scale.max)}>, "reason": "<short>"}.`,
|
|
3043
|
+
`Do not include prose outside the JSON.`,
|
|
3044
|
+
``,
|
|
3045
|
+
`Input:`,
|
|
3046
|
+
JSON.stringify(args.input),
|
|
3047
|
+
``,
|
|
3048
|
+
`Output:`,
|
|
3049
|
+
JSON.stringify(args.output)
|
|
3050
|
+
].join("\n");
|
|
3051
|
+
}
|
|
3052
|
+
function parse_score(reply, scale) {
|
|
3053
|
+
const candidate = extract_json_object(reply.trim());
|
|
3054
|
+
if (candidate === void 0) return void 0;
|
|
3055
|
+
let parsed;
|
|
3056
|
+
try {
|
|
3057
|
+
parsed = JSON.parse(candidate);
|
|
3058
|
+
} catch {
|
|
3059
|
+
return;
|
|
3060
|
+
}
|
|
3061
|
+
if (!is_record(parsed)) return void 0;
|
|
3062
|
+
const raw_score = parsed["score"];
|
|
3063
|
+
if (typeof raw_score !== "number" || !Number.isFinite(raw_score)) return void 0;
|
|
3064
|
+
const clamped = Math.max(scale.min, Math.min(scale.max, raw_score));
|
|
3065
|
+
const raw_reason = parsed["reason"];
|
|
3066
|
+
const reason = typeof raw_reason === "string" ? raw_reason : void 0;
|
|
3067
|
+
return reason === void 0 ? { score: clamped } : {
|
|
3068
|
+
score: clamped,
|
|
3069
|
+
reason
|
|
3070
|
+
};
|
|
3071
|
+
}
|
|
3072
|
+
function extract_json_object(text) {
|
|
3073
|
+
if (text.startsWith("{")) return text;
|
|
3074
|
+
const start = text.indexOf("{");
|
|
3075
|
+
const end = text.lastIndexOf("}");
|
|
3076
|
+
if (start === -1 || end === -1 || end < start) return void 0;
|
|
3077
|
+
return text.slice(start, end + 1);
|
|
3078
|
+
}
|
|
3079
|
+
//#endregion
|
|
3080
|
+
//#region packages/composites/src/regression.ts
|
|
3081
|
+
/**
|
|
3082
|
+
* regression_compare: diff two BenchReports for regressions.
|
|
3083
|
+
*
|
|
3084
|
+
* The contract: given a `current` report and a `baseline` report, produce a
|
|
3085
|
+
* `RegressionReport` whose `ok` flag is false when any tracked metric got
|
|
3086
|
+
* worse beyond a threshold. The function does NOT short-circuit on first
|
|
3087
|
+
* failure — every metric and every per-case delta is computed so callers can
|
|
3088
|
+
* print or assert against the whole report.
|
|
3089
|
+
*
|
|
3090
|
+
* Tracked metrics:
|
|
3091
|
+
* - pass_rate (any drop is a regression by default)
|
|
3092
|
+
* - mean_scores.<name> (per judge; any drop is a regression by default)
|
|
3093
|
+
* - total_cost_usd (a relative cost increase above cost_threshold
|
|
3094
|
+
* counts as a regression; default 10%)
|
|
3095
|
+
*
|
|
3096
|
+
* Baselines are plain JSON, written via `write_baseline` and read via
|
|
3097
|
+
* `read_baseline`. They live wherever the caller decides — typically
|
|
3098
|
+
* `bench/<flow>/baseline.json`, checked into git per spec/eval.md §3.
|
|
3099
|
+
*/
|
|
3100
|
+
function regression_compare(current, baseline, options = {}) {
|
|
3101
|
+
const score_threshold = options.score_threshold ?? 0;
|
|
3102
|
+
const cost_threshold = options.cost_threshold ?? .1;
|
|
3103
|
+
const deltas = [];
|
|
3104
|
+
const pass_delta = current.summary.pass_rate - baseline.summary.pass_rate;
|
|
3105
|
+
deltas.push({
|
|
3106
|
+
metric: "pass_rate",
|
|
3107
|
+
baseline: baseline.summary.pass_rate,
|
|
3108
|
+
current: current.summary.pass_rate,
|
|
3109
|
+
delta: pass_delta,
|
|
3110
|
+
is_regression: pass_delta < -score_threshold
|
|
3111
|
+
});
|
|
3112
|
+
const judge_names = collect_judge_names(current, baseline);
|
|
3113
|
+
for (const name of judge_names) {
|
|
3114
|
+
const c = current.summary.mean_scores[name] ?? 0;
|
|
3115
|
+
const b = baseline.summary.mean_scores[name] ?? 0;
|
|
3116
|
+
const d = c - b;
|
|
3117
|
+
deltas.push({
|
|
3118
|
+
metric: `mean_scores.${name}`,
|
|
3119
|
+
baseline: b,
|
|
3120
|
+
current: c,
|
|
3121
|
+
delta: d,
|
|
3122
|
+
is_regression: d < -score_threshold
|
|
3123
|
+
});
|
|
3124
|
+
}
|
|
3125
|
+
const cb = baseline.summary.total_cost_usd;
|
|
3126
|
+
const cc = current.summary.total_cost_usd;
|
|
3127
|
+
const cost_delta = cc - cb;
|
|
3128
|
+
const cost_ratio = cb === 0 ? cc > 0 ? Infinity : 0 : cost_delta / cb;
|
|
3129
|
+
deltas.push({
|
|
3130
|
+
metric: "total_cost_usd",
|
|
3131
|
+
baseline: cb,
|
|
3132
|
+
current: cc,
|
|
3133
|
+
delta: cost_delta,
|
|
3134
|
+
is_regression: cost_ratio > cost_threshold
|
|
3135
|
+
});
|
|
3136
|
+
const per_case = compute_per_case(current, baseline, score_threshold, cost_threshold);
|
|
3137
|
+
return {
|
|
3138
|
+
ok: !deltas.some((d) => d.is_regression) && !per_case.some((c) => c.is_regression),
|
|
3139
|
+
deltas,
|
|
3140
|
+
per_case
|
|
3141
|
+
};
|
|
3142
|
+
}
|
|
3143
|
+
function compute_per_case(current, baseline, score_threshold, cost_threshold) {
|
|
3144
|
+
const by_id_current = index_by_case_id(current.cases);
|
|
3145
|
+
const by_id_baseline = index_by_case_id(baseline.cases);
|
|
3146
|
+
const all_ids = new Set([...by_id_current.keys(), ...by_id_baseline.keys()]);
|
|
3147
|
+
const out = [];
|
|
3148
|
+
for (const id of all_ids) {
|
|
3149
|
+
const c = by_id_current.get(id);
|
|
3150
|
+
const b = by_id_baseline.get(id);
|
|
3151
|
+
const score_deltas = {};
|
|
3152
|
+
let any_score_regression = false;
|
|
3153
|
+
const judge_names = new Set([...Object.keys(c?.scores ?? {}), ...Object.keys(b?.scores ?? {})]);
|
|
3154
|
+
for (const name of judge_names) {
|
|
3155
|
+
const delta = score_value(c?.scores[name]) - score_value(b?.scores[name]);
|
|
3156
|
+
score_deltas[name] = delta;
|
|
3157
|
+
if (delta < -score_threshold) any_score_regression = true;
|
|
3158
|
+
}
|
|
3159
|
+
const cost_delta = (c?.cost_usd ?? 0) - (b?.cost_usd ?? 0);
|
|
3160
|
+
const cost_ratio_regression = b?.cost_usd !== void 0 && b.cost_usd > 0 ? cost_delta / b.cost_usd > cost_threshold : (c?.cost_usd ?? 0) > 0 && (b?.cost_usd ?? 0) === 0 && cost_threshold < Infinity;
|
|
3161
|
+
const ok_regression = b?.ok === true && c?.ok !== true;
|
|
3162
|
+
out.push({
|
|
3163
|
+
case_id: id,
|
|
3164
|
+
...b !== void 0 ? { baseline: b } : {},
|
|
3165
|
+
...c !== void 0 ? { current: c } : {},
|
|
3166
|
+
score_deltas,
|
|
3167
|
+
cost_delta,
|
|
3168
|
+
is_regression: any_score_regression || cost_ratio_regression || ok_regression
|
|
3169
|
+
});
|
|
3170
|
+
}
|
|
3171
|
+
return out;
|
|
3172
|
+
}
|
|
3173
|
+
function score_value(s) {
|
|
3174
|
+
if (s === void 0 || s === null) return 0;
|
|
3175
|
+
if (typeof s === "number") return Number.isFinite(s) ? s : 0;
|
|
3176
|
+
if (typeof s === "object" && "score" in s) {
|
|
3177
|
+
const v = s["score"];
|
|
3178
|
+
return typeof v === "number" && Number.isFinite(v) ? v : 0;
|
|
3179
|
+
}
|
|
3180
|
+
return 0;
|
|
3181
|
+
}
|
|
3182
|
+
function index_by_case_id(cases) {
|
|
3183
|
+
const m = /* @__PURE__ */ new Map();
|
|
3184
|
+
for (const c of cases) m.set(c.case_id, c);
|
|
3185
|
+
return m;
|
|
3186
|
+
}
|
|
3187
|
+
function collect_judge_names(a, b) {
|
|
3188
|
+
return [...new Set([...Object.keys(a.summary.mean_scores), ...Object.keys(b.summary.mean_scores)])].toSorted();
|
|
3189
|
+
}
|
|
3190
|
+
async function read_baseline(path) {
|
|
3191
|
+
const text = await readFile(path, "utf8");
|
|
3192
|
+
const parsed = JSON.parse(text);
|
|
3193
|
+
validate_report(parsed, path);
|
|
3194
|
+
return parsed;
|
|
3195
|
+
}
|
|
3196
|
+
async function write_baseline(path, report) {
|
|
3197
|
+
await mkdir(dirname(path), { recursive: true });
|
|
3198
|
+
await writeFile(path, `${JSON.stringify(report, null, 2)}\n`, "utf8");
|
|
3199
|
+
}
|
|
3200
|
+
function validate_report(value, path) {
|
|
3201
|
+
if (value === null || typeof value !== "object") throw new Error(`baseline at ${path} is not an object`);
|
|
3202
|
+
const v = value;
|
|
3203
|
+
if (typeof v.flow_name !== "string" || typeof v.run_id !== "string") throw new Error(`baseline at ${path} missing flow_name/run_id`);
|
|
3204
|
+
if (!Array.isArray(v.cases) || typeof v.summary !== "object" || v.summary === null) throw new Error(`baseline at ${path} missing cases/summary`);
|
|
3205
|
+
}
|
|
3206
|
+
//#endregion
|
|
2595
3207
|
//#region packages/engine/src/version.ts
|
|
2596
|
-
const version$1 = "0.
|
|
3208
|
+
const version$1 = "0.4.0";
|
|
2597
3209
|
//#endregion
|
|
2598
3210
|
//#region packages/engine/src/errors.ts
|
|
2599
3211
|
var rate_limit_error = class extends Error {
|
|
@@ -3442,17 +4054,38 @@ const DEFAULT_SETTING_SOURCES = Object.freeze(["project", "local"]);
|
|
|
3442
4054
|
* Authentication enforcement for the claude_cli adapter (spec §4, §6.1).
|
|
3443
4055
|
*
|
|
3444
4056
|
* - build_env composes the subprocess env from config + caller overrides.
|
|
3445
|
-
*
|
|
3446
|
-
*
|
|
3447
|
-
*
|
|
3448
|
-
*
|
|
3449
|
-
*
|
|
3450
|
-
*
|
|
4057
|
+
* Regardless of auth_mode, it seeds the standard process-env keys
|
|
4058
|
+
* (PATH, HOME, SHELL, USER, LOGNAME, LANG, TMPDIR) so the spawned
|
|
4059
|
+
* subprocess can locate the sandbox wrapper (greywall/bwrap) and the
|
|
4060
|
+
* claude binary on PATH. Set `inherit_env: false` to opt out and start
|
|
4061
|
+
* from an empty env. Under oauth mode the *full* process.env is also
|
|
4062
|
+
* inherited (the CLI needs HOME/etc plus session-specific vars to reach
|
|
4063
|
+
* the logged-in OAuth session). Auth-mode scrub runs last on the merged
|
|
4064
|
+
* result so nothing — caller env, inherited env, or config — can
|
|
4065
|
+
* re-introduce ANTHROPIC_API_KEY under 'oauth'.
|
|
3451
4066
|
* - validate_auth_config throws engine_config_error synchronously when
|
|
3452
4067
|
* auth_mode === 'api_key' without a non-empty api_key.
|
|
3453
4068
|
* - stderr_is_auth_failure matches captured stderr against the frozen
|
|
3454
4069
|
* CLI_AUTH_ERROR_PATTERNS list, case-insensitively.
|
|
3455
4070
|
*/
|
|
4071
|
+
/**
|
|
4072
|
+
* Standard env keys forwarded from process.env so the spawn target
|
|
4073
|
+
* (greywall/bwrap on PATH, then `claude`) can be resolved and run.
|
|
4074
|
+
*
|
|
4075
|
+
* Mirrors `STANDARD_KEYS` in
|
|
4076
|
+
* packages/fascicle/src/forward_standard_env.ts. Keep the two lists in
|
|
4077
|
+
* sync; engine cannot import from fascicle (the umbrella depends on
|
|
4078
|
+
* engine, not the other way).
|
|
4079
|
+
*/
|
|
4080
|
+
const STANDARD_ENV_KEYS = [
|
|
4081
|
+
"PATH",
|
|
4082
|
+
"HOME",
|
|
4083
|
+
"SHELL",
|
|
4084
|
+
"USER",
|
|
4085
|
+
"LOGNAME",
|
|
4086
|
+
"LANG",
|
|
4087
|
+
"TMPDIR"
|
|
4088
|
+
];
|
|
3456
4089
|
function validate_auth_config(config) {
|
|
3457
4090
|
if ((config.auth_mode ?? "auto") === "api_key") {
|
|
3458
4091
|
if ((typeof config.api_key === "string" ? config.api_key : "").length === 0) throw new engine_config_error("api_key is required for auth_mode: api_key", "claude_cli");
|
|
@@ -3460,8 +4093,11 @@ function validate_auth_config(config) {
|
|
|
3460
4093
|
}
|
|
3461
4094
|
function build_env(config, caller_env, auth_mode) {
|
|
3462
4095
|
const env = {};
|
|
3463
|
-
if (
|
|
4096
|
+
if (config.inherit_env !== false) if (auth_mode === "oauth") {
|
|
3464
4097
|
for (const [k, v] of Object.entries(process.env)) if (typeof v === "string") env[k] = v;
|
|
4098
|
+
} else for (const key of STANDARD_ENV_KEYS) {
|
|
4099
|
+
const v = process.env[key];
|
|
4100
|
+
if (typeof v === "string") env[key] = v;
|
|
3465
4101
|
}
|
|
3466
4102
|
if (caller_env !== void 0) {
|
|
3467
4103
|
for (const [k, v] of Object.entries(caller_env)) if (typeof v === "string") env[k] = v;
|
|
@@ -3524,6 +4160,31 @@ function merge_allowed_tools(call_allowlist, tool_names) {
|
|
|
3524
4160
|
}
|
|
3525
4161
|
//#endregion
|
|
3526
4162
|
//#region packages/engine/src/providers/claude_cli/sandbox.ts
|
|
4163
|
+
/**
|
|
4164
|
+
* Sandbox wrapper for the claude_cli adapter (spec §5.1, F30).
|
|
4165
|
+
*
|
|
4166
|
+
* Builds the `{ spawn_cmd, prefix_args }` pair for bwrap and greywall. When no
|
|
4167
|
+
* sandbox is configured, returns { spawn_cmd: binary, prefix_args: [] } — the
|
|
4168
|
+
* caller then concatenates prefix_args before the CLI argv.
|
|
4169
|
+
*
|
|
4170
|
+
* Both wrappers allow the binary to execute while restricting network reach
|
|
4171
|
+
* to the allowlist and write access to additional paths on top of the
|
|
4172
|
+
* sandbox's default-read-only filesystem.
|
|
4173
|
+
*
|
|
4174
|
+
* greywall 0.3+ removed the `--allow-host` and `--rw` flags; policy now
|
|
4175
|
+
* lives in a JSON settings file passed via `--settings <path>`. Schema is
|
|
4176
|
+
* documented in `greywall --help`:
|
|
4177
|
+
*
|
|
4178
|
+
* {
|
|
4179
|
+
* "network": { "allowHosts": [...] },
|
|
4180
|
+
* "filesystem": { "allowWrite": [...] }
|
|
4181
|
+
* }
|
|
4182
|
+
*
|
|
4183
|
+
* `filesystem.allowWrite` is honored directly; `network.allowHosts` is
|
|
4184
|
+
* preserved for documentation/forward-compat (greywall ignores unknown
|
|
4185
|
+
* fields and host-level allowlisting is currently enforced at the
|
|
4186
|
+
* greyproxy layer).
|
|
4187
|
+
*/
|
|
3527
4188
|
function build_sandbox_plan(binary, sandbox) {
|
|
3528
4189
|
if (sandbox === void 0) return {
|
|
3529
4190
|
spawn_cmd: binary,
|
|
@@ -3566,14 +4227,21 @@ function build_bwrap_args(binary, sandbox) {
|
|
|
3566
4227
|
return args;
|
|
3567
4228
|
}
|
|
3568
4229
|
function build_greywall_args(binary, sandbox) {
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
|
|
4230
|
+
return [
|
|
4231
|
+
"--settings",
|
|
4232
|
+
sandbox.settings_path ?? write_greywall_settings(sandbox),
|
|
4233
|
+
"--",
|
|
4234
|
+
binary
|
|
4235
|
+
];
|
|
4236
|
+
}
|
|
4237
|
+
function write_greywall_settings(sandbox) {
|
|
4238
|
+
const path = join(mkdtempSync(join(tmpdir(), `fascicle-greywall-${process.pid}-`)), "greywall.json");
|
|
4239
|
+
const payload = {
|
|
4240
|
+
network: { allowHosts: [...sandbox.network_allowlist ?? []] },
|
|
4241
|
+
filesystem: { allowWrite: [...sandbox.additional_write_paths ?? []] }
|
|
4242
|
+
};
|
|
4243
|
+
writeFileSync(path, JSON.stringify(payload, null, 2), "utf8");
|
|
4244
|
+
return path;
|
|
3577
4245
|
}
|
|
3578
4246
|
//#endregion
|
|
3579
4247
|
//#region packages/engine/src/providers/claude_cli/spawn.ts
|
|
@@ -6093,6 +6761,544 @@ function forward_standard_env() {
|
|
|
6093
6761
|
return out;
|
|
6094
6762
|
}
|
|
6095
6763
|
//#endregion
|
|
6096
|
-
|
|
6764
|
+
//#region packages/viewer/src/broadcast.ts
|
|
6765
|
+
function create_broadcaster(options) {
|
|
6766
|
+
const max = Math.max(1, options.buffer | 0);
|
|
6767
|
+
const ring = [];
|
|
6768
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
6769
|
+
let next_id = 1;
|
|
6770
|
+
const emit = (event) => {
|
|
6771
|
+
const entry = {
|
|
6772
|
+
id: next_id++,
|
|
6773
|
+
event
|
|
6774
|
+
};
|
|
6775
|
+
ring.push(entry);
|
|
6776
|
+
if (ring.length > max) ring.splice(0, ring.length - max);
|
|
6777
|
+
for (const fn of Array.from(subscribers)) try {
|
|
6778
|
+
fn(entry);
|
|
6779
|
+
} catch (err) {
|
|
6780
|
+
subscribers.delete(fn);
|
|
6781
|
+
if (options.on_subscriber_error) options.on_subscriber_error(err);
|
|
6782
|
+
}
|
|
6783
|
+
return entry;
|
|
6784
|
+
};
|
|
6785
|
+
const subscribe = (fn) => {
|
|
6786
|
+
subscribers.add(fn);
|
|
6787
|
+
return () => {
|
|
6788
|
+
subscribers.delete(fn);
|
|
6789
|
+
};
|
|
6790
|
+
};
|
|
6791
|
+
const snapshot = () => [...ring];
|
|
6792
|
+
const snapshot_after = (last_id) => ring.filter((e) => e.id > last_id);
|
|
6793
|
+
const size = () => ring.length;
|
|
6794
|
+
return {
|
|
6795
|
+
emit,
|
|
6796
|
+
subscribe,
|
|
6797
|
+
snapshot,
|
|
6798
|
+
snapshot_after,
|
|
6799
|
+
size
|
|
6800
|
+
};
|
|
6801
|
+
}
|
|
6802
|
+
//#endregion
|
|
6803
|
+
//#region packages/viewer/src/server.ts
|
|
6804
|
+
/**
|
|
6805
|
+
* Tiny HTTP server: 5 routes, SSE fan-out, NDJSON ingest.
|
|
6806
|
+
*
|
|
6807
|
+
* GET / -> static viewer.html
|
|
6808
|
+
* GET /api/events -> text/event-stream replaying the ring buffer
|
|
6809
|
+
* GET /api/snapshot -> JSON dump of the ring buffer
|
|
6810
|
+
* POST /api/ingest -> newline-delimited trajectory events (http_logger)
|
|
6811
|
+
* GET /api/health -> { ok: true }
|
|
6812
|
+
*
|
|
6813
|
+
* The server owns nothing but the socket. The broadcaster owns the event
|
|
6814
|
+
* history; the static html lives on disk. SSE clients reconnect with
|
|
6815
|
+
* `Last-Event-ID` and the server replays anything past their cursor from
|
|
6816
|
+
* the ring buffer; older events are gone (bounded memory by design).
|
|
6817
|
+
*/
|
|
6818
|
+
const SSE_HEARTBEAT_MS = 15e3;
|
|
6819
|
+
const STATIC_HTML = resolve(dirname(fileURLToPath(import.meta.url)), "static", "viewer.html");
|
|
6820
|
+
function start_server(options) {
|
|
6821
|
+
const { broadcaster, host, port, on_parse_error } = options;
|
|
6822
|
+
const handler = (req, res) => {
|
|
6823
|
+
const url = new URL(req.url ?? "/", `http://${req.headers.host ?? `${host}:${port}`}`);
|
|
6824
|
+
const route = `${req.method ?? "GET"} ${url.pathname}`;
|
|
6825
|
+
if (route === "GET /" || route === "GET /index.html") {
|
|
6826
|
+
serve_static_html(res);
|
|
6827
|
+
return;
|
|
6828
|
+
}
|
|
6829
|
+
if (route === "GET /api/health") {
|
|
6830
|
+
send_json(res, 200, { ok: true });
|
|
6831
|
+
return;
|
|
6832
|
+
}
|
|
6833
|
+
if (route === "GET /api/snapshot") {
|
|
6834
|
+
send_json(res, 200, { events: broadcaster.snapshot() });
|
|
6835
|
+
return;
|
|
6836
|
+
}
|
|
6837
|
+
if (route === "GET /api/events") {
|
|
6838
|
+
handle_sse(req, res, broadcaster);
|
|
6839
|
+
return;
|
|
6840
|
+
}
|
|
6841
|
+
if (route === "POST /api/ingest") {
|
|
6842
|
+
handle_ingest(req, res, broadcaster, on_parse_error);
|
|
6843
|
+
return;
|
|
6844
|
+
}
|
|
6845
|
+
res.statusCode = 404;
|
|
6846
|
+
res.setHeader("content-type", "application/json");
|
|
6847
|
+
res.end(JSON.stringify({
|
|
6848
|
+
error: "not_found",
|
|
6849
|
+
route
|
|
6850
|
+
}));
|
|
6851
|
+
};
|
|
6852
|
+
const http_server = createServer(handler);
|
|
6853
|
+
return new Promise((res_ok, res_err) => {
|
|
6854
|
+
http_server.once("error", res_err);
|
|
6855
|
+
http_server.listen(port, host, () => {
|
|
6856
|
+
http_server.removeListener("error", res_err);
|
|
6857
|
+
const addr = http_server.address();
|
|
6858
|
+
res_ok({
|
|
6859
|
+
url: `http://${host}:${addr !== null && typeof addr === "object" && "port" in addr ? addr.port : port}`,
|
|
6860
|
+
close: () => close_server(http_server)
|
|
6861
|
+
});
|
|
6862
|
+
});
|
|
6863
|
+
});
|
|
6864
|
+
}
|
|
6865
|
+
function close_server(server) {
|
|
6866
|
+
return new Promise((res_ok, res_err) => {
|
|
6867
|
+
server.close((err) => {
|
|
6868
|
+
if (err) res_err(err);
|
|
6869
|
+
else res_ok();
|
|
6870
|
+
});
|
|
6871
|
+
server.closeAllConnections?.();
|
|
6872
|
+
});
|
|
6873
|
+
}
|
|
6874
|
+
function send_json(res, status, body) {
|
|
6875
|
+
res.statusCode = status;
|
|
6876
|
+
res.setHeader("content-type", "application/json");
|
|
6877
|
+
res.end(JSON.stringify(body));
|
|
6878
|
+
}
|
|
6879
|
+
function serve_static_html(res) {
|
|
6880
|
+
let size;
|
|
6881
|
+
try {
|
|
6882
|
+
size = statSync(STATIC_HTML).size;
|
|
6883
|
+
} catch {
|
|
6884
|
+
res.statusCode = 500;
|
|
6885
|
+
res.setHeader("content-type", "text/plain");
|
|
6886
|
+
res.end("viewer.html missing");
|
|
6887
|
+
return;
|
|
6888
|
+
}
|
|
6889
|
+
res.statusCode = 200;
|
|
6890
|
+
res.setHeader("content-type", "text/html; charset=utf-8");
|
|
6891
|
+
res.setHeader("cache-control", "no-store");
|
|
6892
|
+
res.setHeader("content-length", String(size));
|
|
6893
|
+
createReadStream(STATIC_HTML).pipe(res);
|
|
6894
|
+
}
|
|
6895
|
+
function handle_sse(req, res, broadcaster) {
|
|
6896
|
+
res.statusCode = 200;
|
|
6897
|
+
res.setHeader("content-type", "text/event-stream");
|
|
6898
|
+
res.setHeader("cache-control", "no-store");
|
|
6899
|
+
res.setHeader("connection", "keep-alive");
|
|
6900
|
+
res.setHeader("x-accel-buffering", "no");
|
|
6901
|
+
const last_event_id = parse_last_event_id(req.headers["last-event-id"]);
|
|
6902
|
+
for (const entry of broadcaster.snapshot_after(last_event_id)) write_event(res, entry.id, entry.event);
|
|
6903
|
+
const heartbeat = setInterval(() => {
|
|
6904
|
+
res.write(": heartbeat\n\n");
|
|
6905
|
+
}, SSE_HEARTBEAT_MS);
|
|
6906
|
+
const unsubscribe = broadcaster.subscribe((entry) => {
|
|
6907
|
+
write_event(res, entry.id, entry.event);
|
|
6908
|
+
});
|
|
6909
|
+
const close = () => {
|
|
6910
|
+
clearInterval(heartbeat);
|
|
6911
|
+
unsubscribe();
|
|
6912
|
+
if (!res.writableEnded) {
|
|
6913
|
+
res.write("event: close\ndata: {}\n\n");
|
|
6914
|
+
res.end();
|
|
6915
|
+
}
|
|
6916
|
+
};
|
|
6917
|
+
req.on("close", close);
|
|
6918
|
+
req.on("error", close);
|
|
6919
|
+
}
|
|
6920
|
+
function parse_last_event_id(value) {
|
|
6921
|
+
if (typeof value !== "string") return 0;
|
|
6922
|
+
const n = Number.parseInt(value, 10);
|
|
6923
|
+
return Number.isFinite(n) && n > 0 ? n : 0;
|
|
6924
|
+
}
|
|
6925
|
+
function write_event(res, id, event) {
|
|
6926
|
+
res.write(`id: ${id}\nevent: trajectory\ndata: ${JSON.stringify(event)}\n\n`);
|
|
6927
|
+
}
|
|
6928
|
+
function handle_ingest(req, res, broadcaster, on_parse_error) {
|
|
6929
|
+
let buf = "";
|
|
6930
|
+
let accepted = 0;
|
|
6931
|
+
let rejected = 0;
|
|
6932
|
+
const consume_line = (line) => {
|
|
6933
|
+
if (line.length === 0) return;
|
|
6934
|
+
let parsed;
|
|
6935
|
+
try {
|
|
6936
|
+
parsed = JSON.parse(line);
|
|
6937
|
+
} catch (err) {
|
|
6938
|
+
rejected++;
|
|
6939
|
+
if (on_parse_error) on_parse_error(err, line);
|
|
6940
|
+
return;
|
|
6941
|
+
}
|
|
6942
|
+
const result = trajectory_event_schema.safeParse(parsed);
|
|
6943
|
+
if (!result.success) {
|
|
6944
|
+
rejected++;
|
|
6945
|
+
if (on_parse_error) on_parse_error(result.error, line);
|
|
6946
|
+
return;
|
|
6947
|
+
}
|
|
6948
|
+
broadcaster.emit(result.data);
|
|
6949
|
+
accepted++;
|
|
6950
|
+
};
|
|
6951
|
+
req.setEncoding("utf8");
|
|
6952
|
+
req.on("data", (chunk) => {
|
|
6953
|
+
buf += chunk;
|
|
6954
|
+
let nl = buf.indexOf("\n");
|
|
6955
|
+
while (nl !== -1) {
|
|
6956
|
+
consume_line(buf.slice(0, nl));
|
|
6957
|
+
buf = buf.slice(nl + 1);
|
|
6958
|
+
nl = buf.indexOf("\n");
|
|
6959
|
+
}
|
|
6960
|
+
});
|
|
6961
|
+
req.on("end", () => {
|
|
6962
|
+
if (buf.length > 0) consume_line(buf);
|
|
6963
|
+
send_json(res, 200, {
|
|
6964
|
+
accepted,
|
|
6965
|
+
rejected
|
|
6966
|
+
});
|
|
6967
|
+
});
|
|
6968
|
+
req.on("error", (err) => {
|
|
6969
|
+
if (on_parse_error) on_parse_error(err, "");
|
|
6970
|
+
send_json(res, 400, { error: "bad_request" });
|
|
6971
|
+
});
|
|
6972
|
+
}
|
|
6973
|
+
//#endregion
|
|
6974
|
+
//#region packages/viewer/src/tail.ts
|
|
6975
|
+
/**
|
|
6976
|
+
* JSONL file tailer.
|
|
6977
|
+
*
|
|
6978
|
+
* Watches a file for appends and emits one parsed trajectory event per
|
|
6979
|
+
* complete line. Handles three pathological cases the spec calls out:
|
|
6980
|
+
*
|
|
6981
|
+
* - Truncation / rotation: a watcher event whose stat is shorter than the
|
|
6982
|
+
* current offset means the file shrunk. Reset offset to 0 and re-stream.
|
|
6983
|
+
* - Partial last line: if the trailing chunk has no `\n`, buffer it and
|
|
6984
|
+
* wait. Never parse a half-written line.
|
|
6985
|
+
* - Schema failures: a line that fails `trajectory_event_schema.parse` is
|
|
6986
|
+
* dropped and surfaced to `on_parse_error`. The stream keeps going.
|
|
6987
|
+
*
|
|
6988
|
+
* The watcher is fs.watch-based with a debounced re-read; on platforms where
|
|
6989
|
+
* fs.watch is flaky for appends we still catch up because the next tick
|
|
6990
|
+
* re-stats and re-reads from the saved offset.
|
|
6991
|
+
*/
|
|
6992
|
+
function start_tail(options) {
|
|
6993
|
+
const { path, on_event, on_parse_error, on_io_error } = options;
|
|
6994
|
+
let offset = 0;
|
|
6995
|
+
let buffer = "";
|
|
6996
|
+
let busy = false;
|
|
6997
|
+
let pending = false;
|
|
6998
|
+
let closed = false;
|
|
6999
|
+
let watcher = null;
|
|
7000
|
+
let last_drain = Promise.resolve();
|
|
7001
|
+
const handle_line = (line) => {
|
|
7002
|
+
if (line.length === 0) return;
|
|
7003
|
+
let parsed;
|
|
7004
|
+
try {
|
|
7005
|
+
parsed = JSON.parse(line);
|
|
7006
|
+
} catch (err) {
|
|
7007
|
+
if (on_parse_error) on_parse_error(err, line);
|
|
7008
|
+
return;
|
|
7009
|
+
}
|
|
7010
|
+
const result = trajectory_event_schema.safeParse(parsed);
|
|
7011
|
+
if (!result.success) {
|
|
7012
|
+
if (on_parse_error) on_parse_error(result.error, line);
|
|
7013
|
+
return;
|
|
7014
|
+
}
|
|
7015
|
+
on_event(result.data);
|
|
7016
|
+
};
|
|
7017
|
+
const read_more = async () => {
|
|
7018
|
+
let st;
|
|
7019
|
+
try {
|
|
7020
|
+
st = await stat(path);
|
|
7021
|
+
} catch (err) {
|
|
7022
|
+
if (on_io_error) on_io_error(err);
|
|
7023
|
+
return;
|
|
7024
|
+
}
|
|
7025
|
+
if (st.size < offset) {
|
|
7026
|
+
offset = 0;
|
|
7027
|
+
buffer = "";
|
|
7028
|
+
}
|
|
7029
|
+
if (st.size === offset) return;
|
|
7030
|
+
const fh = await open(path, "r").catch((err) => {
|
|
7031
|
+
if (on_io_error) on_io_error(err);
|
|
7032
|
+
return null;
|
|
7033
|
+
});
|
|
7034
|
+
if (!fh) return;
|
|
7035
|
+
try {
|
|
7036
|
+
const length = st.size - offset;
|
|
7037
|
+
const buf = Buffer.alloc(length);
|
|
7038
|
+
const { bytesRead } = await fh.read(buf, 0, length, offset);
|
|
7039
|
+
offset += bytesRead;
|
|
7040
|
+
buffer += buf.subarray(0, bytesRead).toString("utf8");
|
|
7041
|
+
} finally {
|
|
7042
|
+
await fh.close();
|
|
7043
|
+
}
|
|
7044
|
+
let nl = buffer.indexOf("\n");
|
|
7045
|
+
while (nl !== -1) {
|
|
7046
|
+
const line = buffer.slice(0, nl);
|
|
7047
|
+
buffer = buffer.slice(nl + 1);
|
|
7048
|
+
handle_line(line);
|
|
7049
|
+
nl = buffer.indexOf("\n");
|
|
7050
|
+
}
|
|
7051
|
+
};
|
|
7052
|
+
const schedule = () => {
|
|
7053
|
+
if (closed) return;
|
|
7054
|
+
if (busy) {
|
|
7055
|
+
pending = true;
|
|
7056
|
+
return;
|
|
7057
|
+
}
|
|
7058
|
+
busy = true;
|
|
7059
|
+
last_drain = (async () => {
|
|
7060
|
+
try {
|
|
7061
|
+
let keep_going = true;
|
|
7062
|
+
while (keep_going) {
|
|
7063
|
+
pending = false;
|
|
7064
|
+
await read_more();
|
|
7065
|
+
keep_going = pending && !closed;
|
|
7066
|
+
}
|
|
7067
|
+
} finally {
|
|
7068
|
+
busy = false;
|
|
7069
|
+
}
|
|
7070
|
+
})();
|
|
7071
|
+
};
|
|
7072
|
+
schedule();
|
|
7073
|
+
try {
|
|
7074
|
+
watcher = watch(path, { persistent: true }, () => {
|
|
7075
|
+
schedule();
|
|
7076
|
+
});
|
|
7077
|
+
} catch (err) {
|
|
7078
|
+
if (on_io_error) on_io_error(err);
|
|
7079
|
+
}
|
|
7080
|
+
const stop = () => {
|
|
7081
|
+
closed = true;
|
|
7082
|
+
if (watcher) {
|
|
7083
|
+
watcher.close();
|
|
7084
|
+
watcher = null;
|
|
7085
|
+
}
|
|
7086
|
+
};
|
|
7087
|
+
const drain = async () => {
|
|
7088
|
+
schedule();
|
|
7089
|
+
await last_drain;
|
|
7090
|
+
};
|
|
7091
|
+
return {
|
|
7092
|
+
stop,
|
|
7093
|
+
drain
|
|
7094
|
+
};
|
|
7095
|
+
}
|
|
7096
|
+
//#endregion
|
|
7097
|
+
//#region packages/viewer/src/cli.ts
|
|
7098
|
+
/**
|
|
7099
|
+
* fascicle-viewer CLI.
|
|
7100
|
+
*
|
|
7101
|
+
* fascicle-viewer <path> # tail a JSONL file (primary)
|
|
7102
|
+
* fascicle-viewer --listen # accept HTTP push only
|
|
7103
|
+
* fascicle-viewer <path> --listen # both producers feed the same broadcaster
|
|
7104
|
+
*
|
|
7105
|
+
* Flags: --port <n> --host <h> --buffer <n> --no-open --help
|
|
7106
|
+
*
|
|
7107
|
+
* Defaults bind 127.0.0.1:4242 and open the user's default browser. SIGINT
|
|
7108
|
+
* shuts everything down cleanly. The CLI body is exposed as `run_viewer_cli`
|
|
7109
|
+
* so the umbrella's bin shim can drive it from a bundled `dist/index.js`.
|
|
7110
|
+
*/
|
|
7111
|
+
const HELP = `\
|
|
7112
|
+
fascicle-viewer — minimal in-repo dashboard for fascicle trajectory streams.
|
|
7113
|
+
|
|
7114
|
+
Usage:
|
|
7115
|
+
fascicle-viewer <path> tail a JSONL file
|
|
7116
|
+
fascicle-viewer --listen accept HTTP push only
|
|
7117
|
+
fascicle-viewer <path> --listen both producers feed the same broadcaster
|
|
7118
|
+
|
|
7119
|
+
Options:
|
|
7120
|
+
--port <n> port (default 4242)
|
|
7121
|
+
--host <h> bind host (default 127.0.0.1; --host 0.0.0.0 warns)
|
|
7122
|
+
--buffer <n> ring-buffer size (default 1000)
|
|
7123
|
+
--no-open do not open the browser
|
|
7124
|
+
--help show this message
|
|
7125
|
+
`;
|
|
7126
|
+
function parse(argv) {
|
|
7127
|
+
const { values, positionals } = parseArgs({
|
|
7128
|
+
args: [...argv],
|
|
7129
|
+
allowPositionals: true,
|
|
7130
|
+
options: {
|
|
7131
|
+
port: {
|
|
7132
|
+
type: "string",
|
|
7133
|
+
default: "4242"
|
|
7134
|
+
},
|
|
7135
|
+
host: {
|
|
7136
|
+
type: "string",
|
|
7137
|
+
default: "127.0.0.1"
|
|
7138
|
+
},
|
|
7139
|
+
buffer: {
|
|
7140
|
+
type: "string",
|
|
7141
|
+
default: "1000"
|
|
7142
|
+
},
|
|
7143
|
+
listen: {
|
|
7144
|
+
type: "boolean",
|
|
7145
|
+
default: false
|
|
7146
|
+
},
|
|
7147
|
+
open: {
|
|
7148
|
+
type: "boolean",
|
|
7149
|
+
default: true
|
|
7150
|
+
},
|
|
7151
|
+
"no-open": {
|
|
7152
|
+
type: "boolean",
|
|
7153
|
+
default: false
|
|
7154
|
+
},
|
|
7155
|
+
help: {
|
|
7156
|
+
type: "boolean",
|
|
7157
|
+
default: false
|
|
7158
|
+
}
|
|
7159
|
+
}
|
|
7160
|
+
});
|
|
7161
|
+
if (values.help) {
|
|
7162
|
+
process.stdout.write(HELP);
|
|
7163
|
+
process.exit(0);
|
|
7164
|
+
}
|
|
7165
|
+
const port_raw = values.port ?? "4242";
|
|
7166
|
+
const buffer_raw = values.buffer ?? "1000";
|
|
7167
|
+
const port = Number.parseInt(port_raw, 10);
|
|
7168
|
+
const buffer = Number.parseInt(buffer_raw, 10);
|
|
7169
|
+
if (!Number.isFinite(port) || port <= 0) {
|
|
7170
|
+
process.stderr.write(`invalid --port: ${port_raw}\n`);
|
|
7171
|
+
process.exit(2);
|
|
7172
|
+
}
|
|
7173
|
+
if (!Number.isFinite(buffer) || buffer <= 0) {
|
|
7174
|
+
process.stderr.write(`invalid --buffer: ${buffer_raw}\n`);
|
|
7175
|
+
process.exit(2);
|
|
7176
|
+
}
|
|
7177
|
+
const path = positionals[0];
|
|
7178
|
+
const listen = values.listen ?? false;
|
|
7179
|
+
if (path === void 0 && !listen) {
|
|
7180
|
+
process.stderr.write("fascicle-viewer: a <path> is required unless --listen is set\n");
|
|
7181
|
+
process.stderr.write(HELP);
|
|
7182
|
+
process.exit(2);
|
|
7183
|
+
}
|
|
7184
|
+
return {
|
|
7185
|
+
...path !== void 0 ? { path } : {},
|
|
7186
|
+
host: values.host ?? "127.0.0.1",
|
|
7187
|
+
port,
|
|
7188
|
+
buffer,
|
|
7189
|
+
listen,
|
|
7190
|
+
open: !values["no-open"] && (values.open ?? true)
|
|
7191
|
+
};
|
|
7192
|
+
}
|
|
7193
|
+
function open_browser(url) {
|
|
7194
|
+
const platform = process.platform;
|
|
7195
|
+
const cmd = platform === "darwin" ? "open" : platform === "win32" ? "cmd" : "xdg-open";
|
|
7196
|
+
const args = platform === "win32" ? [
|
|
7197
|
+
"/c",
|
|
7198
|
+
"start",
|
|
7199
|
+
"\"\"",
|
|
7200
|
+
url
|
|
7201
|
+
] : [url];
|
|
7202
|
+
try {
|
|
7203
|
+
const child = spawn(cmd, args, {
|
|
7204
|
+
stdio: "ignore",
|
|
7205
|
+
detached: true
|
|
7206
|
+
});
|
|
7207
|
+
child.on("error", () => {});
|
|
7208
|
+
child.unref();
|
|
7209
|
+
} catch {}
|
|
7210
|
+
}
|
|
7211
|
+
async function run_viewer_cli(argv) {
|
|
7212
|
+
const args = parse(argv);
|
|
7213
|
+
if (args.path !== void 0 && !existsSync(args.path)) process.stderr.write(`fascicle-viewer: warning: ${args.path} does not exist yet — will pick up events once it is created\n`);
|
|
7214
|
+
if (args.host === "0.0.0.0") process.stderr.write("fascicle-viewer: warning: binding 0.0.0.0 — this dashboard has no auth and exposes whatever is in your trajectory stream\n");
|
|
7215
|
+
const handle = await start_viewer({
|
|
7216
|
+
...args.path !== void 0 ? { path: args.path } : {},
|
|
7217
|
+
host: args.host,
|
|
7218
|
+
port: args.port,
|
|
7219
|
+
buffer: args.buffer,
|
|
7220
|
+
on_parse_error: (err, line) => {
|
|
7221
|
+
const preview = line.length > 120 ? `${line.slice(0, 120)}…` : line;
|
|
7222
|
+
process.stderr.write(`fascicle-viewer: skipped malformed line: ${preview}\n ${describe_error(err)}\n`);
|
|
7223
|
+
},
|
|
7224
|
+
on_io_error: (err) => {
|
|
7225
|
+
process.stderr.write(`fascicle-viewer: io error: ${describe_error(err)}\n`);
|
|
7226
|
+
}
|
|
7227
|
+
});
|
|
7228
|
+
if (args.path !== void 0) process.stderr.write(`watching ${args.path}\n`);
|
|
7229
|
+
if (args.listen || args.path === void 0) process.stderr.write(`listening for HTTP push on ${handle.url}/api/ingest\n`);
|
|
7230
|
+
process.stderr.write(`viewer at ${handle.url}\n`);
|
|
7231
|
+
if (args.open) open_browser(handle.url);
|
|
7232
|
+
const shutdown = async () => {
|
|
7233
|
+
process.stderr.write("\nfascicle-viewer: shutting down\n");
|
|
7234
|
+
await handle.close();
|
|
7235
|
+
process.exit(0);
|
|
7236
|
+
};
|
|
7237
|
+
process.on("SIGINT", () => {
|
|
7238
|
+
shutdown();
|
|
7239
|
+
});
|
|
7240
|
+
process.on("SIGTERM", () => {
|
|
7241
|
+
shutdown();
|
|
7242
|
+
});
|
|
7243
|
+
}
|
|
7244
|
+
function describe_error(err) {
|
|
7245
|
+
if (err instanceof Error) return err.message;
|
|
7246
|
+
try {
|
|
7247
|
+
return JSON.stringify(err);
|
|
7248
|
+
} catch {
|
|
7249
|
+
return String(err);
|
|
7250
|
+
}
|
|
7251
|
+
}
|
|
7252
|
+
if (process.argv[1] !== void 0 && process.argv[1] === fileURLToPath(import.meta.url)) run_viewer_cli(process.argv.slice(2)).catch((err) => {
|
|
7253
|
+
process.stderr.write(`fascicle-viewer: ${describe_error(err)}\n`);
|
|
7254
|
+
process.exit(1);
|
|
7255
|
+
});
|
|
7256
|
+
//#endregion
|
|
7257
|
+
//#region packages/viewer/src/index.ts
|
|
7258
|
+
/**
|
|
7259
|
+
* Public surface for @repo/viewer.
|
|
7260
|
+
*
|
|
7261
|
+
* `start_viewer({ ... })` is the programmatic embed: spin up the server,
|
|
7262
|
+
* optionally start tailing a JSONL file, and return a handle that can stop
|
|
7263
|
+
* both. `start_viewer` only depends on @repo/core's wire-format schema; it
|
|
7264
|
+
* never reaches into the engine, composites, or any provider.
|
|
7265
|
+
*/
|
|
7266
|
+
async function start_viewer(options = {}) {
|
|
7267
|
+
const host = options.host ?? "127.0.0.1";
|
|
7268
|
+
const port = options.port ?? 4242;
|
|
7269
|
+
const broadcaster = create_broadcaster({
|
|
7270
|
+
buffer: options.buffer ?? 1e3,
|
|
7271
|
+
...options.on_subscriber_error ? { on_subscriber_error: options.on_subscriber_error } : {}
|
|
7272
|
+
});
|
|
7273
|
+
const server = await start_server({
|
|
7274
|
+
broadcaster,
|
|
7275
|
+
host,
|
|
7276
|
+
port,
|
|
7277
|
+
...options.on_parse_error ? { on_parse_error: options.on_parse_error } : {}
|
|
7278
|
+
});
|
|
7279
|
+
let tail = null;
|
|
7280
|
+
if (options.path !== void 0) tail = start_tail({
|
|
7281
|
+
path: options.path,
|
|
7282
|
+
on_event: (event) => {
|
|
7283
|
+
broadcaster.emit(event);
|
|
7284
|
+
},
|
|
7285
|
+
...options.on_parse_error ? { on_parse_error: options.on_parse_error } : {},
|
|
7286
|
+
...options.on_io_error ? { on_io_error: options.on_io_error } : {}
|
|
7287
|
+
});
|
|
7288
|
+
return wrap_handle(server, tail, host, port);
|
|
7289
|
+
}
|
|
7290
|
+
function wrap_handle(server, tail, host, port) {
|
|
7291
|
+
return {
|
|
7292
|
+
url: server.url,
|
|
7293
|
+
host,
|
|
7294
|
+
port,
|
|
7295
|
+
close: async () => {
|
|
7296
|
+
if (tail) tail.stop();
|
|
7297
|
+
await server.close();
|
|
7298
|
+
}
|
|
7299
|
+
};
|
|
7300
|
+
}
|
|
7301
|
+
//#endregion
|
|
7302
|
+
export { STEP_KINDS, aborted_error, adversarial, bench, branch, checkpoint, compose, consensus, version as core_version, create_engine, custom_event_schema, describe, describe_cycle_error, emit_event_schema, engine_config_error, version$1 as engine_version, ensemble, ensemble_step, fallback, flow_schema, forward_standard_env, improve, is_step_kind, judge_equals, judge_llm, judge_with, learn, loop, map, model_call, model_not_found_error, normalize_score, on_chunk_error, parallel, pipe, provider_capability_error, provider_error, provider_not_configured_error, rate_limit_error, read_baseline, regression_compare, resume_validation_error, retry, run, run_viewer_cli, schema_validation_error, scope, sequence, span_end_event_schema, span_start_event_schema, start_viewer, stash, step, suspend, suspended_error, timeout, timeout_error, tool_approval_denied_error, tool_error, tournament, trajectory_event_schema, use, write_baseline };
|
|
6097
7303
|
|
|
6098
7304
|
//# sourceMappingURL=index.js.map
|