@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 359 lines 13.5 KB
Raw
1 import {
2 Array as Arr,
3 Data,
4 Deferred,
5 Effect,
6 Result,
7 Option,
8 Predicate,
9 Schema,
10 Context,
11 Layer,
12 type Scope,
13 } from 'effect';
14 import { JsonRecordSchema } from 'src/effects/json';
15 import type {
16 RealtimeAuthResponse as CliRealtimeAuthResponse,
17 RealtimeCredentialsResponse as CliRealtimeCredentialsResponse,
18 } from '@composio/client/resources/_internal/cli';
19 import { ComposioClientSingleton, ComposioSessionRepository } from 'src/services/composio-clients';
20 import { idempotent, validated } from 'src/services/composio-request';
21
22 const RawRealtimeEvent = JsonRecordSchema;
23 type RawRealtimeEvent = typeof RawRealtimeEvent.Type;
24 const decodeRawRealtimeEvent = Schema.decodeUnknownOption(RawRealtimeEvent);
25
26 const ChunkedRealtimeEvent = Schema.Struct({
27 id: Schema.String,
28 index: Schema.Int,
29 chunk: Schema.String,
30 final: Schema.Boolean,
31 });
32 const decodeChunkedRealtimeEvent = Schema.decodeUnknownOption(ChunkedRealtimeEvent);
33
34 export class TriggerRealtimeSubscriptionError extends Data.TaggedError(
35 'services/TriggerRealtimeSubscriptionError'
36 )<{
37 readonly message: string;
38 readonly cause?: unknown;
39 }> {}
40
41 export interface ListenOptions {
42 /**
43 * Runs once Pusher confirmed the subscription (`pusher:subscription_succeeded`), in a scope
44 * that stays open while it does: the place to register for a cooperative stop
45 * (`ShutdownSignal.cooperate`).
46 */
47 readonly onSubscribed?: Effect.Effect<void, never, Scope.Scope>;
48 }
49
50 const subscriptionError = (message: string) => (cause: unknown) =>
51 new TriggerRealtimeSubscriptionError({ message, cause });
52
53 type PusherConstructor = (typeof import('pusher-js'))['default'];
54
55 const isPusherConstructor = (value: unknown): value is PusherConstructor =>
56 Predicate.isFunction(value);
57
58 const propertyOf = (value: unknown, property: string): unknown =>
59 Predicate.hasProperty(value, property) ? value[property] : undefined;
60
61 /**
62 * Resolves the Pusher constructor across ESM/CJS interop shapes. pusher-js
63 * ships CJS and has changed its export shape across versions: 8.4.x and 8.6.0
64 * export the constructor itself (`module.exports = Pusher`), while 8.5.0's
65 * Node bundle exports a namespace object (`module.exports = { Pusher }`) — an
66 * undocumented upstream packaging regression (pusher/pusher-js#935, fixed by
67 * pusher/pusher-js#936, first released in 8.6.0). With 8.5.0,
68 * `module.default` stops being callable under both Node and Bun — from source
69 * and in compiled binaries alike (issue #3918). The probes cover, in order: a
70 * defensively double-wrapped default (`module.default.default`), the direct
71 * class export (`module.default`, 8.4.0/8.6.0), a raw CJS module that is
72 * itself the constructor, and the named `Pusher` export on either level
73 * (`module.default.Pusher` is the 8.5.0 shape that crashed compiled release
74 * binaries; runtimes also hoist it to `module.Pusher`).
75 *
76 * Exported for tests.
77 */
78 export const resolvePusherConstructor = (
79 pusherModule: unknown
80 ): Option.Option<PusherConstructor> => {
81 const moduleDefault = propertyOf(pusherModule, 'default');
82 return Arr.findFirst(
83 [
84 propertyOf(moduleDefault, 'default'),
85 moduleDefault,
86 pusherModule,
87 propertyOf(moduleDefault, 'Pusher'),
88 propertyOf(pusherModule, 'Pusher'),
89 ],
90 isPusherConstructor
91 );
92 };
93
94 /**
95 * Service for listening to trigger events over Composio CLI realtime channels.
96 * Uses:
97 * - `cli.realtime.credentials` to fetch Pusher credentials + project nano id
98 * - `cli.realtime.auth` for private channel auth callbacks
99 */
100 const makeTriggersRealtime = Effect.gen(function* () {
101 const sessionRepo = yield* ComposioSessionRepository;
102 const clientSingleton = yield* ComposioClientSingleton;
103 const services = yield* Effect.context<never>();
104
105 const listenWith = (params: {
106 getRealtimeCredentials: () => Effect.Effect<
107 CliRealtimeCredentialsResponse,
108 TriggerRealtimeSubscriptionError
109 >;
110 authRealtimeChannel: (params: {
111 channel_name: string;
112 socket_id: string;
113 }) => Effect.Effect<CliRealtimeAuthResponse, TriggerRealtimeSubscriptionError>;
114 onEvent: (data: RawRealtimeEvent) => void;
115 options?: ListenOptions;
116 }) =>
117 Effect.acquireUseRelease(
118 Effect.gen(function* () {
119 const creds = yield* params.getRealtimeCredentials();
120 const channelName = `private-cli-${creds.project_id}`;
121
122 const pusherModule = yield* Effect.tryPromise({
123 try: () => import('pusher-js'),
124 catch: subscriptionError('Failed to load the realtime client'),
125 });
126
127 const Pusher = yield* Effect.fromOption(resolvePusherConstructor(pusherModule)).pipe(
128 Effect.mapError(subscriptionError('Realtime client module does not expose a constructor'))
129 );
130
131 const pusher = yield* Effect.try({
132 try: () =>
133 new Pusher(creds.pusher_key, {
134 cluster: creds.pusher_cluster,
135 channelAuthorization: {
136 customHandler: (authOptions, callback) => {
137 const channel_name = authOptions.channelName;
138 const socket_id = authOptions.socketId;
139
140 const doAuth = async () => {
141 const response = await Effect.runPromiseWith(services)(
142 params.authRealtimeChannel({
143 channel_name,
144 socket_id,
145 })
146 );
147 // Pusher private channels verify signatures without channel_data.
148 // Some auth endpoints may still return channel_data, which can cause
149 // "Invalid signature" if included in the verification input.
150 const normalizedResponse = channel_name.startsWith('private-')
151 ? { auth: response.auth }
152 : response;
153 return normalizedResponse;
154 };
155
156 void doAuth()
157 .then(data => callback(null, data))
158 .catch(cause =>
159 callback(cause instanceof Error ? cause : new Error(String(cause)), null)
160 );
161 },
162 },
163 }),
164 catch: subscriptionError('Failed to construct the realtime client'),
165 });
166
167 const channel = pusher.subscribe(channelName);
168
169 // Pusher confirms or rejects the subscription later, once the socket is connected and the
170 // channel is authorized. pusher-js does not catch listener exceptions, so these callbacks
171 // only complete the Deferred and never throw.
172 const subscribed = Deferred.makeUnsafe<void, TriggerRealtimeSubscriptionError>();
173 channel.bind('pusher:subscription_succeeded', () => {
174 Deferred.doneUnsafe(subscribed, Effect.void);
175 });
176 channel.bind('pusher:subscription_error', (status: unknown) => {
177 Deferred.doneUnsafe(
178 subscribed,
179 Effect.fail(subscriptionError('Failed to subscribe to realtime trigger events')(status))
180 );
181 });
182
183 // Pusher has a 10 KB per-message limit. When the Composio backend
184 // produces a trigger payload that exceeds this, it splits the JSON
185 // into numbered chunks sent as separate `chunked-trigger_to_client`
186 // events. We reassemble them here by accumulating chunks keyed by
187 // event id, then joining once the final chunk arrives and every
188 // intermediate index is present.
189 //
190 // Guard-rails:
191 // - Reject chunk indices outside [0, MAX_CHUNK_INDEX] to prevent a
192 // single malformed event from allocating a huge sparse array.
193 // - Evict incomplete entries older than CHUNK_TTL_MS so that lost
194 // chunks don't leak memory over long-running sessions.
195 // - Cap the total number of in-flight reassemblies to bound memory
196 // usage even under pathological input.
197 const MAX_CHUNK_INDEX = 1_000;
198 const CHUNK_TTL_MS = 60_000;
199 const MAX_PENDING_CHUNKS = 100;
200
201 type PendingChunkedEvent = {
202 chunks: string[];
203 receivedFinal: boolean;
204 createdAt: number;
205 };
206
207 const chunkedEvents = new Map<string, PendingChunkedEvent>();
208
209 // Periodic sweep: drop entries that have been sitting incomplete for
210 // longer than CHUNK_TTL_MS. Runs every CHUNK_TTL_MS and is cleared
211 // on shutdown.
212 const cleanupInterval = setInterval(() => {
213 const now = Date.now();
214 for (const [id, entry] of chunkedEvents) {
215 if (now - entry.createdAt > CHUNK_TTL_MS) {
216 chunkedEvents.delete(id);
217 }
218 }
219 }, CHUNK_TTL_MS);
220
221 channel.bind('trigger_to_client', (eventData: unknown) => {
222 params.onEvent(Option.getOrElse(decodeRawRealtimeEvent(eventData), () => ({})));
223 });
224
225 channel.bind('chunked-trigger_to_client', (data: unknown) => {
226 const decoded = decodeChunkedRealtimeEvent(data);
227 if (Option.isNone(decoded)) {
228 return;
229 }
230 const typed = decoded.value;
231
232 // Reject non-integer or out-of-range indices to prevent a single
233 // malformed event from creating a massive sparse array.
234 if (!Number.isInteger(typed.index) || typed.index < 0 || typed.index > MAX_CHUNK_INDEX) {
235 return;
236 }
237
238 if (!chunkedEvents.has(typed.id)) {
239 // If we already have too many in-flight reassemblies, drop the
240 // oldest one to make room.
241 if (chunkedEvents.size >= MAX_PENDING_CHUNKS) {
242 const oldestId = chunkedEvents.keys().next().value;
243 if (oldestId !== undefined) chunkedEvents.delete(oldestId);
244 }
245
246 chunkedEvents.set(typed.id, {
247 chunks: [],
248 receivedFinal: false,
249 createdAt: Date.now(),
250 });
251 }
252
253 const current = chunkedEvents.get(typed.id);
254 if (!current) {
255 return;
256 }
257 current.chunks[typed.index] = typed.chunk;
258 if (typed.final) {
259 current.receivedFinal = true;
260 }
261
262 // Completeness check: for a dense array, .length equals the
263 // number of defined keys. For a sparse array (missing chunks),
264 // .length is highestIndex + 1 but Object.keys only counts the
265 // indices that were actually assigned, so the two diverge.
266 if (
267 current.receivedFinal &&
268 current.chunks.length === Object.keys(current.chunks).length
269 ) {
270 const reassembled = current.chunks.join('');
271 chunkedEvents.delete(typed.id);
272 // Silently discard events that fail to parse after chunk
273 // reassembly; the buffer entry is already cleared either way.
274 const parsed = Result.try((): unknown => JSON.parse(reassembled)).pipe(
275 Result.getSuccess,
276 Option.flatMap(decodeRawRealtimeEvent)
277 );
278 if (Option.isSome(parsed)) {
279 params.onEvent(parsed.value);
280 }
281 }
282 });
283
284 return {
285 subscribed: Deferred.await(subscribed),
286 shutdown: async () => {
287 clearInterval(cleanupInterval);
288 channel.unbind_all?.();
289 pusher.unsubscribe(channelName);
290 pusher.disconnect();
291 },
292 };
293 }),
294 resource =>
295 resource.subscribed.pipe(
296 Effect.andThen(params.options?.onSubscribed ?? Effect.void),
297 Effect.andThen(Effect.never),
298 Effect.scoped
299 ),
300 resource =>
301 Effect.tryPromise({
302 try: () => resource.shutdown(),
303 catch: subscriptionError('Failed to shut down the realtime subscription'),
304 }).pipe(Effect.catch(() => Effect.void))
305 );
306
307 const listen = (onEvent: (data: RawRealtimeEvent) => void, options?: ListenOptions) =>
308 listenWith({
309 getRealtimeCredentials: () =>
310 sessionRepo
311 .getRealtimeCredentials()
312 .pipe(Effect.mapError(subscriptionError('Failed to fetch realtime credentials'))),
313 authRealtimeChannel: params =>
314 sessionRepo
315 .authRealtimeChannel(params)
316 .pipe(Effect.mapError(subscriptionError('Failed to authorize the realtime channel'))),
317 onEvent,
318 options,
319 });
320
321 const listenInProject = (
322 scope: { orgId: string; projectId: string },
323 onEvent: (data: RawRealtimeEvent) => void,
324 options?: ListenOptions
325 ) =>
326 Effect.gen(function* () {
327 const client = yield* clientSingleton.getFor({
328 orgId: scope.orgId,
329 projectId: scope.projectId,
330 });
331
332 return yield* listenWith({
333 getRealtimeCredentials: () =>
334 client._internal.cli.realtime
335 .credentials({ ...idempotent, ...validated })
336 .pipe(Effect.mapError(subscriptionError('Failed to fetch realtime credentials'))),
337 authRealtimeChannel: params =>
338 client._internal.cli.realtime
339 .auth(params, idempotent)
340 .pipe(Effect.mapError(subscriptionError('Failed to authorize the realtime channel'))),
341 onEvent,
342 options,
343 });
344 });
345
346 return { listen, listenInProject };
347 });
348
349 export type TriggersRealtimeShape = Effect.Success<typeof makeTriggersRealtime>;
350
351 export class TriggersRealtime extends Context.Service<TriggersRealtime, TriggersRealtimeShape>()(
352 'services/TriggersRealtime'
353 ) {
354 static readonly Default = Layer.effect(TriggersRealtime, makeTriggersRealtime).pipe(
355 Layer.provide(
356 Layer.mergeAll(ComposioSessionRepository.Default, ComposioClientSingleton.Default)
357 )
358 );
359 }