UI/UX changes, added feature to switch to other LLM APIs
This commit is contained in:
@@ -9,6 +9,9 @@ const DEFAULT_TASKS = [
|
||||
|
||||
const DEFAULT_SETTINGS = {
|
||||
apiKey: "",
|
||||
apiBaseUrl: "https://api.openai.com/v1",
|
||||
apiKeyHeader: "Authorization",
|
||||
apiKeyPrefix: "Bearer ",
|
||||
model: "gpt-4o-mini",
|
||||
systemPrompt:
|
||||
"You are a precise, honest assistant. Be concise, highlight uncertainties, and avoid inventing details.",
|
||||
@@ -89,10 +92,25 @@ function buildUserMessage(resume, task, posting) {
|
||||
}
|
||||
|
||||
async function handleAnalysisRequest(port, payload, signal) {
|
||||
const { apiKey, model, systemPrompt, resume, taskText, postingText } = payload || {};
|
||||
const {
|
||||
apiKey,
|
||||
apiBaseUrl,
|
||||
apiKeyHeader,
|
||||
apiKeyPrefix,
|
||||
model,
|
||||
systemPrompt,
|
||||
resume,
|
||||
taskText,
|
||||
postingText
|
||||
} = payload || {};
|
||||
|
||||
if (!apiKey) {
|
||||
port.postMessage({ type: "ERROR", message: "Missing OpenAI API key." });
|
||||
if (!apiBaseUrl) {
|
||||
port.postMessage({ type: "ERROR", message: "Missing API base URL." });
|
||||
return;
|
||||
}
|
||||
|
||||
if (apiKeyHeader && !apiKey) {
|
||||
port.postMessage({ type: "ERROR", message: "Missing API key." });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -115,6 +133,9 @@ async function handleAnalysisRequest(port, payload, signal) {
|
||||
|
||||
await streamChatCompletion({
|
||||
apiKey,
|
||||
apiBaseUrl,
|
||||
apiKeyHeader,
|
||||
apiKeyPrefix,
|
||||
model,
|
||||
systemPrompt: systemPrompt || "",
|
||||
userMessage,
|
||||
@@ -125,20 +146,49 @@ async function handleAnalysisRequest(port, payload, signal) {
|
||||
port.postMessage({ type: "DONE" });
|
||||
}
|
||||
|
||||
function buildChatUrl(apiBaseUrl) {
|
||||
const trimmed = (apiBaseUrl || "").trim().replace(/\/+$/, "");
|
||||
if (!trimmed) return "";
|
||||
if (trimmed.endsWith("/chat/completions")) return trimmed;
|
||||
return `${trimmed}/chat/completions`;
|
||||
}
|
||||
|
||||
function buildAuthHeader(apiKeyHeader, apiKeyPrefix, apiKey) {
|
||||
if (!apiKeyHeader) return null;
|
||||
return {
|
||||
name: apiKeyHeader,
|
||||
value: `${apiKeyPrefix || ""}${apiKey || ""}`
|
||||
};
|
||||
}
|
||||
|
||||
async function streamChatCompletion({
|
||||
apiKey,
|
||||
apiBaseUrl,
|
||||
apiKeyHeader,
|
||||
apiKeyPrefix,
|
||||
model,
|
||||
systemPrompt,
|
||||
userMessage,
|
||||
signal,
|
||||
onDelta
|
||||
}) {
|
||||
const response = await fetch("https://api.openai.com/v1/chat/completions", {
|
||||
const chatUrl = buildChatUrl(apiBaseUrl);
|
||||
if (!chatUrl) {
|
||||
throw new Error("Invalid API base URL.");
|
||||
}
|
||||
|
||||
const headers = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
|
||||
const authHeader = buildAuthHeader(apiKeyHeader, apiKeyPrefix, apiKey);
|
||||
if (authHeader) {
|
||||
headers[authHeader.name] = authHeader.value;
|
||||
}
|
||||
|
||||
const response = await fetch(chatUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiKey}`
|
||||
},
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
stream: true,
|
||||
|
||||
Reference in New Issue
Block a user