Skip to content

Commit 5491bd5

Browse files
committed
Improvements to ChatBot requests
- Now Chat Bot requests can be done with a generic id
1 parent 2ef30d3 commit 5491bd5

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

samp-chatbot.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
// Natives
88

9-
native RequestToChatBot(const prompt[], playerid);
9+
native RequestToChatBot(const prompt[], id);
1010
native SelectChatBot(type);
1111
native SetAPIKey(const apiKey[]);
1212
native SetSystemPrompt(const systemPrompt[]);
1313
native SetModel(const model[]);
1414

1515
// Callbacks
1616

17-
forward OnChatBotResponse(prompt[], response[], playerid);
17+
forward OnChatBotResponse(prompt[], response[], id);

src/main.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,16 @@ static std::string GetBotAnswer(int type, nlohmann::json response)
160160
catch (std::exception exc)
161161
{
162162
logprintf("ChatBot Plugin Exception GetBotAnswer(): %s\n", exc.what());
163+
logprintf("Chatbot Plugin Exception Response:\n%s", response.dump(4).c_str());
164+
163165
return response.dump(4).c_str();
164166
}
165167
}
166168

167169
return "";
168170
}
169171

170-
static void DoRequest(std::string prompt, int playerid)
172+
static void DoRequest(std::string prompt, int id)
171173
{
172174
std::string response;
173175

@@ -220,7 +222,7 @@ static void DoRequest(std::string prompt, int playerid)
220222

221223
std::string answer = GetBotAnswer(curChatBot, jresponse);
222224

223-
AIResponse resp(playerid, prompt, answer);
225+
AIResponse resp(id, prompt, answer);
224226

225227
responseLock.lock();
226228
responses.push(resp);
@@ -245,14 +247,14 @@ static void RequestsThread()
245247
requestLock.unlock();
246248

247249
std::string prompt = curRequest.GetPrompt();
248-
int playerid = curRequest.GetPlayerID();
250+
int id = curRequest.GetID();
249251

250-
if (!prompt.empty() && playerid >= 0)
252+
if (!prompt.empty())
251253
{
252254
#ifdef _DEBUG
253255
logprintf("\nnew request: %s\n", prompt.c_str());
254256
#endif
255-
DoRequest(prompt, playerid);
257+
DoRequest(prompt, id);
256258
curRequest.Clear();
257259
}
258260
else
@@ -271,7 +273,7 @@ PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
271273
{
272274
pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
273275
logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
274-
logprintf("\n\nChatBot API Plugin by SimoSbara loaded\n", PLUGIN_VERSION);
276+
logprintf("\n\nChatBot API Plugin %s by SimoSbara loaded\n", PLUGIN_VERSION);
275277

276278
EncodingHelper::Init();
277279

@@ -286,22 +288,22 @@ PLUGIN_EXPORT void PLUGIN_CALL Unload()
286288
{
287289
running = false;
288290

289-
logprintf("\n\nChatBot API Plugin by SimoSbara unloaded\n", PLUGIN_VERSION);
291+
logprintf("\n\nChatBot API Plugin %s by SimoSbara unloaded\n", PLUGIN_VERSION);
290292
}
291293

292294
static cell AMX_NATIVE_CALL n_RequestToChatBot(AMX* amx, cell* params)
293295
{
294296
char* pRequest = NULL;
295297

296-
CHECK_PARAMS(2, "RequestToChatBot"); //playerid, request string
298+
CHECK_PARAMS(2, "RequestToChatBot"); //id int, request string
297299

298300
amx_StrParam(amx, params[1], pRequest);
299301

300-
int playerID = static_cast<int>(params[2]);
302+
int id = static_cast<int>(params[2]);
301303

302-
if (playerID >= 0 && pRequest)
304+
if (pRequest)
303305
{
304-
AIRequest newRequest(playerID, std::string(pRequest));
306+
AIRequest newRequest(id, std::string(pRequest));
305307

306308
requestLock.lock();
307309
requestes.push(newRequest);
@@ -431,7 +433,7 @@ PLUGIN_EXPORT void PLUGIN_CALL ProcessTick()
431433
if (!amx_FindPublic(*a, "OnChatBotResponse", &amxIndex))
432434
{
433435
//parametri al contrario
434-
amx_Push(*a, response.GetPlayerID());
436+
amx_Push(*a, response.GetID());
435437
amx_PushString(*a, &amxAddresses[0], NULL, response.GetResponse().c_str(), 0, 0);
436438
amx_PushString(*a, &amxAddresses[1], NULL, response.GetPrompt().c_str(), 0, 0);
437439
amx_Exec(*a, NULL, amxIndex);

src/main.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef MAIN_H
22
#define MAIN_H
33

4-
#define PLUGIN_VERSION "v1.3.1"
4+
#define PLUGIN_VERSION "v1.3.5"
55

66
#include <sdk/plugin.h>
77
#ifdef _WIN32
@@ -128,18 +128,18 @@ class AIRequest
128128
public:
129129
AIRequest()
130130
{
131-
this->playerid = -1;
131+
this->id = -1;
132132
}
133133

134-
AIRequest(int playerid, std::string prompt)
134+
AIRequest(int id, std::string prompt)
135135
{
136-
this->playerid = playerid;
136+
this->id = id;
137137
this->prompt = prompt;
138138
}
139139

140140
void Clear()
141141
{
142-
this->playerid = -1;
142+
this->id = -1;
143143
this->prompt.clear();
144144
}
145145

@@ -148,22 +148,22 @@ class AIRequest
148148
return this->prompt;
149149
}
150150

151-
int GetPlayerID()
151+
int GetID()
152152
{
153-
return this->playerid;
153+
return this->id;
154154
}
155155

156156
private:
157157
std::string prompt;
158-
int playerid; //who requests
158+
int id; //generic ID
159159
};
160160

161161
class AIResponse
162162
{
163163
public:
164-
AIResponse(int playerid, std::string prompt, std::string response)
164+
AIResponse(int id, std::string prompt, std::string response)
165165
{
166-
this->playerid = playerid;
166+
this->id = id;
167167
this->prompt = prompt;
168168
this->response = response;
169169
}
@@ -178,15 +178,15 @@ class AIResponse
178178
return this->response;
179179
}
180180

181-
int GetPlayerID()
181+
int GetID()
182182
{
183-
return this->playerid;
183+
return this->id;
184184
}
185185

186186
private:
187187
std::string prompt; //prompt from the player
188188
std::string response; //response of gpt
189-
int playerid; //who has requested
189+
int id; //ID of the original request
190190
};
191191

192192

0 commit comments

Comments
 (0)