@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 160 lines 5.67 KB
Raw
1 import * as FileSystem from 'effect/FileSystem';
2 import * as Path from 'effect/Path';
3 import { HttpClient } from 'effect/http';
4 import { Effect, Context, Layer } from 'effect';
5 import {
6 inferSkillReleaseChannel,
7 installSkill,
8 resolveInstalledSkillName,
9 resolveSkillReleaseTag,
10 resolveTargetSkillPath,
11 SKILL_RELEASE_TAG_FILENAME,
12 } from 'src/effects/install-skill';
13 import { GITHUB_CONFIG } from 'src/config';
14 import { APP_VERSION } from 'src/constants';
15 import { resolveRunningCliReleaseTag } from 'src/services/run-companion-modules';
16 import { NodeOs } from './node-os';
17
18 export const resolveSetupSkillReleaseTag = (
19 execPath = process.execPath,
20 fallbackVersion = APP_VERSION
21 ) =>
22 Effect.gen(function* () {
23 const httpClient = yield* HttpClient.HttpClient;
24 const githubConfig = yield* GITHUB_CONFIG;
25 const installedReleaseTag = yield* resolveRunningCliReleaseTag(execPath, fallbackVersion);
26
27 return yield* resolveSkillReleaseTag({
28 channel: inferSkillReleaseChannel(fallbackVersion),
29 githubConfig,
30 httpClient,
31 installedReleaseTag,
32 });
33 });
34
35 const checkClaudeSkillCurrent = (
36 fs: FileSystem.FileSystem,
37 path: Path.Path,
38 home: string,
39 releaseTag: string
40 ) =>
41 Effect.gen(function* () {
42 const skillName = resolveInstalledSkillName();
43 const target = resolveTargetSkillPath({ home, path, skillName, target: 'claude' });
44 const installedReleaseTag = yield* fs
45 .readFileString(path.join(target, SKILL_RELEASE_TAG_FILENAME), 'utf8')
46 .pipe(
47 Effect.map(value => value.trim()),
48 Effect.catch(() => Effect.succeed(undefined))
49 );
50 if (installedReleaseTag !== releaseTag) return false;
51 return yield* fs.readFileString(path.join(target, 'SKILL.md'), 'utf8').pipe(
52 Effect.as(true),
53 Effect.catch(() => Effect.succeed(false))
54 );
55 });
56
57 export const isClaudeSkillCurrent = (home: string, releaseTag: string) =>
58 Effect.gen(function* () {
59 const fs = yield* FileSystem.FileSystem;
60 const path = yield* Path.Path;
61 return yield* checkClaudeSkillCurrent(fs, path, home, releaseTag);
62 });
63
64 const isLinkedTo = (fs: FileSystem.FileSystem, path: Path.Path, source: string, target: string) =>
65 fs.readLink(source).pipe(
66 Effect.map(link => path.resolve(path.dirname(source), link) === target),
67 Effect.catch(() => Effect.succeed(false))
68 );
69
70 export const hasManagedClaudeSkill = (home: string) =>
71 Effect.gen(function* () {
72 const fs = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const skillName = resolveInstalledSkillName();
75 const canonicalSkill = path.join(home, '.agents', 'skills', skillName);
76 const claudeSkill = resolveTargetSkillPath({ home, path, skillName, target: 'claude' });
77
78 if (yield* isLinkedTo(fs, path, claudeSkill, canonicalSkill)) return true;
79 const isManaged = yield* fs.exists(path.join(canonicalSkill, SKILL_RELEASE_TAG_FILENAME));
80 if (!isManaged) return false;
81
82 const otherTargets = (['codex', 'openclaw'] as const).map(target =>
83 resolveTargetSkillPath({ home, path, skillName, target })
84 );
85 const references = yield* Effect.all(
86 otherTargets.map(target => isLinkedTo(fs, path, target, canonicalSkill))
87 );
88 return !references.some(Boolean);
89 });
90
91 export const removeManagedClaudeSkill = (home: string) =>
92 Effect.gen(function* () {
93 const fs = yield* FileSystem.FileSystem;
94 const path = yield* Path.Path;
95 const skillName = resolveInstalledSkillName();
96 const canonicalSkill = path.join(home, '.agents', 'skills', skillName);
97 const claudeSkill = resolveTargetSkillPath({ home, path, skillName, target: 'claude' });
98
99 let changed = false;
100 if (yield* isLinkedTo(fs, path, claudeSkill, canonicalSkill)) {
101 yield* fs.remove(claudeSkill, { recursive: true, force: true });
102 changed = true;
103 }
104
105 const isManaged = yield* fs.exists(path.join(canonicalSkill, SKILL_RELEASE_TAG_FILENAME));
106 if (!isManaged) return changed;
107
108 const otherTargets = (['codex', 'openclaw'] as const).map(target =>
109 resolveTargetSkillPath({ home, path, skillName, target })
110 );
111 const references = yield* Effect.all(
112 otherTargets.map(target => isLinkedTo(fs, path, target, canonicalSkill))
113 );
114 const stillReferenced = references.some(Boolean);
115 if (!stillReferenced) {
116 yield* fs.remove(canonicalSkill, { recursive: true, force: true });
117 changed = true;
118 }
119 return changed;
120 }).pipe(Effect.uninterruptible);
121
122 const makeSetupSkillInstaller = Effect.gen(function* () {
123 const fs = yield* FileSystem.FileSystem;
124 const path = yield* Path.Path;
125 const os = yield* NodeOs;
126 const isCurrent = (releaseTag: string) =>
127 checkClaudeSkillCurrent(fs, path, os.homedir, releaseTag);
128 const releaseTag = yield* Effect.cached(
129 resolveSetupSkillReleaseTag().pipe(
130 Effect.provideService(FileSystem.FileSystem, fs),
131 Effect.provideService(Path.Path, path)
132 )
133 );
134
135 return {
136 isClaudeSkillReady: Effect.flatMap(releaseTag, isCurrent),
137 hasManagedClaudeSkill: hasManagedClaudeSkill(os.homedir),
138 ensureClaudeSkill: Effect.gen(function* () {
139 const targetReleaseTag = yield* releaseTag;
140 if (yield* isCurrent(targetReleaseTag)) return false;
141
142 yield* installSkill({
143 target: 'claude',
144 releaseTag: targetReleaseTag,
145 silent: true,
146 });
147 return true;
148 }),
149 removeClaudeSkill: removeManagedClaudeSkill(os.homedir),
150 };
151 });
152
153 export type SetupSkillInstallerShape = Effect.Success<typeof makeSetupSkillInstaller>;
154
155 export class SetupSkillInstaller extends Context.Service<
156 SetupSkillInstaller,
157 SetupSkillInstallerShape
158 >()('services/SetupSkillInstaller') {
159 static readonly Default = Layer.effect(SetupSkillInstaller, makeSetupSkillInstaller);
160 }