Code Enhancement Suite
Run ID: 69b6f9fd896970b0894648b52026-03-29Development
PantheraHive BOS
BOS Dashboard

Workflow Step: AI Refactor (collab)

Workflow Name: Code Enhancement Suite

Category: Development

Description (Workflow): Test run

Topic: AI Technology

Execution Time Allotted: 5 min (+100 cr)


1. Step Overview and Objective

This is the second step, ai_refactor, within the "Code Enhancement Suite" workflow. Leveraging the collab application, this phase focuses on applying advanced AI algorithms to analyze the provided codebase, identify areas for improvement, and generate refactored code. The primary objective is to enhance code readability, maintainability, efficiency, and robustness without altering its external behavior or core functionality.

For this test run focused on "AI Technology," we will simulate the refactoring of a common component: a data preprocessing function often found in Machine Learning pipelines.


2. Original Code Analysis (Simulated Input)

Based on the "AI Technology" topic, the AI assistant (via collab) received the following Python function for analysis. This function aims to preprocess a Pandas DataFrame, handling missing values, creating basic features, and normalizing numerical columns.

text • 1,264 chars
---

### 3. AI Refactoring Analysis and Rationale

The `collab` AI analyzed the `preprocess_data_for_ml` function and identified several areas for improvement, primarily concerning:

*   **Single Responsibility Principle (SRP):** The function performs multiple distinct operations (missing value imputation, feature engineering, scaling), making it long and harder to test/maintain.
*   **Readability and Clarity:** Debug `print` statements pollute the function's core logic. Repetitive `config.get` calls and `col in processed_df.columns` checks.
*   **Modularity:** Sub-tasks are not encapsulated in their own functions.
*   **Robustness:** While basic checks are present (`if max_val > min_val`), more explicit error handling or logging could be beneficial for production.
*   **Pythonic Style:** Could leverage more idiomatic Pandas operations or helper functions.
*   **Constants:** The "UNKNOWN_CATEGORY" string is a magic string.

The AI's refactoring strategy focuses on breaking down the monolithic function into smaller, specialized, and reusable components, enhancing overall code quality.

---

### 4. Refactored Code (Generated by AI)

Here is the refactored version of the `preprocess_data_for_ml` function, generated by the `collab` AI assistant:

Sandboxed live preview

Workflow Execution: Code Enhancement Suite - Step 1/3: Code Analysis

Workflow Name: Code Enhancement Suite

Category: Development

Description: Analyze, refactor, and optimize existing code

Current Step: analyze_code

App Used: collab


Analysis Report: AI Technology Codebase (Simulated)

User Input Summary:

  • Description: Test run
  • Topic: AI Technology
  • Execution Time: 5 min (+100 cr)

Simulated Code Context:

For this analysis, we simulated a typical Python-based machine learning project focused on deep learning for an "AI Technology" application (e.g., an image classification model built with TensorFlow/PyTorch). The codebase includes modules for data loading, model definition, training loops, evaluation, and some utility functions.


1. Overview of Codebase Health

The simulated codebase demonstrates a functional deep learning pipeline. However, the analysis reveals several areas where maintainability, performance, and scalability could be significantly improved. The project appears to have evolved organically, leading to some inconsistencies and technical debt. Key findings point towards opportunities in streamlining data pipelines, enhancing model training efficiency, and improving overall code structure.


2. Simulated Code Quality Metrics

Below are simulated metrics that provide a quantitative snapshot of the codebase's current state. These metrics help identify areas of high complexity, potential bugs, and maintenance challenges.

| Metric | Value (Simulated) | Threshold (Good) | Interpretation |

| :----------------------- | :---------------- | :--------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

| Maintainability Index| 58/100 | >60 | Indicates moderate maintainability. Below the ideal threshold, suggesting that the code might be difficult to understand and modify without introducing new bugs. |

| Cyclomatic Complexity| 12 | <10 | Average complexity per function is slightly high, especially in data processing and model training functions. This increases the risk of bugs and makes testing challenging. |

| Code Duplication | 18% | <5% | Significant duplication detected, particularly in data preprocessing steps, model configuration, and utility functions across different modules. This inflates LOC and complicates updates. |

| Test Coverage | 45% | >80% | Low test coverage, particularly for core training logic and custom layers. This poses a significant risk for regressions during refactoring and new feature development. |

| Lines of Code (LOC) | 2,150 | N/A | Moderate size for an AI project. The issue is less about total LOC and more about the distribution and quality of the lines. |

| Technical Debt Ratio | 0.8 | <0.5 | For every 1 hour spent on development, approximately 0.8 hours are needed to fix existing issues or improve code quality. This suggests a growing backlog of technical debt. |


3. Identified Issues & Areas for Improvement

This section details specific findings categorized by impact area, providing actionable insights for the subsequent refactoring and optimization steps.

3.1. Performance Bottlenecks

  • Inefficient Data Loading:

* Finding: Data loading and augmentation operations are performed synchronously on the main thread, leading to CPU bottlenecks during GPU-intensive training. Custom Dataset implementations lack multiprocessing or prefetching capabilities.

* Impact: Significantly slows down training, underutilizes GPU resources.

* Example: A load_image_and_preprocess function is called sequentially within the training loop.

  • Suboptimal Model Inference:

* Finding: Inference functions for deployment often reload the entire model for each prediction or use CPU for operations that could be offloaded to GPU.

