@promptbook/components 0.114.0-35 → 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
@@ -39,7 +39,7 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
39
39
  * @generated
40
40
  * @see https://github.com/webgptorg/promptbook
41
41
  */
42
- const PROMPTBOOK_ENGINE_VERSION = '0.114.0-35';
42
+ const PROMPTBOOK_ENGINE_VERSION = '0.114.0-38';
43
43
  /**
44
44
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
45
45
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -50490,6 +50490,245 @@ function stripInlineKnowledgeMetadata(metadata) {
50490
50490
  return Object.keys(rest).length > 0 ? rest : undefined;
50491
50491
  }
50492
50492
 
50493
+ /**
50494
+ * Pattern matching a markdown section heading (`## Title`) that separates system-message sections
50495
+ *
50496
+ * @private internal constant of `deduplicateSystemMessage`
50497
+ */
50498
+ const SYSTEM_MESSAGE_SECTION_HEADING_PATTERN = /^##(?!#)\s*(.+?)\s*$/;
50499
+ /**
50500
+ * Pattern matching a fenced code block delimiter which suspends section and block splitting
50501
+ *
50502
+ * @private internal constant of `deduplicateSystemMessage`
50503
+ */
50504
+ const CODE_FENCE_PATTERN = /^\s*(?:```|~~~)/;
50505
+ /**
50506
+ * Pattern matching a markdown list item which keeps merged bullet lists visually compact
50507
+ *
50508
+ * @private internal constant of `deduplicateSystemMessage`
50509
+ */
50510
+ const LIST_ITEM_PATTERN = /^\s*(?:[-*+]|\d+[.)])\s/;
50511
+ /**
50512
+ * Removes duplicated instructions from a generated system message
50513
+ *
50514
+ * Commitments are applied one by one, so a commitment used multiple times in one book repeats its whole
50515
+ * section, including the shared guidance preamble. For example two `WRITING RULES` commitments emit the
50516
+ * `## Writing rules` heading and its guidance paragraph twice. This function merges all sections sharing
50517
+ * one heading into the position of their first occurrence and keeps every identical block only once.
50518
+ *
50519
+ * @param systemMessage The assembled system message which may contain repeated sections and blocks
50520
+ * @returns The system message where each section heading and each identical block appears exactly once
50521
+ *
50522
+ * @private internal utility of `createAgentModelRequirementsWithCommitments`
50523
+ */
50524
+ function deduplicateSystemMessage(systemMessage) {
50525
+ if (!systemMessage.trim()) {
50526
+ return systemMessage;
50527
+ }
50528
+ return splitSystemMessageIntoRegions(systemMessage)
50529
+ .reduce(mergeRegionSharingHeading, [])
50530
+ .map(removeDuplicateBlocksFromRegion)
50531
+ .map(renderRegion)
50532
+ .filter((renderedRegion) => renderedRegion !== '')
50533
+ .join('\n\n');
50534
+ }
50535
+ /**
50536
+ * Splits the system message into the intro region and one region per `## Title` section
50537
+ *
50538
+ * @param systemMessage The system message to split
50539
+ * @returns Regions in their original order, without empty ones
50540
+ *
50541
+ * @private internal utility of `deduplicateSystemMessage`
50542
+ */
50543
+ function splitSystemMessageIntoRegions(systemMessage) {
50544
+ const regions = [];
50545
+ let headingLine = null;
50546
+ let bodyLines = [];
50547
+ let isInsideCodeFence = false;
50548
+ for (const line of systemMessage.split(/\r?\n/)) {
50549
+ if (CODE_FENCE_PATTERN.test(line)) {
50550
+ isInsideCodeFence = !isInsideCodeFence;
50551
+ }
50552
+ if (isInsideCodeFence || !SYSTEM_MESSAGE_SECTION_HEADING_PATTERN.test(line)) {
50553
+ bodyLines.push(line);
50554
+ continue;
50555
+ }
50556
+ regions.push(createRegion(headingLine, bodyLines));
50557
+ headingLine = line;
50558
+ bodyLines = [];
50559
+ }
50560
+ regions.push(createRegion(headingLine, bodyLines));
50561
+ return regions.filter((region) => region.headingLine !== null || region.blocks.length > 0);
50562
+ }
50563
+ /**
50564
+ * Creates one region from its heading line and its raw body lines
50565
+ *
50566
+ * @param headingLine The original `## Title` line, or `null` for the intro before the first heading
50567
+ * @param bodyLines The raw lines following the heading line
50568
+ * @returns The region with its body already split into blocks
50569
+ *
50570
+ * @private internal utility of `splitSystemMessageIntoRegions`
50571
+ */
50572
+ function createRegion(headingLine, bodyLines) {
50573
+ return {
50574
+ titleKey: headingLine === null ? null : normalizeSectionTitle(headingLine),
50575
+ headingLine,
50576
+ isBodySeparatedByBlankLine: bodyLines[0] !== undefined && bodyLines[0].trim() === '',
50577
+ blocks: splitBodyIntoBlocks(bodyLines),
50578
+ };
50579
+ }
50580
+ /**
50581
+ * Normalizes a heading line into a key usable for matching sections which belong together
50582
+ *
50583
+ * @param headingLine The original `## Title` line
50584
+ * @returns Lowercased title without the markdown heading prefix
50585
+ *
50586
+ * @private internal utility of `createRegion`
50587
+ */
50588
+ function normalizeSectionTitle(headingLine) {
50589
+ var _a, _b;
50590
+ 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();
50591
+ }
50592
+ /**
50593
+ * Splits raw body lines into blocks separated by blank lines while keeping fenced code blocks intact
50594
+ *
50595
+ * @param bodyLines The raw lines of one region body
50596
+ * @returns Non-empty blocks in their original order
50597
+ *
50598
+ * @private internal utility of `createRegion`
50599
+ */
50600
+ function splitBodyIntoBlocks(bodyLines) {
50601
+ const blockTexts = [];
50602
+ let blockLines = [];
50603
+ let isInsideCodeFence = false;
50604
+ for (const line of bodyLines) {
50605
+ if (CODE_FENCE_PATTERN.test(line)) {
50606
+ isInsideCodeFence = !isInsideCodeFence;
50607
+ }
50608
+ if (!isInsideCodeFence && line.trim() === '') {
50609
+ blockTexts.push(blockLines.join('\n'));
50610
+ blockLines = [];
50611
+ continue;
50612
+ }
50613
+ blockLines.push(line);
50614
+ }
50615
+ blockTexts.push(blockLines.join('\n'));
50616
+ return blockTexts
50617
+ .filter((blockText) => blockText.trim() !== '')
50618
+ .map((blockText) => ({ text: blockText, isOpeningMergedSection: false }));
50619
+ }
50620
+ /**
50621
+ * Appends one region to the already merged regions, merging it into an earlier region with the same heading
50622
+ *
50623
+ * @param mergedRegions Regions merged so far, used as the reducer accumulator
50624
+ * @param region The next region in original order
50625
+ * @returns The accumulator with the region either appended or merged into its earlier twin
50626
+ *
50627
+ * @private internal utility of `deduplicateSystemMessage`
50628
+ */
50629
+ function mergeRegionSharingHeading(mergedRegions, region) {
50630
+ const existingRegionIndex = mergedRegions.findIndex((mergedRegion) => region.titleKey !== null && mergedRegion.titleKey === region.titleKey);
50631
+ if (existingRegionIndex === -1) {
50632
+ return [...mergedRegions, region];
50633
+ }
50634
+ return mergedRegions.map((mergedRegion, index) => index !== existingRegionIndex
50635
+ ? mergedRegion
50636
+ : {
50637
+ ...mergedRegion,
50638
+ blocks: [...mergedRegion.blocks, ...markFirstBlockAsOpeningMergedSection(region.blocks)],
50639
+ });
50640
+ }
50641
+ /**
50642
+ * Marks the first block of a merged section occurrence so the merge seam can be rendered as one list
50643
+ *
50644
+ * @param blocks The blocks of the section occurrence being merged into an earlier section
50645
+ * @returns The same blocks with the first one marked as opening a merged section
50646
+ *
50647
+ * @private internal utility of `mergeRegionSharingHeading`
50648
+ */
50649
+ function markFirstBlockAsOpeningMergedSection(blocks) {
50650
+ return blocks.map((block, index) => (index === 0 ? { ...block, isOpeningMergedSection: true } : block));
50651
+ }
50652
+ /**
50653
+ * Keeps only the first occurrence of every identical block inside one region
50654
+ *
50655
+ * @param region The region whose blocks may repeat
50656
+ * @returns The region without repeated blocks
50657
+ *
50658
+ * @private internal utility of `deduplicateSystemMessage`
50659
+ */
50660
+ function removeDuplicateBlocksFromRegion(region) {
50661
+ const alreadyRenderedTexts = new Set();
50662
+ const uniqueBlocks = [];
50663
+ let isMergeSeamPending = false;
50664
+ for (const block of region.blocks) {
50665
+ if (alreadyRenderedTexts.has(block.text)) {
50666
+ // Note: A removed block must still hand its merge seam over to the next block which survives
50667
+ isMergeSeamPending = isMergeSeamPending || block.isOpeningMergedSection;
50668
+ continue;
50669
+ }
50670
+ alreadyRenderedTexts.add(block.text);
50671
+ uniqueBlocks.push({
50672
+ text: block.text,
50673
+ isOpeningMergedSection: block.isOpeningMergedSection || isMergeSeamPending,
50674
+ });
50675
+ isMergeSeamPending = false;
50676
+ }
50677
+ return { ...region, blocks: uniqueBlocks };
50678
+ }
50679
+ /**
50680
+ * Renders one region back into its markdown representation
50681
+ *
50682
+ * @param region The region to render
50683
+ * @returns The heading line together with its joined blocks
50684
+ *
50685
+ * @private internal utility of `deduplicateSystemMessage`
50686
+ */
50687
+ function renderRegion(region) {
50688
+ const body = joinBlocks(region.blocks);
50689
+ if (region.headingLine === null) {
50690
+ return body;
50691
+ }
50692
+ if (body === '') {
50693
+ return region.headingLine;
50694
+ }
50695
+ return `${region.headingLine}${region.isBodySeparatedByBlankLine ? '\n\n' : '\n'}${body}`;
50696
+ }
50697
+ /**
50698
+ * Joins blocks of one region back together
50699
+ *
50700
+ * @param blocks The blocks to join in their original order
50701
+ * @returns The joined body of one region
50702
+ *
50703
+ * @private internal utility of `renderRegion`
50704
+ */
50705
+ function joinBlocks(blocks) {
50706
+ return blocks
50707
+ .map((block, index) => index === 0 ? block.text : `${createBlockSeparator(blocks[index - 1], block)}${block.text}`)
50708
+ .join('');
50709
+ }
50710
+ /**
50711
+ * Chooses the separator between two neighboring blocks
50712
+ *
50713
+ * Blocks are normally separated by a blank line, exactly as they were written. Only where two sections were
50714
+ * merged together their lists are joined into one compact list instead of two separate ones.
50715
+ *
50716
+ * @param previousBlock The block rendered before the separator
50717
+ * @param nextBlock The block rendered after the separator
50718
+ * @returns Either a single or a double newline
50719
+ *
50720
+ * @private internal utility of `joinBlocks`
50721
+ */
50722
+ function createBlockSeparator(previousBlock, nextBlock) {
50723
+ if (!nextBlock.isOpeningMergedSection) {
50724
+ return '\n\n';
50725
+ }
50726
+ const previousBlockLines = previousBlock.text.split('\n');
50727
+ const isListContinuing = LIST_ITEM_PATTERN.test(previousBlockLines[previousBlockLines.length - 1]) &&
50728
+ LIST_ITEM_PATTERN.test(nextBlock.text.split('\n')[0]);
50729
+ return isListContinuing ? '\n' : '\n\n';
50730
+ }
50731
+
50493
50732
  /**
50494
50733
  * Removes single-hash comment lines (`# Comment`) from a system message
50495
50734
  * This is used to clean up the final system message before sending it to the AI model
@@ -50563,14 +50802,14 @@ function createInitialAgentModelRequirements(agentName, modelName) {
50563
50802
  * Performs the final system-message cleanup pass after all other augmentation steps are complete.
50564
50803
  *
50565
50804
  * @param requirements - Fully built requirements before final cleanup.
50566
- * @returns Requirements with comment lines removed from the final system message.
50805
+ * @returns Requirements with comment lines removed and repeated instructions deduplicated in the final system message.
50567
50806
  *
50568
50807
  * @private internal utility of `createAgentModelRequirementsWithCommitments`
50569
50808
  */
50570
50809
  function finalizeRequirements(requirements) {
50571
50810
  return {
50572
50811
  ...requirements,
50573
- systemMessage: removeCommentsFromSystemMessage(requirements.systemMessage),
50812
+ systemMessage: deduplicateSystemMessage(removeCommentsFromSystemMessage(requirements.systemMessage)),
50574
50813
  };
50575
50814
  }
50576
50815