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