Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 9b53b7b

Browse files
committed
chore(core): migrate client protocol to BARE (#1203)
Fixes KIT-247
1 parent 05e19f8 commit 9b53b7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1933
-860
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,4 @@ Cargo.lock
184184
**/.wrangler
185185
**/.DS_Store
186186
.aider*
187+
/packages/core/dist/schemas/

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"devDependencies": {
1515
"@biomejs/biome": "^2.1.1",
16+
"tsup": "^8.4.0",
1617
"@types/ws": "^8.5.14",
1718
"commander": "^14.0.0",
1819
"dedent": "^1.5.3",

packages/core/package.json

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,122 +24,122 @@
2424
"exports": {
2525
".": {
2626
"import": {
27-
"types": "./dist/mod.d.ts",
28-
"default": "./dist/mod.js"
27+
"types": "./dist/tsup/mod.d.ts",
28+
"default": "./dist/tsup/mod.js"
2929
},
3030
"require": {
31-
"types": "./dist/mod.d.cts",
32-
"default": "./dist/mod.cjs"
31+
"types": "./dist/tsup/mod.d.cts",
32+
"default": "./dist/tsup/mod.cjs"
3333
}
3434
},
3535
"./client": {
3636
"import": {
37-
"types": "./dist/client/mod.d.ts",
38-
"default": "./dist/client/mod.js"
37+
"types": "./dist/tsup/client/mod.d.ts",
38+
"default": "./dist/tsup/client/mod.js"
3939
},
4040
"require": {
41-
"types": "./dist/client/mod.d.cts",
42-
"default": "./dist/client/mod.cjs"
41+
"types": "./dist/tsup/client/mod.d.cts",
42+
"default": "./dist/tsup/client/mod.cjs"
4343
}
4444
},
4545
"./log": {
4646
"import": {
47-
"types": "./dist/common/log.d.ts",
48-
"default": "./dist/common/log.js"
47+
"types": "./dist/tsup/common/log.d.ts",
48+
"default": "./dist/tsup/common/log.js"
4949
},
5050
"require": {
51-
"types": "./dist/common/log.d.cts",
52-
"default": "./dist/common/log.cjs"
51+
"types": "./dist/tsup/common/log.d.cts",
52+
"default": "./dist/tsup/common/log.cjs"
5353
}
5454
},
5555
"./errors": {
5656
"import": {
57-
"types": "./dist/actor/errors.d.ts",
58-
"default": "./dist/actor/errors.js"
57+
"types": "./dist/tsup/actor/errors.d.ts",
58+
"default": "./dist/tsup/actor/errors.js"
5959
},
6060
"require": {
61-
"types": "./dist/actor/errors.d.cts",
62-
"default": "./dist/actor/errors.cjs"
61+
"types": "./dist/tsup/actor/errors.d.cts",
62+
"default": "./dist/tsup/actor/errors.cjs"
6363
}
6464
},
6565
"./utils": {
6666
"import": {
67-
"types": "./dist/utils.d.ts",
68-
"default": "./dist/utils.js"
67+
"types": "./dist/tsup/utils.d.ts",
68+
"default": "./dist/tsup/utils.js"
6969
},
7070
"require": {
71-
"types": "./dist/utils.d.cts",
72-
"default": "./dist/utils.cjs"
71+
"types": "./dist/tsup/utils.d.cts",
72+
"default": "./dist/tsup/utils.cjs"
7373
}
7474
},
7575
"./driver-helpers": {
7676
"import": {
77-
"types": "./dist/driver-helpers/mod.d.ts",
78-
"default": "./dist/driver-helpers/mod.js"
77+
"types": "./dist/tsup/driver-helpers/mod.d.ts",
78+
"default": "./dist/tsup/driver-helpers/mod.js"
7979
},
8080
"require": {
81-
"types": "./dist/driver-helpers/mod.d.cts",
82-
"default": "./dist/driver-helpers/mod.cjs"
81+
"types": "./dist/tsup/driver-helpers/mod.d.cts",
82+
"default": "./dist/tsup/driver-helpers/mod.cjs"
8383
}
8484
},
8585
"./driver-helpers/websocket": {
8686
"import": {
87-
"types": "./dist/common/websocket.d.ts",
88-
"default": "./dist/common/websocket.js"
87+
"types": "./dist/tsup/common/websocket.d.ts",
88+
"default": "./dist/tsup/common/websocket.js"
8989
},
9090
"require": {
91-
"types": "./dist/common/websocket.d.cts",
92-
"default": "./dist/common/websocket.cjs"
91+
"types": "./dist/tsup/common/websocket.d.cts",
92+
"default": "./dist/tsup/common/websocket.cjs"
9393
}
9494
},
9595
"./driver-test-suite": {
9696
"import": {
97-
"types": "./dist/driver-test-suite/mod.d.ts",
98-
"default": "./dist/driver-test-suite/mod.js"
97+
"types": "./dist/tsup/driver-test-suite/mod.d.ts",
98+
"default": "./dist/tsup/driver-test-suite/mod.js"
9999
},
100100
"require": {
101-
"types": "./dist/driver-test-suite/mod.d.cts",
102-
"default": "./dist/driver-test-suite/mod.cjs"
101+
"types": "./dist/tsup/driver-test-suite/mod.d.cts",
102+
"default": "./dist/tsup/driver-test-suite/mod.cjs"
103103
}
104104
},
105105
"./topologies/coordinate": {
106106
"import": {
107-
"types": "./dist/topologies/coordinate/mod.d.ts",
108-
"default": "./dist/topologies/coordinate/mod.js"
107+
"types": "./dist/tsup/topologies/coordinate/mod.d.ts",
108+
"default": "./dist/tsup/topologies/coordinate/mod.js"
109109
},
110110
"require": {
111-
"types": "./dist/topologies/coordinate/mod.d.cts",
112-
"default": "./dist/topologies/coordinate/mod.cjs"
111+
"types": "./dist/tsup/topologies/coordinate/mod.d.cts",
112+
"default": "./dist/tsup/topologies/coordinate/mod.cjs"
113113
}
114114
},
115115
"./topologies/partition": {
116116
"import": {
117-
"types": "./dist/topologies/partition/mod.d.ts",
118-
"default": "./dist/topologies/partition/mod.js"
117+
"types": "./dist/tsup/topologies/partition/mod.d.ts",
118+
"default": "./dist/tsup/topologies/partition/mod.js"
119119
},
120120
"require": {
121-
"types": "./dist/topologies/partition/mod.d.cts",
122-
"default": "./dist/topologies/partition/mod.cjs"
121+
"types": "./dist/tsup/topologies/partition/mod.d.cts",
122+
"default": "./dist/tsup/topologies/partition/mod.cjs"
123123
}
124124
},
125125
"./test": {
126126
"import": {
127-
"types": "./dist/test/mod.d.ts",
128-
"default": "./dist/test/mod.js"
127+
"types": "./dist/tsup/test/mod.d.ts",
128+
"default": "./dist/tsup/test/mod.js"
129129
},
130130
"require": {
131-
"types": "./dist/test/mod.d.cts",
132-
"default": "./dist/test/mod.cjs"
131+
"types": "./dist/tsup/test/mod.d.cts",
132+
"default": "./dist/tsup/test/mod.cjs"
133133
}
134134
},
135135
"./inspector": {
136136
"import": {
137-
"types": "./dist/inspector/mod.d.ts",
138-
"default": "./dist/inspector/mod.js"
137+
"types": "./dist/tsup/inspector/mod.d.ts",
138+
"default": "./dist/tsup/inspector/mod.js"
139139
},
140140
"require": {
141-
"types": "./dist/inspector/mod.d.cts",
142-
"default": "./dist/inspector/mod.cjs"
141+
"types": "./dist/tsup/inspector/mod.d.cts",
142+
"default": "./dist/tsup/inspector/mod.cjs"
143143
}
144144
}
145145
},
@@ -150,16 +150,18 @@
150150
"scripts": {
151151
"dev": "pnpm build --watch",
152152
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/common/websocket.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/test/mod.ts src/inspector/mod.ts",
153+
"build:schema": "node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/client-protocol/v1.bare -o dist/schemas/client-protocol/v1.ts && node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/file-system-driver/v1.bare -o dist/schemas/file-system-driver/v1.ts ",
153154
"check-types": "tsc --noEmit",
154-
"boop": "tsc --outDir dist/test -d",
155155
"test": "vitest run",
156156
"test:watch": "vitest",
157157
"dump-openapi": "tsx scripts/dump-openapi.ts"
158158
},
159159
"dependencies": {
160+
"@bare-ts/lib": "~0.3.0",
160161
"@hono/standard-validator": "^0.1.3",
161162
"@hono/zod-openapi": "^0.19.10",
162163
"@rivetkit/fast-json-patch": "^3.1.2",
164+
"@rivetkit/versioned-data-util": "workspace:*",
163165
"cbor-x": "^1.6.0",
164166
"hono": "^4.7.0",
165167
"invariant": "^2.2.4",
@@ -173,6 +175,7 @@
173175
"@hono/node-server": "^1.18.2",
174176
"@hono/node-ws": "^1.1.1",
175177
"@rivet-gg/actor-core": "^25.1.0",
178+
"@rivetkit/bare-compiler": "workspace:*",
176179
"@types/invariant": "^2",
177180
"@types/node": "^22.13.1",
178181
"@types/ws": "^8",
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# MARK: Message To Client
2+
type Init struct {
3+
actorId: str
4+
connectionId: str
5+
connectionToken: str
6+
}
7+
8+
type Error struct {
9+
code: str
10+
message: str
11+
metadata: optional<data>
12+
actionId: optional<uint>
13+
}
14+
15+
type ActionResponse struct {
16+
id: uint
17+
output: data
18+
}
19+
20+
type Event struct {
21+
name: str
22+
# CBOR array
23+
args: data
24+
}
25+
26+
type ToClientBody union {
27+
Init |
28+
Error |
29+
ActionResponse |
30+
Event
31+
}
32+
33+
type ToClient struct {
34+
body: ToClientBody
35+
}
36+
37+
# MARK: Message To Server
38+
type ActionRequest struct {
39+
id: uint
40+
name: str
41+
# CBOR array
42+
args: data
43+
}
44+
45+
type SubscriptionRequest struct {
46+
eventName: str
47+
subscribe: bool
48+
}
49+
50+
type ToServerBody union {
51+
ActionRequest |
52+
SubscriptionRequest
53+
}
54+
55+
type ToServer struct {
56+
body: ToServerBody
57+
}
58+
59+
# MARK: HTTP Action
60+
type HttpActionRequest struct {
61+
# CBOR array
62+
args: data
63+
}
64+
65+
type HttpActionResponse struct {
66+
output: data
67+
}
68+
69+
# MARK: HTTP Error
70+
type HttpResponseError struct {
71+
code: str
72+
message: str
73+
metadata: optional<data>
74+
}
75+
76+
# MARK: HTTP Resolve
77+
type HttpResolveRequest void
78+
79+
type HttpResolveResponse struct {
80+
actorId: str
81+
}
82+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# File System Driver Schema (v1)
2+
3+
# MARK: Actor State
4+
# Represents the persisted state for an actor on disk.
5+
# Note: createdAt is not persisted; it is derived from the file's birthtime.
6+
type ActorState struct {
7+
id: str
8+
name: str
9+
key: list<str>
10+
persistedData: data
11+
}
12+
13+
# MARK: Actor Alarm
14+
# Represents a scheduled alarm for an actor.
15+
# Stored per-actor; the actor id is implied by the filename.
16+
# The timestamp is milliseconds since epoch.
17+
type ActorAlarm struct {
18+
timestamp: uint
19+
}
20+

packages/core/src/actor/connection.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import type * as messageToClient from "@/actor/protocol/message/to-client";
2-
import type * as wsToClient from "@/actor/protocol/message/to-client";
1+
import * as cbor from "cbor-x";
2+
import type * as protocol from "@/schemas/client-protocol/mod";
3+
import { TO_CLIENT_VERSIONED } from "@/schemas/client-protocol/versioned";
4+
import { bufferToArrayBuffer } from "@/utils";
35
import type { AnyDatabaseProvider } from "./database";
46
import { type ConnDriver, ConnectionReadyState } from "./driver";
57
import * as errors from "./errors";
@@ -162,7 +164,7 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
162164
*
163165
* @protected
164166
*/
165-
public _sendMessage(message: CachedSerializer<messageToClient.ToClient>) {
167+
public _sendMessage(message: CachedSerializer<protocol.ToClient>) {
166168
this.#driver.sendMessage?.(this.#actor, this, this.__persist.ds, message);
167169
}
168170

@@ -181,14 +183,18 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
181183
connId: this.id,
182184
});
183185
this._sendMessage(
184-
new CachedSerializer<wsToClient.ToClient>({
185-
b: {
186-
ev: {
187-
n: eventName,
188-
a: args,
186+
new CachedSerializer<protocol.ToClient>(
187+
{
188+
body: {
189+
tag: "Event",
190+
val: {
191+
name: eventName,
192+
args: bufferToArrayBuffer(cbor.encode(args)),
193+
},
189194
},
190195
},
191-
}),
196+
TO_CLIENT_VERSIONED,
197+
),
192198
);
193199
}
194200

packages/core/src/actor/driver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type * as messageToClient from "@/actor/protocol/message/to-client";
21
import type { CachedSerializer } from "@/actor/protocol/serde";
32
import type { AnyClient } from "@/client/client";
43
import type { ManagerDriver } from "@/manager/driver";
54
import type { RegistryConfig } from "@/registry/config";
65
import type { RunConfig } from "@/registry/run-config";
6+
import type * as protocol from "@/schemas/client-protocol/mod";
77
import type { AnyConn, ConnectionDriver } from "./connection";
88
import type { GenericConnGlobalState } from "./generic-conn-driver";
99
import type { AnyActorInstance } from "./instance";
@@ -42,7 +42,7 @@ export interface ActorDriver {
4242
*/
4343
getDatabase(actorId: string): Promise<unknown | undefined>;
4444

45-
sleep?(actorId: string): void;
45+
sleep?(actorId: string): Promise<void>;
4646

4747
shutdown?(immediate: boolean): Promise<void>;
4848
}
@@ -60,7 +60,7 @@ export interface ConnDriver<ConnDriverState = unknown> {
6060
actor: AnyActorInstance,
6161
conn: AnyConn,
6262
state: ConnDriverState,
63-
message: CachedSerializer<messageToClient.ToClient>,
63+
message: CachedSerializer<protocol.ToClient>,
6464
): void;
6565

6666
/**

0 commit comments

Comments
 (0)