@promptbook/remote-server 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
@@ -34,7 +34,7 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
34
34
  * @generated
35
35
  * @see https://github.com/webgptorg/promptbook
36
36
  */
37
- const PROMPTBOOK_ENGINE_VERSION = '0.114.0-35';
37
+ const PROMPTBOOK_ENGINE_VERSION = '0.114.0-38';
38
38
  /**
39
39
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
40
40
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -31113,6 +31113,245 @@ function stripInlineKnowledgeMetadata(metadata) {
31113
31113
  return Object.keys(rest).length > 0 ? rest : undefined;
31114
31114
  }
31115
31115
 
31116
+ /**
31117
+ * Pattern matching a markdown section heading (`## Title`) that separates system-message sections
31118
+ *
31119
+ * @private internal constant of `deduplicateSystemMessage`
31120
+ */
31121
+ const SYSTEM_MESSAGE_SECTION_HEADING_PATTERN = /^##(?!#)\s*(.+?)\s*$/;
31122
+ /**
31123
+ * Pattern matching a fenced code block delimiter which suspends section and block splitting
31124
+ *
31125
+ * @private internal constant of `deduplicateSystemMessage`
31126
+ */
31127
+ const CODE_FENCE_PATTERN = /^\s*(?:```|~~~)/;
31128
+ /**
31129
+ * Pattern matching a markdown list item which keeps merged bullet lists visually compact
31130
+ *
31131
+ * @private internal constant of `deduplicateSystemMessage`
31132
+ */
31133
+ const LIST_ITEM_PATTERN = /^\s*(?:[-*+]|\d+[.)])\s/;
31134
+ /**
31135
+ * Removes duplicated instructions from a generated system message
31136
+ *
31137
+ * Commitments are applied one by one, so a commitment used multiple times in one book repeats its whole
31138
+ * section, including the shared guidance preamble. For example two `WRITING RULES` commitments emit the
31139
+ * `## Writing rules` heading and its guidance paragraph twice. This function merges all sections sharing
31140
+ * one heading into the position of their first occurrence and keeps every identical block only once.
31141
+ *
31142
+ * @param systemMessage The assembled system message which may contain repeated sections and blocks
31143
+ * @returns The system message where each section heading and each identical block appears exactly once
31144
+ *
31145
+ * @private internal utility of `createAgentModelRequirementsWithCommitments`
31146
+ */
31147
+ function deduplicateSystemMessage(systemMessage) {
31148
+ if (!systemMessage.trim()) {
31149
+ return systemMessage;
31150
+ }
31151
+ return splitSystemMessageIntoRegions(systemMessage)
31152
+ .reduce(mergeRegionSharingHeading, [])
31153
+ .map(removeDuplicateBlocksFromRegion)
31154
+ .map(renderRegion)
31155
+ .filter((renderedRegion) => renderedRegion !== '')
31156
+ .join('\n\n');
31157
+ }
31158
+ /**
31159
+ * Splits the system message into the intro region and one region per `## Title` section
31160
+ *
31161
+ * @param systemMessage The system message to split
31162
+ * @returns Regions in their original order, without empty ones
31163
+ *
31164
+ * @private internal utility of `deduplicateSystemMessage`
31165
+ */
31166
+ function splitSystemMessageIntoRegions(systemMessage) {
31167
+ const regions = [];
31168
+ let headingLine = null;
31169
+ let bodyLines = [];
31170
+ let isInsideCodeFence = false;
31171
+ for (const line of systemMessage.split(/\r?\n/)) {
31172
+ if (CODE_FENCE_PATTERN.test(line)) {
31173
+ isInsideCodeFence = !isInsideCodeFence;
31174
+ }
31175
+ if (isInsideCodeFence || !SYSTEM_MESSAGE_SECTION_HEADING_PATTERN.test(line)) {
31176
+ bodyLines.push(line);
31177
+ continue;
31178
+ }
31179
+ regions.push(createRegion(headingLine, bodyLines));
31180
+ headingLine = line;
31181
+ bodyLines = [];
31182
+ }
31183
+ regions.push(createRegion(headingLine, bodyLines));
31184
+ return regions.filter((region) => region.headingLine !== null || region.blocks.length > 0);
31185
+ }
31186
+ /**
31187
+ * Creates one region from its heading line and its raw body lines
31188
+ *
31189
+ * @param headingLine The original `## Title` line, or `null` for the intro before the first heading
31190
+ * @param bodyLines The raw lines following the heading line
31191
+ * @returns The region with its body already split into blocks
31192
+ *
31193
+ * @private internal utility of `splitSystemMessageIntoRegions`
31194
+ */
31195
+ function createRegion(headingLine, bodyLines) {
31196
+ return {
31197
+ titleKey: headingLine === null ? null : normalizeSectionTitle(headingLine),
31198
+ headingLine,
31199
+ isBodySeparatedByBlankLine: bodyLines[0] !== undefined && bodyLines[0].trim() === '',
31200
+ blocks: splitBodyIntoBlocks(bodyLines),
31201
+ };
31202
+ }
31203
+ /**
31204
+ * Normalizes a heading line into a key usable for matching sections which belong together
31205
+ *
31206
+ * @param headingLine The original `## Title` line
31207
+ * @returns Lowercased title without the markdown heading prefix
31208
+ *
31209
+ * @private internal utility of `createRegion`
31210
+ */
31211
+ function normalizeSectionTitle(headingLine) {
31212
+ var _a, _b;
31213
+ 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();
31214
+ }
31215
+ /**
31216
+ * Splits raw body lines into blocks separated by blank lines while keeping fenced code blocks intact
31217
+ *
31218
+ * @param bodyLines The raw lines of one region body
31219
+ * @returns Non-empty blocks in their original order
31220
+ *
31221
+ * @private internal utility of `createRegion`
31222
+ */
31223
+ function splitBodyIntoBlocks(bodyLines) {
31224
+ const blockTexts = [];
31225
+ let blockLines = [];
31226
+ let isInsideCodeFence = false;
31227
+ for (const line of bodyLines) {
31228
+ if (CODE_FENCE_PATTERN.test(line)) {
31229
+ isInsideCodeFence = !isInsideCodeFence;
31230
+ }
31231
+ if (!isInsideCodeFence && line.trim() === '') {
31232
+ blockTexts.push(blockLines.join('\n'));
31233
+ blockLines = [];
31234
+ continue;
31235
+ }
31236
+ blockLines.push(line);
31237
+ }
31238
+ blockTexts.push(blockLines.join('\n'));
31239
+ return blockTexts
31240
+ .filter((blockText) => blockText.trim() !== '')
31241
+ .map((blockText) => ({ text: blockText, isOpeningMergedSection: false }));
31242
+ }
31243
+ /**
31244
+ * Appends one region to the already merged regions, merging it into an earlier region with the same heading
31245
+ *
31246
+ * @param mergedRegions Regions merged so far, used as the reducer accumulator
31247
+ * @param region The next region in original order
31248
+ * @returns The accumulator with the region either appended or merged into its earlier twin
31249
+ *
31250
+ * @private internal utility of `deduplicateSystemMessage`
31251
+ */
31252
+ function mergeRegionSharingHeading(mergedRegions, region) {
31253
+ const existingRegionIndex = mergedRegions.findIndex((mergedRegion) => region.titleKey !== null && mergedRegion.titleKey === region.titleKey);
31254
+ if (existingRegionIndex === -1) {
31255
+ return [...mergedRegions, region];
31256
+ }
31257
+ return mergedRegions.map((mergedRegion, index) => index !== existingRegionIndex
31258
+ ? mergedRegion
31259
+ : {
31260
+ ...mergedRegion,
31261
+ blocks: [...mergedRegion.blocks, ...markFirstBlockAsOpeningMergedSection(region.blocks)],
31262
+ });
31263
+ }
31264
+ /**
31265
+ * Marks the first block of a merged section occurrence so the merge seam can be rendered as one list
31266
+ *
31267
+ * @param blocks The blocks of the section occurrence being merged into an earlier section
31268
+ * @returns The same blocks with the first one marked as opening a merged section
31269
+ *
31270
+ * @private internal utility of `mergeRegionSharingHeading`
31271
+ */
31272
+ function markFirstBlockAsOpeningMergedSection(blocks) {
31273
+ return blocks.map((block, index) => (index === 0 ? { ...block, isOpeningMergedSection: true } : block));
31274
+ }
31275
+ /**
31276
+ * Keeps only the first occurrence of every identical block inside one region
31277
+ *
31278
+ * @param region The region whose blocks may repeat
31279
+ * @returns The region without repeated blocks
31280
+ *
31281
+ * @private internal utility of `deduplicateSystemMessage`
31282
+ */
31283
+ function removeDuplicateBlocksFromRegion(region) {
31284
+ const alreadyRenderedTexts = new Set();
31285
+ const uniqueBlocks = [];
31286
+ let isMergeSeamPending = false;
31287
+ for (const block of region.blocks) {
31288
+ if (alreadyRenderedTexts.has(block.text)) {
31289
+ // Note: A removed block must still hand its merge seam over to the next block which survives
31290
+ isMergeSeamPending = isMergeSeamPending || block.isOpeningMergedSection;
31291
+ continue;
31292
+ }
31293
+ alreadyRenderedTexts.add(block.text);
31294
+ uniqueBlocks.push({
31295
+ text: block.text,
31296
+ isOpeningMergedSection: block.isOpeningMergedSection || isMergeSeamPending,
31297
+ });
31298
+ isMergeSeamPending = false;
31299
+ }
31300
+ return { ...region, blocks: uniqueBlocks };
31301
+ }
31302
+ /**
31303
+ * Renders one region back into its markdown representation
31304
+ *
31305
+ * @param region The region to render
31306
+ * @returns The heading line together with its joined blocks
31307
+ *
31308
+ * @private internal utility of `deduplicateSystemMessage`
31309
+ */
31310
+ function renderRegion(region) {
31311
+ const body = joinBlocks(region.blocks);
31312
+ if (region.headingLine === null) {
31313
+ return body;
31314
+ }
31315
+ if (body === '') {
31316
+ return region.headingLine;
31317
+ }
31318
+ return `${region.headingLine}${region.isBodySeparatedByBlankLine ? '\n\n' : '\n'}${body}`;
31319
+ }
31320
+ /**
31321
+ * Joins blocks of one region back together
31322
+ *
31323
+ * @param blocks The blocks to join in their original order
31324
+ * @returns The joined body of one region
31325
+ *
31326
+ * @private internal utility of `renderRegion`
31327
+ */
31328
+ function joinBlocks(blocks) {
31329
+ return blocks
31330
+ .map((block, index) => index === 0 ? block.text : `${createBlockSeparator(blocks[index - 1], block)}${block.text}`)
31331
+ .join('');
31332
+ }
31333
+ /**
31334
+ * Chooses the separator between two neighboring blocks
31335
+ *
31336
+ * Blocks are normally separated by a blank line, exactly as they were written. Only where two sections were
31337
+ * merged together their lists are joined into one compact list instead of two separate ones.
31338
+ *
31339
+ * @param previousBlock The block rendered before the separator
31340
+ * @param nextBlock The block rendered after the separator
31341
+ * @returns Either a single or a double newline
31342
+ *
31343
+ * @private internal utility of `joinBlocks`
31344
+ */
31345
+ function createBlockSeparator(previousBlock, nextBlock) {
31346
+ if (!nextBlock.isOpeningMergedSection) {
31347
+ return '\n\n';
31348
+ }
31349
+ const previousBlockLines = previousBlock.text.split('\n');
31350
+ const isListContinuing = LIST_ITEM_PATTERN.test(previousBlockLines[previousBlockLines.length - 1]) &&
31351
+ LIST_ITEM_PATTERN.test(nextBlock.text.split('\n')[0]);
31352
+ return isListContinuing ? '\n' : '\n\n';
31353
+ }
31354
+
31116
31355
  /**
31117
31356
  * Removes single-hash comment lines (`# Comment`) from a system message
31118
31357
  * This is used to clean up the final system message before sending it to the AI model
@@ -31186,14 +31425,14 @@ function createInitialAgentModelRequirements(agentName, modelName) {
31186
31425
  * Performs the final system-message cleanup pass after all other augmentation steps are complete.
31187
31426
  *
31188
31427
  * @param requirements - Fully built requirements before final cleanup.
31189
- * @returns Requirements with comment lines removed from the final system message.
31428
+ * @returns Requirements with comment lines removed and repeated instructions deduplicated in the final system message.
31190
31429
  *
31191
31430
  * @private internal utility of `createAgentModelRequirementsWithCommitments`
31192
31431
  */
31193
31432
  function finalizeRequirements(requirements) {
31194
31433
  return {
31195
31434
  ...requirements,
31196
- systemMessage: removeCommentsFromSystemMessage(requirements.systemMessage),
31435
+ systemMessage: deduplicateSystemMessage(removeCommentsFromSystemMessage(requirements.systemMessage)),
31197
31436
  };
31198
31437
  }
31199
31438