biz-email-builder-shared 1.6.49 → 1.6.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.
@@ -0,0 +1,10 @@
1
+ import { IMessageSchema } from '../entity';
2
+ interface IPromptOptions {
3
+ model: string;
4
+ apiKey: string;
5
+ parseJSON: boolean;
6
+ }
7
+ export declare function getMessage(promptConfig: IMessageSchema, payload: Record<string, any>): string;
8
+ export declare const getPromptResult: (retryCount: number, options: IPromptOptions, userMessage: IMessageSchema, systemMessage: IMessageSchema, payload: Record<string, any>) => Promise<string | object>;
9
+ export {};
10
+ //# sourceMappingURL=getPromptResult.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPromptResult.d.ts","sourceRoot":"","sources":["../../src/utilities/getPromptResult.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C,UAAU,cAAc;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,OAAO,CAAC;CACpB;AAOD,wBAAgB,UAAU,CAAC,YAAY,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAO7F;AAED,eAAO,MAAM,eAAe,eACd,MAAM,WACT,cAAc,eACV,cAAc,iBACZ,cAAc,WACpB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAC3B,OAAO,CAAC,MAAM,GAAG,MAAM,CA4DzB,CAAC"}
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
3
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
4
+ var m = o[Symbol.asyncIterator], i;
5
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
6
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
7
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
8
+ };
9
+ var __importDefault = (this && this.__importDefault) || function (mod) {
10
+ return (mod && mod.__esModule) ? mod : { "default": mod };
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.getPromptResult = void 0;
14
+ exports.getMessage = getMessage;
15
+ const openai_1 = __importDefault(require("openai"));
16
+ function getMessage(promptConfig, payload) {
17
+ if (!promptConfig)
18
+ return "";
19
+ const { keys = [], message = "" } = promptConfig;
20
+ return keys.reduce((msg, key) => {
21
+ var _a;
22
+ return msg.replace(`\$\{${key}\}`, (_a = payload[key]) !== null && _a !== void 0 ? _a : "");
23
+ }, message).trim();
24
+ }
25
+ const getPromptResult = async (retryCount, options, userMessage, systemMessage, payload) => {
26
+ var _a, e_1, _b, _c;
27
+ var _d, _e, _f;
28
+ const { apiKey, model, parseJSON } = options;
29
+ const openai = new openai_1.default({ apiKey });
30
+ const systemRefinePrompt = getMessage(systemMessage, payload);
31
+ const userRefinePrompt = getMessage(userMessage, payload);
32
+ const messages = [];
33
+ if (systemRefinePrompt) {
34
+ messages.push({ role: 'system', content: systemRefinePrompt });
35
+ }
36
+ if (parseJSON) {
37
+ messages.push({
38
+ role: 'system',
39
+ content: 'Content must be provided in JSON format. Validate JSON before responding and return only valid JSON.',
40
+ }, {
41
+ role: 'system',
42
+ content: 'Return only the JSON code. Do not include any text before or after the JSON.',
43
+ });
44
+ }
45
+ if (userRefinePrompt) {
46
+ messages.push({ role: 'user', content: userRefinePrompt });
47
+ }
48
+ let content = "";
49
+ try {
50
+ const stream = await openai.chat.completions.create({
51
+ model,
52
+ messages,
53
+ stream: true,
54
+ });
55
+ try {
56
+ for (var _g = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = await stream_1.next(), _a = stream_1_1.done, !_a; _g = true) {
57
+ _c = stream_1_1.value;
58
+ _g = false;
59
+ const part = _c;
60
+ content += (_f = (_e = (_d = part.choices[0]) === null || _d === void 0 ? void 0 : _d.delta) === null || _e === void 0 ? void 0 : _e.content) !== null && _f !== void 0 ? _f : '';
61
+ }
62
+ }
63
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
64
+ finally {
65
+ try {
66
+ if (!_g && !_a && (_b = stream_1.return)) await _b.call(stream_1);
67
+ }
68
+ finally { if (e_1) throw e_1.error; }
69
+ }
70
+ if (parseJSON) {
71
+ const sanitized = content.replace(/```json|```/g, "");
72
+ return JSON.parse(sanitized);
73
+ }
74
+ return content;
75
+ }
76
+ catch (error) {
77
+ console.error("getPromptResult error =>", error);
78
+ if (retryCount <= 1) {
79
+ throw new Error((error === null || error === void 0 ? void 0 : error.status) ? `OPENAI - ${error.status}` : error.message || "Unknown OpenAI error");
80
+ }
81
+ return (0, exports.getPromptResult)(retryCount - 1, options, userMessage, systemMessage, payload);
82
+ }
83
+ };
84
+ exports.getPromptResult = getPromptResult;
@@ -10,4 +10,5 @@ export * from "./sendMailViaSMTP";
10
10
  export * from "./sendMailViaEnvFallback";
11
11
  export * from "./sendMail";
12
12
  export * from "./sendFromGmail";
13
+ export * from "./getPromptResult";
13
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utilities/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utilities/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC"}
@@ -26,3 +26,4 @@ __exportStar(require("./sendMailViaSMTP"), exports);
26
26
  __exportStar(require("./sendMailViaEnvFallback"), exports);
27
27
  __exportStar(require("./sendMail"), exports);
28
28
  __exportStar(require("./sendFromGmail"), exports);
29
+ __exportStar(require("./getPromptResult"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "biz-email-builder-shared",
3
- "version": "1.6.49",
3
+ "version": "1.6.50",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -18,6 +18,7 @@
18
18
  "jsonwebtoken": "^9.0.2",
19
19
  "mongoose": "^8.10.1",
20
20
  "nodemailer": "^6.10.0",
21
+ "openai": "^5.5.0",
21
22
  "typescript": "^5.3.3"
22
23
  },
23
24
  "devDependencies": {