@runtypelabs/cli 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -158,6 +158,8 @@ init_cjs_shims();
158
158
  var import_commander20 = require("commander");
159
159
  var import_chalk24 = __toESM(require("chalk"));
160
160
  var import_dotenv = require("dotenv");
161
+ var import_fs7 = require("fs");
162
+ var import_path4 = require("path");
161
163
 
162
164
  // src/commands/auth.ts
163
165
  init_cjs_shims();
@@ -186,19 +188,19 @@ var CallbackServer = class {
186
188
  this.codeReject = reject;
187
189
  });
188
190
  this.app.get("/callback", (req, res) => {
189
- const { code, error } = req.query;
191
+ const { token, error } = req.query;
190
192
  if (error) {
191
193
  res.send(this.errorHTML(error));
192
194
  this.codeReject(new Error(error));
193
195
  return;
194
196
  }
195
- if (!code) {
196
- res.send(this.errorHTML("No authorization code received"));
197
- this.codeReject(new Error("No authorization code received"));
197
+ if (!token) {
198
+ res.send(this.errorHTML("No authentication token received"));
199
+ this.codeReject(new Error("No authentication token received"));
198
200
  return;
199
201
  }
200
202
  res.send(this.successHTML());
201
- this.codeResolve(code);
203
+ this.codeResolve(token);
202
204
  setTimeout(() => this.stop(), 1e3);
203
205
  });
204
206
  this.app.get("/health", (_req, res) => {
@@ -547,7 +549,7 @@ function getDefaultTemperature() {
547
549
  }
548
550
 
549
551
  // src/auth/api-key-manager.ts
550
- var isDashboardAuthResponse = (value) => {
552
+ var isCliTokenResponse = (value) => {
551
553
  if (!value || typeof value !== "object") {
552
554
  return false;
553
555
  }
@@ -562,29 +564,36 @@ var isAuthMeResponse = (value) => {
562
564
  return typeof record.user_id === "string";
563
565
  };
564
566
  var ApiKeyManager = class {
565
- async exchangeSessionForApiKey(authCode, apiUrl, dashboardUrl) {
566
- void apiUrl;
567
- const base = dashboardUrl ?? getDashboardUrl();
568
- const authResponse = await fetch(`${base.replace(/\/$/, "")}/api/cli/auth?code=${encodeURIComponent(authCode)}`, {
569
- method: "GET",
567
+ /**
568
+ * Exchange a Clerk session JWT for a persistent API key.
569
+ *
570
+ * Calls POST /v1/auth/cli-token on the Runtype API directly,
571
+ * passing the Clerk JWT as a Bearer token. The API verifies the JWT
572
+ * with Clerk and creates an API key for the CLI.
573
+ */
574
+ async exchangeSessionForApiKey(clerkJwt, apiUrl) {
575
+ const baseUrl = apiUrl || getApiUrl();
576
+ const response = await fetch(`${baseUrl}/${getApiVersion()}/auth/cli-token`, {
577
+ method: "POST",
570
578
  headers: {
571
- "Content-Type": "application/json"
579
+ "Content-Type": "application/json",
580
+ Authorization: `Bearer ${clerkJwt}`
572
581
  }
573
582
  });
574
- if (!authResponse.ok) {
575
- const error = await authResponse.text();
583
+ if (!response.ok) {
584
+ const error = await response.text();
576
585
  throw new Error(`Authentication failed: ${error}`);
577
586
  }
578
- const authData = await authResponse.json();
579
- if (!isDashboardAuthResponse(authData)) {
587
+ const data = await response.json();
588
+ if (!isCliTokenResponse(data)) {
580
589
  throw new Error("Invalid authentication response format");
581
590
  }
582
591
  const result = {
583
- key: authData.apiKey,
584
- userId: authData.userId
592
+ key: data.apiKey,
593
+ userId: data.userId
585
594
  };
586
- if (authData.orgId) {
587
- result.orgId = authData.orgId;
595
+ if (data.orgId) {
596
+ result.orgId = data.orgId;
588
597
  }
589
598
  return result;
590
599
  }
@@ -637,8 +646,7 @@ authCommand.command("signup").description("Create a new Runtype account").option
637
646
  const apiKeyManager = new ApiKeyManager();
638
647
  const { key, userId, orgId } = await apiKeyManager.exchangeSessionForApiKey(
639
648
  sessionToken,
640
- options.apiUrl || getApiUrl(),
641
- options.dashboardUrl || getDashboardUrl()
649
+ options.apiUrl || getApiUrl()
642
650
  );
643
651
  spinner.text = "Storing credentials securely...";
644
652
  const store = new CredentialStore();
@@ -704,8 +712,7 @@ authCommand.command("login").description("Login to existing account").option("--
704
712
  const apiKeyManager = new ApiKeyManager();
705
713
  const { key, userId, orgId } = await apiKeyManager.exchangeSessionForApiKey(
706
714
  sessionToken,
707
- options.apiUrl || getApiUrl(),
708
- options.dashboardUrl || getDashboardUrl()
715
+ options.apiUrl || getApiUrl()
709
716
  );
710
717
  spinner.text = "Storing credentials...";
711
718
  await store.saveCredentials({
@@ -981,8 +988,7 @@ async function handleBrowserLogin(store, apiUrl) {
981
988
  const apiKeyManager = new ApiKeyManager();
982
989
  const { key, userId, orgId } = await apiKeyManager.exchangeSessionForApiKey(
983
990
  sessionToken,
984
- apiUrl || getApiUrl(),
985
- getDashboardUrl()
991
+ apiUrl || getApiUrl()
986
992
  );
987
993
  spinner.text = "Storing credentials...";
988
994
  await store.saveCredentials({
@@ -2596,8 +2602,7 @@ async function handleBrowserAuth(store, mode, apiUrl) {
2596
2602
  const apiKeyManager = new ApiKeyManager();
2597
2603
  const { key, userId, orgId } = await apiKeyManager.exchangeSessionForApiKey(
2598
2604
  sessionToken,
2599
- apiUrl || getApiUrl(),
2600
- getDashboardUrl()
2605
+ apiUrl || getApiUrl()
2601
2606
  );
2602
2607
  await store.saveCredentials({
2603
2608
  apiKey: key,
@@ -4494,8 +4499,17 @@ flowVersionsCommand.command("publish <flowId>").description("Publish a version")
4494
4499
  // src/index.ts
4495
4500
  init_credential_store();
4496
4501
  (0, import_dotenv.config)();
4502
+ function getPackageVersion() {
4503
+ try {
4504
+ const pkgPath = (0, import_path4.join)(__dirname, "..", "package.json");
4505
+ const pkg = JSON.parse((0, import_fs7.readFileSync)(pkgPath, "utf-8"));
4506
+ return pkg.version || "0.0.0";
4507
+ } catch {
4508
+ return "0.0.0";
4509
+ }
4510
+ }
4497
4511
  var program = new import_commander20.Command();
4498
- program.name("runtype").description("CLI for Runtype AI Platform").version("0.1.0").option("-v, --verbose", "Enable verbose output").option("--api-url <url>", "Override API URL").option("--json", "Output in JSON format");
4512
+ program.name("runtype").description("CLI for Runtype AI Platform").version(getPackageVersion()).option("-v, --verbose", "Enable verbose output").option("--api-url <url>", "Override API URL").option("--json", "Output in JSON format");
4499
4513
  program.addCommand(initCommand);
4500
4514
  program.addCommand(authCommand);
4501
4515
  program.addCommand(flowsCommand);
@@ -4532,7 +4546,7 @@ try {
4532
4546
  } else if (commanderError.code === "commander.unknownOption") {
4533
4547
  console.error(import_chalk24.default.red(`Error: ${commanderError.message}`));
4534
4548
  process.exit(1);
4535
- } else if (commanderError.code === "commander.help") {
4549
+ } else if (commanderError.code === "commander.help" || commanderError.code === "commander.version") {
4536
4550
  process.exit(0);
4537
4551
  } else {
4538
4552
  console.error(import_chalk24.default.red("An unexpected error occurred:"));