@composio/core 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BaseProvider-Brb3o86-.d.mts +29425 -0
- package/dist/{CustomTool-DEB4BQPl.mjs → CustomTool-D9qwyMrv.mjs} +1 -1
- package/dist/{CustomTool-DH18rVQu.d.mts → CustomTool-Dmi6ti3I.d.mts} +1 -1
- package/dist/CustomTool-DrJpOhY2.d.mts +74 -0
- package/dist/{composio-CHnEggnv.d.mts → composio-DHIKRdeG.d.mts} +253 -37
- package/dist/composio-vMwhASRc.d.mts +5698 -0
- package/dist/{customTool.types-DpIdOJjL.d.mts → customTool.types-CKH2R2ia.d.mts} +344 -25
- package/dist/customTool.types-DXin7Cfc.d.mts +2773 -0
- package/dist/docs/error-consistency.md +97 -0
- package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +306 -0
- package/dist/docs/reference/sdk-reference/typescript/composio.mdx +205 -0
- package/dist/docs/reference/sdk-reference/typescript/connected-accounts.mdx +488 -0
- package/dist/docs/reference/sdk-reference/typescript/experimental.mdx +94 -0
- package/dist/docs/reference/sdk-reference/typescript/index.mdx +67 -0
- package/dist/docs/reference/sdk-reference/typescript/mcp.mdx +304 -0
- package/dist/docs/reference/sdk-reference/typescript/meta.json +17 -0
- package/dist/docs/reference/sdk-reference/typescript/remote-file.mdx +107 -0
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +163 -0
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +229 -0
- package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +131 -0
- package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +169 -0
- package/dist/docs/reference/sdk-reference/typescript/tools.mdx +453 -0
- package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +434 -0
- package/dist/{errors-D5f4pfvI.mjs → errors-DSIlYfl5.mjs} +2 -2
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/index.d.mts +27 -4
- package/dist/index.mjs +423 -123
- package/dist/{jsonSchema-daqF3S3d.mjs → jsonSchema-CFKdONpP.mjs} +1 -1
- package/dist/{types-CaI5Z4PA.d.mts → types-D4Id3wXA.d.mts} +1 -1
- package/dist/types-qTbYYBJw.d.mts +12 -0
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -1
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.mjs +2 -2
- package/docs/error-consistency.md +97 -0
- package/package.json +4 -3
- package/src/composio.ts +509 -0
- package/src/errors/AuthConfigErrors.ts +23 -0
- package/src/errors/ComposioError.ts +408 -0
- package/src/errors/ConnectedAccountsErrors.ts +158 -0
- package/src/errors/ConnectionRequestErrors.ts +32 -0
- package/src/errors/FileModifierErrors.ts +100 -0
- package/src/errors/PusherErrors.ts +28 -0
- package/src/errors/RemoteFileErrors.ts +62 -0
- package/src/errors/SDKErrors.ts +92 -0
- package/src/errors/ToolErrors.ts +244 -0
- package/src/errors/ToolkitErrors.ts +39 -0
- package/src/errors/TriggerErrors.ts +120 -0
- package/src/errors/ValidationErrors.ts +99 -0
- package/src/errors/index.ts +41 -0
- package/src/experimental/index.ts +28 -0
- package/src/index.ts +64 -0
- package/src/lib/toolRouterConstants.ts +1 -0
- package/src/lib/toolRouterParams.ts +306 -0
- package/src/models/AuthConfigs.ts +398 -0
- package/src/models/AuthScheme.ts +206 -0
- package/src/models/ConnectedAccounts.ts +735 -0
- package/src/models/ConnectionRequest.ts +150 -0
- package/src/models/CustomTool.ts +676 -0
- package/src/models/Experimental.ts +202 -0
- package/src/models/Files.node.ts +93 -0
- package/src/models/Files.workerd.ts +49 -0
- package/src/models/MCP.ts +478 -0
- package/src/models/RemoteFile.ts +172 -0
- package/src/models/SessionContext.ts +147 -0
- package/src/models/Sessions.ts +21 -0
- package/src/models/ToolRouter.ts +391 -0
- package/src/models/ToolRouterSession.ts +827 -0
- package/src/models/ToolRouterSessionFileMount.ts +389 -0
- package/src/models/Toolkits.ts +412 -0
- package/src/models/Tools.ts +1260 -0
- package/src/models/Triggers.ts +1381 -0
- package/src/models/customToolExecution.ts +118 -0
- package/src/models/inlineCustomToolsPayload.ts +17 -0
- package/src/models/proxyParamsTransform.ts +26 -0
- package/src/provider/BaseProvider.ts +138 -0
- package/src/provider/ComposioProvider.ts +29 -0
- package/src/provider/OpenAIProvider.ts +547 -0
- package/src/services/internal/InternalService.ts +50 -0
- package/src/services/internal/InternalService.types.ts +14 -0
- package/src/services/pusher/Pusher.ts +230 -0
- package/src/services/telemetry/TelemetryService.ts +48 -0
- package/src/services/telemetry/TelemetryService.types.ts +67 -0
- package/src/telemetry/BatchProcessor.ts +77 -0
- package/src/telemetry/README.md +123 -0
- package/src/telemetry/Telemetry.ts +356 -0
- package/src/types/ToolRouterSessionFilesMount.types.ts +96 -0
- package/src/types/authConfigs.types.ts +174 -0
- package/src/types/composio.types.ts +1 -0
- package/src/types/connectedAccountAuthStates.types.ts +852 -0
- package/src/types/connectedAccounts.types.ts +395 -0
- package/src/types/connectionRequest.types.ts +16 -0
- package/src/types/customTool.types.ts +261 -0
- package/src/types/files.types.ts +15 -0
- package/src/types/mcp.experimental.types.ts +163 -0
- package/src/types/mcp.types.ts +362 -0
- package/src/types/modifiers.types.ts +663 -0
- package/src/types/org.types.ts +0 -0
- package/src/types/provider.types.ts +41 -0
- package/src/types/pusher.types.ts +18 -0
- package/src/types/requestOptions.types.ts +62 -0
- package/src/types/telemetry.types.ts +89 -0
- package/src/types/tool.types.ts +408 -0
- package/src/types/toolRouter.types.ts +761 -0
- package/src/types/toolkit.types.ts +130 -0
- package/src/types/triggers.types.ts +401 -0
- package/src/types/webhookEvents.types.ts +219 -0
- package/src/utils/buffer.ts +30 -0
- package/src/utils/cancellation.ts +22 -0
- package/src/utils/config-defaults/ConfigDefaults.node.ts +7 -0
- package/src/utils/config-defaults/ConfigDefaults.workerd.ts +10 -0
- package/src/utils/config-defaults/index.ts +1 -0
- package/src/utils/config-defaults/types.ts +14 -0
- package/src/utils/constants.ts +22 -0
- package/src/utils/crypto.ts +34 -0
- package/src/utils/env.ts +34 -0
- package/src/utils/fileDirs.ts +81 -0
- package/src/utils/fileUtils.node.ts +458 -0
- package/src/utils/json-schema.ts +1 -0
- package/src/utils/jsonSchema.ts +355 -0
- package/src/utils/logger.ts +99 -0
- package/src/utils/mime.ts +75 -0
- package/src/utils/modifiers/FileToolModifier.node.ts +477 -0
- package/src/utils/modifiers/FileToolModifier.utils.neutral.ts +132 -0
- package/src/utils/modifiers/FileToolModifier.workerd.ts +51 -0
- package/src/utils/pagination.ts +73 -0
- package/src/utils/pusher.ts +286 -0
- package/src/utils/schemaPropertyKeys.ts +433 -0
- package/src/utils/sdk.ts +110 -0
- package/src/utils/sensitiveFileUploadPaths.node.ts +105 -0
- package/src/utils/session.ts +114 -0
- package/src/utils/toolArguments.ts +73 -0
- package/src/utils/toolkitVersion.ts +26 -0
- package/src/utils/transform.ts +42 -0
- package/src/utils/transformers/authConfigs.ts +107 -0
- package/src/utils/transformers/connectedAccounts.ts +119 -0
- package/src/utils/transformers/mcp.ts +164 -0
- package/src/utils/transformers/toolRouterResponseTransform.ts +160 -0
- package/src/utils/transformers/toolkits.ts +100 -0
- package/src/utils/transformers/triggers.ts +118 -0
- package/src/utils/uploadDirAllowlist.node.ts +178 -0
- package/src/utils/uuid.ts +7 -0
- package/src/utils/version.ts +58 -0
- package/src/utils/zodSchema.ts +90 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pagination parameters that getAllPages will pass to the fetch function.
|
|
3
|
+
*/
|
|
4
|
+
type PaginationParams = {
|
|
5
|
+
cursor?: string;
|
|
6
|
+
limit: number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Extracts the item type from a paginated response.
|
|
11
|
+
*/
|
|
12
|
+
type ExtractItemType<T> = T extends { items: Array<infer R> } ? R : never;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Generic, type-safe utility for loading all pages from paginated API responses.
|
|
16
|
+
*
|
|
17
|
+
* This utility automatically handles pagination by:
|
|
18
|
+
* - Invoking the provided method with `limit: 100`
|
|
19
|
+
* - Traversing all pages using the `next_cursor` field
|
|
20
|
+
* - Collecting and returning all items from all pages
|
|
21
|
+
*
|
|
22
|
+
* Types are automatically inferred from the function signature, so you don't need to specify them manually.
|
|
23
|
+
*
|
|
24
|
+
* @param fetchFn - A function that accepts pagination parameters and returns a Promise of a paginated response.
|
|
25
|
+
* Base parameters should be captured in the function closure.
|
|
26
|
+
* @returns A Promise that resolves to an array of all items from all pages
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { getAllPages } from './utils/pagination';
|
|
31
|
+
*
|
|
32
|
+
* // Fetch all tools - types are automatically inferred!
|
|
33
|
+
* const allTools = await getAllPages((params) =>
|
|
34
|
+
* client.tools.list({
|
|
35
|
+
* ...params,
|
|
36
|
+
* tool_slugs: 'tool1,tool2',
|
|
37
|
+
* })
|
|
38
|
+
* );
|
|
39
|
+
* // allTools is inferred as Array<ToolListResponse.Item>
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export async function getAllPages<
|
|
43
|
+
TFn extends (
|
|
44
|
+
params: PaginationParams
|
|
45
|
+
) => Promise<{ items: Array<unknown>; next_cursor?: string | null }>,
|
|
46
|
+
>(fetchFn: TFn): Promise<Array<ExtractItemType<Awaited<ReturnType<TFn>>>>> {
|
|
47
|
+
type ResponseType = Awaited<ReturnType<TFn>>;
|
|
48
|
+
type ItemType = ExtractItemType<ResponseType>;
|
|
49
|
+
|
|
50
|
+
const allItems: Array<ItemType> = [];
|
|
51
|
+
let cursor: string | null | undefined = undefined;
|
|
52
|
+
|
|
53
|
+
const MAX_LIMIT_ALLOWED_BY_API = 100;
|
|
54
|
+
|
|
55
|
+
while (true) {
|
|
56
|
+
const params: PaginationParams = {
|
|
57
|
+
...(cursor !== undefined && { cursor }),
|
|
58
|
+
limit: MAX_LIMIT_ALLOWED_BY_API,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const response = await fetchFn(params);
|
|
62
|
+
allItems.push(...(response.items as Array<ItemType>));
|
|
63
|
+
|
|
64
|
+
// Stop if there's no next cursor
|
|
65
|
+
if (!response.next_cursor) {
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
cursor = response.next_cursor;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return allItems;
|
|
73
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { PusherSubscriptionError } from '../errors/PusherErrors';
|
|
2
|
+
import { CLIENT_PUSHER_KEY } from './constants';
|
|
3
|
+
import logger from './logger';
|
|
4
|
+
|
|
5
|
+
const PUSHER_KEY = CLIENT_PUSHER_KEY || 'ff9f18c208855d77a152';
|
|
6
|
+
const PUSHER_CLUSTER = 'mt1';
|
|
7
|
+
|
|
8
|
+
type Channel = {
|
|
9
|
+
subscribe: (channelName: string) => unknown;
|
|
10
|
+
unsubscribe: (channelName: string) => unknown;
|
|
11
|
+
bind: (event: string, callback: (data: Record<string, unknown>) => void) => unknown;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type PusherClient = {
|
|
15
|
+
subscribe: (channelName: string) => Channel;
|
|
16
|
+
unsubscribe: (channelName: string) => unknown;
|
|
17
|
+
bind: (event: string, callback: (data: Record<string, unknown>) => void) => unknown;
|
|
18
|
+
connection: {
|
|
19
|
+
bind: (event: string, callback: (error: Error) => void) => void;
|
|
20
|
+
socket_id?: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type TChunkedTriggerData = {
|
|
25
|
+
id: string;
|
|
26
|
+
index: number;
|
|
27
|
+
chunk: string;
|
|
28
|
+
final: boolean;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type TriggerData = {
|
|
32
|
+
appName: string;
|
|
33
|
+
clientId: number;
|
|
34
|
+
payload: Record<string, unknown>;
|
|
35
|
+
originalPayload: Record<string, unknown>;
|
|
36
|
+
metadata: {
|
|
37
|
+
id: string;
|
|
38
|
+
connectionId: string;
|
|
39
|
+
triggerName: string;
|
|
40
|
+
triggerData: string;
|
|
41
|
+
triggerConfig: Record<string, unknown>;
|
|
42
|
+
connection: {
|
|
43
|
+
id: string;
|
|
44
|
+
integrationId: string;
|
|
45
|
+
clientUniqueUserId: string;
|
|
46
|
+
status: string;
|
|
47
|
+
connectedAccountNanoId: string;
|
|
48
|
+
authConfigNanoId: string;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export class PusherUtils {
|
|
54
|
+
static pusherClient: PusherClient;
|
|
55
|
+
|
|
56
|
+
static async getPusherClient(baseURL: string, apiKey: string): Promise<PusherClient> {
|
|
57
|
+
try {
|
|
58
|
+
if (!PusherUtils.pusherClient) {
|
|
59
|
+
const { default: PusherClient } = await import('pusher-js');
|
|
60
|
+
const authEndpoint = `${baseURL}/api/v3/internal/sdk/realtime/auth`;
|
|
61
|
+
PusherUtils.pusherClient = new PusherClient(PUSHER_KEY, {
|
|
62
|
+
cluster: PUSHER_CLUSTER,
|
|
63
|
+
channelAuthorization: {
|
|
64
|
+
// `transport` and `endpoint` are required by pusher-js's types but are
|
|
65
|
+
// unused at runtime: `customHandler` fully owns the auth request below.
|
|
66
|
+
transport: 'ajax',
|
|
67
|
+
endpoint: authEndpoint,
|
|
68
|
+
customHandler: (params, callback) => {
|
|
69
|
+
fetch(authEndpoint, {
|
|
70
|
+
method: 'POST',
|
|
71
|
+
headers: {
|
|
72
|
+
'x-api-key': apiKey,
|
|
73
|
+
'Content-Type': 'application/json',
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
socket_id: params.socketId,
|
|
77
|
+
channel_name: params.channelName,
|
|
78
|
+
}),
|
|
79
|
+
})
|
|
80
|
+
.then(async response => {
|
|
81
|
+
const data = await response.text();
|
|
82
|
+
logger.debug('Pusher auth response:', data);
|
|
83
|
+
try {
|
|
84
|
+
callback(null, JSON.parse(data));
|
|
85
|
+
} catch (e) {
|
|
86
|
+
logger.error('Failed to parse auth response:', e);
|
|
87
|
+
callback(new Error('Invalid auth response format'), null);
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
.catch((error: unknown) => {
|
|
91
|
+
logger.error('Pusher auth request failed:', error);
|
|
92
|
+
callback(error instanceof Error ? error : new Error(String(error)), null);
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Add connection error handling
|
|
99
|
+
PusherUtils.pusherClient.connection.bind('error', (err: Error) => {
|
|
100
|
+
logger.error('Pusher connection error:', err);
|
|
101
|
+
throw new Error(`Pusher connection error: ${err.message}`);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return PusherUtils.pusherClient;
|
|
105
|
+
} catch (error: unknown) {
|
|
106
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
107
|
+
logger.error('Failed to initialize Pusher client:', error);
|
|
108
|
+
throw new Error(`Failed to initialize Pusher client: ${errorMessage}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Subscribes to a Pusher channel and binds an event to a callback function.
|
|
114
|
+
* @param {string} channelName - The name of the channel to subscribe to.
|
|
115
|
+
* @param {string} event - The event to bind to the channel.
|
|
116
|
+
* @param {(data: Record<string, unknown>) => void} fn - The callback function to execute when the event is triggered.
|
|
117
|
+
* @returns {PusherClient} The Pusher client instance.
|
|
118
|
+
*/
|
|
119
|
+
static async subscribe(
|
|
120
|
+
channelName: string,
|
|
121
|
+
event: string,
|
|
122
|
+
fn: (data: Record<string, unknown>) => void
|
|
123
|
+
): Promise<void> {
|
|
124
|
+
try {
|
|
125
|
+
await PusherUtils.pusherClient.subscribe(channelName).bind(event, fn);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
logger.error(`Error subscribing to ${channelName} with event ${event}: ${error}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Unsubscribes from a Pusher channel.
|
|
133
|
+
* @param {string} channelName - The name of the channel to unsubscribe from.
|
|
134
|
+
* @returns {void}
|
|
135
|
+
*/
|
|
136
|
+
static async unsubscribe(channelName: string): Promise<void> {
|
|
137
|
+
PusherUtils.pusherClient.unsubscribe(channelName);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Binds an event to a channel with support for chunked messages.
|
|
142
|
+
* @param {PusherClient} channel - The Pusher channel to bind the event to.
|
|
143
|
+
* @param {string} event - The event to bind to the channel.
|
|
144
|
+
* @param {(data: unknown) => void} callback - The callback function to execute when the event is triggered.
|
|
145
|
+
*/
|
|
146
|
+
private static bindWithChunking(
|
|
147
|
+
channel: PusherClient,
|
|
148
|
+
event: string,
|
|
149
|
+
callback: (data: Record<string, unknown>) => void
|
|
150
|
+
): void {
|
|
151
|
+
try {
|
|
152
|
+
// Wrap the callback to handle errors
|
|
153
|
+
const safeCallback = (data: Record<string, unknown>) => {
|
|
154
|
+
try {
|
|
155
|
+
callback(data);
|
|
156
|
+
} catch (error: unknown) {
|
|
157
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
158
|
+
logger.error('Error in chunked event callback:', errorMessage);
|
|
159
|
+
// Don't throw here to prevent breaking the subscription
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
channel.bind(event, safeCallback);
|
|
164
|
+
|
|
165
|
+
// Now the chunked variation. Allows arbitrarily long messages.
|
|
166
|
+
const events: {
|
|
167
|
+
[key: string]: { chunks: string[]; receivedFinal: boolean };
|
|
168
|
+
} = {};
|
|
169
|
+
|
|
170
|
+
channel.bind('chunked-' + event, data => {
|
|
171
|
+
try {
|
|
172
|
+
const typedData = data as TChunkedTriggerData;
|
|
173
|
+
|
|
174
|
+
// Validate chunked data
|
|
175
|
+
if (
|
|
176
|
+
!typedData ||
|
|
177
|
+
typeof typedData.id !== 'string' ||
|
|
178
|
+
typeof typedData.index !== 'number'
|
|
179
|
+
) {
|
|
180
|
+
throw new Error('Invalid chunked trigger data format');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!events.hasOwnProperty(typedData.id)) {
|
|
184
|
+
events[typedData.id] = { chunks: [], receivedFinal: false };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const ev = events[typedData.id];
|
|
188
|
+
ev.chunks[typedData.index] = typedData.chunk;
|
|
189
|
+
|
|
190
|
+
if (typedData.final) ev.receivedFinal = true;
|
|
191
|
+
|
|
192
|
+
if (ev.receivedFinal && ev.chunks.length === Object.keys(ev.chunks).length) {
|
|
193
|
+
try {
|
|
194
|
+
const parsedData = JSON.parse(ev.chunks.join(''));
|
|
195
|
+
safeCallback(parsedData);
|
|
196
|
+
} catch (parseError: unknown) {
|
|
197
|
+
const errorMessage =
|
|
198
|
+
parseError instanceof Error ? parseError.message : String(parseError);
|
|
199
|
+
logger.error('Failed to parse chunked data:', errorMessage);
|
|
200
|
+
} finally {
|
|
201
|
+
delete events[typedData.id];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
} catch (error: unknown) {
|
|
205
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
206
|
+
logger.error('Error processing chunked trigger data:', errorMessage);
|
|
207
|
+
// Clean up the event data to prevent memory leaks
|
|
208
|
+
if (data && typeof data === 'object' && 'id' in data) {
|
|
209
|
+
delete events[data.id as string];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
} catch (error: unknown) {
|
|
214
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
215
|
+
logger.error('Failed to bind chunked events:', error);
|
|
216
|
+
throw new Error(`Failed to bind chunked events: ${errorMessage}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Subscribes to a trigger channel for a client and handles chunked data.
|
|
222
|
+
* @param {string} clientId - The unique identifier for the client subscribing to the events.
|
|
223
|
+
* @param {(data: TriggerData) => void} fn - The callback function to execute when trigger data is received.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* composio.trigger.subscribe((data) => {
|
|
228
|
+
* console.log(data);
|
|
229
|
+
* });
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
static triggerSubscribe(clientId: string, fn: (data: TriggerData) => void): void {
|
|
233
|
+
try {
|
|
234
|
+
if (!PusherUtils.pusherClient) {
|
|
235
|
+
throw new Error('Pusher client not initialized');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const channel = PusherUtils.pusherClient.subscribe(`private-${clientId}_triggers`);
|
|
239
|
+
|
|
240
|
+
// Add subscription error handling
|
|
241
|
+
channel.bind('pusher:subscription_error', (data: Record<string, unknown>) => {
|
|
242
|
+
const error = data.error ? String(data.error) : 'Unknown subscription error';
|
|
243
|
+
throw new PusherSubscriptionError(`Trigger subscription error`, {
|
|
244
|
+
cause: error,
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// Wrap the callback to handle errors
|
|
249
|
+
const safeCallback = (data: TriggerData) => {
|
|
250
|
+
try {
|
|
251
|
+
fn(data);
|
|
252
|
+
} catch (error: unknown) {
|
|
253
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
254
|
+
logger.error('Error in trigger callback:', errorMessage);
|
|
255
|
+
// Don't throw here to prevent breaking the subscription
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
PusherUtils.bindWithChunking(
|
|
260
|
+
channel as PusherClient,
|
|
261
|
+
'trigger_to_client',
|
|
262
|
+
safeCallback as (data: unknown) => void
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
logger.info(`Subscribed to triggers. You should start receiving events now.`);
|
|
266
|
+
} catch (error: unknown) {
|
|
267
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
268
|
+
logger.error('Failed to subscribe to triggers:', error);
|
|
269
|
+
throw new Error(`Failed to subscribe to triggers: ${errorMessage}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
static triggerUnsubscribe(clientId: string): void {
|
|
274
|
+
try {
|
|
275
|
+
if (!PusherUtils.pusherClient) {
|
|
276
|
+
throw new Error('Pusher client not initialized');
|
|
277
|
+
}
|
|
278
|
+
PusherUtils.pusherClient.unsubscribe(`private-${clientId}_triggers`);
|
|
279
|
+
logger.info('Successfully unsubscribed from triggers');
|
|
280
|
+
} catch (error: unknown) {
|
|
281
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
282
|
+
logger.error('Failed to unsubscribe from triggers:', error);
|
|
283
|
+
throw new Error(`Failed to unsubscribe from triggers: ${errorMessage}`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|