* Impact: High latency for predictions, inefficient resource usage.

* Example: A REST API endpoint reloads a .h5 model file for every incoming request.

  • Unoptimized Numerical Operations:

* Finding: Use of standard Python loops for tensor operations where vectorized NumPy or framework-specific (TensorFlow/PyTorch) operations would be significantly faster.

* Impact: Performance degradation for computationally heavy tasks.

* Example: Manual element-wise operations on image tensors instead of tf.math.multiply or torch.mul.

3.2. Code Readability & Maintainability

  • Lack of Consistent Styling & Docstrings:

* Finding: Inconsistent naming conventions (e.g., camelCase, snake_case mixed), absence of type hints, and sparse or outdated docstrings make understanding function purpose and arguments difficult.

* Impact: Increases cognitive load for developers, hinders onboarding, makes debugging harder.

* Example: A function trainModel has no docstring and mixes batch_size with learningRate.

  • Long & Complex Functions:

* Finding: Several functions, especially the main training loop and complex data preprocessing steps, exceed 100 lines of code and contain multiple levels of nested logic.

* Impact: High cyclomatic complexity, difficult to test, prone to errors.

* Example: A single train_epoch function handles data loading, forward pass, backward pass, metric calculation, and logging.

  • Magic Numbers & Hardcoded Values:

* Finding: Critical parameters (e.g., learning rates, batch sizes, image dimensions, number of epochs) are hardcoded directly within functions rather than being defined as constants or configuration parameters.

* Impact: Difficult to modify, error-prone when experimenting, reduces flexibility.

* Example: model.compile(optimizer=Adam(0.001)) or IMG_SIZE = 224 appearing directly in multiple files.

3.3. Scalability & Modularity

  • Tightly Coupled Components:

* Finding: Strong dependencies between data loaders, model architectures, and training utilities. Changes in one component often require modifications in multiple others.

* Impact: Limits reusability, makes independent testing difficult, slows down development.

* Example: The Model class directly imports and uses specific DataLoader functions, rather than accepting a generic data iterator.

  • Lack of Configuration Management:

* Finding: No centralized configuration system (e.g., YAML, JSON, Argparse). Parameters are scattered across scripts.

* Impact: Inconsistent experiments, difficult to reproduce results, manual effort for parameter tuning.

* Example: Running python train.py requires manual editing of parameters within the script.

  • Poorly Defined Module Boundaries:

* Finding: Utility functions are sometimes mixed with core logic, or related functionalities are spread across unrelated modules.

* Impact: Disorganized codebase, difficult to locate specific functionalities, encourages duplication.

* Example: Image utility functions are in model.py instead of a dedicated utils.py or data_helpers.py.

3.4. Error Handling & Robustness

  • Insufficient Error Handling:

* Finding: Critical operations (e.g., file I/O, external API calls, model loading) lack try-except blocks or provide generic error messages.

* Impact: Application crashes unexpectedly, difficult to diagnose issues in production.

* Example: A file reading operation for a dataset does not handle FileNotFoundError.

  • Lack of Input Validation:

* Finding: Functions do not validate input parameters, assuming correct types and ranges.

* Impact: Runtime errors, unexpected behavior, potential security vulnerabilities.

* Example: A function predict(image) doesn't check if image is a valid tensor or NumPy array.

3.5. AI/ML Specific Issues

  • Suboptimal Hyperparameter Management:

* Finding: Hyperparameters are hardcoded or manually adjusted, making systematic experimentation and tuning cumbersome. No clear mechanism for logging or comparing experimental runs.

* Impact: Limits ability to find optimal model configurations, difficulty in reproducing best results.

* Example: Manually changing LR = 0.0001 in a script for each new experiment.

  • Lack of Experiment Tracking:

* Finding: No integration with experiment tracking tools (e.g., MLflow, Weights & Biases, TensorBoard for full logging). Metrics, model checkpoints, and configurations are not systematically logged or versioned.

* Impact: Inability to compare models effectively, lost insights from previous runs, difficult to revert to previous best models.

  • Data Leakage Potential:

* Finding: Data preprocessing or augmentation steps might be applied before splitting into train/validation/test sets, leading to information leakage.

* Impact: Overestimated model performance, poor generalization to unseen data.

* Example: Normalization statistics are computed on the entire dataset before splitting.

  • Manual Device Management:

* Finding: Device (CPU/GPU) allocation is often hardcoded or manually managed, making the code less portable across different environments.

* Impact: Requires code changes for different hardware setups, potential for incorrect device usage.

* Example: model.to('cuda:0') or with tf.device('/GPU:0'): is explicitly written in multiple places without abstraction.


4. Preliminary Recommendations for Refactoring & Optimization

Based on the detailed analysis, the following high-level recommendations are provided to guide the next steps of the "Code Enhancement Suite":

  1. Introduce a Configuration Management System: Centralize all hyperparameters and system configurations (e.g., using Hydra, ConfigArgParse, or simple YAML files).
  2. Optimize Data Pipeline: Implement asynchronous data loading with multiprocessing/multi-threading (e.g., tf.data.Dataset.prefetch, PyTorch DataLoader with num_workers). Consider using data augmentation libraries optimized for GPU.
  3. Modularize & Decouple Components: Break down large functions into smaller, single-responsibility units. Establish clear interfaces between modules (e.g., data, model, training, evaluation, utilities).
  4. Enhance Code Readability: Enforce consistent coding style (e.g., using Black, Flake8), add comprehensive docstrings, and leverage type hints.
  5. Implement Robust Error Handling & Validation: Add try-except blocks for critical operations and input validation for public functions.
  6. Integrate Experiment Tracking: Adopt an experiment tracking tool (e.g., MLflow, Weights & Biases) to log metrics, parameters, and model artifacts systematically.
  7. Address Duplication: Identify and refactor duplicated code into reusable utility functions or classes.
  8. Improve Testing: Increase test coverage, especially for critical data processing, model logic, and custom layers.

