forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.ts
More file actions
413 lines (376 loc) · 13.9 KB
/
Copy pathcreate.ts
File metadata and controls
413 lines (376 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { Config, Flags, Args } from "@oclif/core";
import inquirer from "inquirer";
import chalk from "chalk";
import fs from "fs-extra";
import path from "path";
import { promisify } from "util";
import { pipeline } from "stream";
import { createWriteStream } from "fs";
import { extract } from "tar";
import ora, { Ora } from "ora";
import { BaseCommand } from "./base-command.js";
import {
cloneGitHubSubdirectory,
isValidGitHubUrl,
} from "../lib/init/scaffold/github.js";
const streamPipeline = promisify(pipeline);
const theme = {
primary: chalk.magenta,
secondary: chalk.gray,
tertiary: chalk.gray,
error: chalk.red,
command: chalk.blue,
success: chalk.green,
warning: chalk.yellow,
divider: chalk.gray("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"),
bottomPadding: "",
};
interface CreateOptions {
projectName: string;
agentFramework: AgentFramework;
}
type AgentFramework =
| "langgraph-py"
| "langgraph-js"
| "flows"
| "mastra"
| "pydantic-ai"
| "llamaindex"
| "agno"
| "ag2"
| "adk"
| "aws-strands-py"
| "a2a"
| "microsoft-agent-framework-dotnet"
| "microsoft-agent-framework-py"
| "mcp-apps";
const TEMPLATE_REPOS: Record<AgentFramework, string> = {
"langgraph-py":
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/langgraph-python",
"langgraph-js":
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/langgraph-js",
mastra:
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/mastra",
flows:
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/crewai-flows",
llamaindex:
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/llamaindex",
agno: "https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/agno",
"pydantic-ai":
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/pydantic-ai",
ag2: "ag2ai/ag2-copilotkit-starter",
adk: "https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/adk",
"aws-strands-py":
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/strands-python",
a2a: "https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/a2a-middleware",
"microsoft-agent-framework-dotnet":
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/ms-agent-framework-dotnet",
"microsoft-agent-framework-py":
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/ms-agent-framework-python",
"mcp-apps":
"https://github.com/CopilotKit/CopilotKit/tree/main/examples/integrations/mcp-apps",
};
const FRAMEWORK_DOCUMENTATION: Record<AgentFramework, string> = {
"langgraph-py":
"https://langchain-ai.github.io/langgraph/concepts/why-langgraph",
"langgraph-js": "https://langchain-ai.github.io/langgraphjs",
flows: "https://docs.crewai.com/guides/flows/first-flow",
mastra: "https://mastra.ai/en/docs",
"pydantic-ai": "https://ai.pydantic.dev/ag-ui/",
llamaindex: "https://docs.llamaindex.ai/en/stable",
agno: "https://docs.agno.com/",
ag2: "https://docs.ag2.ai/latest/docs/user-guide/basic-concepts/overview",
adk: "https://google.github.io/adk-docs/",
"aws-strands-py": "https://strandsagents.com/latest/documentation/docs/",
a2a: "https://a2a-protocol.org/latest/",
"microsoft-agent-framework-dotnet":
"https://learn.microsoft.com/en-us/agent-framework/",
"microsoft-agent-framework-py":
"https://learn.microsoft.com/en-us/agent-framework/",
"mcp-apps": "https://modelcontextprotocol.github.io/ext-apps",
};
const FRAMEWORK_EMOJI: Record<AgentFramework, string> = {
"langgraph-js": "🦜",
"langgraph-py": "🦜",
flows: "👥",
mastra: "🌑",
"pydantic-ai": "🔼",
llamaindex: "🦙",
ag2: "🤖",
agno: "🅰️",
adk: "🤖",
a2a: "🤖",
"aws-strands-py": "🧬",
"microsoft-agent-framework-dotnet": "🟦",
"microsoft-agent-framework-py": "🟦",
"mcp-apps": "♍",
};
const KITE = `
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠙⣿⡛⠻⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠋⠀⠀⠈⢿⡄⠀⠀⠀⠈⠉⠙⣻⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠈⢿⡄⠀⢀⣠⣴⠾⠋⢸⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠁⢀⣀⣀⣀⣀⣤⣤⡾⢿⡟⠛⠉⠀⠀⠀⠀⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡛⠛⠛⠛⠉⠉⠉⠁⠀⢠⡿⣿⡀⠀⠀⠀⠀⠀⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⣰⡟⠀⠸⣧⠀⠀⠀⠀⢠⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠀⠀⢀⣼⠏⠀⠀⠀⣿⡀⠀⠀⠀⢸⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠂⣠⡿⠁⠀⠀⠀⠀⢸⡇⠀⠀⠀⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⣡⣾⣿⣄⠀⠀⠀⠀⠀⢸⡇⠀⠀⢰⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡟⠛⡿⠋⣡⣾⣿⣿⣿⣿⣦⡀⠀⠀⠀⢸⡇⠀⠀⣿⣿⣿⣿
⣿⣿⣿⣿⡿⠿⣿⠷⠂⡀⠘⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⢸⡇⠀⣼⣿⣿⣿⣿
⣿⣿⠻⢿⡷⠀⠁⠴⣿⣷⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠀⣾⠇⣴⣿⣿⣿⣿⣿
⡿⠛⠀⠀⢴⣾⣷⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣤⣿⣾⣿⣿⣿⣿⣿⣿
⣷⣾⣿⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
`;
export default class Create extends BaseCommand {
static description = "Create a new CopilotKit project";
static examples = [
"$ copilotkit create my-app",
"$ copilotkit create my-app --framework langgraph-js",
"$ copilotkit create -n my-app -f langgraph-js",
];
static flags = {
...BaseCommand.flags,
framework: Flags.string({
char: "f",
description: "Agent framework to use",
options: Object.keys(TEMPLATE_REPOS),
required: false,
}),
name: Flags.string({
char: "n",
description: "Name of the project",
required: false,
}),
"no-banner": Flags.boolean({
char: "q",
description: "Removes the banner",
default: false,
required: false,
}),
project: Flags.string({
description: "project ID (deprecated, kept for backwards compatibility)",
}),
};
static args = {
projectName: Args.string({
description: "Name of the project",
required: false,
}),
};
constructor(argv: string[], config: Config) {
super(argv, config);
}
async run() {
const { args, flags } = await this.parse(Create);
if (!flags["no-banner"]) {
this.log(theme.primary(KITE));
this.log(theme.primary("~ Welcome to CopilotKit! ~\n"));
this.log(theme.divider);
if (!flags.name && !args.projectName && !flags.framework) {
this.log(
"\n" + theme.secondary("Just a few questions to get started!\n"),
);
}
}
const projectNameInput =
flags.name || args.projectName || (await this.promptProjectName());
const projectName = projectNameInput.trim();
const usingCurrentDir = projectName === "." || projectName === "./";
const agentFramework =
flags.framework || (await this.promptAgentFramework());
const projectDir = usingCurrentDir
? process.cwd()
: path.resolve(process.cwd(), projectName);
if (usingCurrentDir) {
const allowedEntries = new Set([".git", ".gitignore", ".DS_Store"]);
const existingEntries = await fs.readdir(projectDir);
const blockingEntries = existingEntries.filter(
(entry) => !allowedEntries.has(entry),
);
if (blockingEntries.length > 0) {
this.log(theme.error("\nCurrent directory is not empty."));
this.log(
theme.secondary(
"\nPlease run create in an empty directory or specify a new project name.",
),
);
this.exit(1);
}
} else if (await fs.pathExists(projectDir)) {
this.log(theme.error(`\nDirectory "${projectName}" already exists.`));
this.log(theme.secondary("\nYou can:"));
this.log(theme.secondary(" 1. Choose a different project name"));
this.log(
theme.secondary(
" 2. Remove the existing directory manually if you want to use this name\n",
),
);
this.exit(1);
}
const options: CreateOptions = {
projectName,
agentFramework: agentFramework as AgentFramework,
};
const spinner = ora({
text: theme.secondary.bold("Creating your project..."),
color: "cyan",
spinner: "dots",
}).start();
try {
await fs.ensureDir(projectDir);
spinner.text = theme.secondary.bold("Downloading template...");
await this.downloadTemplate(projectDir, options.agentFramework, spinner);
const displayName = usingCurrentDir
? "current directory"
: `"${projectName}"`;
spinner.succeed(
theme.secondary.bold(`Project ${displayName} created successfully!`),
);
} catch (error: any) {
spinner.fail(theme.error(`Failed to create project: ${error.message}`));
this.exit(1);
}
this.log("\n" + theme.divider);
this.log(
"\n" +
theme.secondary.bold(
`🪁🤝${FRAMEWORK_EMOJI[options.agentFramework]} All set! \n\nYour project is ready to explore CopilotKit locally.`,
),
);
this.log("\n" + theme.secondary("Next steps:"));
if (usingCurrentDir) {
this.log(
theme.secondary(
" • You are already inside your new project directory",
),
);
} else {
this.log(theme.secondary(` • ${theme.command(`cd ${projectName}`)}`));
}
this.log(
theme.secondary(" • Follow the setup instructions in the README.md"),
);
this.log("\n" + theme.secondary("Documentation:"));
this.log(
theme.secondary(" • ") + theme.command("https://docs.copilotkit.ai"),
);
this.log(
theme.secondary(" • ") +
theme.command(FRAMEWORK_DOCUMENTATION[options.agentFramework]),
);
this.log(theme.bottomPadding);
}
private async promptProjectName(): Promise<string> {
const { projectName } = await inquirer.prompt([
{
type: "input",
name: "projectName",
message: theme.secondary("What is your project named?"),
validate: (input: string) => {
if (!input) return theme.error("Project name is required");
if (!/^[a-z0-9-]+$/.test(input)) {
return theme.error(
"Project name can only contain lowercase letters, numbers, and hyphens",
);
}
if (input.length > 30) {
return theme.error("Project name must be less than 30 characters");
}
return true;
},
},
]);
return projectName;
}
private async promptAgentFramework(): Promise<string> {
const { framework } = await inquirer.prompt([
{
type: "list",
name: "framework",
message: theme.secondary(
"Which agent framework would you like to use?",
),
choices: [
{
name: `${FRAMEWORK_EMOJI["langgraph-py"]} LangGraph (Python)`,
value: "langgraph-py",
},
{
name: `${FRAMEWORK_EMOJI["langgraph-js"]} LangGraph (JavaScript)`,
value: "langgraph-js",
},
{ name: `${FRAMEWORK_EMOJI.mastra} Mastra`, value: "mastra" },
{
name: `${FRAMEWORK_EMOJI["pydantic-ai"]} Pydantic AI`,
value: "pydantic-ai",
},
{
name: `${FRAMEWORK_EMOJI["aws-strands-py"]} AWS Strands (Python)`,
value: "aws-strands-py",
},
{ name: `${FRAMEWORK_EMOJI.adk} ADK`, value: "adk" },
{
name: `${FRAMEWORK_EMOJI["microsoft-agent-framework-dotnet"]} Microsoft Agent Framework (.NET)`,
value: "microsoft-agent-framework-dotnet",
},
{
name: `${FRAMEWORK_EMOJI["microsoft-agent-framework-py"]} Microsoft Agent Framework (Python)`,
value: "microsoft-agent-framework-py",
},
{
name: `${FRAMEWORK_EMOJI["mcp-apps"]} MCP Apps`,
value: "mcp-apps",
},
{ name: `${FRAMEWORK_EMOJI.flows} CrewAI Flows`, value: "flows" },
{
name: `${FRAMEWORK_EMOJI.llamaindex} LlamaIndex`,
value: "llamaindex",
},
{ name: `${FRAMEWORK_EMOJI.agno} Agno`, value: "agno" },
{ name: `${FRAMEWORK_EMOJI.ag2} AG2`, value: "ag2" },
{ name: `${FRAMEWORK_EMOJI.a2a} A2A`, value: "a2a" },
],
},
]);
return framework;
}
private async downloadTemplate(
projectDir: string,
framework: AgentFramework,
spinner: Ora,
): Promise<void> {
const templateRef = TEMPLATE_REPOS[framework];
// Monorepo subdirectory URLs use sparse checkout; standalone repos use tarball download
if (isValidGitHubUrl(templateRef)) {
const success = await cloneGitHubSubdirectory(
templateRef,
projectDir,
spinner,
);
if (!success) {
throw new Error(`Failed to clone template from ${templateRef}`);
}
return;
}
// Fallback: standalone repo tarball (e.g. ag2ai/ag2-copilotkit-starter)
const url = `https://github.com/${templateRef}/archive/refs/heads/main.tar.gz`;
try {
const response = await fetch(url);
if (!response.ok)
throw new Error(`Failed to download template: ${response.statusText}`);
const tempFile = path.join(projectDir, "template.tar.gz");
const fileStream = createWriteStream(tempFile);
if (!response.body) throw new Error("Failed to get response body");
await streamPipeline(response.body as any, fileStream);
await extract({
file: tempFile,
cwd: projectDir,
strip: 1,
});
await fs.remove(tempFile);
} catch (error: any) {
throw new Error(`Failed to download template: ${error.message}`);
}
}
}