Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 174 lines 4.25 KB
Raw
1 import {
2 CLI_EXPERIMENTAL_FEATURES,
3 isExperimentalFeatureEnabledByDefault,
4 type CliReleaseChannel,
5 } from '../../src/experimental-features';
6
7 export type SkillReleaseChannel = CliReleaseChannel;
8
9 export type SkillFeatureFlag =
10 (typeof CLI_EXPERIMENTAL_FEATURES)[keyof typeof CLI_EXPERIMENTAL_FEATURES];
11
12 export type SkillBuildContext = {
13 channel: SkillReleaseChannel;
14 experimentalFeatures: Readonly<Record<SkillFeatureFlag, boolean>>;
15 };
16
17 export type FeatureScoped = {
18 features?: SkillFeatureFlag[];
19 };
20
21 export type CommandSnippet = FeatureScoped & {
22 code: string;
23 description?: string;
24 };
25
26 export type ReferenceSection = FeatureScoped & {
27 body?: string[];
28 commands?: CommandSnippet[];
29 title: string;
30 };
31
32 export type ReferenceDocument = {
33 intro?: string[];
34 slug: string;
35 title: string;
36 sections: ReferenceSection[];
37 };
38
39 const TOP_LEVEL_COMMANDS = new Set([
40 'artifacts',
41 'config',
42 'dev',
43 'execute',
44 'link',
45 'listen',
46 'login',
47 'logout',
48 'orgs',
49 'proxy',
50 'run',
51 'search',
52 'tools',
53 'whoami',
54 ]);
55
56 export const resolveSkillBuildContext = (
57 channel: SkillReleaseChannel,
58 overrides?: Partial<Record<SkillFeatureFlag, boolean>>
59 ): SkillBuildContext => ({
60 channel,
61 experimentalFeatures: {
62 [CLI_EXPERIMENTAL_FEATURES.LISTEN]: isExperimentalFeatureEnabledByDefault(
63 CLI_EXPERIMENTAL_FEATURES.LISTEN,
64 channel
65 ),
66 ...overrides,
67 },
68 });
69
70 export const isEnabledForBuild = (build: SkillBuildContext, value?: FeatureScoped) =>
71 !value?.features || value.features.every(feature => build.experimentalFeatures[feature]);
72
73 export const renderCommandSnippet = (snippet: CommandSnippet) => {
74 const lines: string[] = [];
75
76 if (snippet.description) {
77 lines.push(snippet.description + ':');
78 }
79
80 lines.push('```bash', snippet.code, '```');
81 return lines.join('\n');
82 };
83
84 export const renderReferenceDocument = (document: ReferenceDocument, build: SkillBuildContext) => {
85 const lines = [`# ${document.title}`];
86
87 for (const introLine of document.intro ?? []) {
88 lines.push('', introLine);
89 }
90
91 for (const section of document.sections.filter(section => isEnabledForBuild(build, section))) {
92 lines.push('', `## ${section.title}`);
93
94 for (const bodyLine of section.body ?? []) {
95 lines.push('', bodyLine);
96 }
97
98 for (const command of (section.commands ?? []).filter(command =>
99 isEnabledForBuild(build, command)
100 )) {
101 lines.push('', renderCommandSnippet(command));
102 }
103 }
104
105 return lines.join('\n') + '\n';
106 };
107
108 export const validateCommandSnippet = (snippet: CommandSnippet, documentSlug: string) => {
109 const errors: string[] = [];
110 const lines = snippet.code.split('\n');
111 let inQuotedCommand = false;
112 let expectingContinuation = false;
113
114 for (const rawLine of lines) {
115 const line = rawLine.trim();
116 if (line.length === 0 || line.startsWith('#')) {
117 continue;
118 }
119
120 if (inQuotedCommand) {
121 const quoteCount = (line.match(/'/g) ?? []).length;
122 if (line === "'" || quoteCount % 2 === 1) {
123 inQuotedCommand = false;
124 }
125 continue;
126 }
127
128 if (expectingContinuation) {
129 expectingContinuation = line.endsWith('\\');
130 continue;
131 }
132
133 if (
134 line.startsWith('cat ') ||
135 line.startsWith('echo ') ||
136 line.startsWith('source ') ||
137 line.startsWith('export ')
138 ) {
139 continue;
140 }
141
142 if (!line.startsWith('composio ')) {
143 errors.push(`[${documentSlug}] command must start with "composio": ${line}`);
144 continue;
145 }
146
147 const normalized = line.endsWith('\\') ? line.slice(0, -1).trim() : line;
148 const parts = normalized.split(/\s+/);
149 const topLevel = parts[1];
150 if (!TOP_LEVEL_COMMANDS.has(topLevel)) {
151 errors.push(`[${documentSlug}] unknown top-level command "${topLevel}" in: ${normalized}`);
152 }
153
154 expectingContinuation = line.endsWith('\\');
155 const singleQuoteCount = (line.match(/'/g) ?? []).length;
156 if (singleQuoteCount % 2 === 1) {
157 inQuotedCommand = true;
158 }
159 }
160
161 return errors;
162 };
163
164 export const validateReferenceDocument = (document: ReferenceDocument) => {
165 const errors: string[] = [];
166
167 for (const section of document.sections) {
168 for (const command of section.commands ?? []) {
169 errors.push(...validateCommandSnippet(command, document.slug));
170 }
171 }
172
173 return errors;
174 };