Skip to content

Commit b727a43

Browse files
committed
revision
1 parent 1bc906e commit b727a43

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

src/eMarket/model/eMarket/Core/JsonRpc.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ class JsonRpc {
3030
private $methods_available = [];
3131
public static $response = [];
3232

33-
/**
34-
* Constructor
35-
*
36-
*/
37-
public function __construct() {
38-
39-
}
40-
4133
/**
4234
* Return jsonRpc data from key name
4335
*

src/eMarket/model/eMarket/JsonRpc/ChatGPT.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ private function request(): void {
7676
'temperature' => 0.7
7777
];
7878

79-
echo $this->curl($request,
79+
$output = json_decode($this->curl($request,
8080
'https://api.openai.com/v1/chat/completions',
8181
['Content-Type: application/json',
8282
'Accept: application/json',
8383
'User-Agent: eMarket',
84-
'Authorization: Bearer ' . $this->token]);
84+
'Authorization: Bearer ' . $this->token]));
85+
86+
$this->responseBuilder([$output], $this->jsonrpc['id']);
8587
}
8688
}
8789

@@ -100,8 +102,8 @@ private function apiKey(): void {
100102
->set('my_data', $chatgpt_token)
101103
->where('login=', $decrypt_login)
102104
->save();
103-
104-
echo json_encode([lang('chatgpt_api_key_saved')]);
105+
106+
$this->responseBuilder([lang('chatgpt_api_key_saved')], $this->jsonrpc['id']);
105107
}
106108
}
107109
}

src/eMarket/model/library/js/classes/chatgpt/chatgpt.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
| GNU GENERAL PUBLIC LICENSE v.3.0 |
33
| https://github.com/musicman3/eMarket |
44
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
5-
/* global Ajax, Randomizer */
5+
/* global Ajax, Randomizer, JsonRpc */
66

77
/**
88
* ChatGPT
@@ -29,6 +29,7 @@ class ChatGPT {
2929
static request(content = 'Say this is a test!') {
3030

3131
var randomizer = new Randomizer();
32+
sessionStorage.setItem('ChatGPT.request.id', randomizer.uid(32));
3233

3334
var jsonRpcRequest = [
3435
{
@@ -38,7 +39,7 @@ class ChatGPT {
3839
'message': content,
3940
'login': document.querySelector('#user_login').dataset.login
4041
},
41-
'id': randomizer.uid(32)
42+
'id': sessionStorage.getItem('ChatGPT.request.id')
4243
}
4344
];
4445

@@ -59,6 +60,7 @@ class ChatGPT {
5960
static apiKey(content = '') {
6061

6162
var randomizer = new Randomizer();
63+
sessionStorage.setItem('ChatGPT.apiKey.id', randomizer.uid(32));
6264

6365
var jsonRpcRequest = [
6466
{
@@ -68,7 +70,7 @@ class ChatGPT {
6870
'api_key': content,
6971
'login': document.querySelector('#user_login').dataset.login
7072
},
71-
'id': randomizer.uid(32)
73+
'id': sessionStorage.getItem('ChatGPT.apiKey.id')
7274
}
7375
];
7476

@@ -84,9 +86,14 @@ class ChatGPT {
8486
* @param data {String} (data)
8587
*/
8688
static save(data) {
87-
var input = JSON.parse(data);
88-
document.querySelector('#chat_bot').value = input[0];
89-
document.querySelector('#chatgpt_key').value = '';
89+
if (data !== null && data !== undefined) {
90+
91+
var input = JSON.parse(data);
92+
input = JsonRpc.jsonRpcSelect(input, sessionStorage.getItem('ChatGPT.apiKey.id'));
93+
94+
document.querySelector('#chat_bot').value = input.result[0];
95+
document.querySelector('#chatgpt_key').value = '';
96+
}
9097
}
9198

9299
/**
@@ -138,15 +145,20 @@ class ChatGPT {
138145
* @param data {Object} (ChatGPT response)
139146
*/
140147
static Response(data) {
141-
var input = JSON.parse(data);
142-
if (input !== undefined && input.choices !== undefined) {
143-
document.querySelector('#chat_bot').value = input.choices[0].message.content;
144-
document.querySelector('#chat_user').disabled = false;
145-
document.querySelector('#chat_user').value = '';
146-
document.querySelector('#chat_user').focus();
147-
} else {
148-
document.querySelector('#chat_bot').value = input.error.message;
148+
if (data !== null && data !== undefined) {
149+
150+
var input = JSON.parse(data);
151+
input = JsonRpc.jsonRpcSelect(input, sessionStorage.getItem('ChatGPT.request.id'));
152+
153+
if (input !== undefined && input.choices !== undefined) {
154+
document.querySelector('#chat_bot').value = input.result.choices[0].message.content;
155+
document.querySelector('#chat_user').disabled = false;
156+
document.querySelector('#chat_user').value = '';
157+
document.querySelector('#chat_user').focus();
158+
} else {
159+
document.querySelector('#chat_bot').value = input.result[0].error.message;
160+
}
161+
ChatGPT.removeClass();
149162
}
150-
ChatGPT.removeClass();
151163
}
152164
}

0 commit comments

Comments
 (0)