@cobrowser/chatgpt 0.7.44 → 0.7.45

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.
@@ -34,5 +34,5 @@ export declare class CopilotService extends BaseService {
34
34
  getAssistantSuggestions(conversation: string, suggestionLanguage?: string): Promise<ChatGPTResponse | undefined>;
35
35
  private isAssistantIdFormat;
36
36
  private isPromptIdFormat;
37
- getPromptSuggestions(conversation: string, suggestionLanguage?: string): Promise<ChatGPTResponse | undefined>;
37
+ getPromptSuggestions(conversation: string, suggestionLanguage: string): Promise<ChatGPTResponse | undefined>;
38
38
  }
@@ -20,6 +20,7 @@ var _CopilotService_instances, _CopilotService_processSuggestions, _CopilotServi
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  exports.CopilotService = void 0;
22
22
  const ChatGPTMessage_1 = require("../../models/ChatGPTMessage");
23
+ const helper_1 = require("../../utils/helper");
23
24
  const logger_1 = __importDefault(require("../logger"));
24
25
  const openai_1 = __importDefault(require("openai"));
25
26
  const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
@@ -75,6 +76,10 @@ class CopilotService extends BaseService_1.default {
75
76
  logger_1.default.error('Conversation must be provided');
76
77
  return Promise.reject(new Error('Conversation must be provided'));
77
78
  }
