@capybara-hq/plugin-sdk 0.1.0-rc.3 → 0.1.0-rc.31
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/agent-docs/agent-templates.md +15 -3
- package/agent-docs/concepts.md +5 -0
- package/agent-docs/connector-templates.md +2 -1
- package/agent-docs/extension-points/channel.md +1 -1
- package/agent-docs/extension-points/config-source.md +1 -1
- package/agent-docs/extension-points/connector.md +1 -1
- package/agent-docs/extension-points/context.md +15 -0
- package/agent-docs/extension-points/counter.md +1 -1
- package/agent-docs/extension-points/directory.md +1 -1
- package/agent-docs/extension-points/file-preview.md +1 -1
- package/agent-docs/extension-points/identity-source.md +1 -1
- package/agent-docs/extension-points/model-provider.md +4 -2
- package/agent-docs/extension-points/object-store.md +3 -1
- package/agent-docs/extension-points/sandbox.md +3 -3
- package/agent-docs/extension-points/secret-store.md +1 -1
- package/agent-docs/extension-points/web-fetch.md +1 -1
- package/agent-docs/extension-points/web-search.md +1 -1
- package/agent-docs/extensions.md +29 -4
- package/agent-docs/plugin-format.md +43 -3
- package/agent-docs/setup.md +46 -0
- package/agent-docs/skills.md +1 -1
- package/agent-docs/tool-naming.md +1 -1
- package/dist/agent-template.d.ts +15 -6
- package/dist/agent-template.js +32 -13
- package/dist/bootstrap.d.ts +15 -1
- package/dist/bootstrap.js +17 -1
- package/dist/cards.d.ts +56 -13
- package/dist/cards.js +90 -20
- package/dist/channel.d.ts +111 -48
- package/dist/channel.js +10 -5
- package/dist/client-css.d.ts +36 -0
- package/dist/client-css.js +326 -0
- package/dist/client-section.d.ts +35 -0
- package/dist/client-section.js +133 -0
- package/dist/config-ui.d.ts +22 -0
- package/dist/config-ui.js +64 -4
- package/dist/connector-auth.d.ts +7 -2
- package/dist/connector-auth.js +29 -8
- package/dist/connector.d.ts +74 -32
- package/dist/context.d.ts +40 -0
- package/dist/context.js +2 -0
- package/dist/ctx.d.ts +135 -26
- package/dist/define-extension.d.ts +26 -12
- package/dist/define-extension.js +11 -4
- package/dist/device-protocol-v3.d.ts +117 -0
- package/dist/device-protocol-v3.js +409 -0
- package/dist/device-protocol.d.ts +330 -0
- package/dist/device-protocol.js +567 -0
- package/dist/extension-point.d.ts +2 -0
- package/dist/extension-point.js +5 -2
- package/dist/file-preview.d.ts +1 -1
- package/dist/git-hosts.d.ts +44 -0
- package/dist/git-hosts.js +81 -0
- package/dist/hooks.d.ts +111 -21
- package/dist/hooks.js +5 -0
- package/dist/http-access.d.ts +52 -0
- package/dist/http-access.js +12 -0
- package/dist/identity.d.ts +22 -9
- package/dist/index.d.ts +23 -1
- package/dist/index.js +24 -1
- package/dist/marketplace.d.ts +26 -0
- package/dist/marketplace.js +43 -1
- package/dist/model-params.d.ts +41 -22
- package/dist/model-params.js +28 -15
- package/dist/model.d.ts +180 -71
- package/dist/model.js +71 -19
- package/dist/net.d.ts +52 -0
- package/dist/net.js +34 -0
- package/dist/permissions.d.ts +42 -20
- package/dist/permissions.js +97 -40
- package/dist/plugin-manifest.d.ts +45 -5
- package/dist/plugin-manifest.js +169 -6
- package/dist/plugin-package.d.ts +82 -38
- package/dist/plugin-package.js +61 -29
- package/dist/sandbox-linux-runtime-source.d.ts +1 -0
- package/dist/sandbox-linux-runtime-source.js +1886 -0
- package/dist/sandbox-native-runtime-source.d.ts +8 -0
- package/dist/sandbox-native-runtime-source.js +325 -0
- package/dist/sandbox-operation.d.ts +248 -0
- package/dist/sandbox-operation.js +245 -0
- package/dist/sandbox-runtime-source.d.ts +19 -0
- package/dist/sandbox-runtime-source.js +28 -0
- package/dist/sandbox-write-denies.d.ts +60 -0
- package/dist/sandbox-write-denies.js +630 -0
- package/dist/sandbox.d.ts +201 -16
- package/dist/sandbox.js +89 -14
- package/dist/secret.d.ts +4 -1
- package/dist/secret.js +4 -1
- package/dist/setup-check.d.ts +115 -0
- package/dist/setup-check.js +142 -0
- package/dist/setup-guide-parse.d.ts +121 -0
- package/dist/setup-guide-parse.js +350 -0
- package/dist/setup-guide-render.d.ts +45 -0
- package/dist/setup-guide-render.js +351 -0
- package/dist/setup-guide.d.ts +94 -0
- package/dist/setup-guide.js +336 -0
- package/dist/skill-invocation.d.ts +4 -3
- package/dist/skill-invocation.js +5 -4
- package/dist/skill.d.ts +34 -21
- package/dist/skill.js +48 -31
- package/dist/storage.d.ts +52 -25
- package/dist/theme-assets.d.ts +39 -0
- package/dist/theme-assets.js +449 -0
- package/dist/theme-readability.d.ts +37 -0
- package/dist/theme-readability.js +145 -0
- package/dist/theme-tokens.generated.d.ts +279 -0
- package/dist/theme-tokens.generated.js +174 -0
- package/dist/theme.d.ts +152 -0
- package/dist/theme.js +392 -0
- package/dist/tool.d.ts +69 -26
- package/dist/tool.js +29 -9
- package/dist/ui-parts.d.ts +23 -0
- package/dist/ui-parts.js +337 -0
- package/dist/ui-resolve.d.ts +35 -0
- package/dist/ui-resolve.js +42 -0
- package/dist/ui-slots.d.ts +53 -0
- package/dist/ui-slots.js +238 -0
- package/dist/ui.d.ts +191 -79
- package/dist/ui.js +101 -27
- package/dist/url.d.ts +4 -1
- package/dist/url.js +4 -1
- package/package.json +7 -1
- package/schemas/bootstrap.v1.schema.json +6 -0
- package/schemas/connector-template.v1.schema.json +35 -0
- package/schemas/marketplace.v1.schema.json +14 -0
- package/schemas/plugin.v1.schema.json +115 -3
- package/schemas/theme.v1.schema.json +225 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/** Development staging only. Task 8 replaces v2 and removes this module and its staged names together. */
|
|
2
|
+
import { DeviceProtocolError, parseDeviceFrame, parseOpResult, } from "./device-protocol.js";
|
|
3
|
+
import { validateAllowHost, validateSandboxPathShape } from "./sandbox.js";
|
|
4
|
+
import { parseSandboxBrowseScope, parseSandboxExecutionRef, parseSandboxOperationRef, parseSandboxOperationScope, parseSandboxPreparationScope, } from "./sandbox-operation.js";
|
|
5
|
+
export const STAGED_DEVICE_PROTOCOL_VERSION = 3;
|
|
6
|
+
const object = (value) => {
|
|
7
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
8
|
+
throw new DeviceProtocolError("Expected an object");
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
function keys(value, allowed) {
|
|
12
|
+
for (const key of Object.keys(value))
|
|
13
|
+
if (!allowed.includes(key))
|
|
14
|
+
throw new DeviceProtocolError(`Unknown field ${key}`);
|
|
15
|
+
}
|
|
16
|
+
function string(value, max = 128) {
|
|
17
|
+
if (typeof value !== "string" || !value.length || value.length > max)
|
|
18
|
+
throw new DeviceProtocolError("Expected a bounded nonempty string");
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function integer(value, min = 0, max = Number.MAX_SAFE_INTEGER) {
|
|
22
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < min || value > max)
|
|
23
|
+
throw new DeviceProtocolError("Expected a bounded integer");
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function path(value) {
|
|
27
|
+
if (typeof value !== "string" || validateSandboxPathShape(value).length)
|
|
28
|
+
throw new DeviceProtocolError("Expected an absolute filesystem path");
|
|
29
|
+
}
|
|
30
|
+
function freeze(value) {
|
|
31
|
+
if (value !== null && typeof value === "object") {
|
|
32
|
+
for (const v of Object.values(value))
|
|
33
|
+
freeze(v);
|
|
34
|
+
Object.freeze(value);
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function support(value) {
|
|
39
|
+
const o = object(value);
|
|
40
|
+
keys(o, ["version", "restricted", "fullAccess", "background", "unavailableReason"]);
|
|
41
|
+
if (o.version !== 1 || typeof o.fullAccess !== "boolean" || typeof o.background !== "boolean")
|
|
42
|
+
throw new DeviceProtocolError("Invalid runtime permission report");
|
|
43
|
+
if (o.restricted !== null) {
|
|
44
|
+
const r = object(o.restricted);
|
|
45
|
+
keys(r, ["filesystem", "network", "native", "concurrentScopes"]);
|
|
46
|
+
for (const k of ["filesystem", "network", "native", "concurrentScopes"])
|
|
47
|
+
if (r[k] !== true)
|
|
48
|
+
throw new DeviceProtocolError("Incomplete restricted runtime report");
|
|
49
|
+
}
|
|
50
|
+
if (o.unavailableReason !== undefined)
|
|
51
|
+
string(o.unavailableReason, 4096);
|
|
52
|
+
return o;
|
|
53
|
+
}
|
|
54
|
+
/** Strictly parses one authenticated owner-ceiling report (B2); throws {@link DeviceProtocolError}. */
|
|
55
|
+
export function parseOwnerCeilingReportV3(value) {
|
|
56
|
+
return freeze(structuredClone(owner(value)));
|
|
57
|
+
}
|
|
58
|
+
function narrowing(value) {
|
|
59
|
+
const o = object(value);
|
|
60
|
+
keys(o, ["fullAccess", "restrictedProjectReach"]);
|
|
61
|
+
if ((o.fullAccess !== undefined && o.fullAccess !== false) ||
|
|
62
|
+
(o.restrictedProjectReach !== undefined && o.restrictedProjectReach !== "workspace"))
|
|
63
|
+
throw new DeviceProtocolError("The deployment may only narrow the owner ceiling");
|
|
64
|
+
return o;
|
|
65
|
+
}
|
|
66
|
+
function owner(value) {
|
|
67
|
+
const o = object(value);
|
|
68
|
+
keys(o, [
|
|
69
|
+
"version",
|
|
70
|
+
"sequence",
|
|
71
|
+
"revision",
|
|
72
|
+
"fullAccess",
|
|
73
|
+
"restrictedProjectReach",
|
|
74
|
+
"restrictedAllowedDomains",
|
|
75
|
+
]);
|
|
76
|
+
integer(o.sequence);
|
|
77
|
+
if (o.version !== 2 || !["workspace", "os-user"].includes(String(o.restrictedProjectReach)))
|
|
78
|
+
throw new DeviceProtocolError("Invalid owner ceiling report version or project reach");
|
|
79
|
+
string(o.revision, 512);
|
|
80
|
+
if (typeof o.fullAccess !== "boolean" ||
|
|
81
|
+
!Array.isArray(o.restrictedAllowedDomains) ||
|
|
82
|
+
o.restrictedAllowedDomains.length > 100 ||
|
|
83
|
+
o.restrictedAllowedDomains.some((h) => typeof h !== "string" || validateAllowHost(h).length))
|
|
84
|
+
throw new DeviceProtocolError("Invalid owner ceiling");
|
|
85
|
+
return o;
|
|
86
|
+
}
|
|
87
|
+
const GENERATION = /^[0-9]{1,16}$/;
|
|
88
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
89
|
+
function generation(value) {
|
|
90
|
+
if (typeof value !== "string" || !GENERATION.test(value))
|
|
91
|
+
throw new DeviceProtocolError("Expected an instance generation");
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
function bindRequest(a) {
|
|
95
|
+
string(a.instanceId);
|
|
96
|
+
generation(a.instanceGeneration);
|
|
97
|
+
if (typeof a.bindingId !== "string" || !UUID.test(a.bindingId))
|
|
98
|
+
throw new DeviceProtocolError("Expected a binding id");
|
|
99
|
+
if (a.expectedPreviousGeneration !== null) {
|
|
100
|
+
generation(a.expectedPreviousGeneration);
|
|
101
|
+
if (BigInt(a.expectedPreviousGeneration) >= BigInt(a.instanceGeneration))
|
|
102
|
+
throw new DeviceProtocolError("Previous generation must precede the bound generation");
|
|
103
|
+
}
|
|
104
|
+
return a;
|
|
105
|
+
}
|
|
106
|
+
function identity(value) {
|
|
107
|
+
const o = object(value);
|
|
108
|
+
keys(o, ["runtimeHash", "helperHash", "imageFingerprint", "instanceGeneration"]);
|
|
109
|
+
for (const key of ["runtimeHash", "helperHash", "imageFingerprint"])
|
|
110
|
+
if (typeof o[key] !== "string" || !/^[a-f0-9]{64}$/.test(o[key]))
|
|
111
|
+
throw new DeviceProtocolError("Incomplete runtime identity");
|
|
112
|
+
string(o.instanceGeneration);
|
|
113
|
+
return o;
|
|
114
|
+
}
|
|
115
|
+
const natives = ["exec", "readFile", "writeFile", "stat", "list", "scan"];
|
|
116
|
+
const legacy = ["create", "stop", "policy", "policyStatus"];
|
|
117
|
+
function request(o) {
|
|
118
|
+
keys(o, ["type", "id", "op", "args", "deadlineMs"]);
|
|
119
|
+
const id = string(o.id), op = string(o.op), a = object(o.args), deadlineMs = integer(o.deadlineMs, 1);
|
|
120
|
+
if (legacy.includes(op)) {
|
|
121
|
+
if (op === "policy" && a.ownerCeiling !== undefined)
|
|
122
|
+
narrowing(a.ownerCeiling);
|
|
123
|
+
const parsed = parseDeviceFrame(JSON.stringify(o), "kernel");
|
|
124
|
+
if (parsed.type !== "req")
|
|
125
|
+
throw new DeviceProtocolError("Expected request");
|
|
126
|
+
return freeze({ ...parsed, op: op, args: object(parsed.args) });
|
|
127
|
+
}
|
|
128
|
+
if (op === "bindInstance") {
|
|
129
|
+
keys(a, ["instanceId", "instanceGeneration", "bindingId", "expectedPreviousGeneration"]);
|
|
130
|
+
bindRequest(a);
|
|
131
|
+
}
|
|
132
|
+
else if (op === "revokeOperation") {
|
|
133
|
+
keys(a, ["ref"]);
|
|
134
|
+
a.ref = parseSandboxOperationRef(a.ref);
|
|
135
|
+
}
|
|
136
|
+
else if (["backgroundStatus", "backgroundTail", "backgroundReadOutput", "backgroundStop"].includes(op)) {
|
|
137
|
+
keys(a, [
|
|
138
|
+
"ref",
|
|
139
|
+
...(op === "backgroundReadOutput" ? ["offset", "maxBytes"] : []),
|
|
140
|
+
...(op === "backgroundTail" ? ["maxBytes"] : op === "backgroundStop" ? ["graceMs"] : []),
|
|
141
|
+
]);
|
|
142
|
+
a.ref = parseSandboxExecutionRef(a.ref);
|
|
143
|
+
if (op === "backgroundTail" || op === "backgroundReadOutput")
|
|
144
|
+
integer(a.maxBytes, 0, 65536);
|
|
145
|
+
if (op === "backgroundReadOutput")
|
|
146
|
+
integer(a.offset);
|
|
147
|
+
if (op === "backgroundStop")
|
|
148
|
+
integer(a.graceMs, 0, 60000);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const preparation = op.startsWith("prepare"), browsing = op.startsWith("browse"), background = op === "backgroundStart";
|
|
152
|
+
const method = background ? "exec" : preparation ? op.slice(7) : browsing ? op.slice(6) : op;
|
|
153
|
+
const native = method[0]?.toLowerCase() + method.slice(1);
|
|
154
|
+
if (!natives.includes(native) ||
|
|
155
|
+
(preparation && !["readFile", "writeFile", "stat"].includes(native)) ||
|
|
156
|
+
(browsing && !["stat", "list", "scan"].includes(native)))
|
|
157
|
+
throw new DeviceProtocolError("Unknown operation");
|
|
158
|
+
a.scope = preparation
|
|
159
|
+
? parseSandboxPreparationScope(a.scope)
|
|
160
|
+
: browsing
|
|
161
|
+
? parseSandboxBrowseScope(a.scope)
|
|
162
|
+
: parseSandboxOperationScope(a.scope);
|
|
163
|
+
const fields = native === "exec"
|
|
164
|
+
? ["command", "cwd", "env", "timeoutMs", "maxOutputBytes", "limits", "grants"]
|
|
165
|
+
: native === "readFile"
|
|
166
|
+
? ["path", "maxBytes"]
|
|
167
|
+
: native === "writeFile"
|
|
168
|
+
? ["path", "dataB64"]
|
|
169
|
+
: native === "stat"
|
|
170
|
+
? ["path"]
|
|
171
|
+
: native === "list"
|
|
172
|
+
? ["path", "limit"]
|
|
173
|
+
: ["base"];
|
|
174
|
+
keys(a, ["scope", ...fields, ...(background ? ["taskId", "outputPath", "retainUntilMs"] : [])]);
|
|
175
|
+
if (native !== "exec")
|
|
176
|
+
path(a[native === "scan" ? "base" : "path"]);
|
|
177
|
+
const validated = { ...a };
|
|
178
|
+
delete validated.scope;
|
|
179
|
+
if (native === "exec") {
|
|
180
|
+
const scope = parseSandboxOperationScope(a.scope);
|
|
181
|
+
validated.tag = scope.tag;
|
|
182
|
+
validated.actor = scope.actor;
|
|
183
|
+
if (background) {
|
|
184
|
+
string(a.taskId);
|
|
185
|
+
path(a.outputPath);
|
|
186
|
+
integer(a.retainUntilMs, scope.admitBeforeMs);
|
|
187
|
+
delete validated.taskId;
|
|
188
|
+
delete validated.outputPath;
|
|
189
|
+
delete validated.retainUntilMs;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else if (native !== "writeFile")
|
|
193
|
+
validated.limits = { mode: "sandbox" };
|
|
194
|
+
// Reuse v2's pure value guards; inserted mode is never returned or used as authority.
|
|
195
|
+
parseDeviceFrame(JSON.stringify({ type: "req", id, op: native, args: validated, deadlineMs }), "kernel");
|
|
196
|
+
if (native === "writeFile" &&
|
|
197
|
+
(typeof a.dataB64 !== "string" ||
|
|
198
|
+
!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(a.dataB64)))
|
|
199
|
+
throw new DeviceProtocolError("Invalid base64 payload");
|
|
200
|
+
}
|
|
201
|
+
return freeze({ type: "req", id, op: op, args: a, deadlineMs });
|
|
202
|
+
}
|
|
203
|
+
/** Strict scope-bearing frames; active parseDeviceFrame still speaks v2. */
|
|
204
|
+
export function parseDeviceFrameV3(raw, from, maxFrameBytes) {
|
|
205
|
+
if (maxFrameBytes !== undefined &&
|
|
206
|
+
new TextEncoder().encode(raw).byteLength > integer(maxFrameBytes, 1))
|
|
207
|
+
throw new DeviceProtocolError("Frame exceeds negotiated capacity; check device and deployment frame limits");
|
|
208
|
+
let parsed;
|
|
209
|
+
try {
|
|
210
|
+
parsed = JSON.parse(raw);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
throw new DeviceProtocolError("Frame is not valid JSON");
|
|
214
|
+
}
|
|
215
|
+
const o = object(parsed);
|
|
216
|
+
if (o.type === "ready") {
|
|
217
|
+
if (from !== "kernel")
|
|
218
|
+
throw new DeviceProtocolError("Ready must come from kernel");
|
|
219
|
+
keys(o, ["type", "connectionId", "maxFrameBytes"]);
|
|
220
|
+
return freeze({
|
|
221
|
+
type: "ready",
|
|
222
|
+
connectionId: string(o.connectionId),
|
|
223
|
+
maxFrameBytes: integer(o.maxFrameBytes, 1024),
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
if (o.type === "req") {
|
|
227
|
+
if (from !== "kernel")
|
|
228
|
+
throw new DeviceProtocolError("Request must come from kernel");
|
|
229
|
+
return request(o);
|
|
230
|
+
}
|
|
231
|
+
if (o.type === "hello") {
|
|
232
|
+
if (from !== "device" || o.protocol !== 3)
|
|
233
|
+
throw new DeviceProtocolError("Expected protocol 3 hello");
|
|
234
|
+
const transport = object(o.transport);
|
|
235
|
+
keys(transport, ["maxFrameBytes"]);
|
|
236
|
+
const maxFrameBytes = integer(transport.maxFrameBytes, 1024);
|
|
237
|
+
const permissionSupport = support(o.permissionSupport);
|
|
238
|
+
if (o.instanceGeneration === null &&
|
|
239
|
+
(o.runtimeIdentity !== undefined ||
|
|
240
|
+
permissionSupport.restricted !== null ||
|
|
241
|
+
permissionSupport.fullAccess ||
|
|
242
|
+
permissionSupport.background ||
|
|
243
|
+
!permissionSupport.unavailableReason))
|
|
244
|
+
throw new DeviceProtocolError("Unbound hello cannot advertise runtime capability");
|
|
245
|
+
const base = parseDeviceFrame(raw, from);
|
|
246
|
+
if (base.type !== "hello")
|
|
247
|
+
throw new DeviceProtocolError("Expected hello");
|
|
248
|
+
let runtimeIdentity;
|
|
249
|
+
if (o.runtimeIdentity !== undefined) {
|
|
250
|
+
runtimeIdentity = identity(o.runtimeIdentity);
|
|
251
|
+
if (runtimeIdentity.instanceGeneration !== string(o.instanceGeneration))
|
|
252
|
+
throw new DeviceProtocolError("Runtime identity generation mismatch");
|
|
253
|
+
}
|
|
254
|
+
return freeze({
|
|
255
|
+
...base,
|
|
256
|
+
protocol: 3,
|
|
257
|
+
permissionSupport,
|
|
258
|
+
transport: { maxFrameBytes },
|
|
259
|
+
ownerCeiling: owner(o.ownerCeiling),
|
|
260
|
+
instanceGeneration: o.instanceGeneration === null ? null : string(o.instanceGeneration),
|
|
261
|
+
...(runtimeIdentity && { runtimeIdentity }),
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
if (o.type === "policy") {
|
|
265
|
+
if (from !== "device")
|
|
266
|
+
throw new DeviceProtocolError("Policy must come from device");
|
|
267
|
+
keys(o, ["type", "policy", "ownerCeiling"]);
|
|
268
|
+
const base = parseDeviceFrame(JSON.stringify({ type: "policy", policy: o.policy }), from);
|
|
269
|
+
if (base.type !== "policy")
|
|
270
|
+
throw new DeviceProtocolError("Expected policy");
|
|
271
|
+
return freeze({ ...base, ownerCeiling: owner(o.ownerCeiling) });
|
|
272
|
+
}
|
|
273
|
+
if (o.type === "res" && o.ok === false) {
|
|
274
|
+
if (from !== "device")
|
|
275
|
+
throw new DeviceProtocolError("Response must come from device");
|
|
276
|
+
keys(o, ["type", "id", "ok", "error"]);
|
|
277
|
+
const e = object(o.error);
|
|
278
|
+
keys(e, ["kind", "message"]);
|
|
279
|
+
if (!["gone", "fs_gone", "policy", "error", "denied", "unsupported", "unknown"].includes(String(e.kind)))
|
|
280
|
+
throw new DeviceProtocolError("Unknown outcome kind");
|
|
281
|
+
return freeze({
|
|
282
|
+
type: "res",
|
|
283
|
+
id: string(o.id),
|
|
284
|
+
ok: false,
|
|
285
|
+
error: {
|
|
286
|
+
kind: e.kind,
|
|
287
|
+
message: string(e.message, 8192),
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
const base = parseDeviceFrame(raw, from);
|
|
292
|
+
if (base.type === "hello" || base.type === "req" || base.type === "policy")
|
|
293
|
+
throw new DeviceProtocolError("Expected staged frame");
|
|
294
|
+
return freeze(base);
|
|
295
|
+
}
|
|
296
|
+
export function parseOperationResultV3(op, result, request) {
|
|
297
|
+
if (op === "bindInstance") {
|
|
298
|
+
if (!request || !("bindingId" in request))
|
|
299
|
+
throw new DeviceProtocolError("Binding validation requires the exact request");
|
|
300
|
+
const o = object(result);
|
|
301
|
+
keys(o, [
|
|
302
|
+
"instanceId",
|
|
303
|
+
"instanceGeneration",
|
|
304
|
+
"bindingId",
|
|
305
|
+
"runtimeIdentity",
|
|
306
|
+
"permissionSupport",
|
|
307
|
+
"ownerCeiling",
|
|
308
|
+
]);
|
|
309
|
+
const runtimeIdentity = identity(o.runtimeIdentity);
|
|
310
|
+
if (o.instanceId !== request.instanceId ||
|
|
311
|
+
o.instanceGeneration !== request.instanceGeneration ||
|
|
312
|
+
o.bindingId !== request.bindingId ||
|
|
313
|
+
runtimeIdentity.instanceGeneration !== request.instanceGeneration)
|
|
314
|
+
throw new DeviceProtocolError("Binding answer belongs to another instance generation");
|
|
315
|
+
return freeze({
|
|
316
|
+
instanceId: request.instanceId,
|
|
317
|
+
instanceGeneration: request.instanceGeneration,
|
|
318
|
+
bindingId: request.bindingId,
|
|
319
|
+
runtimeIdentity,
|
|
320
|
+
permissionSupport: support(o.permissionSupport),
|
|
321
|
+
ownerCeiling: owner(o.ownerCeiling),
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
if (op === "backgroundReadOutput") {
|
|
325
|
+
if (!request || !("offset" in request))
|
|
326
|
+
throw new DeviceProtocolError("Output validation requires the exact request bounds");
|
|
327
|
+
const offset = integer(request.offset), maxBytes = integer(request.maxBytes, 0, 65536);
|
|
328
|
+
const o = object(result);
|
|
329
|
+
if ("unavailable" in o) {
|
|
330
|
+
keys(o, ["unavailable"]);
|
|
331
|
+
if (o.unavailable !== "lost" && o.unavailable !== "expired")
|
|
332
|
+
throw new DeviceProtocolError("Invalid output outcome");
|
|
333
|
+
return freeze(o);
|
|
334
|
+
}
|
|
335
|
+
keys(o, ["dataB64", "offset", "nextOffset", "totalBytes"]);
|
|
336
|
+
const total = integer(o.totalBytes), next = integer(o.nextOffset);
|
|
337
|
+
if (integer(o.offset) !== offset ||
|
|
338
|
+
typeof o.dataB64 !== "string" ||
|
|
339
|
+
o.dataB64.length > 87384 ||
|
|
340
|
+
!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(o.dataB64))
|
|
341
|
+
throw new DeviceProtocolError("Invalid output range or base64");
|
|
342
|
+
const decoded = atob(o.dataB64);
|
|
343
|
+
if (btoa(decoded) !== o.dataB64 ||
|
|
344
|
+
decoded.length !== Math.min(maxBytes, Math.max(total - offset, 0)) ||
|
|
345
|
+
!Number.isSafeInteger(offset + decoded.length) ||
|
|
346
|
+
next !== offset + decoded.length)
|
|
347
|
+
throw new DeviceProtocolError("Inconsistent output range; retry from the same offset");
|
|
348
|
+
return freeze(o);
|
|
349
|
+
}
|
|
350
|
+
if (op === "backgroundStart")
|
|
351
|
+
return parseSandboxExecutionRef(result);
|
|
352
|
+
if (op === "backgroundStatus") {
|
|
353
|
+
const o = object(result);
|
|
354
|
+
keys(o, ["state", "exitCode", "timedOut"]);
|
|
355
|
+
if (o.state === "running" || o.state === "lost")
|
|
356
|
+
return freeze({ state: o.state });
|
|
357
|
+
if (o.state !== "exited" ||
|
|
358
|
+
(o.exitCode !== null && !Number.isInteger(o.exitCode)) ||
|
|
359
|
+
typeof o.timedOut !== "boolean")
|
|
360
|
+
throw new DeviceProtocolError("Invalid execution status");
|
|
361
|
+
return freeze(o);
|
|
362
|
+
}
|
|
363
|
+
if (op === "backgroundTail") {
|
|
364
|
+
if (result && typeof result === "object" && "unavailable" in result) {
|
|
365
|
+
const o = object(result);
|
|
366
|
+
keys(o, ["unavailable"]);
|
|
367
|
+
if (o.unavailable !== "lost" && o.unavailable !== "expired")
|
|
368
|
+
throw new DeviceProtocolError("Invalid output outcome");
|
|
369
|
+
return freeze(o);
|
|
370
|
+
}
|
|
371
|
+
return parseOpResult("readFile", result);
|
|
372
|
+
}
|
|
373
|
+
if (op === "backgroundStop") {
|
|
374
|
+
const o = object(result);
|
|
375
|
+
keys(o, ["outcome", "exitCode", "timedOut"]);
|
|
376
|
+
if (!["already-exited", "stopped", "cancel-pending", "lost"].includes(String(o.outcome)))
|
|
377
|
+
throw new DeviceProtocolError("Invalid stop outcome");
|
|
378
|
+
if (o.outcome === "already-exited" &&
|
|
379
|
+
((o.exitCode !== null && !Number.isInteger(o.exitCode)) || typeof o.timedOut !== "boolean"))
|
|
380
|
+
throw new DeviceProtocolError("Invalid stopped execution status");
|
|
381
|
+
return freeze(o);
|
|
382
|
+
}
|
|
383
|
+
if (op === "policy") {
|
|
384
|
+
const o = object(result);
|
|
385
|
+
keys(o, ["policy", "ownerCeiling"]);
|
|
386
|
+
return freeze({
|
|
387
|
+
...parseOpResult("policy", { policy: o.policy }),
|
|
388
|
+
ownerCeiling: owner(o.ownerCeiling),
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
if (op === "policyStatus") {
|
|
392
|
+
const o = object(result);
|
|
393
|
+
keys(o, ["outcome", "policy", "message", "ownerCeiling"]);
|
|
394
|
+
const { ownerCeiling, ...rest } = o;
|
|
395
|
+
const status = parseOpResult("policyStatus", rest);
|
|
396
|
+
if (status.outcome === "applied" && ownerCeiling === undefined)
|
|
397
|
+
throw new DeviceProtocolError("An applied policy status carries the owner report");
|
|
398
|
+
if (ownerCeiling !== undefined)
|
|
399
|
+
status.ownerCeiling = owner(ownerCeiling);
|
|
400
|
+
return freeze(status);
|
|
401
|
+
}
|
|
402
|
+
if (op === "revokeOperation") {
|
|
403
|
+
if (result !== null)
|
|
404
|
+
throw new DeviceProtocolError("Revoke result must be null");
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
const raw = op.startsWith("prepare") ? op.slice(7) : op.startsWith("browse") ? op.slice(6) : op;
|
|
408
|
+
return parseOpResult((raw.charAt(0).toLowerCase() + raw.slice(1)), result);
|
|
409
|
+
}
|