5. Next Steps

This concludes the analyze_code step. The identified issues and preliminary recommendations will now feed into the subsequent stages of the "Code Enhancement Suite" workflow.

Next Step: Step 2/3: refactor_code and Step 3/3: optimize_code will leverage this analysis to generate specific refactoring suggestions and optimization strategies for the identified areas.


5. Detailed Refactoring Rationale

The collab AI applied the following refactoring principles and changes:

  1. Modularization (Single Responsibility Principle):

* The original function was broken down into several smaller, focused helper functions: _handle_missing_numerical, _handle_missing_categorical, _create_interaction_feature, _create_text_length_feature, and _min_max_scale.

* Each helper function now has a single, clear responsibility, making them easier to understand, test, and reuse independently.

* The main preprocess_data_for_ml_refactored function now acts as an orchestrator, calling these helper functions in a logical sequence.

  1. Improved Logging:

* Replaced print statements with Python's standard logging module. This provides more control over log levels (INFO, WARNING, DEBUG), output format, and destination (console, file).

* logging.info is used for successful operations, logging.warning for non-critical issues (e.g., column not found), and logging.debug for detailed internal checks.

  1. Enhanced Robustness and Error Handling:

* Added explicit checks within helper functions to ensure columns exist in the DataFrame (if col in df.columns). If a column is missing, a logging.warning is issued instead of potentially raising an error or silently failing.

* Added checks for df[col].isnull().any() before attempting fillna to avoid unnecessary operations and log more precisely.

* The _min_max_scale function explicitly handles cases where max_val == min_val to prevent division by zero, setting the column to 0.0.

  1. Readability and Clarity:

* Helper functions are prefixed with an underscore (_) to denote them as internal helpers, adhering to Python conventions.

* Type hints (df: pd.DataFrame, columns: list) were added to function signatures, improving code clarity and enabling static analysis tools.

* Docstrings were updated for all functions to clearly describe their purpose, arguments, and return values.

* Constants like DEFAULT_MISSING_CAT_VALUE were extracted into a PreprocessingConstants class, making them easily configurable and improving readability by replacing "magic strings".

  1. Configurability Enhancement:

* The config dictionary structure was slightly modified for feature engineering to allow for multiple interaction features and more flexible naming, demonstrating how a more robust configuration can be handled.

* The config.get() method is consistently used with a default empty list ([]) to safely handle cases where keys might be missing, preventing KeyError.

  1. Code Consistency:

* Consistent use of df.copy() at the beginning of the main function ensures that the original DataFrame remains unmodified, promoting immutability where appropriate and preventing unintended side effects.


6. Key Improvements Summary

| Feature | Original Code | Refactored Code (AI-Generated) | Benefit |

| :---------------------- | :---------------------------------------------- | :------------------------------------------------------------ | :----------------------------------------------------------------------- |

| Modularity | Single, long function performing multiple tasks | Broken into 5 specialized helper functions | Easier to read, test, debug, and reuse individual components. |

| Logging | print() statements for debugging | logging module (INFO, WARNING, DEBUG levels) | Professional, configurable output; separates logs from core logic. |

| Robustness | Basic checks for division by zero | Explicit column existence checks, isnull().any() checks | Prevents errors from missing columns, handles edge cases gracefully. |

| Readability | Monolithic block, magic strings | Helper functions, type hints, constants, improved docstrings | Clearer intent, easier to understand and maintain. |

| Configurability | Implicit feature names, fixed interaction | Flexible config for multiple interactions, customizable names | More adaptable to diverse ML pipeline requirements without code changes. |

| Maintainability | High coupling, hard to modify | Low coupling, high cohesion | Changes in one area are less likely to impact others. |


7. Recommendations for Integration

  1. Review and Validate: Carefully review the refactored code to ensure it aligns with your project's specific requirements and coding standards. While AI-generated, human oversight is crucial.
  2. Unit Testing: Implement or update unit tests for the preprocess_data_for_ml_refactored function and its helper functions. This is critical to verify that the refactored code maintains the exact same behavior as the original.
  3. Performance Benchmarking: If performance is critical, benchmark the refactored code against the original, especially if you have large datasets. While refactoring primarily targets quality, some structural changes might have minor performance implications (positive or negative).
  4. Configuration Update: Update your config dictionaries to match the new, more flexible structure expected by preprocess_data_for_ml_refactored.
  5. Logging Integration: Integrate the logging setup with your application's overall logging strategy to ensure consistent output and monitoring.

8. Next Steps

The next and final step in the "Code Enhancement Suite" workflow is code_review. In this phase, the refactored code will be presented for a final human review, focusing on best practices, potential further optimizations, and overall project alignment, before being marked as ready for integration.

Step 3: collab

Workflow Execution: Code Enhancement Suite - Step 3: ai_debug

Workflow Category: Development

Workflow Description: Analyze, refactor, and optimize existing code

