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
17const SetupOperationId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
18// Environments supply these and clients open them, so a `javascript:` URL would run as the client.
19const SignInUrl = TrimmedNonEmptyString.check(
20 Schema.isMaxLength(16_384),
21 Schema.isPattern(/^https?:\/\//i),
22);
23
24export 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});
32export type ProviderAuthMethod = typeof ProviderAuthMethod.Type;
33
34export 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]);
66export type ProviderAuthInteraction = typeof ProviderAuthInteraction.Type;
67
68export 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 size: Schema.optionalKey(
77 Schema.Struct({
78 cols: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 500 })),
79 rows: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 200 })),
80 }),
81 ),
82 }),
83 Schema.Struct({
84 type: Schema.Literal("credentials"),
85 values: Schema.Record(SetupOperationId, Schema.String.check(Schema.isMaxLength(16_384))).check(
86 Schema.isMaxProperties(16),
87 ),
88 }),
89]);
90export type ProviderAuthResponse = typeof ProviderAuthResponse.Type;
91
92export const ProviderAuthStartInput = Schema.Struct({
93 instanceId: ProviderInstanceId,
94 methodId: Schema.optionalKey(SetupOperationId),
95 returnUrl: Schema.optionalKey(Schema.String),
96 callbackMode: Schema.optionalKey(Schema.Literals(["server", "client"])),
97});
98export type ProviderAuthStartInput = typeof ProviderAuthStartInput.Type;
99
100export const ProviderAuthRemoveMethodInput = Schema.Struct({
101 instanceId: ProviderInstanceId,
102 methodId: SetupOperationId,
103});
104
105export const ProviderAuthRespondInput = Schema.Struct({
106 instanceId: ProviderInstanceId,
107 flowId: SetupOperationId,
108 interactionId: SetupOperationId,
109 response: ProviderAuthResponse,
110});
111export type ProviderAuthRespondInput = typeof ProviderAuthRespondInput.Type;
112
113export const ProviderAuthState = Schema.Struct({
114 instanceId: ProviderInstanceId,
115 phase: Schema.Literals([
116 "idle",
117 "starting",
118 "waiting",
119 "verifying",
120 "succeeded",
121 "failed",
122 "cancelled",
123 ]),
124 flowId: Schema.NullOr(SetupOperationId),
125 authorizationUrl: Schema.NullOr(SignInUrl),
126 expiresAt: Schema.NullOr(IsoDateTime),
127 message: Schema.NullOr(Schema.String),
128 // Drop unknown auth methods rather than rejecting the entire state.
129 methods: Schema.optionalKey(
130 ForwardCompatibleArray(ProviderAuthMethod).check(Schema.isMaxLength(32)),
131 ),
132 interaction: ForwardCompatibleOptional(Schema.NullOr(ProviderAuthInteraction)),
133 credentialOwner: ForwardCompatibleOptional(Schema.Literals(["provider", "t3"])),
134});
135export type ProviderAuthState = typeof ProviderAuthState.Type;
136
137export const ProviderAuthCompleteInput = Schema.Struct({
138 instanceId: ProviderInstanceId,
139 flowId: SetupOperationId,
140 callbackUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(16_384)),
141});
142export type ProviderAuthCompleteInput = typeof ProviderAuthCompleteInput.Type;
143
144export const ProviderAuthCancelInput = Schema.Struct({
145 instanceId: ProviderInstanceId,
146 flowId: SetupOperationId,
147});
148export type ProviderAuthCancelInput = typeof ProviderAuthCancelInput.Type;
149
150const ByteCount = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
151
152export const ProviderInstallState = Schema.Struct({
153 driver: ProviderDriverKind,
154 operationId: Schema.NullOr(SetupOperationId),
155 phase: Schema.Literals([
156 "idle",
157 "downloading",
158 "extracting",
159 "verifying",
160 "succeeded",
161 "failed",
162 "cancelled",
163 ]),
164 downloadedBytes: ByteCount,
165 totalBytes: Schema.NullOr(ByteCount),
166 version: Schema.NullOr(TrimmedNonEmptyString),
167 installedVersion: Schema.NullOr(TrimmedNonEmptyString),
168 executablePath: Schema.optionalKey(Schema.NullOr(TrimmedNonEmptyString)),
169 source: Schema.optionalKey(Schema.NullOr(Schema.Literals(["managed", "local"]))),
170 canRemove: Schema.Boolean,
171 message: Schema.NullOr(Schema.String),
172});
173export type ProviderInstallState = typeof ProviderInstallState.Type;
174
175export const ProviderInstallCancelInput = Schema.Struct({
176 instanceId: ProviderInstanceId,
177 operationId: SetupOperationId,
178});
179export type ProviderInstallCancelInput = typeof ProviderInstallCancelInput.Type;
180
181/** Safe setup failure text. Never include OAuth codes, URLs, or native token data. */
182export class ProviderSetupError extends Schema.TaggedError<ProviderSetupError>()(
183 "ProviderSetupError",
184 {
185 instanceId: ProviderInstanceId,
186 operation: Schema.String,
187 detail: Schema.String,
188 cause: Schema.optional(Schema.Defect()),
189 },
190) {
191 override get message(): string {
192 return this.detail;
193 }
194}
195
196export const ChatGptReconnectProfile = Schema.Struct({
197 clientId: Schema.String.check(Schema.isPattern(/^oaiapp_[\w-]+$/u)),
198 subject: Schema.optionalKey(Schema.String),
199 email: Schema.optionalKey(Schema.NullOr(Schema.String)),
200 redirectUri: Schema.optionalKey(
201 Schema.String.check(
202 Schema.isPattern(/^http:\/\/(?:127\.0\.0\.1|localhost):[1-9]\d{0,4}\/auth\/callback$/u),
203 ),
204 ),
205 connectionLabel: Schema.optionalKey(Schema.String),
206 sharingEnabled: Schema.optionalKey(Schema.Boolean),
207 idTokenHint: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(16_384))),
208});
209export type ChatGptReconnectProfile = typeof ChatGptReconnectProfile.Type;
210export const ChatGptTransferredProfile = Schema.Struct({
211 registration: ChatGptReconnectProfile,
212 credentials: Schema.Struct({
213 clientId: Schema.String,
214 accessToken: Schema.NonEmptyString.check(Schema.isMaxLength(16_384)),
215 refreshToken: Schema.NullOr(Schema.String.check(Schema.isMaxLength(16_384))),
216 idToken: Schema.NonEmptyString.check(Schema.isMaxLength(16_384)),
217 issuer: Schema.String,
218 expiresAt: Schema.Finite,
219 earliestRefreshAt: Schema.NullOr(Schema.Finite),
220 scopes: Schema.Array(Schema.String),
221 subject: Schema.String,
222 email: Schema.NullOr(Schema.String),
223 }),
224});
225export type ChatGptTransferredProfile = typeof ChatGptTransferredProfile.Type;
226export const ChatGptReconnectProfileInput = Schema.Struct({
227 instanceId: ProviderInstanceId,
228 methodId: Schema.String,
229});
230export const ChatGptImportProfileInput = Schema.Struct({
231 instanceId: ProviderInstanceId,
232 profile: ChatGptTransferredProfile,
233});
234export const ChatGptHandoffInput = Schema.Struct({
235 instanceId: ProviderInstanceId,
236 environmentId: EnvironmentId,
237 attemptId: Schema.String.check(Schema.isMaxLength(128)),
238 returnUrl: Schema.String.check(Schema.isMaxLength(4_096)),
239 profile: Schema.NullOr(ChatGptReconnectProfile),
240});
241export type ChatGptHandoffInput = typeof ChatGptHandoffInput.Type;
242export const ChatGptHandoffState = Schema.Union([
243 Schema.Struct({ phase: Schema.Literal("auth"), state: ProviderAuthState }),
244 Schema.Struct({ phase: Schema.Literal("finished"), profile: ChatGptTransferredProfile }),
245]);
246export type ChatGptHandoffState = typeof ChatGptHandoffState.Type;