README.md
  1  # DriftKit Context Engineering Module
  2  
  3  ## Overview
  4  
  5  The `driftkit-context-engineering` module provides a comprehensive prompt management and engineering platform for AI applications. It features advanced template processing, multiple storage backends, sophisticated testing frameworks, and a modern Vue.js frontend for prompt development and evaluation.
  6  
  7  ## Spring Boot Initialization
  8  
  9  To use the context engineering module in your Spring Boot application:
 10  
 11  ```java
 12  @SpringBootApplication
 13  @Import({PromptServiceAutoConfiguration.class, ContextEngineeringConfig.class})
 14  @ComponentScan(basePackages = {"ai.driftkit.context"}) // Scan context components
 15  @EnableMongoRepositories(basePackages = "ai.driftkit.context.repository") // For MongoDB repositories
 16  public class YourApplication {
 17      public static void main(String[] args) {
 18          SpringApplication.run(YourApplication.class, args);
 19      }
 20  }
 21  ```
 22  
 23  The module provides:
 24  - **Auto-configuration**: `PromptServiceAutoConfiguration` - Automatically registers PromptService beans
 25  - **Configuration**: `ContextEngineeringConfig` - Provides `EtlConfig` bean
 26  - **Services**: Multiple prompt storage implementations (filesystem, in-memory, MongoDB)
 27  - **REST Controllers**: Comprehensive prompt management APIs
 28  - **Frontend**: Vue.js application served at `/prompt-engineering`
 29  
 30  ## Architecture
 31  
 32  ### Module Structure
 33  
 34  ```
 35  driftkit-context-engineering/
 36  ├── driftkit-context-engineering-core/     # Core prompt management
 37  │   ├── domain/                            # Domain objects
 38  │   ├── service/                           # Core services and abstractions
 39  │   └── util/                              # Utility classes
 40  ├── driftkit-context-engineering-spring-boot-starter/ # Spring Boot integration
 41  │   ├── config/                            # Configuration classes
 42  │   ├── controller/                        # REST API endpoints
 43  │   ├── repository/                        # Data access layer
 44  │   ├── service/                           # Spring-specific services
 45  │   └── frontend/                          # Vue.js frontend application
 46  │       ├── src/components/                # Vue components
 47  │       ├── src/views/                     # Application views
 48  │       └── package.json                   # Frontend dependencies
 49  └── pom.xml                               # Parent module configuration
 50  ```
 51  
 52  ### Key Dependencies
 53  
 54  - **Vue.js 3** - Modern frontend framework with TypeScript
 55  - **Spring Boot** - Web framework and auto-configuration
 56  - **MongoDB** - Document persistence for production use
 57  - **Apache Commons JEXL** - Expression language for templates
 58  - **Jackson** - JSON processing and serialization
 59  - **Chart.js** - Data visualization for analytics
 60  - **CodeMirror** - Code editing with syntax highlighting
 61  
 62  ## Core Abstractions
 63  
 64  ### PromptServiceBase Interface
 65  
 66  The central abstraction for prompt management across different storage backends. It provides configuration support, CRUD operations, and advanced retrieval methods with language and state filtering.
 67  
 68  ### TemplateEngine Class
 69  
 70  Advanced template processing engine with AST-based parsing and caching. It supports dot notation for nested object access, reflection-based field access for POJOs, template and reflection caching for performance, and complex condition evaluation with boolean operators.
 71  
 72  **Template Features:**
 73  - **Variable Substitution** - `{{variable}}` with dot notation support
 74  - **Conditional Rendering** - `{{#if condition}}...{{/if}}`
 75  - **List Iteration** - `{{#list collection as item}}...{{/list}}`
 76  - **Boolean Logic** - Complex conditions with `&&` and `||`
 77  - **Comparison Operators** - `>`, `<`, `>=`, `<=`, `==`, `!=`
 78  - **Size Comparisons** - `{{#if collection.size > 0}}`
 79  - **Performance Optimization** - Template and reflection caching
 80  
 81  ## Storage Implementations
 82  
 83  ### InMemoryPromptService
 84  
 85  In-memory storage implementation for development and testing environments.
 86  
 87  **Key Features:**
 88  - **Thread-Safe** - ConcurrentHashMap for concurrent access
 89  - **Fast Access** - No I/O operations
 90  - **Method Indexing** - Optimized retrieval by method name
 91  - **Automatic ID Generation** - UUID-based unique identifiers
 92  
 93  ### FileSystemPromptService
 94  
 95  JSON file-based persistence with thread safety:
 96  
 97  ```java
 98  public class FileSystemPromptService implements PromptServiceBase {
 99      private final ObjectMapper objectMapper = new ObjectMapper()
100          .configure(SerializationFeature.INDENT_OUTPUT, true);
101      
102      private String promptsFilePath;
103      private final Map<String, Prompt> prompts = new ConcurrentHashMap<>();
104      
105      @Override
106      public boolean supportsName(String name) {
107          return "filesystem".equals(name);
108      }
109      
110      @Override
111      public void configure(Map<String, String> config) {
112          this.promptsFilePath = config.get("promptsFilePath");
113          loadFromFile();
114      }
115      
116      @Override
117      public synchronized Prompt savePrompt(Prompt prompt) {
118          if (prompt.getId() == null) {
119              prompt.setId(UUID.randomUUID().toString());
120          }
121          
122          prompt.setUpdatedTime(System.currentTimeMillis());
123          prompts.put(prompt.getId(), prompt);
124          
125          saveToFile();
126          return prompt;
127      }
128      
129      private synchronized void loadFromFile() {
130          try {
131              File file = new File(promptsFilePath);
132              if (file.exists()) {
133                  CollectionType listType = objectMapper.getTypeFactory()
134                      .constructCollectionType(List.class, Prompt.class);
135                  List<Prompt> promptList = objectMapper.readValue(file, listType);
136                  
137                  prompts.clear();
138                  promptList.forEach(prompt -> prompts.put(prompt.getId(), prompt));
139              }
140          } catch (Exception e) {
141              log.error("Failed to load prompts from file: {}", promptsFilePath, e);
142          }
143      }
144      
145      private synchronized void saveToFile() {
146          try {
147              File file = new File(promptsFilePath);
148              file.getParentFile().mkdirs();
149              
150              List<Prompt> promptList = new ArrayList<>(prompts.values());
151              objectMapper.writeValue(file, promptList);
152          } catch (Exception e) {
153              log.error("Failed to save prompts to file: {}", promptsFilePath, e);
154          }
155      }
156  }
157  ```
158  
159  **Configuration Example:**
160  ```yaml
161  driftkit:
162    promptServices:
163      - name: "file-prompts"
164        type: "filesystem"
165        promptsFilePath: "/data/prompts/prompts.json"
166  ```
167  
168  ### MongodbPromptService
169  
170  Production-ready MongoDB integration with Spring Data:
171  
172  ```java
173  @Component
174  public class MongodbPromptService implements PromptServiceBase {
175      
176      @Override
177      public boolean supportsName(String name) {
178          return "mongodb".equals(name);
179      }
180      
181      @Override
182      public Prompt savePrompt(Prompt prompt) {
183          PromptRepository repository = getRepository();
184          
185          if (prompt.getId() == null) {
186              prompt.setId(UUID.randomUUID().toString());
187              prompt.setCreatedTime(System.currentTimeMillis());
188          }
189          
190          // Handle state transitions for version control
191          if (prompt.getState() == State.CURRENT) {
192              // Mark existing current prompts as replaced
193              List<Prompt> existing = repository.findByMethodAndState(prompt.getMethod(), State.CURRENT);
194              existing.forEach(p -> {
195                  p.setState(State.REPLACED);
196                  repository.save(p);
197              });
198          }
199          
200          prompt.setUpdatedTime(System.currentTimeMillis());
201          return repository.save(prompt);
202      }
203      
204      @Override
205      public List<Prompt> getPromptsByMethodsAndState(List<String> methods, State state) {
206          PromptRepository repository = getRepository();
207          return repository.findByMethodIsInAndState(methods, state);
208      }
209      
210      private PromptRepository getRepository() {
211          ApplicationContext context = ApplicationContextProvider.getApplicationContext();
212          return context.getBean(PromptRepository.class);
213      }
214  }
215  ```
216  
217  **Advanced Features:**
218  - **Version Control** - Automatic state management (CURRENT/REPLACED)
219  - **Spring Integration** - Uses ApplicationContext for bean access
220  - **Query Optimization** - Custom repository methods for efficient retrieval
221  - **State Management** - Complex workflow states for prompt lifecycle
222  
223  ## Domain Objects
224  
225  ### Prompt Entity
226  
227  Comprehensive prompt representation with metadata and lifecycle management:
228  
229  ```java
230  @Data
231  @Document(collection = "prompts")
232  public class Prompt {
233      @Id
234      private String id;
235      private String method;           // Logical grouping identifier
236      private String message;          // Main prompt template
237      private String systemMessage;    // System context
238      private String modelId;          // Target AI model
239      private String workflow;         // Associated workflow
240      
241      // State management
242      private State state = State.MODERATION;
243      private ResolveStrategy resolveStrategy = ResolveStrategy.CURRENT;
244      private Language language = Language.GENERAL;
245      
246      // Model parameters
247      private Double temperature;
248      private boolean jsonRequest = false;
249      private boolean jsonResponse = false;
250      private Boolean logprobs;
251      private Integer topLogprobs;
252      
253      // Lifecycle timestamps
254      private long createdTime;
255      private long updatedTime;
256      private long approvedTime;
257      
258      // Template processing
259      public String applyVariables(Map<String, Object> variables) {
260          String processedMessage = TemplateEngine.renderTemplate(this.message, variables);
261          String processedSystemMessage = TemplateEngine.renderTemplate(this.systemMessage, variables);
262          
263          // Create a copy with processed templates
264          Prompt processed = new Prompt();
265          BeanUtils.copyProperties(this, processed);
266          processed.setMessage(processedMessage);
267          processed.setSystemMessage(processedSystemMessage);
268          
269          return processed;
270      }
271      
272      public enum State {
273          MODERATION,      // Under review
274          MANUAL_TESTING,  // Manual testing phase
275          AUTO_TESTING,    // Automated testing phase
276          CURRENT,         // Active production prompt
277          REPLACED         // Superseded by newer version
278      }
279      
280      public enum ResolveStrategy {
281          LAST_VERSION,    // Use most recent version
282          CURRENT          // Use currently active version
283      }
284  }
285  ```
286  
287  ### PromptRequest
288  
289  Comprehensive request structure for prompt execution:
290  
291  ```java
292  @Data
293  @AllArgsConstructor
294  @NoArgsConstructor
295  public class PromptRequest {
296      private String chatId;
297      private String workflow;
298      private String modelId;
299      private String checkerPrompt;      // Validation prompt
300      private String purpose;            // Request purpose/description
301      private String imageMimeType;      // For image-based requests
302      
303      private List<PromptIdRequest> promptIds;  // Multiple prompt execution
304      private Map<String, Object> variables;    // Template variables
305      private Language language = Language.GENERAL;
306      
307      // Model parameters
308      private Boolean logprobs;
309      private Integer topLogprobs;
310      
311      // Features
312      private boolean savePrompt = false;       // Whether to save this as a prompt
313      private List<String> imageBase64;         // Base64-encoded images
314      
315      @Data
316      @AllArgsConstructor
317      @NoArgsConstructor
318      public static class PromptIdRequest {
319          private String promptId;
320          private Map<String, Object> variables;
321      }
322  }
323  ```
324  
325  ### DictionaryItem
326  
327  Enhanced metadata structure for reusable content:
328  
329  ```java
330  @Data
331  @Document(collection = "dictionary_items")
332  public class DictionaryItem {
333      @Id
334      private String id;
335      private String groupId;          // Logical grouping
336      private String name;             // Display name
337      private int index;               // Ordering within group
338      private Language language;       // Language specificity
339      
340      private List<String> markers;    // Key indicators/tags
341      private List<String> samples;    // Example content
342      
343      // Dictionary integration in templates
344      // Usage: dict:itemId-markers: or dict:itemId-samples:
345  }
346  ```
347  
348  ## Vue.js Frontend Application
349  
350  ### Main Application Structure
351  
352  The frontend provides a comprehensive prompt engineering interface:
353  
354  ```typescript
355  // App.vue - Main application component
356  export default {
357    data() {
358      return {
359        authenticated: false,
360        username: '',
361        password: '',
362        activeTab: 'chat',
363        globalModals: {
364          documentProcessing: false,
365          indexingTask: false
366        }
367      }
368    },
369    
370    methods: {
371      async authenticate() {
372        try {
373          const credentials = btoa(`${this.username}:${this.password}`);
374          const response = await axios.get('/api/auth/check', {
375            headers: { 'Authorization': `Basic ${credentials}` }
376          });
377          
378          if (response.status === 200) {
379            this.authenticated = true;
380            this.setupAxiosInterceptors(credentials);
381          }
382        } catch (error) {
383          this.showError('Authentication failed');
384        }
385      }
386    }
387  }
388  ```
389  
390  ### Prompts Management Interface
391  
392  Advanced prompt editing and management interface with the following features:
393  - Search and filter prompts by language and state
394  - Folder-based organization
395  - Version control and history
396  - Visual prompt editor with syntax highlighting
397  - Test panel for prompt validation
398  - Variable detection and management
399  - Model settings and temperature control
400  - Promote to production workflow
401  
402  ![Prompts Management Interface](./screens/prompts.png)
403  
404  ### Test Sets Management
405  
406  Comprehensive testing framework with multiple evaluation types:
407  - Create and manage test sets for prompts
408  - Multiple evaluation types: JSON Schema, Keywords, Exact Match, LLM Evaluation, Manual Review, Regex, Field Value, Array Length
409  - Batch test execution with pass/fail statistics
410  - Test case management with variables and expected results
411  - Test run history and reporting
412  - Visual test results with evaluation details
413  
414  ![Test Sets Management](./screens/test-sets.png)
415  
416  ![Test Sets with Details](./screens/test-sets-hidden-details.png)
417          
418  ### Dashboard Overview
419  
420  The main dashboard provides a centralized view of the prompt engineering platform:
421  - System health monitoring
422  - Recent activity tracking  
423  - Quick access to prompts and test sets
424  - Performance metrics and statistics
425  
426  ![Dashboard](./screens/dashboard.png)
427  
428  ### Test Set Evaluation Results
429  
430  Detailed evaluation results with comprehensive metrics:
431  
432  ![Test Set Evaluation](./screens/prompts-test-set-evaluation.png)
433  
434  ## Spring Boot Integration
435  
436  ### REST API Controller
437  
438  Comprehensive prompt management endpoints with full CRUD operations, prompt execution, and version management.
439      
440  
441  ### Configuration Classes
442  
443  #### ContextEngineeringConfig
444  
445  Spring Boot configuration for prompt services with automatic service discovery and conditional bean creation.
446  
447  #### ApplicationContextProvider
448  
449  Utility for accessing Spring context in non-Spring managed classes, enabling service discovery across the application.
450  
451  ## Advanced Features
452  
453  ### Dictionary Integration
454  
455  Dynamic content integration in templates with support for reusable content markers and samples through dictionary items.
456  
457  ### Prompt Versioning and State Management
458  
459  Advanced lifecycle management with automatic state transitions, version history tracking, and rollback capabilities.
460  
461  ## Demo Examples
462  
463  ### 1. Customer Support Prompt Management
464  
465  This example demonstrates managing prompts for a multi-language customer support system.
466  
467  ```java
468  @Service
469  public class CustomerSupportPrompts {
470      
471      private final PromptService promptService;
472      
473      public CustomerSupportPrompts() throws Exception {
474          Map<String, String> config = Map.of(
475              "type", "mongodb",
476              "database", "customer-support",
477              "collection", "prompts"
478          );
479          this.promptService = PromptServiceFactory.fromConfig(
480              new PromptServiceConfig("support-prompts", config)
481          );
482      }
483      
484      public void initializeSupportPrompts() {
485          // Create greeting prompt
486          Prompt greetingPrompt = new Prompt();
487          greetingPrompt.setMethod("customer.greeting");
488          greetingPrompt.setMessage("Hello {{customerName}}! How can I assist you today?");
489          greetingPrompt.setSystemMessage("You are a helpful customer support agent.");
490          greetingPrompt.setState(State.CURRENT);
491          greetingPrompt.setLanguage(Language.ENGLISH);
492          greetingPrompt.setTemperature(0.7);
493          promptService.savePrompt(greetingPrompt);
494          
495          // Create order inquiry prompt
496          Prompt orderPrompt = new Prompt();
497          orderPrompt.setMethod("customer.order_inquiry");
498          orderPrompt.setMessage("""
499              I'll help you check your order {{orderId}}.
500              
501              {{#if order.status}}
502              Current status: {{order.status}}
503              {{/if}}
504              
505              {{#if order.trackingNumber}}
506              Tracking number: {{order.trackingNumber}}
507              {{/if}}
508              """);
509          orderPrompt.setState(State.CURRENT);
510          orderPrompt.setLanguage(Language.ENGLISH);
511          promptService.savePrompt(orderPrompt);
512      }
513      
514      public String handleCustomerInquiry(String customerId, String orderId) throws Exception {
515          Map<String, Object> variables = Map.of(
516              "customerName", getCustomerName(customerId),
517              "orderId", orderId,
518              "order", getOrderDetails(orderId)
519          );
520          
521          Prompt prompt = promptService.getCurrentPromptOrThrow(
522              "customer.order_inquiry", 
523              Language.ENGLISH
524          );
525          
526          return prompt.applyVariables(variables).getMessage();
527      }
528  }
529  ```
530  
531  ### 2. Dynamic Email Template System
532  
533  This example shows how to use the template engine for dynamic email generation.
534  
535  ```java
536  @Service
537  public class EmailTemplateService {
538      
539      private final PromptService promptService;
540      private final DictionaryItemRepository dictionaryRepo;
541      
542      public void createMarketingEmailTemplate() {
543          // Create dictionary items for reusable content
544          DictionaryItem features = new DictionaryItem();
545          features.setId("product-features");
546          features.setGroupId("marketing");
547          features.setName("Product Features");
548          features.setMarkers(List.of(
549              "Advanced AI capabilities",
550              "Real-time processing",
551              "Enterprise security"
552          ));
553          dictionaryRepo.save(features);
554          
555          // Create email template with dictionary integration
556          Prompt emailPrompt = new Prompt();
557          emailPrompt.setMethod("marketing.product_announcement");
558          emailPrompt.setMessage("""
559              Subject: Introducing {{productName}} - Transform Your Business
560              
561              Hi {{recipient.firstName}},
562              
563              We're excited to announce {{productName}}!
564              
565              Key Features:
566              dict:product-features-markers:
567              
568              {{#if recipient.isPremiumCustomer}}
569              As a valued premium customer, you get:
570              - 30% early bird discount
571              - Priority support
572              - Extended trial period
573              {{/if}}
574              
575              {{#list specialOffers as offer}}
576              Special Offer: {{offer.description}} - Save {{offer.discount}}%
577              {{/list}}
578              
579              Best regards,
580              {{companyName}} Team
581              """);
582          emailPrompt.setState(State.CURRENT);
583          promptService.savePrompt(emailPrompt);
584      }
585      
586      public String generateEmail(EmailRecipient recipient, Product product) throws Exception {
587          Map<String, Object> variables = Map.of(
588              "recipient", recipient,
589              "productName", product.getName(),
590              "specialOffers", product.getOffers(),
591              "companyName", "TechCorp"
592          );
593          
594          Prompt prompt = promptService.getCurrentPromptOrThrow(
595              "marketing.product_announcement",
596              Language.ENGLISH
597          );
598          
599          return prompt.applyVariables(variables).getMessage();
600      }
601  }
602  ```
603  
604  ### 3. A/B Testing Prompt Variations
605  
606  This example demonstrates testing different prompt variations for optimization.
607  
608  ```java
609  @Service
610  public class PromptABTestingService {
611      
612      private final PromptService promptService;
613      private final TestSetService testSetService;
614      
615      public void createABTestVariations() {
616          String baseMethod = "chatbot.product_recommendation";
617          
618          // Variation A - Friendly approach
619          Prompt variantA = new Prompt();
620          variantA.setMethod(baseMethod + ".variant_a");
621          variantA.setMessage("""
622              Based on your interests in {{userInterests}}, 
623              I think you'd love {{productName}}! 
624              It's perfect for {{userCase}}.
625              """);
626          variantA.setSystemMessage("Be friendly and enthusiastic");
627          variantA.setState(State.AUTO_TESTING);
628          variantA.setTemperature(0.8);
629          promptService.savePrompt(variantA);
630          
631          // Variation B - Professional approach  
632          Prompt variantB = new Prompt();
633          variantB.setMethod(baseMethod + ".variant_b");
634          variantB.setMessage("""
635              Analysis of your requirements ({{userInterests}}) 
636              indicates that {{productName}} would be optimal 
637              for your use case: {{userCase}}.
638              """);
639          variantB.setSystemMessage("Be professional and data-driven");
640          variantB.setState(State.AUTO_TESTING);
641          variantB.setTemperature(0.3);
642          promptService.savePrompt(variantB);
643          
644          // Create test set
645          TestSet testSet = new TestSet();
646          testSet.setName("Product Recommendation A/B Test");
647          testSet.setPromptMethods(List.of(
648              baseMethod + ".variant_a",
649              baseMethod + ".variant_b"
650          ));
651          testSet.setEvaluationType(EvaluationType.LLM_EVALUATION);
652          testSet.setEvaluationPrompt("""
653              Rate the recommendation on:
654              1. Relevance (0-10)
655              2. Persuasiveness (0-10)
656              3. Clarity (0-10)
657              Return JSON: {"relevance": X, "persuasiveness": Y, "clarity": Z}
658              """);
659          
660          // Add test cases
661          testSet.addTestCase(
662              Map.of(
663                  "userInterests", "photography and travel",
664                  "productName", "ProCamera X1",
665                  "userCase", "capturing stunning travel moments"
666              ),
667              "Should recommend camera enthusiastically"
668          );
669          
670          testSetService.save(testSet);
671      }
672      
673      public PromptTestResults runABTest() throws Exception {
674          TestSet testSet = testSetService.findByName("Product Recommendation A/B Test");
675          return testSetService.executeTests(testSet);
676      }
677  }
678  ```
679  
680  ### 4. Multi-Stage Workflow Prompts
681  
682  This example shows managing prompts for complex multi-stage workflows.
683  
684  ```java
685  @Service  
686  public class DocumentAnalysisWorkflow {
687      
688      private final PromptService promptService;
689      
690      public void setupDocumentAnalysisPrompts() {
691          // Stage 1: Initial classification
692          Prompt classificationPrompt = new Prompt();
693          classificationPrompt.setMethod("doc_analysis.classify");
694          classificationPrompt.setMessage("""
695              Analyze this document and classify it:
696              
697              Document: {{documentContent}}
698              
699              Categories: {{#list categories as cat}}{{cat}}, {{/list}}
700              
701              Return the most appropriate category.
702              """);
703          classificationPrompt.setJsonResponse(true);
704          classificationPrompt.setState(State.CURRENT);
705          promptService.savePrompt(classificationPrompt);
706          
707          // Stage 2: Extract key information
708          Prompt extractionPrompt = new Prompt();
709          extractionPrompt.setMethod("doc_analysis.extract");
710          extractionPrompt.setMessage("""
711              Extract key information from this {{documentType}} document:
712              
713              {{documentContent}}
714              
715              Focus on:
716              {{#list extractionFields as field}}
717              - {{field}}
718              {{/list}}
719              """);
720          extractionPrompt.setJsonResponse(true);
721          extractionPrompt.setState(State.CURRENT);
722          promptService.savePrompt(extractionPrompt);
723          
724          // Stage 3: Generate summary
725          Prompt summaryPrompt = new Prompt();
726          summaryPrompt.setMethod("doc_analysis.summarize");
727          summaryPrompt.setMessage("""
728              Create a {{summaryLength}}-word summary of this {{documentType}}:
729              
730              Key points extracted:
731              {{#list keyPoints as point}}
732              - {{point.field}}: {{point.value}}
733              {{/list}}
734              
735              Original document: {{documentContent}}
736              """);
737          summaryPrompt.setState(State.CURRENT);
738          promptService.savePrompt(summaryPrompt);
739      }
740      
741      public DocumentAnalysisResult analyzeDocument(String content) throws Exception {
742          // Stage 1: Classify
743          Prompt classifyPrompt = promptService.getCurrentPromptOrThrow(
744              "doc_analysis.classify", Language.GENERAL
745          );
746          String docType = executeClassification(classifyPrompt, content);
747          
748          // Stage 2: Extract based on type
749          List<String> fields = getFieldsForType(docType);
750          Prompt extractPrompt = promptService.getCurrentPromptOrThrow(
751              "doc_analysis.extract", Language.GENERAL
752          );
753          Map<String, Object> extracted = executeExtraction(
754              extractPrompt, content, docType, fields
755          );
756          
757          // Stage 3: Summarize
758          Prompt summaryPrompt = promptService.getCurrentPromptOrThrow(
759              "doc_analysis.summarize", Language.GENERAL
760          );
761          String summary = executeSummary(
762              summaryPrompt, content, docType, extracted
763          );
764          
765          return new DocumentAnalysisResult(docType, extracted, summary);
766      }
767  }
768  ```
769  
770  ### 5. Prompt Performance Monitoring
771  
772  This example demonstrates tracking and optimizing prompt performance.
773  
774  ```java
775  @Service
776  public class PromptPerformanceMonitor {
777      
778      private final PromptService promptService;
779      private final MetricsService metricsService;
780      
781      public void trackPromptExecution(String promptId, ExecutionMetrics metrics) {
782          PromptMetrics promptMetrics = new PromptMetrics();
783          promptMetrics.setPromptId(promptId);
784          promptMetrics.setExecutionTime(metrics.getDuration());
785          promptMetrics.setTokensUsed(metrics.getTokenCount());
786          promptMetrics.setCost(calculateCost(metrics));
787          promptMetrics.setSuccess(metrics.isSuccess());
788          promptMetrics.setTimestamp(System.currentTimeMillis());
789          
790          metricsService.record(promptMetrics);
791          
792          // Check if optimization needed
793          if (shouldOptimize(promptId)) {
794              optimizePrompt(promptId);
795          }
796      }
797      
798      private boolean shouldOptimize(String promptId) {
799          PromptStats stats = metricsService.getStats(promptId, 24); // Last 24 hours
800          
801          return stats.getAverageTokens() > 1000 || 
802                 stats.getSuccessRate() < 0.9 ||
803                 stats.getAverageDuration() > 5000; // 5 seconds
804      }
805      
806      private void optimizePrompt(String promptId) throws Exception {
807          Prompt prompt = promptService.getPromptById(promptId).orElseThrow();
808          
809          // Create optimized version
810          Prompt optimized = new Prompt();
811          optimized.setMethod(prompt.getMethod());
812          optimized.setMessage(compressPrompt(prompt.getMessage()));
813          optimized.setSystemMessage(prompt.getSystemMessage());
814          optimized.setState(State.AUTO_TESTING);
815          optimized.setTemperature(prompt.getTemperature() - 0.1); // Reduce randomness
816          
817          promptService.savePrompt(optimized);
818          
819          // Schedule A/B test
820          scheduleABTest(prompt.getId(), optimized.getId());
821      }
822      
823      private String compressPrompt(String message) {
824          // Remove redundant instructions
825          // Simplify complex sentences
826          // Use more concise language
827          return message; // Simplified version
828      }
829      
830      public PromptHealthReport generateHealthReport() {
831          List<Prompt> activePrompts = promptService.getPromptsByMethodsAndState(
832              null, State.CURRENT
833          );
834          
835          Map<String, PromptHealth> health = new HashMap<>();
836          
837          for (Prompt prompt : activePrompts) {
838              PromptStats stats = metricsService.getStats(prompt.getId(), 168); // Week
839              
840              PromptHealth promptHealth = new PromptHealth();
841              promptHealth.setPromptId(prompt.getId());
842              promptHealth.setMethod(prompt.getMethod());
843              promptHealth.setExecutions(stats.getTotalExecutions());
844              promptHealth.setSuccessRate(stats.getSuccessRate());
845              promptHealth.setAverageLatency(stats.getAverageDuration());
846              promptHealth.setAverageTokens(stats.getAverageTokens());
847              promptHealth.setHealthScore(calculateHealthScore(stats));
848              
849              health.put(prompt.getId(), promptHealth);
850          }
851          
852          return new PromptHealthReport(health);
853      }
854  }
855  ```
856  
857  ## Configuration
858  
859  ### Application Configuration
860  
861  ```yaml
862  driftkit:
863    promptServices:
864      - name: "primary"
865        type: "mongodb"
866        database: "driftkit"
867        collection: "prompts"
868        
869    dictionaryServices:
870      - name: "primary"
871        type: "mongodb"
872        database: "driftkit"
873        collection: "dictionary_items"
874        
875  spring:
876    data:
877      mongodb:
878        uri: "${MONGODB_URI}"
879        database: "driftkit"
880        
881    web:
882      resources:
883        static-locations: classpath:/static/
884        
885  server:
886    port: 8080
887    servlet:
888      context-path: /
889  ```
890  
891  ### Frontend Build Configuration
892  
893  ```xml
894  <!-- pom.xml frontend build integration -->
895  <plugin>
896    <groupId>com.github.eirslett</groupId>
897    <artifactId>frontend-maven-plugin</artifactId>
898    <version>1.12.1</version>
899    <configuration>
900      <workingDirectory>src/main/frontend</workingDirectory>
901      <installDirectory>target</installDirectory>
902    </configuration>
903    <executions>
904      <execution>
905        <id>install node and npm</id>
906        <goals>
907          <goal>install-node-and-npm</goal>
908        </goals>
909        <configuration>
910          <nodeVersion>v18.16.0</nodeVersion>
911          <npmVersion>9.5.1</npmVersion>
912        </configuration>
913      </execution>
914      <execution>
915        <id>npm install</id>
916        <goals>
917          <goal>npm</goal>
918        </goals>
919        <configuration>
920          <arguments>install</arguments>
921        </configuration>
922      </execution>
923      <execution>
924        <id>npm run build</id>
925        <goals>
926          <goal>npm</goal>
927        </goals>
928        <configuration>
929          <arguments>run build</arguments>
930        </configuration>
931      </execution>
932    </executions>
933  </plugin>
934  ```
935  
936  ## Summary
937  
938  The driftkit-context-engineering module provides a comprehensive prompt engineering platform with:
939  - Multiple storage backends (MongoDB, filesystem, in-memory)
940  - Advanced template engine with conditionals and loops
941  - Version control and state management
942  - A/B testing capabilities
943  - Performance monitoring and optimization
944  - Visual management interface with test automation
945  - Dictionary-based content reuse
946  - Multi-language support
947  
948  This comprehensive documentation covers all aspects of the driftkit-context-engineering module, providing detailed information about prompt management, template processing, multi-storage backends, advanced testing frameworks, and the modern Vue.js frontend interface. The module offers a complete solution for prompt engineering in AI applications with sophisticated versioning, testing, and evaluation capabilities.