User Input - Description: Test run

User Input - Topic: AI Technology

User Input - Execution Time: 5 min (+100 cr)

Current Step: ai_debug (Step 3 of 3)

Application: collab


AI Debugging Report: Identifying & Resolving Issues in AI Code

This report details the debugging phase for your AI Technology codebase within the "Code Enhancement Suite." Leveraging the collab environment, this step focuses on identifying potential bugs, performance bottlenecks, and logical inconsistencies typically found in AI/ML applications, especially after initial analysis and refactoring.

Given the "Test run" description and "AI Technology" topic, this report provides a structured approach to common AI debugging challenges, offering actionable strategies and specific recommendations.

1. Overview of Debugging Focus Areas for AI Technology

Debugging AI code often extends beyond traditional software debugging, encompassing data integrity, model behavior, and resource utilization. Our focus for this ai_debug step includes:

  • Data Pipeline Integrity: Ensuring data is correctly loaded, preprocessed, augmented, and batched without errors or unintended transformations.
  • Model Convergence & Stability: Diagnosing issues like exploding/vanishing gradients, slow convergence, or non-convergence during training.
  • Performance Bottlenecks: Identifying areas where training or inference speed is suboptimal, often related to data loading, GPU utilization, or inefficient model architecture.
  • Memory Management: Detecting memory leaks or excessive memory consumption, particularly critical in collab environments with limited resources.
  • Logical Errors in AI Algorithms: Pinpointing flaws in custom loss functions, model architectures, or training loops that lead to incorrect predictions or poor performance.
  • Reproducibility Issues: Ensuring that training runs produce consistent results across different executions or environments.

2. Identified Potential Issues & Diagnostic Strategies (Hypothetical)

While specific code was not provided for this "Test run," we outline common issues observed in AI projects and the corresponding debugging strategies.

| Potential Issue Category | Common Symptoms | Diagnostic Strategy & Tools |

| :------------------------------- | :---------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

| Data Preprocessing Anomalies | NaNs in input, unexpected distributions, poor model performance, shape mismatches. | Data Visualization: Histograms, scatter plots, correlation matrices of features (pre/post-processing). Data Validation: Assertions for expected ranges, types, and shapes. Small Data Set Test: Train/evaluate on a very small, known dataset to isolate data issues. |

| Model Training Instabilities | Loss exploding/stalling, NaNs in loss, very high/low accuracy, no convergence. | Gradient Monitoring: Use torch.autograd.grad or TensorFlow's tf.GradientTape to inspect gradients (magnitude, NaNs). Learning Rate Sweeps: Experiment with different learning rates. Batch Size Impact: Test different batch sizes. Loss Visualization: Plot training/validation loss and metrics over epochs. |

| Inference Performance Bottlenecks | Slow prediction times, underutilized GPU/CPU. | Profiling: Use cProfile (Python), PyTorch Profiler (torch.profiler), TensorFlow Profiler (tf.profiler). GPU Monitoring: nvidia-smi (if applicable in collab runtime). Batch Inference: Ensure efficient batching for inference. |

| Memory Leaks / Inefficiencies | OOM errors, increasing RAM/GPU memory usage over time. | Resource Monitoring: htop, nvidia-smi (if applicable), memory_profiler (Python). Garbage Collection: Explicitly del large tensors/objects and gc.collect(). Context Managers: Use torch.no_grad() or tf.GradientTape(persistent=False) where appropriate. |

| Hyperparameter Mismatches | Suboptimal performance, overfitting/underfitting. | Experiment Tracking: Use MLflow, Weights & Biases, or simple CSV logging to track hyperparameter combinations and results. Grid/Random Search: Systematically explore hyperparameter space. Validation Set Performance: Closely monitor performance on an independent validation set. |

| Reproducibility Issues | Different results on subsequent runs with same code/data. | Seed Management: Set random seeds for numpy, torch/tensorflow, random module, and potentially CUDA (torch.backends.cudnn.deterministic = True). Environment Control: Document exact library versions. |

3. Specific Recommendations for AI Technology Debugging in collab

Given the collab environment, here are tailored recommendations:

  1. Leverage collab's GPU/TPU Runtimes:

* Monitoring: Use !nvidia-smi (for GPU) or !gcloud compute tpus list (for TPU) in code cells to quickly check resource utilization.

* Memory Optimization: Be aggressive with del and torch.cuda.empty_cache() (for PyTorch) or tf.keras.backend.clear_session() (for TensorFlow) to free up memory between experiments or large data loading steps.

  1. Integrate Interactive Debugging:

* pdb: Use import pdb; pdb.set_trace() directly in your collab cells to step through code execution.

* ipdb: For an enhanced pdb experience in Jupyter/Colab notebooks, install !pip install ipdb and use %pdb on or from IPython.core.debugger import set_trace; set_trace().

  1. Visual Debugging with TensorBoard (or similar):

* Setup: Use !pip install tensorboard and then from torch.utils.tensorboard import SummaryWriter (PyTorch) or tf.summary.create_file_writer (TensorFlow).

* Logging: Log loss, metrics, images, model graphs, and even embedding projections.

* Launching: Run %load_ext tensorboard and then %tensorboard --logdir logs in a collab cell to view your experiment results interactively.

  1. Unit Testing for AI Components:

* While often overlooked in notebooks, consider writing small unit tests for critical components: custom layers, loss functions, data loaders, and preprocessing steps.

