Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

15.1 KB
Raw Blame
import {
  BaseExecutor,
  mergeUpstreamExtraHeaders,
  setUserAgentHeader,
  type ExecuteInput,
  type ProviderCredentials,
} from "./base.ts";
import { PROVIDERS } from "../config/constants.ts";
import {
  getQoderDashscopeCompatHeaders,
  QODER_DEFAULT_USER_AGENT,
} from "../config/providerHeaderProfiles.ts";
import { randomUUID } from "node:crypto";
import { sanitizeQwenThinkingToolChoice } from "../services/qwenThinking.ts";
import {
  buildQoderChunk,
  buildQoderCompletionPayload,
  buildQoderPrompt,
  createQoderErrorResponse,
  isQoderUpstreamBusy,
  parseQoderCliFailure,
  parseQoderRetryAfterSeconds,
  parseQoderCliResult,
  runQoderCli,
} from "../services/qoderCli.ts";
import { sanitizeErrorMessage } from "../utils/error.ts";
import { hasQoderCallerTools, resolveQoderAuthToken } from "../services/qoderCapabilities.ts";

/** Retry hint sent with a 429 `qodercli_busy` (local queue full). */
const QODER_CLI_BUSY_RETRY_AFTER_SECONDS = 5;

function truncate(text: string, max: number): string {
  if (text.length <= max) return text;
  return `${text.slice(0, max)}…`;
}

/**
 * Wrap a full qodercli reply as an OpenAI-compatible SSE stream (role chunk →
 * content chunk → stop chunk → [DONE]). qodercli's `--print` mode returns the
 * whole answer at once, so there are no incremental deltas to forward.
 */
function buildQoderCliSseStream(model: string, text: string): ReadableStream<Uint8Array> {
  const id = `chatcmpl-${randomUUID()}`;
  const created = Math.floor(Date.now() / 1000);
  const encoder = new TextEncoder();
  const send = (obj: unknown) => encoder.encode(`data: ${JSON.stringify(obj)}\n\n`);
  return new ReadableStream<Uint8Array>({
    start(controller) {
      controller.enqueue(
        send(buildQoderChunk({ id, model, created, delta: { role: "assistant", content: "" } }))
      );
      if (text) {
        controller.enqueue(send(buildQoderChunk({ id, model, created, delta: { content: text } })));
      }
      controller.enqueue(
        send(buildQoderChunk({ id, model, created, delta: {}, finishReason: "stop" }))
      );
      controller.enqueue(encoder.encode("data: [DONE]\n\n"));
      controller.close();
    },
  });
}

/**
 * Peek at the first SSE event from a Qoder response to detect upstream errors
 * that Qoder wraps inside an HTTP 200 SSE envelope ({statusCodeValue, body}).
 * Returns a proper HTTP error Response when found, so downstream fallback
 * logic (combo routing, account fallback) can trigger. For success, re-creates
 * the stream with the first chunk prepended so the body passes through
 * transparently.
 */
async function unwrapQoderEnvelope(response: Response): Promise<Response> {
  if (!response.ok || !response.body) {
    return response;
  }

  const reader = response.body.getReader();
  const decoder = new TextDecoder();
  const peekedChunks: Uint8Array[] = [];
  let peekedText = "";
  let peekedBytes = 0;
  let reachedEnd = false;

  while (peekedBytes < 64 * 1024) {
    const { done, value } = await reader.read();
    if (done) {
      reachedEnd = true;
      peekedText += decoder.decode();
      break;
    }

    peekedChunks.push(value);
    peekedBytes += value.byteLength;
    peekedText += decoder.decode(value, { stream: true });

    if (peekedText.includes("\n\n") || peekedText.includes("\r\n\r\n")) {
      break;
    }
  }

  if (peekedChunks.length === 0 && reachedEnd) {
    reader.cancel();
    return new Response(
      JSON.stringify({ error: { message: "[qoder] empty response", type: "provider_error" } }),
      { status: 502, headers: { "Content-Type": "application/json" } }
    );
  }

  let errorStatus: number | null = null;
  let errorMsg = "";
  for (const line of peekedText.split("\n")) {
    const trimmed = line.trim();
    if (!trimmed.startsWith("data:")) continue;
    const jsonStr = trimmed.slice(5).trim();
    if (jsonStr === "[DONE]") break;
    try {
      const envelope = JSON.parse(jsonStr) as Record<string, unknown>;
      const statusVal =
        typeof envelope.statusCodeValue === "number" ? envelope.statusCodeValue : 200;
      if (statusVal !== 200) {
        errorStatus = statusVal >= 400 ? statusVal : 502;
        errorMsg =
          typeof envelope.body === "string" ? envelope.body : `upstream status ${statusVal}`;
      }
    } catch {
      // Malformed JSON — treat as non-error; downstream handling parses it.
    }
    break;
  }

  if (errorStatus) {
    reader.cancel();
    // #3510: a 403 envelope carrying the nested code 10605 is Qoder's temporary
    // queue throttle, not an auth failure. Surface it as 503 upstream_busy (same
    // classification as the CLI path) so healthy accounts are not parked on the
    // auth cooldown.
    if (isQoderUpstreamBusy(errorMsg)) {
      return createQoderErrorResponse({
        status: 503,
        message: `[qoder error ${errorStatus}: ${sanitizeErrorMessage(truncate(errorMsg, 200))}]`,
        code: "upstream_busy",
        retryAfterSeconds: parseQoderRetryAfterSeconds(errorMsg),
      });
    }
    const errType =
      errorStatus === 401 || errorStatus === 403 ? "authentication_error" : "provider_error";
    return new Response(
      JSON.stringify({
        error: {
          message: `[qoder error ${errorStatus}: ${sanitizeErrorMessage(truncate(errorMsg, 200))}]`,
          type: errType,
        },
      }),
      { status: errorStatus, headers: { "Content-Type": "application/json" } }
    );
  }

  // Re-create the stream with every peeked chunk prepended so the success body
  // passes through byte-for-byte unchanged.
  const restStream = new ReadableStream<Uint8Array>({
    start(controller) {
      for (const chunk of peekedChunks) {
        controller.enqueue(chunk);
      }
    },
    pull(controller) {
      return reader.read().then(({ done, value }) => {
        if (done) {
          controller.close();
          return;
        }
        controller.enqueue(value);
      });
    },
    cancel() {
      reader.cancel();
    },
  });

  return new Response(restStream, {
    status: response.status,
    statusText: response.statusText,
    headers: new Headers(response.headers),
  });
}

