1
+import { dirname, resolve } from 'node:path';
2
+import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
3
+import * as BunRuntime from '@effect/platform-bun/BunRuntime';
4
+import { Data, Effect, FileSystem, Schema } from 'effect';
5
+import { compare, valid } from 'semver';
6
+
7
+export const branch = 'chore/update-composio-client';
8
+export const paths = [
9
+ 'pnpm-workspace.yaml',
10
+ 'pnpm-lock.yaml',
11
+ 'python/pyproject.toml',
12
+ 'python/setup.py',
13
+ 'uv.lock',
14
+ '.changeset/composio-client-update.md',
15
+ 'ts/packages/cli/CHANGELOG.md',
16
+] as const;
17
+
18
+// The pattern admits only X.Y.Z; semver round-trips it, so whitespace and
19
+// out-of-range components are rejected too.
20
+export const stableVersion = Schema.String.check(
21
+ Schema.makeFilter(
22
+ value => /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(value) && valid(value) === value,
23
+ { message: 'Expected a canonical stable semver version' }
24
+ )
25
+);
26
+const sha = Schema.String.check(Schema.isPattern(/^[a-f0-9]{40}$/));
27
+const dispatch = Schema.Struct({
28
+ version: stableVersion,
29
+ source_sha: sha,
30
+ source_repository: Schema.Literal('ComposioHQ/composio-client'),
31
+});
32
+const dispatchEvent = Schema.Struct({ client_payload: Schema.Unknown });
33
+const pypiFile = Schema.Struct({ yanked: Schema.Boolean, filename: Schema.String });
34
+const npmIndex = Schema.Struct({ versions: Schema.Record(Schema.String, Schema.Unknown) });
35
+const pypiIndex = Schema.Struct({
36
+ releases: Schema.Record(Schema.String, Schema.Array(pypiFile)),
37
+});
38
+const npmRelease = Schema.Struct({
39
+ name: Schema.Literal('@composio/client'),
40
+ version: stableVersion,
41
+ dist: Schema.Struct({
42
+ integrity: Schema.NonEmptyString,
43
+ tarball: Schema.String.check(Schema.isPattern(/^https:\/\/registry\.npmjs\.org\/\S+$/)),
44
+ }),
45
+});
46
+const pypiRelease = Schema.Struct({
47
+ info: Schema.Struct({ name: Schema.Literal('composio-client'), version: stableVersion }),
48
+ urls: Schema.Array(pypiFile),
49
+});
50
+const artifactSchema = Schema.Struct({
51
+ version: stableVersion,
52
+ base_sha: sha,
53
+ files: Schema.Array(
54
+ Schema.Struct({ path: Schema.Literals(paths), content: Schema.String })
55
+ ).check(Schema.isMinLength(1), Schema.isMaxLength(paths.length)),
56
+});
57
+
58
+const decode = <S extends Schema.Codec<unknown, unknown>>(schema: S) =>
59
+ Schema.decodeUnknownSync(schema);
60
+// Inputs crossing a trust boundary must not carry fields the updater ignores.
61
+const decodeExact = <S extends Schema.Codec<unknown, unknown>>(schema: S) =>
62
+ Schema.decodeUnknownSync(schema, { onExcessProperty: 'error' });
63
+
64
+export const isStableVersion = Schema.is(stableVersion);
65
+
66
+export function validateEvent(name: string, payload: unknown) {
67
+ if (name === 'repository_dispatch') {
68
+ return decodeExact(dispatch)(decode(dispatchEvent)(payload).client_payload);
69
+ }
70
+ if (name !== 'schedule' && name !== 'workflow_dispatch')
71
+ throw new Error(`Unsupported event: ${name}`);
72
+ return undefined;
73
+}
74
+
75
+export function commonStableVersion(npmData: unknown, pypiData: unknown): string {
76
+ const npm = decode(npmIndex)(npmData);
77
+ const pypi = decode(pypiIndex)(pypiData);
78
+ const versions = Object.keys(npm.versions).filter(
79
+ version => isStableVersion(version) && pypi.releases[version]?.some(file => !file.yanked)
80
+ );
81
+ versions.sort(compare);
82
+ const latest = versions.at(-1);
83
+ if (!latest) throw new Error('npm and PyPI have no common stable composio-client release');
84
+ return latest;
85
+}
86
+
87
+export function verifyReleases(version: string, npmData: unknown, pypiData: unknown) {
88
+ const npm = decode(npmRelease)(npmData);
89
+ const pypi = decode(pypiRelease)(pypiData);
90
+ if (
91
+ npm.version !== version ||
92
+ pypi.info.version !== version ||
93
+ !pypi.urls.some(file => !file.yanked && file.filename.endsWith('.whl'))
94
+ ) {
95
+ throw new Error(`Both registries must expose usable composio-client ${version} distributions`);
96
+ }
97
+}
98
+
99
+export const decodeArtifact = decodeExact(artifactSchema);
100
+
101
+const catalogPattern = /^(\s*'@composio\/client':\s*)([^\s#]+)(\s*)$/gm;
102
+const pythonPattern = /("composio-client==)([^"\s]+)(")/g;
103
+
104
+function onePin(content: string, pattern: RegExp, label: string): string {
105
+ const matches = [...content.matchAll(pattern)];
106
+ if (matches.length !== 1) throw new Error(`Expected exactly one ${label} pin`);
107
+ return decode(stableVersion)(matches[0]?.[2]);
108
+}
109
+
110
+function required(files: Record<string, string>, path: string): string {
111
+ const content = files[path];
112
+ if (content === undefined) throw new Error(`Missing ${path}`);
113
+ return content;
114
+}
115
+
116
+export function catalogVersion(content: string) {
117
+ return onePin(content, catalogPattern, 'catalog');
118
+}
119
+
120
+export function planUpdates(
121
+ files: Record<string, string>,
122
+ targetInput: string,
123
+ existingCandidate?: string
124
+) {
125
+ const target = decode(stableVersion)(targetInput);
126
+ const current = [
127
+ catalogVersion(required(files, 'pnpm-workspace.yaml')),
128
+ onePin(required(files, 'python/pyproject.toml'), pythonPattern, 'Python pyproject'),
129
+ onePin(required(files, 'python/setup.py'), pythonPattern, 'Python setup'),
130
+ ];
131
+ if (current[1] !== current[2])
132
+ throw new Error('Python client pins disagree; reconcile metadata first');
133
+ if (
134
+ [...current, ...(existingCandidate ? [decode(stableVersion)(existingCandidate)] : [])].some(
135
+ version => compare(version, target) > 0
136
+ )
137
+ ) {
138
+ throw new Error(`Refusing to downgrade a current or pending client pin to ${target}`);
139
+ }
140
+ const changed: Record<string, string> = {};
141
+ if (current[0] !== target) {
142
+ changed['pnpm-workspace.yaml'] = required(files, 'pnpm-workspace.yaml').replace(
143
+ catalogPattern,
144
+ `$1${target}$3`
145
+ );
146
+ changed['.changeset/composio-client-update.md'] =
147
+ `---\n'@composio/core': patch\n---\n\nUpdate @composio/client to ${target} to include the latest API changes.\n`;
148
+ const changelog = required(files, 'ts/packages/cli/CHANGELOG.md');
149
+ const header = '# @composio/cli\n\n## Unreleased\n';
150
+ const marker = '### Patch Changes\n';
151
+ if (!changelog.startsWith(header)) {
152
+ throw new Error('CLI changelog is missing its Unreleased section');
153
+ }
154
+ const nextRelease = changelog.indexOf('\n## ', header.length);
155
+ const end = nextRelease === -1 ? changelog.length : nextRelease;
156
+ const unreleased = changelog.slice(header.length, end);
157
+ const note = `- Update @composio/client to ${target} to include the latest API changes.\n`;
158
+ const updated = unreleased.includes(marker)
159
+ ? unreleased.replace(marker, `${marker}\n${note}`)
160
+ : `\n${marker}\n${note}${unreleased}`;
161
+ changed['ts/packages/cli/CHANGELOG.md'] = `${header}${updated}${changelog.slice(end)}`;
162
+ }
163
+ if (current[1] !== target) {
164
+ for (const path of ['python/pyproject.toml', 'python/setup.py']) {
165
+ changed[path] = required(files, path).replace(pythonPattern, `$1${target}$3`);
166
+ }
167
+ }
168
+ return { changed, typescript: current[0] !== target, python: current[1] !== target };
169
+}
170
+
171
+export class ClientUpdateIoError extends Data.TaggedError('ClientUpdateIoError')<{
172
+ readonly operation: string;
173
+ readonly path: string;
174
+ readonly message: string;
175
+ readonly cause: unknown;
176
+}> {}
177
+export class ClientUpdateRegistryError extends Data.TaggedError('ClientUpdateRegistryError')<{
178
+ readonly url: string;
179
+ readonly message: string;
180
+ readonly cause?: unknown;
181
+}> {}
182
+export class ClientUpdateCommandError extends Data.TaggedError('ClientUpdateCommandError')<{
183
+ readonly command: readonly string[];
184
+ readonly message: string;
185
+ readonly cause?: unknown;
186
+}> {}
187
+export class ClientUpdateValidationError extends Data.TaggedError('ClientUpdateValidationError')<{
188
+ readonly message: string;
189
+ readonly cause?: unknown;
190
+}> {}
191
+type ClientUpdateError =
192
+ | ClientUpdateIoError
193
+ | ClientUpdateRegistryError
194
+ | ClientUpdateCommandError
195
+ | ClientUpdateValidationError;
196
+
197
+const checked = <A>(operation: () => A) =>
198
+ Effect.try({
199
+ try: operation,
200
+ catch: cause =>
201
+ new ClientUpdateValidationError({
202
+ message: cause instanceof Error ? cause.message : 'Client update validation failed',
203
+ cause,
204
+ }),
205
+ });
206
+const file = <A, E>(
207
+ operation: string,
208
+ path: string,
209
+ action: (fs: FileSystem.FileSystem) => Effect.Effect<A, E>
210
+) =>
211
+ Effect.gen(function* () {
212
+ return yield* action(yield* FileSystem.FileSystem);
213
+ }).pipe(
214
+ Effect.mapError(
215
+ cause =>
216
+ new ClientUpdateIoError({ operation, path, message: `Cannot ${operation}: ${path}`, cause })
217
+ )
218
+ );
219
+const read = (path: string) => file('read file', path, fs => fs.readFileString(path));
220
+const write = (path: string, content: string) =>
221
+ file('write file', path, fs => fs.writeFileString(path, content));
222
+const directory = (path: string) =>
223
+ file('create directory', path, fs => fs.makeDirectory(path, { recursive: true }));
224
+
225
+const registryJson = (url: string) =>
226
+ Effect.gen(function* () {
227
+ const response = yield* Effect.tryPromise({
228
+ try: signal =>
229
+ fetch(url, {
230
+ signal: AbortSignal.any([signal, AbortSignal.timeout(60_000)]),
231
+ headers: { accept: 'application/json' },
232
+ redirect: 'error',
233
+ }),
234
+ catch: cause =>
235
+ new ClientUpdateRegistryError({ url, message: `Registry request failed: ${url}`, cause }),
236
+ });
237
+ if (!response.ok)
238
+ return yield* Effect.fail(
239
+ new ClientUpdateRegistryError({
240
+ url,
241
+ message: `Registry request failed: ${url} (${response.status})`,
242
+ })
243
+ );
244
+ return yield* Effect.tryPromise({
245
+ try: () => response.json(),
246
+ catch: cause =>
247
+ new ClientUpdateRegistryError({ url, message: `Invalid registry JSON: ${url}`, cause }),
248
+ });
249
+ });
250
+
251
+const processResult = (args: string[], cwd: string, capture = false) =>
252
+ Effect.acquireUseRelease(
253
+ Effect.try({
254
+ try: () =>
255
+ Bun.spawn(args, {
256
+ cwd,
257
+ stdout: capture ? 'pipe' : 'inherit',
258
+ stderr: capture ? 'ignore' : 'inherit',
259
+ }),
260
+ catch: cause =>
261
+ new ClientUpdateCommandError({
262
+ command: args,
263
+ message: `Cannot start command: ${args.join(' ')}`,
264
+ cause,
265
+ }),
266
+ }),
267
+ child =>
268
+ Effect.gen(function* () {
269
+ const [stdout, exitCode] = yield* Effect.all(
270
+ [
271
+ capture
272
+ ? Effect.tryPromise({
273
+ try: () => new Response(child.stdout).text(),
274
+ catch: cause =>
275
+ new ClientUpdateCommandError({
276
+ command: args,
277
+ message: `Cannot read command output: ${args.join(' ')}`,
278
+ cause,
279
+ }),
280
+ })
281
+ : Effect.succeed(''),
282
+ Effect.tryPromise({
283
+ try: () => child.exited,
284
+ catch: cause =>
285
+ new ClientUpdateCommandError({
286
+ command: args,
287
+ message: `Cannot await command: ${args.join(' ')}`,
288
+ cause,
289
+ }),
290
+ }),
291
+ ],
292
+ { concurrency: 'unbounded' }
293
+ );
294
+ return { stdout, exitCode };
295
+ }),
296
+ child =>
297
+ Effect.try({
298
+ try: () => {
299
+ if (child.exitCode === null) child.kill();
300
+ },
301
+ catch: cause =>
302
+ new ClientUpdateCommandError({
303
+ command: args,
304
+ message: 'Cannot stop interrupted command',
305
+ cause,
306
+ }),
307
+ }).pipe(Effect.ignore)
308
+ );
309
+const command = (args: string[], cwd: string) =>
310
+ Effect.gen(function* () {
311
+ const result = yield* processResult(args, cwd);
312
+ if (result.exitCode !== 0)
313
+ return yield* Effect.fail(
314
+ new ClientUpdateCommandError({
315
+ command: args,
316
+ message: `Command failed: ${args.join(' ')}`,
317
+ })
318
+ );
319
+ });
320
+const git = (args: string[], root: string, allowFailure = false) =>
321
+ Effect.gen(function* () {
322
+ const result = yield* processResult(['git', ...args], root, true);
323
+ if (!allowFailure && result.exitCode !== 0)
324
+ return yield* Effect.fail(
325
+ new ClientUpdateCommandError({
326
+ command: ['git', ...args],
327
+ message: `Git command failed: ${args[0]}`,
328
+ })
329
+ );
330
+ return result;
331
+ });
332
+const output = (name: string, value: string) =>
333
+ Effect.gen(function* () {
334
+ const path = process.env.GITHUB_OUTPUT;
335
+ if (path)
336
+ yield* file('append workflow output', path, fs =>
337
+ fs.writeFileString(path, `${name}=${value}\n`, { flag: 'a' })
338
+ );
339
+ });
340
+
341
+export const prepare = (
342
+ root: string,
343
+ options: {
344
+ eventName: string;
345
+ event: unknown;
346
+ registry?: (url: string) => Effect.Effect<unknown, ClientUpdateError>;
347
+ runCommand?: (
348
+ args: string[],
349
+ cwd: string
350
+ ) => Effect.Effect<void, ClientUpdateError, FileSystem.FileSystem>;
351
+ setOutput?: (
352
+ name: string,
353
+ value: string
354
+ ) => Effect.Effect<void, ClientUpdateError, FileSystem.FileSystem>;
355
+ }
356
+) =>
357
+ Effect.gen(function* () {
358
+ const readRegistry = options.registry ?? registryJson;
359
+ const runCommand = options.runCommand ?? command;
360
+ const setOutput = options.setOutput ?? output;
361
+ const source = yield* checked(() => validateEvent(options.eventName, options.event));
362
+ const artifactPath = resolve(root, '.artifacts/client-update/update.json');
363
+ yield* file('remove stale artifact', artifactPath, fs =>
364
+ fs.remove(artifactPath, { force: true })
365
+ );
366
+ const [npm, pypi] = yield* Effect.all(
367
+ [
368
+ readRegistry('https://registry.npmjs.org/@composio%2fclient'),
369
+ readRegistry('https://pypi.org/pypi/composio-client/json'),
370
+ ],
371
+ { concurrency: 'unbounded' }
372
+ );
373
+ const version = yield* checked(() => commonStableVersion(npm, pypi));
374
+ // A dispatch may race the registry CDN. Retry next hour rather than silently acknowledging an older release.
375
+ if (source && compare(version, source.version) < 0)
376
+ return yield* Effect.fail(
377
+ new ClientUpdateValidationError({
378
+ message: `Dispatched ${source.version} is not yet available on both registries`,
379
+ })
380
+ );
381
+ const [npmExact, pypiExact] = yield* Effect.all(
382
+ [
383
+ readRegistry(`https://registry.npmjs.org/@composio%2fclient/${version}`),
384
+ readRegistry(`https://pypi.org/pypi/composio-client/${version}/json`),
385
+ ],
386
+ { concurrency: 'unbounded' }
387
+ );
388
+ yield* checked(() => verifyReleases(version, npmExact, pypiExact));
389
+ const files = Object.fromEntries(
390
+ yield* Effect.all(
391
+ [
392
+ 'pnpm-workspace.yaml',
393
+ 'python/pyproject.toml',
394
+ 'python/setup.py',
395
+ 'ts/packages/cli/CHANGELOG.md',
396
+ ].map(path =>
397
+ Effect.gen(function* () {
398
+ return [path, yield* read(resolve(root, path))];
399
+ })
400
+ ),
401
+ { concurrency: 'unbounded' }
402
+ )
403
+ );
404
+ const candidate = yield* git(
405
+ ['show', `refs/remotes/origin/${branch}:pnpm-workspace.yaml`],
406
+ root,
407
+ true
408
+ );
409
+ const existing =
410
+ candidate.exitCode === 0 ? yield* checked(() => catalogVersion(candidate.stdout)) : undefined;
411
+ const plan = yield* checked(() => planUpdates(files, version, existing));
412
+ const changed = Object.keys(plan.changed).length > 0;
413
+ yield* setOutput('changed', String(changed));
414
+ yield* setOutput('version', version);
415
+ if (!changed) {
416
+ yield* Effect.sync(() => console.log(`All client pins already use ${version}`));
417
+ return;
418
+ }
419
+ for (const [path, content] of Object.entries(plan.changed)) {
420
+ yield* directory(dirname(resolve(root, path)));
421
+ yield* write(resolve(root, path), content);
422
+ }
423
+ if (plan.typescript) {
424
+ yield* runCommand(
425
+ ['pnpm', 'install', '--lockfile-only', '--ignore-scripts', '--no-frozen-lockfile'],
426
+ root
427
+ );
428
+ }
429
+ if (plan.python) {
430
+ const python = yield* checked(() => decode(Schema.NonEmptyString)(Bun.which('python')));
431
+ yield* runCommand(
432
+ ['uv', 'lock', '--python', python, '--upgrade-package', 'composio-client'],
433
+ root
434
+ );
435
+ }
436
+ const diff = yield* git(['diff', '--name-only'], root);
437
+ const diffPaths = diff.stdout.trim().split('\n').filter(Boolean);
438
+ if (diffPaths.some(path => !paths.some(allowed => allowed === path))) {
439
+ return yield* Effect.fail(
440
+ new ClientUpdateValidationError({
441
+ message: 'Lock generation changed files outside the client update allowlist',
442
+ })
443
+ );
444
+ }
445
+ const base = yield* git(['rev-parse', 'HEAD'], root);
446
+ const baseSha = yield* checked(() => decode(sha)(base.stdout.trim()));
447
+ const artifactFiles = yield* Effect.all(
448
+ [...new Set([...diffPaths, ...Object.keys(plan.changed)])].map(path =>
449
+ Effect.gen(function* () {
450
+ return { path, content: yield* read(resolve(root, path)) };
451
+ })
452
+ ),
453
+ { concurrency: 'unbounded' }
454
+ );
455
+ const artifact = yield* checked(() =>
456
+ decodeArtifact({
457
+ version,
458
+ base_sha: baseSha,
459
+ files: artifactFiles,
460
+ })
461
+ );
462
+ yield* directory(resolve(root, '.artifacts/client-update'));
463
+ yield* write(artifactPath, JSON.stringify(artifact));
464
+ yield* Effect.sync(() =>
465
+ console.log(`Prepared client ${version}; npm and PyPI exact distributions verified`)
466
+ );
467
+ });
468
+
469
+export const applyArtifact = (root: string, data: unknown, baseSha: string) =>
470
+ Effect.gen(function* () {
471
+ const artifact = yield* checked(() => decodeArtifact(data));
472
+ if (artifact.base_sha !== baseSha)
473
+ return yield* Effect.fail(
474
+ new ClientUpdateValidationError({
475
+ message: 'next advanced after preparation; retry against its current revision',
476
+ })
477
+ );
478
+ if (new Set(artifact.files.map(file => file.path)).size !== artifact.files.length)
479
+ return yield* Effect.fail(
480
+ new ClientUpdateValidationError({ message: 'Duplicate artifact path' })
481
+ );
482
+ for (const file of artifact.files) {
483
+ yield* directory(dirname(resolve(root, file.path)));
484
+ yield* write(resolve(root, file.path), file.content);
485
+ }
486
+ return artifact.version;
487
+ });
488
+
489
+if (import.meta.main) {
490
+ const main = Effect.gen(function* () {
491
+ const root = process.cwd();
492
+ if (process.argv[2] === 'prepare') {
493
+ const eventText = process.env.GITHUB_EVENT_PATH
494
+ ? yield* read(process.env.GITHUB_EVENT_PATH)
495
+ : '{}';
496
+ yield* prepare(root, {
497
+ eventName: process.env.GITHUB_EVENT_NAME ?? 'workflow_dispatch',
498
+ event: yield* checked(() => JSON.parse(eventText)),
499
+ });
500
+ } else if (process.argv[2] === 'apply') {
501
+ const head = yield* git(['rev-parse', 'HEAD'], root);
502
+ const artifactText = yield* read(resolve(root, '.artifacts/client-update/update.json'));
503
+ const version = yield* applyArtifact(
504
+ root,
505
+ yield* checked(() => JSON.parse(artifactText)),
506
+ head.stdout.trim()
507
+ );
508
+ yield* output('version', version);
509
+ } else
510
+ return yield* Effect.fail(
511
+ new ClientUpdateValidationError({
512
+ message: 'Usage: bun scripts/update-composio-client.ts prepare|apply',
513
+ })
514
+ );
515
+ });
516
+ BunRuntime.runMain(
517
+ main.pipe(
518
+ Effect.tapError(error => Effect.sync(() => console.error(error.message))),
519
+ Effect.provide(BunFileSystem.layer)
520
+ ),
521
+ {
522
+ disableErrorReporting: true,
523
+ }
524
+ );
525
+}