@flowdrop/flowdrop 2.6.0 → 2.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.
Files changed (72) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/dist/api/enhanced-client.d.ts +29 -1
  3. package/dist/api/enhanced-client.js +83 -9
  4. package/dist/chat/commandClassifier.d.ts +2 -1
  5. package/dist/chat/commandClassifier.js +4 -1
  6. package/dist/chat/index.d.ts +5 -1
  7. package/dist/chat/index.js +2 -0
  8. package/dist/chat/toolCatalogue.d.ts +27 -0
  9. package/dist/chat/toolCatalogue.js +35 -0
  10. package/dist/chat/turnDriver.d.ts +143 -0
  11. package/dist/chat/turnDriver.js +124 -0
  12. package/dist/commands/executor.js +270 -37
  13. package/dist/commands/index.d.ts +1 -1
  14. package/dist/commands/parser.js +20 -0
  15. package/dist/commands/types.d.ts +77 -12
  16. package/dist/components/App.svelte +47 -56
  17. package/dist/components/SettingsPanel.svelte +13 -0
  18. package/dist/components/WorkflowEditor.svelte +4 -2
  19. package/dist/components/WorkflowEditor.svelte.d.ts +2 -0
  20. package/dist/components/chat/AIChatPanel.svelte +410 -22
  21. package/dist/components/console/CommandConsole.svelte +8 -0
  22. package/dist/components/console/formatters.d.ts +9 -1
  23. package/dist/components/console/formatters.js +54 -0
  24. package/dist/components/toast/DismissibleToast.svelte +53 -0
  25. package/dist/components/toast/DismissibleToast.svelte.d.ts +24 -0
  26. package/dist/components/toast/WarningIcon.svelte +28 -0
  27. package/dist/components/toast/WarningIcon.svelte.d.ts +26 -0
  28. package/dist/config/constants.d.ts +7 -3
  29. package/dist/config/constants.js +7 -3
  30. package/dist/config/endpoints.d.ts +7 -0
  31. package/dist/config/endpoints.js +2 -1
  32. package/dist/core/index.d.ts +1 -1
  33. package/dist/editor/index.d.ts +1 -1
  34. package/dist/editor/index.js +1 -1
  35. package/dist/messages/defaults.d.ts +74 -0
  36. package/dist/messages/defaults.js +35 -1
  37. package/dist/services/chatService.d.ts +21 -3
  38. package/dist/services/chatService.js +33 -1
  39. package/dist/services/globalSave.d.ts +7 -1
  40. package/dist/services/globalSave.js +20 -4
  41. package/dist/services/toastService.d.ts +23 -2
  42. package/dist/services/toastService.js +72 -28
  43. package/dist/stores/instanceContainer.svelte.d.ts +34 -0
  44. package/dist/stores/instanceContainer.svelte.js +30 -0
  45. package/dist/stores/workflowStore.svelte.d.ts +10 -0
  46. package/dist/stores/workflowStore.svelte.js +15 -0
  47. package/dist/styles/toast.css +62 -0
  48. package/dist/styles/tokens.css +6 -0
  49. package/dist/svelte-app.d.ts +16 -4
  50. package/dist/svelte-app.js +45 -14
  51. package/dist/types/chat.d.ts +59 -0
  52. package/dist/types/events.d.ts +23 -0
  53. package/dist/types/events.js +3 -1
  54. package/dist/types/index.d.ts +49 -0
  55. package/dist/types/settings.d.ts +18 -1
  56. package/dist/types/settings.js +1 -0
  57. package/dist/webmcp/WebMCPConfirm.svelte +168 -61
  58. package/dist/webmcp/WebMCPConfirm.svelte.d.ts +14 -4
  59. package/dist/webmcp/bridge.d.ts +63 -0
  60. package/dist/webmcp/bridge.js +106 -0
  61. package/dist/webmcp/descriptors.d.ts +8 -1
  62. package/dist/webmcp/descriptors.js +108 -17
  63. package/dist/webmcp/gate.d.ts +44 -2
  64. package/dist/webmcp/gate.js +61 -10
  65. package/dist/webmcp/index.d.ts +7 -2
  66. package/dist/webmcp/index.js +4 -1
  67. package/dist/webmcp/register.d.ts +11 -10
  68. package/dist/webmcp/register.js +72 -160
  69. package/dist/webmcp/runtime.d.ts +33 -0
  70. package/dist/webmcp/runtime.js +500 -0
  71. package/dist/webmcp/types.d.ts +196 -2
  72. package/package.json +1 -1
