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

providerSetup.ts

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