@dench.com/cli 2.0.2 → 2.0.3

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.
Files changed (3) hide show
  1. package/dench.ts +16 -18
  2. package/host.ts +17 -2
  3. package/package.json +1 -1
package/dench.ts CHANGED
@@ -16,11 +16,11 @@ import { ConvexHttpClient } from "convex/browser";
16
16
  import { makeFunctionReference } from "convex/server";
17
17
  import { agentKindLabel, normalizeAgentKind } from "./agentKind";
18
18
  import {
19
+ apiHostForDenchHost,
19
20
  DEFAULT_HOST,
20
21
  isLocalHost,
21
22
  LOCAL_HOST,
22
23
  normalizeHost,
23
- PRODUCTION_API_URL,
24
24
  PRODUCTION_CONVEX_URL,
25
25
  PRODUCTION_HOST,
26
26
  resolveBackendAlias,
@@ -1993,16 +1993,6 @@ async function resolveOrgChoiceForOtp(input: {
1993
1993
  return { newOrganizationName: newName.trim() };
1994
1994
  }
1995
1995
 
1996
- function normalizeApiBase(input: string) {
1997
- const trimmed = input.trim();
1998
- const withProtocol = /^https?:\/\//.test(trimmed)
1999
- ? trimmed
2000
- : /^(localhost|127\.0\.0\.1|\[::1\])(?::|$)/.test(trimmed)
2001
- ? `http://${trimmed}`
2002
- : `https://${trimmed}`;
2003
- return new URL(withProtocol).origin;
2004
- }
2005
-
2006
1996
  async function parseResponseBody(response: Response) {
2007
1997
  const text = await response.text();
2008
1998
  if (!text) return {};
@@ -2256,16 +2246,14 @@ function resolveApiKeyConvexUrl(): string | undefined {
2256
2246
  /**
2257
2247
  * Resolve the Next.js API host the CLI should hit for HTTP calls (e.g.
2258
2248
  * Stripe billing topup, the gateway proxy). Same defense-in-depth
2259
- * pattern as `resolveApiKeyConvexUrl`: env wins, then the resolved
2260
- * Dench host the runtime is targeting, and finally
2261
- * `PRODUCTION_API_URL` as the last resort.
2249
+ * pattern as `resolveApiKeyConvexUrl`: env wins, then the canonical API
2250
+ * origin for the Dench host the runtime is targeting.
2262
2251
  */
2263
2252
  function resolveApiHost(fallbackHost: string): string {
2264
2253
  return (
2265
2254
  process.env.DENCH_API_URL?.trim() ||
2266
2255
  process.env.DENCH_INTERNAL_BASE?.trim() ||
2267
- fallbackHost ||
2268
- PRODUCTION_API_URL
2256
+ apiHostForDenchHost(fallbackHost)
2269
2257
  );
2270
2258
  }
2271
2259
 
@@ -2305,7 +2293,7 @@ async function getRuntime() {
2305
2293
  if (stored.status === "found") {
2306
2294
  return {
2307
2295
  mode: "session" as const,
2308
- host,
2296
+ host: resolveApiHost(host),
2309
2297
  client: new ConvexHttpClient(stored.session.convexUrl),
2310
2298
  sessionToken: stored.session.sessionToken,
2311
2299
  organization: stored.session.organization,
@@ -2486,7 +2474,17 @@ function gatewayAuthError(
2486
2474
  });
2487
2475
  }
2488
2476
  if (response.status === 401) {
2489
- return loginRequiredError(command);
2477
+ return new CliError(
2478
+ `${command} could not exchange the active Dench session for a gateway key (${code})`,
2479
+ {
2480
+ code,
2481
+ status: response.status,
2482
+ nextActions: [
2483
+ "Run dench sessions --json to confirm the selected session.",
2484
+ 'Run dench signin --kind <kind> --name "AI Agent - Project" to refresh the agent session if it expired.',
2485
+ ],
2486
+ },
2487
+ );
2490
2488
  }
2491
2489
  return new CliError(`${command} could not get a gateway key (${code})`, {
2492
2490
  code,
package/host.ts CHANGED
@@ -14,7 +14,8 @@ export const DEFAULT_HOST = PRODUCTION_HOST;
14
14
  * deployment + Next.js host backs `https://www.dench.com`.
15
15
  */
16
16
  export const PRODUCTION_API_URL = "https://www.dench.com";
17
- export const PRODUCTION_CONVEX_URL = "https://beaming-alligator-67.convex.cloud";
17
+ export const PRODUCTION_CONVEX_URL =
18
+ "https://beaming-alligator-67.convex.cloud";
18
19
  export const PRODUCTION_GATEWAY_URL = "https://gateway.merseoriginals.com";
19
20
 
20
21
  export type BackendAlias = "production" | "prod" | "staging" | "local";
@@ -52,6 +53,16 @@ export function isLocalHost(input: string) {
52
53
  );
53
54
  }
54
55
 
56
+ export function apiHostForDenchHost(input: string | undefined) {
57
+ if (!input?.trim()) return PRODUCTION_API_URL;
58
+ const normalized = normalizeHost(input);
59
+ // `dench.com` redirects to `www.dench.com` in production. Fetch drops the
60
+ // Authorization header across that redirect, so CLI API calls must start on
61
+ // the canonical API origin.
62
+ if (normalized === PRODUCTION_HOST) return PRODUCTION_API_URL;
63
+ return normalized;
64
+ }
65
+
55
66
  /**
56
67
  * Resolve `local|staging|prod|production|<url>` -> a normalized host
57
68
  * origin. Aliases let `dench backend set <alias>` (and `--backend
@@ -73,7 +84,11 @@ export function resolveBackendAlias(input: string): string {
73
84
  if (normalized === "staging" || normalized === "stage") {
74
85
  return STAGING_HOST;
75
86
  }
76
- if (normalized === "local" || normalized === "localhost" || normalized === "dev") {
87
+ if (
88
+ normalized === "local" ||
89
+ normalized === "localhost" ||
90
+ normalized === "dev"
91
+ ) {
77
92
  return LOCAL_HOST;
78
93
  }
79
94
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dench.com/cli",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Dench agent workspace CLI. v2 unifies auth behind `dench signin`; the legacy `dench login` / `dench onboard` / `dench setup` / `dench what-can-i-do` / `dench register` commands now error with a redirect.",
5
5
  "type": "module",
6
6
  "bin": {