Find code by asking what it does. A CLI for coding agents that uses Jev to discover relevant files and source context.
TypeScript ai-sdkclaude-codeclicode-searchcodexcoding-agentscontext-retrievaldeveloper-toolsjevsemantic-searchtypescriptvercel-ai-gateway
fix: show provider rejection details in doctor (#8)
David Zhangfa1bc0554979
diff --git a/apps/cli/README.md b/apps/cli/README.mdindex 5c8e57f..6f371e5 100644--- a/apps/cli/README.md+++ b/apps/cli/README.md@@ -16,6 +16,11 @@ jg "How are telemetry events recorded and sent?" ./my-project`auth` asks you to choose a provider, then saves its key in an owner-only config file.Run `jg --help` for the supported provider names. Searches use that provider until you run auth again.`doctor` verifies access with synthetic input and names the selected provider.+When the provider rejects the request, doctor reports the HTTP status and its+error message, when available. A working key can still lack model access or paid+credits; follow the provider’s explanation before replacing the key. Saved keys+and raw response bodies are excluded from diagnostic output. If a provider echoes+a key with inserted separators, its message is omitted.For unattended setup, pipe the key from your secret manager to:```shdiff --git a/apps/cli/package.json b/apps/cli/package.jsonindex 67e85ab..676dcd7 100644--- a/apps/cli/package.json+++ b/apps/cli/package.json@@ -1,6 +1,6 @@{"name": "@dzhng/jevgrep",- "version": "0.3.0",+ "version": "0.3.1","description": "Content-aware source retrieval for coding agents","license": "MIT","repository": {diff --git a/apps/cli/src/errors.ts b/apps/cli/src/errors.tsindex 1933573..9d89b30 100644--- a/apps/cli/src/errors.ts+++ b/apps/cli/src/errors.ts@@ -1,2 +1,2 @@-/** Only explicitly authored messages are safe to print; SDK/OS errors can contain secrets. */+/** Only authored messages and sanitized provider diagnostics are safe to print; raw SDK/OS errors can contain secrets. */export class CliError extends Error {}diff --git a/apps/cli/src/index.ts b/apps/cli/src/index.tsindex 0393439..87a6a57 100644--- a/apps/cli/src/index.ts+++ b/apps/cli/src/index.ts@@ -58,7 +58,7 @@ async function main() {}case "doctor": {const credentials = await loadCredentials();- const { createEvaluator } = await import("@repo/core");+ const { createEvaluator, EvaluationFailure } = await import("@repo/core");const evaluator = createEvaluator({...credentials,signal: controller.signal,@@ -80,6 +80,15 @@ async function main() {await write(`Jev connection verified through ${providers[credentials.provider].label}.\n`);} catch (error) {if (error instanceof CliError) throw error;+ if (error instanceof EvaluationFailure && error.diagnostic) {+ const { statusCode, message } = error.diagnostic;+ throw new CliError(+ `Jev connection check failed through ${providers[credentials.provider].label} (HTTP ${statusCode}).\n` ++ (message+ ? `Provider: ${message}`+ : "Check your saved key, model access, and provider billing."),+ );+ }throw new CliError(`Jev connection check failed through ${providers[credentials.provider].label}. Check your saved key, model access, and network.`,);diff --git a/bun.lock b/bun.lockindex 4aafe7d..428061f 100644--- a/bun.lock+++ b/bun.lock@@ -13,7 +13,7 @@},"apps/cli": {"name": "@dzhng/jevgrep",- "version": "0.3.0",+ "version": "0.3.1","bin": {"jg": "./dist/bin/index.js",},diff --git a/packages/core/src/evaluator.ts b/packages/core/src/evaluator.tsindex ff4475b..5545f22 100644--- a/packages/core/src/evaluator.ts+++ b/packages/core/src/evaluator.ts@@ -3,6 +3,7 @@ import { createTypeSafeAi } from "@ai-sdk/typesafe-ai";import { providers, type ProviderId } from "./providers";import { type createEvaluationCache, type CacheInput } from "./cache";import { setTimeout as delay } from "node:timers/promises";+import { stripVTControlCharacters } from "node:util";export type EvaluationRequest = {state: Parameters<typeof evaluate>[0]["state"];@@ -18,6 +19,7 @@ export class EvaluationFailure extends Error {| "cancelled"| "source-invalid",public readonly splitEligible = false,+ public readonly diagnostic?: { statusCode: number; message?: string },) {super(`Jev evaluation failed: ${kind}`);this.name = "EvaluationFailure";@@ -160,7 +162,11 @@ export function createEvaluator(options: {: undefined;if (status === 401 || status === 403) {authenticationFailure.abort();- throw new EvaluationFailure("authentication");+ throw new EvaluationFailure(+ "authentication",+ false,+ providerDiagnostic(error, options.apiKey),+ );}if (requests >= (options.requestLimit ?? 50_000))throw new EvaluationFailure("request-limit");@@ -176,6 +182,7 @@ export function createEvaluator(options: {throw new EvaluationFailure("provider",navigation && multiple && transient && status !== 429,+ providerDiagnostic(error, options.apiKey),);}}@@ -183,3 +190,31 @@ export function createEvaluator(options: {},};}++// Only explicit text fields from a parsed provider error are suitable for doctor output.+// SDK messages can instead serialize arbitrary detail objects, requests, or response bodies.+function providerDiagnostic(error: unknown, apiKey: string): EvaluationFailure["diagnostic"] {+ if (!APICallError.isInstance(error) || error.statusCode === undefined) return undefined;+ const data: unknown = error.data;+ let message: unknown;+ if (data && typeof data === "object") {+ const nested = "error" in data ? data.error : undefined;+ message =+ ("message" in data && typeof data.message === "string" ? data.message : undefined) ??+ (typeof nested === "string"+ ? nested+ : nested && typeof nested === "object" && "message" in nested+ ? nested.message+ : undefined) ??+ ("detail" in data && typeof data.detail === "string" ? data.detail : undefined);+ }+ if (typeof message !== "string") return { statusCode: error.statusCode };+ const safe = stripVTControlCharacters(message)+ .replace(/[\p{Cc}\p{Cf}]+/gu, " ")+ .trim();+ const redacted = apiKey ? safe.replaceAll(apiKey, "[redacted]") : safe;+ // A provider may split a credential with whitespace or invisible separators.+ if (apiKey && redacted.replace(/\s/gu, "").includes(apiKey))+ return { statusCode: error.statusCode };+ return { statusCode: error.statusCode, message: redacted.slice(0, 500) || undefined };+}diff --git a/test/installed.test.mjs b/test/installed.test.mjsindex 4ffa638..80e2189 100644--- a/test/installed.test.mjs+++ b/test/installed.test.mjs@@ -574,6 +574,78 @@ test("doctor uses the installed SDK while missing credentials fail cleanly", asyassert.equal(fixture.requests.length, before);});+test("doctor explains provider access restrictions without exposing credentials or metadata", async (t) => {+ const fixture = await context(t, ({ response }) => {+ response.writeHead(403, { "content-type": "application/json" });+ response.end(+ JSON.stringify({+ error: {+ message: `Free tier users do not have access to this model. Upgrade to paid credits. Token: ${fixtureKey} ${fixtureKey.slice(0, 10)}\u001b[31m${fixtureKey.slice(10)}\n\u001b[0m`,+ type: "no_providers_available",+ param: { secret: fixtureKey },+ },+ providerMetadata: { private: "DO_NOT_PRINT_PROVIDER_METADATA" },+ }),+ );+ return true;+ });+ const result = await fixture.run(["doctor"]);+ assert.equal(result.code, 1);+ assert.match(result.stdout, /Vercel AI Gateway.*HTTP 403/);+ assert.match(+ result.stdout,+ /Free tier users do not have access to this model\. Upgrade to paid credits\./,+ );+ assert.ok(!result.stdout.includes("DO_NOT_PRINT_PROVIDER_METADATA"));+ assert.ok(!result.stdout.includes("\u001b"));+ assert.equal(result.stdout.trim().split("\n").length, 2);+ assert.equal(fixture.requests.length, 1);+});++test("doctor omits messages containing keys split by separators", async (t) => {+ const fixture = await context(t);+ const first = fixtureKey.slice(0, 12);+ const second = fixtureKey.slice(12);+ for (const separator of ["\n", "\t", "\u200b", " ", "\u2028"]) {+ fixture.mode = ({ response }) => {+ response.writeHead(403, { "content-type": "application/json" });+ response.end(+ JSON.stringify({ error: { message: `Rejected token ${first}${separator}${second}` } }),+ );+ return true;+ };+ const result = await fixture.run(["doctor"]);+ assert.equal(result.code, 1);+ assert.match(result.stdout, /HTTP 403/);+ assert.match(result.stdout, /Check your saved key, model access, and provider billing/);+ assert.ok(!result.stdout.includes(first));+ assert.ok(!result.stdout.includes(second));+ assert.ok(!result.stdout.includes("Rejected token"));+ }+});++test("doctor reports HTTP failures without printing raw response bodies", async (t) => {+ const fixture = await context(t);+ for (const body of [+ `<html>${fixtureKey} PRIVATE_RESPONSE_BODY</html>`,+ JSON.stringify({ detail: { secret: fixtureKey, private: "PRIVATE_RESPONSE_BODY" } }),+ "",+ ]) {+ fixture.mode = ({ response }) => {+ response.writeHead(502, { "content-type": "application/json" });+ response.end(body);+ return true;+ };+ const before = fixture.requests.length;+ const result = await fixture.run(["doctor"]);+ assert.equal(result.code, 1);+ assert.match(result.stdout, /Vercel AI Gateway.*HTTP 502/);+ assert.match(result.stdout, /Check your saved key, model access, and provider billing/);+ assert.ok(!result.stdout.includes("PRIVATE_RESPONSE_BODY"));+ assert.equal(fixture.requests.length - before, 2);+ }+});+test("provider authentication failure stops immediately with fatal exit 1", async (t) => {const fixture = await context(t, "unauthorized");const result = await fixture.run([query]);