@dungle-scrubs/tether-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/dist/event-builders.d.ts +244 -0
- package/dist/event-builders.js +259 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/records.d.ts +313 -0
- package/dist/records.js +72 -0
- package/dist/rest-schemas.d.ts +149 -0
- package/dist/rest-schemas.js +131 -0
- package/dist/task-contracts.d.ts +245 -0
- package/dist/task-contracts.js +256 -0
- package/dist/task-payload-display.d.ts +13 -0
- package/dist/task-payload-display.js +44 -0
- package/dist/websocket.d.ts +215 -0
- package/dist/websocket.js +177 -0
- package/package.json +43 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { SessionEvent } from "./records.js";
|
|
3
|
+
/** WebSocket task-claim command schema. */
|
|
4
|
+
export declare const wsTaskClaimMessageSchema: z.ZodObject<{
|
|
5
|
+
op: z.ZodLiteral<"task.claim">;
|
|
6
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
7
|
+
taskId: z.ZodString;
|
|
8
|
+
}, z.core.$strip>;
|
|
9
|
+
/** WebSocket task claim-refresh command schema. */
|
|
10
|
+
export declare const wsTaskRefreshMessageSchema: z.ZodObject<{
|
|
11
|
+
op: z.ZodLiteral<"task.refresh">;
|
|
12
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
13
|
+
taskId: z.ZodString;
|
|
14
|
+
}, z.core.$strip>;
|
|
15
|
+
/** WebSocket task-cancel command schema. */
|
|
16
|
+
export declare const wsTaskCancelMessageSchema: z.ZodObject<{
|
|
17
|
+
op: z.ZodLiteral<"task.cancel">;
|
|
18
|
+
reason: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
19
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
20
|
+
taskId: z.ZodString;
|
|
21
|
+
}, z.core.$strip>;
|
|
22
|
+
/** WebSocket task-complete command schema. */
|
|
23
|
+
export declare const wsTaskCompleteMessageSchema: z.ZodObject<{
|
|
24
|
+
op: z.ZodLiteral<"task.complete">;
|
|
25
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
26
|
+
result: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
27
|
+
taskId: z.ZodString;
|
|
28
|
+
}, z.core.$strip>;
|
|
29
|
+
/** WebSocket task-fail command schema. */
|
|
30
|
+
export declare const wsTaskFailMessageSchema: z.ZodObject<{
|
|
31
|
+
failure: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
32
|
+
op: z.ZodLiteral<"task.fail">;
|
|
33
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
34
|
+
taskId: z.ZodString;
|
|
35
|
+
}, z.core.$strip>;
|
|
36
|
+
/** WebSocket task-release command schema. */
|
|
37
|
+
export declare const wsTaskReleaseMessageSchema: z.ZodObject<{
|
|
38
|
+
op: z.ZodLiteral<"task.release">;
|
|
39
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
40
|
+
taskId: z.ZodString;
|
|
41
|
+
}, z.core.$strip>;
|
|
42
|
+
/** WebSocket publish command schema. */
|
|
43
|
+
export declare const wsPublishMessageSchema: z.ZodObject<{
|
|
44
|
+
eventId: z.ZodOptional<z.ZodString>;
|
|
45
|
+
op: z.ZodLiteral<"publish">;
|
|
46
|
+
payload: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
47
|
+
producerId: z.ZodString;
|
|
48
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
49
|
+
type: z.ZodString;
|
|
50
|
+
}, z.core.$strip>;
|
|
51
|
+
/** WebSocket server event envelope schema. */
|
|
52
|
+
export declare const webSocketEventEnvelopeSchema: z.ZodObject<{
|
|
53
|
+
event: z.ZodObject<{
|
|
54
|
+
createdAt: z.ZodString;
|
|
55
|
+
eventId: z.ZodString;
|
|
56
|
+
payload: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
57
|
+
producerId: z.ZodString;
|
|
58
|
+
seq: z.ZodNumber;
|
|
59
|
+
sessionId: z.ZodString;
|
|
60
|
+
type: z.ZodPipe<z.ZodString, z.ZodTransform<(string & {}) | "agent.output" | "approval.recorded" | "control.cancel" | "participant.heartbeat" | "participant.joined" | "participant.updated" | "session.created" | "task.claim_expired" | "task.claimed" | "task.completed" | "task.created" | "task.failed" | "task.progress" | "task.released" | "user.message", string>>;
|
|
61
|
+
}, z.core.$strip>;
|
|
62
|
+
op: z.ZodLiteral<"event">;
|
|
63
|
+
}, z.core.$strip>;
|
|
64
|
+
/** WebSocket replay-complete envelope schema. */
|
|
65
|
+
export declare const webSocketReplayCompleteEnvelopeSchema: z.ZodObject<{
|
|
66
|
+
op: z.ZodLiteral<"replay.complete">;
|
|
67
|
+
}, z.core.$strip>;
|
|
68
|
+
/** WebSocket command-result envelope schema. */
|
|
69
|
+
export declare const webSocketCommandResultEnvelopeSchema: z.ZodObject<{
|
|
70
|
+
command: z.ZodString;
|
|
71
|
+
op: z.ZodLiteral<"command.result">;
|
|
72
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
73
|
+
task: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
74
|
+
cancelledAt: z.ZodNullable<z.ZodString>;
|
|
75
|
+
claimExpiredAt: z.ZodNullable<z.ZodString>;
|
|
76
|
+
claimExpiredBy: z.ZodNullable<z.ZodString>;
|
|
77
|
+
claimExpiresAt: z.ZodNullable<z.ZodString>;
|
|
78
|
+
claimedAt: z.ZodNullable<z.ZodString>;
|
|
79
|
+
claimedBy: z.ZodNullable<z.ZodString>;
|
|
80
|
+
completedAt: z.ZodNullable<z.ZodString>;
|
|
81
|
+
createdAt: z.ZodString;
|
|
82
|
+
failedAt: z.ZodNullable<z.ZodString>;
|
|
83
|
+
failure: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
84
|
+
input: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
85
|
+
kind: z.ZodString;
|
|
86
|
+
objective: z.ZodString;
|
|
87
|
+
releasedAt: z.ZodNullable<z.ZodString>;
|
|
88
|
+
releasedBy: z.ZodNullable<z.ZodString>;
|
|
89
|
+
result: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
90
|
+
sessionId: z.ZodString;
|
|
91
|
+
taskId: z.ZodString;
|
|
92
|
+
}, z.core.$strip>>>;
|
|
93
|
+
}, z.core.$loose>;
|
|
94
|
+
/** WebSocket error envelope schema. */
|
|
95
|
+
export declare const webSocketErrorEnvelopeSchema: z.ZodObject<{
|
|
96
|
+
error: z.ZodString;
|
|
97
|
+
op: z.ZodLiteral<"error">;
|
|
98
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
99
|
+
}, z.core.$loose>;
|
|
100
|
+
/** Runtime validator for server-to-client WebSocket envelopes. */
|
|
101
|
+
export declare const webSocketServerEnvelopeSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
102
|
+
command: z.ZodString;
|
|
103
|
+
op: z.ZodLiteral<"command.result">;
|
|
104
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
105
|
+
task: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
106
|
+
cancelledAt: z.ZodNullable<z.ZodString>;
|
|
107
|
+
claimExpiredAt: z.ZodNullable<z.ZodString>;
|
|
108
|
+
claimExpiredBy: z.ZodNullable<z.ZodString>;
|
|
109
|
+
claimExpiresAt: z.ZodNullable<z.ZodString>;
|
|
110
|
+
claimedAt: z.ZodNullable<z.ZodString>;
|
|
111
|
+
claimedBy: z.ZodNullable<z.ZodString>;
|
|
112
|
+
completedAt: z.ZodNullable<z.ZodString>;
|
|
113
|
+
createdAt: z.ZodString;
|
|
114
|
+
failedAt: z.ZodNullable<z.ZodString>;
|
|
115
|
+
failure: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
116
|
+
input: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
117
|
+
kind: z.ZodString;
|
|
118
|
+
objective: z.ZodString;
|
|
119
|
+
releasedAt: z.ZodNullable<z.ZodString>;
|
|
120
|
+
releasedBy: z.ZodNullable<z.ZodString>;
|
|
121
|
+
result: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
122
|
+
sessionId: z.ZodString;
|
|
123
|
+
taskId: z.ZodString;
|
|
124
|
+
}, z.core.$strip>>>;
|
|
125
|
+
}, z.core.$loose>, z.ZodObject<{
|
|
126
|
+
error: z.ZodString;
|
|
127
|
+
op: z.ZodLiteral<"error">;
|
|
128
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
129
|
+
}, z.core.$loose>, z.ZodObject<{
|
|
130
|
+
event: z.ZodObject<{
|
|
131
|
+
createdAt: z.ZodString;
|
|
132
|
+
eventId: z.ZodString;
|
|
133
|
+
payload: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
134
|
+
producerId: z.ZodString;
|
|
135
|
+
seq: z.ZodNumber;
|
|
136
|
+
sessionId: z.ZodString;
|
|
137
|
+
type: z.ZodPipe<z.ZodString, z.ZodTransform<(string & {}) | "agent.output" | "approval.recorded" | "control.cancel" | "participant.heartbeat" | "participant.joined" | "participant.updated" | "session.created" | "task.claim_expired" | "task.claimed" | "task.completed" | "task.created" | "task.failed" | "task.progress" | "task.released" | "user.message", string>>;
|
|
138
|
+
}, z.core.$strip>;
|
|
139
|
+
op: z.ZodLiteral<"event">;
|
|
140
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
141
|
+
op: z.ZodLiteral<"replay.complete">;
|
|
142
|
+
}, z.core.$strip>]>;
|
|
143
|
+
/** Parsed WebSocket command-result envelope. */
|
|
144
|
+
export type CommandResultEnvelope = z.infer<typeof webSocketCommandResultEnvelopeSchema>;
|
|
145
|
+
/** Parsed server-to-client WebSocket envelope. */
|
|
146
|
+
export type WebSocketServerEnvelope = z.infer<typeof webSocketServerEnvelopeSchema>;
|
|
147
|
+
/** Parsed WebSocket task command message. */
|
|
148
|
+
export type WebSocketTaskCommandMessage = z.infer<typeof wsTaskCancelMessageSchema> | z.infer<typeof wsTaskClaimMessageSchema> | z.infer<typeof wsTaskCompleteMessageSchema> | z.infer<typeof wsTaskFailMessageSchema> | z.infer<typeof wsTaskRefreshMessageSchema> | z.infer<typeof wsTaskReleaseMessageSchema>;
|
|
149
|
+
/**
|
|
150
|
+
* WebSocket client command messages that can be correlated with a command
|
|
151
|
+
* result envelope.
|
|
152
|
+
*/
|
|
153
|
+
export type WebSocketCommandMessage = WebSocketTaskCommandMessage | z.infer<typeof wsPublishMessageSchema>;
|
|
154
|
+
interface SerializeCommandEnvelopeInput {
|
|
155
|
+
readonly command: string;
|
|
156
|
+
readonly payload: Record<string, unknown>;
|
|
157
|
+
readonly requestId?: string;
|
|
158
|
+
}
|
|
159
|
+
interface SerializeErrorEnvelopeInput {
|
|
160
|
+
readonly details?: Record<string, unknown>;
|
|
161
|
+
readonly error: string;
|
|
162
|
+
readonly requestId?: string;
|
|
163
|
+
}
|
|
164
|
+
/** Creates a WebSocket publish message for participant-originated events. */
|
|
165
|
+
export declare function buildWsPublishMessage(input: {
|
|
166
|
+
readonly eventId?: string;
|
|
167
|
+
readonly payload: Record<string, unknown>;
|
|
168
|
+
readonly producerId: string;
|
|
169
|
+
readonly requestId?: string;
|
|
170
|
+
readonly type: string;
|
|
171
|
+
}): z.infer<typeof wsPublishMessageSchema>;
|
|
172
|
+
/** Creates a WebSocket task-claim command. */
|
|
173
|
+
export declare function buildWsTaskClaimMessage(input: {
|
|
174
|
+
readonly requestId?: string;
|
|
175
|
+
readonly taskId: string;
|
|
176
|
+
}): z.infer<typeof wsTaskClaimMessageSchema>;
|
|
177
|
+
/** Creates a WebSocket task claim-refresh command. */
|
|
178
|
+
export declare function buildWsTaskRefreshMessage(input: {
|
|
179
|
+
readonly requestId?: string;
|
|
180
|
+
readonly taskId: string;
|
|
181
|
+
}): z.infer<typeof wsTaskRefreshMessageSchema>;
|
|
182
|
+
/** Creates a WebSocket task-cancel command. */
|
|
183
|
+
export declare function buildWsTaskCancelMessage(input: {
|
|
184
|
+
readonly reason?: Record<string, unknown>;
|
|
185
|
+
readonly requestId?: string;
|
|
186
|
+
readonly taskId: string;
|
|
187
|
+
}): z.infer<typeof wsTaskCancelMessageSchema>;
|
|
188
|
+
/** Creates a WebSocket task-complete command. */
|
|
189
|
+
export declare function buildWsTaskCompleteMessage(input: {
|
|
190
|
+
readonly requestId?: string;
|
|
191
|
+
readonly result: Record<string, unknown>;
|
|
192
|
+
readonly taskId: string;
|
|
193
|
+
}): z.infer<typeof wsTaskCompleteMessageSchema>;
|
|
194
|
+
/** Creates a WebSocket task-fail command. */
|
|
195
|
+
export declare function buildWsTaskFailMessage(input: {
|
|
196
|
+
readonly failure: Record<string, unknown>;
|
|
197
|
+
readonly requestId?: string;
|
|
198
|
+
readonly taskId: string;
|
|
199
|
+
}): z.infer<typeof wsTaskFailMessageSchema>;
|
|
200
|
+
/** Creates a WebSocket task-release command. */
|
|
201
|
+
export declare function buildWsTaskReleaseMessage(input: {
|
|
202
|
+
readonly requestId?: string;
|
|
203
|
+
readonly taskId: string;
|
|
204
|
+
}): z.infer<typeof wsTaskReleaseMessageSchema>;
|
|
205
|
+
/** Parses server-to-client WebSocket envelopes into the known protocol union. */
|
|
206
|
+
export declare function parseWebSocketServerEnvelope(value: unknown): WebSocketServerEnvelope | null;
|
|
207
|
+
/** Serializes a session event envelope for WebSocket subscribers. */
|
|
208
|
+
export declare function serializeEventEnvelope(event: SessionEvent): string;
|
|
209
|
+
/** Serializes the replay-complete marker sent after historical events. */
|
|
210
|
+
export declare function serializeReplayCompleteEnvelope(): string;
|
|
211
|
+
/** Serializes a command result envelope while preserving command-specific fields. */
|
|
212
|
+
export declare function serializeCommandResultEnvelope(input: SerializeCommandEnvelopeInput): string;
|
|
213
|
+
/** Serializes a command or connection error envelope. */
|
|
214
|
+
export declare function serializeErrorEnvelope(input: SerializeErrorEnvelopeInput): string;
|
|
215
|
+
export {};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { sessionEventSchema } from "./event-builders.js";
|
|
3
|
+
import { taskRecordSchema, webSocketOperation } from "./records.js";
|
|
4
|
+
/** WebSocket task-claim command schema. */
|
|
5
|
+
export const wsTaskClaimMessageSchema = z.object({
|
|
6
|
+
op: z.literal(webSocketOperation.taskClaim),
|
|
7
|
+
requestId: z.string().min(1).optional(),
|
|
8
|
+
taskId: z.string().min(1),
|
|
9
|
+
});
|
|
10
|
+
/** WebSocket task claim-refresh command schema. */
|
|
11
|
+
export const wsTaskRefreshMessageSchema = z.object({
|
|
12
|
+
op: z.literal(webSocketOperation.taskRefresh),
|
|
13
|
+
requestId: z.string().min(1).optional(),
|
|
14
|
+
taskId: z.string().min(1),
|
|
15
|
+
});
|
|
16
|
+
/** WebSocket task-cancel command schema. */
|
|
17
|
+
export const wsTaskCancelMessageSchema = z.object({
|
|
18
|
+
op: z.literal(webSocketOperation.taskCancel),
|
|
19
|
+
reason: z.record(z.string(), z.unknown()).default({}),
|
|
20
|
+
requestId: z.string().min(1).optional(),
|
|
21
|
+
taskId: z.string().min(1),
|
|
22
|
+
});
|
|
23
|
+
/** WebSocket task-complete command schema. */
|
|
24
|
+
export const wsTaskCompleteMessageSchema = z.object({
|
|
25
|
+
op: z.literal(webSocketOperation.taskComplete),
|
|
26
|
+
requestId: z.string().min(1).optional(),
|
|
27
|
+
result: z.record(z.string(), z.unknown()).default({}),
|
|
28
|
+
taskId: z.string().min(1),
|
|
29
|
+
});
|
|
30
|
+
/** WebSocket task-fail command schema. */
|
|
31
|
+
export const wsTaskFailMessageSchema = z.object({
|
|
32
|
+
failure: z.record(z.string(), z.unknown()).default({}),
|
|
33
|
+
op: z.literal(webSocketOperation.taskFail),
|
|
34
|
+
requestId: z.string().min(1).optional(),
|
|
35
|
+
taskId: z.string().min(1),
|
|
36
|
+
});
|
|
37
|
+
/** WebSocket task-release command schema. */
|
|
38
|
+
export const wsTaskReleaseMessageSchema = z.object({
|
|
39
|
+
op: z.literal(webSocketOperation.taskRelease),
|
|
40
|
+
requestId: z.string().min(1).optional(),
|
|
41
|
+
taskId: z.string().min(1),
|
|
42
|
+
});
|
|
43
|
+
/** WebSocket publish command schema. */
|
|
44
|
+
export const wsPublishMessageSchema = z.object({
|
|
45
|
+
eventId: z.string().min(1).optional(),
|
|
46
|
+
op: z.literal(webSocketOperation.publish),
|
|
47
|
+
payload: z.record(z.string(), z.unknown()).default({}),
|
|
48
|
+
producerId: z.string().min(1),
|
|
49
|
+
requestId: z.string().min(1).optional(),
|
|
50
|
+
type: z.string().min(1),
|
|
51
|
+
});
|
|
52
|
+
/** WebSocket server event envelope schema. */
|
|
53
|
+
export const webSocketEventEnvelopeSchema = z.object({
|
|
54
|
+
event: sessionEventSchema,
|
|
55
|
+
op: z.literal(webSocketOperation.event),
|
|
56
|
+
});
|
|
57
|
+
/** WebSocket replay-complete envelope schema. */
|
|
58
|
+
export const webSocketReplayCompleteEnvelopeSchema = z.object({
|
|
59
|
+
op: z.literal(webSocketOperation.replayComplete),
|
|
60
|
+
});
|
|
61
|
+
/** WebSocket command-result envelope schema. */
|
|
62
|
+
export const webSocketCommandResultEnvelopeSchema = z
|
|
63
|
+
.object({
|
|
64
|
+
command: z.string().min(1),
|
|
65
|
+
op: z.literal(webSocketOperation.commandResult),
|
|
66
|
+
requestId: z.string().min(1).optional(),
|
|
67
|
+
task: taskRecordSchema.nullable().optional(),
|
|
68
|
+
})
|
|
69
|
+
.passthrough();
|
|
70
|
+
/** WebSocket error envelope schema. */
|
|
71
|
+
export const webSocketErrorEnvelopeSchema = z
|
|
72
|
+
.object({
|
|
73
|
+
error: z.string(),
|
|
74
|
+
op: z.literal(webSocketOperation.error),
|
|
75
|
+
requestId: z.string().min(1).optional(),
|
|
76
|
+
})
|
|
77
|
+
.passthrough();
|
|
78
|
+
/** Runtime validator for server-to-client WebSocket envelopes. */
|
|
79
|
+
export const webSocketServerEnvelopeSchema = z.union([
|
|
80
|
+
webSocketCommandResultEnvelopeSchema,
|
|
81
|
+
webSocketErrorEnvelopeSchema,
|
|
82
|
+
webSocketEventEnvelopeSchema,
|
|
83
|
+
webSocketReplayCompleteEnvelopeSchema,
|
|
84
|
+
]);
|
|
85
|
+
/** Creates a WebSocket publish message for participant-originated events. */
|
|
86
|
+
export function buildWsPublishMessage(input) {
|
|
87
|
+
return {
|
|
88
|
+
...(input.eventId !== undefined ? { eventId: input.eventId } : {}),
|
|
89
|
+
op: webSocketOperation.publish,
|
|
90
|
+
payload: input.payload,
|
|
91
|
+
producerId: input.producerId,
|
|
92
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
93
|
+
type: input.type,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** Creates a WebSocket task-claim command. */
|
|
97
|
+
export function buildWsTaskClaimMessage(input) {
|
|
98
|
+
return {
|
|
99
|
+
op: webSocketOperation.taskClaim,
|
|
100
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
101
|
+
taskId: input.taskId,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/** Creates a WebSocket task claim-refresh command. */
|
|
105
|
+
export function buildWsTaskRefreshMessage(input) {
|
|
106
|
+
return {
|
|
107
|
+
op: webSocketOperation.taskRefresh,
|
|
108
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
109
|
+
taskId: input.taskId,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** Creates a WebSocket task-cancel command. */
|
|
113
|
+
export function buildWsTaskCancelMessage(input) {
|
|
114
|
+
return {
|
|
115
|
+
op: webSocketOperation.taskCancel,
|
|
116
|
+
reason: input.reason ?? {},
|
|
117
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
118
|
+
taskId: input.taskId,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** Creates a WebSocket task-complete command. */
|
|
122
|
+
export function buildWsTaskCompleteMessage(input) {
|
|
123
|
+
return {
|
|
124
|
+
op: webSocketOperation.taskComplete,
|
|
125
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
126
|
+
result: input.result,
|
|
127
|
+
taskId: input.taskId,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** Creates a WebSocket task-fail command. */
|
|
131
|
+
export function buildWsTaskFailMessage(input) {
|
|
132
|
+
return {
|
|
133
|
+
failure: input.failure,
|
|
134
|
+
op: webSocketOperation.taskFail,
|
|
135
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
136
|
+
taskId: input.taskId,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/** Creates a WebSocket task-release command. */
|
|
140
|
+
export function buildWsTaskReleaseMessage(input) {
|
|
141
|
+
return {
|
|
142
|
+
op: webSocketOperation.taskRelease,
|
|
143
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
144
|
+
taskId: input.taskId,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Parses server-to-client WebSocket envelopes into the known protocol union. */
|
|
148
|
+
export function parseWebSocketServerEnvelope(value) {
|
|
149
|
+
const parsed = webSocketServerEnvelopeSchema.safeParse(value);
|
|
150
|
+
return parsed.success ? parsed.data : null;
|
|
151
|
+
}
|
|
152
|
+
/** Serializes a session event envelope for WebSocket subscribers. */
|
|
153
|
+
export function serializeEventEnvelope(event) {
|
|
154
|
+
return JSON.stringify({ event, op: webSocketOperation.event });
|
|
155
|
+
}
|
|
156
|
+
/** Serializes the replay-complete marker sent after historical events. */
|
|
157
|
+
export function serializeReplayCompleteEnvelope() {
|
|
158
|
+
return JSON.stringify({ op: webSocketOperation.replayComplete });
|
|
159
|
+
}
|
|
160
|
+
/** Serializes a command result envelope while preserving command-specific fields. */
|
|
161
|
+
export function serializeCommandResultEnvelope(input) {
|
|
162
|
+
return JSON.stringify({
|
|
163
|
+
...input.payload,
|
|
164
|
+
command: input.command,
|
|
165
|
+
op: webSocketOperation.commandResult,
|
|
166
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/** Serializes a command or connection error envelope. */
|
|
170
|
+
export function serializeErrorEnvelope(input) {
|
|
171
|
+
return JSON.stringify({
|
|
172
|
+
...(input.details ?? {}),
|
|
173
|
+
error: input.error,
|
|
174
|
+
op: webSocketOperation.error,
|
|
175
|
+
...(input.requestId !== undefined ? { requestId: input.requestId } : {}),
|
|
176
|
+
});
|
|
177
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dungle-scrubs/tether-protocol",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared Tether protocol records, validators, and event builders.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Kevin Frilot",
|
|
7
|
+
"homepage": "https://github.com/dungle-scrubs/tether/tree/main/packages/protocol#readme",
|
|
8
|
+
"bugs": "https://github.com/dungle-scrubs/tether/issues",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/dungle-scrubs/tether.git",
|
|
12
|
+
"directory": "packages/protocol"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p tsconfig.json",
|
|
29
|
+
"format": "biome format --config-path=../../tooling/biome.json --write .",
|
|
30
|
+
"format:check": "biome format --config-path=../../tooling/biome.json .",
|
|
31
|
+
"lint": "biome lint --config-path=../../tooling/biome.json .",
|
|
32
|
+
"prepublishOnly": "pnpm run build",
|
|
33
|
+
"test": "vitest run",
|
|
34
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"zod": "^4.4.3"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^25.9.1",
|
|
41
|
+
"vitest": "^4.1.7"
|
|
42
|
+
}
|
|
43
|
+
}
|