@@ -761,6 +761,43 @@ export interface NodeMetadata {
761
761
  * Allows storing additional configuration and UI state data at the node type level
762
762
  */
763
763
  extensions?: NodeExtensions;
764
+ /**
765
+ * The host's confirmation policy for instances of this type, when the host
766
+ * publishes one: whether a run pauses for a person before this node acts.
767
+ * Surfaced to browser agents by `describe_type`; the library never reads it.
768
+ */
769
+ confirmation?: NodeConfirmation;
770
+ /**
771
+ * What the current user may do with this type, computed by the host and
772
+ * passed through untouched. `add: false` means `add_node` will be refused
773
+ * server-side; the tools pre-empt with `FORBIDDEN`.
774
+ */
775
+ can?: Record<string, boolean>;
776
+ /**
777
+ * Guidance the host writes for agents reading this type — usage notes,
778
+ * example configurations, types it is usually wired to. Surfaced verbatim by
779
+ * `describe_type`; the library never interprets it.
780
+ */
781
+ agent?: NodeAgentGuidance;
782
+ }
783
+ /**
784
+ * A host's confirmation policy for a node type (see `NodeMetadata.confirmation`).
785
+ */
786
+ export interface NodeConfirmation {
787
+ /** `ask` — a run pauses for approval before the node acts; `skip` — it does not. */
788
+ policy: 'ask' | 'skip';
789
+ /** Where the policy came from: set on the type, or derived from the plugin. */
790
+ source?: 'policy' | 'plugin' | string;
791
+ /** Controls the instance author may exercise (e.g. `waive`, `require`). */
792
+ authorControls?: string[];
793
+ /** Controls a runtime input may exercise. */
794
+ dynamicControls?: string[];
795
+ }
796
+ /** Host-written guidance for agents (see `NodeMetadata.agent`). */
797
+ export interface NodeAgentGuidance {
798
+ usage?: string;
799
+ examples?: string[];
800
+ pairsWith?: string[];
764
801
  }
765
802
  /**
766
803
  * Common base interface for all schema properties
@@ -1373,6 +1410,18 @@ export interface Workflow {
1373
1410
  config?: Record<string, unknown>;
1374
1411
  /** Public contract for callers. Absent = the workflow declares no interface. */
1375
1412
  interface?: WorkflowInterface;
1413
+ /**
1414
+ * What the current user may do with this workflow, computed by the host and
1415
+ * passed through untouched (`save`, `run`, …). The browser-agent tools
1416
+ * pre-empt with `FORBIDDEN` when a key is `false`; the server stays the
1417
+ * authority either way.
1418
+ */
1419
+ can?: Record<string, boolean>;
1420
+ /**
1421
+ * The revision the editor loaded, as the host stamps it. Sent back on save
1422
+ * so the server can refuse a stale write with `CONFLICT`.
1423
+ */
1424
+ revision?: string | number;
1376
1425
  }
1377
1426
  /**
1378
1427
  * API response types
@@ -105,7 +105,24 @@ export interface BehaviorSettings {
105
105
  undoHistoryLimit: number;
106
106
  /** Show confirmation dialog before deleting nodes */
107
107
  confirmDelete: boolean;
108
- /** Automatically re-submit batch failures to the AI for self-correction */
108
+ /**
109
+ * How the AI assistant works with the editor.
110
+ *
111
+ * - `'tools'` (default): the assistant calls the editor's tools — the same
112
+ * `describe_type`, `batch`, `save`, `run`, … a WebMCP browser agent gets —
113
+ * in a loop inside one message; reads run at once, document changes ask
114
+ * once per round in the approval dialog. Needs a backend with the
115
+ * `chat.toolResults` endpoint; without one the panel falls back to `'dsl'`
116
+ * for the session and says so.
117
+ * - `'dsl'`: the legacy text mode — one reply holding a ```flowdrop block,
118
+ * previewed and applied on click. Deprecated; kept for one minor.
119
+ */
120
+ chatMode: 'tools' | 'dsl';
121
+ /**
122
+ * Automatically re-submit batch failures to the AI for self-correction.
123
+ * Legacy `'dsl'` mode only: in `'tools'` mode the tool result itself tells
124
+ * the assistant what went wrong, in the same turn.
125
+ */
109
126
  chatAutoRetry: boolean;
