@aibranch/llm-meta-widget 0.7.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/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 DBCLS
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
# llm_meta_widget
|
|
2
|
+
|
|
3
|
+
Embeddable browser chat widget for the [llm_meta](https://github.com/pubannotation) ecosystem.
|
|
4
|
+
|
|
5
|
+
Client-orchestrated: the widget fetches host-side action schemas + host-published `.well-known/mcp.json` manifests at boot, dispatches tool_calls locally (page-embedded actions) or directly to MCP endpoints (host-wide well-known), and consumes the meta-server's SSE `single_llm_calls` API. Ships one Rails helper + one view partial + a served orchestrator JS module.
|
|
6
|
+
|
|
7
|
+
**No** Devise, DB migrations, ChatManager, or PromptNavigator. Adds only `rails >= 8.0` as a runtime dep — so hosts that haven't bumped to 8.1 can adopt it without a Rails upgrade.
|
|
8
|
+
|
|
9
|
+
## What you need first
|
|
10
|
+
|
|
11
|
+
The widget is a browser front end: it needs something to answer the chat. That
|
|
12
|
+
can be **[llm_meta_server](https://github.com/pubannotation)** — a hub that
|
|
13
|
+
holds provider credentials and streams responses back — or **an Ollama**,
|
|
14
|
+
which the widget talks to directly with no server component of ours in
|
|
15
|
+
between.
|
|
16
|
+
|
|
17
|
+
Those are separate from where tools come from. A hub also registers MCP tools
|
|
18
|
+
(Class 1 below), and you can point at one for tools while a local Ollama
|
|
19
|
+
answers the chat, or use no hub at all. Page actions and your own
|
|
20
|
+
`.well-known/mcp.json` never involve a hub either way.
|
|
21
|
+
|
|
22
|
+
Nothing else is required: no database, no migrations, no JavaScript build
|
|
23
|
+
step, no Node at runtime.
|
|
24
|
+
|
|
25
|
+
| Requirement | Version |
|
|
26
|
+
|---|---|
|
|
27
|
+
| Ruby | >= 3.2 |
|
|
28
|
+
| Rails | >= 8.0 (8.1 not required) |
|
|
29
|
+
| An llm_meta_server **or** an Ollama | any |
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
# Gemfile
|
|
35
|
+
gem "llm_meta_widget", "~> 0.4"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
bundle install
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
That is the whole install. There is **no `mount` to add** — the engine
|
|
43
|
+
deliberately skips `isolate_namespace`, so the helper is included into
|
|
44
|
+
ActionView and the asset routes appear at the host's top level on their own.
|
|
45
|
+
There are no migrations and no generators to run.
|
|
46
|
+
|
|
47
|
+
## Minimal working example
|
|
48
|
+
|
|
49
|
+
Put this on any view — a fresh `pages/demo.html.erb` is fine:
|
|
50
|
+
|
|
51
|
+
```erb
|
|
52
|
+
<h1>Demo</h1>
|
|
53
|
+
|
|
54
|
+
<%= llm_meta_widget(llm_url: "https://your-meta-server.example",
|
|
55
|
+
model: "qwen3-6-35b-fast",
|
|
56
|
+
greeting: "Hi — ask me anything about this page.") %>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Start the app, open that page, and click the round toggle at the bottom right.
|
|
60
|
+
**Success looks like:** a panel opens showing your greeting, you type "hello",
|
|
61
|
+
the role label turns into a spinning gear reading "Working…", and the reply
|
|
62
|
+
streams in.
|
|
63
|
+
|
|
64
|
+
If the panel opens but nothing comes back, it is almost always CORS. The hub
|
|
65
|
+
must list your app's origin in its `config/initializers/cors.rb` for the
|
|
66
|
+
`/api/*` resource, because the widget calls the hub **from the visitor's
|
|
67
|
+
browser**, not from your server. Open the browser console: a blocked request
|
|
68
|
+
says so explicitly.
|
|
69
|
+
|
|
70
|
+
Two more things worth knowing before you go further:
|
|
71
|
+
|
|
72
|
+
- `llm_url` must be reachable from your visitors' browsers, not just from
|
|
73
|
+
your server. `localhost` works only while you are the visitor — which is
|
|
74
|
+
fine when each visitor runs their own Ollama.
|
|
75
|
+
- The model name is the hub's name for it (`GET /api/llms` lists them), not
|
|
76
|
+
the provider's.
|
|
77
|
+
|
|
78
|
+
Once that works, the widget can converse but cannot *do* anything. To let the
|
|
79
|
+
LLM act on your page or call your own services, declare tools using any of the
|
|
80
|
+
**three tool classes** below.
|
|
81
|
+
|
|
82
|
+
## Any host: the custom element
|
|
83
|
+
|
|
84
|
+
The widget is a custom element in a single self-contained ES module, so a host
|
|
85
|
+
that is not Rails needs no gem, no template engine and no asset pipeline — two
|
|
86
|
+
lines of HTML:
|
|
87
|
+
|
|
88
|
+
```html
|
|
89
|
+
<script type="module" src="https://cdn.jsdelivr.net/npm/@aibranch/llm-meta-widget@0.7"></script>
|
|
90
|
+
<llm-meta-widget llm-url="https://your-hub.example"
|
|
91
|
+
model="qwen3-8-27b-fast"
|
|
92
|
+
greeting="Hi — ask me anything about this page."></llm-meta-widget>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Nothing else is needed: the stylesheets and the markdown renderer are bundled
|
|
96
|
+
in, and the element injects its own styles. The host serves no CSS and no JS.
|
|
97
|
+
|
|
98
|
+
Three ways to get that one file, in descending order of convenience:
|
|
99
|
+
|
|
100
|
+
- **the CDN**, as above — `@aibranch/llm-meta-widget` on npm, no path needed
|
|
101
|
+
because the package's `main` is the bundle;
|
|
102
|
+
- **the gem**, which serves the identical file at
|
|
103
|
+
`/llm_meta_widget_assets/llm-meta-widget.js` for Rails hosts, and is what the
|
|
104
|
+
`llm_meta_widget` helper points at;
|
|
105
|
+
- **self-hosted** — copy it out of the package or the gem and serve it as a
|
|
106
|
+
static asset, if you would rather not depend on a CDN.
|
|
107
|
+
|
|
108
|
+
The two Rails-specific pieces are gone from the host's side of the contract.
|
|
109
|
+
What is NOT gone is the host page's own contract, because those pieces belong to
|
|
110
|
+
the host: the `#ai-actions` JSON block, `window.aiState` and `window.aiActions`
|
|
111
|
+
are declared exactly as they are under Rails (see the three tool classes below).
|
|
112
|
+
|
|
113
|
+
**Attributes** map one-to-one onto the helper's keyword options. Three are not
|
|
114
|
+
obvious and are the ones that bite:
|
|
115
|
+
|
|
116
|
+
| attribute | notes |
|
|
117
|
+
|---|---|
|
|
118
|
+
| `llm-url`, `model` | required |
|
|
119
|
+
| `tool-hub-url` | omit for no Class 1; `""` is the same as omitting |
|
|
120
|
+
| `llm-provider` | `llm_meta_server` (default) or `ollama` |
|
|
121
|
+
| `api-key-uuid`, `greeting`, `max-rounds` | as the helper options |
|
|
122
|
+
| `actions-schema-id`, `state-global`, `actions-global`, `remote-tools-schema-id` | as the helper options |
|
|
123
|
+
| `models`, `hub-tools` | comma-separated. Omitted **or empty** means no allowlist — an allowlist permitting nothing is never what anyone meant |
|
|
124
|
+
| `enable-model-picker`, `enable-tool-picker` | **value attributes, not boolean attributes.** They default to true, so presence cannot mean true. Disable with `enable-tool-picker="false"`; any other value is true |
|
|
125
|
+
| `well-known-urls` | **tri-state**, because an attribute cannot express nil-versus-empty: omitted = auto-discover same-origin `/.well-known/mcp.json`; `""` = discovery off; `"a,b"` = fetch those |
|
|
126
|
+
|
|
127
|
+
That last one is the quiet failure to watch for: expecting discovery off and
|
|
128
|
+
getting a same-origin fetch looks like nothing at all, except a 404 in the
|
|
129
|
+
console.
|
|
130
|
+
|
|
131
|
+
One widget per page. The panel uses fixed element ids, so a second
|
|
132
|
+
`<llm-meta-widget>` is ignored with a console warning rather than fighting the
|
|
133
|
+
first over every lookup.
|
|
134
|
+
|
|
135
|
+
**Building it.** `npm run build` bundles `element.js`, `config.js`,
|
|
136
|
+
`orchestrator.js`, the vendored `marked` and both stylesheets with esbuild. The
|
|
137
|
+
result is committed and shipped in the gem, because a gem cannot run a build
|
|
138
|
+
step on install — and `npm test` fails if the committed bundle has drifted from
|
|
139
|
+
its sources.
|
|
140
|
+
|
|
141
|
+
## Three tool classes
|
|
142
|
+
|
|
143
|
+
The widget classifies every tool_call the LLM emits by name and dispatches to one of three execution paths. Each class has a different declaration, different visibility to the LLM, and different execution semantics.
|
|
144
|
+
|
|
145
|
+
### Class 3 — Page-embedded actions (`window.aiActions`)
|
|
146
|
+
|
|
147
|
+
Declared inline on the same view as the widget. Runs as JavaScript in the browser — no HTTP hop, no server involvement. Best for actions that mutate the page's own state ("click this button", "add to selection", "scroll to X").
|
|
148
|
+
|
|
149
|
+
**Declaration** — three script blocks on the view:
|
|
150
|
+
|
|
151
|
+
```html
|
|
152
|
+
<!-- (a) Tool schemas the LLM sees. JSON Schema vocabulary — in practice
|
|
153
|
+
type, properties, required, description, items, enum. No $schema is
|
|
154
|
+
declared and nothing here validates one: the object is forwarded to
|
|
155
|
+
the provider as-is, and each provider accepts its own subset. -->
|
|
156
|
+
<script type="application/json" id="ai-actions">
|
|
157
|
+
[
|
|
158
|
+
{ "name": "add_dictionaries",
|
|
159
|
+
"description": "Add the named dictionaries to the current selection.",
|
|
160
|
+
"input_schema": {
|
|
161
|
+
"type": "object",
|
|
162
|
+
"properties": { "names": { "type": "array", "items": { "type": "string" } } },
|
|
163
|
+
"required": ["names"]
|
|
164
|
+
} }
|
|
165
|
+
]
|
|
166
|
+
</script>
|
|
167
|
+
|
|
168
|
+
<!-- (b) State readers folded into the LLM's system prompt EVERY turn, -->
|
|
169
|
+
<!-- so the LLM can answer from page state without a tool_call. -->
|
|
170
|
+
<script>
|
|
171
|
+
window.aiState = {
|
|
172
|
+
text: function() { return $("#text").val() || ""; },
|
|
173
|
+
selected_dictionaries: function() { return getSelected(); }
|
|
174
|
+
};
|
|
175
|
+
</script>
|
|
176
|
+
|
|
177
|
+
<!-- (c) Action implementations invoked when the LLM emits a matching tool_call. -->
|
|
178
|
+
<script>
|
|
179
|
+
window.aiActions = {
|
|
180
|
+
add_dictionaries: function(args) { /* mutate the page here */ }
|
|
181
|
+
};
|
|
182
|
+
</script>
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**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 the results are JSON-serialized into a `Current page state:` block appended to the system prompt — the LLM can answer from state directly instead of tool-calling for lookups.
|
|
186
|
+
|
|
187
|
+
**When the tool_call fires.** During the turn, as soon as the LLM emits the
|
|
188
|
+
call. Since 0.4.0 the action's outcome — `{ok: true, applied: "<name>"}`, or
|
|
189
|
+
the error message if it raised — is fed back to the LLM as a tool result, and
|
|
190
|
+
the turn continues. Before 0.4.0 these were fire-and-forget, which meant a
|
|
191
|
+
turn whose only calls were page actions ended there: any task shaped *change
|
|
192
|
+
the page, then do something with it* was cut off after the write. The change
|
|
193
|
+
also means the LLM can see that an action failed and correct itself, instead
|
|
194
|
+
of the failure being visible only to the visitor.
|
|
195
|
+
|
|
196
|
+
Budget for it: every page action now costs a round, so a flow that writes twice
|
|
197
|
+
and then calls a tool needs roughly five or six. See `max_rounds:` below.
|
|
198
|
+
|
|
199
|
+
**Args**: parsed JS object matching the `input_schema`. Return value ignored. Async allowed (widget doesn't await, but browser will still execute the promise). Exceptions logged and surfaced as an `❌ <name>` chip in the message footer.
|
|
200
|
+
|
|
201
|
+
**LLM tried a name that isn't declared** → widget appends a `system: Not available on this page: <name>` message and continues.
|
|
202
|
+
|
|
203
|
+
### Class 2 — Host-wide MCP (`.well-known/mcp.json`)
|
|
204
|
+
|
|
205
|
+
Declared once per host origin via a well-known manifest. Widget fetches the manifest at boot, exposes the listed tools to the LLM, and (on tool_call) POSTs directly to each tool's endpoint over MCP JSON-RPC. **Meta-server is not involved** for Class 2 tool execution.
|
|
206
|
+
|
|
207
|
+
**Declaration.** Publish at `https://your-host.example/.well-known/mcp.json`:
|
|
208
|
+
|
|
209
|
+
```json
|
|
210
|
+
{
|
|
211
|
+
"servers": [{
|
|
212
|
+
"name": "myhost",
|
|
213
|
+
"url": "/mcp",
|
|
214
|
+
"tools": [
|
|
215
|
+
{ "name": "annotate", "description": "…", "input_schema": {…} }
|
|
216
|
+
]
|
|
217
|
+
}]
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
- `url` is resolved against the host's origin (`/mcp` → `https://your-host.example/mcp`). Absolute URLs are also allowed but must be same-origin for the browser to reach them (otherwise CORS blocks the widget).
|
|
222
|
+
- The `tools[]` array in the manifest IS the tool list — the widget does not additionally call MCP `tools/list` on discovery. Keep the manifest in sync with the endpoint.
|
|
223
|
+
|
|
224
|
+
**Auto-discovery.** By default the widget fetches `<same-origin>/.well-known/mcp.json` at boot. To override, pass `well_known_urls:` to the helper: an explicit array (e.g. `["https://other.example/.well-known/mcp.json"]`) or `[]` to disable entirely.
|
|
225
|
+
|
|
226
|
+
**What the LLM sees.** Every tool listed in every discovered manifest is exposed as a callable. Names must be unique across manifests (if a name collides with a Class 1 or Class 3 tool, Class 3 wins, then Class 2, then Class 1).
|
|
227
|
+
|
|
228
|
+
**When the tool_call fires.** Synchronously during the turn — widget POSTs `{ jsonrpc, method: "tools/call", params: { name, arguments } }` to the tool's URL, awaits the JSON-RPC response, and feeds the result back to the LLM as tool output for the next round. Standard MCP loop.
|
|
229
|
+
|
|
230
|
+
**Error path.** HTTP 4xx/5xx, network failure, or an MCP JSON-RPC error → widget marks that dispatch as errored; visitor sees a red chip in the message footer; the LLM's next round sees the error as the tool's return value (so it can explain what went wrong instead of pretending it worked).
|
|
231
|
+
|
|
232
|
+
### Class 1 — Hub-registered MCP (via the meta-server)
|
|
233
|
+
|
|
234
|
+
Declared out-of-band on the meta-server (via the hub's admin UI or `/user/:id/mcp_servers` API). The widget doesn't own these — they're a shared pool visible across all consumers of the hub. Suited for third-party MCP servers (PubDictionaries, TogoMCP, Brave Search, …) that a single visitor might want to pick and choose from.
|
|
235
|
+
|
|
236
|
+
**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`.
|
|
237
|
+
|
|
238
|
+
**What the LLM sees.** Only tools from servers the visitor has **enabled via the tool picker** (see "Level-1 pickers" below). Nothing is auto-selected — the visitor opts in per session.
|
|
239
|
+
|
|
240
|
+
**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.
|
|
241
|
+
|
|
242
|
+
**Error path.** Hub-side errors (rate limit, timeout, MCP server unavailable, upstream failure) arrive as SSE `event: error` frames with typed codes (`mcp_unavailable`, `timeout`, `rate_limit`, …); the widget surfaces them in the message bubble with a per-code prefix.
|
|
243
|
+
|
|
244
|
+
## Level-1 pickers
|
|
245
|
+
|
|
246
|
+
By default the widget renders two pickers below the input textarea:
|
|
247
|
+
|
|
248
|
+
- **Model dropdown** — populated from the hub's `GET /api/llms` (anon path returns Ollama-only, since the widget's LLM calls use `api_key_uuid: "ollama-local"`).
|
|
249
|
+
- **Tool picker** — populated from the hub's `GET /api/mcp_servers` (anon path returns `public_to_anonymous: true` servers). Two-level UX: server bulk-toggle + individual tool checkboxes on expand.
|
|
250
|
+
|
|
251
|
+
Both pickers require **CORS**: the meta-server must include the host's origin in its `config/initializers/cors.rb` allowlist (`/api/*` resource). Without CORS, the fetch is silently blocked and pickers stay empty.
|
|
252
|
+
|
|
253
|
+
To adjust picker behavior at the helper call site:
|
|
254
|
+
|
|
255
|
+
```erb
|
|
256
|
+
<%= llm_meta_widget(
|
|
257
|
+
llm_url: "https://your-meta-server.example",
|
|
258
|
+
tool_hub_url: "https://your-meta-server.example",
|
|
259
|
+
model: "qwen3-6-35b-fast", # initial selection
|
|
260
|
+
enable_model_picker: true, # false → hide picker, use fixed `model:`
|
|
261
|
+
enable_tool_picker: true, # false → hide picker, no Class-1 tools
|
|
262
|
+
models: nil, # nil = all anon models; ["qwen3-6-35b-fast", …] = allowlist
|
|
263
|
+
hub_tools: nil # nil = all anon-public MCPs; ["togomcp", …] = allowlist by server name
|
|
264
|
+
) %>
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
To lock the widget to the fixed `model:` prop and disable Class-1 tools entirely (level-0 mode — Class 2 & 3 still work):
|
|
268
|
+
|
|
269
|
+
```erb
|
|
270
|
+
<%= llm_meta_widget(llm_url: "…", model: "…",
|
|
271
|
+
enable_model_picker: false,
|
|
272
|
+
enable_tool_picker: false) %>
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## All helper options
|
|
276
|
+
|
|
277
|
+
| Option | Default | Purpose |
|
|
278
|
+
|---|---|---|
|
|
279
|
+
| `llm_url:` | required | Who answers the chat — an llm_meta_server, or an Ollama |
|
|
280
|
+
| `llm_provider:` | `:llm_meta_server` | What `llm_url` speaks. `:ollama` talks to Ollama's `/api/chat` directly |
|
|
281
|
+
| `tool_hub_url:` | `nil` | An llm_meta_server whose registered MCP tools to offer. Absent = none (page actions and your own `.well-known` MCP are unaffected) |
|
|
282
|
+
| `model:` | required | Initial model (also the fallback when picker is disabled) |
|
|
283
|
+
| `api_key_uuid:` | `"ollama-local"` | Hub API-key uuid to invoke |
|
|
284
|
+
| `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 |
|
|
285
|
+
| `actions_schema_id:` | `"ai-actions"` | DOM id of the Class-3 schema block |
|
|
286
|
+
| `state_global:` | `"aiState"` | Global window object holding Class-3 state readers |
|
|
287
|
+
| `actions_global:` | `"aiActions"` | Global window object holding Class-3 implementations |
|
|
288
|
+
| `remote_tools_schema_id:` | `"remote-mcp-tools"` | Optional DOM id for pre-configured Class-1 tools (bypasses picker) |
|
|
289
|
+
| `well_known_urls:` | `nil` | `nil` = auto-discover same-origin; explicit array = fetch those; `[]` = disable |
|
|
290
|
+
| `greeting:` | `nil` | First thing a visitor sees when the panel opens, above the offered prompt templates. `nil` = a generic line |
|
|
291
|
+
| `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 |
|
|
292
|
+
| `enable_model_picker:` | `true` | Show model dropdown (Level-1) |
|
|
293
|
+
| `enable_tool_picker:` | `true` | Show tool picker (Level-1) |
|
|
294
|
+
| `models:` | `nil` | Model-name allowlist; `nil` = all anon-available |
|
|
295
|
+
| `hub_tools:` | `nil` | MCP-server-name allowlist; `nil` = all anon-public |
|
|
296
|
+
|
|
297
|
+
## Surviving navigation
|
|
298
|
+
|
|
299
|
+
A page action that navigates — submitting a form, following a link — no longer
|
|
300
|
+
loses the conversation. The transcript, the record of which tools ran, and the
|
|
301
|
+
model's own context are kept in `sessionStorage`: per tab, gone when the tab
|
|
302
|
+
closes, never sent anywhere.
|
|
303
|
+
|
|
304
|
+
The key is the page's **path**, not its full URL, so a form submit that returns
|
|
305
|
+
to the same page with a new query string keeps the thread, while moving to a
|
|
306
|
+
different page starts a fresh one. Nothing to configure.
|
|
307
|
+
|
|
308
|
+
Two consequences worth knowing. An action of yours that navigates is now safe
|
|
309
|
+
to declare — before this, offering one meant offering to wipe the visitor's
|
|
310
|
+
chat. And if the navigation lands on an error page that does not render the
|
|
311
|
+
widget, the conversation is still in storage but there is no panel to show it
|
|
312
|
+
until the visitor returns to a page that has one.
|
|
313
|
+
|
|
314
|
+
## Choosing what answers the chat, and whose tools to offer
|
|
315
|
+
|
|
316
|
+
Two independent settings, because they are two jobs. Neither implies the
|
|
317
|
+
other, so there is nothing to disable and nothing to inherit — what a widget
|
|
318
|
+
does is what its call site says.
|
|
319
|
+
|
|
320
|
+
```erb
|
|
321
|
+
<%# a hub answers the chat; its registered tools are offered too %>
|
|
322
|
+
<% hub = "https://your-meta-server.example" %>
|
|
323
|
+
<%= llm_meta_widget(llm_url: hub, tool_hub_url: hub, model: "qwen3-6-35b-fast") %>
|
|
324
|
+
|
|
325
|
+
<%# a hub answers the chat; no hub-registered tools %>
|
|
326
|
+
<%= llm_meta_widget(llm_url: hub, model: "qwen3-6-35b-fast") %>
|
|
327
|
+
|
|
328
|
+
<%# the visitor's own Ollama answers; a hub still supplies its tools %>
|
|
329
|
+
<%= llm_meta_widget(llm_url: "http://localhost:11434", llm_provider: :ollama,
|
|
330
|
+
tool_hub_url: hub, model: "qwen3.8:27b") %>
|
|
331
|
+
|
|
332
|
+
<%# nothing of ours in the path at all %>
|
|
333
|
+
<%= llm_meta_widget(llm_url: "http://localhost:11434", llm_provider: :ollama,
|
|
334
|
+
model: "qwen3.8:27b") %>
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
**The Ollama path.** The widget POSTs to `{llm_url}/api/chat` and reads
|
|
338
|
+
Ollama's NDJSON stream; tool schemas go over as OpenAI-shaped functions and
|
|
339
|
+
`tool_calls` come back, so Class 2 and Class 3 tools work exactly as they do
|
|
340
|
+
against a hub. The model picker lists `{llm_url}/api/tags`. Ollama must be
|
|
341
|
+
told to accept your page's origin — `OLLAMA_ORIGINS=https://your-site.example`
|
|
342
|
+
— which is the same CORS story as the hub, configured elsewhere.
|
|
343
|
+
|
|
344
|
+
What you give up without a `tool_hub_url` is Class 1 only: tools registered on
|
|
345
|
+
somebody's hub. That is not "no tools" — page actions and your own
|
|
346
|
+
`.well-known/mcp.json` are untouched, and they are the interesting ones for an
|
|
347
|
+
assistant embedded in your page.
|
|
348
|
+
|
|
349
|
+
**Why anyone would want the last shape:** with a local Ollama and local MCP
|
|
350
|
+
endpoints, nothing a visitor types leaves the machine. No credentials to hold,
|
|
351
|
+
no retention policy to write.
|
|
352
|
+
|
|
353
|
+
## Declaring resources and prompts (static-primitives extension)
|
|
354
|
+
|
|
355
|
+
If you operate the MCP endpoint behind your `.well-known/mcp.json`, you can
|
|
356
|
+
also offer **prompts** (one-click starting points, shown as buttons above the
|
|
357
|
+
input) and **resources** (reference data the widget can put in the model's
|
|
358
|
+
context). The widget reads two optional fields the
|
|
359
|
+
`io.modelcontextprotocol/static-primitives` extension defines on a
|
|
360
|
+
`resources/list` entry:
|
|
361
|
+
|
|
362
|
+
```json
|
|
363
|
+
{
|
|
364
|
+
"uri": "yourhost://catalog",
|
|
365
|
+
"name": "Catalog",
|
|
366
|
+
"mimeType": "application/json",
|
|
367
|
+
"_meta": {
|
|
368
|
+
"io.modelcontextprotocol/static-primitives": {
|
|
369
|
+
"sizeBytes": 1298,
|
|
370
|
+
"volatility": "stable",
|
|
371
|
+
"autoAttach": true
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
- **`sizeBytes`** — the byte length of the payload `resources/read` returns.
|
|
378
|
+
The widget decides whether it can afford the resource *before* fetching it;
|
|
379
|
+
over its budget (8 KB), the bytes never cross the wire and the LLM is left
|
|
380
|
+
to ask for what it needs through your tools instead.
|
|
381
|
+
- **`volatility`** — `"stable"` (read once, re-used) or `"volatile"` (re-read
|
|
382
|
+
on every turn that uses it).
|
|
383
|
+
- **`autoAttach`** — may the widget put this in the model's context without
|
|
384
|
+
the visitor asking?
|
|
385
|
+
|
|
386
|
+
All three are optional. A server that declares none behaves exactly as one
|
|
387
|
+
that predates the extension: the resource is fetched, trimmed to budget and
|
|
388
|
+
carried in the model's context.
|
|
389
|
+
|
|
390
|
+
A prompt is offered as a button labelled by its `title`. Declare its arguments
|
|
391
|
+
as **optional** and fill them from page state where you can: an argument the
|
|
392
|
+
visitor must supply by hand makes the button useless to the visitor who needed
|
|
393
|
+
it most, since they must already know your form to press it.
|
|
394
|
+
|
|
395
|
+
## Further reading
|
|
396
|
+
|
|
397
|
+
- **The static-primitives extension** — `io.modelcontextprotocol/static-primitives`,
|
|
398
|
+
a follow-on to [SEP-2127](https://github.com/modelcontextprotocol/modelcontextprotocol)
|
|
399
|
+
(Server Cards). The fields above are a reference implementation of its
|
|
400
|
+
current draft shape.
|
|
401
|
+
- **MCP itself** — <https://modelcontextprotocol.io>
|
|
402
|
+
- **A worked adopter** — PubDictionaries' annotation page
|
|
403
|
+
(<https://pubdictionaries.org/text_annotation>) runs this widget with all
|
|
404
|
+
three tool classes plus prompts and resources. Its
|
|
405
|
+
`app/views/annotation/text_annotation.html.erb` and `app/controllers/mcp_controller.rb`
|
|
406
|
+
are the fullest example available.
|
|
407
|
+
- **Issues and questions** — <https://github.com/jdkim/llm_meta_widget/issues>
|
|
408
|
+
|
|
409
|
+
## License
|
|
410
|
+
|
|
411
|
+
Apache-2.0.
|