| 1 | import { OpenAI } from "openai/index"; |
| 2 | import { |
| 3 | ChatCompletionCreateParams, |
| 4 | CompletionCreateParamsNonStreaming, |
| 5 | CompletionCreateParamsStreaming, |
| 6 | } from "openai/resources/index"; |
| 7 | import { z } from "zod"; |
| 8 | import { ContinueProxyConfigSchema } from "../types.js"; |
| 9 | import { FimCreateParamsStreaming, RerankCreateParams } from "./base.js"; |
| 10 | import { OpenAIApi } from "./OpenAI.js"; |
| 11 | |
| 12 | export interface ContinueProperties { |
| 13 | apiKeyLocation?: string; |
| 14 | envSecretLocations?: Record<string, string>; |
| 15 | apiBase?: string; |
| 16 | orgScopeId?: string | null; |
| 17 | env?: Record<string, any>; |
| 18 | } |
| 19 | |
| 20 | export interface ProxyModelName { |
| 21 | ownerSlug: string; |
| 22 | packageSlug: string; |
| 23 | provider: string; |
| 24 | model: string; |
| 25 | } |
| 26 | |
| 27 | export class ContinueProxyApi extends OpenAIApi { |
| 28 | // The apiKey and apiBase are set to the values for the proxy, |
| 29 | // but we need to keep track of the actual values that the proxy will use |
| 30 | // to call whatever LLM API is chosen |
| 31 | private actualApiBase?: string; |
| 32 | |
| 33 | // Contains extra properties that we pass along to the proxy. Originally from `env` property on LLMOptions |
| 34 | private configEnv?: Record<string, any>; |
| 35 | |
| 36 | // Store the continue proxy config separately |
| 37 | private continueProxyConfig: z.infer<typeof ContinueProxyConfigSchema>; |
| 38 | |
| 39 | constructor(config: z.infer<typeof ContinueProxyConfigSchema>) { |
| 40 | // Convert ContinueProxyConfigSchema to OpenAIConfigSchema format |
| 41 | const openaiConfig = { |
| 42 | provider: "openai" as const, |
| 43 | apiKey: config.apiKey, |
| 44 | apiBase: config.env?.proxyUrl |
| 45 | ? new URL("model-proxy/v1/", config.env?.proxyUrl).toString() |
| 46 | : "https://api.continue.dev/model-proxy/v1/", |
| 47 | requestOptions: config.requestOptions, |
| 48 | }; |
| 49 | |
| 50 | super(openaiConfig); |
| 51 | |
| 52 | this.continueProxyConfig = config; |
| 53 | this.configEnv = config.env; |
| 54 | this.actualApiBase = config.apiBase; |
| 55 | } |
| 56 | |
| 57 | protected extraBodyProperties(): Record<string, any> { |
| 58 | const continueProperties: ContinueProperties = { |
| 59 | apiKeyLocation: this.continueProxyConfig.env?.apiKeyLocation, |
| 60 | envSecretLocations: this.continueProxyConfig.env?.envSecretLocations, |
| 61 | apiBase: this.actualApiBase, |
| 62 | orgScopeId: this.continueProxyConfig.env?.orgScopeId ?? null, |
| 63 | env: this.configEnv, |
| 64 | }; |
| 65 | return { |
| 66 | continueProperties, |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | private modifyBodyWithContinueProperties<T extends Record<string, any>>( |
| 71 | body: T, |
| 72 | ): T { |
| 73 | return { |
| 74 | ...body, |
| 75 | ...this.extraBodyProperties(), |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | modifyChatBody<T extends ChatCompletionCreateParams>(body: T): T { |
| 80 | // First apply OpenAI-specific modifications |
| 81 | const modifiedBody = super.modifyChatBody(body); |
| 82 | // Then add Continue properties |
| 83 | return this.modifyBodyWithContinueProperties(modifiedBody); |
| 84 | } |
| 85 | |
| 86 | modifyCompletionBody< |
| 87 | T extends |
| 88 | | CompletionCreateParamsNonStreaming |
| 89 | | CompletionCreateParamsStreaming, |
| 90 | >(body: T): T { |
| 91 | return this.modifyBodyWithContinueProperties(body); |
| 92 | } |
| 93 | |
| 94 | modifyFimBody<T extends FimCreateParamsStreaming>(body: T): T { |
| 95 | const modifiedBody = super.modifyFimBody(body); |
| 96 | return this.modifyBodyWithContinueProperties(modifiedBody); |
| 97 | } |
| 98 | |
| 99 | protected getHeaders(): Record<string, string> { |
| 100 | return { |
| 101 | "Content-Type": "application/json", |
| 102 | Accept: "application/json", |
| 103 | "x-api-key": this.continueProxyConfig.apiKey ?? "", |
| 104 | Authorization: `Bearer ${this.continueProxyConfig.apiKey}`, |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | modifyEmbedBody<T extends OpenAI.Embeddings.EmbeddingCreateParams>( |
| 109 | body: T, |
| 110 | ): T { |
| 111 | return this.modifyBodyWithContinueProperties(body); |
| 112 | } |
| 113 | |
| 114 | modifyRerankBody<T extends RerankCreateParams>(body: T): T { |
| 115 | return { |
| 116 | ...body, |
| 117 | ...this.extraBodyProperties(), |
| 118 | }; |
| 119 | } |
| 120 | } |