Spaces:
Running
Running
Commit ·
d4f2db0
1
Parent(s): a09c97b
feat: integrate FreeDeepseekAPI background proxy and routing in AIService
Browse filesFiles changed (6)
18 unmodified lines192021222324252618 unmodified linesWORKDIR /appENV ASPNETCORE_URLS=http://+:7860EXPOSE 7860# Copy published .NET appCOPY --from=build /app/publish .# Set entrypointENTRYPOINT ["dotnet", "FlowAPI.dll"]18 unmodified lines19202122232425262728293031323334353618 unmodified linesWORKDIR /appENV ASPNETCORE_URLS=http://+:7860EXPOSE 7860# Install Node.js 20RUN apt-get update && apt-get install -y curl && \curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \apt-get install -y nodejs && \rm -rf /var/lib/apt/lists/*# Copy published .NET appCOPY --from=build /app/publish .# Copy FreeDeepseekAPI proxyCOPY FlowAPI/FreeDeepseekAPI ./FreeDeepseekAPI# Set entrypointENTRYPOINT ["dotnet", "FlowAPI.dll"]
121 unmodified lines122123124125126127636 unmodified lines764765766767768121 unmodified linesprivate async Task<string> CallGeminiApiAsync(object[] contents, string? systemInstruction = null, string? responseSchemaJson = null){var apiKey = _config["Gemini:ApiKey"] ?? Environment.GetEnvironmentVariable("GEMINI_API_KEY");if (string.IsNullOrEmpty(apiKey)){636 unmodified linespublic List<GeminiNode> NewNodes { get; set; } = new();public List<GeminiEdge> NewEdges { get; set; } = new();}}}121 unmodified lines122123124125126127128129130131132133134636 unmodified lines771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887121 unmodified linesprivate async Task<string> CallGeminiApiAsync(object[] contents, string? systemInstruction = null, string? responseSchemaJson = null){var deepseekToken = Environment.GetEnvironmentVariable("DEEPSEEK_TOKEN");var deepseekCookie = Environment.GetEnvironmentVariable("DEEPSEEK_COOKIE");if (!string.IsNullOrEmpty(deepseekToken) && !string.IsNullOrEmpty(deepseekCookie)){return await CallDeepSeekProxyApiAsync(contents, systemInstruction, responseSchemaJson);}var apiKey = _config["Gemini:ApiKey"] ?? Environment.GetEnvironmentVariable("GEMINI_API_KEY");if (string.IsNullOrEmpty(apiKey)){636 unmodified linespublic List<GeminiNode> NewNodes { get; set; } = new();public List<GeminiEdge> NewEdges { get; set; } = new();}private async Task<string> CallDeepSeekProxyApiAsync(object[] contents, string? systemInstruction = null, string? responseSchemaJson = null){var messages = new List<object>();if (responseSchemaJson != null){var schemaInstructions = $"\n\n[STRICT REQUIREMENT] You MUST respond ONLY with a JSON object that strictly adheres to the following JSON schema:\n{responseSchemaJson}\nDo not wrap the JSON in markdown code blocks like ```json ... ```, just output the raw JSON text.";if (!string.IsNullOrEmpty(systemInstruction)){systemInstruction += schemaInstructions;}else{systemInstruction = schemaInstructions;}}if (!string.IsNullOrEmpty(systemInstruction)){messages.Add(new { role = "system", content = systemInstruction });}foreach (var content in contents){try{var json = JsonSerializer.Serialize(content);using var doc = JsonDocument.Parse(json);var root = doc.RootElement;string role = "user";if (root.TryGetProperty("role", out var roleProp)){var rStr = roleProp.GetString();role = rStr == "model" ? "assistant" : (rStr == "system" ? "system" : "user");}string text = string.Empty;if (root.TryGetProperty("parts", out var partsProp) && partsProp.ValueKind == JsonValueKind.Array && partsProp.GetArrayLength() > 0){var firstPart = partsProp[0];if (firstPart.TryGetProperty("text", out var textProp)){text = textProp.GetString() ?? string.Empty;}}if (!string.IsNullOrEmpty(text)){messages.Add(new { role = role, content = text });}}catch (Exception ex){Console.WriteLine($"[DeepSeekProxy] Error mapping message: {ex.Message}");}}var requestBody = new{model = "deepseek-chat",messages = messages,stream = false};using var httpClient = new HttpClient();httpClient.Timeout = TimeSpan.FromMinutes(5);var jsonPayload = JsonSerializer.Serialize(requestBody);using var requestContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");HttpResponseMessage? response = null;string responseString = string.Empty;int maxRetries = 3;int delayMs = 1500;for (int attempt = 1; attempt <= maxRetries; attempt++){response = await httpClient.PostAsync("http://localhost:9655/v1/chat/completions", requestContent);responseString = await response.Content.ReadAsStringAsync();if (response.IsSuccessStatusCode){break;}int statusCode = (int)response.StatusCode;if (attempt < maxRetries && (statusCode == 503 || statusCode == 429 || statusCode == 500 || statusCode == 502)){await Task.Delay(delayMs * attempt);continue;}throw new Exception($"DeepSeek Proxy API error (Status {response.StatusCode}): {responseString}");}using var resDoc = JsonDocument.Parse(responseString);var textResponse = resDoc.RootElement.GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString();if (string.IsNullOrEmpty(textResponse)){throw new Exception("Empty response received from DeepSeek Proxy API.");}return textResponse;}}}
19 unmodified lines2021222319 unmodified lines<ProjectReference Include="..\FlowAPI.Infrastructure\FlowAPI.Infrastructure.csproj" /></ItemGroup></Project>19 unmodified lines2021222324252627282919 unmodified lines<ProjectReference Include="..\FlowAPI.Infrastructure\FlowAPI.Infrastructure.csproj" /></ItemGroup><ItemGroup><None Update="FreeDeepseekAPI\**"><CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory></None></ItemGroup></Project>
123456{ "name": "free-deepseek-api", "version": "0.1.0", "main": "server.js", "type": "commonjs"}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400#!/usr/bin/env node/** * OpenAI-compatible API server wrapping DeepSeek Web API * Supports BOTH streaming (SSE) and non-streaming modes * Includes tool calling: injects tool definitions into system prompt, * parses LLM text responses for TOOL_CALL patterns, returns OpenAI tool_calls format. * * Per-agent sessions: each unique `user` field gets its own DeepSeek web session. * Auto-reset: sessions reset when message chain > 50 messages or age > 2 hours. * Listens on 0.0.0.0:9655 */
const http = require('http');const fs = require('fs');const os = require('os');const path = require('path');const readline = require('readline');const { spawnSync } = require('child_process');
const SERVER_HOST = os.hostname(); // Dynamic hostname detectionconst SERVER_PUBLIC_IP = (() => { try { const interfaces = os.networkInterfaces(); for (const name of Object.keys(interfaces)) { for (const iface of interfaces[name]) { if (iface.family === 'IPv4' && !iface.internal) return iface.address; } } } catch (e) {} return 'localhost';})();
const FORGETMEAI_WATERMARK = 't.me/forgetmeai';const PORT = Number(process.env.PORT || 9655);const HOST = process.env.HOST || '0.0.0.0';function formatWatermark(prefix = 'ForgetMeAI') { return `${prefix}: ${FORGETMEAI_WATERMARK}`; }function printBanner() { console.log(`███████ ██████ ███████ ███████ ██████ ███████ ███████ ███████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████ ██████ █████ █████ ██ ██ █████ █████ █████ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ███████ ███████ ██████ ███████ ███████ ███████ ██ ██
FreeDeepseekAPI — API-прокси для DeepSeek Web Chat ${formatWatermark()}`);}function prompt(question) { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); return new Promise(resolve => rl.question(question, ans => { rl.close(); resolve(ans); }));}function isTruthy(value) { return typeof value === 'string' && ['1','true','yes','on'].includes(value.trim().toLowerCase()); }
// === Per-Agent Session Store ===const sessions = new Map(); // keyed by agent ID (from `user` field)const MAX_HISTORY_LENGTH = 15;const MAX_HISTORY_CHARS = 10000;const MAX_MESSAGE_DEPTH = 100; // auto-reset after this many messagesconst SESSION_TTL_MS = 2 * 60 * 60 * 1000; // 2 hours
// === DeepSeek Web API Config — loaded from external config file ===const DS_CONFIG_PATH = process.env.DEEPSEEK_AUTH_PATH || path.join(__dirname, 'deepseek-auth.json');let DS_CONFIG = {};let BASE_HEADERS = {};function buildBaseHeaders() { return { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36", "x-client-platform": "web", "x-client-version": "2.0.0", "x-client-locale": "ru", "x-client-timezone-offset": "14400", "x-app-version": "2.0.0", "Authorization": `Bearer ${DS_CONFIG.token || ''}`, "x-hif-dliq": DS_CONFIG.hif_dliq || '', "x-hif-leim": DS_CONFIG.hif_leim || '', "Origin": "https://chat.deepseek.com", "Referer": "https://chat.deepseek.com/", "Cookie": DS_CONFIG.cookie || '', "Content-Type": "application/json", };}function loadDeepSeekConfig({ fatal = true } = {}) { try { if (fs.existsSync(DS_CONFIG_PATH)) { const raw = fs.readFileSync(DS_CONFIG_PATH, 'utf8'); DS_CONFIG = JSON.parse(raw); console.log(`[DS-API] Loaded auth config from ${DS_CONFIG_PATH}`); } else { DS_CONFIG = { token: process.env.DEEPSEEK_TOKEN || '', cookie: process.env.DEEPSEEK_COOKIE || '', hif_dliq: process.env.DEEPSEEK_HIF_DLIQ || '', hif_leim: process.env.DEEPSEEK_HIF_LEIM || '', wasmUrl: process.env.DEEPSEEK_WASM_URL || 'https://fe-static.deepseek.com/chat/static/sha3_wasm_bg.7b9ca65ddd.wasm', }; console.log(`[DS-API] Loaded auth config from process.env`); } BASE_HEADERS = buildBaseHeaders(); return true; } catch (e) { DS_CONFIG = {}; BASE_HEADERS = buildBaseHeaders(); if (fatal) { console.error(`[DS-API] FATAL: Could not load auth config: ${e.message}`); process.exit(1); } return false; }}function hasAuthConfig() { return !!(DS_CONFIG.token && DS_CONFIG.cookie); }loadDeepSeekConfig({ fatal: false });
function createSession() { return { id: null, parentMessageId: null, createdAt: null, messageCount: 0, history: [], };}
function getOrCreateAgentSession(agentId) { if (!sessions.has(agentId)) { sessions.set(agentId, createSession()); } return sessions.get(agentId);}
async function solvePOW(challenge) { const resp = await fetch(DS_CONFIG.wasmUrl); const wasmBytes = await resp.arrayBuffer(); const mod = await WebAssembly.instantiate(wasmBytes, { wbg: {} }); const e = mod.instance.exports; const encoder = new TextEncoder(); const prefix = challenge.salt + '_' + challenge.expire_at + '_'; const cBytes = encoder.encode(challenge.challenge); const pBytes = encoder.encode(prefix); const cP = e.__wbindgen_export_0(cBytes.length, 1) >>> 0; const pP = e.__wbindgen_export_0(pBytes.length, 1) >>> 0; new Uint8Array(e.memory.buffer, cP, cBytes.length).set(cBytes); new Uint8Array(e.memory.buffer, pP, pBytes.length).set(pBytes); const sp = e.__wbindgen_add_to_stack_pointer(-16); e.wasm_solve(sp, cP, cBytes.length, pP, pBytes.length, challenge.difficulty); const dv = new DataView(e.memory.buffer); const code = dv.getInt32(sp, true); const ans = dv.getFloat64(sp + 8, true); e.__wbindgen_add_to_stack_pointer(16); if (code === 0 || !Number.isFinite(ans) || ans <= 0) throw new Error('POW failed'); return Math.floor(ans);}
const MODEL_CONFIGS = { // DeepSeek Web real model_type: default / UI name: "Быстрый". // Public model family: DeepSeek-V3.2-Exp chat mode (fast, no visible reasoning). 'deepseek-chat': { model_type: 'default', thinking_enabled: false, search_enabled: false, real_model: 'DeepSeek-V4-Flash non-thinking (DeepSeek Web “Быстрый” / default)', capabilities: { reasoning: false, web_search: false, files: true }, supported: true, }, 'deepseek-v3': { model_type: 'default', thinking_enabled: false, search_enabled: false, real_model: 'DeepSeek-V4-Flash non-thinking (DeepSeek Web “Быстрый” / default)', capabilities: { reasoning: false, web_search: false, files: true }, supported: true, }, 'deepseek-default': { model_type: 'default', thinking_enabled: false, search_enabled: false, real_model: 'DeepSeek-V4-Flash non-thinking (DeepSeek Web “Быстрый” / default)', capabilities: { reasoning: false, web_search: false, files: true }, supported: true, }, // Same DeepSeek Web default model, but with thinking_enabled=true. UI exposes it as thinking/reasoning mode. 'deepseek-reasoner': { model_type: 'default', thinking_enabled: true, search_enabled: false, real_model: 'DeepSeek-V4-Flash thinking mode (DeepSeek Web “Быстрый” + thinking_enabled)', capabilities: { reasoning: true, web_search: false, files: true }, supported: true, }, 'deepseek-r1': { model_type: 'default', thinking_enabled: true, search_enabled: false, real_model: 'DeepSeek-V4-Flash thinking mode; R1-compatible alias, not a separate R1 model_type in current Web API', capabilities: { reasoning: true, web_search: false, files: true }, supported: true, }, 'deepseek-chat-search': { model_type: 'default', thinking_enabled: false, search_enabled: true, real_model: 'DeepSeek-V4-Flash non-thinking (DeepSeek Web “Быстрый” / default) + web search', capabilities: { reasoning: false, web_search: true, files: true }, supported: true, }, 'deepseek-default-search': { model_type: 'default', thinking_enabled: false, search_enabled: true, real_model: 'DeepSeek-V4-Flash non-thinking (DeepSeek Web “Быстрый” / default) + web search', capabilities: { reasoning: false, web_search: true, files: true }, supported: true, }, 'deepseek-reasoner-search': { model_type: 'default', thinking_enabled: true, search_enabled: true, real_model: 'DeepSeek-V4-Flash thinking mode + web search', capabilities: { reasoning: true, web_search: true, files: true }, supported: true, }, 'deepseek-r1-search': { model_type: 'default', thinking_enabled: true, search_enabled: true, real_model: 'DeepSeek-V4-Flash thinking mode + web search; R1-compatible alias', capabilities: { reasoning: true, web_search: true, files: true }, supported: true, }, // DeepSeek Web UI name: “Эксперт”. Requires current web client headers (x-client-version=2.0.0). 'deepseek-expert': { model_type: 'expert', thinking_enabled: false, search_enabled: false, real_model: 'DeepSeek Web “Эксперт” (limited resources)', capabilities: { reasoning: false, web_search: false, files: false }, supported: true, }, 'deepseek-v4-pro': { model_type: 'expert', thinking_enabled: true, search_enabled: false, real_model: 'DeepSeek Web “Эксперт” + thinking mode (exposed as deepseek-v4-pro alias)', capabilities: { reasoning: true, web_search: false, files: false }, supported: true, }, 'deepseek-expert-search': { model_type: 'expert', thinking_enabled: false, search_enabled: true, real_model: 'DeepSeek Web “Эксперт” + search requested, but Expert has search_feature=null in remote config', capabilities: { reasoning: false, web_search: false, files: false }, supported: false, unavailable_reason: 'Expert mode is rejected; remote config says search is not available for Expert.', }, 'deepseek-vision': { model_type: 'vision', thinking_enabled: false, search_enabled: false, real_model: 'DeepSeek Web “Распознавание” / image understanding beta', capabilities: { reasoning: false, web_search: false, files: true, vision: true }, supported: false, unavailable_reason: 'Current Web API returns: Vision is temporarily unavailable (backend_err_by_model).', },};
const SUPPORTED_MODEL_IDS = Object.keys(MODEL_CONFIGS).filter(id => MODEL_CONFIGS[id].supported);const ALL_MODEL_CAPABILITIES = Object.fromEntries(Object.entries(MODEL_CONFIGS).map(([id, cfg]) => [id, { id, real_model: cfg.real_model, model_type: cfg.model_type, thinking_enabled: cfg.thinking_enabled, search_enabled: cfg.search_enabled, capabilities: cfg.capabilities, supported: cfg.supported, unavailable_reason: cfg.unavailable_reason || null,}]));
function resolveModelConfig(model) { const requested = String(model || 'deepseek-chat').toLowerCase(); return MODEL_CONFIGS[requested] || MODEL_CONFIGS['deepseek-chat'];}function isKnownModel(model) { return Object.prototype.hasOwnProperty.call(MODEL_CONFIGS, String(model || '').toLowerCase()); }function isSupportedModel(model) { return resolveModelConfig(model).supported === true; }
async function askDeepSeekStream(prompt, agentId, model = 'deepseek-default') { const modelCfg = resolveModelConfig(model); const session = getOrCreateAgentSession(agentId); const agentTag = `[${agentId}]`;
// Auto-reset on deep message chain if (session.id && session.messageCount >= MAX_MESSAGE_DEPTH) { console.log(`${agentTag} Session ${session.id} hit ${session.messageCount} messages. Auto-resetting.`); session.id = null; session.parentMessageId = null; session.createdAt = null; session.messageCount = 0; // History preserved for context injection }
// Reset expired sessions (DeepSeek web sessions last ~1-2 hours) if (session.id && session.createdAt && (Date.now() - session.createdAt > SESSION_TTL_MS)) { console.log(`${agentTag} Session ${session.id} expired (age: ${Math.round((Date.now() - session.createdAt) / 60000)}min). Creating new...`); session.id = null; session.parentMessageId = null; session.createdAt = null; session.messageCount = 0; }
const cr = await fetch('https://chat.deepseek.com/api/v0/chat/create_pow_challenge', { method: 'POST', headers: BASE_HEADERS, body: JSON.stringify({ target_path: '/api/v0/chat/completion' }) }); const chalJson = JSON.parse(await cr.text()); const challenge = chalJson.data.biz_data.challenge; const answer = await solvePOW(challenge);
if (!session.id) { const sr = await fetch('https://chat.deepseek.com/api/v0/chat_session/create', { method: 'POST', headers: BASE_HEADERS, body: '{}' }); const sessionData = await sr.json(); session.id = sessionData.data.biz_data.chat_session?.id || sessionData.data.biz_data.id; session.parentMessageId = null; session.createdAt = Date.now(); session.messageCount = 0; console.log(`${agentTag} Created new session: ${session.id}`); } else { console.log(`${agentTag} Reusing session: ${session.id} (parent: ${session.parentMessageId}, msg#${session.messageCount})`); }
const powB64 = Buffer.from(JSON.stringify({ algorithm: challenge.algorithm, challenge: challenge.challenge, salt: challenge.salt, answer: answer, signature: challenge.signature, target_path: '/api/v0/chat/completion' })).toString('base64'); const resp = await fetch('https://chat.deepseek.com/api/v0/chat/completion', { method: 'POST', headers: { ...BASE_HEADERS, 'X-DS-PoW-Response': powB64 }, body: JSON.stringify({ chat_session_id: session.id, parent_message_id: session.parentMessageId, model_type: modelCfg.model_type, prompt: prompt, ref_file_ids: [], thinking_enabled: modelCfg.thinking_enabled, search_enabled: modelCfg.search_enabled, action: null, preempt: false, }) });
// If session expired, reset and retry once if (resp.status !== 200) { const errText = await resp.text(); console.log(`${agentTag} Session error (${resp.status}): ${errText.substring(0, 100)}`); if (resp.status === 400 || resp.status === 404 || resp.status === 500) { console.log(`${agentTag} Session ${session.id} expired. Creating new session...`); session.id = null; session.parentMessageId = null; session.createdAt = null; session.messageCount = 0;
const sr2 = await fetch('https://chat.deepseek.com/api/v0/chat_session/create', { method: 'POST', headers: BASE_HEADERS, body: '{}' }); const sessionData2 = await sr2.json(); session.id = sessionData2.data.biz_data.chat_session?.id || sessionData2.data.biz_data.id; session.parentMessageId = null; session.createdAt = Date.now(); console.log(`${agentTag} Created new session: ${session.id}`);
const newPowB64 = Buffer.from(JSON.stringify({ algorithm: challenge.algorithm, challenge: challenge.challenge, salt: challenge.salt, answer: answer, signature: challenge.signature, target_path: '/api/v0/chat/completion' })).toString('base64'); const resp2 = await fetch('https://chat.deepseek.com/api/v0/chat/completion', { method: 'POST', headers: { ...BASE_HEADERS, 'X-DS-PoW-Response': newPowB64 }, body: JSON.stringify({ chat_session_id: session.id, parent_message_id: null, model_type: modelCfg.model_type, prompt: prompt, ref_file_ids: [], thinking_enabled: modelCfg.thinking_enabled, search_enabled: modelCfg.search_enabled, action: null, preempt: false, }) }); return { resp: resp2, agentId }; } }
return { resp, agentId };}
// === Tool Calling Support ===
function formatToolDefinitions(tools) { if (!tools || tools.length === 0) return ''; let text = '\n\n--- TOOL REQUEST SYSTEM ---\n'; text += 'You are an AI that ONLY REASONS and REQUESTS tool executions. You do NOT run any commands yourself.\n'; text += 'When you need data from the local server, REQUEST exactly one tool call. Prefer strict JSON:\n'; text += '{"tool_call":{"name":"<function_name>","arguments":{...}}}\n\n'; text += 'Legacy format is also accepted: TOOL_CALL: <function_name>\narguments: <JSON arguments>\n\n'; text += 'Your response will be sent to the local gateway, which executes the command and sends the output back in the next message.\n\n'; text += 'RULES:\n'; text += '1. You ONLY output the tool request — you never run anything yourself\n'; text += '2. Do NOT simulate, guess, or fabricate command output — wait for the actual result\n'; text += '3. The tool runs on ' + SERVER_HOST + ' (' + SERVER_PUBLIC_IP + '), the local server — NOT on DeepSeek\n'; text += '4. After the tool executes, the result will be sent to you as a new user/tool message\n'; text += '5. Never add explanation before or after the tool request when requesting a tool\n'; text += '6. Keep arguments compact. Do not include large file contents unless the tool schema requires it.\n\n'; text += 'Available functions:\n'; for (const tool of tools) { if (tool.type === 'function' && tool.function) { const fn = tool.function; text += `\n## ${fn.name}\n`; text += `${fn.description || ''}\n`; if (fn.parameters) { text += `Parameters: ${JSON.stringify(fn.parameters)}\n`; } } } text += '\n--- END TOOL REQUEST SYSTEM ---\n'; text += '\nREMEMBER: Request tools only with strict JSON or TOOL_CALL legacy format. Never simulate results.'; return text;}
function extractBalancedJsonAt(text, startIndex) { let braceDepth = 0; let inString = false; let escape = false; for (let i = startIndex; i < text.length; i++) { const ch = text[i]; if (escape) { escape = false; continue; } if (ch === '\\' && inString) { escape = true; continue; } if (ch === '"') { inString = !inString; continue; } if (!inString) { if (ch === '{') braceDepth++; if (ch === '}') { braceDepth--; if (braceDepth === 0) return text.substring(startIndex, i + 1); } } } return null;}
function coerceToolCallObject(obj) { if (!obj || typeof obj !== 'object') return null; const candidate = obj.tool_call || obj.tool || obj.function_call || obj; if (!candidate || typeof candidate !== 'object') return null; const fn = candidate.function && typeof candidate.function === 'object' ? candidate.function : candidate; const name = fn.name || candidate.name || obj.name; let args = fn.arguments ?? candidate.arguments ?? candidate.input ?? obj.arguments ?? obj.input ?? {}; if (!name || typeof name !== 'string') return null; if (typeof args === 'string') { try { args = JSON.parse(args); } catch (e) { args = { raw: args }; } } if (!args || typeof args !== 'object' || Array.isArray(args)) args = { value: args }; return { name, arguments: JSON.stringify(args) };}
function parseJsonToolCandidate(raw, label = 'json') { if (!raw) return null; try { const parsed = JSON.parse(raw); const tc = coerceToolCallObject(parsed); if (tc) { console.log(`[parseToolCall] SUCCESS ${label}: ${tc.name} (args=${tc.arguments.length} chars)`); return tc; } } catch (e) { console.log(`[parseToolCall] ${label} JSON.parse failed: ${e.message.substring(0, 100)}`); } return null;}
function parseToolCall(text) { if (!text || typeof text !== 'string') return null;
// XML-ish wrappers used by some agent prompts. const xmlMatch = text.match(/<tool_call[^>]*>([\s\S]*?)<\/tool_call>/i); if (xmlMatch) { const inner = xmlMatch[1].trim(); const tc = parseJsonToolCandidate(inner, 'xml'); if (tc) return tc; }
// Fenced JSON blocks. const fenceRe = /```(?:json)?\s*([\s\S]*?)```/gi; let fence; while ((fence = fenceRe.exec(text)) !== null) { const tc = parseJsonToolCandidate(fence[1].trim(), 'fenced'); if (tc) return tc; }
// Legacy TOOL_CALL: name + first balanced JSON object after it. const match = text.match(/TOOL_CALL:\s*([\w-]+)\s*/i); if (match) { const name = match[1]; const afterMatch = text.substring(match.index + match[0].length); const braceIdx = afterMatch.indexOf('{'); if (braceIdx !== -1) { const rawJson = extractBalancedJsonAt(afterMatch, braceIdx); if (rawJson) { try { const args = JSON.parse(rawJson); console.log(`[parseToolCall] SUCCESS legacy: ${name} (args=${rawJson.length} chars)`); return { name, arguments: JSON.stringify(args) }; } catch (e) { console.log(`[parseToolCall] legacy JSON.parse failed: ${e.message.substring(0,100)}`); } } else { console.log(`[parseToolCall] TOOL_CALL:${name} found but JSON braces are unbalanced`); } } else { console.log(`[parseToolCall] TOOL_CALL:${name} found but no { after it`); } }
// First balanced JSON object in the whole response. Supports: // {"tool_call":{"name":"...","arguments":{...}}}, {"name":"...","arguments":{...}}, etc. for (let i = 0; i < text.length; i++) { if (text[i] !== '{') continue; const rawJson = extractBalancedJsonAt(text, i); if (!rawJson) continue; const tc = parseJsonToolCandidate(rawJson, 'inline'); if (tc) return tc; }
console.log(`[parseToolCall] No tool call match in ${text.length} chars`); return null;}
/** * Strip surrogate characters and other problematic Unicode from text * to prevent httpx/urlencode crashes when the gateway sends to Telegram. */function sanitizeContent(text) { return text.replace(/[\ud800-\udfff]/g, '');}
function estimateTokens(text) { return text ? Math.ceil(String(text).length / 4) : 0;}
function buildUsage(prompt, content, reasoningContent = '') { const promptTokens = estimateTokens(prompt); const contentTokens = estimateTokens(content); const reasoningTokens = estimateTokens(reasoningContent); const completionTokens = contentTokens + reasoningTokens; return { prompt_tokens: promptTokens, completion_tokens: completionTokens, total_tokens: promptTokens + completionTokens, completion_tokens_details: { reasoning_tokens: reasoningTokens } };}
function buildToolCallResponse(toolCall, model = 'deepseek-default', prompt = '', reasoningContent = '') { const id = 'call_' + Date.now() + '_' + Math.random().toString(36).substring(2, 8); const message = { role: 'assistant', content: null, tool_calls: [{ id: id, type: 'function', function: { name: toolCall.name, arguments: toolCall.arguments } }] }; // Do not attach reasoning to tool-call turns. Some agent clients treat any // reasoning/text payload as a final assistant answer and stop their tool loop. return { id: 'ds-' + Date.now(), object: 'chat.completion', created: Math.floor(Date.now() / 1000), model, choices: [{ index: 0, message, finish_reason: 'tool_calls' }], usage: buildUsage(prompt, '', reasoningContent), watermark: FORGETMEAI_WATERMARK };}
function buildTextResponse(content, prompt, model = 'deepseek-default', reasoningContent = '') { const message = { role: 'assistant', content }; if (reasoningContent) message.reasoning_content = reasoningContent; return { id: 'ds-' + Date.now(), object: 'chat.completion', created: Math.floor(Date.now() / 1000), model, choices: [{ index: 0, message, finish_reason: 'stop' }], usage: buildUsage(prompt, content, reasoningContent), watermark: FORGETMEAI_WATERMARK };}
function normalizeMessageContent(content) { if (content === null || content === undefined) return ''; if (typeof content === 'string') return content; if (Array.isArray(content)) { return content.map(part => { if (typeof part === 'string') return part; if (!part || typeof part !== 'object') return ''; if (part.type === 'text' || part.type === 'input_text' || part.type === 'output_text') return part.text || ''; if (part.type === 'tool_result') return `[Tool Result ${part.tool_use_id || ''}]\n${normalizeMessageContent(part.content)}`; if (part.type === 'image_url') return `[Image: ${part.image_url?.url || ''}]`; return part.text || part.content || JSON.stringify(part); }).filter(Boolean).join('\n'); } return String(content);}
function normalizeAnthropicTools(tools = []) { return (tools || []).map(tool => ({ type: 'function', function: { name: tool.name, description: tool.description || '', parameters: tool.input_schema || tool.parameters || { type: 'object', properties: {} } } })).filter(tool => tool.function.name);}
function normalizeResponsesTools(tools = []) { return (tools || []).map(tool => { if (tool.type === 'function' && tool.function) return tool; if (tool.type === 'function' && tool.name) { return { type: 'function', function: { name: tool.name, description: tool.description || '', parameters: tool.parameters || { type: 'object', properties: {} } } }; } return null; }).filter(Boolean);}
function normalizeResponsesInput(input) { if (typeof input === 'string') return [{ role: 'user', content: input }]; if (!Array.isArray(input)) return []; const messages = []; for (const item of input) { if (!item || typeof item !== 'object') continue; if (item.type === 'message') { messages.push({ role: item.role || 'user', content: normalizeMessageContent(item.content) }); } else if (item.role) { messages.push({ role: item.role, content: normalizeMessageContent(item.content) }); } else if (item.type === 'function_call_output') { messages.push({ role: 'tool', tool_call_id: item.call_id, content: item.output || '' }); } else if (item.type === 'input_text') { messages.push({ role: 'user', content: item.text || '' }); } } return messages;}
function normalizeApiParams(params, apiMode) { if (apiMode === 'anthropic') { const messages = []; if (params.system) messages.push({ role: 'system', content: normalizeMessageContent(params.system) }); for (const msg of params.messages || []) { if (msg.role === 'assistant' && Array.isArray(msg.content)) { const toolUses = msg.content.filter(part => part && part.type === 'tool_use'); const text = normalizeMessageContent(msg.content.filter(part => !part || part.type !== 'tool_use')); if (text) messages.push({ role: 'assistant', content: text }); for (const tu of toolUses) { messages.push({ role: 'assistant', content: null, tool_calls: [{ id: tu.id, type: 'function', function: { name: tu.name, arguments: JSON.stringify(tu.input || {}) } }] }); } } else if (msg.role === 'user' && Array.isArray(msg.content) && msg.content.some(part => part && part.type === 'tool_result')) { for (const part of msg.content) { if (part && part.type === 'tool_result') messages.push({ role: 'tool', tool_call_id: part.tool_use_id, content: normalizeMessageContent(part.content) }); else messages.push({ role: 'user', content: normalizeMessageContent(part) }); } } else { messages.push({ role: msg.role || 'user', content: normalizeMessageContent(msg.content) }); } } return { ...params, model: params.model || 'deepseek-chat', messages, tools: normalizeAnthropicTools(params.tools || []), stream: params.stream === true, user: params.metadata?.user_id || params.user, }; } if (apiMode === 'responses') { const messages = normalizeResponsesInput(params.input); if (params.instructions) messages.unshift({ role: 'system', content: params.instructions }); return { ...params, model: params.model || 'deepseek-chat', messages, tools: normalizeResponsesTools(params.tools || []), stream: params.stream === true, user: params.user, }; } return params;}
function safeJsonParseObject(text, fallback = {}) { try { const parsed = JSON.parse(text || '{}'); return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : fallback; } catch (e) { return fallback; }}
function toAnthropicResponse(openaiResp) { const choice = openaiResp.choices[0]; const msg = choice.message || {}; const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0; const content = []; if (hasToolCalls) { for (const tc of msg.tool_calls) { content.push({ type: 'tool_use', id: tc.id, name: tc.function.name, input: safeJsonParseObject(tc.function.arguments) }); } } else { content.push({ type: 'text', text: msg.content || '' }); } const response = { id: 'msg_' + openaiResp.id, type: 'message', role: 'assistant', model: openaiResp.model, content, stop_reason: choice.finish_reason === 'tool_calls' ? 'tool_use' : 'end_turn', stop_sequence: null, usage: { input_tokens: openaiResp.usage?.prompt_tokens || 0, output_tokens: openaiResp.usage?.completion_tokens || 0, }, watermark: FORGETMEAI_WATERMARK, }; if (!hasToolCalls && msg.reasoning_content) response.reasoning_content = msg.reasoning_content; return response;}
function writeSse(res, event, data) { if (event) res.write(`event: ${event}\n`); res.write(`data: ${JSON.stringify(data)}\n\n`);}
function sendAnthropicStream(res, openaiResp) { res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*' }); const choice = openaiResp.choices[0]; const msg = choice.message || {}; const message = toAnthropicResponse(openaiResp); const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0; writeSse(res, 'message_start', { type: 'message_start', message: { ...message, content: [] } });
// Anthropic-compatible clients expect a tool turn to be made of tool_use // content blocks. If we emit DeepSeek reasoning as a text block before the // tool_use block, some agents treat the turn as a normal text answer and do // not execute the tool. Keep tool streaming clean: tool_use blocks only. if (hasToolCalls) { msg.tool_calls.forEach((tc, i) => { writeSse(res, 'content_block_start', { type: 'content_block_start', index: i, content_block: { type: 'tool_use', id: tc.id, name: tc.function.name, input: {} } }); writeSse(res, 'content_block_delta', { type: 'content_block_delta', index: i, delta: { type: 'input_json_delta', partial_json: tc.function.arguments || '{}' } }); writeSse(res, 'content_block_stop', { type: 'content_block_stop', index: i }); }); writeSse(res, 'message_delta', { type: 'message_delta', delta: { stop_reason: 'tool_use', stop_sequence: null }, usage: message.usage }); } else { if (msg.reasoning_content) { writeSse(res, 'content_block_start', { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } }); writeSse(res, 'content_block_delta', { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: `[reasoning]\n${msg.reasoning_content}\n[/reasoning]\n` } }); writeSse(res, 'content_block_stop', { type: 'content_block_stop', index: 0 }); } const offset = msg.reasoning_content ? 1 : 0; writeSse(res, 'content_block_start', { type: 'content_block_start', index: offset, content_block: { type: 'text', text: '' } }); const text = msg.content || ''; for (let i = 0; i < text.length; i += 80) { writeSse(res, 'content_block_delta', { type: 'content_block_delta', index: offset, delta: { type: 'text_delta', text: text.substring(i, i + 80) } }); } writeSse(res, 'content_block_stop', { type: 'content_block_stop', index: offset }); writeSse(res, 'message_delta', { type: 'message_delta', delta: { stop_reason: 'end_turn', stop_sequence: null }, usage: message.usage }); } writeSse(res, 'message_stop', { type: 'message_stop' }); res.end();}
function toResponsesResponse(openaiResp) { const choice = openaiResp.choices[0]; const msg = choice.message || {}; const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0; const output = []; if (!hasToolCalls && msg.reasoning_content) { output.push({ id: 'rs_' + Date.now(), type: 'reasoning', summary: [{ type: 'summary_text', text: msg.reasoning_content }] }); } if (hasToolCalls) { for (const tc of msg.tool_calls) { output.push({ type: 'function_call', id: 'fc_' + tc.id, call_id: tc.id, name: tc.function.name, arguments: tc.function.arguments || '{}' }); } } else { output.push({ id: 'msg_' + Date.now(), type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: msg.content || '', annotations: [] }] }); } return { id: openaiResp.id.replace(/^ds-/, 'resp_'), object: 'response', created_at: openaiResp.created, status: 'completed', model: openaiResp.model, output, output_text: msg.content || '', usage: { input_tokens: openaiResp.usage?.prompt_tokens || 0, output_tokens: openaiResp.usage?.completion_tokens || 0, total_tokens: openaiResp.usage?.total_tokens || 0, output_tokens_details: { reasoning_tokens: openaiResp.usage?.completion_tokens_details?.reasoning_tokens || 0 }, }, watermark: FORGETMEAI_WATERMARK, };}
function sendResponsesStream(res, openaiResp) { res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*' }); const response = toResponsesResponse(openaiResp); const choice = openaiResp.choices[0]; const msg = choice.message || {}; const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0; writeSse(res, 'response.created', { type: 'response.created', response: { ...response, status: 'in_progress', output: [] } }); writeSse(res, 'response.in_progress', { type: 'response.in_progress', response: { ...response, status: 'in_progress', output: [] } }); let outputIndex = 0; if (!hasToolCalls && msg.reasoning_content) { const reasoningItem = { id: 'rs_' + Date.now(), type: 'reasoning', summary: [], status: 'completed' }; writeSse(res, 'response.output_item.added', { type: 'response.output_item.added', output_index: outputIndex, item: { ...reasoningItem, status: 'in_progress' } }); writeSse(res, 'response.reasoning_summary_text.delta', { type: 'response.reasoning_summary_text.delta', output_index: outputIndex, summary_index: 0, delta: msg.reasoning_content }); writeSse(res, 'response.output_item.done', { type: 'response.output_item.done', output_index: outputIndex, item: { ...reasoningItem, summary: [{ type: 'summary_text', text: msg.reasoning_content }] } }); outputIndex++; } if (hasToolCalls) { msg.tool_calls.forEach((tc) => { const item = { type: 'function_call', id: 'fc_' + tc.id, call_id: tc.id, name: tc.function.name, arguments: tc.function.arguments || '{}', status: 'completed' }; writeSse(res, 'response.output_item.added', { type: 'response.output_item.added', output_index: outputIndex, item: { ...item, arguments: '', status: 'in_progress' } }); writeSse(res, 'response.function_call_arguments.delta', { type: 'response.function_call_arguments.delta', output_index: outputIndex, item_id: item.id, delta: item.arguments }); writeSse(res, 'response.function_call_arguments.done', { type: 'response.function_call_arguments.done', output_index: outputIndex, item_id: item.id, arguments: item.arguments }); writeSse(res, 'response.output_item.done', { type: 'response.output_item.done', output_index: outputIndex, item }); outputIndex++; }); } else { const text = msg.content || ''; const item = { id: 'msg_' + Date.now(), type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text, annotations: [] }] }; writeSse(res, 'response.output_item.added', { type: 'response.output_item.added', output_index: outputIndex, item: { ...item, status: 'in_progress', content: [] } }); writeSse(res, 'response.content_part.added', { type: 'response.content_part.added', output_index: outputIndex, content_index: 0, item_id: item.id, part: { type: 'output_text', text: '', annotations: [] } }); for (let i = 0; i < text.length; i += 80) { writeSse(res, 'response.output_text.delta', { type: 'response.output_text.delta', output_index: outputIndex, content_index: 0, item_id: item.id, delta: text.substring(i, i + 80) }); } writeSse(res, 'response.output_text.done', { type: 'response.output_text.done', output_index: outputIndex, content_index: 0, item_id: item.id, text }); writeSse(res, 'response.content_part.done', { type: 'response.content_part.done', output_index: outputIndex, content_index: 0, item_id: item.id, part: item.content[0] }); writeSse(res, 'response.output_item.done', { type: 'response.output_item.done', output_index: outputIndex, item }); } writeSse(res, 'response.completed', { type: 'response.completed', response }); res.write('data: [DONE]\n\n'); res.end();}
function sendOpenAIStream(res, openaiResp) { res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*' }); const choice = openaiResp.choices[0]; const msg = choice.message || {}; const id = openaiResp.id; const created = openaiResp.created; const model = openaiResp.model; const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0; if (!hasToolCalls && msg.reasoning_content) { for (let i = 0; i < msg.reasoning_content.length; i += 50) { const chunk = msg.reasoning_content.substring(i, i + 50); res.write(`data: ${JSON.stringify({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { reasoning_content: chunk }, finish_reason: null }] })}\n\n`); } } if (hasToolCalls) { res.write(`data: ${JSON.stringify({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { role: 'assistant', content: null, tool_calls: msg.tool_calls }, finish_reason: null }] })}\n\n`); res.write(`data: ${JSON.stringify({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: {}, finish_reason: 'tool_calls' }] })}\n\ndata: [DONE]\n\n`); } else { for (let i = 0; i < (msg.content || '').length; i += 50) { const chunk = msg.content.substring(i, i + 50); res.write(`data: ${JSON.stringify({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { content: chunk }, finish_reason: null }] })}\n\n`); } res.write(`data: ${JSON.stringify({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\ndata: [DONE]\n\n`); } res.end();}
function storeHistory(agentId, prompt, content, toolCall) { const session = getOrCreateAgentSession(agentId); const assistantResponse = toolCall ? `TOOL_CALL: ${toolCall.name}\narguments: ${toolCall.arguments}` : content; // Save last 500 chars of the prompt for history context const shortPrompt = prompt.length > 500 ? '...' + prompt.substring(prompt.length - 500) : prompt; session.history.push({ user: shortPrompt, assistant: assistantResponse }); while (session.history.length > MAX_HISTORY_LENGTH) session.history.shift(); let historyChars = session.history.reduce((sum, e) => sum + e.user.length + e.assistant.length, 0); while (historyChars > MAX_HISTORY_CHARS && session.history.length > 1) { const removed = session.history.shift(); historyChars -= removed.user.length + removed.assistant.length; }}
// Extract MEDIA: paths from tool results that contain screenshot pathsfunction extractScreenshotPaths(messages) { const paths = []; const fs = require('fs'); for (const msg of messages) { if (msg.role === 'tool' && msg.content) { // Look for screenshot_path or path fields in JSON tool results // These come DIRECTLY from browser_vision — always the real path const pngMatch = msg.content.match(/["'](screenshot_path|path)["']\s*:\s*["']([^"']+\.(?:png|jpg|jpeg|webp|gif))["']/i); if (pngMatch) { const filePath = pngMatch[2]; if (filePath.startsWith('/') && fs.existsSync(filePath)) { paths.push(`MEDIA:${filePath}`); } } // Also catch plain MEDIA: tags const mediaMatch = msg.content.match(/MEDIA:(\S+)/g); if (mediaMatch) { for (const tag of mediaMatch) { const extractedPath = tag.replace(/^MEDIA:/, ''); if (fs.existsSync(extractedPath) && !paths.includes(tag)) { paths.push(tag); } } } } // Check user/assistant messages for paths mentioned in conversation text // Only include if the file ACTUALLY EXISTS (DeepSeek hallucinates paths) if ((msg.role === 'user' || msg.role === 'assistant') && msg.content) { const content = typeof msg.content === 'string' ? msg.content : ''; const pathRegex = /(\/[^\s<>"']+\.(?:png|jpg|jpeg|webp|gif))/gi; let match; while ((match = pathRegex.exec(content)) !== null) { const filePath = match[1]; if (filePath.startsWith('/') && fs.existsSync(filePath) && !paths.includes(`MEDIA:${filePath}`)) { paths.push(`MEDIA:${filePath}`); } } } } return paths;}
function formatMessages(messages, tools) { let systemPrompt = ''; for (const msg of messages) { if (msg.role === 'system' && msg.content) { systemPrompt += msg.content + '\n'; } } systemPrompt += formatToolDefinitions(tools);
// Build full conversation history for DeepSeek's context let conversation = ''; for (const msg of messages) { if (msg.role === 'system') continue; // already in systemPrompt if (msg.role === 'user' && msg.content) { conversation += `User: ${msg.content}\n\n`; } else if (msg.role === 'assistant') { if (msg.tool_calls && msg.tool_calls.length > 0) { // This was a tool call response from a previous turn for (const tc of msg.tool_calls) { conversation += `Assistant: TOOL_CALL: ${tc.function.name}\narguments: ${tc.function.arguments}\n\n`; } } else if (msg.content) { conversation += `Assistant: ${msg.content}\n\n`; } } else if (msg.role === 'tool' && msg.content) { // Tool execution result — send back to DeepSeek as context const truncated = msg.content.length > 8000 ? msg.content.substring(0, 8000) + '\n...[truncated]' : msg.content; conversation += `[Tool Result]\n${truncated}\n\n`; } } // The last user message + full conversation context return { prompt: conversation.trim(), systemPrompt: systemPrompt.trim() };}
// === HTTP Server ===const server = http.createServer(async (req, res) => { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
// Health check if (req.method === 'GET' && (url.pathname === '/' || url.pathname === '/health')) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ status: 'ok', service: 'FreeDeepseekAPI', watermark: FORGETMEAI_WATERMARK, models: SUPPORTED_MODEL_IDS, unsupported_models: Object.keys(MODEL_CONFIGS).filter(id => !MODEL_CONFIGS[id].supported), agents: sessions.size, config_ready: hasAuthConfig() })); return; }
// Models: OpenAI-compatible list exposes only aliases verified to work through this proxy. if (req.method === 'GET' && url.pathname === '/v1/models') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ object: 'list', data: SUPPORTED_MODEL_IDS.map(id => ({ id, object: 'model', created: 1700000000, owned_by: 'deepseek-web', real_model: MODEL_CONFIGS[id].real_model, capabilities: MODEL_CONFIGS[id].capabilities })) })); return; }
// Full mapping, including Web models observed but not currently usable through the direct API. if (req.method === 'GET' && (url.pathname === '/v1/model-capabilities' || url.pathname === '/api/model-capabilities')) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ object: 'model_capabilities', watermark: FORGETMEAI_WATERMARK, data: ALL_MODEL_CAPABILITIES })); return; }
// Sessions status if (req.method === 'GET' && url.pathname === '/v1/sessions') { const agentList = []; for (const [agentId, session] of sessions) { agentList.push({ agent: agentId, session_id: session.id, message_count: session.messageCount, history_size: session.history.length, age_min: session.createdAt ? Math.round((Date.now() - session.createdAt) / 60000) : 0, }); } res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ agents: agentList, total: agentList.length })); return; }
// Reset session for a specific agent (or all if no agent specified) if (req.method === 'POST' && url.pathname === '/reset-session') { const agentId = url.searchParams.get('agent') || 'default'; if (agentId === 'all') { const count = sessions.size; sessions.clear(); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ status: 'all_sessions_cleared', count })); return; } const session = sessions.get(agentId); if (!session) { res.writeHead(404, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: `No session for agent: ${agentId}` })); return; } const historyCount = session.history.length; const historyPreview = session.history.map(e => e.user.substring(0, 40)).join(' | '); session.id = null; session.parentMessageId = null; session.createdAt = null; session.messageCount = 0; res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ status: 'session_reset', agent: agentId, history_preserved: historyCount, history: historyPreview })); return; }
const apiMode = url.pathname === '/v1/messages' ? 'anthropic' : (url.pathname === '/v1/responses' ? 'responses' : 'openai'); const acceptedPostPaths = ['/v1/chat/completions', '/v1/messages', '/v1/responses']; if (req.method !== 'POST' || !acceptedPostPaths.includes(url.pathname)) { res.writeHead(404); res.end('Not found'); return; }
let body = ''; req.on('data', chunk => body += chunk); req.on('end', async () => { try { const rawParams = JSON.parse(body || '{}'); const params = normalizeApiParams(rawParams, apiMode); const messages = params.messages || []; const tools = params.tools || []; const stream = params.stream === true; const requestedModel = String(params.model || 'deepseek-chat').toLowerCase(); if (!isKnownModel(requestedModel)) { res.writeHead(400, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: `Unknown model: ${requestedModel}`, type: 'invalid_model', supported_models: SUPPORTED_MODEL_IDS, model_capabilities_url: '/v1/model-capabilities' } })); return; } if (!isSupportedModel(requestedModel)) { const cfg = resolveModelConfig(requestedModel); res.writeHead(400, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: `${requestedModel} is not currently supported through this DeepSeek Web API path`, type: 'unsupported_model', model: requestedModel, real_model: cfg.real_model, reason: cfg.unavailable_reason, capabilities: cfg.capabilities, supported_models: SUPPORTED_MODEL_IDS } })); return; } // Use remote IP for session isolation (local gets 'dev-agent', external per-IP) const remoteAddr = req.socket.remoteAddress || 'unknown'; const requestedSession = req.headers['x-agent-session'] || params.session || params.user; const agentId = requestedSession ? String(requestedSession) : ((remoteAddr === '127.0.0.1' || remoteAddr === '::1' || remoteAddr === '::ffff:127.0.0.1') ? 'dev-agent' : remoteAddr); const agentTag = `[${agentId}]`; const { prompt, systemPrompt } = formatMessages(messages, tools);
const session = getOrCreateAgentSession(agentId);
// Build history prefix if starting fresh let historyPrefix = ''; if (!session.id && session.history.length > 0) { historyPrefix = '[Previous conversation]\n'; for (const exchange of session.history) { historyPrefix += `User: ${exchange.user}\nAssistant: ${exchange.assistant}\n\n`; } historyPrefix += '[Continue from here]\n\n'; }
const fullPrompt = systemPrompt ? `${systemPrompt}\n\n${historyPrefix}${prompt}` : `${historyPrefix}${prompt}`;
const startTime = Date.now(); const { resp: dsResp } = await askDeepSeekStream(fullPrompt, agentId, requestedModel);
// Process streaming response from DeepSeek — returns { content, reasoningContent, messageId, finishReason } async function readDeepSeekResponse(readable) { let buffer = ''; let lastPath = null; const fragments = []; let fullContent = ''; let reasoningContent = ''; let newMessageId = null; let finishReason = null; let modelError = null;
const rebuildFragmentText = () => { const responseText = fragments .filter(f => f && f.type === 'RESPONSE' && typeof f.content === 'string') .map(f => f.content) .join(''); const thinkText = fragments .filter(f => f && (f.type === 'THINK' || f.type === 'REASONING') && typeof f.content === 'string') .map(f => f.content) .join(''); if (responseText) fullContent = responseText; reasoningContent = thinkText; };
const appendFragments = (value) => { const incoming = Array.isArray(value) ? value : [value]; for (const fragment of incoming) { if (fragment && typeof fragment === 'object') fragments.push({ ...fragment }); } rebuildFragmentText(); };
for await (const chunk of readable) { buffer += new TextDecoder().decode(chunk, { stream: true }); const lines = buffer.split('\n'); buffer = lines.pop() || ''; for (const line of lines) { if (line.startsWith('data: ')) { try { const d = JSON.parse(line.slice(6)); if (d.response_message_id !== undefined && !newMessageId) newMessageId = d.response_message_id; if (d.type === 'error' || d.finish_reason || d.content) { modelError = { type: d.type || 'error', content: d.content || '', finish_reason: d.finish_reason || null }; if (d.finish_reason) finishReason = d.finish_reason; } if (d.p !== undefined) lastPath = d.p; if (d.v && typeof d.v === 'object' && d.v.response) { if (d.v.response.message_id !== undefined) { newMessageId = d.v.response.message_id; } if (d.v.response.content !== undefined) { fullContent = d.v.response.content; } if (Array.isArray(d.v.response.fragments)) { fragments.length = 0; appendFragments(d.v.response.fragments); } if (d.v.response.finish_reason !== undefined) { finishReason = d.v.response.finish_reason; } } if (lastPath === 'response/fragments' && d.v !== undefined) { appendFragments(d.v); } if (lastPath === 'response/fragments/-1/content' && d.v !== undefined && typeof d.v !== 'object') { if (fragments.length > 0) { const lastFragment = fragments[fragments.length - 1]; lastFragment.content = `${lastFragment.content || ''}${d.v}`; rebuildFragmentText(); } } if (lastPath === 'response/content' && d.v !== undefined && typeof d.v !== 'object') { fullContent += d.v; } if (lastPath === 'response/finish_reason' && d.v !== undefined) { finishReason = d.v; } if (lastPath === 'response/status' && d.v !== undefined && d.v !== 'FINISHED') { finishReason = d.v; } } catch (e) { } } } }
if (newMessageId) { session.parentMessageId = newMessageId; session.messageCount++; } else { console.log(`${agentTag} WARNING: could not extract message_id`); }
return { content: fullContent, reasoningContent, messageId: newMessageId, finishReason, modelError }; }
let { content: fullContent, reasoningContent, finishReason, modelError } = await readDeepSeekResponse(dsResp.body); fullContent = sanitizeContent(fullContent); reasoningContent = sanitizeContent(reasoningContent || ''); const elapsed = Date.now() - startTime; console.log(`${agentTag} Got ${fullContent.length} chars (+${reasoningContent.length} reasoning chars) in ${elapsed}ms (msg#${session.messageCount})`);
if ((!fullContent || fullContent.trim().length === 0) && modelError) { res.writeHead(502, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: modelError.content || 'DeepSeek returned an error without content', type: modelError.finish_reason || modelError.type || 'deepseek_model_error', model: requestedModel, real_model: resolveModelConfig(requestedModel).real_model } })); return; }
// Empty response — retry loop with fresh sessions let retryAttempt = 0; const MAX_RETRIES = 10; while (!fullContent || fullContent.trim().length === 0) { retryAttempt++; if (retryAttempt > MAX_RETRIES) { console.log(`${agentTag} Empty after ${MAX_RETRIES} retries. Giving up.`); res.writeHead(502, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: `DeepSeek returned empty content after ${MAX_RETRIES} retries`, type: 'empty_response', agent: agentId, session_id: session.id, message_count: session.messageCount, history_length: session.history.length, retry_attempts: retryAttempt - 1, } })); return; } console.log(`${agentTag} Empty response (msg#${session.messageCount}, retry ${retryAttempt}/${MAX_RETRIES}). Resetting session...`); session.id = null; session.parentMessageId = null; session.createdAt = null; session.messageCount = 0; // Brief delay before retry to let DeepSeek breathe await new Promise(r => setTimeout(r, Math.min(1000 * retryAttempt, 5000))); const { resp: retryResp } = await askDeepSeekStream(fullPrompt, agentId, requestedModel); const retryResult = await readDeepSeekResponse(retryResp.body); const retryContent = retryResult && retryResult.content ? sanitizeContent(retryResult.content) : ''; const retryReasoning = retryResult && retryResult.reasoningContent ? sanitizeContent(retryResult.reasoningContent) : ''; if (retryContent && retryContent.trim().length > 0) { console.log(`${agentTag} Retry ${retryAttempt} succeeded`); fullContent = retryContent; reasoningContent = retryReasoning; } }
// Auto-continuation: if finish_reason is 'length' or content is very long (>25000 chars), // send a continuation request to get the rest of the response let continuationRounds = 0; const MAX_CONTINUATION = 2; while ((finishReason === 'length' || fullContent.length > 25000) && continuationRounds < MAX_CONTINUATION) { continuationRounds++; console.log(`${agentTag} Response ${fullContent.length} chars (finish=${finishReason}). Auto-continuing (${continuationRounds}/${MAX_CONTINUATION})...`); await new Promise(r => setTimeout(r, 500)); const { resp: contResp } = await askDeepSeekStream('continue', agentId, requestedModel); const contResult = await readDeepSeekResponse(contResp.body); const contContent = contResult && contResult.content ? sanitizeContent(contResult.content) : ''; const contReasoning = contResult && contResult.reasoningContent ? sanitizeContent(contResult.reasoningContent) : ''; if (contContent && contContent.trim().length > 0 && !contContent.includes('I am an AI')) { fullContent += '\n' + contContent; if (contReasoning) reasoningContent += (reasoningContent ? '\n' : '') + contReasoning; finishReason = contResult.finishReason; console.log(`${agentTag} Continuation added ${contContent.length} chars (total: ${fullContent.length})`); } else { console.log(`${agentTag} Continuation returned nothing useful, stopping`); break; } }
let toolCall = parseToolCall(fullContent); // Retry if TOOL_CALL was found but JSON was truncated/invalid if (!toolCall && /TOOL_CALL:\s*\w/i.test(fullContent)) { console.log(`${agentTag} TOOL_CALL detected but JSON invalid/truncated (${fullContent.length} chars). Retrying with stricter prompt...`); session.id = null; session.parentMessageId = null; session.createdAt = null; session.messageCount = 0; await new Promise(r => setTimeout(r, 1000)); const strictPrompt = fullPrompt + '\n\n[STRICT INSTRUCTION] Your previous response had a TOOL_CALL but the arguments were too long and got cut off. Keep the arguments SHORT — no large file contents. Just use a minimal example or reference the file by name. Output ONLY: TOOL_CALL: <function>\narguments: <short JSON>'; const { resp: retryResp2 } = await askDeepSeekStream(strictPrompt, agentId, requestedModel); const retryResult2 = await readDeepSeekResponse(retryResp2.body); const retryContent2 = retryResult2 && retryResult2.content ? sanitizeContent(retryResult2.content) : ''; if (retryContent2 && retryContent2.trim()) { const retryTc = parseToolCall(retryContent2); if (retryTc) { console.log(`${agentTag} Retry with strict prompt succeeded: ${retryTc.name}`); fullContent = retryContent2; reasoningContent = retryResult2.reasoningContent ? sanitizeContent(retryResult2.reasoningContent) : ''; toolCall = retryTc; } else { console.log(`${agentTag} Retry still has broken JSON. Sending as text.`); reasoningContent = retryResult2.reasoningContent ? sanitizeContent(retryResult2.reasoningContent) : reasoningContent; } } } // Check if any tool results in the current conversation contained a screenshot path. // If so, and the response doesn't already have MEDIA:, inject it so the gateway // delivers the file to Telegram. if (!fullContent.includes('MEDIA:')) { const screenshotPaths = extractScreenshotPaths(messages); if (screenshotPaths.length > 0) { fullContent += '\n\n' + screenshotPaths.join('\n'); console.log(`${agentTag} Injected MEDIA paths into response: ${screenshotPaths.join(', ')}`); } }
storeHistory(agentId, prompt, fullContent, toolCall);
const openaiResponse = toolCall ? buildToolCallResponse(toolCall, requestedModel, fullPrompt, reasoningContent) : buildTextResponse(fullContent, fullPrompt, requestedModel, reasoningContent);
if (stream) { if (apiMode === 'anthropic') { sendAnthropicStream(res, openaiResponse); } else if (apiMode === 'responses') { sendResponsesStream(res, openaiResponse); } else { sendOpenAIStream(res, openaiResponse); } console.log(`${agentTag} Streamed ${apiMode} (tool=${!!toolCall}) in ${elapsed}ms`); } else { res.writeHead(200, { 'Content-Type': 'application/json' }); if (apiMode === 'anthropic') { res.end(JSON.stringify(toAnthropicResponse(openaiResponse))); } else if (apiMode === 'responses') { res.end(JSON.stringify(toResponsesResponse(openaiResponse))); } else { res.end(JSON.stringify(openaiResponse)); } console.log(`${agentTag} Response ${apiMode} (tool=${!!toolCall}, ${elapsed}ms, ${fullContent.length} chars)`); } } catch (e) { console.log('[DS-API] Error:', e.message); res.writeHead(500, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: e.message, type: 'server_error' } })); } });});
async function runAuthScript() { const script = path.join(__dirname, 'scripts', 'deepseek_chrome_auth.js'); const result = spawnSync(process.execPath, [script], { stdio: 'inherit', env: process.env }); loadDeepSeekConfig({ fatal: false }); return result.status === 0 && hasAuthConfig();}
function printStatus() { console.log(`\n${formatWatermark()}`); console.log(`Auth: ${hasAuthConfig() ? '✅ OK' : '❌ не найден deepseek-auth.json'}`); console.log(`Auth file: ${DS_CONFIG_PATH}`); console.log(`Рабочие модели: ${SUPPORTED_MODEL_IDS.join(', ')}`); console.log('Нерабочие/скрытые aliases: ' + Object.keys(MODEL_CONFIGS).filter(id => !MODEL_CONFIGS[id].supported).join(', ')); console.log('Capabilities: GET /v1/model-capabilities');}
async function showStartupMenu() { if (isTruthy(process.env.SKIP_ACCOUNT_MENU) || isTruthy(process.env.NON_INTERACTIVE)) { if (!hasAuthConfig()) loadDeepSeekConfig({ fatal: true }); return true; } while (true) { printStatus(); console.log('\n=== Меню ==='); console.log(`ForgetMeAI: ${FORGETMEAI_WATERMARK}`); console.log('1 - Авторизоваться / обновить DeepSeek login'); console.log('2 - Показать модели и статусы'); console.log('3 - Запустить прокси (по умолчанию)'); console.log('4 - Выход'); let choice = await prompt('Ваш выбор (Enter = 3): '); if (!choice) choice = '3'; if (choice === '1') { await runAuthScript(); } else if (choice === '2') { console.log(JSON.stringify(ALL_MODEL_CAPABILITIES, null, 2)); await prompt('\nНажмите Enter, чтобы вернуться в меню...'); } else if (choice === '3') { if (!hasAuthConfig()) { console.log('Нужен deepseek-auth.json. Запустите пункт 1.'); continue; } return true; } else if (choice === '4') { return false; } }}
async function main() { printBanner(); const shouldStart = await showStartupMenu(); if (!shouldStart) process.exit(0); server.listen(PORT, HOST, () => { console.log(`[DS-API] Server on http://${HOST}:${PORT} (multi-agent sessions enabled)`); console.log(`[DS-API] ${formatWatermark()}`); console.log('[DS-API] POST /v1/chat/completions (OpenAI Chat Completions, stream=true|false)'); console.log('[DS-API] POST /v1/messages — Anthropic Messages shim for Claude Code'); console.log('[DS-API] POST /v1/responses — OpenAI Responses API shim'); console.log('[DS-API] GET /v1/models — supported OpenAI-compatible models'); console.log('[DS-API] GET /v1/model-capabilities — real model mapping and capabilities'); console.log('[DS-API] GET /v1/sessions — list active agent sessions'); console.log('[DS-API] POST /reset-session?agent=<id> — reset agent session'); console.log('[DS-API] POST /reset-session?agent=all — reset ALL sessions'); });}
main().catch(err => { console.error('[DS-API] FATAL:', err); process.exit(1); });108 unmodified lines109110111112113114108 unmodified linesvar app = builder.Build();// --- Auto-migrate database and seed ---using (var scope = app.Services.CreateScope()){108 unmodified lines109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176108 unmodified linesvar app = builder.Build();// --- Start FreeDeepseekAPI proxy in background ---try{var dsPath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "FreeDeepseekAPI", "server.js");if (System.IO.File.Exists(dsPath)){System.Console.WriteLine($"[Main] Starting FreeDeepseekAPI proxy: node {dsPath}...");var processStartInfo = new System.Diagnostics.ProcessStartInfo{FileName = "node",Arguments = dsPath,UseShellExecute = false,CreateNoWindow = true,RedirectStandardOutput = true,RedirectStandardError = true};processStartInfo.EnvironmentVariables["NON_INTERACTIVE"] = "1";processStartInfo.EnvironmentVariables["PORT"] = "9655";// Forward any relevant DeepSeek vars to the proxy processforeach (var key in new[] { "DEEPSEEK_TOKEN", "DEEPSEEK_COOKIE", "DEEPSEEK_HIF_DLIQ", "DEEPSEEK_HIF_LEIM", "DEEPSEEK_WASM_URL" }){var val = System.Environment.GetEnvironmentVariable(key);if (!string.IsNullOrEmpty(val)){processStartInfo.EnvironmentVariables[key] = val;}}var dsProcess = System.Diagnostics.Process.Start(processStartInfo);if (dsProcess != null){// Read output in background to prevent hangingSystem.Threading.Tasks.Task.Run(() => {try {while (!dsProcess.StandardOutput.EndOfStream) {var line = dsProcess.StandardOutput.ReadLine();System.Console.WriteLine($"[DeepSeekProxy-Out] {line}");}} catch {}});System.Threading.Tasks.Task.Run(() => {try {while (!dsProcess.StandardError.EndOfStream) {var line = dsProcess.StandardError.ReadLine();System.Console.WriteLine($"[DeepSeekProxy-Err] {line}");}} catch {}});System.Console.WriteLine("[Main] FreeDeepseekAPI proxy started successfully.");}}else{System.Console.WriteLine($"[Main] FreeDeepseekAPI proxy file not found at: {dsPath}");}}catch (System.Exception ex){System.Console.WriteLine($"[Main] Failed to start FreeDeepseekAPI proxy: {ex.Message}");}// --- Auto-migrate database and seed ---using (var scope = app.Services.CreateScope()){
Dockerfile+10
FlowAPI.Application/Services/AIService.cs+119
FlowAPI/FlowAPI.csproj+6
FlowAPI/FreeDeepseekAPI/package.jsonAdded+6
FlowAPI/FreeDeepseekAPI/server.jsAdded+1400
FlowAPI/Program.cs+62