@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, 03:37 UTC

next
ts 171 lines 6.19 KB
Raw
1 import { Data, Effect, FileSystem, Path, Schema } from 'effect';
2 import { ChildProcess, ChildProcessSpawner } from 'effect/process';
3
4 const Language = Schema.Literals(['typescript', 'python']);
5 type Language = typeof Language.Type;
6
7 const ReleaseDocumentationPlan = Schema.Struct({
8 releases: Schema.Array(
9 Schema.Struct({ name: Schema.String, newVersion: Schema.String, language: Language })
10 ),
11 changesets: Schema.Array(Schema.Struct({ summary: Schema.String })),
12 });
13 export type ReleaseDocumentationPlan = typeof ReleaseDocumentationPlan.Encoded;
14
15 const REQUEST_PATH = /^\/ComposioHQ\/composio\/issues\/\d+$/;
16 const isRequestUrl = (value: string) =>
17 URL.canParse(value) &&
18 new URL(value).origin === 'https://github.com' &&
19 REQUEST_PATH.test(new URL(value).pathname);
20
21 const ReleaseNoteOptions = Schema.Struct({
22 releaseId: Schema.String.check(Schema.isPattern(/^[A-Za-z0-9][A-Za-z0-9_-]*$/)),
23 date: Schema.String.check(Schema.isPattern(/^\d{4}-\d{2}-\d{2}$/)),
24 requestUrl: Schema.String.check(
25 Schema.makeFilter(value =>
26 isRequestUrl(value) ? undefined : 'The request URL must link to a repository issue'
27 )
28 ),
29 });
30 export type ReleaseNoteOptions = typeof ReleaseNoteOptions.Type;
31
32 export class ReleaseNotesError extends Data.TaggedError('ReleaseNotesError')<{
33 readonly message: string;
34 }> {}
35
36 /** How each language appears in the docs version table and how its reference docs are generated. */
37 const LANGUAGE_DOCS: Record<
38 Language,
39 {
40 readonly label: (name: string) => string;
41 readonly reference: {
42 readonly command: string;
43 readonly args: ReadonlyArray<string>;
44 readonly directory: string;
45 };
46 }
47 > = {
48 typescript: {
49 label: name => `TypeScript \`${name}\``,
50 reference: {
51 command: 'pnpm',
52 args: ['--filter', '@composio/core', 'generate:docs'],
53 directory: '.',
54 },
55 },
56 python: {
57 label: () => 'Python `composio`',
58 reference: {
59 command: 'uv',
60 args: [
61 'run',
62 '--frozen',
63 '--only-group',
64 'docs-reference',
65 'python',
66 'scripts/generate-docs.py',
67 ],
68 directory: 'python',
69 },
70 },
71 };
72
73 type Plan = typeof ReleaseDocumentationPlan.Type;
74
75 const targets = (plan: Plan) =>
76 Object.fromEntries(plan.releases.map(release => [release.name, release.newVersion]));
77
78 const versionTable = (plan: Plan) =>
79 [
80 '| SDK | Version |',
81 '| --- | --- |',
82 ...plan.releases.map(
83 release =>
84 `| ${LANGUAGE_DOCS[release.language].label(release.name)} | \`${release.newVersion}\` |`
85 ),
86 ].join('\n');
87
88 const entryPath = (options: ReleaseNoteOptions) => {
89 const [year, month, day] = options.date.split('-');
90 return `docs/content/changelog/${month}-${day}-${year.slice(2)}-sdk-${options.releaseId}.mdx`;
91 };
92
93 // A backtick run that is not escaped, closed by a run of the same length, as CommonMark pairs them.
94 const CODE = /((?<![\\`])(`+)(?!`)[\s\S]*?(?<!`)\2(?!`))/;
95
96 /**
97 * Keeps a contributor's changeset summary as Markdown while disabling MDX: braces and tags
98 * outside code become literal text, and a line cannot start an `import` or `export` statement.
99 */
100 export const literalMdx = (markdown: string) =>
101 markdown
102 .split(CODE)
103 .map((part, index) =>
104 index % 3 === 1
105 ? part
106 : index % 3 === 2
107 ? ''
108 : part
109 .replace(/[{}]/g, brace => `\\${brace}`)
110 .replaceAll('<', '&lt;')
111 .replace(
112 /^(\s*)(import|export)\b/gm,
113 (_match, indent: string, word: string) =>
114 `${indent}&#${word.charCodeAt(0)};${word.slice(1)}`
115 )
116 )
117 .join('');
118
119 const renderEntry = (plan: Plan, options: ReleaseNoteOptions) => {
120 const summaries = [
121 ...new Set(plan.changesets.map(note => literalMdx(note.summary.trim())).filter(Boolean)),
122 ];
123 return `---\ntitle: ${JSON.stringify(`SDK release ${options.releaseId}`)}\ndate: ${JSON.stringify(options.date)}\nsdkRelease: ${JSON.stringify(options.releaseId)}\nsdkReleaseTargets: ${JSON.stringify(targets(plan))}\n---\n\nSee the [release request](${options.requestUrl}) for publication status.\n\n### SDK Versions\n\n${versionTable(plan)}\n\n### Changes\n\n${summaries.join('\n\n')}\n`;
124 };
125
126 /** Writes the release's changelog entry; preparation always starts from a clean checkout. */
127 export const generateSdkReleaseNotes = (
128 cwd: string,
129 value: ReleaseDocumentationPlan,
130 input: ReleaseNoteOptions
131 ) =>
132 Effect.gen(function* () {
133 const fs = yield* FileSystem.FileSystem;
134 const path = yield* Path.Path;
135 const plan = yield* Schema.decodeUnknownEffect(ReleaseDocumentationPlan)(value);
136 const options = yield* Schema.decodeUnknownEffect(ReleaseNoteOptions)(input);
137 if (!plan.releases.length)
138 return yield* new ReleaseNotesError({ message: 'Nothing to document' });
139 const relativePath = entryPath(options);
140 const absolutePath = path.resolve(cwd, relativePath);
141 yield* fs.makeDirectory(path.dirname(absolutePath), { recursive: true });
142 yield* fs.writeFileString(absolutePath, renderEntry(plan, options), { flag: 'wx' });
143 return relativePath;
144 });
145
146 /** Regenerates the symbol reference for each released language, streaming generator output. */
147 export const generateSdkReleaseReferences = (cwd: string, value: ReleaseDocumentationPlan) =>
148 Effect.gen(function* () {
149 const path = yield* Path.Path;
150 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
151 const plan = yield* Schema.decodeUnknownEffect(ReleaseDocumentationPlan)(value);
152 const languages = [...new Set(plan.releases.map(release => release.language))];
153 return yield* Effect.forEach(languages, language =>
154 Effect.gen(function* () {
155 const { command, args, directory } = LANGUAGE_DOCS[language].reference;
156 const exitCode = yield* spawner.exitCode(
157 ChildProcess.make(command, args, {
158 cwd: path.resolve(cwd, directory),
159 stdin: 'inherit',
160 stdout: 'inherit',
161 stderr: 'inherit',
162 })
163 );
164 if (exitCode !== 0)
165 return yield* new ReleaseNotesError({
166 message: `${language} reference generation failed: ${exitCode}`,
167 });
168 return `docs/content/reference/sdk-reference/${language}`;
169 })
170 );
171 });