Update aiEngine.js
Browse files11 unmodified lines121314151617181920212223242526272829303132333435361 unmodified line3839404142434412 unmodified lines5758596061626364651 unmodified line676869707172734 unmodified lines7879808182838411 unmodified linesif (fs.existsSync(promptsPath)) prompts = JSON.parse(fs.readFileSync(promptsPath, 'utf8'));} catch (e) { console.error("Prompt Load Error:", e); }// --- IMPROVED CONTEXT FLATTENER ---const flattenHistory = (history, currentInput, systemPrompt, limit = 10, gdd = null) => {// Slice history based on the specific role's limitconst recentHistory = history.slice(-limit);let context = recentHistory.map(m => {const roleName = m.role === 'model' ? 'Assistant' : 'User';return `${roleName}: ${m.parts[0].text}`;}).join('\n');// If GDD is provided (for PM), inject it as the project foundationconst projectAnchor = gdd ? `[GAME DESIGN DOCUMENT - REFERENCE ONLY]:\n${gdd}\n\n` : "";return `System: ${systemPrompt}\n\n${projectAnchor}${context}\nUser: ${currentInput}\nAssistant:`;};const handleStreamResponse = async (response, onThink, onOutput) => {if (!response.ok) throw new Error(`Stream Error: ${response.statusText}`);const reader = response.body.getReader();const decoder = new TextDecoder("utf-8");let fullStreamData = "";1 unmodified linewhile (true) {const { done, value } = await reader.read();if (done) break;const chunk = decoder.decode(value, { stream: true });fullStreamData += chunk;12 unmodified linesif (fullStreamData.includes("__USAGE__")) {const parts = fullStreamData.split("__USAGE__");finalOutput = parts[0];try {usage = JSON.parse(parts[1]);} catch (e) { console.warn("Usage Parse Failed"); }}const cleanText = finalOutput.split("__THINK__")[0].trim();1 unmodified line};export const AIEngine = {// --- PM: 30 Turn Memory + GDD ---callPMStream: async (history, input, onThink, onOutput, gdd = null) => {const prompt = flattenHistory(history, input, prompts.pm_system_prompt, 30, gdd);const response = await fetch(`${REMOTE_SERVER_URL}/api/stream`, {4 unmodified linesreturn await handleStreamResponse(response, onThink, onOutput);},// --- Worker: 15 Turn Memory ---callWorkerStream: async (history, input, onThink, onOutput, images = []) => {const prompt = flattenHistory(history, input, prompts.worker_system_prompt, 15, null);const response = await fetch(`${REMOTE_SERVER_URL}/api/stream`, {11 unmodified lines121314151617181920212223242526272829301 unmodified line32333435363712 unmodified lines505152535455561 unmodified line585960616263644 unmodified lines6970717273747511 unmodified linesif (fs.existsSync(promptsPath)) prompts = JSON.parse(fs.readFileSync(promptsPath, 'utf8'));} catch (e) { console.error("Prompt Load Error:", e); }// --- IMPROVED CONTEXT FLATTENER WITH DIFFERENTIATED LIMITS ---const flattenHistory = (history, currentInput, systemPrompt, limit = 15, gdd = null) => {const recentHistory = history.slice(-limit);let context = recentHistory.map(m => {const roleName = m.role === 'model' ? 'Assistant' : 'User';return `${roleName}: ${m.parts[0].text}`;}).join('\n');const projectAnchor = gdd ? `[GAME DESIGN DOCUMENT - REFERENCE ONLY]:\n${gdd}\n\n` : "";return `System: ${systemPrompt}\n\n${projectAnchor}${context}\nUser: ${currentInput}\nAssistant:`;};const handleStreamResponse = async (response, onThink, onOutput) => {if (!response.ok) throw new Error(`Stream Error: ${response.statusText}`);const reader = response.body.getReader();const decoder = new TextDecoder("utf-8");let fullStreamData = "";1 unmodified linewhile (true) {const { done, value } = await reader.read();if (done) break;const chunk = decoder.decode(value, { stream: true });fullStreamData += chunk;12 unmodified linesif (fullStreamData.includes("__USAGE__")) {const parts = fullStreamData.split("__USAGE__");finalOutput = parts[0];try { usage = JSON.parse(parts[1]); } catch (e) { console.warn("Usage Parse Failed"); }}const cleanText = finalOutput.split("__THINK__")[0].trim();1 unmodified line};export const AIEngine = {// PM: 30 Turns + GDDcallPMStream: async (history, input, onThink, onOutput, gdd = null) => {const prompt = flattenHistory(history, input, prompts.pm_system_prompt, 30, gdd);const response = await fetch(`${REMOTE_SERVER_URL}/api/stream`, {4 unmodified linesreturn await handleStreamResponse(response, onThink, onOutput);},// Worker: 15 TurnscallWorkerStream: async (history, input, onThink, onOutput, images = []) => {const prompt = flattenHistory(history, input, prompts.worker_system_prompt, 15, null);const response = await fetch(`${REMOTE_SERVER_URL}/api/stream`, {
aiEngine.js+5-14