Lines 49-89typescript
50 * Uses local Ollama when MEGACOMPACT_RAPTOR_MODEL is set (localhost-only);
51 * otherwise falls back to deterministic extractive. The `fetch` is a localhost
52 * call inside the PREVENT-PI-004 exception — annotated accordingly.
54export function summarizeCluster(messages: EngineMessage[]): ClusterSummary {
55 const ollama = ollamaEndpoint();
56 if (!ollama) return extractiveClusterSummary(messages);
57 return ollamaSummarize(messages, ollama);
60function ollamaSummarize(messages: EngineMessage[], ollama: { url: string; model: string }): ClusterSummary {
61 // The fetch below is localhost-only (loopback Ollama) — the PREVENT-PI-004
62 // sanctioned local-model exceptions (same class as /dashboard, HttpEmbedder).
63 const prompt = messages.map((m) => `${m.role}: ${m.text}`).join("\n");
64 // Synchronous bridge: spawnSync an inline worker so the call blocks without
65 // deadlocking fetch (mirrors HttpEmbedder — Atomics.wait on main thread would
66 // hang). A blocked main thread cannot pump the socket.
67 const WORKER = String.raw`
68 const url = process.env.R_URL, model = process.env.R_MODEL, prompt = process.env.R_PROMPT;
70 const r = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model, prompt, stream: false }) }); // guardrails-allow PREVENT-PI-004: localhost-only user-spawned Ollama server (BYO local model, never remote)
71 const j = await r.json();
72 process.stdout.write(JSON.stringify({ ok: r.ok, text: j.response || "" }));
74 process.stdout.write(JSON.stringify({ ok: false, error: String(e && e.message ? e.message : e) }));
77 const res = spawnSync(process.execPath, ["-e", WORKER], {
78 encoding: "utf8",
HighCommand Output Exfiltration
Source combines command execution, command-output handling, and outbound requests; review data flow before blocking.
src/dedup/raptor/summarizer.tsView on unpkg · L69 79 env: { ...process.env, R_URL: ollama.url, R_MODEL: ollama.model, R_PROMPT: prompt },
81 let parsed: { ok: boolean; text?: string; error?: string } = { ok: false, error: "no response" };
82 if (typeof res.stdout === "string" && res.stdout.length > 0) {
83 try { parsed = JSON.parse(res.stdout); } catch { parsed = { ok: false, error: "bad json" }; }
85 if (!parsed.ok || !parsed.text) {
86 // Ollama unavailable → deterministic extractive fallback (never fail the build).
87 return extractiveClusterSummary(messages);
89 const summary = parsed.text.trim();