extend
Creates a new builder instance based on this one, allowing for variations or specializations without modifying the original.
This method performs a deep copy of the current builder’s state and returns a new independent instance. Changes to the extended builder won’t affect the original, making it perfect for creating specialized versions of a base prompt configuration.
extend(): SystemPromptBuilder
A new SystemPromptBuilder instance with copied state
Example
// Create base support assistant
const baseSupport = createPromptBuilder()
.withIdentity("You are a helpful support assistant")
.withCapabilities(["Answer questions", "Provide guidance"])
.withGuardrails()
.withTone("Professional and friendly");
// Extend for technical support
const technicalSupport = baseSupport.extend()
.withIdentity("You are a technical support specialist")
.withCapabilities(["Debug technical issues", "Explain technical concepts"])
.withContext("Product: SaaS Platform, Tech Stack: React + Node.js");
// Original remains unchanged
console.log(baseSupport.build()); // Still the base version
console.log(technicalSupport.build()); // Extended version
merge
Merges another builder’s configuration into this one, allowing composition of different behavioral patterns and reusable prompt components.
This method combines two builders following specific merge rules:
Merge Rules:
- Identity: Uses this builder’s identity (not overridden)
- Capabilities: Combines both lists (removes duplicates)
- Tools: Combines both lists (throws error if duplicate tool names)
- Constraints: Combines both lists
- Examples: Combines both lists
- Context: Appends source context to this builder’s context
- Tone/Output/Error Handling: This builder’s values take precedence (not overridden if set)
- Guardrails: Enabled if either has it enabled
- Forbidden Topics: Combines both lists (removes duplicates)
merge(source: SystemPromptBuilder): this
source
SystemPromptBuilder
required
The builder to merge into this one
This builder instance for method chaining
Throws an error if duplicate tool names are detected during merge.
Example
// Create reusable security patterns
const securityBuilder = createPromptBuilder()
.withGuardrails()
.withConstraint("must", "Always verify user identity before sharing sensitive data")
.withConstraint("must_not", "Never log or store personal information")
.withForbiddenTopics(["Internal system details", "Other users' data"]);
// Create domain-specific builder
const customerService = createPromptBuilder()
.withIdentity("You are a customer service assistant")
.withCapabilities(["Process returns", "Track orders"])
.withTone("Empathetic and solution-oriented");
// Merge security into customer service
const secureCustomerService = customerService.merge(securityBuilder);
// Now has both customer service features AND security constraints
Advanced Example: Composable Patterns
// Define reusable patterns
const securityPattern = createPromptBuilder()
.withGuardrails()
.withConstraints("must", [
"Verify authentication",
"Log security events"
]);
const compliancePattern = createPromptBuilder()
.withForbiddenTopics(["Medical advice", "Legal advice"])
.withConstraint("must", "Include compliance disclaimers");
const professionalTone = createPromptBuilder()
.withTone("Professional, clear, and concise")
.withOutput("Use structured responses with clear sections");
// Compose multiple patterns
const enterpriseAgent = createPromptBuilder()
.withIdentity("Enterprise support assistant")
.withCapabilities(["Handle enterprise customers", "Escalate issues"])
.merge(securityPattern)
.merge(compliancePattern)
.merge(professionalTone);