Open History to see the last commit
1import { ProviderSetupError, type ProviderInstanceId } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Stream from "effect/Stream";
5
6import { ProviderAuthService } from "../Services/ProviderAuthService.ts";
7import { ProviderInstanceRegistry } from "../Services/ProviderInstanceRegistry.ts";
8import { ProviderService } from "../Services/ProviderService.ts";
9import { ProviderSessionDirectory } from "../Services/ProviderSessionDirectory.ts";
10
11export const makeProviderAuthService = Effect.gen(function* () {
12 const registry = yield* ProviderInstanceRegistry;
13 const providers = yield* ProviderService;
14 const directory = yield* ProviderSessionDirectory;
15
16 const getController = Effect.fn("ProviderAuthService.getController")(function* (
17 instanceId: ProviderInstanceId,
18 operation: string,
19 ) {
20 const instance = yield* registry.getInstance(instanceId);
21 if (!instance?.auth) {
22 return yield* new ProviderSetupError({
23 instanceId,
24 operation,
25 detail: instance
26 ? "This provider does not support sign-in in T3 Code."
27 : "This provider instance is no longer available.",
28 });
29 }
30 return instance.auth;
31 });
32
33 const stopSessions = Effect.fn("ProviderAuthService.stopSessions")(function* (
34 instanceId: ProviderInstanceId,
35 ) {
36 const bindings = yield* directory.listBindings().pipe(
37 Effect.mapError(
38 () =>
39 new ProviderSetupError({
40 instanceId,
41 operation: "stopSessions",
42 detail: "Could not read the provider's active sessions. Try again.",
43 }),
44 ),
45 );
46 const sessions = yield* providers.listSessions();
47 const threadIds = new Set(
48 bindings
49 .filter(
50 (binding) => binding.providerInstanceId === instanceId && binding.status !== "stopped",
51 )
52 .map((binding) => binding.threadId),
53 );
54 for (const session of sessions) {
55 if (session.providerInstanceId === instanceId) {
56 threadIds.add(session.threadId);
57 }
58 }
59 yield* Effect.forEach(
60 threadIds,
61 (threadId) =>
62 providers.stopSession({ threadId }).pipe(
63 Effect.mapError(
64 () =>
65 new ProviderSetupError({
66 instanceId,
67 operation: "stopSessions",
68 detail: "Could not stop all sessions for this provider. Try again.",
69 }),
70 ),
71 ),
72 { discard: true },
73 );
74 });
75
76 return ProviderAuthService.of({
77 start: Effect.fn("ProviderAuthService.start")(function* (input, ownerSessionId) {
78 const auth = yield* getController(input.instanceId, "start");
79 return yield* auth.start(ownerSessionId, stopSessions(input.instanceId));
80 }),
81 complete: Effect.fn("ProviderAuthService.complete")(function* (input, ownerSessionId) {
82 const auth = yield* getController(input.instanceId, "complete");
83 return yield* auth.complete(ownerSessionId, input);
84 }),
85 cancel: Effect.fn("ProviderAuthService.cancel")(function* (input, ownerSessionId) {
86 const auth = yield* getController(input.instanceId, "cancel");
87 return yield* auth.cancel(ownerSessionId, input.flowId);
88 }),
89 logout: Effect.fn("ProviderAuthService.logout")(function* (input) {
90 const auth = yield* getController(input.instanceId, "logout");
91 return yield* auth.logout(stopSessions(input.instanceId));
92 }),
93 subscribe: (input, ownerSessionId) =>
94 Effect.gen(function* () {
95 const changes = yield* registry.subscribeChanges;
96 const initial = yield* getController(input.instanceId, "subscribe");
97 return Stream.concat(
98 Stream.succeed(initial),
99 Stream.fromSubscription(changes).pipe(
100 Stream.mapEffect(() => getController(input.instanceId, "subscribe")),
101 ),
102 ).pipe(
103 Stream.changesWith((previous, next) => previous === next),
104 Stream.switchMap((auth) => auth.subscribe(ownerSessionId)),
105 );
106 }).pipe(Stream.unwrap),
107 tryHandlePromptCommand: Effect.fn("ProviderAuthService.tryHandlePromptCommand")(
108 function* (input) {
109 const instance = yield* registry.getInstance(input.instanceId);
110 if (!instance?.auth?.isLogoutPrompt?.(input.text, input.hasAttachments)) {
111 return false;
112 }
113 yield* instance.auth.logout(stopSessions(input.instanceId));
114 return true;
115 },
116 ),
117 });
118});
119
120export const ProviderAuthServiceLive = Layer.effect(ProviderAuthService, makeProviderAuthService);