dp-worker 0.1.4 → 0.1.5

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 (35) hide show
  1. package/dist/browser/chrome-process.js +7 -6
  2. package/dist/browser/chrome-process.js.map +1 -1
  3. package/dist/chrome-extension/omni-flow/background.js +24 -6
  4. package/node_modules/dp-shared/dist/configs-contracts.d.ts +5 -0
  5. package/node_modules/dp-shared/dist/configs-contracts.d.ts.map +1 -1
  6. package/node_modules/dp-shared/dist/configs-contracts.js +5 -0
  7. package/node_modules/dp-shared/dist/configs-contracts.js.map +1 -1
  8. package/node_modules/dp-shared/dist/db-rows.d.ts +1 -1
  9. package/node_modules/dp-shared/dist/db-rows.d.ts.map +1 -1
  10. package/node_modules/dp-shared/dist/db-rows.js +1 -1
  11. package/node_modules/dp-shared/dist/db-rows.js.map +1 -1
  12. package/node_modules/dp-shared/dist/identities.d.ts +2 -0
  13. package/node_modules/dp-shared/dist/identities.d.ts.map +1 -1
  14. package/node_modules/dp-shared/dist/identities.js +2 -0
  15. package/node_modules/dp-shared/dist/identities.js.map +1 -1
  16. package/node_modules/dp-shared/dist/task-history-contracts.d.ts +6 -0
  17. package/node_modules/dp-shared/dist/task-history-contracts.d.ts.map +1 -1
  18. package/node_modules/dp-shared/dist/task-history-contracts.js +3 -0
  19. package/node_modules/dp-shared/dist/task-history-contracts.js.map +1 -1
  20. package/node_modules/dp-shared/dist/worker-detail-contracts.d.ts +2 -0
  21. package/node_modules/dp-shared/dist/worker-detail-contracts.d.ts.map +1 -1
  22. package/package.json +1 -1
  23. package/node_modules/dp-shared/src/admin-broadcast.ts +0 -38
  24. package/node_modules/dp-shared/src/audit-contracts.ts +0 -33
  25. package/node_modules/dp-shared/src/configs-contracts.ts +0 -206
  26. package/node_modules/dp-shared/src/db-rows.ts +0 -280
  27. package/node_modules/dp-shared/src/deploy-contracts.ts +0 -38
  28. package/node_modules/dp-shared/src/domain.ts +0 -124
  29. package/node_modules/dp-shared/src/identities.ts +0 -36
  30. package/node_modules/dp-shared/src/index.ts +0 -25
  31. package/node_modules/dp-shared/src/omni-flow-session.ts +0 -80
  32. package/node_modules/dp-shared/src/rest-contracts.ts +0 -409
  33. package/node_modules/dp-shared/src/task-history-contracts.ts +0 -115
  34. package/node_modules/dp-shared/src/worker-detail-contracts.ts +0 -230
  35. package/node_modules/dp-shared/src/ws-protocol.ts +0 -435
