feat: add support for Grok Web and Perplexity Web providers

This commit is contained in:
decolua
2026-04-22 11:58:53 +07:00
parent eeb2dc9e30
commit abb04c5366
17 changed files with 1583 additions and 13 deletions

View File

@@ -398,6 +398,29 @@ export const PROVIDER_MODELS = {
{ id: "qwen/qwen3-next-80b-a3b-instruct-maas", name: "Qwen3 Next 80B Instruct (Vertex)" },
{ id: "zai-org/glm-5-maas", name: "GLM-5 (Vertex)" },
],
"grok-web": [
{ id: "grok-3", name: "Grok 3" },
{ id: "grok-3-mini", name: "Grok 3 Mini (Thinking)" },
{ id: "grok-3-thinking", name: "Grok 3 Thinking" },
{ id: "grok-4", name: "Grok 4" },
{ id: "grok-4-mini", name: "Grok 4 Mini (Thinking)" },
{ id: "grok-4-thinking", name: "Grok 4 Thinking" },
{ id: "grok-4-heavy", name: "Grok 4 Heavy (SuperGrok)" },
{ id: "grok-4.1-mini", name: "Grok 4.1 Mini (Thinking)" },
{ id: "grok-4.1-fast", name: "Grok 4.1 Fast" },
{ id: "grok-4.1-expert", name: "Grok 4.1 Expert" },
{ id: "grok-4.1-thinking", name: "Grok 4.1 Thinking" },
{ id: "grok-4.2", name: "Grok 4.2 (4.20 Beta)" },
],
"perplexity-web": [
{ id: "pplx-auto", name: "Perplexity Auto (Free)" },
{ id: "pplx-sonar", name: "Perplexity Sonar" },
{ id: "pplx-gpt", name: "GPT-5.4 (via Perplexity)" },
{ id: "pplx-gemini", name: "Gemini 3.1 Pro (via Perplexity)" },
{ id: "pplx-sonnet", name: "Claude Sonnet 4.6 (via Perplexity)" },
{ id: "pplx-opus", name: "Claude Opus 4.6 (via Perplexity)" },
{ id: "pplx-nemotron", name: "Nemotron 3 Super (via Perplexity)" },
],
// TTS entries are loaded from ttsModels.js via buildTtsProviderModels()
...buildTtsProviderModels(),

View File

@@ -72,7 +72,6 @@ export const PROVIDERS = {
"User-Agent": "codex-cli/1.0.18 (macOS; arm64)"
},
clientId: "app_EMoamEEZ73f0CkXaXp7hrann",
clientSecret: "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",
tokenUrl: "https://auth.openai.com/oauth/token"
},
qwen: {
@@ -338,6 +337,16 @@ export const PROVIDERS = {
headers: { "x-opencode-client": "desktop" },
noAuth: true
},
"grok-web": {
baseUrl: "https://grok.com/rest/app-chat/conversations/new",
format: "grok-web",
authType: "cookie"
},
"perplexity-web": {
baseUrl: "https://www.perplexity.ai/rest/sse/perplexity_ask",
format: "perplexity-web",
authType: "cookie"
},
};
export const OLLAMA_LOCAL_DEFAULT_HOST = "http://localhost:11434";

View File

