-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatNetsuite_public.js
283 lines (275 loc) · 11.3 KB
/
ChatNetsuite_public.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
* @author Jiyun Mei
* @email karthous@outlook.com
* @since 2023.4
*/
define(['N/https', 'N/ui/serverWidget', 'N/file', './chatNetsuite_config'],
(https, serverWidget, file, config) => {
const {
VERSION_NO,
CHAT_API_URL,
DRAW_API_URL,
HEADERS,
CHAT_MODEL,
DRAW_MODEL,
DRAW_QUALITY,
PROMPT
} = config;
const clientScriptModulePath = "./chatNetsuite_router_public.js";
/**
* Handles the request when a user visits the page.
* If the request method is GET, the initPage will be rendered.
* Otherwise, the chatPage or drawPage is rendered based on users' choice.
* @param {Object} scriptContext
*/
const onRequest = (scriptContext) => {
const {custpage_chat, custpage_draw} = scriptContext.request.parameters;
scriptContext.response.writePage({
pageObject: custpage_chat ? chatPage(scriptContext) : custpage_draw ? drawPage(scriptContext) : initPage()});
}
/**
* Initializes a form with two checkbox fields representing two features
* and the title 'ChatNetsuite v[VERSION_NO]'.
*/
const initPage = () => {
let form = serverWidget.createForm({title: 'ChatNetsuite v' + VERSION_NO});
form.clientScriptModulePath = clientScriptModulePath;
form = addBtnToPage(form, ['chat', 'draw']);
let news = form.addField({
id: 'custpage_news',
type: serverWidget.FieldType.INLINEHTML,
label: 'News'
});
news.defaultValue = "<small>If you need more features, send a request to the administrator.</small>";
return form;
}
/**
* Renders the chat page with a form with the input and answer.
* @param {Object} scriptContext
*/
const chatPage = (scriptContext) => {
let text = scriptContext.request.parameters.custpage_input;
let pastMsg = scriptContext.request.parameters.custpage_pstmsg;
if (!text) {
text = PROMPT;
}
let messages = [];
if (pastMsg) {
pastMsg = '[' + pastMsg + ']';
messages = JSON.parse(pastMsg);
}
messages.push({"role": 'user', "content": text});
// Set payload
let payload = {
"model": CHAT_MODEL,
"messages": messages
// "temperature": 1,
// "top_p": 1,
// "n": 1,
// "stream": false
};
// Make API call
let response = https.post({
url: CHAT_API_URL,
body: JSON.stringify(payload),
headers: HEADERS
});
// Handle response codes
let answer;
switch (response.code) {
case 200:
answer = JSON.parse(response.body).choices[0].message.content;
messages.push(JSON.parse(response.body).choices[0].message);
break;
case 401:
answer = 'Incorrect API key provided';
messages.push({"role": 'AI', "content": answer});
break;
case 402:
answer = 'Server refused to access, please try again later';
messages.push({"role": 'AI', "content": answer});
break;
case 502:
answer = 'Bad Gateway';
messages.push({"role": 'AI', "content": answer});
break;
case 503:
answer = 'Server is busy, please try again later';
messages.push({"role": 'AI', "content": answer});
break;
case 504:
answer = 'Gateway Time-out';
messages.push({"role": 'AI', "content": answer});
break;
case 500:
answer = 'Internal Server Error';
messages.push({"role": 'AI', "content": answer});
break;
default:
answer = 'Unexpected Error';
messages.push({"role": 'AI', "content": answer});
}
// Create form
let form = serverWidget.createForm({
title: 'ChatNetsuite v' + VERSION_NO
});
form.clientScriptModulePath = clientScriptModulePath;
form = addBtnToPage(form, ['draw']);
let fieldGroup = form.addFieldGroup({
id: 'custpage_field_group_form',
label: ' '
});
fieldGroup.isSingleColumn = true;
let bodyText = form.addField({
id: 'custpage_body_text',
type: serverWidget.FieldType.INLINEHTML,
label: ' ',
container: 'custpage_field_group_form'
});
bodyText.defaultValue = '<br>';
messages.forEach((message, index) => {
if (index % 2 === 0) {
bodyText.defaultValue += '<h2 style="color:#607799;">You:</h2>' +
'<article style="font-size:medium;">' + message.content + '</article><br>';
} else {
bodyText.defaultValue += '<h2 style="color:#607799;">ChatNetSuite:</h2>' +
'<article style="font-size:medium;">' + message.content + '</article><br>';
}
});
bodyText.defaultValue += '<br>';
let inputText = form.addField({
id: 'custpage_input',
type: serverWidget.FieldType.LONGTEXT,
label: 'You:',
container: 'custpage_field_group_form'
});
let msgText = form.addField({
id: 'custpage_pstmsg',
type: serverWidget.FieldType.LONGTEXT,
label: ' ',
container: 'custpage_field_group_end'
});
msgText.defaultValue = '';
messages.forEach((message, j) => {
msgText.defaultValue += JSON.stringify(message);
if (j !== messages.length - 1) { msgText.defaultValue += ','; }
});
msgText.updateDisplayType({displayType: serverWidget.FieldDisplayType.HIDDEN});
form.addSubmitButton({label: 'Submit'});
let chatField = form.addField({
id: 'custpage_chat',
label: 'Chat',
type: serverWidget.FieldType.TEXT
});
chatField.defaultValue = 'T';
chatField.updateDisplayType({displayType : serverWidget.FieldDisplayType.HIDDEN});
return form;
}
/**
* Renders the draw page with user input and the image generated.
* @param {Object} scriptContext
*/
const drawPage = (scriptContext) => {
let text = scriptContext.request.parameters.custpage_input;
let form = serverWidget.createForm({title: 'ChatNetsuite v' + VERSION_NO});
form.clientScriptModulePath = clientScriptModulePath;
form = addBtnToPage(form, ['chat']);
let fieldGroup = form.addFieldGroup({
id: 'custpage_field_group_form',
label: ' '
});
fieldGroup.isSingleColumn = true;
let inputField = form.addField({
id: 'custpage_input',
label: 'Describe an image',
type: serverWidget.FieldType.LONGTEXT,
container: 'custpage_field_group_form'
});
if (text) {
// Set payload
let payload = {
"model": DRAW_MODEL,
"prompt": text,
"n": 1,
"size": '1024x1024',
"quality": DRAW_QUALITY
// "response_format": 'url'
};
// Make API call
let response = https.post({
url: DRAW_API_URL,
body: JSON.stringify(payload),
headers: HEADERS
});
let answer;
switch (response.code) {
case 200:
answer = '';
break;
case 401:
answer = 'Incorrect API key provided';
break;
case 402:
answer = 'Server refused to access, please try again later';
break;
case 502:
answer = 'Bad Gateway';
break;
case 503:
answer = 'Server is busy, please try again later';
break;
case 504:
answer = 'Gateway Time-out';
break;
case 500:
answer = 'Internal Server Error';
break;
default:
answer = 'Unexpected Error';
}
let imgUrl = JSON.parse(response.body).data[0].url;
let outputField = form.addField({
id: 'custpage_output',
label: 'Image generated',
type: serverWidget.FieldType.INLINEHTML,
container: 'custpage_field_group_form'
});
outputField.defaultValue = answer;
if (answer === '') {
outputField.defaultValue += '<img src="' + imgUrl + '" alt="' + text + '" />';
}
}
let imageField = form.addField({
id: 'custpage_draw',
label: 'Draw',
type: serverWidget.FieldType.TEXT,
container: 'custpage_field_group_form'
});
imageField.defaultValue = 'T';
imageField.updateDisplayType({displayType : serverWidget.FieldDisplayType.HIDDEN});
form.addSubmitButton({label: 'Submit'});
return form;
}
const addBtnToPage = (form, btnNames=[]) => {
const buttons = {
chat: {
id: 'custpage_chat',
label: 'Chat',
functionName: 'chat()'
},
draw: {
id: 'custpage_draw',
label: 'Draw',
functionName: 'draw()'
},
};
btnNames.forEach(btnName => {
const button = buttons[btnName];
if (button) { form.addButton(button); }
});
return form;
}
return {onRequest}
});