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, 03:37 UTC
| 1 | import { Effect, FileSystem, Path, Schema } from 'effect'; |
| 2 | import { Toml } from 'effect/encoding'; |
| 3 | import { ensure, fail, readFile, spawn, withoutReleaseTokens, writeFile } from './runtime'; |
| 4 | |
| 5 | const PyProject = Schema.Struct({ |
| 6 | project: Schema.Struct({ name: Schema.String, version: Schema.String }), |
| 7 | }); |
| 8 | |
| 9 | export interface PythonReleasePackage { |
| 10 | readonly name: string; |
| 11 | readonly version: string; |
| 12 | readonly directory: string; |
| 13 | } |
| 14 | |
| 15 | export interface PythonMetadataOptions { |
| 16 | readonly updateLockfile?: boolean; |
| 17 | } |
| 18 | |
| 19 | const CORE_DIRECTORY = 'python'; |
| 20 | const PROVIDERS_DIRECTORY = 'python/providers'; |
| 21 | const RUNTIME_VERSION_PATH = 'python/composio/__version__.py'; |
| 22 | |
| 23 | /** Each pattern captures `(prefix)(quote)(version)`. */ |
| 24 | export const PROJECT_VERSION = /(^\[project\][\s\S]*?^version\s*=\s*)(["'])([^"']+)\2/m; |
| 25 | export const SETUP_VERSION = /(\bversion\s*=\s*)(["'])([^"']+)\2/; |
| 26 | export const RUNTIME_VERSION = /(^\s*__version__\s*=\s*)(["'])([^"']+)\2/m; |
| 27 | |
| 28 | const mirroredVersion = (contents: string, pattern: RegExp) => contents.match(pattern)?.[3]; |
| 29 | const withVersion = (contents: string, pattern: RegExp, version: string) => |
| 30 | contents.replace( |
| 31 | pattern, |
| 32 | (_match, prefix: string, quote: string) => `${prefix}${quote}${version}${quote}` |
| 33 | ); |
| 34 | |
| 35 | /** Providers mirror their version in setup.py; the core package keeps it in pyproject.toml only. */ |
| 36 | const hasSetupMirror = (directory: string) => directory !== CORE_DIRECTORY; |
| 37 | |
| 38 | const parsePyProject = (contents: string) => |
| 39 | Effect.fromResult(Toml.parse(contents)).pipe( |
| 40 | Effect.flatMap(Schema.decodeUnknownEffect(PyProject)) |
| 41 | ); |
| 42 | |
| 43 | const providerDirectories = (cwd: string) => |
| 44 | Effect.gen(function* () { |
| 45 | const fs = yield* FileSystem.FileSystem; |
| 46 | const path = yield* Path.Path; |
| 47 | const entries = yield* fs |
| 48 | .readDirectory(path.resolve(cwd, PROVIDERS_DIRECTORY)) |
| 49 | .pipe(Effect.catchReason('PlatformError', 'NotFound', () => Effect.succeed([]))); |
| 50 | const candidates = entries |
| 51 | .toSorted((a, b) => a.localeCompare(b)) |
| 52 | .map(entry => `${PROVIDERS_DIRECTORY}/${entry}`); |
| 53 | return yield* Effect.filter(candidates, directory => |
| 54 | Effect.gen(function* () { |
| 55 | const info = yield* fs.stat(path.resolve(cwd, directory)); |
| 56 | return ( |
| 57 | info.type === 'Directory' && |
| 58 | (yield* fs.exists(path.resolve(cwd, directory, 'pyproject.toml'))) |
| 59 | ); |
| 60 | }) |
| 61 | ); |
| 62 | }); |
| 63 | |
| 64 | const readPackage = (cwd: string, directory: string) => |
| 65 | Effect.gen(function* () { |
| 66 | const path = yield* Path.Path; |
| 67 | const project = yield* parsePyProject( |
| 68 | yield* readFile(path.resolve(cwd, directory, 'pyproject.toml')) |
| 69 | ); |
| 70 | if (hasSetupMirror(directory)) { |
| 71 | const setupPath = `${directory}/setup.py`; |
| 72 | const setup = yield* readFile(path.resolve(cwd, setupPath)); |
| 73 | yield* ensure( |
| 74 | mirroredVersion(setup, SETUP_VERSION) === project.project.version, |
| 75 | `Python version mirror mismatch in ${setupPath}` |
| 76 | ); |
| 77 | } |
| 78 | return { |
| 79 | name: project.project.name.toLowerCase().replace(/[-_.]+/g, '-'), |
| 80 | version: project.project.version, |
| 81 | directory, |
| 82 | } satisfies PythonReleasePackage; |
| 83 | }); |
| 84 | |
| 85 | export const discoverPythonPackages = (cwd: string) => |
| 86 | Effect.gen(function* () { |
| 87 | const path = yield* Path.Path; |
| 88 | const directories = [CORE_DIRECTORY, ...(yield* providerDirectories(cwd))]; |
| 89 | const packages = yield* Effect.forEach(directories, directory => readPackage(cwd, directory), { |
| 90 | concurrency: 'unbounded', |
| 91 | }); |
| 92 | const runtime = yield* readFile(path.resolve(cwd, RUNTIME_VERSION_PATH)); |
| 93 | yield* ensure( |
| 94 | mirroredVersion(runtime, RUNTIME_VERSION) === packages[0].version, |
| 95 | `Python version mirror mismatch in ${RUNTIME_VERSION_PATH}` |
| 96 | ); |
| 97 | return packages; |
| 98 | }); |
| 99 | |
| 100 | interface Edit { |
| 101 | readonly path: string; |
| 102 | readonly contents: string; |
| 103 | } |
| 104 | |
| 105 | const editVersion = (absolutePath: string, pattern: RegExp, version: string, missing: string) => |
| 106 | Effect.gen(function* () { |
| 107 | const contents = yield* readFile(absolutePath); |
| 108 | yield* ensure(pattern.test(contents), missing); |
| 109 | const updated = withVersion(contents, pattern, version); |
| 110 | return updated === contents |
| 111 | ? undefined |
| 112 | : ({ path: absolutePath, contents: updated } satisfies Edit); |
| 113 | }); |
| 114 | |
| 115 | const editPyProject = (absolutePath: string, directory: string, version: string) => |
| 116 | Effect.gen(function* () { |
| 117 | const contents = yield* readFile(absolutePath); |
| 118 | const updated = withVersion(contents, PROJECT_VERSION, version); |
| 119 | const project = yield* parsePyProject(updated); |
| 120 | yield* ensure( |
| 121 | project.project.version === version, |
| 122 | `Cannot update Python project version in ${directory}/pyproject.toml` |
| 123 | ); |
| 124 | return updated === contents |
| 125 | ? undefined |
| 126 | : ({ path: absolutePath, contents: updated } satisfies Edit); |
| 127 | }); |
| 128 | |
| 129 | const lockWorkspace = (cwd: string) => |
| 130 | spawn('uv', ['lock'], { cwd, env: withoutReleaseTokens }).pipe( |
| 131 | Effect.mapError(error => error.message), |
| 132 | Effect.flatMap(result => |
| 133 | result.exitCode === 0 ? Effect.void : Effect.fail(result.stderr || 'unknown failure') |
| 134 | ), |
| 135 | Effect.catch(detail => fail(`uv lock failed: ${detail}`)) |
| 136 | ); |
| 137 | |
| 138 | export const synchronizePythonMetadata = ( |
| 139 | cwd: string, |
| 140 | version: string, |
| 141 | options: PythonMetadataOptions = {} |
| 142 | ) => |
| 143 | Effect.gen(function* () { |
| 144 | yield* ensure( |
| 145 | /^\d+\.\d+\.\d+(b\d+)?$/.test(version), |
| 146 | `Python release requires a final or beta PEP 440 version, received ${version}` |
| 147 | ); |
| 148 | const path = yield* Path.Path; |
| 149 | const packages = yield* discoverPythonPackages(cwd); |
| 150 | const edits: Array<Edit | undefined> = []; |
| 151 | for (const pkg of packages) { |
| 152 | edits.push( |
| 153 | yield* editPyProject( |
| 154 | path.resolve(cwd, pkg.directory, 'pyproject.toml'), |
| 155 | pkg.directory, |
| 156 | version |
| 157 | ) |
| 158 | ); |
| 159 | if (hasSetupMirror(pkg.directory)) |
| 160 | edits.push( |
| 161 | yield* editVersion( |
| 162 | path.resolve(cwd, pkg.directory, 'setup.py'), |
| 163 | SETUP_VERSION, |
| 164 | version, |
| 165 | `Cannot find provider setup version in ${pkg.directory}/setup.py` |
| 166 | ) |
| 167 | ); |
| 168 | } |
| 169 | edits.push( |
| 170 | yield* editVersion( |
| 171 | path.resolve(cwd, RUNTIME_VERSION_PATH), |
| 172 | RUNTIME_VERSION, |
| 173 | version, |
| 174 | 'Cannot find Python runtime __version__' |
| 175 | ) |
| 176 | ); |
| 177 | const changed = edits.filter(edit => edit !== undefined); |
| 178 | yield* Effect.forEach(changed, edit => writeFile(edit.path, edit.contents), { discard: true }); |
| 179 | const changedPaths = changed.map(edit => path.relative(cwd, edit.path)); |
| 180 | if (options.updateLockfile === false) return changedPaths; |
| 181 | yield* lockWorkspace(cwd); |
| 182 | return [...changedPaths, 'uv.lock']; |
| 183 | }); |