mod.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env -S deno run --allow-read=. --allow-write=. --allow-run --allow-net --allow-env
  2. import { parse } from "./deps.ts";
  3. import { blue, red, gray, yellow, bold } from "./deps.ts";
  4. import { buildApiHandler } from "./api/mod.ts";
  5. import { StandaloneAgent } from "./core/StandaloneAgent.ts";
  6. import { SYSTEM_PROMPT } from "./core/prompts.ts";
  7. import type { ApiHandler, AgentConfig } from "./types.d.ts";
  8. // Parse command line arguments
  9. const args = parse(Deno.args, {
  10. string: ["model", "key"],
  11. boolean: ["help"],
  12. alias: {
  13. m: "model",
  14. k: "key",
  15. h: "help"
  16. },
  17. default: {
  18. model: "anthropic/claude-3.5-sonnet"
  19. },
  20. });
  21. if (args.help || Deno.args.length === 0) {
  22. console.log(blue("\nCline - AI Coding Assistant\n"));
  23. console.log("Usage:");
  24. console.log(" cline <task> [options]\n");
  25. console.log("Required Permissions:");
  26. console.log(" --allow-read=. Read files in working directory");
  27. console.log(" --allow-write=. Write files in working directory");
  28. console.log(" --allow-run Execute commands (with interactive prompts)\n");
  29. console.log(" --allow-net Make API calls");
  30. console.log(" --allow-env Access environment variables\n");
  31. console.log("Pre-approved Commands:");
  32. console.log(" npm - Package management (install, run, test, build)");
  33. console.log(" git - Version control (status, add, commit, push, pull, clone)");
  34. console.log(" deno - Deno runtime (run, test, fmt, lint, check)");
  35. console.log(" ls - List directory contents");
  36. console.log(" cat - Show file contents");
  37. console.log(" echo - Print text");
  38. console.log(" find - Search for files");
  39. console.log("\nOther commands will prompt for confirmation before execution.\n");
  40. console.log("Options:");
  41. console.log(" -m, --model <model> LLM model to use (default: \"anthropic/claude-3.5-sonnet\")");
  42. console.log(" -k, --key <key> OpenRouter API key (or set OPENROUTER_API_KEY env var)");
  43. console.log(" -h, --help Display help for command\n");
  44. console.log("Examples:");
  45. console.log(gray(" # Run pre-approved command"));
  46. console.log(" cline \"Run npm install\"\n");
  47. console.log(gray(" # Run command that requires confirmation"));
  48. console.log(" cline \"Run yarn install\"\n");
  49. Deno.exit(0);
  50. }
  51. // Verify required permissions
  52. const requiredPermissions = [
  53. { name: "read", path: "." },
  54. { name: "write", path: "." },
  55. { name: "run" },
  56. { name: "net" },
  57. { name: "env" }
  58. ] as const;
  59. for (const permission of requiredPermissions) {
  60. const status = await Deno.permissions.query(permission);
  61. if (status.state !== "granted") {
  62. console.error(red(`Error: Missing required permission`));
  63. console.error(yellow(`Hint: Run with the following permissions:`));
  64. console.error(yellow(` deno run ${requiredPermissions.map(p =>
  65. "path" in p ? `--allow-${p.name}=${p.path}` : `--allow-${p.name}`
  66. ).join(" ")} cli/mod.ts ...\n`));
  67. Deno.exit(1);
  68. }
  69. }
  70. const task = args._[0] as string;
  71. const apiKey = args.key || Deno.env.get("OPENROUTER_API_KEY");
  72. if (!apiKey) {
  73. console.error(red("Error: OpenRouter API key is required. Set it with --key or OPENROUTER_API_KEY env var"));
  74. console.error(yellow("Get your API key from: https://openrouter.ai/keys"));
  75. Deno.exit(1);
  76. }
  77. try {
  78. const workingDir = Deno.cwd();
  79. // Initialize API handler
  80. const apiHandler = buildApiHandler({
  81. model: args.model,
  82. apiKey
  83. });
  84. // Create agent instance
  85. const agent = new StandaloneAgent({
  86. api: apiHandler,
  87. systemPrompt: await SYSTEM_PROMPT(workingDir),
  88. workingDir
  89. });
  90. // Run the task
  91. console.log(blue(`\nStarting task: ${bold(task)}`));
  92. console.log(gray(`Working directory: ${workingDir}`));
  93. console.log(gray(`Model: ${args.model}`));
  94. console.log(gray("---\n"));
  95. await agent.runTask(task);
  96. } catch (error) {
  97. if (error instanceof Error) {
  98. console.error(red(`\nError: ${error.message}`));
  99. } else {
  100. console.error(red("\nAn unknown error occurred"));
  101. }
  102. Deno.exit(1);
  103. }