@flowdrop/flowdrop 2.5.0 → 2.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/CHANGELOG.md +59 -0
- package/dist/chat/commandClassifier.d.ts +19 -1
- package/dist/chat/commandClassifier.js +33 -1
- package/dist/chat/index.d.ts +1 -1
- package/dist/chat/index.js +1 -1
- package/dist/commands/executor.js +270 -37
- package/dist/commands/index.d.ts +1 -1
- package/dist/commands/parser.js +20 -0
- package/dist/commands/types.d.ts +77 -12
- package/dist/components/App.svelte +8 -0
- package/dist/components/BindablePortListbox.svelte +426 -0
- package/dist/components/BindablePortListbox.svelte.d.ts +23 -0
- package/dist/components/WorkflowInterfaceEditor.svelte +142 -36
- package/dist/components/WorkflowInterfaceEditor.svelte.d.ts +8 -0
- package/dist/components/WorkflowInterfaceEntryCard.svelte +537 -294
- package/dist/components/WorkflowInterfaceEntryCard.svelte.d.ts +6 -3
- package/dist/components/WorkflowInterfaceEntryComposer.svelte +331 -0
- package/dist/components/WorkflowInterfaceEntryComposer.svelte.d.ts +19 -0
- package/dist/components/console/CommandConsole.svelte +8 -0
- package/dist/components/console/formatters.d.ts +9 -1
- package/dist/components/console/formatters.js +54 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/messages/context.d.ts +6 -0
- package/dist/messages/context.js +8 -0
- package/dist/messages/defaults.d.ts +78 -2
- package/dist/messages/defaults.js +50 -3
- package/dist/messages/index.d.ts +1 -1
- package/dist/messages/index.js +1 -1
- package/dist/services/globalSave.d.ts +7 -1
- package/dist/services/globalSave.js +17 -3
- package/dist/stores/instanceContainer.svelte.d.ts +26 -0
- package/dist/stores/instanceContainer.svelte.js +24 -0
- package/dist/stores/workflowStore.svelte.d.ts +10 -0
- package/dist/stores/workflowStore.svelte.js +15 -0
- package/dist/stories/WebMCPFakeAgent.svelte +229 -0
- package/dist/stories/WebMCPFakeAgent.svelte.d.ts +10 -0
- package/dist/styles/base.css +4 -2
- package/dist/styles/tokens.css +6 -0
- package/dist/svelte-app.d.ts +24 -2
- package/dist/svelte-app.js +65 -3
- package/dist/types/index.d.ts +49 -0
- package/dist/utils/focus.d.ts +18 -0
- package/dist/utils/focus.js +24 -0
- package/dist/utils/whenWorkflowLoaded.svelte.d.ts +17 -0
- package/dist/utils/whenWorkflowLoaded.svelte.js +42 -0
- package/dist/utils/workflowInterface.d.ts +47 -3
- package/dist/utils/workflowInterface.js +84 -9
- package/dist/webmcp/WebMCP.stories.svelte +101 -0
- package/dist/webmcp/WebMCP.stories.svelte.d.ts +18 -0
- package/dist/webmcp/WebMCPConfirm.svelte +216 -0
- package/dist/webmcp/WebMCPConfirm.svelte.d.ts +21 -0
- package/dist/webmcp/bridge.d.ts +63 -0
- package/dist/webmcp/bridge.js +106 -0
- package/dist/webmcp/descriptors.d.ts +59 -0
- package/dist/webmcp/descriptors.js +504 -0
- package/dist/webmcp/fake.d.ts +23 -0
- package/dist/webmcp/fake.js +42 -0
- package/dist/webmcp/gate.d.ts +76 -0
- package/dist/webmcp/gate.js +188 -0
- package/dist/webmcp/index.d.ts +36 -0
- package/dist/webmcp/index.js +32 -0
- package/dist/webmcp/register.d.ts +33 -0
- package/dist/webmcp/register.js +504 -0
- package/dist/webmcp/types.d.ts +268 -0
- package/dist/webmcp/types.js +23 -0
- package/dist/webmcp/validate.d.ts +32 -0
- package/dist/webmcp/validate.js +118 -0
- package/package.json +6 -1
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — tool descriptors.
|
|
3
|
+
*
|
|
4
|
+
* Maps the command union to `{verb, description, inputSchema, readOnly,
|
|
5
|
+
* toCommands}` records. Transport-free: no DOM, no Svelte, nothing from the
|
|
6
|
+
* runtime — so the same records can back a server-side MCP endpoint later.
|
|
7
|
+
*
|
|
8
|
+
* The schemas are written by hand next to the interfaces they mirror
|
|
9
|
+
* (`commands/types.ts`). Each exposed command is one record in `COMMANDS`:
|
|
10
|
+
* its description, its input schema, how validated arguments become the
|
|
11
|
+
* command, and how the command reads back to a human in the confirm dialog.
|
|
12
|
+
* The coverage test in `tests/unit/webmcp` asserts every literal of the
|
|
13
|
+
* `Command` union is either exposed here or on the explicit exclusion list,
|
|
14
|
+
* and that every exposed record carries all four parts, so a new DSL verb
|
|
15
|
+
* cannot slip through unclassified or half-described.
|
|
16
|
+
*
|
|
17
|
+
* @module webmcp/descriptors
|
|
18
|
+
*/
|
|
19
|
+
import { isMutatingCommand, VIEW_COMMAND_TYPES } from '../chat/commandClassifier.js';
|
|
20
|
+
import { ToolArgumentError } from './types.js';
|
|
21
|
+
import { validateToolArgs } from './validate.js';
|
|
22
|
+
export { validateToolArgs };
|
|
23
|
+
// ============================================================================
|
|
24
|
+
// What is exposed
|
|
25
|
+
// ============================================================================
|
|
26
|
+
/** Command types exposed as one tool each (verb === command type). */
|
|
27
|
+
export const EXPOSED_COMMAND_TYPES = [
|
|
28
|
+
'add_node',
|
|
29
|
+
'delete_node',
|
|
30
|
+
'rename_node',
|
|
31
|
+
'move_node',
|
|
32
|
+
'swap_node',
|
|
33
|
+
'set_config',
|
|
34
|
+
'get_config',
|
|
35
|
+
'connect',
|
|
36
|
+
'disconnect_ports',
|
|
37
|
+
'disconnect_node',
|
|
38
|
+
'list_nodes',
|
|
39
|
+
'list_edges',
|
|
40
|
+
'list_types',
|
|
41
|
+
'describe_type',
|
|
42
|
+
'search_types',
|
|
43
|
+
'info',
|
|
44
|
+
'undo',
|
|
45
|
+
'redo',
|
|
46
|
+
'auto_layout',
|
|
47
|
+
'beautify_layout'
|
|
48
|
+
];
|
|
49
|
+
/**
|
|
50
|
+
* Command types folded into the single `view` tool (its `action` enum). The
|
|
51
|
+
* list itself lives in the command classifier, next to `isViewCommand`, so
|
|
52
|
+
* the chat panel and this adapter cannot disagree about what a view command is.
|
|
53
|
+
*/
|
|
54
|
+
export { VIEW_COMMAND_TYPES };
|
|
55
|
+
/**
|
|
56
|
+
* Command types deliberately not exposed.
|
|
57
|
+
* - `clear` wipes the whole workflow; an agent can delete nodes one by one and
|
|
58
|
+
* each deletion is gated.
|
|
59
|
+
* - `help` describes the text DSL; the tool schemas are the help.
|
|
60
|
+
*/
|
|
61
|
+
export const EXCLUDED_COMMAND_TYPES = [
|
|
62
|
+
'clear',
|
|
63
|
+
'help'
|
|
64
|
+
];
|
|
65
|
+
/** Extra tool verbs that are not command types. */
|
|
66
|
+
export const COMPOSITE_VERBS = ['view', 'batch'];
|
|
67
|
+
// ============================================================================
|
|
68
|
+
// Schema fragments
|
|
69
|
+
// ============================================================================
|
|
70
|
+
const NODE_ID_DESCRIPTION = 'Node id in short form, e.g. "http_request.1" — the `nodeId` values returned by list_nodes and add_node. Full ids are accepted too.';
|
|
71
|
+
/**
|
|
72
|
+
* The recommended call order, appended to the tools where it applies —
|
|
73
|
+
* add_node, set_config, swap_node and batch, the ones that take a type or a
|
|
74
|
+
* config key. For a browser agent the tool descriptions are the whole manual,
|
|
75
|
+
* so the manual says how the tools fit together; saying it once per relevant
|
|
76
|
+
* tool keeps the manual short.
|
|
77
|
+
*/
|
|
78
|
+
const CALL_ORDER = ' Call describe_type before adding or configuring a type you have not seen; group several changes in one batch; save when done.';
|
|
79
|
+
const nodeId = (description = NODE_ID_DESCRIPTION) => ({
|
|
80
|
+
type: 'string',
|
|
81
|
+
description
|
|
82
|
+
});
|
|
83
|
+
const position = {
|
|
84
|
+
type: 'object',
|
|
85
|
+
description: 'Canvas position in pixels.',
|
|
86
|
+
properties: {
|
|
87
|
+
x: { type: 'number', description: 'Horizontal position.' },
|
|
88
|
+
y: { type: 'number', description: 'Vertical position.' }
|
|
89
|
+
},
|
|
90
|
+
required: ['x', 'y'],
|
|
91
|
+
additionalProperties: false
|
|
92
|
+
};
|
|
93
|
+
const port = (which) => ({
|
|
94
|
+
type: 'string',
|
|
95
|
+
description: `${which} port id, as listed under inputs/outputs by the info tool.`
|
|
96
|
+
});
|
|
97
|
+
const configValue = {
|
|
98
|
+
description: 'The new value. Strings are stored verbatim; numbers, booleans, arrays and objects are stored typed.',
|
|
99
|
+
anyOf: [
|
|
100
|
+
{ type: 'string' },
|
|
101
|
+
{ type: 'number' },
|
|
102
|
+
{ type: 'boolean' },
|
|
103
|
+
{ type: 'object' },
|
|
104
|
+
{ type: 'array' }
|
|
105
|
+
]
|
|
106
|
+
};
|
|
107
|
+
const object = (properties, required = []) => ({
|
|
108
|
+
type: 'object',
|
|
109
|
+
properties,
|
|
110
|
+
...(required.length > 0 ? { required } : {}),
|
|
111
|
+
additionalProperties: false
|
|
112
|
+
});
|
|
113
|
+
const EMPTY = object({});
|
|
114
|
+
/**
|
|
115
|
+
* `SetConfigCommand.value` is the DSL's raw token: the executor parses it with
|
|
116
|
+
* the same rules the text DSL uses (`"…"` → string, bare `12` → number, `true`
|
|
117
|
+
* → boolean, `{…}`/`[…]` → JSON). JSON-encoding every typed argument routes
|
|
118
|
+
* each shape to the right rule, and keeps a string like `"true"` a string.
|
|
119
|
+
*/
|
|
120
|
+
function encodeConfigValue(value) {
|
|
121
|
+
return JSON.stringify(value);
|
|
122
|
+
}
|
|
123
|
+
const q = (v) => JSON.stringify(v);
|
|
124
|
+
const at = (p) => `(${p.x}, ${p.y})`;
|
|
125
|
+
const PORT_PAIR = object({
|
|
126
|
+
sourceNodeId: nodeId('Node the edge starts at.'),
|
|
127
|
+
sourcePort: port('Output'),
|
|
128
|
+
targetNodeId: nodeId('Node the edge ends at.'),
|
|
129
|
+
targetPort: port('Input')
|
|
130
|
+
}, ['sourceNodeId', 'sourcePort', 'targetNodeId', 'targetPort']);
|
|
131
|
+
/** One record per exposed command, keyed by command type. */
|
|
132
|
+
const COMMANDS = {
|
|
133
|
+
add_node: {
|
|
134
|
+
description: "Add a node of the given type to the workflow. Returns the new node id; the node starts with the type's default config." +
|
|
135
|
+
CALL_ORDER,
|
|
136
|
+
inputSchema: object({
|
|
137
|
+
nodeTypeId: {
|
|
138
|
+
type: 'string',
|
|
139
|
+
description: 'Node type id as returned by list_types / search_types (`typeId`), e.g. "http_request". Unknown ids fail and name near misses.'
|
|
140
|
+
},
|
|
141
|
+
position: {
|
|
142
|
+
...position,
|
|
143
|
+
description: 'Optional canvas position; auto-placed when omitted.'
|
|
144
|
+
}
|
|
145
|
+
}, ['nodeTypeId']),
|
|
146
|
+
build: (a) => ({
|
|
147
|
+
type: 'add_node',
|
|
148
|
+
nodeTypeId: a.nodeTypeId,
|
|
149
|
+
...(a.position ? { position: a.position } : {})
|
|
150
|
+
}),
|
|
151
|
+
summarize: (c) => `Add node ${c.nodeTypeId}${c.position ? ` at ${at(c.position)}` : ''}`
|
|
152
|
+
},
|
|
153
|
+
delete_node: {
|
|
154
|
+
description: 'Delete a node and every edge attached to it.',
|
|
155
|
+
inputSchema: object({ nodeId: nodeId() }, ['nodeId']),
|
|
156
|
+
build: (a) => ({ type: 'delete_node', nodeId: a.nodeId }),
|
|
157
|
+
summarize: (c) => `Delete node ${c.nodeId}`
|
|
158
|
+
},
|
|
159
|
+
rename_node: {
|
|
160
|
+
description: 'Change the display label of a node.',
|
|
161
|
+
inputSchema: object({ nodeId: nodeId(), label: { type: 'string', description: 'New display label.' } }, ['nodeId', 'label']),
|
|
162
|
+
build: (a) => ({ type: 'rename_node', nodeId: a.nodeId, label: a.label }),
|
|
163
|
+
summarize: (c) => `Rename ${c.nodeId} to ${q(c.label)}`
|
|
164
|
+
},
|
|
165
|
+
move_node: {
|
|
166
|
+
description: 'Move a node to a canvas position.',
|
|
167
|
+
inputSchema: object({ nodeId: nodeId(), position }, ['nodeId', 'position']),
|
|
168
|
+
build: (a) => ({ type: 'move_node', nodeId: a.nodeId, position: a.position }),
|
|
169
|
+
summarize: (c) => `Move ${c.nodeId} to ${at(c.position)}`
|
|
170
|
+
},
|
|
171
|
+
swap_node: {
|
|
172
|
+
description: 'Replace a node with one of another type, keeping compatible connections and config. Reports what was dropped.' +
|
|
173
|
+
CALL_ORDER,
|
|
174
|
+
inputSchema: object({
|
|
175
|
+
nodeId: nodeId('Node to replace.'),
|
|
176
|
+
newTypeId: {
|
|
177
|
+
type: 'string',
|
|
178
|
+
description: 'Replacement node type id (see list_types / describe_type). Compatible ports and config carry over.'
|
|
179
|
+
}
|
|
180
|
+
}, ['nodeId', 'newTypeId']),
|
|
181
|
+
build: (a) => ({
|
|
182
|
+
type: 'swap_node',
|
|
183
|
+
nodeId: a.nodeId,
|
|
184
|
+
newTypeId: a.newTypeId
|
|
185
|
+
}),
|
|
186
|
+
summarize: (c) => `Swap ${c.nodeId} for a ${c.newTypeId}`
|
|
187
|
+
},
|
|
188
|
+
set_config: {
|
|
189
|
+
description: 'Set one configuration value on a node. A value outside the enum, of the wrong type, or under a key the schema does not declare is stored with a warning that names the allowed values / keys — fatal only when strict.' +
|
|
190
|
+
CALL_ORDER,
|
|
191
|
+
inputSchema: object({
|
|
192
|
+
nodeId: nodeId(),
|
|
193
|
+
key: {
|
|
194
|
+
type: 'string',
|
|
195
|
+
description: 'Config key, as listed under `config` by describe_type (with type, enum and default) or get_config.'
|
|
196
|
+
},
|
|
197
|
+
value: configValue,
|
|
198
|
+
strict: {
|
|
199
|
+
type: 'boolean',
|
|
200
|
+
description: 'When true, a value the schema rejects fails the call instead of warning.'
|
|
201
|
+
}
|
|
202
|
+
}, ['nodeId', 'key', 'value']),
|
|
203
|
+
build: (a) => ({
|
|
204
|
+
type: 'set_config',
|
|
205
|
+
nodeId: a.nodeId,
|
|
206
|
+
key: a.key,
|
|
207
|
+
value: encodeConfigValue(a.value),
|
|
208
|
+
...(a.strict !== undefined ? { strict: a.strict } : {})
|
|
209
|
+
}),
|
|
210
|
+
summarize: (c) => `Set ${c.nodeId}.${c.key} = ${c.value}`
|
|
211
|
+
},
|
|
212
|
+
get_config: {
|
|
213
|
+
description: 'Read configuration from a node. With `key`: that value and its schema entry (type, enum, default). Without: every current value under `values` plus the whole config schema.',
|
|
214
|
+
inputSchema: object({
|
|
215
|
+
nodeId: nodeId(),
|
|
216
|
+
key: {
|
|
217
|
+
type: 'string',
|
|
218
|
+
description: 'Config key to read; omit for all values and the schema.'
|
|
219
|
+
}
|
|
220
|
+
}, ['nodeId']),
|
|
221
|
+
build: (a) => ({
|
|
222
|
+
type: 'get_config',
|
|
223
|
+
nodeId: a.nodeId,
|
|
224
|
+
...(a.key !== undefined ? { key: a.key } : {})
|
|
225
|
+
}),
|
|
226
|
+
summarize: (c) => c.key !== undefined ? `Read ${c.nodeId}.${c.key}` : `Read config of ${c.nodeId}`
|
|
227
|
+
},
|
|
228
|
+
connect: {
|
|
229
|
+
description: 'Connect an output port of one node to an input port of another. An unknown port fails and lists the ports on both sides; the existing edge is kept when the connection is already there.',
|
|
230
|
+
inputSchema: PORT_PAIR,
|
|
231
|
+
build: (a) => ({
|
|
232
|
+
type: 'connect',
|
|
233
|
+
sourceNodeId: a.sourceNodeId,
|
|
234
|
+
sourcePort: a.sourcePort,
|
|
235
|
+
targetNodeId: a.targetNodeId,
|
|
236
|
+
targetPort: a.targetPort
|
|
237
|
+
}),
|
|
238
|
+
summarize: (c) => `Connect ${c.sourceNodeId}:${c.sourcePort} → ${c.targetNodeId}:${c.targetPort}`
|
|
239
|
+
},
|
|
240
|
+
disconnect_ports: {
|
|
241
|
+
description: 'Remove the edge between two specific ports.',
|
|
242
|
+
inputSchema: PORT_PAIR,
|
|
243
|
+
build: (a) => ({
|
|
244
|
+
type: 'disconnect_ports',
|
|
245
|
+
sourceNodeId: a.sourceNodeId,
|
|
246
|
+
sourcePort: a.sourcePort,
|
|
247
|
+
targetNodeId: a.targetNodeId,
|
|
248
|
+
targetPort: a.targetPort
|
|
249
|
+
}),
|
|
250
|
+
summarize: (c) => `Disconnect ${c.sourceNodeId}:${c.sourcePort} → ${c.targetNodeId}:${c.targetPort}`
|
|
251
|
+
},
|
|
252
|
+
disconnect_node: {
|
|
253
|
+
description: 'Remove every edge attached to a node.',
|
|
254
|
+
inputSchema: object({ nodeId: nodeId() }, ['nodeId']),
|
|
255
|
+
build: (a) => ({ type: 'disconnect_node', nodeId: a.nodeId }),
|
|
256
|
+
summarize: (c) => `Disconnect every edge of ${c.nodeId}`
|
|
257
|
+
},
|
|
258
|
+
list_nodes: {
|
|
259
|
+
description: 'List the nodes in the workflow with their ids, labels and types.',
|
|
260
|
+
inputSchema: EMPTY,
|
|
261
|
+
build: () => ({ type: 'list_nodes' }),
|
|
262
|
+
summarize: () => 'List nodes'
|
|
263
|
+
},
|
|
264
|
+
list_edges: {
|
|
265
|
+
description: 'List the edges in the workflow with source/target node ids and ports.',
|
|
266
|
+
inputSchema: EMPTY,
|
|
267
|
+
build: () => ({ type: 'list_edges' }),
|
|
268
|
+
summarize: () => 'List edges'
|
|
269
|
+
},
|
|
270
|
+
list_types: {
|
|
271
|
+
description: 'List every node type that can be added: `typeId`, name, category, description, tags. A type not in this list is unavailable in this editor. Use search_types to narrow, describe_type for ports and config.',
|
|
272
|
+
inputSchema: EMPTY,
|
|
273
|
+
build: () => ({ type: 'list_types' }),
|
|
274
|
+
summarize: () => 'List node types'
|
|
275
|
+
},
|
|
276
|
+
describe_type: {
|
|
277
|
+
description: 'Describe one node type before using it: input and output ports (id, data type, required), config keys (type, enum, default, description), and — when the host publishes them — the confirmation policy (`ask` means a run pauses for a person at this node), what you may do with it (`can`) and host guidance (`agent`). Call this before add_node and set_config.',
|
|
278
|
+
inputSchema: object({
|
|
279
|
+
nodeTypeId: {
|
|
280
|
+
type: 'string',
|
|
281
|
+
description: 'Node type id (`typeId` from list_types / search_types), e.g. "http_request".'
|
|
282
|
+
}
|
|
283
|
+
}, ['nodeTypeId']),
|
|
284
|
+
build: (a) => ({ type: 'describe_type', nodeTypeId: a.nodeTypeId }),
|
|
285
|
+
summarize: (c) => `Describe type ${c.nodeTypeId}`
|
|
286
|
+
},
|
|
287
|
+
search_types: {
|
|
288
|
+
description: 'Find node types by a case-insensitive substring of their id, name, description or tags, e.g. "markdown" or "http". Returns the same rows as list_types.',
|
|
289
|
+
inputSchema: object({ query: { type: 'string', description: 'Text to look for.' } }, ['query']),
|
|
290
|
+
build: (a) => ({ type: 'search_types', query: a.query }),
|
|
291
|
+
summarize: (c) => `Search types for ${q(c.query)}`
|
|
292
|
+
},
|
|
293
|
+
info: {
|
|
294
|
+
description: "Describe one node instance: type, position, current config values, input and output ports, and connected edges. For the type's schema and defaults use describe_type.",
|
|
295
|
+
inputSchema: object({ nodeId: nodeId() }, ['nodeId']),
|
|
296
|
+
build: (a) => ({ type: 'info', nodeId: a.nodeId }),
|
|
297
|
+
summarize: (c) => `Describe ${c.nodeId}`
|
|
298
|
+
},
|
|
299
|
+
undo: {
|
|
300
|
+
description: 'Undo the last change. Each tool call that changed the workflow is one undo step; a save cannot be undone.',
|
|
301
|
+
inputSchema: EMPTY,
|
|
302
|
+
build: () => ({ type: 'undo' }),
|
|
303
|
+
summarize: () => 'Undo'
|
|
304
|
+
},
|
|
305
|
+
redo: {
|
|
306
|
+
description: 'Redo the last undone change.',
|
|
307
|
+
inputSchema: EMPTY,
|
|
308
|
+
build: () => ({ type: 'redo' }),
|
|
309
|
+
summarize: () => 'Redo'
|
|
310
|
+
},
|
|
311
|
+
auto_layout: {
|
|
312
|
+
description: 'Re-arrange all nodes automatically. Moves existing nodes.',
|
|
313
|
+
inputSchema: object({
|
|
314
|
+
direction: {
|
|
315
|
+
type: 'string',
|
|
316
|
+
enum: ['horizontal', 'vertical'],
|
|
317
|
+
description: 'Flow direction. Default horizontal.'
|
|
318
|
+
}
|
|
319
|
+
}),
|
|
320
|
+
build: (a) => ({
|
|
321
|
+
type: 'auto_layout',
|
|
322
|
+
...(a.direction ? { direction: a.direction } : {})
|
|
323
|
+
}),
|
|
324
|
+
summarize: (c) => `Auto-layout (${c.direction ?? 'horizontal'})`
|
|
325
|
+
},
|
|
326
|
+
beautify_layout: {
|
|
327
|
+
description: 'Tidy the layout of all nodes. Moves existing nodes.',
|
|
328
|
+
inputSchema: EMPTY,
|
|
329
|
+
build: () => ({ type: 'beautify_layout' }),
|
|
330
|
+
summarize: () => 'Beautify layout'
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
/** The record for one exposed command — for tests that check its shape. */
|
|
334
|
+
export function commandRecord(type) {
|
|
335
|
+
return COMMANDS[type];
|
|
336
|
+
}
|
|
337
|
+
// ============================================================================
|
|
338
|
+
// view + batch
|
|
339
|
+
// ============================================================================
|
|
340
|
+
const VIEW_ACTIONS = [
|
|
341
|
+
'select_node',
|
|
342
|
+
'open_config',
|
|
343
|
+
'fit_view',
|
|
344
|
+
'zoom_in',
|
|
345
|
+
'zoom_out',
|
|
346
|
+
'zoom_to',
|
|
347
|
+
'pan_to',
|
|
348
|
+
'reset_view'
|
|
349
|
+
];
|
|
350
|
+
const VIEW_TO_COMMAND = {
|
|
351
|
+
select_node: 'select_node',
|
|
352
|
+
open_config: 'config_open',
|
|
353
|
+
fit_view: 'canvas_fit_view',
|
|
354
|
+
zoom_in: 'canvas_zoom_in',
|
|
355
|
+
zoom_out: 'canvas_zoom_out',
|
|
356
|
+
zoom_to: 'canvas_zoom_to',
|
|
357
|
+
pan_to: 'canvas_pan_to',
|
|
358
|
+
reset_view: 'canvas_reset_view'
|
|
359
|
+
};
|
|
360
|
+
const VIEW_SCHEMA = object({
|
|
361
|
+
action: {
|
|
362
|
+
type: 'string',
|
|
363
|
+
enum: VIEW_ACTIONS,
|
|
364
|
+
description: 'select_node / open_config need nodeId; zoom_to needs level; pan_to needs position; the rest take nothing else.'
|
|
365
|
+
},
|
|
366
|
+
nodeId: nodeId('Node for select_node and open_config.'),
|
|
367
|
+
level: {
|
|
368
|
+
type: 'number',
|
|
369
|
+
description: 'Zoom level for zoom_to (1 = 100%).',
|
|
370
|
+
minimum: 0.1,
|
|
371
|
+
maximum: 4
|
|
372
|
+
},
|
|
373
|
+
position: { ...position, description: 'Canvas position for pan_to.' }
|
|
374
|
+
}, ['action']);
|
|
375
|
+
function buildViewCommand(args) {
|
|
376
|
+
const action = args.action;
|
|
377
|
+
const requireArg = (key) => {
|
|
378
|
+
if (args[key] === undefined)
|
|
379
|
+
throw new ToolArgumentError(`${key} is required for action "${action}"`);
|
|
380
|
+
};
|
|
381
|
+
switch (action) {
|
|
382
|
+
case 'select_node':
|
|
383
|
+
requireArg('nodeId');
|
|
384
|
+
return { type: 'select_node', nodeId: args.nodeId };
|
|
385
|
+
case 'open_config':
|
|
386
|
+
requireArg('nodeId');
|
|
387
|
+
return { type: 'config_open', nodeId: args.nodeId };
|
|
388
|
+
case 'zoom_to':
|
|
389
|
+
requireArg('level');
|
|
390
|
+
return { type: 'canvas_zoom_to', level: args.level };
|
|
391
|
+
case 'pan_to':
|
|
392
|
+
requireArg('position');
|
|
393
|
+
return { type: 'canvas_pan_to', position: args.position };
|
|
394
|
+
default:
|
|
395
|
+
return { type: VIEW_TO_COMMAND[action] };
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const BATCH_ITEM_TYPES = [...EXPOSED_COMMAND_TYPES, 'view'];
|
|
399
|
+
const BATCH_SCHEMA = object({
|
|
400
|
+
commands: {
|
|
401
|
+
type: 'array',
|
|
402
|
+
description: 'Commands to run in order. Each item is the argument object of one tool plus a `type` naming that tool (without the prefix), e.g. {"type":"add_node","nodeTypeId":"http_request"}. Read-only items are allowed but pointless here.',
|
|
403
|
+
items: {
|
|
404
|
+
type: 'object',
|
|
405
|
+
properties: {
|
|
406
|
+
type: { type: 'string', enum: BATCH_ITEM_TYPES, description: 'Tool verb.' }
|
|
407
|
+
},
|
|
408
|
+
required: ['type'],
|
|
409
|
+
additionalProperties: true
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}, ['commands']);
|
|
413
|
+
/** Build the command(s) for one tool verb from validated args. */
|
|
414
|
+
function commandsForVerb(verb, args, viewEnabled) {
|
|
415
|
+
if (verb === 'view') {
|
|
416
|
+
if (!viewEnabled)
|
|
417
|
+
throw new ToolArgumentError('view is not available in this editor');
|
|
418
|
+
return [buildViewCommand(validateToolArgs(VIEW_SCHEMA, args))];
|
|
419
|
+
}
|
|
420
|
+
if (EXPOSED_COMMAND_TYPES.includes(verb)) {
|
|
421
|
+
const record = COMMANDS[verb];
|
|
422
|
+
return [record.build(validateToolArgs(record.inputSchema, args))];
|
|
423
|
+
}
|
|
424
|
+
throw new ToolArgumentError(`unknown command type "${verb}"`);
|
|
425
|
+
}
|
|
426
|
+
function buildBatchCommands(args, viewEnabled) {
|
|
427
|
+
const items = args.commands;
|
|
428
|
+
if (items.length === 0)
|
|
429
|
+
throw new ToolArgumentError('commands must not be empty');
|
|
430
|
+
return items.flatMap((item, i) => {
|
|
431
|
+
const { type, ...rest } = item;
|
|
432
|
+
try {
|
|
433
|
+
return commandsForVerb(type, rest, viewEnabled);
|
|
434
|
+
}
|
|
435
|
+
catch (err) {
|
|
436
|
+
if (err instanceof ToolArgumentError) {
|
|
437
|
+
throw new ToolArgumentError(`commands[${i}] (${String(type)}): ${err.message}`);
|
|
438
|
+
}
|
|
439
|
+
throw err;
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Build the descriptor list. Pure; call it as often as you like.
|
|
445
|
+
*/
|
|
446
|
+
export function buildToolDescriptors(options = {}) {
|
|
447
|
+
const viewEnabled = options.view ?? true;
|
|
448
|
+
const singles = EXPOSED_COMMAND_TYPES.map((type) => {
|
|
449
|
+
const record = COMMANDS[type];
|
|
450
|
+
return {
|
|
451
|
+
verb: type,
|
|
452
|
+
description: record.description,
|
|
453
|
+
inputSchema: record.inputSchema,
|
|
454
|
+
readOnly: !isMutatingCommand(type),
|
|
455
|
+
toCommands: (args) => [record.build(args)]
|
|
456
|
+
};
|
|
457
|
+
});
|
|
458
|
+
const view = viewEnabled
|
|
459
|
+
? [
|
|
460
|
+
{
|
|
461
|
+
verb: 'view',
|
|
462
|
+
description: 'Drive the editor view: select a node, open its config panel, or move the canvas viewport (fit, zoom, pan, reset). Changes nothing in the workflow.',
|
|
463
|
+
inputSchema: VIEW_SCHEMA,
|
|
464
|
+
// Classified as the chat panel classifies them: not in the read-only
|
|
465
|
+
// set, so they pass through the gate.
|
|
466
|
+
readOnly: false,
|
|
467
|
+
toCommands: (args) => [buildViewCommand(args)]
|
|
468
|
+
}
|
|
469
|
+
]
|
|
470
|
+
: [];
|
|
471
|
+
const batch = {
|
|
472
|
+
verb: 'batch',
|
|
473
|
+
description: 'Run several commands as one transaction: all succeed or none apply, one approval covers them all, and one undo reverts them all. Prefer this over many single calls when building a flow — describe the types first, batch the adds, connects and set_configs, then save.',
|
|
474
|
+
inputSchema: BATCH_SCHEMA,
|
|
475
|
+
readOnly: false,
|
|
476
|
+
toCommands: (args) => buildBatchCommands(args, viewEnabled)
|
|
477
|
+
};
|
|
478
|
+
return [...singles, ...view, batch];
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Summaries for the commands that are not one tool each: the view actions
|
|
482
|
+
* (folded into `view`) and the two excluded verbs, which can still appear in
|
|
483
|
+
* a command list a host asks the gate about.
|
|
484
|
+
*/
|
|
485
|
+
const OTHER_SUMMARIES = {
|
|
486
|
+
select_node: (c) => `Select ${c.nodeId}`,
|
|
487
|
+
config_open: (c) => `Open config of ${c.nodeId}`,
|
|
488
|
+
canvas_fit_view: () => 'Fit view',
|
|
489
|
+
canvas_zoom_in: () => 'Zoom in',
|
|
490
|
+
canvas_zoom_out: () => 'Zoom out',
|
|
491
|
+
canvas_zoom_to: (c) => `Zoom to ${c.level}`,
|
|
492
|
+
canvas_pan_to: (c) => `Pan to ${at(c.position)}`,
|
|
493
|
+
canvas_reset_view: () => 'Reset view',
|
|
494
|
+
clear: () => 'Clear the workflow',
|
|
495
|
+
help: () => 'Help'
|
|
496
|
+
};
|
|
497
|
+
/** One line describing a command for a human about to approve it. */
|
|
498
|
+
export function describeCommand(command) {
|
|
499
|
+
const type = command.type;
|
|
500
|
+
if (type in COMMANDS) {
|
|
501
|
+
return COMMANDS[type].summarize(command);
|
|
502
|
+
}
|
|
503
|
+
return OTHER_SUMMARIES[type](command);
|
|
504
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — a fake runtime for tests and demos.
|
|
3
|
+
*
|
|
4
|
+
* Browsers without the origin trial have no `document.modelContext`, and CI
|
|
5
|
+
* never will. This records `registerTool` calls, honours the abort signal the
|
|
6
|
+
* way the spec describes, and lets a test or a storybook panel invoke a tool
|
|
7
|
+
* by name. Install it with `attachWebMCP(instance, { modelContext: fake })`
|
|
8
|
+
* or assign it to `document.modelContext` before mounting.
|
|
9
|
+
*
|
|
10
|
+
* @module webmcp/fake
|
|
11
|
+
*/
|
|
12
|
+
import type { ModelContextLike, RegisteredToolDefinition, ToolResult } from './types.js';
|
|
13
|
+
export interface FakeModelContext extends ModelContextLike {
|
|
14
|
+
/** Registered tools by name, in registration order. */
|
|
15
|
+
readonly tools: Map<string, RegisteredToolDefinition>;
|
|
16
|
+
/** Invoke a tool by its full name; returns the runtime's result envelope. */
|
|
17
|
+
executeTool(name: string, input?: unknown): Promise<ToolResult>;
|
|
18
|
+
/** Invoke a tool and parse the JSON text it returns. */
|
|
19
|
+
call(name: string, input?: unknown): Promise<Record<string, unknown>>;
|
|
20
|
+
/** Fires after every register / unregister, like the spec's `toolchange`. */
|
|
21
|
+
onToolChange(listener: () => void): () => void;
|
|
22
|
+
}
|
|
23
|
+
export declare function createFakeModelContext(): FakeModelContext;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — a fake runtime for tests and demos.
|
|
3
|
+
*
|
|
4
|
+
* Browsers without the origin trial have no `document.modelContext`, and CI
|
|
5
|
+
* never will. This records `registerTool` calls, honours the abort signal the
|
|
6
|
+
* way the spec describes, and lets a test or a storybook panel invoke a tool
|
|
7
|
+
* by name. Install it with `attachWebMCP(instance, { modelContext: fake })`
|
|
8
|
+
* or assign it to `document.modelContext` before mounting.
|
|
9
|
+
*
|
|
10
|
+
* @module webmcp/fake
|
|
11
|
+
*/
|
|
12
|
+
export function createFakeModelContext() {
|
|
13
|
+
const tools = new Map();
|
|
14
|
+
const listeners = new Set();
|
|
15
|
+
const notify = () => listeners.forEach((l) => l());
|
|
16
|
+
return {
|
|
17
|
+
tools,
|
|
18
|
+
registerTool(tool, options) {
|
|
19
|
+
tools.set(tool.name, tool);
|
|
20
|
+
options?.signal?.addEventListener('abort', () => {
|
|
21
|
+
tools.delete(tool.name);
|
|
22
|
+
notify();
|
|
23
|
+
}, { once: true });
|
|
24
|
+
notify();
|
|
25
|
+
return Promise.resolve();
|
|
26
|
+
},
|
|
27
|
+
async executeTool(name, input = {}) {
|
|
28
|
+
const tool = tools.get(name);
|
|
29
|
+
if (!tool)
|
|
30
|
+
throw new Error(`[fake modelContext] no tool named "${name}"`);
|
|
31
|
+
return tool.execute(input);
|
|
32
|
+
},
|
|
33
|
+
async call(name, input = {}) {
|
|
34
|
+
const result = await this.executeTool(name, input);
|
|
35
|
+
return JSON.parse(result.content[0].text);
|
|
36
|
+
},
|
|
37
|
+
onToolChange(listener) {
|
|
38
|
+
listeners.add(listener);
|
|
39
|
+
return () => listeners.delete(listener);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — approval gate.
|
|
3
|
+
*
|
|
4
|
+
* Tool calls that would change the document pass through here before anything
|
|
5
|
+
* runs. The policy is the host's (`WebMCPOptions.approval`); the default is a
|
|
6
|
+
* confirm dialog rendered inside the page, because the WebMCP permission model
|
|
7
|
+
* is about which origins may *see* tools, not about whether a given call may
|
|
8
|
+
* change the user's document. Any agent or extension on the page can call a
|
|
9
|
+
* tool, so the gate must not be weaker than the chat panel's click-to-apply.
|
|
10
|
+
*
|
|
11
|
+
* Two ergonomics live here too. A batch shows a *summary* of what changes
|
|
12
|
+
* (nodes added, edges connected, config keys set) above the line list, so the
|
|
13
|
+
* person reads what the agent is doing rather than a verb list. And the
|
|
14
|
+
* dialog can offer "apply further edits without asking" for the rest of the
|
|
15
|
+
* page's life — edits only: `save` and `run` are consequential and always ask.
|
|
16
|
+
* The gate cannot tell one agent from another, so the offer is worded as what
|
|
17
|
+
* it is: every caller on the page is covered once it is ticked. It is still a
|
|
18
|
+
* click-to-apply — one the person chose to make once.
|
|
19
|
+
*
|
|
20
|
+
* @module webmcp/gate
|
|
21
|
+
*/
|
|
22
|
+
import type { Command } from '../commands/types.js';
|
|
23
|
+
import type { Messages, MessagesOverride } from '../messages/index.js';
|
|
24
|
+
import type { WebMCPApproval, WebMCPApprovalRequest } from './types.js';
|
|
25
|
+
/** What the gate needs to know about the call it is asking approval for. */
|
|
26
|
+
export interface GateRequest extends WebMCPApprovalRequest {
|
|
27
|
+
/**
|
|
28
|
+
* Dialog lines, one per command. Defaults to `commands.map(describeCommand)`;
|
|
29
|
+
* `save` and `run` have no commands to describe, so the gate fills in their
|
|
30
|
+
* line itself when this is omitted.
|
|
31
|
+
*/
|
|
32
|
+
lines?: string[];
|
|
33
|
+
/** Extra sentence shown under the title, e.g. "This cannot be undone." */
|
|
34
|
+
hint?: string;
|
|
35
|
+
/**
|
|
36
|
+
* A consequential call (`save`, `run`) is never covered by the "don't ask
|
|
37
|
+
* again for edits" choice and never offers it. Defaults to `tool` being
|
|
38
|
+
* `save` or `run`.
|
|
39
|
+
*/
|
|
40
|
+
consequential?: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface ApprovalGate {
|
|
43
|
+
/**
|
|
44
|
+
* Ask whether `commands` may run. Resolves `true` to run. Rejects with
|
|
45
|
+
* {@link GateBusyError} when a decision is already pending — the caller
|
|
46
|
+
* turns that into a `busy` tool error instead of stacking dialogs.
|
|
47
|
+
*/
|
|
48
|
+
request(commands: Command[], request: GateRequest): Promise<boolean>;
|
|
49
|
+
/** True while a decision is pending. */
|
|
50
|
+
readonly busy: boolean;
|
|
51
|
+
/** True once the person chose to apply further edits without asking. */
|
|
52
|
+
readonly editsPreApproved: boolean;
|
|
53
|
+
/** Dismiss any open dialog (as a rejection) and release resources. */
|
|
54
|
+
dispose(): void;
|
|
55
|
+
}
|
|
56
|
+
/** A second mutating call arrived while a decision was pending. */
|
|
57
|
+
export declare class GateBusyError extends Error {
|
|
58
|
+
constructor();
|
|
59
|
+
}
|
|
60
|
+
export interface CreateGateOptions {
|
|
61
|
+
/** Where the built-in dialog mounts. Default `document.body`. */
|
|
62
|
+
container?: HTMLElement;
|
|
63
|
+
/** Name shown in the dialog title; read at request time. */
|
|
64
|
+
editorName: () => string;
|
|
65
|
+
/** Strings for the dialog, as a partial override or a getter for one. */
|
|
66
|
+
messages?: MessagesOverride | (() => MessagesOverride);
|
|
67
|
+
/** Offer the "don't ask again for edits" choice. Default true. */
|
|
68
|
+
rememberEdits?: boolean;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* One sentence saying what a list of commands does, for the dialog's hint:
|
|
72
|
+
* "3 changes — adds 2 nodes, connects 1 edge. Applied together, undone together."
|
|
73
|
+
* Returns `undefined` for a single command, whose one line already says it all.
|
|
74
|
+
*/
|
|
75
|
+
export declare function summarizeCommands(commands: Command[], m: Messages['webmcp']): string | undefined;
|
|
76
|
+
export declare function createApprovalGate(approval: WebMCPApproval, options: CreateGateOptions): ApprovalGate;
|