@konneal/engine 0.2.193 → 0.2.194

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.
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  handleAsk
3
- } from "./chunk-RX7FL5D6.js";
3
+ } from "./chunk-GJHB3LY4.js";
4
4
  import "./chunk-IRCDOFP4.js";
5
- import "./chunk-UZHUND67.js";
6
- import "./chunk-73GZKS2T.js";
7
- import "./chunk-QYNFNOKE.js";
5
+ import "./chunk-2DBXRO4E.js";
6
+ import "./chunk-JEPZ7SNP.js";
7
+ import "./chunk-5OSQMWRV.js";
8
8
  import "./chunk-M4OCMIWU.js";
9
9
  import "./chunk-ETLSWSZ7.js";
10
10
  import "./chunk-J4UAO5XI.js";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  embed,
3
3
  unitPayloadRows
4
- } from "./chunk-QYNFNOKE.js";
4
+ } from "./chunk-5OSQMWRV.js";
5
5
  import {
6
6
  standardKeysFrom
7
7
  } from "./chunk-J4UAO5XI.js";
@@ -114,6 +114,15 @@ async function publicationUnitRows(db, type, docidentifierLike, limit) {
114
114
  }
115
115
  return [];
116
116
  }
117
+ async function writeUnitDescription(db, unitId, description, docId) {
118
+ if (docId) {
119
+ try {
120
+ await db.prepare("UPDATE unit_payloads_doc SET payload = json_set(payload, '$.description', ?1) WHERE unit_id = ?2 AND doc_id = ?3").bind(description, unitId, docId).run();
121
+ } catch {
122
+ }
123
+ }
124
+ await db.prepare("UPDATE unit_payloads SET payload = json_set(payload, '$.description', ?1) WHERE unit_id = ?2 AND (?3 IS NULL OR doc_id = ?3)").bind(description, unitId, docId ?? null).run();
125
+ }
117
126
 
118
127
  export {
119
128
  embed,
@@ -122,5 +131,6 @@ export {
122
131
  rerank,
123
132
  generateOnce,
124
133
  unitPayloadRows,
125
- publicationUnitRows
134
+ publicationUnitRows,
135
+ writeUnitDescription
126
136
  };
@@ -6,7 +6,7 @@ import {
6
6
  sessionFrom,
7
7
  telemetry,
8
8
  understandQuery
9
- } from "./chunk-73GZKS2T.js";
9
+ } from "./chunk-JEPZ7SNP.js";
10
10
  import {
11
11
  corsHeaders,
12
12
  err,
@@ -33,7 +33,7 @@ import {
33
33
  tableRetyped,
34
34
  toolNote,
35
35
  verdictNote
36
- } from "./chunk-UZHUND67.js";
36
+ } from "./chunk-2DBXRO4E.js";
37
37
  import {
38
38
  NO_CONTEXT,
39
39
  STAGE_NAMES,
@@ -77,7 +77,7 @@ import {
77
77
  tokenBudget,
78
78
  understandQuery,
79
79
  usageTotal
80
- } from "./chunk-73GZKS2T.js";
80
+ } from "./chunk-JEPZ7SNP.js";
81
81
  import {
82
82
  CAPACITY_PAUSE_MS,
83
83
  embed,
@@ -85,7 +85,7 @@ import {
85
85
  isCapacityError,
86
86
  publicationUnitRows,
87
87
  unitPayloadRows
88
- } from "./chunk-QYNFNOKE.js";
88
+ } from "./chunk-5OSQMWRV.js";
89
89
  import {
90
90
  corsHeaders,
91
91
  err,
@@ -2,7 +2,7 @@ import {
2
2
  embed,
3
3
  rerank,
4
4
  unitPayloadRows
5
- } from "./chunk-QYNFNOKE.js";
5
+ } from "./chunk-5OSQMWRV.js";
6
6
  import {
7
7
  bubbleConfirmPage,
8
8
  isAllowedBubbleOrigin
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  handleSearch
3
- } from "./chunk-3MV3UO6Q.js";
4
- import "./chunk-73GZKS2T.js";
5
- import "./chunk-QYNFNOKE.js";
3
+ } from "./chunk-6HW7R5SI.js";
4
+ import "./chunk-JEPZ7SNP.js";
5
+ import "./chunk-5OSQMWRV.js";
6
6
  import "./chunk-M4OCMIWU.js";
