fix: Enhance CometAPI construction with model provider context length and relaxed API key validation

This commit is contained in:
TensorNull 2025-09-18 09:46:39 +08:00
parent 712d1241fb
commit b228b5feab

View file

@ -1,4 +1,5 @@
import { LLMOptions } from "../../index.js";
import { allModelProviders } from "@continuedev/llm-info";
import OpenAI from "./OpenAI.js";
/**
@ -47,29 +48,37 @@ class CometAPI extends OpenAI {
// Validate required configuration before calling super
CometAPI.validateConfig(options);
super(options);
// Align contextLength with llm-info for cometapi specifically (non-breaking for others)
try {
const cometProvider = allModelProviders.find((p) => p.id === "cometapi");
const info = cometProvider?.models.find((m) =>
m.regex ? m.regex.test(this.model) : m.model === this.model,
);
if (info?.contextLength) {
// Always prefer cometapi-specific llm-info over generic provider matches
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - accessing protected for targeted fix
this._contextLength = info.contextLength;
}
} catch {
// no-op: do not fail construction on metadata issues
}
}
/**
* Validate CometAPI configuration
*/
private static validateConfig(options: LLMOptions): void {
// Allow constructing without API key (tests that only instantiate should pass).
// Enforce credentials at request time instead.
if (!options.apiKey) {
throw new CometAPIAuthenticationError(
"CometAPI requires an API key. Please get one from https://cometapi.com/api-keys",
);
}
// Only warn in non-test environments
if (
!options.apiKey.startsWith("sk-comet-") &&
!options.apiKey.startsWith("sk-") &&
typeof process !== "undefined" &&
process.env?.NODE_ENV !== "test" &&
!options.apiKey.includes("test")
) {
console.warn(
"CometAPI: API key format may be incorrect. Expected format: sk-comet-...",
);
if (typeof process !== "undefined" && process.env?.NODE_ENV !== "test") {
console.warn(
"CometAPI: No API key provided. Requests will fail until an API key is configured. Get one at https://api.cometapi.com/console/token",
);
}
return;
}
if (options.apiBase && !CometAPI.isValidApiBase(options.apiBase)) {