* Tools like pytest can be run directly in collab cells: !pytest your_test_file.py.

  1. Small-Scale Reproducibility Tests:

* Before scaling up, train your model on a tiny subset of your data (e.g., 10-100 samples) with fixed random seeds. This helps quickly verify if the model can overfit this small set, indicating basic functionality.

* If it fails to overfit, there's likely a fundamental bug in the model, loss, or training loop.

  1. Data Loader Validation:

* Iterate through your DataLoader (PyTorch) or tf.data.Dataset (TensorFlow) for a few batches and print shapes, data types, and min/max values. Visualize a few samples to ensure they look as expected. This catches common issues like incorrect augmentations or corrupted data.

4. Actionable Steps for the User

To effectively debug your AI code based on the insights from this ai_debug step:

  1. Prioritize Areas: Review the "Identified Potential Issues" table and determine which categories are most relevant to your current project's challenges.
  2. Implement Monitoring: Integrate TensorBoard or similar logging for key metrics (loss, accuracy, gradients, learning rate) to gain visibility into training dynamics.
  3. Execute Diagnostic Tests:

* Data Check: Run a comprehensive data validation script.

* Small Data Overfit Test: Attempt to overfit a tiny dataset to verify basic model and training loop integrity.

* Resource Profiling: Use nvidia-smi and/or torch.profiler/tf.profiler to identify performance bottlenecks.

  1. Interactive Debugging: When an issue is suspected, use pdb/ipdb to step through the code execution, especially around custom logic, loss calculations, and data flow.
  2. Iterative Refinement: Debugging is an iterative process. Apply fixes, re-run experiments, and continue monitoring until issues are resolved.

5. Next Steps & Collaboration

This ai_debug report provides a comprehensive framework for identifying and addressing issues in your AI codebase.

  • Further Assistance: If specific bugs or performance challenges persist after applying these strategies, provide detailed code snippets, error logs, and expected vs. actual behavior. This will allow for a more targeted analysis.
  • Documentation: Document any significant bugs found and their resolutions. This builds a valuable knowledge base for future development.
  • Continuous Integration: Consider integrating automated tests and checks into your CI/CD pipeline to catch regressions early.

This concludes the ai_debug step for your "Code Enhancement Suite" workflow. By systematically applying these debugging techniques, you can significantly improve the robustness, performance, and correctness of your AI Technology codebase.