7
7
  import "./chunk-ETLSWSZ7.js";
8
8
  import "./chunk-J4UAO5XI.js";
@@ -4,8 +4,8 @@ import {
4
4
  parseToolCall,
5
5
  runTool,
6
6
  toolNote
7
- } from "./chunk-UZHUND67.js";
8
- import "./chunk-QYNFNOKE.js";
7
+ } from "./chunk-2DBXRO4E.js";
8
+ import "./chunk-5OSQMWRV.js";
9
9
  import "./chunk-J4UAO5XI.js";
10
10
  import "./chunk-5Y3CDK64.js";
11
11
  import "./chunk-3FYJM7LH.js";
@@ -22,3 +22,8 @@ export declare function unitPayloadRows(db: StoreQuery, keys: readonly UnitKey[]
22
22
  * pattern), from the document-scoped table — or the legacy one where
23
23
  * that deployment has none. */
24
24
  export declare function publicationUnitRows(db: StoreQuery, type: string, docidentifierLike: string, limit: number): Promise<UnitRow[]>;
25
+ /** Store a generated description on a unit — on its document's row when
26
+ * the document is known, and on the legacy row only when that row IS
27
+ * the same document's: a bare unit id names the last writer, and a
28
+ * caption meant for one publication must never land on another's. */
29
+ export declare function writeUnitDescription(db: StoreQuery, unitId: string, description: string, docId?: string | null): Promise<void>;
@@ -9,16 +9,16 @@ import {
9
9
  listUserDocuments,
10
10
  scoreFaithfulness,
11
11
  scoreJudge
12
- } from "../../chunk-RX7FL5D6.js";
12
+ } from "../../chunk-GJHB3LY4.js";
13
13
  import "../../chunk-IRCDOFP4.js";