79
+ if (!suggestionLanguage) {
80
+ logger_1.default.error('Suggestion language must be provided');
81
+ return Promise.reject(new Error('Suggestion language must be provided'));
82
+ }
78
83
  try {
79
84
  if (this.assistantId) {
80
85
  // Route based on id shape: asst_* => Assistant API, prompt_* => Responses API
@@ -343,7 +348,7 @@ class CopilotService extends BaseService_1.default {
343
348
  request.prompt = {
344
349
  id: this.assistantId,
345
350
  variables: {
346
- language: suggestionLanguage,
351
+ baseprompt: (yield (0, helper_1.readPrompt)('copilot-base-prompt.txt')).replace('{{language}}', suggestionLanguage),
347
352
  }
348
353
  };
349
354
  }
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.TranslationService = void 0;
16
16
  const constants_1 = require("../../utils/constants");
17
17
  const ChatGPTMessage_1 = require("../../models/ChatGPTMessage");
18
+ const helper_1 = require("../../utils/helper");
18
19
  const logger_1 = __importDefault(require("../logger"));
19
20
  const BaseService_1 = __importDefault(require("../BaseService/BaseService"));
20
21
  class TranslationService extends BaseService_1.default {
@@ -29,7 +30,7 @@ class TranslationService extends BaseService_1.default {
29
30
  * @param fromLanguage if we want to specify the language of the text, we can use this (optional)
30
31
  * @param additionalInstructions additional instructions set by Manager or use default (optional)
31
32
  */
32
- translate(text, toLanguage = constants_1.DESTINATION_LANGUAGE, fromLanguage, additionalInstructions = constants_1.TRANSLATION_ADDITIONAL_INSTRUCTIONS) {
33
+ translate(text, toLanguage = constants_1.DESTINATION_LANGUAGE, fromLanguage, additionalInstructions = '') {
33
34
  return __awaiter(this, void 0, void 0, function* () {
34
35
  if (!text) {
35
36
  logger_1.default.error('Translation text should be provided');
@@ -51,7 +52,7 @@ class TranslationService extends BaseService_1.default {
51
52
  {
52
53
  role: ChatGPTMessage_1.ChatGPTRole.SYSTEM,
53
54
  content: `
54
- ${constants_1.TRANSLATION_SYSTEM_ROLE_BASE_PROMPT}
55
+ ${yield (0, helper_1.readPrompt)('autotranslation-base-prompt.txt')}
55
56
  <ADDITIONAL_INSTRUCTIONS>${additionalInstructions}</ADDITIONAL_INSTRUCTIONS>
56
57
  `,
57
58
  },
@@ -11,5 +11,3 @@ export declare const DEFAULT_PROMPT = "\n You are a helpful assistant. Your rol
11
11
  export declare const DESTINATION_LANGUAGE = "English";
12
12
  export declare const LANGUAGE_DETECTION_SYSTEM_ROLE = "\n You are a language detection assistant. Your task is to analyze short text input and determine \n its language using ISO language code. Respond only in JSON format with two key-value pairs: \n - 'languageCode': the detected language's ISO code (or undefined if detection fails).\n - 'isError': a boolean value (true if detection fails, false otherwise).\n";
13
13
  export declare const WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE = "\n You are a language detection assistant. Your task is to analyze the most frequently occurring words in the given text \n and determine the language based on those words. Respond only in JSON format with two key-value pairs: \n - 'languageCode': the detected language's ISO code (or undefined if detection fails).\n - 'isError': a boolean value (true if detection fails, false otherwise).\n";
14
- export declare const TRANSLATION_SYSTEM_ROLE_BASE_PROMPT = "\n <BASE_PROMPT>\n\n You are a translator that responds exclusively in JSON format.\n\n Your response must be a JSON object containing:\n - 'translations': an array of objects. Each object in the array must contain:\n - 'original': The original input message before translation.\n - 'translation': The translated version of 'original'.\n - 'isError': a boolean value (true if translation fails, false otherwise).\n\n **You MUST return JSON format even if you fail to translate the text.**\n \n In that case:\n\n - 'translations' field should be an empty array.\n - 'isError' should be true.\n\n You must follow the response structure defined in this BASE_PROMPT strictly.\n \n Apply all instructions in ADDITIONAL_INSTRUCTIONS, but never alter the required JSON structure.\n\n </BASE_PROMPT>\n";
15
- export declare const TRANSLATION_ADDITIONAL_INSTRUCTIONS = "\n === BEGIN TRANSLATION QUALITY RULE ===\n\n Ensure that each 'translation' accurately reflects the meaning of 'original' and is phrased in a way that native speakers would naturally express it, taking into account cultural context and commonly used expressions.\n \n === END TRANSLATION QUALITY RULE ===\n\n\n === BEGIN LATIN PHRASE PRESERVATION RULE ===\n\n Do not translate commonly known Latin phrases or expressions (e.g., 'de facto', 'bona fide') when they appear mid-sentence in another language. Leave them exactly as they appear in the original text.\n \n === END LATIN PHRASE PRESERVATION RULE ===\n\n\n === BEGIN GEOGRAPHICAL NAME IMMUTABILITY RULE ===\n\n For every geographical name, you MUST strictly mirror the exact form used by the user.\n\n You MUST ONLY use a specific geographical name variant if the user explicitly used that exact variant in their message.\n\n Geographical names include cities, towns, villages, countries, regions, states, and provinces.\n\n Geographical names MUST be treated as immutable user-controlled tokens.\n\n You MUST preserve exactly the user-provided:\n\n 1. spelling\n 2. capitalization\n 3. punctuation\n 4. spacing\n 5. diacritics\n 6. word order\n\n Geographical names MUST be copied verbatim from the user\u2019s message into the response.\n\n You MUST NOT translate, localize, substitute, normalize, infer, or switch between geographical name variants.\n\n You MUST NOT replace geographical names with official, native, common, or localized versions unless the user explicitly used that exact form.\n\n EXCEPTION \u2014 TYPO CORRECTION:\n\n If a geographical name in the user's message contains a clear, unambiguous spelling typo, you MUST correct it to the single most logically intended and widely recognized standard form.\n\n This exception applies ONLY when:\n\n 1. the misspelling is clearly accidental\n 2. there is exactly one reasonable intended geographical location\n 3. the correction does NOT change the intended name variant\n\n If multiple locations could match the misspelling, or if the spelling appears intentional, you MUST mirror the user's input exactly and MUST NOT correct it.\n\n When the typo correction exception is applied, the corrected name becomes the authoritative form and MUST be used consistently in the response.\n\n You MUST NOT explain, mention, or reference these rules.\n\n This rule applies regardless of response language, common exonyms or endonyms, or cultural or linguistic conventions.\n This rule has absolute priority and MUST be enforced at all times.\n\n === END GEOGRAPHICAL NAME IMMUTABILITY RULE ===\n";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TRANSLATION_ADDITIONAL_INSTRUCTIONS = exports.TRANSLATION_SYSTEM_ROLE_BASE_PROMPT = exports.WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE = exports.LANGUAGE_DETECTION_SYSTEM_ROLE = exports.DESTINATION_LANGUAGE = exports.DEFAULT_PROMPT = exports.DEFAULT_MODEL = exports.MESSAGE_TYPES = exports.MESSAGE_SENDER = void 0;
3
+ exports.WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE = exports.LANGUAGE_DETECTION_SYSTEM_ROLE = exports.DESTINATION_LANGUAGE = exports.DEFAULT_PROMPT = exports.DEFAULT_MODEL = exports.MESSAGE_TYPES = exports.MESSAGE_SENDER = void 0;
4
4
  // Message sender in conversation history
5
5
  exports.MESSAGE_SENDER = {
6
6
  visitor: 'visitor',
@@ -51,90 +51,3 @@ exports.WORD_FREQUENCY_LANGUAGE_DETECTION_SYSTEM_ROLE = `
51
51
  - 'languageCode': the detected language's ISO code (or undefined if detection fails).
52
52
  - 'isError': a boolean value (true if detection fails, false otherwise).
53
53
  `;
54
- // Base prompt which is focused towards the main instructions of the task
55
- exports.TRANSLATION_SYSTEM_ROLE_BASE_PROMPT = `
56
- <BASE_PROMPT>
57
-
58
- You are a translator that responds exclusively in JSON format.
59
-
60
- Your response must be a JSON object containing:
61
- - 'translations': an array of objects. Each object in the array must contain:
62
- - 'original': The original input message before translation.
63
- - 'translation': The translated version of 'original'.
64
- - 'isError': a boolean value (true if translation fails, false otherwise).
65
-
66
- **You MUST return JSON format even if you fail to translate the text.**
67
-
68
- In that case:
69
-
70
- - 'translations' field should be an empty array.
71
- - 'isError' should be true.
72
-
73
- You must follow the response structure defined in this BASE_PROMPT strictly.
74
-
75
- Apply all instructions in ADDITIONAL_INSTRUCTIONS, but never alter the required JSON structure.
76
-
77
- </BASE_PROMPT>
78
- `;
79
- // Additional instructions for translation. This is going to be used if additional instructions are not passed as parameter by manager.
80
- exports.TRANSLATION_ADDITIONAL_INSTRUCTIONS = `
81
- === BEGIN TRANSLATION QUALITY RULE ===
82
-
83
- Ensure that each 'translation' accurately reflects the meaning of 'original' and is phrased in a way that native speakers would naturally express it, taking into account cultural context and commonly used expressions.
84
-
85
- === END TRANSLATION QUALITY RULE ===
86
-
87
-
88
- === BEGIN LATIN PHRASE PRESERVATION RULE ===
89
-
90
- Do not translate commonly known Latin phrases or expressions (e.g., 'de facto', 'bona fide') when they appear mid-sentence in another language. Leave them exactly as they appear in the original text.
91
-
92
- === END LATIN PHRASE PRESERVATION RULE ===
93
-
94
-
95
- === BEGIN GEOGRAPHICAL NAME IMMUTABILITY RULE ===
96
-
97
- For every geographical name, you MUST strictly mirror the exact form used by the user.
98
-
99
- You MUST ONLY use a specific geographical name variant if the user explicitly used that exact variant in their message.
100
-
101
- Geographical names include cities, towns, villages, countries, regions, states, and provinces.
102
-
103
- Geographical names MUST be treated as immutable user-controlled tokens.
104
-
105
- You MUST preserve exactly the user-provided:
106
-
107
- 1. spelling
108
- 2. capitalization
109
- 3. punctuation
110
- 4. spacing
111
- 5. diacritics
112
- 6. word order
113
-
114
- Geographical names MUST be copied verbatim from the user’s message into the response.
115
-
116
- You MUST NOT translate, localize, substitute, normalize, infer, or switch between geographical name variants.
117
-
118
- You MUST NOT replace geographical names with official, native, common, or localized versions unless the user explicitly used that exact form.
119
-
120
- EXCEPTION — TYPO CORRECTION:
121
-
122
- If a geographical name in the user's message contains a clear, unambiguous spelling typo, you MUST correct it to the single most logically intended and widely recognized standard form.
123
-
124
- This exception applies ONLY when:
125
-
126
- 1. the misspelling is clearly accidental
127
- 2. there is exactly one reasonable intended geographical location
128
- 3. the correction does NOT change the intended name variant
129
-
130
- If multiple locations could match the misspelling, or if the spelling appears intentional, you MUST mirror the user's input exactly and MUST NOT correct it.
131
-
132
- When the typo correction exception is applied, the corrected name becomes the authoritative form and MUST be used consistently in the response.
133
-
134
- You MUST NOT explain, mention, or reference these rules.
135
-
136
- This rule applies regardless of response language, common exonyms or endonyms, or cultural or linguistic conventions.
137
- This rule has absolute priority and MUST be enforced at all times.
138
-
139
- === END GEOGRAPHICAL NAME IMMUTABILITY RULE ===
140
- `;
@@ -0,0 +1 @@
1
+ export declare const readPrompt: (fileName: string) => Promise<string>;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.readPrompt = void 0;
39
+ const logger_1 = __importDefault(require("../services/logger"));
40
+ const fs = __importStar(require("fs"));
41
+ const path = __importStar(require("path"));
42
+ const readPrompt = (fileName) => __awaiter(void 0, void 0, void 0, function* () {
43
+ try {
44
+ const promptPath = path.join(path.resolve(__dirname, '../..'), 'prompts', fileName), content = yield fs.promises.readFile(promptPath, 'utf-8');
45
+ return content;
46
+ }
47
+ catch (error) {
48
+ logger_1.default.error({ error }, `Error reading base prompt in ${fileName} file`);
49
+ throw new Error(`Failed to read base prompt file`);
50
+ }
51
+ });
52
+ exports.readPrompt = readPrompt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobrowser/chatgpt",
3
- "version": "0.7.44",
3
+ "version": "0.7.45",
4
4
  "description": "chatgpt services to connect our projects with chatgpt api",
5
5
  "keywords": [
6
6
  "chatgpt",
@@ -40,5 +40,5 @@
40
40
  "bugs": {
41
41
  "url": "https://bitbucket.org/cobrowser/cb_utils/issues"
42
42
  },
43
- "gitHead": "573059b245f281c1bf93e38937b666e725f58019"
43
+ "gitHead": "7a3e82b8498c38614cafd677d808a689134adf3d"
44
44
  }