@chatpanel/events 0.4.0 → 0.5.0
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/index.js +1 -0
- package/package.json +7 -5
- package/skill-vars.js +237 -0
package/index.js
CHANGED
|
@@ -43,4 +43,5 @@ export { createManifest, ManifestError, SOURCES } from './manifest.js';
|
|
|
43
43
|
export { createKernel, meetDecisions, KernelError, REQUIRED_PLUGINS, ALLOW_ALL } from './kernel.js';
|
|
44
44
|
export { replay, formatReport, parseJsonl, toJsonl } from './harness.js';
|
|
45
45
|
export { compileQuery, findMatches, matchIndexFor, expandReplacement, replaceMatch, replaceAll, replaceAllInRange, MAX_MATCHES } from './text-search.js';
|
|
46
|
+
export { SKILL_VARS, SKILL_VAR_NAMES, skillVar, skillVarPattern, parseSkillVars, lintSkillPrompt, suggestSkillVar, substituteSkillVars, skillVarGuidance, SkillVarError } from './skill-vars.js';
|
|
46
47
|
export { outlineOf, parseListItem, continueList, indentSelection, toggleWrap, toggleLinePrefix, toggleTask, toggleLink, docStats, selectionStats } from './markdown-authoring.js';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chatpanel/events",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The canonical ChatPanel event-log and capability contracts
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "The canonical ChatPanel event-log and capability contracts \u2014 typed durable facts, clock-free deterministic linearization, schema upcasting, and the invariants the replay harness asserts. Pure, dependency-free ESM shared by the ChatPanel extension, gateway and bridge.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"exports": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"./router.js": "./router.js",
|
|
26
26
|
"./rules.js": "./rules.js",
|
|
27
27
|
"./search-engines.js": "./search-engines.js",
|
|
28
|
+
"./skill-vars.js": "./skill-vars.js",
|
|
28
29
|
"./sources-retrieval.js": "./sources-retrieval.js",
|
|
29
30
|
"./sources.js": "./sources.js",
|
|
30
31
|
"./store.js": "./store.js",
|
|
@@ -35,6 +36,8 @@
|
|
|
35
36
|
"./upcast.js": "./upcast.js"
|
|
36
37
|
},
|
|
37
38
|
"files": [
|
|
39
|
+
"LICENSE",
|
|
40
|
+
"README.md",
|
|
38
41
|
"adapters.js",
|
|
39
42
|
"capability.js",
|
|
40
43
|
"citations.js",
|
|
@@ -55,6 +58,7 @@
|
|
|
55
58
|
"router.js",
|
|
56
59
|
"rules.js",
|
|
57
60
|
"search-engines.js",
|
|
61
|
+
"skill-vars.js",
|
|
58
62
|
"sources-retrieval.js",
|
|
59
63
|
"sources.js",
|
|
60
64
|
"store.js",
|
|
@@ -62,9 +66,7 @@
|
|
|
62
66
|
"tool-groups.js",
|
|
63
67
|
"tool-need.js",
|
|
64
68
|
"trajectory.js",
|
|
65
|
-
"upcast.js"
|
|
66
|
-
"LICENSE",
|
|
67
|
-
"README.md"
|
|
69
|
+
"upcast.js"
|
|
68
70
|
],
|
|
69
71
|
"scripts": {
|
|
70
72
|
"test": "node --test tests/*.test.js"
|
package/skill-vars.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// skill-vars.js — the placeholders a skill prompt may carry, declared ONCE.
|
|
2
|
+
//
|
|
3
|
+
// A skill prompt can interpolate a few runtime values: what the user typed, the
|
|
4
|
+
// selection, the page URL/title, today's date. Until this module existed that set
|
|
5
|
+
// lived as a regex chain inside the extension's side panel, and nothing else knew
|
|
6
|
+
// it — so three things drifted apart:
|
|
7
|
+
//
|
|
8
|
+
// • the EDITOR advertised placeholders in a placeholder attribute,
|
|
9
|
+
// • the PANEL substituted a different set,
|
|
10
|
+
// • prompt-assist was told to "preserve any {{placeholders}} verbatim" without
|
|
11
|
+
// being told which ones exist — so the model invented {{content}}, guarded it,
|
|
12
|
+
// and the user shipped a prompt with a slot nothing would ever fill.
|
|
13
|
+
//
|
|
14
|
+
// That last failure is silent by construction: an unknown placeholder is just text,
|
|
15
|
+
// so the model receives the literal characters `{{content}}` and usually papers over
|
|
16
|
+
// it. The skill looks broken for a reason no surface names. Declaring the set once
|
|
17
|
+
// makes the same list authoritative for the editor's lint, the assist prompt, and
|
|
18
|
+
// substitution — a fourth client (mobile, the gateway, the bridge) inherits it
|
|
19
|
+
// rather than re-deriving it.
|
|
20
|
+
//
|
|
21
|
+
// Platform access is INJECTED, never imported: the page URL comes from `chrome.tabs`
|
|
22
|
+
// in the extension, from the request in the gateway, and from nothing at all in a
|
|
23
|
+
// batch run. This module knows the contract; the host knows how to satisfy it.
|
|
24
|
+
|
|
25
|
+
export class SkillVarError extends Error {
|
|
26
|
+
constructor(code, message) { super(message); this.name = 'SkillVarError'; this.code = code; }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The complete set. `source` says where the value comes from, which is what the
|
|
31
|
+
* lint and the assist guidance need to explain it:
|
|
32
|
+
* 'args' — the text the user supplied with the invocation
|
|
33
|
+
* 'resolver' — the host fills it (page URL, title, selection, date)
|
|
34
|
+
*/
|
|
35
|
+
export const SKILL_VARS = Object.freeze([
|
|
36
|
+
Object.freeze({
|
|
37
|
+
name: 'input',
|
|
38
|
+
source: 'args',
|
|
39
|
+
labelled: true, // {{input:label}} — the label is a hint to the author, not sent
|
|
40
|
+
summary: 'What the user typed after the /command — or whatever is already in the composer when they pick the skill from the menu.',
|
|
41
|
+
}),
|
|
42
|
+
Object.freeze({
|
|
43
|
+
name: 'selection',
|
|
44
|
+
source: 'resolver',
|
|
45
|
+
summary: 'The text selected on the page right now.',
|
|
46
|
+
}),
|
|
47
|
+
Object.freeze({
|
|
48
|
+
name: 'url',
|
|
49
|
+
source: 'resolver',
|
|
50
|
+
summary: "The active tab's URL.",
|
|
51
|
+
}),
|
|
52
|
+
Object.freeze({
|
|
53
|
+
name: 'title',
|
|
54
|
+
source: 'resolver',
|
|
55
|
+
summary: "The active tab's title.",
|
|
56
|
+
}),
|
|
57
|
+
Object.freeze({
|
|
58
|
+
name: 'date',
|
|
59
|
+
source: 'resolver',
|
|
60
|
+
summary: "Today's date.",
|
|
61
|
+
}),
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
export const SKILL_VAR_NAMES = Object.freeze(SKILL_VARS.map((v) => v.name));
|
|
65
|
+
|
|
66
|
+
const BY_NAME = new Map(SKILL_VARS.map((v) => [v.name, v]));
|
|
67
|
+
|
|
68
|
+
export function skillVar(name) {
|
|
69
|
+
return BY_NAME.get(String(name || '').trim().toLowerCase()) || null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Any {{ token }}, with an optional :label. Deliberately permissive on what it
|
|
73
|
+
// CAPTURES — an unknown name has to be recognised as a placeholder before it can be
|
|
74
|
+
// reported as unknown. A pattern that only matched known names would make the
|
|
75
|
+
// {{content}} class of bug invisible all over again.
|
|
76
|
+
const TOKEN = /\{\{\s*([a-z_][a-z0-9_-]*)\s*(?::([^}]*))?\s*\}\}/gi;
|
|
77
|
+
|
|
78
|
+
/** The token pattern for one variable — `g` and `i`, fresh each call (no lastIndex sharing). */
|
|
79
|
+
export function skillVarPattern(name) {
|
|
80
|
+
const v = skillVar(name);
|
|
81
|
+
if (!v) throw new SkillVarError('UNKNOWN_VAR', `unknown skill variable '${name}'`);
|
|
82
|
+
return v.labelled
|
|
83
|
+
? new RegExp(`\\{\\{\\s*${v.name}(?::[^}]*)?\\s*\\}\\}`, 'gi')
|
|
84
|
+
: new RegExp(`\\{\\{\\s*${v.name}\\s*\\}\\}`, 'gi');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Every placeholder in a prompt, in source order.
|
|
89
|
+
* -> [{ name, label, raw, index, known }]
|
|
90
|
+
*/
|
|
91
|
+
export function parseSkillVars(text) {
|
|
92
|
+
const out = [];
|
|
93
|
+
const src = String(text || '');
|
|
94
|
+
TOKEN.lastIndex = 0;
|
|
95
|
+
let m = TOKEN.exec(src);
|
|
96
|
+
while (m) {
|
|
97
|
+
const name = m[1].toLowerCase();
|
|
98
|
+
out.push({
|
|
99
|
+
name,
|
|
100
|
+
label: (m[2] || '').trim(),
|
|
101
|
+
raw: m[0],
|
|
102
|
+
index: m.index,
|
|
103
|
+
known: BY_NAME.has(name),
|
|
104
|
+
});
|
|
105
|
+
m = TOKEN.exec(src);
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Damerau-free Levenshtein, bounded by the short names we compare. Enough to turn
|
|
111
|
+
// {{content}} → {{input}}? No — those are not near neighbours, which is the point:
|
|
112
|
+
// a bad suggestion is worse than none, so `suggestSkillVar` falls back to the input
|
|
113
|
+
// slot only when the unknown name READS like a content slot.
|
|
114
|
+
function distance(a, b) {
|
|
115
|
+
const m = a.length;
|
|
116
|
+
const n = b.length;
|
|
117
|
+
let prev = Array.from({ length: n + 1 }, (_, j) => j);
|
|
118
|
+
for (let i = 1; i <= m; i += 1) {
|
|
119
|
+
const row = [i];
|
|
120
|
+
for (let j = 1; j <= n; j += 1) {
|
|
121
|
+
row[j] = Math.min(
|
|
122
|
+
prev[j] + 1,
|
|
123
|
+
row[j - 1] + 1,
|
|
124
|
+
prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
prev = row;
|
|
128
|
+
}
|
|
129
|
+
return prev[n];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Names a model reaches for when it invents a "put the user's text here" slot. These
|
|
133
|
+
// are NOT aliases — nothing substitutes them, because silently filling a placeholder
|
|
134
|
+
// the user never agreed to is how a prompt starts meaning something else. They only
|
|
135
|
+
// make the suggestion in the lint concrete.
|
|
136
|
+
const INPUT_SHAPED = new Set([
|
|
137
|
+
'content', 'text', 'body', 'draft', 'document', 'doc', 'args', 'arguments',
|
|
138
|
+
'query', 'question', 'task', 'request', 'prompt', 'message', 'user_input', 'userinput',
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
/** Nearest known variable for a misspelling or an invented name, or '' if none is close. */
|
|
142
|
+
export function suggestSkillVar(name) {
|
|
143
|
+
const q = String(name || '').trim().toLowerCase();
|
|
144
|
+
if (!q || BY_NAME.has(q)) return '';
|
|
145
|
+
if (INPUT_SHAPED.has(q)) return 'input';
|
|
146
|
+
let best = '';
|
|
147
|
+
let bestD = Infinity;
|
|
148
|
+
for (const known of SKILL_VAR_NAMES) {
|
|
149
|
+
const d = distance(q, known);
|
|
150
|
+
if (d < bestD) { bestD = d; best = known; }
|
|
151
|
+
}
|
|
152
|
+
// Two edits, or a third of a longer name — enough to catch a transposition
|
|
153
|
+
// ("titel") without turning an unrelated word into a confident wrong guess.
|
|
154
|
+
return bestD <= Math.max(2, Math.floor(q.length / 3)) ? best : '';
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Static check of a prompt, for the skill editor.
|
|
159
|
+
* -> { known: [names], unknown: [{ name, raw, suggestion }], hasInput: boolean }
|
|
160
|
+
*
|
|
161
|
+
* `hasInput` is what decides whether the caller's text goes INTO the prompt or gets
|
|
162
|
+
* appended after it, so the editor can explain which of the two will happen.
|
|
163
|
+
*/
|
|
164
|
+
export function lintSkillPrompt(text) {
|
|
165
|
+
const seen = parseSkillVars(text);
|
|
166
|
+
const known = [];
|
|
167
|
+
const unknown = [];
|
|
168
|
+
const dedupe = new Set();
|
|
169
|
+
for (const tok of seen) {
|
|
170
|
+
if (dedupe.has(tok.name)) continue;
|
|
171
|
+
dedupe.add(tok.name);
|
|
172
|
+
if (tok.known) known.push(tok.name);
|
|
173
|
+
else unknown.push({ name: tok.name, raw: tok.raw, suggestion: suggestSkillVar(tok.name) });
|
|
174
|
+
}
|
|
175
|
+
return { known, unknown, hasInput: dedupe.has('input') };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Fill a prompt's placeholders.
|
|
180
|
+
*
|
|
181
|
+
* @param text the skill prompt
|
|
182
|
+
* @param args the user's text, for {{input}}
|
|
183
|
+
* @param resolvers { selection, url, title, date } — each () => string | Promise<string>.
|
|
184
|
+
* A resolver is called ONLY when its variable actually appears, so a
|
|
185
|
+
* prompt without {{selection}} never pays for a tab read. A missing
|
|
186
|
+
* resolver, or one that throws, yields '' and is reported in `empty`
|
|
187
|
+
* rather than failing the turn.
|
|
188
|
+
*
|
|
189
|
+
* -> { text, filled: [names], empty: [names], unknown: [{ name, raw, suggestion }] }
|
|
190
|
+
*
|
|
191
|
+
* UNKNOWN PLACEHOLDERS ARE LEFT ALONE. Rewriting a user's prompt because we did not
|
|
192
|
+
* recognise a token would be a silent edit of the thing they wrote; reporting it lets
|
|
193
|
+
* the caller say so out loud, which is the actual fix.
|
|
194
|
+
*/
|
|
195
|
+
export async function substituteSkillVars(text, { args = '', resolvers = {} } = {}) {
|
|
196
|
+
const src = String(text || '');
|
|
197
|
+
const lint = lintSkillPrompt(src);
|
|
198
|
+
if (!lint.known.length) return { text: src, filled: [], empty: [], unknown: lint.unknown };
|
|
199
|
+
|
|
200
|
+
const filled = [];
|
|
201
|
+
const empty = [];
|
|
202
|
+
let out = src;
|
|
203
|
+
|
|
204
|
+
for (const name of lint.known) {
|
|
205
|
+
let value = '';
|
|
206
|
+
if (name === 'input') {
|
|
207
|
+
value = args == null ? '' : String(args);
|
|
208
|
+
} else {
|
|
209
|
+
const resolve = resolvers[name];
|
|
210
|
+
if (typeof resolve === 'function') {
|
|
211
|
+
try {
|
|
212
|
+
value = (await resolve()) ?? '';
|
|
213
|
+
} catch {
|
|
214
|
+
value = ''; // a dead tab or a denied permission is an empty slot, not a failure
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
value = String(value);
|
|
218
|
+
}
|
|
219
|
+
if (value.trim()) filled.push(name);
|
|
220
|
+
else empty.push(name);
|
|
221
|
+
out = out.replace(skillVarPattern(name), () => value);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return { text: out, filled, empty, unknown: lint.unknown };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The sentence prompt-assist needs so the model stops inventing placeholders.
|
|
229
|
+
* Generated from SKILL_VARS so a variable added here reaches the assist prompt
|
|
230
|
+
* without anyone remembering to update a string.
|
|
231
|
+
*/
|
|
232
|
+
export function skillVarGuidance() {
|
|
233
|
+
const list = SKILL_VARS.map((v) => `{{${v.name}}} (${v.summary})`).join(' ');
|
|
234
|
+
return `The prompt may use ONLY these placeholders, and they are filled at run time: ${list} `
|
|
235
|
+
+ 'Preserve the ones already present verbatim, and never invent another — an unrecognised '
|
|
236
|
+
+ `placeholder is sent to the model as literal text. For the user's own text use {{input}}.`;
|
|
237
|
+
}
|