@promptbook/node 0.112.0-26 → 0.112.0-28

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
@@ -14,8 +14,8 @@ import { forTime } from 'waitasecond';
14
14
  import * as dotenv from 'dotenv';
15
15
  import sha256 from 'crypto-js/sha256';
16
16
  import { Subject, BehaviorSubject } from 'rxjs';
17
- import { lookup, extension } from 'mime-types';
18
17
  import moment from 'moment';
18
+ import { lookup, extension } from 'mime-types';
19
19
  import { parse, unparse } from 'papaparse';
20
20
  import { Agent as Agent$1, setDefaultOpenAIClient, setDefaultOpenAIKey, fileSearchTool, tool, run } from '@openai/agents';
21
21
  import Bottleneck from 'bottleneck';
@@ -35,7 +35,7 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
35
35
  * @generated
36
36
  * @see https://github.com/webgptorg/promptbook
37
37
  */
38
- const PROMPTBOOK_ENGINE_VERSION = '0.112.0-26';
38
+ const PROMPTBOOK_ENGINE_VERSION = '0.112.0-28';
39
39
  /**
40
40
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
41
41
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -19183,6 +19183,147 @@ class UseCommitmentDefinition extends BaseCommitmentDefinition {
19183
19183
  * Note: [💞] Ignore a discrepancy between file name and entity name
19184
19184
  */
19185
19185
 
