Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providerSetup.ts

252 lines8.9 KB

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 // Newer servers may add method types, interactions, or owners; older
134 // clients drop what they cannot decode instead of rejecting the state.
135 methods: Schema.optionalKey(
136 ForwardCompatibleArray(ProviderAuthMethod).check(Schema.isMaxLength(32)),
137 ),
138 interaction: ForwardCompatibleOptional(Schema.NullOr(ProviderAuthInteraction)),
139 credentialOwner: ForwardCompatibleOptional(Schema.Literals(["provider", "t3"])),
140});
141export type ProviderAuthState = typeof ProviderAuthState.Type;
142​
143export const ProviderAuthCompleteInput = Schema.Struct({
144 instanceId: ProviderInstanceId,
145 flowId: SetupOperationId,
146 callbackUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(16_384)),
147});
148export type ProviderAuthCompleteInput = typeof ProviderAuthCompleteInput.Type;
149​
150export const ProviderAuthCancelInput = Schema.Struct({
151 instanceId: ProviderInstanceId,
152 flowId: SetupOperationId,
153});
154export type ProviderAuthCancelInput = typeof ProviderAuthCancelInput.Type;
155​
156const ByteCount = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
157​
158export const ProviderInstallState = Schema.Struct({
159 driver: ProviderDriverKind,
160 operationId: Schema.NullOr(SetupOperationId),
161 phase: Schema.Literals([
162 "idle",
163 "downloading",
164 "extracting",
165 "verifying",
166 "succeeded",
167 "failed",
168 "cancelled",
169 ]),
170 downloadedBytes: ByteCount,
171 totalBytes: Schema.NullOr(ByteCount),
172 version: Schema.NullOr(TrimmedNonEmptyString),
173 installedVersion: Schema.NullOr(TrimmedNonEmptyString),
174 executablePath: Schema.optionalKey(Schema.NullOr(TrimmedNonEmptyString)),
175 source: Schema.optionalKey(Schema.NullOr(Schema.Literals(["managed", "local"]))),
176 canRemove: Schema.Boolean,
177 message: Schema.NullOr(Schema.String),
178});
179export type ProviderInstallState = typeof ProviderInstallState.Type;
180​
181export const ProviderInstallCancelInput = Schema.Struct({
182 instanceId: ProviderInstanceId,
183 operationId: SetupOperationId,
184});
185export type ProviderInstallCancelInput = typeof ProviderInstallCancelInput.Type;
186​
187/** Safe setup failure text. Never include OAuth codes, URLs, or native token data. */
188export class ProviderSetupError extends Schema.TaggedError<ProviderSetupError>()(
189 "ProviderSetupError",
190 {
191 instanceId: ProviderInstanceId,
192 operation: Schema.String,
193 detail: Schema.String,
194 cause: Schema.optional(Schema.Defect()),
195 },
196) {
197 override get message(): string {
198 return this.detail;
199 }
201​
202export const ChatGptReconnectProfile = Schema.Struct({
203 clientId: Schema.String.check(Schema.isPattern(/^oaiapp_[\w-]+$/u)),
204 subject: Schema.optionalKey(Schema.String),
205 email: Schema.optionalKey(Schema.NullOr(Schema.String)),
206 redirectUri: Schema.optionalKey(
207 Schema.String.check(
208 Schema.isPattern(/^http:\/\/(?:127\.0\.0\.1|localhost):[1-9]\d{0,4}\/auth\/callback$/u),
209 ),
210 ),
211 connectionLabel: Schema.optionalKey(Schema.String),
212 sharingEnabled: Schema.optionalKey(Schema.Boolean),
213 idTokenHint: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(16_384))),
214});
215export type ChatGptReconnectProfile = typeof ChatGptReconnectProfile.Type;
216export const ChatGptTransferredProfile = Schema.Struct({
217 registration: ChatGptReconnectProfile,
218 credentials: Schema.Struct({
219 clientId: Schema.String,
220 accessToken: Schema.NonEmptyString.check(Schema.isMaxLength(16_384)),
221 refreshToken: Schema.NullOr(Schema.String.check(Schema.isMaxLength(16_384))),
222 idToken: Schema.NonEmptyString.check(Schema.isMaxLength(16_384)),
223 issuer: Schema.String,
224 expiresAt: Schema.Finite,
225 earliestRefreshAt: Schema.NullOr(Schema.Finite),
226 scopes: Schema.Array(Schema.String),
227 subject: Schema.String,
228 email: Schema.NullOr(Schema.String),
229 }),
230});
231export type ChatGptTransferredProfile = typeof ChatGptTransferredProfile.Type;
232export const ChatGptReconnectProfileInput = Schema.Struct({
233 instanceId: ProviderInstanceId,
234 methodId: Schema.String,
235});
236export const ChatGptImportProfileInput = Schema.Struct({
237 instanceId: ProviderInstanceId,
238 profile: ChatGptTransferredProfile,
239});
240export const ChatGptHandoffInput = Schema.Struct({
241 instanceId: ProviderInstanceId,
242 environmentId: EnvironmentId,
243 attemptId: Schema.String.check(Schema.isMaxLength(128)),
244 returnUrl: Schema.String.check(Schema.isMaxLength(4_096)),
245 profile: Schema.NullOr(ChatGptReconnectProfile),
246});
247export type ChatGptHandoffInput = typeof ChatGptHandoffInput.Type;
248export const ChatGptHandoffState = Schema.Union([
249 Schema.Struct({ phase: Schema.Literal("auth"), state: ProviderAuthState }),
250 Schema.Struct({ phase: Schema.Literal("finished"), profile: ChatGptTransferredProfile }),
251]);
252export type ChatGptHandoffState = typeof ChatGptHandoffState.Type;