code_enhancement_suite.txt
Download source file
Copy all content
Full output as text
Download ZIP
IDE-ready project ZIP
Copy share link
Permanent URL for this run
Get Embed Code
Embed this result on any website
Print / Save PDF
Use browser print dialog
\n\n\n"); var hasSrcMain=Object.keys(extracted).some(function(k){return k.indexOf("src/main")>=0;}); if(!hasSrcMain) zip.file(folder+"src/main."+ext,"import React from 'react'\nimport ReactDOM from 'react-dom/client'\nimport App from './App'\nimport './index.css'\n\nReactDOM.createRoot(document.getElementById('root')!).render(\n \n \n \n)\n"); var hasSrcApp=Object.keys(extracted).some(function(k){return k==="src/App."+ext||k==="App."+ext;}); if(!hasSrcApp) zip.file(folder+"src/App."+ext,"import React from 'react'\nimport './App.css'\n\nfunction App(){\n return(\n
\n
\n

"+slugTitle(pn)+"

\n

Built with PantheraHive BOS

\n
\n
\n )\n}\nexport default App\n"); zip.file(folder+"src/index.css","*{margin:0;padding:0;box-sizing:border-box}\nbody{font-family:system-ui,-apple-system,sans-serif;background:#f0f2f5;color:#1a1a2e}\n.app{min-height:100vh;display:flex;flex-direction:column}\n.app-header{flex:1;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:12px;padding:40px}\nh1{font-size:2.5rem;font-weight:700}\n"); zip.file(folder+"src/App.css",""); zip.file(folder+"src/components/.gitkeep",""); zip.file(folder+"src/pages/.gitkeep",""); zip.file(folder+"src/hooks/.gitkeep",""); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\nnpm install\nnpm run dev\n\`\`\`\n\n## Build\n\`\`\`bash\nnpm run build\n\`\`\`\n\n## Open in IDE\nOpen the project folder in VS Code or WebStorm.\n"); zip.file(folder+".gitignore","node_modules/\ndist/\n.env\n.DS_Store\n*.local\n"); } /* --- Vue (Vite + Composition API + TypeScript) --- */ function buildVue(zip,folder,app,code,panelTxt){ var pn=pkgName(app); var C=cc(pn); var extracted=extractCode(panelTxt); zip.file(folder+"package.json",'{\n "name": "'+pn+'",\n "version": "0.0.0",\n "type": "module",\n "scripts": {\n "dev": "vite",\n "build": "vue-tsc -b && vite build",\n "preview": "vite preview"\n },\n "dependencies": {\n "vue": "^3.5.13",\n "vue-router": "^4.4.5",\n "pinia": "^2.3.0",\n "axios": "^1.7.9"\n },\n "devDependencies": {\n "@vitejs/plugin-vue": "^5.2.1",\n "typescript": "~5.7.3",\n "vite": "^6.0.5",\n "vue-tsc": "^2.2.0"\n }\n}\n'); zip.file(folder+"vite.config.ts","import { defineConfig } from 'vite'\nimport vue from '@vitejs/plugin-vue'\nimport { resolve } from 'path'\n\nexport default defineConfig({\n plugins: [vue()],\n resolve: { alias: { '@': resolve(__dirname,'src') } }\n})\n"); zip.file(folder+"tsconfig.json",'{"files":[],"references":[{"path":"./tsconfig.app.json"},{"path":"./tsconfig.node.json"}]}\n'); zip.file(folder+"tsconfig.app.json",'{\n "compilerOptions":{\n "target":"ES2020","useDefineForClassFields":true,"module":"ESNext","lib":["ES2020","DOM","DOM.Iterable"],\n "skipLibCheck":true,"moduleResolution":"bundler","allowImportingTsExtensions":true,\n "isolatedModules":true,"moduleDetection":"force","noEmit":true,"jsxImportSource":"vue",\n "strict":true,"paths":{"@/*":["./src/*"]}\n },\n "include":["src/**/*.ts","src/**/*.d.ts","src/**/*.tsx","src/**/*.vue"]\n}\n'); zip.file(folder+"env.d.ts","/// \n"); zip.file(folder+"index.html","\n\n\n \n \n "+slugTitle(pn)+"\n\n\n
\n \n\n\n"); var hasMain=Object.keys(extracted).some(function(k){return k==="src/main.ts"||k==="main.ts";}); if(!hasMain) zip.file(folder+"src/main.ts","import { createApp } from 'vue'\nimport { createPinia } from 'pinia'\nimport App from './App.vue'\nimport './assets/main.css'\n\nconst app = createApp(App)\napp.use(createPinia())\napp.mount('#app')\n"); var hasApp=Object.keys(extracted).some(function(k){return k.indexOf("App.vue")>=0;}); if(!hasApp) zip.file(folder+"src/App.vue","\n\n\n\n\n"); zip.file(folder+"src/assets/main.css","*{margin:0;padding:0;box-sizing:border-box}body{font-family:system-ui,sans-serif;background:#fff;color:#213547}\n"); zip.file(folder+"src/components/.gitkeep",""); zip.file(folder+"src/views/.gitkeep",""); zip.file(folder+"src/stores/.gitkeep",""); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\nnpm install\nnpm run dev\n\`\`\`\n\n## Build\n\`\`\`bash\nnpm run build\n\`\`\`\n\nOpen in VS Code or WebStorm.\n"); zip.file(folder+".gitignore","node_modules/\ndist/\n.env\n.DS_Store\n*.local\n"); } /* --- Angular (v19 standalone) --- */ function buildAngular(zip,folder,app,code,panelTxt){ var pn=pkgName(app); var C=cc(pn); var sel=pn.replace(/_/g,"-"); var extracted=extractCode(panelTxt); zip.file(folder+"package.json",'{\n "name": "'+pn+'",\n "version": "0.0.0",\n "scripts": {\n "ng": "ng",\n "start": "ng serve",\n "build": "ng build",\n "test": "ng test"\n },\n "dependencies": {\n "@angular/animations": "^19.0.0",\n "@angular/common": "^19.0.0",\n "@angular/compiler": "^19.0.0",\n "@angular/core": "^19.0.0",\n "@angular/forms": "^19.0.0",\n "@angular/platform-browser": "^19.0.0",\n "@angular/platform-browser-dynamic": "^19.0.0",\n "@angular/router": "^19.0.0",\n "rxjs": "~7.8.0",\n "tslib": "^2.3.0",\n "zone.js": "~0.15.0"\n },\n "devDependencies": {\n "@angular-devkit/build-angular": "^19.0.0",\n "@angular/cli": "^19.0.0",\n "@angular/compiler-cli": "^19.0.0",\n "typescript": "~5.6.0"\n }\n}\n'); zip.file(folder+"angular.json",'{\n "$schema": "./node_modules/@angular/cli/lib/config/schema.json",\n "version": 1,\n "newProjectRoot": "projects",\n "projects": {\n "'+pn+'": {\n "projectType": "application",\n "root": "",\n "sourceRoot": "src",\n "prefix": "app",\n "architect": {\n "build": {\n "builder": "@angular-devkit/build-angular:application",\n "options": {\n "outputPath": "dist/'+pn+'",\n "index": "src/index.html",\n "browser": "src/main.ts",\n "tsConfig": "tsconfig.app.json",\n "styles": ["src/styles.css"],\n "scripts": []\n }\n },\n "serve": {"builder":"@angular-devkit/build-angular:dev-server","configurations":{"production":{"buildTarget":"'+pn+':build:production"},"development":{"buildTarget":"'+pn+':build:development"}},"defaultConfiguration":"development"}\n }\n }\n }\n}\n'); zip.file(folder+"tsconfig.json",'{\n "compileOnSave": false,\n "compilerOptions": {"baseUrl":"./","outDir":"./dist/out-tsc","forceConsistentCasingInFileNames":true,"strict":true,"noImplicitOverride":true,"noPropertyAccessFromIndexSignature":true,"noImplicitReturns":true,"noFallthroughCasesInSwitch":true,"paths":{"@/*":["src/*"]},"skipLibCheck":true,"esModuleInterop":true,"sourceMap":true,"declaration":false,"experimentalDecorators":true,"moduleResolution":"bundler","importHelpers":true,"target":"ES2022","module":"ES2022","useDefineForClassFields":false,"lib":["ES2022","dom"]},\n "references":[{"path":"./tsconfig.app.json"}]\n}\n'); zip.file(folder+"tsconfig.app.json",'{\n "extends":"./tsconfig.json",\n "compilerOptions":{"outDir":"./dist/out-tsc","types":[]},\n "files":["src/main.ts"],\n "include":["src/**/*.d.ts"]\n}\n'); zip.file(folder+"src/index.html","\n\n\n \n "+slugTitle(pn)+"\n \n \n \n\n\n \n\n\n"); zip.file(folder+"src/main.ts","import { bootstrapApplication } from '@angular/platform-browser';\nimport { appConfig } from './app/app.config';\nimport { AppComponent } from './app/app.component';\n\nbootstrapApplication(AppComponent, appConfig)\n .catch(err => console.error(err));\n"); zip.file(folder+"src/styles.css","* { margin: 0; padding: 0; box-sizing: border-box; }\nbody { font-family: system-ui, -apple-system, sans-serif; background: #f9fafb; color: #111827; }\n"); var hasComp=Object.keys(extracted).some(function(k){return k.indexOf("app.component")>=0;}); if(!hasComp){ zip.file(folder+"src/app/app.component.ts","import { Component } from '@angular/core';\nimport { RouterOutlet } from '@angular/router';\n\n@Component({\n selector: 'app-root',\n standalone: true,\n imports: [RouterOutlet],\n templateUrl: './app.component.html',\n styleUrl: './app.component.css'\n})\nexport class AppComponent {\n title = '"+pn+"';\n}\n"); zip.file(folder+"src/app/app.component.html","
\n
\n

"+slugTitle(pn)+"

\n

Built with PantheraHive BOS

\n
\n \n
\n"); zip.file(folder+"src/app/app.component.css",".app-header{display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:60vh;gap:16px}h1{font-size:2.5rem;font-weight:700;color:#6366f1}\n"); } zip.file(folder+"src/app/app.config.ts","import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';\nimport { provideRouter } from '@angular/router';\nimport { routes } from './app.routes';\n\nexport const appConfig: ApplicationConfig = {\n providers: [\n provideZoneChangeDetection({ eventCoalescing: true }),\n provideRouter(routes)\n ]\n};\n"); zip.file(folder+"src/app/app.routes.ts","import { Routes } from '@angular/router';\n\nexport const routes: Routes = [];\n"); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\nnpm install\nng serve\n# or: npm start\n\`\`\`\n\n## Build\n\`\`\`bash\nng build\n\`\`\`\n\nOpen in VS Code with Angular Language Service extension.\n"); zip.file(folder+".gitignore","node_modules/\ndist/\n.env\n.DS_Store\n*.local\n.angular/\n"); } /* --- Python --- */ function buildPython(zip,folder,app,code){ var title=slugTitle(app); var pn=pkgName(app); var src=code.replace(/^\`\`\`[\w]*\n?/m,"").replace(/\n?\`\`\`$/m,"").trim(); var reqMap={"numpy":"numpy","pandas":"pandas","sklearn":"scikit-learn","tensorflow":"tensorflow","torch":"torch","flask":"flask","fastapi":"fastapi","uvicorn":"uvicorn","requests":"requests","sqlalchemy":"sqlalchemy","pydantic":"pydantic","dotenv":"python-dotenv","PIL":"Pillow","cv2":"opencv-python","matplotlib":"matplotlib","seaborn":"seaborn","scipy":"scipy"}; var reqs=[]; Object.keys(reqMap).forEach(function(k){if(src.indexOf("import "+k)>=0||src.indexOf("from "+k)>=0)reqs.push(reqMap[k]);}); var reqsTxt=reqs.length?reqs.join("\n"):"# add dependencies here\n"; zip.file(folder+"main.py",src||"# "+title+"\n# Generated by PantheraHive BOS\n\nprint(title+\" loaded\")\n"); zip.file(folder+"requirements.txt",reqsTxt); zip.file(folder+".env.example","# Environment variables\n"); zip.file(folder+"README.md","# "+title+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\npython3 -m venv .venv\nsource .venv/bin/activate\npip install -r requirements.txt\n\`\`\`\n\n## Run\n\`\`\`bash\npython main.py\n\`\`\`\n"); zip.file(folder+".gitignore",".venv/\n__pycache__/\n*.pyc\n.env\n.DS_Store\n"); } /* --- Node.js --- */ function buildNode(zip,folder,app,code){ var title=slugTitle(app); var pn=pkgName(app); var src=code.replace(/^\`\`\`[\w]*\n?/m,"").replace(/\n?\`\`\`$/m,"").trim(); var depMap={"mongoose":"^8.0.0","dotenv":"^16.4.5","axios":"^1.7.9","cors":"^2.8.5","bcryptjs":"^2.4.3","jsonwebtoken":"^9.0.2","socket.io":"^4.7.4","uuid":"^9.0.1","zod":"^3.22.4","express":"^4.18.2"}; var deps={}; Object.keys(depMap).forEach(function(k){if(src.indexOf(k)>=0)deps[k]=depMap[k];}); if(!deps["express"])deps["express"]="^4.18.2"; var pkgJson=JSON.stringify({"name":pn,"version":"1.0.0","main":"src/index.js","scripts":{"start":"node src/index.js","dev":"nodemon src/index.js"},"dependencies":deps,"devDependencies":{"nodemon":"^3.0.3"}},null,2)+"\n"; zip.file(folder+"package.json",pkgJson); var fallback="const express=require(\"express\");\nconst app=express();\napp.use(express.json());\n\napp.get(\"/\",(req,res)=>{\n res.json({message:\""+title+" API\"});\n});\n\nconst PORT=process.env.PORT||3000;\napp.listen(PORT,()=>console.log(\"Server on port \"+PORT));\n"; zip.file(folder+"src/index.js",src||fallback); zip.file(folder+".env.example","PORT=3000\n"); zip.file(folder+".gitignore","node_modules/\n.env\n.DS_Store\n"); zip.file(folder+"README.md","# "+title+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\nnpm install\n\`\`\`\n\n## Run\n\`\`\`bash\nnpm run dev\n\`\`\`\n"); } /* --- Vanilla HTML --- */ function buildVanillaHtml(zip,folder,app,code){ var title=slugTitle(app); var isFullDoc=code.trim().toLowerCase().indexOf("=0||code.trim().toLowerCase().indexOf("=0; var indexHtml=isFullDoc?code:"\n\n\n\n\n"+title+"\n\n\n\n"+code+"\n\n\n\n"; zip.file(folder+"index.html",indexHtml); zip.file(folder+"style.css","/* "+title+" — styles */\n*{margin:0;padding:0;box-sizing:border-box}\nbody{font-family:system-ui,-apple-system,sans-serif;background:#fff;color:#1a1a2e}\n"); zip.file(folder+"script.js","/* "+title+" — scripts */\n"); zip.file(folder+"assets/.gitkeep",""); zip.file(folder+"README.md","# "+title+"\n\nGenerated by PantheraHive BOS.\n\n## Open\nDouble-click \`index.html\` in your browser.\n\nOr serve locally:\n\`\`\`bash\nnpx serve .\n# or\npython3 -m http.server 3000\n\`\`\`\n"); zip.file(folder+".gitignore",".DS_Store\nnode_modules/\n.env\n"); } /* ===== MAIN ===== */ var sc=document.createElement("script"); sc.src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"; sc.onerror=function(){ if(lbl)lbl.textContent="Download ZIP"; alert("JSZip load failed — check connection."); }; sc.onload=function(){ var zip=new JSZip(); var base=(_phFname||"output").replace(/\.[^.]+$/,""); var app=base.toLowerCase().replace(/[^a-z0-9]+/g,"_").replace(/^_+|_+$/g,"")||"my_app"; var folder=app+"/"; var vc=document.getElementById("panel-content"); var panelTxt=vc?(vc.innerText||vc.textContent||""):""; var lang=detectLang(_phCode,panelTxt); if(_phIsHtml){ buildVanillaHtml(zip,folder,app,_phCode); } else if(lang==="flutter"){ buildFlutter(zip,folder,app,_phCode,panelTxt); } else if(lang==="react-native"){ buildReactNative(zip,folder,app,_phCode,panelTxt); } else if(lang==="swift"){ buildSwift(zip,folder,app,_phCode,panelTxt); } else if(lang==="kotlin"){ buildKotlin(zip,folder,app,_phCode,panelTxt); } else if(lang==="react"){ buildReact(zip,folder,app,_phCode,panelTxt); } else if(lang==="vue"){ buildVue(zip,folder,app,_phCode,panelTxt); } else if(lang==="angular"){ buildAngular(zip,folder,app,_phCode,panelTxt); } else if(lang==="python"){ buildPython(zip,folder,app,_phCode); } else if(lang==="node"){ buildNode(zip,folder,app,_phCode); } else { /* Document/content workflow */ var title=app.replace(/_/g," "); var md=_phAll||_phCode||panelTxt||"No content"; zip.file(folder+app+".md",md); var h=""+title+""; h+="

"+title+"

"; var hc=md.replace(/&/g,"&").replace(//g,">"); hc=hc.replace(/^### (.+)$/gm,"

$1

"); hc=hc.replace(/^## (.+)$/gm,"

$1

"); hc=hc.replace(/^# (.+)$/gm,"

$1

"); hc=hc.replace(/\*\*(.+?)\*\*/g,"$1"); hc=hc.replace(/\n{2,}/g,"

"); h+="

"+hc+"

Generated by PantheraHive BOS
"; zip.file(folder+app+".html",h); zip.file(folder+"README.md","# "+title+"\n\nGenerated by PantheraHive BOS.\n\nFiles:\n- "+app+".md (Markdown)\n- "+app+".html (styled HTML)\n"); } zip.generateAsync({type:"blob"}).then(function(blob){ var a=document.createElement("a"); a.href=URL.createObjectURL(blob); a.download=app+".zip"; a.click(); URL.revokeObjectURL(a.href); if(lbl)lbl.textContent="Download ZIP"; }); }; document.head.appendChild(sc); } function phShare(){navigator.clipboard.writeText(window.location.href).then(function(){var el=document.getElementById("ph-share-lbl");if(el){el.textContent="Link copied!";setTimeout(function(){el.textContent="Copy share link";},2500);}});}function phEmbed(){var runId=window.location.pathname.split("/").pop().replace(".html","");var embedUrl="https://pantherahive.com/embed/"+runId;var code='';navigator.clipboard.writeText(code).then(function(){var el=document.getElementById("ph-embed-lbl");if(el){el.textContent="Embed code copied!";setTimeout(function(){el.textContent="Get Embed Code";},2500);}});}