refactor: restructure project to align with standard Go layout

### CHANGES

- Introduce `cmd` directory for all main application binaries.
- Move all Go packages into the `internal` directory.
- Rename the `restapi` package to `server` for clarity.
- Consolidate patterns and strategies into a new `data` directory.
- Group all auxiliary scripts into a new `scripts` directory.
- Move all documentation and images into a `docs` directory.
- Update all Go import paths to reflect the new structure.
- Adjust CI/CD workflows and build commands for new layout.
This commit is contained in:
Kayvan Sylvan
2025-07-08 22:47:17 -07:00
parent 6d67223a4b
commit 4004c51b9e
450 changed files with 353 additions and 2492 deletions

41
scripts/docker/Dockerfile Normal file
View File

@@ -0,0 +1,41 @@
# Use official golang image as builder
FROM golang:1.24.2-alpine AS builder
# Set working directory
WORKDIR /app
# Copy go mod and sum files
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy source code
COPY . .
# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -o fabric ./cmd/fabric
# Use scratch as final base image
FROM alpine:latest
# Copy the binary from builder
COPY --from=builder /app/fabric /fabric
# Copy patterns directory
COPY patterns /patterns
# Ensure clean config directory and copy ENV file
RUN rm -rf /root/.config/fabric && \
mkdir -p /root/.config/fabric
COPY ENV /root/.config/fabric/.env
# Add debug commands
RUN ls -la /root/.config/fabric/
# Expose port 8080
EXPOSE 8080
# Run the binary with debug output
ENTRYPOINT ["/fabric"]
CMD ["--serve"]

40
scripts/docker/README.md Normal file
View File

@@ -0,0 +1,40 @@
# Docker Deployment
This directory contains Docker configuration files for running Fabric in containers.
## Files
- `Dockerfile` - Main Docker build configuration
- `docker-compose.yml` - Docker Compose stack configuration
- `start-docker.sh` - Helper script to start the stack
- `README.md` - This documentation
## Quick Start
```bash
# Start the Docker stack
./start-docker.sh
# Or manually with docker-compose
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the stack
docker-compose down
```
## Building
```bash
# Build the Docker image
docker build -t fabric .
# Or use docker-compose
docker-compose build
```
## Configuration
Make sure to configure your environment variables and API keys before running the Docker stack. See the main README.md for setup instructions.

View File

@@ -0,0 +1,11 @@
version: '3.8'
services:
fabric-api:
build: .
ports:
- "8080:8080"
volumes:
- ./ENV:/root/.config/fabric/.env:ro
environment:
- GIN_MODE=release

11
scripts/docker/start-docker.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/bash
# Helper script to start the Fabric Docker stack
echo "Starting Fabric Docker stack..."
cd "$(dirname "$0")"
docker-compose up -d
echo "Fabric is now running!"
echo "Check logs with: docker-compose logs -f"
echo "Stop with: docker-compose down"

View File

