Elffuss BraidHub

kikocis/continue public

d691089ac9f7 · 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 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 if (response.status === 499) {
109 return; // Aborted by user
110 }
111
112 const result = (await response.json()) as any;
113 const mergedCode = result.mergedCode;
114
115 yield chatChunk({
116 content: mergedCode,
117 model: body.model,
118 });
119 }
120
121 completionNonStream(
122 body: CompletionCreateParamsNonStreaming,
123 signal: AbortSignal,
124 ): Promise<Completion> {
125 throw new Error(
126 "Relace provider does not support non-streaming completion.",
127 );
128 }
129 completionStream(
130 body: CompletionCreateParamsStreaming,
131 signal: AbortSignal,
132 ): AsyncGenerator<Completion> {
133 throw new Error("Relace provider does not support streaming completion.");
134 }
135 fimStream(
136 body: FimCreateParamsStreaming,
137 signal: AbortSignal,
138 ): AsyncGenerator<ChatCompletionChunk> {
139 throw new Error(
140 "Relace provider does not support streaming FIM completion.",
141 );
142 }
143 embed(body: EmbeddingCreateParams): Promise<CreateEmbeddingResponse> {
144 throw new Error("Relace provider does not support embeddings.");
145 }
146 rerank(body: RerankCreateParams): Promise<CreateRerankResponse> {
147 throw new Error("Relace provider does not support reranking.");
148 }
149 list(): Promise<Model[]> {
150 throw new Error("Relace provider does not support model listing.");
151 }
152}