ggkhappy-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,229 @@
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
+ });
26
+ const sessionFileEventSchema = z.object({
27
+ t: z.literal("file"),
28
+ ref: z.string(),
29
+ name: z.string(),
30
+ size: z.number(),
31
+ mimeType: z.string().optional(),
32
+ image: z.object({
33
+ width: z.number(),
34
+ height: z.number(),
35
+ thumbhash: z.string()
36
+ }).optional()
37
+ });
38
+ const sessionTurnStartEventSchema = z.object({
39
+ t: z.literal("turn-start")
40
+ });
41
+ const sessionStartEventSchema = z.object({
42
+ t: z.literal("start"),
43
+ title: z.string().optional()
44
+ });
45
+ const sessionTurnEndStatusSchema = z.enum(["completed", "failed", "cancelled"]);
46
+ const sessionTurnEndEventSchema = z.object({
47
+ t: z.literal("turn-end"),
48
+ status: sessionTurnEndStatusSchema
49
+ });
50
+ const sessionStopEventSchema = z.object({
51
+ t: z.literal("stop")
52
+ });
53
+ const sessionEventSchema = z.discriminatedUnion("t", [
54
+ sessionTextEventSchema,
55
+ sessionServiceMessageEventSchema,
56
+ sessionToolCallStartEventSchema,
57
+ sessionToolCallEndEventSchema,
58
+ sessionFileEventSchema,
59
+ sessionTurnStartEventSchema,
60
+ sessionStartEventSchema,
61
+ sessionTurnEndEventSchema,
62
+ sessionStopEventSchema
63
+ ]);
64
+ const sessionEnvelopeSchema = z.object({
65
+ id: z.string(),
66
+ time: z.number(),
67
+ role: sessionRoleSchema,
68
+ turn: z.string().optional(),
69
+ subagent: z.string().refine((value) => isCuid(value), {
70
+ message: "subagent must be a cuid2 value"
71
+ }).optional(),
72
+ ev: sessionEventSchema
73
+ }).superRefine((envelope, ctx) => {
74
+ if (envelope.ev.t === "service" && envelope.role !== "agent") {
75
+ ctx.addIssue({
76
+ code: z.ZodIssueCode.custom,
77
+ message: 'service events must use role "agent"',
78
+ path: ["role"]
79
+ });
80
+ }
81
+ if ((envelope.ev.t === "start" || envelope.ev.t === "stop") && envelope.role !== "agent") {
82
+ ctx.addIssue({
83
+ code: z.ZodIssueCode.custom,
84
+ message: `${envelope.ev.t} events must use role "agent"`,
85
+ path: ["role"]
86
+ });
87
+ }
88
+ });
89
+ function createEnvelope(role, ev, opts = {}) {
90
+ return sessionEnvelopeSchema.parse({
91
+ id: opts.id ?? createId(),
92
+ time: opts.time ?? Date.now(),
93
+ role,
94
+ ...opts.turn ? { turn: opts.turn } : {},
95
+ ...opts.subagent ? { subagent: opts.subagent } : {},
96
+ ev
97
+ });
98
+ }
99
+
100
+ const MessageMetaSchema = z.object({
101
+ sentFrom: z.string().optional(),
102
+ permissionMode: z.enum(["default", "acceptEdits", "bypassPermissions", "plan", "read-only", "safe-yolo", "yolo"]).optional(),
103
+ model: z.string().nullable().optional(),
104
+ fallbackModel: z.string().nullable().optional(),
105
+ customSystemPrompt: z.string().nullable().optional(),
106
+ appendSystemPrompt: z.string().nullable().optional(),
107
+ allowedTools: z.array(z.string()).nullable().optional(),
108
+ disallowedTools: z.array(z.string()).nullable().optional(),
109
+ displayText: z.string().optional()
110
+ });
111
+
112
+ const UserMessageSchema = z.object({
113
+ role: z.literal("user"),
114
+ content: z.object({
115
+ type: z.literal("text"),
116
+ text: z.string()
117
+ }),
118
+ localKey: z.string().optional(),
119
+ meta: MessageMetaSchema.optional()
120
+ });
121
+ const AgentMessageSchema = z.object({
122
+ role: z.literal("agent"),
123
+ content: z.object({
124
+ type: z.string()
125
+ }).passthrough(),
126
+ meta: MessageMetaSchema.optional()
127
+ });
128
+ const LegacyMessageContentSchema = z.discriminatedUnion("role", [UserMessageSchema, AgentMessageSchema]);
129
+
130
+ const SessionMessageContentSchema = z.object({
131
+ c: z.string(),
132
+ t: z.literal("encrypted")
133
+ });
134
+ const SessionMessageSchema = z.object({
135
+ id: z.string(),
136
+ seq: z.number(),
137
+ localId: z.string().nullish(),
138
+ content: SessionMessageContentSchema,
139
+ createdAt: z.number(),
140
+ updatedAt: z.number()
141
+ });
142
+ const SessionProtocolMessageSchema = z.object({
143
+ role: z.literal("session"),
144
+ content: sessionEnvelopeSchema,
145
+ meta: MessageMetaSchema.optional()
146
+ });
147
+ const MessageContentSchema = z.discriminatedUnion("role", [
148
+ UserMessageSchema,
149
+ AgentMessageSchema,
150
+ SessionProtocolMessageSchema
151
+ ]);
152
+ const VersionedEncryptedValueSchema = z.object({
153
+ version: z.number(),
154
+ value: z.string()
155
+ });
156
+ const VersionedNullableEncryptedValueSchema = z.object({
157
+ version: z.number(),
158
+ value: z.string().nullable()
159
+ });
160
+ const UpdateNewMessageBodySchema = z.object({
161
+ t: z.literal("new-message"),
162
+ sid: z.string(),
163
+ message: SessionMessageSchema
164
+ });
165
+ const UpdateSessionBodySchema = z.object({
166
+ t: z.literal("update-session"),
167
+ id: z.string(),
168
+ metadata: VersionedEncryptedValueSchema.nullish(),
169
+ agentState: VersionedNullableEncryptedValueSchema.nullish()
170
+ });
171
+ const VersionedMachineEncryptedValueSchema = z.object({
172
+ version: z.number(),
173
+ value: z.string()
174
+ });
175
+ const UpdateMachineBodySchema = z.object({
176
+ t: z.literal("update-machine"),
177
+ machineId: z.string(),
178
+ metadata: VersionedMachineEncryptedValueSchema.nullish(),
179
+ daemonState: VersionedMachineEncryptedValueSchema.nullish(),
180
+ active: z.boolean().optional(),
181
+ activeAt: z.number().optional()
182
+ });
183
+ const CoreUpdateBodySchema = z.discriminatedUnion("t", [
184
+ UpdateNewMessageBodySchema,
185
+ UpdateSessionBodySchema,
186
+ UpdateMachineBodySchema
187
+ ]);
188
+ const CoreUpdateContainerSchema = z.object({
189
+ id: z.string(),
190
+ seq: z.number(),
191
+ body: CoreUpdateBodySchema,
192
+ createdAt: z.number()
193
+ });
194
+ const ApiMessageSchema = SessionMessageSchema;
195
+ const ApiUpdateNewMessageSchema = UpdateNewMessageBodySchema;
196
+ const ApiUpdateSessionStateSchema = UpdateSessionBodySchema;
197
+ const ApiUpdateMachineStateSchema = UpdateMachineBodySchema;
198
+ const UpdateBodySchema = UpdateNewMessageBodySchema;
199
+ const UpdateSchema = CoreUpdateContainerSchema;
200
+
201
+ const VoiceConversationGrantedSchema = z.object({
202
+ allowed: z.literal(true),
203
+ conversationToken: z.string(),
204
+ conversationId: z.string(),
205
+ agentId: z.string(),
206
+ elevenUserId: z.string(),
207
+ usedSeconds: z.number(),
208
+ limitSeconds: z.number()
209
+ });
210
+ const VoiceConversationDeniedSchema = z.object({
211
+ allowed: z.literal(false),
212
+ reason: z.enum(["voice_hard_limit_reached", "subscription_required", "voice_conversation_limit_reached"]),
213
+ usedSeconds: z.number(),
214
+ limitSeconds: z.number(),
215
+ agentId: z.string()
216
+ });
217
+ const VoiceConversationResponseSchema = z.discriminatedUnion("allowed", [
218
+ VoiceConversationGrantedSchema,
219
+ VoiceConversationDeniedSchema
220
+ ]);
221
+ const VoiceUsageResponseSchema = z.object({
222
+ usedSeconds: z.number(),
223
+ limitSeconds: z.number(),
224
+ conversationCount: z.number(),
225
+ conversationLimit: z.number(),
226
+ elevenUserId: z.string()
227
+ });
228
+
229
+ 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, createEnvelope, sessionEnvelopeSchema, sessionEventSchema, sessionFileEventSchema, sessionRoleSchema, sessionServiceMessageEventSchema, sessionStartEventSchema, sessionStopEventSchema, sessionTextEventSchema, sessionToolCallEndEventSchema, sessionToolCallStartEventSchema, sessionTurnEndEventSchema, sessionTurnEndStatusSchema, sessionTurnStartEventSchema };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "ggkhappy-wire",
3
+ "version": "0.1.0",
4
+ "description": "Shared message wire types and Zod schemas for Happy clients and services",
5
+ "author": "Kirill Dubovitskiy",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "homepage": "https://github.com/slopus/happy/tree/main/packages/happy-wire",
9
+ "bugs": "https://github.com/slopus/happy/issues",
10
+ "repository": "slopus/happy",
11
+ "main": "./dist/index.cjs",
12
+ "module": "./dist/index.mjs",
13
+ "types": "./dist/index.d.cts",
14
+ "exports": {
15
+ ".": {
16
+ "require": {
17
+ "types": "./dist/index.d.cts",
18
+ "default": "./dist/index.cjs"
19
+ },
20
+ "import": {
21
+ "types": "./dist/index.d.mts",
22
+ "default": "./dist/index.mjs"
23
+ }
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "package.json",
29
+ "README.md"
30
+ ],
31
+ "dependencies": {
32
+ "@paralleldrive/cuid2": "^2.2.2",
33
+ "zod": "3.25.76"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": ">=20",
37
+ "pkgroll": "^2.14.2",
38
+ "release-it": "^19.0.6",
39
+ "shx": "^0.3.3",
40
+ "typescript": "5.9.3",
41
+ "vitest": "^3.2.4"
42
+ },
43
+ "publishConfig": {
44
+ "registry": "https://registry.npmjs.org"
45
+ },
46
+ "scripts": {
47
+ "typecheck": "tsc --noEmit",
48
+ "build": "shx rm -rf dist && tsc --noEmit && pkgroll",
49
+ "test": "pnpm run build && vitest run",
50
+ "release": "npx --no-install release-it"
51
+ }
52
+ }