@composio/core 0.24.0 → 0.26.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/{CustomTool-CnJxXr07.mjs → CustomTool-BYHB7sPs.mjs} +1 -1
- package/dist/{CustomTool-CNJysHMK.d.mts → CustomTool-DfP4ldQl.d.mts} +1 -1
- package/dist/{cancellation-BQibgpRG.mjs → cancellation-ZoRh6EJQ.mjs} +55 -3
- package/dist/{composio-BFTtZj82.d.mts → composio-BMDH-1Qj.d.mts} +35 -15
- package/dist/{customTool.types-D57QrTns.d.mts → customTool.types-BoOELgJF.d.mts} +346 -23
- package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +10 -10
- package/dist/docs/reference/sdk-reference/typescript/composio.mdx +2 -2
- package/dist/docs/reference/sdk-reference/typescript/connected-accounts.mdx +22 -22
- package/dist/docs/reference/sdk-reference/typescript/custom-toolkits.mdx +8 -8
- package/dist/docs/reference/sdk-reference/typescript/experimental.mdx +2 -2
- package/dist/docs/reference/sdk-reference/typescript/index.mdx +1 -1
- package/dist/docs/reference/sdk-reference/typescript/keyring.mdx +2 -2
- package/dist/docs/reference/sdk-reference/typescript/logs.mdx +5 -5
- package/dist/docs/reference/sdk-reference/typescript/mcp.mdx +15 -15
- package/dist/docs/reference/sdk-reference/typescript/meta.json +1 -1
- package/dist/docs/reference/sdk-reference/typescript/session-configs.mdx +5 -5
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +10 -10
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +56 -21
- package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +6 -6
- package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +22 -22
- package/dist/docs/reference/sdk-reference/typescript/tools.mdx +18 -18
- package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +25 -25
- package/dist/docs/reference/sdk-reference/typescript/usage.mdx +6 -6
- package/dist/docs/reference/sdk-reference/typescript/webhook-endpoints.mdx +14 -14
- package/dist/docs/reference/sdk-reference/typescript/webhook-subscriptions.mdx +13 -13
- package/dist/{errors-BN15NcCW.mjs → errors-Cly0vXFh.mjs} +5 -5
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/index.d.mts +19 -11
- package/dist/index.mjs +693 -196
- package/dist/{types-q8TwAngA.d.mts → types-DHNV_T_z.d.mts} +2 -2
- 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.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.mjs +2 -2
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -1
- package/package.json +5 -5
- package/src/composio.ts +81 -67
- package/src/errors/ToolRouterErrors.ts +5 -5
- package/src/lib/toolRouterMcp.ts +2 -0
- package/src/lib/toolRouterParams.ts +28 -16
- package/src/models/ConnectedAccounts.ts +7 -16
- package/src/models/ToolRouter.ts +8 -1
- package/src/models/ToolRouterSession.ts +17 -0
- package/src/models/Tools.ts +1 -4
- package/src/models/Triggers.ts +14 -7
- package/src/services/internal/InternalService.ts +3 -4
- package/src/services/pusher/Pusher.ts +170 -92
- package/src/types/connectedAccountAuthStates.types.ts +2 -1
- package/src/types/pusher.types.ts +2 -0
- package/src/types/requestOptions.types.ts +1 -10
- package/src/types/toolRouter.types.ts +104 -18
- package/src/utils/jsonSchema.ts +76 -4
- package/src/utils/sdk.ts +172 -11
- package/src/utils/session.ts +25 -47
- package/src/utils/toolkitDeprecation.ts +271 -0
- package/src/utils/transformers/connectedAccounts.ts +9 -20
- package/src/utils/transformers/mcp.ts +3 -10
- package/src/utils/userCallback.ts +41 -0
package/src/models/Triggers.ts
CHANGED
|
@@ -60,6 +60,7 @@ import { hmacSha256Base64, timingSafeEqual } from '../utils/crypto';
|
|
|
60
60
|
import { CONFIG_DEFAULTS } from '../utils/config-defaults';
|
|
61
61
|
import { ComposioRequestOptions } from '../types/requestOptions.types';
|
|
62
62
|
import { withCancellation } from '../utils/cancellation';
|
|
63
|
+
import { invokeUserCallback } from '../utils/userCallback';
|
|
63
64
|
|
|
64
65
|
/**
|
|
65
66
|
* Safely converts a value to a string, returning the default if the value is null, undefined, or empty.
|
|
@@ -157,7 +158,12 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
157
158
|
|
|
158
159
|
constructor(client: ComposioClient, config?: ComposioConfig<TProvider>) {
|
|
159
160
|
this.client = client;
|
|
160
|
-
this.pusherService = new PusherService(client, {
|
|
161
|
+
this.pusherService = new PusherService(client, {
|
|
162
|
+
apiKey: config?.apiKey,
|
|
163
|
+
userApiKey: config?.userApiKey,
|
|
164
|
+
orgApiKey: config?.orgApiKey,
|
|
165
|
+
defaultHeaders: config?.defaultHeaders,
|
|
166
|
+
});
|
|
161
167
|
this.toolkitVersions = config?.toolkitVersions ?? CONFIG_DEFAULTS.toolkitVersions;
|
|
162
168
|
telemetry.instrument(this, 'Triggers');
|
|
163
169
|
}
|
|
@@ -525,7 +531,8 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
525
531
|
/**
|
|
526
532
|
* Subscribe to all the triggers
|
|
527
533
|
*
|
|
528
|
-
* @param fn - The function to call when a trigger is received
|
|
534
|
+
* @param fn - The function to call when a trigger is received. It may be `async`. Errors
|
|
535
|
+
* thrown from — or promises rejected by — the function are contained and logged.
|
|
529
536
|
* @param filters - The filters to apply to the triggers
|
|
530
537
|
* @param onSubscriptionError - Optional callback invoked with the raw Pusher payload when
|
|
531
538
|
* the underlying subscription fails (for example on auth or permission rejection). The
|
|
@@ -563,11 +570,7 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
563
570
|
const parsedData = this.parsePusherPayload(_data);
|
|
564
571
|
|
|
565
572
|
if (this.shouldSendTriggerAfterFilters(parsedFilters.data, parsedData)) {
|
|
566
|
-
|
|
567
|
-
fn(parsedData);
|
|
568
|
-
} catch (error) {
|
|
569
|
-
logger.error('❌ Error in trigger callback:', error);
|
|
570
|
-
}
|
|
573
|
+
invokeUserCallback('❌ Error in trigger callback:', () => fn(parsedData));
|
|
571
574
|
} else {
|
|
572
575
|
logger.debug('Trigger does not match filters', JSON.stringify(parsedFilters.data, null, 2));
|
|
573
576
|
}
|
|
@@ -700,6 +703,10 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
|
|
|
700
703
|
/**
|
|
701
704
|
* Unsubscribe from all the triggers
|
|
702
705
|
*
|
|
706
|
+
* Ends every subscription made with {@link subscribe} on this instance, including one that is
|
|
707
|
+
* still connecting, and closes the realtime connection so that a script can exit. A later
|
|
708
|
+
* `subscribe()` opens a new connection.
|
|
709
|
+
*
|
|
703
710
|
* @returns {Promise<void>}
|
|
704
711
|
*
|
|
705
712
|
* @example
|
|
@@ -24,10 +24,9 @@ export class InternalService {
|
|
|
24
24
|
* @returns {SDKRealtimeCredentialsResponse} The SDK realtime credentials
|
|
25
25
|
*/
|
|
26
26
|
async getSDKRealtimeCredentials(): Promise<SDKRealtimeCredentialsResponse> {
|
|
27
|
-
const response = await this.client.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
});
|
|
27
|
+
const response = await this.client.get<ComposioSDKRealtimeCredentialsResponse>(
|
|
28
|
+
SDK_REALTIME_CREDENTIALS_ENDPOINT
|
|
29
|
+
);
|
|
31
30
|
|
|
32
31
|
const parsedResponse = SDKRealtimeCredentialsResponseSchema.safeParse({
|
|
33
32
|
pusherKey: response.pusher_key,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ComposioClient from '@composio/client';
|
|
2
|
-
import { PusherClient, TChunkedTriggerData } from '../../types/pusher.types';
|
|
2
|
+
import { PusherChannel, PusherClient, TChunkedTriggerData } from '../../types/pusher.types';
|
|
3
3
|
import { InternalService } from '../internal/InternalService';
|
|
4
4
|
import { SDKRealtimeCredentialsResponse } from '../internal/InternalService.types';
|
|
5
5
|
import {
|
|
@@ -8,89 +8,153 @@ import {
|
|
|
8
8
|
ComposioFailedToSubscribeToPusherChannelError,
|
|
9
9
|
} from '../../errors/TriggerErrors';
|
|
10
10
|
import logger from '../../utils/logger';
|
|
11
|
+
import { invokeUserCallback } from '../../utils/userCallback';
|
|
11
12
|
import { telemetry } from '../../telemetry/Telemetry';
|
|
12
13
|
import { resolveCredentialHeaders } from '../../utils/sdk';
|
|
13
14
|
import type { ComposioRequestHeaders } from '../../types/composio.types';
|
|
14
15
|
|
|
15
16
|
export type PusherServiceOptions = {
|
|
17
|
+
/** The project API key the owning SDK instance resolved, or `null` when disabled. */
|
|
18
|
+
apiKey?: string | null;
|
|
19
|
+
/** The user API key the owning SDK instance resolved, or `null`. */
|
|
20
|
+
userApiKey?: string | null;
|
|
21
|
+
/** The organization API key the owning SDK instance resolved, or `null`. */
|
|
22
|
+
orgApiKey?: string | null;
|
|
16
23
|
/** Default headers of the owning SDK instance; only `x-user-api-key` is consulted. */
|
|
17
24
|
defaultHeaders?: ComposioRequestHeaders;
|
|
18
25
|
};
|
|
19
26
|
|
|
27
|
+
/** A pusher-js client and the triggers channel it subscribed to. */
|
|
28
|
+
type TriggerConnection = {
|
|
29
|
+
client: PusherClient;
|
|
30
|
+
channel: PusherChannel;
|
|
31
|
+
channelName: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Setup of the connection for one subscription period, which runs from the first `subscribe()`
|
|
36
|
+
* to the next `unsubscribe()`. Concurrent `subscribe()` calls share `ready`; `unsubscribe()`
|
|
37
|
+
* aborts `controller` so that setup still in flight neither connects nor installs handlers.
|
|
38
|
+
*/
|
|
39
|
+
type ConnectionAttempt = {
|
|
40
|
+
controller: AbortController;
|
|
41
|
+
ready: Promise<TriggerConnection>;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Owns the realtime connection that delivers trigger events.
|
|
46
|
+
*
|
|
47
|
+
* Each subscription period gets its own pusher-js client, which `unsubscribe()` disconnects.
|
|
48
|
+
* Reusing one client across periods is unsafe: pusher-js keeps a channel, its bound handlers and
|
|
49
|
+
* its cancellation flag through `disconnect()` and resubscribes every kept channel on the next
|
|
50
|
+
* `connect()`, and a channel-auth reply requested on the old socket can arrive after a reconnect
|
|
51
|
+
* and be sent on the new one.
|
|
52
|
+
*/
|
|
20
53
|
export class PusherService {
|
|
21
|
-
// these values are set via the Apollo API `/internal/sdk/realtime/credentials` endpoint
|
|
22
|
-
private clientId!: string;
|
|
23
|
-
private pusherKey!: string;
|
|
24
|
-
private pusherCluster!: string;
|
|
25
|
-
private pusherChannel!: string;
|
|
26
54
|
// these details are set via the client SDK
|
|
27
55
|
private pusherBaseURL!: string;
|
|
28
56
|
private authHeaders!: Record<string, string>;
|
|
29
|
-
private pusherClient!: PusherClient;
|
|
30
57
|
private composioClient!: ComposioClient;
|
|
58
|
+
private attempt?: ConnectionAttempt;
|
|
59
|
+
// Set as soon as the current attempt creates its client, so that `unsubscribe()` can close it
|
|
60
|
+
// before the `subscribe()` calls waiting on `attempt.ready` resume.
|
|
61
|
+
private connection?: TriggerConnection;
|
|
31
62
|
|
|
32
63
|
constructor(client: ComposioClient, options: PusherServiceOptions = {}) {
|
|
33
64
|
this.composioClient = client;
|
|
34
65
|
this.pusherBaseURL = client.baseURL;
|
|
35
|
-
// Channel authorization mirrors the
|
|
36
|
-
// environment is never consulted here.
|
|
66
|
+
// Channel authorization mirrors the SDK instance's effective auth, from the
|
|
67
|
+
// keys the SDK resolved; the environment is never consulted here.
|
|
37
68
|
this.authHeaders = resolveCredentialHeaders({
|
|
38
|
-
apiKey:
|
|
39
|
-
userApiKey:
|
|
69
|
+
apiKey: options.apiKey,
|
|
70
|
+
userApiKey: options.userApiKey,
|
|
71
|
+
orgApiKey: options.orgApiKey,
|
|
40
72
|
defaultHeaders: options.defaultHeaders,
|
|
41
73
|
});
|
|
42
74
|
telemetry.instrument(this, 'PusherService');
|
|
43
75
|
}
|
|
44
76
|
|
|
45
77
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* This method is called when the Pusher client is first used.
|
|
49
|
-
* It will fetch the SDK realtime credentials from the Apollo API and create a Pusher client.
|
|
78
|
+
* Returns the connection attempt of the current subscription period, starting one if needed.
|
|
79
|
+
* A failed attempt is forgotten, so the next `subscribe()` retries.
|
|
50
80
|
*/
|
|
51
|
-
private
|
|
52
|
-
if (
|
|
53
|
-
const internalService = new InternalService(this.composioClient);
|
|
54
|
-
let sdkRealtimeCredentials: SDKRealtimeCredentialsResponse;
|
|
55
|
-
try {
|
|
56
|
-
sdkRealtimeCredentials = await internalService.getSDKRealtimeCredentials();
|
|
57
|
-
} catch (error) {
|
|
58
|
-
throw new ComposioFailedToGetSDKRealtimeCredentialsError(
|
|
59
|
-
'Failed to get SDK realtime credentials',
|
|
60
|
-
{
|
|
61
|
-
cause: error,
|
|
62
|
-
}
|
|
63
|
-
);
|
|
64
|
-
}
|
|
81
|
+
private connect(): ConnectionAttempt {
|
|
82
|
+
if (this.attempt) return this.attempt;
|
|
65
83
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
84
|
+
const controller = new AbortController();
|
|
85
|
+
const ready = this.openConnection(controller.signal);
|
|
86
|
+
const attempt = { controller, ready };
|
|
87
|
+
this.attempt = attempt;
|
|
88
|
+
ready.catch(() => {
|
|
89
|
+
if (this.attempt === attempt) this.attempt = undefined;
|
|
90
|
+
});
|
|
91
|
+
return attempt;
|
|
92
|
+
}
|
|
70
93
|
|
|
71
|
-
|
|
72
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Fetches the SDK realtime credentials from the Apollo API, creates a Pusher client and
|
|
96
|
+
* subscribes it to the triggers channel.
|
|
97
|
+
*/
|
|
98
|
+
private async openConnection(signal: AbortSignal): Promise<TriggerConnection> {
|
|
99
|
+
const internalService = new InternalService(this.composioClient);
|
|
100
|
+
let sdkRealtimeCredentials: SDKRealtimeCredentialsResponse;
|
|
101
|
+
try {
|
|
102
|
+
sdkRealtimeCredentials = await internalService.getSDKRealtimeCredentials();
|
|
103
|
+
} catch (error) {
|
|
104
|
+
throw new ComposioFailedToGetSDKRealtimeCredentialsError(
|
|
105
|
+
'Failed to get SDK realtime credentials',
|
|
106
|
+
{
|
|
107
|
+
cause: error,
|
|
108
|
+
}
|
|
73
109
|
);
|
|
110
|
+
}
|
|
74
111
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
112
|
+
const { projectId, pusherKey, pusherCluster } = sdkRealtimeCredentials;
|
|
113
|
+
const channelName = `private-${projectId}_triggers`;
|
|
114
|
+
|
|
115
|
+
let Pusher: (typeof import('pusher-js'))['default'];
|
|
116
|
+
try {
|
|
117
|
+
({ default: Pusher } = await import('pusher-js'));
|
|
118
|
+
} catch (error) {
|
|
119
|
+
throw new ComposioFailedToCreatePusherClientError('Failed to create Pusher client', {
|
|
120
|
+
cause: error,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// unsubscribe() ran while the credentials or pusher-js were loading. Everything below is
|
|
125
|
+
// synchronous, so this is the last point at which it can cancel the attempt.
|
|
126
|
+
if (signal.aborted) {
|
|
127
|
+
throw new Error('Connection attempt cancelled by unsubscribe()');
|
|
91
128
|
}
|
|
92
129
|
|
|
93
|
-
|
|
130
|
+
logger.debug(
|
|
131
|
+
`[PusherService] Creating Pusher client for client ID: ${projectId} in cluster ${pusherCluster}`
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
let client: PusherClient;
|
|
135
|
+
try {
|
|
136
|
+
client = new Pusher(pusherKey, {
|
|
137
|
+
cluster: pusherCluster,
|
|
138
|
+
channelAuthorization: {
|
|
139
|
+
endpoint: `${this.pusherBaseURL}/api/v3/internal/sdk/realtime/auth`,
|
|
140
|
+
headers: { ...this.authHeaders },
|
|
141
|
+
transport: 'ajax',
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
} catch (error) {
|
|
145
|
+
throw new ComposioFailedToCreatePusherClientError('Failed to create Pusher client', {
|
|
146
|
+
cause: error,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
logger.debug(`[PusherService] Subscribing to channel: ${channelName}`);
|
|
151
|
+
try {
|
|
152
|
+
this.connection = { client, channel: client.subscribe(channelName), channelName };
|
|
153
|
+
} catch (error) {
|
|
154
|
+
client.disconnect();
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
return this.connection;
|
|
94
158
|
}
|
|
95
159
|
|
|
96
160
|
/**
|
|
@@ -102,7 +166,7 @@ export class PusherService {
|
|
|
102
166
|
* @param callback - The function to call when the event is received
|
|
103
167
|
*/
|
|
104
168
|
private bindWithChunking(
|
|
105
|
-
channel:
|
|
169
|
+
channel: PusherChannel,
|
|
106
170
|
event: string,
|
|
107
171
|
callback: (data: Record<string, unknown>) => void
|
|
108
172
|
): void {
|
|
@@ -167,11 +231,10 @@ export class PusherService {
|
|
|
167
231
|
/**
|
|
168
232
|
* Subscribes to pusher to receive events from the server
|
|
169
233
|
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
234
|
+
* The first call of a subscription period creates the Pusher client; concurrent calls share
|
|
235
|
+
* it. A call still waiting for the client when `unsubscribe()` runs resolves without
|
|
236
|
+
* subscribing.
|
|
172
237
|
*
|
|
173
|
-
* @param channelName - The name of the Pusher channel to subscribe to
|
|
174
|
-
* @param event - The event to subscribe to
|
|
175
238
|
* @param fn - The function to call when the event is received
|
|
176
239
|
* @param onSubscriptionError - Optional callback invoked with the raw payload when the
|
|
177
240
|
* Pusher subscription fails (for example on auth or permission rejection). Errors thrown
|
|
@@ -181,30 +244,38 @@ export class PusherService {
|
|
|
181
244
|
fn: (data: Record<string, unknown>) => void,
|
|
182
245
|
onSubscriptionError?: (data: Record<string, unknown>) => void
|
|
183
246
|
) {
|
|
247
|
+
const attempt = this.connect();
|
|
248
|
+
let connection: TriggerConnection;
|
|
184
249
|
try {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
250
|
+
connection = await attempt.ready;
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (attempt.controller.signal.aborted) {
|
|
253
|
+
logger.debug('[PusherService] Subscribe cancelled by unsubscribe() before it connected');
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
throw new ComposioFailedToSubscribeToPusherChannelError(
|
|
257
|
+
'Failed to subscribe to Pusher channel',
|
|
258
|
+
{
|
|
259
|
+
cause: error,
|
|
260
|
+
}
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
// unsubscribe() ran after the client was created but before this call resumed.
|
|
264
|
+
if (attempt.controller.signal.aborted) {
|
|
265
|
+
logger.debug('[PusherService] Subscribe cancelled by unsubscribe() before it connected');
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
188
268
|
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
const errorMessage =
|
|
192
|
-
callbackError instanceof Error ? callbackError.message : String(callbackError);
|
|
193
|
-
logger.error('❌ Error in subscription error callback:', errorMessage);
|
|
194
|
-
};
|
|
269
|
+
try {
|
|
270
|
+
const { channel } = connection;
|
|
195
271
|
channel.bind('pusher:subscription_error', (data: Record<string, unknown>) => {
|
|
196
272
|
logger.error('Trigger subscription error:', data);
|
|
197
273
|
|
|
198
274
|
// surface the failure to the caller without letting a faulty
|
|
199
275
|
// handler crash the host (same containment as the trigger callback).
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
try {
|
|
204
|
-
Promise.resolve(onSubscriptionError?.(data)).catch(logCallbackFailure);
|
|
205
|
-
} catch (callbackError: unknown) {
|
|
206
|
-
logCallbackFailure(callbackError);
|
|
207
|
-
}
|
|
276
|
+
invokeUserCallback('❌ Error in subscription error callback:', () =>
|
|
277
|
+
onSubscriptionError?.(data)
|
|
278
|
+
);
|
|
208
279
|
});
|
|
209
280
|
|
|
210
281
|
// log success only when Pusher itself confirms the subscription
|
|
@@ -212,18 +283,12 @@ export class PusherService {
|
|
|
212
283
|
logger.info(`✅ Subscribed to triggers. You should start receiving events now.`);
|
|
213
284
|
});
|
|
214
285
|
|
|
215
|
-
//
|
|
286
|
+
// don't let a failing callback break the subscription
|
|
216
287
|
const safeCallback = (data: Record<string, unknown>) => {
|
|
217
|
-
|
|
218
|
-
fn(data);
|
|
219
|
-
} catch (error: unknown) {
|
|
220
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
221
|
-
logger.error('❌ Error in trigger callback:', errorMessage);
|
|
222
|
-
// don't throw here to prevent breaking the subscription
|
|
223
|
-
}
|
|
288
|
+
invokeUserCallback('❌ Error in trigger callback:', () => fn(data));
|
|
224
289
|
};
|
|
225
290
|
|
|
226
|
-
this.bindWithChunking(channel
|
|
291
|
+
this.bindWithChunking(channel, 'trigger_to_client', safeCallback);
|
|
227
292
|
} catch (error) {
|
|
228
293
|
throw new ComposioFailedToSubscribeToPusherChannelError(
|
|
229
294
|
'Failed to subscribe to Pusher channel',
|
|
@@ -235,19 +300,29 @@ export class PusherService {
|
|
|
235
300
|
}
|
|
236
301
|
|
|
237
302
|
/**
|
|
238
|
-
* Unsubscribes from
|
|
239
|
-
*
|
|
240
|
-
* This method is used to unsubscribe from a Pusher channel.
|
|
241
|
-
* It will create a Pusher client if it doesn't exist.
|
|
303
|
+
* Unsubscribes from the triggers channel and closes the connection
|
|
242
304
|
*
|
|
243
|
-
*
|
|
305
|
+
* This ends the subscription period: setup still in flight is cancelled, the handlers bound by
|
|
306
|
+
* every `subscribe()` call are removed, and the Pusher client is disconnected. Without a prior
|
|
307
|
+
* `subscribe()` this does nothing; it never creates a client or fetches credentials.
|
|
244
308
|
*/
|
|
245
309
|
async unsubscribe() {
|
|
310
|
+
const { attempt, connection } = this;
|
|
311
|
+
this.attempt = undefined;
|
|
312
|
+
this.connection = undefined;
|
|
313
|
+
attempt?.controller.abort();
|
|
314
|
+
|
|
315
|
+
if (!connection) {
|
|
316
|
+
logger.debug('[PusherService] No open connection to unsubscribe from');
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
logger.debug(`[PusherService] Unsubscribing from channel: ${connection.channelName}`);
|
|
246
321
|
try {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
322
|
+
// pusher-js keeps a reference to every client it creates, so release the handlers this
|
|
323
|
+
// service bound and the user callbacks they hold.
|
|
324
|
+
connection.channel.unbind_all();
|
|
325
|
+
connection.client.unsubscribe(connection.channelName);
|
|
251
326
|
} catch (error) {
|
|
252
327
|
throw new ComposioFailedToSubscribeToPusherChannelError(
|
|
253
328
|
'Failed to unsubscribe from Pusher channel',
|
|
@@ -255,6 +330,9 @@ export class PusherService {
|
|
|
255
330
|
cause: error,
|
|
256
331
|
}
|
|
257
332
|
);
|
|
333
|
+
} finally {
|
|
334
|
+
connection.client.disconnect();
|
|
258
335
|
}
|
|
336
|
+
logger.info(`✅ Unsubscribed from triggers.`);
|
|
259
337
|
}
|
|
260
338
|
}
|
|
@@ -222,7 +222,8 @@ const S2SOauth2ConnectionDataSchema = z.discriminatedUnion('status', [
|
|
|
222
222
|
}).catchall(z.unknown()),
|
|
223
223
|
]);
|
|
224
224
|
const CustomS2SOauth2ConnectionDataSchema = BaseSchemeRaw.extend({
|
|
225
|
-
|
|
225
|
+
// Required by the API: custom connection data supplies the token itself.
|
|
226
|
+
access_token: z.string(),
|
|
226
227
|
id_token: z.string().optional(),
|
|
227
228
|
token_type: z.string().optional(),
|
|
228
229
|
refresh_token: z.string().nullish().optional(),
|
|
@@ -2,12 +2,14 @@ export interface PusherClient {
|
|
|
2
2
|
subscribe: (channelName: string) => PusherChannel;
|
|
3
3
|
unsubscribe: (channelName: string) => void;
|
|
4
4
|
bind: (event: string, callback: (data: Record<string, unknown>) => void) => void;
|
|
5
|
+
disconnect: () => void;
|
|
5
6
|
}
|
|
6
7
|
|
|
7
8
|
export interface PusherChannel {
|
|
8
9
|
subscribe: (channelName: string) => unknown;
|
|
9
10
|
unsubscribe: (channelName: string) => void;
|
|
10
11
|
bind: (event: string, callback: (data: Record<string, unknown>) => void) => void;
|
|
12
|
+
unbind_all: () => void;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export type TChunkedTriggerData = {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type { RequestOptions as ClientRequestOptions } from '@composio/client';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Per-request transport options for cancelling an SDK call.
|
|
@@ -38,15 +38,6 @@ export type ComposioRequestOptions = {
|
|
|
38
38
|
signal?: AbortSignal;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
-
/**
|
|
42
|
-
* The `@composio/client` per-request transport options, reached via a public
|
|
43
|
-
* method whose only parameter is that type (`tools.retrieveEnum`). The
|
|
44
|
-
* `internal/*` subpath that declares `RequestOptions` is not in the package's
|
|
45
|
-
* `exports`, so we derive it structurally instead of deep-importing it.
|
|
46
|
-
* @internal
|
|
47
|
-
*/
|
|
48
|
-
type ClientRequestOptions = NonNullable<Parameters<ComposioClient['tools']['retrieveEnum']>[0]>;
|
|
49
|
-
|
|
50
41
|
/**
|
|
51
42
|
* Compile-time guards: {@link ComposioRequestOptions} is forwarded verbatim to
|
|
52
43
|
* every `@composio/client` call, so it MUST stay a structural subset of the
|