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