@cobrowser/chatgpt 0.7.48 → 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.
@@ -1,17 +1,17 @@
1
- export declare enum ChatGPTRole {
1
+ export declare enum ChatGptRole {
2
2
  USER = "user",
3
- SYSTEM = "system",
4
- ASSISTANT = "assistant"
3
+ ASSISTANT = "assistant",
4
+ SYSTEM = "system"
5
5
  }
6
- export interface ChatGPTRequestBody {
6
+ export interface ChatGptRequestBody {
7
7
  model: string;
8
8
  response_format?: {
9
9
  type: string;
10
10
  };
11
- messages: ChatGPTMessage[];
11
+ messages: ChatGptMessage[];
12
12
  }
13
- interface ChatGPTMessage {
14
- role: ChatGPTRole;
13
+ interface ChatGptMessage {
14
+ role: ChatGptRole;
15
15
  content: string;
16
16
  }
17
- export default ChatGPTMessage;
17
+ export default ChatGptMessage;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatGPTRole = void 0;
4
- var ChatGPTRole;
5
- (function (ChatGPTRole) {
6
- ChatGPTRole["USER"] = "user";
7
- ChatGPTRole["SYSTEM"] = "system";
8
- ChatGPTRole["ASSISTANT"] = "assistant";
9
- })(ChatGPTRole = exports.ChatGPTRole || (exports.ChatGPTRole = {}));
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 = {}));
@@ -1,6 +1,6 @@
1
- interface ChatGPTUsageTokens {
1
+ interface ChatGptUsageTokens {
2
2
  prompt_tokens: Number;
3
3
  completion_tokens: Number;
4
4
  total_tokens: Number;
5
5
  }
6
- export default ChatGPTUsageTokens;
6
+ export default ChatGptUsageTokens;
@@ -1,8 +1,8 @@
1
- import ChatGptUsageTokens from './ChatGptUsageTokens';
2
- interface ChatGptResponse {
3
- firstChoice?: string;
4
- usageTokens?: ChatGptUsageTokens;
1
+ import ChatGPTUsageTokens from './ChatGPTUsageTokens';
2
+ interface ChatGPTResponse {
3
+ data?: any;
4
+ usageTokens?: ChatGPTUsageTokens;
5
5
  threadId?: string;
6
- requiredActions?: string;
6
+ requiredActions?: any;
7
7
  }
8
- export default ChatGptResponse;
8
+ export default ChatGPTResponse;
@@ -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
- const response = yield openai.responses.create(responseRequest);
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 ((_b = response.conversation) === null || _b === void 0 ? void 0 : _b.id) {
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.48",
3
+ "version": "0.7.50",
4
4
  "description": "chatgpt services to connect our projects with chatgpt api",
5
5
  "keywords": [
6
6
  "chatgpt",
@@ -21,11 +21,10 @@
21
21
  "precommit": "npm run lint && npm run test && npm run upgrade && npm run security"
22
22
  },
23
23
  "dependencies": {
24
- "@cobrowser/logger": "^2.0.6",
25
- "@cobrowser/xss-validation": "^2.0.1",
26
- "axios": "^1.15.0",
27
- "axios-mock-adapter": "^1.22.0",
28
- "openai": "^6.3.0"
24
+ "@cobrowser/logger": "^2.0.7",
25
+ "@cobrowser/xss-validation": "^2.0.3",
26
+ "axios": "^1.16.0",
27
+ "openai": "^6.36.0"
29
28
  },
30
29
  "directories": {
31
30
  "dist": "dist"
@@ -41,5 +40,8 @@
41
40
  "bugs": {
42
41
  "url": "https://bitbucket.org/cobrowser/cb_utils/issues"
43
42
  },
44
- "gitHead": "e91cfd6162da6c34d8f44002d5b5fe18b9a8f311"
43
+ "devDependencies": {
44
+ "axios-mock-adapter": "^2.1.0"
45
+ },
46
+ "gitHead": "c31dea8d5882f65809dda8f5b5aef14e5b9c2b3e"
45
47
  }