bugxhunter 0.8.7 → 0.9.1

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.
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Diagrams for the Playground: a diagram spec a model wrote (drawgen's YAML/JSON format)
3
+ * becomes an editable .drawio.svg. The work happens in the separate `render` service
4
+ * (docker/render.Dockerfile), so model output is never parsed in the process that holds
5
+ * the vault key. Without OPEN_RUNNER_RENDER_URL (e.g. outside Docker) rendering is off
6
+ * and the UI shows the spec as code.
7
+ */
8
+ import { httpError } from "./errors.js";
9
+ export const RENDER_URL = (process.env.OPEN_RUNNER_RENDER_URL ?? "").replace(/\/+$/, "");
10
+ export const MAX_SPEC = 200 * 1024;
11
+ export const enabled = () => Boolean(RENDER_URL);
12
+ export async function status() {
13
+ if (!RENDER_URL)
14
+ return { enabled: false, ok: false };
15
+ try {
16
+ const r = await fetch(`${RENDER_URL}/health`, { signal: AbortSignal.timeout(3000) });
17
+ const j = (await r.json());
18
+ return { enabled: true, ok: r.ok, drawgen: j.drawgen };
19
+ }
20
+ catch {
21
+ return { enabled: true, ok: false };
22
+ }
23
+ }
24
+ /** Render one spec. Spec mistakes come back as 422 with drawgen's message (for the model to fix). */
25
+ export async function drawio(spec) {
26
+ if (!RENDER_URL)
27
+ throw httpError(503, "Diagram rendering is off: it needs the render service (part of the Docker setup).");
28
+ if (typeof spec !== "string" && (typeof spec !== "object" || spec === null || Array.isArray(spec))) {
29
+ throw httpError(400, "spec must be YAML/JSON text or an object");
30
+ }
31
+ const body = JSON.stringify({ spec });
32
+ if (Buffer.byteLength(body) > MAX_SPEC)
33
+ throw httpError(413, `The diagram spec is over ${MAX_SPEC / 1024} KB.`);
34
+ let r;
35
+ try {
36
+ r = await fetch(`${RENDER_URL}/drawio`, {
37
+ method: "POST",
38
+ headers: { "Content-Type": "application/json" },
39
+ body,
40
+ signal: AbortSignal.timeout(20_000),
41
+ });
42
+ }
43
+ catch {
44
+ throw httpError(503, "The render service is not answering.");
45
+ }
46
+ const j = (await r.json().catch(() => ({})));
47
+ if (!r.ok || !j.svg)
48
+ throw httpError(r.status === 200 ? 502 : r.status, j.error?.message || `Rendering failed (${r.status}).`);
49
+ return j;
50
+ }
51
+ export const MAX_DIAGRAM_FILE = 3 * 1024 * 1024 - 1024;
52
+ /** A .drawio / .drawio.svg file a person attached -> the outline a model reviews. */
53
+ export async function outline(file) {
54
+ if (!RENDER_URL)
55
+ throw httpError(503, "Reading draw.io files needs the render service (part of the Docker setup).");
56
+ if (typeof file !== "string" || !file.trim())
57
+ throw httpError(400, "file must be the text of a .drawio or .drawio.svg file");
58
+ const body = JSON.stringify({ file });
59
+ if (Buffer.byteLength(body) > MAX_DIAGRAM_FILE)
60
+ throw httpError(413, "The diagram file is over 3 MB.");
61
+ let r;
62
+ try {
63
+ r = await fetch(`${RENDER_URL}/drawio/outline`, {
64
+ method: "POST",
65
+ headers: { "Content-Type": "application/json" },
66
+ body,
67
+ signal: AbortSignal.timeout(20_000),
68
+ });
69
+ }
70
+ catch {
71
+ throw httpError(503, "The render service is not answering.");
72
+ }
73
+ const j = (await r.json().catch(() => ({})));
74
+ if (!r.ok || typeof j.outline !== "string")
75
+ throw httpError(r.status === 200 ? 502 : r.status, j.error?.message || `Reading the diagram failed (${r.status}).`);
76
+ return { outline: j.outline, pages: j.pages ?? [], chars: j.chars ?? j.outline.length };
77
+ }
78
+ //# sourceMappingURL=render.js.map
@@ -10,6 +10,8 @@
10
10
  * parsed provider message (so tier/rate-limit `429`s surface cleanly).
11
11
  */
12
12
  export const SCX_DEFAULT_BASE_URL = "https://api.scx.ai/v1";
13
+ /** Limit for a whole non-streamed completion (see chat()). */
14
+ export const NON_STREAM_TIMEOUT_MS = Number(process.env.OPEN_RUNNER_NON_STREAM_TIMEOUT_MS ?? 600_000);
13
15
  export class SCXError extends Error {
14
16
  status;
15
17
  type;
@@ -37,10 +39,10 @@ export class SCXClient {
37
39
  }
38
40
  /** Low-level fetch that injects auth, JSON headers, and a timeout. */
39
41
  async request(path, init = {}) {
40
- const { json, headers, ...rest } = init;
42
+ const { json, headers, timeoutMs = this.timeoutMs, ...rest } = init;
41
43
  const apiKey = this.key(); // may throw (vault sealed): before any network call
42
44
  const ctrl = new AbortController();
43
- const t = setTimeout(() => ctrl.abort(), this.timeoutMs);
45
+ const t = setTimeout(() => ctrl.abort(), timeoutMs);
44
46
  try {
45
47
  return await fetch(`${this.baseUrl}${path}`, {
46
48
  ...rest,
@@ -54,6 +56,13 @@ export class SCXClient {
54
56
  body: json !== undefined ? JSON.stringify(json) : rest.body,
55
57
  });
56
58
  }
59
+ catch (e) {
60
+ if (ctrl.signal.aborted) {
61
+ const host = new URL(this.baseUrl).host;
62
+ throw new SCXError(504, `${host} did not start answering within ${Math.round(timeoutMs / 1000)}s. The model may be busy or queued; try again or pick another model.`, "timeout");
63
+ }
64
+ throw e;
65
+ }
57
66
  finally {
58
67
  clearTimeout(t);
59
68
  }
@@ -92,10 +101,13 @@ export class SCXClient {
92
101
  return data.data;
93
102
  }
94
103
  // --- Chat Completions -----------------------------------------------------
95
- async chat(params) {
104
+ async chat(params, opts = {}) {
105
+ // Non-streamed: nothing arrives until the whole answer is written, which a reasoning model
106
+ // can take minutes over, so allow longer than the "started answering" limit for streams.
96
107
  const res = await this.request("/chat/completions", {
97
108
  method: "POST",
98
109
  json: { ...params, stream: false },
110
+ timeoutMs: opts.timeoutMs ?? Math.max(this.timeoutMs, NON_STREAM_TIMEOUT_MS),
99
111
  });
100
112
  return this.json(res);
101
113
  }
@@ -272,6 +272,26 @@ export function setItem(name, value) {
272
272
  save({ ...v, items: { ...v.items, [name]: seal(dek, value, name) } });
273
273
  audit("vault.item.set", { name });
274
274
  }
275
+ /**
276
+ * Seal data for another at-rest store (Playground sessions) with the vault key, which never
277
+ * leaves this module. The AAD binds the ciphertext to its record, so a sealed blob can't be
278
+ * moved to another record and still open. Needs a vault that is set up and unlocked.
279
+ */
280
+ export function sealData(plaintext, aad) {
281
+ if (!load())
282
+ throw httpError(409, "Set up the vault (sidebar → keys) to save Playground sessions; they are encrypted with it.");
283
+ if (!dek)
284
+ throw httpError(503, "The vault is sealed (locked). Unlock it to save.");
285
+ touch();
286
+ return seal(dek, plaintext, aad);
287
+ }
288
+ /** Open data sealed by sealData(). Throws if sealed, or if the blob or its AAD was tampered with. */
289
+ export function openData(sealed, aad) {
290
+ if (!dek)
291
+ throw httpError(503, "The vault is sealed (locked). Unlock it to open saved sessions.");
292
+ touch();
293
+ return open(dek, sealed, aad).toString("utf8");
294
+ }
275
295
  export function deleteItem(name) {
276
296
  const v = load();
277
297
  if (!v)