@cobrowser/chatgpt 0.7.49 → 0.7.51
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 +8 -8
- package/dist/models/ChatGptMessage.js +7 -7
- package/dist/models/{ChatGPTResponse.d.ts → ChatGptResponse.d.ts} +1 -1
- package/dist/models/ChatGptUsageTokens.d.ts +2 -2
- package/dist/prompts/copilot-base-prompt.txt +4 -0
- package/dist/services/AssistantService/AssistantService.d.ts +1 -1
- package/dist/services/BaseService/BaseService.d.ts +1 -1
- package/dist/services/ChatService/ChatService.d.ts +1 -1
- package/dist/services/ChatService/ChatService.js +5 -5
- package/dist/services/CopilotService/CopilotService.d.ts +1 -1
- package/dist/services/CopilotService/CopilotService.js +2 -2
- package/dist/services/ResponseService/ResponseService.d.ts +3 -1
- package/dist/services/ResponseService/ResponseService.js +36 -3
- package/dist/services/TranslationService/TranslationService.d.ts +1 -1
- package/dist/services/TranslationService/TranslationService.js +7 -7
- package/package.json +2 -2
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
export declare enum
|
|
1
|
+
export declare enum ChatGPTRole {
|
|
2
2
|
USER = "user",
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
SYSTEM = "system",
|
|
4
|
+
ASSISTANT = "assistant"
|
|
5
5
|
}
|
|
6
|
-
export interface
|
|
6
|
+
export interface ChatGPTRequestBody {
|
|
7
7
|
model: string;
|
|
8
8
|
response_format?: {
|
|
9
9
|
type: string;
|
|
10
10
|
};
|
|
11
|
-
messages:
|
|
11
|
+
messages: ChatGPTMessage[];
|
|
12
12
|
}
|
|
13
|
-
interface
|
|
14
|
-
role:
|
|
13
|
+
interface ChatGPTMessage {
|
|
14
|
+
role: ChatGPTRole;
|
|
15
15
|
content: string;
|
|
16
16
|
}
|
|
17
|
-
export default
|
|
17
|
+
export default ChatGPTMessage;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
var
|
|
5
|
-
(function (
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
})(
|
|
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 = {}));
|
|
@@ -8,6 +8,10 @@ OUTPUT FORMAT — HARD
|
|
|
8
8
|
Your response must ONLY contain a valid JSON array of 2 to 3 strings.
|
|
9
9
|
Each string must be a complete customer-facing response.
|
|
10
10
|
|
|
11
|
+
OUTPUT SANITIZATION — HARD
|
|
12
|
+
- Never show citations, references, IDs, or source markers (e.g. filecite, turn0fileX)
|
|
13
|
+
- Always remove any internal markers from the final message
|
|
14
|
+
|
|
11
15
|
CONSISTENCY RULE — HARD
|
|
12
16
|
All responses MUST follow the exact same decision path and outcome.
|
|
13
17
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AssistantResponseFormatOption } from 'openai/resources/beta/threads/threads';
|
|
2
2
|
import BaseService from '../BaseService/BaseService';
|
|
3
|
-
import ChatGPTResponse from '../../models/
|
|
3
|
+
import ChatGPTResponse from '../../models/ChatGptResponse';
|
|
4
4
|
export declare class AssistantService extends BaseService {
|
|
5
5
|
assistantId: string | undefined;
|
|
6
6
|
threadId: string | undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
-
import ChatGPTUsageTokens from '../../models/
|
|
2
|
+
import ChatGPTUsageTokens from '../../models/ChatGptUsageTokens';
|
|
3
3
|
declare class BaseService {
|
|
4
4
|
readonly BASE_CHAT_COMPLETION_URL = "https://api.openai.com/v1/chat/completions";
|
|
5
5
|
request: AxiosInstance;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BaseService from '../BaseService/BaseService';
|
|
2
|
-
import ChatGPTResponse from 'src/models/
|
|
2
|
+
import ChatGPTResponse from 'src/models/ChatGptResponse';
|
|
3
3
|
import ConversationHistory from 'src/models/ConversationHistory';
|
|
4
4
|
export declare class ChatService extends BaseService {
|
|
5
5
|
#private;
|
|
@@ -19,7 +19,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
19
19
|
var _ChatService_instances, _ChatService_formatConversationHistory;
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
21
|
exports.ChatService = void 0;
|
|
22
|
-
const
|
|
22
|
+
const ChatGptMessage_1 = require("../../models/ChatGptMessage");
|
|
23
23
|
const constants_1 = require("../../utils/constants");
|
|
24
24
|
const logger_1 = __importDefault(require("../logger"));
|
|
25
25
|
const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
|
|
@@ -66,7 +66,7 @@ exports.ChatService = ChatService;
|
|
|
66
66
|
_ChatService_instances = new WeakSet(), _ChatService_formatConversationHistory = function _ChatService_formatConversationHistory(context, conversationHistory) {
|
|
67
67
|
const formatted = [
|
|
68
68
|
{
|
|
69
|
-
role:
|
|
69
|
+
role: ChatGptMessage_1.ChatGPTRole.SYSTEM,
|
|
70
70
|
content: context || constants_1.DEFAULT_PROMPT,
|
|
71
71
|
}
|
|
72
72
|
];
|
|
@@ -76,19 +76,19 @@ _ChatService_instances = new WeakSet(), _ChatService_formatConversationHistory =
|
|
|
76
76
|
for (const body of messageBody) {
|
|
77
77
|
if (body.type === constants_1.MESSAGE_TYPES.text && message.from === constants_1.MESSAGE_SENDER.visitor) {
|
|
78
78
|
formatted.push({
|
|
79
|
-
role:
|
|
79
|
+
role: ChatGptMessage_1.ChatGPTRole.USER,
|
|
80
80
|
content: body.text
|
|
81
81
|
});
|
|
82
82
|
}
|
|
83
83
|
else if (body.type === constants_1.MESSAGE_TYPES.text && message.from === constants_1.MESSAGE_SENDER.chatbot) {
|
|
84
84
|
formatted.push({
|
|
85
|
-
role:
|
|
85
|
+
role: ChatGptMessage_1.ChatGPTRole.ASSISTANT,
|
|
86
86
|
content: body.text
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
else if (body.type === constants_1.MESSAGE_TYPES.button && message.from === constants_1.MESSAGE_SENDER.chatbot) {
|
|
90
90
|
formatted.push({
|
|
91
|
-
role:
|
|
91
|
+
role: ChatGptMessage_1.ChatGPTRole.ASSISTANT,
|
|
92
92
|
content: body.label
|
|
93
93
|
});
|
|
94
94
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BaseService from '../BaseService/BaseService';
|
|
2
|
-
import ChatGPTResponse from '../../models/
|
|
2
|
+
import ChatGPTResponse from '../../models/ChatGptResponse';
|
|
3
3
|
export declare class CopilotService extends BaseService {
|
|
4
4
|
#private;
|
|
5
5
|
assistantId: string | undefined;
|
|
@@ -19,7 +19,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
19
19
|
var _CopilotService_instances, _CopilotService_processSuggestions, _CopilotService_prepareAssistantResponseAnswer;
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
21
|
exports.CopilotService = void 0;
|
|
22
|
-
const
|
|
22
|
+
const ChatGptMessage_1 = require("../../models/ChatGptMessage");
|
|
23
23
|
const helper_1 = require("../../utils/helper");
|
|
24
24
|
const logger_1 = __importDefault(require("../logger"));
|
|
25
25
|
const openai_1 = __importDefault(require("openai"));
|
|
@@ -117,7 +117,7 @@ class CopilotService extends BaseService_1.default {
|
|
|
117
117
|
|
|
118
118
|
${suggestionLanguage ? `The replies must be in ${suggestionLanguage}.` : ''}
|
|
119
119
|
`.trim(), mainPromptMessage = {
|
|
120
|
-
role:
|
|
120
|
+
role: ChatGptMessage_1.ChatGPTRole.USER,
|
|
121
121
|
content: mainPrompt,
|
|
122
122
|
},
|
|
123
123
|
// the body we need to send to the request
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import ChatGPTResponse from '../../models/
|
|
1
|
+
import ChatGPTResponse from '../../models/ChatGptResponse';
|
|
2
2
|
import BaseService from '../BaseService/BaseService';
|
|
3
3
|
interface ResponseServiceOptions {
|
|
4
4
|
instructions?: string;
|
|
@@ -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) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BaseService from '../BaseService/BaseService';
|
|
2
|
-
import ChatGPTResponse from '../../models/
|
|
2
|
+
import ChatGPTResponse from '../../models/ChatGptResponse';
|
|
3
3
|
export declare class TranslationService extends BaseService {
|
|
4
4
|
constructor(apiKey?: string, model?: string, apiUrl?: string, maxNumberOfChoices?: number);
|
|
5
5
|
/**
|
|
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.TranslationService = void 0;
|
|
16
16
|
const constants_1 = require("../../utils/constants");
|
|
17
|
-
const
|
|
17
|
+
const ChatGptMessage_1 = require("../../models/ChatGptMessage");
|
|
18
18
|
const helper_1 = require("../../utils/helper");
|
|
19
19
|
const logger_1 = __importDefault(require("../logger"));
|
|
20
20
|
const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
|
|
@@ -50,14 +50,14 @@ class TranslationService extends BaseService_1.default {
|
|
|
50
50
|
response_format: { type: 'json_object' },
|
|
51
51
|
messages: [
|
|
52
52
|
{
|
|
53
|
-
role:
|
|
53
|
+
role: ChatGptMessage_1.ChatGPTRole.SYSTEM,
|
|
54
54
|
content: `
|
|
55
55
|
${yield (0, helper_1.readPrompt)('autotranslation-base-prompt.txt')}
|
|
56
56
|
<ADDITIONAL_INSTRUCTIONS>${additionalInstructions}</ADDITIONAL_INSTRUCTIONS>
|
|
57
57
|
`,
|
|
58
58
|
},
|
|
59
59
|
{
|
|
60
|
-
role:
|
|
60
|
+
role: ChatGptMessage_1.ChatGPTRole.USER,
|
|
61
61
|
content: translateText,
|
|
62
62
|
},
|
|
63
63
|
],
|
|
@@ -92,11 +92,11 @@ class TranslationService extends BaseService_1.default {
|
|
|
92
92
|
response_format: { type: 'json_object' },
|
|
93
93
|
messages: [
|
|
94
94
|
{
|
|
95
|
-
role:
|
|
95
|
+
role: ChatGptMessage_1.ChatGPTRole.SYSTEM,
|
|
96
96
|
content: constants_1.LANGUAGE_DETECTION_SYSTEM_ROLE,
|
|
97
97
|
},
|
|
98
98
|
{
|
|
99
|
-
role:
|
|
99
|
+
role: ChatGptMessage_1.ChatGPTRole.USER,
|
|
100
100
|
content: translateText,
|
|
101
101
|
}
|
|
102
102
|
],
|
|
@@ -131,11 +131,11 @@ class TranslationService extends BaseService_1.default {
|
|
|
131
131
|
response_format: { type: 'json_object' },
|
|
132
132
|
messages: [
|
|
133
133
|
{
|
|
134
|
-
role:
|
|
134
|
+
role: ChatGptMessage_1.ChatGPTRole.SYSTEM,
|
|
135
135
|
content: constants_1.WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE,
|
|
136
136
|
},
|
|
137
137
|
{
|
|
138
|
-
role:
|
|
138
|
+
role: ChatGptMessage_1.ChatGPTRole.USER,
|
|
139
139
|
content: translateText,
|
|
140
140
|
}
|
|
141
141
|
],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobrowser/chatgpt",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.51",
|
|
4
4
|
"description": "chatgpt services to connect our projects with chatgpt api",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chatgpt",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"@cobrowser/logger": "^2.0.7",
|
|
25
25
|
"@cobrowser/xss-validation": "^2.0.3",
|
|
26
26
|
"axios": "^1.16.0",
|
|
27
|
-
"openai": "^6.
|
|
27
|
+
"openai": "^6.38.0"
|
|
28
28
|
},
|
|
29
29
|
"directories": {
|
|
30
30
|
"dist": "dist"
|