@ahmadposten/talos-wire 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/index.cjs ADDED
@@ -0,0 +1,371 @@
1
+ 'use strict';
2
+
3
+ var z = require('zod');
4
+ var cuid2 = require('@paralleldrive/cuid2');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var z__namespace = /*#__PURE__*/_interopNamespaceDefault(z);
24
+
25
+ const sessionRoleSchema = z__namespace.enum(["user", "agent"]);
26
+ const sessionTextEventSchema = z__namespace.object({
27
+ t: z__namespace.literal("text"),
28
+ text: z__namespace.string(),
29
+ thinking: z__namespace.boolean().optional()
30
+ });
31
+ const sessionServiceMessageEventSchema = z__namespace.object({
32
+ t: z__namespace.literal("service"),
33
+ text: z__namespace.string()
34
+ });
35
+ const sessionToolCallStartEventSchema = z__namespace.object({
36
+ t: z__namespace.literal("tool-call-start"),
37
+ call: z__namespace.string(),
38
+ name: z__namespace.string(),
39
+ title: z__namespace.string(),
40
+ description: z__namespace.string(),
41
+ args: z__namespace.record(z__namespace.string(), z__namespace.unknown())
42
+ });
43
+ const sessionToolCallEndEventSchema = z__namespace.object({
44
+ t: z__namespace.literal("tool-call-end"),
45
+ call: z__namespace.string(),
46
+ // The tool's own output. Optional for back-compat: older CLIs send only
47
+ // `call`, and clients must keep treating a missing result as "no output".
48
+ // Without this the protocol drops every result — which silently broke the
49
+ // todo panel, since TaskCreate reports the task id only in its result.
50
+ result: z__namespace.unknown().optional(),
51
+ isError: z__namespace.boolean().optional()
52
+ });
53
+ const sessionFileEventSchema = z__namespace.object({
54
+ t: z__namespace.literal("file"),
55
+ ref: z__namespace.string(),
56
+ name: z__namespace.string(),
57
+ size: z__namespace.number(),
58
+ mimeType: z__namespace.string().optional(),
59
+ image: z__namespace.object({
60
+ width: z__namespace.number(),
61
+ height: z__namespace.number(),
62
+ thumbhash: z__namespace.string()
63
+ }).optional()
64
+ });
65
+ const sessionTurnStartEventSchema = z__namespace.object({
66
+ t: z__namespace.literal("turn-start")
67
+ });
68
+ const sessionStartEventSchema = z__namespace.object({
69
+ t: z__namespace.literal("start"),
70
+ title: z__namespace.string().optional()
71
+ });
72
+ const sessionTurnEndStatusSchema = z__namespace.enum(["completed", "failed", "cancelled"]);
73
+ const sessionTurnEndEventSchema = z__namespace.object({
74
+ t: z__namespace.literal("turn-end"),
75
+ status: sessionTurnEndStatusSchema
76
+ });
77
+ const sessionStopEventSchema = z__namespace.object({
78
+ t: z__namespace.literal("stop")
79
+ });
80
+ const sessionEventSchema = z__namespace.discriminatedUnion("t", [
81
+ sessionTextEventSchema,
82
+ sessionServiceMessageEventSchema,
83
+ sessionToolCallStartEventSchema,
84
+ sessionToolCallEndEventSchema,
85
+ sessionFileEventSchema,
86
+ sessionTurnStartEventSchema,
87
+ sessionStartEventSchema,
88
+ sessionTurnEndEventSchema,
89
+ sessionStopEventSchema
90
+ ]);
91
+ const sessionEnvelopeSchema = z__namespace.object({
92
+ id: z__namespace.string(),
93
+ time: z__namespace.number(),
94
+ role: sessionRoleSchema,
95
+ turn: z__namespace.string().optional(),
96
+ subagent: z__namespace.string().refine((value) => cuid2.isCuid(value), {
97
+ message: "subagent must be a cuid2 value"
98
+ }).optional(),
99
+ // Underlying agent-protocol message id (e.g. Claude's `uuid` in the
100
+ // session JSONL). Set on text-bearing envelopes so the app can let
101
+ // users pick a precise rewind point for session fork / duplicate.
102
+ claudeUuid: z__namespace.string().min(1).optional(),
103
+ // Codex app-server item id for this envelope. Used as the precise
104
+ // rollback point for Codex thread duplicate/fork-from-message.
105
+ codexItemId: z__namespace.string().min(1).optional(),
106
+ ev: sessionEventSchema
107
+ }).superRefine((envelope, ctx) => {
108
+ if (envelope.ev.t === "service" && envelope.role !== "agent") {
109
+ ctx.addIssue({
110
+ code: z__namespace.ZodIssueCode.custom,
111
+ message: 'service events must use role "agent"',
112
+ path: ["role"]
113
+ });
114
+ }
115
+ if ((envelope.ev.t === "start" || envelope.ev.t === "stop") && envelope.role !== "agent") {
116
+ ctx.addIssue({
117
+ code: z__namespace.ZodIssueCode.custom,
118
+ message: `${envelope.ev.t} events must use role "agent"`,
119
+ path: ["role"]
120
+ });
121
+ }
122
+ });
123
+ function createEnvelope(role, ev, opts = {}) {
124
+ return sessionEnvelopeSchema.parse({
125
+ id: opts.id ?? cuid2.createId(),
126
+ time: opts.time ?? Date.now(),
127
+ role,
128
+ ...opts.turn ? { turn: opts.turn } : {},
129
+ ...opts.subagent ? { subagent: opts.subagent } : {},
130
+ ...opts.claudeUuid ? { claudeUuid: opts.claudeUuid } : {},
131
+ ...opts.codexItemId ? { codexItemId: opts.codexItemId } : {},
132
+ ev
133
+ });
134
+ }
135
+
136
+ const MessageMetaSchema = z__namespace.object({
137
+ sentFrom: z__namespace.string().optional(),
138
+ permissionMode: z__namespace.enum(["default", "acceptEdits", "bypassPermissions", "plan", "read-only", "safe-yolo", "yolo"]).optional(),
139
+ model: z__namespace.string().nullable().optional(),
140
+ fallbackModel: z__namespace.string().nullable().optional(),
141
+ customSystemPrompt: z__namespace.string().nullable().optional(),
142
+ appendSystemPrompt: z__namespace.string().nullable().optional(),
143
+ allowedTools: z__namespace.array(z__namespace.string()).nullable().optional(),
144
+ disallowedTools: z__namespace.array(z__namespace.string()).nullable().optional(),
145
+ effort: z__namespace.string().nullable().optional(),
146
+ displayText: z__namespace.string().optional()
147
+ });
148
+
149
+ const UserMessageSchema = z__namespace.object({
150
+ role: z__namespace.literal("user"),
151
+ content: z__namespace.object({
152
+ type: z__namespace.literal("text"),
153
+ text: z__namespace.string()
154
+ }),
155
+ localKey: z__namespace.string().optional(),
156
+ meta: MessageMetaSchema.optional()
157
+ });
158
+ const AgentMessageSchema = z__namespace.object({
159
+ role: z__namespace.literal("agent"),
160
+ content: z__namespace.object({
161
+ type: z__namespace.string()
162
+ }).passthrough(),
163
+ meta: MessageMetaSchema.optional()
164
+ });
165
+ const LegacyMessageContentSchema = z__namespace.discriminatedUnion("role", [UserMessageSchema, AgentMessageSchema]);
166
+
167
+ const SessionMessageContentSchema = z__namespace.object({
168
+ c: z__namespace.string(),
169
+ t: z__namespace.literal("encrypted")
170
+ });
171
+ const SessionMessageSchema = z__namespace.object({
172
+ id: z__namespace.string(),
173
+ seq: z__namespace.number(),
174
+ localId: z__namespace.string().nullish(),
175
+ content: SessionMessageContentSchema,
176
+ createdAt: z__namespace.number(),
177
+ updatedAt: z__namespace.number()
178
+ });
179
+ const SessionProtocolMessageSchema = z__namespace.object({
180
+ role: z__namespace.literal("session"),
181
+ content: sessionEnvelopeSchema,
182
+ meta: MessageMetaSchema.optional()
183
+ });
184
+ const MessageContentSchema = z__namespace.discriminatedUnion("role", [
185
+ UserMessageSchema,
186
+ AgentMessageSchema,
187
+ SessionProtocolMessageSchema
188
+ ]);
189
+ const VersionedEncryptedValueSchema = z__namespace.object({
190
+ version: z__namespace.number(),
191
+ value: z__namespace.string()
192
+ });
193
+ const VersionedNullableEncryptedValueSchema = z__namespace.object({
194
+ version: z__namespace.number(),
195
+ value: z__namespace.string().nullable()
196
+ });
197
+ const UpdateNewMessageBodySchema = z__namespace.object({
198
+ t: z__namespace.literal("new-message"),
199
+ sid: z__namespace.string(),
200
+ message: SessionMessageSchema
201
+ });
202
+ const UpdateSessionBodySchema = z__namespace.object({
203
+ t: z__namespace.literal("update-session"),
204
+ id: z__namespace.string(),
205
+ metadata: VersionedEncryptedValueSchema.nullish(),
206
+ agentState: VersionedNullableEncryptedValueSchema.nullish()
207
+ });
208
+ const VersionedMachineEncryptedValueSchema = z__namespace.object({
209
+ version: z__namespace.number(),
210
+ value: z__namespace.string()
211
+ });
212
+ const UpdateMachineBodySchema = z__namespace.object({
213
+ t: z__namespace.literal("update-machine"),
214
+ machineId: z__namespace.string(),
215
+ metadata: VersionedMachineEncryptedValueSchema.nullish(),
216
+ daemonState: VersionedMachineEncryptedValueSchema.nullish(),
217
+ active: z__namespace.boolean().optional(),
218
+ activeAt: z__namespace.number().optional()
219
+ });
220
+ const CoreUpdateBodySchema = z__namespace.discriminatedUnion("t", [
221
+ UpdateNewMessageBodySchema,
222
+ UpdateSessionBodySchema,
223
+ UpdateMachineBodySchema
224
+ ]);
225
+ const CoreUpdateContainerSchema = z__namespace.object({
226
+ id: z__namespace.string(),
227
+ seq: z__namespace.number(),
228
+ body: CoreUpdateBodySchema,
229
+ createdAt: z__namespace.number()
230
+ });
231
+ const ApiMessageSchema = SessionMessageSchema;
232
+ const ApiUpdateNewMessageSchema = UpdateNewMessageBodySchema;
233
+ const ApiUpdateSessionStateSchema = UpdateSessionBodySchema;
234
+ const ApiUpdateMachineStateSchema = UpdateMachineBodySchema;
235
+ const UpdateBodySchema = UpdateNewMessageBodySchema;
236
+ const UpdateSchema = CoreUpdateContainerSchema;
237
+
238
+ const VoiceConversationGrantedSchema = z__namespace.object({
239
+ allowed: z__namespace.literal(true),
240
+ conversationToken: z__namespace.string(),
241
+ conversationId: z__namespace.string(),
242
+ agentId: z__namespace.string(),
243
+ elevenUserId: z__namespace.string(),
244
+ usedSeconds: z__namespace.number(),
245
+ limitSeconds: z__namespace.number()
246
+ });
247
+ const VoiceConversationDeniedSchema = z__namespace.object({
248
+ allowed: z__namespace.literal(false),
249
+ reason: z__namespace.enum(["voice_hard_limit_reached", "subscription_required", "voice_conversation_limit_reached"]),
250
+ usedSeconds: z__namespace.number(),
251
+ limitSeconds: z__namespace.number(),
252
+ agentId: z__namespace.string()
253
+ });
254
+ const VoiceConversationResponseSchema = z__namespace.discriminatedUnion("allowed", [
255
+ VoiceConversationGrantedSchema,
256
+ VoiceConversationDeniedSchema
257
+ ]);
258
+ const VoiceUsageResponseSchema = z__namespace.object({
259
+ usedSeconds: z__namespace.number(),
260
+ limitSeconds: z__namespace.number(),
261
+ conversationCount: z__namespace.number(),
262
+ conversationLimit: z__namespace.number(),
263
+ elevenUserId: z__namespace.string()
264
+ });
265
+
266
+ const encryptionContexts = {
267
+ content: "Happy EnCoder",
268
+ analytics: "Happy Coder",
269
+ blobs: "Happy Blobs",
270
+ serverTokens: "happy-server-tokens"
271
+ };
272
+ const legacyServerBanner = "Welcome to Happy Server!";
273
+ const authenticationContexts = {
274
+ persistent: "handy",
275
+ github: "github-happy"
276
+ };
277
+ const rpcMethods = {
278
+ spawnSession: "spawn-happy-session",
279
+ resumeSession: "resume-happy-session"
280
+ };
281
+ const legacyInstallation = {
282
+ accountLinkPrefix: "happy:///account?",
283
+ homeDirectory: ".happy",
284
+ homeEnvironment: "HAPPY_HOME_DIR",
285
+ serverEnvironment: "HAPPY_SERVER_URL",
286
+ webappEnvironment: "HAPPY_WEBAPP_URL",
287
+ defaultServerUrl: "https://api.happy.ahposten.com",
288
+ defaultWebappUrl: "https://app.happy.engineering"
289
+ };
290
+ const metadataAliases = {
291
+ happyCliVersion: "talosCliVersion",
292
+ happyHomeDir: "talosHomeDir",
293
+ happyLibDir: "talosLibDir",
294
+ happyToolsDir: "talosToolsDir",
295
+ requiresHappyAgentAuth: "requiresTalosAgentAuth",
296
+ happyAgentAuthenticated: "talosAgentAuthenticated"
297
+ };
298
+ function isRecord(value) {
299
+ return value !== null && typeof value === "object" && !Array.isArray(value);
300
+ }
301
+ function normalizeMetadata(value) {
302
+ if (!isRecord(value)) return value;
303
+ const result = { ...value };
304
+ for (const [oldKey, key] of Object.entries(metadataAliases)) {
305
+ if (result[key] === void 0 && result[oldKey] !== void 0) result[key] = result[oldKey];
306
+ delete result[oldKey];
307
+ }
308
+ if (isRecord(result.resumeSupport)) result.resumeSupport = normalizeMetadata(result.resumeSupport);
309
+ if (result.shutdownSource === "happy-app") result.shutdownSource = "talos-app";
310
+ if (result.shutdownSource === "happy-cli") result.shutdownSource = "talos-cli";
311
+ return result;
312
+ }
313
+ function toWireMetadata(value) {
314
+ if (!isRecord(value)) return value;
315
+ const result = { ...value };
316
+ for (const [oldKey, key] of Object.entries(metadataAliases)) {
317
+ if (result[key] !== void 0) result[oldKey] = result[key];
318
+ }
319
+ if (isRecord(result.resumeSupport)) result.resumeSupport = toWireMetadata(result.resumeSupport);
320
+ if (result.shutdownSource === "talos-app") result.shutdownSource = "happy-app";
321
+ if (result.shutdownSource === "talos-cli") result.shutdownSource = "happy-cli";
322
+ return result;
323
+ }
324
+
325
+ exports.AgentMessageSchema = AgentMessageSchema;
326
+ exports.ApiMessageSchema = ApiMessageSchema;
327
+ exports.ApiUpdateMachineStateSchema = ApiUpdateMachineStateSchema;
328
+ exports.ApiUpdateNewMessageSchema = ApiUpdateNewMessageSchema;
329
+ exports.ApiUpdateSessionStateSchema = ApiUpdateSessionStateSchema;
330
+ exports.CoreUpdateBodySchema = CoreUpdateBodySchema;
331
+ exports.CoreUpdateContainerSchema = CoreUpdateContainerSchema;
332
+ exports.LegacyMessageContentSchema = LegacyMessageContentSchema;
333
+ exports.MessageContentSchema = MessageContentSchema;
334
+ exports.MessageMetaSchema = MessageMetaSchema;
335
+ exports.SessionMessageContentSchema = SessionMessageContentSchema;
336
+ exports.SessionMessageSchema = SessionMessageSchema;
337
+ exports.SessionProtocolMessageSchema = SessionProtocolMessageSchema;
338
+ exports.UpdateBodySchema = UpdateBodySchema;
339
+ exports.UpdateMachineBodySchema = UpdateMachineBodySchema;
340
+ exports.UpdateNewMessageBodySchema = UpdateNewMessageBodySchema;
341
+ exports.UpdateSchema = UpdateSchema;
342
+ exports.UpdateSessionBodySchema = UpdateSessionBodySchema;
343
+ exports.UserMessageSchema = UserMessageSchema;
344
+ exports.VersionedEncryptedValueSchema = VersionedEncryptedValueSchema;
345
+ exports.VersionedMachineEncryptedValueSchema = VersionedMachineEncryptedValueSchema;
346
+ exports.VersionedNullableEncryptedValueSchema = VersionedNullableEncryptedValueSchema;
347
+ exports.VoiceConversationDeniedSchema = VoiceConversationDeniedSchema;
348
+ exports.VoiceConversationGrantedSchema = VoiceConversationGrantedSchema;
349
+ exports.VoiceConversationResponseSchema = VoiceConversationResponseSchema;
350
+ exports.VoiceUsageResponseSchema = VoiceUsageResponseSchema;
351
+ exports.authenticationContexts = authenticationContexts;
352
+ exports.createEnvelope = createEnvelope;
353
+ exports.encryptionContexts = encryptionContexts;
354
+ exports.legacyInstallation = legacyInstallation;
355
+ exports.legacyServerBanner = legacyServerBanner;
356
+ exports.normalizeMetadata = normalizeMetadata;
357
+ exports.rpcMethods = rpcMethods;
358
+ exports.sessionEnvelopeSchema = sessionEnvelopeSchema;
359
+ exports.sessionEventSchema = sessionEventSchema;
360
+ exports.sessionFileEventSchema = sessionFileEventSchema;
361
+ exports.sessionRoleSchema = sessionRoleSchema;
362
+ exports.sessionServiceMessageEventSchema = sessionServiceMessageEventSchema;
363
+ exports.sessionStartEventSchema = sessionStartEventSchema;
364
+ exports.sessionStopEventSchema = sessionStopEventSchema;
365
+ exports.sessionTextEventSchema = sessionTextEventSchema;
366
+ exports.sessionToolCallEndEventSchema = sessionToolCallEndEventSchema;
367
+ exports.sessionToolCallStartEventSchema = sessionToolCallStartEventSchema;
368
+ exports.sessionTurnEndEventSchema = sessionTurnEndEventSchema;
369
+ exports.sessionTurnEndStatusSchema = sessionTurnEndStatusSchema;
370
+ exports.sessionTurnStartEventSchema = sessionTurnStartEventSchema;
371
+ exports.toWireMetadata = toWireMetadata;