110
127
  /**
111
128
  * Allow the AI assistant's commands to reposition nodes
@@ -75,6 +75,7 @@ export const DEFAULT_BEHAVIOR_SETTINGS = {
75
75
  storeDraftsInBrowser: true,
76
76
  undoHistoryLimit: 50,
77
77
  confirmDelete: false,
78
+ chatMode: 'tools',
78
79
  chatAutoRetry: true,
79
80
  chatAllowLayoutChanges: true
80
81
  };
@@ -13,29 +13,46 @@
13
13
  import { focusOnMount } from '../utils/focus.js';
14
14
 
15
15
  interface Props {
16
- /** Editor / workflow name, so two editors on one page are distinguishable. */
17
- editorName: string;
16
+ /** The title — who wants to change which editor (see `GateRequest.title`). */
17
+ title: string;
18
18
  /** One human-readable line per command (see `describeCommand`). */
19
19
  lines: string[];
20
- /** Called exactly once with the decision. */
21
- onResolve: (approved: boolean) => void;
20
+ /**
21
+ * Overrides the default "N changes — applied together, undone together"
22
+ * hint, e.g. for `save`, which has no commands to count.
23
+ */
24
+ hint?: string;
25
+ /**
26
+ * Show the "apply further edits without asking" checkbox. Off for
27
+ * consequential calls (`save`, `run`), which always ask.
28
+ */
29
+ offerRemember?: boolean;
30
+ /** Called exactly once with the decision and whether to remember it for edits. */
31
+ onResolve: (approved: boolean, remember?: boolean) => void;
22
32
  }
23
33
 
24
- let { editorName, lines, onResolve }: Props = $props();
34
+ let { title, lines, hint, offerRemember = false, onResolve }: Props = $props();
25
35
 
26
36
  let rejectButton = $state<HTMLButtonElement | null>(null);
27
37
  let approveButton = $state<HTMLButtonElement | null>(null);
38
+ let rememberBox = $state<HTMLInputElement | null>(null);
39
+ let remember = $state(false);
28
40
 
29
- // Focus starts on Apply and stays inside the dialog: Tab and Shift+Tab move
30
- // between the two buttons, Escape rejects. The handler sits on the dialog
31
- // itself, which holds focus through its buttons, not on the window.
41
+ // Focus starts on Apply and stays inside the dialog: Tab cycles Apply →
42
+ // Reject → (checkbox) → Apply, Escape rejects. The handler sits on the
43
+ // dialog itself, which holds focus through its controls, not on the window.
32
44
  function handleKeydown(event: KeyboardEvent): void {
33
45
  if (event.key === 'Escape') {
34
46
  event.preventDefault();
35
47
  onResolve(false);
36
48
  } else if (event.key === 'Tab') {
37
49
  event.preventDefault();
38
- (document.activeElement === approveButton ? rejectButton : approveButton)?.focus();
50
+ const order = [approveButton, rejectButton, rememberBox].filter(
51
+ (el): el is HTMLButtonElement | HTMLInputElement => el !== null
52
+ );
53
+ const i = order.indexOf(document.activeElement as HTMLButtonElement | HTMLInputElement);
54
+ const step = event.shiftKey ? -1 : 1;
55
+ order[(i + step + order.length) % order.length]?.focus();
39
56
  }
40
57
  }
41
58
  </script>
@@ -50,21 +67,36 @@
50
67
  tabindex="-1"
51
68
  onkeydown={handleKeydown}
52
69
  >
