@devries-cameron20 / composio Mirror · read-only
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, 04:45 UTC

next
ts 187 lines 5.98 KB
Raw
1 import { Data, Effect, Option, Schema } from 'effect';
2 import { HttpClient, HttpClientResponse } from 'effect/http';
3 import { semverComparator } from 'src/effects/compare-semver';
4 import type { CliReleaseChannel } from 'src/constants';
5 import type { GitHubConfig } from 'src/config';
6
7 export const GitHubReleaseAsset = Schema.Struct({
8 name: Schema.NonEmptyString,
9 browser_download_url: Schema.NonEmptyString,
10 // Present on every real GitHub asset; optional so a release payload that omits
11 // it still decodes and the download simply reports progress without a total.
12 size: Schema.optional(Schema.Number),
13 });
14 export type GitHubReleaseAsset = typeof GitHubReleaseAsset.Type;
15 export const GitHubReleaseAssets = Schema.Array(GitHubReleaseAsset);
16
17 export const GitHubRelease = Schema.Struct({
18 tag_name: Schema.NonEmptyString,
19 prerelease: Schema.optional(Schema.Boolean),
20 draft: Schema.optional(Schema.Boolean),
21 assets: GitHubReleaseAssets,
22 });
23 export type GitHubRelease = typeof GitHubRelease.Type;
24
25 // Decoded per element: the endpoint returns up to 100 releases spanning the
26 // whole repo, and one deviant entry (a draft mid-upload with a null asset
27 // URL) must not fail resolution for the well-formed CLI releases.
28 const RawGitHubReleases = Schema.Array(Schema.Unknown);
29 const decodeGitHubRelease = Schema.decodeUnknownOption(GitHubRelease);
30
31 export class CliReleaseResolutionError extends Data.TaggedError(
32 'effects/CliReleaseResolutionError'
33 )<{
34 readonly cause?: unknown;
35 readonly message: string;
36 readonly reason: 'request' | 'http-status' | 'decode' | 'not-found' | 'compare';
37 readonly status?: number;
38 }> {}
39
40 export type GitHubRepoConfig = Pick<GitHubConfig, 'apiBaseUrl' | 'owner' | 'repo'>;
41
42 export const CLI_RELEASE_TAG_PATTERN = /^@composio\/cli@\d+\.\d+\.\d+.*$/;
43
44 const GitHubReleaseByTag = Schema.Struct({ assets: GitHubReleaseAssets });
45
46 export const fetchCliReleaseByTag = ({
47 githubConfig,
48 httpClient,
49 tag,
50 }: {
51 githubConfig: GitHubRepoConfig;
52 httpClient: HttpClient.HttpClient;
53 tag: string;
54 }) =>
55 Effect.gen(function* () {
56 const releaseUrl = `${githubConfig.apiBaseUrl}/repos/${githubConfig.owner}/${githubConfig.repo}/releases/tags/${encodeURIComponent(tag)}`;
57 const releaseResponse = yield* httpClient.get(releaseUrl).pipe(
58 Effect.mapError(
59 cause =>
60 new CliReleaseResolutionError({
61 cause,
62 message: `Failed to fetch release ${tag}`,
63 reason: 'request',
64 })
65 )
66 );
67
68 if (releaseResponse.status < 200 || releaseResponse.status >= 300) {
69 return yield* new CliReleaseResolutionError({
70 message: `Release ${tag} not found (HTTP ${releaseResponse.status})`,
71 reason: 'http-status',
72 status: releaseResponse.status,
73 });
74 }
75
76 return yield* HttpClientResponse.schemaBodyJson(GitHubReleaseByTag)(releaseResponse).pipe(
77 Effect.mapError(
78 cause =>
79 new CliReleaseResolutionError({
80 cause,
81 message: `Failed to decode release ${tag}`,
82 reason: 'decode',
83 })
84 )
85 );
86 });
87
88 export const fetchLatestCliRelease = ({
89 assetDescription,
90 channel,
91 githubConfig,
92 hasRequiredAsset,
93 httpClient,
94 }: {
95 assetDescription: string;
96 channel: CliReleaseChannel;
97 githubConfig: GitHubRepoConfig;
98 hasRequiredAsset: (release: GitHubRelease) => boolean;
99 httpClient: HttpClient.HttpClient;
100 }) =>
101 Effect.gen(function* () {
102 const releaseUrl = `${githubConfig.apiBaseUrl}/repos/${githubConfig.owner}/${githubConfig.repo}/releases?per_page=100`;
103 const releaseResponse = yield* httpClient.get(releaseUrl).pipe(
104 Effect.mapError(
105 cause =>
106 new CliReleaseResolutionError({
107 cause,
108 message: `Failed to fetch ${channel} releases from GitHub`,
109 reason: 'request',
110 })
111 )
112 );
113
114 if (releaseResponse.status < 200 || releaseResponse.status >= 300) {
115 return yield* Effect.fail(
116 new CliReleaseResolutionError({
117 message: `Failed to fetch ${channel} releases from GitHub (HTTP ${releaseResponse.status})`,
118 reason: 'http-status',
119 status: releaseResponse.status,
120 })
121 );
122 }
123
124 const rawReleases = yield* HttpClientResponse.schemaBodyJson(RawGitHubReleases)(
125 releaseResponse
126 ).pipe(
127 Effect.mapError(
128 cause =>
129 new CliReleaseResolutionError({
130 cause,
131 message: 'Failed to decode GitHub releases response',
132 reason: 'decode',
133 })
134 )
135 );
136 const releases = rawReleases.flatMap(entry =>
137 Option.match(decodeGitHubRelease(entry), {
138 onNone: () => [],
139 onSome: release => [release],
140 })
141 );
142 if (releases.length < rawReleases.length) {
143 yield* Effect.logDebug(
144 `Skipped ${rawReleases.length - releases.length} malformed GitHub release entries.`
145 );
146 }
147
148 const prerelease = channel === 'beta';
149 const matchingReleases = releases.filter(
150 release =>
151 (release.prerelease === undefined
152 ? prerelease === false
153 : release.prerelease === prerelease) &&
154 release.draft !== true &&
155 CLI_RELEASE_TAG_PATTERN.test(release.tag_name) &&
156 hasRequiredAsset(release)
157 );
158
159 if (matchingReleases.length === 0) {
160 return yield* Effect.fail(
161 new CliReleaseResolutionError({
162 message: `No ${channel} CLI releases found with ${assetDescription}`,
163 reason: 'not-found',
164 })
165 );
166 }
167
168 let latest = matchingReleases[0];
169 for (const release of matchingReleases.slice(1)) {
170 const comparison = yield* semverComparator(latest.tag_name, release.tag_name).pipe(
171 Effect.mapError(
172 cause =>
173 new CliReleaseResolutionError({
174 cause,
175 message: `Failed to compare release versions: ${latest.tag_name} and ${release.tag_name}`,
176 reason: 'compare',
177 })
178 )
179 );
180
181 if (comparison < 0) {
182 latest = release;
183 }
184 }
185
186 return latest;
187 });