| 1 | import { |
| 2 | ChatCompletion, |
| 3 | ChatCompletionChunk, |
| 4 | ChatCompletionCreateParams, |
| 5 | ChatCompletionCreateParamsNonStreaming, |
| 6 | ChatCompletionCreateParamsStreaming, |
| 7 | Completion, |
| 8 | CompletionCreateParamsNonStreaming, |
| 9 | CompletionCreateParamsStreaming, |
| 10 | CreateEmbeddingResponse, |
| 11 | EmbeddingCreateParams, |
| 12 | Model, |
| 13 | } from "openai/resources/index"; |
| 14 | import { CometAPIConfig } from "../types.js"; |
| 15 | import { |
| 16 | BaseLlmApi, |
| 17 | CreateRerankResponse, |
| 18 | FimCreateParamsStreaming, |
| 19 | RerankCreateParams, |
| 20 | } from "./base.js"; |
| 21 | import { OpenAIApi } from "./OpenAI.js"; |
| 22 | |
| 23 | /** |
| 24 | * CometAPI adapter - extends OpenAI adapter since CometAPI is OpenAI-compatible |
| 25 | * |
| 26 | * CometAPI provides access to multiple LLM providers (GPT, Claude, Gemini, etc.) |
| 27 | * through a unified OpenAI-compatible API interface. |
| 28 | */ |
| 29 | export class CometAPIApi extends OpenAIApi implements BaseLlmApi { |
| 30 | // Store the original CometAPI config separately |
| 31 | private cometConfig: CometAPIConfig; |
| 32 | |
| 33 | constructor(config: CometAPIConfig) { |
| 34 | // CometAPI uses OpenAI-compatible API, so we can reuse OpenAI adapter |
| 35 | // Convert CometAPI config to OpenAI-compatible config for the base class |
| 36 | const openAICompatibleConfig = { |
| 37 | ...config, |
| 38 | provider: "openai" as const, |
| 39 | apiBase: config.apiBase ?? "https://api.cometapi.com/v1/", |
| 40 | }; |
| 41 | super(openAICompatibleConfig); |
| 42 | |
| 43 | // Store original config after super() call |
| 44 | this.cometConfig = config; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Override list method to handle CometAPI-specific model filtering |
| 49 | * The core filtering logic is handled in the CometAPI provider class |
| 50 | */ |
| 51 | async list(): Promise<Model[]> { |
| 52 | try { |
| 53 | return await super.list(); |
| 54 | } catch (error) { |
| 55 | // Fallback to empty list if model listing fails |
| 56 | console.warn("CometAPI: Failed to fetch model list", error); |
| 57 | return []; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Chat completion - uses OpenAI-compatible format |
| 63 | */ |
| 64 | async chatCompletionNonStream( |
| 65 | body: ChatCompletionCreateParamsNonStreaming, |
| 66 | signal: AbortSignal, |
| 67 | ): Promise<ChatCompletion> { |
| 68 | return super.chatCompletionNonStream(body, signal); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Streaming chat completion - uses OpenAI-compatible format |
| 73 | */ |
| 74 | async *chatCompletionStream( |
| 75 | body: ChatCompletionCreateParamsStreaming, |
| 76 | signal: AbortSignal, |
| 77 | ): AsyncGenerator<ChatCompletionChunk> { |
| 78 | yield* super.chatCompletionStream(body, signal); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Legacy completion endpoint support |
| 83 | */ |
| 84 | completionNonStream( |
| 85 | body: CompletionCreateParamsNonStreaming, |
| 86 | signal: AbortSignal, |
| 87 | ): Promise<Completion> { |
| 88 | return super.completionNonStream(body, signal); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Legacy streaming completion endpoint support |
| 93 | */ |
| 94 | completionStream( |
| 95 | body: CompletionCreateParamsStreaming, |
| 96 | signal: AbortSignal, |
| 97 | ): AsyncGenerator<Completion> { |
| 98 | return super.completionStream(body, signal); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Fill-in-the-middle completion support |
| 103 | */ |
| 104 | fimStream( |
| 105 | body: FimCreateParamsStreaming, |
| 106 | signal: AbortSignal, |
| 107 | ): AsyncGenerator<ChatCompletionChunk> { |
| 108 | return super.fimStream(body, signal); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Embeddings support (if available through CometAPI) |
| 113 | */ |
| 114 | async embed(body: EmbeddingCreateParams): Promise<CreateEmbeddingResponse> { |
| 115 | try { |
| 116 | return await super.embed(body); |
| 117 | } catch (error) { |
| 118 | throw new Error(`CometAPI embeddings not supported: ${error}`); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Reranking support (if available through CometAPI) |
| 124 | */ |
| 125 | async rerank(body: RerankCreateParams): Promise<CreateRerankResponse> { |
| 126 | try { |
| 127 | return await super.rerank(body); |
| 128 | } catch (error) { |
| 129 | throw new Error(`CometAPI reranking not supported: ${error}`); |
| 130 | } |
| 131 | } |
| 132 | } |