Elffuss BraidHub

kikocis/continue public

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