The best way to control Claude Code, Codex and more from any computer or phone

main
ts 248 lines 8.61 KB
Raw
1 import * as Schema from "effect/Schema";
2
3 import {
4 EnvironmentId,
5 ForwardCompatibleArray,
6 ForwardCompatibleOptional,
7 IsoDateTime,
8 TrimmedNonEmptyString,
9 } from "./baseSchemas.ts";
10 import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
11
12 export const ProviderSetupInput = Schema.Struct({
13 instanceId: ProviderInstanceId,
14 });
15 export type ProviderSetupInput = typeof ProviderSetupInput.Type;
16
17 const SetupOperationId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
18 // Environments supply these and clients open them, so a `javascript:` URL would run as the client.
19 export const SignInUrl = TrimmedNonEmptyString.check(
20 Schema.isMaxLength(16_384),
21 Schema.isPattern(/^https?:\/\//i),
22 );
23
24 export const ProviderAuthMethod = Schema.Struct({
25 id: SetupOperationId,
26 accountEmail: Schema.optional(TrimmedNonEmptyString),
27 canRemove: Schema.optionalKey(Schema.Boolean),
28 name: TrimmedNonEmptyString,
29 description: Schema.NullOr(Schema.String),
30 type: Schema.Literals(["agent", "terminal", "credentials"]),
31 });
32 export type ProviderAuthMethod = typeof ProviderAuthMethod.Type;
33
34 export const ProviderAuthInteraction = Schema.Union([
35 Schema.Struct({
36 type: Schema.Literal("browser"),
37 id: SetupOperationId,
38 url: SignInUrl,
39 requiresConsent: Schema.Boolean,
40 acceptsCallback: Schema.optionalKey(Schema.Boolean),
41 }),
42 Schema.Struct({
43 type: Schema.Literal("deviceCode"),
44 id: SetupOperationId,
45 url: SignInUrl,
46 userCode: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
47 }),
48 Schema.Struct({
49 type: Schema.Literal("terminal"),
50 id: SetupOperationId,
51 output: Schema.String.check(Schema.isMaxLength(16_384)),
52 outputOffset: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
53 }),
54 Schema.Struct({
55 type: Schema.Literal("credentials"),
56 id: SetupOperationId,
57 fields: Schema.Array(
58 Schema.Struct({
59 name: SetupOperationId,
60 label: TrimmedNonEmptyString,
61 secret: Schema.Boolean,
62 }),
63 ).check(Schema.isMaxLength(16)),
64 }),
65 ]);
66 export type ProviderAuthInteraction = typeof ProviderAuthInteraction.Type;
67
68 export const ProviderAuthResponse = Schema.Union([
69 Schema.Struct({
70 type: Schema.Literal("browser"),
71 action: Schema.Literals(["accept", "decline"]),
72 }),
73 Schema.Struct({
74 type: Schema.Literal("terminal"),
75 data: Schema.String.check(Schema.isMaxLength(4_096)),
76 // `data` holds one byte per UTF-16 code unit, as in `TerminalWriteInput`.
77 binary: Schema.optionalKey(Schema.Boolean),
78 size: Schema.optionalKey(
79 Schema.Struct({
80 cols: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 500 })),
81 rows: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 200 })),
82 }),
83 ),
84 }),
85 Schema.Struct({
86 type: Schema.Literal("credentials"),
87 values: Schema.Record(SetupOperationId, Schema.String.check(Schema.isMaxLength(16_384))).check(
88 Schema.isMaxProperties(16),
89 ),
90 }),
91 ]);
92 export type ProviderAuthResponse = typeof ProviderAuthResponse.Type;
93
94 export const ProviderAuthStartInput = Schema.Struct({
95 instanceId: ProviderInstanceId,
96 methodId: Schema.optionalKey(SetupOperationId),
97 returnUrl: Schema.optionalKey(Schema.String),
98 callbackMode: Schema.optionalKey(Schema.Literals(["server", "client"])),
99 });
100 export type ProviderAuthStartInput = typeof ProviderAuthStartInput.Type;
101
102 export const ProviderAuthRemoveMethodInput = Schema.Struct({
103 instanceId: ProviderInstanceId,
104 methodId: SetupOperationId,
105 });
106
107 export const ProviderAuthRespondInput = Schema.Struct({
108 instanceId: ProviderInstanceId,
109 flowId: SetupOperationId,
110 interactionId: SetupOperationId,
111 response: ProviderAuthResponse,
112 });
113 export type ProviderAuthRespondInput = typeof ProviderAuthRespondInput.Type;
114
115 export const ProviderAuthState = Schema.Struct({
116 instanceId: ProviderInstanceId,
117 phase: Schema.Literals([
118 "idle",
119 "starting",
120 "waiting",
121 "verifying",
122 "succeeded",
123 "failed",
124 "cancelled",
125 ]),
126 flowId: Schema.NullOr(SetupOperationId),
127 authorizationUrl: Schema.NullOr(SignInUrl),
128 expiresAt: Schema.NullOr(IsoDateTime),
129 message: Schema.NullOr(Schema.String),
130 // Drop unknown auth methods rather than rejecting the entire state.
131 methods: Schema.optionalKey(
132 ForwardCompatibleArray(ProviderAuthMethod).check(Schema.isMaxLength(32)),
133 ),
134 interaction: ForwardCompatibleOptional(Schema.NullOr(ProviderAuthInteraction)),
135 credentialOwner: ForwardCompatibleOptional(Schema.Literals(["provider", "t3"])),
136 });
137 export type ProviderAuthState = typeof ProviderAuthState.Type;
138
139 export const ProviderAuthCompleteInput = Schema.Struct({
140 instanceId: ProviderInstanceId,
141 flowId: SetupOperationId,
142 callbackUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(16_384)),
143 });
144 export type ProviderAuthCompleteInput = typeof ProviderAuthCompleteInput.Type;
145
146 export const ProviderAuthCancelInput = Schema.Struct({
147 instanceId: ProviderInstanceId,
148 flowId: SetupOperationId,
149 });
150 export type ProviderAuthCancelInput = typeof ProviderAuthCancelInput.Type;
151
152 const ByteCount = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
153
154 export const ProviderInstallState = Schema.Struct({
155 driver: ProviderDriverKind,
156 operationId: Schema.NullOr(SetupOperationId),
157 phase: Schema.Literals([
158 "idle",
159 "downloading",
160 "extracting",
161 "verifying",
162 "succeeded",
163 "failed",
164 "cancelled",
165 ]),
166 downloadedBytes: ByteCount,
167 totalBytes: Schema.NullOr(ByteCount),
168 version: Schema.NullOr(TrimmedNonEmptyString),
169 installedVersion: Schema.NullOr(TrimmedNonEmptyString),
170 executablePath: Schema.optionalKey(Schema.NullOr(TrimmedNonEmptyString)),
171 source: Schema.optionalKey(Schema.NullOr(Schema.Literals(["managed", "local"]))),
172 canRemove: Schema.Boolean,
173 message: Schema.NullOr(Schema.String),
174 });
175 export type ProviderInstallState = typeof ProviderInstallState.Type;
176
177 export const ProviderInstallCancelInput = Schema.Struct({
178 instanceId: ProviderInstanceId,
179 operationId: SetupOperationId,
180 });
181 export type ProviderInstallCancelInput = typeof ProviderInstallCancelInput.Type;
182
183 /** Safe setup failure text. Never include OAuth codes, URLs, or native token data. */
184 export class ProviderSetupError extends Schema.TaggedError<ProviderSetupError>()(
185 "ProviderSetupError",
186 {
187 instanceId: ProviderInstanceId,
188 operation: Schema.String,
189 detail: Schema.String,
190 cause: Schema.optional(Schema.Defect()),
191 },
192 ) {
193 override get message(): string {
194 return this.detail;
195 }
196 }
197
198 export const ChatGptReconnectProfile = Schema.Struct({
199 clientId: Schema.String.check(Schema.isPattern(/^oaiapp_[\w-]+$/u)),
200 subject: Schema.optionalKey(Schema.String),
201 email: Schema.optionalKey(Schema.NullOr(Schema.String)),
202 redirectUri: Schema.optionalKey(
203 Schema.String.check(
204 Schema.isPattern(/^http:\/\/(?:127\.0\.0\.1|localhost):[1-9]\d{0,4}\/auth\/callback$/u),
205 ),
206 ),
207 connectionLabel: Schema.optionalKey(Schema.String),
208 sharingEnabled: Schema.optionalKey(Schema.Boolean),
209 idTokenHint: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(16_384))),
210 });
211 export type ChatGptReconnectProfile = typeof ChatGptReconnectProfile.Type;
212 export const ChatGptTransferredProfile = Schema.Struct({
213 registration: ChatGptReconnectProfile,
214 credentials: Schema.Struct({
215 clientId: Schema.String,
216 accessToken: Schema.NonEmptyString.check(Schema.isMaxLength(16_384)),
217 refreshToken: Schema.NullOr(Schema.String.check(Schema.isMaxLength(16_384))),
218 idToken: Schema.NonEmptyString.check(Schema.isMaxLength(16_384)),
219 issuer: Schema.String,
220 expiresAt: Schema.Finite,
221 earliestRefreshAt: Schema.NullOr(Schema.Finite),
222 scopes: Schema.Array(Schema.String),
223 subject: Schema.String,
224 email: Schema.NullOr(Schema.String),
225 }),
226 });
227 export type ChatGptTransferredProfile = typeof ChatGptTransferredProfile.Type;
228 export const ChatGptReconnectProfileInput = Schema.Struct({
229 instanceId: ProviderInstanceId,
230 methodId: Schema.String,
231 });
232 export const ChatGptImportProfileInput = Schema.Struct({
233 instanceId: ProviderInstanceId,
234 profile: ChatGptTransferredProfile,
235 });
236 export const ChatGptHandoffInput = Schema.Struct({
237 instanceId: ProviderInstanceId,
238 environmentId: EnvironmentId,
239 attemptId: Schema.String.check(Schema.isMaxLength(128)),
240 returnUrl: Schema.String.check(Schema.isMaxLength(4_096)),
241 profile: Schema.NullOr(ChatGptReconnectProfile),
242 });
243 export type ChatGptHandoffInput = typeof ChatGptHandoffInput.Type;
244 export const ChatGptHandoffState = Schema.Union([
245 Schema.Struct({ phase: Schema.Literal("auth"), state: ProviderAuthState }),
246 Schema.Struct({ phase: Schema.Literal("finished"), profile: ChatGptTransferredProfile }),
247 ]);
248 export type ChatGptHandoffState = typeof ChatGptHandoffState.Type;