@capybara-hq/plugin-sdk 0.1.0-rc.8 → 0.1.0-rc.80

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 (149) hide show
  1. package/README.md +1 -1
  2. package/agent-docs/agent-templates.md +14 -2
  3. package/agent-docs/concepts.md +14 -5
  4. package/agent-docs/connector-templates.md +11 -3
  5. package/agent-docs/extension-points/channel.md +1 -1
  6. package/agent-docs/extension-points/context.md +15 -0
  7. package/agent-docs/extension-points/device-provider.md +15 -0
  8. package/agent-docs/extension-points/file-preview.md +3 -1
  9. package/agent-docs/extension-points/identity-source.md +2 -0
  10. package/agent-docs/extension-points/model-provider.md +3 -1
  11. package/agent-docs/extension-points/object-store.md +2 -0
  12. package/agent-docs/extension-points/push-provider.md +17 -0
  13. package/agent-docs/extensions.md +77 -4
  14. package/agent-docs/plugin-format.md +45 -10
  15. package/agent-docs/setup.md +54 -0
  16. package/dist/agent-template.d.ts +15 -6
  17. package/dist/agent-template.js +32 -13
  18. package/dist/avatar-presets.d.ts +8 -0
  19. package/dist/avatar-presets.js +17 -0
  20. package/dist/bootstrap.d.ts +109 -1
  21. package/dist/bootstrap.js +185 -2
  22. package/dist/cards.d.ts +60 -13
  23. package/dist/cards.js +88 -18
  24. package/dist/channel.d.ts +128 -47
  25. package/dist/channel.js +10 -5
  26. package/dist/client-css.d.ts +36 -0
  27. package/dist/client-css.js +326 -0
  28. package/dist/client-section.d.ts +35 -0
  29. package/dist/client-section.js +133 -0
  30. package/dist/config-ui.d.ts +28 -6
  31. package/dist/config-ui.js +88 -25
  32. package/dist/config-url-fetch.d.ts +19 -0
  33. package/dist/config-url-fetch.js +43 -0
  34. package/dist/connector-auth.d.ts +13 -2
  35. package/dist/connector-auth.js +82 -8
  36. package/dist/connector.d.ts +74 -32
  37. package/dist/context.d.ts +40 -0
  38. package/dist/context.js +2 -0
  39. package/dist/ctx.d.ts +224 -30
  40. package/dist/ctx.js +9 -1
  41. package/dist/define-extension.d.ts +23 -13
  42. package/dist/define-extension.js +19 -5
  43. package/dist/device-denied-paths.d.ts +20 -0
  44. package/dist/device-denied-paths.js +118 -0
  45. package/dist/device-operation.d.ts +339 -0
  46. package/dist/device-operation.js +312 -0
  47. package/dist/device-protocol.d.ts +285 -50
  48. package/dist/device-protocol.js +755 -90
  49. package/dist/device-provider.d.ts +780 -0
  50. package/dist/device-provider.js +409 -0
  51. package/dist/device-service.d.ts +126 -0
  52. package/dist/device-service.js +211 -0
  53. package/dist/dial-in.d.ts +286 -0
  54. package/dist/dial-in.js +213 -0
  55. package/dist/directory.d.ts +15 -12
  56. package/dist/extension-point.d.ts +2 -0
  57. package/dist/extension-point.js +7 -3
  58. package/dist/field-meta.d.ts +19 -0
  59. package/dist/field-meta.js +54 -0
  60. package/dist/file-preview.d.ts +43 -10
  61. package/dist/git-hosts.d.ts +53 -0
  62. package/dist/git-hosts.js +98 -0
  63. package/dist/hooks.d.ts +125 -20
  64. package/dist/hooks.js +5 -0
  65. package/dist/http-access.d.ts +71 -0
  66. package/dist/http-access.js +12 -0
  67. package/dist/identity.d.ts +70 -9
  68. package/dist/identity.js +46 -1
  69. package/dist/index.d.ts +28 -4
  70. package/dist/index.js +28 -4
  71. package/dist/model-params.d.ts +41 -22
  72. package/dist/model-params.js +28 -15
  73. package/dist/model.d.ts +210 -75
  74. package/dist/model.js +152 -19
  75. package/dist/native-failure.d.ts +13 -0
  76. package/dist/native-failure.js +31 -0
  77. package/dist/net.d.ts +52 -0
  78. package/dist/net.js +34 -0
  79. package/dist/network-classes.d.ts +98 -0
  80. package/dist/network-classes.js +291 -0
  81. package/dist/permissions.d.ts +65 -22
  82. package/dist/permissions.js +139 -43
  83. package/dist/platform-only.d.ts +27 -0
  84. package/dist/platform-only.js +35 -0
  85. package/dist/plugin-manifest.d.ts +30 -6
  86. package/dist/plugin-manifest.js +147 -55
  87. package/dist/plugin-package.d.ts +82 -39
  88. package/dist/plugin-package.js +68 -41
  89. package/dist/push.d.ts +75 -0
  90. package/dist/push.js +87 -0
  91. package/dist/sandbox-linux-runtime-source.d.ts +1 -0
  92. package/dist/sandbox-linux-runtime-source.js +2343 -0
  93. package/dist/sandbox-native-runtime-source.d.ts +10 -0
  94. package/dist/sandbox-native-runtime-source.js +480 -0
  95. package/dist/sandbox-runtime-source.d.ts +47 -0
  96. package/dist/sandbox-runtime-source.js +73 -0
  97. package/dist/sandbox-write-denies.d.ts +139 -0
  98. package/dist/sandbox-write-denies.js +869 -0
  99. package/dist/secret.d.ts +8 -2
  100. package/dist/secret.js +11 -4
  101. package/dist/setup-check.d.ts +128 -0
  102. package/dist/setup-check.js +142 -0
  103. package/dist/setup-guide-parse.d.ts +121 -0
  104. package/dist/setup-guide-parse.js +356 -0
  105. package/dist/setup-guide-render.d.ts +45 -0
  106. package/dist/setup-guide-render.js +351 -0
  107. package/dist/setup-guide.d.ts +86 -72
  108. package/dist/setup-guide.js +340 -108
  109. package/dist/shell-capabilities.d.ts +211 -0
  110. package/dist/shell-capabilities.js +44 -0
  111. package/dist/shell-environment.d.ts +57 -0
  112. package/dist/shell-environment.js +21 -0
  113. package/dist/skill-invocation.d.ts +5 -0
  114. package/dist/skill-invocation.js +4 -3
  115. package/dist/skill.d.ts +27 -17
  116. package/dist/skill.js +40 -26
  117. package/dist/storage.d.ts +52 -25
  118. package/dist/svg-viewbox.d.ts +10 -0
  119. package/dist/svg-viewbox.js +24 -0
  120. package/dist/theme-assets.d.ts +25 -9
  121. package/dist/theme-assets.js +87 -47
  122. package/dist/theme-tokens.generated.d.ts +26 -2
  123. package/dist/theme-tokens.generated.js +17 -0
  124. package/dist/theme.d.ts +48 -10
  125. package/dist/theme.js +100 -21
  126. package/dist/tool.d.ts +156 -30
  127. package/dist/tool.js +91 -9
  128. package/dist/ui-parts.d.ts +23 -0
  129. package/dist/ui-parts.js +385 -0
  130. package/dist/ui-resolve.d.ts +35 -0
  131. package/dist/ui-resolve.js +42 -0
  132. package/dist/ui-slots.d.ts +65 -0
  133. package/dist/ui-slots.js +261 -0
  134. package/dist/ui.d.ts +245 -77
  135. package/dist/ui.js +104 -27
  136. package/dist/url.d.ts +4 -1
  137. package/dist/url.js +4 -1
  138. package/dist/web.d.ts +26 -20
  139. package/package.json +12 -1
  140. package/schemas/bootstrap-extra.v1.schema.json +186 -0
  141. package/schemas/bootstrap.v1.schema.json +20 -0
  142. package/schemas/connector-template.v1.schema.json +62 -4
  143. package/schemas/plugin.v1.schema.json +74 -143
  144. package/schemas/theme.v1.schema.json +77 -0
  145. package/agent-docs/extension-points/sandbox.md +0 -15
  146. package/dist/org-access.d.ts +0 -27
  147. package/dist/org-access.js +0 -35
  148. package/dist/sandbox.d.ts +0 -254
  149. package/dist/sandbox.js +0 -153
