| 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 |
nothing calls this directly
no outgoing calls
no test coverage detected