| 1 | import { |
| 2 | Completion, |
| 3 | CompletionCreateParamsNonStreaming, |
| 4 | CompletionCreateParamsStreaming, |
| 5 | CompletionUsage, |
| 6 | } from "openai/resources/completions.mjs"; |
| 7 | import { |
| 8 | CreateEmbeddingResponse, |
| 9 | EmbeddingCreateParams, |
| 10 | } from "openai/resources/embeddings.mjs"; |
| 11 | import { |
| 12 | ChatCompletion, |
| 13 | ChatCompletionChunk, |
| 14 | ChatCompletionCreateParamsNonStreaming, |
| 15 | ChatCompletionCreateParamsStreaming, |
| 16 | } from "openai/resources/index.mjs"; |
| 17 | import { Model } from "openai/resources/models.mjs"; |
| 18 | import { z } from "zod"; |
| 19 | import { OpenAIConfigSchema } from "../types.js"; |
| 20 | import { |
| 21 | chatChunk, |
| 22 | chatCompletion, |
| 23 | customFetch, |
| 24 | usageChatChunk, |
| 25 | } from "../util.js"; |
| 26 | import { |
| 27 | BaseLlmApi, |
| 28 | CreateRerankResponse, |
| 29 | FimCreateParamsStreaming, |
| 30 | RerankCreateParams, |
| 31 | } from "./base.js"; |
| 32 | |
| 33 | type UsageInfo = Pick< |
| 34 | CompletionUsage, |
| 35 | "total_tokens" | "completion_tokens" | "prompt_tokens" |
| 36 | >; |
| 37 | |
| 38 | // Relace only supports apply through a /v1/apply endpoint |
| 39 | export class RelaceApi implements BaseLlmApi { |
| 40 | private apiBase = "https://instantapply.endpoint.relace.run/v1/"; |
| 41 | |
| 42 | constructor(private readonly config: z.infer<typeof OpenAIConfigSchema>) { |
| 43 | this.apiBase = config.apiBase ?? this.apiBase; |
| 44 | if (!this.apiBase.endsWith("/")) { |
| 45 | this.apiBase += "/"; |
| 46 | } |
| 47 | this.config = config; |
| 48 | } |
| 49 | |
| 50 | async chatCompletionNonStream( |
| 51 | body: ChatCompletionCreateParamsNonStreaming, |
| 52 | signal: AbortSignal, |
| 53 | ): Promise<ChatCompletion> { |
| 54 | let content = ""; |
| 55 | let usage: UsageInfo | undefined = undefined; |
| 56 | |
| 57 | // Convert the non-streaming params to streaming params |
| 58 | const streamingBody: ChatCompletionCreateParamsStreaming = { |
| 59 | ...body, |
| 60 | stream: true, |
| 61 | }; |
| 62 | |
| 63 | for await (const chunk of this.chatCompletionStream( |
| 64 | streamingBody, |
| 65 | signal, |
| 66 | )) { |
| 67 | if (chunk.choices.length > 0) { |
| 68 | content += chunk.choices[0]?.delta?.content || ""; |
| 69 | } |
| 70 | if (chunk.usage) { |
| 71 | usage = chunk.usage; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return chatCompletion({ |
| 76 | content, |
| 77 | model: body.model, |
| 78 | usage, |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | // We convert from what would be sent to OpenAI (a prediction for the existing code and a user message with the new code) |
| 83 | // to Relace's format |
| 84 | async *chatCompletionStream( |
| 85 | body: ChatCompletionCreateParamsStreaming, |
| 86 | signal: AbortSignal, |
| 87 | ): AsyncGenerator<ChatCompletionChunk> { |
| 88 | const headers = { |
| 89 | "Content-Type": "application/json", |
| 90 | Authorization: `Bearer ${this.config.apiKey}`, |
| 91 | }; |
| 92 | |
| 93 | const prediction = body.prediction?.content ?? ""; |
| 94 | const initialCode = |
| 95 | typeof prediction === "string" |
| 96 | ? prediction |
| 97 | : prediction.map((p) => p.text).join(""); |
| 98 | |
| 99 | const userContent = body.messages.find((m) => m.role === "user")?.content; |
| 100 | if (!userContent) { |
| 101 | throw new Error("No edit snippet provided."); |
| 102 | } |
| 103 | |
| 104 | const editSnippet = |
| 105 | typeof userContent === "string" |
| 106 | ? userContent |
| 107 | : userContent |
| 108 | .filter((p) => p.type === "text") |
| 109 | .map((p) => p.text) |
| 110 | .join(""); |
| 111 | |
| 112 | const data = { |
| 113 | initialCode, |
| 114 | editSnippet, |
| 115 | }; |
| 116 | |
| 117 | const url = this.apiBase + "code/apply"; |
| 118 | const response = await customFetch(this.config.requestOptions)(url, { |
| 119 | method: "POST", |
| 120 | headers, |
| 121 | body: JSON.stringify(data), |
| 122 | signal, |
| 123 | }); |
| 124 | |
| 125 | if (response.status === 499) { |
| 126 | return; // Aborted by user |
| 127 | } |
| 128 | |
| 129 | const result = (await response.json()) as any; |
| 130 | const mergedCode = result.mergedCode; |
| 131 | |
| 132 | yield chatChunk({ |
| 133 | content: mergedCode, |
| 134 | model: body.model, |
| 135 | }); |
| 136 | |
| 137 | yield usageChatChunk({ |
| 138 | model: body.model, |
| 139 | usage: { |
| 140 | prompt_tokens: result.usage.prompt_tokens || 0, |
| 141 | completion_tokens: result.usage.completion_tokens || 0, |
| 142 | total_tokens: result.usage.total_tokens, |
| 143 | }, |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | completionNonStream( |
| 148 | body: CompletionCreateParamsNonStreaming, |
| 149 | signal: AbortSignal, |
| 150 | ): Promise<Completion> { |
| 151 | throw new Error( |
| 152 | "Relace provider does not support non-streaming completion.", |
| 153 | ); |
| 154 | } |
| 155 | completionStream( |
| 156 | body: CompletionCreateParamsStreaming, |
| 157 | signal: AbortSignal, |
| 158 | ): AsyncGenerator<Completion> { |
| 159 | throw new Error("Relace provider does not support streaming completion."); |
| 160 | } |
| 161 | fimStream( |
| 162 | body: FimCreateParamsStreaming, |
| 163 | signal: AbortSignal, |
| 164 | ): AsyncGenerator<ChatCompletionChunk> { |
| 165 | throw new Error( |
| 166 | "Relace provider does not support streaming FIM completion.", |
| 167 | ); |
| 168 | } |
| 169 | embed(body: EmbeddingCreateParams): Promise<CreateEmbeddingResponse> { |
| 170 | throw new Error("Relace provider does not support embeddings."); |
| 171 | } |
| 172 | rerank(body: RerankCreateParams): Promise<CreateRerankResponse> { |
| 173 | throw new Error("Relace provider does not support reranking."); |
| 174 | } |
| 175 | list(): Promise<Model[]> { |
| 176 | throw new Error("Relace provider does not support model listing."); |
| 177 | } |
| 178 | } |