@promptbook/utils 0.101.0-2 โ 0.101.0-4
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/README.md
CHANGED
@@ -48,9 +48,34 @@ npm i ptbk
|
|
48
48
|
npm install @promptbook/utils
|
49
49
|
```
|
50
50
|
|
51
|
-
|
51
|
+
Comprehensive utility functions for text processing, validation, normalization, and LLM input/output handling in the Promptbook ecosystem.
|
52
52
|
|
53
|
-
|
53
|
+
## ๐ฏ Purpose and Motivation
|
54
|
+
|
55
|
+
The utils package provides a rich collection of utility functions that are essential for working with LLM inputs and outputs. It handles common tasks like text normalization, parameter templating, validation, and postprocessing, eliminating the need to implement these utilities from scratch in every promptbook application.
|
56
|
+
|
57
|
+
## ๐ง High-Level Functionality
|
58
|
+
|
59
|
+
This package offers utilities across multiple domains:
|
60
|
+
|
61
|
+
- **Text Processing**: Counting, splitting, and analyzing text content
|
62
|
+
- **Template System**: Secure parameter substitution and prompt formatting
|
63
|
+
- **Normalization**: Converting text to various naming conventions and formats
|
64
|
+
- **Validation**: Comprehensive validation for URLs, emails, file paths, and more
|
65
|
+
- **Serialization**: JSON handling, deep cloning, and object manipulation
|
66
|
+
- **Environment Detection**: Runtime environment identification utilities
|
67
|
+
- **Format Parsing**: Support for CSV, JSON, XML validation and parsing
|
68
|
+
|
69
|
+
## โจ Key Features
|
70
|
+
|
71
|
+
- ๐ **Secure Templating** - Prompt injection protection with template functions
|
72
|
+
- ๐ **Text Analysis** - Count words, sentences, paragraphs, pages, and characters
|
73
|
+
- ๐ **Case Conversion** - Support for kebab-case, camelCase, PascalCase, SCREAMING_CASE
|
74
|
+
- โ
**Comprehensive Validation** - Email, URL, file path, UUID, and format validators
|
75
|
+
- ๐งน **Text Cleaning** - Remove emojis, quotes, diacritics, and normalize whitespace
|
76
|
+
- ๐ฆ **Serialization Tools** - Deep cloning, JSON export, and serialization checking
|
77
|
+
- ๐ **Environment Aware** - Detect browser, Node.js, Jest, and Web Worker environments
|
78
|
+
- ๐ฏ **LLM Optimized** - Functions specifically designed for LLM input/output processing
|
54
79
|
|
55
80
|
## Simple templating
|
56
81
|
|
@@ -184,133 +209,149 @@ import { unwrapResult } from '@promptbook/utils';
|
|
184
209
|
unwrapResult('Best greeting for the user is "Hi Pavol!"'); // 'Hi Pavol!'
|
185
210
|
```
|
186
211
|
|
187
|
-
##
|
188
|
-
|
189
|
-
## ๐ฆ Complete List of Exported Entities
|
212
|
+
## ๐ฆ Exported Entities
|
190
213
|
|
191
214
|
### Version Information
|
192
|
-
|
193
|
-
-
|
215
|
+
|
216
|
+
- `BOOK_LANGUAGE_VERSION` - Current book language version
|
217
|
+
- `PROMPTBOOK_ENGINE_VERSION` - Current engine version
|
194
218
|
|
195
219
|
### Configuration Constants
|
196
|
-
|
197
|
-
-
|
220
|
+
|
221
|
+
- `VALUE_STRINGS` - Standard value strings
|
222
|
+
- `SMALL_NUMBER` - Small number constant
|
198
223
|
|
199
224
|
### Visualization
|
200
|
-
|
225
|
+
|
226
|
+
- `renderPromptbookMermaid` - Render promptbook as Mermaid diagram
|
201
227
|
|
202
228
|
### Error Handling
|
203
|
-
|
204
|
-
-
|
229
|
+
|
230
|
+
- `deserializeError` - Deserialize error objects
|
231
|
+
- `serializeError` - Serialize error objects
|
205
232
|
|
206
233
|
### Async Utilities
|
207
|
-
|
234
|
+
|
235
|
+
- `forEachAsync` - Async forEach implementation
|
208
236
|
|
209
237
|
### Format Validation
|
210
|
-
|
211
|
-
-
|
212
|
-
-
|
213
|
-
-
|
238
|
+
|
239
|
+
- `isValidCsvString` - Validate CSV string format
|
240
|
+
- `isValidJsonString` - Validate JSON string format
|
241
|
+
- `jsonParse` - Safe JSON parsing
|
242
|
+
- `isValidXmlString` - Validate XML string format
|
214
243
|
|
215
244
|
### Template Functions
|
216
|
-
|
217
|
-
-
|
245
|
+
|
246
|
+
- `prompt` - Template tag for secure prompt formatting
|
247
|
+
- `promptTemplate` - Alias for prompt template tag
|
218
248
|
|
219
249
|
### Environment Detection
|
220
|
-
|
221
|
-
-
|
222
|
-
-
|
223
|
-
-
|
224
|
-
-
|
250
|
+
|
251
|
+
- `$getCurrentDate` - Get current date (side effect)
|
252
|
+
- `$isRunningInBrowser` - Check if running in browser
|
253
|
+
- `$isRunningInJest` - Check if running in Jest
|
254
|
+
- `$isRunningInNode` - Check if running in Node.js
|
255
|
+
- `$isRunningInWebWorker` - Check if running in Web Worker
|
225
256
|
|
226
257
|
### Text Counting and Analysis
|
227
|
-
|
228
|
-
-
|
229
|
-
-
|
230
|
-
-
|
231
|
-
-
|
232
|
-
-
|
233
|
-
-
|
234
|
-
-
|
235
|
-
-
|
236
|
-
-
|
258
|
+
|
259
|
+
- `CHARACTERS_PER_STANDARD_LINE` - Characters per standard line constant
|
260
|
+
- `LINES_PER_STANDARD_PAGE` - Lines per standard page constant
|
261
|
+
- `countCharacters` - Count characters in text
|
262
|
+
- `countLines` - Count lines in text
|
263
|
+
- `countPages` - Count pages in text
|
264
|
+
- `countParagraphs` - Count paragraphs in text
|
265
|
+
- `splitIntoSentences` - Split text into sentences
|
266
|
+
- `countSentences` - Count sentences in text
|
267
|
+
- `countWords` - Count words in text
|
268
|
+
- `CountUtils` - Utility object with all counting functions
|
237
269
|
|
238
270
|
### Text Normalization
|
239
|
-
|
240
|
-
-
|
241
|
-
-
|
242
|
-
-
|
243
|
-
-
|
244
|
-
-
|
245
|
-
-
|
246
|
-
-
|
247
|
-
-
|
248
|
-
-
|
249
|
-
-
|
250
|
-
-
|
251
|
-
-
|
252
|
-
-
|
253
|
-
-
|
254
|
-
-
|
255
|
-
-
|
256
|
-
-
|
257
|
-
-
|
258
|
-
-
|
259
|
-
-
|
260
|
-
-
|
261
|
-
-
|
262
|
-
-
|
263
|
-
-
|
271
|
+
|
272
|
+
- `capitalize` - Capitalize first letter
|
273
|
+
- `decapitalize` - Decapitalize first letter
|
274
|
+
- `DIACRITIC_VARIANTS_LETTERS` - Diacritic variants mapping
|
275
|
+
- `string_keyword` - Keyword string type (type)
|
276
|
+
- `Keywords` - Keywords type (type)
|
277
|
+
- `isValidKeyword` - Validate keyword format
|
278
|
+
- `nameToUriPart` - Convert name to URI part
|
279
|
+
- `nameToUriParts` - Convert name to URI parts
|
280
|
+
- `string_kebab_case` - Kebab case string type (type)
|
281
|
+
- `normalizeToKebabCase` - Convert to kebab-case
|
282
|
+
- `string_camelCase` - Camel case string type (type)
|
283
|
+
- `normalizeTo_camelCase` - Convert to camelCase
|
284
|
+
- `string_PascalCase` - Pascal case string type (type)
|
285
|
+
- `normalizeTo_PascalCase` - Convert to PascalCase
|
286
|
+
- `string_SCREAMING_CASE` - Screaming case string type (type)
|
287
|
+
- `normalizeTo_SCREAMING_CASE` - Convert to SCREAMING_CASE
|
288
|
+
- `normalizeTo_snake_case` - Convert to snake_case
|
289
|
+
- `normalizeWhitespaces` - Normalize whitespace characters
|
290
|
+
- `orderJson` - Order JSON object properties
|
291
|
+
- `parseKeywords` - Parse keywords from input
|
292
|
+
- `parseKeywordsFromString` - Parse keywords from string
|
293
|
+
- `removeDiacritics` - Remove diacritic marks
|
294
|
+
- `searchKeywords` - Search within keywords
|
295
|
+
- `suffixUrl` - Add suffix to URL
|
296
|
+
- `titleToName` - Convert title to name format
|
264
297
|
|
265
298
|
### Text Organization
|
266
|
-
|
299
|
+
|
300
|
+
- `spaceTrim` - Trim spaces while preserving structure
|
267
301
|
|
268
302
|
### Parameter Processing
|
269
|
-
|
270
|
-
-
|
271
|
-
-
|
272
|
-
-
|
303
|
+
|
304
|
+
- `extractParameterNames` - Extract parameter names from template
|
305
|
+
- `numberToString` - Convert number to string
|
306
|
+
- `templateParameters` - Replace template parameters
|
307
|
+
- `valueToString` - Convert value to string
|
273
308
|
|
274
309
|
### Parsing Utilities
|
275
|
-
|
310
|
+
|
311
|
+
- `parseNumber` - Parse number from string
|
276
312
|
|
277
313
|
### Text Processing
|
278
|
-
|
279
|
-
-
|
314
|
+
|
315
|
+
- `removeEmojis` - Remove emoji characters
|
316
|
+
- `removeQuotes` - Remove quote characters
|
280
317
|
|
281
318
|
### Serialization
|
282
|
-
|
283
|
-
-
|
284
|
-
-
|
285
|
-
-
|
286
|
-
-
|
287
|
-
-
|
288
|
-
-
|
319
|
+
|
320
|
+
- `$deepFreeze` - Deep freeze object (side effect)
|
321
|
+
- `checkSerializableAsJson` - Check if serializable as JSON
|
322
|
+
- `clonePipeline` - Clone pipeline object
|
323
|
+
- `deepClone` - Deep clone object
|
324
|
+
- `exportJson` - Export object as JSON
|
325
|
+
- `isSerializableAsJson` - Check if object is JSON serializable
|
326
|
+
- `jsonStringsToJsons` - Convert JSON strings to objects
|
289
327
|
|
290
328
|
### Set Operations
|
291
|
-
|
292
|
-
-
|
293
|
-
-
|
329
|
+
|
330
|
+
- `difference` - Set difference operation
|
331
|
+
- `intersection` - Set intersection operation
|
332
|
+
- `union` - Set union operation
|
294
333
|
|
295
334
|
### Code Processing
|
296
|
-
|
297
|
-
-
|
298
|
-
-
|
335
|
+
|
336
|
+
- `trimCodeBlock` - Trim code block formatting
|
337
|
+
- `trimEndOfCodeBlock` - Trim end of code block
|
338
|
+
- `unwrapResult` - Extract result from wrapped output
|
299
339
|
|
300
340
|
### Validation
|
301
|
-
|
302
|
-
-
|
303
|
-
-
|
304
|
-
-
|
305
|
-
-
|
306
|
-
-
|
307
|
-
-
|
308
|
-
-
|
309
|
-
-
|
310
|
-
-
|
311
|
-
-
|
312
|
-
|
313
|
-
|
341
|
+
|
342
|
+
- `isValidEmail` - Validate email address format
|
343
|
+
- `isRootPath` - Check if path is root path
|
344
|
+
- `isValidFilePath` - Validate file path format
|
345
|
+
- `isValidJavascriptName` - Validate JavaScript identifier
|
346
|
+
- `isValidPromptbookVersion` - Validate promptbook version
|
347
|
+
- `isValidSemanticVersion` - Validate semantic version
|
348
|
+
- `isHostnameOnPrivateNetwork` - Check if hostname is on private network
|
349
|
+
- `isUrlOnPrivateNetwork` - Check if URL is on private network
|
350
|
+
- `isValidPipelineUrl` - Validate pipeline URL format
|
351
|
+
- `isValidUrl` - Validate URL format
|
352
|
+
- `isValidUuid` - Validate UUID format
|
353
|
+
|
354
|
+
> ๐ก This package provides utility functions for promptbook applications. For the core functionality, see [@promptbook/core](#-packages) or install all packages with `npm i ptbk`
|
314
355
|
|
315
356
|
|
316
357
|
---
|
package/esm/index.es.js
CHANGED
@@ -16,7 +16,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
16
16
|
* @generated
|
17
17
|
* @see https://github.com/webgptorg/promptbook
|
18
18
|
*/
|
19
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.101.0-
|
19
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.101.0-4';
|
20
20
|
/**
|
21
21
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
22
22
|
* Note: [๐] Ignore a discrepancy between file name and entity name
|
@@ -119,4 +119,24 @@ export type ChatProps = {
|
|
119
119
|
* Keys should match ChatMessage.from values (e.g., 'USER', 'AGENT_{id}', etc.)
|
120
120
|
*/
|
121
121
|
readonly participants?: ReadonlyArray<ChatParticipant>;
|
122
|
+
/**
|
123
|
+
* Optional callback for handling user feedback on messages
|
124
|
+
* When provided, star rating buttons (1-5 stars) will be displayed next to each message
|
125
|
+
*
|
126
|
+
* @param feedback - Object containing the feedback data
|
127
|
+
* @param feedback.message - The message being rated
|
128
|
+
* @param feedback.rating - Star rating from 1 to 5
|
129
|
+
* @param feedback.textRating - Optional text feedback/note from user
|
130
|
+
* @param feedback.chatThread - Complete chat thread as string
|
131
|
+
* @param feedback.expectedAnswer - Optional expected answer provided by user
|
132
|
+
* @param feedback.url - Current page URL where feedback was given
|
133
|
+
*/
|
134
|
+
onFeedback?(feedback: {
|
135
|
+
message: ChatMessage;
|
136
|
+
rating: number;
|
137
|
+
textRating: string;
|
138
|
+
chatThread: string;
|
139
|
+
expectedAnswer: string | null;
|
140
|
+
url: string;
|
141
|
+
}): Promisable<void>;
|
122
142
|
};
|
@@ -15,7 +15,7 @@ export declare const BOOK_LANGUAGE_VERSION: string_semantic_version;
|
|
15
15
|
export declare const PROMPTBOOK_ENGINE_VERSION: string_promptbook_version;
|
16
16
|
/**
|
17
17
|
* Represents the version string of the Promptbook engine.
|
18
|
-
* It follows semantic versioning (e.g., `0.101.0-
|
18
|
+
* It follows semantic versioning (e.g., `0.101.0-3`).
|
19
19
|
*
|
20
20
|
* @generated
|
21
21
|
*/
|
package/package.json
CHANGED
package/umd/index.umd.js
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
* @generated
|
23
23
|
* @see https://github.com/webgptorg/promptbook
|
24
24
|
*/
|
25
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.101.0-
|
25
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.101.0-4';
|
26
26
|
/**
|
27
27
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
28
28
|
* Note: [๐] Ignore a discrepancy between file name and entity name
|