@ai-matrx/desktop-protocol 0.1.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 ADDED
@@ -0,0 +1,35 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ First release — the Matrx 2 phone slice, lane A.
6
+
7
+ - Protocol v1 schemas (zod 4), promoted from the accepted draft: the SPEC §4 envelope, the §5 error
8
+ model plus `PROTOCOL_VERSION_UNSUPPORTED`, `PROTOCOL_ERROR`, `UNKNOWN_OPERATION`,
9
+ `DEVICE_OFFLINE`, `PAYLOAD_TOO_LARGE` and a typed `reason`, and every `cap.op` of `sysinfo`,
10
+ `fs`, `exec` and `session`, with the `OPS` / `EVENTS` registries.
11
+ - `./frame` — zod-free 16-byte frame codec (`encodeFrame`, `decodeFrame`, `stampCid`, `readCid`).
12
+ - `./client` — `createDesktopClient`: hello, typed requests, credit-gated streams and STDIN,
13
+ transparent reattach with `since_seq`, heartbeat, relay reauth; reconnect via
14
+ `@ai-matrx/realtime`'s `createBackoff`.
15
+ - `./react` — `DesktopClientProvider`, `useDesktopConnection`, `useDesktopRequest`,
16
+ `useDesktopEvent`.
17
+ - Generated twins: `generated/desktop-protocol.schema.json` and
18
+ `aidream/_generated/desktop_protocol.py` (Pydantic, `StrictModel` base, `OPS`/`EVENTS`,
19
+ `validate_wire`), held identical by `pnpm generate --check`.
20
+
21
+ Defects found in the draft and fixed here (not preserved):
22
+
23
+ - **Case 19** — Pydantic accepted an explicit `null` on an optional, non-nullable field that zod
24
+ refuses. Fixed by the generated `StrictModel` base.
25
+ - **`DeviceId`** was `z.uuid()`, which generated `Annotated[UUID, Field(pattern=…)]` — a model
26
+ Pydantic can never validate, so `Welcome` and `SysinfoGetResult` were unusable in Python. Now a
27
+ plain uuid pattern; `generate` refuses `format` + `pattern` on one node.
28
+ - **Integral floats** — `2.0` is the integer 2 to zod (JSON.parse) but not to strict Pydantic.
29
+ `validate_wire` mirrors JavaScript.
30
+ - `.meta({ id })` wrote zod's process-global registry at import time; names now come from
31
+ `NAMED_SCHEMAS` into a private registry, and the lint bans `.meta`/`.describe`.
32
+
33
+ ### Consumer action
34
+
35
+ None — new package.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AI Matrix Engine
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # `@ai-matrx/desktop-protocol`
2
+
3
+ **The one wire contract between Matrx 2 (the desktop app) and everything that drives it** — the
4
+ phone console, the web app, the extension and the server. One zod source; a generated JSON Schema
5
+ and Pydantic twin that CI keeps byte-identical (SPEC P11: contracts are generated, never written
6
+ twice).
7
+
8
+ ```bash
9
+ pnpm add @ai-matrx/desktop-protocol
10
+ ```
11
+
12
+ | Entry | What it is |
13
+ |---|---|
14
+ | `@ai-matrx/desktop-protocol` | Schemas for the envelope and every `cap.op`, `OPS` / `EVENTS` registries, `ErrorCode`, close codes, `DesktopProtocolError`. Every schema name is also its inferred type. |
15
+ | `@ai-matrx/desktop-protocol/frame` | The 16-byte binary frame codec. Zod-free (the relay Worker's hot path). |
16
+ | `@ai-matrx/desktop-protocol/client` | `createDesktopClient` — reconnecting client with typed requests, credit-gated streams, transparent terminal reattach, heartbeat and relay reauth. |
17
+ | `@ai-matrx/desktop-protocol/react` | `DesktopClientProvider`, `useDesktopConnection`, `useDesktopRequest`, `useDesktopEvent`. |
18
+
19
+ ## Client
20
+
21
+ ```ts
22
+ import { createDesktopClient } from "@ai-matrx/desktop-protocol/client";
23
+
24
+ const client = createDesktopClient({
25
+ url: `wss://relay.matrxserver.com/v1/devices/${deviceId}/connect`,
26
+ // Re-read on every reconnect; tokens ride the subprotocol, never the URL.
27
+ protocols: async () => ["matrx.v1", `bearer.${await getAccessToken()}`],
28
+ getToken: getAccessToken, // answers relay.auth_expiring without dropping the socket
29
+ clientType: "web",
30
+ clientVersion: "1.0.0",
31
+ });
32
+
33
+ const listing = await client.request("fs.list", { path: "/Users/me" }); // typed by op name
34
+
35
+ const term = client.stream("exec.pty.start", { cols: 80, rows: 24 }, {
36
+ onData: (bytes) => new Promise((done) => xterm.write(bytes, done)), // credit follows consumption
37
+ onSnapshot: (bytes) => {
38
+ xterm.reset();
39
+ xterm.write(bytes);
40
+ },
41
+ });
42
+ term.write("ls\n");
43
+ ```
44
+
45
+ - **Backpressure:** credit for a stream is granted only after `onData` returns (or its promise
46
+ settles), so a slow phone never makes the relay or the Mac buffer without bound.
47
+ - **Reattach:** a resource stream (terminal, watch) survives a dropped socket. On reconnect the
48
+ client sends `session.attach` with `since_seq` = the last frame your `onData` consumed; the core
49
+ replays the gap or sends `gap` + a screen snapshot. The same `DesktopStream` keeps working.
50
+ - **Errors** are `DesktopProtocolError` with `code`, `retryable` and `data.reason` — branch on
51
+ those, never on messages. A dropped socket rejects unary work with `DEVICE_OFFLINE` (retryable).
52
+ - **Terminal closes** (`4010` revoked, `4003` forbidden, `4004` not found, `4009` replaced, `4426`
53
+ upgrade) end the client for good; everything else reconnects with the shared
54
+ `@ai-matrx/realtime` backoff (stability reset + jitter).
55
+ - Background problems (malformed messages, sequence gaps, heartbeat death, reconnect alarm) go to
56
+ `onDiagnostic` — loud by default (`console.warn`).
57
+
58
+ ## React
59
+
60
+ ```tsx
61
+ <DesktopClientProvider client={client}>
62
+ <Files />
63
+ </DesktopClientProvider>
64
+
65
+ function Files() {
66
+ const { status } = useDesktopConnection();
67
+ const { data, error, loading, refetch } = useDesktopRequest("fs.list", { path: "/Users/me" });
68
+ useDesktopEvent("fs.changed", () => void refetch());
69
+ }
70
+ ```
71
+
72
+ ## Python (aidream)
73
+
74
+ ```python
75
+ from aidream._generated.desktop_protocol import OPS, FsListParams, validate_wire
76
+
77
+ params = validate_wire(FsListParams, raw_json) # same verdict as zod, always
78
+ ```
79
+
80
+ `validate_wire` is the one Python entry point: strict (no `"2"` → 2) and JavaScript-faithful
81
+ (`2.0` is the integer 2). `OPS` maps every op to its params/result models, kind and errors.
82
+
83
+ ## Changing the protocol
84
+
85
+ Edit `src/schema/*.ts`, then `pnpm generate` and commit all three files. Rules (enforced by
86
+ `pnpm lint` and by `generate`): `z.strictObject` only; no `.refine`/`.transform`/`z.coerce`/
87
+ `.meta`; never optional and nullable at once; a new named schema goes into `NAMED_SCHEMAS`; add a
88
+ parity case to `corpus/parity-corpus.json` for any new wire shape.
89
+
90
+ MIT © AI Matrix Engine