@dench.com/cli 2.0.0 → 2.0.2

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/image.ts CHANGED
@@ -3,9 +3,9 @@
3
3
  * the Dench Cloud Gateway.
4
4
  *
5
5
  * Mirrors `dench search`: bypasses Convex entirely and talks straight HTTP
6
- * to the gateway, authenticating with the `DENCH_API_KEY` baked into every
7
- * sandbox at create time. Works anywhere that env var is exported (sandbox,
8
- * CI, local dev).
6
+ * to the gateway, authenticating with either `DENCH_API_KEY` or a
7
+ * paid-workspace gateway key resolved from the active agent session by the
8
+ * top-level dispatcher.
9
9
  *
10
10
  * Persistence model: the gateway already writes the bytes to Convex Storage
11
11
  * + the org's fileTree on success (so the workspace UI sees the file
@@ -27,38 +27,33 @@
27
27
 
28
28
  import { mkdir, readFile, writeFile } from "node:fs/promises";
29
29
  import { dirname, isAbsolute, resolve } from "node:path";
30
- import {
31
- CliArgError,
32
- getFlag,
33
- hasFlag,
34
- shift as shiftRaw,
35
- } from "./lib/cli-args";
30
+ import { getFlag, hasFlag } from "./lib/cli-args";
36
31
 
37
32
  type ImageCliContext = {
38
33
  args: string[];
39
34
  jsonOutput: boolean;
35
+ bearerToken?: string;
36
+ gatewayBaseUrl?: string;
40
37
  };
41
38
 
42
39
  class ImageCliError extends Error {}
43
40
 
44
- function shift(args: string[], expected: string): string {
45
- try {
46
- return shiftRaw(args, expected);
47
- } catch (error) {
48
- if (error instanceof CliArgError) {
49
- throw new ImageCliError(error.message);
50
- }
51
- throw error;
52
- }
53
- }
54
-
55
41
  const ALLOWED_QUALITIES = new Set(["auto", "low", "medium", "high"]);
56
42
  const ALLOWED_FORMATS = new Set(["png", "jpeg", "webp"]);
57
43
 
