mock functions removed

This commit is contained in:
mic 2024-07-21 15:26:10 +02:00
parent ee2540a8c8
commit d942b026c0

View file

@ -1,5 +1,4 @@
import { OpenAI } from '../js/api/openai.js'; import { OpenAI } from '../js/api/openai.js';
const MOCK_TOKENS = ['Good', ' morning', ' Mr', ' Plop', 'py', ',', 'and', ' I', ' said', '\n', '"', 'Good', ' morn', 'ing', ' Mrs',' Plop', 'py', ,'"', '\n', 'Oh', ' how', ' the', ' win', 'ter', ' even', 'ings', ' must', ' just', ' fly'];
let api_key_chatgpt = null; let api_key_chatgpt = null;
let openai = null; let openai = null;
@ -7,36 +6,11 @@ let openai = null;
let conversationHistory = []; let conversationHistory = [];
let assistantResponseAccumulator = ''; let assistantResponseAccumulator = '';
function mockDelay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function processMockTokens() {
for (const token of MOCK_TOKENS) {
await mockDelay(Math.random() * 50 + 50); // Random delay between 100ms and 150ms
postMessage({ type: 'newToken', payload: { token } });
}
postMessage({ type: 'tokensDone' });
}
self.onmessage = async function(event) { self.onmessage = async function(event) {
if (event.data.type === 'init') { if (event.data.type === 'init') {
api_key_chatgpt = event.data.api_key_chatgpt; api_key_chatgpt = event.data.api_key_chatgpt;
openai = new OpenAI(api_key_chatgpt, true); openai = new OpenAI(api_key_chatgpt, true);
} else if (event.data.type === 'chatMessage') { } else if (event.data.type === 'chatMessage') {
// MOCK
if( api_key_chatgpt === null ) {
// Simulate sending the message to an HTTP endpoint
await mockDelay(1000); // Wait for 1 second
// Notify that the chat message was sent
postMessage({ type: 'messageSent' });
// Start processing tokens
await processMockTokens();
// OPENAI
} else {
conversationHistory.push({ role: 'user', content: event.data.message }); conversationHistory.push({ role: 'user', content: event.data.message });
// https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo // https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo
@ -87,5 +61,4 @@ self.onmessage = async function(event) {
} }
} }
} }
}
}; };