14
14
  import {
15
15
  TOOLS_REGISTRY,
16
16
  bindModelNode,
17
17
  modelGroundingBlock
18
- } from "../../chunk-UZHUND67.js";
18
+ } from "../../chunk-2DBXRO4E.js";
19
19
  import {
20
20
  handleSearch
21
- } from "../../chunk-3MV3UO6Q.js";
21
+ } from "../../chunk-6HW7R5SI.js";
22
22
  import {
23
23
  buildMessages,
24
24
  citations,
@@ -43,11 +43,13 @@ import {
43
43
  sessionFrom,
44
44
  telemetry,
45
45
  understandQuery
46
- } from "../../chunk-73GZKS2T.js";
46
+ } from "../../chunk-JEPZ7SNP.js";
47
47
  import {
48
48
  embed,
49
- generateOnce
50
- } from "../../chunk-QYNFNOKE.js";
49
+ generateOnce,
50
+ unitPayloadRows,
51
+ writeUnitDescription
52
+ } from "../../chunk-5OSQMWRV.js";
51
53
  import {
52
54
  authenticate,
53
55
  corsHeaders,
@@ -589,8 +591,8 @@ ${chain || "(no successor edges recorded)"}` }
589
591
  return err(502, "family_summary_failed", String(e?.message ?? e).slice(0, 200));
590
592
  }
591
593
  }
592
- async function loadUnitAsset(env, unitId) {
593
- const row = await env.DB.prepare("SELECT payload, docidentifier FROM unit_payloads WHERE unit_id = ?1").bind(unitId).first();
594
+ async function loadUnitAsset(env, unitId, docId) {
595
+ const [row] = await unitPayloadRows(env.DB, [{ unit_id: unitId, doc_id: docId || null }]);
594
596
  if (!row) return { error: err(404, "not_found", "no unit_payload row for that id") };
595
597
  const payload = JSON.parse(String(row.payload));
596
598
  const uri = payload.uri ?? "";
@@ -612,11 +614,12 @@ async function handleVisionJudge(env, req) {
612
614
  if (auth !== `Bearer ${env.ADMIN_TOKEN}`) return err(401, "unauthorized", "Invalid admin token");
613
615
  const body = await readJson(req);
614
616
  const unitId = typeof body?.unit_id === "string" ? body.unit_id : "";
617
+ const docId = typeof body?.doc_id === "string" ? body.doc_id.slice(0, 200) : "";
615
618
  const capA = typeof body?.captions?.a === "string" ? body.captions.a.slice(0, 2e3) : "";
616
619
  const capB = typeof body?.captions?.b === "string" ? body.captions.b.slice(0, 2e3) : "";
617
620
  if (!unitId || !capA || !capB) return err(400, "invalid_input", "unit_id and captions.a/.b required");
618
621
  try {
619
- const loaded = await loadUnitAsset(env, unitId);
622
+ const loaded = await loadUnitAsset(env, unitId, docId);
620
623
  if (loaded.error) return loaded.error;
621
624
  const swap = Math.random() < 0.5;
622
625
  const first = swap ? capB : capA;
@@ -659,12 +662,13 @@ async function handleCaption(env, req) {
659
662
  if (auth !== `Bearer ${env.ADMIN_TOKEN}`) return err(401, "unauthorized", "Invalid admin token");
660
663
  const body = await readJson(req);
661
664
  const unitId = typeof body?.unit_id === "string" ? body.unit_id : "";
665
+ const docId = typeof body?.doc_id === "string" ? body.doc_id.slice(0, 200) : "";
662
666
  const context = typeof body?.context === "string" ? body.context.slice(0, 400) : "";
663
667
  const override = typeof body?.model === "string" && body.model.startsWith("@cf/") ? body.model : null;
664
668
  const dryRun = body?.dry_run === true;
665
669
  if (!unitId) return err(400, "invalid_input", "unit_id required");
666
670
  try {
667
- const loaded = await loadUnitAsset(env, unitId);
671
+ const loaded = await loadUnitAsset(env, unitId, docId);
668
672
  if (loaded.error) return loaded.error;
669
673
  const row = loaded.row;
670
674
  const b64 = loaded.b64;
@@ -700,8 +704,8 @@ async function handleCaption(env, req) {
700
704
  const end = Math.max(cut.lastIndexOf(". "), cut.lastIndexOf("! "), cut.lastIndexOf("? "));
701
705
  return end === -1 ? cut.slice(0, cut.lastIndexOf(" ")) : cut.slice(0, end + 1);
702
706
  })();
703
- if (!dryRun) await env.DB.prepare("UPDATE unit_payloads SET payload = json_set(payload, '$.description', ?1) WHERE unit_id = ?2").bind(desc, unitId).run();
704
- return json({ ok: true, unit_id: unitId, model: override ?? roleModel(env, "caption"), dry_run: dryRun || void 0, description: desc });
707
+ if (!dryRun) await writeUnitDescription(env.DB, unitId, desc, docId);
708
+ return json({ ok: true, unit_id: unitId, doc_id: docId || void 0, model: override ?? roleModel(env, "caption"), dry_run: dryRun || void 0, description: desc });
705
709
  } catch (e) {
706
710
  return err(502, "caption_failed", String(e).slice(0, 200));
707
711
  }
@@ -1184,13 +1188,13 @@ async function callToolOnce(env, ctx, _req, tier, key, name, args, t0) {
1184
1188
  // stream:false forces the JSON lane (anon defaults to SSE)
1185
1189
  body: JSON.stringify({ ...args, stream: false })
1186
1190
  });
1187
- const { runTool, TOOLS_REGISTRY: TOOLS_REGISTRY2 } = await import("../../tools-JWKGPVEG.js");
1191
+ const { runTool, TOOLS_REGISTRY: TOOLS_REGISTRY2 } = await import("../../tools-JBCL7UQO.js");
1188
1192
  let payload;
1189
1193
  if (TOOLS_REGISTRY2.some((t) => t.name === name && t.audiences.includes("mcp"))) {
1190
1194
  const r = await runTool({ ...env, DB: env.DB }, { name, args }, "mcp");
1191
1195
  payload = r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
1192
1196
  } else {
1193
- const res = name === "ask" ? await (await import("../../ask-XQR7LQPG.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-UMSZNYGP.js")).handleSearch(env, ctx, inner, tier, key);
1197
+ const res = name === "ask" ? await (await import("../../ask-CDJCSFI4.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-3V6EFKFX.js")).handleSearch(env, ctx, inner, tier, key);
1194
1198
  payload = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
1195
1199
  }
1196
1200
  telemetry(env, ctx, tier, `mcp:${name}`, null, !payload?.error, JSON.stringify(payload).length, await sha256Hex(`${name}:${JSON.stringify(args ?? {})}`), void 0, void 0, { durationMs: Date.now() - t0 });
package/package.json CHANGED
@@ -27,7 +27,7 @@
27
27
  "playwright": "^1.62.1",
28
28
  "yaml": "^2.9.1"
29
29
  },
30
- "version": "0.2.193",
30
+ "version": "0.2.194",
31
31
  "description": "The Konneal engine: the publisher-agnostic build pipeline and API plane for standards intelligence (retrieval, answer contract, verdicts, evaluation).",
32
32
  "license": "BSD-3-Clause",
33
33
  "type": "module",
@@ -15,6 +15,7 @@ import precisionPrompt from "../prompts/precision.md";
15
15
  import { scoreFaithfulness } from "./faithfulness";
16
16
  import { scoreJudge } from "./grader";
17
17
  import { portModelRunner } from "./env.ts";
18
+ import { unitPayloadRows, writeUnitDescription } from "./unitPayloads.ts";
18
19
  import { P } from "./profile.ts";
19
20
  import { fill, promptVars } from "./pipeline.ts";
20
21
 
@@ -265,9 +266,11 @@ export async function handleFamilySummary(env: Env, req: Request): Promise<Respo
265
266
  * passes like embedding smoothing (G-ETSI-4) — the binding is the
266
267
  * credential, admin-token gated exactly like /admin/enrich. */
267
268
  /** The unit's vision asset, base64-encoded for the message part — the
268
- * one load path the caption and the pairwise judge share. */
269
- async function loadUnitAsset(env: Env, unitId: string) {
270
- const row = await env.DB.prepare("SELECT payload, docidentifier FROM unit_payloads WHERE unit_id = ?1").bind(unitId).first<any>();
269
+ * one load path the caption and the pairwise judge share. The unit is
270
+ * read within its document when the caller names one: a unit id alone
271
+ * is shared across publications and names only its last writer. */
272
+ async function loadUnitAsset(env: Env, unitId: string, docId?: string) {
273
+ const [row] = await unitPayloadRows(env.DB, [{ unit_id: unitId, doc_id: docId || null }]);
271
274
  if (!row) return { error: err(404, "not_found", "no unit_payload row for that id") };
272
275
  const payload = JSON.parse(String(row.payload));
273
276
  const uri = payload.uri ?? "";
@@ -296,11 +299,12 @@ export async function handleVisionJudge(env: Env, req: Request): Promise<Respons
296
299
  if (auth !== `Bearer ${env.ADMIN_TOKEN}`) return err(401, "unauthorized", "Invalid admin token");
297
300
  const body = await readJson(req);
298
301
  const unitId = typeof body?.unit_id === "string" ? body.unit_id : "";
302
+ const docId = typeof body?.doc_id === "string" ? body.doc_id.slice(0, 200) : "";
299
303
  const capA = typeof body?.captions?.a === "string" ? body.captions.a.slice(0, 2000) : "";
300
304
  const capB = typeof body?.captions?.b === "string" ? body.captions.b.slice(0, 2000) : "";
301
305
  if (!unitId || !capA || !capB) return err(400, "invalid_input", "unit_id and captions.a/.b required");
302
306
  try {
303
- const loaded = await loadUnitAsset(env, unitId);
307
+ const loaded = await loadUnitAsset(env, unitId, docId);
304
308
  if (loaded.error) return loaded.error;
305
309
  // randomized presentation order: the judge's letter bias must not
306
310
  // correlate with either arm
@@ -344,15 +348,16 @@ export async function handleCaption(env: Env, req: Request): Promise<Response> {
344
348
  if (auth !== `Bearer ${env.ADMIN_TOKEN}`) return err(401, "unauthorized", "Invalid admin token");
345
349
  const body = await readJson(req);
346
350
  const unitId = typeof body?.unit_id === "string" ? body.unit_id : "";
351
+ const docId = typeof body?.doc_id === "string" ? body.doc_id.slice(0, 200) : "";
347
352
  const context = typeof body?.context === "string" ? body.context.slice(0, 400) : "";
348
353
  // the A/B arm (TODO.sota/18.4): a per-request model override for the
349
354
  // measurement, admin-gated like the route itself; dry_run skips the
350
- // payload write so the arms never mix state in unit_payloads
355
+ // payload write so the arms never mix state in the unit payloads
351
356
  const override = typeof body?.model === "string" && body.model.startsWith("@cf/") ? body.model : null;
352
357
  const dryRun = body?.dry_run === true;
353
358
  if (!unitId) return err(400, "invalid_input", "unit_id required");
354
359
  try {
355
- const loaded = await loadUnitAsset(env, unitId);
360
+ const loaded = await loadUnitAsset(env, unitId, docId);
356
361
  if (loaded.error) return loaded.error;
357
362
  const row = loaded.row;
358
363
  const b64 = loaded.b64;
@@ -395,8 +400,8 @@ export async function handleCaption(env: Env, req: Request): Promise<Response> {
395
400
  const end = Math.max(cut.lastIndexOf(". "), cut.lastIndexOf("! "), cut.lastIndexOf("? "));
396
401
  return end === -1 ? cut.slice(0, cut.lastIndexOf(" ")) : cut.slice(0, end + 1);
397
402
  })();
398
- if (!dryRun) await env.DB.prepare("UPDATE unit_payloads SET payload = json_set(payload, '$.description', ?1) WHERE unit_id = ?2").bind(desc, unitId).run();
399
- return json({ ok: true, unit_id: unitId, model: override ?? roleModel(env, "caption"), dry_run: dryRun || undefined, description: desc });
403
+ if (!dryRun) await writeUnitDescription(env.DB, unitId, desc, docId);
404
+ return json({ ok: true, unit_id: unitId, doc_id: docId || undefined, model: override ?? roleModel(env, "caption"), dry_run: dryRun || undefined, description: desc });
400
405
  } catch (e) {
401
406
  return err(502, "caption_failed", String(e).slice(0, 200));
402
407
  }
@@ -90,3 +90,24 @@ export async function publicationUnitRows(
90
90
  }
91
91
  return [];
92
92
  }
93
+
94
+ /** Store a generated description on a unit — on its document's row when
95
+ * the document is known, and on the legacy row only when that row IS
96
+ * the same document's: a bare unit id names the last writer, and a
97
+ * caption meant for one publication must never land on another's. */
98
+ export async function writeUnitDescription(db: StoreQuery, unitId: string, description: string, docId?: string | null): Promise<void> {
99
+ if (docId) {
100
+ try {
101
+ await db
102
+ .prepare("UPDATE unit_payloads_doc SET payload = json_set(payload, '$.description', ?1) WHERE unit_id = ?2 AND doc_id = ?3")
103
+ .bind(description, unitId, docId)
104
+ .run();
105
+ } catch {
106
+ // a deployment without the document-scoped table
107
+ }
108
+ }
109
+ await db
110
+ .prepare("UPDATE unit_payloads SET payload = json_set(payload, '$.description', ?1) WHERE unit_id = ?2 AND (?3 IS NULL OR doc_id = ?3)")
111
+ .bind(description, unitId, docId ?? null)
112
+ .run();
113
+ }