58
- function resolveGatewayBaseUrl(): string {
44
+ function normalizeGatewayBaseUrl(value: string): string {
45
+ return value.replace(/\/+$/, "").replace(/\/v1$/, "");
46
+ }
47
+
48
+ function resolveGatewayBaseUrl(
49
+ ctx?: Pick<ImageCliContext, "gatewayBaseUrl">,
50
+ ): string {
51
+ if (ctx?.gatewayBaseUrl?.trim()) {
52
+ return normalizeGatewayBaseUrl(ctx.gatewayBaseUrl.trim());
53
+ }
59
54
  const explicit =
60
55
  process.env.DENCH_GATEWAY_URL?.trim() || process.env.GATEWAY_URL?.trim();
61
- if (explicit) return explicit.replace(/\/+$/, "");
56
+ if (explicit) return normalizeGatewayBaseUrl(explicit);
62
57
  const host = process.env.DENCH_HOST?.trim();
63
58
  if (host && /localhost|127\.0\.0\.1/.test(host)) {
64
59
  return "http://localhost:8787";
@@ -66,16 +61,17 @@ function resolveGatewayBaseUrl(): string {
66
61
  return "https://gateway.merseoriginals.com";
67
62
  }
68
63
 
69
- function requireApiKey(): string {
70
- const apiKey = process.env.DENCH_API_KEY?.trim();
71
- if (!apiKey) {
64
+ function requireBearerToken(
65
+ ctx?: Pick<ImageCliContext, "bearerToken">,
66
+ ): string {
67
+ const bearerToken =
68
+ ctx?.bearerToken?.trim() || process.env.DENCH_API_KEY?.trim();
69
+ if (!bearerToken) {
72
70
  throw new ImageCliError(
73
- "DENCH_API_KEY is not set. Inside a Dench sandbox it is baked into " +
74
- "the env automatically; outside, export it manually or run " +
75
- "`dench login` first.",
71
+ "No Dench gateway credential found. Set DENCH_API_KEY or run `dench signin` for a paid workspace.",
76
72
  );
77
73
  }
78
- return apiKey;
74
+ return bearerToken;
79
75
  }
80
76
 
81
77
  function resolveDefaultVolumeRoot(): string {
@@ -125,16 +121,17 @@ type GatewayImageResponse = {
125
121
  };
126
122
 
127
123
  async function callGateway(
124
+ ctx: ImageCliContext,
128
125
  path: "/v1/images/generations" | "/v1/images/edits",
129
126
  body: Record<string, unknown>,
130
127
  ): Promise<GatewayImageResponse> {
131
- const apiKey = requireApiKey();
132
- const baseUrl = resolveGatewayBaseUrl();
128
+ const bearerToken = requireBearerToken(ctx);
129
+ const baseUrl = resolveGatewayBaseUrl(ctx);
133
130
  const response = await fetch(`${baseUrl}${path}`, {
134
131
  method: "POST",
135
132
  headers: {
136
133
  "content-type": "application/json",
137
- Authorization: `Bearer ${apiKey}`,
134
+ Authorization: `Bearer ${bearerToken}`,
138
135
  },
139
136
  body: JSON.stringify(body),
140
137
  });
@@ -246,7 +243,16 @@ function parseCommonOpts(args: string[]): CommonImageOpts {
246
243
  `Invalid --moderation: ${moderation}. Allowed: auto, low`,
247
244
  );
248
245
  }
249
- return { size, quality, format, savePath, output, noWrite, background, moderation };
246
+ return {
247
+ size,
248
+ quality,
249
+ format,
250
+ savePath,
251
+ output,
252
+ noWrite,
253
+ background,
254
+ moderation,
255
+ };
250
256
  }
251
257
 
252
258
  function buildGenerationBody(
@@ -283,7 +289,7 @@ async function runGenerateSubcommand(ctx: ImageCliContext): Promise<void> {
283
289
  );
284
290
  }
285
291
  const body = buildGenerationBody(prompt, opts);
286
- const result = await callGateway("/v1/images/generations", body);
292
+ const result = await callGateway(ctx, "/v1/images/generations", body);
287
293
  if (!result.b64_json) {
288
294
  throw new ImageCliError("Gateway response did not include image bytes.");
289
295
  }
@@ -341,14 +347,16 @@ async function runEditSubcommand(ctx: ImageCliContext): Promise<void> {
341
347
  `Too many --input paths (${inputs.length}); the gateway accepts at most 8.`,
342
348
  );
343
349
  }
344
- const inputBase64 = await Promise.all(inputs.map((p) => readBase64FromPath(p)));
350
+ const inputBase64 = await Promise.all(
351
+ inputs.map((p) => readBase64FromPath(p)),
352
+ );
345
353
  const maskBase64 = mask ? await readBase64FromPath(mask) : undefined;
346
354
  const body = {
347
355
  ...buildGenerationBody(prompt, opts),
348
356
  input_images_base64: inputBase64,
349
357
  ...(maskBase64 ? { mask_base64: maskBase64 } : {}),
350
358
  };
351
- const result = await callGateway("/v1/images/edits", body);
359
+ const result = await callGateway(ctx, "/v1/images/edits", body);
352
360
  if (!result.b64_json) {
353
361
  throw new ImageCliError("Gateway response did not include image bytes.");
354
362
  }
@@ -379,9 +387,10 @@ function imageHelp(): void {
379
387
  console.log(`Usage: dench image <subcommand>
380
388
 
381
389
  Generate or edit images via the Dench Cloud Gateway (gpt-image-2). Auths
382
- with DENCH_API_KEY (baked into every sandbox automatically). Each call
383
- writes the bytes to Convex Storage (workspace tree updates immediately)
384
- and, when running inside a sandbox, also writes the decoded bytes to
390
+ with DENCH_API_KEY or the active \`dench signin\` agent session. Agent
391
+ sessions require a paid workspace. Each call writes the bytes to Convex
392
+ Storage (workspace tree updates immediately) and, when running inside a sandbox,
393
+ also writes the decoded bytes to
385
394
  local disk so subsequent bash commands see the file without waiting on
386
395
  the daemon's downward sync.
387
396
 
@@ -397,7 +406,7 @@ Edit / remix one or more existing images (image-to-image):
397
406
  [--save-path ...] [--output ...] [--no-write] [--json]
398
407
 
399
408
  Environment variables:
400
- DENCH_API_KEY Required. Bearer token sent to the gateway.
409
+ DENCH_API_KEY Optional when a dench signin agent session is active.
401
410
  DENCH_GATEWAY_URL Override the gateway base URL.
402
411
  GATEWAY_URL Same, used when DENCH_GATEWAY_URL is unset.
403
412
  DENCH_HOST If it points at localhost, defaults to
@@ -81,7 +81,7 @@ export function resolveEnrichmentGatewayBaseUrl(
81
81
  env: EnrichmentGatewayEnv = process.env,
82
82
  ): string {
83
83
  const explicit = env.DENCH_GATEWAY_URL?.trim() || env.GATEWAY_URL?.trim();
84
- if (explicit) return explicit.replace(/\/+$/, "");
84
+ if (explicit) return explicit.replace(/\/+$/, "").replace(/\/v1$/, "");
85
85
 
86
86
  const host = env.DENCH_HOST?.trim();
87
87
  if (host && /localhost|127\.0\.0\.1/.test(host)) {
@@ -1248,7 +1248,7 @@ async function callGatewayJson(
1248
1248
 
1249
1249
  function resolveBaseUrl(options: EnrichmentGatewayOptions): string {
1250
1250
  return (
1251
- options.baseUrl?.trim().replace(/\/+$/, "") ??
1251
+ options.baseUrl?.trim().replace(/\/+$/, "").replace(/\/v1$/, "") ??
1252
1252
  resolveEnrichmentGatewayBaseUrl(options.env)
1253
1253
  );
1254
1254
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dench.com/cli",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
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": {
package/search.ts CHANGED
@@ -2,11 +2,9 @@
2
2
  * `dench search` — live web access through the Dench Cloud Gateway.
3
3
  *
4
4
  * The gateway exposes Exa Search at three routes; we mirror them as
5
- * three subcommands and authenticate with the `DENCH_API_KEY` baked
6
- * into every sandbox at create time. Unlike most other CLI surfaces
7
- * this one bypasses Convex entirely — it speaks straight HTTP to the
8
- * gateway, so it works in any environment that has the env var
9
- * (sandboxes, CI, local dev with `DENCH_API_KEY` exported manually).
5
+ * three subcommands and authenticate with either `DENCH_API_KEY` or a
6
+ * paid-workspace gateway key resolved from the active agent session by
7
+ * the top-level dispatcher.
10
8
  *
11
9
  * Subcommands:
12
10
  * search "<query>" [--num-results N] [--type auto|fast|deep|deep-reasoning|neural]
@@ -17,32 +15,21 @@
17
15
  * search answer "<query>" [--json]
18
16
  */
19
17
 
20
- import {
21
- CliArgError,
22
- getFlag,
23
- hasFlag,
24
- shift as shiftRaw,
25
- } from "./lib/cli-args";
18
+ import { getFlag } from "./lib/cli-args";
26
19
 
27
20
  type SearchCliContext = {
28
21
  args: string[];
29
22
  jsonOutput: boolean;
23
+ bearerToken?: string;
24
+ gatewayBaseUrl?: string;
30
25
  };
31
26
 
32
27
  class SearchCliError extends Error {}
33
28
 
34
- function shift(args: string[], expected: string): string {
35
- try {
36
- return shiftRaw(args, expected);
37
- } catch (error) {
38
- if (error instanceof CliArgError) {
39
- throw new SearchCliError(error.message);
40
- }
41
- throw error;
42
- }
43
- }
44
-
45
- function parsePositiveInt(name: string, raw: string | undefined): number | undefined {
29
+ function parsePositiveInt(
30
+ name: string,
31
+ raw: string | undefined,
32
+ ): number | undefined {
46
33
  if (raw === undefined) return undefined;
47
34
  const parsed = Number(raw);
48
35
  if (!Number.isFinite(parsed) || parsed <= 0) {
@@ -78,10 +65,19 @@ const ALLOWED_CATEGORIES = new Set([
78
65
  "people",
79
66
  ]);
80
67
 
81
- function resolveGatewayBaseUrl(): string {
68
+ function normalizeGatewayBaseUrl(value: string): string {
69
+ return value.replace(/\/+$/, "").replace(/\/v1$/, "");
70
+ }
71
+
72
+ function resolveGatewayBaseUrl(
73
+ ctx?: Pick<SearchCliContext, "gatewayBaseUrl">,
74
+ ): string {
75
+ if (ctx?.gatewayBaseUrl?.trim()) {
76
+ return normalizeGatewayBaseUrl(ctx.gatewayBaseUrl.trim());
77
+ }
82
78
  const explicit =
83
79
  process.env.DENCH_GATEWAY_URL?.trim() || process.env.GATEWAY_URL?.trim();
84
- if (explicit) return explicit.replace(/\/+$/, "");
80
+ if (explicit) return normalizeGatewayBaseUrl(explicit);
85
81
  // Local dev: the gateway listens on :8787 (matches gateway/src/index.ts
86
82
  // and the dev-server skill). Anything that smells like localhost in
87
83
  // DENCH_HOST routes to the local gateway too so `dench --host
@@ -93,29 +89,31 @@ function resolveGatewayBaseUrl(): string {
93
89
  return "https://gateway.merseoriginals.com";
94
90
  }
95
91
 
96
- function requireApiKey(): string {
97
- const apiKey = process.env.DENCH_API_KEY?.trim();
98
- if (!apiKey) {
92
+ function requireBearerToken(
93
+ ctx?: Pick<SearchCliContext, "bearerToken">,
94
+ ): string {
95
+ const bearerToken =
96
+ ctx?.bearerToken?.trim() || process.env.DENCH_API_KEY?.trim();
97
+ if (!bearerToken) {
99
98
  throw new SearchCliError(
100
- "DENCH_API_KEY is not set. Inside a Dench sandbox it is baked into " +
101
- "the env automatically; outside, export it manually or run " +
102
- "`dench login` first.",
99
+ "No Dench gateway credential found. Set DENCH_API_KEY or run `dench signin` for a paid workspace.",
103
100
  );
104
101
  }
105
- return apiKey;
102
+ return bearerToken;
106
103
  }
107
104
 
108
105
  async function callGateway(
106
+ ctx: SearchCliContext,
109
107
  path: "/v1/search" | "/v1/search/contents" | "/v1/search/answer",
110
108
  body: Record<string, unknown>,
111
109
  ): Promise<unknown> {
112
- const apiKey = requireApiKey();
113
- const baseUrl = resolveGatewayBaseUrl();
110
+ const bearerToken = requireBearerToken(ctx);
111
+ const baseUrl = resolveGatewayBaseUrl(ctx);
114
112
  const response = await fetch(`${baseUrl}${path}`, {
115
113
  method: "POST",
116
114
  headers: {
117
115
  "content-type": "application/json",
118
- Authorization: `Bearer ${apiKey}`,
116
+ Authorization: `Bearer ${bearerToken}`,
119
117
  },
120
118
  body: JSON.stringify(body),
121
119
  });
@@ -143,7 +141,9 @@ function asString(value: unknown): string | undefined {
143
141
  }
144
142
 
145
143
  function asNumber(value: unknown): number | undefined {
146
- return typeof value === "number" && Number.isFinite(value) ? value : undefined;
144
+ return typeof value === "number" && Number.isFinite(value)
145
+ ? value
146
+ : undefined;
147
147
  }
148
148
 
149
149
  function asObject(value: unknown): Record<string, unknown> | undefined {
@@ -184,7 +184,8 @@ function formatSearchResults(payload: unknown, limit?: number): string {
184
184
  lines.push("(no results)");
185
185
  return lines.join("\n");
186
186
  }
187
- const cap = limit !== undefined ? Math.min(results.length, limit) : results.length;
187
+ const cap =
188
+ limit !== undefined ? Math.min(results.length, limit) : results.length;
188
189
  for (let i = 0; i < cap; i++) {
189
190
  const result = asObject(results[i]);
190
191
  if (!result) continue;
@@ -197,7 +198,9 @@ function formatSearchResults(payload: unknown, limit?: number): string {
197
198
  const snippet =
198
199
  asString(result.text) ??
199
200
  (Array.isArray(result.highlights)
200
- ? result.highlights.filter((h): h is string => typeof h === "string").join(" ")
201
+ ? result.highlights
202
+ .filter((h): h is string => typeof h === "string")
203
+ .join(" ")
201
204
  : undefined) ??
202
205
  asString(result.summary);
203
206
  lines.push(`${i + 1}. ${title}`);
@@ -284,7 +287,10 @@ function out(ctx: SearchCliContext, payload: unknown, formatted: string): void {
284
287
  // ────────────────────────────────────────────────────────────────────────────
285
288
 
286
289
  async function runSearchSubcommand(ctx: SearchCliContext): Promise<void> {
287
- const numResults = parsePositiveInt("--num-results", getFlag(ctx.args, "--num-results"));
290
+ const numResults = parsePositiveInt(
291
+ "--num-results",
292
+ getFlag(ctx.args, "--num-results"),
293
+ );
288
294
  const type = getFlag(ctx.args, "--type");
289
295
  if (type !== undefined && !ALLOWED_TYPES.has(type)) {
290
296
  throw new SearchCliError(
@@ -321,12 +327,15 @@ async function runSearchSubcommand(ctx: SearchCliContext): Promise<void> {
321
327
  if (category) body.category = category;
322
328
  if (includeDomains) body.includeDomains = includeDomains;
323
329
  if (excludeDomains) body.excludeDomains = excludeDomains;
324
- const payload = await callGateway("/v1/search", body);
330
+ const payload = await callGateway(ctx, "/v1/search", body);
325
331
  out(ctx, payload, formatSearchResults(payload, numResults));
326
332
  }
327
333
 
328
334
  async function runContentsSubcommand(ctx: SearchCliContext): Promise<void> {
329
- const maxChars = parsePositiveInt("--max-chars", getFlag(ctx.args, "--max-chars"));
335
+ const maxChars = parsePositiveInt(
336
+ "--max-chars",
337
+ getFlag(ctx.args, "--max-chars"),
338
+ );
330
339
  const summary = getFlag(ctx.args, "--summary");
331
340
  const urls = ctx.args.slice();
332
341
  ctx.args.length = 0;
@@ -346,7 +355,7 @@ async function runContentsSubcommand(ctx: SearchCliContext): Promise<void> {
346
355
  highlights: { maxCharacters: 200 },
347
356
  };
348
357
  if (summary) body.summary = { query: summary };
349
- const payload = await callGateway("/v1/search/contents", body);
358
+ const payload = await callGateway(ctx, "/v1/search/contents", body);
350
359
  out(ctx, payload, formatContentsResults(payload));
351
360
  }
352
361
 
@@ -355,11 +364,9 @@ async function runAnswerSubcommand(ctx: SearchCliContext): Promise<void> {
355
364
  ctx.args.length = 0;
356
365
  const query = queryParts.join(" ").trim();
357
366
  if (!query) {
358
- throw new SearchCliError(
359
- 'Usage: dench search answer "<query>" [--json]',
360
- );
367
+ throw new SearchCliError('Usage: dench search answer "<query>" [--json]');
361
368
  }
362
- const payload = await callGateway("/v1/search/answer", {
369
+ const payload = await callGateway(ctx, "/v1/search/answer", {
363
370
  query,
364
371
  text: true,
365
372
  stream: false,
@@ -371,7 +378,8 @@ function searchHelp(): void {
371
378
  console.log(`Usage: dench search <subcommand>
372
379
 
373
380
  Live web access through the Dench Cloud Gateway (Exa Search). Auths
374
- with DENCH_API_KEY (baked into every sandbox automatically).
381
+ with DENCH_API_KEY or the active \`dench signin\` agent session. Agent
382
+ sessions require a paid workspace.
375
383
 
376
384
  Search the web (default subcommand):
377
385
  dench search "<query>" [--num-results N] [--type auto|fast|deep|deep-reasoning|neural]
@@ -386,7 +394,7 @@ One-shot synthesised answer with citations (Exa /search/answer):
386
394
  dench search answer "<query>" [--json]
387
395
 
388
396
  Environment variables:
389
- DENCH_API_KEY Required. Bearer token sent to the gateway.
397
+ DENCH_API_KEY Optional when a dench signin agent session is active.
390
398
  DENCH_GATEWAY_URL Override the gateway base URL.
391
399
  GATEWAY_URL Same, used when DENCH_GATEWAY_URL is unset.
392
400
  DENCH_HOST If it points at localhost, defaults to
package/tools.ts CHANGED
@@ -3,13 +3,9 @@
3
3
  * Composio's get-apps / get-tools / execute-tool surface.
4
4
  *
5
5
  * Mirrors `denchclaw`'s pattern: this CLI is a thin wrapper over five
6
- * gateway endpoints, authenticated with the unified `DENCH_API_KEY`
7
- * baked into every Dench sandbox at create time. There is no Convex
8
- * session involved; that means these subcommands work in any
9
- * environment that has the API key (sandboxes, CI, local dev with the
10
- * key exported manually). The previous implementation forced
11
- * `requireSessionRuntime` and routed through a Convex action, which
12
- * broke `apiKey` runtime mode entirely.
6
+ * gateway endpoints, authenticated with either `DENCH_API_KEY` or a
7
+ * paid-workspace gateway key resolved from the active agent session by
8
+ * the top-level dispatcher.
13
9
  *
14
10
  * Subcommands:
15
11
  *
@@ -20,7 +16,7 @@
20
16
  * dench tool connect <toolkit> [--callback-url <url>] [--json] — print OAuth redirect URL
21
17
  * dench tool disconnect <connectionId> [--json]
22
18
  *
23
- * Endpoints used (all `Authorization: Bearer DENCH_API_KEY`):
19
+ * Endpoints used (all `Authorization: Bearer <gateway credential>`):
24
20
  *
25
21
  * GET /v1/composio/connections
26
22
  * GET /v1/composio/toolkits?search=<slug>
@@ -36,10 +32,7 @@
36
32
  * working.
37
33
  */
38
34
 
39
- import {
40
- PRODUCTION_API_URL,
41
- PRODUCTION_GATEWAY_URL,
42
- } from "./host";
35
+ import { PRODUCTION_API_URL, PRODUCTION_GATEWAY_URL } from "./host";
43
36
  import {
44
37
  CliArgError,
45
38
  getFlag,
@@ -55,8 +48,15 @@ import {
55
48
  export type ToolCliContext = {
56
49
  args: string[];
57
50
  jsonOutput: boolean;
51
+ bearerToken?: string;
52
+ gatewayBaseUrl?: string;
58
53
  };
59
54
 
55
+ type GatewayAuthContext = Pick<
56
+ ToolCliContext,
57
+ "bearerToken" | "gatewayBaseUrl"
58
+ >;
59
+
60
60
  export async function runToolCommand(ctx: ToolCliContext): Promise<void> {
61
61
  const sub = ctx.args[0];
62
62
  if (!sub || sub === "help" || sub === "--help" || sub === "-h") {
@@ -91,11 +91,14 @@ export async function runToolCommand(ctx: ToolCliContext): Promise<void> {
91
91
 
92
92
  /**
93
93
  * Summarise the user's connected apps for `dench context` and friends.
94
- * Bypasses any Convex session and speaks straight to the gateway.
94
+ * Speaks straight to the gateway with either DENCH_API_KEY or a gateway
95
+ * key resolved from the active agent session.
95
96
  * Returns the same legacy shape the previous Convex-routed path did so
96
97
  * existing parsers don't need to change.
97
98
  */
98
- export async function fetchConnectedAppsSummary(): Promise<{
99
+ export async function fetchConnectedAppsSummary(
100
+ ctx: GatewayAuthContext = {},
101
+ ): Promise<{
99
102
  available: boolean;
100
103
  count: number;
101
104
  connections: Array<{
@@ -109,7 +112,7 @@ export async function fetchConnectedAppsSummary(): Promise<{
109
112
  }> {
110
113
  let payload: unknown;
111
114
  try {
112
- payload = await callGateway("GET", "/v1/composio/connections");
115
+ payload = await callGateway(ctx, "GET", "/v1/composio/connections");
113
116
  } catch (error) {
114
117
  if (error instanceof MissingApiKeyError) {
115
118
  return {
@@ -139,6 +142,7 @@ async function runStatusSubcommand(ctx: ToolCliContext): Promise<void> {
139
142
  if (filterToolkit) ctx.args.shift();
140
143
 
141
144
  const connectionsPayload = await callGateway(
145
+ ctx,
142
146
  "GET",
143
147
  "/v1/composio/connections",
144
148
  );
@@ -146,6 +150,7 @@ async function runStatusSubcommand(ctx: ToolCliContext): Promise<void> {
146
150
  if (filterToolkit) {
147
151
  const search = encodeURIComponent(filterToolkit);
148
152
  toolkitsPayload = await callGateway(
153
+ ctx,
149
154
  "GET",
150
155
  `/v1/composio/toolkits?search=${search}`,
151
156
  );
@@ -194,7 +199,12 @@ async function runSearchSubcommand(ctx: ToolCliContext): Promise<void> {
194
199
  if (toolkit) body.toolkit_slug = toolkit;
195
200
  if (limit) body.limit = limit;
196
201
 
197
- const payload = await callGateway("POST", "/v1/composio/tools/search", body);
202
+ const payload = await callGateway(
203
+ ctx,
204
+ "POST",
205
+ "/v1/composio/tools/search",
206
+ body,
207
+ );
198
208
  if (ctx.jsonOutput) {
199
209
  console.log(JSON.stringify(payload, null, 2));
200
210
  return;
@@ -231,7 +241,12 @@ async function runRunSubcommand(ctx: ToolCliContext): Promise<void> {
231
241
 
232
242
  let payload: unknown;
233
243
  try {
234
- payload = await callGateway("POST", "/v1/composio/tools/execute", body);
244
+ payload = await callGateway(
245
+ ctx,
246
+ "POST",
247
+ "/v1/composio/tools/execute",
248
+ body,
249
+ );
235
250
  } catch (error) {
236
251
  if (error instanceof GatewayHttpError && isLikelyNoConnection(error)) {
237
252
  const noConnPayload = noConnectionPayload(toolSlug, error);
@@ -266,7 +281,7 @@ async function runConnectSubcommand(ctx: ToolCliContext): Promise<void> {
266
281
  process.env.DENCH_OAUTH_CALLBACK_URL?.trim() ??
267
282
  `${resolveAppPublicOrigin()}/api/composio/callback`;
268
283
 
269
- const payload = await callGateway("POST", "/v1/composio/connect", {
284
+ const payload = await callGateway(ctx, "POST", "/v1/composio/connect", {
270
285
  toolkit,
271
286
  callback_url: callbackUrl,
272
287
  });
@@ -313,7 +328,7 @@ async function runDisconnectSubcommand(ctx: ToolCliContext): Promise<void> {
313
328
  const path = `/v1/composio/connections/${encodeURIComponent(connectionId)}`;
314
329
  let payload: unknown = { ok: true, deleted: true };
315
330
  try {
316
- payload = await callGateway("DELETE", path);
331
+ payload = await callGateway(ctx, "DELETE", path);
317
332
  } catch (error) {
318
333
  // Composio returns 404 for connections that are already gone — surface
319
334
  // that as a soft success so callers can keep their local state in sync.
@@ -350,9 +365,8 @@ class ToolCliError extends Error {
350
365
  class MissingApiKeyError extends ToolCliError {
351
366
  constructor() {
352
367
  super(
353
- "DENCH_API_KEY is not set. Inside a Dench sandbox it is baked into " +
354
- "the env automatically; outside, export it manually or run " +
355
- "`dench login` and copy the api-key from the output.",
368
+ "No Dench gateway credential found. Set DENCH_API_KEY or run " +
369
+ "`dench signin` for a paid workspace.",
356
370
  );
357
371
  this.name = "MissingApiKeyError";
358
372
  }
@@ -376,10 +390,19 @@ class GatewayHttpError extends ToolCliError {
376
390
  }
377
391
  }
378
392
 
379
- function resolveGatewayBaseUrl(): string {
393
+ function normalizeGatewayBaseUrl(value: string): string {
394
+ return value.replace(/\/+$/, "").replace(/\/v1$/, "");
395
+ }
396
+
397
+ function resolveGatewayBaseUrl(
398
+ ctx?: Pick<ToolCliContext, "gatewayBaseUrl">,
399
+ ): string {
400
+ if (ctx?.gatewayBaseUrl?.trim()) {
401
+ return normalizeGatewayBaseUrl(ctx.gatewayBaseUrl.trim());
402
+ }
380
403
  const explicit =
381
404
  process.env.DENCH_GATEWAY_URL?.trim() || process.env.GATEWAY_URL?.trim();
382
- if (explicit) return explicit.replace(/\/+$/, "");
405
+ if (explicit) return normalizeGatewayBaseUrl(explicit);
383
406
  const host = process.env.DENCH_HOST?.trim();
384
407
  if (host && /localhost|127\.0\.0\.1/.test(host)) {
385
408
  return "http://localhost:8787";
@@ -410,24 +433,26 @@ function resolveAppPublicOrigin(): string {
410
433
  return PRODUCTION_API_URL;
411
434
  }
412
435
 
413
- function requireApiKey(): string {
414
- const apiKey = process.env.DENCH_API_KEY?.trim();
415
- if (!apiKey) throw new MissingApiKeyError();
416
- return apiKey;
436
+ function requireBearerToken(ctx?: Pick<ToolCliContext, "bearerToken">): string {
437
+ const bearerToken =
438
+ ctx?.bearerToken?.trim() || process.env.DENCH_API_KEY?.trim();
439
+ if (!bearerToken) throw new MissingApiKeyError();
440
+ return bearerToken;
417
441
  }
418
442
 
419
443
  async function callGateway(
444
+ ctx: GatewayAuthContext,
420
445
  method: "GET" | "POST" | "DELETE",
421
446
  path: string,
422
447
  body?: Record<string, unknown>,
423
448
  ): Promise<unknown> {
424
- const apiKey = requireApiKey();
425
- const baseUrl = resolveGatewayBaseUrl();
449
+ const bearerToken = requireBearerToken(ctx);
450
+ const baseUrl = resolveGatewayBaseUrl(ctx);
426
451
  const init: RequestInit = {
427
452
  method,
428
453
  headers: {
429
454
  "content-type": "application/json",
430
- authorization: `Bearer ${apiKey}`,
455
+ authorization: `Bearer ${bearerToken}`,
431
456
  },
432
457
  };
433
458
  if (body !== undefined) init.body = JSON.stringify(body);
@@ -730,8 +755,8 @@ Usage:
730
755
  dench tool disconnect <connectionId> [--json]
731
756
 
732
757
  Notes:
733
- All commands authenticate with DENCH_API_KEY (Bearer) directly to the
734
- Dench Cloud Gateway. No \`dench login\` agent session required.
758
+ All commands authenticate with DENCH_API_KEY or the active \`dench signin\`
759
+ agent session. Agent sessions require a paid workspace.
735
760
  dench tool connect prints the OAuth redirect URL for the human to open.
736
761
  dench tool run output is redacted for display; pass --json for raw output.
737
762
  Override the gateway base with DENCH_GATEWAY_URL or GATEWAY_URL.
@@ -804,7 +829,7 @@ function parsePositiveInt(name: string, raw: string): number {
804
829
  // them as unused when callers tree-shake.
805
830
  export { ToolCliError, GatewayHttpError, MissingApiKeyError };
806
831
  // Kept reachable so a future caller can build their own dispatcher.
807
- export { resolveGatewayBaseUrl, requireApiKey, callGateway };
832
+ export { resolveGatewayBaseUrl, requireBearerToken, callGateway };
808
833
  // shiftRaw / hasFlag from cli-args are not used in this module yet but
809
834
  // keep the import group consistent with cli/search.ts conventions.
810
835
  void shiftRaw;