File size: 3,685 Bytes
e13ec6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function createLoadingIndicator() {
  const loadingDiv = document.createElement("div");
  loadingDiv.className = "chat-message assistant-message";
  loadingDiv.innerHTML = `
                <div class="loading-dots">
                    <div class="dot"></div>
                    <div class="dot"></div>
                    <div class="dot"></div>
                </div>
            `;
  return loadingDiv;
}

async function addProgramaticMessage(message) {
  const chatHistory = document.getElementById("chatHistory");
  const loadingIndicator = createLoadingIndicator();
  chatHistory.appendChild(loadingIndicator);
  await new Promise(resolve => setTimeout(resolve, 2000));
  chatHistory.removeChild(loadingIndicator);
  addMessageToChat("assistant", message);
}

async function sendMessage() {
  const prompt = document.getElementById("prompt").value.trim();
  if (!prompt) return;

  addMessageToChat("user", prompt);
  document.getElementById("prompt").value = "";

  const chatHistory = document.getElementById("chatHistory");
  const loadingIndicator = createLoadingIndicator();
  chatHistory.appendChild(loadingIndicator);

  // First, get the stress prompt and send it
  const stressPrompt = gameState.getStressPrompt();
  const stressMessages = [
    {
      role: "system",
      content: stressPrompt,
    },
    { role: "user", content: prompt },
  ];
  console.log("stressMessages", stressMessages);

  const stressResponse = await mistralAPI.sendMessage(stressMessages);
  const stressChange = JSON.parse(stressResponse).stressChange || 0;

  girlfriend.updateStressLevel(stressChange);
 

  let masterPrompt = gameState.getPrompt();
  const recentMessages = chatMessages.slice(-5);

  const messages = [
    { role: "system", content: masterPrompt },
    ...recentMessages,
    { role: "user", content: prompt },
  ];

  const assistantResponse = await mistralAPI.sendMessage(messages);

  chatHistory.removeChild(loadingIndicator);

  const jsonStart = assistantResponse.indexOf("{");
  const jsonEnd = assistantResponse.lastIndexOf("}") + 1;
  const jsonContent = assistantResponse.substring(jsonStart, jsonEnd);
  const jsonResponse = JSON.parse(jsonContent);

  if (jsonResponse.action) {
    girlfriend.handleAction(jsonResponse);
  }

  if (jsonResponse.textMessage) {
    addMessageToChat("assistant", jsonResponse.textMessage);
  }
}

function addMessageToChat(role, content) {
  const chatHistory = document.getElementById("chatHistory");
  const messageDiv = document.createElement("div");
  messageDiv.className = `chat-message ${role}-message`;
  messageDiv.textContent = content;
  chatHistory.appendChild(messageDiv);
  chatHistory.scrollTop = chatHistory.scrollHeight;

  chatMessages.push({ role, content });

  if (role === "assistant" && localStorage.getItem("isSoundOn") !== "false") {
    playMessageSound();
  }
}

// Update Message function to use girlfriend instance
function Message() {
  if (girlfriend) {
    // Play message sound when sending a message
    if (localStorage.getItem("isSoundOn") !== "false") {
      playMessageSound();
    }
    sendMessage();
  }
}

// Allow sending message with Enter
document.addEventListener("DOMContentLoaded", () => {
  document.getElementById("prompt").addEventListener("keypress", function (e) {
    if (e.key === "Enter") {
      e.preventDefault();
      Message();
    }
  });

  try {
    const bgMusic = document.getElementById("bgMusic");
    bgMusic.currentTime = 10;
    bgMusic.play().catch((error) => {
      console.log("Autoplay prevented:", error);
      isSoundOn = false;
      updateSoundIcon();
    });
  } catch (error) {
    console.error("Error playing audio:", error);
  }
});