@@ -0,0 +1,211 @@
1
+ /**
2
+ * The device service socket protocol: how a device provider (or the machine program serving a
3
+ * dial-in device) talks to a service it started with `DeviceProvider.ensureService`. Every
4
+ * provider and every service speaks it, so a service runs the same on every provider.
5
+ *
6
+ * The provider starts the service with {@link DEVICE_SERVICE_SOCKET_ENV} naming the unix socket
7
+ * the service listens on and {@link DEVICE_SERVICE_STATE_ENV} naming its state directory (the
8
+ * only place it may write besides the workspace downloads). The spec's files are written before
9
+ * start into the service's code directory: a directory the service can only read where the
10
+ * provider can separate code from state, else the state directory. On a connection,
11
+ * both sides exchange length-prefixed messages ({@link encodeServiceMessage}). The first message
12
+ * from the provider is JSON whose `kind` is one of {@link SERVICE_CONNECTION_KINDS}:
13
+ *
14
+ * - `{"kind":"request"}`: the next provider message is the request body; the service answers
15
+ * with exactly one message and closes the connection.
16
+ * - `{"kind":"stream"}`: both sides keep exchanging messages until either side closes.
17
+ *
18
+ * **Viewer streams.** On a stream the kernel opens for a viewer (a person's panel, through a
19
+ * viewer ticket), the first stream message after `{"kind":"stream"}` is written by the kernel, not
20
+ * the panel: the viewer context {@link DeviceServiceViewerContext},
21
+ * `{"kind":"viewer","conversationId":"<id>","rootConversationId":"<id>"}`, naming the ticket's
22
+ * conversation (checked at mint to be owned by the viewing person) and the root of its
23
+ * conversation tree (the conversation itself outside sub-agents). The panel's own messages
24
+ * follow, unchanged and in order. A service reads only that first message as context
25
+ * ({@link parseViewerContext}); a later message of the same shape comes from the panel and is
26
+ * never context. Providers and the device program pass the viewer context through like any
27
+ * stream message.
28
+ */
29
+ /**
30
+ * The request body the kernel sends a running service before restarting it on new files (an
31
+ * upgraded extension, {@link DeviceServiceRollCheck}). A service that answers
32
+ * `{"busy":true,"reason":"…"}` is kept running (a person is using it); the kernel asks again at a
33
+ * later ensure. Any other answer, an error or no answer within a few seconds lets the restart go
34
+ * ahead, so a service that does not know the check is restarted as before. Every service gets
35
+ * this body on its request connection, so a service must ignore a request body it does not know
36
+ * (answer an error, act on nothing), never treat an arbitrary body as a command.
37
+ */
38
+ export const DEVICE_SERVICE_ROLL_CHECK = '{"capybara":"roll-check"}';
39
+ /** Whether `body` is the kernel's {@link DEVICE_SERVICE_ROLL_CHECK} request. */
40
+ export function isRollCheck(body) {
41
+ const text = typeof body === "string" ? body : new TextDecoder().decode(body);
42
+ try {
43
+ const v = JSON.parse(text);
44
+ return (typeof v === "object" && v !== null && v.capybara === "roll-check");
45
+ }
46
+ catch {
47
+ return false;
48
+ }
49
+ }
50
+ /** Reads a service's answer to the roll check: busy only for `{"busy":true}` (never throws). */
51
+ export function parseRollCheck(body) {
52
+ const text = typeof body === "string" ? body : new TextDecoder().decode(body);
53
+ try {
54
+ const v = JSON.parse(text);
55
+ if (typeof v === "object" && v !== null && v.busy === true)
56
+ return {
57
+ busy: true,
58
+ ...(typeof v.reason === "string" && { reason: v.reason.slice(0, 200) }),
59
+ };
60
+ }
61
+ catch {
62
+ // not JSON: not busy
63
+ }
64
+ return { busy: false };
65
+ }
66
+ /** Names the unix socket path the service listens on. */
67
+ export const DEVICE_SERVICE_SOCKET_ENV = "CAPYBARA_SERVICE_SOCKET";
68
+ /** Names the service's state directory (its own data, its write root; not where its code is). */
69
+ export const DEVICE_SERVICE_STATE_ENV = "CAPYBARA_SERVICE_STATE";
70
+ /**
71
+ * Names the device's workspace root. Every provider sets it for every service (a spec cannot set
72
+ * it); a service confines the files it takes in (uploads) and lets out (downloads) to it.
73
+ */
74
+ export const DEVICE_SERVICE_WORKSPACE_ENV = "CAPYBARA_SERVICE_WORKSPACE";
75
+ /**
76
+ * Names the service's install directory, where the provider gives it one (device-local does;
77
+ * absent elsewhere): a place for what the service fetches once and then only runs, such as a
78
+ * browser, apart from its state. The service may write it until it creates the regular file
79
+ * {@link DEVICE_SERVICE_INSTALL_SEAL} in it; from its next start on the directory is read only
80
+ * for the service, so nothing the service later runs (a compromised browser included) can
81
+ * replace what was installed. The provider empties it at every start that does not find it
82
+ * sealed under the service's current files (unsealed, or a plugin upgrade since), so a service
83
+ * installs afresh while unsealed and never trusts what it finds there. The process that seals
84
+ * keeps its write access until it exits, so a service runs no untrusted input (no web page) while
85
+ * unsealed, seals only a complete install and then exits; the next start, reviewed again, runs it
86
+ * read only.
87
+ */
88
+ export const DEVICE_SERVICE_INSTALL_ENV = "CAPYBARA_SERVICE_INSTALL";
89
+ /** The file that seals a service's install directory ({@link DEVICE_SERVICE_INSTALL_ENV}). */
90
+ export const DEVICE_SERVICE_INSTALL_SEAL = ".sealed";
91
+ /**
92
+ * Names the Mach service allowances the provider's sandbox gives the service (macOS: device-local
93
+ * sets it, a spec cannot). Its value {@link DEVICE_SERVICE_MACH_CHROMIUM} means the full Chromium
94
+ * build can start there (its `MachPortRendezvousServer` check-in is allowed); absent, a service
95
+ * must not count on it (an older capybara-device refuses it).
96
+ */
97
+ export const DEVICE_SERVICE_MACH_ENV = "CAPYBARA_SERVICE_MACH";
98
+ /** {@link DEVICE_SERVICE_MACH_ENV}'s value for the full Chromium build's Mach services. */
99
+ export const DEVICE_SERVICE_MACH_CHROMIUM = "chromium";
100
+ /** The largest message body, in bytes, either side may send on a service connection. */
101
+ export const DEVICE_SERVICE_MAX_MESSAGE = 8 * 1024 * 1024;
102
+ /** The kinds a connection's first message may name. */
103
+ export const SERVICE_CONNECTION_KINDS = ["request", "stream"];
104
+ const HEADER = 4;
105
+ /**
106
+ * Frames one message: a 4-byte big-endian length followed by `body`. Throws a `RangeError` for a
107
+ * body above {@link DEVICE_SERVICE_MAX_MESSAGE}.
108
+ */
109
+ export function encodeServiceMessage(body) {
110
+ if (body.byteLength > DEVICE_SERVICE_MAX_MESSAGE)
111
+ throw new RangeError(`service message of ${body.byteLength} bytes exceeds ${DEVICE_SERVICE_MAX_MESSAGE} bytes`);
112
+ const out = new Uint8Array(HEADER + body.byteLength);
113
+ new DataView(out.buffer).setUint32(0, body.byteLength);
114
+ out.set(body, HEADER);
115
+ return out;
116
+ }
117
+ /**
118
+ * Splits a byte stream into the messages {@link encodeServiceMessage} framed, whatever the chunk
119
+ * boundaries. `push` returns the messages completed by `chunk` (copies, safe to keep). A declared
120
+ * length above {@link DEVICE_SERVICE_MAX_MESSAGE} throws a `RangeError` as soon as the header is
121
+ * read; the connection should then be closed (the decoder is not usable afterwards).
122
+ */
123
+ export class ServiceMessageDecoder {
124
+ // unread bytes, oldest first from index `first`; `pending` is their total length
125
+ chunks = [];
126
+ first = 0;
127
+ pending = 0;
128
+ // the declared length of the message being read, once its header is complete
129
+ need = null;
130
+ push(chunk) {
131
+ if (chunk.byteLength > 0) {
132
+ this.chunks.push(chunk.slice());
133
+ this.pending += chunk.byteLength;
134
+ }
135
+ const out = [];
136
+ for (;;) {
137
+ if (this.need === null) {
138
+ if (this.pending < HEADER)
139
+ break;
140
+ const header = this.take(HEADER);
141
+ const len = new DataView(header.buffer, header.byteOffset, HEADER).getUint32(0);
142
+ if (len > DEVICE_SERVICE_MAX_MESSAGE)
143
+ throw new RangeError(`service message of ${len} bytes exceeds ${DEVICE_SERVICE_MAX_MESSAGE} bytes`);
144
+ this.need = len;
145
+ }
146
+ if (this.pending < this.need)
147
+ break;
148
+ out.push(this.take(this.need));
149
+ this.need = null;
150
+ }
151
+ return out;
152
+ }
153
+ /** Removes the first `n` (at most `pending`) bytes into one new array, copying each byte once. */
154
+ take(n) {
155
+ const out = new Uint8Array(n);
156
+ let at = 0;
157
+ while (at < n) {
158
+ // never undefined: callers take at most `pending` bytes
159
+ const head = this.chunks[this.first];
160
+ if (head === undefined)
161
+ break;
162
+ const k = Math.min(head.byteLength, n - at);
163
+ out.set(head.subarray(0, k), at);
164
+ at += k;
165
+ if (k === head.byteLength)
166
+ this.first++;
167
+ else
168
+ this.chunks[this.first] = head.subarray(k);
169
+ }
170
+ // drop consumed chunks in one go (no per-chunk shift)
171
+ if (this.first > 64 || this.first === this.chunks.length) {
172
+ this.chunks = this.chunks.slice(this.first);
173
+ this.first = 0;
174
+ }
175
+ this.pending -= n;
176
+ return out;
177
+ }
178
+ }
179
+ /** The body of the viewer context stream message (UTF-8 JSON; the provider frames it). */
180
+ export function encodeViewerContext(ctx) {
181
+ return new TextEncoder().encode(JSON.stringify({
182
+ kind: "viewer",
183
+ conversationId: ctx.conversationId,
184
+ rootConversationId: ctx.rootConversationId,
185
+ }));
186
+ }
187
+ /**
188
+ * Reads a stream message body as the viewer context: `kind` is `"viewer"` and both ids are
189
+ * non-empty strings; only those three fields are returned. Null for anything else (not UTF-8,
190
+ * not JSON, another shape).
191
+ */
192
+ export function parseViewerContext(body) {
193
+ let msg;
194
+ try {
195
+ const text = typeof body === "string" ? body : new TextDecoder("utf-8", { fatal: true }).decode(body);
196
+ msg = JSON.parse(text);
197
+ }
198
+ catch {
199
+ return null;
200
+ }
201
+ if (typeof msg !== "object" || msg === null || Array.isArray(msg))
202
+ return null;
203
+ const { kind, conversationId, rootConversationId } = msg;
204
+ if (kind !== "viewer")
205
+ return null;
206
+ if (typeof conversationId !== "string" || conversationId === "")
207
+ return null;
208
+ if (typeof rootConversationId !== "string" || rootConversationId === "")
209
+ return null;
210
+ return { kind: "viewer", conversationId, rootConversationId };
211
+ }
@@ -0,0 +1,286 @@
1
+ /**
2
+ * The dial-in channel: the kernel's generic way for a machine to connect to a deployment and
3
+ * serve one device of a `device-provider@1` extension that declares `permissions.dialIn`. The
4
+ * kernel signs the machine in, holds its socket on the gateway role, bounds every frame by the
5
+ * negotiated size and relays the frames' opaque `body` between the machine and the device's
6
+ * provider. It never reads a body: what a body means is the provider's protocol, named by
7
+ * `permissions.dialIn.protocol` and by the machine's hello.
8
+ */
9
+ import type { DeviceStream } from "./device-provider.js";
10
+ /**
11
+ * A dial-in protocol: `<name>/<major>`, a lower-case name of 1 to 41 characters and a major
12
+ * version from 1 to 9999 (for example `capybara-device/6`). A machine's hello must name exactly
13
+ * the protocol of the provider serving its device.
14
+ */
15
+ export declare const DIAL_IN_PROTOCOL_RE: RegExp;
16
+ /**
17
+ * The credit, in messages, each side of a dial-in stream starts with for the other: a side sends a
18
+ * `stream-data` only while it holds credit, and grants more with `stream-credit` as it consumes
19
+ * messages, so one stream cannot fill the connection and starve the machine's other frames.
20
+ */
21
+ export declare const DIAL_IN_STREAM_INITIAL_CREDIT = 8;
22
+ /**
23
+ * The stream frames both sides send ({@link DialInChannel.openStream}). `data` is one whole message
24
+ * in standard base64; the frame, like every frame, fits the connection's `maxFrameBytes`. On a
25
+ * connection whose hello and ready both set `binaryStreamData`, both sides send `stream-data` as
26
+ * a binary WebSocket message instead ({@link encodeDialInStreamData}: no base64, no JSON).
27
+ */
28
+ export type DialInStreamFrame = {
29
+ type: "stream-data";
30
+ streamId: string;
31
+ data: string;
32
+ }
33
+ /** Grants the peer `messages` more `stream-data` frames on the stream. */
34
+ | {
35
+ type: "stream-credit";
36
+ streamId: string;
37
+ messages: number;
38
+ }
39
+ /** Either side ends the stream; no frame of it follows. */
40
+ | {
41
+ type: "stream-close";
42
+ streamId: string;
43
+ reason: string;
44
+ };
45
+ /** A frame from the machine. The kernel checks the envelope; `body` is the provider's. */
46
+ export type DialInMachineFrame = {
47
+ /** The first frame after the socket opens. */
48
+ type: "hello";
49
+ /** Must equal the serving provider's `permissions.dialIn.protocol`. */
50
+ protocol: string;
51
+ /** Random per run of the machine's program, the same on each of its connections. */
52
+ programRunId: string;
53
+ program: {
54
+ version: string;
55
+ os: string;
56
+ arch: string;
57
+ bootId?: string;
58
+ };
59
+ /**
60
+ * The largest frame, in UTF-8 bytes, the machine accepts; `binaryStreamData`: the machine
61
+ * sends and accepts binary `stream-data` ({@link encodeDialInStreamData}) once the kernel's
62
+ * ready says so too.
63
+ */
64
+ transport: {
65
+ maxFrameBytes: number;
66
+ binaryStreamData?: true;
67
+ };
68
+ body: unknown;
69
+ }
70
+ /** The answer to the kernel's `req` with the same `id`. */
71
+ | {
72
+ type: "res";
73
+ id: string;
74
+ body: unknown;
75
+ }
76
+ /** An unsolicited frame, handed to {@link DialInHandler.onFrame}. */
77
+ | {
78
+ type: "event";
79
+ body: unknown;
80
+ }
81
+ /** The answer to the kernel's `stream-open`: the machine opened the stream, or why not. */
82
+ | {
83
+ type: "stream-opened";
84
+ streamId: string;
85
+ ok: boolean;
86
+ error?: string;
87
+ } | DialInStreamFrame;
88
+ /** A frame from the kernel to the machine. */
89
+ export type DialInKernelFrame =
90
+ /**
91
+ * The kernel accepted the hello (the provider's `onHello` answered ok). `maxFrameBytes` is the
92
+ * connection's effective frame capacity: the smaller of the machine's `transport.maxFrameBytes`
93
+ * and the kernel's limit. Neither side sends a larger frame on this connection.
94
+ */
95
+ {
96
+ type: "ready";
97
+ connectionId: string;
98
+ maxFrameBytes: number;
99
+ /**
100
+ * Set when the machine's hello offered it: from now on both sides send `stream-data` only
101
+ * as binary messages ({@link encodeDialInStreamData}); every other frame stays JSON text.
102
+ */
103
+ binaryStreamData?: true;
104
+ }
105
+ /** A provider's request ({@link DialInChannel.request}); answered by a `res` with its `id`. */
106
+ | {
107
+ type: "req";
108
+ id: string;
109
+ deadlineMs: number;
110
+ body: unknown;
111
+ }
112
+ /** The kernel no longer waits for request `id` (aborted or timed out). */
113
+ | {
114
+ type: "cancel";
115
+ id: string;
116
+ }
117
+ /** The machine should finish its work and disconnect (`drain`) or stop at once (`stop`). */
118
+ | {
119
+ type: "stop";
120
+ reason: "drain" | "stop";
121
+ }
122
+ /** Opens a stream on the machine; `body` names the target in the provider's protocol. */
123
+ | {
124
+ type: "stream-open";
125
+ streamId: string;
126
+ body: unknown;
127
+ } | DialInStreamFrame;
128
+ /** A machine frame that is not a valid dial-in envelope. The kernel closes the socket on it. */
129
+ export declare class DialInProtocolError extends Error {
130
+ constructor(message: string);
131
+ }
132
+ /**
133
+ * Strictly parses one machine frame: the text must be at most `maxBytes` UTF-8 bytes (checked
134
+ * before `JSON.parse`), an object of a known `type` with exactly that type's keys. `body` is kept
135
+ * as parsed, never inspected. Throws {@link DialInProtocolError}; a `maxBytes` that is not a
136
+ * positive integer throws a `RangeError`.
137
+ */
138
+ export declare function parseDialInMachineFrame(text: string, maxBytes: number): DialInMachineFrame;
139
+ /**
140
+ * The machine's side of {@link parseDialInMachineFrame}: strictly parses one kernel frame, at most
141
+ * `maxBytes` UTF-8 bytes (checked before `JSON.parse`), an object of a known `type` with exactly
142
+ * that type's keys; `body` is kept as parsed. Throws {@link DialInProtocolError}; a `maxBytes`
143
+ * that is not a positive integer throws a `RangeError`.
144
+ */
145
+ export declare function parseDialInKernelFrame(text: string, maxBytes: number): DialInKernelFrame;
146
+ /** The first byte of a binary `stream-data` message (the only binary dial-in message). */
147
+ export declare const DIAL_IN_STREAM_DATA_TAG = 1;
148
+ /**
149
+ * One `stream-data` as a binary WebSocket message, for a connection whose hello and ready set
150
+ * `binaryStreamData`: the tag {@link DIAL_IN_STREAM_DATA_TAG}, the stream id's length in bytes (one
151
+ * byte), the id in ASCII, then the message's bytes as they are. Like every frame it must fit the
152
+ * connection's `maxFrameBytes` (its byte length). Throws a `RangeError` for an id that is not a
153
+ * dial-in id.
154
+ */
155
+ export declare function encodeDialInStreamData(streamId: string, data: Uint8Array): Uint8Array;
156
+ /**
157
+ * Reads a binary `stream-data` message ({@link encodeDialInStreamData}); `data` is a view into
158
+ * `message`. Throws {@link DialInProtocolError} for anything else: another tag, a truncated
159
+ * header, an id that is not a dial-in id.
160
+ */
161
+ export declare function decodeDialInStreamData(message: Uint8Array): {
162
+ streamId: string;
163
+ data: Uint8Array;
164
+ };
165
+ /**
166
+ * The provider's side of the dial-in channel: `DeviceProvider.dialIn`, required when the extension
167
+ * declares `permissions.dialIn = { protocol: "<name>/<major>" }`.
168
+ */
169
+ export interface DialInHandler {
170
+ /**
171
+ * Called on the gateway replica holding the socket, after the kernel authenticated it.
172
+ * `connection` names the machine and the connection the hello arrived on.
173
+ */
174
+ onHello(deviceId: string, hello: unknown, signal: AbortSignal, connection: DialInConnectionInfo): Promise<{
175
+ ok: true;
176
+ } | {
177
+ ok: false;
178
+ code: string;
179
+ message: string;
180
+ }>;
181
+ /** Unsolicited frames from the machine (not responses to requests), with their connection. */
182
+ onFrame?(deviceId: string, frame: unknown, connection: DialInConnectionInfo): Promise<void>;
183
+ /**
184
+ * The connection whose hello this provider accepted closed. `connection` is that connection's,
185
+ * so a `replaced` close names the old machine, never the one that replaced it.
186
+ */
187
+ onClose?(deviceId: string, reason: "offline" | "replaced" | "revoked" | "deleted", connection: DialInConnectionInfo): void;
188
+ }
189
+ /** Which machine and which of its connections a {@link DialInHandler} call is about. */
190
+ export interface DialInConnectionInfo {
191
+ /**
192
+ * The kernel's id of the signed-in machine (its credential): a machine signing in again to the
193
+ * same device, or another machine replacing it, gets another id.
194
+ */
195
+ machineId: string;
196
+ /** The connection id, the same the kernel sends to the machine in `ready`. */
197
+ connectionId: string;
198
+ }
199
+ /**
200
+ * `ctx.dialIn` (requires `permissions.dialIn`): talks to the machines serving this extension's
201
+ * devices. Each call checks that the device is served by the calling extension.
202
+ */
203
+ export interface DialInChannel {
204
+ /**
205
+ * Sends `frame` as the body of a `req` and resolves the body of the machine's `res`. Throws
206
+ * {@link DeviceOfflineError} when no machine holds the device within the reconnect grace,
207
+ * {@link DialInAnswerTimeoutError} when the machine stayed connected but sent no answer within
208
+ * `timeoutMs` plus a short margin (the outcome is unknown), and `DeviceRequestNotSentError` when
209
+ * the request provably never reached the machine (given up while still queued).
210
+ */
211
+ request(deviceId: string, frame: unknown, opts: {
212
+ timeoutMs: number;
213
+ signal?: AbortSignal;
214
+ }): Promise<unknown>;
215
+ online(deviceId: string): Promise<boolean>;
216
+ /**
217
+ * The negotiated frame capacity of the machine's current connection: the `maxFrameBytes` the
218
+ * kernel sent in that connection's `ready` (the smaller of the machine's announced
219
+ * `transport.maxFrameBytes` and the deployment's limit). A provider sizes its request bodies
220
+ * and the answers it asks for by it: a `req` frame larger than this is refused. Null when no
221
+ * machine holds the device now. Read from shared state at every call, so a reconnect with
222
+ * another capacity shows at once; it can change between this call and a later request.
223
+ */
224
+ capacity(deviceId: string): Promise<{
225
+ maxFrameBytes: number;
226
+ } | null>;
227
+ stop(deviceId: string, reason: "drain" | "stop"): Promise<void>;
228
+ /**
229
+ * Opens a stream to the machine serving the device: sends `stream-open` with `body` (the
230
+ * target, in the provider's protocol) and resolves once the machine answers `stream-opened` ok.
231
+ * Stream frames go straight over the machine's socket, never through shared state, so this
232
+ * works only on the gateway replica holding that socket: elsewhere it throws
233
+ * {@link DialInStreamElsewhereError}. Throws {@link DeviceOfflineError} when no machine holds the
234
+ * device. Each message must fit the connection's frame capacity once base64-encoded in a
235
+ * `stream-data` frame. `signal` bounds the opening only.
236
+ */
237
+ openStream(deviceId: string, body: unknown, opts: {
238
+ signal: AbortSignal;
239
+ }): Promise<DeviceStream>;
240
+ }
241
+ /**
242
+ * {@link DialInChannel.openStream} was called on a replica that does not hold the machine's
243
+ * socket: the caller must reach the holding gateway replica instead.
244
+ */
245
+ export declare class DialInStreamElsewhereError extends Error {
246
+ readonly code = "device.stream_elsewhere";
247
+ constructor(deviceId: string, opts?: {
248
+ cause?: unknown;
249
+ });
250
+ }
251
+ /**
252
+ * `ctx.devices` (requires `provides: ["device-provider@1"]`): what a device provider tells the
253
+ * kernel about its devices.
254
+ */
255
+ export interface DevicesCtx {
256
+ /**
257
+ * The device's report (`DeviceProvider.report`) or its permission support may have changed: the
258
+ * kernel reads the report (and the permission support of the device's instance, if any) again
259
+ * and stores them. When the revision changed, it invalidates what was approved against the old
260
+ * one; when only display text changed under the same revision, the new text is stored and no
261
+ * approval is invalidated.
262
+ */
263
+ reportChanged(deviceId: string): Promise<void>;
264
+ }
265
+ /** No machine serves the device (it is not connected, and did not reconnect within the grace). */
266
+ export declare class DeviceOfflineError extends Error {
267
+ readonly code = "device.offline";
268
+ constructor(deviceId: string, opts?: {
269
+ cause?: unknown;
270
+ });
271
+ }
272
+ /** Recognizes {@link DeviceOfflineError} by name and code, so another SDK copy's error matches. */
273
+ export declare function isDeviceOfflineError(e: unknown): e is DeviceOfflineError;
274
+ /**
275
+ * The machine stayed connected but sent no answer within the request's deadline plus a short
276
+ * margin (`ctx.dialIn.request`): it may have run the request, so the outcome is unknown. A cancel
277
+ * was queued for it.
278
+ */
279
+ export declare class DialInAnswerTimeoutError extends Error {
280
+ readonly code = "device.answer_timeout";
281
+ constructor(deadlineMs: number, opts?: {
282
+ cause?: unknown;
283
+ });
284
+ }
285
+ /** Recognizes {@link DialInAnswerTimeoutError} by name and code, so another SDK copy's error matches. */
286
+ export declare function isDialInAnswerTimeoutError(e: unknown): e is DialInAnswerTimeoutError;