@cobrowser/chatgpt 0.7.49 → 0.7.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/models/ChatGPTMessage.d.ts +17 -0
- package/dist/models/ChatGPTMessage.js +9 -0
- package/dist/models/ChatGPTUsageTokens.d.ts +6 -0
- package/dist/models/ChatGptResponse.d.ts +8 -0
- package/dist/models/ChatGptResponse.js +2 -0
- package/dist/models/ChatGptUsageTokens.js +2 -0
- package/dist/services/ResponseService/ResponseService.d.ts +2 -0
- package/dist/services/ResponseService/ResponseService.js +36 -3
- package/package.json +3 -2
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare enum ChatGptRole {
|
|
2
|
+
USER = "user",
|
|
3
|
+
ASSISTANT = "assistant",
|
|
4
|
+
SYSTEM = "system"
|
|
5
|
+
}
|
|
6
|
+
export interface ChatGptRequestBody {
|
|
7
|
+
model: string;
|
|
8
|
+
response_format?: {
|
|
9
|
+
type: string;
|
|
10
|
+
};
|
|
11
|
+
messages: ChatGptMessage[];
|
|
12
|
+
}
|
|
13
|
+
interface ChatGptMessage {
|
|
14
|
+
role: ChatGptRole;
|
|
15
|
+
content: string;
|
|
16
|
+
}
|
|
17
|
+
export default ChatGptMessage;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChatGptRole = void 0;
|
|
4
|
+
var ChatGptRole;
|
|
5
|
+
(function (ChatGptRole) {
|
|
6
|
+
ChatGptRole["USER"] = "user";
|
|
7
|
+
ChatGptRole["ASSISTANT"] = "assistant";
|
|
8
|
+
ChatGptRole["SYSTEM"] = "system";
|
|
9
|
+
})(ChatGptRole = exports.ChatGptRole || (exports.ChatGptRole = {}));
|
|
@@ -8,6 +8,8 @@ interface ResponseServiceOptions {
|
|
|
8
8
|
conversationId?: string;
|
|
9
9
|
}
|
|
10
10
|
export declare class ResponseService extends BaseService {
|
|
11
|
+
WAIT_TIME_FOR_CONVERSATION_BUSY: number;
|
|
12
|
+
MAX_ATTEMPTS_FOR_CONVERSATION_BUSY: number;
|
|
11
13
|
promptId: string | undefined;
|
|
12
14
|
conversationId: string | undefined;
|
|
13
15
|
lastResponseId: string | undefined;
|
|
@@ -19,6 +19,18 @@ const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
|
|
|
19
19
|
class ResponseService extends BaseService_1.default {
|
|
20
20
|
constructor(apiKey, promptId, conversationId) {
|
|
21
21
|
super(apiKey);
|
|
22
|
+
Object.defineProperty(this, "WAIT_TIME_FOR_CONVERSATION_BUSY", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: 1500
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "MAX_ATTEMPTS_FOR_CONVERSATION_BUSY", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: 6
|
|
33
|
+
});
|
|
22
34
|
Object.defineProperty(this, "promptId", {
|
|
23
35
|
enumerable: true,
|
|
24
36
|
configurable: true,
|
|
@@ -58,7 +70,7 @@ class ResponseService extends BaseService_1.default {
|
|
|
58
70
|
* @param runOptions
|
|
59
71
|
*/
|
|
60
72
|
getReply(message, runOptions = {}) {
|
|
61
|
-
var _a, _b;
|
|
73
|
+
var _a, _b, _c, _d, _e;
|
|
62
74
|
return __awaiter(this, void 0, void 0, function* () {
|
|
63
75
|
if (!this.promptId) {
|
|
64
76
|
logger_1.default.error('Prompt ID is required for Response API');
|
|
@@ -115,9 +127,30 @@ class ResponseService extends BaseService_1.default {
|
|
|
115
127
|
id: this.promptId
|
|
116
128
|
};
|
|
117
129
|
}
|
|
118
|
-
|
|
130
|
+
/**
|
|
131
|
+
* As OPENAI API is rate limited (only 1 message to respond at a time)
|
|
132
|
+
* We need to handle the conversation busy error.
|
|
133
|
+
* If the conversation is busy, wait for the WAIT_TIME_FOR_CONVERSATION_BUSY and try again.
|
|
134
|
+
* If the conversation is busy after MAX_ATTEMPTS_FOR_CONVERSATION_BUSY attempts, throw an error.
|
|
135
|
+
*/
|
|
136
|
+
let response;
|
|
137
|
+
for (let attempt = 1; attempt <= this.MAX_ATTEMPTS_FOR_CONVERSATION_BUSY; attempt++) {
|
|
138
|
+
try {
|
|
139
|
+
response = yield openai.responses.create(responseRequest);
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
const errorMessage = (_d = (_c = (_b = error === null || error === void 0 ? void 0 : error.error) === null || _b === void 0 ? void 0 : _b.message) !== null && _c !== void 0 ? _c : error === null || error === void 0 ? void 0 : error.message) !== null && _d !== void 0 ? _d : '';
|
|
144
|
+
const isConversationBusy = (error === null || error === void 0 ? void 0 : error.status) === 400
|
|
145
|
+
&& errorMessage.includes('Another process is currently operating on this conversation');
|
|
146
|
+
if (!isConversationBusy || attempt === this.MAX_ATTEMPTS_FOR_CONVERSATION_BUSY) {
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
yield new Promise((resolve) => setTimeout(resolve, this.WAIT_TIME_FOR_CONVERSATION_BUSY));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
119
152
|
this.lastResponseId = response.id;
|
|
120
|
-
if ((
|
|
153
|
+
if ((_e = response.conversation) === null || _e === void 0 ? void 0 : _e.id) {
|
|
121
154
|
this.conversationId = response.conversation.id;
|
|
122
155
|
}
|
|
123
156
|
else if (runOptions.conversationId) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobrowser/chatgpt",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.50",
|
|
4
4
|
"description": "chatgpt services to connect our projects with chatgpt api",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chatgpt",
|
|
@@ -42,5 +42,6 @@
|
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"axios-mock-adapter": "^2.1.0"
|
|
45
|
-
}
|
|
45
|
+
},
|
|
46
|
+
"gitHead": "c31dea8d5882f65809dda8f5b5aef14e5b9c2b3e"
|
|
46
47
|
}
|