@crewhaus/gateway-protocol 0.3.2 → 0.4.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/dist/index.d.ts CHANGED
@@ -238,6 +238,19 @@ export declare function decodeRequest(raw: unknown): RequestEnvelopeT & {
238
238
  };
239
239
  export declare function encodeSuccess(id: string, result: unknown): ResponseEnvelopeT;
240
240
  export declare function encodeError(id: string, code: string, message: string, data?: unknown): ResponseEnvelopeT;
241
+ /** MIME type of a `runs.subscribe` response body. */
242
+ export declare const SSE_CONTENT_TYPE: "text/event-stream";
243
+ /**
244
+ * Encode one trace event as an SSE `data:` frame. `event` is serialized with
245
+ * `JSON.stringify` (a TraceEvent is always JSON-serializable), so the frame is
246
+ * a single `data:` line terminated by the mandatory blank line.
247
+ */
248
+ export declare function encodeSseEvent(event: unknown): string;
249
+ /**
250
+ * Encode an SSE comment frame (heartbeat / open marker). Any newline in `text`
251
+ * is collapsed to a space so the comment stays a single well-formed frame.
252
+ */
253
+ export declare function encodeSseComment(text: string): string;
241
254
  export declare const ErrorCode: {
242
255
  readonly Unauthorized: "unauthorized";
243
256
  readonly Forbidden: "forbidden";
package/dist/index.js CHANGED
@@ -165,6 +165,45 @@ export function encodeError(id, code, message, data) {
165
165
  };
166
166
  }
167
167
  // ---------------------------------------------------------------------------
168
+ // `runs.subscribe` — Server-Sent Events framing.
169
+ //
170
+ // `runs.subscribe` is the ONE method that does not answer with a JSON
171
+ // `ResponseEnvelope`: it upgrades to a long-lived `text/event-stream` that
172
+ // replays the run's buffered trace events and then live-streams new ones.
173
+ // The frame format lives HERE (the wire-contract package) so the daemon and
174
+ // every reference client encode/parse it identically:
175
+ //
176
+ // - each trace event is one SSE `data:` frame carrying the event's JSON.
177
+ // `JSON.stringify` never emits a literal newline (newlines inside string
178
+ // fields are escaped to `\n`), so one event is always exactly one `data:`
179
+ // line — no multi-line-`data:` reassembly is required on the read side.
180
+ // - heartbeats and the connection-open marker are SSE COMMENT frames
181
+ // (`:`-prefixed); a spec-compliant client ignores them, so they keep
182
+ // intermediaries from idling the connection out without polluting the
183
+ // event stream. A comment body must not contain a newline.
184
+ //
185
+ // A stream carries no envelope `id`/`protocol` — those are per-request-reply
186
+ // fields; the subscription is a fire-hose keyed by the `runId` in the
187
+ // originating `runs.subscribe` request.
188
+ // ---------------------------------------------------------------------------
189
+ /** MIME type of a `runs.subscribe` response body. */
190
+ export const SSE_CONTENT_TYPE = "text/event-stream";
191
+ /**
192
+ * Encode one trace event as an SSE `data:` frame. `event` is serialized with
193
+ * `JSON.stringify` (a TraceEvent is always JSON-serializable), so the frame is
194
+ * a single `data:` line terminated by the mandatory blank line.
195
+ */
196
+ export function encodeSseEvent(event) {
197
+ return `data: ${JSON.stringify(event)}\n\n`;
198
+ }
199
+ /**
200
+ * Encode an SSE comment frame (heartbeat / open marker). Any newline in `text`
201
+ * is collapsed to a space so the comment stays a single well-formed frame.
202
+ */
203
+ export function encodeSseComment(text) {
204
+ return `: ${text.replace(/[\r\n]+/g, " ")}\n\n`;
205
+ }
206
+ // ---------------------------------------------------------------------------
168
207
  // Standard error codes — wire-stable so reference clients can switch on them.
169
208
  // ---------------------------------------------------------------------------
170
209
  export const ErrorCode = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/gateway-protocol",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "JSON-RPC wire protocol for the managed-daemon gateway — versioned envelope + Zod schemas",
6
6
  "main": "dist/index.js",
@@ -15,7 +15,7 @@
15
15
  "test": "bun test src"
16
16
  },
17
17
  "dependencies": {
18
- "@crewhaus/errors": "0.3.2",
18
+ "@crewhaus/errors": "0.4.0",
19
19
  "zod": "^3.23.8"
20
20
  },
21
21
  "license": "Apache-2.0",