export class QoderExecutor extends BaseExecutor {
  constructor() {
    super("qoder", PROVIDERS.qoder);
  }

  buildHeaders(
    credentials: ProviderCredentials,
    stream = true,
    clientHeaders?: Record<string, string> | null,
    model?: string
  ): Record<string, string> {
    const headers = super.buildHeaders(credentials, stream, clientHeaders, model);
    setUserAgentHeader(headers, QODER_DEFAULT_USER_AGENT);
    return headers;
  }

  transformRequest(model: string, body: unknown): Record<string, unknown> {
    const payload = {
      ...(typeof body === "object" && body !== null ? body : {}),
      model,
    };

    return sanitizeQwenThinkingToolChoice(payload, "QoderExecutor");
  }

  async execute({ model, body, stream, credentials, signal, upstreamExtraHeaders }: ExecuteInput) {
    const token = resolveQoderAuthToken(credentials);

    if (!token) {
      return {
        response: new Response(
          JSON.stringify({
            error: {
              message: "Qoder access token or API Key is required. Please sign in or set a PAT.",
              type: "authentication_error",
              code: "token_required",
            },
          }),
          { status: 401, headers: { "Content-Type": "application/json" } }
        ),
        url: "https://dashscope.aliyuncs.com",
        headers: { "Content-Type": "application/json" },
        transformedBody: body,
      };
    }

    const resolvedModel = model || "qwen3.8-max-preview";

    // Detect token type: PAT (Personal Access Token) starts with "pt-".
    // PATs are driven through the local qodercli binary (see executeViaQoderCli);
    // only the qodercli binary can produce the WASM-signed Cosy request the raw
    // HTTP path can no longer replicate.
    const isPatToken = token.startsWith("pt-");
    if (isPatToken) {
      if (hasQoderCallerTools(body)) {
        return {
          response: createQoderErrorResponse({
            status: 400,
            message:
              "Qoder PAT/CLI connections do not support caller tool calling. Use a compatible HTTP connection or another provider for agent requests.",
            code: "tool_calling_unsupported",
          }),
          url: "qodercli://stdio",
          headers: {},
          transformedBody: body,
        };
      }
      return this.executeViaQoderCli({ model: resolvedModel, body, stream, token, signal });
    }

    // Non-PAT tokens (OAuth apiKey / DashScope key) → DashScope OpenAI-compatible API.
    let mappedModel = resolvedModel;
    if (resolvedModel === "qwen3.5-plus" || resolvedModel === "qwen3.6-plus") {
      mappedModel = "coder-model";
    } else if (resolvedModel === "vision-model") {
      mappedModel = "qwen3-vl-plus";
    }
    let endpointUrl = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";

    // Check for custom API base via credentials (overrides the default)
    let credentialsApiBase: unknown;
    if (typeof credentials === "object" && credentials !== null) {
      const credsObj = credentials as Record<string, unknown>;
      credentialsApiBase = credsObj.customApiBase || credsObj.resourceUrl;
    }
    if (typeof credentialsApiBase === "string" && credentialsApiBase.trim()) {
      let base = credentialsApiBase.trim();
      if (!base.startsWith("http")) base = `https://${base}`;
      if (!base.endsWith("/v1")) base = base.endsWith("/") ? `${base}v1` : `${base}/v1`;
      endpointUrl = `${base}/chat/completions`;
    }

    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      Authorization: `Bearer ${token}`,
      ...getQoderDashscopeCompatHeaders(),
    };

    mergeUpstreamExtraHeaders(headers, upstreamExtraHeaders);

    const payload = this.transformRequest(mappedModel, body);

