Nexchan commited on
Commit
b041a57
·
verified ·
1 Parent(s): f7b0b99

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +35 -19
index.js CHANGED
@@ -83,17 +83,17 @@ app.get('/v2/send', async (req, res) => {
83
  chatData = {
84
  lastChat: DateTime.local(),
85
  data: {
86
- history: [
87
- {
88
- role: "user",
89
- parts: [{text: "hello, can you answer my question with a cute kaomoji like this 。⁠◕⁠‿⁠◕⁠。"}]
90
- },
91
- {
92
- role: "model",
93
- parts: [{text: "sure (⁠◠⁠‿⁠◕⁠) , so.... (⁠ ⁠/⁠^⁠ω⁠^⁠)⁠/⁠♪⁠♪ how can I help dear user (⁠ ⁠╹⁠▽⁠╹⁠ ⁠)?"}],
94
- },
95
- ],
96
- generationConfig: {
97
  maxOutputTokens: 500,
98
  },
99
  }
@@ -102,16 +102,31 @@ app.get('/v2/send', async (req, res) => {
102
 
103
  const botResponse = await sendMessageV2(chatData.data, message);
104
 
105
- // Simpan pesan user ke riwayat obrolan
106
- chatData.data.history.push({
107
- role: 'user',
108
- parts: [{text: message}]
109
  });
110
- // Simpan bot response ke riwayat obrolan
111
- chatData.data.history.push({
112
- role: 'model',
113
- parts: [{text: botResponse}]
114
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  // Update waktu terakhir obrolan
116
  chatData.lastChat = DateTime.local();
117
  db_chatHistory[id] = chatData;
@@ -128,6 +143,7 @@ app.get('/v2/send', async (req, res) => {
128
  });
129
 
130
 
 
131
  app.get('/v2/listuser', (req, res) => {
132
  const userList = Object.keys(db_chatHistory);
133
  res.status(200).json({ userList });
 
83
  chatData = {
84
  lastChat: DateTime.local(),
85
  data: {
86
+ history: [
87
+ {
88
+ role: "user",
89
+ parts: [{text: "hello, can you answer my question with a cute kaomoji like this 。⁠◕⁠‿⁠◕⁠。"}]
90
+ },
91
+ {
92
+ role: "model",
93
+ parts: [{text: "sure (⁠◠⁠‿⁠◕⁠) , so.... (⁠ ⁠/⁠^⁠ω⁠^⁠)⁠/⁠♪⁠♪ how can I help dear user (⁠ ⁠╹⁠▽⁠╹⁠ ⁠)?"}],
94
+ },
95
+ ],
96
+ generationConfig: {
97
  maxOutputTokens: 500,
98
  },
99
  }
 
102
 
103
  const botResponse = await sendMessageV2(chatData.data, message);
104
 
105
+ // Cek apakah pesan user sudah ada di riwayat
106
+ const userMessageExists = chatData.data.history.some(item => {
107
+ return item.role === 'user' && item.parts[0].text === message;
 
108
  });
109
+
110
+ // Cek apakah bot response sudah ada di riwayat
111
+ const botResponseExists = chatData.data.history.some(item => {
112
+ return item.role === 'model' && item.parts[0].text === botResponse;
113
  });
114
+
115
+ // Jika tidak ada, tambahkan ke riwayat
116
+ if (!userMessageExists) {
117
+ chatData.data.history.push({
118
+ role: 'user',
119
+ parts: [{text: message}]
120
+ });
121
+ }
122
+
123
+ if (!botResponseExists) {
124
+ chatData.data.history.push({
125
+ role: 'model',
126
+ parts: [{text: botResponse}]
127
+ });
128
+ }
129
+
130
  // Update waktu terakhir obrolan
131
  chatData.lastChat = DateTime.local();
132
  db_chatHistory[id] = chatData;
 
143
  });
144
 
145
 
146
+
147
  app.get('/v2/listuser', (req, res) => {
148
  const userList = Object.keys(db_chatHistory);
149
  res.status(200).json({ userList });