@@ -1,230 +0,0 @@
1
- import { z } from "zod/v4";
2
- import {
3
- ConfigEtag,
4
- ContentHash,
5
- EffectiveWorkerStatus,
6
- EffectiveDirectorStatus,
7
- IanaTimezone,
8
- ProcessStatus,
9
- ProxyProtocol,
10
- RuntimeStatus,
11
- SocialPlatform,
12
- Uuid,
13
- } from "./domain.js";
14
- import { IdentityRow } from "./identities.js";
15
-
16
- /**
17
- * REST contracts for the Web-UI /workers/:id detail surface (wave 1 Agent B).
18
- *
19
- * Split out from rest-contracts.ts to keep the existing wire surface stable
20
- * while the dp-worker detail tabs land; everything here is browser-facing and
21
- * never reaches the dp-worker or dp-director.
22
- */
23
-
24
- /* ============================================================================
25
- * dp-worker create / detail
26
- * ============================================================================ */
27
-
28
- /**
29
- * Body for POST /api/workers. The api_key may be supplied by the caller (64
30
- * lowercase hex chars — 32 crypto-random bytes) or omitted to have the
31
- * server generate one. The column is stored plaintext per v2.13, so the
32
- * operator can read or reset it later from the dp-worker detail surface.
33
- */
34
- export const WorkerCreateRequestSchema = z.object({
35
- name: z.string().min(1).max(120),
36
- director_id: Uuid.optional(),
37
- llm_profile_id: Uuid,
38
- proxy_id: Uuid.optional(),
39
- identity_id: Uuid.nullable().optional(),
40
- /** Pre-generated 64-char hex api_key from the UI. Server generates one if absent. */
41
- api_key: z
42
- .string()
43
- .regex(/^[0-9a-f]{64}$/, "api_key must be 64 lowercase hex chars")
44
- .optional(),
45
- telegram_bot_token: z.string().min(1).optional(),
46
- telegram_allowed_users: z.array(z.number().int()).optional(),
47
- headless: z.boolean().optional(),
48
- });
49
- export type WorkerCreateRequest = z.infer<typeof WorkerCreateRequestSchema>;
50
-
51
- /** The plaintext api_key wire shape returned after create. */
52
- export const WorkerCreateResponseSchema = z.object({
53
- id: Uuid,
54
- name: z.string(),
55
- api_key: z.string().min(1),
56
- config_etag: ConfigEtag,
57
- });
58
- export type WorkerCreateResponse = z.infer<typeof WorkerCreateResponseSchema>;
59
-
60
- const ProxyJoinSchema = z.object({
61
- id: Uuid,
62
- label: z.string(),
63
- protocol: ProxyProtocol,
64
- host: z.string(),
65
- port: z.number().int().positive(),
66
- username: z.string(),
67
- timezone: IanaTimezone,
68
- });
69
-
70
- const LlmProfileJoinSchema = z.object({
71
- id: Uuid,
72
- name: z.string(),
73
- model: z.string(),
74
- });
75
-
76
- const DirectorJoinSchema = z.object({
77
- id: Uuid,
78
- name: z.string(),
79
- host_label: z.string(),
80
- status: EffectiveDirectorStatus,
81
- });
82
-
83
- const SessionHeadSchema = z.object({
84
- id: Uuid,
85
- content_hash: ContentHash,
86
- size_bytes: z.number().int().positive(),
87
- uploaded_at: z.string().datetime(),
88
- uploaded_by: Uuid,
89
- r2_object_key: z.string(),
90
- });
91
-
92
- export const WorkerDetailResponseSchema = z.object({
93
- id: Uuid,
94
- name: z.string(),
95
- director_id: Uuid.optional(),
96
- director: DirectorJoinSchema.optional(),
97
- status: EffectiveWorkerStatus,
98
- process_status: ProcessStatus.optional(),
99
- runtime_status: RuntimeStatus.optional(),
100
- process_observed_at: z.string().datetime().optional(),
101
- runtime_observed_at: z.string().datetime().optional(),
102
- last_heartbeat_at: z.string().datetime().optional(),
103
- effective_status: EffectiveWorkerStatus.optional(),
104
- current_task_id: Uuid.optional(),
105
- config_etag: ConfigEtag,
106
- headless: z.boolean(),
107
- identity_id: Uuid.nullable().optional(),
108
- telegram_bot_token: z.string().nullable(),
109
- telegram_allowed_users: z.array(z.number().int()).optional(),
110
- created_at: z.string().datetime(),
111
- updated_at: z.string().datetime(),
112
- proxy: ProxyJoinSchema.optional(),
113
- llm_profile: LlmProfileJoinSchema,
114
- social_accounts_count: z.number().int().nonnegative(),
115
- scheduled_tasks_count: z.number().int().nonnegative(),
116
- current_session: SessionHeadSchema.optional(),
117
- /**
118
- * Plaintext dp-worker api_key. Surfaced read-only on the detail page so the
119
- * operator can copy it into an dp-director spawn command or re-paste during
120
- * fleet migration. Per v2.13 (internal-use stance,
121
- * feedback_security_simple memory) the key is stored plaintext in
122
- * `dp_workers.api_key`, so re-displaying it carries no
123
- * additional disclosure risk.
124
- */
125
- api_key: z.string().min(1),
126
- /**
127
- * Full identity row when the dp-worker is bound to an identity; null otherwise.
128
- * Populated via LEFT JOIN in getWorkerDetail (M3.T2).
129
- */
130
- identity: IdentityRow.nullable().optional(),
131
- });
132
- export type WorkerDetailResponse = z.infer<typeof WorkerDetailResponseSchema>;
133
-
134
- /* ============================================================================
135
- * Social accounts CRUD
136
- * ============================================================================ */
137
-
138
- /**
139
- * Sub-account model:
140
- * - One row per (identity, platform) — enforced by UNIQUE.
141
- * - UI restricts to platform ∈ {x, youtube, reddit}; the DB enum stays
142
- * wider for backward compat but the API rejects others.
143
- * - login_type='password' carries email + optional username (handle) +
144
- * password; login_type='oauth' carries no secrets (assumed pre-logged-in
145
- * in the Chrome profile).
146
- * - persona_text=null means "inherit from identity.persona_text". The
147
- * bootstrap composer resolves this before handing config to dp-worker.
148
- */
149
- const SubAccountPlatform = z.enum(["x", "youtube", "reddit"]);
150
-
151
- export const SocialAccountBodySchema = z.object({
152
- platform: SubAccountPlatform,
153
- profile_name: z.string().min(1).max(120),
154
- login_type: z.enum(["password", "oauth"]),
155
- username: z.string().min(1).max(120).optional(),
156
- email: z.string().email().optional(),
157
- password: z.string().min(1).optional(),
158
- /** Omit / null → inherit identity persona. */
159
- persona_text: z.string().max(4000).nullable().optional(),
160
- persona_other_json: z.record(z.string(), z.unknown()).optional(),
161
- enabled: z.boolean().default(true),
162
- });
163
- export type SocialAccountBody = z.infer<typeof SocialAccountBodySchema>;
164
-
165
- export const SocialAccountPatchSchema = z
166
- .object({
167
- profile_name: z.string().min(1).max(120).optional(),
168
- login_type: z.enum(["password", "oauth"]).optional(),
169
- username: z.string().min(1).max(120).nullable().optional(),
170
- email: z.string().email().nullable().optional(),
171
- password: z.string().min(1).nullable().optional(),
172
- persona_text: z.string().max(4000).nullable().optional(),
173
- persona_other_json: z.record(z.string(), z.unknown()).nullable().optional(),
174
- enabled: z.boolean().optional(),
175
- })
176
- .refine((v) => Object.keys(v).length > 0, {
177
- message: "patch body must contain at least one field",
178
- });
179
- export type SocialAccountPatch = z.infer<typeof SocialAccountPatchSchema>;
180
-
181
- export const SocialAccountResponseSchema = z.object({
182
- id: Uuid,
183
- identity_id: Uuid,
184
- platform: SocialPlatform,
185
- profile_name: z.string(),
186
- login_type: z.enum(["password", "oauth"]),
187
- username: z.string().nullable(),
188
- email: z.string().nullable(),
189
- password: z.string().nullable(),
190
- persona_text: z.string().nullable(),
191
- persona_other_json: z.record(z.string(), z.unknown()).optional(),
192
- enabled: z.boolean(),
193
- created_at: z.string().datetime(),
194
- updated_at: z.string().datetime(),
195
- });
196
- export type SocialAccountResponse = z.infer<typeof SocialAccountResponseSchema>;
197
-
198
- /* ============================================================================
199
- * Chrome sessions history list
200
- *
201
- * GET /api/workers/:id/sessions returns past chrome_sessions newest-first with
202
- * opaque cursor pagination, mirroring the `task-history.ts` shape. The
203
- * `is_current` flag is true for the single row whose content_hash matches
204
- * `dp_workers.session_content_hash` — the SessionTab uses it to render
205
- * a "current" badge without a second round-trip to /api/workers/:id detail.
206
- *
207
- * Named `ChromeSessionListRow*` to avoid a name collision with the
208
- * existing `ChromeSessionRow` postgres-row schema in `db-rows.ts` — they
209
- * describe different surfaces (DB row vs. browser-facing list row + flag).
210
- * ============================================================================ */
211
-
212
- export const ChromeSessionListRowSchema = z.object({
213
- id: Uuid,
214
- content_hash: ContentHash,
215
- size_bytes: z.number().int().positive(),
216
- uploaded_at: z.string().datetime(),
217
- uploaded_by: Uuid,
218
- r2_object_key: z.string(),
219
- note: z.string().optional(),
220
- is_current: z.boolean(),
221
- });
222
- export type ChromeSessionListRow = z.infer<typeof ChromeSessionListRowSchema>;
223
-
224
- export const ListChromeSessionsResponseSchema = z.object({
225
- items: z.array(ChromeSessionListRowSchema),
226
- next_cursor: z.string().nullable(),
227
- });
228
- export type ListChromeSessionsResponse = z.infer<
229
- typeof ListChromeSessionsResponseSchema
230
- >;
@@ -1,435 +0,0 @@
1
- import { z } from "zod/v4";
2
- import {
3
- ConfigEtag,
4
- DpWorkerRuntimeMode,
5
- DpWorkerUpgradeStatus,
6
- RuntimeStatus,
7
- ShortId,
8
- TaskRunStatus,
9
- Uuid,
10
- } from "./domain.js";
11
-
12
- /**
13
- * WebSocket message envelopes.
14
- *
15
- * Every message that crosses the wire has the same outer shape; the inner
16
- * `type` discriminates the payload. Senders that need a reply set `id`;
17
- * receivers reply with `ack { ref_id: <that id> }`.
18
- *
19
- * See docs/architecture.md §Communication protocols → WebSocket for the
20
- * authoritative list of message types per channel.
21
- */
22
-
23
- const Envelope = <T extends z.ZodType>(type: string, payload: T) =>
24
- z.object({
25
- /** Sender-assigned message id (typically uuid). Required when an ack is expected. */
26
- id: z.string().min(1).optional(),
27
- type: z.literal(type),
28
- /** Unix ms at send time. */
29
- ts: z.number().int().positive(),
30
- payload,
31
- });
32
-
33
- /* ============================================================================
34
- * Generic envelope (every channel)
35
- * ============================================================================ */
36
-
37
- export const AckMessage = Envelope(
38
- "ack",
39
- z.object({
40
- /** id of the message being acknowledged. */
41
- ref_id: z.string().min(1),
42
- /** Optional outcome — for "dispatched_ack" style acks the dp-worker reports back its current state. */
43
- status: z.string().optional(),
44
- error: z.string().optional(),
45
- })
46
- );
47
-
48
- export const PingRequestMessage = Envelope(
49
- "ping_request",
50
- z.object({
51
- ref_id: z.string().min(1),
52
- })
53
- );
54
-
55
- export const PongMessage = Envelope(
56
- "pong",
57
- z.object({
58
- ref_id: z.string().min(1),
59
- status: RuntimeStatus.optional(),
60
- current_task_id: Uuid.optional(),
61
- chrome_uptime_ms: z.number().int().nonnegative().optional(),
62
- })
63
- );
64
-
65
- /* ============================================================================
66
- * center ↔ dp-worker
67
- * ============================================================================ */
68
-
69
- export const TaskDispatchMessage = Envelope(
70
- "task_dispatch",
71
- z.object({
72
- task_run_id: Uuid,
73
- prompt: z.string().min(1),
74
- randomized_delay_ms: z.number().int().nonnegative().default(0),
75
- deadline_ms: z.number().int().positive().optional(),
76
- attachments: z
77
- .array(
78
- z.object({
79
- r2_object_key: z.string().min(1),
80
- filename: z.string().min(1),
81
- mime: z.string().optional(),
82
- })
83
- )
84
- .optional(),
85
- })
86
- );
87
-
88
- export const ChatUserMessage = Envelope(
89
- "chat_user_message",
90
- z.object({
91
- chat_id: Uuid,
92
- message_id: Uuid,
93
- content: z.union([
94
- z.string(),
95
- z.array(
96
- z.object({
97
- type: z.string(),
98
- text: z.string().optional(),
99
- attachment_id: Uuid.optional(),
100
- filename: z.string().optional(),
101
- mime: z.string().optional(),
102
- size_bytes: z.number().int().positive().optional(),
103
- url: z.string().url().optional(),
104
- })
105
- ),
106
- ]),
107
- })
108
- );
109
-
110
- export const ConfigInvalidateMessage = Envelope(
111
- "config_invalidate",
112
- z.object({
113
- which: z.array(z.enum(["llm", "proxy", "persona", "session", "telegram", "social_accounts"])),
114
- new_config_etag: ConfigEtag,
115
- })
116
- );
117
-
118
- export const CancelTaskMessage = Envelope(
119
- "cancel_task",
120
- z.object({
121
- task_run_id: Uuid,
122
- reason: z.string().optional(),
123
- })
124
- );
125
-
126
- export const WorkerHelloMessage = Envelope(
127
- "hello",
128
- z.object({
129
- worker_id: Uuid,
130
- version: z.string().min(1),
131
- config_etag: ConfigEtag,
132
- current_status: RuntimeStatus,
133
- current_task_id: Uuid.optional(),
134
- })
135
- );
136
-
137
- export const StatusChangeMessage = Envelope(
138
- "status_change",
139
- z.object({
140
- status: RuntimeStatus,
141
- current_task_id: Uuid.optional(),
142
- chrome_uptime_ms: z.number().int().nonnegative().optional(),
143
- reason: z.string().optional(),
144
- })
145
- );
146
-
147
- /** Periodic 30s liveness ping from dp-worker to center. Empty payload —
148
- * the envelope's arrival is the signal. */
149
- export const WorkerHeartbeatMessage = Envelope(
150
- "worker_heartbeat",
151
- z.object({}),
152
- );
153
-
154
- export const TaskEventMessage = Envelope(
155
- "task_event",
156
- z.object({
157
- task_run_id: Uuid,
158
- event: z.enum(["dispatched_ack", "started", "step_log", "finished"]),
159
- status: TaskRunStatus.optional(),
160
- error: z.string().optional(),
161
- result_summary: z.string().optional(),
162
- step_index: z.number().int().nonnegative().optional(),
163
- step_log: z.string().optional(),
164
- })
165
- );
166
-
167
- export const ChatMessageChunkMessage = Envelope(
168
- "chat_message_chunk",
169
- z.object({
170
- chat_id: Uuid,
171
- message_id: Uuid,
172
- delta: z.string(),
173
- done: z.boolean(),
174
- })
175
- );
176
-
177
- export const ChatMessageFinalizedMessage = Envelope(
178
- "chat_message_finalized",
179
- z.object({
180
- chat_id: Uuid,
181
- message_id: Uuid,
182
- role: z.string(),
183
- content: z.string(),
184
- tool_traces: z.array(z.record(z.string(), z.unknown())).optional(),
185
- })
186
- );
187
-
188
- /** Returned by dp-worker when a chat arrives while BrowserLock is held by a task. */
189
- export const ChatMessageQueuedMessage = Envelope(
190
- "chat_message_queued",
191
- z.object({
192
- chat_id: Uuid,
193
- message_id: Uuid,
194
- queue_position: z.number().int().nonnegative(),
195
- })
196
- );
197
-
198
- export const AttachmentUploadRequestMessage = Envelope(
199
- "attachment_upload",
200
- z.object({
201
- request_id: ShortId,
202
- filename: z.string().min(1),
203
- mime: z.string().optional(),
204
- size_bytes: z.number().int().positive(),
205
- })
206
- );
207
-
208
- export const ErrorReportMessage = Envelope(
209
- "error_report",
210
- z.object({
211
- code: z.string().min(1),
212
- message: z.string().min(1),
213
- stack: z.string().optional(),
214
- })
215
- );
216
-
217
- /* ----- Payload type aliases ----- *
218
- * TypeScript's `Extract<UnionType, { type: "x" }>` is fragile on inferred
219
- * discriminated unions whose variants share structural shape (all wrap
220
- * `{ id?, type, ts, payload }`). Re-exporting payload types directly per
221
- * message keeps consumers from having to fight that pattern.
222
- */
223
- export type TaskDispatchPayload = z.infer<typeof TaskDispatchMessage>["payload"];
224
- export type ChatUserMessagePayload = z.infer<typeof ChatUserMessage>["payload"];
225
- export type ConfigInvalidatePayload = z.infer<typeof ConfigInvalidateMessage>["payload"];
226
- export type CancelTaskPayload = z.infer<typeof CancelTaskMessage>["payload"];
227
- export type PingRequestPayload = z.infer<typeof PingRequestMessage>["payload"];
228
- export type PongPayload = z.infer<typeof PongMessage>["payload"];
229
- export type WorkerHelloPayload = z.infer<typeof WorkerHelloMessage>["payload"];
230
- export type StatusChangePayload = z.infer<typeof StatusChangeMessage>["payload"];
231
- export type TaskEventPayload = z.infer<typeof TaskEventMessage>["payload"];
232
- export type ChatMessageChunkPayload = z.infer<typeof ChatMessageChunkMessage>["payload"];
233
- export type ChatMessageFinalizedPayload = z.infer<typeof ChatMessageFinalizedMessage>["payload"];
234
- export type ChatMessageQueuedPayload = z.infer<typeof ChatMessageQueuedMessage>["payload"];
235
- export type WorkerHeartbeatPayload = z.infer<typeof WorkerHeartbeatMessage>["payload"];
236
-
237
- /** Union of every message that may travel on a center↔dp-worker WebSocket. */
238
- export const CenterWorkerWsMessage = z.discriminatedUnion("type", [
239
- AckMessage,
240
- PingRequestMessage,
241
- PongMessage,
242
- // center → dp-worker
243
- TaskDispatchMessage,
244
- ChatUserMessage,
245
- ConfigInvalidateMessage,
246
- CancelTaskMessage,
247
- // dp-worker → center
248
- WorkerHelloMessage,
249
- StatusChangeMessage,
250
- WorkerHeartbeatMessage,
251
- TaskEventMessage,
252
- ChatMessageChunkMessage,
253
- ChatMessageFinalizedMessage,
254
- ChatMessageQueuedMessage,
255
- AttachmentUploadRequestMessage,
256
- ErrorReportMessage,
257
- ]);
258
- export type CenterWorkerWsMessage = z.infer<typeof CenterWorkerWsMessage>;
259
-
260
- /* ============================================================================
261
- * center ↔ dp-director
262
- * ============================================================================ */
263
-
264
- export const SpawnWorkerMessage = Envelope(
265
- "spawn_worker",
266
- z.object({
267
- worker_id: Uuid,
268
- api_key: z.string().min(1),
269
- center_url: z.string().url(),
270
- version: z.string().optional(),
271
- workspace_path: z.string().optional(),
272
- })
273
- );
274
-
275
- export const StopWorkerMessage = Envelope(
276
- "stop_worker",
277
- z.object({
278
- worker_id: Uuid,
279
- graceful: z.boolean().default(true),
280
- })
281
- );
282
-
283
- export const RestartWorkerMessage = Envelope(
284
- "restart_worker",
285
- z.object({
286
- worker_id: Uuid,
287
- })
288
- );
289
-
290
- export const UpgradeDpWorkerMessage = Envelope(
291
- "upgrade_dp_worker",
292
- z.object({
293
- request_id: z.string().min(1),
294
- })
295
- );
296
-
297
- export const QueryWorkerStatusMessage = Envelope(
298
- "query_worker_status",
299
- z.object({
300
- worker_ids: z.array(Uuid).min(1),
301
- })
302
- );
303
-
304
- export const ReconcileWorkersMessage = Envelope(
305
- "reconcile_workers",
306
- z.object({
307
- generation: z.string().min(1),
308
- workers: z.array(
309
- z.object({
310
- worker_id: Uuid,
311
- api_key: z.string().min(1),
312
- center_url: z.string().url(),
313
- workspace_path: z.string().optional(),
314
- })
315
- ),
316
- stop_unlisted: z.boolean().default(true),
317
- })
318
- );
319
-
320
- export const DirectorHelloMessage = Envelope(
321
- "hello",
322
- z.object({
323
- director_id: Uuid,
324
- host_label: z.string().min(1).optional(),
325
- version: z.string().min(1),
326
- worker_pids: z.record(Uuid, z.number().int().positive()),
327
- capabilities: z.array(z.string()).default([]),
328
- dp_worker_runtime_mode: DpWorkerRuntimeMode.optional(),
329
- dp_worker_version: z.string().min(1).optional(),
330
- dp_worker_latest_version: z.string().min(1).optional(),
331
- dp_worker_upgrade_status: DpWorkerUpgradeStatus.optional(),
332
- })
333
- );
334
-
335
- /**
336
- * Sent by DirectorConnection DO to the dp-director immediately after the WS upgrade so
337
- * the dp-director learns its own canonical director_id (the DO resolved it from the
338
- * api_key during the upgrade and knows the authoritative value). The dp-director
339
- * waits for this before sending `hello`.
340
- */
341
- export const WelcomeMessage = Envelope(
342
- "welcome",
343
- z.object({
344
- director_id: Uuid,
345
- })
346
- );
347
-
348
- export const WorkerStateChangeMessage = Envelope(
349
- "worker_state_change",
350
- z.object({
351
- worker_id: Uuid,
352
- state: z.enum(["running", "stopped", "crashed", "crashed_loop"]),
353
- pid: z.number().int().positive().optional(),
354
- exit_code: z.number().int().optional(),
355
- })
356
- );
357
-
358
- export const DirectorHeartbeatMessage = Envelope(
359
- "director_heartbeat",
360
- z.object({
361
- running_workers: z.array(Uuid),
362
- cpu: z.number().nonnegative().optional(),
363
- mem: z.number().nonnegative().optional(),
364
- })
365
- );
366
-
367
- export const DpWorkerUpgradeStatusMessage = Envelope(
368
- "dp_worker_upgrade_status",
369
- z.object({
370
- request_id: z.string().min(1),
371
- status: DpWorkerUpgradeStatus,
372
- runtime_mode: DpWorkerRuntimeMode,
373
- current_version: z.string().min(1).optional(),
374
- latest_version: z.string().min(1).optional(),
375
- message: z.string().optional(),
376
- })
377
- );
378
-
379
- export type SpawnWorkerPayload = z.infer<typeof SpawnWorkerMessage>["payload"];
380
- export type StopWorkerPayload = z.infer<typeof StopWorkerMessage>["payload"];
381
- export type RestartWorkerPayload = z.infer<typeof RestartWorkerMessage>["payload"];
382
- export type UpgradeDpWorkerPayload = z.infer<typeof UpgradeDpWorkerMessage>["payload"];
383
- export type QueryWorkerStatusPayload = z.infer<typeof QueryWorkerStatusMessage>["payload"];
384
- export type ReconcileWorkersPayload = z.infer<typeof ReconcileWorkersMessage>["payload"];
385
- export type DirectorHelloPayload = z.infer<typeof DirectorHelloMessage>["payload"];
386
- export type WorkerStateChangePayload = z.infer<typeof WorkerStateChangeMessage>["payload"];
387
- export type DirectorHeartbeatPayload = z.infer<typeof DirectorHeartbeatMessage>["payload"];
388
- export type WelcomePayload = z.infer<typeof WelcomeMessage>["payload"];
389
- export type DpWorkerUpgradeStatusPayload = z.infer<typeof DpWorkerUpgradeStatusMessage>["payload"];
390
-
391
- /** Union of every message that may travel on a center↔dp-director WebSocket. */
392
- export const CenterDirectorWsMessage = z.discriminatedUnion("type", [
393
- AckMessage,
394
- PingRequestMessage,
395
- PongMessage,
396
- // center → dp-director
397
- SpawnWorkerMessage,
398
- StopWorkerMessage,
399
- RestartWorkerMessage,
400
- UpgradeDpWorkerMessage,
401
- QueryWorkerStatusMessage,
402
- ReconcileWorkersMessage,
403
- WelcomeMessage,
404
- // dp-director → center
405
- DirectorHelloMessage,
406
- WorkerStateChangeMessage,
407
- DirectorHeartbeatMessage,
408
- DpWorkerUpgradeStatusMessage,
409
- ]);
410
- export type CenterDirectorWsMessage = z.infer<typeof CenterDirectorWsMessage>;
411
-
412
- /* ============================================================================
413
- * Web UI ↔ ChatBroadcast DO
414
- * ============================================================================ */
415
-
416
- /** UI sends to subscribe to a chat (auth is via JWT in connect URL). */
417
- export const ChatSubscribeMessage = Envelope(
418
- "subscribe",
419
- z.object({
420
- chat_id: Uuid,
421
- since_message_id: Uuid.optional(),
422
- })
423
- );
424
-
425
- /** ChatBroadcast pushes the same chunks dp-worker emits, plus user-side echoes. */
426
- export const ChatBroadcastChunkMessage = ChatMessageChunkMessage;
427
- export const ChatBroadcastFinalizedMessage = ChatMessageFinalizedMessage;
428
-
429
- export const CenterWebChatWsMessage = z.discriminatedUnion("type", [
430
- AckMessage,
431
- ChatSubscribeMessage,
432
- ChatBroadcastChunkMessage,
433
- ChatBroadcastFinalizedMessage,
434
- ]);
435
- export type CenterWebChatWsMessage = z.infer<typeof CenterWebChatWsMessage>;