@aibranch/llm-meta-widget 0.7.5 → 0.8.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/README.md
CHANGED
|
@@ -211,10 +211,17 @@ Declared inline on the same view as the widget. Runs as JavaScript in the browse
|
|
|
211
211
|
|
|
212
212
|
<!-- (b) State readers folded into the LLM's system prompt EVERY turn, -->
|
|
213
213
|
<!-- so the LLM can answer from page state without a tool_call. -->
|
|
214
|
+
<!-- Each reader declares what it returns, like every other channel. -->
|
|
214
215
|
<script>
|
|
215
216
|
window.aiState = {
|
|
216
|
-
text:
|
|
217
|
-
|
|
217
|
+
text: {
|
|
218
|
+
description: "the text the user wants to annotate",
|
|
219
|
+
read: function() { return $("#text").val() || ""; }
|
|
220
|
+
},
|
|
221
|
+
selected_dictionaries: {
|
|
222
|
+
description: "the dictionaries the user has chosen for annotation",
|
|
223
|
+
read: function() { return getSelected(); }
|
|
224
|
+
}
|
|
218
225
|
};
|
|
219
226
|
</script>
|
|
220
227
|
|
|
@@ -226,7 +233,17 @@ Declared inline on the same view as the widget. Runs as JavaScript in the browse
|
|
|
226
233
|
</script>
|
|
227
234
|
```
|
|
228
235
|
|
|
229
|
-
**What the LLM sees.** The `ai-actions` JSON is passed to the meta-server as `local_tools` for every turn. Alongside it, every reader in `window.aiState` is invoked (each turn) and
|
|
236
|
+
**What the LLM sees.** The `ai-actions` JSON is passed to the meta-server as `local_tools` for every turn. Alongside it, every reader in `window.aiState` is invoked (each turn) and rendered into a `Current page state:` block appended to the system prompt, one line per reader, with its description beside its value:
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
Current page state:
|
|
240
|
+
- text (the text the user wants to annotate): "The stomach was examined."
|
|
241
|
+
- selected_dictionaries (the dictionaries the user has chosen for annotation): ["uberon"]
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
So the LLM can answer from state directly instead of tool-calling for lookups, and knows what each value *means* rather than guessing from the key.
|
|
245
|
+
|
|
246
|
+
**Breaking change in 0.8.0.** A reader must be `{ description, read }`. The bare-function form — `text: function() {…}` — is removed, with no fallback. A key that still uses it is reported to the console by name, with the shape it should have, and skipped; the page's other readers keep working. Descriptions are not decoration: every other channel the widget declares to the model already carries one (Class 3 tool schemas, Class 2 `.well-known` tools, Class 1 hub tools, resources), and state readers were the exception.
|
|
230
247
|
|
|
231
248
|
**When the tool_call fires.** During the turn, as soon as the LLM emits the
|
|
232
249
|
call. Since 0.4.0 the action's outcome — `{ok: true, applied: "<name>"}`, or
|
|
@@ -279,7 +296,7 @@ Declared out-of-band on the meta-server (via the hub's admin UI or `/user/:id/mc
|
|
|
279
296
|
|
|
280
297
|
**Declaration.** No widget-side change. Register the server on the hub → flip `public: true` (visible to signed-in users) and optionally `public_to_anonymous: true` (visible to widget visitors without a login). See the meta-server's `Api::McpServersController`.
|
|
281
298
|
|
|
282
|
-
**What the LLM sees.** Only tools
|
|
299
|
+
**What the LLM sees.** Only tools the visitor has **enabled via the tool picker** (see "Level-1 pickers" below) — nothing is auto-selected, the visitor opts in per session. A page can set the starting selection instead, with `remote_tools_schema_id:`: those tools arrive already ticked, and the visitor can still untick them.
|
|
283
300
|
|
|
284
301
|
**When the tool_call fires.** Synchronously during the turn — widget POSTs to the hub's `/api/llm_api_keys/:uuid/models/:name/single_llm_calls` endpoint with `tool_ids: [...]`; the hub proxies to each MCP server and streams results back through SSE.
|
|
285
302
|
|
|
@@ -332,9 +349,9 @@ translate directly.
|
|
|
332
349
|
| `api_key_uuid:` | `"ollama-local"` | Hub API-key uuid to invoke |
|
|
333
350
|
| `element_path:` | `"/llm_meta_widget_assets/llm-meta-widget.js"` | The bundled element the page loads. Served by the gem's engine; override to load it from elsewhere |
|
|
334
351
|
| `actions_schema_id:` | `"ai-actions"` | DOM id of the Class-3 schema block |
|
|
335
|
-
| `state_global:` | `"aiState"` | Global window object holding Class-3 state readers |
|
|
352
|
+
| `state_global:` | `"aiState"` | Global window object holding Class-3 state readers, each `{ description, read }` (see the breaking change in 0.8.0) |
|
|
336
353
|
| `actions_global:` | `"aiActions"` | Global window object holding Class-3 implementations |
|
|
337
|
-
| `remote_tools_schema_id:` | `"remote-mcp-tools"` |
|
|
354
|
+
| `remote_tools_schema_id:` | `"remote-mcp-tools"` | DOM id of a JSON block listing Class-1 tools (`{id, name, description, input_schema}`) to start with. They seed the picker rather than bypassing it — shown ticked, and the visitor may untick them. Read once, at boot |
|
|
338
355
|
| `well_known_urls:` | `nil` | `nil` = auto-discover same-origin; explicit array = fetch those; `[]` = disable |
|
|
339
356
|
| `greeting:` | `nil` | First thing a visitor sees when the panel opens, above the offered prompt templates. `nil` = a generic line |
|
|
340
357
|
| `max_rounds:` | `3` | Cap on tool-call rounds per LLM turn. Page actions cost a round each since 0.4.0 — raise it for multi-step flows |
|
|
@@ -457,6 +474,10 @@ it most, since they must already know your form to press it.
|
|
|
457
474
|
behind the custom-element move: the npm package and its CDN URL, why `main`,
|
|
458
475
|
`exports` and `sideEffects` are set the way they are, and why the gem and the npm
|
|
459
476
|
package must never drift in version.
|
|
477
|
+
- **A page you can run** — `examples/connection-test.html` checks every call the
|
|
478
|
+
widget makes to a hub, one at a time, then mounts the widget with a tool
|
|
479
|
+
already selected. Serve it from an origin the hub allows and open it: if
|
|
480
|
+
something in the chain is wrong, it names which step.
|
|
460
481
|
- **Issues and questions** — <https://github.com/jdkim/llm_meta_widget/issues>
|
|
461
482
|
|
|
462
483
|
## License
|
|
@@ -642,7 +642,8 @@ async function resourceLinesForTurn({ plan, cached, endpoint, read, budgetBytes
|
|
|
642
642
|
}
|
|
643
643
|
var PROMPT_ARG_ALIASES = { dictionaries: "selected_dictionaries" };
|
|
644
644
|
function promptArgFromState(argName, state) {
|
|
645
|
-
const
|
|
645
|
+
const entry = state && state[argName] || state && state[PROMPT_ARG_ALIASES[argName]];
|
|
646
|
+
const reader = entry && typeof entry.read === "function" ? entry.read : null;
|
|
646
647
|
if (typeof reader !== "function") return "";
|
|
647
648
|
let value;
|
|
648
649
|
try {
|
|
@@ -3765,17 +3766,23 @@ function boot(cfg) {
|
|
|
3765
3766
|
var actionsSchemaEl = document.getElementById(ACTIONS_SCHEMA_ID);
|
|
3766
3767
|
var localTools = actionsSchemaEl ? JSON.parse(actionsSchemaEl.textContent) : [];
|
|
3767
3768
|
var remoteToolsEl = document.getElementById(REMOTE_TOOLS_SCHEMA_ID);
|
|
3768
|
-
var
|
|
3769
|
+
var baselineRemoteTools = remoteToolsEl ? JSON.parse(remoteToolsEl.textContent) : [];
|
|
3770
|
+
var remoteTools = baselineRemoteTools.slice();
|
|
3769
3771
|
var hubMcpServers = [];
|
|
3770
3772
|
var selectedToolIds = /* @__PURE__ */ new Set();
|
|
3773
|
+
baselineRemoteTools.forEach(function(t) {
|
|
3774
|
+
selectedToolIds.add(t.id);
|
|
3775
|
+
});
|
|
3771
3776
|
function anyAllowedByAllowlist(name, allowlist) {
|
|
3772
3777
|
return allowlist === null || allowlist.indexOf(name) >= 0;
|
|
3773
3778
|
}
|
|
3774
3779
|
function refreshRemoteToolsFromPicker() {
|
|
3775
3780
|
var flat = [];
|
|
3781
|
+
var seen = {};
|
|
3776
3782
|
hubMcpServers.forEach(function(s) {
|
|
3777
3783
|
(s.tools || []).forEach(function(t) {
|
|
3778
3784
|
if (!selectedToolIds.has(t.id)) return;
|
|
3785
|
+
seen[t.id] = true;
|
|
3779
3786
|
flat.push({
|
|
3780
3787
|
id: t.id,
|
|
3781
3788
|
name: t.name,
|
|
@@ -3784,6 +3791,12 @@ function boot(cfg) {
|
|
|
3784
3791
|
});
|
|
3785
3792
|
});
|
|
3786
3793
|
});
|
|
3794
|
+
baselineRemoteTools.forEach(function(t) {
|
|
3795
|
+
if (!seen[t.id] && selectedToolIds.has(t.id)) {
|
|
3796
|
+
flat.push(t);
|
|
3797
|
+
seen[t.id] = true;
|
|
3798
|
+
}
|
|
3799
|
+
});
|
|
3787
3800
|
remoteTools = flat;
|
|
3788
3801
|
if (toolsCountEl) {
|
|
3789
3802
|
toolsCountEl.textContent = flat.length;
|
|
@@ -4254,17 +4267,51 @@ function boot(cfg) {
|
|
|
4254
4267
|
currentThinkingBlock = null;
|
|
4255
4268
|
currentThinkingBody = null;
|
|
4256
4269
|
}
|
|
4270
|
+
function stateReaders() {
|
|
4271
|
+
var declared = window[STATE_GLOBAL] || {};
|
|
4272
|
+
var out = [];
|
|
4273
|
+
Object.keys(declared).forEach(function(key) {
|
|
4274
|
+
var entry = declared[key];
|
|
4275
|
+
var hasDescription = entry && typeof entry.description === "string" && entry.description.trim() !== "";
|
|
4276
|
+
var hasRead = entry && typeof entry.read === "function";
|
|
4277
|
+
if (!hasDescription || !hasRead) {
|
|
4278
|
+
console.error(
|
|
4279
|
+
"[llm-meta-widget] " + STATE_GLOBAL + "." + key + ' is not a valid state reader. Expected { description: "what this value is", read: function () { \u2026 } }, got ' + (typeof entry === "function" ? "a bare function (the 0.7 form, removed in 0.8)" : describeValue(entry)) + ". Skipping " + key + "."
|
|
4280
|
+
);
|
|
4281
|
+
return;
|
|
4282
|
+
}
|
|
4283
|
+
out.push({ key, description: entry.description, read: entry.read });
|
|
4284
|
+
});
|
|
4285
|
+
return out;
|
|
4286
|
+
}
|
|
4287
|
+
function describeValue(v) {
|
|
4288
|
+
if (v === null) return "null";
|
|
4289
|
+
if (Array.isArray(v)) return "an array";
|
|
4290
|
+
if (typeof v === "object") {
|
|
4291
|
+
var missing = [];
|
|
4292
|
+
if (typeof v.description !== "string" || v.description.trim() === "") missing.push("description");
|
|
4293
|
+
if (typeof v.read !== "function") missing.push("read");
|
|
4294
|
+
return "an object missing " + missing.join(" and ");
|
|
4295
|
+
}
|
|
4296
|
+
return typeof v;
|
|
4297
|
+
}
|
|
4257
4298
|
function currentPageState() {
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
Object.keys(reader).forEach(function(k) {
|
|
4299
|
+
return stateReaders().map(function(r) {
|
|
4300
|
+
var value;
|
|
4261
4301
|
try {
|
|
4262
|
-
|
|
4302
|
+
value = r.read();
|
|
4263
4303
|
} catch (e) {
|
|
4264
|
-
|
|
4304
|
+
value = "<error: " + e.message + ">";
|
|
4265
4305
|
}
|
|
4306
|
+
return { key: r.key, description: r.description, value };
|
|
4307
|
+
});
|
|
4308
|
+
}
|
|
4309
|
+
function pageStateLines() {
|
|
4310
|
+
var readers = currentPageState();
|
|
4311
|
+
if (readers.length === 0) return ["(this page declares no state)"];
|
|
4312
|
+
return readers.map(function(r) {
|
|
4313
|
+
return "- " + r.key + " (" + r.description + "): " + JSON.stringify(r.value);
|
|
4266
4314
|
});
|
|
4267
|
-
return out;
|
|
4268
4315
|
}
|
|
4269
4316
|
function currentSystemPrompt(resourceLines) {
|
|
4270
4317
|
return [
|
|
@@ -4276,9 +4323,8 @@ function boot(cfg) {
|
|
|
4276
4323
|
"3. After a tool returns a result, use it: either take the next step the task needs, or \u2014 if the task is done \u2014 answer in plain text. Do not stop silently after a tool call.",
|
|
4277
4324
|
"4. NEVER repeat a call you have already made with the same or similar arguments \u2014 its result is already in the conversation history.",
|
|
4278
4325
|
"",
|
|
4279
|
-
"Current page state:"
|
|
4280
|
-
|
|
4281
|
-
].concat(resourceLines || []).join("\n");
|
|
4326
|
+
"Current page state:"
|
|
4327
|
+
].concat(pageStateLines()).concat(resourceLines || []).join("\n");
|
|
4282
4328
|
}
|
|
4283
4329
|
async function resourceLinesForThisTurn() {
|
|
4284
4330
|
var turn = await resourceLinesForTurn({
|
|
@@ -971,7 +971,12 @@ export async function resourceLinesForTurn({ plan, cached, endpoint, read, budge
|
|
|
971
971
|
const PROMPT_ARG_ALIASES = { dictionaries: "selected_dictionaries" }
|
|
972
972
|
|
|
973
973
|
export function promptArgFromState(argName, state) {
|
|
974
|
-
|
|
974
|
+
// Same object shape the system-prompt annex consumes: { description, read }.
|
|
975
|
+
// The bare-function form was removed in 0.8.0, so an entry without `read` is
|
|
976
|
+
// simply not a reader — the prompt argument stays empty and the visitor fills
|
|
977
|
+
// it, which is the documented behaviour for state the page cannot supply.
|
|
978
|
+
const entry = (state && state[argName]) || (state && state[PROMPT_ARG_ALIASES[argName]])
|
|
979
|
+
const reader = entry && typeof entry.read === "function" ? entry.read : null
|
|
975
980
|
if (typeof reader !== "function") return ""
|
|
976
981
|
let value
|
|
977
982
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aibranch/llm-meta-widget",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"//version": "Must stay in step with lib/llm_meta_widget/version.rb. Two independently versioned copies of the same client is the drift docs/custom-element-distribution.md warns about: bump both, release both.",
|
|
5
5
|
"description": "Embeddable browser chat widget: a custom element that discovers tools at page, origin and hub scope and dispatches each tool call to the right one. Two lines of HTML on any host, Rails or not.",
|
|
6
6
|
"keywords": [
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"esbuild": "^0.28.2",
|
|
51
51
|
"eslint": "^9.0.0",
|
|
52
|
-
"globals": "^15.0.0"
|
|
52
|
+
"globals": "^15.0.0",
|
|
53
|
+
"jsdom": "^29.1.1"
|
|
53
54
|
}
|
|
54
55
|
}
|