Elffuss BraidHub

kikocis/continue public

refactor and add safer parsing to error messages
Changeset6072b6346135d2eb4aad426fbe754231d117e66b9bf140767dcb7fc15be6155d
AuthorNate <sestinj@gmail.com> · 2025-07-02 22:14 -07:00
CommitterNate <sestinj@gmail.com> · 2025-07-02 22:14 -07:00
Parents5810b3f3bf39

Browse the files at this changeset

1 file changed

modified core/llm/index.ts +48 −38
@@ -7,6 +7,7 @@
77 constructLlmApi,
88 } from "@continuedev/openai-adapters";
99 import Handlebars from "handlebars";
10+import { Response } from "node-fetch";
1011
1112 import { DevDataSqliteDb } from "../data/devdataSqlite.js";
1213 import { DataLogger } from "../data/log.js";
@@ -384,6 +385,50 @@
384385 }
385386 }
386387
388+ private async parseError(resp: Response): Promise<Error> {
389+ let text = await resp.text();
390+
391+ if (resp.status === 404 && !resp.url.includes("/v1")) {
392+ const parsedError = JSON.parse(text);
393+ const errorMessageRaw = parsedError?.error ?? parsedError?.message;
394+ const error =
395+ typeof errorMessageRaw === "string"
396+ ? errorMessageRaw.replace(/"/g, "'")
397+ : undefined;
398+ let model = error?.match(/model '(.*)' not found/)?.[1];
399+ if (model && resp.url.match("127.0.0.1:11434")) {
400+ text = `The model "${model}" was not found. To download it, run \`ollama run ${model}\`.`;
401+ return new LLMError(text, this); // No need to add HTTP status details
402+ } else if (text.includes("/api/chat")) {
403+ text =
404+ "The /api/chat endpoint was not found. This may mean that you are using an older version of Ollama that does not support /api/chat. Upgrading to the latest version will solve the issue.";
405+ } else {
406+ text =
407+ "This may mean that you forgot to add '/v1' to the end of your 'apiBase' in config.json.";
408+ }
409+ } else if (resp.status === 404 && resp.url.includes("api.openai.com")) {
410+ text =
411+ "You may need to add pre-paid credits before using the OpenAI API.";
412+ } else if (
413+ resp.status === 401 &&
414+ (resp.url.includes("api.mistral.ai") ||
415+ resp.url.includes("codestral.mistral.ai"))
416+ ) {
417+ if (resp.url.includes("codestral.mistral.ai")) {
418+ return new Error(
419+ "You are using a Mistral API key, which is not compatible with the Codestral API. Please either obtain a Codestral API key, or use the Mistral API by setting 'apiBase' to 'https://api.mistral.ai/v1' in config.json.",
420+ );
421+ } else {
422+ return new Error(
423+ "You are using a Codestral API key, which is not compatible with the Mistral API. Please either obtain a Mistral API key, or use the the Codestral API by setting 'apiBase' to 'https://codestral.mistral.ai/v1' in config.json.",
424+ );
425+ }
426+ }
427+ return new Error(
428+ `HTTP ${resp.status} ${resp.statusText} from ${resp.url}\n\n${text}`,
429+ );
430+ }
431+
387432 fetch(url: RequestInfo | URL, init?: RequestInit): Promise<Response> {
388433 // Custom Node.js fetch
389434 const customFetch = async (input: URL | RequestInfo, init: any) => {
@@ -399,44 +444,9 @@
399444 if (resp.status === 499) {
400445 return resp; // client side cancellation
401446 }
402- let text = await resp.text();
403- if (resp.status === 404 && !resp.url.includes("/v1")) {
404- const error = JSON.parse(text)?.error?.replace(/"/g, "'");
405- let model = error?.match(/model '(.*)' not found/)?.[1];
406- if (model && resp.url.match("127.0.0.1:11434")) {
407- text = `The model "${model}" was not found. To download it, run \`ollama run ${model}\`.`;
408- throw new LLMError(text, this); // No need to add HTTP status details
409- } else if (text.includes("/api/chat")) {
410- text =
411- "The /api/chat endpoint was not found. This may mean that you are using an older version of Ollama that does not support /api/chat. Upgrading to the latest version will solve the issue.";
412- } else {
413- text =
414- "This may mean that you forgot to add '/v1' to the end of your 'apiBase' in config.json.";
415- }
416- } else if (
417- resp.status === 404 &&
418- resp.url.includes("api.openai.com")
419- ) {
420- text =
421- "You may need to add pre-paid credits before using the OpenAI API.";
422- } else if (
423- resp.status === 401 &&
424- (resp.url.includes("api.mistral.ai") ||
425- resp.url.includes("codestral.mistral.ai"))
426- ) {
427- if (resp.url.includes("codestral.mistral.ai")) {
428- throw new Error(
429- "You are using a Mistral API key, which is not compatible with the Codestral API. Please either obtain a Codestral API key, or use the Mistral API by setting 'apiBase' to 'https://api.mistral.ai/v1' in config.json.",
430- );
431- } else {
432- throw new Error(
433- "You are using a Codestral API key, which is not compatible with the Mistral API. Please either obtain a Mistral API key, or use the the Codestral API by setting 'apiBase' to 'https://codestral.mistral.ai/v1' in config.json.",
434- );
435- }
436- }
437- throw new Error(
438- `HTTP ${resp.status} ${resp.statusText} from ${resp.url}\n\n${text}`,
439- );
447+
448+ const error = await this.parseError(resp);
449+ throw error;
440450 }
441451
442452 return resp;