@promptbook/browser 0.114.0-34 → 0.114.0-38

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/esm/index.es.js CHANGED
@@ -26,7 +26,7 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
26
26
  * @generated
27
27
  * @see https://github.com/webgptorg/promptbook
28
28
  */
29
- const PROMPTBOOK_ENGINE_VERSION = '0.114.0-34';
29
+ const PROMPTBOOK_ENGINE_VERSION = '0.114.0-38';
30
30
  /**
31
31
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
32
32
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -27061,6 +27061,245 @@ function stripInlineKnowledgeMetadata(metadata) {
27061
27061
  return Object.keys(rest).length > 0 ? rest : undefined;
27062
27062
  }
27063
27063
 
27064
+ /**
27065
+ * Pattern matching a markdown section heading (`## Title`) that separates system-message sections
27066
+ *
27067
+ * @private internal constant of `deduplicateSystemMessage`
27068
+ */
27069
+ const SYSTEM_MESSAGE_SECTION_HEADING_PATTERN = /^##(?!#)\s*(.+?)\s*$/;
27070
+ /**
27071
+ * Pattern matching a fenced code block delimiter which suspends section and block splitting
27072
+ *
27073
+ * @private internal constant of `deduplicateSystemMessage`
27074
+ */
27075
+ const CODE_FENCE_PATTERN = /^\s*(?:```|~~~)/;
27076
+ /**
27077
+ * Pattern matching a markdown list item which keeps merged bullet lists visually compact
27078
+ *
27079
+ * @private internal constant of `deduplicateSystemMessage`
27080
+ */
27081
+ const LIST_ITEM_PATTERN = /^\s*(?:[-*+]|\d+[.)])\s/;
27082
+ /**
27083
+ * Removes duplicated instructions from a generated system message
27084
+ *
27085
+ * Commitments are applied one by one, so a commitment used multiple times in one book repeats its whole
27086
+ * section, including the shared guidance preamble. For example two `WRITING RULES` commitments emit the
27087
+ * `## Writing rules` heading and its guidance paragraph twice. This function merges all sections sharing
27088
+ * one heading into the position of their first occurrence and keeps every identical block only once.
27089
+ *
27090
+ * @param systemMessage The assembled system message which may contain repeated sections and blocks
27091
+ * @returns The system message where each section heading and each identical block appears exactly once
27092
+ *
27093
+ * @private internal utility of `createAgentModelRequirementsWithCommitments`
27094
+ */
27095
+ function deduplicateSystemMessage(systemMessage) {
27096
+ if (!systemMessage.trim()) {
27097
+ return systemMessage;
27098
+ }
27099
+ return splitSystemMessageIntoRegions(systemMessage)
27100
+ .reduce(mergeRegionSharingHeading, [])
27101
+ .map(removeDuplicateBlocksFromRegion)
27102
+ .map(renderRegion)
27103
+ .filter((renderedRegion) => renderedRegion !== '')
27104
+ .join('\n\n');
27105
+ }
27106
+ /**
27107
+ * Splits the system message into the intro region and one region per `## Title` section
27108
+ *
27109
+ * @param systemMessage The system message to split
27110
+ * @returns Regions in their original order, without empty ones
27111
+ *
27112
+ * @private internal utility of `deduplicateSystemMessage`
27113
+ */
27114
+ function splitSystemMessageIntoRegions(systemMessage) {
27115
+ const regions = [];
27116
+ let headingLine = null;
27117
+ let bodyLines = [];
27118
+ let isInsideCodeFence = false;
27119
+ for (const line of systemMessage.split(/\r?\n/)) {
27120
+ if (CODE_FENCE_PATTERN.test(line)) {
27121
+ isInsideCodeFence = !isInsideCodeFence;
27122
+ }
27123
+ if (isInsideCodeFence || !SYSTEM_MESSAGE_SECTION_HEADING_PATTERN.test(line)) {
27124
+ bodyLines.push(line);
27125
+ continue;
27126
+ }
27127
+ regions.push(createRegion(headingLine, bodyLines));
27128
+ headingLine = line;
27129
+ bodyLines = [];
27130
+ }
27131
+ regions.push(createRegion(headingLine, bodyLines));
27132
+ return regions.filter((region) => region.headingLine !== null || region.blocks.length > 0);
27133
+ }
27134
+ /**
27135
+ * Creates one region from its heading line and its raw body lines
27136
+ *
27137
+ * @param headingLine The original `## Title` line, or `null` for the intro before the first heading
27138
+ * @param bodyLines The raw lines following the heading line
27139
+ * @returns The region with its body already split into blocks
27140
+ *
27141
+ * @private internal utility of `splitSystemMessageIntoRegions`
27142
+ */
27143
+ function createRegion(headingLine, bodyLines) {
27144
+ return {
27145
+ titleKey: headingLine === null ? null : normalizeSectionTitle(headingLine),
27146
+ headingLine,
27147
+ isBodySeparatedByBlankLine: bodyLines[0] !== undefined && bodyLines[0].trim() === '',
27148
+ blocks: splitBodyIntoBlocks(bodyLines),
27149
+ };
27150
+ }
27151
+ /**
27152
+ * Normalizes a heading line into a key usable for matching sections which belong together
27153
+ *
27154
+ * @param headingLine The original `## Title` line
27155
+ * @returns Lowercased title without the markdown heading prefix
27156
+ *
27157
+ * @private internal utility of `createRegion`
27158
+ */
27159
+ function normalizeSectionTitle(headingLine) {
27160
+ var _a, _b;
27161
+ return ((_b = (_a = SYSTEM_MESSAGE_SECTION_HEADING_PATTERN.exec(headingLine)) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : headingLine).toLowerCase();
27162
+ }
27163
+ /**
27164
+ * Splits raw body lines into blocks separated by blank lines while keeping fenced code blocks intact
27165
+ *
27166
+ * @param bodyLines The raw lines of one region body
27167
+ * @returns Non-empty blocks in their original order
27168
+ *
27169
+ * @private internal utility of `createRegion`
27170
+ */
27171
+ function splitBodyIntoBlocks(bodyLines) {
27172
+ const blockTexts = [];
27173
+ let blockLines = [];
27174
+ let isInsideCodeFence = false;
27175
+ for (const line of bodyLines) {
27176
+ if (CODE_FENCE_PATTERN.test(line)) {
27177
+ isInsideCodeFence = !isInsideCodeFence;
27178
+ }
27179
+ if (!isInsideCodeFence && line.trim() === '') {
27180
+ blockTexts.push(blockLines.join('\n'));
27181
+ blockLines = [];
27182
+ continue;
27183
+ }
27184
+ blockLines.push(line);
27185
+ }
27186
+ blockTexts.push(blockLines.join('\n'));
27187
+ return blockTexts
27188
+ .filter((blockText) => blockText.trim() !== '')
27189
+ .map((blockText) => ({ text: blockText, isOpeningMergedSection: false }));
27190
+ }
27191
+ /**
27192
+ * Appends one region to the already merged regions, merging it into an earlier region with the same heading
27193
+ *
27194
+ * @param mergedRegions Regions merged so far, used as the reducer accumulator
27195
+ * @param region The next region in original order
27196
+ * @returns The accumulator with the region either appended or merged into its earlier twin
27197
+ *
27198
+ * @private internal utility of `deduplicateSystemMessage`
27199
+ */
27200
+ function mergeRegionSharingHeading(mergedRegions, region) {
27201
+ const existingRegionIndex = mergedRegions.findIndex((mergedRegion) => region.titleKey !== null && mergedRegion.titleKey === region.titleKey);
27202
+ if (existingRegionIndex === -1) {
27203
+ return [...mergedRegions, region];
27204
+ }
27205
+ return mergedRegions.map((mergedRegion, index) => index !== existingRegionIndex
27206
+ ? mergedRegion
27207
+ : {
27208
+ ...mergedRegion,
27209
+ blocks: [...mergedRegion.blocks, ...markFirstBlockAsOpeningMergedSection(region.blocks)],
27210
+ });
27211
+ }
27212
+ /**
27213
+ * Marks the first block of a merged section occurrence so the merge seam can be rendered as one list
27214
+ *
27215
+ * @param blocks The blocks of the section occurrence being merged into an earlier section
27216
+ * @returns The same blocks with the first one marked as opening a merged section
27217
+ *
27218
+ * @private internal utility of `mergeRegionSharingHeading`
27219
+ */
27220
+ function markFirstBlockAsOpeningMergedSection(blocks) {
27221
+ return blocks.map((block, index) => (index === 0 ? { ...block, isOpeningMergedSection: true } : block));
27222
+ }
27223
+ /**
27224
+ * Keeps only the first occurrence of every identical block inside one region
27225
+ *
27226
+ * @param region The region whose blocks may repeat
27227
+ * @returns The region without repeated blocks
27228
+ *
27229
+ * @private internal utility of `deduplicateSystemMessage`
27230
+ */
27231
+ function removeDuplicateBlocksFromRegion(region) {
27232
+ const alreadyRenderedTexts = new Set();
27233
+ const uniqueBlocks = [];
27234
+ let isMergeSeamPending = false;
27235
+ for (const block of region.blocks) {
27236
+ if (alreadyRenderedTexts.has(block.text)) {
27237
+ // Note: A removed block must still hand its merge seam over to the next block which survives
27238
+ isMergeSeamPending = isMergeSeamPending || block.isOpeningMergedSection;
27239
+ continue;
27240
+ }
27241
+ alreadyRenderedTexts.add(block.text);
27242
+ uniqueBlocks.push({
27243
+ text: block.text,
27244
+ isOpeningMergedSection: block.isOpeningMergedSection || isMergeSeamPending,
27245
+ });
27246
+ isMergeSeamPending = false;
27247
+ }
27248
+ return { ...region, blocks: uniqueBlocks };
27249
+ }
27250
+ /**
27251
+ * Renders one region back into its markdown representation
27252
+ *
27253
+ * @param region The region to render
27254
+ * @returns The heading line together with its joined blocks
27255
+ *
27256
+ * @private internal utility of `deduplicateSystemMessage`
27257
+ */
27258
+ function renderRegion(region) {
27259
+ const body = joinBlocks(region.blocks);
27260
+ if (region.headingLine === null) {
27261
+ return body;
27262
+ }
27263
+ if (body === '') {
27264
+ return region.headingLine;
27265
+ }
27266
+ return `${region.headingLine}${region.isBodySeparatedByBlankLine ? '\n\n' : '\n'}${body}`;
27267
+ }
27268
+ /**
27269
+ * Joins blocks of one region back together
27270
+ *
27271
+ * @param blocks The blocks to join in their original order
27272
+ * @returns The joined body of one region
27273
+ *
27274
+ * @private internal utility of `renderRegion`
27275
+ */
27276
+ function joinBlocks(blocks) {
27277
+ return blocks
27278
+ .map((block, index) => index === 0 ? block.text : `${createBlockSeparator(blocks[index - 1], block)}${block.text}`)
27279
+ .join('');
27280
+ }
27281
+ /**
27282
+ * Chooses the separator between two neighboring blocks
27283
+ *
27284
+ * Blocks are normally separated by a blank line, exactly as they were written. Only where two sections were
27285
+ * merged together their lists are joined into one compact list instead of two separate ones.
27286
+ *
27287
+ * @param previousBlock The block rendered before the separator
27288
+ * @param nextBlock The block rendered after the separator
27289
+ * @returns Either a single or a double newline
27290
+ *
27291
+ * @private internal utility of `joinBlocks`
27292
+ */
27293
+ function createBlockSeparator(previousBlock, nextBlock) {
27294
+ if (!nextBlock.isOpeningMergedSection) {
27295
+ return '\n\n';
27296
+ }
27297
+ const previousBlockLines = previousBlock.text.split('\n');
27298
+ const isListContinuing = LIST_ITEM_PATTERN.test(previousBlockLines[previousBlockLines.length - 1]) &&
27299
+ LIST_ITEM_PATTERN.test(nextBlock.text.split('\n')[0]);
27300
+ return isListContinuing ? '\n' : '\n\n';
27301
+ }
27302
+
27064
27303
  /**
27065
27304
  * Removes single-hash comment lines (`# Comment`) from a system message
27066
27305
  * This is used to clean up the final system message before sending it to the AI model
@@ -27134,14 +27373,14 @@ function createInitialAgentModelRequirements(agentName, modelName) {
27134
27373
  * Performs the final system-message cleanup pass after all other augmentation steps are complete.
27135
27374
  *
27136
27375
  * @param requirements - Fully built requirements before final cleanup.
27137
- * @returns Requirements with comment lines removed from the final system message.
27376
+ * @returns Requirements with comment lines removed and repeated instructions deduplicated in the final system message.
27138
27377
  *
27139
27378
  * @private internal utility of `createAgentModelRequirementsWithCommitments`
27140
27379
  */
27141
27380
  function finalizeRequirements(requirements) {
27142
27381
  return {
27143
27382
  ...requirements,
27144
- systemMessage: removeCommentsFromSystemMessage(requirements.systemMessage),
27383
+ systemMessage: deduplicateSystemMessage(removeCommentsFromSystemMessage(requirements.systemMessage)),
27145
27384
  };
27146
27385
  }
27147
27386