Repository navigation
Expand file tree
/
Copy pathcontrol.ts
More file actions
156 lines (153 loc) · 5.37 KB
/
Copy pathcontrol.ts
File metadata and controls
156 lines (153 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { z } from 'zod';
import { automationRegistrationSchema, automationSettingsSchema } from './conditions';
import { RpcSecretEnvelopeSchema, RpcSecretPublicKeySchema } from '../rpc-secret';
const id = z.string().min(1).max(512);
export const AUTOMATION_CONTROL_PATH = '/automation-control';
export const automationCommandSchema = z.discriminatedUnion('type', [
automationRegistrationSchema
.extend({ type: z.literal('register'), sessionId: id.optional() })
.strict(),
z
.object({
type: z.literal('configure'),
sessionId: id.optional(),
settings: automationSettingsSchema,
})
.strict(),
z.object({ type: z.literal('settings'), sessionId: id.optional() }).strict(),
z.object({ type: z.literal('list'), sessionId: id.optional() }).strict(),
z.object({ type: z.literal('show'), id }).strict(),
z.object({ type: z.literal('cancel'), id }).strict(),
z
.object({
type: z.literal('activities'),
sessionId: id.optional(),
limit: z.number().int().min(1).max(100).default(50),
before: z.object({ updatedAt: z.number().int().nonnegative(), id }).strict().optional(),
})
.strict(),
]);
export const automationControlRequestSchema = z
.object({
machineId: id,
workspaceId: id,
requesterSessionId: id.optional(),
command: automationCommandSchema,
})
.strict();
export type AutomationControlRequest = z.infer<typeof automationControlRequestSchema>;
export type AutomationCommand = z.infer<typeof automationCommandSchema>;
export const automationControlResponseSchema = z.discriminatedUnion('ok', [
z.object({ ok: z.literal(true), result: z.unknown() }).strict(),
z.object({ ok: z.literal(false), error: z.string() }).strict(),
]);
export type AutomationControlResponse = z.infer<typeof automationControlResponseSchema>;
/** Remote UI controls are restricted to one Session and never accept programs. */
export const remoteAutomationOperationSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('list'), after: id.optional() }).strict(),
z.object({ type: z.literal('settings') }).strict(),
z.object({ type: z.literal('configure'), settings: automationSettingsSchema }).strict(),
z.object({ type: z.literal('cancel'), id }).strict(),
]);
export const remoteAutomationIntentSchema = z
.object({
workspaceId: id,
machineId: id,
sessionId: id,
requesterUserId: id,
runtimeNonce: z.string().uuid(),
requestId: z.string().uuid(),
responseKey: RpcSecretPublicKeySchema,
operation: remoteAutomationOperationSchema,
})
.strict();
export const remoteAutomationProofSchema = z
.object({
runtimeNonce: z.string().uuid(),
requestId: z.string().uuid(),
requestToken: z.string().min(1).max(8192),
})
.strict();
export type RemoteAutomationOperation = z.infer<typeof remoteAutomationOperationSchema>;
export type RemoteAutomationIntent = z.infer<typeof remoteAutomationIntentSchema>;
export type RemoteAutomationProof = z.infer<typeof remoteAutomationProofSchema>;
export const remoteAutomationWaitSchema = z
.object({
id,
sessionId: id,
reason: z.string().max(2000).optional(),
state: z.enum(['waiting', 'checking', 'matched', 'cancelled', 'expired', 'failed']),
nextCheckAt: z.number(),
expiresAt: z.number().optional(),
continuation: z
.object({ state: z.string(), reason: z.string().optional() })
.strict()
.optional(),
})
.strict();
export const remoteAutomationResultSchema = z.discriminatedUnion('type', [
z
.object({
type: z.literal('list'),
waits: z.array(remoteAutomationWaitSchema),
next: id.optional(),
})
.strict(),
z.object({ type: z.literal('settings'), settings: automationSettingsSchema }).strict(),
z.object({ type: z.literal('cancel') }).strict(),
]);
export type RemoteAutomationResult = z.infer<typeof remoteAutomationResultSchema>;
export const remoteAutomationHandshakeSchema = z.discriminatedUnion('success', [
z
.object({
type: z.literal('machine/automation-control_response'),
machineId: id,
success: z.literal(true),
runtimeNonce: z.string().uuid(),
})
.strict(),
z
.object({
type: z.literal('machine/automation-control_response'),
machineId: id,
success: z.literal(false),
error: z.string(),
})
.strict(),
]);
export const remoteAutomationRpcResultSchema = z.discriminatedUnion('success', [
z
.object({
type: z.literal('session/automation-control_response'),
sessionId: id,
success: z.literal(true),
result: remoteAutomationResultSchema,
})
.strict(),
z
.object({
type: z.literal('session/automation-control_response'),
sessionId: id,
success: z.literal(false),
error: z.string(),
})
.strict(),
]);
export type RemoteAutomationHandshake = z.infer<typeof remoteAutomationHandshakeSchema>;
export type RemoteAutomationRpcResult = z.infer<typeof remoteAutomationRpcResultSchema>;
export const remoteAutomationSecretEnvelopeSchema = RpcSecretEnvelopeSchema.extend({
ciphertext: z
.string()
.min(1)
.max(65_536)
.regex(/^[A-Za-z0-9_-]+$/),
});
export const remoteAutomationEncryptedResponseSchema = z
.object({
type: z.literal('session/automation-control_encrypted'),
payload: remoteAutomationSecretEnvelopeSchema,
})
.strict();
export type RemoteAutomationEncryptedResponse = z.infer<
typeof remoteAutomationEncryptedResponseSchema
>;