@kya-os/contracts 1.7.25 → 1.7.26

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.
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ /**
3
+ * Compute Process Contracts
4
+ *
5
+ * Schemas for the @kya-os/compute <-> AgentShield API communication.
6
+ * These define the heartbeat protocol, config bundle format,
7
+ * and control server status frames.
8
+ *
9
+ * @package @kya-os/contracts/compute
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ControlStatusSchema = exports.ConfigBundleSchema = exports.HeartbeatResponseSchema = exports.ComputeCommandSchema = exports.UpdateVersionCommandSchema = exports.UpdateConfigCommandSchema = exports.RestartCommandSchema = exports.StopCommandSchema = exports.HeartbeatRequestSchema = void 0;
13
+ const zod_1 = require("zod");
14
+ const agent_deployment_js_1 = require("./agent-deployment.js");
15
+ // ============================================================================
16
+ // Heartbeat Protocol
17
+ // ============================================================================
18
+ /**
19
+ * Heartbeat request body.
20
+ * Sent by compute process: POST /api/v1/molti/agents/{did}/heartbeat
21
+ * Auth: X-Heartbeat-Token header
22
+ */
23
+ exports.HeartbeatRequestSchema = zod_1.z.object({
24
+ /** Agent DID this heartbeat is for */
25
+ agentDid: agent_deployment_js_1.DidStringSchema,
26
+ /** Current compute process status */
27
+ status: zod_1.z.enum(["starting", "running", "error", "stopping"]),
28
+ /** @kya-os/compute package version (from package.json) */
29
+ computeVersion: zod_1.z.string().min(1).max(50),
30
+ /** Installed OpenClaw version (from npm ls) */
31
+ openclawVersion: zod_1.z.string().max(50).optional(),
32
+ /** Fly machine ID (from FLY_MACHINE_ID env var) */
33
+ machineId: zod_1.z.string().max(255).optional(),
34
+ /** Seconds since compute process started */
35
+ uptimeSeconds: zod_1.z.number().nonnegative().optional(),
36
+ /** Current RSS memory in MB */
37
+ memoryMb: zod_1.z.number().nonnegative().optional(),
38
+ });
39
+ // ============================================================================
40
+ // Compute Commands (Discriminated Union)
41
+ // ============================================================================
42
+ /**
43
+ * Stop command — graceful shutdown, no payload accepted.
44
+ */
45
+ exports.StopCommandSchema = zod_1.z
46
+ .object({
47
+ type: zod_1.z.literal("stop"),
48
+ })
49
+ .strict();
50
+ /**
51
+ * Restart command — restart the compute process, no payload accepted.
52
+ */
53
+ exports.RestartCommandSchema = zod_1.z
54
+ .object({
55
+ type: zod_1.z.literal("restart"),
56
+ })
57
+ .strict();
58
+ /**
59
+ * Update config command — triggers config re-fetch, no payload accepted.
60
+ */
61
+ exports.UpdateConfigCommandSchema = zod_1.z
62
+ .object({
63
+ type: zod_1.z.literal("update_config"),
64
+ })
65
+ .strict();
66
+ /**
67
+ * Update version command — requires target version in payload.
68
+ */
69
+ exports.UpdateVersionCommandSchema = zod_1.z
70
+ .object({
71
+ type: zod_1.z.literal("update_version"),
72
+ payload: zod_1.z.object({
73
+ /** Target OpenClaw version to install */
74
+ version: zod_1.z.string().min(1).max(50),
75
+ }),
76
+ })
77
+ .strict();
78
+ /**
79
+ * Command that Agent Shield can send to the compute process
80
+ * via the heartbeat response.
81
+ *
82
+ * Discriminated union by `type` field:
83
+ * - `stop` / `restart` / `update_config` — no payload
84
+ * - `update_version` — requires `{ version: string }` payload
85
+ */
86
+ exports.ComputeCommandSchema = zod_1.z.discriminatedUnion("type", [
87
+ exports.StopCommandSchema,
88
+ exports.RestartCommandSchema,
89
+ exports.UpdateConfigCommandSchema,
90
+ exports.UpdateVersionCommandSchema,
91
+ ]);
92
+ /**
93
+ * Heartbeat response from Agent Shield.
94
+ * Returned by: POST /api/v1/molti/agents/{did}/heartbeat
95
+ */
96
+ exports.HeartbeatResponseSchema = zod_1.z.object({
97
+ /** Always true for successful heartbeats */
98
+ ack: zod_1.z.literal(true),
99
+ /** Pending commands for the compute process to execute */
100
+ commands: zod_1.z.array(exports.ComputeCommandSchema).default([]),
101
+ /** ISO timestamp of last config change (compute compares to detect updates) */
102
+ configUpdatedAt: zod_1.z.string().datetime().optional(),
103
+ });
104
+ // ============================================================================
105
+ // Config Bundle
106
+ // ============================================================================
107
+ /**
108
+ * Config bundle returned by Agent Shield to the compute process.
109
+ * Fetched on boot: GET /api/v1/molti/deployments/{did}/config-bundle
110
+ * Auth: X-Heartbeat-Token header (for managed) or X-API-Key (for BYOK)
111
+ *
112
+ * IMPORTANT: This extends the existing config-bundle endpoint.
113
+ * BYOK deployments continue to use the existing shape (version 1).
114
+ * Managed deployments use this shape (version 2+).
115
+ *
116
+ * **Private Key Delivery**: The agent's private key is NOT included in the config bundle.
117
+ * It is delivered as the `AGENT_PRIVATE_KEY` environment variable at Fly machine creation
118
+ * time and persists in the machine's env. This avoids transmitting the key on every
119
+ * config fetch and keeps it out of the config-bundle response surface.
120
+ */
121
+ exports.ConfigBundleSchema = zod_1.z.object({
122
+ /** Schema version — 2 for managed bundles */
123
+ version: zod_1.z.number().int().positive(),
124
+ /** Kill switch — if true, compute must exit immediately */
125
+ killed: zod_1.z.boolean(),
126
+ /** Target OpenClaw version to install */
127
+ openclawVersion: zod_1.z.string().min(1).max(50),
128
+ /** Agent personality/system prompt file contents (max 100KB) */
129
+ soulFile: zod_1.z.string().max(100000).nullable(),
130
+ /** Gateway auth token for OpenClaw */
131
+ gatewayToken: zod_1.z.string().min(1).max(500),
132
+ /** Channel configurations */
133
+ channels: zod_1.z.object({
134
+ telegram: zod_1.z
135
+ .object({
136
+ enabled: zod_1.z.boolean(),
137
+ token: zod_1.z.string().optional(),
138
+ })
139
+ .optional(),
140
+ discord: zod_1.z
141
+ .object({
142
+ enabled: zod_1.z.boolean(),
143
+ token: zod_1.z.string().optional(),
144
+ })
145
+ .optional(),
146
+ slack: zod_1.z
147
+ .object({
148
+ enabled: zod_1.z.boolean(),
149
+ token: zod_1.z.string().optional(),
150
+ appToken: zod_1.z.string().optional(),
151
+ })
152
+ .optional(),
153
+ }),
154
+ /** AI provider configuration — key is decrypted, sent over TLS */
155
+ aiProvider: zod_1.z.object({
156
+ type: zod_1.z.enum(["anthropic", "openai"]),
157
+ apiKey: zod_1.z.string().min(1).max(500),
158
+ }),
159
+ /** ISO timestamp of last config modification */
160
+ updatedAt: zod_1.z.string().datetime(),
161
+ });
162
+ // ============================================================================
163
+ // Control Server (WebSocket :18790)
164
+ // ============================================================================
165
+ /**
166
+ * Status frame pushed from compute control server to dashboard.
167
+ * Sent on connect + every 5 seconds.
168
+ */
169
+ exports.ControlStatusSchema = zod_1.z.object({
170
+ /** Whether the agent runtime is healthy */
171
+ healthy: zod_1.z.boolean(),
172
+ /** Seconds since compute process started */
173
+ uptimeSeconds: zod_1.z.number().nonnegative(),
174
+ /** Installed OpenClaw version */
175
+ openclawVersion: zod_1.z.string().min(1).max(50),
176
+ /** @kya-os/compute version */
177
+ computeVersion: zod_1.z.string().min(1).max(50),
178
+ /** Number of active WS connections to control server */
179
+ activeConnections: zod_1.z.number().int().nonnegative(),
180
+ /** Current RSS memory in MB */
181
+ memoryMb: zod_1.z.number().nonnegative(),
182
+ /** CPU usage percentage (0-100) */
183
+ cpuPercent: zod_1.z.number().nonnegative().max(100),
184
+ });