# AI Programming Prompts for @ticatec/dyna-js

This document contains optimized prompts for AI assistants (like Claude, ChatGPT, etc.) to help you work with the `@ticatec/dyna-js` library more effectively.

## 🤖 AI System Prompt

Copy this prompt when starting a new conversation with an AI assistant:

```
You are working with the @ticatec/dyna-js library, a modern TypeScript library for safe dynamic code execution in browser environments.

Key Context:
- This is a browser-only library (ESM modules, ES2020+)
- It provides two main classes: DynaJs and ModuleLoader
- DynaJs: Execute dynamic code with configurable security policies
- ModuleLoader: Manage dynamic modules with caching and version control
- StorageManager: Unified localStorage API for module storage

Project Structure:
- src/DynaJs.ts - Core dynamic code execution engine
- src/ModuleLoader.ts - Module management with singleton pattern
- src/StorageManager.ts - Unified localStorage operations
- src/types.ts - TypeScript type definitions
- src/utils.ts - Utility functions
- src/index.ts - Main entry point

Important Architecture Decisions:
1. Pure browser environment - no Node.js support
2. ESM-only module system
3. localStorage is the only storage backend (simple, sufficient for this use case)
4. Singleton pattern for ModuleLoader
5. Parallel module loading for better performance
6. Unified storage structure (one JSON object per module)
```

## 📝 Common Tasks

### Task 1: Understanding the Codebase

```
Please analyze the @ticatec/dyna-js library codebase and provide:
1. Overview of the main classes and their responsibilities
2. How the storage system works
3. The security model and how it can be configured
4. Performance optimizations implemented
5. Any potential issues or areas for improvement

Focus on understanding the architecture and design patterns used.
```

### Task 2: Adding New Features

```
I want to add a new feature to the ModuleLoader class. Here's what I need:

[Describe your feature requirements]

Please:
1. Analyze the existing ModuleLoader implementation
2. Design the new feature following the existing patterns
3. Consider how it interacts with StorageManager
4. Think about backward compatibility
5. Provide implementation suggestions with code examples

Keep in mind:
- This is a browser-only library
- Use the existing singleton pattern
- Follow the storage conventions (unified JSON structure)
- Maintain parallel loading optimization where applicable
```

### Task 3: Debugging an Issue

```
I'm experiencing an issue with @ticatec/dyna-js:

[Describe the issue with code examples and error messages]

Please help me debug by:
1. Identifying the root cause
2. Suggesting fixes that follow the library's patterns
3. Considering edge cases and potential side effects
4. Providing test cases to verify the fix

Context:
- Browser: [Chrome/Firefox/Safari/Edge version]
- Library version: [x.y.z]
- Error: [full error message and stack trace]
```

### Task 4: Optimizing Performance

```
I need to optimize performance in this use case:

[Describe your use case with code examples]

Please analyze:
1. Current performance bottlenecks
2. How to leverage parallel loading features
3. Storage optimization strategies
4. Caching opportunities
5. Memory management considerations

Provide specific code improvements with explanations.
```

### Task 5: Security Review

```
I want to ensure I'm using @ticatec/dyna-js securely. Please review:

[Your configuration and usage code]

For:
1. Security configuration best practices
2. Potential vulnerabilities in my setup
3. Recommendations for improvement
4. Edge cases that might compromise security

Consider:
- validateCode settings
- allowBrowserAPIs settings
- Default imports and what they expose
- Input validation needs
- Timeout configurations
```

### Task 6: Migration from Old Version

```
I need to migrate from version [old version] to [new version].

Key concerns:
1. Breaking changes in the API
2. Storage format changes and migration strategy
3. Configuration changes needed
4. New features I should adopt
5. Deprecation warnings to address

Please provide:
- Migration guide with code examples
- Backward compatibility considerations
- Testing checklist
```

### Task 7: Custom Storage Backend

```
I want to understand how to implement a custom storage solution.

Current understanding:
- The library uses localStorage via StorageManager
- Storage is unified (single JSON per module)
- Version control is built-in

Please explain:
1. The current storage architecture
2. How StorageManager works
3. What would need to change for a custom backend (e.g., IndexedDB)
4. Migration strategy from localStorage
5. Pros and cons of different storage options

Note: The current implementation uses localStorage intentionally for simplicity. This is exploratory.
```

### Task 8: Writing Tests

```
Help me write comprehensive tests for [specific feature/component].

Please provide:
1. Unit test cases for normal operation
2. Edge cases and error conditions
3. Integration tests if needed
4. Mock strategies for localStorage
5. Test utilities and helpers

Framework: [Jest/Vitest/Mocha/etc.]
```

### Task 9: Code Review

```
Please review my implementation using @ticatec/dyna-js:

[Your code]

Check for:
1. Correct usage of the library API
2. Security best practices
3. Performance considerations
4. Error handling
5. TypeScript type safety
6. Code organization and readability

Provide:
- Specific issues found
- Improvement suggestions
- Code style recommendations
- Potential bugs or edge cases
```

### Task 10: Documentation

```
I need help documenting [feature/component/use case].

Please create:
1. Clear overview of what it does
2. When to use it (use cases)
3. Quick start example
4. API reference
5. Common patterns and best practices
6. Troubleshooting section

Target audience: [beginner/intermediate/advanced developers]
Format: [Markdown/JSdoc/TypeDoc/etc.]
```

