@konneal/engine 0.2.188 → 0.2.190
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/dist/ai.d.ts +7 -0
- package/dist/ask-6SWT3KSC.js +16 -0
- package/dist/{chunk-4AP763GX.js → chunk-22WB6IVC.js} +7 -9
- package/dist/{chunk-G435JWGV.js → chunk-5PKKAHY4.js} +5 -5
- package/dist/{chunk-4BQA7PJ3.js → chunk-7YMIUTPY.js} +1 -1
- package/dist/{chunk-L32WSANI.js → chunk-AVNHVFEB.js} +26 -3
- package/dist/{chunk-VT5ZHCOC.js → chunk-ECNBEZGK.js} +5 -5
- package/dist/{chunk-NHYYMRZC.js → chunk-GJQT4IMD.js} +1 -1
- package/dist/{chunk-YGQCVPX6.js → chunk-H6ZULBW2.js} +4 -1
- package/dist/{chunk-SLYQ6L52.js → chunk-Q7QL3457.js} +52 -38
- package/dist/{chunk-A2HQNENL.js → chunk-WRSUXIS7.js} +139 -28
- package/dist/config.d.ts +1 -2
- package/dist/context.d.ts +14 -0
- package/dist/docOverview.d.ts +35 -0
- package/dist/{env-BZWHCSCL.js → env-4LR6Y5CC.js} +1 -1
- package/dist/operations.d.ts +34 -0
- package/dist/prompts/understanding.md +2 -1
- package/dist/reflect.d.ts +1 -1
- package/dist/search-DE5ADCVM.js +14 -0
- package/dist/{tools-CYSL26HX.js → tools-7GKMDWVB.js} +5 -5
- package/dist/understandContract.d.ts +3 -0
- package/dist/worker_mcp/src/index.js +2 -2
- package/dist/worker_public/src/config.js +1 -1
- package/dist/worker_public/src/index.js +11 -11
- package/dist/worker_public/src/requestScope.js +2 -2
- package/package.json +1 -1
- package/workers/worker_public/prompts/understanding.md +2 -1
- package/workers/worker_public/src/ai.ts +30 -6
- package/workers/worker_public/src/ask.ts +118 -21
- package/workers/worker_public/src/config.ts +7 -9
- package/workers/worker_public/src/context.ts +14 -0
- package/workers/worker_public/src/docOverview.ts +77 -0
- package/workers/worker_public/src/faithfulness.ts +11 -1
- package/workers/worker_public/src/lexical.ts +21 -19
- package/workers/worker_public/src/operations.ts +40 -0
- package/workers/worker_public/src/ports/cloudflare/adapters.ts +4 -2
- package/workers/worker_public/src/reflect.ts +8 -7
- package/workers/worker_public/src/stages/dense.ts +29 -42
- package/workers/worker_public/src/stages/glossary.ts +1 -1
- package/workers/worker_public/src/stages/rerank.ts +6 -2
- package/workers/worker_public/src/understandContract.ts +4 -0
- package/dist/ask-TJK7DQ45.js +0 -16
- package/dist/search-FJAOGMEO.js +0 -14
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
// The prompt is data (prompts/reflect.md), bundled as text.
|
|
7
7
|
import reflectPrompt from "../prompts/reflect.md";
|
|
8
|
+
import { generationSampling } from "./config";
|
|
8
9
|
|
|
9
10
|
export interface ReflectionResult {
|
|
10
11
|
grounded: boolean;
|
|
@@ -17,12 +18,16 @@ export async function reflect(
|
|
|
17
18
|
question: string,
|
|
18
19
|
answer: string,
|
|
19
20
|
passages: string[],
|
|
21
|
+
notes?: string,
|
|
20
22
|
): Promise<ReflectionResult | null> {
|
|
21
23
|
if (!answer || !passages.length) return null;
|
|
24
|
+
// 900 chars per passage clears the ~250-char contextual-enrichment prefix
|
|
25
|
+
// so the critic sees the actual clause text alongside the authoritative notes.
|
|
22
26
|
const ctx = passages
|
|
23
27
|
.slice(0, 8)
|
|
24
|
-
.map((p, i) => `[${i + 1}] ${p.replace(/\s+/g, " ").slice(0,
|
|
28
|
+
.map((p, i) => `[${i + 1}] ${p.replace(/\s+/g, " ").slice(0, 900)}`)
|
|
25
29
|
.join("\n");
|
|
30
|
+
const noteBlock = notes?.trim() ? `\n\nAuthoritative notes:\n${notes.trim().slice(0, 1500)}` : "";
|
|
26
31
|
|
|
27
32
|
const timeout = new Promise<null>((r) => setTimeout(() => r(null), 6000));
|
|
28
33
|
const call = (async () => {
|
|
@@ -34,16 +39,12 @@ export async function reflect(
|
|
|
34
39
|
},
|
|
35
40
|
{
|
|
36
41
|
role: "user",
|
|
37
|
-
content: `Question: ${question}\n\nAnswer:\n${answer.slice(0, 1500)}\n\nPassages:\n${ctx}`,
|
|
42
|
+
content: `Question: ${question}\n\nAnswer:\n${answer.slice(0, 1500)}\n\nPassages:\n${ctx}${noteBlock}`,
|
|
38
43
|
},
|
|
39
44
|
],
|
|
40
|
-
// reasoning shares this budget — starved budgets silently disable
|
|
41
|
-
// the reflection layer (null = no retry ever fires); DeepSeek-V4
|
|
42
|
-
// card: keep reasoning on with headroom + temp 1.0 / top_p 1.0
|
|
43
45
|
max_tokens: 3072,
|
|
44
46
|
reasoning_effort: "low",
|
|
45
|
-
|
|
46
|
-
top_p: 1.0,
|
|
47
|
+
...generationSampling(model),
|
|
47
48
|
});
|
|
48
49
|
const text = typeof res?.response === "string" ? res.response : res?.choices?.[0]?.message?.content;
|
|
49
50
|
const m = (text ?? "").match(/\{[\s\S]*\}/);
|
|
@@ -3,18 +3,19 @@
|
|
|
3
3
|
// query, no filter — bit-for-bit same candidates, one serial round-trip
|
|
4
4
|
// saved), the doc/edition-filtered query with its guessed-pin drop and
|
|
5
5
|
// sparse-filter widen, and the plain unfiltered query.
|
|
6
|
-
import { LIMITS
|
|
7
|
-
import { matchesDocScope } from "../context.ts";
|
|
6
|
+
import { LIMITS } from "../config.ts";
|
|
7
|
+
import { docScopeFilter, matchesDocScope } from "../context.ts";
|
|
8
8
|
import type { Stage } from "./types.ts";
|
|
9
9
|
|
|
10
10
|
export const dense: Stage = {
|
|
11
11
|
name: "dense",
|
|
12
12
|
run: async (c) => {
|
|
13
13
|
const { env, filters, vector, opts, rq, folded } = c;
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
// the
|
|
17
|
-
// the
|
|
14
|
+
// The scope does not ride the first query: the platform's metadata
|
|
15
|
+
// filter answers only on the fields the index has indexed for it, so
|
|
16
|
+
// the unfiltered top-K is scoped in code, and the document's own
|
|
17
|
+
// nearest chunks come from the native filter only when that scope
|
|
18
|
+
// starves (below).
|
|
18
19
|
const q: any = { topK: LIMITS.retrieveK, returnMetadata: "all" };
|
|
19
20
|
const optimistic = opts.optimisticHits ?? [];
|
|
20
21
|
const sameLane = rq === folded; // optimistic vector === this lane's vector
|
|
@@ -36,48 +37,34 @@ export const dense: Stage = {
|
|
|
36
37
|
}
|
|
37
38
|
if (filters?.doc_number) {
|
|
38
39
|
const allMatches: any[] = (await env.VECTORIZE.query(vector, q)).matches ?? [];
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
// the asked document, so the scope cut to nothing and the answer
|
|
47
|
-
// ran on other lanes' leftovers): re-query at a deep topK and
|
|
48
|
-
// scope in code — the NATIVE metadata filter is dead on this
|
|
49
|
-
// platform (measured 2026-10-05: filters return empty for values
|
|
50
|
-
// proven present), so the document's own nearest neighbors are
|
|
51
|
-
// reachable only through a wider net. Fires only when the scope
|
|
52
|
-
// starves to zero — zero behavior when the scope matches.
|
|
53
|
-
if (!matches.length) {
|
|
40
|
+
const before = allMatches.length;
|
|
41
|
+
let docMatches = allMatches.filter((m) => matchesDocScope(m.metadata ?? {}, filters.doc_number!));
|
|
42
|
+
// When the global top-K holds fewer than rerankKeep hits for the named
|
|
43
|
+
// doc_number (e.g. 0 hits, or 1 stray R 29 hit when asking about D 29),
|
|
44
|
+
// query Vectorize's native doc_number filter before widening to unrelated
|
|
45
|
+
// documents.
|
|
46
|
+
if (docMatches.length < LIMITS.rerankKeep) {
|
|
54
47
|
try {
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
48
|
+
const own: any = await env.VECTORIZE.query(vector, { ...q, filter: docScopeFilter(filters.doc_number) });
|
|
49
|
+
const ownMatches = (own.matches ?? []).filter((m: any) => matchesDocScope(m.metadata ?? {}, filters.doc_number!));
|
|
50
|
+
const seen = new Set(docMatches.map((m: any) => m.id));
|
|
51
|
+
const added = ownMatches.filter((m: any) => !seen.has(m.id));
|
|
52
|
+
if (added.length) {
|
|
53
|
+
docMatches = [...docMatches, ...added];
|
|
54
|
+
console.log("dense scope sparse → native doc filter:", docMatches.length, "doc members");
|
|
55
|
+
}
|
|
58
56
|
} catch (e) {
|
|
59
|
-
// the deep net must never break the ask: a rejected re-query
|
|
60
|
-
// (topK above the platform cap, a transient index error)
|
|
61
|
-
// degrades to the pre-fallback state — other lanes' leftovers
|
|
62
57
|
console.log("dense scope starvation re-query failed:", String(e).slice(0, 120));
|
|
63
58
|
}
|
|
64
59
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
// editions EXIST: a wrong pin starves the doc filter and the
|
|
70
|
-
// widen then floods the pool with superseded editions. Drop to
|
|
71
|
-
// the doc-only filter and let family-relative steering rank
|
|
72
|
-
// editions downstream. A pin the user actually asked for
|
|
73
|
-
// survives — its edition exists in the corpus.
|
|
74
|
-
// the same guess-drop, in code: a pinned edition that starved the
|
|
75
|
-
// scoped set drops to the doc-only scope
|
|
60
|
+
let matches: any[] = filters.edition
|
|
61
|
+
? docMatches.filter((m) => !m.metadata?.edition || String(m.metadata.edition) === filters.edition)
|
|
62
|
+
: docMatches;
|
|
63
|
+
console.log("dense scope (in code):", before, "→", matches.length, "in doc", filters.doc_number, filters.edition ? `@${filters.edition}` : "");
|
|
76
64
|
if (filters && filters.edition && matches.length < 3) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
matches = docOnly;
|
|
65
|
+
if (docMatches.length > matches.length) {
|
|
66
|
+
console.log("edition pin dropped:", filters.doc_number, "@", filters.edition, "→", docMatches.length, "doc-scoped hits (edition not in corpus)");
|
|
67
|
+
matches = docMatches;
|
|
81
68
|
filters.edition = undefined;
|
|
82
69
|
}
|
|
83
70
|
}
|
|
@@ -39,7 +39,7 @@ export const glossary: Stage = {
|
|
|
39
39
|
definition: String(m.metadata?.chunk_text ?? "").split(" — ").slice(1).join(" — ").slice(0, 300),
|
|
40
40
|
docidentifier: String(m.metadata?.docidentifier ?? ""),
|
|
41
41
|
doc_number: String(m.metadata?.doc_number ?? ""),
|
|
42
|
-
score: rs ? rs[i] : m.score,
|
|
42
|
+
score: rs && Number.isFinite(rs[i]) ? rs[i] : m.score,
|
|
43
43
|
}))
|
|
44
44
|
.filter((x: any) => x.term && x.definition);
|
|
45
45
|
})();
|
|
@@ -26,7 +26,11 @@ export const rerankStage: Stage = {
|
|
|
26
26
|
const scores = await rerank(portModelRunner(c.env), MODELS.rerank, c.query, c.hits.map((h) => h.text));
|
|
27
27
|
console.log("stage: rerank", Date.now() - tRerank, "ms over", c.hits.length, "candidates");
|
|
28
28
|
if (scores) {
|
|
29
|
-
|
|
29
|
+
const finite = scores.filter((s) => Number.isFinite(s));
|
|
30
|
+
const minFinite = finite.length ? Math.min(...finite) : 0;
|
|
31
|
+
const span = finite.length ? Math.max(...finite) - minFinite : 0;
|
|
32
|
+
const floor = minFinite - Math.max(0.01, span);
|
|
33
|
+
c.hits.forEach((h, i) => (h.rerank_score = Number.isFinite(scores[i]) ? scores[i] : floor));
|
|
30
34
|
c.hits.sort((a, b) => (b.rerank_score ?? -Infinity) - (a.rerank_score ?? -Infinity));
|
|
31
35
|
if (c.filter?.doc_number) {
|
|
32
36
|
// pin the scoped doc's OWN front matter: the synthetic family
|
|
@@ -38,7 +42,7 @@ export const rerankStage: Stage = {
|
|
|
38
42
|
const families = c.hits.filter((h) => h.metadata.clause_anchor === "family" && String(h.metadata.doc_number ?? "").split("-")[0] === String(c.filter!.doc_number).split("-")[0]);
|
|
39
43
|
const own = families.length
|
|
40
44
|
? families
|
|
41
|
-
: c.hits.filter((h) => String(h.metadata.doc_number ?? "") === String(c.filter!.doc_number).split("-")[0] && /^(overview|)$/i.test(String(h.metadata.clause_anchor ?? "")));
|
|
45
|
+
: c.hits.filter((h) => h.text.trim().length > 0 && h.metadata.doctype !== "certificate" && String(h.metadata.doc_number ?? "") === String(c.filter!.doc_number).split("-")[0] && /^(overview|)$/i.test(String(h.metadata.clause_anchor ?? "")));
|
|
42
46
|
if (own.length) {
|
|
43
47
|
const ownIds = new Set(own.map((h) => h.id));
|
|
44
48
|
c.hits = [...own, ...c.hits.filter((h) => !ownIds.has(h.id))];
|
|
@@ -13,6 +13,9 @@ export interface QueryUnderstanding {
|
|
|
13
13
|
language?: string | null;
|
|
14
14
|
/** the question is about a process around publications (certify, apply…) */
|
|
15
15
|
process_intent: boolean;
|
|
16
|
+
/** the question asks what the NAMED publication is or covers as a whole
|
|
17
|
+
* (an overview of the document), not something inside it */
|
|
18
|
+
doc_overview: boolean;
|
|
16
19
|
/** definition-style question whose subject is `term` */
|
|
17
20
|
term: string | null;
|
|
18
21
|
/** corpus-terminology mapping of everyday wording (drift→creep) */
|
|
@@ -44,6 +47,7 @@ export function extractJson(text: string): QueryUnderstanding | null {
|
|
|
44
47
|
edition: typeof raw.edition === "string" && /^\d{4}$/.test(raw.edition) ? raw.edition : null,
|
|
45
48
|
language: typeof raw.language === "string" && /^[a-z]{2}$/.test(raw.language) ? raw.language : null,
|
|
46
49
|
process_intent: raw.process_intent === true,
|
|
50
|
+
doc_overview: raw.doc_overview === true,
|
|
47
51
|
term: typeof raw.term === "string" && raw.term.trim() ? raw.term.trim().slice(0, 60) : null,
|
|
48
52
|
defined_terms: Array.isArray(raw.defined_terms)
|
|
49
53
|
? raw.defined_terms.filter((t: unknown) => typeof t === "string" && (t as string).trim()).map((t: string) => t.trim().slice(0, 60)).slice(0, 4)
|
package/dist/ask-TJK7DQ45.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
handleAsk
|
|
3
|
-
} from "./chunk-A2HQNENL.js";
|
|
4
|
-
import "./chunk-IRCDOFP4.js";
|
|
5
|
-
import "./chunk-VT5ZHCOC.js";
|
|
6
|
-
import "./chunk-SLYQ6L52.js";
|
|
7
|
-
import "./chunk-L32WSANI.js";
|
|
8
|
-
import "./chunk-NHYYMRZC.js";
|
|
9
|
-
import "./chunk-ETLSWSZ7.js";
|
|
10
|
-
import "./chunk-4BQA7PJ3.js";
|
|
11
|
-
import "./chunk-4AP763GX.js";
|
|
12
|
-
import "./chunk-3FYJM7LH.js";
|
|
13
|
-
import "./chunk-YGQCVPX6.js";
|
|
14
|
-
export {
|
|
15
|
-
handleAsk
|
|
16
|
-
};
|
package/dist/search-FJAOGMEO.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
handleSearch
|
|
3
|
-
} from "./chunk-G435JWGV.js";
|
|
4
|
-
import "./chunk-SLYQ6L52.js";
|
|
5
|
-
import "./chunk-L32WSANI.js";
|
|
6
|
-
import "./chunk-NHYYMRZC.js";
|
|
7
|
-
import "./chunk-ETLSWSZ7.js";
|
|
8
|
-
import "./chunk-4BQA7PJ3.js";
|
|
9
|
-
import "./chunk-4AP763GX.js";
|
|
10
|
-
import "./chunk-3FYJM7LH.js";
|
|
11
|
-
import "./chunk-YGQCVPX6.js";
|
|
12
|
-
export {
|
|
13
|
-
handleSearch
|
|
14
|
-
};
|