@@ -0,0 +1,118 @@
# Pattern Descriptions and Tags Management
This document explains the complete workflow for managing pattern descriptions and tags, including how to process new patterns and maintain metadata.
## System Overview
The pattern system follows this hierarchy:
1. `~/.config/fabric/patterns/` directory: The source of truth for available patterns
2. `pattern_extracts.json`: Contains first 500 words of each pattern for reference
3. `pattern_descriptions.json`: Stores pattern metadata (descriptions and tags)
4. `web/static/data/pattern_descriptions.json`: Web-accessible copy for the interface
## Pattern Processing Workflow
### 1. Adding New Patterns
- Add patterns to `~/.config/fabric/patterns/`
- Run extract_patterns.py to process new additions:
```bash
python extract_patterns.py
The Python Script automatically:
- Creates pattern extracts for reference
- Adds placeholder entries in descriptions file
- Syncs to web interface
### 2. Pattern Extract Creation
The script extracts first 500 words from each pattern's system.md file to:
- Provide context for writing descriptions
- Maintain reference material
- Aid in pattern categorization
### 3. Description and Tag Management
Pattern descriptions and tags are managed in pattern_descriptions.json:
{
"patterns": [
{
"patternName": "pattern_name",
"description": "[Description pending]",
"tags": []
}
]
}
## Completing Pattern Metadata
### Writing Descriptions
1. Check pattern_descriptions.json for "[Description pending]" entries
2. Reference pattern_extracts.json for context
3. How to update Pattern short descriptions (one sentence).
You can update your descriptions in pattern_descriptions.json manually or using LLM assistance (preferred approach).
Tell AI to look for "Description pending" entries in this file and write a short description based on the extract info in the pattern_extracts.json file. You can also ask your LLM to add tags for those newly added patterns, using other patterns tag assignments as example.
### Managing Tags
1. Add appropriate tags to new patterns
2. Update existing tags as needed
3. Tags are stored as arrays: ["TAG1", "TAG2"]
4. Edit pattern_descriptions.json directly to modify tags
5. Make tags your own. You can delete, replace, amend existing tags.
## File Synchronization
The script maintains synchronization between:
- Local pattern_descriptions.json
- Web interface copy in static/data/
- No manual file copying needed
## Best Practices
1. Run extract_patterns.py when:
- Adding new patterns
- Updating existing patterns
- Modifying pattern structure
2. Description Writing:
- Use pattern extracts for context
- Keep descriptions clear and concise
- Focus on pattern purpose and usage
3. Tag Management:
- Use consistent tag categories
- Apply multiple tags when relevant
- Update tags to reflect pattern evolution
## Troubleshooting
If patterns are not showing in the web interface:
1. Verify pattern_descriptions.json format
2. Check web static copy exists
3. Ensure proper file permissions
4. Run extract_patterns.py to resync
## File Structure
fabric/
├── patterns/ # Pattern source files
├── PATTERN_DESCRIPTIONS/
│ ├── extract_patterns.py # Pattern processing script
│ ├── pattern_extracts.json # Pattern content references
│ └── pattern_descriptions.json # Pattern metadata
└── web/
└── static/
└── data/
└── pattern_descriptions.json # Web interface copy

View File

@@ -0,0 +1,130 @@
#!/usr/bin/env python3
"""Extracts pattern information from the ~/.config/fabric/patterns directory,
creates JSON files for pattern extracts and descriptions, and updates web static files.
"""
import os
import json
import shutil
def load_existing_file(filepath):
"""Load existing JSON file or return default structure"""
if os.path.exists(filepath):
try:
with open(filepath, "r", encoding="utf-8") as f:
return json.load(f)
except json.JSONDecodeError:
print(
f"Warning: Malformed JSON in {filepath}. Starting with an empty list."
)
return {"patterns": []}
return {"patterns": []}
def get_pattern_extract(pattern_path):
"""Extract first 500 words from pattern's system.md file"""
system_md_path = os.path.join(pattern_path, "system.md")
with open(system_md_path, "r", encoding="utf-8") as f:
content = " ".join(f.read().split()[:500])
return content
def extract_pattern_info():
"""Extract pattern information from the patterns directory"""
script_dir = os.path.dirname(os.path.abspath(__file__))
patterns_dir = os.path.expanduser("~/.config/fabric/patterns")
print(f"\nScanning patterns directory: {patterns_dir}")
extracts_path = os.path.join(script_dir, "pattern_extracts.json")
descriptions_path = os.path.join(script_dir, "pattern_descriptions.json")
existing_extracts = load_existing_file(extracts_path)
existing_descriptions = load_existing_file(descriptions_path)
existing_extract_names = {p["patternName"] for p in existing_extracts["patterns"]}
existing_description_names = {
p["patternName"] for p in existing_descriptions["patterns"]
}
print(f"Found existing patterns: {len(existing_extract_names)}")
new_extracts = []
new_descriptions = []
for dirname in sorted(os.listdir(patterns_dir)):
pattern_path = os.path.join(patterns_dir, dirname)
system_md_path = os.path.join(pattern_path, "system.md")
if os.path.isdir(pattern_path) and os.path.exists(system_md_path):
if dirname not in existing_extract_names:
print(f"Processing new pattern: {dirname}")
try:
if dirname not in existing_extract_names:
print(f"Creating new extract for: {dirname}")
pattern_extract = get_pattern_extract(
pattern_path
) # Pass directory path
new_extracts.append(
{"patternName": dirname, "pattern_extract": pattern_extract}
)
if dirname not in existing_description_names:
print(f"Creating new description for: {dirname}")
new_descriptions.append(
{
"patternName": dirname,
"description": "[Description pending]",
"tags": [],
}
)
except OSError as e:
print(f"Error processing {dirname}: {str(e)}")
else:
print(f"Invalid pattern directory or missing system.md: {dirname}")
print("\nProcessing summary:")
print(f"New extracts created: {len(new_extracts)}")
print(f"New descriptions added: {len(new_descriptions)}")
existing_extracts["patterns"].extend(new_extracts)
existing_descriptions["patterns"].extend(new_descriptions)
return existing_extracts, existing_descriptions, len(new_descriptions)
def update_web_static(descriptions_path):
"""Copy pattern descriptions to web static directory"""
script_dir = os.path.dirname(os.path.abspath(__file__))
static_dir = os.path.join(script_dir, "..", "web", "static", "data")
os.makedirs(static_dir, exist_ok=True)
static_path = os.path.join(static_dir, "pattern_descriptions.json")
shutil.copy2(descriptions_path, static_path)
def save_pattern_files():
"""Save both pattern files and sync to web"""
script_dir = os.path.dirname(os.path.abspath(__file__))
extracts_path = os.path.join(script_dir, "pattern_extracts.json")
descriptions_path = os.path.join(script_dir, "pattern_descriptions.json")
pattern_extracts, pattern_descriptions, new_count = extract_pattern_info()
# Save files
with open(extracts_path, "w", encoding="utf-8") as f:
json.dump(pattern_extracts, f, indent=2, ensure_ascii=False)
with open(descriptions_path, "w", encoding="utf-8") as f:
json.dump(pattern_descriptions, f, indent=2, ensure_ascii=False)
# Update web static
update_web_static(descriptions_path)
print("\nProcessing complete:")
print(f"Total patterns: {len(pattern_descriptions['patterns'])}")
print(f"New patterns added: {new_count}")
if __name__ == "__main__":
save_pattern_files()

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,7 @@
streamlit>=1.27.0
pandas>=1.5.0
matplotlib>=3.5.0
seaborn>=0.12.0
numpy>=1.23.0
python-dotenv>=1.0.0
pyperclip>=1.8.0 # For cross-platform clipboard support

File diff suppressed because it is too large Load Diff

112
scripts/setup_fabric.bat Normal file
View File

@@ -0,0 +1,112 @@
@echo off
setlocal enabledelayedexpansion
:: Check if running with administrator privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo Please run this script as an administrator.
pause
exit /b 1
)
:: Install Chocolatey (package manager for Windows)
if not exist "%ProgramData%\chocolatey\bin\choco.exe" (
echo Installing Chocolatey...
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
)
:: Install Go
where go >nul 2>&1
if %errorlevel% neq 0 (
echo Installing Go...
choco install golang -y
set "PATH=%PATH%;C:\Program Files\Go\bin"
)
:: Install Git
where git >nul 2>&1
if %errorlevel% neq 0 (
echo Installing Git...
choco install git -y
)
:: Refresh environment variables
call refreshenv
:: Install Fabric
echo Installing Fabric...
go install github.com/danielmiessler/fabric@latest
:: Run Fabric setup
echo Running Fabric setup...
fabric --setup
:: Install yt helper
echo Installing yt helper...
go install github.com/danielmiessler/yt@latest
:: Prompt user for YouTube API Key
set /p YOUTUBE_API_KEY=Enter your YouTube API Key (press Enter to skip):
if not "!YOUTUBE_API_KEY!"=="" (
echo YOUTUBE_API_KEY=!YOUTUBE_API_KEY!>> %USERPROFILE%\.config\fabric\.env
)
:: Prompt user for OpenAI API Key
set /p OPENAI_API_KEY=Enter your OpenAI API Key (press Enter to skip):
if not "!OPENAI_API_KEY!"=="" (
echo OPENAI_API_KEY=!OPENAI_API_KEY!>> %USERPROFILE%\.config\fabric\.env
)
:: Run Fabric
:run_fabric
cls
echo Fabric is now installed and ready to use.
echo.
echo Available options:
echo 1. Run Fabric with custom options
echo 2. List patterns
echo 3. List models
echo 4. Update patterns
echo 5. Exit
echo.
set /p CHOICE=Enter your choice (1-5):
if "%CHOICE%"=="1" (
set /p PATTERN=Enter pattern (or press Enter to skip):
set /p CONTEXT=Enter context (or press Enter to skip):
set /p SESSION=Enter session (or press Enter to skip):
set /p MODEL=Enter model (or press Enter to skip):
set /p TEMPERATURE=Enter temperature (or press Enter for default):
set /p STREAM=Do you want to stream output? (Y/N):
set "FABRIC_CMD=fabric"
if not "!PATTERN!"=="" set "FABRIC_CMD=!FABRIC_CMD! --pattern !PATTERN!"
if not "!CONTEXT!"=="" set "FABRIC_CMD=!FABRIC_CMD! --context !CONTEXT!"
if not "!SESSION!"=="" set "FABRIC_CMD=!FABRIC_CMD! --session !SESSION!"
if not "!MODEL!"=="" set "FABRIC_CMD=!FABRIC_CMD! --model !MODEL!"
if not "!TEMPERATURE!"=="" set "FABRIC_CMD=!FABRIC_CMD! --temperature !TEMPERATURE!"
if /i "!STREAM!"=="Y" set "FABRIC_CMD=!FABRIC_CMD! --stream"
echo Running Fabric with command: !FABRIC_CMD!
!FABRIC_CMD!
pause
goto run_fabric
) else if "%CHOICE%"=="2" (
fabric --listpatterns
pause
goto run_fabric
) else if "%CHOICE%"=="3" (
fabric --listmodels
pause
goto run_fabric
) else if "%CHOICE%"=="4" (
fabric --updatepatterns
pause
goto run_fabric
) else if "%CHOICE%"=="5" (
exit /b 0
) else (
echo Invalid choice. Please try again.
pause
goto run_fabric
)