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