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

ProviderAuthService.ts

104 lines4.1 KB

1import type {
2 ChatGptReconnectProfile,
3 ChatGptTransferredProfile,
4 ProviderAuthRespondInput,
5 ProviderAuthStartInput,
6 ProviderAuthState,
7 ProviderInstanceId,
8 ProviderSetupError,
9} from "@t3tools/contracts";
10import * as Context from "effect/Context";
11import type * as Effect from "effect/Effect";
12import type * as Stream from "effect/Stream";
13import type * as Scope from "effect/Scope";
14​
15export interface ProviderAuthController {
16 /** Equal keys mean these instances share credentials on this environment. */
17 readonly credentialBinding?: { readonly owner: "provider" | "t3"; readonly key: string };
18 readonly reconnectProfile?: (
19 methodId: string,
20 ) => Effect.Effect<ChatGptReconnectProfile | null, ProviderSetupError>;
21 readonly importProfile?: (
22 profile: ChatGptTransferredProfile,
23 stopSessions: Effect.Effect<void, ProviderSetupError>,
24 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
25 readonly adoptCredentials?: (
26 update: Effect.Effect<void, ProviderSetupError>,
27 stopSessions: Effect.Effect<void, ProviderSetupError>,
28 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
29 readonly isChangingCredentials?: Effect.Effect<boolean>;
30 readonly invalidate?: Effect.Effect<void>;
31 readonly refreshMethods?: Effect.Effect<void>;
32 readonly withAccess?: <A, E, R>(
33 task: Effect.Effect<A, E, R>,
34 ) => Effect.Effect<A, E | ProviderSetupError, R | Scope.Scope>;
35 readonly start: (
36 ownerSessionId: string,
37 stopSessions?: Effect.Effect<void, ProviderSetupError>,
38 methodId?: string,
39 returnUrl?: string,
40 callbackMode?: "server" | "client",
41 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
42 readonly complete: (
43 ownerSessionId: string,
44 input: { readonly flowId: string; readonly callbackUrl: string },
45 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
46 readonly cancel: (
47 ownerSessionId: string,
48 flowId: string,
49 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
50 readonly respond?: (
51 ownerSessionId: string,
52 input: ProviderAuthRespondInput,
53 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
54 /** The controller closes process admission before it stops routed sessions. */
55 readonly logout: (
56 stopSessions: Effect.Effect<void, ProviderSetupError>,
57 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
58 readonly subscribe: (ownerSessionId: string) => Stream.Stream<ProviderAuthState>;
59 readonly isLogoutPrompt?: (text: string, hasAttachments: boolean) => boolean;
61​
62interface ProviderAuthTarget {
63 readonly instanceId: ProviderInstanceId;
65​
66export interface ProviderAuthServiceShape {
67 readonly reconnectProfile: (
68 input: ProviderAuthTarget & { methodId: string },
69 ) => Effect.Effect<ChatGptReconnectProfile | null, ProviderSetupError>;
70 readonly importProfile: (
71 input: ProviderAuthTarget & { profile: ChatGptTransferredProfile },
72 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
73 readonly start: (
74 input: ProviderAuthStartInput,
75 ownerSessionId: string,
76 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
77 readonly complete: (
78 input: ProviderAuthTarget & { readonly flowId: string; readonly callbackUrl: string },
79 ownerSessionId: string,
80 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
81 readonly respond: (
82 input: ProviderAuthRespondInput,
83 ownerSessionId: string,
84 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
85 readonly cancel: (
86 input: ProviderAuthTarget & { readonly flowId: string },
87 ownerSessionId: string,
88 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
89 readonly logout: (
90 input: ProviderAuthTarget,
91 ) => Effect.Effect<ProviderAuthState, ProviderSetupError>;
92 readonly subscribe: (
93 input: ProviderAuthTarget,
94 ownerSessionId: string,
95 ) => Stream.Stream<ProviderAuthState, ProviderSetupError>;
96 readonly tryHandlePromptCommand: (
97 input: ProviderAuthTarget & { readonly text: string; readonly hasAttachments: boolean },
98 ) => Effect.Effect<boolean, ProviderSetupError>;
100​
101export class ProviderAuthService extends Context.Service<
102 ProviderAuthService,
103 ProviderAuthServiceShape
104>()("t3/provider/Services/ProviderAuthService") {}