Revert "feat(ui): redesign menu and add explain, review, changelog, suggest commands"
Build / bun-build (push) Has been cancelled
Build / bun-build (push) Has been cancelled
This reverts commit 1e370be8af.
This commit is contained in:
@@ -1,20 +1,29 @@
|
||||
import type { Config, StreamCallbacks } from "./types";
|
||||
import type { Config } from "./types";
|
||||
|
||||
interface ChatMessage {
|
||||
role: "system" | "user" | "assistant";
|
||||
content: string;
|
||||
}
|
||||
|
||||
const MAX_RETRIES = 3;
|
||||
const RETRY_DELAY_MS = 1000;
|
||||
|
||||
async function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
interface ChatCompletionResponse {
|
||||
choices?: Array<{
|
||||
message?: {
|
||||
content?: string | null;
|
||||
};
|
||||
finish_reason?: string;
|
||||
}>;
|
||||
error?: {
|
||||
message?: string;
|
||||
type?: string;
|
||||
code?: string;
|
||||
};
|
||||
}
|
||||
|
||||
const MAX_RETRIES = 3;
|
||||
const RETRY_DELAY = 1000;
|
||||
|
||||
function cleanMessage(raw: string): string {
|
||||
let msg = raw.trim();
|
||||
// Strip code fences if the whole response is wrapped
|
||||
if (msg.startsWith("```") && msg.endsWith("```")) {
|
||||
const lines = msg.split("\n");
|
||||
if (lines.length > 2) {
|
||||
@@ -26,14 +35,16 @@ function cleanMessage(raw: string): string {
|
||||
return msg;
|
||||
}
|
||||
|
||||
async function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export async function callAI(
|
||||
config: Config,
|
||||
systemPrompt: string,
|
||||
userPrompt: string,
|
||||
callbacks?: StreamCallbacks,
|
||||
): Promise<string> {
|
||||
const url = `${config.apiBase.replace(/\/$/, "")}/chat/completions`;
|
||||
const stream = callbacks != null;
|
||||
|
||||
const messages: ChatMessage[] = [
|
||||
{ role: "system", content: systemPrompt },
|
||||
@@ -53,45 +64,45 @@ export async function callAI(
|
||||
max_tokens: config.maxTokens,
|
||||
temperature: config.temperature,
|
||||
messages,
|
||||
stream,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
if (response.status === 429 && attempt < MAX_RETRIES) {
|
||||
await sleep(RETRY_DELAY_MS * attempt);
|
||||
await sleep(RETRY_DELAY * attempt);
|
||||
continue;
|
||||
}
|
||||
throw new Error(`API request failed (${response.status}): ${text}`);
|
||||
}
|
||||
|
||||
if (stream && response.body) {
|
||||
return await readStream(response.body, callbacks!);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as {
|
||||
choices?: Array<{ message?: { content?: string | null }; finish_reason?: string }>;
|
||||
error?: { message?: string; type?: string; code?: string };
|
||||
};
|
||||
const data = (await response.json()) as ChatCompletionResponse;
|
||||
|
||||
if (data.error) {
|
||||
throw new Error(`API error: ${data.error.message ?? JSON.stringify(data.error)}`);
|
||||
throw new Error(
|
||||
`API error: ${data.error.message ?? JSON.stringify(data.error)}`,
|
||||
);
|
||||
}
|
||||
|
||||
const raw = data.choices?.[0]?.message?.content;
|
||||
const finishReason = data.choices?.[0]?.finish_reason;
|
||||
|
||||
if (raw && raw.trim()) return raw;
|
||||
if (raw && raw.trim()) {
|
||||
return raw;
|
||||
}
|
||||
|
||||
if (finishReason === "length") {
|
||||
throw new Error("Response truncated (max_tokens too low). Try increasing GAI_MAX_TOKENS.");
|
||||
throw new Error(
|
||||
"Response truncated (max_tokens too low). Try increasing GAI_MAX_TOKENS.",
|
||||
);
|
||||
}
|
||||
|
||||
if (finishReason === "content_filter") {
|
||||
throw new Error("Response blocked by content filter.");
|
||||
}
|
||||
|
||||
if (attempt < MAX_RETRIES) {
|
||||
await sleep(RETRY_DELAY_MS * attempt);
|
||||
await sleep(RETRY_DELAY * attempt);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -102,70 +113,21 @@ export async function callAI(
|
||||
if (attempt >= MAX_RETRIES) throw err;
|
||||
if (err instanceof Error && err.message.startsWith("API error")) throw err;
|
||||
if (err instanceof Error && err.message.includes("max_tokens")) throw err;
|
||||
if (err instanceof Error && err.message.includes("content filter")) throw err;
|
||||
await sleep(RETRY_DELAY_MS * attempt);
|
||||
if (err instanceof Error && err.message.includes("content filter"))
|
||||
throw err;
|
||||
await sleep(RETRY_DELAY * attempt);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("Failed to generate response");
|
||||
}
|
||||
|
||||
async function readStream(body: ReadableStream<Uint8Array>, callbacks: StreamCallbacks): Promise<string> {
|
||||
const reader = body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let fullText = "";
|
||||
let buffer = "";
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split("\n");
|
||||
// Keep the last potentially incomplete line
|
||||
buffer = lines.pop() ?? "";
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || !trimmed.startsWith("data:")) continue;
|
||||
|
||||
const data = trimmed.slice(5).trim();
|
||||
if (data === "[DONE]") continue;
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data) as {
|
||||
choices?: Array<{ delta?: { content?: string }; finish_reason?: string }>;
|
||||
};
|
||||
const token = parsed.choices?.[0]?.delta?.content;
|
||||
if (token) {
|
||||
fullText += token;
|
||||
callbacks.onToken?.(token);
|
||||
}
|
||||
const finishReason = parsed.choices?.[0]?.finish_reason;
|
||||
if (finishReason === "length") {
|
||||
callbacks.onError?.(new Error("Response truncated (max_tokens too low)."));
|
||||
}
|
||||
} catch {
|
||||
// Skip unparseable SSE lines
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
|
||||
callbacks.onDone?.(fullText);
|
||||
return fullText;
|
||||
}
|
||||
|
||||
export async function generateCommitMessage(
|
||||
config: Config,
|
||||
systemPrompt: string,
|
||||
userPrompt: string,
|
||||
callbacks?: StreamCallbacks,
|
||||
): Promise<string> {
|
||||
const raw = await callAI(config, systemPrompt, userPrompt, callbacks);
|
||||
const raw = await callAI(config, systemPrompt, userPrompt);
|
||||
return cleanMessage(raw);
|
||||
}
|
||||
|
||||
@@ -173,9 +135,8 @@ export async function generatePRMessage(
|
||||
config: Config,
|
||||
systemPrompt: string,
|
||||
userPrompt: string,
|
||||
callbacks?: StreamCallbacks,
|
||||
): Promise<{ title: string; body: string }> {
|
||||
const raw = await callAI(config, systemPrompt, userPrompt, callbacks);
|
||||
const raw = await callAI(config, systemPrompt, userPrompt);
|
||||
const cleaned = cleanMessage(raw);
|
||||
|
||||
const lines = cleaned.split("\n");
|
||||
@@ -187,5 +148,6 @@ export async function generatePRMessage(
|
||||
}
|
||||
|
||||
const body = lines.slice(bodyStart).join("\n").trim();
|
||||
|
||||
return { title, body };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user