53
- <h2 id="fd-webmcp-confirm-title" class="fd-webmcp-confirm__title">
54
- {m().webmcp.confirmTitle({ name: editorName })}
55
- </h2>
56
- <p class="fd-webmcp-confirm__hint">
57
- {m().webmcp.confirmCount({ count: lines.length })}
58
- </p>
59
- <ol id="fd-webmcp-confirm-list" class="fd-webmcp-confirm__list">
60
- {#each lines as line, i (i)}
61
- <li class="fd-webmcp-confirm__line">{line}</li>
62
- {/each}
63
- </ol>
64
- <div class="fd-webmcp-confirm__actions">
70
+ <header class="fd-webmcp-confirm__header">
71
+ <h2 id="fd-webmcp-confirm-title" class="fd-webmcp-confirm__title">
72
+ {title}
73
+ </h2>
74
+ <p class="fd-webmcp-confirm__hint">
75
+ {hint ?? m().webmcp.confirmCount({ count: lines.length })}
76
+ </p>
77
+ </header>
78
+ <div class="fd-webmcp-confirm__body">
79
+ <ol id="fd-webmcp-confirm-list" class="fd-webmcp-confirm__list">
80
+ {#each lines as line, i (i)}
81
+ <li class="fd-webmcp-confirm__line">{line}</li>
82
+ {/each}
83
+ </ol>
84
+ {#if offerRemember}
85
+ <label class="fd-webmcp-confirm__remember">
86
+ <input
87
+ type="checkbox"
88
+ data-testid="flowdrop-webmcp-remember"
89
+ bind:this={rememberBox}
90
+ bind:checked={remember}
91
+ />
92
+ <span>{m().webmcp.rememberEdits}</span>
93
+ </label>
94
+ {/if}
95
+ </div>
96
+ <footer class="fd-webmcp-confirm__actions">
65
97
  <button
66
98
  type="button"
67
- class="fd-webmcp-confirm__button fd-webmcp-confirm__button--reject"
99
+ class="flowdrop-btn flowdrop-btn--outline fd-webmcp-confirm__button"
68
100
  data-testid="flowdrop-webmcp-reject"
69
101
  bind:this={rejectButton}
70
102
  onclick={() => onResolve(false)}
@@ -73,19 +105,25 @@
73
105
  </button>
74
106
  <button
75
107
  type="button"
76
- class="fd-webmcp-confirm__button fd-webmcp-confirm__button--approve"
108
+ class="flowdrop-btn flowdrop-btn--primary fd-webmcp-confirm__button"
77
109
  data-testid="flowdrop-webmcp-approve"
78
110
  bind:this={approveButton}
79
111
  {@attach focusOnMount()}
80
- onclick={() => onResolve(true)}
112
+ onclick={() => onResolve(true, remember)}
81
113
  >
82
114
  {m().webmcp.apply}
83
115
  </button>
84
- </div>
116
+ </footer>
85
117
  </div>
86
118
  </div>
87
119
 
88
120
  <style>
121
+ /* Mounted on document.body, outside the editor tree: the tokens come from
122
+ :root (tokens.css), the button classes from base.css — both global — and
123
+ the dark skin from [data-theme='dark'] on <html>. Only the font has no
124
+ host to inherit from, so it is set here. The shell follows
125
+ SettingsModal: blurred backdrop, header / body / footer with the xl
126
+ rhythm, and an enter animation. */
89
127
  .fd-webmcp-confirm {
90
128
  position: fixed;
91
129
  inset: 0;
@@ -93,78 +131,147 @@
93
131
  display: flex;
94
132
  align-items: center;
95
133
  justify-content: center;
96
- background: rgb(0 0 0 / 0.4);
97
- font-family: inherit;
134
+ padding: var(--fd-space-xl, 1rem);
135
+ background: rgb(0 0 0 / 0.5);
136
+ backdrop-filter: blur(4px);
137
+ font-family: var(--fd-font-sans, system-ui, sans-serif);
138
+ font-size: var(--fd-text-sm, 0.875rem);
139
+ line-height: var(--fd-leading-normal, 1.5);
98
140
  }
99
141
 
100
142
  .fd-webmcp-confirm__dialog {
101
- min-width: 20rem;
102
- max-width: min(36rem, calc(100vw - 2rem));
103
- max-height: calc(100vh - 2rem);
104
- overflow: auto;
105
- padding: var(--fd-space-lg, 1rem);
143
+ display: flex;
144
+ flex-direction: column;
145
+ width: 100%;
146
+ max-width: 36rem;
147
+ max-height: calc(100vh - 2 * var(--fd-space-xl, 1rem));
148
+ overflow: hidden;
106
149
  border: 1px solid var(--fd-border, #d4d4d8);
107
- border-radius: var(--fd-radius-lg, 0.75rem);
150
+ border-radius: var(--fd-radius-lg, 0.5rem);
108
151
  background: var(--fd-background, #fff);
109
152
  color: var(--fd-foreground, #18181b);
110
153
  box-shadow: var(--fd-shadow-xl, 0 20px 40px rgb(0 0 0 / 0.25));
154
+ animation: fd-webmcp-confirm-enter 0.2s ease-out;
155
+ }
156
+
157
+ .fd-webmcp-confirm__dialog:focus {
158
+ outline: none;
159
+ }
160
+
161
+ @keyframes fd-webmcp-confirm-enter {
162
+ from {
163
+ opacity: 0;
164
+ transform: scale(0.95) translateY(-10px);
165
+ }
166
+ to {
167
+ opacity: 1;
168
+ transform: scale(1) translateY(0);
169
+ }
170
+ }
171
+
172
+ .fd-webmcp-confirm__header {
173
+ flex-shrink: 0;
174
+ padding: var(--fd-space-xl, 1rem) var(--fd-space-xl, 1rem) var(--fd-space-lg, 0.875rem);
175
+ border-bottom: 1px solid var(--fd-border, #d4d4d8);
111
176
  }
112
177
 
113
178
  .fd-webmcp-confirm__title {
114
- margin: 0 0 var(--fd-space-xs, 0.25rem);
115
- font-size: var(--fd-text-md, 1rem);
179
+ margin: 0 0 var(--fd-space-2xs, 0.25rem);
180
+ font-size: var(--fd-text-lg, 1.125rem);
116
181
  font-weight: 600;
182
+ line-height: var(--fd-leading-tight, 1.3);
183
+ color: var(--fd-foreground, #18181b);
184
+ overflow-wrap: anywhere;
117
185
  }
118
186
 
119
187
  .fd-webmcp-confirm__hint {
120
- margin: 0 0 var(--fd-space-md, 0.75rem);
188
+ margin: 0;
121
189
  font-size: var(--fd-text-sm, 0.875rem);
122
190
  color: var(--fd-muted-foreground, #71717a);
123
191
  }
124
192
 
193
+ .fd-webmcp-confirm__body {
194
+ flex: 1;
195
+ min-height: 0;
196
+ overflow: auto;
197
+ padding: var(--fd-space-xl, 1rem);
198
+ }
199
+
125
200
  .fd-webmcp-confirm__list {
126
- margin: 0 0 var(--fd-space-lg, 1rem);
127
- padding: var(--fd-space-sm, 0.5rem) var(--fd-space-sm, 0.5rem) var(--fd-space-sm, 0.5rem)
128
- var(--fd-space-xl, 1.5rem);
129
- border-radius: var(--fd-radius-md, 0.5rem);
201
+ margin: 0;
202
+ padding: var(--fd-space-md, 0.75rem) var(--fd-space-lg, 0.875rem) var(--fd-space-md, 0.75rem)
203
+ var(--fd-space-3xl, 2.25rem);
204
+ border: 1px solid var(--fd-border, #d4d4d8);
205
+ border-radius: var(--fd-radius-md, 0.375rem);
130
206
  background: var(--fd-muted, #f4f4f5);
131
207
  font-family: var(--fd-font-mono, ui-monospace, monospace);
132
- font-size: var(--fd-text-sm, 0.875rem);
208
+ font-size: var(--fd-text-xs, 0.8125rem);
209
+ line-height: var(--fd-leading-relaxed, 1.6);
210
+ color: var(--fd-foreground, #18181b);
133
211
  }
134
212
 
135
213
  .fd-webmcp-confirm__line {
136
- padding: 0.125rem 0;
214
+ padding: var(--fd-space-3xs, 0.125rem) 0;
137
215
  overflow-wrap: anywhere;
138
216
  }
139
217
 
140
- .fd-webmcp-confirm__actions {
218
+ .fd-webmcp-confirm__line::marker {
219
+ color: var(--fd-muted-foreground, #71717a);
220
+ }
221
+
222
+ .fd-webmcp-confirm__remember {
141
223
  display: flex;
142
- justify-content: flex-end;
143
- gap: var(--fd-space-sm, 0.5rem);
224
+ align-items: flex-start;
225
+ gap: var(--fd-space-sm, 0.625rem);
226
+ margin: var(--fd-space-lg, 0.875rem) 0 0;
227
+ font-size: var(--fd-text-sm, 0.875rem);
228
+ color: var(--fd-muted-foreground, #71717a);
229
+ cursor: pointer;
144
230
  }
145
231
 
146
- .fd-webmcp-confirm__button {
147
- padding: var(--fd-space-xs, 0.375rem) var(--fd-space-md, 0.75rem);
148
- border: 1px solid var(--fd-border, #d4d4d8);
149
- border-radius: var(--fd-control-radius, 0.375rem);
150
- background: var(--fd-background, #fff);
151
- color: inherit;
152
- font: inherit;
232
+ .fd-webmcp-confirm__remember input {
233
+ flex-shrink: 0;
234
+ width: 1rem;
235
+ height: 1rem;
236
+ margin: 0.2em 0 0;
237
+ accent-color: var(--fd-primary, #2563eb);
153
238
  cursor: pointer;
154
239
  }
155
240
 
156
- .fd-webmcp-confirm__button--approve {
157
- border-color: var(--fd-primary, #2563eb);
158
- background: var(--fd-primary, #2563eb);
159
- color: var(--fd-primary-foreground, #fff);
241
+ .fd-webmcp-confirm__remember:hover {
242
+ color: var(--fd-foreground, #18181b);
160
243
  }
161
244
 
162
- .fd-webmcp-confirm__button--approve:hover {
163
- background: var(--fd-primary-hover, #1d4ed8);
245
+ .fd-webmcp-confirm__actions {
246
+ flex-shrink: 0;
247
+ display: flex;
248
+ justify-content: flex-end;
249
+ gap: var(--fd-space-sm, 0.625rem);
250
+ padding: var(--fd-space-lg, 0.875rem) var(--fd-space-xl, 1rem);
251
+ border-top: 1px solid var(--fd-border, #d4d4d8);
252
+ background: var(--fd-muted, #f4f4f5);
253
+ }
254
+
255
+ /* The buttons are .flowdrop-btn from base.css; this only keeps them from
256
+ inheriting the host page's button font when base.css is absent. */
257
+ .fd-webmcp-confirm__button {
258
+ font-family: inherit;
164
259
  }
165
260
 
166
- .fd-webmcp-confirm__button:focus-visible {
167
- outline: 2px solid var(--fd-primary, #2563eb);
168
- outline-offset: 2px;
261
+ @media (max-width: 640px) {
262
+ .fd-webmcp-confirm {
263
+ padding: 0;
264
+ align-items: flex-end;
265
+ }
266
+
267
+ .fd-webmcp-confirm__dialog {
268
+ max-width: 100%;
269
+ max-height: 100vh;
270
+ border-radius: var(--fd-radius-lg, 0.5rem) var(--fd-radius-lg, 0.5rem) 0 0;
271
+ }
272
+
273
+ .fd-webmcp-confirm__actions {
274
+ flex-direction: column-reverse;
275
+ }
169
276
  }
170
277
  </style>
@@ -1,10 +1,20 @@
1
1
  interface Props {
2
- /** Editor / workflow name, so two editors on one page are distinguishable. */
3
- editorName: string;
2
+ /** The title — who wants to change which editor (see `GateRequest.title`). */
3
+ title: string;
4
4
  /** One human-readable line per command (see `describeCommand`). */
5
5
  lines: string[];
6
- /** Called exactly once with the decision. */
7
- onResolve: (approved: boolean) => void;
6
+ /**
7
+ * Overrides the default "N changes — applied together, undone together"
8
+ * hint, e.g. for `save`, which has no commands to count.
9
+ */
10
+ hint?: string;
11
+ /**
12
+ * Show the "apply further edits without asking" checkbox. Off for
13
+ * consequential calls (`save`, `run`), which always ask.
14
+ */
15
+ offerRemember?: boolean;
16
+ /** Called exactly once with the decision and whether to remember it for edits. */
17
+ onResolve: (approved: boolean, remember?: boolean) => void;
8
18
  }
9
19
  declare const WebMCPConfirm: import("svelte").Component<Props, {}, "">;
10
20
  type WebMCPConfirm = ReturnType<typeof WebMCPConfirm>;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * WebMCP adapter — a bridge to a desktop MCP client for browsers without the
3
+ * origin trial.
4
+ *
5
+ * Chrome's `document.modelContext` is the runtime the adapter was written for,
6
+ * but it sits behind a flag and only Chrome's own agent consumes it. This
7
+ * module presents the same {@link ModelContextLike} surface over a *widget*
8
+ * that relays tools to a local MCP server — the shape of
9
+ * `@jason.today/webmcp`'s page-side `WebMCP` class:
10
+ * `registerTool(name, description, inputSchema, execute)`, with tool calls
11
+ * arriving over its websocket and the returned value passed to the MCP client
12
+ * as the tool result. The adapter's tools already return MCP-shaped
13
+ * `{content: [{type: 'text', text}]}` results, so nothing is translated.
14
+ *
15
+ * Install it before the editor mounts:
16
+ *
17
+ * ```ts
18
+ * import { installBridgedModelContext } from '@flowdrop/flowdrop/webmcp';
19
+ * const widget = new WebMCP(); // the bridge's page-side script
20
+ * installBridgedModelContext(widget); // no-op when a real runtime exists
21
+ * ```
22
+ *
23
+ * The widget cannot unregister a tool. When the adapter aborts a
24
+ * registration (editor destroyed), the relayed function keeps answering — with
25
+ * a structured `UNAVAILABLE` error — until the page unloads. A re-registration
26
+ * under the same name takes over again.
27
+ *
28
+ * The relay's MCP server answers a tool call with a timeout after 30 seconds.
29
+ * A confirm dialog left open longer than that returns a timeout to the agent
30
+ * while the dialog stays up; approving it then still applies the change. Hosts
31
+ * that expect slow approvals should say so in the dialog copy or use a custom
32
+ * `approval` callback.
33
+ *
34
+ * Nothing here decides *who* may reach the page: the bridge is localhost-only
35
+ * and token-gated by design, and the adapter's confirm dialog and the
36
+ * server's permissions still gate every mutation. Hosts should still make the
37
+ * bridge an explicit opt-in rather than a default.
38
+ *
39
+ * @module webmcp/bridge
40
+ */
41
+ import type { ModelContextLike, RegisteredToolDefinition, ToolInputSchema } from './types.js';
42
+ /** The page-side widget surface the bridge relays to (`@jason.today/webmcp`). */
43
+ export interface WebMCPWidgetLike {
44
+ registerTool(name: string, description: string, inputSchema: ToolInputSchema, execute: (args: unknown) => unknown): unknown;
45
+ }
46
+ export interface BridgedModelContext extends ModelContextLike {
47
+ /** Tools currently registered through the bridge, by name. */
48
+ readonly tools: ReadonlyMap<string, RegisteredToolDefinition>;
49
+ }
50
+ /**
51
+ * Wrap a bridge widget as a `modelContext`-shaped runtime.
52
+ *
53
+ * Each `registerTool` is relayed once per name; the relayed function looks the
54
+ * tool up at call time so aborts and re-registrations are honoured without the
55
+ * widget having to know about them.
56
+ */
57
+ export declare function createBridgedModelContext(widget: WebMCPWidgetLike): BridgedModelContext;
58
+ /**
59
+ * Install a bridged runtime as `document.modelContext` unless a runtime is
60
+ * already present (Chrome's, or one installed earlier). Returns the runtime
61
+ * in effect, or null outside a browser (or when `target` is null).
62
+ */
63
+ export declare function installBridgedModelContext(widget: WebMCPWidgetLike, target?: object | null): ModelContextLike | null;
@@ -0,0 +1,106 @@
1
+ /**
2
+ * WebMCP adapter — a bridge to a desktop MCP client for browsers without the
3
+ * origin trial.
4
+ *
5
+ * Chrome's `document.modelContext` is the runtime the adapter was written for,
6
+ * but it sits behind a flag and only Chrome's own agent consumes it. This
7
+ * module presents the same {@link ModelContextLike} surface over a *widget*
8
+ * that relays tools to a local MCP server — the shape of
9
+ * `@jason.today/webmcp`'s page-side `WebMCP` class:
10
+ * `registerTool(name, description, inputSchema, execute)`, with tool calls
11
+ * arriving over its websocket and the returned value passed to the MCP client
12
+ * as the tool result. The adapter's tools already return MCP-shaped
13
+ * `{content: [{type: 'text', text}]}` results, so nothing is translated.
14
+ *
15
+ * Install it before the editor mounts:
16
+ *
17
+ * ```ts
18
+ * import { installBridgedModelContext } from '@flowdrop/flowdrop/webmcp';
19
+ * const widget = new WebMCP(); // the bridge's page-side script
20
+ * installBridgedModelContext(widget); // no-op when a real runtime exists
21
+ * ```
22
+ *
23
+ * The widget cannot unregister a tool. When the adapter aborts a
24
+ * registration (editor destroyed), the relayed function keeps answering — with
25
+ * a structured `UNAVAILABLE` error — until the page unloads. A re-registration
26
+ * under the same name takes over again.
27
+ *
28
+ * The relay's MCP server answers a tool call with a timeout after 30 seconds.
29
+ * A confirm dialog left open longer than that returns a timeout to the agent
30
+ * while the dialog stays up; approving it then still applies the change. Hosts
31
+ * that expect slow approvals should say so in the dialog copy or use a custom
32
+ * `approval` callback.
33
+ *
34
+ * Nothing here decides *who* may reach the page: the bridge is localhost-only
35
+ * and token-gated by design, and the adapter's confirm dialog and the
36
+ * server's permissions still gate every mutation. Hosts should still make the
37
+ * bridge an explicit opt-in rather than a default.
38
+ *
39
+ * @module webmcp/bridge
40
+ */
41
+ function unavailable(name) {
42
+ return {
43
+ content: [
44
+ {
45
+ type: 'text',
46
+ text: JSON.stringify({
47
+ ok: false,
48
+ code: 'UNAVAILABLE',
49
+ error: `Tool "${name}" is no longer registered; the editor that offered it was closed. Reload the page and try again.`
50
+ })
51
+ }
52
+ ],
53
+ isError: true
54
+ };
55
+ }
56
+ /**
57
+ * Wrap a bridge widget as a `modelContext`-shaped runtime.
58
+ *
59
+ * Each `registerTool` is relayed once per name; the relayed function looks the
60
+ * tool up at call time so aborts and re-registrations are honoured without the
61
+ * widget having to know about them.
62
+ */
63
+ export function createBridgedModelContext(widget) {
64
+ const tools = new Map();
65
+ const relayed = new Set();
66
+ return {
67
+ tools,
68
+ registerTool(tool, options) {
69
+ tools.set(tool.name, tool);
70
+ options?.signal?.addEventListener('abort', () => {
71
+ if (tools.get(tool.name) === tool)
72
+ tools.delete(tool.name);
73
+ }, { once: true });
74
+ if (!relayed.has(tool.name)) {
75
+ relayed.add(tool.name);
76
+ widget.registerTool(tool.name, tool.description, tool.inputSchema, (args) => {
77
+ const live = tools.get(tool.name);
78
+ if (!live)
79
+ return unavailable(tool.name);
80
+ return live.execute((args ?? {}));
81
+ });
82
+ }
83
+ return Promise.resolve();
84
+ }
85
+ };
86
+ }
87
+ /**
88
+ * Install a bridged runtime as `document.modelContext` unless a runtime is
89
+ * already present (Chrome's, or one installed earlier). Returns the runtime
90
+ * in effect, or null outside a browser (or when `target` is null).
91
+ */
92
+ export function installBridgedModelContext(widget, target = typeof document === 'undefined' ? null : document) {
93
+ if (!target)
94
+ return null;
95
+ const host = target;
96
+ if (host.modelContext)
97
+ return host.modelContext;
98
+ const nav = typeof navigator === 'undefined'
99
+ ? undefined
100
+ : navigator;
101
+ if (nav?.modelContext)
102
+ return nav.modelContext;
103
+ const bridged = createBridgedModelContext(widget);
104
+ Object.defineProperty(target, 'modelContext', { value: bridged, configurable: true });
105
+ return bridged;
106
+ }
@@ -17,12 +17,13 @@
17
17
  * @module webmcp/descriptors
18
18
  */
19
19
  import type { Command } from '../commands/types.js';
20
+ import type { Messages } from '../messages/types.js';
20
21
  import { VIEW_COMMAND_TYPES } from '../chat/commandClassifier.js';
21
22
  import { type ToolDescriptor, type ToolInputSchema } from './types.js';
22
23
  import { validateToolArgs } from './validate.js';
23
24
  export { validateToolArgs };
24
25
  /** Command types exposed as one tool each (verb === command type). */
25
- export declare const EXPOSED_COMMAND_TYPES: readonly ["add_node", "delete_node", "rename_node", "move_node", "swap_node", "set_config", "get_config", "connect", "disconnect_ports", "disconnect_node", "list_nodes", "list_edges", "list_types", "info", "undo", "redo", "auto_layout", "beautify_layout"];
26
+ export declare const EXPOSED_COMMAND_TYPES: readonly ["add_node", "delete_node", "rename_node", "move_node", "swap_node", "set_config", "get_config", "connect", "disconnect_ports", "disconnect_node", "list_nodes", "list_edges", "list_types", "describe_type", "search_types", "info", "undo", "redo", "auto_layout", "beautify_layout"];
26
27
  /**
27
28
  * Command types folded into the single `view` tool (its `action` enum). The
28
29
  * list itself lives in the command classifier, next to `isViewCommand`, so
@@ -57,3 +58,9 @@ export interface BuildDescriptorsOptions {
57
58
  export declare function buildToolDescriptors(options?: BuildDescriptorsOptions): ToolDescriptor[];
58
59
  /** One line describing a command for a human about to approve it. */
59
60
  export declare function describeCommand(command: Command): string;
61
+ /**
62
+ * One sentence saying what a list of commands does, for the approval dialog's hint and the chat transcript:
63
+ * "3 changes — adds 2 nodes, connects 1 edge. Applied together, undone together."
64
+ * Returns `undefined` for a single command, whose one line already says it all.
65
+ */
66
+ export declare function summarizeCommands(commands: Command[], m: Messages['webmcp']): string | undefined;