@@ -0,0 +1,345 @@
import { BaseExecutor } from "./base.js";
import { PROVIDERS } from "../config/providers.js";
const GROK_CHAT_API = PROVIDERS["grok-web"].baseUrl;
const GROK_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36";
const MODEL_MAP = {
"grok-3": { grokModel: "grok-3", modelMode: "MODEL_MODE_GROK_3", isThinking: false },
"grok-3-mini": { grokModel: "grok-3", modelMode: "MODEL_MODE_GROK_3_MINI_THINKING", isThinking: true },
"grok-3-thinking": { grokModel: "grok-3", modelMode: "MODEL_MODE_GROK_3_THINKING", isThinking: true },
"grok-4": { grokModel: "grok-4", modelMode: "MODEL_MODE_GROK_4", isThinking: false },
"grok-4-mini": { grokModel: "grok-4-mini", modelMode: "MODEL_MODE_GROK_4_MINI_THINKING", isThinking: true },
"grok-4-thinking": { grokModel: "grok-4", modelMode: "MODEL_MODE_GROK_4_THINKING", isThinking: true },
"grok-4-heavy": { grokModel: "grok-4", modelMode: "MODEL_MODE_HEAVY", isThinking: true },
"grok-4.1-mini": { grokModel: "grok-4-1-thinking-1129", modelMode: "MODEL_MODE_GROK_4_1_MINI_THINKING", isThinking: true },
"grok-4.1-fast": { grokModel: "grok-4-1-thinking-1129", modelMode: "MODEL_MODE_FAST", isThinking: false },
"grok-4.1-expert": { grokModel: "grok-4-1-thinking-1129", modelMode: "MODEL_MODE_EXPERT", isThinking: true },
"grok-4.1-thinking": { grokModel: "grok-4-1-thinking-1129", modelMode: "MODEL_MODE_GROK_4_1_THINKING", isThinking: true },
"grok-4.2": { grokModel: "grok-420", modelMode: "MODEL_MODE_GROK_420", isThinking: false },
"grok-4.20": { grokModel: "grok-420", modelMode: "MODEL_MODE_GROK_420", isThinking: false },
"grok-4.20-beta": { grokModel: "grok-420", modelMode: "MODEL_MODE_GROK_420", isThinking: false },
};
function randomString(length, alphanumeric = false) {
const chars = alphanumeric ? "abcdefghijklmnopqrstuvwxyz0123456789" : "abcdefghijklmnopqrstuvwxyz";
let result = "";
for (let i = 0; i < length; i++) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
function generateStatsigId() {
const msg = Math.random() < 0.5
? `e:TypeError: Cannot read properties of null (reading 'children["${randomString(5, true)}"]')`
: `e:TypeError: Cannot read properties of undefined (reading '${randomString(10)}')`;
return btoa(msg);
}
function randomHex(bytes) {
const arr = new Uint8Array(bytes);
crypto.getRandomValues(arr);
return Array.from(arr, (b) => b.toString(16).padStart(2, "0")).join("");
}
function parseOpenAIMessages(messages) {
const extracted = [];
for (const msg of messages) {
let role = String(msg.role || "user");
if (role === "developer") role = "system";
let content = "";
if (typeof msg.content === "string") {
content = msg.content;
} else if (Array.isArray(msg.content)) {
content = msg.content.filter((c) => c.type === "text").map((c) => String(c.text || "")).join(" ");
}
if (!content.trim()) continue;
extracted.push({ role, text: content });
}
let lastUserIdx = -1;
for (let i = extracted.length - 1; i >= 0; i--) {
if (extracted[i].role === "user") { lastUserIdx = i; break; }
}
const parts = [];
for (let i = 0; i < extracted.length; i++) {
const { role, text } = extracted[i];
parts.push(i === lastUserIdx ? text : `${role}: ${text}`);
}
return parts.join("\n\n");
}
async function* readGrokNdjsonEvents(body, signal) {
const reader = body.getReader();
const decoder = new TextDecoder();
let buffer = "";
try {
while (true) {
if (signal?.aborted) return;
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
while (true) {
const idx = buffer.indexOf("\n");
if (idx < 0) break;
const line = buffer.slice(0, idx).trim();
buffer = buffer.slice(idx + 1);
if (!line) continue;
try { yield JSON.parse(line); } catch { /* skip */ }
}
}
buffer += decoder.decode();
const remaining = buffer.trim();
if (remaining) {
try { yield JSON.parse(remaining); } catch { /* skip */ }
}
} finally {
reader.releaseLock();
}
}
async function* extractContent(eventStream, isThinkingModel, signal) {
let fingerprint = "";
let responseId = "";
let thinkOpened = false;
for await (const event of readGrokNdjsonEvents(eventStream, signal)) {
if (event.error) {
yield { error: event.error.message || `Grok error: ${event.error.code}`, done: true };
return;
}
const resp = event.result?.response;
if (!resp) continue;
if (resp.llmInfo?.modelHash && !fingerprint) fingerprint = resp.llmInfo.modelHash;
if (resp.responseId) responseId = resp.responseId;
if (resp.modelResponse) {
const mr = resp.modelResponse;
if (thinkOpened && isThinkingModel) {
if (mr.message) yield { thinking: mr.message };
thinkOpened = false;
}
if (mr.message) yield { fullMessage: mr.message, fingerprint, responseId };
if (mr.metadata?.llm_info?.modelHash) fingerprint = mr.metadata.llm_info.modelHash;
continue;
}
if (resp.token != null) yield { delta: resp.token, fingerprint, responseId };
}
yield { done: true, fingerprint, responseId };
}
function sseChunk(data) {
return `data: ${JSON.stringify(data)}\n\n`;
}
function buildStreamingResponse(eventStream, model, cid, created, isThinkingModel, signal) {
const encoder = new TextEncoder();
return new ReadableStream({
async start(controller) {
try {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null, logprobs: null }],
})));
let fp = "";
for await (const chunk of extractContent(eventStream, isThinkingModel, signal)) {
if (chunk.fingerprint) fp = chunk.fingerprint;
if (chunk.error) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: fp || null,
choices: [{ index: 0, delta: { content: `[Error: ${chunk.error}]` }, finish_reason: null, logprobs: null }],
})));
break;
}
if (chunk.thinking) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: fp || null,
choices: [{ index: 0, delta: { reasoning_content: chunk.thinking }, finish_reason: null, logprobs: null }],
})));
continue;
}
if (chunk.done) break;
if (chunk.delta) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: fp || null,
choices: [{ index: 0, delta: { content: chunk.delta }, finish_reason: null, logprobs: null }],
})));
}
}
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: fp || null,
choices: [{ index: 0, delta: {}, finish_reason: "stop", logprobs: null }],
})));
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
} catch (err) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: { content: `[Stream error: ${err.message || String(err)}]` }, finish_reason: "stop", logprobs: null }],
})));
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
} finally {
controller.close();
}
},
});
}
async function buildNonStreamingResponse(eventStream, model, cid, created, isThinkingModel, signal) {
let fullContent = "";
let fingerprint = "";
const thinkingParts = [];
for await (const chunk of extractContent(eventStream, isThinkingModel, signal)) {
if (chunk.fingerprint) fingerprint = chunk.fingerprint;
if (chunk.error) {
return new Response(JSON.stringify({
error: { message: chunk.error, type: "upstream_error", code: "GROK_ERROR" },
}), { status: 502, headers: { "Content-Type": "application/json" } });
}
if (chunk.thinking) { thinkingParts.push(chunk.thinking); continue; }
if (chunk.done) break;
if (chunk.fullMessage) fullContent = chunk.fullMessage;
else if (chunk.delta) fullContent += chunk.delta;
}
const msg = { role: "assistant", content: fullContent };
if (thinkingParts.length > 0) msg.reasoning_content = thinkingParts.join("\n");
const promptTokens = Math.ceil(fullContent.length / 4);
const completionTokens = Math.ceil(fullContent.length / 4);
return new Response(JSON.stringify({
id: cid, object: "chat.completion", created, model, system_fingerprint: fingerprint || null,
choices: [{ index: 0, message: msg, finish_reason: "stop", logprobs: null }],
usage: { prompt_tokens: promptTokens, completion_tokens: completionTokens, total_tokens: promptTokens + completionTokens },
}), { status: 200, headers: { "Content-Type": "application/json" } });
}
export class GrokWebExecutor extends BaseExecutor {
constructor() {
super("grok-web", PROVIDERS["grok-web"]);
}
async execute({ model, body, stream, credentials, signal, log }) {
const messages = body?.messages;
if (!messages || !Array.isArray(messages) || messages.length === 0) {
const errResp = new Response(JSON.stringify({
error: { message: "Missing or empty messages array", type: "invalid_request" },
}), { status: 400, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: GROK_CHAT_API, headers: {}, transformedBody: body };
}
const modelInfo = MODEL_MAP[model];
if (!modelInfo) log?.info?.("GROK-WEB", `Unmapped model ${model}, defaulting to grok-4.1-fast`);
const { grokModel, modelMode, isThinking } = modelInfo || MODEL_MAP["grok-4.1-fast"];
const message = parseOpenAIMessages(messages);
if (!message.trim()) {
const errResp = new Response(JSON.stringify({
error: { message: "Empty query after processing", type: "invalid_request" },
}), { status: 400, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: GROK_CHAT_API, headers: {}, transformedBody: body };
}
const grokPayload = {
temporary: true, modelName: grokModel, modelMode, message,
fileAttachments: [], imageAttachments: [],
disableSearch: false, enableImageGeneration: false, returnImageBytes: false,
returnRawGrokInXaiRequest: false, enableImageStreaming: false, imageGenerationCount: 0,
forceConcise: false, toolOverrides: {}, enableSideBySide: true, sendFinalMetadata: true,
isReasoning: false, disableTextFollowUps: false, disableMemory: true,
forceSideBySide: false, isAsyncChat: false, disableSelfHarmShortCircuit: false,
deviceEnvInfo: {
darkModeEnabled: false, devicePixelRatio: 2,
screenWidth: 2056, screenHeight: 1329, viewportWidth: 2056, viewportHeight: 1083,
},
};
const traceId = randomHex(16);
const spanId = randomHex(8);
const headers = {
Accept: "*/*",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.9",
Baggage: "sentry-environment=production,sentry-release=d6add6fb0460641fd482d767a335ef72b9b6abb8,sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c",
"Cache-Control": "no-cache",
"Content-Type": "application/json",
Origin: "https://grok.com",
Pragma: "no-cache",
Referer: "https://grok.com/",
"Sec-Ch-Ua": '"Google Chrome";v="136", "Chromium";v="136", "Not(A:Brand";v="24"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"macOS"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": GROK_USER_AGENT,
"x-statsig-id": generateStatsigId(),
"x-xai-request-id": crypto.randomUUID(),
traceparent: `00-${traceId}-${spanId}-00`,
};
// Strip "sso=" prefix if user pasted it
if (credentials.apiKey) {
let token = credentials.apiKey;
if (token.startsWith("sso=")) token = token.slice(4);
headers["Cookie"] = `sso=${token}`;
}
log?.info?.("GROK-WEB", `Query to ${model} (grok=${grokModel}, mode=${modelMode}), len=${message.length}`);
let response;
try {
response = await fetch(GROK_CHAT_API, {
method: "POST", headers, body: JSON.stringify(grokPayload), signal,
});
} catch (err) {
log?.error?.("GROK-WEB", `Fetch failed: ${err.message || String(err)}`);
const errResp = new Response(JSON.stringify({
error: { message: `Grok connection failed: ${err.message || String(err)}`, type: "upstream_error" },
}), { status: 502, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: GROK_CHAT_API, headers, transformedBody: grokPayload };
}
if (!response.ok) {
const status = response.status;
let errMsg = `Grok returned HTTP ${status}`;
if (status === 401 || status === 403) errMsg = "Grok auth failed — SSO cookie may be expired. Re-paste your sso cookie value from grok.com.";
else if (status === 429) errMsg = "Grok rate limited. Wait a moment and retry, or rotate cookies.";
log?.warn?.("GROK-WEB", errMsg);
const errResp = new Response(JSON.stringify({
error: { message: errMsg, type: "upstream_error", code: `HTTP_${status}` },
}), { status, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: GROK_CHAT_API, headers, transformedBody: grokPayload };
}
if (!response.body) {
const errResp = new Response(JSON.stringify({
error: { message: "Grok returned empty response body", type: "upstream_error" },
}), { status: 502, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: GROK_CHAT_API, headers, transformedBody: grokPayload };
}
const cid = `chatcmpl-grok-${crypto.randomUUID().slice(0, 12)}`;
const created = Math.floor(Date.now() / 1000);
let finalResponse;
if (stream) {
const sseStream = buildStreamingResponse(response.body, model, cid, created, isThinking, signal);
finalResponse = new Response(sseStream, {
status: 200,
headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "X-Accel-Buffering": "no" },
});
} else {
finalResponse = await buildNonStreamingResponse(response.body, model, cid, created, isThinking, signal);
}
return { response: finalResponse, url: GROK_CHAT_API, headers, transformedBody: grokPayload };
}
}
export default GrokWebExecutor;

View File

@@ -9,6 +9,8 @@ import { CursorExecutor } from "./cursor.js";
import { VertexExecutor } from "./vertex.js";
import { QwenExecutor } from "./qwen.js";
import { OpenCodeExecutor } from "./opencode.js";
import { GrokWebExecutor } from "./grok-web.js";
import { PerplexityWebExecutor } from "./perplexity-web.js";
import { DefaultExecutor } from "./default.js";
const executors = {
@@ -25,6 +27,8 @@ const executors = {
"vertex-partner": new VertexExecutor("vertex-partner"),
qwen: new QwenExecutor(),
opencode: new OpenCodeExecutor(),
"grok-web": new GrokWebExecutor(),
"perplexity-web": new PerplexityWebExecutor(),
};
const defaultCache = new Map();
@@ -52,3 +56,5 @@ export { VertexExecutor } from "./vertex.js";
export { DefaultExecutor } from "./default.js";
export { QwenExecutor } from "./qwen.js";
export { OpenCodeExecutor } from "./opencode.js";
export { GrokWebExecutor } from "./grok-web.js";
export { PerplexityWebExecutor } from "./perplexity-web.js";

View File

@@ -0,0 +1,507 @@
import { BaseExecutor } from "./base.js";
import { PROVIDERS } from "../config/providers.js";
const PPLX_SSE_ENDPOINT = PROVIDERS["perplexity-web"].baseUrl;
const PPLX_API_VERSION = "2.18";
const PPLX_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36";
const MODEL_MAP = {
"pplx-auto": ["concise", "pplx_pro"],
"pplx-sonar": ["copilot", "experimental"],
"pplx-gpt": ["copilot", "gpt54"],
"pplx-gemini": ["copilot", "gemini31pro_high"],
"pplx-sonnet": ["copilot", "claude46sonnet"],
"pplx-opus": ["copilot", "claude46opus"],
"pplx-nemotron": ["copilot", "nv_nemotron_3_super"],
};
const THINKING_MAP = {
"pplx-gpt": "gpt54_thinking",
"pplx-sonnet": "claude46sonnetthinking",
"pplx-opus": "claude46opusthinking",
};
const CITATION_RE = /\[\d+\]/g;
const GROK_TAG_RE = /<grok:[^>]*>.*?<\/grok:[^>]*>/gs;
const GROK_SELF_RE = /<grok:[^>]*\/>/g;
const XML_DECL_RE = /<[?]xml[^?]*[?]>/g;
const RESPONSE_TAG_RE = /<\/?response\b[^>]*>/gi;
const MULTI_SPACE = / {2,}/g;
const MULTI_NL = /\n{3,}/g;
const SESSION_MAX_AGE_MS = 3600_000;
const SESSION_MAX_ENTRIES = 200;
const sessionCache = new Map();
// FNV-1a hash for session key lookup
function sessionKey(history) {
const parts = history.map((h) => `${h.role}:${h.content}`).join("\n");
let hash = 0x811c9dc5;
for (let i = 0; i < parts.length; i++) {
hash ^= parts.charCodeAt(i);
hash = (hash * 0x01000193) >>> 0;
}
return hash.toString(16).padStart(8, "0");
}
function sessionLookup(history) {
if (history.length === 0) return null;
const key = sessionKey(history);
const entry = sessionCache.get(key);
if (!entry) return null;
if (Date.now() - entry.ts > SESSION_MAX_AGE_MS) {
sessionCache.delete(key);
return null;
}
return entry.backendUuid;
}
function sessionStore(history, currentMsg, responseText, backendUuid) {
if (!backendUuid) return;
const full = [...history, { role: "user", content: currentMsg }, { role: "assistant", content: responseText }];
const key = sessionKey(full);
sessionCache.set(key, { backendUuid, ts: Date.now() });
if (sessionCache.size > SESSION_MAX_ENTRIES) {
let oldestKey = null;
let oldestTs = Infinity;
for (const [k, v] of sessionCache) {
if (v.ts < oldestTs) { oldestTs = v.ts; oldestKey = k; }
}
if (oldestKey) sessionCache.delete(oldestKey);
}
}
function cleanResponse(text, strip = true) {
let t = text;
t = t.replace(XML_DECL_RE, "");
t = t.replace(CITATION_RE, "");
t = t.replace(GROK_TAG_RE, "");
t = t.replace(GROK_SELF_RE, "");
t = t.replace(RESPONSE_TAG_RE, "");
if (strip) {
t = t.replace(MULTI_SPACE, " ");
t = t.replace(MULTI_NL, "\n\n");
t = t.trim();
}
return t;
}
async function* readPplxSseEvents(body, signal) {
const reader = body.getReader();
const decoder = new TextDecoder();
let buffer = "";
let dataLines = [];
function flush() {
if (dataLines.length === 0) return null;
const payload = dataLines.join("\n");
dataLines = [];
const trimmed = payload.trim();
if (!trimmed || trimmed === "[DONE]") return "done";
try { return JSON.parse(trimmed); } catch { return null; }
}
try {
while (true) {
if (signal?.aborted) return;
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
while (true) {
const idx = buffer.indexOf("\n");
if (idx < 0) break;
const rawLine = buffer.slice(0, idx);
buffer = buffer.slice(idx + 1);
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
if (line === "") {
const parsed = flush();
if (parsed === "done") return;
if (parsed) yield parsed;
continue;
}
if (line.startsWith("data:")) dataLines.push(line.slice(5).trimStart());
if (line === "event: end_of_stream") return;
}
}
buffer += decoder.decode();
if (buffer.trim().startsWith("data:")) dataLines.push(buffer.trim().slice(5).trimStart());
const tail = flush();
if (tail && tail !== "done") yield tail;
} finally {
reader.releaseLock();
}
}
function parseOpenAIMessages(messages) {
let systemMsg = "";
const history = [];
for (const msg of messages) {
let role = String(msg.role || "user");
if (role === "developer") role = "system";
let content = "";
if (typeof msg.content === "string") content = msg.content;
else if (Array.isArray(msg.content)) {
content = msg.content.filter((c) => c.type === "text").map((c) => String(c.text || "")).join(" ");
}
if (!content.trim()) continue;
if (role === "system") systemMsg += content + "\n";
else if (role === "user" || role === "assistant") history.push({ role, content });
}
let currentMsg = "";
if (history.length > 0 && history[history.length - 1].role === "user") {
currentMsg = history.pop().content;
}
return { systemMsg, history, currentMsg };
}
function buildPplxRequestBody(query, mode, modelPref, followUpUuid) {
const tz = typeof Intl !== "undefined" ? Intl.DateTimeFormat().resolvedOptions().timeZone : "UTC";
return {
query_str: query,
params: {
query_str: query,
search_focus: "internet",
mode,
model_preference: modelPref,
sources: ["web"],
attachments: [],
frontend_uuid: crypto.randomUUID(),
frontend_context_uuid: crypto.randomUUID(),
version: PPLX_API_VERSION,
language: "en-US",
timezone: tz,
search_recency_filter: null,
is_incognito: true,
use_schematized_api: true,
last_backend_uuid: followUpUuid,
},
};
}
function formatToolsHint(tools) {
if (!Array.isArray(tools) || tools.length === 0) return "";
const lines = tools.map((t) => {
const fn = t?.function || t || {};
const name = fn.name || "unnamed";
const desc = (fn.description || "").split("\n")[0].slice(0, 200);
return `- ${name}: ${desc}`;
});
return `Available tools (reference only, cannot invoke):\n${lines.join("\n")}`;
}
function buildQuery(parsed, followUpUuid, tools) {
if (followUpUuid) return parsed.currentMsg;
const obj = {};
const instr = [];
if (parsed.systemMsg.trim()) instr.push(parsed.systemMsg.trim());
const toolsHint = formatToolsHint(tools);
if (toolsHint) instr.push(toolsHint);
instr.push("You have built-in web search. Answer questions directly using search results.");
obj.instructions = instr;
if (parsed.history.length > 0) obj.history = parsed.history;
if (parsed.currentMsg) obj.query = parsed.currentMsg;
else if (parsed.history.length === 0) obj.query = "";
const json = JSON.stringify(obj);
return json.length > 96000 ? json.slice(-96000) : json;
}
async function* extractContent(eventStream, signal) {
let fullAnswer = "";
let backendUuid = null;
let seenLen = 0;
const seenThinking = new Set();
for await (const event of readPplxSseEvents(eventStream, signal)) {
if (event.error_code || event.error_message) {
yield { error: event.error_message || `Perplexity error: ${event.error_code}`, done: true };
return;
}
if (event.backend_uuid) backendUuid = event.backend_uuid;
const blocks = event.blocks ?? [];
for (const block of blocks) {
const usage = block.intended_usage ?? "";
if (usage === "pro_search_steps" && block.plan_block?.steps) {
for (const step of block.plan_block.steps) {
if (step.step_type === "SEARCH_WEB") {
for (const q of step.search_web_content?.queries ?? []) {
const qr = q.query ?? "";
if (qr && !seenThinking.has(qr)) {
seenThinking.add(qr);
yield { thinking: `Searching: ${qr}`, backendUuid: backendUuid ?? undefined };
}
}
} else if (step.step_type === "READ_RESULTS") {
for (const u of (step.read_results_content?.urls ?? []).slice(0, 3)) {
if (u && !seenThinking.has(u)) {
seenThinking.add(u);
yield { thinking: `Reading: ${u}`, backendUuid: backendUuid ?? undefined };
}
}
}
}
}
if (usage === "plan" && block.plan_block?.goals) {
for (const goal of block.plan_block.goals) {
const desc = goal.description ?? "";
if (desc && !seenThinking.has(desc)) {
seenThinking.add(desc);
yield { thinking: desc, backendUuid: backendUuid ?? undefined };
}
}
}
if (!usage.includes("markdown")) continue;
const mb = block.markdown_block;
if (!mb) continue;
const chunks = mb.chunks ?? [];
if (chunks.length === 0) continue;
if (mb.progress === "DONE") {
fullAnswer = chunks.join("");
} else {
const chunkText = chunks.join("");
const cumulative = fullAnswer + chunkText;
if (cumulative.length > seenLen) {
const delta = cumulative.slice(seenLen);
fullAnswer = cumulative;
seenLen = cumulative.length;
yield { delta, answer: fullAnswer, backendUuid: backendUuid ?? undefined };
}
}
}
if (blocks.length === 0 && event.text) {
const t = event.text.trim();
if (t.length > seenLen) {
const delta = t.slice(seenLen);
fullAnswer = t;
seenLen = t.length;
yield { delta, answer: fullAnswer, backendUuid: backendUuid ?? undefined };
}
}
if (event.final || event.status === "COMPLETED") break;
}
yield { delta: "", answer: fullAnswer, backendUuid: backendUuid ?? undefined, done: true };
}
function sseChunk(data) {
return `data: ${JSON.stringify(data)}\n\n`;
}
function buildStreamingResponse(eventStream, model, cid, created, history, currentMsg, signal) {
const encoder = new TextEncoder();
return new ReadableStream({
async start(controller) {
try {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null, logprobs: null }],
})));
let fullAnswer = "";
let respBackendUuid = null;
for await (const chunk of extractContent(eventStream, signal)) {
if (chunk.backendUuid) respBackendUuid = chunk.backendUuid;
if (chunk.error) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: { content: `[Error: ${chunk.error}]` }, finish_reason: null, logprobs: null }],
})));
break;
}
if (chunk.thinking) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: { reasoning_content: chunk.thinking + "\n" }, finish_reason: null, logprobs: null }],
})));
continue;
}
if (chunk.done) { fullAnswer = chunk.answer || fullAnswer; break; }
let dt = chunk.delta || "";
if (dt) {
dt = cleanResponse(dt, false);
if (dt) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: { content: dt }, finish_reason: null, logprobs: null }],
})));
}
}
if (chunk.answer) fullAnswer = chunk.answer;
}
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: {}, finish_reason: "stop", logprobs: null }],
})));
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
sessionStore(history, currentMsg, cleanResponse(fullAnswer), respBackendUuid);
} catch (err) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: { content: `[Stream error: ${err.message || String(err)}]` }, finish_reason: "stop", logprobs: null }],
})));
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
} finally {
controller.close();
}
},
});
}
async function buildNonStreamingResponse(eventStream, model, cid, created, history, currentMsg, signal) {
let fullAnswer = "";
let respBackendUuid = null;
const thinkingParts = [];
for await (const chunk of extractContent(eventStream, signal)) {
if (chunk.backendUuid) respBackendUuid = chunk.backendUuid;
if (chunk.error) {
return new Response(JSON.stringify({
error: { message: chunk.error, type: "upstream_error", code: "PPLX_ERROR" },
}), { status: 502, headers: { "Content-Type": "application/json" } });
}
if (chunk.thinking) { thinkingParts.push(chunk.thinking); continue; }
if (chunk.done) { fullAnswer = chunk.answer || fullAnswer; break; }
if (chunk.answer) fullAnswer = chunk.answer;
}
fullAnswer = cleanResponse(fullAnswer);
sessionStore(history, currentMsg, fullAnswer, respBackendUuid);
const reasoningContent = thinkingParts.length > 0 ? thinkingParts.join("\n") : undefined;
const msg = { role: "assistant", content: fullAnswer };
if (reasoningContent) msg.reasoning_content = reasoningContent;
const promptTokens = Math.ceil(currentMsg.length / 4);
const completionTokens = Math.ceil(fullAnswer.length / 4);
return new Response(JSON.stringify({
id: cid, object: "chat.completion", created, model, system_fingerprint: null,
choices: [{ index: 0, message: msg, finish_reason: "stop", logprobs: null }],
usage: { prompt_tokens: promptTokens, completion_tokens: completionTokens, total_tokens: promptTokens + completionTokens },
}), { status: 200, headers: { "Content-Type": "application/json" } });
}
export class PerplexityWebExecutor extends BaseExecutor {
constructor() {
super("perplexity-web", PROVIDERS["perplexity-web"]);
}
async execute({ model, body, stream, credentials, signal, log }) {
const messages = body?.messages;
if (!messages || !Array.isArray(messages) || messages.length === 0) {
const errResp = new Response(JSON.stringify({
error: { message: "Missing or empty messages array", type: "invalid_request" },
}), { status: 400, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: PPLX_SSE_ENDPOINT, headers: {}, transformedBody: body };
}
const thinking = body?.thinking === true || (body?.reasoning_effort != null && body.reasoning_effort !== "none");
let pplxMode;
let modelPref;
if (thinking && THINKING_MAP[model]) {
pplxMode = "copilot";
modelPref = THINKING_MAP[model];
log?.info?.("PPLX-WEB", `Thinking mode → ${model} using ${modelPref}`);
} else if (MODEL_MAP[model]) {
[pplxMode, modelPref] = MODEL_MAP[model];
} else {
pplxMode = "copilot";
modelPref = model;
log?.info?.("PPLX-WEB", `Unmapped model ${model}, using as raw preference`);
}
const parsed = parseOpenAIMessages(messages);
const followUpUuid = sessionLookup(parsed.history);
if (followUpUuid) log?.info?.("PPLX-WEB", `Session continue: ${followUpUuid.slice(0, 12)}...`);
const query = buildQuery(parsed, followUpUuid, body?.tools);
if (!query.trim()) {
const errResp = new Response(JSON.stringify({
error: { message: "Empty query after processing", type: "invalid_request" },
}), { status: 400, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: PPLX_SSE_ENDPOINT, headers: {}, transformedBody: body };
}
const pplxBody = buildPplxRequestBody(query, pplxMode, modelPref, followUpUuid);
const headers = {
"Content-Type": "application/json",
Accept: "text/event-stream",
Origin: "https://www.perplexity.ai",
Referer: "https://www.perplexity.ai/",
"User-Agent": PPLX_USER_AGENT,
"X-App-ApiClient": "default",
"X-App-ApiVersion": PPLX_API_VERSION,
};
if (credentials.accessToken) {
headers["Authorization"] = `Bearer ${credentials.accessToken}`;
} else if (credentials.apiKey) {
headers["Cookie"] = `__Secure-next-auth.session-token=${credentials.apiKey}`;
}
log?.info?.("PPLX-WEB", `Query to ${model} (pref=${modelPref}, mode=${pplxMode}), len=${query.length}`);
const fetchOptions = { method: "POST", headers, body: JSON.stringify(pplxBody) };
if (signal) fetchOptions.signal = signal;
let response;
try {
response = await fetch(PPLX_SSE_ENDPOINT, fetchOptions);
} catch (err) {
log?.error?.("PPLX-WEB", `Fetch failed: ${err.message || String(err)}`);
const errResp = new Response(JSON.stringify({
error: { message: `Perplexity connection failed: ${err.message || String(err)}`, type: "upstream_error" },
}), { status: 502, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: PPLX_SSE_ENDPOINT, headers, transformedBody: pplxBody };
}
if (!response.ok) {
const status = response.status;
let errMsg = `Perplexity returned HTTP ${status}`;
if (status === 401 || status === 403) errMsg = "Perplexity auth failed — session cookie may be expired. Re-paste your __Secure-next-auth.session-token.";
else if (status === 429) errMsg = "Perplexity rate limited. Wait a moment and retry.";
log?.warn?.("PPLX-WEB", errMsg);
const errResp = new Response(JSON.stringify({
error: { message: errMsg, type: "upstream_error", code: `HTTP_${status}` },
}), { status, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: PPLX_SSE_ENDPOINT, headers, transformedBody: pplxBody };
}
if (!response.body) {
const errResp = new Response(JSON.stringify({
error: { message: "Perplexity returned empty response body", type: "upstream_error" },
}), { status: 502, headers: { "Content-Type": "application/json" } });
return { response: errResp, url: PPLX_SSE_ENDPOINT, headers, transformedBody: pplxBody };
}
const cid = `chatcmpl-pplx-${crypto.randomUUID().slice(0, 12)}`;
const created = Math.floor(Date.now() / 1000);
let finalResponse;
if (stream) {
const sseStream = buildStreamingResponse(response.body, model, cid, created, parsed.history, parsed.currentMsg, signal);
finalResponse = new Response(sseStream, {
status: 200,
headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "X-Accel-Buffering": "no" },
});
} else {
finalResponse = await buildNonStreamingResponse(response.body, model, cid, created, parsed.history, parsed.currentMsg, signal);
}
return { response: finalResponse, url: PPLX_SSE_ENDPOINT, headers, transformedBody: pplxBody };
}
}
export { parseOpenAIMessages, buildQuery, buildPplxRequestBody, formatToolsHint, sessionKey };
export default PerplexityWebExecutor;

View File

@@ -53,6 +53,11 @@ const ALIAS_TO_PROVIDER_ID = {
vertex: "vertex",
vxp: "vertex-partner",
"vertex-partner": "vertex-partner",
// Web cookie providers
gw: "grok-web",
"grok-web": "grok-web",
pw: "perplexity-web",
"perplexity-web": "perplexity-web",
};
/**

View File

@@ -206,7 +206,7 @@ export async function refreshCodexToken(refreshToken, log) {
grant_type: "refresh_token",
refresh_token: refreshToken,
client_id: PROVIDERS.codex.clientId,
scope: "openid profile email offline_access",
scope: "openid profile email",
}),
});