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