-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud-agent.tsx
More file actions
135 lines (113 loc) · 3.5 KB
/
cloud-agent.tsx
File metadata and controls
135 lines (113 loc) · 3.5 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
#!/usr/bin/env node
/**
* Cloud Agents CLI
* Main entry point for the cloud-agent command
*/
import React from "react";
import { render } from "ink";
import { Command } from "commander";
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { CloudAgentsApiClient } from "./src/api/client.js";
import { App } from "./src/components/App.js";
import { detectRepoAndRef } from "./src/utils/git.js";
import { registerCommands } from "./src/commands/index.js";
import type { CommandContext } from "./src/cli/types.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pkg = JSON.parse(readFileSync(join(__dirname, "package.json"), "utf-8"));
const VERSION =
typeof pkg.version === "string" && pkg.version.length > 0
? pkg.version
: "0.0.0";
function printApiKeyError(): void {
console.error("Error: CURSOR_API_KEY environment variable is not set");
console.error("");
console.error("Please set it with:");
console.error(" export CURSOR_API_KEY=your_api_key");
console.error("");
console.error("You can obtain an API key from:");
console.error(" https://cursor.com/settings");
}
function createProgram(
version: string,
context?: CommandContext | null
): Command {
const program = new Command()
.name("cloud-agent")
.description("CLI tool for managing Cursor Cloud Agents")
.version(version);
if (context) {
registerCommands(program, context);
}
return program;
}
async function showInteractiveMenu(
apiClient: CloudAgentsApiClient,
context: CommandContext
): Promise<void> {
const gitInfo = await detectRepoAndRef(context.workingDir);
const repositoryFilter = gitInfo?.repository;
const { waitUntilExit } = render(
<App
apiClient={apiClient}
context={context}
initialView="menu"
repositoryFilter={repositoryFilter}
/>
);
await waitUntilExit();
}
async function main() {
const args = process.argv.slice(2);
const isHelp = args.includes("--help") || args.includes("-h");
const isNonInteractive =
args.includes("--non-interactive") || args.includes("--no-interactive");
if (isHelp || (args.length === 0 && isNonInteractive)) {
createProgram(VERSION).help();
return;
}
// Commands that don't require an API key
const noApiKeyCommands = ["agents-md"];
const firstArg = args.length > 0 && !args[0].startsWith("-") ? args[0] : null;
const needsApiKey = !firstArg || !noApiKeyCommands.includes(firstArg);
const apiKey = process.env.CURSOR_API_KEY;
if (needsApiKey && !apiKey) {
printApiKeyError();
process.exit(1);
}
const apiClient = apiKey ? new CloudAgentsApiClient(apiKey) : (null as any);
const workingDir = process.cwd();
const context: CommandContext = {
apiClient,
workingDir,
};
const program = createProgram(VERSION, context);
const hasCommand =
args.length > 0 &&
!args[0].startsWith("-") &&
program.commands.some((cmd) => cmd.name() === args[0]);
const isTTY = process.stdin.isTTY;
if (!hasCommand) {
if (!isNonInteractive && isTTY) {
await showInteractiveMenu(apiClient, context);
return;
} else {
program.help();
return;
}
}
await program.parseAsync(process.argv);
}
main().catch((error: unknown) => {
if (error instanceof Error) {
console.error("Error:", error.message || error);
if (process.env.DEBUG) {
console.error(error.stack);
}
} else {
console.error("Error:", error);
}
process.exit(1);
});