@shobyde/boaz 0.0.0-stage → 0.8.2
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/LICENSE +21 -0
- package/README.md +422 -2
- package/dist/build/codes.d.ts +16 -0
- package/dist/build/codes.d.ts.map +1 -0
- package/dist/build/codes.js +72 -0
- package/dist/build/codes.js.map +1 -0
- package/dist/build/extract.d.ts +45 -0
- package/dist/build/extract.d.ts.map +1 -0
- package/dist/build/extract.js +484 -0
- package/dist/build/extract.js.map +1 -0
- package/dist/build/glob.d.ts +11 -0
- package/dist/build/glob.d.ts.map +1 -0
- package/dist/build/glob.js +88 -0
- package/dist/build/glob.js.map +1 -0
- package/dist/build/index.d.ts +7 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +4 -0
- package/dist/build/index.js.map +1 -0
- package/dist/build/probes.d.ts +41 -0
- package/dist/build/probes.d.ts.map +1 -0
- package/dist/build/probes.js +134 -0
- package/dist/build/probes.js.map +1 -0
- package/dist/build/report.d.ts +11 -0
- package/dist/build/report.d.ts.map +1 -0
- package/dist/build/report.js +39 -0
- package/dist/build/report.js.map +1 -0
- package/dist/cli/audit.d.ts +15 -0
- package/dist/cli/audit.d.ts.map +1 -0
- package/dist/cli/audit.js +68 -0
- package/dist/cli/audit.js.map +1 -0
- package/dist/cli/bench.d.ts +7 -0
- package/dist/cli/bench.d.ts.map +1 -0
- package/dist/cli/bench.js +147 -0
- package/dist/cli/bench.js.map +1 -0
- package/dist/cli/harness.d.ts +45 -0
- package/dist/cli/harness.d.ts.map +1 -0
- package/dist/cli/harness.js +90 -0
- package/dist/cli/harness.js.map +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +404 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/probes.d.ts +20 -0
- package/dist/cli/probes.d.ts.map +1 -0
- package/dist/cli/probes.js +27 -0
- package/dist/cli/probes.js.map +1 -0
- package/dist/cli/skill-template.d.ts +2 -0
- package/dist/cli/skill-template.d.ts.map +1 -0
- package/dist/cli/skill-template.js +254 -0
- package/dist/cli/skill-template.js.map +1 -0
- package/dist/core/actions.d.ts +67 -0
- package/dist/core/actions.d.ts.map +1 -0
- package/dist/core/actions.js +357 -0
- package/dist/core/actions.js.map +1 -0
- package/dist/core/define.d.ts +25 -0
- package/dist/core/define.d.ts.map +1 -0
- package/dist/core/define.js +76 -0
- package/dist/core/define.js.map +1 -0
- package/dist/core/execute.d.ts +75 -0
- package/dist/core/execute.d.ts.map +1 -0
- package/dist/core/execute.js +145 -0
- package/dist/core/execute.js.map +1 -0
- package/dist/core/index.d.ts +35 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +20 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/lemmas.d.ts +32 -0
- package/dist/core/lemmas.d.ts.map +1 -0
- package/dist/core/lemmas.js +80 -0
- package/dist/core/lemmas.js.map +1 -0
- package/dist/core/lexical-engine.d.ts +18 -0
- package/dist/core/lexical-engine.d.ts.map +1 -0
- package/dist/core/lexical-engine.js +296 -0
- package/dist/core/lexical-engine.js.map +1 -0
- package/dist/core/loop.d.ts +51 -0
- package/dist/core/loop.d.ts.map +1 -0
- package/dist/core/loop.js +432 -0
- package/dist/core/loop.js.map +1 -0
- package/dist/core/mcp.d.ts +119 -0
- package/dist/core/mcp.d.ts.map +1 -0
- package/dist/core/mcp.js +253 -0
- package/dist/core/mcp.js.map +1 -0
- package/dist/core/memory.d.ts +106 -0
- package/dist/core/memory.d.ts.map +1 -0
- package/dist/core/memory.js +185 -0
- package/dist/core/memory.js.map +1 -0
- package/dist/core/plan.d.ts +83 -0
- package/dist/core/plan.d.ts.map +1 -0
- package/dist/core/plan.js +258 -0
- package/dist/core/plan.js.map +1 -0
- package/dist/core/preferences.d.ts +54 -0
- package/dist/core/preferences.d.ts.map +1 -0
- package/dist/core/preferences.js +83 -0
- package/dist/core/preferences.js.map +1 -0
- package/dist/core/prompt.d.ts +23 -0
- package/dist/core/prompt.d.ts.map +1 -0
- package/dist/core/prompt.js +76 -0
- package/dist/core/prompt.js.map +1 -0
- package/dist/core/question.d.ts +96 -0
- package/dist/core/question.d.ts.map +1 -0
- package/dist/core/question.js +501 -0
- package/dist/core/question.js.map +1 -0
- package/dist/core/registry.d.ts +33 -0
- package/dist/core/registry.d.ts.map +1 -0
- package/dist/core/registry.js +72 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/retriever.d.ts +48 -0
- package/dist/core/retriever.d.ts.map +1 -0
- package/dist/core/retriever.js +123 -0
- package/dist/core/retriever.js.map +1 -0
- package/dist/core/schema.d.ts +27 -0
- package/dist/core/schema.d.ts.map +1 -0
- package/dist/core/schema.js +231 -0
- package/dist/core/schema.js.map +1 -0
- package/dist/core/skill.d.ts +105 -0
- package/dist/core/skill.d.ts.map +1 -0
- package/dist/core/skill.js +205 -0
- package/dist/core/skill.js.map +1 -0
- package/dist/core/summarise.d.ts +52 -0
- package/dist/core/summarise.d.ts.map +1 -0
- package/dist/core/summarise.js +146 -0
- package/dist/core/summarise.js.map +1 -0
- package/dist/core/tools.d.ts +21 -0
- package/dist/core/tools.d.ts.map +1 -0
- package/dist/core/tools.js +120 -0
- package/dist/core/tools.js.map +1 -0
- package/dist/core/types.d.ts +199 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +5 -0
- package/dist/core/types.js.map +1 -0
- package/dist/dom/index.d.ts +22 -0
- package/dist/dom/index.d.ts.map +1 -0
- package/dist/dom/index.js +94 -0
- package/dist/dom/index.js.map +1 -0
- package/dist/extract/index.d.ts +36 -0
- package/dist/extract/index.d.ts.map +1 -0
- package/dist/extract/index.js +86 -0
- package/dist/extract/index.js.map +1 -0
- package/dist/react/index.d.ts +52 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +131 -0
- package/dist/react/index.js.map +1 -0
- package/dist/rules/engine.d.ts +152 -0
- package/dist/rules/engine.d.ts.map +1 -0
- package/dist/rules/engine.js +364 -0
- package/dist/rules/engine.js.map +1 -0
- package/dist/rules/facts.d.ts +63 -0
- package/dist/rules/facts.d.ts.map +1 -0
- package/dist/rules/facts.js +115 -0
- package/dist/rules/facts.js.map +1 -0
- package/dist/rules/index.d.ts +10 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +5 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/rules/knowledge.d.ts +78 -0
- package/dist/rules/knowledge.d.ts.map +1 -0
- package/dist/rules/knowledge.js +254 -0
- package/dist/rules/knowledge.js.map +1 -0
- package/dist/vite/index.d.ts +26 -0
- package/dist/vite/index.d.ts.map +1 -0
- package/dist/vite/index.js +112 -0
- package/dist/vite/index.js.map +1 -0
- package/package.json +108 -4
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export interface SkillStep {
|
|
2
|
+
/** Used by checks and by later steps that read this result. */
|
|
3
|
+
id: string;
|
|
4
|
+
capabilityId: string;
|
|
5
|
+
title?: string;
|
|
6
|
+
/** Arguments the skill itself supplies (never read from the question). */
|
|
7
|
+
args?: Record<string, unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* One argument taken from an earlier step's result: `{ step: 'sections',
|
|
10
|
+
* path: 'text', as: 'text' }`. The runner passes the resolved value as an
|
|
11
|
+
* argument, so a step can consume what the previous step produced.
|
|
12
|
+
*/
|
|
13
|
+
input?: {
|
|
14
|
+
step: string;
|
|
15
|
+
path: string;
|
|
16
|
+
as: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export type SkillCheckKind = 'min-items' | 'max-items' | 'distinct' | 'verbatim' | 'non-empty';
|
|
20
|
+
export interface SkillCheck {
|
|
21
|
+
id: string;
|
|
22
|
+
/** The step whose result this check reads. */
|
|
23
|
+
after: string;
|
|
24
|
+
kind: SkillCheckKind;
|
|
25
|
+
/** Dot path into that result. `.length` works because arrays have one. */
|
|
26
|
+
path: string;
|
|
27
|
+
/** Threshold for min-items / max-items / distinct (max similarity). */
|
|
28
|
+
value?: number;
|
|
29
|
+
/** Field of each item for distinct and verbatim. Default `text`. */
|
|
30
|
+
field?: string;
|
|
31
|
+
/** For verbatim: the path holding the source text the item must appear in. */
|
|
32
|
+
against?: string;
|
|
33
|
+
/** One plain sentence: what the check saw, shown when it passes or fails. */
|
|
34
|
+
message: string;
|
|
35
|
+
}
|
|
36
|
+
export interface Skill {
|
|
37
|
+
id: string;
|
|
38
|
+
title: string;
|
|
39
|
+
trigger: {
|
|
40
|
+
/** Phrases a visitor would type. Matched whole, case and punctuation folded. */
|
|
41
|
+
phrases: string[];
|
|
42
|
+
/** Optional extra condition, e.g. a shape the phrases cannot express. */
|
|
43
|
+
require?: RegExp;
|
|
44
|
+
};
|
|
45
|
+
steps: SkillStep[];
|
|
46
|
+
checks: SkillCheck[];
|
|
47
|
+
stop: {
|
|
48
|
+
/** What to do when a check fails. Default `stop`. */
|
|
49
|
+
onCheckFail?: 'stop' | 'continue';
|
|
50
|
+
/** Cap on executed steps. Default 4. */
|
|
51
|
+
maxSteps?: number;
|
|
52
|
+
};
|
|
53
|
+
/** The host's own wording. The library decides the shape, not the prose. */
|
|
54
|
+
compose?: (run: SkillRun) => string;
|
|
55
|
+
/** The plain-English "how" line under an answer. */
|
|
56
|
+
explain?: (run: SkillRun) => string;
|
|
57
|
+
}
|
|
58
|
+
export interface SkillStepRecord {
|
|
59
|
+
id: string;
|
|
60
|
+
capabilityId: string;
|
|
61
|
+
title: string;
|
|
62
|
+
status: 'ran' | 'skipped' | 'failed';
|
|
63
|
+
reason?: 'check-failed' | 'previous-failed' | 'step-limit' | 'missing-input';
|
|
64
|
+
args?: Record<string, unknown>;
|
|
65
|
+
result?: unknown;
|
|
66
|
+
error?: string;
|
|
67
|
+
}
|
|
68
|
+
export interface SkillCheckRecord extends SkillCheck {
|
|
69
|
+
ok: boolean;
|
|
70
|
+
/** What the check actually read, so a failure is inspectable. */
|
|
71
|
+
observed: string;
|
|
72
|
+
}
|
|
73
|
+
export interface SkillRun {
|
|
74
|
+
skillId: string;
|
|
75
|
+
title: string;
|
|
76
|
+
steps: SkillStepRecord[];
|
|
77
|
+
checks: SkillCheckRecord[];
|
|
78
|
+
/** True when every step ran and every check passed. */
|
|
79
|
+
ok: boolean;
|
|
80
|
+
/** The check that stopped the run, if one did. */
|
|
81
|
+
stopped?: {
|
|
82
|
+
check: string;
|
|
83
|
+
message: string;
|
|
84
|
+
};
|
|
85
|
+
failedAt?: string;
|
|
86
|
+
}
|
|
87
|
+
export interface RunSkillOptions {
|
|
88
|
+
/** Runs one capability by id. The host's ordinary path, never a second one. */
|
|
89
|
+
call: (capabilityId: string, args: Record<string, unknown>) => Promise<unknown> | unknown;
|
|
90
|
+
maxSteps?: number;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The skill whose trigger phrase the question contains. Longest phrase wins, so
|
|
94
|
+
* "summarise this page in three points" beats a skill triggered by "summarise".
|
|
95
|
+
* A phrase has to appear as whole words, or "page" triggers on "pager".
|
|
96
|
+
*/
|
|
97
|
+
export declare function matchSkill(skill: Skill, query: string): string | null;
|
|
98
|
+
export declare function selectSkill(skills: Skill[], query: string): Skill | null;
|
|
99
|
+
/**
|
|
100
|
+
* Runs a skill against the host's capability path. Steps run in order; a check
|
|
101
|
+
* that fails stops the run before the step it guards, which is what keeps a
|
|
102
|
+
* short page from being padded to three points.
|
|
103
|
+
*/
|
|
104
|
+
export declare function runSkill(skill: Skill, options: RunSkillOptions): Promise<SkillRun>;
|
|
105
|
+
//# sourceMappingURL=skill.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"skill.d.ts","sourceRoot":"","sources":["../../src/core/skill.ts"],"names":[],"mappings":"AAqBA,MAAM,WAAW,SAAS;IACxB,+DAA+D;IAC/D,EAAE,EAAE,MAAM,CAAC;IACX,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,0EAA0E;IAC1E,IAAI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I,CAUrE;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,CAQxE;AAsED;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC,CA0ExF"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/* ============================================================
|
|
2
|
+
Declarative skills without a model.
|
|
3
|
+
|
|
4
|
+
A plan (0.7.6) is split off one sentence and runs once. A skill is
|
|
5
|
+
the thing a host needs when the task has a shape the visitor did not
|
|
6
|
+
spell out: a trigger, two or more capability calls where a later call
|
|
7
|
+
consumes an earlier one's output, checks between them, and a stop
|
|
8
|
+
condition that fires before the skill invents work it cannot do.
|
|
9
|
+
|
|
10
|
+
Everything here is data plus a runner. There is no code in a skill
|
|
11
|
+
beyond the host's own compose/explain functions, so what a skill will
|
|
12
|
+
do can be read off the file and audited the way a capability is.
|
|
13
|
+
|
|
14
|
+
The interesting property is the stop condition: "summarise this page
|
|
15
|
+
in three points" on a page with two sentences must say it cannot,
|
|
16
|
+
rather than pad. That is a check, and it runs before the step that
|
|
17
|
+
would pad.
|
|
18
|
+
============================================================ */
|
|
19
|
+
import { tokenize } from './retriever.js';
|
|
20
|
+
function normalise(text) {
|
|
21
|
+
return String(text)
|
|
22
|
+
.toLowerCase()
|
|
23
|
+
.replace(/[\u2018\u2019]/g, "'")
|
|
24
|
+
.replace(/[^a-z0-9' ]+/g, ' ')
|
|
25
|
+
.replace(/\s+/g, ' ')
|
|
26
|
+
.trim();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The skill whose trigger phrase the question contains. Longest phrase wins, so
|
|
30
|
+
* "summarise this page in three points" beats a skill triggered by "summarise".
|
|
31
|
+
* A phrase has to appear as whole words, or "page" triggers on "pager".
|
|
32
|
+
*/
|
|
33
|
+
export function matchSkill(skill, query) {
|
|
34
|
+
if (skill.trigger.require && !skill.trigger.require.test(query))
|
|
35
|
+
return null;
|
|
36
|
+
const haystack = ` ${normalise(query)} `;
|
|
37
|
+
let best = null;
|
|
38
|
+
for (const phrase of skill.trigger.phrases) {
|
|
39
|
+
const needle = ` ${normalise(phrase)} `;
|
|
40
|
+
if (!needle.trim())
|
|
41
|
+
continue;
|
|
42
|
+
if (haystack.includes(needle) && (!best || phrase.length > best.length))
|
|
43
|
+
best = phrase;
|
|
44
|
+
}
|
|
45
|
+
return best;
|
|
46
|
+
}
|
|
47
|
+
export function selectSkill(skills, query) {
|
|
48
|
+
let winner = null;
|
|
49
|
+
for (const skill of skills) {
|
|
50
|
+
const phrase = matchSkill(skill, query);
|
|
51
|
+
if (!phrase)
|
|
52
|
+
continue;
|
|
53
|
+
if (!winner || phrase.length > winner.phrase.length)
|
|
54
|
+
winner = { skill, phrase };
|
|
55
|
+
}
|
|
56
|
+
return winner?.skill ?? null;
|
|
57
|
+
}
|
|
58
|
+
function getPath(value, path) {
|
|
59
|
+
if (!path)
|
|
60
|
+
return value;
|
|
61
|
+
let cursor = value;
|
|
62
|
+
for (const key of path.split('.')) {
|
|
63
|
+
if (cursor === null || cursor === undefined)
|
|
64
|
+
return undefined;
|
|
65
|
+
cursor = cursor[key];
|
|
66
|
+
}
|
|
67
|
+
return cursor;
|
|
68
|
+
}
|
|
69
|
+
function items(value) {
|
|
70
|
+
return Array.isArray(value) ? value : [];
|
|
71
|
+
}
|
|
72
|
+
/** Token-set Jaccard. Two items at or above the threshold are the same point. */
|
|
73
|
+
function similarity(a, b) {
|
|
74
|
+
const left = new Set(tokenize(a));
|
|
75
|
+
const right = new Set(tokenize(b));
|
|
76
|
+
if (left.size === 0 || right.size === 0)
|
|
77
|
+
return 0;
|
|
78
|
+
let shared = 0;
|
|
79
|
+
for (const token of left)
|
|
80
|
+
if (right.has(token))
|
|
81
|
+
shared += 1;
|
|
82
|
+
return shared / (left.size + right.size - shared);
|
|
83
|
+
}
|
|
84
|
+
function evaluate(check, result) {
|
|
85
|
+
const at = getPath(result, check.path);
|
|
86
|
+
const field = check.field ?? 'text';
|
|
87
|
+
switch (check.kind) {
|
|
88
|
+
case 'min-items': {
|
|
89
|
+
const count = Array.isArray(at) ? at.length : typeof at === 'number' ? at : 0;
|
|
90
|
+
return { ok: count >= (check.value ?? 1), observed: `${count} item${count === 1 ? '' : 's'}` };
|
|
91
|
+
}
|
|
92
|
+
case 'max-items': {
|
|
93
|
+
const count = Array.isArray(at) ? at.length : typeof at === 'number' ? at : 0;
|
|
94
|
+
return { ok: count <= (check.value ?? 0), observed: `${count} item${count === 1 ? '' : 's'}` };
|
|
95
|
+
}
|
|
96
|
+
case 'non-empty': {
|
|
97
|
+
const filled = Array.isArray(at) ? at.length > 0 : typeof at === 'string' ? at.trim().length > 0 : at !== undefined && at !== null;
|
|
98
|
+
return { ok: filled, observed: filled ? 'present' : 'empty' };
|
|
99
|
+
}
|
|
100
|
+
case 'distinct': {
|
|
101
|
+
const rows = items(at).map((row) => String(row[field] ?? ''));
|
|
102
|
+
const limit = check.value ?? 0.8;
|
|
103
|
+
for (let i = 0; i < rows.length; i += 1) {
|
|
104
|
+
for (let j = i + 1; j < rows.length; j += 1) {
|
|
105
|
+
const score = similarity(rows[i], rows[j]);
|
|
106
|
+
if (score >= limit) {
|
|
107
|
+
return { ok: false, observed: `items ${i + 1} and ${j + 1} repeat each other (${score.toFixed(2)})` };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return { ok: true, observed: `${rows.length} items, no pair above ${limit}` };
|
|
112
|
+
}
|
|
113
|
+
case 'verbatim': {
|
|
114
|
+
const source = normalise(String(getPath(result, check.against ?? 'text') ?? ''));
|
|
115
|
+
const rows = items(at);
|
|
116
|
+
const missing = rows.filter((row) => {
|
|
117
|
+
const text = normalise(String(row[field] ?? ''));
|
|
118
|
+
return text.length > 0 && !source.includes(text);
|
|
119
|
+
});
|
|
120
|
+
return {
|
|
121
|
+
ok: missing.length === 0,
|
|
122
|
+
observed: missing.length === 0 ? `${rows.length} quoted` : `${missing.length} not in the source`,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Runs a skill against the host's capability path. Steps run in order; a check
|
|
129
|
+
* that fails stops the run before the step it guards, which is what keeps a
|
|
130
|
+
* short page from being padded to three points.
|
|
131
|
+
*/
|
|
132
|
+
export async function runSkill(skill, options) {
|
|
133
|
+
const maxSteps = Math.max(1, options.maxSteps ?? skill.stop.maxSteps ?? 4);
|
|
134
|
+
const onCheckFail = skill.stop.onCheckFail ?? 'stop';
|
|
135
|
+
const steps = [];
|
|
136
|
+
const checks = [];
|
|
137
|
+
const results = new Map();
|
|
138
|
+
const record = (step) => {
|
|
139
|
+
steps.push(step);
|
|
140
|
+
return step;
|
|
141
|
+
};
|
|
142
|
+
let stopped;
|
|
143
|
+
let ran = 0;
|
|
144
|
+
for (const step of skill.steps) {
|
|
145
|
+
const title = step.title ?? step.capabilityId;
|
|
146
|
+
const base = { id: step.id, capabilityId: step.capabilityId, title };
|
|
147
|
+
if (stopped) {
|
|
148
|
+
record({ ...base, status: 'skipped', reason: 'check-failed' });
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (steps.some((entry) => entry.status === 'failed')) {
|
|
152
|
+
record({ ...base, status: 'skipped', reason: 'previous-failed' });
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (ran >= maxSteps) {
|
|
156
|
+
record({ ...base, status: 'skipped', reason: 'step-limit' });
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
let args = { ...(step.args ?? {}) };
|
|
160
|
+
if (step.input) {
|
|
161
|
+
const value = getPath(results.get(step.input.step), step.input.path);
|
|
162
|
+
if (value === undefined) {
|
|
163
|
+
record({ ...base, status: 'skipped', reason: 'missing-input' });
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
args = { ...args, [step.input.as]: value };
|
|
167
|
+
}
|
|
168
|
+
let result;
|
|
169
|
+
try {
|
|
170
|
+
result = await options.call(step.capabilityId, args);
|
|
171
|
+
results.set(step.id, result);
|
|
172
|
+
record({ ...base, status: 'ran', args, result });
|
|
173
|
+
ran += 1;
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
record({
|
|
177
|
+
...base,
|
|
178
|
+
status: 'failed',
|
|
179
|
+
args,
|
|
180
|
+
error: error instanceof Error ? error.message : String(error),
|
|
181
|
+
});
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const guards = skill.checks.filter((check) => check.after === step.id);
|
|
185
|
+
for (const check of guards) {
|
|
186
|
+
const outcome = evaluate(check, result);
|
|
187
|
+
checks.push({ ...check, ...outcome });
|
|
188
|
+
if (!outcome.ok && onCheckFail === 'stop') {
|
|
189
|
+
stopped = { check: check.id, message: check.message };
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const failed = steps.find((step) => step.status === 'failed');
|
|
195
|
+
return {
|
|
196
|
+
skillId: skill.id,
|
|
197
|
+
title: skill.title,
|
|
198
|
+
steps,
|
|
199
|
+
checks,
|
|
200
|
+
ok: !failed && !stopped && checks.every((check) => check.ok),
|
|
201
|
+
...(stopped ? { stopped } : {}),
|
|
202
|
+
...(failed ? { failedAt: failed.id } : {}),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=skill.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"skill.js","sourceRoot":"","sources":["../../src/core/skill.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;kEAiBkE;AAElE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA8F1C,SAAS,SAAS,CAAC,IAAY;IAC7B,OAAO,MAAM,CAAC,IAAI,CAAC;SAChB,WAAW,EAAE;SACb,OAAO,CAAC,iBAAi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AAE,GAAG,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;YACtC,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,WAAW,KAAK,MAAM,EAAE,CAAC;gBAC1C,OAAO,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;gBACtD,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,KAAK,QAAQ,CAAC,CAAC;IAC9D,OAAO;QACL,OAAO,EAAE,KAAK,CAAC,EAAE;QACjB,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,KAAK;QACL,MAAM;QACN,EAAE,EAAE,CAAC,MAAM,IAAI,CAAC,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5D,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/B,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC3C,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export interface SummarySentence {
|
|
2
|
+
/** Stable id, unique within the corpus. */
|
|
3
|
+
id: string;
|
|
4
|
+
/** One sentence. Titles and headings are not sentences. */
|
|
5
|
+
text: string;
|
|
6
|
+
/** The document this sentence came from; the citation the caller shows. */
|
|
7
|
+
source: string;
|
|
8
|
+
/** Human label for the source, e.g. a post title. */
|
|
9
|
+
sourceTitle?: string;
|
|
10
|
+
/** Optional grouping (tag, section) used to keep picks from one cluster. */
|
|
11
|
+
group?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface SummaryPick extends SummarySentence {
|
|
14
|
+
/** Cosine similarity to the question, 0 when the question names nothing. */
|
|
15
|
+
relevance: number;
|
|
16
|
+
/** 1 - the closest similarity to an already picked sentence. */
|
|
17
|
+
novelty: number;
|
|
18
|
+
/** The MMR score that decided the pick. */
|
|
19
|
+
score: number;
|
|
20
|
+
}
|
|
21
|
+
export interface SummaryOptions {
|
|
22
|
+
/** The visitor's words. Empty means "diversify over everything". */
|
|
23
|
+
query?: string;
|
|
24
|
+
/** How many sentences to return. Default 3. */
|
|
25
|
+
k?: number;
|
|
26
|
+
/** Relevance weight in MMR; 1 is pure relevance, 0 pure novelty. Default 0.7. */
|
|
27
|
+
lambda?: number;
|
|
28
|
+
/** At most this many picks per source document. Default 1. */
|
|
29
|
+
maxPerSource?: number;
|
|
30
|
+
/** Sentences shorter than this (in characters) are not summarised. Default 24. */
|
|
31
|
+
minLength?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Stop before quoting a sentence with no relevance to the question, once at
|
|
34
|
+
* least one pick has some. A summary that pads to k by adding off-topic
|
|
35
|
+
* sentences claims coverage the corpus does not have; for "which posts cover
|
|
36
|
+
* X" the honest answer is fewer sentences. Default true.
|
|
37
|
+
*/
|
|
38
|
+
stopAtZeroRelevance?: boolean;
|
|
39
|
+
}
|
|
40
|
+
export interface SummaryResult {
|
|
41
|
+
picks: SummaryPick[];
|
|
42
|
+
/** Candidate sentences considered after filtering. */
|
|
43
|
+
considered: number;
|
|
44
|
+
/** The query terms actually used, after tokenizing and stemming. */
|
|
45
|
+
terms: string[];
|
|
46
|
+
/** True when the question named nothing in the corpus. */
|
|
47
|
+
unguided: boolean;
|
|
48
|
+
}
|
|
49
|
+
/** Split prose into sentences without a model: punctuation, then length. */
|
|
50
|
+
export declare function splitSentences(text: string): string[];
|
|
51
|
+
export declare function summarise(sentences: SummarySentence[], options?: SummaryOptions): SummaryResult;
|
|
52
|
+
//# sourceMappingURL=summarise.d.ts.map
|
|
@@ -0,0 +1 @@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AAmB,GAC3B,aAAa,CAmGf"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/* ============================================================
|
|
2
|
+
Extractive summarisation without a model.
|
|
3
|
+
|
|
4
|
+
Given a handful of candidate sentences (each tagged with the
|
|
5
|
+
document it came from), pick k of them: the ones most related to
|
|
6
|
+
the question, and not saying the same thing as each other. That
|
|
7
|
+
is Maximal Marginal Relevance over tf-idf cosine, which needs no
|
|
8
|
+
weights, no training data and no inference budget.
|
|
9
|
+
|
|
10
|
+
The two failures this exists to avoid:
|
|
11
|
+
- a plain relevance ranking returns one document's family of
|
|
12
|
+
sentences and calls it a summary;
|
|
13
|
+
- a plain first-k-by-date list ignores the question entirely.
|
|
14
|
+
|
|
15
|
+
It returns the picks with their scores so the caller can show
|
|
16
|
+
what was used, and so a harness can score a summary instead of
|
|
17
|
+
trusting it.
|
|
18
|
+
============================================================ */
|
|
19
|
+
import { tokenize } from './retriever.js';
|
|
20
|
+
function termCounts(tokens) {
|
|
21
|
+
const counts = new Map();
|
|
22
|
+
for (const token of tokens)
|
|
23
|
+
counts.set(token, (counts.get(token) ?? 0) + 1);
|
|
24
|
+
return counts;
|
|
25
|
+
}
|
|
26
|
+
function cosine(a, b) {
|
|
27
|
+
let dot = 0;
|
|
28
|
+
for (const [term, weight] of Object.entries(a))
|
|
29
|
+
dot += weight * (b[term] ?? 0);
|
|
30
|
+
return dot; // both sides are unit vectors
|
|
31
|
+
}
|
|
32
|
+
function normalise(vector) {
|
|
33
|
+
let sum = 0;
|
|
34
|
+
for (const weight of Object.values(vector))
|
|
35
|
+
sum += weight * weight;
|
|
36
|
+
const length = Math.sqrt(sum);
|
|
37
|
+
if (length === 0)
|
|
38
|
+
return {};
|
|
39
|
+
for (const term of Object.keys(vector))
|
|
40
|
+
vector[term] /= length;
|
|
41
|
+
return vector;
|
|
42
|
+
}
|
|
43
|
+
/** Split prose into sentences without a model: punctuation, then length. */
|
|
44
|
+
export function splitSentences(text) {
|
|
45
|
+
return String(text)
|
|
46
|
+
.replace(/\s+/g, ' ')
|
|
47
|
+
.split(/(?<=[.!?])\s+(?=[A-Z0-9"'\u201c(])/)
|
|
48
|
+
.map((sentence) => sentence.trim())
|
|
49
|
+
.filter((sentence) => sentence.length > 0);
|
|
50
|
+
}
|
|
51
|
+
export function summarise(sentences, options = {}) {
|
|
52
|
+
const k = Math.max(1, options.k ?? 3);
|
|
53
|
+
const lambda = Math.min(1, Math.max(0, options.lambda ?? 0.7));
|
|
54
|
+
const maxPerSource = Math.max(1, options.maxPerSource ?? 1);
|
|
55
|
+
const minLength = options.minLength ?? 24;
|
|
56
|
+
const stopAtZero = options.stopAtZeroRelevance ?? true;
|
|
57
|
+
const candidates = sentences.filter((sentence) => sentence.text && sentence.text.trim().length >= minLength);
|
|
58
|
+
const terms = tokenize(options.query ?? '');
|
|
59
|
+
if (candidates.length === 0) {
|
|
60
|
+
return { picks: [], considered: 0, terms, unguided: terms.length === 0 };
|
|
61
|
+
}
|
|
62
|
+
const tokenised = candidates.map((sentence) => tokenize(`${sentence.text} ${sentence.sourceTitle ?? ''} ${sentence.group ?? ''}`));
|
|
63
|
+
const documentFrequency = new Map();
|
|
64
|
+
for (const tokens of tokenised) {
|
|
65
|
+
for (const term of new Set(tokens)) {
|
|
66
|
+
documentFrequency.set(term, (documentFrequency.get(term) ?? 0) + 1);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const total = candidates.length;
|
|
70
|
+
const idf = (term) => Math.log(1 + total / (1 + (documentFrequency.get(term) ?? 0)));
|
|
71
|
+
const vectors = tokenised.map((tokens) => {
|
|
72
|
+
const vector = {};
|
|
73
|
+
for (const [term, count] of termCounts(tokens))
|
|
74
|
+
vector[term] = count * idf(term);
|
|
75
|
+
return normalise(vector);
|
|
76
|
+
});
|
|
77
|
+
const queryVector = {};
|
|
78
|
+
for (const [term, count] of termCounts(terms))
|
|
79
|
+
queryVector[term] = count * idf(term);
|
|
80
|
+
normalise(queryVector);
|
|
81
|
+
const relevance = vectors.map((vector) => Object.keys(queryVector).length === 0 ? 1 : cosine(queryVector, vector));
|
|
82
|
+
const unguided = Object.keys(queryVector).length === 0;
|
|
83
|
+
const onTopic = relevance.some((value) => value > 0);
|
|
84
|
+
const picked = [];
|
|
85
|
+
const perSource = new Map();
|
|
86
|
+
const used = new Set();
|
|
87
|
+
while (picked.length < Math.min(k, candidates.length)) {
|
|
88
|
+
const eligible = [];
|
|
89
|
+
for (let index = 0; index < candidates.length; index += 1) {
|
|
90
|
+
if (used.has(index))
|
|
91
|
+
continue;
|
|
92
|
+
const source = candidates[index].source;
|
|
93
|
+
if ((perSource.get(source) ?? 0) >= maxPerSource)
|
|
94
|
+
continue;
|
|
95
|
+
let nearest = 0;
|
|
96
|
+
for (const selected of picked) {
|
|
97
|
+
nearest = Math.max(nearest, cosine(vectors[index], vectors[selected]));
|
|
98
|
+
}
|
|
99
|
+
eligible.push({ index, relevance: relevance[index], novelty: 1 - nearest });
|
|
100
|
+
}
|
|
101
|
+
if (eligible.length === 0)
|
|
102
|
+
break;
|
|
103
|
+
// Pad only when the corpus covers nothing at all: then the question is
|
|
104
|
+
// effectively unguided and diverse sentences are the honest answer. When it
|
|
105
|
+
// does cover the question, stop rather than quote a sentence that does not.
|
|
106
|
+
if (stopAtZero &&
|
|
107
|
+
picked.length > 0 &&
|
|
108
|
+
onTopic &&
|
|
109
|
+
!eligible.some((row) => row.relevance > 0)) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
// Classic MMR: relevance minus the closest similarity to a pick, both as
|
|
113
|
+
// cosines in the same space. Scoring novelty as `1 - similarity` instead
|
|
114
|
+
// looks equivalent and is not: unrelated sentences still share a stopword,
|
|
115
|
+
// so novelty sits near 0.95 for every candidate and drowns relevance, which
|
|
116
|
+
// is what makes the second pick drift off the question.
|
|
117
|
+
const scaled = penalise(eligible, lambda);
|
|
118
|
+
let best = scaled[0];
|
|
119
|
+
for (const candidate of scaled)
|
|
120
|
+
if (candidate.score > best.score)
|
|
121
|
+
best = candidate;
|
|
122
|
+
used.add(best.index);
|
|
123
|
+
picked.push(best.index);
|
|
124
|
+
perSource.set(candidates[best.index].source, (perSource.get(candidates[best.index].source) ?? 0) + 1);
|
|
125
|
+
candidates[best.index].relevance = round(best.relevance);
|
|
126
|
+
candidates[best.index].novelty = round(best.novelty);
|
|
127
|
+
candidates[best.index].score = round(best.score);
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
picks: picked.map((index) => candidates[index]),
|
|
131
|
+
considered: candidates.length,
|
|
132
|
+
terms,
|
|
133
|
+
unguided,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** lambda * relevance - (1 - lambda) * closest similarity to a pick. */
|
|
137
|
+
function penalise(rows, lambda) {
|
|
138
|
+
return rows.map((row) => ({
|
|
139
|
+
...row,
|
|
140
|
+
score: lambda * row.relevance - (1 - lambda) * (1 - row.novelty),
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
function round(value) {
|
|
144
|
+
return Math.round(value * 1000) / 1000;
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=summarise.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"summarise.js","sourceRoot":"","sources":["../../src/core/summarise.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;kEAiBkE;AAElE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAwD1C,SAAS,UAAU,CAAC,MAAgB;IAClC,MAAM,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;IACzC,KAAK,MAAM,KAAK,IAAI,MAAM;QAAE,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5E,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,MAAM,CAAC,CAAS,EAAE,CAAS;IAClC,IAAI,GAAG,GAAG,CAAC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EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAgB,CAAC;QAC9D,UAAU,EAAE,UAAU,CAAC,MAAM;QAC7B,KAAK;QACL,QAAQ;KACT,CAAC;AACJ,CAAC;AAED,wEAAwE;AACxE,SAAS,QAAQ,CACf,IAA6D,EAC7D,MAAc;IAEd,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACxB,GAAG,GAAG;QACN,KAAK,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,OAAO,CAAC;KACjE,CAAC,CAAC,CAAC;AACN,CAAC;AAED,SAAS,KAAK,CAAC,KAAa;IAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type ParsedModelOutput = {
|
|
2
|
+
kind: 'text';
|
|
3
|
+
text: string;
|
|
4
|
+
} | {
|
|
5
|
+
kind: 'tool';
|
|
6
|
+
capabilityId: string;
|
|
7
|
+
args: Record<string, unknown>;
|
|
8
|
+
} | {
|
|
9
|
+
kind: 'malformed';
|
|
10
|
+
error: string;
|
|
11
|
+
raw: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Extracts one tool call from whatever a small local model actually emitted:
|
|
15
|
+
* a bare JSON object, a fenced block, a fenced block wrapped in prose, or a
|
|
16
|
+
* JSON string nested inside `arguments`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseModelOutput(text: string): ParsedModelOutput;
|
|
19
|
+
/** Parses the `model_output` of a native function-calling response body. */
|
|
20
|
+
export declare function parseNativeToolCall(payload: unknown): ParsedModelOutput;
|
|
21
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/core/tools.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,GACzB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,GACrE;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AA2CtD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,iBAAiB,CA0ChE;AAED,4EAA4E;AAC5E,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,iBAAiB,CAoBvE"}
|