## 🔍 Code Analysis Prompts

### Understanding DynaJs

```
Explain how the DynaJs class executes dynamic code safely:
1. How does new Function() work in this context?
2. What security measures are in place?
3. How are default imports injected?
4. What does the validation check for?
5. How are browser APIs controlled?

Include code examples showing the flow from input to execution.
```

### Understanding ModuleLoader

```
Explain the ModuleLoader architecture:
1. How does the singleton pattern work?
2. What is the module lifecycle?
3. How does parallel loading work?
4. How is version control implemented?
5. How does caching work?

Include a diagram or flow description of the checkFreshScripts process.
```

### Understanding StorageManager

```
Explain the storage system:
1. What is the unified storage structure?
2. Why was this design chosen?
3. How does version control work in storage?
4. What error handling is built-in?
5. How does quota exceeded get handled?

Show examples of storage operations and the resulting localStorage structure.
```

## 🛠️ Implementation Prompts

### Adding a New Method to DynaJs

```
I want to add a new method to DynaJs called [method name].

Requirements:
[Your requirements]

Please:
1. Follow existing method patterns
2. Maintain type safety
3. Add proper JSDoc comments
4. Consider security implications
5. Provide example usage

Include the complete method implementation.
```

### Extending ModuleLoader

```
I need to extend ModuleLoader to [requirement].

Please:
1. Maintain the singleton pattern
2. Consider storage implications
3. Think about backward compatibility
4. Provide TypeScript types
5. Include usage examples

Focus on integrating cleanly with existing code.
```

### Custom Validation Function

```
I need a custom validation function for dynamic code.

Requirements:
[Your requirements]

Please:
1. Show how to integrate with DynaJs
2. Provide regex patterns or AST validation
3. Consider performance impact
4. Include test cases
5. Document security implications
```

## 🐛 Bug Fixing Prompts

### Module Not Found Error

```
I'm getting "Module not found in storage" error from ModuleLoader.createModule().

My code:
[Your code]

Debugging help needed:
1. What causes this error?
2. How to prevent it?
3. How to add better error messages?
4. Troubleshooting steps
5. Common mistakes that lead to this
```

### Storage Quota Exceeded

```
I'm hitting localStorage quota limits.

Current usage:
[Your storage stats]

Help me:
1. Understand why quota is exceeded
2. Implement cleanup strategies
3. Monitor storage usage proactively
4. Migrate old data if needed
5. Prevent future quota issues

Provide code examples for each solution.
```

### Parallel Loading Issues

```
Module loading isn't working in parallel as expected.

My code:
[Your code]

Please analyze:
1. Is parallel loading actually being used?
2. What might block parallelization?
3. How to verify parallel execution?
4. Performance measurement approach
5. Optimization opportunities
```

## 📚 Learning Prompts

### Teaching the Library

```
Teach me how to use @ticatec/dyna-js for [specific use case].

Start with:
1. High-level overview
2. Step-by-step implementation
3. Common patterns
4. Best practices
5. Pitfalls to avoid

Provide complete, runnable examples for each step.
```

### Comparing Approaches

```
Compare different ways to [task] using @ticatec/dyna-js:

Option 1: [Approach 1]
Option 2: [Approach 2]
Option 3: [Approach 3]

For each approach, analyze:
- Code complexity
- Performance
- Security
- Maintainability
- Use case fit

Recommend the best approach with rationale.
```

### Architecture Deep Dive

```
Explain the architectural decisions in @ticatec/dyna-js:

1. Why browser-only instead of universal?
2. Why ESM-only instead of dual module?
3. Why localStorage instead of IndexedDB?
4. Why singleton pattern for ModuleLoader?
5. Why unified storage structure?

For each decision:
- Explain the rationale
- Discuss trade-offs
- Consider alternatives
- Identify when to reconsider
```

## 🎯 Specific Use Cases

### Dynamic Form Builder

```
I want to build a dynamic form builder using @ticatec/dyna-js.

Requirements:
[Your requirements]

Please design:
1. Overall architecture
2. DynaJs configuration
3. Module structure for form definitions
4. Storage strategy
5. Security considerations

Provide a complete working example.
```

### Plugin System

```
I want to create a plugin system using ModuleLoader.

Requirements:
[Your requirements]

Help me design:
1. Plugin interface
2. Plugin loading strategy
3. Version management for plugins
4. Security sandboxing
5. Hot-reload capability

Include code examples and architecture diagram.
```

### Remote Code Execution

```
I want to safely execute code from a remote server.

Requirements:
[Your requirements]

Please address:
1. Security best practices
2. Validation strategy
3. Timeout handling
4. Error recovery
5. Caching strategy

Provide complete implementation with security analysis.
```

## 💡 Tips for Best Results

1. **Be Specific**: Include code snippets, error messages, and context
2. **State Your Goal**: Clearly explain what you want to achieve
3. **Provide Context**: Share relevant configuration and environment details
4. **Ask Follow-ups**: Request clarification or alternatives if needed
5. **Test Incrementally**: Implement and test suggestions in small steps

## 🔧 Additional Resources

When working with AI assistants, you may also want to reference:

- README.md - Complete API documentation
- README_CN.md - Chinese documentation
- Source code in src/ directory - Implementation details
- TypeScript type definitions - Type safety information
- Git history - Evolution of features and fixes