Aplikasi Go yang menyediakan interface WebSocket untuk berkomunikasi dengan Google Vertex AI. Aplikasi ini mendukung input teks dan audio real-time dengan Text-to-Speech (TTS) response.
Aplikasi menggunakan Proxy Pattern dengan elemen MVC (Model-View-Controller):
- Model: WebSocket proxy ke Google Vertex AI
- View: Interface web dengan HTML/CSS/JavaScript
- Controller: WebSocket handlers yang mengatur komunikasi
- Real-time WebSocket Communication dengan Google Vertex AI
- Audio Input Support - Recording dan streaming audio ke AI
- Text-to-Speech Response - AI response dalam format audio
- Multi-language Support - Bahasa Inggris dan Indonesia
- Stock API Integration - Integrasi dengan Finnhub API
- Weather API Integration - Integrasi dengan OpenWeatherMap API
- Modern Web Interface - Responsive dan user-friendly
websocket-ai/
βββ cmd/ # Entry points aplikasi
β βββ ai-gateway/ # WebSocket proxy ke Vertex AI (Port 8081)
β β βββ main.go
β β βββ ai-gateway.exe # Compiled binary
β βββ static-server/ # Static file server (Port 8080)
β βββ main.go
β βββ static-server.exe # Compiled binary
βββ internal/ # Internal packages (business logic)
β βββ gateway/ # AI gateway logic
β β βββ websocket.go # WebSocket handlers
β β βββ vertex_ai.go # Vertex AI integration
β β βββ config.go # Configuration management
β β βββ logger.go # Structured logging
β β βββ metrics.go # Performance metrics
β β βββ middleware.go # HTTP middleware
β βββ server/ # Static server logic
β β βββ static.go # CORS & static file handling
β βββ models/ # Data structures
β βββ message.go # WebSocket message models
βββ scripts/ # Python utilities & analytics
β βββ data_processor.py # Data processing & analytics
β βββ audio_processor.py # Audio processing utilities
β βββ run_analytics.py # Analytics runner
β βββ requirements.txt # Python dependencies
βββ web/ # Frontend files (minimal HTML)
β βββ templates/ # HTML templates
β β βββ index.html # Main interface (minimal)
β β βββ assets/ # Images dan assets
β βββ static/ # Separated CSS & JS
β βββ styles.css # Extracted CSS
β βββ app.js # Extracted JavaScript
β βββ tools/ # API tools
β βββ stock-api.js
β βββ weather-api.js
βββ run.bat # Windows runner script
βββ run.sh # Linux/Mac runner script
βββ go.mod
βββ go.sum
βββ README.md
- Go 1.24+ (sesuai dengan go.mod)
- Google Cloud Platform Account dengan Vertex AI API enabled
- Service Account dengan credentials untuk Vertex AI
- Browser modern dengan WebSocket dan Web Audio API support
-
Clone repository
git clone <repository-url> cd websocket-ai
-
Install dependencies
go mod download
-
Setup Google Cloud credentials
# Set environment variable untuk service account export GOOGLE_APPLICATION_CREDENTIALS="path/to/service-account.json"
-
Jalankan aplikasi
Cara Mudah (Recommended):
# Windows run.bat # Linux/Mac ./run.sh
Cara Manual:
Terminal 1 - Static File Server:
cd cmd/static-server go run main.goTerminal 2 - AI Gateway:
cd cmd/ai-gateway go run main.go -
Akses aplikasi
- Buka browser ke
http://localhost:8080 - WebSocket akan otomatis connect ke
ws://localhost:8081/ws
- Buka browser ke
- Buat project di Google Cloud Console
- Enable Vertex AI API
- Buat service account dengan role "Vertex AI User"
- Download JSON credentials
- Set environment variable
GOOGLE_APPLICATION_CREDENTIALS
Untuk fitur tambahan, set API keys di file JavaScript:
Stock API (Finnhub):
// templates/static/tools/stock-api.js
const FINNHUB_API_KEY = 'your-finnhub-api-key';Weather API (OpenWeatherMap):
// templates/static/tools/weather-api.js
const OPENWEATHER_API_KEY = 'your-openweather-api-key';- Text Chat: Ketik pesan dan tekan Enter atau klik Send
- Voice Chat: Klik Record untuk mulai recording, klik Stop untuk mengirim
- Real-time Response: AI akan merespons dengan teks dan audio
- Multi-modal: Support untuk text, audio, dan image input
GET /- Main interfaceWS /ws- WebSocket endpoint untuk AI communicationGET /static/*- Static files (CSS, JS, images)
{
"type": "text|audio|audio_end|image",
"content": "data atau text"
}{
"status": "success|streaming|fail",
"code": 200,
"response": "AI response text",
"audio": "base64-encoded-audio",
"partial": "streaming text"
}Sebelum (Struktur Lama):
websocket-ai/
βββ proxy/ # β Nama membingungkan
β βββ proxy.go # β Tidak jelas fungsinya
βββ server/ # β Nama membingungkan
β βββ server.go # β Tidak jelas fungsinya
βββ templates/ # β Struktur tidak terorganisir
Sesudah (Struktur Baru):
websocket-ai/
βββ cmd/ # β
Entry points yang jelas
β βββ ai-gateway/ # β
WebSocket proxy ke Vertex AI
β βββ static-server/ # β
Static file server
βββ internal/ # β
Internal packages
β βββ gateway/ # β
AI gateway logic
β βββ server/ # β
Static server logic
β βββ models/ # β
Data structures
βββ web/ # β
Frontend files
-
Penamaan Jelas:
ai-gatewayvsproxy- lebih jelas fungsinyastatic-servervsserver- lebih jelas fungsinya
-
Organisasi Lebih Baik:
cmd/untuk entry pointsinternal/untuk business logicweb/untuk frontend filesscripts/untuk Python utilities
-
Separation of Concerns:
- Gateway logic terpisah dari main
- Models terpisah dari business logic
- Static server logic terpisah
- CSS/JS terpisah dari HTML
-
Mudah Dikembangkan:
- Struktur mengikuti Go best practices
- Import paths yang jelas
- Package dependencies yang terorganisir
Masalah Sebelumnya:
- HTML file sangat besar (498 baris) karena CSS dan JavaScript inline
- Dominasi HTML 43.9% vs Go 21.5% dan Python 13.8%
Solusi yang Diterapkan:
-
π HTML Minimal:
- Dipisahkan CSS ke
web/static/styles.css - Dipisahkan JavaScript ke
web/static/app.js - HTML sekarang hanya 50 baris (90% lebih kecil!)
- Dipisahkan CSS ke
-
π§ Fitur Go Tambahan:
config.go- Configuration managementlogger.go- Structured loggingmetrics.go- Performance metricsmiddleware.go- HTTP middleware- Menambah kompleksitas Go untuk meningkatkan persentase
-
π Script Python:
data_processor.py- Analytics dan data processingaudio_processor.py- Audio processing utilitiesrun_analytics.py- Analytics runner- Menambah persentase Python
Hasil Optimisasi:
- β HTML berkurang drastis (dari 498 β 50 baris)
- β Go code bertambah signifikan (config, logging, metrics, middleware)
- β Python scripts ditambahkan untuk analytics dan audio processing
- β Struktur lebih modular dan maintainable
Aplikasi menyediakan script runner untuk kemudahan:
- Windows:
run.bat- Double-click untuk menjalankan kedua server - Linux/Mac:
run.sh-./run.shuntuk menjalankan kedua server
Script ini akan:
- Menjalankan Static File Server di port 8080
- Menjalankan AI Gateway di port 8081
- Menampilkan URL akses aplikasi
Setup Python Environment:
cd scripts
pip install -r requirements.txtJalankan Analytics:
python run_analytics.pyAudio Processing:
python audio_processor.pyData Processing:
python data_processor.pyApache License 2.0 - Lihat file LICENSE untuk detail lengkap.
- Fork repository
- Buat feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push ke branch (
git push origin feature/amazing-feature) - Buat Pull Request