@composio/core 0.19.0 → 0.21.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-BMlZfJVz.mjs → CustomTool-B-_VjWpy.mjs} +1 -1
- package/dist/{CustomTool-CB5vm1Jc.d.mts → CustomTool-BHOYxLZX.d.mts} +1 -1
- package/dist/{FileModifierErrors-BqFmpRsm.mjs → FileModifierErrors-CdGSABoq.mjs} +1 -1
- package/dist/{SsrfErrors-C91YhASE.mjs → SsrfErrors-DiQldIdC.mjs} +18 -7
- package/dist/{Telemetry-HlftxMx6.mjs → Telemetry-JRJJYS4I.mjs} +2 -2
- package/dist/{cancellation-CXpfmiVo.mjs → cancellation-DV8CIi52.mjs} +2 -2
- package/dist/{composio-DLGaeN70.d.mts → composio-BTZsFajb.d.mts} +3149 -117
- package/dist/{customTool.types-BAmEh3MB.d.mts → customTool.types-D_w21L56.d.mts} +784 -56
- package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +10 -10
- package/dist/docs/reference/sdk-reference/typescript/composio.mdx +7 -3
- package/dist/docs/reference/sdk-reference/typescript/connected-accounts.mdx +101 -18
- package/dist/docs/reference/sdk-reference/typescript/custom-toolkits.mdx +123 -0
- package/dist/docs/reference/sdk-reference/typescript/experimental.mdx +9 -2
- package/dist/docs/reference/sdk-reference/typescript/index.mdx +8 -0
- package/dist/docs/reference/sdk-reference/typescript/keyring.mdx +56 -0
- package/dist/docs/reference/sdk-reference/typescript/logs.mdx +78 -0
- package/dist/docs/reference/sdk-reference/typescript/mcp.mdx +15 -15
- package/dist/docs/reference/sdk-reference/typescript/meta.json +9 -1
- package/dist/docs/reference/sdk-reference/typescript/session-configs.mdx +83 -0
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +10 -10
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +86 -27
- package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +26 -6
- package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +149 -12
- 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 +78 -0
- package/dist/docs/reference/sdk-reference/typescript/webhook-endpoints.mdx +162 -0
- package/dist/docs/reference/sdk-reference/typescript/webhook-subscriptions.mdx +218 -0
- package/dist/docs/reference/sdk-reference/typescript/webhooks.mdx +20 -0
- package/dist/{errors-BuEKehbI.mjs → errors-Dyiu3ifQ.mjs} +127 -4
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-L7XD8UPr.mjs → fileUtils.node-DNLhLg0W.mjs} +4 -4
- package/dist/index.d.mts +281 -55
- package/dist/index.mjs +3552 -949
- package/dist/models/Files.node.mjs +2 -2
- package/dist/{sensitiveFileUploadPaths-B6pBt9lO.mjs → sensitiveFileUploadPaths-BlLp-Yo2.mjs} +1 -1
- package/dist/{ssrfGuard.node-B9xQB4za.mjs → ssrfGuard.node-BZ270bcu.mjs} +1 -1
- package/dist/{types-CxLPO0Xq.d.mts → types-CgRsUGqm.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 +5 -5
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -1
- package/dist/utils/ssrfGuard.node.mjs +1 -1
- package/dist/utils/ssrfGuard.workerd.mjs +1 -1
- package/package.json +4 -2
- package/src/composio.ts +215 -10
- package/src/errors/ConnectedAccountsErrors.ts +47 -0
- package/src/errors/SDKErrors.ts +52 -0
- package/src/errors/ToolRouterErrors.ts +61 -0
- package/src/errors/index.ts +3 -0
- package/src/index.ts +13 -0
- package/src/lib/sessionConfigConflict.ts +43 -0
- package/src/lib/toolRouterMcp.ts +132 -0
- package/src/lib/toolRouterParams.ts +149 -37
- package/src/lib/toolRouterSourceSessionConfig.ts +12 -0
- package/src/models/ConnectedAccounts.ts +118 -2
- package/src/models/CustomToolkits.ts +169 -0
- package/src/models/Experimental.ts +27 -0
- package/src/models/Keyring.ts +65 -0
- package/src/models/Logs.ts +101 -0
- package/src/models/RemoteFile.ts +25 -8
- package/src/models/SessionConfigs.ts +117 -0
- package/src/models/ToolRouter.ts +61 -17
- package/src/models/ToolRouterSession.ts +149 -14
- package/src/models/Toolkits.ts +180 -0
- package/src/models/Tools.ts +10 -6
- package/src/models/Triggers.ts +3 -88
- package/src/models/Usage.ts +119 -0
- package/src/models/Webhooks.ts +520 -0
- package/src/services/pusher/Pusher.ts +17 -6
- package/src/types/authConfigs.types.ts +2 -0
- package/src/types/connectedAccounts.types.ts +54 -0
- package/src/types/customToolkits.types.ts +102 -0
- package/src/types/keyring.types.ts +32 -0
- package/src/types/logs.types.ts +108 -0
- package/src/types/sessionConfigs.types.ts +81 -0
- package/src/types/tool.types.ts +2 -0
- package/src/types/toolRouter.types.ts +352 -16
- package/src/types/toolkit.types.ts +144 -0
- package/src/types/usage.types.ts +81 -0
- package/src/types/webhooks.types.ts +143 -0
- package/src/utils/constants.ts +1 -5
- package/src/utils/logger.ts +39 -7
- package/src/utils/safePath.ts +172 -0
- package/src/utils/sdk.ts +385 -22
- package/src/utils/transformers/connectedAccounts.ts +37 -2
- package/src/utils/transformers/customToolkits.ts +98 -0
- package/src/utils/transformers/keyring.ts +24 -0
- package/src/utils/transformers/logs.ts +85 -0
- package/src/utils/transformers/sessionConfigs.ts +54 -0
- package/src/utils/transformers/toolRouterResponseTransform.ts +6 -0
- package/src/utils/transformers/toolkits.ts +106 -3
- package/src/utils/transformers/triggers.ts +2 -2
- package/src/utils/transformers/usage.ts +77 -0
- package/src/utils/transformers/webhooks.ts +139 -0
package/src/utils/sdk.ts
CHANGED
|
@@ -1,12 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { z } from 'zod/v3';
|
|
2
|
+
import {
|
|
3
|
+
ComposioAPIKeyKindError,
|
|
4
|
+
ComposioNoAPIKeyError,
|
|
5
|
+
ComposioScopeConfigError,
|
|
6
|
+
} from '../errors/SDKErrors';
|
|
2
7
|
import { COMPOSIO_DIR, DEFAULT_BASE_URL, USER_DATA_FILE_NAME } from './constants';
|
|
3
8
|
import { getEnvsWithPrefix, getEnvVariable } from './env';
|
|
4
9
|
import logger from './logger';
|
|
5
10
|
import { ComposioError } from '../errors/ComposioError';
|
|
6
11
|
import { ToolkitVersion, ToolkitVersionParam, ToolkitVersions } from '../types/tool.types';
|
|
12
|
+
import type { ComposioRequestHeaders } from '../types/composio.types';
|
|
7
13
|
import { normalizeToolkitSlug } from './toolkitVersion';
|
|
8
14
|
import { platform } from '#platform';
|
|
9
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Header that carries a Composio user API key (`uak_...`) instead of a project
|
|
18
|
+
* API key. It is the only default header the credential resolver reads.
|
|
19
|
+
*/
|
|
20
|
+
export const USER_API_KEY_HEADER = 'x-user-api-key';
|
|
21
|
+
|
|
22
|
+
/** Prefix of Composio user API keys as issued by `composio login`. */
|
|
23
|
+
const USER_API_KEY_PREFIX = 'uak_';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Header that carries the project API key. The SDK only ever sets it from the
|
|
27
|
+
* resolved `apiKey`; a caller-placed entry in `defaultHeaders` is rejected
|
|
28
|
+
* when project-key auth is disabled.
|
|
29
|
+
*/
|
|
30
|
+
export const PROJECT_API_KEY_HEADER = 'x-api-key';
|
|
31
|
+
|
|
32
|
+
/** Header carrying the organization nano ID that scopes user-key requests. */
|
|
33
|
+
export const ORG_ID_HEADER = 'x-org-id';
|
|
34
|
+
|
|
35
|
+
/** Header carrying the project nano ID that scopes user-key requests. */
|
|
36
|
+
export const PROJECT_ID_HEADER = 'x-project-id';
|
|
37
|
+
|
|
10
38
|
// File path helpers
|
|
11
39
|
export const userDataPath = () => {
|
|
12
40
|
try {
|
|
@@ -22,38 +50,373 @@ export const userDataPath = () => {
|
|
|
22
50
|
};
|
|
23
51
|
|
|
24
52
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
53
|
+
* Shape of `~/.composio/user_data.json`. The file is written by the Composio
|
|
54
|
+
* CLI; only the two fields the SDK consumes are validated, everything else is
|
|
55
|
+
* carried through untouched.
|
|
56
|
+
*/
|
|
57
|
+
const UserDataFileSchema = z
|
|
58
|
+
.object({
|
|
59
|
+
api_key: z.string().nullish(),
|
|
60
|
+
base_url: z.string().nullish(),
|
|
61
|
+
})
|
|
62
|
+
.passthrough();
|
|
63
|
+
|
|
64
|
+
export type UserDataFile = z.infer<typeof UserDataFileSchema>;
|
|
65
|
+
|
|
66
|
+
export type UserDataFileRead =
|
|
67
|
+
/** No file system, no home directory, or no file: nothing to fall back to. */
|
|
68
|
+
| { status: 'missing' }
|
|
69
|
+
/** The file exists but is not JSON or does not have the expected shape. */
|
|
70
|
+
| { status: 'malformed'; path: string; reason: string }
|
|
71
|
+
| { status: 'ok'; path: string; data: UserDataFile };
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Reads and validates the user data JSON file from the Composio directory.
|
|
75
|
+
* File contents never appear in the returned diagnostics.
|
|
28
76
|
*/
|
|
29
|
-
export const
|
|
77
|
+
export const readUserDataFile = (): UserDataFileRead => {
|
|
78
|
+
if (!platform.supportsFileSystem) {
|
|
79
|
+
return { status: 'missing' };
|
|
80
|
+
}
|
|
81
|
+
const dataPath = userDataPath();
|
|
82
|
+
if (!dataPath) {
|
|
83
|
+
return { status: 'missing' };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let raw: string | Uint8Array;
|
|
30
87
|
try {
|
|
31
|
-
|
|
32
|
-
if (!dataPath || !platform.supportsFileSystem) {
|
|
33
|
-
return {};
|
|
34
|
-
}
|
|
35
|
-
const data = platform.readFileSync(dataPath, 'utf8');
|
|
36
|
-
return JSON.parse(data as string);
|
|
88
|
+
raw = platform.readFileSync(dataPath, 'utf8');
|
|
37
89
|
} catch (_error) {
|
|
38
90
|
logger.debug('Environment', 'No user data file found');
|
|
39
|
-
return {};
|
|
91
|
+
return { status: 'missing' };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let json: unknown;
|
|
95
|
+
try {
|
|
96
|
+
json = JSON.parse(typeof raw === 'string' ? raw : new TextDecoder().decode(raw));
|
|
97
|
+
} catch (_error) {
|
|
98
|
+
logger.debug('Environment', `User data file at ${dataPath} could not be parsed as JSON`);
|
|
99
|
+
return { status: 'malformed', path: dataPath, reason: 'could not be parsed as JSON' };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const parsed = UserDataFileSchema.safeParse(json);
|
|
103
|
+
if (!parsed.success) {
|
|
104
|
+
const fields = parsed.error.issues
|
|
105
|
+
.map(issue => issue.path.join('.'))
|
|
106
|
+
.filter(Boolean)
|
|
107
|
+
.join(', ');
|
|
108
|
+
const reason = fields
|
|
109
|
+
? `has an unexpected shape (invalid field${fields.includes(',') ? 's' : ''}: ${fields})`
|
|
110
|
+
: 'has an unexpected shape (expected a JSON object)';
|
|
111
|
+
logger.debug('Environment', `User data file at ${dataPath} ${reason}`);
|
|
112
|
+
return { status: 'malformed', path: dataPath, reason };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return { status: 'ok', path: dataPath, data: parsed.data };
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const findHeader = (
|
|
119
|
+
headers: ComposioRequestHeaders | undefined,
|
|
120
|
+
name: string
|
|
121
|
+
): { name: string; value: string } | undefined => {
|
|
122
|
+
if (!headers) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const wanted = name.toLowerCase();
|
|
126
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
127
|
+
if (key.toLowerCase() === wanted) {
|
|
128
|
+
return { name: key, value };
|
|
129
|
+
}
|
|
40
130
|
}
|
|
131
|
+
return undefined;
|
|
41
132
|
};
|
|
42
133
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Returns the `x-user-api-key` entry from a header map, if it carries a
|
|
136
|
+
* non-empty value. Matches the header name case-insensitively and ignores
|
|
137
|
+
* every other header.
|
|
138
|
+
*/
|
|
139
|
+
export const getUserApiKeyHeader = (
|
|
140
|
+
headers: ComposioRequestHeaders | undefined
|
|
141
|
+
): { name: string; value: string } | undefined => {
|
|
142
|
+
const header = findHeader(headers, USER_API_KEY_HEADER);
|
|
143
|
+
return header && typeof header.value === 'string' && header.value.length > 0 ? header : undefined;
|
|
144
|
+
};
|
|
47
145
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
146
|
+
export type CredentialHeaderInput = {
|
|
147
|
+
/** The project API key the client resolved, or `null` when disabled. */
|
|
148
|
+
apiKey: string | null | undefined;
|
|
149
|
+
/** The user API key the client resolved (`userApiKey` option or `COMPOSIO_USER_API_KEY`). */
|
|
150
|
+
userApiKey: string | null | undefined;
|
|
151
|
+
/** The default headers configured on the SDK instance; only `x-user-api-key` is consulted. */
|
|
152
|
+
defaultHeaders: ComposioRequestHeaders | undefined;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The single credential header that mirrors what the API client puts on the
|
|
157
|
+
* wire for an SDK instance. A non-empty `x-user-api-key` entry in the default
|
|
158
|
+
* headers wins: the client treats a caller-placed credential header as the
|
|
159
|
+
* credential and suppresses the configured keys. Otherwise the project key
|
|
160
|
+
* travels as `x-api-key`, otherwise the resolved user API key as
|
|
161
|
+
* `x-user-api-key`. Empty when no credential is held. The environment is
|
|
162
|
+
* never consulted here.
|
|
163
|
+
*/
|
|
164
|
+
export const resolveCredentialHeaders = (input: CredentialHeaderInput): Record<string, string> => {
|
|
165
|
+
const userApiKeyHeader = getUserApiKeyHeader(input.defaultHeaders);
|
|
166
|
+
if (userApiKeyHeader) {
|
|
167
|
+
return { [USER_API_KEY_HEADER]: userApiKeyHeader.value };
|
|
168
|
+
}
|
|
169
|
+
if (hasValue(input.apiKey)) {
|
|
170
|
+
return { [PROJECT_API_KEY_HEADER]: input.apiKey };
|
|
171
|
+
}
|
|
172
|
+
if (hasValue(input.userApiKey)) {
|
|
173
|
+
return { [USER_API_KEY_HEADER]: input.userApiKey };
|
|
174
|
+
}
|
|
175
|
+
return {};
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
export type SDKScopeOptions = {
|
|
179
|
+
/** The `orgId` constructor option. */
|
|
180
|
+
orgId?: string;
|
|
181
|
+
/** The `projectId` constructor option. */
|
|
182
|
+
projectId?: string;
|
|
183
|
+
/** Default headers the SDK will attach to every request. */
|
|
184
|
+
defaultHeaders?: ComposioRequestHeaders;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export type SDKScope = {
|
|
188
|
+
orgId?: string;
|
|
189
|
+
projectId?: string;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const resolveScopeValue = (
|
|
193
|
+
option: string | undefined,
|
|
194
|
+
headers: ComposioRequestHeaders | undefined,
|
|
195
|
+
headerName: string,
|
|
196
|
+
optionName: 'orgId' | 'projectId'
|
|
197
|
+
): string | undefined => {
|
|
198
|
+
const header = findHeader(headers, headerName);
|
|
199
|
+
const headerValue = header && header.value.length > 0 ? header.value : undefined;
|
|
200
|
+
const optionValue = hasValue(option) ? option : undefined;
|
|
201
|
+
if (optionValue !== undefined && headerValue !== undefined && optionValue !== headerValue) {
|
|
202
|
+
throw new ComposioScopeConfigError(
|
|
203
|
+
`\`${optionName}\` and the \`${headerName}\` default header disagree`,
|
|
204
|
+
{
|
|
205
|
+
cause: `The ${optionName} option and the ${headerName} entry in defaultHeaders carry different values; the SDK keeps a single scope source`,
|
|
206
|
+
meta: { option: optionName, header: headerName },
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
return optionValue ?? headerValue;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Resolves the organization/project scope of an SDK instance.
|
|
215
|
+
*
|
|
216
|
+
* The scope comes from the `orgId` / `projectId` options, or from the
|
|
217
|
+
* `x-org-id` / `x-project-id` default headers when the options are omitted.
|
|
218
|
+
* A value present in both places must agree, and the two IDs must be supplied
|
|
219
|
+
* together: a user API key without an explicit scope keeps the API default
|
|
220
|
+
* (the developer project), so a half-configured scope is rejected instead of
|
|
221
|
+
* silently selecting a project.
|
|
222
|
+
*/
|
|
223
|
+
export function resolveSDKScope(options: SDKScopeOptions): SDKScope {
|
|
224
|
+
const orgId = resolveScopeValue(options.orgId, options.defaultHeaders, ORG_ID_HEADER, 'orgId');
|
|
225
|
+
const projectId = resolveScopeValue(
|
|
226
|
+
options.projectId,
|
|
227
|
+
options.defaultHeaders,
|
|
228
|
+
PROJECT_ID_HEADER,
|
|
229
|
+
'projectId'
|
|
230
|
+
);
|
|
231
|
+
if ((orgId === undefined) !== (projectId === undefined)) {
|
|
232
|
+
throw new ComposioScopeConfigError(
|
|
233
|
+
orgId === undefined
|
|
234
|
+
? '`projectId` requires `orgId`: the organization and project nano IDs scope requests together'
|
|
235
|
+
: '`orgId` requires `projectId`: the organization and project nano IDs scope requests together',
|
|
236
|
+
{
|
|
237
|
+
cause: 'Only one of the two scope identifiers was configured',
|
|
238
|
+
meta: { orgId: orgId !== undefined, projectId: projectId !== undefined },
|
|
239
|
+
}
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
return orgId === undefined || projectId === undefined ? {} : { orgId, projectId };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** The `x-org-id` / `x-project-id` headers for a resolved scope. */
|
|
246
|
+
export const getScopeHeaders = (scope: SDKScope): ComposioRequestHeaders =>
|
|
247
|
+
scope.orgId && scope.projectId
|
|
248
|
+
? { [ORG_ID_HEADER]: scope.orgId, [PROJECT_ID_HEADER]: scope.projectId }
|
|
249
|
+
: {};
|
|
250
|
+
|
|
251
|
+
export type SDKConfigOptions = {
|
|
252
|
+
/** Default headers the SDK will attach to every request. */
|
|
253
|
+
defaultHeaders?: ComposioRequestHeaders;
|
|
254
|
+
/**
|
|
255
|
+
* The `userApiKey` constructor option, exactly as the caller passed it:
|
|
256
|
+
* `undefined` lets the API client read `COMPOSIO_USER_API_KEY`, `null`
|
|
257
|
+
* disables that fallback.
|
|
258
|
+
*/
|
|
259
|
+
userApiKey?: string | null;
|
|
260
|
+
/**
|
|
261
|
+
* The `orgApiKey` constructor option, exactly as the caller passed it:
|
|
262
|
+
* `undefined` lets the API client read `COMPOSIO_ORG_API_KEY`, `null`
|
|
263
|
+
* disables that fallback.
|
|
264
|
+
*/
|
|
265
|
+
orgApiKey?: string | null;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
export type SDKConfig = {
|
|
269
|
+
baseURL: string;
|
|
270
|
+
/** The project API key to send as `x-api-key`, or `null` when project-key auth is disabled. */
|
|
271
|
+
apiKey: string | null;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const hasValue = (value: string | null | undefined): value is string =>
|
|
275
|
+
typeof value === 'string' && value.length > 0;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Mirrors how the API client resolves `userApiKey` / `orgApiKey`: an explicit
|
|
279
|
+
* value (including `null`) is final, `undefined` falls back to the environment.
|
|
280
|
+
*/
|
|
281
|
+
const resolveAlternateCredential = (
|
|
282
|
+
explicit: string | null | undefined,
|
|
283
|
+
envName: string
|
|
284
|
+
): string | undefined => {
|
|
285
|
+
if (explicit !== undefined) {
|
|
286
|
+
return hasValue(explicit) ? explicit : undefined;
|
|
287
|
+
}
|
|
288
|
+
const fromEnv = getEnvVariable(envName);
|
|
289
|
+
return hasValue(fromEnv) ? fromEnv : undefined;
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The user API key the API client will hold: the `userApiKey` option when it
|
|
294
|
+
* was passed (including `null`, which disables the fallback), otherwise
|
|
295
|
+
* `COMPOSIO_USER_API_KEY`.
|
|
296
|
+
*/
|
|
297
|
+
export const resolveUserApiKey = (userApiKey: string | null | undefined): string | undefined =>
|
|
298
|
+
resolveAlternateCredential(userApiKey, 'COMPOSIO_USER_API_KEY');
|
|
51
299
|
|
|
52
|
-
|
|
53
|
-
|
|
300
|
+
/**
|
|
301
|
+
* Whether the instance holds a credential other than the project key: a user
|
|
302
|
+
* or organization API key (explicit or environment-resolved, as the API client
|
|
303
|
+
* will see it), or a user API key placed in the default headers.
|
|
304
|
+
*/
|
|
305
|
+
const hasAlternateCredential = (options: SDKConfigOptions): boolean =>
|
|
306
|
+
resolveUserApiKey(options.userApiKey) !== undefined ||
|
|
307
|
+
resolveAlternateCredential(options.orgApiKey, 'COMPOSIO_ORG_API_KEY') !== undefined ||
|
|
308
|
+
getUserApiKeyHeader(options.defaultHeaders) !== undefined;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Resolves the base URL and project API key the SDK will use.
|
|
312
|
+
*
|
|
313
|
+
* Project key resolution distinguishes three caller intents:
|
|
314
|
+
* - a non-empty string is used as-is and nothing else is consulted;
|
|
315
|
+
* - `undefined` (or an empty string) falls back to `COMPOSIO_API_KEY`, then to
|
|
316
|
+
* the `api_key` stored by the CLI in `~/.composio/user_data.json`;
|
|
317
|
+
* - `null` disables project-key auth: neither `COMPOSIO_API_KEY` nor the stored
|
|
318
|
+
* `api_key` is read, and the resolver only succeeds when the instance holds
|
|
319
|
+
* another credential: a `userApiKey` or `orgApiKey` (explicit, or resolved
|
|
320
|
+
* from `COMPOSIO_USER_API_KEY` / `COMPOSIO_ORG_API_KEY` the way the API
|
|
321
|
+
* client does), or a non-empty `x-user-api-key` entry in `defaultHeaders`.
|
|
322
|
+
* An `x-api-key` entry in `defaultHeaders` (any letter case) is rejected in
|
|
323
|
+
* that mode: the project credential is only ever set from `apiKey`.
|
|
324
|
+
*
|
|
325
|
+
* A stored `uak_` user key is never used as a project key: when it is the only
|
|
326
|
+
* candidate the resolver throws a {@link ComposioAPIKeyKindError} that names the
|
|
327
|
+
* problem without echoing the key.
|
|
328
|
+
*/
|
|
329
|
+
export function getSDKConfig(
|
|
330
|
+
baseUrl?: string | null,
|
|
331
|
+
apiKey?: string | null,
|
|
332
|
+
options: SDKConfigOptions = {}
|
|
333
|
+
): SDKConfig {
|
|
334
|
+
let userData: UserDataFileRead | undefined;
|
|
335
|
+
const readUserData = () => {
|
|
336
|
+
userData ??= readUserDataFile();
|
|
337
|
+
return userData;
|
|
338
|
+
};
|
|
339
|
+
const userDataValue = (field: 'api_key' | 'base_url'): string | undefined => {
|
|
340
|
+
const read = readUserData();
|
|
341
|
+
return read.status === 'ok' && hasValue(read.data[field]) ? read.data[field] : undefined;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const resolveApiKey = (): string | null => {
|
|
345
|
+
if (apiKey === null) {
|
|
346
|
+
const projectKeyHeader = findHeader(options.defaultHeaders, PROJECT_API_KEY_HEADER);
|
|
347
|
+
if (projectKeyHeader) {
|
|
348
|
+
throw new ComposioNoAPIKeyError(
|
|
349
|
+
`Project API key resolution is disabled, but \`defaultHeaders\` carries a \`${projectKeyHeader.name}\` entry`,
|
|
350
|
+
{
|
|
351
|
+
cause:
|
|
352
|
+
'`apiKey: null` sends no project key, so a raw x-api-key default header would bypass that choice; the SDK keeps a single source for the project credential',
|
|
353
|
+
possibleFixes: [
|
|
354
|
+
`Pass the project API key via \`apiKey\` instead of a raw \`${PROJECT_API_KEY_HEADER}\` default header`,
|
|
355
|
+
`Remove the \`${projectKeyHeader.name}\` entry from \`defaultHeaders\` to authenticate with the user or organization API key alone`,
|
|
356
|
+
],
|
|
357
|
+
}
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
if (hasAlternateCredential(options)) {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
throw new ComposioNoAPIKeyError('Project API key resolution is disabled', {
|
|
364
|
+
cause:
|
|
365
|
+
'`apiKey: null` turns off the COMPOSIO_API_KEY and user config file fallbacks, and no user or organization API key was configured',
|
|
366
|
+
possibleFixes: [
|
|
367
|
+
'Pass a project API key via `apiKey`, or omit it to fall back to COMPOSIO_API_KEY',
|
|
368
|
+
'To authenticate with a user API key instead, keep `apiKey: null` and pass `userApiKey` (or set COMPOSIO_USER_API_KEY)',
|
|
369
|
+
`Alternatively, keep \`apiKey: null\` and set the \`${USER_API_KEY_HEADER}\` default header`,
|
|
370
|
+
],
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (hasValue(apiKey)) {
|
|
375
|
+
return apiKey;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const envApiKey = getEnvVariable('COMPOSIO_API_KEY');
|
|
379
|
+
if (hasValue(envApiKey)) {
|
|
380
|
+
return envApiKey;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const read = readUserData();
|
|
384
|
+
const storedApiKey = userDataValue('api_key');
|
|
385
|
+
if (read.status === 'ok' && hasValue(storedApiKey)) {
|
|
386
|
+
if (storedApiKey.startsWith(USER_API_KEY_PREFIX)) {
|
|
387
|
+
throw new ComposioAPIKeyKindError(undefined, {
|
|
388
|
+
cause: `The user config file at ${read.path} holds a user API key written by the Composio CLI; it cannot authenticate SDK requests as a project`,
|
|
389
|
+
meta: { userDataFilePath: read.path },
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
return storedApiKey;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (read.status === 'malformed') {
|
|
396
|
+
throw new ComposioNoAPIKeyError('No Composio API key provided', {
|
|
397
|
+
cause: `No API key was passed in the params or in COMPOSIO_API_KEY, and the user config file at ${read.path} ${read.reason}`,
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
throw new ComposioNoAPIKeyError();
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
let apiKeyParsed: string | null;
|
|
404
|
+
try {
|
|
405
|
+
apiKeyParsed = resolveApiKey();
|
|
406
|
+
} catch (error) {
|
|
407
|
+
ComposioError.handleAndThrow(error);
|
|
54
408
|
}
|
|
55
409
|
|
|
56
|
-
|
|
410
|
+
const baseURLParsed =
|
|
411
|
+
(hasValue(baseUrl) ? baseUrl : undefined) ??
|
|
412
|
+
getEnvVariable('COMPOSIO_BASE_URL') ??
|
|
413
|
+
userDataValue('base_url') ??
|
|
414
|
+
DEFAULT_BASE_URL;
|
|
415
|
+
|
|
416
|
+
logger.debug(
|
|
417
|
+
'Environment',
|
|
418
|
+
apiKeyParsed === null ? 'API Key: [DISABLED]' : 'API Key: [REDACTED]'
|
|
419
|
+
);
|
|
57
420
|
logger.debug('Environment', `Base URL: ${baseURLParsed}`);
|
|
58
421
|
|
|
59
422
|
return { baseURL: baseURLParsed, apiKey: apiKeyParsed };
|
|
@@ -1,20 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ConnectedAccountListResponse as RawConnectedAccountListResponse,
|
|
3
3
|
ConnectedAccountRetrieveResponse as RawConnectedAccountRetrieveResponse,
|
|
4
|
+
type ConnectedAccounts as ClientConnectedAccounts,
|
|
4
5
|
} from '@composio/client/resources/connected-accounts';
|
|
5
6
|
import {
|
|
6
7
|
ConnectedAccountListResponse,
|
|
7
8
|
ConnectedAccountListResponseSchema,
|
|
8
9
|
ConnectedAccountRetrieveResponse,
|
|
9
10
|
ConnectedAccountRetrieveResponseSchema,
|
|
11
|
+
ConnectedAccountRevokeResponse,
|
|
12
|
+
ConnectedAccountRevokeResponseSchema,
|
|
13
|
+
ConnectedAccountCompleteAuthResponse,
|
|
14
|
+
ConnectedAccountCompleteAuthResponseSchema,
|
|
10
15
|
} from '../../types/connectedAccounts.types';
|
|
16
|
+
|
|
17
|
+
// The revoke and complete-auth response types are only reachable through the
|
|
18
|
+
// resource class namespace on the published subpath.
|
|
19
|
+
type RawConnectedAccountRevokeResponse = ClientConnectedAccounts.ConnectedAccountRevokeResponse;
|
|
20
|
+
type RawConnectedAccountCompleteAuthResponse =
|
|
21
|
+
ClientConnectedAccounts.ConnectedAccountCompleteAuthResponse;
|
|
11
22
|
import { ConnectionDataSchema } from '../../types/connectedAccountAuthStates.types';
|
|
12
23
|
import logger from '../logger';
|
|
13
24
|
import { transform } from '../transform';
|
|
14
25
|
|
|
15
26
|
type RawConnectedAccountResponseWithLabels = (
|
|
16
|
-
|
|
|
17
|
-
| RawConnectedAccountListResponse['items'][0]
|
|
27
|
+
RawConnectedAccountRetrieveResponse | RawConnectedAccountListResponse['items'][0]
|
|
18
28
|
) & {
|
|
19
29
|
word_id?: string | null;
|
|
20
30
|
alias?: string | null;
|
|
@@ -117,3 +127,28 @@ export function transformConnectedAccountListResponse(
|
|
|
117
127
|
totalPages: response.total_pages,
|
|
118
128
|
}));
|
|
119
129
|
}
|
|
130
|
+
|
|
131
|
+
export const transformConnectedAccountRevokeResponse = (
|
|
132
|
+
response: RawConnectedAccountRevokeResponse
|
|
133
|
+
): ConnectedAccountRevokeResponse => {
|
|
134
|
+
return transform(response)
|
|
135
|
+
.with(ConnectedAccountRevokeResponseSchema)
|
|
136
|
+
.using(response => ({
|
|
137
|
+
revokedTokens: response.revoked_tokens,
|
|
138
|
+
connectedAccount: {
|
|
139
|
+
id: response.connected_account.id,
|
|
140
|
+
status: response.connected_account.status,
|
|
141
|
+
},
|
|
142
|
+
}));
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const transformConnectedAccountCompleteAuthResponse = (
|
|
146
|
+
response: RawConnectedAccountCompleteAuthResponse
|
|
147
|
+
): ConnectedAccountCompleteAuthResponse => {
|
|
148
|
+
return transform(response)
|
|
149
|
+
.with(ConnectedAccountCompleteAuthResponseSchema)
|
|
150
|
+
.using(response => ({
|
|
151
|
+
connectedAccountId: response.connected_account_id,
|
|
152
|
+
toolkitSlug: response.toolkit_slug,
|
|
153
|
+
}));
|
|
154
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CustomDeleteToolkitResponse as RawCustomDeleteToolkitResponse,
|
|
3
|
+
CustomSyncParams as RawCustomSyncParams,
|
|
4
|
+
CustomSyncResponse as RawCustomSyncResponse,
|
|
5
|
+
CustomUpsertParams as RawCustomUpsertParams,
|
|
6
|
+
CustomUpsertResponse as RawCustomUpsertResponse,
|
|
7
|
+
} from '@composio/client/resources/custom';
|
|
8
|
+
import {
|
|
9
|
+
CustomToolkitAuthScheme,
|
|
10
|
+
CustomToolkitDeleteResponse,
|
|
11
|
+
CustomToolkitDeleteResponseSchema,
|
|
12
|
+
CustomToolkitSyncParams,
|
|
13
|
+
CustomToolkitSyncResponse,
|
|
14
|
+
CustomToolkitSyncResponseSchema,
|
|
15
|
+
CustomToolkitUpsertParams,
|
|
16
|
+
CustomToolkitUpsertResponse,
|
|
17
|
+
CustomToolkitUpsertResponseSchema,
|
|
18
|
+
} from '../../types/customToolkits.types';
|
|
19
|
+
import { transform } from '../transform';
|
|
20
|
+
|
|
21
|
+
type RawCustomToolkitAuthScheme = RawCustomUpsertParams['toolkit_config']['auth_schemes'][number];
|
|
22
|
+
|
|
23
|
+
const transformCustomToolkitAuthScheme = (
|
|
24
|
+
scheme: CustomToolkitAuthScheme
|
|
25
|
+
): RawCustomToolkitAuthScheme => {
|
|
26
|
+
switch (scheme.mode) {
|
|
27
|
+
case 'NO_AUTH':
|
|
28
|
+
return { mode: 'NO_AUTH' };
|
|
29
|
+
case 'API_KEY':
|
|
30
|
+
return {
|
|
31
|
+
mode: 'API_KEY',
|
|
32
|
+
headers: scheme.headers,
|
|
33
|
+
api_key_field: scheme.apiKeyField && {
|
|
34
|
+
display_name: scheme.apiKeyField.displayName,
|
|
35
|
+
description: scheme.apiKeyField.description,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
case 'DCR_OAUTH':
|
|
39
|
+
return { mode: 'DCR_OAUTH', discovery_url: scheme.discoveryUrl };
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const transformCustomToolkitUpsertParams = (
|
|
44
|
+
params: CustomToolkitUpsertParams
|
|
45
|
+
): RawCustomUpsertParams => ({
|
|
46
|
+
slug: params.slug,
|
|
47
|
+
toolkit_config: {
|
|
48
|
+
name: params.toolkitConfig.name,
|
|
49
|
+
app_url: params.toolkitConfig.appUrl,
|
|
50
|
+
logo_file: params.toolkitConfig.logoFile && {
|
|
51
|
+
content: params.toolkitConfig.logoFile.content,
|
|
52
|
+
mime_type: params.toolkitConfig.logoFile.mimeType,
|
|
53
|
+
},
|
|
54
|
+
auth_schemes: params.toolkitConfig.authSchemes.map(transformCustomToolkitAuthScheme),
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export const transformCustomToolkitUpsertResponse = (
|
|
59
|
+
response: RawCustomUpsertResponse
|
|
60
|
+
): CustomToolkitUpsertResponse => {
|
|
61
|
+
return transform(response)
|
|
62
|
+
.with(CustomToolkitUpsertResponseSchema)
|
|
63
|
+
.using(response => ({ slug: response.slug }));
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const transformCustomToolkitSyncParams = (
|
|
67
|
+
slug: string,
|
|
68
|
+
params: CustomToolkitSyncParams
|
|
69
|
+
): RawCustomSyncParams => ({
|
|
70
|
+
slug,
|
|
71
|
+
connected_account_id: params.connectedAccountId,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export const transformCustomToolkitSyncResponse = (
|
|
75
|
+
response: RawCustomSyncResponse
|
|
76
|
+
): CustomToolkitSyncResponse => {
|
|
77
|
+
return transform(response)
|
|
78
|
+
.with(CustomToolkitSyncResponseSchema)
|
|
79
|
+
.using(response => ({
|
|
80
|
+
slug: response.slug,
|
|
81
|
+
version: response.version,
|
|
82
|
+
syncedCount: response.synced_count,
|
|
83
|
+
}));
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const transformCustomToolkitDeleteResponse = (
|
|
87
|
+
response: RawCustomDeleteToolkitResponse
|
|
88
|
+
): CustomToolkitDeleteResponse => {
|
|
89
|
+
return transform(response)
|
|
90
|
+
.with(CustomToolkitDeleteResponseSchema)
|
|
91
|
+
.using(response => ({
|
|
92
|
+
slug: response.slug,
|
|
93
|
+
deleted: response.deleted,
|
|
94
|
+
revokeJobIds: response.revoke_job_ids,
|
|
95
|
+
authConfigsDeleted: response.auth_configs_deleted,
|
|
96
|
+
connectedAccountsDeleted: response.connected_accounts_deleted,
|
|
97
|
+
}));
|
|
98
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { KeyringListTransferKeysResponse as RawKeyringListTransferKeysResponse } from '@composio/client/resources/keyring';
|
|
2
|
+
import {
|
|
3
|
+
KeyringTransferKeysResponse,
|
|
4
|
+
KeyringTransferKeysResponseSchema,
|
|
5
|
+
} from '../../types/keyring.types';
|
|
6
|
+
import { transform } from '../transform';
|
|
7
|
+
|
|
8
|
+
export const transformKeyringTransferKeysResponse = (
|
|
9
|
+
response: RawKeyringListTransferKeysResponse
|
|
10
|
+
): KeyringTransferKeysResponse => {
|
|
11
|
+
return transform(response)
|
|
12
|
+
.with(KeyringTransferKeysResponseSchema)
|
|
13
|
+
.using(response => ({
|
|
14
|
+
activeKid: response.active_kid,
|
|
15
|
+
keys: response.keys.map(key => ({
|
|
16
|
+
kty: key.kty,
|
|
17
|
+
n: key.n,
|
|
18
|
+
e: key.e,
|
|
19
|
+
alg: key.alg,
|
|
20
|
+
use: key.use,
|
|
21
|
+
kid: key.kid,
|
|
22
|
+
})),
|
|
23
|
+
}));
|
|
24
|
+
};
|