@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,83 @@
|
|
|
1
|
+
import type { ManifestCapability } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* One sentence can ask for two things. "Open the archive and copy his email"
|
|
4
|
+
* is not one capability call, and no keyword index can return two answers to
|
|
5
|
+
* one query, so the split has to happen before retrieval and be explicit about
|
|
6
|
+
* what it could not place.
|
|
7
|
+
*
|
|
8
|
+
* The split is conservative on purpose. A second clause counts only when it
|
|
9
|
+
* opens with its own verb: "show me Hobnob and Readback" names two objects of
|
|
10
|
+
* one request and stays one request.
|
|
11
|
+
*/
|
|
12
|
+
export interface PlannedStep {
|
|
13
|
+
/** The clause the visitor wrote, verbatim. */
|
|
14
|
+
text: string;
|
|
15
|
+
/** The verb the clause opened with, canonicalised to a known controller verb. */
|
|
16
|
+
verb: string | null;
|
|
17
|
+
capabilityId: string;
|
|
18
|
+
title: string;
|
|
19
|
+
kind: string;
|
|
20
|
+
/** Arguments read off the clause. Null means a required argument was not stated. */
|
|
21
|
+
args: Record<string, unknown> | null;
|
|
22
|
+
/** What must have happened before this step can run. */
|
|
23
|
+
needs: 'none' | 'route' | 'args';
|
|
24
|
+
/** For a step that acts on a page: the route it needs to be on. */
|
|
25
|
+
route?: string | null;
|
|
26
|
+
/**
|
|
27
|
+
* Runner-ups for the same clause, best first, excluding routes (a wrong page
|
|
28
|
+
* is worse than a refusal). The executor tries these when the chosen
|
|
29
|
+
* capability throws or returns nothing, which is the only recovery a
|
|
30
|
+
* model-free host can make: it knows what else could have served the words.
|
|
31
|
+
*/
|
|
32
|
+
alternates?: PlannedAlternate[];
|
|
33
|
+
score: number;
|
|
34
|
+
}
|
|
35
|
+
export interface PlannedAlternate {
|
|
36
|
+
capabilityId: string;
|
|
37
|
+
title: string;
|
|
38
|
+
kind: string;
|
|
39
|
+
/** Arguments read off the clause for this capability, or null when it needs one it never got. */
|
|
40
|
+
args: Record<string, unknown> | null;
|
|
41
|
+
}
|
|
42
|
+
export interface PlanOptions {
|
|
43
|
+
capabilities: ManifestCapability[];
|
|
44
|
+
/** App command verbs, on top of the default controller set. */
|
|
45
|
+
controllerVerbs?: string[];
|
|
46
|
+
}
|
|
47
|
+
export interface RequestPlan {
|
|
48
|
+
steps: PlannedStep[];
|
|
49
|
+
/** Why the sentence is not a plan. Absent when it is one. */
|
|
50
|
+
declined?: 'single-request' | 'no-second-verb' | 'unmapped-clause';
|
|
51
|
+
/** The clause nothing could answer, when declined is 'unmapped-clause'. */
|
|
52
|
+
failed?: string;
|
|
53
|
+
/** A sentence the app can show before it runs the steps. */
|
|
54
|
+
summary: string;
|
|
55
|
+
}
|
|
56
|
+
export interface RoutedRequest {
|
|
57
|
+
capabilityId: string;
|
|
58
|
+
title: string;
|
|
59
|
+
kind: string;
|
|
60
|
+
args: Record<string, unknown> | null;
|
|
61
|
+
verb: string | null;
|
|
62
|
+
/** Declared the query every capability refused to route, when it did. */
|
|
63
|
+
declined?: 'unmapped-clause';
|
|
64
|
+
}
|
|
65
|
+
export interface RouteOptions extends PlanOptions {
|
|
66
|
+
/** `question` prefers query/knowledge capabilities; `request` prefers routes/actions. */
|
|
67
|
+
prefer?: 'question' | 'request';
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Routes one request to one capability. `planRequest` answers "what did this
|
|
71
|
+
* sentence ask for"; this answers "which tool serves it", for a host that only
|
|
72
|
+
* has a single call to make. Both share one ranking rule, so a tool surface and
|
|
73
|
+
* a plan cannot disagree about the same words.
|
|
74
|
+
*/
|
|
75
|
+
export declare function routeRequest(query: string, options: RouteOptions): RoutedRequest;
|
|
76
|
+
/**
|
|
77
|
+
* Plans a request, or declines to. Nothing here reads a model: the sentence is
|
|
78
|
+
* cut at its own connectives, each clause is routed on its own words, and the
|
|
79
|
+
* only cross-clause reasoning is a precondition check (a page action after a
|
|
80
|
+
* route, or arguments a clause never stated).
|
|
81
|
+
*/
|
|
82
|
+
export declare function planRequest(query: string, options: PlanOptions): RequestPlan;
|
|
83
|
+
//# sourceMappingURL=plan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../../src/core/plan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGrD;;;;;;;;;GASG;AACH,MAAM,WAAW,WAAW;IAC1B,8CAA8C;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,iFAAiF;IACjF,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,oFAAoF;IACpF,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IACrC,wDAAwD;IACxD,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;IACjC,mEAAmE;IACnE,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAChC,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,YA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EFAULT_VERBS = 'show|open|go|take|read|expand|copy|download|print|visit|browse|scroll|jump|navigate|see|find|list'.split('|');
|
|
3
|
+
const LEAD_FILLER = /^(?:please|also|then|and|now|next|after that|afterwards|can you|could you|would you)\b[\s,]*/i;
|
|
4
|
+
/** Boundaries that can join two requests. "and" alone usually joins two objects. */
|
|
5
|
+
const CONNECTIVE = /\b(and then|then|after that|afterwards|and|plus|also)\b/gi;
|
|
6
|
+
function verbSet(extra = []) {
|
|
7
|
+
return new Set([
|
|
8
|
+
...DEFAULT_VERBS,
|
|
9
|
+
...extra.map((verb) => verb.replace(/[^a-z]/gi, '').toLowerCase()).filter(Boolean),
|
|
10
|
+
]);
|
|
11
|
+
}
|
|
12
|
+
/** The verb a clause opens with, after filler and a leading "me"-style object. */
|
|
13
|
+
function leadingVerb(text, verbs) {
|
|
14
|
+
let rest = text.trim();
|
|
15
|
+
for (let i = 0; i < 3; i += 1) {
|
|
16
|
+
const next = rest.replace(LEAD_FILLER, '');
|
|
17
|
+
if (next === rest && i > 0)
|
|
18
|
+
break;
|
|
19
|
+
rest = next;
|
|
20
|
+
}
|
|
21
|
+
const first = tokenize(rest)[0];
|
|
22
|
+
if (first && verbs.has(first))
|
|
23
|
+
return first;
|
|
24
|
+
// "take me to the blog" opens with take; "go to" opens with go. Both are
|
|
25
|
+
// already single tokens, so only the "me ..." forms need looking past.
|
|
26
|
+
const afterMe = /^[a-z]+\s+(?:me|us)\s+/i.exec(rest);
|
|
27
|
+
if (afterMe) {
|
|
28
|
+
const verb = afterMe[0].trim().split(/\s+/)[0].toLowerCase();
|
|
29
|
+
if (verbs.has(verb))
|
|
30
|
+
return verb;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Cuts the sentence at every connective whose right side starts with a verb.
|
|
36
|
+
* Positions are kept so each clause keeps the visitor's own words.
|
|
37
|
+
*/
|
|
38
|
+
function splitClauses(query, verbs) {
|
|
39
|
+
const clauses = [];
|
|
40
|
+
let cursor = 0;
|
|
41
|
+
CONNECTIVE.lastIndex = 0;
|
|
42
|
+
for (let match = CONNECTIVE.exec(query); match; match = CONNECTIVE.exec(query)) {
|
|
43
|
+
const after = query.slice(match.index + match[0].length);
|
|
44
|
+
const trimmed = after.replace(/^[,\s]+/, '');
|
|
45
|
+
if (!trimmed)
|
|
46
|
+
continue;
|
|
47
|
+
if (!leadingVerb(trimmed, verbs))
|
|
48
|
+
continue;
|
|
49
|
+
clauses.push(query.slice(cursor, match.index).trim());
|
|
50
|
+
cursor = match.index + match[0].length;
|
|
51
|
+
// Keep scanning from the same place: the new clause may hold another one.
|
|
52
|
+
CONNECTIVE.lastIndex = cursor;
|
|
53
|
+
}
|
|
54
|
+
clauses.push(query.slice(cursor).trim());
|
|
55
|
+
return clauses.filter(Boolean);
|
|
56
|
+
}
|
|
57
|
+
/** Reads the enum/email arguments a schema requires out of the clause itself. */
|
|
58
|
+
function inferArgs(capability, clause) {
|
|
59
|
+
const schema = capability.paramsSchema;
|
|
60
|
+
const required = schema?.required ?? [];
|
|
61
|
+
if (required.length === 0)
|
|
62
|
+
return {};
|
|
63
|
+
const tokens = new Set(tokenize(clause));
|
|
64
|
+
const args = {};
|
|
65
|
+
for (const name of required) {
|
|
66
|
+
const property = schema?.properties?.[name];
|
|
67
|
+
if (!property)
|
|
68
|
+
return null;
|
|
69
|
+
if (property.enum) {
|
|
70
|
+
const hit = property.enum.find((value) => tokenize(String(value)).some((word) => tokens.has(word)));
|
|
71
|
+
if (hit === undefined)
|
|
72
|
+
return null;
|
|
73
|
+
args[name] = hit;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (property.type === 'string') {
|
|
77
|
+
const found = /[^\s@<>()"']+@[^\s@<>()"']+\.[^\s@<>()"']+/.exec(clause)?.[0];
|
|
78
|
+
if (!found)
|
|
79
|
+
return null;
|
|
80
|
+
args[name] = found;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return args;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The verb is routing information, not a search term. Left in, "show me the
|
|
89
|
+
* skills" scores every capability whose title opens with "show" on the word
|
|
90
|
+
* "show"; the topic words are what decide between them.
|
|
91
|
+
*/
|
|
92
|
+
function topicOf(clause, verb) {
|
|
93
|
+
if (!verb)
|
|
94
|
+
return clause;
|
|
95
|
+
const stripped = clause
|
|
96
|
+
.replace(LEAD_FILLER, '')
|
|
97
|
+
.replace(new RegExp(`\\b${verb}(?:\\s+(?:me|us)\\b)?`, 'i'), ' ')
|
|
98
|
+
.replace(/\s+/g, ' ')
|
|
99
|
+
.trim();
|
|
100
|
+
return stripped || clause;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A clause can name only the object: "expand hobnob" has no topic word the
|
|
104
|
+
* index knows, because Hobnob is a value in the schema, not a keyword. A
|
|
105
|
+
* capability that enumerates the name is the one that can act on it.
|
|
106
|
+
*/
|
|
107
|
+
function enumOwners(capabilities, clause) {
|
|
108
|
+
const tokens = new Set(tokenize(clause));
|
|
109
|
+
return capabilities.filter((capability) => (capability.paramsSchema?.required ?? []).length > 0 &&
|
|
110
|
+
capability.paramsSchema.required.every((name) => capability.paramsSchema?.properties?.[name]?.enum?.some((value) => tokenize(String(value)).some((word) => tokens.has(word)))));
|
|
111
|
+
}
|
|
112
|
+
/** Nouns that name a specific thing rather than a place. */
|
|
113
|
+
const NAMED_THING = /\b(card|expand|print|download|copy|link|source|repo|site|archive|resume|email|linkedin|pdf|post|posts|article|articles|entry)\b/i;
|
|
114
|
+
function verbBoost(capabilityId, title, verb) {
|
|
115
|
+
if (!verb)
|
|
116
|
+
return 1;
|
|
117
|
+
const [first] = tokenize(title);
|
|
118
|
+
if (first === verb)
|
|
119
|
+
return 1.5;
|
|
120
|
+
return tokenize(capabilityId).includes(verb) ? 1.2 : 1;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Routes one clause to one capability, with its arguments and its runner-ups.
|
|
124
|
+
* A plan is several of these in order; a single request is exactly one, which
|
|
125
|
+
* is why both paths call this instead of keeping two ranking rules.
|
|
126
|
+
*/
|
|
127
|
+
function selectForClause(clause, ctx) {
|
|
128
|
+
const verb = leadingVerb(clause, ctx.verbs);
|
|
129
|
+
const scored = ctx.index
|
|
130
|
+
.search(topicOf(clause, verb), { limit: 5, boost: false })
|
|
131
|
+
.map((entry) => ({ ...entry, score: entry.score * verbBoost(entry.capability.id, entry.capability.title, verb) }))
|
|
132
|
+
.sort((a, b) => b.score - a.score);
|
|
133
|
+
const ranked = scored.filter((entry) => entry.score > 0);
|
|
134
|
+
// The engine's own rule, kept per clause: a request that names no specific
|
|
135
|
+
// thing asks for a place. "Show me the projects" is the page; "expand the
|
|
136
|
+
// card" is the card.
|
|
137
|
+
const place = topicOf(clause, verb);
|
|
138
|
+
// A verb plus an argument it consumes is a complete call, so it decides
|
|
139
|
+
// first: "expand hobnob" is the expand capability even though the object
|
|
140
|
+
// only appears in the card's own keywords. A verb with no argument is just
|
|
141
|
+
// a verb, and the topic words decide.
|
|
142
|
+
const scoredById = new Map(scored.map((entry) => [entry.capability.id, entry.score]));
|
|
143
|
+
const verbAgreed = ctx.capabilities
|
|
144
|
+
.filter((capability) => verbBoost(capability.id, capability.title, verb) > 1 &&
|
|
145
|
+
Object.keys(inferArgs(capability, clause) ?? {}).length > 0)
|
|
146
|
+
.sort((a, b) => (scoredById.get(b.id) ?? 0) - (scoredById.get(a.id) ?? 0))[0];
|
|
147
|
+
const route = NAMED_THING.test(place) ? undefined : ranked.find((entry) => entry.capability.kind === 'route');
|
|
148
|
+
// A request names a place or an action; knowledge paragraphs and queries
|
|
149
|
+
// only answer it when nothing else matched.
|
|
150
|
+
const actionable = ranked.find((entry) => ['route', 'action'].includes(String(entry.capability.kind)));
|
|
151
|
+
// A question asked of a tool surface wants the capability that answers, not
|
|
152
|
+
// the page it is about: "how many posts has he written" is `blog.stats` even
|
|
153
|
+
// though the archive is the loudest keyword match.
|
|
154
|
+
const answerable = ranked.find((entry) => ['query', 'knowledge'].includes(String(entry.capability.kind)));
|
|
155
|
+
const pick = verbAgreed
|
|
156
|
+
? { capability: verbAgreed, score: scoredById.get(verbAgreed.id) ?? 0 }
|
|
157
|
+
: ctx.prefer === 'question'
|
|
158
|
+
? answerable ?? route ?? actionable ?? ranked[0]
|
|
159
|
+
: route ?? actionable ?? ranked[0];
|
|
160
|
+
const owner = !pick || pick.score <= 0
|
|
161
|
+
? enumOwners(ctx.capabilities, clause).sort((a, b) => verbBoost(b.id, b.title, verb) - verbBoost(a.id, a.title, verb))[0]
|
|
162
|
+
: undefined;
|
|
163
|
+
if (!pick && !owner)
|
|
164
|
+
return { declined: 'unmapped-clause' };
|
|
165
|
+
const capability = owner ?? pick.capability;
|
|
166
|
+
const score = owner ? 0 : pick.score;
|
|
167
|
+
const args = inferArgs(capability, clause);
|
|
168
|
+
// Recovery is a ranked fact, not a guess: what else scored on these words,
|
|
169
|
+
// could run without an argument the sentence never gave, and is not a place.
|
|
170
|
+
const alternates = String(capability.kind) === 'route'
|
|
171
|
+
? []
|
|
172
|
+
: ranked
|
|
173
|
+
.filter((entry) => entry.capability.id !== capability.id && String(entry.capability.kind) !== 'route')
|
|
174
|
+
.map((entry) => ({
|
|
175
|
+
capabilityId: entry.capability.id,
|
|
176
|
+
title: entry.capability.title,
|
|
177
|
+
kind: String(entry.capability.kind),
|
|
178
|
+
args: inferArgs(entry.capability, clause),
|
|
179
|
+
}))
|
|
180
|
+
.filter((alternate) => alternate.args !== null)
|
|
181
|
+
.slice(0, 2);
|
|
182
|
+
return { verb, capability, score, args, alternates };
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Routes one request to one capability. `planRequest` answers "what did this
|
|
186
|
+
* sentence ask for"; this answers "which tool serves it", for a host that only
|
|
187
|
+
* has a single call to make. Both share one ranking rule, so a tool surface and
|
|
188
|
+
* a plan cannot disagree about the same words.
|
|
189
|
+
*/
|
|
190
|
+
export function routeRequest(query, options) {
|
|
191
|
+
const selection = selectForClause(query, {
|
|
192
|
+
verbs: verbSet(options.controllerVerbs),
|
|
193
|
+
index: new CapabilityIndex(options.capabilities),
|
|
194
|
+
capabilities: options.capabilities,
|
|
195
|
+
...(options.prefer ? { prefer: options.prefer } : {}),
|
|
196
|
+
});
|
|
197
|
+
if ('declined' in selection)
|
|
198
|
+
return { capabilityId: '', title: '', kind: '', args: null, verb: null, declined: 'unmapped-clause' };
|
|
199
|
+
return {
|
|
200
|
+
capabilityId: selection.capability.id,
|
|
201
|
+
title: selection.capability.title,
|
|
202
|
+
kind: String(selection.capability.kind),
|
|
203
|
+
args: selection.args,
|
|
204
|
+
verb: selection.verb,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Plans a request, or declines to. Nothing here reads a model: the sentence is
|
|
209
|
+
* cut at its own connectives, each clause is routed on its own words, and the
|
|
210
|
+
* only cross-clause reasoning is a precondition check (a page action after a
|
|
211
|
+
* route, or arguments a clause never stated).
|
|
212
|
+
*/
|
|
213
|
+
export function planRequest(query, options) {
|
|
214
|
+
const verbs = verbSet(options.controllerVerbs);
|
|
215
|
+
const ctx = {
|
|
216
|
+
verbs,
|
|
217
|
+
index: new CapabilityIndex(options.capabilities),
|
|
218
|
+
capabilities: options.capabilities,
|
|
219
|
+
};
|
|
220
|
+
const clauses = splitClauses(query, verbs);
|
|
221
|
+
if (clauses.length < 2) {
|
|
222
|
+
const connected = /\b(and then|then|after that|afterwards|and|plus|also)\b/i.test(query);
|
|
223
|
+
return { steps: [], declined: connected ? 'no-second-verb' : 'single-request', summary: '' };
|
|
224
|
+
}
|
|
225
|
+
const steps = [];
|
|
226
|
+
for (const clause of clauses) {
|
|
227
|
+
const selection = selectForClause(clause, ctx);
|
|
228
|
+
if ('declined' in selection) {
|
|
229
|
+
return { steps, declined: 'unmapped-clause', failed: clause, summary: '' };
|
|
230
|
+
}
|
|
231
|
+
const { verb, capability, score, args, alternates } = selection;
|
|
232
|
+
const previous = steps[steps.length - 1];
|
|
233
|
+
const needs = !previous
|
|
234
|
+
? 'none'
|
|
235
|
+
: args === null
|
|
236
|
+
? 'args'
|
|
237
|
+
: previous.kind === 'route'
|
|
238
|
+
? 'route'
|
|
239
|
+
: 'none';
|
|
240
|
+
steps.push({
|
|
241
|
+
text: clause,
|
|
242
|
+
verb,
|
|
243
|
+
capabilityId: capability.id,
|
|
244
|
+
title: capability.title,
|
|
245
|
+
kind: String(capability.kind),
|
|
246
|
+
args,
|
|
247
|
+
needs,
|
|
248
|
+
...(capability.route ? { route: capability.route } : {}),
|
|
249
|
+
...(alternates.length > 0 && needs !== 'args' ? { alternates } : {}),
|
|
250
|
+
score: Number(score.toFixed(3)),
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
const summary = steps
|
|
254
|
+
.map((step, i) => (i === 0 ? step.title : step.title.charAt(0).toLowerCase() + step.title.slice(1)))
|
|
255
|
+
.join(', then ');
|
|
256
|
+
return { steps, summary };
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=plan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan.js","sourceRoot":"","sources":["../../src/core/plan.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA4D3D,MAAM,aAAa,GACjB,mGAAmG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAEjH,MAAM,WAAW,GAAG,+FAA+F,CAAC;AAEpH,oFAAoF;AACpF,MAAM,UAAU,GAAG,2DAA2D,CAAC;AAE/E,SAAS,OAAO,CAAC,QAAkB,EAAE;IACnC,OAAO,IAAI,GAAG,CAAC;QACb,GAAG,aAAa;QAChB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,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.11).
|
|
3
|
+
*
|
|
4
|
+
* A visitor who says "keep it short" is not making a request for one answer: they
|
|
5
|
+
* are asserting something about the session. The page keeps that the way it keeps
|
|
6
|
+
* every other fact it reasons over, as a `prefers(<key>, <value>)` assertion, so
|
|
7
|
+
* a host can report why an answer was short instead of leaving a module variable
|
|
8
|
+
* whose only evidence is the answer's length.
|
|
9
|
+
*
|
|
10
|
+
* The store is also the honesty boundary. A host declares the keys and the values
|
|
11
|
+
* it can act on. Anything else is refused at `record()` rather than stored and
|
|
12
|
+
* silently ignored, so a host that cannot change its tone cannot appear to have
|
|
13
|
+
* accepted a tone preference.
|
|
14
|
+
*/
|
|
15
|
+
export interface PreferenceDeclaration {
|
|
16
|
+
/** Key the assertion is made under, e.g. `register`. */
|
|
17
|
+
id: string;
|
|
18
|
+
/** The values this host can act on. Anything else is refused, not guessed. */
|
|
19
|
+
values: string[];
|
|
20
|
+
/** What `get()` reports when nothing has been stated. */
|
|
21
|
+
defaultValue?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface StatedPreference {
|
|
24
|
+
id: string;
|
|
25
|
+
value: string;
|
|
26
|
+
/** 1-based count of preferences stated in this session. */
|
|
27
|
+
turn: number;
|
|
28
|
+
/** The visitor's words, kept for the recap and the trace. */
|
|
29
|
+
utterance: string;
|
|
30
|
+
}
|
|
31
|
+
export interface PreferenceRecordOptions {
|
|
32
|
+
utterance?: string;
|
|
33
|
+
/** Override the store's own counter, when the host counts turns itself. */
|
|
34
|
+
turn?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface PreferenceMemory {
|
|
37
|
+
readonly declarations: PreferenceDeclaration[];
|
|
38
|
+
isDeclared(id: string): boolean;
|
|
39
|
+
accepts(id: string, value: string): boolean;
|
|
40
|
+
/** Store a stated preference; `null` when the key or the value is not declared. */
|
|
41
|
+
record(id: string, value: string, options?: PreferenceRecordOptions): StatedPreference | null;
|
|
42
|
+
/** The stated value, else the declaration's default, else `null`. */
|
|
43
|
+
get(id: string): string | null;
|
|
44
|
+
/** Every stated preference, oldest first. */
|
|
45
|
+
stated(id?: string): StatedPreference[];
|
|
46
|
+
/** The assertions a rule could join: `prefers(register, terse)`. */
|
|
47
|
+
facts(): string[];
|
|
48
|
+
/** Forget what was stated; the declaration's default applies again. */
|
|
49
|
+
reset(id?: string): void;
|
|
50
|
+
}
|
|
51
|
+
export declare function createPreferenceMemory({ preferences }: {
|
|
52
|
+
preferences: PreferenceDeclaration[];
|
|
53
|
+
}): PreferenceMemory;
|
|
54
|
+
//# sourceMappingURL=preferences.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preferences.d.ts","sourceRoot":"","sources":["../../src/core/preferences.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,MAAM,WAAW,qBAAqB;IACpC,wDAAwD;IACxD,EAAE,EAAE,MAAM,CAAC;IACX,8EAA8E;IAC9E,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,yDAAyD;IACzD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,2DAA2D;IAC3D,IAAI,EAAE,MAAM,CAAC;IACb,6DAA6D;IAC7D,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,uBAAuB;IACtC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2EAA2E;IAC3E,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,YAAY,EAAE,qBAAqB,EAAE,CAAC;IAC/C,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC;IAChC,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAC5C,mFAAmF;IACnF,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,uBAAuB,GAAG,gBAAgB,GAAG,IAAI,CAAC;IAC9F,qEAAqE;IACrE,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC/B,6CAA6C;IAC7C,MAAM,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAAC;IACxC,oEAAoE;IACpE,KAAK,IAAI,MAAM,EAAE,CAAC;IAClB,uEAAuE;IACvE,KAAK,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAID,wBAAgB,sBAAsB,CAAC,EAAE,WAAW,EAAE,EAAE;IAAE,WAAW,EAAE,qBAAqB,EAAE,CAAA;CAAE,GAAG,gBAAgB,CAiElH"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stated preferences as facts (0.7.11).
|
|
3
|
+
*
|
|
4
|
+
* A visitor who says "keep it short" is not making a request for one answer: they
|
|
5
|
+
* are asserting something about the session. The page keeps that the way it keeps
|
|
6
|
+
* every other fact it reasons over, as a `prefers(<key>, <value>)` assertion, so
|
|
7
|
+
* a host can report why an answer was short instead of leaving a module variable
|
|
8
|
+
* whose only evidence is the answer's length.
|
|
9
|
+
*
|
|
10
|
+
* The store is also the honesty boundary. A host declares the keys and the values
|
|
11
|
+
* it can act on. Anything else is refused at `record()` rather than stored and
|
|
12
|
+
* silently ignored, so a host that cannot change its tone cannot appear to have
|
|
13
|
+
* accepted a tone preference.
|
|
14
|
+
*/
|
|
15
|
+
const normalize = (value) => String(value ?? '').trim().toLowerCase().replace(/[\s-]+/g, '_');
|
|
16
|
+
export function createPreferenceMemory({ preferences }) {
|
|
17
|
+
const declarations = preferences.map((declaration) => ({
|
|
18
|
+
...declaration,
|
|
19
|
+
values: declaration.values.map(normalize),
|
|
20
|
+
defaultValue: declaration.defaultValue ? normalize(declaration.defaultValue) : undefined,
|
|
21
|
+
}));
|
|
22
|
+
const byId = new Map(declarations.map((declaration) => [declaration.id, declaration]));
|
|
23
|
+
let history = [];
|
|
24
|
+
let turn = 0;
|
|
25
|
+
const isDeclared = (id) => byId.has(String(id));
|
|
26
|
+
const accepts = (id, value) => {
|
|
27
|
+
const declaration = byId.get(String(id));
|
|
28
|
+
return Boolean(declaration && declaration.values.includes(normalize(value)));
|
|
29
|
+
};
|
|
30
|
+
const latest = (id) => {
|
|
31
|
+
for (let at = history.length - 1; at >= 0; at -= 1) {
|
|
32
|
+
if (history[at].id === id)
|
|
33
|
+
return history[at];
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
declarations,
|
|
39
|
+
isDeclared,
|
|
40
|
+
accepts,
|
|
41
|
+
record(id, value, options = {}) {
|
|
42
|
+
if (!accepts(id, value))
|
|
43
|
+
return null;
|
|
44
|
+
const entry = {
|
|
45
|
+
id: String(id),
|
|
46
|
+
value: normalize(value),
|
|
47
|
+
turn: options.turn ?? (turn += 1),
|
|
48
|
+
utterance: String(options.utterance ?? ''),
|
|
49
|
+
};
|
|
50
|
+
if (options.turn !== undefined)
|
|
51
|
+
turn = Math.max(turn, options.turn);
|
|
52
|
+
history.push(entry);
|
|
53
|
+
return entry;
|
|
54
|
+
},
|
|
55
|
+
get(id) {
|
|
56
|
+
const stated = latest(String(id));
|
|
57
|
+
if (stated)
|
|
58
|
+
return stated.value;
|
|
59
|
+
const declaration = byId.get(String(id));
|
|
60
|
+
return declaration?.defaultValue ?? null;
|
|
61
|
+
},
|
|
62
|
+
stated(id) {
|
|
63
|
+
return id === undefined ? [...history] : history.filter((entry) => entry.id === String(id));
|
|
64
|
+
},
|
|
65
|
+
facts() {
|
|
66
|
+
return declarations
|
|
67
|
+
.map((declaration) => latest(declaration.id))
|
|
68
|
+
.filter((entry) => Boolean(entry))
|
|
69
|
+
.map((entry) => `prefers(${entry.id}, ${entry.value})`);
|
|
70
|
+
},
|
|
71
|
+
reset(id) {
|
|
72
|
+
if (id === undefined) {
|
|
73
|
+
// A full reset is a new session: turn numbers start again, so a recap
|
|
74
|
+
// after a reset cannot cite a turn from the session before it.
|
|
75
|
+
history = [];
|
|
76
|
+
turn = 0;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
history = history.filter((entry) => entry.id !== String(id));
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=preferences.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preferences.js","sourceRoot":"","sources":["../../src/core/preferences.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AA0CH,MAAM,SAAS,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;AAEtG,MAAM,UAAU,sBAAsB,CAAC,EAAE,WAAW,EAA4C;IAC9F,MAAM,YAAY,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;QACrD,GAAG,WAAW;QACd,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC;QACzC,YAAY,EAAE,WAAW,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS;KACzF,CAAC,CAAC,CAAC;IACJ,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,WAAW,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IACvF,IAAI,OAAO,GAAuB,EAAE,CAAC;IACrC,IAAI,IAAI,GAAG,CAAC,CAAC;IAEb,MAAM,UAAU,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,C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M,CAAC,EAAE,CAAC,CAAC,CAAC;QAC/D,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ManifestCapability, RuntimeSnapshot, ToolSpec } from './types.js';
|
|
2
|
+
/** Rough token estimate: ~4 characters per token is close enough for budgeting. */
|
|
3
|
+
export declare function estimateTokens(text: string): number;
|
|
4
|
+
export interface PromptInput {
|
|
5
|
+
capabilities: ManifestCapability[];
|
|
6
|
+
snapshot: RuntimeSnapshot;
|
|
7
|
+
/** Token budget for the capability list itself. Default 1500. */
|
|
8
|
+
budgetTokens?: number;
|
|
9
|
+
instructions?: string;
|
|
10
|
+
/** Grounding text for this turn: page content, docs, a transcript. */
|
|
11
|
+
context?: string;
|
|
12
|
+
/** Token budget for that text. Default 700. */
|
|
13
|
+
contextBudgetTokens?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Builds the system prompt for one turn. Everything in here is authored by the
|
|
17
|
+
* host app, never by the model, and the capability list is the only source of
|
|
18
|
+
* callable ids — the loop rejects anything else.
|
|
19
|
+
*/
|
|
20
|
+
export declare function buildSystemPrompt(input: PromptInput): string;
|
|
21
|
+
/** Native tool specs for engines that support OpenAI-style function calling. */
|
|
22
|
+
export declare function buildToolSpecs(capabilities: ManifestCapability[]): ToolSpec[];
|
|
23
|
+
//# sourceMappingURL=prompt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt.d.ts","sourceRoot":"","sources":["../../src/core/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEhF,mFAAmF;AACnF,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAiBD,MAAM,WAAW,WAAW;IAC1B,YAAY,EAAE,kBAAkB,EAAE,CAAC;IACnC,QAAQ,EAAE,eAAe,CAAC;IAC1B,iEAAiE;IACjE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,+CAA+C;IAC/C,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAiE5D;AAED,gFAAgF;AAChF,wBAAgB,cAAc,CAAC,YAAY,EAAE,kBAAkB,EAAE,GAAG,QAAQ,EAAE,CAQ7E"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/** Rough token estimate: ~4 characters per token is close enough for budgeting. */
|
|
2
|
+
export function estimateTokens(text) {
|
|
3
|
+
return Math.ceil(text.length / 4);
|
|
4
|
+
}
|
|
5
|
+
function describeArgs(capability) {
|
|
6
|
+
const schema = capability.paramsSchema;
|
|
7
|
+
if (!schema || !schema.properties || Object.keys(schema.properties).length === 0) {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
const required = new Set(schema.required ?? []);
|
|
11
|
+
const parts = Object.entries(schema.properties).map(([name, prop]) => {
|
|
12
|
+
let type = prop.type ? String(prop.type) : 'value';
|
|
13
|
+
if (prop.enum)
|
|
14
|
+
type = prop.enum.map((v) => JSON.stringify(v)).join('|');
|
|
15
|
+
const optional = required.has(name) ? '' : '?';
|
|
16
|
+
return `${name}${optional}:${type}`;
|
|
17
|
+
});
|
|
18
|
+
return parts.join(', ');
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Builds the system prompt for one turn. Everything in here is authored by the
|
|
22
|
+
* host app, never by the model, and the capability list is the only source of
|
|
23
|
+
* callable ids — the loop rejects anything else.
|
|
24
|
+
*/
|
|
25
|
+
export function buildSystemPrompt(input) {
|
|
26
|
+
const { capabilities, snapshot } = input;
|
|
27
|
+
const budget = input.budgetTokens ?? 1500;
|
|
28
|
+
const header = [
|
|
29
|
+
'You are the assistant built into this web application. You help the visitor find screens and perform actions.',
|
|
30
|
+
'',
|
|
31
|
+
`Current route: ${snapshot.currentRoute}`,
|
|
32
|
+
];
|
|
33
|
+
if (snapshot.state && Object.keys(snapshot.state).length > 0) {
|
|
34
|
+
header.push(`Client state: ${JSON.stringify(snapshot.state)}`);
|
|
35
|
+
}
|
|
36
|
+
header.push('', 'Reply in one of exactly two shapes:', '1. Plain prose for an answer that needs no action.', '2. A single fenced ```json block calling one capability:', '```json', '{"tool":"<capability id>","arguments":{}}', '```', '', 'Rules:', '- Only call ids from the Capabilities list. Never invent an id, an argument, a URL or a selector.', '- Use argument names and values exactly as the schema allows. Omit optional arguments you do not need.', '- route capabilities take no arguments.', '- Call one capability per reply. You may be asked again after the result.', '- If nothing listed does what was asked, say so plainly and name the closest capability.', '- Answer in at most two sentences. Never claim an action happened unless a tool result says it did.');
|
|
37
|
+
if (input.instructions)
|
|
38
|
+
header.push('', input.instructions);
|
|
39
|
+
if (input.context && input.context.trim()) {
|
|
40
|
+
const limit = (input.contextBudgetTokens ?? 700) * 4;
|
|
41
|
+
const raw = input.context.trim();
|
|
42
|
+
const body = raw.length > limit ? `${raw.slice(0, limit).trimEnd()}\n[content truncated]` : raw;
|
|
43
|
+
header.push('', 'Page content, for answering questions about this site:', body, '', 'Use the page content to explain things. When the visitor would be better off seeing a section, say so and offer to take them there.');
|
|
44
|
+
}
|
|
45
|
+
const lines = capabilities.map((capability) => {
|
|
46
|
+
const route = capability.route ? ` [${capability.route}]` : '';
|
|
47
|
+
const args = describeArgs(capability);
|
|
48
|
+
const confirm = capability.requiresConfirmation ? ' (asks the user to confirm)' : '';
|
|
49
|
+
const argLine = args ? ` args{${args}}` : '';
|
|
50
|
+
return `- ${capability.id} (${capability.kind}${route})${confirm} ${capability.title}: ${capability.description}${argLine}`;
|
|
51
|
+
});
|
|
52
|
+
const fixed = `${header.join('\n')}\n\nCapabilities:\n`;
|
|
53
|
+
const room = budget * 4 - fixed.length - 40;
|
|
54
|
+
const kept = [];
|
|
55
|
+
let used = 0;
|
|
56
|
+
for (const line of lines) {
|
|
57
|
+
if (used + line.length + 1 > room && kept.length > 0)
|
|
58
|
+
break;
|
|
59
|
+
kept.push(line);
|
|
60
|
+
used += line.length + 1;
|
|
61
|
+
}
|
|
62
|
+
if (kept.length < lines.length) {
|
|
63
|
+
kept.push(`- (${lines.length - kept.length} more capabilities are not listed; ask the user to narrow the request)`);
|
|
64
|
+
}
|
|
65
|
+
return `${fixed}${kept.join('\n')}`;
|
|
66
|
+
}
|
|
67
|
+
/** Native tool specs for engines that support OpenAI-style function calling. */
|
|
68
|
+
export function buildToolSpecs(capabilities) {
|
|
69
|
+
return capabilities.map((capability) => ({
|
|
70
|
+
name: capability.id,
|
|
71
|
+
description: `${capability.title}. ${capability.description}`,
|
|
72
|
+
parameters: capability.paramsSchema ??
|
|
73
|
+
{ type: 'object', properties: {}, additionalProperties: false },
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../src/core/prompt.ts"],"names":[],"mappings":"AAEA,mFAAmF;AACnF,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,SAAS,YAAY,CAAC,UAA8B;IAClD,MAAM,MAAM,GAAG,UAAU,CAAC,YAAY,CAAC;IACvC,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjF,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;IAChD,MA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