Update aiEngine.js
Browse files19 unmodified lines202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686934 unmodified lines10410510610710810911026 unmodified lines13713813914014114214321 unmodified lines16516616716816917017114 unmodified lines18618718818919019119213 unmodified lines20620720820921021121219 unmodified linesreturn `System: ${systemPrompt}\n\n${context}\nUser: ${currentInput}\nAssistant:`;};// HELPER: STREAM SPLITTERconst 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");// fullText is what gets saved to the DB. It should NOT contain thoughts.let fullText = "";while (true) {const { done, value } = await reader.read();if (done) break;const chunk = decoder.decode(value, { stream: true });// CASE 1: Pure Thought Chunkif (chunk.startsWith("__THINK__")) {const thoughtContent = chunk.replace("__THINK__", "");if (onThink) onThink(thoughtContent);}// CASE 2: Mixed Chunk (Output then Thought)else if (chunk.includes("__THINK__")) {const parts = chunk.split("__THINK__");// part[0] is real outputif (parts[0]) {if (onOutput) onOutput(parts[0]);fullText += parts[0];}// part[1] is thoughtif (parts[1] && onThink) {onThink(parts[1]);}}// CASE 3: Pure Output Chunkelse {if (onOutput) onOutput(chunk);fullText += chunk;}}return { text: fullText, usage: { totalTokenCount: 0 } };};export const AIEngine = {34 unmodified linesmodel: "gpt",prompt: fullPrompt,system_prompt: systemPrompt,images: images // Pass images to Main Agent})});26 unmodified linesreturn {text: result.data,usage: { totalTokenCount: 0 }};} catch (error) {console.log("PM error: ",error);21 unmodified linesreturn {text: result.data,usage: { totalTokenCount: 0 }};} catch (error) {console.log("Worker error: ",error);14 unmodified lines});const result = await response.json();return JSON.parse(result.data);} catch (error) {console.log("GenerateQ error: ",error)13 unmodified lines});const result = await response.json();return JSON.parse(result.data);} catch (error) {console.log("GenerateQ error: ",error)19 unmodified lines20212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787934 unmodified lines11411511611711811912026 unmodified lines14714814915015115215321 unmodified lines17517617717817918018114 unmodified lines19619719819920020120220320420520613 unmodified lines22022122222322422522622722819 unmodified linesreturn `System: ${systemPrompt}\n\n${context}\nUser: ${currentInput}\nAssistant:`;};// HELPER: STREAM SPLITTER & USAGE PARSERconst 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 fullText = "";let usage = { totalTokenCount: 0 }; // Defaultwhile (true) {const { done, value } = await reader.read();if (done) break;let chunk = decoder.decode(value, { stream: true });// CHECK FOR USAGE FOOTERif (chunk.includes("__USAGE__")) {const parts = chunk.split("__USAGE__");chunk = parts[0]; // The text parttry {if (parts[1]) {usage = JSON.parse(parts[1]);}} catch (e) {console.warn("Failed to parse usage footer:", e);}}// STANDARD CHUNK PROCESSINGif (chunk.startsWith("__THINK__")) {const thoughtContent = chunk.replace("__THINK__", "");if (onThink) onThink(thoughtContent);}else if (chunk.includes("__THINK__")) {const parts = chunk.split("__THINK__");if (parts[0]) {if (onOutput) onOutput(parts[0]);fullText += parts[0];}if (parts[1] && onThink) {onThink(parts[1]);}}else {if (onOutput) onOutput(chunk);fullText += chunk;}}return {text: fullText,usage: usage};};export const AIEngine = {34 unmodified linesmodel: "gpt",prompt: fullPrompt,system_prompt: systemPrompt,images: images})});26 unmodified linesreturn {text: result.data,usage: result.usage || { totalTokenCount: 0 }};} catch (error) {console.log("PM error: ",error);21 unmodified linesreturn {text: result.data,usage: result.usage || { totalTokenCount: 0 }};} catch (error) {console.log("Worker error: ",error);14 unmodified lines});const result = await response.json();return {...JSON.parse(result.data),usage: result.usage || { totalTokenCount: 0 }};} catch (error) {console.log("GenerateQ error: ",error)13 unmodified lines});const result = await response.json();const parsed = JSON.parse(result.data);parsed.usage = result.usage || { totalTokenCount: 0 };return parsed;} catch (error) {console.log("GenerateQ error: ",error)
aiEngine.js+32-16