@devries-cameron20 / composio Mirror · read-only / commits / f478a3d1f
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, 11:00 UTC

fix(core): keep `ConnectionRequest.status` live and restore its telemetry (#4678)

This PR: - closes https://github.com/ComposioHQ/composio/issues/4663 - replaces the `{ ...state }` snapshot in `createConnectionRequest()` with a single mutable request object, so `request.status` and `toJSON()` read the same value - records the first retrieved status before the terminal check, so an immediate `FAILED`/`EXPIRED`/`REVOKED` is reflected instead of `INITIATED` - keeps `id`/`status`/`redirectUrl` as plain assignable fields; getter-only accessors (the approach in the issue's linked branch) would throw a `TypeError` on assignment in ESM strict mode despite the writable `ConnectionRequest` type - matches the Python SDK, where `wait_for_connection()` already updates `self.status` on every retrieve, so no Python change is needed - restores telemetry for `ConnectionRequest.waitForConnection()`, which stopped when #1678 turned the `ConnectionRequest` class into a factory: `telemetry.instrument()` only scanned the prototype, and a plain object's methods are own properties. It now reads own properties for plain objects; class instances are unchanged - fixes the stale `@returns` JSDoc on `createConnectionRequest()` - adds regression tests for the immediate-`ACTIVE`, polling and `REVOKED` paths plus field assignability (4 fail on `next` without the fix), and for instrumenting plain objects (fails without the `Telemetry.ts` change)

Alberto Schiabel committed Sep 28, 2026 at 19:48 UTC f478a3d1f55415c0ff3181c9eb51d328c31c5cc8
6 files changed +103 -20
.changeset/connection-request-live-status.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Keep `ConnectionRequest.status` in sync with `waitForConnection()`. The request now reports `ACTIVE` once the connection completes, or the terminal status (`FAILED`, `EXPIRED`, `REVOKED`) when it fails, matching `toJSON()` and the Python SDK.
.changeset/connection-request-telemetry.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Restore telemetry for `ConnectionRequest.waitForConnection()`. `telemetry.instrument()` now also instruments async methods defined directly on plain objects, such as the ones returned by `createConnectionRequest()`.
ts/packages/core/src/models/ConnectionRequest.ts
+21 -18
@@ -27,8 +27,7 @@ import { ConnectionRequest, ConnectionRequestState } from '../types/connectionRe
27 * @param {string} connectedAccountId - The ID of the connected account
28 * @param {ConnectedAccountStatus} [status] - Initial status of the connection
29 * @param {string | null} [redirectUrl] - OAuth redirect URL if applicable
30 - * @returns {ConnectionRequestState & { waitForConnection: (timeout?: number) => Promise<ConnectedAccountRetrieveResponse> }}
31 - * Connection request object with state and methods
30 + * @returns {ConnectionRequest} Connection request object with state and methods
31 */
32 export function createConnectionRequest(
33 client: ComposioClient,
@@ -36,13 +35,22 @@ export function createConnectionRequest(
35 status?: ConnectedAccountStatus,
36 redirectUrl?: string | null
37 ): ConnectionRequest {
39 - const state: ConnectionRequestState = {
38 + // `waitForConnection` records each observed status on this same object, so
39 + // `request.status` and `toJSON()` never disagree.
40 + const request: ConnectionRequest = {
41 id: connectedAccountId,
42 status: status || ConnectedAccountStatuses.INITIATED,
43 redirectUrl,
44 + waitForConnection,
45 + toJSON: (): ConnectionRequestState => ({
46 + id: request.id,
47 + status: request.status,
48 + redirectUrl: request.redirectUrl,
49 + }),
50 + toString: () => JSON.stringify(request.toJSON(), null, 2),
51 };
52
45 - telemetry.instrument(state, 'ConnectionRequest');
53 + telemetry.instrument(request, 'ConnectionRequest');
54
55 /**
56 * Waits for the connection request to complete and become active.
@@ -87,7 +95,7 @@ export function createConnectionRequest(
95 `Connection request failed with status: ${response.status}${response.status_reason ? `, reason: ${response.status_reason}` : ''}`,
96 {
97 meta: {
90 - connectedAccountId: state.id,
98 + connectedAccountId: request.id,
99 status: response.status,
100 statusReason: response.status_reason,
101 },
@@ -97,19 +105,19 @@ export function createConnectionRequest(
105 };
106
107 try {
100 - const response = await client.connectedAccounts.retrieve(state.id);
108 + const response = await client.connectedAccounts.retrieve(request.id);
109 + request.status = response.status;
110 if (response.status === ConnectedAccountStatuses.ACTIVE) {
102 - state.status = ConnectedAccountStatuses.ACTIVE;
111 return transformConnectedAccountResponse(response);
112 }
113 failIfTerminal(response);
114 } catch (error) {
115 if (error instanceof ComposioClient.NotFoundError) {
116 throw new ComposioConnectedAccountNotFoundError(
109 - `Connected account with id ${state.id} not found`,
117 + `Connected account with id ${request.id} not found`,
118 {
119 meta: {
112 - connectedAccountId: state.id,
120 + connectedAccountId: request.id,
121 },
122 }
123 );
@@ -123,9 +131,9 @@ export function createConnectionRequest(
131
132 while (Date.now() - start < timeout) {
133 try {
126 - const response = await client.connectedAccounts.retrieve(state.id);
134 + const response = await client.connectedAccounts.retrieve(request.id);
135
128 - state.status = response.status;
136 + request.status = response.status;
137 if (response.status === ConnectedAccountStatuses.ACTIVE) {
138 return transformConnectedAccountResponse(response);
139 }
@@ -138,13 +146,8 @@ export function createConnectionRequest(
146 }
147 }
148
141 - throw new ConnectionRequestTimeoutError(`Connection request timed out for ${state.id}`);
149 + throw new ConnectionRequestTimeoutError(`Connection request timed out for ${request.id}`);
150 }
151
144 - return {
145 - ...state,
146 - waitForConnection,
147 - toJSON: () => ({ ...state }),
148 - toString: () => JSON.stringify(state, null, 2),
149 - };
152 + return request;
153 }
ts/packages/core/src/telemetry/Telemetry.ts
+4 -2
@@ -94,8 +94,10 @@ export class TelemetryTransport {
94 */
95 instrument<T extends object>(instance: T, fileName?: string) {
96 const proto = Object.getPrototypeOf(instance);
97 - const methodNames = Object.getOwnPropertyNames(proto).filter(key => {
98 - const descriptor = Object.getOwnPropertyDescriptor(proto, key);
97 + // Objects returned by factory functions carry their methods as own properties.
98 + const methodSource = proto === Object.prototype || proto === null ? instance : proto;
99 + const methodNames = Object.getOwnPropertyNames(methodSource).filter(key => {
100 + const descriptor = Object.getOwnPropertyDescriptor(methodSource, key);
101 return (
102 key !== 'constructor' &&
103 descriptor &&
ts/packages/core/test/connectedAccounts/connectionRequest.test.ts
+53
@@ -46,6 +46,21 @@ describe('ConnectionRequest', () => {
46
47 expect(connectionRequest).toHaveProperty('redirectUrl', undefined);
48 });
49 +
50 + it('should keep its fields assignable and reflected in toJSON()', () => {
51 + connectionRequest = createConnectionRequest(
52 + mockClient as unknown as ComposioClient,
53 + connectedAccountId
54 + );
55 +
56 + connectionRequest.redirectUrl = redirectUrl;
57 +
58 + expect(connectionRequest.toJSON()).toEqual({
59 + id: connectedAccountId,
60 + status: ConnectedAccountStatuses.INITIATED,
61 + redirectUrl,
62 + });
63 + });
64 });
65
66 describe('waitForConnection', () => {
@@ -98,6 +113,41 @@ describe('ConnectionRequest', () => {
113 expect(result).toHaveProperty('state.authScheme', 'OAUTH2');
114 });
115
116 + it('should report ACTIVE on the request once waitForConnection resolves', async () => {
117 + connectionRequest = createConnectionRequest(
118 + mockClient as unknown as ComposioClient,
119 + connectedAccountId,
120 + ConnectedAccountStatuses.INITIATED,
121 + redirectUrl
122 + );
123 + mockClient.connectedAccounts.retrieve.mockResolvedValueOnce({
124 + id: connectedAccountId,
125 + status: ConnectedAccountStatuses.ACTIVE,
126 + auth_config: {
127 + id: 'auth_config_123',
128 + auth_scheme: 'OAUTH2',
129 + is_composio_managed: true,
130 + is_disabled: false,
131 + },
132 + user_id: 'user_123',
133 + data: {},
134 + params: {},
135 + is_disabled: false,
136 + created_at: '2023-01-01T00:00:00Z',
137 + updated_at: '2023-01-01T00:00:00Z',
138 + status_reason: null,
139 + toolkit: {
140 + slug: 'test-toolkit',
141 + },
142 + });
143 +
144 + await connectionRequest.waitForConnection();
145 +
146 + expect(connectionRequest.status).toBe(ConnectedAccountStatuses.ACTIVE);
147 + expect(connectionRequest.toJSON().status).toBe(ConnectedAccountStatuses.ACTIVE);
148 + expect(JSON.parse(connectionRequest.toString()).status).toBe(ConnectedAccountStatuses.ACTIVE);
149 + });
150 +
151 it('should poll until status becomes ACTIVE', async () => {
152 // Use fake timers to control timing
153 vi.useFakeTimers();
@@ -158,6 +208,7 @@ describe('ConnectionRequest', () => {
208 expect(mockClient.connectedAccounts.retrieve).toHaveBeenCalledTimes(2);
209 expect(result).toHaveProperty('id', connectedAccountId);
210 expect(result).toHaveProperty('status', ConnectedAccountStatuses.ACTIVE);
211 + expect(connectionRequest.status).toBe(ConnectedAccountStatuses.ACTIVE);
212 });
213
214 it('should throw ConnectionRequestTimeoutError if the request times out', async () => {
@@ -242,6 +293,8 @@ describe('ConnectionRequest', () => {
293 await expect(connectionRequest.waitForConnection(3000)).rejects.toThrow(
294 ConnectionRequestFailedError
295 );
296 + expect(connectionRequest.status).toBe(ConnectedAccountStatuses.REVOKED);
297 + expect(connectionRequest.toJSON().status).toBe(ConnectedAccountStatuses.REVOKED);
298 });
299 });
300
ts/packages/core/test/telemetry/TelemetryService.test.ts
+15
@@ -338,6 +338,7 @@ describe('TelemetryTransport', () => {
338
339 afterEach(() => {
340 vi.restoreAllMocks();
341 + vi.unstubAllEnvs();
342 });
343
344 it('should send SDK_INITIALIZED metric on setup', () => {
@@ -373,6 +374,20 @@ describe('TelemetryTransport', () => {
374 expect(sendMetricSpy).toHaveBeenCalled();
375 });
376
377 + it('should instrument async methods defined on plain objects', async () => {
378 + vi.stubEnv('NODE_ENV', 'development');
379 + const instance = { double: async (a: number) => a * 2 };
380 +
381 + transport.instrument(instance, 'PlainObject');
382 +
383 + await expect(instance.double(21)).resolves.toBe(42);
384 + await transport.flush();
385 + const functionNames = sendMetricSpy.mock.calls.flatMap(([payloads]) =>
386 + payloads.map((payload: TelemetryPayload) => payload.functionName)
387 + );
388 + expect(functionNames).toContain('PlainObject.double');
389 + });
390 +
391 it('should send error telemetry if instrumented method throws', async () => {
392 class TestClass {
393 async fail() {