File size: 2,186 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { detectRestrictedEnvironment } from "../utils/environment.mjs";
import { t } from "../i18n.mjs";

const RESOURCES = {
  combos: "/dashboard/combos",
  providers: "/dashboard/providers",
  "api-manager": "/dashboard/api-manager",
  "cli-tools": "/dashboard/cli-tools",
  agents: "/dashboard/agents",
  settings: "/dashboard/settings",
  logs: "/dashboard/logs",
  memory: "/dashboard/memory",
  skills: "/dashboard/skills",
  evals: "/dashboard/evals",
  audit: "/dashboard/audit",
  cost: "/dashboard/cost",
  resilience: "/dashboard/resilience",
  pricing: "/dashboard/pricing",
  tunnels: "/dashboard/tunnels",
  quota: "/dashboard/quota",
};

export function registerOpen(program) {
  program
    .command("open [resource] [id]")
    .description(t("open.description"))
    .option("--url", t("open.url"))
    .action(async (resource, id, opts, cmd) => {
      const globalOpts = cmd.optsWithGlobals();
      const baseUrl = globalOpts.baseUrl || "http://localhost:20128";
      let path = "/dashboard";

      if (resource) {
        const base = RESOURCES[resource];
        if (!base) {
          process.stderr.write(
            `Unknown resource: ${resource}\nAvailable: ${Object.keys(RESOURCES).join(", ")}\n`
          );
          process.exit(2);
        }
        path = base;
        if (id) {
          if (resource === "logs") {
            path += `?request=${encodeURIComponent(id)}`;
          } else if (resource === "settings") {
            path += `/${encodeURIComponent(id)}`;
          } else {
            path += `/${encodeURIComponent(id)}`;
          }
        }
      }

      const url = `${baseUrl}${path}`;

      if (opts.url) {
        process.stdout.write(url + "\n");
        return;
      }

      const env = detectRestrictedEnvironment();
      if (!env.canOpenBrowser) {
        process.stdout.write(url + "\n");
        if (env.hint) process.stderr.write(`[${env.type}] ${env.hint}\n`);
        return;
      }

      try {
        const openPkg = (await import("open")).default;
        await openPkg(url);
        process.stderr.write(`Opening: ${url}\n`);
      } catch {
        process.stdout.write(url + "\n");
      }
    });
}