Elffuss BraidHub

kikocis/continue public

19cfb6ec829a · 4.2 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 headers = {
71 "Content-Type": "application/json",
72 Authorization: `Bearer ${this.config.apiKey}`,
73 };
74
75 const prediction = body.prediction?.content ?? "";
76 const initialCode =
77 typeof prediction === "string"
78 ? prediction
79 : prediction.map((p) => p.text).join("");
80
81 const userContent = body.messages.find((m) => m.role === "user")?.content;
82 if (!userContent) {
83 throw new Error("No edit snippet provided.");
84 }
85
86 const editSnippet =
87 typeof userContent === "string"
88 ? userContent
89 : userContent
90 .filter((p) => p.type === "text")
91 .map((p) => p.text)
92 .join("");
93
94 const data = {
95 initialCode,
96 editSnippet,
97 };
98
99 const url = this.apiBase + "code/apply";
100 const response = await customFetch(this.config.requestOptions)(url, {
101 method: "POST",
102 headers,
103 body: JSON.stringify(data),
104 signal,
105 });
106
107 if (response.status === 499) {
108 return; // Aborted by user
109 }
110
111 const result = (await response.json()) as any;
112 const mergedCode = result.mergedCode;
113
114 yield chatChunk({
115 content: mergedCode,
116 model: body.model,
117 });
118 }
119
120 completionNonStream(
121 body: CompletionCreateParamsNonStreaming,
122 signal: AbortSignal,
123 ): Promise<Completion> {
124 throw new Error(
125 "Relace provider does not support non-streaming completion.",
126 );
127 }
128 completionStream(
129 body: CompletionCreateParamsStreaming,
130 signal: AbortSignal,
131 ): AsyncGenerator<Completion> {
132 throw new Error("Relace provider does not support streaming completion.");
133 }
134 fimStream(
135 body: FimCreateParamsStreaming,
136 signal: AbortSignal,
137 ): AsyncGenerator<ChatCompletionChunk> {
138 throw new Error(
139 "Relace provider does not support streaming FIM completion.",
140 );
141 }
142 embed(body: EmbeddingCreateParams): Promise<CreateEmbeddingResponse> {
143 throw new Error("Relace provider does not support embeddings.");
144 }
145 rerank(body: RerankCreateParams): Promise<CreateRerankResponse> {
146 throw new Error("Relace provider does not support reranking.");
147 }
148 list(): Promise<Model[]> {
149 throw new Error("Relace provider does not support model listing.");
150 }
151}