Repository navigation
Expand file tree
/
Copy pathplugin.wit
More file actions
435 lines (369 loc) · 15.6 KB
/
Copy pathplugin.wit
File metadata and controls
435 lines (369 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/// Contract between the TickNode engine (host, wasmtime) and a node plugin (WASM component).
///
/// A plugin is a set of node types. Each graph node is a separate `node` resource inside the
/// plugin instance, so a node retains its own state across calls. Async work (delays, HTTP)
/// is executed by the host: the plugin receives a `task-id`, and the result arrives later via a `task` trigger.
package ticknode:plugin@0.1.0;
interface types {
/// Data value — postcard-encoded `ticknode_shared::DataValue`.
/// WIT has no recursive types, and values can be nested (lists, objects).
type value = list<u8>;
variant scalar-type {
%bool, number, %string, time, datetime, duration, color, object, any,
/// Custom plugin type by full name (`ha.state`), declared via `types`.
named(string),
}
/// Pin data type: `list-depth = 2`, `base = number` means `list<list<number>>`.
record data-type {
base: scalar-type,
list-depth: u8,
}
record field-definition {
name: string,
title: string,
description: string,
data-type: data-type,
/// An optional field may be absent from the value.
optional: bool,
}
record enum-value {
/// The value as passed (`DataValue::String`).
value: string,
title: string,
}
variant type-kind {
/// Struct: value is a `DataValue::Object` with a type tag.
structure(list<field-definition>),
/// Enum: value is a `DataValue::String` from the enumeration.
enumeration(list<enum-value>),
/// Opaque signed i64, optionally labelled by a plugin option source.
identifier(option<string>),
}
/// Custom plugin data type.
record type-definition {
/// Full dotted name, e.g. `ha.state`.
name: string,
title: string,
description: string,
/// `#rrggbb` — color of pins and wires in the editor.
color: option<string>,
kind: type-kind,
}
variant pin-type {
exec,
data(data-type),
}
enum pin-direction { input, output }
record pin {
id: u32,
name: string,
pin-type: pin-type,
direction: pin-direction,
/// Required input: without a connection or value on the node, the host does not call `execute`.
required: bool,
}
variant property-kind {
/// Free text (the default for properties that are not described).
text,
/// Drop-down with static (value, label) options.
%select(list<tuple<string, string>>),
/// Searchable picker over the options the plugin publishes at runtime under this source name
/// (`host.set-options`).
dynamic(string),
/// Wall-clock text or HH:MM:SS. The flag permits signed offsets; empty is unset.
duration(bool),
/// HH:MM or HH:MM:SS, empty is unset.
time,
/// Comma-separated values stored in option order.
multi-select(list<tuple<string, string>>),
multiline,
/// Persisted metadata, omitted from the node and inspector.
hidden,
}
/// Describes a node property so the editor can show a title, a drop-down or a picker.
record property-definition {
name: string,
title: string,
description: string,
kind: property-kind,
input: option<u32>,
}
/// One option of a dynamic property.
record property-option {
/// The value stored in the property.
value: string,
/// Primary line.
label: string,
/// Secondary line, e.g. the entity id.
detail: string,
/// Material Design Icons name, e.g. `mdi:lightbulb`.
icon: option<string>,
}
record node-definition {
type-name: string,
title: string,
category: string,
description: string,
/// Detailed explanation of how the node works ("info" tooltip in the editor).
help: string,
pins: list<pin>,
/// Event source node: the host calls `node.poll` every tick. Otherwise `poll` is not called.
polls: bool,
/// Properties shown with a title, a drop-down or a picker. Others stay plain text rows.
properties: list<property-definition>,
/// Relative work per node call; native logic costs one unit.
cost: u32,
/// Pure node: no exec pins; the host calls `execute` when an output is pulled and caches the result like native pure nodes.
pure: bool,
}
variant capability {
/// HTTP(S) requests to listed domains and their subdomains; `*` — any domain.
http(list<string>),
/// Plugin's own directory, accessible via `std::fs` (WASI) as the current working directory.
storage,
/// Persistent WebSocket connections (`ws://`, `wss://`) to listed hosts and their subdomains; `*` — any host.
websocket(list<string>),
}
record property {
name: string,
value: value,
}
variant setting-kind {
text,
number,
%bool,
/// Token or password: the editor cannot read it back.
secret,
url,
/// (value, label) pairs.
select(list<tuple<string, string>>),
}
/// Plugin setting configured by the user in the UI.
record setting-definition {
key: string,
title: string,
description: string,
kind: setting-kind,
/// Without a value (and without a `default`), the plugin is not considered configured.
required: bool,
/// Default value — postcard-encoded `DataValue`.
default: option<value>,
}
record resource-action {
title: string,
type-name: string,
property: string,
requires-source: option<string>,
}
record resource-collection {
id: string,
title: string,
icon: string,
source: string,
actions: list<resource-action>,
}
record plugin-manifest {
name: string,
version: string,
author: string,
capabilities: list<capability>,
settings: list<setting-definition>,
resources: list<resource-collection>,
values-namespace: option<string>,
}
variant part-data {
text(string),
blob(string),
}
record multipart-part {
name: string,
file-name: option<string>,
content-type: option<string>,
data: part-data,
}
record http-request {
method: string,
url: string,
headers: list<tuple<string, string>>,
body: list<u8>,
/// 0 — default host timeout.
timeout-ms: u32,
multipart: list<multipart-part>,
save-to-blob: bool,
}
record http-response {
status: u16,
headers: list<tuple<string, string>>,
body: list<u8>,
}
record blob-response {
status: u16,
headers: list<tuple<string, string>>,
blob: string,
size: u64,
content-type: option<string>,
}
type task-id = u64;
variant task-output {
delay,
http(http-response),
blob(blob-response),
}
record blob-info {
id: string,
size: u64,
content-type: option<string>,
file-name: option<string>,
created: string,
}
record task-done {
id: task-id,
%result: result<task-output, string>,
}
/// Handle of a plugin-owned WebSocket connection. Valid only within one plugin instance:
/// after a crash (instance recreated) all connections are closed and old ids are void.
type ws-id = u64;
/// Plugin-level connection and HTTP events delivered to `plugin-tick`.
variant ws-event {
/// The connection is established.
opened(ws-id),
/// A text frame was received.
message(tuple<ws-id, string>),
/// The connection ended (or could not be opened); carries the reason.
closed(tuple<ws-id, string>),
/// A plugin-owned HTTP task completed.
http-done(task-done),
}
/// Reason for calling `node.execute`.
variant trigger {
/// Pulse on an input exec pin (or manual trigger from the editor).
pin(u32),
/// A task previously started by the node has completed.
task(task-done),
}
variant output {
/// Pulse from an output exec pin (downstream nodes execute on the next tick).
exec(u32),
/// New value on an output data pin.
data(tuple<u32, value>),
/// Remove the current value from an output data pin.
clear(u32),
}
/// Graph node id: the (high, low) 64-bit halves of its UUID (WIT has no 128-bit integer).
type node-id = tuple<u64, u64>;
/// Outputs produced by a node outside of its own `execute`/`poll` (see `plugin-tick`).
record emission {
/// Graph node id (as given to `create-node`).
node: node-id,
outputs: list<output>,
}
record value-field {
path: string,
title: string,
data-type: data-type,
}
record value-entry {
key: string,
title: string,
icon: option<string>,
fields: list<value-field>,
}
enum log-level { debug, info, warn, error }
/// Translatable message for the user (shown in the UI, e.g. a `configure` error). `key` is a Fluent
/// message id from the plugin's `translations` (it must start with the plugin name slug, otherwise
/// the host drops it); `fallback` is the final English text used when the key cannot be resolved.
record ui-error {
key: string,
args: list<tuple<string, string>>,
fallback: string,
}
}
/// Host services. Task-starting functions work only during `execute`/`poll`
/// (the task belongs to the currently executing node).
interface host {
use types.{log-level, task-id, http-request, ws-id, property-option, ui-error, blob-info as blob-meta, value-entry, value};
/// Log message to the editor console (on behalf of the current node). Logs are never translated:
/// always write them in English.
log: func(level: log-level, message: string);
/// Reports a continuing structured node error; log=true also writes its English fallback.
node-error: func(message: ui-error, log: bool);
/// Current tick number.
current-tick: func() -> u64;
/// Tick duration in milliseconds.
tick-duration-ms: func() -> u32;
/// Delay for `ticks` ticks; result is `task-output::delay`.
delay: func(ticks: u64) -> result<task-id, string>;
/// HTTP request; requires `capability::http` for the domain. Without a current node it belongs
/// to the plugin instance (maximum 8 concurrent tasks) and completes via `http-done` in plugin-tick.
start-http: func(request: http-request) -> result<task-id, string>;
/// Cancels a task owned by the current node, or by this plugin when no node is current.
cancel-task: func(id: task-id) -> bool;
/// Opens a WebSocket connection owned by the plugin (not by a node); usable from any plugin call.
/// Requires `capability::websocket` for the host. Returns immediately: the connection is opened
/// asynchronously and the plugin receives `ws-event::opened` (or `closed` on failure) in `plugin-tick`.
/// At most 4 connections per plugin.
ws-connect: func(url: string, headers: list<tuple<string, string>>) -> result<ws-id, string>;
/// Sends a text frame (max 16 MiB). An error if the connection is unknown or closed.
ws-send: func(id: ws-id, text: string) -> result<_, string>;
/// Closes the connection; no `closed` event is delivered for it afterwards.
ws-close: func(id: ws-id);
/// Replaces the plugin's options for a dynamic property `source` (see `property-kind.dynamic`).
/// Usable from any plugin call. The host caps the number of options per source (20 000) and the
/// length of every string (options violating the limits are dropped and logged); an empty list
/// removes the source. Unchanged lists are not re-sent to editors.
set-options: func(source: string, options: list<property-option>);
/// Stores binary data in the host blob store (up to 16 MiB per call); returns the blob ID.
blob-put: func(data: list<u8>, content-type: option<string>, file-name: option<string>) -> result<string, string>;
/// Reads a range from a blob (len clamped to 4 MiB).
blob-read: func(id: string, offset: u64, len: u32) -> result<list<u8>, string>;
/// Returns metadata of a blob in the host store.
blob-info: func(id: string) -> option<blob-meta>;
/// Replaces the read-only value catalog of this plugin.
set-value-catalog: func(entries: list<value-entry>);
/// Publishes a watched entry; none makes it unavailable.
set-value: func(key: string, value: option<value>);
}
interface nodes {
use types.{plugin-manifest, node-definition, type-definition, property, value, trigger, output, ui-error, ws-event, emission, node-id};
manifest: func() -> plugin-manifest;
/// Applies settings (following the manifest schema; values are saved or defaults). Called by the host
/// after loading, on every settings change, and after recreating the plugin instance
/// (crash) — before recreating nodes. `err` is shown to the user (translated via its `key`);
/// the plugin remains loaded.
configure: func(settings: list<property>) -> result<_, ui-error>;
/// Definitions of all node types with default properties.
definitions: func() -> list<node-definition>;
/// Custom plugin data types (structs and enums). The host reads them alongside `definitions`.
types: func() -> list<type-definition>;
/// Translations shipped by the plugin: `(locale code, Fluent source)` pairs, e.g. `("uk", "...")`.
/// Definition texts are the English fallback, so an `en` resource is optional. The host validates
/// the syntax and keeps only messages with the plugin's own id prefixes. The host reads them
/// alongside `definitions`.
translations: func() -> list<tuple<string, string>>;
resource node {
/// Pins may depend on properties.
definition: func() -> node-definition;
properties: func() -> list<property>;
set-property: func(name: string, value: value);
/// Interactive validation only; loading and restoring stored state bypass this hook.
validate-property: func(name: string, value: value) -> result<_, ui-error>;
/// `inputs` — values of connected input data pins.
execute: func(trigger: trigger, inputs: list<tuple<u32, value>>) -> list<output>;
/// For event source nodes: called at the start of every tick.
poll: func(tick: u64) -> option<u32>;
}
/// `node` is the graph node id (UUID halves); the plugin uses it to address `emission`s.
create-node: func(type-name: string, node: node-id) -> result<node, string>;
/// Called by the host once per tick for every enabled plugin (even with no events, so the plugin can
/// drive timers and keep-alives), after finished tasks are delivered and before event sources are
/// polled. `events` are WebSocket events and plugin-owned HTTP completions (at most 1000 per tick; the rest
/// stays queued). Returned emissions are applied as if the target node had executed; the host drops
/// emissions for nodes that do not exist or belong to another plugin, and invalid pins or values.
plugin-tick: func(events: list<ws-event>) -> list<emission>;
/// Entry keys relative to the namespace; restored in full after configure/restart.
watch-values: func(added: list<string>, removed: list<string>);
}
world plugin {
import host;
export nodes;
}