@promptbook/core 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.
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Removes duplicated instructions from a generated system message
3
+ *
4
+ * Commitments are applied one by one, so a commitment used multiple times in one book repeats its whole
5
+ * section, including the shared guidance preamble. For example two `WRITING RULES` commitments emit the
6
+ * `## Writing rules` heading and its guidance paragraph twice. This function merges all sections sharing
7
+ * one heading into the position of their first occurrence and keeps every identical block only once.
8
+ *
9
+ * @param systemMessage The assembled system message which may contain repeated sections and blocks
10
+ * @returns The system message where each section heading and each identical block appears exactly once
11
+ *
12
+ * @private internal utility of `createAgentModelRequirementsWithCommitments`
13
+ */
14
+ export declare function deduplicateSystemMessage(systemMessage: string): string;
@@ -0,0 +1,8 @@
1
+ import type { Command } from 'commander';
2
+ /**
3
+ * Registers the CLI commands on a fresh Commander program without parsing arguments or running an action.
4
+ * Tests can configure output and exit handling before registration, so subcommands inherit those settings.
5
+ *
6
+ * @private internal utility of `promptbookCli`
7
+ */
8
+ export declare function $initializePromptbookCliProgram(program: Command): void;
@@ -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.114.0-34`).
18
+ * It follows semantic versioning (e.g., `0.114.0-37`).
19
19
  *
20
20
  * @generated
21
21
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/core",
3
- "version": "0.114.0-35",
3
+ "version": "0.114.0-38",
4
4
  "description": "Promptbook: Create persistent AI agents that turn your company's scattered knowledge into action",
5
5
  "private": false,
6
6
  "sideEffects": false,
package/umd/index.umd.js CHANGED
@@ -28,7 +28,7 @@
28
28
  * @generated
29
29
  * @see https://github.com/webgptorg/promptbook
30
30
  */
31
- const PROMPTBOOK_ENGINE_VERSION = '0.114.0-35';
31
+ const PROMPTBOOK_ENGINE_VERSION = '0.114.0-38';
32
32
  /**
33
33
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
34
34
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -25251,6 +25251,245 @@
25251
25251
  return Object.keys(rest).length > 0 ? rest : undefined;
25252
25252
  }
25253
25253
 
25254
+ /**
25255
+ * Pattern matching a markdown section heading (`## Title`) that separates system-message sections
25256
+ *
25257
+ * @private internal constant of `deduplicateSystemMessage`
25258
+ */
25259
+ const SYSTEM_MESSAGE_SECTION_HEADING_PATTERN = /^##(?!#)\s*(.+?)\s*$/;
25260
+ /**
25261
+ * Pattern matching a fenced code block delimiter which suspends section and block splitting
25262
+ *
25263
+ * @private internal constant of `deduplicateSystemMessage`
25264
+ */
25265
+ const CODE_FENCE_PATTERN = /^\s*(?:```|~~~)/;
25266
+ /**
25267
+ * Pattern matching a markdown list item which keeps merged bullet lists visually compact
25268
+ *
25269
+ * @private internal constant of `deduplicateSystemMessage`
25270
+ */
25271
+ const LIST_ITEM_PATTERN = /^\s*(?:[-*+]|\d+[.)])\s/;
25272
+ /**
25273
+ * Removes duplicated instructions from a generated system message
25274
+ *
25275
+ * Commitments are applied one by one, so a commitment used multiple times in one book repeats its whole
25276
+ * section, including the shared guidance preamble. For example two `WRITING RULES` commitments emit the
25277
+ * `## Writing rules` heading and its guidance paragraph twice. This function merges all sections sharing
25278
+ * one heading into the position of their first occurrence and keeps every identical block only once.
25279
+ *
25280
+ * @param systemMessage The assembled system message which may contain repeated sections and blocks
25281
+ * @returns The system message where each section heading and each identical block appears exactly once
25282
+ *
25283
+ * @private internal utility of `createAgentModelRequirementsWithCommitments`
25284
+ */
25285
+ function deduplicateSystemMessage(systemMessage) {
25286
+ if (!systemMessage.trim()) {
25287
+ return systemMessage;
25288
+ }
25289
+ return splitSystemMessageIntoRegions(systemMessage)
25290
+ .reduce(mergeRegionSharingHeading, [])
25291
+ .map(removeDuplicateBlocksFromRegion)
25292
+ .map(renderRegion)
25293
+ .filter((renderedRegion) => renderedRegion !== '')
25294
+ .join('\n\n');
25295
+ }
25296
+ /**
25297
+ * Splits the system message into the intro region and one region per `## Title` section
25298
+ *
25299
+ * @param systemMessage The system message to split
25300
+ * @returns Regions in their original order, without empty ones
25301
+ *
25302
+ * @private internal utility of `deduplicateSystemMessage`
25303
+ */
25304
+ function splitSystemMessageIntoRegions(systemMessage) {
25305
+ const regions = [];
25306
+ let headingLine = null;
25307
+ let bodyLines = [];
25308
+ let isInsideCodeFence = false;
25309
+ for (const line of systemMessage.split(/\r?\n/)) {
25310
+ if (CODE_FENCE_PATTERN.test(line)) {
25311
+ isInsideCodeFence = !isInsideCodeFence;
25312
+ }
25313
+ if (isInsideCodeFence || !SYSTEM_MESSAGE_SECTION_HEADING_PATTERN.test(line)) {
25314
+ bodyLines.push(line);
25315
+ continue;
25316
+ }
25317
+ regions.push(createRegion(headingLine, bodyLines));
25318
+ headingLine = line;
25319
+ bodyLines = [];
25320
+ }
25321
+ regions.push(createRegion(headingLine, bodyLines));
25322
+ return regions.filter((region) => region.headingLine !== null || region.blocks.length > 0);
25323
+ }
25324
+ /**
25325
+ * Creates one region from its heading line and its raw body lines
25326
+ *
25327
+ * @param headingLine The original `## Title` line, or `null` for the intro before the first heading
25328
+ * @param bodyLines The raw lines following the heading line
25329
+ * @returns The region with its body already split into blocks
25330
+ *
25331
+ * @private internal utility of `splitSystemMessageIntoRegions`
25332
+ */
25333
+ function createRegion(headingLine, bodyLines) {
25334
+ return {
25335
+ titleKey: headingLine === null ? null : normalizeSectionTitle(headingLine),
25336
+ headingLine,
25337
+ isBodySeparatedByBlankLine: bodyLines[0] !== undefined && bodyLines[0].trim() === '',
25338
+ blocks: splitBodyIntoBlocks(bodyLines),
25339
+ };
25340
+ }
25341
+ /**
25342
+ * Normalizes a heading line into a key usable for matching sections which belong together
25343
+ *
25344
+ * @param headingLine The original `## Title` line
25345
+ * @returns Lowercased title without the markdown heading prefix
25346
+ *
25347
+ * @private internal utility of `createRegion`
25348
+ */
25349
+ function normalizeSectionTitle(headingLine) {
25350
+ var _a, _b;
25351
+ 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();
25352
+ }
25353
+ /**
25354
+ * Splits raw body lines into blocks separated by blank lines while keeping fenced code blocks intact
25355
+ *
25356
+ * @param bodyLines The raw lines of one region body
25357
+ * @returns Non-empty blocks in their original order
25358
+ *
25359
+ * @private internal utility of `createRegion`
25360
+ */
25361
+ function splitBodyIntoBlocks(bodyLines) {
25362
+ const blockTexts = [];
25363
+ let blockLines = [];
25364
+ let isInsideCodeFence = false;
25365
+ for (const line of bodyLines) {
25366
+ if (CODE_FENCE_PATTERN.test(line)) {
25367
+ isInsideCodeFence = !isInsideCodeFence;
25368
+ }
25369
+ if (!isInsideCodeFence && line.trim() === '') {
25370
+ blockTexts.push(blockLines.join('\n'));
25371
+ blockLines = [];
25372
+ continue;
25373
+ }
25374
+ blockLines.push(line);
25375
+ }
25376
+ blockTexts.push(blockLines.join('\n'));
25377
+ return blockTexts
25378
+ .filter((blockText) => blockText.trim() !== '')
25379
+ .map((blockText) => ({ text: blockText, isOpeningMergedSection: false }));
25380
+ }
25381
+ /**
25382
+ * Appends one region to the already merged regions, merging it into an earlier region with the same heading
25383
+ *
25384
+ * @param mergedRegions Regions merged so far, used as the reducer accumulator
25385
+ * @param region The next region in original order
25386
+ * @returns The accumulator with the region either appended or merged into its earlier twin
25387
+ *
25388
+ * @private internal utility of `deduplicateSystemMessage`
25389
+ */
25390
+ function mergeRegionSharingHeading(mergedRegions, region) {
25391
+ const existingRegionIndex = mergedRegions.findIndex((mergedRegion) => region.titleKey !== null && mergedRegion.titleKey === region.titleKey);
25392
+ if (existingRegionIndex === -1) {
25393
+ return [...mergedRegions, region];
25394
+ }
25395
+ return mergedRegions.map((mergedRegion, index) => index !== existingRegionIndex
25396
+ ? mergedRegion
25397
+ : {
25398
+ ...mergedRegion,
25399
+ blocks: [...mergedRegion.blocks, ...markFirstBlockAsOpeningMergedSection(region.blocks)],
25400
+ });
25401
+ }
25402
+ /**
25403
+ * Marks the first block of a merged section occurrence so the merge seam can be rendered as one list
25404
+ *
25405
+ * @param blocks The blocks of the section occurrence being merged into an earlier section
25406
+ * @returns The same blocks with the first one marked as opening a merged section
25407
+ *
25408
+ * @private internal utility of `mergeRegionSharingHeading`
25409
+ */
25410
+ function markFirstBlockAsOpeningMergedSection(blocks) {
25411
+ return blocks.map((block, index) => (index === 0 ? { ...block, isOpeningMergedSection: true } : block));
25412
+ }
25413
+ /**
25414
+ * Keeps only the first occurrence of every identical block inside one region
25415
+ *
25416
+ * @param region The region whose blocks may repeat
25417
+ * @returns The region without repeated blocks
25418
+ *
25419
+ * @private internal utility of `deduplicateSystemMessage`
25420
+ */
25421
+ function removeDuplicateBlocksFromRegion(region) {
25422
+ const alreadyRenderedTexts = new Set();
25423
+ const uniqueBlocks = [];
25424
+ let isMergeSeamPending = false;
25425
+ for (const block of region.blocks) {
25426
+ if (alreadyRenderedTexts.has(block.text)) {
25427
+ // Note: A removed block must still hand its merge seam over to the next block which survives
25428
+ isMergeSeamPending = isMergeSeamPending || block.isOpeningMergedSection;
25429
+ continue;
25430
+ }
25431
+ alreadyRenderedTexts.add(block.text);
25432
+ uniqueBlocks.push({
25433
+ text: block.text,
25434
+ isOpeningMergedSection: block.isOpeningMergedSection || isMergeSeamPending,
25435
+ });
25436
+ isMergeSeamPending = false;
25437
+ }
25438
+ return { ...region, blocks: uniqueBlocks };
25439
+ }
25440
+ /**
25441
+ * Renders one region back into its markdown representation
25442
+ *
25443
+ * @param region The region to render
25444
+ * @returns The heading line together with its joined blocks
25445
+ *
25446
+ * @private internal utility of `deduplicateSystemMessage`
25447
+ */
25448
+ function renderRegion(region) {
25449
+ const body = joinBlocks(region.blocks);
25450
+ if (region.headingLine === null) {
25451
+ return body;
25452
+ }
25453
+ if (body === '') {
25454
+ return region.headingLine;
25455
+ }
25456
+ return `${region.headingLine}${region.isBodySeparatedByBlankLine ? '\n\n' : '\n'}${body}`;
25457
+ }
25458
+ /**
25459
+ * Joins blocks of one region back together
25460
+ *
25461
+ * @param blocks The blocks to join in their original order
25462
+ * @returns The joined body of one region
25463
+ *
25464
+ * @private internal utility of `renderRegion`
25465
+ */
25466
+ function joinBlocks(blocks) {
25467
+ return blocks
25468
+ .map((block, index) => index === 0 ? block.text : `${createBlockSeparator(blocks[index - 1], block)}${block.text}`)
25469
+ .join('');
25470
+ }
25471
+ /**
25472
+ * Chooses the separator between two neighboring blocks
25473
+ *
25474
+ * Blocks are normally separated by a blank line, exactly as they were written. Only where two sections were
25475
+ * merged together their lists are joined into one compact list instead of two separate ones.
25476
+ *
25477
+ * @param previousBlock The block rendered before the separator
25478
+ * @param nextBlock The block rendered after the separator
25479
+ * @returns Either a single or a double newline
25480
+ *
25481
+ * @private internal utility of `joinBlocks`
25482
+ */
25483
+ function createBlockSeparator(previousBlock, nextBlock) {
25484
+ if (!nextBlock.isOpeningMergedSection) {
25485
+ return '\n\n';
25486
+ }
25487
+ const previousBlockLines = previousBlock.text.split('\n');
25488
+ const isListContinuing = LIST_ITEM_PATTERN.test(previousBlockLines[previousBlockLines.length - 1]) &&
25489
+ LIST_ITEM_PATTERN.test(nextBlock.text.split('\n')[0]);
25490
+ return isListContinuing ? '\n' : '\n\n';
25491
+ }
25492
+
25254
25493
  /**
25255
25494
  * Removes single-hash comment lines (`# Comment`) from a system message
25256
25495
  * This is used to clean up the final system message before sending it to the AI model
@@ -25324,14 +25563,14 @@
25324
25563
  * Performs the final system-message cleanup pass after all other augmentation steps are complete.
25325
25564
  *
25326
25565
  * @param requirements - Fully built requirements before final cleanup.
25327
- * @returns Requirements with comment lines removed from the final system message.
25566
+ * @returns Requirements with comment lines removed and repeated instructions deduplicated in the final system message.
25328
25567
  *
25329
25568
  * @private internal utility of `createAgentModelRequirementsWithCommitments`
25330
25569
  */
25331
25570
  function finalizeRequirements(requirements) {
25332
25571
  return {
25333
25572
  ...requirements,
25334
- systemMessage: removeCommentsFromSystemMessage(requirements.systemMessage),
25573
+ systemMessage: deduplicateSystemMessage(removeCommentsFromSystemMessage(requirements.systemMessage)),
25335
25574
  };
25336
25575
  }
25337
25576