    const bodyStr = JSON.stringify(payload);

    try {
      const response = await fetch(endpointUrl, {
        method: "POST",
        headers,
        body: bodyStr,
        signal,
      });

      if (!response.ok) {
        let errText = await response.text();
        return {
          response: new Response(
            JSON.stringify({
              error: {
                message: `Qoder API failed with status ${response.status}: ${errText}`,
                type: response.status === 401 ? "authentication_error" : "provider_error",
              },
            }),
            { status: response.status, headers: { "Content-Type": "application/json" } }
          ),
          url: endpointUrl,
          headers,
          transformedBody: payload,
        };
      }

      // Qoder wraps upstream errors inside an HTTP 200 SSE envelope
      // ({statusCodeValue}). Peek at the first event to detect this and return
      // a proper HTTP error so combo/account fallback logic can trigger.
      const unwrapped = await unwrapQoderEnvelope(response);
      return {
        response: unwrapped,
        url: endpointUrl,
        headers,
        transformedBody: payload,
      };
    } catch (e: unknown) {
      const error = e as Error;
      if (error.name === "AbortError") {
        throw error;
      }
      return {
        response: new Response(
          JSON.stringify({
            error: {
              message: `Qoder fetch error: ${sanitizeErrorMessage(error.message)}`,
              type: "provider_error",
            },
          }),
          { status: 502, headers: { "Content-Type": "application/json" } }
        ),
        url: endpointUrl,
        headers,
        transformedBody: payload,
      };
    }
  }

  /**
   * Drive a PAT (`pt-*`) completion through the local qodercli binary. The CLI
   * performs Qoder's WASM-signed Cosy auth internally, so this is the only path
   * that works for PATs now that the pure-HTTP Cosy reimplementation is dead.
   */
  private async executeViaQoderCli({
    model,
    body,
    stream,
    token,
    signal,
  }: {
    model: string;
    body: unknown;
    stream: boolean;
    token: string;
    signal?: AbortSignal | null;
  }): Promise<{
    response: Response;
    url: string;
    headers: Record<string, string>;
    transformedBody: unknown;
  }> {
    const url = "qodercli://stdio";
    const prompt = buildQoderPrompt(body);

    const run = await runQoderCli({ token, prompt, stream: false, model, signal });

    // Honor client cancellation the same way the HTTP path does.
    if (signal?.aborted) {
      const abortError = new Error("Aborted");
      abortError.name = "AbortError";
      throw abortError;
    }

    if (run.busy) {
      // The local qodercli queue is full — a request-scoped condition, not an
      // upstream failure: 429 + Retry-After, never a 5xx that would count
      // against the provider circuit breaker.
      return {
        response: createQoderErrorResponse({
          status: 429,
          message: sanitizeErrorMessage(run.error || "qodercli is busy"),
          code: "qodercli_busy",
          retryAfterSeconds: QODER_CLI_BUSY_RETRY_AFTER_SECONDS,
        }),
        url,
        headers: {},
        transformedBody: body,
      };
    }

    if (run.error && /enoent|not found|no such file|spawn/i.test(run.error)) {
      return {
        response: createQoderErrorResponse({
          status: 502,
          message:
            `Qoder CLI (qodercli) was not found on the OmniRoute host (${run.error}). ` +
            "Install it from https://qoder.com or set CLI_QODER_BIN to its path.",
          code: "cli_not_found",
        }),
        url,
        headers: {},
        transformedBody: body,
      };
    }

    if (!run.ok) {
      return {
        response: createQoderErrorResponse(parseQoderCliFailure(run.stderr, run.stdout)),
        url,
        headers: {},
        transformedBody: body,
      };
    }

    const { text, isError, errorMessage } = parseQoderCliResult(run.stdout);
    if (isError) {
      // When qodercli exits 0 but returns is_error=true with an empty result,
      // the real upstream error is almost always on stderr. Surface it instead
      // of the generic "qodercli returned an error" fallback (#9319).
      let effectiveError = errorMessage;
      if (errorMessage === "qodercli returned an error" && run.stderr.trim()) {
        const stderrTrimmed = run.stderr.trim().slice(0, 300);
        effectiveError = `qodercli returned an error: ${stderrTrimmed}`;
      }
      return {
        response: createQoderErrorResponse(parseQoderCliFailure(effectiveError)),
        url,
        headers: {},
        transformedBody: body,
      };
    }

    if (stream) {
      return {
        response: new Response(buildQoderCliSseStream(model, text), {
          status: 200,
          headers: {
            "Content-Type": "text/event-stream",
            "Cache-Control": "no-cache",
            Connection: "keep-alive",
          },
        }),
        url,
        headers: {},
        transformedBody: body,
      };
    }

    return {
      response: new Response(JSON.stringify(buildQoderCompletionPayload({ model, text })), {
        status: 200,
        headers: { "Content-Type": "application/json" },
      }),
      url,
      headers: {},
      transformedBody: body,
    };
  }
}

export default QoderExecutor;

export const __test__ = {
  unwrapQoderEnvelope,
  truncate,
};