19186
+ /**
19187
+ * All `USE` commitment types currently participating in final system-message aggregation.
19188
+ *
19189
+ * @private internal constant for `aggregateUseCommitmentSystemMessages`
19190
+ */
19191
+ const AGGREGATED_USE_COMMITMENT_TYPES = ['USE BROWSER', 'USE SEARCH ENGINE', 'USE TIME'];
19192
+ /**
19193
+ * Prefix used for temporary in-system-message placeholders that preserve the first-occurrence position of aggregated `USE` sections.
19194
+ *
19195
+ * @private internal constant for `appendAggregatedUseCommitmentPlaceholder`
19196
+ */
19197
+ const AGGREGATED_USE_COMMITMENT_PLACEHOLDER_PREFIX = '# AGGREGATED USE COMMITMENT: ';
19198
+ /**
19199
+ * Type guard for `USE` commitment types that are aggregated in the final system message.
19200
+ *
19201
+ * @param type - Commitment type to check.
19202
+ * @returns `true` when the commitment participates in `USE` system-message aggregation.
19203
+ * @private internal utility of `aggregateUseCommitmentSystemMessages`
19204
+ */
19205
+ function isAggregatedUseCommitmentType(type) {
19206
+ return AGGREGATED_USE_COMMITMENT_TYPES.includes(type);
19207
+ }
19208
+ /**
19209
+ * Creates the placeholder token used to reserve the first-occurrence position of an aggregated `USE` system-message section.
19210
+ *
19211
+ * @param type - Aggregated `USE` commitment type.
19212
+ * @returns Single-line placeholder comment stored in the interim system message.
19213
+ * @private internal utility of `appendAggregatedUseCommitmentPlaceholder`
19214
+ */
19215
+ function getAggregatedUseCommitmentPlaceholder(type) {
19216
+ return `${AGGREGATED_USE_COMMITMENT_PLACEHOLDER_PREFIX}${type}`;
19217
+ }
19218
+ /**
19219
+ * Combines distinct additional instruction blocks in source order.
19220
+ *
19221
+ * @param additionalInstructions - Deduplicated instruction blocks collected from the agent source.
19222
+ * @returns Combined instruction text ready for `formatOptionalInstructionBlock`.
19223
+ * @private internal utility of `createAggregatedUseCommitmentSystemMessage`
19224
+ */
19225
+ function combineAdditionalInstructions(additionalInstructions) {
19226
+ return additionalInstructions.join('\n');
19227
+ }
19228
+ /**
19229
+ * Creates the final aggregated system-message section for a supported `USE` commitment type.
19230
+ *
19231
+ * @param type - Aggregated `USE` commitment type.
19232
+ * @param additionalInstructions - Distinct additional instructions in source order.
19233
+ * @returns Final system-message block for the commitment type.
19234
+ * @private internal utility of `aggregateUseCommitmentSystemMessages`
19235
+ */
19236
+ function createAggregatedUseCommitmentSystemMessage(type, additionalInstructions) {
19237
+ const combinedAdditionalInstructions = combineAdditionalInstructions(additionalInstructions);
19238
+ switch (type) {
19239
+ case 'USE TIME':
19240
+ return spaceTrim$1((block) => `
19241
+ Time and date context:
19242
+ - It is ${moment().format('MMMM YYYY')} now.
19243
+ - If you need more precise current time information, use the tool "get_current_time".
19244
+ ${block(formatOptionalInstructionBlock('Time instructions', combinedAdditionalInstructions))}
19245
+ `);
19246
+ case 'USE BROWSER':
19247
+ return spaceTrim$1((block) => `
19248
+ You have access to browser tools to fetch and access content from the internet.
19249
+ - Use "fetch_url_content" to retrieve content from specific URLs (webpages or documents) using scrapers.
19250
+ - Use "run_browser" for real interactive browser automation (navigation, clicks, typing, waiting, scrolling).
19251
+ When you need to know information from a specific website or document, use the fetch_url_content tool.
19252
+ ${block(formatOptionalInstructionBlock('Browser instructions', combinedAdditionalInstructions))}
19253
+ `);
19254
+ case 'USE SEARCH ENGINE':
19255
+ return spaceTrim$1((block) => `
19256
+ Tool:
19257
+ - You have access to the web search engine via the tool "web_search".
19258
+ - Use it to find up-to-date information or facts that you don't know.
19259
+ - When you need to know some information from the internet, use the tool provided to you.
19260
+ - Do not make up information when you can search for it.
19261
+ - Do not tell the user you cannot search for information, YOU CAN.
19262
+ ${block(formatOptionalInstructionBlock('Search instructions', combinedAdditionalInstructions))}
19263
+ `);
19264
+ }
19265
+ }
19266
+ /**
19267
+ * Adds the placeholder for an aggregated `USE` system-message section only once, preserving the section position from the first occurrence.
19268
+ *
19269
+ * @param requirements - Current model requirements.
19270
+ * @param type - Aggregated `USE` commitment type being applied.
19271
+ * @returns Requirements with the placeholder inserted when it was not already present.
19272
+ * @private internal utility of `USE` commitments
19273
+ */
19274
+ function appendAggregatedUseCommitmentPlaceholder(requirements, type) {
19275
+ const placeholder = getAggregatedUseCommitmentPlaceholder(type);
19276
+ if (requirements.systemMessage.includes(placeholder)) {
19277
+ return requirements;
19278
+ }
19279
+ const systemMessage = requirements.systemMessage.trim()
19280
+ ? `${requirements.systemMessage}\n\n${placeholder}`
19281
+ : placeholder;
19282
+ return {
19283
+ ...requirements,
19284
+ systemMessage,
19285
+ };
19286
+ }
19287
+ /**
19288
+ * Replaces temporary `USE` placeholders with one aggregated system-message block per commitment type.
19289
+ *
19290
+ * Distinct additional-instruction blocks are merged in stable source order while the hard-coded section is emitted only once.
19291
+ *
19292
+ * @param requirements - Model requirements produced by commitment-by-commitment application.
19293
+ * @param commitments - Filtered commitments in their original source order.
19294
+ * @returns Requirements with aggregated `USE` system-message sections.
19295
+ * @private internal utility of `createAgentModelRequirementsWithCommitments`
19296
+ */
19297
+ function aggregateUseCommitmentSystemMessages(requirements, commitments) {
19298
+ const additionalInstructionsByType = new Map();
19299
+ for (const commitment of commitments) {
19300
+ if (!isAggregatedUseCommitmentType(commitment.type)) {
19301
+ continue;
19302
+ }
19303
+ let additionalInstructions = additionalInstructionsByType.get(commitment.type);
19304
+ if (!additionalInstructions) {
19305
+ additionalInstructions = [];
19306
+ additionalInstructionsByType.set(commitment.type, additionalInstructions);
19307
+ }
19308
+ const normalizedContent = spaceTrim$1(commitment.content);
19309
+ if (normalizedContent && !additionalInstructions.includes(normalizedContent)) {
19310
+ additionalInstructions.push(normalizedContent);
19311
+ }
19312
+ }
19313
+ let systemMessage = requirements.systemMessage;
19314
+ for (const [type, additionalInstructions] of additionalInstructionsByType) {
19315
+ const placeholder = getAggregatedUseCommitmentPlaceholder(type);
19316
+ if (!systemMessage.includes(placeholder)) {
19317
+ continue;
19318
+ }
19319
+ systemMessage = systemMessage.replace(placeholder, createAggregatedUseCommitmentSystemMessage(type, additionalInstructions));
19320
+ }
19321
+ return {
19322
+ ...requirements,
19323
+ systemMessage,
19324
+ };
19325
+ }
19326
+
19186
19327
  /**
19187
19328
  * Client-side safe wrapper for fetching URL content
19188
19329
  *
@@ -19233,13 +19374,14 @@ async function fetchUrlContentViaBrowser(url, agentsServerUrl) {
19233
19374
  * 1. One-shot URL fetching: Simple function to fetch and scrape URL content
19234
19375
  * 2. Running browser: For complex tasks like scrolling, clicking, form filling, etc.
19235
19376
  *
19236
- * The content following `USE BROWSER` is ignored (similar to NOTE).
19377
+ * The content following `USE BROWSER` is an arbitrary text that the agent should know
19378
+ * (e.g. browsing scope or preferred sources).
19237
19379
  *
19238
19380
  * Example usage in agent source:
19239
19381
  *
19240
19382
  * ```book
19241
19383
  * USE BROWSER
19242
- * USE BROWSER This will be ignored
19384
+ * USE BROWSER Prefer official documentation and source websites.
19243
19385
  * ```
19244
19386
  *
19245
19387
  * @private [🪔] Maybe export the commitments through some package
@@ -19277,7 +19419,7 @@ class UseBrowserCommitmentDefinition extends BaseCommitmentDefinition {
19277
19419
 
19278
19420
  ## Key aspects
19279
19421
 
19280
- - The content following \`USE BROWSER\` is ignored (similar to NOTE)
19422
+ - The content following \`USE BROWSER\` is an arbitrary text that the agent should know (e.g. browsing scope or preferred sources).
19281
19423
  - Provides two levels of browser access:
19282
19424
  1. **One-shot URL fetching**: Simple function to fetch and scrape URL content (active)
19283
19425
  2. **Running browser**: For complex tasks like scrolling, clicking, form filling, etc. (runtime-dependent)
@@ -19394,20 +19536,14 @@ class UseBrowserCommitmentDefinition extends BaseCommitmentDefinition {
19394
19536
  });
19395
19537
  }
19396
19538
  const updatedTools = [...existingTools, ...toolsToAdd];
19397
- // Return requirements with updated tools and metadata
19398
- return this.appendToSystemMessage({
19539
+ return appendAggregatedUseCommitmentPlaceholder({
19399
19540
  ...requirements,
19400
19541
  tools: updatedTools,
19401
19542
  _metadata: {
19402
19543
  ...requirements._metadata,
19403
19544
  useBrowser: true,
19404
19545
  },
19405
- }, spaceTrim$1(`
19406
- You have access to browser tools to fetch and access content from the internet.
19407
- - Use "fetch_url_content" to retrieve content from specific URLs (webpages or documents) using scrapers.
19408
- - Use "run_browser" for real interactive browser automation (navigation, clicks, typing, waiting, scrolling).
19409
- When you need to know information from a specific website or document, use the fetch_url_content tool.
19410
- `));
19546
+ }, this.type);
19411
19547
  }
19412
19548
  /**
19413
19549
  * Gets the browser tool function implementations.
@@ -23194,7 +23330,6 @@ class UseSearchEngineCommitmentDefinition extends BaseCommitmentDefinition {
23194
23330
  `);
23195
23331
  }
23196
23332
  applyToAgentModelRequirements(requirements, content) {
23197
- const extraInstructions = formatOptionalInstructionBlock('Search instructions', content);
23198
23333
  // Get existing tools array or create new one
23199
23334
  const existingTools = requirements.tools || [];
23200
23335
  // Add 'web_search' to tools if not already present
@@ -23207,7 +23342,6 @@ class UseSearchEngineCommitmentDefinition extends BaseCommitmentDefinition {
23207
23342
  description: spaceTrim$1(`
23208
23343
  Search the internet for information.
23209
23344
  Use this tool when you need to find up-to-date information or facts that you don't know.
23210
- ${!content ? '' : `Search scope / instructions: ${content}`}
23211
23345
  `),
23212
23346
  parameters: {
23213
23347
  type: 'object',
@@ -23245,23 +23379,14 @@ class UseSearchEngineCommitmentDefinition extends BaseCommitmentDefinition {
23245
23379
  },
23246
23380
  },
23247
23381
  ];
23248
- // Return requirements with updated tools and metadata
23249
- return this.appendToSystemMessage({
23382
+ return appendAggregatedUseCommitmentPlaceholder({
23250
23383
  ...requirements,
23251
23384
  tools: updatedTools,
23252
23385
  _metadata: {
23253
23386
  ...requirements._metadata,
23254
23387
  useSearchEngine: content || true,
23255
23388
  },
23256
- }, spaceTrim$1((block) => `
23257
- Tool:
23258
- - You have access to the web search engine via the tool "web_search".
23259
- - Use it to find up-to-date information or facts that you don't know.
23260
- - When you need to know some information from the internet, use the tool provided to you.
23261
- - Do not make up information when you can search for it.
23262
- - Do not tell the user you cannot search for information, YOU CAN.
23263
- ${block(extraInstructions)}
23264
- `));
23389
+ }, this.type);
23265
23390
  }
23266
23391
  /**
23267
23392
  * Gets human-readable titles for tool functions provided by this commitment.
@@ -24372,7 +24497,6 @@ class UseTimeCommitmentDefinition extends BaseCommitmentDefinition {
24372
24497
  `);
24373
24498
  }
24374
24499
  applyToAgentModelRequirements(requirements, content) {
24375
- const extraInstructions = formatOptionalInstructionBlock('Time instructions', content);
24376
24500
  // Get existing tools array or create new one
24377
24501
  const existingTools = requirements.tools || [];
24378
24502
  // Add 'get_current_time' to tools if not already present
@@ -24396,19 +24520,13 @@ class UseTimeCommitmentDefinition extends BaseCommitmentDefinition {
24396
24520
  },
24397
24521
  // <- TODO: !!!! define the function in LLM tools
24398
24522
  ];
24399
- // Return requirements with updated tools and metadata
24400
- return this.appendToSystemMessage({
24523
+ return appendAggregatedUseCommitmentPlaceholder({
24401
24524
  ...requirements,
24402
24525
  tools: updatedTools,
24403
24526
  _metadata: {
24404
24527
  ...requirements._metadata,
24405
24528
  },
24406
- }, spaceTrim$1((block) => `
24407
- Time and date context:
24408
- - It is ${moment().format('MMMM YYYY')} now.
24409
- - If you need more precise current time information, use the tool "get_current_time".
24410
- ${block(extraInstructions)}
24411
- `));
24529
+ }, this.type);
24412
24530
  }
24413
24531
  /**
24414
24532
  * Gets human-readable titles for tool functions provided by this commitment.
@@ -28064,6 +28182,7 @@ async function createAgentModelRequirementsWithCommitments(agentSource, modelNam
28064
28182
  }
28065
28183
  }
28066
28184
  }
28185
+ requirements = aggregateUseCommitmentSystemMessages(requirements, filteredCommitments);
28067
28186
  // Handle IMPORT commitments for generic files
28068
28187
  // Note: This logic could be moved to ImportCommitmentDefinition, but it needs to be asynchronous
28069
28188
  if (requirements.importedFileUrls && requirements.importedFileUrls.length > 0) {