@adobe/spacecat-shared-utils 1.26.6 → 1.28.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-utils-v1.28.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.27.0...@adobe/spacecat-shared-utils-v1.28.0) (2025-01-21)
2
+
3
+
4
+ ### Features
5
+
6
+ * introduce get prompt from static files ([#552](https://github.com/adobe/spacecat-shared/issues/552)) ([0ae7392](https://github.com/adobe/spacecat-shared/commit/0ae739243722812c6b895fe5d843424d23b7f29c))
7
+
8
+ # [@adobe/spacecat-shared-utils-v1.27.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.26.6...@adobe/spacecat-shared-utils-v1.27.0) (2025-01-21)
9
+
10
+
11
+ ### Features
12
+
13
+ * introduce replace placeholders for prompts ([#551](https://github.com/adobe/spacecat-shared/issues/551)) ([d3b542c](https://github.com/adobe/spacecat-shared/commit/d3b542cf1b546256b3108f989220d07410cdc87d))
14
+
1
15
  # [@adobe/spacecat-shared-utils-v1.26.6](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.26.5...@adobe/spacecat-shared-utils-v1.26.6) (2025-01-18)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.26.6",
3
+ "version": "1.28.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "engines": {
package/src/helpers.js CHANGED
@@ -12,6 +12,7 @@
12
12
 
13
13
  import { Parser } from '@json2csv/plainjs';
14
14
  import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
15
+ import { promises as fs } from 'fs';
15
16
  import { isString } from './functions.js';
16
17
 
17
18
  /**
@@ -92,3 +93,41 @@ export function generateCSVFile(data) {
92
93
  const json2csvParser = new Parser();
93
94
  return Buffer.from(json2csvParser.parse(data), 'utf-8');
94
95
  }
96
+
97
+ /**
98
+ * Replaces placeholders in the prompt content with their corresponding values.
99
+ *
100
+ * @param {string} content - The prompt content with placeholders.
101
+ * @param {Object} placeholders - The placeholders and their values.
102
+ * @returns {string} - The content with placeholders replaced.
103
+ */
104
+ export function replacePlaceholders(content, placeholders) {
105
+ return content.replace(/{{(.*?)}}/g, (match, key) => {
106
+ if (key in placeholders) {
107
+ const value = placeholders[key];
108
+ return typeof value === 'object' && value !== null ? JSON.stringify(value) : value;
109
+ } else {
110
+ return match;
111
+ }
112
+ });
113
+ }
114
+
115
+ /**
116
+ * Reads the content of a prompt file asynchronously and replaces any placeholders
117
+ * with the corresponding values. Logs the error and returns null in case of an error.
118
+ *
119
+ * @param {Object} placeholders - A JSON object containing values to replace in the prompt content.
120
+ * @param {String} filename - The filename of the prompt file.
121
+ * @param {Object} log - The logger
122
+ * @returns {Promise<string|null>} - A promise that resolves to a string with the prompt content,
123
+ * or null if an error occurs.
124
+ */
125
+ export async function getPrompt(placeholders, filename, log = console) {
126
+ try {
127
+ const promptContent = await fs.readFile(`./static/prompts/${filename}.prompt`, { encoding: 'utf8' });
128
+ return replacePlaceholders(promptContent, placeholders);
129
+ } catch (error) {
130
+ log.error('Error reading prompt file:', error.message);
131
+ return null;
132
+ }
133
+ }
package/src/index.d.ts CHANGED
@@ -143,6 +143,28 @@ declare function getRUMDomainKey(baseURL: string, ctx: object): Promise<string>;
143
143
  */
144
144
  declare function generateCSVFile(data: object[]): Buffer;
145
145
 
146
+ /**
147
+ * Replaces placeholders in the prompt content with their corresponding values.
148
+ *
149
+ * @param {string} content - The prompt content with placeholders.
150
+ * @param {Object} placeholders - The placeholders and their values.
151
+ * @returns {string} - The content with placeholders replaced.
152
+ */
153
+ declare function replacePlaceholders(content: string, placeholders: object): string;
154
+
155
+ /**
156
+ * Reads the content of a prompt file asynchronously and replaces any placeholders
157
+ * with the corresponding values. Logs the error and returns null in case of an error.
158
+ *
159
+ * @param {Object} placeholders - A JSON object containing values to replace in the prompt content.
160
+ * @param {String} filename - The filename of the prompt file.
161
+ * @param {Object} log - The logger
162
+ * @returns {Promise<string|null>} - A promise that resolves to a string with the prompt content,
163
+ * or null if an error occurs.
164
+ */
165
+ declare function getPrompt(placeholders: object, filename: string, log: object):
166
+ Promise<string|null>;
167
+
146
168
  /**
147
169
  * Retrieves stored metrics from S3.
148
170
  * @param config - Configuration object