Compare commits

...

27 Commits
bun ... v0.0.2

Author SHA1 Message Date
jango-blockchained
54112c9059 chore: manage bun.lockb in Docker and project configuration
- Update .dockerignore to exclude bun.lockb
- Modify .gitignore to track bun.lockb
- Update Dockerfile to remove bun.lockb copy step
- Add bun.lockb binary lockfile to the repository
2025-02-04 15:08:15 +01:00
jango-blockchained
1f79feeccc chore: update GitHub Actions workflow permissions
- Expand workflow permissions to include write access for contents and pull requests
- Ensure comprehensive access for automated CI/CD processes
2025-02-04 15:02:39 +01:00
jango-blockchained
63fd21053c chore: update GitHub Actions workflow for automated versioning and release management
- Replace manual version detection with GitHub Tag Action
- Implement automatic version bumping and tagging
- Add GitHub Release creation with changelog generation
- Simplify Docker image tagging using new version workflow
2025-02-04 15:00:40 +01:00
jango-blockchained
5f078ff227 docs: update environment variable naming from NODE_ENV to BUN_ENV 2025-02-04 14:56:22 +01:00
jango-blockchained
5d0c2f54a2 chore: remove bun.lockb from .dockerignore 2025-02-04 14:23:12 +01:00
jango-blockchained
02284c787b docs: add contributing guidelines and project roadmap
- Create CONTRIBUTING.md with comprehensive guidelines for community contributions
- Develop ROADMAP.md outlining near-term, mid-term, and long-term project goals
- Provide clear instructions for code style, testing, and community engagement
- Define project vision and future enhancement strategies
2025-02-04 04:28:26 +01:00
jango-blockchained
3e97357561 docs: update documentation to use Bun commands and scripts
- Replace npm commands with equivalent Bun commands in GETTING_STARTED.md
- Update TESTING.md to reflect Bun-specific test and development scripts
- Simplify installation, development, and testing instructions
2025-02-04 04:17:58 +01:00
jango-blockchained
cb897d4cf6 docs: add comprehensive architecture documentation for MCP Server
- Create ARCHITECTURE.md with detailed system design overview
- Describe key architectural components including Bun runtime, SSE communication, and modular design
- Highlight security, performance, and scalability features
- Outline future enhancement roadmap
- Provide insights into system design principles and technology choices
2025-02-04 04:14:43 +01:00
jango-blockchained
08e408d68d test: enhance security module with comprehensive token validation and rate limiting tests
- Expanded TokenManager test suite with advanced token encryption and decryption scenarios
- Added detailed rate limiting tests with IP-based tracking and window-based expiration
- Improved test coverage for token validation, tampering detection, and error handling
- Implemented mock configurations for faster test execution
- Enhanced security test scenarios with unique IP addresses and edge case handling
2025-02-04 04:09:40 +01:00
jango-blockchained
1e3bf07547 chore: update TypeScript configuration for enhanced type support and Bun compatibility
- Switch moduleResolution to "bundler" for improved module handling
- Add type definitions for Node.js, WebSocket, JWT, and sanitize-html
- Enable experimental decorators and decorator metadata
- Expand include paths to support additional type definition files
- Maintain existing project structure and path aliases
2025-02-04 03:33:08 +01:00
jango-blockchained
e503da1dfd chore: optimize Bun and TypeScript configuration for improved development workflow
- Update bunfig.toml with environment-specific settings and performance optimizations
- Enhance package.json scripts with additional development and maintenance commands
- Refactor tsconfig.json for better Bun and TypeScript compatibility
- Add hot reloading, profiling, and type checking configurations
- Improve build and development scripts with minification and frozen lockfile options
2025-02-04 03:25:46 +01:00
jango-blockchained
790a37e49f refactor: migrate to Elysia and enhance security middleware
- Replaced Express with Elysia for improved performance and type safety
- Integrated Elysia middleware for rate limiting, security headers, and request validation
- Refactored security utilities to work with Elysia's context and request handling
- Updated token management and validation logic
- Added comprehensive security headers and input sanitization
- Simplified server initialization and error handling
- Updated documentation with new setup and configuration details
2025-02-04 03:09:35 +01:00
jango-blockchained
bc1dc8278a refactor: optimize configuration and tool implementations
- Standardized error handling across tool implementations
- Improved return type consistency for tool execution results
- Simplified configuration parsing and type definitions
- Enhanced type safety for various configuration schemas
- Cleaned up and normalized tool response structures
- Updated SSE and event subscription tool implementations
2025-02-04 00:56:45 +01:00
jango-blockchained
9a02bdaf11 feat: add Bun polyfills and refactor LiteMCP implementation
- Introduced polyfills for Node.js compatibility in Bun runtime
- Moved LiteMCP implementation to a dedicated module
- Updated package.json to include @digital-alchemy/hass dependency
- Refactored import for LiteMCP to use local module path
2025-02-03 22:55:36 +01:00
jango-blockchained
04123a5740 test: enhance security middleware and token validation tests
- Refactored security middleware tests with improved type safety and mock configurations
- Updated token validation tests with more precise token generation and expiration scenarios
- Improved input sanitization and request validation test coverage
- Added comprehensive test cases for error handling and security header configurations
- Enhanced test setup with better environment and secret management
2025-02-03 22:52:18 +01:00
jango-blockchained
e688c94718 chore: remove .cursor directory from git tracking 2025-02-03 22:51:39 +01:00
jango-blockchained
481dc5b1a8 chore: add Bun types and update TypeScript configuration for Bun runtime
- Added `bun-types` to package.json dev dependencies
- Updated tsconfig.json to include Bun types and test directory
- Updated README.md with correct author attribution
- Enhanced test configurations to support Bun testing environment
2025-02-03 22:41:22 +01:00
jango-blockchained
c519d250a1 chore: add .bun/ to .gitignore for Bun runtime configuration 2025-02-03 22:40:48 +01:00
jango-blockchained
10bf5919e4 refactor: enhance middleware and security with advanced protection mechanisms
- Upgraded rate limiter configuration with more granular control and detailed headers
- Improved authentication middleware with enhanced token validation and error responses
- Implemented advanced input sanitization using sanitize-html with comprehensive XSS protection
- Replaced manual security headers with helmet for robust web security configuration
- Enhanced error handling middleware with more detailed logging and specific error type handling
- Updated SSE rate limiting with burst and window-based restrictions
- Improved token validation with more precise signature and claim verification
2025-02-03 22:29:41 +01:00
jango-blockchained
89f2278c25 refactor: improve SSE types and testing utilities
- Enhanced SSE event type definitions with more precise typing
- Added type guard and safe type assertion functions for mock objects
- Updated security test suite to use new type utilities
- Improved type safety for token validation and mock function handling
- Refined event data type to support more flexible event structures
2025-02-03 22:22:26 +01:00
jango-blockchained
a53cec7b28 chore: migrate project to Bun testing framework and update configuration
- Replace Jest with Bun's native testing framework
- Update test configuration and utilities to support Bun test environment
- Add mock implementations for SSE and security testing
- Refactor test setup to use Bun's testing utilities
- Update package dependencies and scripts to align with Bun testing
- Enhance type definitions for Bun test mocking
2025-02-03 22:19:43 +01:00
jango-blockchained
b7856e9d05 Updated .gitignore 2025-02-03 22:10:24 +01:00
jango-blockchained
7891115ebe test: add comprehensive test suite for security and SSE components
- Implemented detailed Jest test configurations for project
- Added test configuration with robust environment setup
- Created comprehensive test suites for:
  * Security middleware
  * Token management
  * SSE security features
- Configured test utilities with mock request/response objects
- Implemented extensive test scenarios covering authentication, rate limiting, and error handling
2025-02-03 22:08:16 +01:00
jango-blockchained
a814c427e9 feat: enhance security configuration and SSE management with robust token validation and client tracking
- Refactored `.env.example` with comprehensive security and configuration parameters
- Added new `security.config.ts` for centralized security configuration management
- Improved middleware with enhanced authentication, request validation, and error handling
- Updated SSE routes and manager with advanced client tracking, rate limiting, and connection management
- Implemented more granular token validation with IP-based rate limiting and connection tracking
- Added detailed error responses and improved logging for security-related events
2025-02-03 22:02:12 +01:00
jango-blockchained
840927998e docs: enhance README with comprehensive project status and Bun runtime details
- Updated project status section with current achievements and upcoming features
- Added detailed performance benefits and optimization sections
- Expanded development workflow and testing instructions
- Included version history and advanced feature descriptions
- Refined Bun runtime documentation and performance highlights
- Improved overall readability and project presentation
2025-02-03 19:32:38 +01:00
jango-blockchained
cf7fb2422e Migrate from BUN_ENV to NODE_ENV and update Home Assistant implementation 2025-02-03 19:18:52 +01:00
jango-blockchained
d46a19c698 docs: update README with Bun runtime migration and comprehensive workflow details
- Replaced Node.js and npm references with Bun runtime configuration
- Updated badges to reflect Bun version
- Added new sections on Bun performance benefits and development workflow
- Expanded troubleshooting and performance optimization guidelines
- Simplified installation, build, and testing instructions
- Included Bun-specific commands for development and testing
2025-02-03 19:07:31 +01:00
89 changed files with 9395 additions and 6639 deletions

View File

@@ -7,7 +7,6 @@ yarn-error.log*
package-lock.json package-lock.json
yarn.lock yarn.lock
pnpm-lock.yaml pnpm-lock.yaml
bun.lockb
# Build output # Build output
dist/ dist/
@@ -74,3 +73,7 @@ temp/
.cloud/ .cloud/
*.db *.db
*.db-* *.db-*
bun.lockb
.cursor/
.cursor*
.cursorconfig

View File

@@ -36,26 +36,50 @@ RATE_LIMIT_REGULAR=100
# Requests per minute per IP for WebSocket connections # Requests per minute per IP for WebSocket connections
RATE_LIMIT_WEBSOCKET=1000 RATE_LIMIT_WEBSOCKET=1000
# Security
# JWT secret for token generation (change this in production!)
JWT_SECRET=your_jwt_secret_key
# CORS configuration (comma-separated list of allowed origins)
CORS_ORIGINS=http://localhost:3000,http://localhost:8123
# Test Configuration
# Only needed if running tests
TEST_HASS_HOST=http://localhost:8123
TEST_HASS_TOKEN=test_token
TEST_HASS_SOCKET_URL=ws://localhost:8123/api/websocket
TEST_PORT=3001
# Security Configuration # Security Configuration
JWT_SECRET=your-secret-key # JWT Configuration
JWT_SECRET=your_jwt_secret_key_min_32_chars
JWT_EXPIRY=86400000
JWT_MAX_AGE=2592000000
JWT_ALGORITHM=HS256
# Rate Limiting # Rate Limiting
RATE_LIMIT_WINDOW_MS=900000 # 15 minutes RATE_LIMIT_WINDOW=900000
RATE_LIMIT_MAX=100 RATE_LIMIT_MAX_REQUESTS=100
# Token Security
TOKEN_MIN_LENGTH=32
MAX_FAILED_ATTEMPTS=5
LOCKOUT_DURATION=900000
# CORS Configuration
CORS_ORIGINS=http://localhost:3000,http://localhost:8123
CORS_METHODS=GET,POST,PUT,DELETE,OPTIONS
CORS_ALLOWED_HEADERS=Content-Type,Authorization,X-Requested-With
CORS_EXPOSED_HEADERS=
CORS_CREDENTIALS=true
CORS_MAX_AGE=86400
# Content Security Policy
CSP_ENABLED=true
CSP_REPORT_ONLY=false
CSP_REPORT_URI=
# SSL/TLS Configuration
REQUIRE_HTTPS=true
HSTS_MAX_AGE=31536000
HSTS_INCLUDE_SUBDOMAINS=true
HSTS_PRELOAD=true
# Cookie Security
COOKIE_SECRET=your_cookie_secret_key_min_32_chars
COOKIE_SECURE=true
COOKIE_HTTP_ONLY=true
COOKIE_SAME_SITE=Strict
# Request Limits
MAX_REQUEST_SIZE=1048576
MAX_REQUEST_FIELDS=1000
# SSE Configuration # SSE Configuration
SSE_MAX_CLIENTS=1000 SSE_MAX_CLIENTS=1000
@@ -71,3 +95,10 @@ LOG_REQUESTS=true
# Version # Version
VERSION=0.1.0 VERSION=0.1.0
# Test Configuration
# Only needed if running tests
TEST_HASS_HOST=http://localhost:8123
TEST_HASS_TOKEN=test_token
TEST_HASS_SOCKET_URL=ws://localhost:8123/api/websocket
TEST_PORT=3001

65
.github/workflows/docker-build-push.yml vendored Normal file
View File

@@ -0,0 +1,65 @@
name: Docker Build and Push
on:
push:
branches: [ "main" ]
tags:
- 'v*.*.*' # Triggers on version tags like v1.0.0
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Required for version detection
- name: Bump version and push tag
id: tag_version
uses: mathieudutour/github-tag-action@v6.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: patch
- name: Create Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.tag_version.outputs.new_tag }}
release_name: Release ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ steps.tag_version.outputs.new_tag }}
type=raw,value=latest
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

8
.gitignore vendored
View File

@@ -65,7 +65,6 @@ home-assistant_v2.db-*
package-lock.json package-lock.json
yarn.lock yarn.lock
pnpm-lock.yaml pnpm-lock.yaml
bun.lockb
coverage/* coverage/*
coverage/ coverage/
@@ -73,3 +72,10 @@ coverage/
.env .env
.env.* .env.*
!.env.*.template !.env.*.template
.cursor/
.cursor/*
.bun/
.cursorconfig
bun.lockb

View File

@@ -1,20 +1,23 @@
# Use Bun as the base image # Use Bun as the base image
FROM oven/bun:1.0.26 FROM oven/bun:1.0.25
# Set working directory # Set working directory
WORKDIR /app WORKDIR /app
# Copy source code # Copy package files
COPY . . COPY package.json ./
# Install dependencies # Install dependencies
RUN bun install RUN bun install
# Copy source code
COPY . .
# Build TypeScript # Build TypeScript
RUN bun run build RUN bun run build
# Expose the port the app runs on # Expose port
EXPOSE 3000 EXPOSE 4000
# Start the application # Start the application
CMD ["bun", "run", "start"] CMD ["bun", "run", "start"]

526
README.md
View File

@@ -1,40 +1,112 @@
# Model Context Protocol Server for Home Assistant # Model Context Protocol (MCP) Server for Home Assistant
The server uses the MCP protocol to share access to a local Home Assistant instance with an LLM application. The Model Context Protocol (MCP) Server is a robust, secure, and high-performance bridge that integrates Home Assistant with Language Learning Models (LLMs), enabling natural language control and real-time monitoring of your smart home devices. Unlock advanced automation, control, and analytics for your Home Assistant ecosystem.
A powerful bridge between your Home Assistant instance and Language Learning Models (LLMs), enabling natural language control and monitoring of your smart home devices through the Model Context Protocol (MCP). This server provides a comprehensive API for managing your entire Home Assistant ecosystem, from device control to system administration.
![License](https://img.shields.io/badge/license-MIT-blue.svg) ![License](https://img.shields.io/badge/license-MIT-blue.svg)
![Node.js](https://img.shields.io/badge/node-%3E%3D20.10.0-green.svg) ![Bun](https://img.shields.io/badge/bun-%3E%3D1.0.26-black)
![Docker Compose](https://img.shields.io/badge/docker-compose-%3E%3D1.27.0-blue.svg)
![NPM](https://img.shields.io/badge/npm-%3E%3D7.0.0-orange.svg)
![TypeScript](https://img.shields.io/badge/typescript-%5E5.0.0-blue.svg) ![TypeScript](https://img.shields.io/badge/typescript-%5E5.0.0-blue.svg)
![Test Coverage](https://img.shields.io/badge/coverage-95%25-brightgreen.svg) ![Test Coverage](https://img.shields.io/badge/coverage-95%25-brightgreen.svg)
## Features ## Table of Contents
- 🎮 **Device Control**: Control any Home Assistant device through natural language - [Overview](#overview)
- 🔄 **Real-time Updates**: Get instant updates through Server-Sent Events (SSE) - [Key Features](#key-features)
- 🤖 **Automation Management**: Create, update, and manage automations - [Architecture & Design](#architecture--design)
- 📊 **State Monitoring**: Track and query device states - [Installation](#installation)
- 🔐 **Secure**: Token-based authentication and rate limiting - [Basic Setup](#basic-setup)
- 📱 **Mobile Ready**: Works with any HTTP-capable client - [Docker Setup (Recommended)](#docker-setup-recommended)
- [Usage](#usage)
- [API & Documentation](#api--documentation)
- [Development](#development)
- [Roadmap & Future Plans](#roadmap--future-plans)
- [Community & Support](#community--support)
- [Contributing](#contributing)
- [Troubleshooting & FAQ](#troubleshooting--faq)
- [License](#license)
## Real-time Updates with SSE ## Overview
The server includes a powerful Server-Sent Events (SSE) system that provides real-time updates from your Home Assistant instance. This allows you to: The MCP Server bridges Home Assistant with advanced LLM integrations to deliver intuitive control, automation, and state monitoring. Leveraging a high-performance runtime and real-time communication protocols, MCP offers a seamless experience for managing your smart home.
- 🔄 Get instant state changes for any device ## Key Features
- 📡 Monitor automation triggers and executions
- 🎯 Subscribe to specific domains or entities
- 📊 Track service calls and script executions
### Quick SSE Example ### Device Control & Monitoring
- **Smart Device Control:** Manage lights, climate, covers, switches, sensors, media players, fans, locks, vacuums, and cameras using natural language commands.
- **Real-time Updates:** Receive instant notifications and updates via Server-Sent Events (SSE).
### System & Automation Management
- **Automation Engine:** Create, modify, and trigger custom automation rules with ease.
- **Add-on & Package Management:** Integrates with HACS for deploying custom integrations, themes, scripts, and applications.
- **Robust System Management:** Features advanced state monitoring, error handling, and security safeguards.
## Architecture & Design
The MCP Server is built with scalability, resilience, and security in mind:
- **High-Performance Runtime:** Powered by Bun for fast startup, efficient memory utilization, and native TypeScript support.
- **Real-time Communication:** Employs Server-Sent Events (SSE) for continuous, real-time data updates.
- **Modular & Extensible:** Designed to support plugins, add-ons, and custom automation scripts, allowing for easy expansion.
- **Secure API Integration:** Implements token-based authentication, rate limiting, and adherence to best security practices.
_For a deeper dive into the system architecture, please refer to our [Architecture Documentation](docs/ARCHITECTURE.md) (if available)._
## Installation
### Basic Setup
1. **Install Bun:** If Bun is not installed:
```bash
curl -fsSL https://bun.sh/install | bash
```
2. **Clone the Repository:**
```bash
git clone https://github.com/jango-blockchained/homeassistant-mcp.git
cd homeassistant-mcp
```
3. **Install Dependencies:**
```bash
bun install
```
4. **Build the Project:**
```bash
bun run build
```
### Docker Setup (Recommended)
1. **Clone the Repository:**
```bash
git clone https://github.com/jango-blockchained/homeassistant-mcp.git
cd homeassistant-mcp
```
2. **Configure Environment:**
```bash
cp .env.example .env
```
Customize the `.env` file with your Home Assistant configuration.
3. **Deploy with Docker Compose:**
```bash
docker compose up -d
```
- View logs: `docker compose logs -f`
- Stop the server: `docker compose down`
4. **Update the Application:**
```bash
git pull && docker compose up -d --build
```
## Usage
Once the server is running, open your browser at [http://localhost:3000](http://localhost:3000). For real-time device updates, integrate the SSE endpoint in your application:
```javascript ```javascript
const eventSource = new EventSource( const eventSource = new EventSource('http://localhost:3000/subscribe_events?token=YOUR_TOKEN&domain=light');
'http://localhost:3000/subscribe_events?token=YOUR_TOKEN&domain=light'
);
eventSource.onmessage = (event) => { eventSource.onmessage = (event) => {
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
@@ -42,359 +114,107 @@ eventSource.onmessage = (event) => {
}; };
``` ```
See [SSE_API.md](docs/SSE_API.md) for complete documentation of the SSE system. ## API & Documentation
## Table of Contents Access comprehensive API details and guides in the docs directory:
- [Key Features](#key-features) - **API Reference:** [API Documentation](docs/API.md)
- [Prerequisites](#prerequisites) - **SSE Documentation:** [SSE API](docs/SSE_API.md)
- [Installation](#installation) - **Troubleshooting Guide:** [TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md)
- [Basic Setup](#basic-setup) - **Architecture Details:** [Architecture Documentation](docs/ARCHITECTURE.md) _(if available)_
- [Docker Setup (Recommended)](#docker-setup-recommended)
- [Configuration](#configuration)
- [Development](#development)
- [API Reference](#api-reference)
- [OpenAI Integration](#openai-integration)
- [Natural Language Integration](#natural-language-integration)
- [Troubleshooting](#troubleshooting)
- [Project Status](#project-status)
- [Contributing](#contributing)
- [Resources](#resources)
- [License](#license)
## Key Features
### Core Functionality 🎮
- **Smart Device Control**
- 💡 **Lights**: Brightness, color temperature, RGB color
- 🌡️ **Climate**: Temperature, HVAC modes, fan modes, humidity
- 🚪 **Covers**: Position and tilt control
- 🔌 **Switches**: On/off control
- 🚨 **Sensors & Contacts**: State monitoring
- 🎵 **Media Players**: Playback control, volume, source selection
- 🌪️ **Fans**: Speed, oscillation, direction
- 🔒 **Locks**: Lock/unlock control
- 🧹 **Vacuums**: Start, stop, return to base
- 📹 **Cameras**: Motion detection, snapshots
### System Management 🛠️
- **Add-on Management**
- Browse available add-ons
- Install/uninstall add-ons
- Start/stop/restart add-ons
- Version management
- Configuration access
- **Package Management (HACS)**
- Integration with Home Assistant Community Store
- Multiple package types support:
- Custom integrations
- Frontend themes
- Python scripts
- AppDaemon apps
- NetDaemon apps
- Version control and updates
- Repository management
- **Automation Management**
- Create and edit automations
- Advanced configuration options:
- Multiple trigger types
- Complex conditions
- Action sequences
- Execution modes
- Duplicate and modify existing automations
- Enable/disable automation rules
- Trigger automation manually
### Architecture Features 🏗️
- **Intelligent Organization**
- Area and floor-based device grouping
- State monitoring and querying
- Smart context awareness
- Historical data access
- **Robust Architecture**
- Comprehensive error handling
- State validation
- Secure API integration
- TypeScript type safety
- Extensive test coverage
## Prerequisites
- **Node.js** 20.10.0 or higher
- **NPM** package manager
- **Docker Compose** for containerization
- Running **Home Assistant** instance
- Home Assistant long-lived access token ([How to get token](https://community.home-assistant.io/t/how-to-get-long-lived-access-token/162159))
- **HACS** installed for package management features
- **Supervisor** access for add-on management
## Installation
### Basic Setup
```bash
# Clone the repository
git clone https://github.com/jango-blockchained/homeassistant-mcp.git
cd homeassistant-mcp
# Install dependencies
npm install
# Build the project
npm run build
```
### Docker Setup (Recommended)
The project includes Docker support for easy deployment and consistent environments across different platforms.
1. **Clone the repository:**
```bash
git clone https://github.com/jango-blockchained/homeassistant-mcp.git
cd homeassistant-mcp
```
2. **Configure environment:**
```bash
cp .env.example .env
```
Edit the `.env` file with your Home Assistant configuration:
```env
# Home Assistant Configuration
HASS_HOST=http://homeassistant.local:8123
HASS_TOKEN=your_home_assistant_token
HASS_SOCKET_URL=ws://homeassistant.local:8123/api/websocket
# Server Configuration
PORT=3000
NODE_ENV=production
DEBUG=false
```
3. **Build and run with Docker Compose:**
```bash
# Build and start the containers
docker compose up -d
# View logs
docker compose logs -f
# Stop the service
docker compose down
```
4. **Verify the installation:**
The server should now be running at `http://localhost:3000`. You can check the health endpoint at `http://localhost:3000/health`.
5. **Update the application:**
```bash
# Pull the latest changes
git pull
# Rebuild and restart the containers
docker compose up -d --build
```
#### Docker Configuration
The Docker setup includes:
- Multi-stage build for optimal image size
- Health checks for container monitoring
- Volume mounting for environment configuration
- Automatic container restart on failure
- Exposed port 3000 for API access
#### Docker Compose Environment Variables
All environment variables can be configured in the `.env` file. The following variables are supported:
- `HASS_HOST`: Your Home Assistant instance URL
- `HASS_TOKEN`: Long-lived access token for Home Assistant
- `HASS_SOCKET_URL`: WebSocket URL for Home Assistant
- `PORT`: Server port (default: 3000)
- `NODE_ENV`: Environment (production/development)
- `DEBUG`: Enable debug mode (true/false)
## Configuration
### Environment Variables
```env
# Home Assistant Configuration
HASS_HOST=http://homeassistant.local:8123 # Your Home Assistant instance URL
HASS_TOKEN=your_home_assistant_token # Long-lived access token
HASS_SOCKET_URL=ws://homeassistant.local:8123/api/websocket # WebSocket URL
# Server Configuration
PORT=3000 # Server port (default: 3000)
NODE_ENV=production # Environment (production/development)
DEBUG=false # Enable debug mode
# Test Configuration
TEST_HASS_HOST=http://localhost:8123 # Test instance URL
TEST_HASS_TOKEN=test_token # Test token
```
### Configuration Files
1. **Development**: Copy `.env.example` to `.env.development`
2. **Production**: Copy `.env.example` to `.env.production`
3. **Testing**: Copy `.env.example` to `.env.test`
## Development ## Development
```bash ### Running in Development Mode
# Development mode with hot reload
npm run dev
# Build project
npm run build
# Production mode
npm run start
# Run tests
npx jest --config=jest.config.cjs
# Run tests with coverage
npx jest --coverage
# Lint code
npm run lint
# Format code
npm run format
```
## API Reference
For detailed API documentation, please refer to:
- [API Documentation](docs/API.md) - Complete API reference
- [SSE API Documentation](docs/SSE_API.md) - Server-Sent Events documentation
## OpenAI Integration
The server includes powerful AI analysis capabilities powered by OpenAI's GPT-4 model. This feature provides intelligent analysis of your Home Assistant setup through two main modes:
### 1. Standard Analysis
Performs a comprehensive system analysis including:
- System Overview
- Performance Analysis
- Security Assessment
- Optimization Recommendations
- Maintenance Tasks
```bash ```bash
# Run standard analysis bun run dev
npm run test:openai
# Select option 1 when prompted
``` ```
### 2. Custom Prompt Analysis ### Running Tests
Allows you to ask specific questions about your Home Assistant setup. The analysis can include: - Execute all tests:
- Device States
- Configuration Details
- Active Devices
- Device Attributes (brightness, temperature, etc.)
```bash
# Run custom analysis
npm run test:openai
# Select option 2 when prompted
```
### Configuration
To use the OpenAI integration, you need to set up your OpenAI API key in the `.env` file:
```env
OPENAI_API_KEY=your_openai_api_key
```
## Troubleshooting
### Common Issues
1. **Node.js Version (`toSorted is not a function`)**
- **Solution:** Update to Node.js 20.10.0+
```bash ```bash
nvm install 20.10.0 bun test
nvm use 20.10.0
``` ```
2. **Connection Issues** - Run tests with coverage:
- Verify Home Assistant is running ```bash
- Check `HASS_HOST` accessibility bun test --coverage
- Validate token permissions ```
- Ensure WebSocket connection for real-time updates
3. **Add-on Management Issues** ### Production Build & Start
- Verify Supervisor access
- Check add-on compatibility
- Validate system resources
4. **HACS Integration Issues** ```bash
- Verify HACS installation bun run build
- Check HACS integration status bun start
- Validate repository access ```
5. **Automation Issues** ## Roadmap & Future Plans
- Verify entity availability
- Check trigger conditions
- Validate service calls
- Monitor execution logs
## Project Status The MCP Server is under active development and improvement. Planned enhancements include:
**Complete** - **Advanced Automation Capabilities:** Introducing more complex automation rules and conditional logic.
- Entity, Floor, and Area access - **Enhanced Security Features:** Additional authentication layers, encryption enhancements, and security monitoring tools.
- Device control (Lights, Climate, Covers, Switches, Contacts) - **User Interface Improvements:** Development of a more intuitive web dashboard for easier device management.
- Add-on management system - **Expanded Integrations:** Support for a wider array of smart home devices and third-party services.
- Package management through HACS - **Performance Optimizations:** Continued efforts to reduce latency and improve resource efficiency.
- Advanced automation configuration
- Basic state management
- Error handling and validation
- Docker containerization
- Jest testing setup
- TypeScript integration
- Environment variable management
- Home Assistant API integration
- Project documentation
🚧 **In Progress** _For additional details, check out our [Roadmap](docs/ROADMAP.md) (if available)._
- WebSocket implementation for real-time updates
- Enhanced security features ## Community & Support
- Tool organization optimization
- Performance optimization Join our community to stay updated, share ideas, and get help:
- Resource context integration
- API documentation generation - **GitHub Issues:** Report bugs or suggest features on our [GitHub Issues Page](https://github.com/jango-blockchained/homeassistant-mcp/issues).
- Multi-platform desktop integration - **Discussion Forums:** Connect with other users and contributors in our community forums.
- Advanced error recovery - **Chat Platforms:** Join our real-time discussions on [Discord](#) or [Slack](#).
- Custom prompt testing
- Enhanced macOS integration
- Type safety improvements
- Testing coverage expansion
## Contributing ## Contributing
1. Fork the repository We welcome your contributions! To get started:
2. Create a feature branch
3. Implement your changes
4. Add tests for new functionality
5. Ensure all tests pass
6. Submit a pull request
## Resources 1. Fork the repository.
2. Create your feature branch:
```bash
git checkout -b feature/your-feature-name
```
3. Install dependencies:
```bash
bun install
```
4. Make your changes and run tests:
```bash
bun test
```
5. Commit and push your changes, then open a Pull Request.
- [MCP Documentation](https://modelcontextprotocol.io/introduction) _For detailed guidelines, see [CONTRIBUTING.md](CONTRIBUTING.md)._
- [Home Assistant Docs](https://www.home-assistant.io)
- [HA REST API](https://developers.home-assistant.io/docs/api/rest) ## Troubleshooting & FAQ
- [HACS Documentation](https://hacs.xyz)
- [TypeScript Documentation](https://www.typescriptlang.org/docs) ### Common Issues
- **Connection Problems:** Ensure that your `HASS_HOST`, authentication token, and WebSocket URL are correctly configured.
- **Docker Deployment:** Confirm that Docker is running and that your `.env` file contains the correct settings.
- **Automation Errors:** Verify entity availability and review your automation configurations for potential issues.
_For more troubleshooting details, refer to [TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md)._
### Frequently Asked Questions
**Q: What platforms does MCP Server support?**
A: MCP Server runs on Linux, macOS, and Windows (Docker is recommended for Windows environments).
**Q: How do I report a bug or request a feature?**
A: Please use our [GitHub Issues Page](https://github.com/jango-blockchained/homeassistant-mcp/issues) to report bugs or request new features.
**Q: Can I contribute to the project?**
A: Absolutely! We welcome contributions from the community. See the [Contributing](#contributing) section for more details.
## License ## License
MIT License - See [LICENSE](LICENSE) file This project is licensed under the MIT License. See [LICENSE](LICENSE) for the full license text.

View File

@@ -1,212 +1,314 @@
import { TokenManager, validateRequest, sanitizeInput, errorHandler } from '../../src/security/index.js'; import { TokenManager, validateRequest, sanitizeInput, errorHandler, rateLimiter, securityHeaders } from '../../src/security/index.js';
import { Request, Response } from 'express'; import { mock, describe, it, expect, beforeEach, afterEach } from 'bun:test';
import jwt from 'jsonwebtoken';
const TEST_SECRET = 'test-secret-that-is-long-enough-for-testing-purposes';
describe('Security Module', () => { describe('Security Module', () => {
beforeEach(() => {
process.env.JWT_SECRET = TEST_SECRET;
});
afterEach(() => {
delete process.env.JWT_SECRET;
});
describe('TokenManager', () => { describe('TokenManager', () => {
const testToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNzE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; const testToken = 'test-token';
const encryptionKey = 'test_encryption_key'; const encryptionKey = 'test-encryption-key-that-is-long-enough';
it('should encrypt and decrypt tokens', () => { it('should encrypt and decrypt tokens', () => {
const encrypted = TokenManager.encryptToken(testToken, encryptionKey); const encrypted = TokenManager.encryptToken(testToken, encryptionKey);
const decrypted = TokenManager.decryptToken(encrypted, encryptionKey); expect(encrypted).toContain('aes-256-gcm:');
const decrypted = TokenManager.decryptToken(encrypted, encryptionKey);
expect(decrypted).toBe(testToken); expect(decrypted).toBe(testToken);
}); });
it('should validate tokens correctly', () => { it('should validate tokens correctly', () => {
expect(TokenManager.validateToken(testToken)).toBe(true); const validToken = jwt.sign({ data: 'test' }, TEST_SECRET, { expiresIn: '1h' });
expect(TokenManager.validateToken('invalid_token')).toBe(false); const result = TokenManager.validateToken(validToken);
expect(TokenManager.validateToken('')).toBe(false); expect(result.valid).toBe(true);
expect(result.error).toBeUndefined();
});
it('should handle empty tokens', () => {
const result = TokenManager.validateToken('');
expect(result.valid).toBe(false);
expect(result.error).toBe('Invalid token format');
}); });
it('should handle expired tokens', () => { it('should handle expired tokens', () => {
const expiredToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; const now = Math.floor(Date.now() / 1000);
expect(TokenManager.validateToken(expiredToken)).toBe(false); const payload = {
data: 'test',
iat: now - 7200, // 2 hours ago
exp: now - 3600 // expired 1 hour ago
};
const token = jwt.sign(payload, TEST_SECRET);
const result = TokenManager.validateToken(token);
expect(result.valid).toBe(false);
expect(result.error).toBe('Token has expired');
});
it('should handle invalid token format', () => {
const result = TokenManager.validateToken('invalid-token');
expect(result.valid).toBe(false);
expect(result.error).toBe('Invalid token format');
});
it('should handle missing JWT secret', () => {
delete process.env.JWT_SECRET;
const payload = { data: 'test' };
const token = jwt.sign(payload, 'some-secret');
const result = TokenManager.validateToken(token);
expect(result.valid).toBe(false);
expect(result.error).toBe('JWT secret not configured');
});
it('should handle rate limiting for failed attempts', () => {
const invalidToken = 'x'.repeat(64);
const testIp = '127.0.0.1';
// First attempt
const firstResult = TokenManager.validateToken(invalidToken, testIp);
expect(firstResult.valid).toBe(false);
// Multiple failed attempts
for (let i = 0; i < 4; i++) {
TokenManager.validateToken(invalidToken, testIp);
}
// Next attempt should be rate limited
const limitedResult = TokenManager.validateToken(invalidToken, testIp);
expect(limitedResult.valid).toBe(false);
expect(limitedResult.error).toBe('Too many failed attempts. Please try again later.');
}); });
}); });
describe('Request Validation', () => { describe('Request Validation', () => {
let mockRequest: Partial<Request>; let mockRequest: any;
let mockResponse: Partial<Response>; let mockResponse: any;
let mockNext: jest.Mock; let mockNext: any;
beforeEach(() => { beforeEach(() => {
mockRequest = { mockRequest = {
method: 'POST', method: 'POST',
headers: { headers: {
'content-type': 'application/json', 'content-type': 'application/json'
authorization: 'Bearer validToken'
}, },
is: jest.fn().mockReturnValue(true), body: {},
body: { test: 'data' } ip: '127.0.0.1'
}; };
mockResponse = { mockResponse = {
status: jest.fn().mockReturnThis(), status: mock(() => mockResponse),
json: jest.fn() json: mock(() => mockResponse),
setHeader: mock(() => mockResponse),
removeHeader: mock(() => mockResponse)
}; };
mockNext = jest.fn();
mockNext = mock(() => { });
}); });
it('should pass valid requests', () => { it('should pass valid requests', () => {
validateRequest( if (mockRequest.headers) {
mockRequest as Request, mockRequest.headers.authorization = 'Bearer valid-token';
mockResponse as Response, }
mockNext const validateTokenSpy = mock(() => ({ valid: true }));
); TokenManager.validateToken = validateTokenSpy;
validateRequest(mockRequest, mockResponse, mockNext);
expect(mockNext).toHaveBeenCalled(); expect(mockNext).toHaveBeenCalled();
expect(mockResponse.status).not.toHaveBeenCalled();
}); });
it('should reject invalid content type', () => { it('should reject invalid content type', () => {
mockRequest.is = jest.fn().mockReturnValue(false); if (mockRequest.headers) {
mockRequest.headers['content-type'] = 'text/plain';
}
validateRequest( validateRequest(mockRequest, mockResponse, mockNext);
mockRequest as Request,
mockResponse as Response,
mockNext
);
expect(mockResponse.status).toHaveBeenCalledWith(415); expect(mockResponse.status).toHaveBeenCalledWith(415);
expect(mockResponse.json).toHaveBeenCalledWith({ expect(mockResponse.json).toHaveBeenCalledWith({
error: 'Unsupported Media Type - Content-Type must be application/json' success: false,
message: 'Unsupported Media Type',
error: 'Content-Type must be application/json',
timestamp: expect.any(String)
}); });
}); });
it('should reject missing token', () => { it('should reject missing token', () => {
mockRequest.headers = {}; if (mockRequest.headers) {
delete mockRequest.headers.authorization;
}
validateRequest( validateRequest(mockRequest, mockResponse, mockNext);
mockRequest as Request,
mockResponse as Response,
mockNext
);
expect(mockResponse.status).toHaveBeenCalledWith(401); expect(mockResponse.status).toHaveBeenCalledWith(401);
expect(mockResponse.json).toHaveBeenCalledWith({ expect(mockResponse.json).toHaveBeenCalledWith({
error: 'Invalid or expired token' success: false,
message: 'Unauthorized',
error: 'Missing or invalid authorization header',
timestamp: expect.any(String)
}); });
}); });
it('should reject invalid request body', () => { it('should reject invalid request body', () => {
mockRequest.body = null; mockRequest.body = null;
validateRequest( validateRequest(mockRequest, mockResponse, mockNext);
mockRequest as Request,
mockResponse as Response,
mockNext
);
expect(mockResponse.status).toHaveBeenCalledWith(400); expect(mockResponse.status).toHaveBeenCalledWith(400);
expect(mockResponse.json).toHaveBeenCalledWith({ expect(mockResponse.json).toHaveBeenCalledWith({
error: 'Invalid request body' success: false,
message: 'Bad Request',
error: 'Invalid request body structure',
timestamp: expect.any(String)
}); });
}); });
}); });
describe('Input Sanitization', () => { describe('Input Sanitization', () => {
let mockRequest: Partial<Request>; let mockRequest: any;
let mockResponse: Partial<Response>; let mockResponse: any;
let mockNext: jest.Mock; let mockNext: any;
beforeEach(() => { beforeEach(() => {
mockRequest = { mockRequest = {
body: {} method: 'POST',
}; headers: {
mockResponse = {}; 'content-type': 'application/json'
mockNext = jest.fn(); },
}); body: {
it('should sanitize HTML tags from request body', () => {
mockRequest.body = {
text: 'Test <script>alert("xss")</script>',
nested: {
html: '<img src="x" onerror="alert(1)">'
}
};
sanitizeInput(
mockRequest as Request,
mockResponse as Response,
mockNext
);
expect(mockRequest.body).toEqual({
text: 'Test alert("xss")', text: 'Test alert("xss")',
nested: { nested: {
html: 'img src="x" onerror="alert(1)"' html: 'img src="x" onerror="alert(1)"'
} }
}
};
mockResponse = {
status: mock(() => mockResponse),
json: mock(() => mockResponse)
};
mockNext = mock(() => { });
});
it('should sanitize HTML tags from request body', () => {
sanitizeInput(mockRequest, mockResponse, mockNext);
expect(mockRequest.body).toEqual({
text: 'Test',
nested: {
html: ''
}
}); });
expect(mockNext).toHaveBeenCalled(); expect(mockNext).toHaveBeenCalled();
}); });
it('should handle non-object body', () => { it('should handle non-object body', () => {
mockRequest.body = 'string body'; mockRequest.body = 'string body';
sanitizeInput(mockRequest, mockResponse, mockNext);
sanitizeInput(
mockRequest as Request,
mockResponse as Response,
mockNext
);
expect(mockRequest.body).toBe('string body');
expect(mockNext).toHaveBeenCalled(); expect(mockNext).toHaveBeenCalled();
}); });
}); });
describe('Error Handler', () => { describe('Error Handler', () => {
let mockRequest: Partial<Request>; let mockRequest: any;
let mockResponse: Partial<Response>; let mockResponse: any;
let mockNext: jest.Mock; let mockNext: any;
const originalEnv = process.env.NODE_ENV;
beforeEach(() => { beforeEach(() => {
mockRequest = {}; mockRequest = {
mockResponse = { method: 'POST',
status: jest.fn().mockReturnThis(), ip: '127.0.0.1'
json: jest.fn()
}; };
mockNext = jest.fn();
});
afterAll(() => { mockResponse = {
process.env.NODE_ENV = originalEnv; status: mock(() => mockResponse),
json: mock(() => mockResponse)
};
mockNext = mock(() => { });
}); });
it('should handle errors in production mode', () => { it('should handle errors in production mode', () => {
process.env.NODE_ENV = 'production'; process.env.NODE_ENV = 'production';
const error = new Error('Test error'); const error = new Error('Test error');
errorHandler(error, mockRequest, mockResponse, mockNext);
errorHandler(
error,
mockRequest as Request,
mockResponse as Response,
mockNext
);
expect(mockResponse.status).toHaveBeenCalledWith(500); expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.json).toHaveBeenCalledWith({ expect(mockResponse.json).toHaveBeenCalledWith({
error: 'Internal Server Error', success: false,
message: undefined message: 'Internal Server Error',
timestamp: expect.any(String)
}); });
}); });
it('should include error message in development mode', () => { it('should include error message in development mode', () => {
process.env.NODE_ENV = 'development'; process.env.NODE_ENV = 'development';
const error = new Error('Test error'); const error = new Error('Test error');
errorHandler(error, mockRequest, mockResponse, mockNext);
errorHandler(
error,
mockRequest as Request,
mockResponse as Response,
mockNext
);
expect(mockResponse.status).toHaveBeenCalledWith(500); expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.json).toHaveBeenCalledWith({ expect(mockResponse.json).toHaveBeenCalledWith({
error: 'Internal Server Error', success: false,
message: 'Test error' message: 'Internal Server Error',
error: 'Test error',
stack: expect.any(String),
timestamp: expect.any(String)
}); });
}); });
}); });
describe('Rate Limiter', () => {
it('should limit requests after threshold', async () => {
const mockContext = {
request: new Request('http://localhost', {
headers: new Headers({
'x-forwarded-for': '127.0.0.1'
})
}),
set: mock(() => { })
};
// Test multiple requests
for (let i = 0; i < 100; i++) {
await rateLimiter.derive(mockContext);
}
// The next request should throw
try {
await rateLimiter.derive(mockContext);
expect(false).toBe(true); // Should not reach here
} catch (error) {
expect(error instanceof Error).toBe(true);
expect(error.message).toBe('Too many requests from this IP, please try again later');
}
});
});
describe('Security Headers', () => {
it('should set security headers', async () => {
const mockHeaders = new Headers();
const mockContext = {
request: new Request('http://localhost', {
headers: mockHeaders
}),
set: mock(() => { })
};
await securityHeaders.derive(mockContext);
// Verify that security headers were set
const headers = mockContext.request.headers;
expect(headers.has('content-security-policy')).toBe(true);
expect(headers.has('x-frame-options')).toBe(true);
expect(headers.has('x-content-type-options')).toBe(true);
expect(headers.has('referrer-policy')).toBe(true);
});
});
}); });

View File

@@ -1,177 +1,156 @@
import { jest, describe, it, expect, beforeEach } from '@jest/globals'; import { describe, it, expect } from 'bun:test';
import { Request, Response, NextFunction } from 'express';
import { import {
validateRequest, checkRateLimit,
sanitizeInput, validateRequestHeaders,
errorHandler, sanitizeValue,
rateLimiter, applySecurityHeaders,
securityHeaders handleError
} from '../../src/security/index.js'; } from '../../src/security/index.js';
type MockRequest = { describe('Security Middleware Utilities', () => {
headers: { describe('Rate Limiter', () => {
'content-type'?: string; it('should allow requests under threshold', () => {
authorization?: string; const ip = '127.0.0.1';
}; expect(() => checkRateLimit(ip, 10)).not.toThrow();
body?: any; });
is: jest.MockInstance<string | false | null, [type: string | string[]]>;
};
type MockResponse = { it('should throw when requests exceed threshold', () => {
status: jest.MockInstance<MockResponse, [code: number]>; const ip = '127.0.0.2';
json: jest.MockInstance<MockResponse, [body: any]>;
setHeader: jest.MockInstance<MockResponse, [name: string, value: string]>;
};
describe('Security Middleware', () => { // Simulate multiple requests
let mockRequest: MockRequest; for (let i = 0; i < 11; i++) {
let mockResponse: MockResponse; if (i < 10) {
let nextFunction: jest.Mock; expect(() => checkRateLimit(ip, 10)).not.toThrow();
} else {
expect(() => checkRateLimit(ip, 10)).toThrow('Too many requests from this IP, please try again later');
}
}
});
beforeEach(() => { it('should reset rate limit after window expires', async () => {
mockRequest = { const ip = '127.0.0.3';
headers: {},
body: {},
is: jest.fn<string | false | null, [string | string[]]>().mockReturnValue('json')
};
mockResponse = { // Simulate multiple requests
status: jest.fn<MockResponse, [number]>().mockReturnThis(), for (let i = 0; i < 11; i++) {
json: jest.fn<MockResponse, [any]>().mockReturnThis(), if (i < 10) {
setHeader: jest.fn<MockResponse, [string, string]>().mockReturnThis() expect(() => checkRateLimit(ip, 10, 50)).not.toThrow();
}; }
}
nextFunction = jest.fn(); // Wait for rate limit window to expire
await new Promise(resolve => setTimeout(resolve, 100));
// Should be able to make requests again
expect(() => checkRateLimit(ip, 10, 50)).not.toThrow();
});
}); });
describe('Request Validation', () => { describe('Request Validation', () => {
it('should pass valid requests', () => { it('should validate content type', () => {
mockRequest.headers.authorization = 'Bearer valid-token'; const mockRequest = new Request('http://localhost', {
validateRequest(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); method: 'POST',
expect(nextFunction).toHaveBeenCalled(); headers: {
'content-type': 'application/json'
}
}); });
it('should reject requests without authorization header', () => { expect(() => validateRequestHeaders(mockRequest)).not.toThrow();
validateRequest(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction);
expect(mockResponse.status).toHaveBeenCalledWith(401);
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({
error: expect.stringContaining('authorization')
}));
}); });
it('should reject requests with invalid authorization format', () => { it('should reject invalid content type', () => {
mockRequest.headers.authorization = 'invalid-format'; const mockRequest = new Request('http://localhost', {
validateRequest(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); method: 'POST',
expect(mockResponse.status).toHaveBeenCalledWith(401); headers: {
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({ 'content-type': 'text/plain'
error: expect.stringContaining('Bearer') }
})); });
expect(() => validateRequestHeaders(mockRequest)).toThrow('Content-Type must be application/json');
});
it('should reject large request bodies', () => {
const mockRequest = new Request('http://localhost', {
method: 'POST',
headers: {
'content-type': 'application/json',
'content-length': '2000000'
}
});
expect(() => validateRequestHeaders(mockRequest)).toThrow('Request body too large');
}); });
}); });
describe('Input Sanitization', () => { describe('Input Sanitization', () => {
it('should pass requests without body', () => { it('should sanitize HTML tags', () => {
delete mockRequest.body; const input = '<script>alert("xss")</script>Hello';
sanitizeInput(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); const sanitized = sanitizeValue(input);
expect(nextFunction).toHaveBeenCalled(); expect(sanitized).toBe('&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;Hello');
}); });
it('should sanitize HTML in request body', () => { it('should sanitize nested objects', () => {
mockRequest.body = { const input = {
text: '<script>alert("xss")</script>Hello', text: '<script>alert("xss")</script>Hello',
nested: { nested: {
html: '<img src="x" onerror="alert(1)">World' html: '<img src="x" onerror="alert(1)">World'
} }
}; };
sanitizeInput(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); const sanitized = sanitizeValue(input);
expect(mockRequest.body.text).toBe('Hello'); expect(sanitized).toEqual({
expect(mockRequest.body.nested.html).toBe('World'); text: '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;Hello',
expect(nextFunction).toHaveBeenCalled(); nested: {
html: '&lt;img src=&quot;x&quot; onerror=&quot;alert(1)&quot;&gt;World'
}
}); });
it('should handle non-object bodies', () => {
mockRequest.body = '<p>text</p>';
sanitizeInput(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction);
expect(mockRequest.body).toBe('text');
expect(nextFunction).toHaveBeenCalled();
}); });
it('should preserve non-string values', () => { it('should preserve non-string values', () => {
mockRequest.body = { const input = {
number: 42, number: 123,
boolean: true, boolean: true,
null: null,
array: [1, 2, 3] array: [1, 2, 3]
}; };
sanitizeInput(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); const sanitized = sanitizeValue(input);
expect(mockRequest.body).toEqual({ expect(sanitized).toEqual(input);
number: 42,
boolean: true,
null: null,
array: [1, 2, 3]
});
expect(nextFunction).toHaveBeenCalled();
});
});
describe('Error Handler', () => {
const originalEnv = process.env.NODE_ENV;
afterAll(() => {
process.env.NODE_ENV = originalEnv;
});
it('should handle errors in production mode', () => {
process.env.NODE_ENV = 'production';
const error = new Error('Test error');
errorHandler(error, mockRequest as Request, mockResponse as Response, nextFunction);
expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.json).toHaveBeenCalledWith({
error: 'Internal Server Error'
});
});
it('should include error details in development mode', () => {
process.env.NODE_ENV = 'development';
const error = new Error('Test error');
errorHandler(error, mockRequest as Request, mockResponse as Response, nextFunction);
expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.json).toHaveBeenCalledWith({
error: 'Test error',
stack: expect.any(String)
});
});
it('should handle non-Error objects', () => {
const error = 'String error message';
errorHandler(
error as any,
mockRequest as Request,
mockResponse as Response,
nextFunction
);
expect(mockResponse.status).toHaveBeenCalledWith(500);
});
});
describe('Rate Limiter', () => {
it('should be configured with correct options', () => {
expect(rateLimiter).toBeDefined();
const middleware = rateLimiter as any;
expect(middleware.windowMs).toBeDefined();
expect(middleware.max).toBeDefined();
}); });
}); });
describe('Security Headers', () => { describe('Security Headers', () => {
it('should set appropriate security headers', () => { it('should apply security headers', () => {
securityHeaders(mockRequest as Request, mockResponse as Response, nextFunction); const mockRequest = new Request('http://localhost');
expect(mockResponse.setHeader).toHaveBeenCalledWith('X-Content-Type-Options', 'nosniff'); const headers = applySecurityHeaders(mockRequest);
expect(mockResponse.setHeader).toHaveBeenCalledWith('X-Frame-Options', 'DENY');
expect(mockResponse.setHeader).toHaveBeenCalledWith('X-XSS-Protection', '1; mode=block'); expect(headers).toBeDefined();
expect(nextFunction).toHaveBeenCalled(); expect(headers['content-security-policy']).toBeDefined();
expect(headers['x-frame-options']).toBeDefined();
expect(headers['x-content-type-options']).toBeDefined();
expect(headers['referrer-policy']).toBeDefined();
});
});
describe('Error Handling', () => {
it('should handle errors in production mode', () => {
const error = new Error('Test error');
const result = handleError(error, 'production');
expect(result).toEqual({
error: true,
message: 'Internal server error',
timestamp: expect.any(String)
});
});
it('should include error details in development mode', () => {
const error = new Error('Test error');
const result = handleError(error, 'development');
expect(result).toEqual({
error: true,
message: 'Internal server error',
timestamp: expect.any(String),
error: 'Test error',
stack: expect.any(String)
});
}); });
}); });
}); });

View File

@@ -1,6 +1,17 @@
import { TokenManager } from '../../src/security/index.js'; import { TokenManager } from '../../src/security/index.js';
import jwt from 'jsonwebtoken';
const TEST_SECRET = 'test-secret-that-is-long-enough-for-testing-purposes';
describe('TokenManager', () => { describe('TokenManager', () => {
beforeAll(() => {
process.env.JWT_SECRET = TEST_SECRET;
});
afterAll(() => {
delete process.env.JWT_SECRET;
});
const encryptionKey = 'test-encryption-key-32-chars-long!!'; const encryptionKey = 'test-encryption-key-32-chars-long!!';
const validToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNjE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; const validToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNjE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
@@ -35,27 +46,51 @@ describe('TokenManager', () => {
describe('Token Validation', () => { describe('Token Validation', () => {
it('should validate correct tokens', () => { it('should validate correct tokens', () => {
const validJwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNjcyNTI3OTk5fQ.Q6cm_sZS6uqfGqO3LQ-0VqNXhqXR6mFh6IP7s0NPnSQ'; const payload = { sub: '123', name: 'Test User', iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + 3600 };
expect(TokenManager.validateToken(validJwt)).toBe(true); const token = jwt.sign(payload, TEST_SECRET);
const result = TokenManager.validateToken(token);
expect(result.valid).toBe(true);
expect(result.error).toBeUndefined();
}); });
it('should reject expired tokens', () => { it('should reject expired tokens', () => {
const expiredToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; const payload = { sub: '123', name: 'Test User', iat: Math.floor(Date.now() / 1000) - 7200, exp: Math.floor(Date.now() / 1000) - 3600 };
expect(TokenManager.validateToken(expiredToken)).toBe(false); const token = jwt.sign(payload, TEST_SECRET);
const result = TokenManager.validateToken(token);
expect(result.valid).toBe(false);
expect(result.error).toBe('Token has expired');
}); });
it('should reject malformed tokens', () => { it('should reject malformed tokens', () => {
expect(TokenManager.validateToken('invalid-token')).toBe(false); const result = TokenManager.validateToken('invalid-token');
expect(result.valid).toBe(false);
expect(result.error).toBe('Token length below minimum requirement');
}); });
it('should reject tokens with invalid signature', () => { it('should reject tokens with invalid signature', () => {
const tamperedToken = validToken.slice(0, -5) + 'xxxxx'; const payload = { sub: '123', name: 'Test User', iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + 3600 };
expect(TokenManager.validateToken(tamperedToken)).toBe(false); const token = jwt.sign(payload, 'different-secret');
const result = TokenManager.validateToken(token);
expect(result.valid).toBe(false);
expect(result.error).toBe('Invalid token signature');
}); });
it('should handle tokens with missing expiration', () => { it('should handle tokens with missing expiration', () => {
const noExpToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.Q6cm_sZS6uqfGqO3LQ-0VqNXhqXR6mFh6IP7s0NPnSQ'; const payload = { sub: '123', name: 'Test User' };
expect(TokenManager.validateToken(noExpToken)).toBe(false); const token = jwt.sign(payload, TEST_SECRET);
const result = TokenManager.validateToken(token);
expect(result.valid).toBe(false);
expect(result.error).toBe('Token missing required claims');
});
it('should handle undefined and null inputs', () => {
const undefinedResult = TokenManager.validateToken(undefined);
expect(undefinedResult.valid).toBe(false);
expect(undefinedResult.error).toBe('Invalid token format');
const nullResult = TokenManager.validateToken(null);
expect(nullResult.valid).toBe(false);
expect(nullResult.error).toBe('Invalid token format');
}); });
}); });
@@ -103,10 +138,5 @@ describe('TokenManager', () => {
it('should handle invalid base64 input', () => { it('should handle invalid base64 input', () => {
expect(() => TokenManager.decryptToken('not-base64!@#$%^', encryptionKey)).toThrow(); expect(() => TokenManager.decryptToken('not-base64!@#$%^', encryptionKey)).toThrow();
}); });
it('should handle undefined and null inputs', () => {
expect(TokenManager.validateToken(undefined as any)).toBe(false);
expect(TokenManager.validateToken(null as any)).toBe(false);
});
}); });
}); });

BIN
bun.lockb Executable file

Binary file not shown.

50
bunfig.toml Normal file
View File

@@ -0,0 +1,50 @@
[test]
preload = ["./src/__tests__/setup.ts"]
coverage = true
coverageThreshold = {
statements = 80,
branches = 70,
functions = 80,
lines = 80
}
timeout = 30000
testMatch = ["**/__tests__/**/*.test.ts"]
testPathIgnorePatterns = ["/node_modules/", "/dist/"]
collectCoverageFrom = [
"src/**/*.{ts,tsx}",
"!src/**/*.d.ts",
"!src/**/*.test.ts",
"!src/types/**/*",
"!src/mocks/**/*"
]
[build]
target = "node"
outdir = "./dist"
minify = true
sourcemap = "external"
[install]
production = false
frozen = true
peer = false
[install.cache]
dir = ".bun"
disable = false
[debug]
port = 9229
[env]
# Environment-specific configurations
development.LOG_LEVEL = "debug"
production.LOG_LEVEL = "warn"
[hot]
restart = true
reload = true
[performance]
gc = true
optimize = true

View File

@@ -417,3 +417,139 @@ async function executeAction() {
console.log('Action result:', data); console.log('Action result:', data);
} }
``` ```
## Security Middleware
### Overview
The security middleware provides a comprehensive set of utility functions to enhance the security of the Home Assistant MCP application. These functions cover various aspects of web security, including:
- Rate limiting
- Request validation
- Input sanitization
- Security headers
- Error handling
### Utility Functions
#### `checkRateLimit(ip: string, maxRequests?: number, windowMs?: number)`
Manages rate limiting for IP addresses to prevent abuse.
**Parameters**:
- `ip`: IP address to track
- `maxRequests`: Maximum number of requests allowed (default: 100)
- `windowMs`: Time window for rate limiting (default: 15 minutes)
**Returns**: `boolean` or throws an error if limit is exceeded
**Example**:
```typescript
try {
checkRateLimit('127.0.0.1'); // Checks rate limit with default settings
} catch (error) {
// Handle rate limit exceeded
}
```
#### `validateRequestHeaders(request: Request, requiredContentType?: string)`
Validates incoming HTTP request headers for security and compliance.
**Parameters**:
- `request`: The incoming HTTP request
- `requiredContentType`: Expected content type (default: 'application/json')
**Checks**:
- Content type
- Request body size
- Authorization header (optional)
**Example**:
```typescript
try {
validateRequestHeaders(request);
} catch (error) {
// Handle validation errors
}
```
#### `sanitizeValue(value: unknown)`
Sanitizes input values to prevent XSS attacks.
**Features**:
- Escapes HTML tags
- Handles nested objects and arrays
- Preserves non-string values
**Example**:
```typescript
const sanitized = sanitizeValue('<script>alert("xss")</script>');
// Returns: '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;'
```
#### `applySecurityHeaders(request: Request, helmetConfig?: HelmetOptions)`
Applies security headers to HTTP requests using Helmet.
**Security Headers**:
- Content Security Policy
- X-Frame-Options
- X-Content-Type-Options
- Referrer Policy
- HSTS (in production)
**Example**:
```typescript
const headers = applySecurityHeaders(request);
```
#### `handleError(error: Error, env?: string)`
Handles error responses with environment-specific details.
**Modes**:
- Production: Generic error message
- Development: Detailed error with stack trace
**Example**:
```typescript
const errorResponse = handleError(error, process.env.NODE_ENV);
```
### Middleware Usage
These utility functions are integrated into Elysia middleware:
```typescript
const app = new Elysia()
.use(rateLimiter) // Rate limiting
.use(validateRequest) // Request validation
.use(sanitizeInput) // Input sanitization
.use(securityHeaders) // Security headers
.use(errorHandler) // Error handling
```
### Best Practices
1. Always validate and sanitize user inputs
2. Use rate limiting to prevent abuse
3. Apply security headers
4. Handle errors gracefully
5. Keep environment-specific error handling
### Security Considerations
- Configurable rate limits
- XSS protection
- Content security policies
- Token validation
- Error information exposure control
### Troubleshooting
- Ensure `JWT_SECRET` is set in environment
- Check content type in requests
- Monitor rate limit errors
- Review error handling in different environments

45
docs/ARCHITECTURE.md Normal file
View File

@@ -0,0 +1,45 @@
# Architecture Documentation for MCP Server
## Overview
The MCP Server is designed as a high-performance, secure, and scalable bridge between Home Assistant and Language Learning Models (LLMs). This document outlines the architectural design principles, core components, and deployment strategies that power the MCP Server.
## Key Architectural Components
### High-Performance Runtime with Bun
- **Fast Startup & Efficiency:** Powered by Bun, the MCP Server benefits from rapid startup times, efficient memory utilization, and native TypeScript support.
- **Optimized Build Process:** Bun's build tools allow for quick iteration and deployment, ensuring minimal downtime and swift performance enhancement.
### Real-time Communication using Server-Sent Events (SSE)
- **Continuous Updates:** The server leverages SSE to deliver real-time notifications and updates, ensuring that any changes in Home Assistant are immediately communicated to connected clients.
- **Scalable Connection Handling:** SSE provides an event-driven model that efficiently manages multiple simultaneous client connections.
### Modular & Extensible Design
- **Plugin Architecture:** Designed with modularity in mind, the MCP Server supports plugins, add-ons, and custom automation scripts, enabling seamless feature expansion without disrupting core functionality.
- **Separation of Concerns:** Different components, such as device management, automation control, and system monitoring, are clearly separated, allowing independent development, testing, and scaling.
### Secure API Integration
- **Token-Based Authentication:** Robust token-based authentication mechanisms restrict access to authorized users and systems.
- **Rate Limiting & Error Handling:** Integrated rate limiting combined with comprehensive error handling ensures system stability and prevents misuse.
- **Best Practices:** All API endpoints follow industry-standard security guidelines to protect data and maintain system integrity.
### Deployment & Scalability
- **Containerized Deployment with Docker:** The use of Docker Compose enables straightforward deployment, management, and scaling of the server and its dependencies.
- **Flexible Environment Configuration:** Environment variables and configuration files (.env) facilitate smooth transitions between development, testing, and production setups.
## Future Enhancements
- **Advanced Automation Logic:** Integration of more complex automation rules and conditional decision-making capabilities.
- **Enhanced Security Measures:** Additional layers of security, such as multi-factor authentication and improved encryption techniques, are on the roadmap.
- **Improved Monitoring & Analytics:** Future updates will introduce advanced performance metrics and real-time analytics to monitor system health and user interactions.
## Conclusion
The architecture of the MCP Server prioritizes performance, scalability, and security. By leveraging Bun's high-performance runtime, employing real-time communication through SSE, and maintaining a modular, secure design, the MCP Server provides a robust platform for integrating Home Assistant with modern LLM functionalities.
*This document is a living document and will be updated as the system evolves.*

36
docs/CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,36 @@
# Contributing to Home Assistant MCP
We welcome contributions from the community! Your help and feedback are essential to improving the MCP Server. Please review the following guidelines before contributing.
## How to Contribute
1. **Fork the Repository**: Create your personal fork of the project on GitHub.
2. **Create a Feature Branch**: Use clear branch names, e.g., `feature/your-feature` or `bugfix/short-description`.
3. **Make Changes**: Develop your feature or fix bugs. Ensure your code follows the project's coding conventions.
4. **Write Tests**: Include tests for new features or bug fixes. We aim for high code coverage.
5. **Submit a Pull Request (PR)**: Once your changes are complete, submit a PR. Our team will review your request and provide feedback.
6. **Address Feedback**: Make any necessary revisions based on code review feedback.
## Code Style Guidelines
- Follow the project's established coding style.
- Use Bun tooling for linting and formatting:
- `bun run lint` for linting
- `bun run format` for formatting
## Documentation
- Update relevant documentation alongside your code changes.
- Ensure that tests pass and coverage remains high.
## Reporting Issues
- Use the GitHub Issues page to report bugs, request new features, or ask questions.
- Provide clear descriptions, replication steps, and any error logs.
## Community
- Join our real-time discussions on our chat platforms (Discord, Slack, etc.).
- Engage with other contributors to exchange ideas and solutions.
Thank you for helping improve the Home Assistant MCP project!

View File

@@ -20,7 +20,7 @@ Before you begin, ensure you have:
2. Install dependencies: 2. Install dependencies:
```bash ```bash
npm install bun install
``` ```
3. Copy the example environment file: 3. Copy the example environment file:
@@ -32,7 +32,7 @@ Before you begin, ensure you have:
```env ```env
# Server Configuration # Server Configuration
PORT=3000 PORT=3000
NODE_ENV=development BUN_ENV=development
# Home Assistant Configuration # Home Assistant Configuration
HASS_HOST=http://your-hass-instance:8123 HASS_HOST=http://your-hass-instance:8123
@@ -47,7 +47,7 @@ Before you begin, ensure you have:
### Environment Variables ### Environment Variables
- `PORT`: The port number for the MCP server (default: 3000) - `PORT`: The port number for the MCP server (default: 3000)
- `NODE_ENV`: The environment mode (development, production, test) - `BUN_ENV`: The environment mode (development, production, test)
- `HASS_HOST`: Your Home Assistant instance URL - `HASS_HOST`: Your Home Assistant instance URL
- `HASS_TOKEN`: Your Home Assistant Long-Lived Access Token - `HASS_TOKEN`: Your Home Assistant Long-Lived Access Token
- `JWT_SECRET`: Secret key for JWT token generation - `JWT_SECRET`: Secret key for JWT token generation
@@ -57,7 +57,7 @@ Before you begin, ensure you have:
For development, you can use: For development, you can use:
```bash ```bash
npm run dev bun run dev
``` ```
This will start the server in development mode with hot reloading. This will start the server in development mode with hot reloading.
@@ -67,8 +67,8 @@ This will start the server in development mode with hot reloading.
For production, build and start the server: For production, build and start the server:
```bash ```bash
npm run build bun run build
npm start bun start
``` ```
## First Steps ## First Steps

51
docs/ROADMAP.md Normal file
View File

@@ -0,0 +1,51 @@
# Roadmap for MCP Server
The following roadmap outlines our planned enhancements and future directions for the Home Assistant MCP Server. This document is a living artifact and will be updated regularly as new features are planned and developed.
## Near-Term Goals
- **Advanced Automation Capabilities:**
- Integrate more sophisticated automation rules with conditional logic and multi-step execution.
- Introduce a visual automation builder to simplify rule creation.
- **Enhanced Security Features:**
- Implement multi-factor authentication for critical actions.
- Strengthen encryption methods and data handling practices.
- Expand monitoring and alerting for potential security breaches.
- **Performance Optimizations:**
- Refine the server's resource utilization to reduce latency.
- Optimize real-time data streaming via SSE for higher throughput.
- Introduce advanced caching mechanisms for frequently requested data.
## Mid-Term Goals
- **User Interface Improvements:**
- Develop an intuitive, web-based dashboard for easier device management and monitoring.
- Provide real-time analytics and performance metrics in the dashboard.
- **Expanded Integrations:**
- Support a broader range of smart home devices and brands.
- Integrate with additional home automation platforms and third-party services.
- **Developer Experience Enhancements:**
- Improve documentation and developer tooling.
- Streamline contribution guidelines and testing setups.
## Long-Term Vision
- **Ecosystem Expansion:**
- Build a modular plugin system that allows community-driven extensions and integrations.
- Enable seamless integration with future technologies in the smart home and AI domains.
- **Scalability and Resilience:**
- Architect the system to support large-scale deployments across multiple instances.
- Incorporate advanced load balancing and failover mechanisms.
## How to Follow the Roadmap
- **Community Involvement:** We encourage community feedback and contributions to help refine and prioritize our roadmap.
- **Regular Updates:** This document is regularly updated with new goals, milestones, and completed tasks.
- **Transparency:** Check the project's GitHub repository and issues for ongoing discussions and updates related to roadmap items.
*This roadmap is intended to serve as a guide and may evolve based on community needs, technological advancements, and strategic priorities.*

514
docs/TESTING.md Normal file
View File

@@ -0,0 +1,514 @@
# Testing Documentation
## Quick Reference
```bash
# Most Common Commands
bun test # Run all tests
bun test --watch # Run tests in watch mode
bun test --coverage # Run tests with coverage
bun test path/to/test.ts # Run specific test file
# Additional Options
DEBUG=true bun test # Run with debug output
bun test --pattern "auth" # Run tests matching pattern
bun test --timeout 60000 # Run with custom timeout
```
## Overview
This document describes the testing setup and practices used in the Home Assistant MCP project. The project uses Bun's test runner for unit and integration testing, with a comprehensive test suite covering security, SSE (Server-Sent Events), middleware, and other core functionalities.
## Test Structure
Tests are organized in two main locations:
1. **Root Level Integration Tests** (`/__tests__/`):
```
__tests__/
├── ai/ # AI/ML component tests
├── api/ # API integration tests
├── context/ # Context management tests
├── hass/ # Home Assistant integration tests
├── schemas/ # Schema validation tests
├── security/ # Security integration tests
├── tools/ # Tools and utilities tests
├── websocket/ # WebSocket integration tests
├── helpers.test.ts # Helper function tests
├── index.test.ts # Main application tests
└── server.test.ts # Server integration tests
```
2. **Component Level Unit Tests** (`src/**/`):
```
src/
├── __tests__/ # Global test setup and utilities
│ └── setup.ts # Global test configuration
├── component/
│ ├── __tests__/ # Component-specific unit tests
│ └── component.ts
```
The root level `__tests__` directory contains integration and end-to-end tests that verify the interaction between different components of the system, while the component-level tests focus on unit testing individual modules.
## Test Configuration
### Bun Test Configuration (`bunfig.toml`)
```toml
[test]
preload = ["./src/__tests__/setup.ts"] # Global test setup
coverage = true # Enable coverage by default
timeout = 30000 # Test timeout in milliseconds
testMatch = ["**/__tests__/**/*.test.ts"] # Test file patterns
```
### Bun Scripts
Available test commands in `package.json`:
```bash
# Run all tests
bun test
# Watch mode for development
bun test --watch
# Generate coverage report
bun test --coverage
# Run linting
bun run lint
# Format code
bun run format
```
## Test Setup
### Global Configuration
The project uses a global test setup file (`src/__tests__/setup.ts`) that provides:
- Environment configuration
- Mock utilities
- Test helper functions
- Global test lifecycle hooks
### Test Environment
Tests run with the following configuration:
- Environment variables are loaded from `.env.test`
- Console output is suppressed during tests (unless DEBUG=true)
- JWT secrets and tokens are automatically configured for testing
- Rate limiting and other security features are properly initialized
## Running Tests
To run the test suite:
```bash
# Basic test run
bun test
# Run tests with coverage
bun test --coverage
# Run specific test file
bun test path/to/test.test.ts
# Run tests in watch mode
bun test --watch
# Run tests with debug output
DEBUG=true bun test
# Run tests with increased timeout
bun test --timeout 60000
# Run tests matching a pattern
bun test --pattern "auth"
```
### Test Environment Setup
1. **Prerequisites**:
- Bun >= 1.0.0
- Node.js dependencies (see package.json)
2. **Environment Files**:
- `.env.test` - Test environment variables
- `.env.development` - Development environment variables
3. **Test Data**:
- Mock responses in `__tests__/mock-responses/`
- Test fixtures in `__tests__/fixtures/`
### Continuous Integration
The project uses GitHub Actions for CI/CD. Tests are automatically run on:
- Pull requests
- Pushes to main branch
- Release tags
## Writing Tests
### Test File Naming
- Test files should be placed in a `__tests__` directory adjacent to the code being tested
- Test files should be named `*.test.ts`
- Test files should mirror the structure of the source code
### Test Structure
```typescript
import { describe, expect, it, beforeEach } from "bun:test";
describe("Module Name", () => {
beforeEach(() => {
// Setup for each test
});
describe("Feature/Function Name", () => {
it("should do something specific", () => {
// Test implementation
});
});
});
```
### Test Utilities
The project provides several test utilities:
```typescript
import { testUtils } from "../__tests__/setup";
// Available utilities:
- mockWebSocket() // Mock WebSocket for SSE tests
- mockResponse() // Mock HTTP response for API tests
- mockRequest() // Mock HTTP request for API tests
- createTestClient() // Create test SSE client
- createTestEvent() // Create test event
- createTestEntity() // Create test Home Assistant entity
- wait() // Helper to wait for async operations
```
## Testing Patterns
### Security Testing
Security tests cover:
- Token validation and encryption
- Rate limiting
- Request validation
- Input sanitization
- Error handling
Example:
```typescript
describe("Security Features", () => {
it("should validate tokens correctly", () => {
const payload = { userId: "123", role: "user" };
const token = jwt.sign(payload, validSecret, { expiresIn: "1h" });
const result = TokenManager.validateToken(token, testIp);
expect(result.valid).toBe(true);
});
});
```
### SSE Testing
SSE tests cover:
- Client authentication
- Message broadcasting
- Rate limiting
- Subscription management
- Client cleanup
Example:
```typescript
describe("SSE Features", () => {
it("should authenticate valid clients", () => {
const client = createTestClient("test-client");
const result = sseManager.addClient(client, validToken);
expect(result?.authenticated).toBe(true);
});
});
```
### Middleware Testing
Middleware tests cover:
- Request validation
- Input sanitization
- Error handling
- Response formatting
Example:
```typescript
describe("Middleware", () => {
it("should sanitize HTML in request body", () => {
const req = mockRequest({
body: { text: '<script>alert("xss")</script>' }
});
sanitizeInput(req, res, next);
expect(req.body.text).toBe("");
});
});
```
### Integration Testing
Integration tests in the root `__tests__` directory cover:
- **AI/ML Components**: Testing machine learning model integrations and predictions
- **API Integration**: End-to-end API route testing
- **Context Management**: Testing context persistence and state management
- **Home Assistant Integration**: Testing communication with Home Assistant
- **Schema Validation**: Testing data validation across the application
- **Security Integration**: Testing security features in a full system context
- **WebSocket Communication**: Testing real-time communication
- **Server Integration**: Testing the complete server setup and configuration
Example integration test:
```typescript
describe("API Integration", () => {
it("should handle a complete authentication flow", async () => {
// Setup test client
const client = await createTestClient();
// Test registration
const regResponse = await client.register(testUser);
expect(regResponse.status).toBe(201);
// Test authentication
const authResponse = await client.authenticate(testCredentials);
expect(authResponse.status).toBe(200);
expect(authResponse.body.token).toBeDefined();
// Test protected endpoint access
const protectedResponse = await client.get("/api/protected", {
headers: { Authorization: `Bearer ${authResponse.body.token}` }
});
expect(protectedResponse.status).toBe(200);
});
});
```
## Security Middleware Testing
### Utility Function Testing
The security middleware now uses a utility-first approach, which allows for more granular and comprehensive testing. Each security function is now independently testable, improving code reliability and maintainability.
#### Key Utility Functions
1. **Rate Limiting (`checkRateLimit`)**
- Tests multiple scenarios:
- Requests under threshold
- Requests exceeding threshold
- Rate limit reset after window expiration
```typescript
// Example test
it('should throw when requests exceed threshold', () => {
const ip = '127.0.0.2';
for (let i = 0; i < 11; i++) {
if (i < 10) {
expect(() => checkRateLimit(ip, 10)).not.toThrow();
} else {
expect(() => checkRateLimit(ip, 10)).toThrow('Too many requests from this IP');
}
}
});
```
2. **Request Validation (`validateRequestHeaders`)**
- Tests content type validation
- Checks request size limits
- Validates authorization headers
```typescript
it('should reject invalid content type', () => {
const mockRequest = new Request('http://localhost', {
method: 'POST',
headers: { 'content-type': 'text/plain' }
});
expect(() => validateRequestHeaders(mockRequest)).toThrow('Content-Type must be application/json');
});
```
3. **Input Sanitization (`sanitizeValue`)**
- Sanitizes HTML tags
- Handles nested objects
- Preserves non-string values
```typescript
it('should sanitize HTML tags', () => {
const input = '<script>alert("xss")</script>Hello';
const sanitized = sanitizeValue(input);
expect(sanitized).toBe('&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;Hello');
});
```
4. **Security Headers (`applySecurityHeaders`)**
- Verifies correct security header application
- Checks CSP, frame options, and other security headers
```typescript
it('should apply security headers', () => {
const mockRequest = new Request('http://localhost');
const headers = applySecurityHeaders(mockRequest);
expect(headers['content-security-policy']).toBeDefined();
expect(headers['x-frame-options']).toBeDefined();
});
```
5. **Error Handling (`handleError`)**
- Tests error responses in production and development modes
- Verifies error message and stack trace inclusion
```typescript
it('should include error details in development mode', () => {
const error = new Error('Test error');
const result = handleError(error, 'development');
expect(result).toEqual({
error: true,
message: 'Internal server error',
error: 'Test error',
stack: expect.any(String)
});
});
```
### Testing Philosophy
- **Isolation**: Each utility function is tested independently
- **Comprehensive Coverage**: Multiple scenarios for each function
- **Predictable Behavior**: Clear expectations for input and output
- **Error Handling**: Robust testing of error conditions
### Best Practices
1. Use minimal, focused test cases
2. Test both successful and failure scenarios
3. Verify input sanitization and security measures
4. Mock external dependencies when necessary
### Running Security Tests
```bash
# Run all tests
bun test
# Run specific security tests
bun test __tests__/security/
```
### Continuous Improvement
- Regularly update test cases
- Add new test scenarios as security requirements evolve
- Perform periodic security audits
## Best Practices
1. **Isolation**: Each test should be independent and not rely on the state of other tests.
2. **Mocking**: Use the provided mock utilities for external dependencies.
3. **Cleanup**: Clean up any resources or state modifications in `afterEach` or `afterAll` hooks.
4. **Descriptive Names**: Use clear, descriptive test names that explain the expected behavior.
5. **Assertions**: Make specific, meaningful assertions rather than general ones.
6. **Setup**: Use `beforeEach` for common test setup to avoid repetition.
7. **Error Cases**: Test both success and error cases for complete coverage.
## Coverage
The project aims for high test coverage, particularly focusing on:
- Security-critical code paths
- API endpoints
- Data validation
- Error handling
- Event broadcasting
Run coverage reports using:
```bash
bun test --coverage
```
## Debugging Tests
To debug tests:
1. Set `DEBUG=true` to enable console output during tests
2. Use the `--watch` flag for development
3. Add `console.log()` statements (they're only shown when DEBUG is true)
4. Use the test utilities' debugging helpers
### Advanced Debugging
1. **Using Node Inspector**:
```bash
# Start tests with inspector
bun test --inspect
# Start tests with inspector and break on first line
bun test --inspect-brk
```
2. **Using VS Code**:
```jsonc
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "bun",
"request": "launch",
"name": "Debug Tests",
"program": "${workspaceFolder}/node_modules/bun/bin/bun",
"args": ["test", "${file}"],
"cwd": "${workspaceFolder}",
"env": { "DEBUG": "true" }
}
]
}
```
3. **Test Isolation**:
To run a single test in isolation:
```typescript
describe.only("specific test suite", () => {
it.only("specific test case", () => {
// Only this test will run
});
});
```
## Contributing
When contributing new code:
1. Add tests for new features
2. Ensure existing tests pass
3. Maintain or improve coverage
4. Follow the existing test patterns and naming conventions
5. Document any new test utilities or patterns
## Coverage Requirements
The project maintains strict coverage requirements:
- Minimum overall coverage: 80%
- Critical paths (security, API, data validation): 90%
- New features must include tests with >= 85% coverage
Coverage reports are generated in multiple formats:
- Console summary
- HTML report (./coverage/index.html)
- LCOV report (./coverage/lcov.info)
To view detailed coverage:
```bash
# Generate and open coverage report
bun test --coverage && open coverage/index.html
```

354
docs/TROUBLESHOOTING.md Normal file
View File

@@ -0,0 +1,354 @@
# Troubleshooting Guide
This guide helps you diagnose and fix common issues with the Home Assistant MCP.
## Common Issues
### Connection Issues
#### Cannot Connect to Home Assistant
**Symptoms:**
- Connection timeout errors
- "Failed to connect to Home Assistant" messages
- 401 Unauthorized errors
**Solutions:**
1. Verify Home Assistant is running
2. Check HASS_HOST environment variable
3. Validate HASS_TOKEN is correct
4. Ensure network connectivity
5. Check firewall settings
#### SSE Connection Drops
**Symptoms:**
- Frequent disconnections
- Missing events
- Connection reset errors
**Solutions:**
1. Check network stability
2. Increase connection timeout
3. Implement reconnection logic
4. Monitor server resources
### Authentication Issues
#### Invalid Token
**Symptoms:**
- 401 Unauthorized responses
- "Invalid token" messages
- Authentication failures
**Solutions:**
1. Generate new Long-Lived Access Token
2. Check token expiration
3. Verify token format
4. Update environment variables
#### Rate Limiting
**Symptoms:**
- 429 Too Many Requests
- "Rate limit exceeded" messages
**Solutions:**
1. Implement request throttling
2. Adjust rate limit settings
3. Cache responses
4. Optimize request patterns
### Tool Issues
#### Tool Not Found
**Symptoms:**
- "Tool not found" errors
- 404 Not Found responses
**Solutions:**
1. Check tool name spelling
2. Verify tool registration
3. Update tool imports
4. Check tool availability
#### Tool Execution Fails
**Symptoms:**
- Tool execution errors
- Unexpected responses
- Timeout issues
**Solutions:**
1. Validate input parameters
2. Check error logs
3. Debug tool implementation
4. Verify Home Assistant permissions
## Debugging
### Server Logs
1. Enable debug logging:
```env
LOG_LEVEL=debug
```
2. Check logs:
```bash
npm run logs
```
3. Filter logs:
```bash
npm run logs | grep "error"
```
### Network Debugging
1. Check API endpoints:
```bash
curl -v http://localhost:3000/api/health
```
2. Monitor SSE connections:
```bash
curl -N http://localhost:3000/api/sse/stats
```
3. Test WebSocket:
```bash
wscat -c ws://localhost:3000
```
### Performance Issues
1. Monitor memory usage:
```bash
npm run stats
```
2. Check response times:
```bash
curl -w "%{time_total}\n" -o /dev/null -s http://localhost:3000/api/health
```
3. Profile code:
```bash
npm run profile
```
## FAQ
### Q: How do I reset my configuration?
A: Delete `.env` and copy `.env.example` to start fresh.
### Q: Why are my events delayed?
A: Check network latency and server load. Consider adjusting buffer sizes.
### Q: How do I update my token?
A: Generate a new token in Home Assistant and update HASS_TOKEN.
### Q: Why do I get "Maximum clients reached"?
A: Adjust SSE_MAX_CLIENTS in configuration or clean up stale connections.
## Error Codes
- `E001`: Connection Error
- `E002`: Authentication Error
- `E003`: Rate Limit Error
- `E004`: Tool Error
- `E005`: Configuration Error
## Support Resources
1. Documentation
- [API Reference](./API.md)
- [Configuration Guide](./configuration/README.md)
- [Development Guide](./development/README.md)
2. Community
- GitHub Issues
- Discussion Forums
- Stack Overflow
3. Tools
- Diagnostic Scripts
- Testing Tools
- Monitoring Tools
## Still Need Help?
1. Create a detailed issue:
- Error messages
- Steps to reproduce
- Environment details
- Logs
2. Contact support:
- GitHub Issues
- Email Support
- Community Forums
## Security Middleware Troubleshooting
### Common Issues and Solutions
#### Rate Limiting Problems
**Symptom**: Unexpected 429 (Too Many Requests) errors
**Possible Causes**:
- Misconfigured rate limit settings
- Shared IP addresses (e.g., behind NAT)
- Aggressive client-side retry mechanisms
**Solutions**:
1. Adjust rate limit parameters
```typescript
// Customize rate limit for specific scenarios
checkRateLimit(ip, maxRequests = 200, windowMs = 30 * 60 * 1000)
```
2. Implement more granular rate limiting
- Use different limits for different endpoints
- Consider user authentication level
#### Request Validation Failures
**Symptom**: 400 or 415 status codes on valid requests
**Possible Causes**:
- Incorrect `Content-Type` header
- Large request payloads
- Malformed authorization headers
**Debugging Steps**:
1. Verify request headers
```typescript
// Check content type and size
validateRequestHeaders(request, 'application/json')
```
2. Log detailed validation errors
```typescript
try {
validateRequestHeaders(request);
} catch (error) {
console.error('Request validation failed:', error.message);
}
```
#### Input Sanitization Issues
**Symptom**: Unexpected data transformation or loss
**Possible Causes**:
- Complex nested objects
- Non-standard input formats
- Overly aggressive sanitization
**Troubleshooting**:
1. Test sanitization with various input types
```typescript
const input = {
text: '<script>alert("xss")</script>',
nested: { html: '<img src="x" onerror="alert(1)">World' }
};
const sanitized = sanitizeValue(input);
```
2. Custom sanitization for specific use cases
```typescript
function customSanitize(value) {
// Add custom sanitization logic
return sanitizeValue(value);
}
```
#### Security Header Configuration
**Symptom**: Missing or incorrect security headers
**Possible Causes**:
- Misconfigured Helmet options
- Environment-specific header requirements
**Solutions**:
1. Custom security header configuration
```typescript
const customHelmetConfig = {
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", 'trusted-cdn.com']
}
}
};
applySecurityHeaders(request, customHelmetConfig);
```
#### Error Handling and Logging
**Symptom**: Inconsistent error responses
**Possible Causes**:
- Incorrect environment configuration
- Unhandled error types
**Debugging Techniques**:
1. Verify environment settings
```typescript
const errorResponse = handleError(error, process.env.NODE_ENV);
```
2. Add custom error handling
```typescript
function enhancedErrorHandler(error, env) {
// Add custom logging or monitoring
console.error('Security error:', error);
return handleError(error, env);
}
```
### Performance and Security Monitoring
1. **Logging**
- Enable debug logging for security events
- Monitor rate limit and validation logs
2. **Metrics**
- Track rate limit hit rates
- Monitor request validation success/failure ratios
3. **Continuous Improvement**
- Regularly review and update security configurations
- Conduct periodic security audits
### Environment-Specific Considerations
#### Development
- More verbose error messages
- Relaxed rate limiting
- Detailed security logs
#### Production
- Minimal error details
- Strict rate limiting
- Comprehensive security headers
### External Resources
- [OWASP Security Guidelines](https://owasp.org/www-project-top-ten/)
- [Helmet.js Documentation](https://helmetjs.github.io/)
- [JWT Security Best Practices](https://jwt.io/introduction)
### Getting Help
If you encounter persistent issues:
1. Check application logs
2. Verify environment configurations
3. Consult the project's issue tracker
4. Reach out to the development team with detailed error information

View File

@@ -7,6 +7,8 @@ This guide provides information for developers who want to contribute to or exte
``` ```
homeassistant-mcp/ homeassistant-mcp/
├── src/ ├── src/
│ ├── __tests__/ # Test files
│ ├── __mocks__/ # Mock files
│ ├── api/ # API endpoints and route handlers │ ├── api/ # API endpoints and route handlers
│ ├── config/ # Configuration management │ ├── config/ # Configuration management
│ ├── hass/ # Home Assistant integration │ ├── hass/ # Home Assistant integration

View File

@@ -1,193 +0,0 @@
# Troubleshooting Guide
This guide helps you diagnose and fix common issues with the Home Assistant MCP.
## Common Issues
### Connection Issues
#### Cannot Connect to Home Assistant
**Symptoms:**
- Connection timeout errors
- "Failed to connect to Home Assistant" messages
- 401 Unauthorized errors
**Solutions:**
1. Verify Home Assistant is running
2. Check HASS_HOST environment variable
3. Validate HASS_TOKEN is correct
4. Ensure network connectivity
5. Check firewall settings
#### SSE Connection Drops
**Symptoms:**
- Frequent disconnections
- Missing events
- Connection reset errors
**Solutions:**
1. Check network stability
2. Increase connection timeout
3. Implement reconnection logic
4. Monitor server resources
### Authentication Issues
#### Invalid Token
**Symptoms:**
- 401 Unauthorized responses
- "Invalid token" messages
- Authentication failures
**Solutions:**
1. Generate new Long-Lived Access Token
2. Check token expiration
3. Verify token format
4. Update environment variables
#### Rate Limiting
**Symptoms:**
- 429 Too Many Requests
- "Rate limit exceeded" messages
**Solutions:**
1. Implement request throttling
2. Adjust rate limit settings
3. Cache responses
4. Optimize request patterns
### Tool Issues
#### Tool Not Found
**Symptoms:**
- "Tool not found" errors
- 404 Not Found responses
**Solutions:**
1. Check tool name spelling
2. Verify tool registration
3. Update tool imports
4. Check tool availability
#### Tool Execution Fails
**Symptoms:**
- Tool execution errors
- Unexpected responses
- Timeout issues
**Solutions:**
1. Validate input parameters
2. Check error logs
3. Debug tool implementation
4. Verify Home Assistant permissions
## Debugging
### Server Logs
1. Enable debug logging:
```env
LOG_LEVEL=debug
```
2. Check logs:
```bash
npm run logs
```
3. Filter logs:
```bash
npm run logs | grep "error"
```
### Network Debugging
1. Check API endpoints:
```bash
curl -v http://localhost:3000/api/health
```
2. Monitor SSE connections:
```bash
curl -N http://localhost:3000/api/sse/stats
```
3. Test WebSocket:
```bash
wscat -c ws://localhost:3000
```
### Performance Issues
1. Monitor memory usage:
```bash
npm run stats
```
2. Check response times:
```bash
curl -w "%{time_total}\n" -o /dev/null -s http://localhost:3000/api/health
```
3. Profile code:
```bash
npm run profile
```
## FAQ
### Q: How do I reset my configuration?
A: Delete `.env` and copy `.env.example` to start fresh.
### Q: Why are my events delayed?
A: Check network latency and server load. Consider adjusting buffer sizes.
### Q: How do I update my token?
A: Generate a new token in Home Assistant and update HASS_TOKEN.
### Q: Why do I get "Maximum clients reached"?
A: Adjust SSE_MAX_CLIENTS in configuration or clean up stale connections.
## Error Codes
- `E001`: Connection Error
- `E002`: Authentication Error
- `E003`: Rate Limit Error
- `E004`: Tool Error
- `E005`: Configuration Error
## Support Resources
1. Documentation
- [API Reference](./API.md)
- [Configuration Guide](./configuration/README.md)
- [Development Guide](./development/README.md)
2. Community
- GitHub Issues
- Discussion Forums
- Stack Overflow
3. Tools
- Diagnostic Scripts
- Testing Tools
- Monitoring Tools
## Still Need Help?
1. Create a detailed issue:
- Error messages
- Steps to reproduce
- Environment details
- Logs
2. Contact support:
- GitHub Issues
- Email Support
- Community Forums

View File

@@ -1,85 +0,0 @@
const path = require('path');
module.exports = (request, options) => {
// Handle chalk and related packages
if (request === 'chalk' || request === '#ansi-styles' || request === '#supports-color') {
return path.resolve(__dirname, 'node_modules', request.replace('#', ''));
}
// Handle source files with .js extension
if (request.endsWith('.js')) {
const tsRequest = request.replace(/\.js$/, '.ts');
try {
return options.defaultResolver(tsRequest, {
...options,
packageFilter: pkg => {
if (pkg.type === 'module') {
if (pkg.exports && pkg.exports.import) {
pkg.main = pkg.exports.import;
} else if (pkg.module) {
pkg.main = pkg.module;
}
}
return pkg;
}
});
} catch (e) {
// If the .ts file doesn't exist, try resolving without extension
try {
return options.defaultResolver(request.replace(/\.js$/, ''), options);
} catch (e2) {
// If that fails too, try resolving with .ts extension
try {
return options.defaultResolver(tsRequest, options);
} catch (e3) {
// If all attempts fail, try resolving the original request
return options.defaultResolver(request, options);
}
}
}
}
// Handle @digital-alchemy packages
if (request.startsWith('@digital-alchemy/')) {
try {
const packagePath = path.resolve(__dirname, 'node_modules', request);
return options.defaultResolver(packagePath, {
...options,
packageFilter: pkg => {
if (pkg.type === 'module') {
if (pkg.exports && pkg.exports.import) {
pkg.main = pkg.exports.import;
} else if (pkg.module) {
pkg.main = pkg.module;
}
}
return pkg;
}
});
} catch (e) {
// If resolution fails, continue with default resolver
}
}
// Call the default resolver with enhanced module resolution
return options.defaultResolver(request, {
...options,
// Handle ESM modules
packageFilter: pkg => {
if (pkg.type === 'module') {
if (pkg.exports) {
if (pkg.exports.import) {
pkg.main = pkg.exports.import;
} else if (typeof pkg.exports === 'string') {
pkg.main = pkg.exports;
}
} else if (pkg.module) {
pkg.main = pkg.module;
}
}
return pkg;
},
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
paths: [...(options.paths || []), path.resolve(__dirname, 'src')]
});
};

View File

@@ -1,17 +0,0 @@
/** @type {import('bun:test').BunTestConfig} */
module.exports = {
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
testMatch: ['**/__tests__/**/*.test.ts'],
collectCoverage: true,
coverageDirectory: 'coverage',
coverageThreshold: {
global: {
statements: 50,
branches: 50,
functions: 50,
lines: 50
}
},
setupFilesAfterEnv: ['./jest.setup.ts']
};

View File

@@ -1,87 +0,0 @@
import { jest } from '@jest/globals';
import dotenv from 'dotenv';
import { TextEncoder, TextDecoder } from 'util';
// Load test environment variables
dotenv.config({ path: '.env.test' });
// Set test environment
process.env.NODE_ENV = 'test';
process.env.ENCRYPTION_KEY = 'test-encryption-key-32-bytes-long!!!';
process.env.JWT_SECRET = 'test-jwt-secret';
process.env.HASS_URL = 'http://localhost:8123';
process.env.HASS_TOKEN = 'test-token';
process.env.CLAUDE_API_KEY = 'test_api_key';
process.env.CLAUDE_MODEL = 'test_model';
// Add TextEncoder and TextDecoder to global scope
Object.defineProperty(global, 'TextEncoder', {
value: TextEncoder,
writable: true
});
Object.defineProperty(global, 'TextDecoder', {
value: TextDecoder,
writable: true
});
// Configure console for tests
const originalConsole = { ...console };
global.console = {
...console,
log: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
};
// Increase test timeout
jest.setTimeout(30000);
// Mock WebSocket
jest.mock('ws', () => {
return {
WebSocket: jest.fn().mockImplementation(() => ({
on: jest.fn(),
send: jest.fn(),
close: jest.fn(),
removeAllListeners: jest.fn()
}))
};
});
// Mock chalk
const createChalkMock = () => {
const handler = {
get(target: any, prop: string) {
if (prop === 'default') {
return createChalkMock();
}
return typeof prop === 'string' ? createChalkMock() : target[prop];
},
apply(target: any, thisArg: any, args: any[]) {
return args[0];
}
};
return new Proxy(() => { }, handler);
};
jest.mock('chalk', () => createChalkMock());
// Mock ansi-styles
jest.mock('ansi-styles', () => ({}), { virtual: true });
// Mock supports-color
jest.mock('supports-color', () => ({}), { virtual: true });
// Reset mocks between tests
beforeEach(() => {
jest.clearAllMocks();
});
// Cleanup after tests
afterEach(() => {
jest.clearAllTimers();
jest.clearAllMocks();
});

View File

@@ -1,66 +1,59 @@
{ {
"name": "homeassistant-mcp", "name": "homeassistant-mcp",
"version": "0.1.0", "version": "1.0.0",
"description": "Model Context Protocol Server for Home Assistant", "description": "Home Assistant Master Control Program",
"type": "module",
"main": "dist/index.js", "main": "dist/index.js",
"type": "module",
"scripts": { "scripts": {
"build": "bun run tsc", "start": "bun run dist/index.js",
"start": "bun run dist/src/index.js", "dev": "bun --hot --watch src/index.ts",
"dev": "bun --watch src/index.ts", "build": "bun build ./src/index.ts --outdir ./dist --target node --minify",
"test": "bun test", "test": "bun test",
"test:coverage": "bun test --coverage",
"test:watch": "bun test --watch", "test:watch": "bun test --watch",
"test:openai": "bun run openai_test.ts", "test:coverage": "bun test --coverage",
"lint": "eslint src --ext .ts", "test:ci": "bun test --coverage --bail",
"lint:fix": "eslint src --ext .ts --fix", "test:update": "bun test --update-snapshots",
"prepare": "bun run build", "test:clear": "bun test --clear-cache",
"clean": "rimraf dist", "test:staged": "bun test --findRelatedTests",
"types:check": "tsc --noEmit", "lint": "eslint . --ext .ts",
"types:install": "bun add -d @types/node @types/jest" "format": "prettier --write \"src/**/*.ts\"",
"prepare": "husky install",
"profile": "bun --inspect src/index.ts",
"clean": "rm -rf dist .bun coverage",
"typecheck": "bun x tsc --noEmit",
"preinstall": "bun install --frozen-lockfile"
}, },
"dependencies": { "dependencies": {
"@digital-alchemy/core": "^24.11.4", "@elysiajs/cors": "^1.2.0",
"@digital-alchemy/hass": "^24.11.4", "@elysiajs/swagger": "^1.2.0",
"@types/chalk": "^0.4.31", "@types/jsonwebtoken": "^9.0.5",
"@types/jsonwebtoken": "^9.0.8", "@types/node": "^20.11.24",
"@types/xmldom": "^0.1.34", "@types/sanitize-html": "^2.9.5",
"@xmldom/xmldom": "^0.9.7", "@types/ws": "^8.5.10",
"ajv": "^8.12.0", "dotenv": "^16.4.5",
"chalk": "^5.4.1", "elysia": "^1.2.11",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-rate-limit": "^7.1.5",
"helmet": "^7.1.0", "helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"litemcp": "^0.7.0", "node-fetch": "^3.3.2",
"uuid": "^9.0.1", "sanitize-html": "^2.11.0",
"winston-daily-rotate-file": "^5.0.0", "typescript": "^5.3.3",
"ws": "^8.16.0", "ws": "^8.16.0",
"zod": "^3.22.4" "zod": "^3.22.4"
}, },
"devDependencies": { "devDependencies": {
"@types/ajv": "^1.0.0", "@types/uuid": "^10.0.0",
"@types/express": "^4.17.21", "@typescript-eslint/eslint-plugin": "^7.1.0",
"@types/express-rate-limit": "^6.0.0", "@typescript-eslint/parser": "^7.1.0",
"@types/glob": "^8.1.0", "bun-types": "^1.2.2",
"@types/helmet": "^4.0.0", "eslint": "^8.57.0",
"@types/jest": "^29.5.14", "eslint-config-prettier": "^9.1.0",
"@types/node": "^20.17.16", "eslint-plugin-prettier": "^5.1.3",
"@types/supertest": "^6.0.2", "husky": "^9.0.11",
"@types/uuid": "^9.0.8", "prettier": "^3.2.5",
"@types/winston": "^2.4.4", "supertest": "^6.3.3",
"@types/ws": "^8.5.10", "uuid": "^11.0.5"
"jest": "^29.7.0",
"node-fetch": "^3.3.2",
"openai": "^4.82.0",
"rimraf": "^5.0.10",
"supertest": "^6.3.4",
"ts-jest": "^29.1.2",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
}, },
"author": "Jango Blockchained", "engines": {
"license": "MIT", "bun": ">=1.0.0"
"packageManager": "bun@1.0.26" }
} }

View File

@@ -0,0 +1,77 @@
import { mock } from "bun:test";
export const LIB_HASS = {
configuration: {
name: "Home Assistant",
version: "2024.2.0",
location_name: "Home",
time_zone: "UTC",
components: ["automation", "script", "light", "switch"],
unit_system: {
temperature: "°C",
length: "m",
mass: "kg",
pressure: "hPa",
volume: "L",
},
},
services: {
light: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve()),
},
switch: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve()),
},
automation: {
trigger: mock(() => Promise.resolve()),
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
},
script: {
turn_on: mock(() => Promise.resolve()),
turn_off: mock(() => Promise.resolve()),
toggle: mock(() => Promise.resolve()),
},
},
states: {
light: {
"light.living_room": {
state: "on",
attributes: {
brightness: 255,
color_temp: 300,
friendly_name: "Living Room Light",
},
},
"light.bedroom": {
state: "off",
attributes: {
friendly_name: "Bedroom Light",
},
},
},
switch: {
"switch.tv": {
state: "off",
attributes: {
friendly_name: "TV",
},
},
},
},
events: {
subscribe: mock(() => Promise.resolve()),
unsubscribe: mock(() => Promise.resolve()),
fire: mock(() => Promise.resolve()),
},
connection: {
subscribeEvents: mock(() => Promise.resolve()),
subscribeMessage: mock(() => Promise.resolve()),
sendMessage: mock(() => Promise.resolve()),
close: mock(() => Promise.resolve()),
},
};

61
src/__mocks__/litemcp.ts Normal file
View File

@@ -0,0 +1,61 @@
export class LiteMCP {
name: string;
version: string;
config: any;
constructor(config: any = {}) {
this.name = "home-assistant";
this.version = "1.0.0";
this.config = config;
}
async start() {
return Promise.resolve();
}
async stop() {
return Promise.resolve();
}
async connect() {
return Promise.resolve();
}
async disconnect() {
return Promise.resolve();
}
async callService(domain: string, service: string, data: any = {}) {
return Promise.resolve({ success: true });
}
async getStates() {
return Promise.resolve([]);
}
async getState(entityId: string) {
return Promise.resolve({
entity_id: entityId,
state: "unknown",
attributes: {},
last_changed: new Date().toISOString(),
last_updated: new Date().toISOString(),
});
}
async setState(entityId: string, state: string, attributes: any = {}) {
return Promise.resolve({ success: true });
}
onStateChanged(callback: (event: any) => void) {
// Mock implementation
}
onEvent(eventType: string, callback: (event: any) => void) {
// Mock implementation
}
}
export const createMCP = (config: any = {}) => {
return new LiteMCP(config);
};

143
src/__tests__/setup.ts Normal file
View File

@@ -0,0 +1,143 @@
import { config } from "dotenv";
import path from "path";
import {
beforeAll,
afterAll,
beforeEach,
describe,
expect,
it,
mock,
test,
} from "bun:test";
// Type definitions for mocks
type MockFn = ReturnType<typeof mock>;
interface MockInstance {
mock: {
calls: unknown[][];
results: unknown[];
instances: unknown[];
lastCall?: unknown[];
};
}
// Test configuration
const TEST_CONFIG = {
TEST_JWT_SECRET: "test_jwt_secret_key_that_is_at_least_32_chars",
TEST_TOKEN: "test_token_that_is_at_least_32_chars_long",
TEST_CLIENT_IP: "127.0.0.1",
};
// Load test environment variables
config({ path: path.resolve(process.cwd(), ".env.test") });
// Global test setup
beforeAll(() => {
// Set required environment variables
process.env.NODE_ENV = "test";
process.env.JWT_SECRET = TEST_CONFIG.TEST_JWT_SECRET;
process.env.TEST_TOKEN = TEST_CONFIG.TEST_TOKEN;
// Configure console output for tests
if (!process.env.DEBUG) {
console.error = mock(() => { });
console.warn = mock(() => { });
console.log = mock(() => { });
}
});
// Reset mocks between tests
beforeEach(() => {
// Clear all mock function calls
const mockFns = Object.values(mock).filter(
(value): value is MockFn => typeof value === "function" && "mock" in value,
);
mockFns.forEach((mockFn) => {
if (mockFn.mock) {
mockFn.mock.calls = [];
mockFn.mock.results = [];
mockFn.mock.instances = [];
mockFn.mock.lastCall = undefined;
}
});
});
// Custom test utilities
const testUtils = {
// Mock WebSocket for SSE tests
mockWebSocket: () => ({
on: mock(() => { }),
send: mock(() => { }),
close: mock(() => { }),
readyState: 1,
OPEN: 1,
removeAllListeners: mock(() => { }),
}),
// Mock HTTP response for API tests
mockResponse: () => {
const res = {
status: mock(() => res),
json: mock(() => res),
send: mock(() => res),
end: mock(() => res),
setHeader: mock(() => res),
writeHead: mock(() => res),
write: mock(() => true),
removeHeader: mock(() => res),
};
return res;
},
// Mock HTTP request for API tests
mockRequest: (overrides: Record<string, unknown> = {}) => ({
headers: { "content-type": "application/json" },
body: {},
query: {},
params: {},
ip: TEST_CONFIG.TEST_CLIENT_IP,
method: "GET",
path: "/api/test",
is: mock((type: string) => type === "application/json"),
...overrides,
}),
// Create test client for SSE tests
createTestClient: (id = "test-client") => ({
id,
ip: TEST_CONFIG.TEST_CLIENT_IP,
connectedAt: new Date(),
send: mock(() => { }),
rateLimit: {
count: 0,
lastReset: Date.now(),
},
connectionTime: Date.now(),
}),
// Create test event for SSE tests
createTestEvent: (type = "test_event", data: unknown = {}) => ({
event_type: type,
data,
origin: "test",
time_fired: new Date().toISOString(),
context: { id: "test" },
}),
// Create test entity for Home Assistant tests
createTestEntity: (entityId = "test.entity", state = "on") => ({
entity_id: entityId,
state,
attributes: {},
last_changed: new Date().toISOString(),
last_updated: new Date().toISOString(),
}),
// Helper to wait for async operations
wait: (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)),
};
// Export test utilities and Bun test functions
export { beforeAll, afterAll, beforeEach, describe, expect, it, mock, test, testUtils };

View File

@@ -1,8 +1,14 @@
import express from 'express'; import express from "express";
import { z } from 'zod'; import { z } from "zod";
import { NLPProcessor } from '../nlp/processor.js'; import { NLPProcessor } from "../nlp/processor.js";
import { AIRateLimit, AIContext, AIResponse, AIError, AIModel } from '../types/index.js'; import {
import rateLimit from 'express-rate-limit'; AIRateLimit,
AIContext,
AIResponse,
AIError,
AIModel,
} from "../types/index.js";
import rateLimit from "express-rate-limit";
const router = express.Router(); const router = express.Router();
const nlpProcessor = new NLPProcessor(); const nlpProcessor = new NLPProcessor();
@@ -15,17 +21,17 @@ const rateLimitConfig: AIRateLimit = {
model_specific_limits: { model_specific_limits: {
claude: { claude: {
requests_per_minute: 100, requests_per_minute: 100,
requests_per_hour: 1000 requests_per_hour: 1000,
}, },
gpt4: { gpt4: {
requests_per_minute: 50, requests_per_minute: 50,
requests_per_hour: 500 requests_per_hour: 500,
}, },
custom: { custom: {
requests_per_minute: 200, requests_per_minute: 200,
requests_per_hour: 2000 requests_per_hour: 2000,
} },
} },
}; };
// Request validation schemas // Request validation schemas
@@ -37,40 +43,42 @@ const interpretRequestSchema = z.object({
timestamp: z.string(), timestamp: z.string(),
location: z.string(), location: z.string(),
previous_actions: z.array(z.any()), previous_actions: z.array(z.any()),
environment_state: z.record(z.any()) environment_state: z.record(z.any()),
}), }),
model: z.enum(['claude', 'gpt4', 'custom']).optional() model: z.enum(["claude", "gpt4", "custom"]).optional(),
}); });
// Rate limiters // Rate limiters
const globalLimiter = rateLimit({ const globalLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute windowMs: 60 * 1000, // 1 minute
max: rateLimitConfig.requests_per_minute max: rateLimitConfig.requests_per_minute,
}); });
const modelSpecificLimiter = (model: string) => rateLimit({ const modelSpecificLimiter = (model: string) =>
rateLimit({
windowMs: 60 * 1000, windowMs: 60 * 1000,
max: rateLimitConfig.model_specific_limits[model as AIModel]?.requests_per_minute || max:
rateLimitConfig.requests_per_minute rateLimitConfig.model_specific_limits[model as AIModel]
}); ?.requests_per_minute || rateLimitConfig.requests_per_minute,
});
// Error handler middleware // Error handler middleware
const errorHandler = ( const errorHandler = (
error: Error, error: Error,
req: express.Request, req: express.Request,
res: express.Response, res: express.Response,
next: express.NextFunction next: express.NextFunction,
) => { ) => {
const aiError: AIError = { const aiError: AIError = {
code: 'PROCESSING_ERROR', code: "PROCESSING_ERROR",
message: error.message, message: error.message,
suggestion: 'Please try again with a different command format', suggestion: "Please try again with a different command format",
recovery_options: [ recovery_options: [
'Simplify your command', "Simplify your command",
'Use standard command patterns', "Use standard command patterns",
'Check device names and parameters' "Check device names and parameters",
], ],
context: req.body.context context: req.body.context,
}; };
res.status(500).json({ error: aiError }); res.status(500).json({ error: aiError });
@@ -78,15 +86,26 @@ const errorHandler = (
// Endpoints // Endpoints
router.post( router.post(
'/interpret', "/interpret",
globalLimiter, globalLimiter,
async (req: express.Request, res: express.Response, next: express.NextFunction) => { async (
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
try { try {
const { input, context, model = 'claude' } = interpretRequestSchema.parse(req.body); const {
input,
context,
model = "claude",
} = interpretRequestSchema.parse(req.body);
// Apply model-specific rate limiting // Apply model-specific rate limiting
modelSpecificLimiter(model)(req, res, async () => { modelSpecificLimiter(model)(req, res, async () => {
const { intent, confidence, error } = await nlpProcessor.processCommand(input, context); const { intent, confidence, error } = await nlpProcessor.processCommand(
input,
context,
);
if (error) { if (error) {
return res.status(400).json({ error }); return res.status(400).json({ error });
@@ -96,39 +115,39 @@ router.post(
if (!isValid) { if (!isValid) {
const suggestions = await nlpProcessor.suggestCorrections(input, { const suggestions = await nlpProcessor.suggestCorrections(input, {
code: 'INVALID_INTENT', code: "INVALID_INTENT",
message: 'Could not understand the command with high confidence', message: "Could not understand the command with high confidence",
suggestion: 'Please try rephrasing your command', suggestion: "Please try rephrasing your command",
recovery_options: [], recovery_options: [],
context context,
}); });
return res.status(400).json({ return res.status(400).json({
error: { error: {
code: 'INVALID_INTENT', code: "INVALID_INTENT",
message: 'Could not understand the command with high confidence', message: "Could not understand the command with high confidence",
suggestion: 'Please try rephrasing your command', suggestion: "Please try rephrasing your command",
recovery_options: suggestions, recovery_options: suggestions,
context context,
} },
}); });
} }
const response: AIResponse = { const response: AIResponse = {
natural_language: `I'll ${intent.action} the ${intent.target.split('.').pop()}`, natural_language: `I'll ${intent.action} the ${intent.target.split(".").pop()}`,
structured_data: { structured_data: {
success: true, success: true,
action_taken: intent.action, action_taken: intent.action,
entities_affected: [intent.target], entities_affected: [intent.target],
state_changes: intent.parameters state_changes: intent.parameters,
}, },
next_suggestions: [ next_suggestions: [
'Would you like to adjust any settings?', "Would you like to adjust any settings?",
'Should I perform this action in other rooms?', "Should I perform this action in other rooms?",
'Would you like to schedule this action?' "Would you like to schedule this action?",
], ],
confidence, confidence,
context context,
}; };
res.json(response); res.json(response);
@@ -136,15 +155,19 @@ router.post(
} catch (error) { } catch (error) {
next(error); next(error);
} }
} },
); );
router.post( router.post(
'/execute', "/execute",
globalLimiter, globalLimiter,
async (req: express.Request, res: express.Response, next: express.NextFunction) => { async (
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
try { try {
const { intent, context, model = 'claude' } = req.body; const { intent, context, model = "claude" } = req.body;
// Apply model-specific rate limiting // Apply model-specific rate limiting
modelSpecificLimiter(model)(req, res, async () => { modelSpecificLimiter(model)(req, res, async () => {
@@ -157,15 +180,15 @@ router.post(
success: true, success: true,
action_taken: intent.action, action_taken: intent.action,
entities_affected: [intent.target], entities_affected: [intent.target],
state_changes: intent.parameters state_changes: intent.parameters,
}, },
next_suggestions: [ next_suggestions: [
'Would you like to verify the state?', "Would you like to verify the state?",
'Should I perform any related actions?', "Should I perform any related actions?",
'Would you like to undo this action?' "Would you like to undo this action?",
], ],
confidence: { overall: 1, intent: 1, entities: 1, context: 1 }, confidence: { overall: 1, intent: 1, entities: 1, context: 1 },
context context,
}; };
res.json(response); res.json(response);
@@ -173,24 +196,28 @@ router.post(
} catch (error) { } catch (error) {
next(error); next(error);
} }
} },
); );
router.get( router.get(
'/suggestions', "/suggestions",
globalLimiter, globalLimiter,
async (req: express.Request, res: express.Response, next: express.NextFunction) => { async (
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
try { try {
const { context, model = 'claude' } = req.body; const { context, model = "claude" } = req.body;
// Apply model-specific rate limiting // Apply model-specific rate limiting
modelSpecificLimiter(model)(req, res, async () => { modelSpecificLimiter(model)(req, res, async () => {
// Generate context-aware suggestions // Generate context-aware suggestions
const suggestions = [ const suggestions = [
'Turn on the lights in the living room', "Turn on the lights in the living room",
'Set the temperature to 72 degrees', "Set the temperature to 72 degrees",
'Show me the current state of all devices', "Show me the current state of all devices",
'Start the evening routine' "Start the evening routine",
]; ];
res.json({ suggestions }); res.json({ suggestions });
@@ -198,7 +225,7 @@ router.get(
} catch (error) { } catch (error) {
next(error); next(error);
} }
} },
); );
// Apply error handler // Apply error handler

View File

@@ -1,4 +1,4 @@
import { AIContext, AIIntent } from '../types/index.js'; import { AIContext, AIIntent } from "../types/index.js";
interface ContextAnalysis { interface ContextAnalysis {
confidence: number; confidence: number;
@@ -19,9 +19,12 @@ export class ContextAnalyzer {
// Location-based context // Location-based context
{ {
condition: (context, intent) => condition: (context, intent) =>
Boolean(context.location && intent.target.includes(context.location.toLowerCase())), Boolean(
context.location &&
intent.target.includes(context.location.toLowerCase()),
),
relevance: 0.8, relevance: 0.8,
params: (context) => ({ location: context.location }) params: (context) => ({ location: context.location }),
}, },
// Time-based context // Time-based context
@@ -32,40 +35,46 @@ export class ContextAnalyzer {
}, },
relevance: 0.6, relevance: 0.6,
params: (context) => ({ params: (context) => ({
time_of_day: this.getTimeOfDay(new Date(context.timestamp)) time_of_day: this.getTimeOfDay(new Date(context.timestamp)),
}) }),
}, },
// Previous action context // Previous action context
{ {
condition: (context, intent) => { condition: (context, intent) => {
const recentActions = context.previous_actions.slice(-3); const recentActions = context.previous_actions.slice(-3);
return recentActions.some(action => return recentActions.some(
(action) =>
action.target === intent.target || action.target === intent.target ||
action.action === intent.action action.action === intent.action,
); );
}, },
relevance: 0.7, relevance: 0.7,
params: (context) => ({ params: (context) => ({
recent_action: context.previous_actions[context.previous_actions.length - 1] recent_action:
}) context.previous_actions[context.previous_actions.length - 1],
}),
}, },
// Environment state context // Environment state context
{ {
condition: (context, intent) => { condition: (context, intent) => {
return Object.keys(context.environment_state).some(key => return Object.keys(context.environment_state).some(
(key) =>
intent.target.includes(key) || intent.target.includes(key) ||
intent.parameters[key] !== undefined intent.parameters[key] !== undefined,
); );
}, },
relevance: 0.9, relevance: 0.9,
params: (context) => ({ environment: context.environment_state }) params: (context) => ({ environment: context.environment_state }),
} },
]; ];
} }
async analyze(intent: AIIntent, context: AIContext): Promise<ContextAnalysis> { async analyze(
intent: AIIntent,
context: AIContext,
): Promise<ContextAnalysis> {
let totalConfidence = 0; let totalConfidence = 0;
let relevantParams: Record<string, any> = {}; let relevantParams: Record<string, any> = {};
let applicableRules = 0; let applicableRules = 0;
@@ -78,30 +87,29 @@ export class ContextAnalyzer {
if (rule.params) { if (rule.params) {
relevantParams = { relevantParams = {
...relevantParams, ...relevantParams,
...rule.params(context) ...rule.params(context),
}; };
} }
} }
} }
// Calculate normalized confidence // Calculate normalized confidence
const confidence = applicableRules > 0 const confidence =
? totalConfidence / applicableRules applicableRules > 0 ? totalConfidence / applicableRules : 0.5; // Default confidence if no rules apply
: 0.5; // Default confidence if no rules apply
return { return {
confidence, confidence,
relevant_params: relevantParams relevant_params: relevantParams,
}; };
} }
private getTimeOfDay(date: Date): string { private getTimeOfDay(date: Date): string {
const hour = date.getHours(); const hour = date.getHours();
if (hour >= 5 && hour < 12) return 'morning'; if (hour >= 5 && hour < 12) return "morning";
if (hour >= 12 && hour < 17) return 'afternoon'; if (hour >= 12 && hour < 17) return "afternoon";
if (hour >= 17 && hour < 22) return 'evening'; if (hour >= 17 && hour < 22) return "evening";
return 'night'; return "night";
} }
async updateContextRules(newRules: ContextRule[]): Promise<void> { async updateContextRules(newRules: ContextRule[]): Promise<void> {
@@ -126,7 +134,10 @@ export class ContextAnalyzer {
} }
// Validate environment state // Validate environment state
if (typeof context.environment_state !== 'object' || context.environment_state === null) { if (
typeof context.environment_state !== "object" ||
context.environment_state === null
) {
return false; return false;
} }

View File

@@ -1,4 +1,4 @@
import { AIContext } from '../types/index.js'; import { AIContext } from "../types/index.js";
interface ExtractedEntities { interface ExtractedEntities {
primary_target: string; primary_target: string;
@@ -18,21 +18,24 @@ export class EntityExtractor {
private initializePatterns(): void { private initializePatterns(): void {
// Device name variations // Device name variations
this.deviceNameMap.set('living room light', 'light.living_room'); this.deviceNameMap.set("living room light", "light.living_room");
this.deviceNameMap.set('kitchen light', 'light.kitchen'); this.deviceNameMap.set("kitchen light", "light.kitchen");
this.deviceNameMap.set('bedroom light', 'light.bedroom'); this.deviceNameMap.set("bedroom light", "light.bedroom");
// Parameter patterns // Parameter patterns
this.parameterPatterns.set('brightness', /(\d+)\s*(%|percent)|bright(ness)?\s+(\d+)/i); this.parameterPatterns.set(
this.parameterPatterns.set('temperature', /(\d+)\s*(degrees?|°)[CF]?/i); "brightness",
this.parameterPatterns.set('color', /(red|green|blue|white|warm|cool)/i); /(\d+)\s*(%|percent)|bright(ness)?\s+(\d+)/i,
);
this.parameterPatterns.set("temperature", /(\d+)\s*(degrees?|°)[CF]?/i);
this.parameterPatterns.set("color", /(red|green|blue|white|warm|cool)/i);
} }
async extract(input: string): Promise<ExtractedEntities> { async extract(input: string): Promise<ExtractedEntities> {
const entities: ExtractedEntities = { const entities: ExtractedEntities = {
primary_target: '', primary_target: "",
parameters: {}, parameters: {},
confidence: 0 confidence: 0,
}; };
try { try {
@@ -48,7 +51,10 @@ export class EntityExtractor {
for (const [param, pattern] of this.parameterPatterns) { for (const [param, pattern] of this.parameterPatterns) {
const match = input.match(pattern); const match = input.match(pattern);
if (match) { if (match) {
entities.parameters[param] = this.normalizeParameterValue(param, match[1]); entities.parameters[param] = this.normalizeParameterValue(
param,
match[1],
);
} }
} }
@@ -57,29 +63,35 @@ export class EntityExtractor {
return entities; return entities;
} catch (error) { } catch (error) {
console.error('Entity extraction error:', error); console.error("Entity extraction error:", error);
return { return {
primary_target: '', primary_target: "",
parameters: {}, parameters: {},
confidence: 0 confidence: 0,
}; };
} }
} }
private normalizeParameterValue(parameter: string, value: string): number | string { private normalizeParameterValue(
parameter: string,
value: string,
): number | string {
switch (parameter) { switch (parameter) {
case 'brightness': case "brightness":
return Math.min(100, Math.max(0, parseInt(value))); return Math.min(100, Math.max(0, parseInt(value)));
case 'temperature': case "temperature":
return parseInt(value); return parseInt(value);
case 'color': case "color":
return value.toLowerCase(); return value.toLowerCase();
default: default:
return value; return value;
} }
} }
private calculateConfidence(entities: ExtractedEntities, input: string): number { private calculateConfidence(
entities: ExtractedEntities,
input: string,
): number {
let confidence = 0; let confidence = 0;
// Device confidence // Device confidence

View File

@@ -18,54 +18,47 @@ export class IntentClassifier {
constructor() { constructor() {
this.actionPatterns = [ this.actionPatterns = [
{ {
action: 'turn_on', action: "turn_on",
patterns: [ patterns: [/turn\s+on/i, /switch\s+on/i, /enable/i, /activate/i],
/turn\s+on/i,
/switch\s+on/i,
/enable/i,
/activate/i
]
}, },
{ {
action: 'turn_off', action: "turn_off",
patterns: [ patterns: [/turn\s+off/i, /switch\s+off/i, /disable/i, /deactivate/i],
/turn\s+off/i,
/switch\s+off/i,
/disable/i,
/deactivate/i
]
}, },
{ {
action: 'set', action: "set",
patterns: [ patterns: [
/set\s+(?:the\s+)?(.+)\s+to/i, /set\s+(?:the\s+)?(.+)\s+to/i,
/change\s+(?:the\s+)?(.+)\s+to/i, /change\s+(?:the\s+)?(.+)\s+to/i,
/adjust\s+(?:the\s+)?(.+)\s+to/i /adjust\s+(?:the\s+)?(.+)\s+to/i,
], ],
parameters: ['brightness', 'temperature', 'color'] parameters: ["brightness", "temperature", "color"],
}, },
{ {
action: 'query', action: "query",
patterns: [ patterns: [
/what\s+is/i, /what\s+is/i,
/get\s+(?:the\s+)?(.+)/i, /get\s+(?:the\s+)?(.+)/i,
/show\s+(?:the\s+)?(.+)/i, /show\s+(?:the\s+)?(.+)/i,
/tell\s+me/i /tell\s+me/i,
] ],
} },
]; ];
} }
async classify( async classify(
input: string, input: string,
extractedEntities: { parameters: Record<string, any>; primary_target: string } extractedEntities: {
parameters: Record<string, any>;
primary_target: string;
},
): Promise<ClassifiedIntent> { ): Promise<ClassifiedIntent> {
let bestMatch: ClassifiedIntent = { let bestMatch: ClassifiedIntent = {
action: '', action: "",
target: '', target: "",
confidence: 0, confidence: 0,
parameters: {}, parameters: {},
raw_input: input raw_input: input,
}; };
for (const actionPattern of this.actionPatterns) { for (const actionPattern of this.actionPatterns) {
@@ -78,8 +71,12 @@ export class IntentClassifier {
action: actionPattern.action, action: actionPattern.action,
target: extractedEntities.primary_target, target: extractedEntities.primary_target,
confidence, confidence,
parameters: this.extractActionParameters(actionPattern, match, extractedEntities), parameters: this.extractActionParameters(
raw_input: input actionPattern,
match,
extractedEntities,
),
raw_input: input,
}; };
} }
} }
@@ -105,7 +102,7 @@ export class IntentClassifier {
} }
// Additional confidence for specific keywords // Additional confidence for specific keywords
const keywords = ['please', 'can you', 'would you']; const keywords = ["please", "can you", "would you"];
for (const keyword of keywords) { for (const keyword of keywords) {
if (input.toLowerCase().includes(keyword)) { if (input.toLowerCase().includes(keyword)) {
confidence += 0.1; confidence += 0.1;
@@ -118,7 +115,10 @@ export class IntentClassifier {
private extractActionParameters( private extractActionParameters(
actionPattern: ActionPattern, actionPattern: ActionPattern,
match: RegExpMatchArray, match: RegExpMatchArray,
extractedEntities: { parameters: Record<string, any>; primary_target: string } extractedEntities: {
parameters: Record<string, any>;
primary_target: string;
},
): Record<string, any> { ): Record<string, any> {
const parameters: Record<string, any> = {}; const parameters: Record<string, any> = {};
@@ -141,37 +141,40 @@ export class IntentClassifier {
private inferFromContext( private inferFromContext(
input: string, input: string,
extractedEntities: { parameters: Record<string, any>; primary_target: string } extractedEntities: {
parameters: Record<string, any>;
primary_target: string;
},
): ClassifiedIntent { ): ClassifiedIntent {
// Default to 'set' action if parameters are present // Default to 'set' action if parameters are present
if (Object.keys(extractedEntities.parameters).length > 0) { if (Object.keys(extractedEntities.parameters).length > 0) {
return { return {
action: 'set', action: "set",
target: extractedEntities.primary_target, target: extractedEntities.primary_target,
confidence: 0.5, confidence: 0.5,
parameters: extractedEntities.parameters, parameters: extractedEntities.parameters,
raw_input: input raw_input: input,
}; };
} }
// Default to 'query' for question-like inputs // Default to 'query' for question-like inputs
if (input.match(/^(what|when|where|who|how|why)/i)) { if (input.match(/^(what|when|where|who|how|why)/i)) {
return { return {
action: 'query', action: "query",
target: extractedEntities.primary_target || 'system', target: extractedEntities.primary_target || "system",
confidence: 0.6, confidence: 0.6,
parameters: {}, parameters: {},
raw_input: input raw_input: input,
}; };
} }
// Fallback with low confidence // Fallback with low confidence
return { return {
action: 'unknown', action: "unknown",
target: extractedEntities.primary_target || 'system', target: extractedEntities.primary_target || "system",
confidence: 0.3, confidence: 0.3,
parameters: {}, parameters: {},
raw_input: input raw_input: input,
}; };
} }
} }

View File

@@ -1,7 +1,7 @@
import { AIIntent, AIContext, AIConfidence, AIError } from '../types/index.js'; import { AIIntent, AIContext, AIConfidence, AIError } from "../types/index.js";
import { EntityExtractor } from './entity-extractor.js'; import { EntityExtractor } from "./entity-extractor.js";
import { IntentClassifier } from './intent-classifier.js'; import { IntentClassifier } from "./intent-classifier.js";
import { ContextAnalyzer } from './context-analyzer.js'; import { ContextAnalyzer } from "./context-analyzer.js";
export class NLPProcessor { export class NLPProcessor {
private entityExtractor: EntityExtractor; private entityExtractor: EntityExtractor;
@@ -16,7 +16,7 @@ export class NLPProcessor {
async processCommand( async processCommand(
input: string, input: string,
context: AIContext context: AIContext,
): Promise<{ ): Promise<{
intent: AIIntent; intent: AIIntent;
confidence: AIConfidence; confidence: AIConfidence;
@@ -30,14 +30,21 @@ export class NLPProcessor {
const intent = await this.intentClassifier.classify(input, entities); const intent = await this.intentClassifier.classify(input, entities);
// Analyze context relevance // Analyze context relevance
const contextRelevance = await this.contextAnalyzer.analyze(intent, context); const contextRelevance = await this.contextAnalyzer.analyze(
intent,
context,
);
// Calculate confidence scores // Calculate confidence scores
const confidence: AIConfidence = { const confidence: AIConfidence = {
overall: (intent.confidence + entities.confidence + contextRelevance.confidence) / 3, overall:
(intent.confidence +
entities.confidence +
contextRelevance.confidence) /
3,
intent: intent.confidence, intent: intent.confidence,
entities: entities.confidence, entities: entities.confidence,
context: contextRelevance.confidence context: contextRelevance.confidence,
}; };
// Create structured intent // Create structured intent
@@ -47,41 +54,42 @@ export class NLPProcessor {
parameters: { parameters: {
...entities.parameters, ...entities.parameters,
...intent.parameters, ...intent.parameters,
context_parameters: contextRelevance.relevant_params context_parameters: contextRelevance.relevant_params,
}, },
raw_input: input raw_input: input,
}; };
return { return {
intent: structuredIntent, intent: structuredIntent,
confidence confidence,
}; };
} catch (error: unknown) { } catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; const errorMessage =
error instanceof Error ? error.message : "Unknown error occurred";
return { return {
intent: { intent: {
action: 'error', action: "error",
target: 'system', target: "system",
parameters: {}, parameters: {},
raw_input: input raw_input: input,
}, },
confidence: { confidence: {
overall: 0, overall: 0,
intent: 0, intent: 0,
entities: 0, entities: 0,
context: 0 context: 0,
}, },
error: { error: {
code: 'NLP_PROCESSING_ERROR', code: "NLP_PROCESSING_ERROR",
message: errorMessage, message: errorMessage,
suggestion: 'Please try rephrasing your command', suggestion: "Please try rephrasing your command",
recovery_options: [ recovery_options: [
'Use simpler language', "Use simpler language",
'Break down the command into smaller parts', "Break down the command into smaller parts",
'Specify the target device explicitly' "Specify the target device explicitly",
], ],
context context,
} },
}; };
} }
} }
@@ -89,7 +97,7 @@ export class NLPProcessor {
async validateIntent( async validateIntent(
intent: AIIntent, intent: AIIntent,
confidence: AIConfidence, confidence: AIConfidence,
threshold = 0.7 threshold = 0.7,
): Promise<boolean> { ): Promise<boolean> {
return ( return (
confidence.overall >= threshold && confidence.overall >= threshold &&
@@ -99,31 +107,28 @@ export class NLPProcessor {
); );
} }
async suggestCorrections( async suggestCorrections(input: string, error: AIError): Promise<string[]> {
input: string,
error: AIError
): Promise<string[]> {
// Implement correction suggestions based on the error // Implement correction suggestions based on the error
const suggestions: string[] = []; const suggestions: string[] = [];
if (error.code === 'ENTITY_NOT_FOUND') { if (error.code === "ENTITY_NOT_FOUND") {
suggestions.push( suggestions.push(
'Try specifying the device name more clearly', "Try specifying the device name more clearly",
'Use the exact device name from your Home Assistant setup' "Use the exact device name from your Home Assistant setup",
); );
} }
if (error.code === 'AMBIGUOUS_INTENT') { if (error.code === "AMBIGUOUS_INTENT") {
suggestions.push( suggestions.push(
'Please specify what you want to do with the device', "Please specify what you want to do with the device",
'Use action words like "turn on", "set", "adjust"' 'Use action words like "turn on", "set", "adjust"',
); );
} }
if (error.code === 'CONTEXT_MISMATCH') { if (error.code === "CONTEXT_MISMATCH") {
suggestions.push( suggestions.push(
'Specify the location if referring to a device', "Specify the location if referring to a device",
'Clarify which device you mean in the current context' "Clarify which device you mean in the current context",
); );
} }

View File

@@ -1,4 +1,4 @@
import { AIModel } from '../types/index.js'; import { AIModel } from "../types/index.js";
interface PromptTemplate { interface PromptTemplate {
system: string; system: string;
@@ -33,13 +33,15 @@ Current context: {context}`,
examples: [ examples: [
{ {
user: "Turn on the living room lights", user: "Turn on the living room lights",
assistant: "I'll turn on the lights in the living room. Would you like me to set a specific brightness level?" assistant:
"I'll turn on the lights in the living room. Would you like me to set a specific brightness level?",
}, },
{ {
user: "Set the temperature to 72 degrees", user: "Set the temperature to 72 degrees",
assistant: "I'll set the temperature to 72°F. I'll monitor the temperature and let you know when it reaches the target." assistant:
} "I'll set the temperature to 72°F. I'll monitor the temperature and let you know when it reaches the target.",
] },
],
}, },
[AIModel.GPT4]: { [AIModel.GPT4]: {
system: `You are a home automation assistant powered by GPT-4. system: `You are a home automation assistant powered by GPT-4.
@@ -52,13 +54,15 @@ Context: {context}`,
examples: [ examples: [
{ {
user: "Dim the bedroom lights to 50%", user: "Dim the bedroom lights to 50%",
assistant: "Setting bedroom light brightness to 50%. The change has been applied successfully." assistant:
"Setting bedroom light brightness to 50%. The change has been applied successfully.",
}, },
{ {
user: "Start the evening routine", user: "Start the evening routine",
assistant: "Initiating evening routine: dimming lights, adjusting temperature, and enabling security system." assistant:
} "Initiating evening routine: dimming lights, adjusting temperature, and enabling security system.",
] },
],
}, },
[AIModel.CUSTOM]: { [AIModel.CUSTOM]: {
system: `Custom home automation assistant configuration. system: `Custom home automation assistant configuration.
@@ -73,14 +77,16 @@ Current context: {context}`,
examples: [ examples: [
{ {
user: "Make it cooler in here", user: "Make it cooler in here",
assistant: "Based on your preferences, I'll lower the temperature by 2 degrees. Current temperature is 74°F, adjusting to 72°F." assistant:
"Based on your preferences, I'll lower the temperature by 2 degrees. Current temperature is 74°F, adjusting to 72°F.",
}, },
{ {
user: "Set up movie mode", user: "Set up movie mode",
assistant: "Activating movie mode: dimming lights to 20%, closing blinds, setting TV input to HDMI 1, and adjusting sound system." assistant:
} "Activating movie mode: dimming lights to 20%, closing blinds, setting TV input to HDMI 1, and adjusting sound system.",
] },
} ],
},
}; };
} }
@@ -95,7 +101,7 @@ Current context: {context}`,
// Replace variables in the prompt // Replace variables in the prompt
for (const [key, value] of Object.entries(variables)) { for (const [key, value] of Object.entries(variables)) {
const placeholder = `{${key}}`; const placeholder = `{${key}}`;
if (typeof value === 'object') { if (typeof value === "object") {
prompt = prompt.replace(placeholder, JSON.stringify(value)); prompt = prompt.replace(placeholder, JSON.stringify(value));
} else { } else {
prompt = prompt.replace(placeholder, String(value)); prompt = prompt.replace(placeholder, String(value));
@@ -115,7 +121,7 @@ Current context: {context}`,
addExample( addExample(
model: AIModel, model: AIModel,
example: { user: string; assistant: string } example: { user: string; assistant: string },
): void { ): void {
this.templates[model].examples.push(example); this.templates[model].examples.push(example);
} }
@@ -124,10 +130,7 @@ Current context: {context}`,
this.templates[model].system = newPrompt; this.templates[model].system = newPrompt;
} }
createCustomTemplate( createCustomTemplate(model: AIModel.CUSTOM, template: PromptTemplate): void {
model: AIModel.CUSTOM,
template: PromptTemplate
): void {
this.templates[model] = template; this.templates[model] = template;
} }
} }

View File

@@ -1,10 +1,10 @@
import { z } from 'zod'; import { z } from "zod";
// AI Model Types // AI Model Types
export enum AIModel { export enum AIModel {
CLAUDE = 'claude', CLAUDE = "claude",
GPT4 = 'gpt4', GPT4 = "gpt4",
CUSTOM = 'custom' CUSTOM = "custom",
} }
// AI Confidence Level // AI Confidence Level
@@ -61,10 +61,13 @@ export interface AIRateLimit {
requests_per_minute: number; requests_per_minute: number;
requests_per_hour: number; requests_per_hour: number;
concurrent_requests: number; concurrent_requests: number;
model_specific_limits: Record<AIModel, { model_specific_limits: Record<
AIModel,
{
requests_per_minute: number; requests_per_minute: number;
requests_per_hour: number; requests_per_hour: number;
}>; }
>;
} }
// Zod Schemas // Zod Schemas
@@ -72,14 +75,14 @@ export const AIConfidenceSchema = z.object({
overall: z.number().min(0).max(1), overall: z.number().min(0).max(1),
intent: z.number().min(0).max(1), intent: z.number().min(0).max(1),
entities: z.number().min(0).max(1), entities: z.number().min(0).max(1),
context: z.number().min(0).max(1) context: z.number().min(0).max(1),
}); });
export const AIIntentSchema = z.object({ export const AIIntentSchema = z.object({
action: z.string(), action: z.string(),
target: z.string(), target: z.string(),
parameters: z.record(z.any()), parameters: z.record(z.any()),
raw_input: z.string() raw_input: z.string(),
}); });
export const AIContextSchema = z.object({ export const AIContextSchema = z.object({
@@ -88,7 +91,7 @@ export const AIContextSchema = z.object({
timestamp: z.string(), timestamp: z.string(),
location: z.string(), location: z.string(),
previous_actions: z.array(AIIntentSchema), previous_actions: z.array(AIIntentSchema),
environment_state: z.record(z.any()) environment_state: z.record(z.any()),
}); });
export const AIResponseSchema = z.object({ export const AIResponseSchema = z.object({
@@ -97,11 +100,11 @@ export const AIResponseSchema = z.object({
success: z.boolean(), success: z.boolean(),
action_taken: z.string(), action_taken: z.string(),
entities_affected: z.array(z.string()), entities_affected: z.array(z.string()),
state_changes: z.record(z.any()) state_changes: z.record(z.any()),
}), }),
next_suggestions: z.array(z.string()), next_suggestions: z.array(z.string()),
confidence: AIConfidenceSchema, confidence: AIConfidenceSchema,
context: AIContextSchema context: AIContextSchema,
}); });
export const AIErrorSchema = z.object({ export const AIErrorSchema = z.object({
@@ -109,15 +112,17 @@ export const AIErrorSchema = z.object({
message: z.string(), message: z.string(),
suggestion: z.string(), suggestion: z.string(),
recovery_options: z.array(z.string()), recovery_options: z.array(z.string()),
context: AIContextSchema context: AIContextSchema,
}); });
export const AIRateLimitSchema = z.object({ export const AIRateLimitSchema = z.object({
requests_per_minute: z.number(), requests_per_minute: z.number(),
requests_per_hour: z.number(), requests_per_hour: z.number(),
concurrent_requests: z.number(), concurrent_requests: z.number(),
model_specific_limits: z.record(z.object({ model_specific_limits: z.record(
z.object({
requests_per_minute: z.number(), requests_per_minute: z.number(),
requests_per_hour: z.number() requests_per_hour: z.number(),
})) }),
),
}); });

View File

@@ -1,21 +1,21 @@
import { Router } from 'express'; import { Router } from "express";
import { MCP_SCHEMA } from '../mcp/schema.js'; import { MCP_SCHEMA } from "../mcp/schema.js";
import { middleware } from '../middleware/index.js'; import { middleware } from "../middleware/index.js";
import { sseManager } from '../sse/index.js'; import { sseManager } from "../sse/index.js";
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from "uuid";
import { TokenManager } from '../security/index.js'; import { TokenManager } from "../security/index.js";
import { tools } from '../tools/index.js'; import { tools } from "../tools/index.js";
import { Tool } from '../interfaces/index.js'; import { Tool } from "../interfaces/index.js";
const router = Router(); const router = Router();
// MCP schema endpoint - no auth required as it's just the schema // MCP schema endpoint - no auth required as it's just the schema
router.get('/mcp', (_req, res) => { router.get("/mcp", (_req, res) => {
res.json(MCP_SCHEMA); res.json(MCP_SCHEMA);
}); });
// MCP execute endpoint - requires authentication // MCP execute endpoint - requires authentication
router.post('/mcp/execute', middleware.authenticate, async (req, res) => { router.post("/mcp/execute", middleware.authenticate, async (req, res) => {
try { try {
const { tool: toolName, parameters } = req.body; const { tool: toolName, parameters } = req.body;
@@ -24,7 +24,7 @@ router.post('/mcp/execute', middleware.authenticate, async (req, res) => {
if (!tool) { if (!tool) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: `Tool '${toolName}' not found` message: `Tool '${toolName}' not found`,
}); });
} }
@@ -34,67 +34,72 @@ router.post('/mcp/execute', middleware.authenticate, async (req, res) => {
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}); });
} }
}); });
// Health check endpoint // Health check endpoint
router.get('/health', (_req, res) => { router.get("/health", (_req, res) => {
res.json({ res.json({
status: 'ok', status: "ok",
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
version: '0.1.0' version: "0.1.0",
}); });
}); });
// List devices endpoint // List devices endpoint
router.get('/list_devices', middleware.authenticate, async (req, res) => { router.get("/list_devices", middleware.authenticate, async (req, res) => {
try { try {
const tool = tools.find((t: Tool) => t.name === 'list_devices'); const tool = tools.find((t: Tool) => t.name === "list_devices");
if (!tool) { if (!tool) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'Tool not found' message: "Tool not found",
}); });
} }
const result = await tool.execute({ token: req.headers.authorization?.replace('Bearer ', '') }); const result = await tool.execute({
token: req.headers.authorization?.replace("Bearer ", ""),
});
res.json(result); res.json(result);
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}); });
} }
}); });
// Device control endpoint // Device control endpoint
router.post('/control', middleware.authenticate, async (req, res) => { router.post("/control", middleware.authenticate, async (req, res) => {
try { try {
const tool = tools.find((t: Tool) => t.name === 'control'); const tool = tools.find((t: Tool) => t.name === "control");
if (!tool) { if (!tool) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'Tool not found' message: "Tool not found",
}); });
} }
const result = await tool.execute({ const result = await tool.execute({
...req.body, ...req.body,
token: req.headers.authorization?.replace('Bearer ', '') token: req.headers.authorization?.replace("Bearer ", ""),
}); });
res.json(result); res.json(result);
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}); });
} }
}); });
// SSE endpoints // SSE endpoints
router.get('/subscribe_events', middleware.wsRateLimiter, (req, res) => { router.get("/subscribe_events", middleware.wsRateLimiter, (req, res) => {
try { try {
// Get token from query parameter // Get token from query parameter
const token = req.query.token?.toString(); const token = req.query.token?.toString();
@@ -102,48 +107,54 @@ router.get('/subscribe_events', middleware.wsRateLimiter, (req, res) => {
if (!token || !TokenManager.validateToken(token)) { if (!token || !TokenManager.validateToken(token)) {
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: 'Unauthorized - Invalid token' message: "Unauthorized - Invalid token",
}); });
} }
// Set SSE headers // Set SSE headers
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'text/event-stream', "Content-Type": "text/event-stream",
'Cache-Control': 'no-cache', "Cache-Control": "no-cache",
'Connection': 'keep-alive', Connection: "keep-alive",
'Access-Control-Allow-Origin': '*' "Access-Control-Allow-Origin": "*",
}); });
// Send initial connection message // Send initial connection message
res.write(`data: ${JSON.stringify({ res.write(
type: 'connection', `data: ${JSON.stringify({
status: 'connected', type: "connection",
timestamp: new Date().toISOString() status: "connected",
})}\n\n`); timestamp: new Date().toISOString(),
})}\n\n`,
);
const clientId = uuidv4(); const clientId = uuidv4();
const client = { const client = {
id: clientId, id: clientId,
send: (data: string) => { send: (data: string) => {
res.write(`data: ${data}\n\n`); res.write(`data: ${data}\n\n`);
} },
}; };
// Add client to SSE manager // Add client to SSE manager
const sseClient = sseManager.addClient(client, token); const sseClient = sseManager.addClient(client, token);
if (!sseClient || !sseClient.authenticated) { if (!sseClient || !sseClient.authenticated) {
res.write(`data: ${JSON.stringify({ res.write(
type: 'error', `data: ${JSON.stringify({
message: sseClient ? 'Authentication failed' : 'Maximum client limit reached', type: "error",
timestamp: new Date().toISOString() message: sseClient
})}\n\n`); ? "Authentication failed"
: "Maximum client limit reached",
timestamp: new Date().toISOString(),
})}\n\n`,
);
return res.end(); return res.end();
} }
// Subscribe to events if specified // Subscribe to events if specified
const events = req.query.events?.toString().split(',').filter(Boolean); const events = req.query.events?.toString().split(",").filter(Boolean);
if (events?.length) { if (events?.length) {
events.forEach(event => sseManager.subscribeToEvent(clientId, event)); events.forEach((event) => sseManager.subscribeToEvent(clientId, event));
} }
// Subscribe to entity if specified // Subscribe to entity if specified
@@ -159,14 +170,14 @@ router.get('/subscribe_events', middleware.wsRateLimiter, (req, res) => {
} }
// Handle client disconnect // Handle client disconnect
req.on('close', () => { req.on("close", () => {
sseManager.removeClient(clientId); sseManager.removeClient(clientId);
}); });
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}); });
} }
}); });
@@ -185,19 +196,20 @@ router.get('/subscribe_events', middleware.wsRateLimiter, (req, res) => {
* - total_entities_tracked: Number of entities being tracked * - total_entities_tracked: Number of entities being tracked
* - subscriptions: Lists of entity, event, and domain subscriptions * - subscriptions: Lists of entity, event, and domain subscriptions
*/ */
router.get('/get_sse_stats', middleware.authenticate, (_req, res) => { router.get("/get_sse_stats", middleware.authenticate, (_req, res) => {
try { try {
const stats = sseManager.getStatistics(); const stats = sseManager.getStatistics();
res.json({ res.json({
success: true, success: true,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
data: stats data: stats,
}); });
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred', message:
timestamp: new Date().toISOString() error instanceof Error ? error.message : "Unknown error occurred",
timestamp: new Date().toISOString(),
}); });
} }
}); });

27
src/commands.ts Normal file
View File

@@ -0,0 +1,27 @@
// Common commands that work with most entities
export const commonCommands = ["turn_on", "turn_off", "toggle"] as const;
// Commands specific to cover entities
export const coverCommands = [
...commonCommands,
"open",
"close",
"stop",
"set_position",
"set_tilt_position",
] as const;
// Commands specific to climate entities
export const climateCommands = [
...commonCommands,
"set_temperature",
"set_hvac_mode",
"set_fan_mode",
"set_humidity",
] as const;
// Types for command validation
export type CommonCommand = (typeof commonCommands)[number];
export type CoverCommand = (typeof coverCommands)[number];
export type ClimateCommand = (typeof climateCommands)[number];
export type Command = CommonCommand | CoverCommand | ClimateCommand;

View File

@@ -0,0 +1,162 @@
import { z } from "zod";
// Test configuration schema
const testConfigSchema = z.object({
// Test Environment
TEST_PORT: z.number().default(3001),
TEST_HOST: z.string().default("http://localhost"),
TEST_WEBSOCKET_PORT: z.number().default(3002),
// Mock Authentication
TEST_JWT_SECRET: z
.string()
.default("test_jwt_secret_key_that_is_at_least_32_chars"),
TEST_TOKEN: z.string().default("test_token_that_is_at_least_32_chars_long"),
TEST_INVALID_TOKEN: z.string().default("invalid_token"),
// Mock Client Settings
TEST_CLIENT_IP: z.string().default("127.0.0.1"),
TEST_MAX_CLIENTS: z.number().default(10),
TEST_PING_INTERVAL: z.number().default(100),
TEST_CLEANUP_INTERVAL: z.number().default(200),
TEST_MAX_CONNECTION_AGE: z.number().default(1000),
// Mock Rate Limiting
TEST_RATE_LIMIT_WINDOW: z.number().default(60000), // 1 minute
TEST_RATE_LIMIT_MAX_REQUESTS: z.number().default(100),
TEST_RATE_LIMIT_WEBSOCKET: z.number().default(1000),
// Mock Events
TEST_EVENT_TYPES: z
.array(z.string())
.default([
"state_changed",
"automation_triggered",
"script_executed",
"service_called",
]),
// Mock Entities
TEST_ENTITIES: z
.array(
z.object({
entity_id: z.string(),
state: z.string(),
attributes: z.record(z.any()),
last_changed: z.string(),
last_updated: z.string(),
}),
)
.default([
{
entity_id: "light.test_light",
state: "on",
attributes: {
brightness: 255,
color_temp: 400,
},
last_changed: new Date().toISOString(),
last_updated: new Date().toISOString(),
},
{
entity_id: "switch.test_switch",
state: "off",
attributes: {},
last_changed: new Date().toISOString(),
last_updated: new Date().toISOString(),
},
]),
// Mock Services
TEST_SERVICES: z
.array(
z.object({
domain: z.string(),
service: z.string(),
data: z.record(z.any()),
}),
)
.default([
{
domain: "light",
service: "turn_on",
data: {
entity_id: "light.test_light",
brightness: 255,
},
},
{
domain: "switch",
service: "turn_off",
data: {
entity_id: "switch.test_switch",
},
},
]),
// Mock Error Scenarios
TEST_ERROR_SCENARIOS: z
.array(
z.object({
type: z.string(),
message: z.string(),
code: z.number(),
}),
)
.default([
{
type: "authentication_error",
message: "Invalid token",
code: 401,
},
{
type: "rate_limit_error",
message: "Too many requests",
code: 429,
},
{
type: "validation_error",
message: "Invalid request body",
code: 400,
},
]),
});
// Parse environment variables or use defaults
const parseTestConfig = () => {
const config = {
TEST_PORT: parseInt(process.env.TEST_PORT || "3001"),
TEST_HOST: process.env.TEST_HOST || "http://localhost",
TEST_WEBSOCKET_PORT: parseInt(process.env.TEST_WEBSOCKET_PORT || "3002"),
TEST_JWT_SECRET:
process.env.TEST_JWT_SECRET ||
"test_jwt_secret_key_that_is_at_least_32_chars",
TEST_TOKEN:
process.env.TEST_TOKEN || "test_token_that_is_at_least_32_chars_long",
TEST_INVALID_TOKEN: process.env.TEST_INVALID_TOKEN || "invalid_token",
TEST_CLIENT_IP: process.env.TEST_CLIENT_IP || "127.0.0.1",
TEST_MAX_CLIENTS: parseInt(process.env.TEST_MAX_CLIENTS || "10"),
TEST_PING_INTERVAL: parseInt(process.env.TEST_PING_INTERVAL || "100"),
TEST_CLEANUP_INTERVAL: parseInt(process.env.TEST_CLEANUP_INTERVAL || "200"),
TEST_MAX_CONNECTION_AGE: parseInt(
process.env.TEST_MAX_CONNECTION_AGE || "1000",
),
TEST_RATE_LIMIT_WINDOW: parseInt(
process.env.TEST_RATE_LIMIT_WINDOW || "60000",
),
TEST_RATE_LIMIT_MAX_REQUESTS: parseInt(
process.env.TEST_RATE_LIMIT_MAX_REQUESTS || "100",
),
TEST_RATE_LIMIT_WEBSOCKET: parseInt(
process.env.TEST_RATE_LIMIT_WEBSOCKET || "1000",
),
};
return testConfigSchema.parse(config);
};
// Export the validated test configuration
export const TEST_CONFIG = parseTestConfig();
// Export types
export type TestConfig = z.infer<typeof testConfigSchema>;

View File

@@ -1,5 +1,6 @@
import { config } from 'dotenv'; import { config } from "dotenv";
import { resolve } from 'path'; import { resolve } from "path";
import { z } from "zod";
/** /**
* Load environment variables based on NODE_ENV * Load environment variables based on NODE_ENV
@@ -7,11 +8,12 @@ import { resolve } from 'path';
* Test: .env.test * Test: .env.test
* Production: .env * Production: .env
*/ */
const envFile = process.env.NODE_ENV === 'production' const envFile =
? '.env' process.env.NODE_ENV === "production"
: process.env.NODE_ENV === 'test' ? ".env"
? '.env.test' : process.env.NODE_ENV === "test"
: '.env.development'; ? ".env.test"
: ".env.development";
console.log(`Loading environment from ${envFile}`); console.log(`Loading environment from ${envFile}`);
config({ path: resolve(process.cwd(), envFile) }); config({ path: resolve(process.cwd(), envFile) });
@@ -20,59 +22,61 @@ config({ path: resolve(process.cwd(), envFile) });
* Application configuration object * Application configuration object
* Contains all configuration settings for the application * Contains all configuration settings for the application
*/ */
export const APP_CONFIG = { export const AppConfigSchema = z.object({
/** Server Configuration */ /** Server Configuration */
PORT: process.env.PORT || 3000, PORT: z.number().default(4000),
NODE_ENV: process.env.NODE_ENV || 'development', NODE_ENV: z
.enum(["development", "production", "test"])
.default("development"),
/** Home Assistant Configuration */ /** Home Assistant Configuration */
HASS_HOST: process.env.HASS_HOST || 'http://192.168.178.63:8123', HASS_HOST: z.string().default("http://192.168.178.63:8123"),
HASS_TOKEN: process.env.HASS_TOKEN, HASS_TOKEN: z.string().optional(),
/** Security Configuration */ /** Security Configuration */
JWT_SECRET: process.env.JWT_SECRET || 'your-secret-key', JWT_SECRET: z.string().default("your-secret-key"),
RATE_LIMIT: { RATE_LIMIT: z.object({
/** Time window for rate limiting in milliseconds */ /** Time window for rate limiting in milliseconds */
windowMs: 15 * 60 * 1000, // 15 minutes windowMs: z.number().default(15 * 60 * 1000), // 15 minutes
/** Maximum number of requests per window */ /** Maximum number of requests per window */
max: 100 // limit each IP to 100 requests per windowMs max: z.number().default(100), // limit each IP to 100 requests per windowMs
}, }),
/** Server-Sent Events Configuration */ /** Server-Sent Events Configuration */
SSE: { SSE: z.object({
/** Maximum number of concurrent SSE clients */ /** Maximum number of concurrent SSE clients */
MAX_CLIENTS: 1000, MAX_CLIENTS: z.number().default(1000),
/** Ping interval in milliseconds to keep connections alive */ /** Ping interval in milliseconds to keep connections alive */
PING_INTERVAL: 30000 // 30 seconds PING_INTERVAL: z.number().default(30000), // 30 seconds
}, }),
/** Logging Configuration */ /** Logging Configuration */
LOGGING: { LOGGING: z.object({
/** Log level (error, warn, info, http, debug) */ /** Log level (error, warn, info, http, debug) */
LEVEL: process.env.LOG_LEVEL || 'info', LEVEL: z.enum(["error", "warn", "info", "debug", "trace"]).default("info"),
/** Directory for log files */ /** Directory for log files */
DIR: process.env.LOG_DIR || 'logs', DIR: z.string().default("logs"),
/** Maximum log file size before rotation */ /** Maximum log file size before rotation */
MAX_SIZE: process.env.LOG_MAX_SIZE || '20m', MAX_SIZE: z.string().default("20m"),
/** Maximum number of days to keep log files */ /** Maximum number of days to keep log files */
MAX_DAYS: process.env.LOG_MAX_DAYS || '14d', MAX_DAYS: z.string().default("14d"),
/** Whether to compress rotated logs */ /** Whether to compress rotated logs */
COMPRESS: process.env.LOG_COMPRESS === 'true', COMPRESS: z.boolean().default(false),
/** Format for timestamps in logs */ /** Format for timestamps in logs */
TIMESTAMP_FORMAT: 'YYYY-MM-DD HH:mm:ss:ms', TIMESTAMP_FORMAT: z.string().default("YYYY-MM-DD HH:mm:ss:ms"),
/** Whether to include request logging */ /** Whether to include request logging */
LOG_REQUESTS: process.env.LOG_REQUESTS === 'true', LOG_REQUESTS: z.boolean().default(false),
}, }),
/** Application Version */ /** Application Version */
VERSION: '0.1.0' VERSION: z.string().default("0.1.0"),
} as const; });
/** Type definition for the configuration object */ /** Type definition for the configuration object */
export type AppConfig = typeof APP_CONFIG; export type AppConfig = z.infer<typeof AppConfigSchema>;
/** Required environment variables that must be set */ /** Required environment variables that must be set */
const requiredEnvVars = ['HASS_TOKEN'] as const; const requiredEnvVars = ["HASS_TOKEN"] as const;
/** /**
* Validate that all required environment variables are set * Validate that all required environment variables are set
@@ -83,3 +87,30 @@ for (const envVar of requiredEnvVars) {
throw new Error(`Missing required environment variable: ${envVar}`); throw new Error(`Missing required environment variable: ${envVar}`);
} }
} }
// Load and validate configuration
export const APP_CONFIG = AppConfigSchema.parse({
PORT: process.env.PORT || 4000,
NODE_ENV: process.env.NODE_ENV || "development",
HASS_HOST: process.env.HASS_HOST || "http://192.168.178.63:8123",
HASS_TOKEN: process.env.HASS_TOKEN,
JWT_SECRET: process.env.JWT_SECRET || "your-secret-key",
RATE_LIMIT: {
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
},
SSE: {
MAX_CLIENTS: 1000,
PING_INTERVAL: 30000, // 30 seconds
},
LOGGING: {
LEVEL: process.env.LOG_LEVEL || "info",
DIR: process.env.LOG_DIR || "logs",
MAX_SIZE: process.env.LOG_MAX_SIZE || "20m",
MAX_DAYS: process.env.LOG_MAX_DAYS || "14d",
COMPRESS: process.env.LOG_COMPRESS === "true",
TIMESTAMP_FORMAT: "YYYY-MM-DD HH:mm:ss:ms",
LOG_REQUESTS: process.env.LOG_REQUESTS === "true",
},
VERSION: "0.1.0",
});

View File

@@ -0,0 +1,35 @@
export const BOILERPLATE_CONFIG = {
configuration: {
LOG_LEVEL: {
type: "string" as const,
default: "debug",
description: "Logging level",
enum: ["error", "warn", "info", "debug", "trace"],
},
CACHE_DIRECTORY: {
type: "string" as const,
default: ".cache",
description: "Directory for cache files",
},
CONFIG_DIRECTORY: {
type: "string" as const,
default: ".config",
description: "Directory for configuration files",
},
DATA_DIRECTORY: {
type: "string" as const,
default: ".data",
description: "Directory for data files",
},
},
internal: {
boilerplate: {
configuration: {
LOG_LEVEL: "debug",
CACHE_DIRECTORY: ".cache",
CONFIG_DIRECTORY: ".config",
DATA_DIRECTORY: ".data",
},
},
},
};

View File

@@ -1,11 +1,50 @@
import dotenv from 'dotenv'; import { config } from "dotenv";
import { resolve } from "path";
// Load environment variables // Load environment variables based on NODE_ENV
dotenv.config(); const envFile =
process.env.NODE_ENV === "production"
? ".env"
: process.env.NODE_ENV === "test"
? ".env.test"
: ".env.development";
config({ path: resolve(process.cwd(), envFile) });
export const HASS_CONFIG = { export const HASS_CONFIG = {
BASE_URL: process.env.HASS_HOST || 'http://homeassistant.local:8123', // Base configuration
TOKEN: process.env.HASS_TOKEN || '', BASE_URL: process.env.HASS_HOST || "http://localhost:8123",
SOCKET_URL: process.env.HASS_SOCKET_URL || '', TOKEN: process.env.HASS_TOKEN || "",
SOCKET_TOKEN: process.env.HASS_TOKEN || '', SOCKET_URL: process.env.HASS_WS_URL || "ws://localhost:8123/api/websocket",
SOCKET_TOKEN: process.env.HASS_TOKEN || "",
// Boilerplate configuration
BOILERPLATE: {
CACHE_DIRECTORY: ".cache",
CONFIG_DIRECTORY: ".config",
DATA_DIRECTORY: ".data",
LOG_LEVEL: "debug",
ENVIRONMENT: process.env.NODE_ENV || "development",
},
// Application configuration
APP_NAME: "homeassistant-mcp",
APP_VERSION: "1.0.0",
// API configuration
API_VERSION: "1.0.0",
API_PREFIX: "/api",
// Security configuration
RATE_LIMIT: {
WINDOW_MS: 15 * 60 * 1000, // 15 minutes
MAX_REQUESTS: 100,
},
// WebSocket configuration
WS_CONFIG: {
AUTO_RECONNECT: true,
MAX_RECONNECT_ATTEMPTS: 3,
RECONNECT_DELAY: 1000,
},
}; };

View File

@@ -1,76 +1,85 @@
import { config } from 'dotenv'; import { config } from "dotenv";
import { resolve } from 'path'; import { resolve } from "path";
// Load environment variables based on NODE_ENV // Load environment variables based on NODE_ENV
const envFile = process.env.NODE_ENV === 'production' const envFile =
? '.env' process.env.NODE_ENV === "production"
: process.env.NODE_ENV === 'test' ? ".env"
? '.env.test' : process.env.NODE_ENV === "test"
: '.env.development'; ? ".env.test"
: ".env.development";
console.log(`Loading environment from ${envFile}`); console.log(`Loading environment from ${envFile}`);
config({ path: resolve(process.cwd(), envFile) }); config({ path: resolve(process.cwd(), envFile) });
// Home Assistant Configuration // Home Assistant Configuration
export const HASS_CONFIG = { export const HASS_CONFIG = {
HOST: process.env.HASS_HOST || 'http://homeassistant.local:8123', HOST: process.env.HASS_HOST || "http://homeassistant.local:8123",
TOKEN: process.env.HASS_TOKEN, TOKEN: process.env.HASS_TOKEN,
SOCKET_URL: process.env.HASS_SOCKET_URL || 'ws://homeassistant.local:8123/api/websocket', SOCKET_URL:
BASE_URL: process.env.HASS_HOST || 'http://homeassistant.local:8123', process.env.HASS_SOCKET_URL ||
SOCKET_TOKEN: process.env.HASS_TOKEN "ws://homeassistant.local:8123/api/websocket",
BASE_URL: process.env.HASS_HOST || "http://homeassistant.local:8123",
SOCKET_TOKEN: process.env.HASS_TOKEN,
}; };
// Server Configuration // Server Configuration
export const SERVER_CONFIG = { export const SERVER_CONFIG = {
PORT: parseInt(process.env.PORT || '3000', 10), PORT: parseInt(process.env.PORT || "3000", 10),
NODE_ENV: process.env.NODE_ENV || 'development', NODE_ENV: process.env.NODE_ENV || "development",
DEBUG: process.env.DEBUG === 'true', DEBUG: process.env.DEBUG === "true",
LOG_LEVEL: process.env.LOG_LEVEL || 'info' LOG_LEVEL: process.env.LOG_LEVEL || "info",
}; };
// AI Configuration // AI Configuration
export const AI_CONFIG = { export const AI_CONFIG = {
PROCESSOR_TYPE: process.env.PROCESSOR_TYPE || 'claude', PROCESSOR_TYPE: process.env.PROCESSOR_TYPE || "claude",
OPENAI_API_KEY: process.env.OPENAI_API_KEY OPENAI_API_KEY: process.env.OPENAI_API_KEY,
}; };
// Rate Limiting Configuration // Rate Limiting Configuration
export const RATE_LIMIT_CONFIG = { export const RATE_LIMIT_CONFIG = {
REGULAR: parseInt(process.env.RATE_LIMIT_REGULAR || '100', 10), REGULAR: parseInt(process.env.RATE_LIMIT_REGULAR || "100", 10),
WEBSOCKET: parseInt(process.env.RATE_LIMIT_WEBSOCKET || '1000', 10) WEBSOCKET: parseInt(process.env.RATE_LIMIT_WEBSOCKET || "1000", 10),
}; };
// Security Configuration // Security Configuration
export const SECURITY_CONFIG = { export const SECURITY_CONFIG = {
JWT_SECRET: process.env.JWT_SECRET || 'default_secret_key_change_in_production', JWT_SECRET:
CORS_ORIGINS: (process.env.CORS_ORIGINS || 'http://localhost:3000,http://localhost:8123') process.env.JWT_SECRET || "default_secret_key_change_in_production",
.split(',') CORS_ORIGINS: (
.map(origin => origin.trim()) process.env.CORS_ORIGINS || "http://localhost:3000,http://localhost:8123"
)
.split(",")
.map((origin) => origin.trim()),
}; };
// Test Configuration // Test Configuration
export const TEST_CONFIG = { export const TEST_CONFIG = {
HASS_HOST: process.env.TEST_HASS_HOST || 'http://localhost:8123', HASS_HOST: process.env.TEST_HASS_HOST || "http://localhost:8123",
HASS_TOKEN: process.env.TEST_HASS_TOKEN || 'test_token', HASS_TOKEN: process.env.TEST_HASS_TOKEN || "test_token",
HASS_SOCKET_URL: process.env.TEST_HASS_SOCKET_URL || 'ws://localhost:8123/api/websocket', HASS_SOCKET_URL:
PORT: parseInt(process.env.TEST_PORT || '3001', 10) process.env.TEST_HASS_SOCKET_URL || "ws://localhost:8123/api/websocket",
PORT: parseInt(process.env.TEST_PORT || "3001", 10),
}; };
// Mock Configuration (for testing) // Mock Configuration (for testing)
export const MOCK_CONFIG = { export const MOCK_CONFIG = {
SERVICES: process.env.MOCK_SERVICES === 'true', SERVICES: process.env.MOCK_SERVICES === "true",
RESPONSES_DIR: process.env.MOCK_RESPONSES_DIR || '__tests__/mock-responses' RESPONSES_DIR: process.env.MOCK_RESPONSES_DIR || "__tests__/mock-responses",
}; };
// Validate required configuration // Validate required configuration
function validateConfig() { function validateConfig() {
const missingVars: string[] = []; const missingVars: string[] = [];
if (!HASS_CONFIG.TOKEN) missingVars.push('HASS_TOKEN'); if (!HASS_CONFIG.TOKEN) missingVars.push("HASS_TOKEN");
if (!SECURITY_CONFIG.JWT_SECRET) missingVars.push('JWT_SECRET'); if (!SECURITY_CONFIG.JWT_SECRET) missingVars.push("JWT_SECRET");
if (missingVars.length > 0) { if (missingVars.length > 0) {
throw new Error(`Missing required environment variables: ${missingVars.join(', ')}`); throw new Error(
`Missing required environment variables: ${missingVars.join(", ")}`,
);
} }
} }
@@ -85,5 +94,5 @@ export const AppConfig = {
RATE_LIMIT: RATE_LIMIT_CONFIG, RATE_LIMIT: RATE_LIMIT_CONFIG,
SECURITY: SECURITY_CONFIG, SECURITY: SECURITY_CONFIG,
TEST: TEST_CONFIG, TEST: TEST_CONFIG,
MOCK: MOCK_CONFIG MOCK: MOCK_CONFIG,
}; };

View File

@@ -0,0 +1,129 @@
import { z } from "zod";
// Security configuration schema
const securityConfigSchema = z.object({
// JWT Configuration
JWT_SECRET: z.string().min(32),
JWT_EXPIRY: z.number().default(24 * 60 * 60 * 1000), // 24 hours
JWT_MAX_AGE: z.number().default(30 * 24 * 60 * 60 * 1000), // 30 days
JWT_ALGORITHM: z.enum(["HS256", "HS384", "HS512"]).default("HS256"),
// Rate Limiting
RATE_LIMIT_WINDOW: z.number().default(15 * 60 * 1000), // 15 minutes
RATE_LIMIT_MAX_REQUESTS: z.number().default(100),
RATE_LIMIT_WEBSOCKET: z.number().default(1000),
// Token Security
TOKEN_MIN_LENGTH: z.number().default(32),
MAX_FAILED_ATTEMPTS: z.number().default(5),
LOCKOUT_DURATION: z.number().default(15 * 60 * 1000), // 15 minutes
// CORS Configuration
CORS_ORIGINS: z
.array(z.string())
.default(["http://localhost:3000", "http://localhost:8123"]),
CORS_METHODS: z
.array(z.string())
.default(["GET", "POST", "PUT", "DELETE", "OPTIONS"]),
CORS_ALLOWED_HEADERS: z
.array(z.string())
.default(["Content-Type", "Authorization", "X-Requested-With"]),
CORS_EXPOSED_HEADERS: z.array(z.string()).default([]),
CORS_CREDENTIALS: z.boolean().default(true),
CORS_MAX_AGE: z.number().default(24 * 60 * 60), // 24 hours
// Content Security Policy
CSP_ENABLED: z.boolean().default(true),
CSP_REPORT_ONLY: z.boolean().default(false),
CSP_REPORT_URI: z.string().optional(),
// SSL/TLS Configuration
REQUIRE_HTTPS: z.boolean().default(process.env.NODE_ENV === "production"),
HSTS_MAX_AGE: z.number().default(31536000), // 1 year
HSTS_INCLUDE_SUBDOMAINS: z.boolean().default(true),
HSTS_PRELOAD: z.boolean().default(true),
// Cookie Security
COOKIE_SECRET: z.string().min(32).optional(),
COOKIE_SECURE: z.boolean().default(process.env.NODE_ENV === "production"),
COOKIE_HTTP_ONLY: z.boolean().default(true),
COOKIE_SAME_SITE: z.enum(["Strict", "Lax", "None"]).default("Strict"),
// Request Limits
MAX_REQUEST_SIZE: z.number().default(1024 * 1024), // 1MB
MAX_REQUEST_FIELDS: z.number().default(1000),
});
// Parse environment variables
const parseEnvConfig = () => {
const config = {
JWT_SECRET:
process.env.JWT_SECRET || "default_secret_key_change_in_production",
JWT_EXPIRY: parseInt(process.env.JWT_EXPIRY || "86400000"),
JWT_MAX_AGE: parseInt(process.env.JWT_MAX_AGE || "2592000000"),
JWT_ALGORITHM: process.env.JWT_ALGORITHM || "HS256",
RATE_LIMIT_WINDOW: parseInt(process.env.RATE_LIMIT_WINDOW || "900000"),
RATE_LIMIT_MAX_REQUESTS: parseInt(
process.env.RATE_LIMIT_MAX_REQUESTS || "100",
),
RATE_LIMIT_WEBSOCKET: parseInt(process.env.RATE_LIMIT_WEBSOCKET || "1000"),
TOKEN_MIN_LENGTH: parseInt(process.env.TOKEN_MIN_LENGTH || "32"),
MAX_FAILED_ATTEMPTS: parseInt(process.env.MAX_FAILED_ATTEMPTS || "5"),
LOCKOUT_DURATION: parseInt(process.env.LOCKOUT_DURATION || "900000"),
CORS_ORIGINS: (
process.env.CORS_ORIGINS || "http://localhost:3000,http://localhost:8123"
)
.split(",")
.map((origin) => origin.trim()),
CORS_METHODS: (process.env.CORS_METHODS || "GET,POST,PUT,DELETE,OPTIONS")
.split(",")
.map((method) => method.trim()),
CORS_ALLOWED_HEADERS: (
process.env.CORS_ALLOWED_HEADERS ||
"Content-Type,Authorization,X-Requested-With"
)
.split(",")
.map((header) => header.trim()),
CORS_EXPOSED_HEADERS: (process.env.CORS_EXPOSED_HEADERS || "")
.split(",")
.filter(Boolean)
.map((header) => header.trim()),
CORS_CREDENTIALS: process.env.CORS_CREDENTIALS !== "false",
CORS_MAX_AGE: parseInt(process.env.CORS_MAX_AGE || "86400"),
CSP_ENABLED: process.env.CSP_ENABLED !== "false",
CSP_REPORT_ONLY: process.env.CSP_REPORT_ONLY === "true",
CSP_REPORT_URI: process.env.CSP_REPORT_URI,
REQUIRE_HTTPS:
process.env.REQUIRE_HTTPS !== "false" &&
process.env.NODE_ENV === "production",
HSTS_MAX_AGE: parseInt(process.env.HSTS_MAX_AGE || "31536000"),
HSTS_INCLUDE_SUBDOMAINS: process.env.HSTS_INCLUDE_SUBDOMAINS !== "false",
HSTS_PRELOAD: process.env.HSTS_PRELOAD !== "false",
COOKIE_SECRET: process.env.COOKIE_SECRET,
COOKIE_SECURE:
process.env.COOKIE_SECURE !== "false" &&
process.env.NODE_ENV === "production",
COOKIE_HTTP_ONLY: process.env.COOKIE_HTTP_ONLY !== "false",
COOKIE_SAME_SITE: (process.env.COOKIE_SAME_SITE || "Strict") as
| "Strict"
| "Lax"
| "None",
MAX_REQUEST_SIZE: parseInt(process.env.MAX_REQUEST_SIZE || "1048576"),
MAX_REQUEST_FIELDS: parseInt(process.env.MAX_REQUEST_FIELDS || "1000"),
};
return securityConfigSchema.parse(config);
};
// Export the validated configuration
export const SECURITY_CONFIG = parseEnvConfig();
// Export types
export type SecurityConfig = z.infer<typeof securityConfigSchema>;

View File

@@ -1,14 +1,14 @@
import { EventEmitter } from 'events'; import { EventEmitter } from "events";
// Resource types // Resource types
export enum ResourceType { export enum ResourceType {
DEVICE = 'device', DEVICE = "device",
AREA = 'area', AREA = "area",
USER = 'user', USER = "user",
AUTOMATION = 'automation', AUTOMATION = "automation",
SCENE = 'scene', SCENE = "scene",
SCRIPT = 'script', SCRIPT = "script",
GROUP = 'group' GROUP = "group",
} }
// Resource state interface // Resource state interface
@@ -23,11 +23,11 @@ export interface ResourceState {
// Resource relationship types // Resource relationship types
export enum RelationType { export enum RelationType {
CONTAINS = 'contains', CONTAINS = "contains",
CONTROLS = 'controls', CONTROLS = "controls",
TRIGGERS = 'triggers', TRIGGERS = "triggers",
DEPENDS_ON = 'depends_on', DEPENDS_ON = "depends_on",
GROUPS = 'groups' GROUPS = "groups",
} }
// Resource relationship interface // Resource relationship interface
@@ -52,7 +52,7 @@ export class ContextManager extends EventEmitter {
// Resource management // Resource management
public addResource(resource: ResourceState): void { public addResource(resource: ResourceState): void {
this.resources.set(resource.id, resource); this.resources.set(resource.id, resource);
this.emit('resource_added', resource); this.emit("resource_added", resource);
} }
public updateResource(id: string, update: Partial<ResourceState>): void { public updateResource(id: string, update: Partial<ResourceState>): void {
@@ -65,10 +65,10 @@ export class ContextManager extends EventEmitter {
const updatedResource = { const updatedResource = {
...resource, ...resource,
...update, ...update,
lastUpdated: Date.now() lastUpdated: Date.now(),
}; };
this.resources.set(id, updatedResource); this.resources.set(id, updatedResource);
this.emit('resource_updated', updatedResource); this.emit("resource_updated", updatedResource);
} }
} }
@@ -78,25 +78,32 @@ export class ContextManager extends EventEmitter {
this.resources.delete(id); this.resources.delete(id);
// Remove related relationships // Remove related relationships
this.relationships = this.relationships.filter( this.relationships = this.relationships.filter(
rel => rel.sourceId !== id && rel.targetId !== id (rel) => rel.sourceId !== id && rel.targetId !== id,
); );
this.emit('resource_removed', resource); this.emit("resource_removed", resource);
} }
} }
// Relationship management // Relationship management
public addRelationship(relationship: ResourceRelationship): void { public addRelationship(relationship: ResourceRelationship): void {
this.relationships.push(relationship); this.relationships.push(relationship);
this.emit('relationship_added', relationship); this.emit("relationship_added", relationship);
} }
public removeRelationship(sourceId: string, targetId: string, type: RelationType): void { public removeRelationship(
sourceId: string,
targetId: string,
type: RelationType,
): void {
const index = this.relationships.findIndex( const index = this.relationships.findIndex(
rel => rel.sourceId === sourceId && rel.targetId === targetId && rel.type === type (rel) =>
rel.sourceId === sourceId &&
rel.targetId === targetId &&
rel.type === type,
); );
if (index !== -1) { if (index !== -1) {
const removed = this.relationships.splice(index, 1)[0]; const removed = this.relationships.splice(index, 1)[0];
this.emit('relationship_removed', removed); this.emit("relationship_removed", removed);
} }
} }
@@ -121,14 +128,14 @@ export class ContextManager extends EventEmitter {
public getResourcesByType(type: ResourceType): ResourceState[] { public getResourcesByType(type: ResourceType): ResourceState[] {
return Array.from(this.resources.values()).filter( return Array.from(this.resources.values()).filter(
resource => resource.type === type (resource) => resource.type === type,
); );
} }
public getRelatedResources( public getRelatedResources(
id: string, id: string,
type?: RelationType, type?: RelationType,
depth: number = 1 depth: number = 1,
): ResourceState[] { ): ResourceState[] {
const related = new Set<ResourceState>(); const related = new Set<ResourceState>();
const visited = new Set<string>(); const visited = new Set<string>();
@@ -138,12 +145,14 @@ export class ContextManager extends EventEmitter {
visited.add(currentId); visited.add(currentId);
this.relationships this.relationships
.filter(rel => .filter(
(rel) =>
(rel.sourceId === currentId || rel.targetId === currentId) && (rel.sourceId === currentId || rel.targetId === currentId) &&
(!type || rel.type === type) (!type || rel.type === type),
) )
.forEach(rel => { .forEach((rel) => {
const relatedId = rel.sourceId === currentId ? rel.targetId : rel.sourceId; const relatedId =
rel.sourceId === currentId ? rel.targetId : rel.sourceId;
const relatedResource = this.resources.get(relatedId); const relatedResource = this.resources.get(relatedId);
if (relatedResource) { if (relatedResource) {
related.add(relatedResource); related.add(relatedResource);
@@ -168,25 +177,29 @@ export class ContextManager extends EventEmitter {
}; };
} { } {
const dependencies = this.relationships const dependencies = this.relationships
.filter(rel => rel.sourceId === id && rel.type === RelationType.DEPENDS_ON) .filter(
.map(rel => rel.targetId); (rel) => rel.sourceId === id && rel.type === RelationType.DEPENDS_ON,
)
.map((rel) => rel.targetId);
const dependents = this.relationships const dependents = this.relationships
.filter(rel => rel.targetId === id && rel.type === RelationType.DEPENDS_ON) .filter(
.map(rel => rel.sourceId); (rel) => rel.targetId === id && rel.type === RelationType.DEPENDS_ON,
)
.map((rel) => rel.sourceId);
const groups = this.relationships const groups = this.relationships
.filter(rel => rel.targetId === id && rel.type === RelationType.GROUPS) .filter((rel) => rel.targetId === id && rel.type === RelationType.GROUPS)
.map(rel => rel.sourceId); .map((rel) => rel.sourceId);
const usage = { const usage = {
triggerCount: this.relationships.filter( triggerCount: this.relationships.filter(
rel => rel.sourceId === id && rel.type === RelationType.TRIGGERS (rel) => rel.sourceId === id && rel.type === RelationType.TRIGGERS,
).length, ).length,
controlCount: this.relationships.filter( controlCount: this.relationships.filter(
rel => rel.sourceId === id && rel.type === RelationType.CONTROLS (rel) => rel.sourceId === id && rel.type === RelationType.CONTROLS,
).length, ).length,
groupCount: groups.length groupCount: groups.length,
}; };
return { dependencies, dependents, groups, usage }; return { dependencies, dependents, groups, usage };
@@ -195,7 +208,7 @@ export class ContextManager extends EventEmitter {
// Event subscriptions // Event subscriptions
public subscribeToResource( public subscribeToResource(
id: string, id: string,
callback: (state: ResourceState) => void callback: (state: ResourceState) => void,
): () => void { ): () => void {
const handler = (resource: ResourceState) => { const handler = (resource: ResourceState) => {
if (resource.id === id) { if (resource.id === id) {
@@ -203,13 +216,13 @@ export class ContextManager extends EventEmitter {
} }
}; };
this.on('resource_updated', handler); this.on("resource_updated", handler);
return () => this.off('resource_updated', handler); return () => this.off("resource_updated", handler);
} }
public subscribeToType( public subscribeToType(
type: ResourceType, type: ResourceType,
callback: (state: ResourceState) => void callback: (state: ResourceState) => void,
): () => void { ): () => void {
const handler = (resource: ResourceState) => { const handler = (resource: ResourceState) => {
if (resource.type === type) { if (resource.type === type) {
@@ -217,8 +230,8 @@ export class ContextManager extends EventEmitter {
} }
}; };
this.on('resource_updated', handler); this.on("resource_updated", handler);
return () => this.off('resource_updated', handler); return () => this.off("resource_updated", handler);
} }
} }

View File

@@ -1,687 +1,125 @@
import { CreateApplication, TServiceParams, ServiceFunction, AlsExtension, GetApisResult, ILogger, InternalDefinition, TContext, TInjectedConfig, TLifecycleBase, TScheduler } from "@digital-alchemy/core"; import type { HassEntity } from "../interfaces/hass.js";
import { Area, Backup, CallProxy, Configure, Device, EntityManager, EventsService, FetchAPI, FetchInternals, Floor, IDByExtension, Label, LIB_HASS, ReferenceService, Registry, WebsocketAPI, Zone } from "@digital-alchemy/hass";
import { DomainSchema } from "../schemas.js";
import { HASS_CONFIG } from "../config/index.js";
import WebSocket from 'ws';
import { EventEmitter } from 'events';
import * as HomeAssistant from '../types/hass.js';
import { HassEntity, HassEvent, HassService } from '../interfaces/hass.js';
type Environments = "development" | "production" | "test"; class HomeAssistantAPI {
private baseUrl: string;
private token: string;
// Define the type for Home Assistant services constructor() {
type HassServiceMethod = (data: Record<string, unknown>) => Promise<void>; this.baseUrl = process.env.HASS_HOST || "http://localhost:8123";
this.token = process.env.HASS_TOKEN || "";
type HassServices = { if (!this.token || this.token === "your_hass_token_here") {
[K in keyof typeof DomainSchema.Values]: { throw new Error("HASS_TOKEN is required but not set in environment variables");
[service: string]: HassServiceMethod;
};
};
// Define the type for Home Assistant instance
interface HassInstance {
states: {
get: () => Promise<HassEntity[]>;
subscribe: (callback: (states: HassEntity[]) => void) => Promise<number>;
unsubscribe: (subscription: number) => void;
};
services: {
get: () => Promise<Record<string, Record<string, HassService>>>;
call: (domain: string, service: string, serviceData?: Record<string, any>) => Promise<void>;
};
connection: {
socket: WebSocket;
subscribeEvents: (callback: (event: HassEvent) => void, eventType?: string) => Promise<number>;
unsubscribeEvents: (subscription: number) => void;
};
subscribeEvents: (callback: (event: HassEvent) => void, eventType?: string) => Promise<number>;
unsubscribeEvents: (subscription: number) => void;
}
// Configuration type for application with more specific constraints
type ApplicationConfiguration = {
NODE_ENV: ServiceFunction<Environments>;
};
// Strict configuration type for Home Assistant
type HassConfiguration = {
BASE_URL: {
type: "string";
description: string;
required: true;
default: string;
};
TOKEN: {
type: "string";
description: string;
required: true;
default: string;
};
SOCKET_URL: {
type: "string";
description: string;
required: true;
default: string;
};
SOCKET_TOKEN: {
type: "string";
description: string;
required: true;
default: string;
};
};
// application
const MY_APP = CreateApplication<ApplicationConfiguration, {}>({
configuration: {
NODE_ENV: {
type: "string",
default: "development",
enum: ["development", "production", "test"],
description: "Code runner addon can set with it's own NODE_ENV",
},
},
services: {
NODE_ENV: () => {
// Directly return the default value or use process.env
return (process.env.NODE_ENV as Environments) || "development";
}
},
libraries: [
{
...LIB_HASS,
configuration: {
BASE_URL: {
type: "string",
description: "Home Assistant base URL",
required: true,
default: HASS_CONFIG.BASE_URL
},
TOKEN: {
type: "string",
description: "Home Assistant long-lived access token",
required: true,
default: HASS_CONFIG.TOKEN
},
SOCKET_URL: {
type: "string",
description: "Home Assistant WebSocket URL",
required: true,
default: HASS_CONFIG.SOCKET_URL
},
SOCKET_TOKEN: {
type: "string",
description: "Home Assistant WebSocket token",
required: true,
default: HASS_CONFIG.SOCKET_TOKEN
}
}
}
],
name: 'hass' as const
});
export interface HassConfig {
host: string;
token: string;
}
const CONFIG: Record<string, HassConfig> = {
development: {
host: process.env.HASS_HOST || 'http://localhost:8123',
token: process.env.HASS_TOKEN || ''
},
production: {
host: process.env.HASS_HOST || '',
token: process.env.HASS_TOKEN || ''
},
test: {
host: 'http://localhost:8123',
token: 'test_token'
}
};
export class HassWebSocketClient extends EventEmitter {
private ws: WebSocket | null = null;
private messageId = 1;
private subscriptions = new Map<number, (data: any) => void>();
private reconnectAttempts = 0;
private options: {
autoReconnect: boolean;
maxReconnectAttempts: number;
reconnectDelay: number;
};
constructor(
private url: string,
private token: string,
options: Partial<typeof HassWebSocketClient.prototype.options> = {}
) {
super();
this.options = {
autoReconnect: true,
maxReconnectAttempts: 3,
reconnectDelay: 1000,
...options
};
} }
async connect(): Promise<void> { console.log(`Initializing Home Assistant API with base URL: ${this.baseUrl}`);
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
return;
} }
return new Promise((resolve, reject) => { private async fetchApi(endpoint: string, options: RequestInit = {}) {
this.ws = new WebSocket(this.url); const url = `${this.baseUrl}/api/${endpoint}`;
console.log(`Making request to: ${url}`);
this.ws.on('open', () => { console.log('Request options:', {
this.emit('open'); method: options.method || 'GET',
const authMessage: HomeAssistant.AuthMessage = { headers: {
type: 'auth', Authorization: 'Bearer [REDACTED]',
access_token: this.token "Content-Type": "application/json",
}; ...options.headers,
this.ws?.send(JSON.stringify(authMessage)); },
body: options.body ? JSON.parse(options.body as string) : undefined
}); });
this.ws.on('message', (data: string) => {
try { try {
const message = JSON.parse(data); const response = await fetch(url, {
this.handleMessage(message); ...options,
headers: {
Authorization: `Bearer ${this.token}`,
"Content-Type": "application/json",
...options.headers,
},
});
if (!response.ok) {
const errorText = await response.text();
console.error('Home Assistant API error:', {
status: response.status,
statusText: response.statusText,
error: errorText
});
throw new Error(`Home Assistant API error: ${response.status} ${response.statusText} - ${errorText}`);
}
const data = await response.json();
console.log('Response data:', data);
return data;
} catch (error) { } catch (error) {
this.emit('error', new Error('Failed to parse message')); console.error('Failed to make request:', error);
} throw error;
});
this.ws.on('close', () => {
this.emit('disconnected');
if (this.options.autoReconnect && this.reconnectAttempts < this.options.maxReconnectAttempts) {
setTimeout(() => {
this.reconnectAttempts++;
this.connect();
}, this.options.reconnectDelay);
}
});
this.ws.on('error', (error) => {
this.emit('error', error);
reject(error);
});
});
}
private handleMessage(message: any): void {
switch (message.type) {
case 'auth_ok':
this.emit('auth_ok');
break;
case 'auth_invalid':
this.emit('auth_invalid');
break;
case 'result':
// Handle command results
break;
case 'event':
if (message.event) {
this.emit('event', message.event);
const subscription = this.subscriptions.get(message.id);
if (subscription) {
subscription(message.event.data);
}
}
break;
default:
this.emit('error', new Error(`Unknown message type: ${message.type}`));
} }
} }
async subscribeEvents(callback: (data: any) => void, eventType?: string): Promise<number> { async getStates(): Promise<HassEntity[]> {
const id = this.messageId++; return this.fetchApi("states");
const message = {
id,
type: 'subscribe_events',
event_type: eventType
};
return new Promise((resolve, reject) => {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
reject(new Error('WebSocket not connected'));
return;
} }
this.subscriptions.set(id, callback); async getState(entityId: string): Promise<HassEntity> {
this.ws.send(JSON.stringify(message)); return this.fetchApi(`states/${entityId}`);
resolve(id);
});
}
async unsubscribeEvents(subscriptionId: number): Promise<void> {
const message = {
id: this.messageId++,
type: 'unsubscribe_events',
subscription: subscriptionId
};
return new Promise((resolve, reject) => {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
reject(new Error('WebSocket not connected'));
return;
}
this.ws.send(JSON.stringify(message));
this.subscriptions.delete(subscriptionId);
resolve();
});
}
disconnect(): void {
if (this.ws) {
this.ws.close();
this.ws = null;
}
}
}
export class HassInstanceImpl implements HassInstance {
public readonly baseUrl: string;
public readonly token: string;
public wsClient: HassWebSocketClient | undefined;
public readonly services: HassInstance['services'];
public readonly states: HassInstance['states'];
public readonly connection: HassInstance['connection'];
constructor(baseUrl: string, token: string) {
this.baseUrl = baseUrl;
this.token = token;
// Initialize services
this.services = {
get: async () => {
const response = await fetch(`${this.baseUrl}/api/services`, {
headers: {
Authorization: `Bearer ${this.token}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Failed to fetch services: ${response.statusText}`);
}
return response.json();
},
call: async (domain: string, service: string, serviceData?: Record<string, any>) => {
const response = await fetch(`${this.baseUrl}/api/services/${domain}/${service}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(serviceData),
});
if (!response.ok) {
throw new Error(`Service call failed: ${response.statusText}`);
}
}
};
// Initialize states
this.states = {
get: async () => {
const response = await fetch(`${this.baseUrl}/api/states`, {
headers: {
Authorization: `Bearer ${this.token}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Failed to fetch states: ${response.statusText}`);
}
return response.json();
},
subscribe: async (callback: (states: HassEntity[]) => void) => {
return this.subscribeEvents((event: HassEvent) => {
if (event.event_type === 'state_changed') {
this.states.get().then(callback);
}
}, 'state_changed');
},
unsubscribe: (subscription: number) => {
this.unsubscribeEvents(subscription);
}
};
// Initialize connection
this.connection = {
socket: new WebSocket(this.baseUrl.replace(/^http/, 'ws') + '/api/websocket'),
subscribeEvents: this.subscribeEvents.bind(this),
unsubscribeEvents: this.unsubscribeEvents.bind(this)
};
this.initialize();
}
public als!: AlsExtension;
public context!: TContext;
public event!: EventEmitter<[never]>;
public internal!: InternalDefinition;
public lifecycle!: TLifecycleBase;
public logger!: ILogger;
public scheduler!: TScheduler;
public config!: TInjectedConfig;
public params!: TServiceParams;
public hass!: GetApisResult<{
area: typeof Area;
backup: typeof Backup;
call: typeof CallProxy;
configure: typeof Configure;
device: typeof Device;
entity: typeof EntityManager;
events: typeof EventsService;
fetch: typeof FetchAPI;
floor: typeof Floor;
idBy: typeof IDByExtension;
internals: typeof FetchInternals;
label: typeof Label;
refBy: typeof ReferenceService;
registry: typeof Registry;
socket: typeof WebsocketAPI;
zone: typeof Zone;
}>;
private initialize() {
// Initialize all required properties with proper type instantiation
this.als = {} as AlsExtension;
this.context = {} as TContext;
this.event = new EventEmitter();
this.internal = {} as InternalDefinition;
this.lifecycle = {} as TLifecycleBase;
this.logger = {} as ILogger;
this.scheduler = {} as TScheduler;
this.config = {} as TInjectedConfig;
this.params = {} as TServiceParams;
this.hass = {} as GetApisResult<any>;
}
async fetchStates(): Promise<HomeAssistant.Entity[]> {
const response = await fetch(`${this.baseUrl}/api/states`, {
headers: {
Authorization: `Bearer ${this.token}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Failed to fetch states: ${response.statusText}`);
}
const data = await response.json();
return data as HomeAssistant.Entity[];
}
async fetchState(entityId: string): Promise<HomeAssistant.Entity> {
const response = await fetch(`${this.baseUrl}/api/states/${entityId}`, {
headers: {
Authorization: `Bearer ${this.token}`,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Failed to fetch state: ${response.statusText}`);
}
const data = await response.json();
return data as HomeAssistant.Entity;
} }
async callService(domain: string, service: string, data: Record<string, any>): Promise<void> { async callService(domain: string, service: string, data: Record<string, any>): Promise<void> {
const response = await fetch(`${this.baseUrl}/api/services/${domain}/${service}`, { await this.fetchApi(`services/${domain}/${service}`, {
method: 'POST', method: "POST",
headers: {
Authorization: `Bearer ${this.token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
if (!response.ok) {
throw new Error(`Service call failed: ${response.statusText}`);
}
}
async subscribeEvents(callback: (event: HassEvent) => void, eventType?: string): Promise<number> {
if (!this.wsClient) {
this.wsClient = new HassWebSocketClient(
this.baseUrl.replace(/^http/, 'ws') + '/api/websocket',
this.token
);
await this.wsClient.connect();
}
return this.wsClient.subscribeEvents((data: any) => {
const hassEvent: HassEvent = {
event_type: data.event_type,
data: data.data,
origin: data.origin,
time_fired: data.time_fired,
context: {
id: data.context.id,
parent_id: data.context.parent_id,
user_id: data.context.user_id
}
};
callback(hassEvent);
}, eventType);
}
async unsubscribeEvents(subscriptionId: number): Promise<void> {
if (this.wsClient) {
await this.wsClient.unsubscribeEvents(subscriptionId);
}
} }
} }
class HomeAssistantInstance implements HassInstance { let instance: HomeAssistantAPI | null = null;
private messageId = 1;
private messageCallbacks = new Map<number, (result: any) => void>();
private eventCallbacks = new Map<number, (event: HassEvent) => void>();
private stateCallbacks = new Map<number, (states: HassEntity[]) => void>();
private _authenticated = false;
private socket: WebSocket;
private readonly _states: HassInstance['states'];
private readonly _services: HassInstance['services'];
private readonly _connection: HassInstance['connection'];
constructor() { export async function get_hass() {
if (!HASS_CONFIG.TOKEN) { if (!instance) {
throw new Error('Home Assistant token is required'); try {
} instance = new HomeAssistantAPI();
// Verify connection by trying to get states
this.socket = new WebSocket(HASS_CONFIG.SOCKET_URL); await instance.getStates();
console.log('Successfully connected to Home Assistant');
this._states = { } catch (error) {
get: async (): Promise<HassEntity[]> => { console.error('Failed to initialize Home Assistant connection:', error);
const message = { instance = null;
type: 'get_states' throw error;
};
return this.sendMessage(message);
},
subscribe: async (callback: (states: HassEntity[]) => void): Promise<number> => {
const id = this.messageId++;
this.stateCallbacks.set(id, callback);
const message = {
type: 'subscribe_events',
event_type: 'state_changed'
};
await this.sendMessage(message);
return id;
},
unsubscribe: (subscription: number): void => {
this.stateCallbacks.delete(subscription);
}
};
this._services = {
get: async (): Promise<Record<string, Record<string, HassService>>> => {
const message = {
type: 'get_services'
};
return this.sendMessage(message);
},
call: async (domain: string, service: string, serviceData?: Record<string, any>): Promise<void> => {
const message = {
type: 'call_service',
domain,
service,
service_data: serviceData
};
await this.sendMessage(message);
}
};
this._connection = {
socket: this.socket,
subscribeEvents: this.subscribeEvents.bind(this),
unsubscribeEvents: this.unsubscribeEvents.bind(this)
};
this.setupWebSocket();
}
get authenticated(): boolean {
return this._authenticated;
}
get states(): HassInstance['states'] {
return this._states;
}
get services(): HassInstance['services'] {
return this._services;
}
get connection(): HassInstance['connection'] {
return this._connection;
}
private setupWebSocket() {
this.socket.on('open', () => {
this.authenticate();
});
this.socket.on('message', (data: WebSocket.Data) => {
if (typeof data === 'string') {
const message = JSON.parse(data);
this.handleMessage(message);
}
});
this.socket.on('close', () => {
console.log('WebSocket connection closed');
// Implement reconnection logic here
});
this.socket.on('error', (error) => {
console.error('WebSocket error:', error);
});
}
private authenticate() {
const auth = {
type: 'auth',
access_token: HASS_CONFIG.TOKEN
};
this.socket.send(JSON.stringify(auth));
}
private handleMessage(message: any) {
if (message.type === 'auth_ok') {
this._authenticated = true;
console.log('Authenticated with Home Assistant');
return;
}
if (message.type === 'auth_invalid') {
console.error('Authentication failed:', message.message);
return;
}
if (message.type === 'event') {
const callback = this.eventCallbacks.get(message.id);
if (callback) {
callback(message.event);
}
return;
}
if (message.type === 'result') {
const callback = this.messageCallbacks.get(message.id);
if (callback) {
callback(message.result);
this.messageCallbacks.delete(message.id);
}
return;
} }
} }
return instance;
private async sendMessage(message: any): Promise<any> {
if (!this._authenticated) {
throw new Error('Not authenticated with Home Assistant');
}
return new Promise((resolve, reject) => {
const id = this.messageId++;
message.id = id;
this.messageCallbacks.set(id, resolve);
this.socket.send(JSON.stringify(message));
// Add timeout
setTimeout(() => {
this.messageCallbacks.delete(id);
reject(new Error('Message timeout'));
}, 10000);
});
}
public async subscribeEvents(callback: (event: HassEvent) => void, eventType?: string): Promise<number> {
const id = this.messageId++;
this.eventCallbacks.set(id, callback);
const message = {
type: 'subscribe_events',
event_type: eventType
};
await this.sendMessage(message);
return id;
}
public unsubscribeEvents(subscription: number): void {
this.eventCallbacks.delete(subscription);
}
} }
let hassInstance: HomeAssistantInstance | null = null; // Helper function to call Home Assistant services
export async function call_service(
export async function get_hass(): Promise<HassInstance> { domain: string,
if (!hassInstance) { service: string,
hassInstance = new HomeAssistantInstance(); data: Record<string, any>,
// Wait for authentication ) {
await new Promise<void>((resolve) => { const hass = await get_hass();
const checkAuth = () => { return hass.callService(domain, service, data);
if (hassInstance?.authenticated) { }
resolve();
} else { // Helper function to list devices
setTimeout(checkAuth, 100); export async function list_devices() {
} const hass = await get_hass();
}; const states = await hass.getStates();
checkAuth(); return states.map((state: HassEntity) => ({
}); entity_id: state.entity_id,
} state: state.state,
return hassInstance; attributes: state.attributes
}));
}
// Helper function to get entity states
export async function get_states() {
const hass = await get_hass();
return hass.getStates();
}
// Helper function to get a specific entity state
export async function get_state(entity_id: string) {
const hass = await get_hass();
return hass.getState(entity_id);
} }

View File

@@ -1,14 +1,14 @@
const check = async () => { const check = async () => {
try { try {
const response = await fetch('http://localhost:3000/health'); const response = await fetch("http://localhost:3000/health");
if (!response.ok) { if (!response.ok) {
console.error('Health check failed:', response.status); console.error("Health check failed:", response.status);
process.exit(1); process.exit(1);
} }
console.log('Health check passed'); console.log("Health check passed");
process.exit(0); process.exit(0);
} catch (error) { } catch (error) {
console.error('Health check failed:', error); console.error("Health check failed:", error);
process.exit(1); process.exit(1);
} }
}; };

View File

@@ -1,73 +1,151 @@
/** import "./polyfills.js";
* Home Assistant MCP (Master Control Program) import { config } from "dotenv";
* Main application entry point import { resolve } from "path";
* import { Elysia } from "elysia";
* This file initializes the Express server and sets up necessary import { cors } from "@elysiajs/cors";
* middleware and routes for the application when not in Claude mode. import { swagger } from "@elysiajs/swagger";
* import {
* @module index rateLimiter,
*/ securityHeaders,
validateRequest,
sanitizeInput,
errorHandler,
} from "./security/index.js";
import {
get_hass,
call_service,
list_devices,
get_states,
get_state,
} from "./hass/index.js";
import { z } from "zod";
import {
commonCommands,
coverCommands,
climateCommands,
type Command,
} from "./commands.js";
import express from 'express'; // Load environment variables based on NODE_ENV
import { APP_CONFIG } from './config/app.config.js'; const envFile =
import { apiRoutes } from './routes/index.js'; process.env.NODE_ENV === "production"
import { securityHeaders, rateLimiter, validateRequest, sanitizeInput, errorHandler } from './security/index.js'; ? ".env"
import { requestLogger, errorLogger } from './middleware/logging.middleware.js'; : process.env.NODE_ENV === "test"
import { get_hass } from './hass/index.js'; ? ".env.test"
import { LiteMCP } from 'litemcp'; : ".env.development";
import { logger } from './utils/logger.js';
import { initLogRotation } from './utils/log-rotation.js';
logger.info('Starting Home Assistant MCP...'); console.log(`Loading environment from ${envFile}`);
logger.info('Initializing Home Assistant connection...'); config({ path: resolve(process.cwd(), envFile) });
// Initialize log rotation // Configuration
initLogRotation(); const HASS_TOKEN = process.env.HASS_TOKEN;
const PORT = parseInt(process.env.PORT || "4000", 10);
/** console.log("Initializing Home Assistant connection...");
* Initialize LiteMCP instance
* This provides the core MCP functionality
*/
const server = new LiteMCP('home-assistant', APP_CONFIG.VERSION);
// Only start Express server when not in Claude mode // Define Tool interface
if (process.env.PROCESSOR_TYPE !== 'claude') { interface Tool {
/** name: string;
* Initialize Express application with security middleware description: string;
* and route handlers parameters: z.ZodType<any>;
*/ execute: (params: any) => Promise<any>;
const app = express();
// Apply logging middleware first to catch all requests
app.use(requestLogger);
// Apply security middleware
app.use(securityHeaders);
app.use(rateLimiter);
app.use(express.json());
app.use(validateRequest);
app.use(sanitizeInput);
/**
* Mount API routes under /api
* All API endpoints are prefixed with /api
*/
app.use('/api', apiRoutes);
/**
* Apply error handling middleware
* This should be the last middleware in the chain
*/
app.use(errorLogger);
app.use(errorHandler);
/**
* Start the server and listen for incoming connections
* The port is configured in the environment variables
*/
app.listen(APP_CONFIG.PORT, () => {
logger.info(`Server is running on port ${APP_CONFIG.PORT}`);
});
} else {
logger.info('Running in Claude mode - Express server disabled');
} }
// Array to store tools
const tools: Tool[] = [];
// Define the list devices tool
const listDevicesTool: Tool = {
name: "list_devices",
description: "List all available Home Assistant devices",
parameters: z.object({}),
execute: async () => {
try {
const devices = await list_devices();
return {
success: true,
devices,
};
} catch (error) {
return {
success: false,
message:
error instanceof Error ? error.message : "Unknown error occurred",
};
}
},
};
// Add tools to the array
tools.push(listDevicesTool);
// Add the Home Assistant control tool
const controlTool: Tool = {
name: "control",
description: "Control Home Assistant devices and services",
parameters: z.object({
command: z.enum([
...commonCommands,
...coverCommands,
...climateCommands,
] as [string, ...string[]]),
entity_id: z.string().describe("The ID of the entity to control"),
}),
execute: async (params: { command: Command; entity_id: string }) => {
try {
const [domain] = params.entity_id.split(".");
await call_service(domain, params.command, {
entity_id: params.entity_id,
});
return {
success: true,
message: `Command ${params.command} executed successfully on ${params.entity_id}`,
};
} catch (error) {
return {
success: false,
message:
error instanceof Error ? error.message : "Unknown error occurred",
};
}
},
};
// Add the control tool to the array
tools.push(controlTool);
// Initialize Elysia app with middleware
const app = new Elysia()
.use(cors())
.use(swagger())
.use(rateLimiter)
.use(securityHeaders)
.use(validateRequest)
.use(sanitizeInput)
.use(errorHandler);
// Health check endpoint
app.get("/health", () => ({
status: "ok",
timestamp: new Date().toISOString(),
version: "0.1.0",
}));
// Create API endpoints for each tool
tools.forEach((tool) => {
app.post(`/api/tools/${tool.name}`, async ({ body }: { body: Record<string, unknown> }) => {
const result = await tool.execute(body);
return result;
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Handle server shutdown
process.on("SIGTERM", () => {
console.log("Received SIGTERM. Shutting down gracefully...");
process.exit(0);
});

View File

@@ -29,7 +29,10 @@ export interface HassInstance {
states: HassStates; states: HassStates;
services: HassServices; services: HassServices;
connection: HassConnection; connection: HassConnection;
subscribeEvents: (callback: (event: HassEvent) => void, eventType?: string) => Promise<number>; subscribeEvents: (
callback: (event: HassEvent) => void,
eventType?: string,
) => Promise<number>;
unsubscribeEvents: (subscription: number) => void; unsubscribeEvents: (subscription: number) => void;
} }
@@ -41,12 +44,19 @@ export interface HassStates {
export interface HassServices { export interface HassServices {
get: () => Promise<Record<string, Record<string, HassService>>>; get: () => Promise<Record<string, Record<string, HassService>>>;
call: (domain: string, service: string, serviceData?: Record<string, any>) => Promise<void>; call: (
domain: string,
service: string,
serviceData?: Record<string, any>,
) => Promise<void>;
} }
export interface HassConnection { export interface HassConnection {
socket: WebSocket; socket: WebSocket;
subscribeEvents: (callback: (event: HassEvent) => void, eventType?: string) => Promise<number>; subscribeEvents: (
callback: (event: HassEvent) => void,
eventType?: string,
) => Promise<number>;
unsubscribeEvents: (subscription: number) => void; unsubscribeEvents: (subscription: number) => void;
} }
@@ -58,13 +68,16 @@ export interface HassService {
domain: string[]; domain: string[];
}; };
}; };
fields: Record<string, { fields: Record<
string,
{
name: string; name: string;
description: string; description: string;
required?: boolean; required?: boolean;
example?: any; example?: any;
selector?: any; selector?: any;
}>; }
>;
} }
export interface HassEvent { export interface HassEvent {

View File

@@ -1,4 +1,4 @@
import { z } from 'zod'; import { z } from "zod";
// Tool interfaces // Tool interfaces
export interface Tool { export interface Tool {
@@ -39,8 +39,8 @@ export type {
HassService, HassService,
HassEvent, HassEvent,
HassEntity, HassEntity,
HassState HassState,
} from './hass.js'; } from "./hass.js";
// Home Assistant interfaces // Home Assistant interfaces
export interface HassAddon { export interface HassAddon {
@@ -92,7 +92,7 @@ export interface HacsResponse {
export interface AutomationConfig { export interface AutomationConfig {
alias: string; alias: string;
description?: string; description?: string;
mode?: 'single' | 'parallel' | 'queued' | 'restart'; mode?: "single" | "parallel" | "queued" | "restart";
trigger: any[]; trigger: any[];
condition?: any[]; condition?: any[];
action: any[]; action: any[];
@@ -125,7 +125,7 @@ export interface HistoryParams {
// Scene interfaces // Scene interfaces
export interface SceneParams { export interface SceneParams {
action: 'list' | 'activate'; action: "list" | "activate";
scene_id?: string; scene_id?: string;
} }
@@ -139,30 +139,43 @@ export interface NotifyParams {
// Automation parameter interfaces // Automation parameter interfaces
export interface AutomationParams { export interface AutomationParams {
action: 'list' | 'toggle' | 'trigger'; action: "list" | "toggle" | "trigger";
automation_id?: string; automation_id?: string;
} }
export interface AddonParams { export interface AddonParams {
action: 'list' | 'info' | 'install' | 'uninstall' | 'start' | 'stop' | 'restart'; action:
| "list"
| "info"
| "install"
| "uninstall"
| "start"
| "stop"
| "restart";
slug?: string; slug?: string;
version?: string; version?: string;
} }
export interface PackageParams { export interface PackageParams {
action: 'list' | 'install' | 'uninstall' | 'update'; action: "list" | "install" | "uninstall" | "update";
category: 'integration' | 'plugin' | 'theme' | 'python_script' | 'appdaemon' | 'netdaemon'; category:
| "integration"
| "plugin"
| "theme"
| "python_script"
| "appdaemon"
| "netdaemon";
repository?: string; repository?: string;
version?: string; version?: string;
} }
export interface AutomationConfigParams { export interface AutomationConfigParams {
action: 'create' | 'update' | 'delete' | 'duplicate'; action: "create" | "update" | "delete" | "duplicate";
automation_id?: string; automation_id?: string;
config?: { config?: {
alias: string; alias: string;
description?: string; description?: string;
mode?: 'single' | 'parallel' | 'queued' | 'restart'; mode?: "single" | "parallel" | "queued" | "restart";
trigger: any[]; trigger: any[];
condition?: any[]; condition?: any[];
action: any[]; action: any[];

67
src/mcp/litemcp.ts Normal file
View File

@@ -0,0 +1,67 @@
import { EventEmitter } from "events";
export class LiteMCP extends EventEmitter {
private static instance: LiteMCP;
private constructor() {
super();
// Initialize with default configuration
this.configure({});
}
public static getInstance(): LiteMCP {
if (!LiteMCP.instance) {
LiteMCP.instance = new LiteMCP();
}
return LiteMCP.instance;
}
public configure(config: Record<string, any>): void {
// Store configuration
this.config = {
...this.defaultConfig,
...config,
};
}
private config: Record<string, any> = {};
private defaultConfig = {
maxRetries: 3,
retryDelay: 1000,
timeout: 5000,
};
public async execute(
command: string,
params: Record<string, any> = {},
): Promise<any> {
try {
// Emit command execution event
this.emit("command", { command, params });
// Execute command logic here
const result = await this.processCommand(command, params);
// Emit success event
this.emit("success", { command, params, result });
return result;
} catch (error) {
// Emit error event
this.emit("error", { command, params, error });
throw error;
}
}
private async processCommand(
command: string,
params: Record<string, any>,
): Promise<any> {
// Command processing logic
return { command, params, status: "processed" };
}
public async shutdown(): Promise<void> {
// Cleanup logic
this.removeAllListeners();
}
}

View File

@@ -1,5 +1,5 @@
import { z } from 'zod'; import { z } from "zod";
import { DomainSchema } from '../schemas.js'; import { DomainSchema } from "../schemas.js";
export const MCP_SCHEMA = { export const MCP_SCHEMA = {
tools: [ tools: [
@@ -24,14 +24,14 @@ export const MCP_SCHEMA = {
"vacuum", "vacuum",
"scene", "scene",
"script", "script",
"camera" "camera",
] ],
}, },
area: { type: "string" }, area: { type: "string" },
floor: { type: "string" } floor: { type: "string" },
},
required: [],
}, },
required: []
}
}, },
{ {
name: "control", name: "control",
@@ -53,8 +53,8 @@ export const MCP_SCHEMA = {
"set_temperature", "set_temperature",
"set_hvac_mode", "set_hvac_mode",
"set_fan_mode", "set_fan_mode",
"set_humidity" "set_humidity",
] ],
}, },
entity_id: { type: "string" }, entity_id: { type: "string" },
state: { type: "string" }, state: { type: "string" },
@@ -64,7 +64,7 @@ export const MCP_SCHEMA = {
type: "array", type: "array",
items: { type: "number" }, items: { type: "number" },
minItems: 3, minItems: 3,
maxItems: 3 maxItems: 3,
}, },
position: { type: "number" }, position: { type: "number" },
tilt_position: { type: "number" }, tilt_position: { type: "number" },
@@ -73,10 +73,10 @@ export const MCP_SCHEMA = {
target_temp_low: { type: "number" }, target_temp_low: { type: "number" },
hvac_mode: { type: "string" }, hvac_mode: { type: "string" },
fan_mode: { type: "string" }, fan_mode: { type: "string" },
humidity: { type: "number" } humidity: { type: "number" },
},
required: ["command", "entity_id"],
}, },
required: ["command", "entity_id"]
}
}, },
{ {
name: "subscribe_events", name: "subscribe_events",
@@ -86,13 +86,13 @@ export const MCP_SCHEMA = {
properties: { properties: {
events: { events: {
type: "array", type: "array",
items: { type: "string" } items: { type: "string" },
}, },
entity_id: { type: "string" }, entity_id: { type: "string" },
domain: { type: "string" } domain: { type: "string" },
},
required: [],
}, },
required: []
}
}, },
{ {
name: "get_sse_stats", name: "get_sse_stats",
@@ -100,8 +100,8 @@ export const MCP_SCHEMA = {
parameters: { parameters: {
type: "object", type: "object",
properties: {}, properties: {},
required: [] required: [],
} },
}, },
{ {
name: "automation_config", name: "automation_config",
@@ -111,7 +111,7 @@ export const MCP_SCHEMA = {
properties: { properties: {
action: { action: {
type: "string", type: "string",
enum: ["list", "toggle", "trigger", "create", "update", "delete"] enum: ["list", "toggle", "trigger", "create", "update", "delete"],
}, },
automation_id: { type: "string" }, automation_id: { type: "string" },
config: { config: {
@@ -121,17 +121,17 @@ export const MCP_SCHEMA = {
description: { type: "string" }, description: { type: "string" },
mode: { mode: {
type: "string", type: "string",
enum: ["single", "parallel", "queued", "restart"] enum: ["single", "parallel", "queued", "restart"],
}, },
trigger: { type: "array" }, trigger: { type: "array" },
condition: { type: "array" }, condition: { type: "array" },
action: { type: "array" } action: { type: "array" },
}, },
required: ["alias", "trigger", "action"] required: ["alias", "trigger", "action"],
} },
},
required: ["action"],
}, },
required: ["action"]
}
}, },
{ {
name: "addon_management", name: "addon_management",
@@ -141,13 +141,21 @@ export const MCP_SCHEMA = {
properties: { properties: {
action: { action: {
type: "string", type: "string",
enum: ["list", "info", "install", "uninstall", "start", "stop", "restart"] enum: [
"list",
"info",
"install",
"uninstall",
"start",
"stop",
"restart",
],
}, },
slug: { type: "string" }, slug: { type: "string" },
version: { type: "string" } version: { type: "string" },
},
required: ["action"],
}, },
required: ["action"]
}
}, },
{ {
name: "package_management", name: "package_management",
@@ -157,17 +165,24 @@ export const MCP_SCHEMA = {
properties: { properties: {
action: { action: {
type: "string", type: "string",
enum: ["list", "install", "uninstall", "update"] enum: ["list", "install", "uninstall", "update"],
}, },
category: { category: {
type: "string", type: "string",
enum: ["integration", "plugin", "theme", "python_script", "appdaemon", "netdaemon"] enum: [
"integration",
"plugin",
"theme",
"python_script",
"appdaemon",
"netdaemon",
],
}, },
repository: { type: "string" }, repository: { type: "string" },
version: { type: "string" } version: { type: "string" },
},
required: ["action", "category"],
}, },
required: ["action", "category"]
}
}, },
{ {
name: "scene_control", name: "scene_control",
@@ -177,12 +192,12 @@ export const MCP_SCHEMA = {
properties: { properties: {
action: { action: {
type: "string", type: "string",
enum: ["list", "activate"] enum: ["list", "activate"],
}, },
scene_id: { type: "string" } scene_id: { type: "string" },
},
required: ["action"],
}, },
required: ["action"]
}
}, },
{ {
name: "notify", name: "notify",
@@ -195,11 +210,11 @@ export const MCP_SCHEMA = {
target: { type: "string" }, target: { type: "string" },
data: { data: {
type: "object", type: "object",
additionalProperties: true additionalProperties: true,
} },
},
required: ["message"],
}, },
required: ["message"]
}
}, },
{ {
name: "history", name: "history",
@@ -211,147 +226,173 @@ export const MCP_SCHEMA = {
start_time: { type: "string" }, start_time: { type: "string" },
end_time: { type: "string" }, end_time: { type: "string" },
minimal_response: { type: "boolean" }, minimal_response: { type: "boolean" },
significant_changes_only: { type: "boolean" } significant_changes_only: { type: "boolean" },
},
required: ["entity_id"],
},
}, },
required: ["entity_id"]
}
}
], ],
prompts: [ prompts: [
{ {
name: "claude", name: "claude",
description: "Claude-specific prompt template for home automation control", description:
"Claude-specific prompt template for home automation control",
template: { template: {
system: "You are Claude, an AI assistant specialized in home automation control through natural language. Your role is to interpret user commands and translate them into specific device control actions. Always maintain context awareness and consider user preferences and patterns. Provide clear, concise responses and suggest relevant follow-up actions.", system:
"You are Claude, an AI assistant specialized in home automation control through natural language. Your role is to interpret user commands and translate them into specific device control actions. Always maintain context awareness and consider user preferences and patterns. Provide clear, concise responses and suggest relevant follow-up actions.",
user: "Control the {device_name} in the {location} by {action} with parameters: {parameters}. Current context: {context}", user: "Control the {device_name} in the {location} by {action} with parameters: {parameters}. Current context: {context}",
examples: [ examples: [
{ {
user: "Turn on the living room lights", user: "Turn on the living room lights",
assistant: "I'll turn on the lights in the living room. Would you like me to set a specific brightness level?" assistant:
"I'll turn on the lights in the living room. Would you like me to set a specific brightness level?",
}, },
{ {
user: "Set the temperature to 72 degrees", user: "Set the temperature to 72 degrees",
assistant: "I'll set the temperature to 72°F. I'll monitor the temperature and let you know when it reaches the target." assistant:
"I'll set the temperature to 72°F. I'll monitor the temperature and let you know when it reaches the target.",
}, },
{ {
user: "Open the kitchen blinds halfway", user: "Open the kitchen blinds halfway",
assistant: "Setting the kitchen blinds to 50% open position. Would you like me to adjust the slat angle as well?" assistant:
"Setting the kitchen blinds to 50% open position. Would you like me to adjust the slat angle as well?",
}, },
{ {
user: "Start the coffee maker", user: "Start the coffee maker",
assistant: "I'll start the coffee maker in the kitchen. Based on your usual preferences, I'll use the strong brew setting. Would you like me to adjust any settings?" assistant:
"I'll start the coffee maker in the kitchen. Based on your usual preferences, I'll use the strong brew setting. Would you like me to adjust any settings?",
}, },
{ {
user: "Set up the house for bedtime", user: "Set up the house for bedtime",
assistant: "I'll activate the bedtime routine: dimming hallway lights to 20%, turning off all other lights, setting temperature to your preferred sleeping temperature of 68°F, locking all doors, and arming the security system." assistant:
"I'll activate the bedtime routine: dimming hallway lights to 20%, turning off all other lights, setting temperature to your preferred sleeping temperature of 68°F, locking all doors, and arming the security system.",
}, },
{ {
user: "Play some music in the bedroom", user: "Play some music in the bedroom",
assistant: "I'll start playing music on the bedroom speaker. Based on this time of day and your preferences, I'll start your 'Evening Relaxation' playlist at 40% volume. Would you like to adjust any of these settings?" assistant:
"I'll start playing music on the bedroom speaker. Based on this time of day and your preferences, I'll start your 'Evening Relaxation' playlist at 40% volume. Would you like to adjust any of these settings?",
}, },
{ {
user: "Check if I left any windows open", user: "Check if I left any windows open",
assistant: "I'll check all window sensors. The kitchen window and guest bedroom window are currently open. Would you like me to notify you if any windows remain open after sunset?" assistant:
} "I'll check all window sensors. The kitchen window and guest bedroom window are currently open. Would you like me to notify you if any windows remain open after sunset?",
] },
} ],
},
}, },
{ {
name: "gpt4", name: "gpt4",
description: "GPT-4 specific prompt template for precise home automation control", description:
"GPT-4 specific prompt template for precise home automation control",
template: { template: {
system: "You are a home automation assistant powered by GPT-4. Focus on precise command interpretation and execution. Maintain high accuracy in device control and parameter settings. Provide feedback on action success and system state changes.", system:
"You are a home automation assistant powered by GPT-4. Focus on precise command interpretation and execution. Maintain high accuracy in device control and parameter settings. Provide feedback on action success and system state changes.",
user: "Command: {action} {device_name} in {location}\nParameters: {parameters}\nContext: {context}", user: "Command: {action} {device_name} in {location}\nParameters: {parameters}\nContext: {context}",
examples: [ examples: [
{ {
user: "Dim the bedroom lights to 50%", user: "Dim the bedroom lights to 50%",
assistant: "Setting bedroom light brightness to 50%. The change has been applied successfully." assistant:
"Setting bedroom light brightness to 50%. The change has been applied successfully.",
}, },
{ {
user: "Start the evening routine", user: "Start the evening routine",
assistant: "Initiating evening routine: dimming lights, adjusting temperature, and enabling security system." assistant:
"Initiating evening routine: dimming lights, adjusting temperature, and enabling security system.",
}, },
{ {
user: "Set living room AC to 74 degrees and high fan", user: "Set living room AC to 74 degrees and high fan",
assistant: "Configuring living room climate control: temperature set to 74°F, fan mode set to high. Current temperature is 78°F, estimated time to target: 15 minutes." assistant:
"Configuring living room climate control: temperature set to 74°F, fan mode set to high. Current temperature is 78°F, estimated time to target: 15 minutes.",
}, },
{ {
user: "Lock all doors and windows", user: "Lock all doors and windows",
assistant: "Security check initiated. All 4 doors locked successfully. 6/7 windows locked. Alert: Kitchen window cannot be locked remotely - manual intervention required." assistant:
"Security check initiated. All 4 doors locked successfully. 6/7 windows locked. Alert: Kitchen window cannot be locked remotely - manual intervention required.",
}, },
{ {
user: "Turn on outdoor lights at sunset", user: "Turn on outdoor lights at sunset",
assistant: "Scheduled task created. Outdoor lights (porch, pathway, garage) will activate at sunset (7:45 PM today). Current light sensor reading: 24,000 lux." assistant:
"Scheduled task created. Outdoor lights (porch, pathway, garage) will activate at sunset (7:45 PM today). Current light sensor reading: 24,000 lux.",
}, },
{ {
user: "Set up the media room for movie night", user: "Set up the media room for movie night",
assistant: "Executing media room configuration: TV power on, input set to HDMI 2, surround sound activated at 40%, lights dimmed to 10%, blackout blinds closed. Room temperature adjusted to 72°F." assistant:
"Executing media room configuration: TV power on, input set to HDMI 2, surround sound activated at 40%, lights dimmed to 10%, blackout blinds closed. Room temperature adjusted to 72°F.",
}, },
{ {
user: "Show me energy usage for today", user: "Show me energy usage for today",
assistant: "Retrieving energy data: Total usage today: 24.5 kWh. Peak consumption: 3.2 kWh (2-3 PM). Major consumers: HVAC (45%), Water Heater (25%), Kitchen Appliances (15%)." assistant:
} "Retrieving energy data: Total usage today: 24.5 kWh. Peak consumption: 3.2 kWh (2-3 PM). Major consumers: HVAC (45%), Water Heater (25%), Kitchen Appliances (15%).",
] },
} ],
},
}, },
{ {
name: "custom", name: "custom",
description: "Customizable prompt template for home automation control", description: "Customizable prompt template for home automation control",
template: { template: {
system: "Custom home automation assistant configuration. Adapt to user preferences and patterns. Learn from interactions and optimize responses. Provide detailed feedback and suggestions.", system:
"Custom home automation assistant configuration. Adapt to user preferences and patterns. Learn from interactions and optimize responses. Provide detailed feedback and suggestions.",
user: "Action requested: {action}\nTarget device: {device_name}\nLocation: {location}\nParameters: {parameters}\nCurrent context: {context}", user: "Action requested: {action}\nTarget device: {device_name}\nLocation: {location}\nParameters: {parameters}\nCurrent context: {context}",
examples: [ examples: [
{ {
user: "Make it cooler in here", user: "Make it cooler in here",
assistant: "Based on your preferences, I'll lower the temperature by 2 degrees. Current temperature is 74°F, adjusting to 72°F." assistant:
"Based on your preferences, I'll lower the temperature by 2 degrees. Current temperature is 74°F, adjusting to 72°F.",
}, },
{ {
user: "Set up movie mode", user: "Set up movie mode",
assistant: "Activating movie mode: dimming lights to 20%, closing blinds, setting TV input to HDMI 1, and adjusting sound system." assistant:
"Activating movie mode: dimming lights to 20%, closing blinds, setting TV input to HDMI 1, and adjusting sound system.",
}, },
{ {
user: "I'm heading to bed", user: "I'm heading to bed",
assistant: "Initiating personalized bedtime sequence: Setting master bedroom temperature to 68°F, activating white noise machine at volume level 3, dimming hallway lights to 15%, turning off all other lights, closing all blinds, and enabling sleep mode on your phone charger." assistant:
"Initiating personalized bedtime sequence: Setting master bedroom temperature to 68°F, activating white noise machine at volume level 3, dimming hallway lights to 15%, turning off all other lights, closing all blinds, and enabling sleep mode on your phone charger.",
}, },
{ {
user: "Start my morning routine", user: "Start my morning routine",
assistant: "Good morning! Executing your weekday morning routine: Gradually increasing bedroom lights (now at 30%), starting coffee maker with your weekday strength setting, setting bathroom heater to 72°F, and pulling up today's weather and calendar on your kitchen display." assistant:
"Good morning! Executing your weekday morning routine: Gradually increasing bedroom lights (now at 30%), starting coffee maker with your weekday strength setting, setting bathroom heater to 72°F, and pulling up today's weather and calendar on your kitchen display.",
}, },
{ {
user: "I'm working from home today", user: "I'm working from home today",
assistant: "Activating work-from-home environment: Setting home office lights to your preferred 5000K color temperature at 80% brightness, adjusting HVAC for daytime occupancy, enabling do-not-disturb on doorbell, and configuring automated breaks reminder every 2 hours." assistant:
"Activating work-from-home environment: Setting home office lights to your preferred 5000K color temperature at 80% brightness, adjusting HVAC for daytime occupancy, enabling do-not-disturb on doorbell, and configuring automated breaks reminder every 2 hours.",
}, },
{ {
user: "We're having guests over", user: "We're having guests over",
assistant: "Preparing home for guests: Adjusting living room and dining room temperature to 72°F, setting accent lighting to warm white (2700K), activating air purifier in high mode, enabling guest WiFi network, and preheating the oven as per your usual entertaining preferences." assistant:
"Preparing home for guests: Adjusting living room and dining room temperature to 72°F, setting accent lighting to warm white (2700K), activating air purifier in high mode, enabling guest WiFi network, and preheating the oven as per your usual entertaining preferences.",
}, },
{ {
user: "Monitor the baby's room", user: "Monitor the baby's room",
assistant: "Activating enhanced nursery monitoring: Setting up temperature alerts (68-72°F range), enabling sound monitoring with smart notification threshold, maintaining humidity at 50%, and activating the smart night light with color changing based on sleep schedule. Current readings: 70°F, 48% humidity, sound level: minimal." assistant:
} "Activating enhanced nursery monitoring: Setting up temperature alerts (68-72°F range), enabling sound monitoring with smart notification threshold, maintaining humidity at 50%, and activating the smart night light with color changing based on sleep schedule. Current readings: 70°F, 48% humidity, sound level: minimal.",
] },
],
}, },
variables: { variables: {
device_name: "string", device_name: "string",
location: "string", location: "string",
action: "string", action: "string",
parameters: "object", parameters: "object",
context: "object" context: "object",
} },
} },
], ],
resources: [ resources: [
{ {
name: "Home Assistant API", name: "Home Assistant API",
url: "https://developers.home-assistant.io/docs/api/rest/" url: "https://developers.home-assistant.io/docs/api/rest/",
}, },
{ {
name: "Home Assistant WebSocket API", name: "Home Assistant WebSocket API",
url: "https://developers.home-assistant.io/docs/api/websocket" url: "https://developers.home-assistant.io/docs/api/websocket",
}, },
{ {
name: "HACS Documentation", name: "HACS Documentation",
url: "https://hacs.xyz" url: "https://hacs.xyz",
} },
] ],
}; };

View File

@@ -0,0 +1,203 @@
import { Request, Response } from "express";
import { validateRequest, sanitizeInput, errorHandler } from "../index";
import { TokenManager } from "../../security/index";
import { jest } from "@jest/globals";
const TEST_SECRET = "test-secret-that-is-long-enough-for-testing-purposes";
describe("Security Middleware", () => {
let mockRequest: Partial<Request>;
let mockResponse: Partial<Response>;
let nextFunction: jest.Mock;
beforeEach(() => {
process.env.JWT_SECRET = TEST_SECRET;
mockRequest = {
method: "POST",
headers: {},
body: {},
ip: "127.0.0.1",
};
const mockJson = jest.fn().mockReturnThis();
const mockStatus = jest.fn().mockReturnThis();
const mockSetHeader = jest.fn().mockReturnThis();
const mockRemoveHeader = jest.fn().mockReturnThis();
mockResponse = {
status: mockStatus as any,
json: mockJson as any,
setHeader: mockSetHeader as any,
removeHeader: mockRemoveHeader as any,
};
nextFunction = jest.fn();
});
afterEach(() => {
delete process.env.JWT_SECRET;
jest.clearAllMocks();
});
describe("Request Validation", () => {
it("should pass valid requests", () => {
mockRequest.headers = {
authorization: "Bearer valid-token",
"content-type": "application/json",
};
jest
.spyOn(TokenManager, "validateToken")
.mockReturnValue({ valid: true });
validateRequest(
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(nextFunction).toHaveBeenCalled();
});
it("should reject requests without authorization header", () => {
validateRequest(
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(mockResponse.status).toHaveBeenCalledWith(401);
expect(mockResponse.json).toHaveBeenCalledWith({
success: false,
message: "Unauthorized",
error: "Missing or invalid authorization header",
timestamp: expect.any(String),
});
});
it("should reject requests with invalid authorization format", () => {
mockRequest.headers = {
authorization: "invalid-format",
"content-type": "application/json",
};
validateRequest(
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(mockResponse.status).toHaveBeenCalledWith(401);
expect(mockResponse.json).toHaveBeenCalledWith({
success: false,
message: "Unauthorized",
error: "Missing or invalid authorization header",
timestamp: expect.any(String),
});
});
it("should reject oversized requests", () => {
mockRequest.headers = {
authorization: "Bearer valid-token",
"content-type": "application/json",
"content-length": "1048577", // 1MB + 1 byte
};
validateRequest(
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(mockResponse.status).toHaveBeenCalledWith(413);
expect(mockResponse.json).toHaveBeenCalledWith({
success: false,
message: "Payload Too Large",
error: "Request body must not exceed 1048576 bytes",
timestamp: expect.any(String),
});
});
});
describe("Input Sanitization", () => {
it("should sanitize HTML in request body", () => {
mockRequest.body = {
text: 'Test <script>alert("xss")</script>',
nested: {
html: '<img src="x" onerror="alert(1)">World',
},
};
sanitizeInput(
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(mockRequest.body.text).toBe("Test ");
expect(mockRequest.body.nested.html).toBe("World");
expect(nextFunction).toHaveBeenCalled();
});
it("should handle non-object bodies", () => {
mockRequest.body = "<p>text</p>";
sanitizeInput(
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(mockRequest.body).toBe("text");
expect(nextFunction).toHaveBeenCalled();
});
it("should preserve non-string values", () => {
mockRequest.body = {
number: 123,
boolean: true,
array: [1, 2, 3],
nested: { value: 456 },
};
sanitizeInput(
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(mockRequest.body).toEqual({
number: 123,
boolean: true,
array: [1, 2, 3],
nested: { value: 456 },
});
expect(nextFunction).toHaveBeenCalled();
});
});
describe("Error Handler", () => {
it("should handle errors in production mode", () => {
process.env.NODE_ENV = "production";
const error = new Error("Test error");
errorHandler(
error,
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.json).toHaveBeenCalledWith({
success: false,
message: "Internal Server Error",
error: "An unexpected error occurred",
timestamp: expect.any(String),
});
});
it("should include error details in development mode", () => {
process.env.NODE_ENV = "development";
const error = new Error("Test error");
errorHandler(
error,
mockRequest as Request,
mockResponse as Response,
nextFunction,
);
expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.json).toHaveBeenCalledWith({
success: false,
message: "Internal Server Error",
error: "Test error",
stack: expect.any(String),
timestamp: expect.any(String),
});
});
});
});

View File

@@ -1,130 +1,285 @@
import { Request, Response, NextFunction } from 'express'; import { Request, Response, NextFunction } from "express";
import { HASS_CONFIG, RATE_LIMIT_CONFIG } from '../config/index.js'; import { HASS_CONFIG, RATE_LIMIT_CONFIG } from "../config/index.js";
import rateLimit from 'express-rate-limit'; import rateLimit from "express-rate-limit";
import { TokenManager } from "../security/index.js";
import sanitizeHtml from "sanitize-html";
import helmet from "helmet";
import { SECURITY_CONFIG } from "../config/security.config.js";
// Rate limiter middleware // Rate limiter middleware with enhanced configuration
export const rateLimiter = rateLimit({ export const rateLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute windowMs: SECURITY_CONFIG.RATE_LIMIT_WINDOW,
max: RATE_LIMIT_CONFIG.REGULAR, max: SECURITY_CONFIG.RATE_LIMIT_MAX_REQUESTS,
message: { message: {
success: false, success: false,
message: 'Too many requests, please try again later.', message: "Too Many Requests",
reset_time: new Date(Date.now() + 60 * 1000).toISOString() error: "Rate limit exceeded. Please try again later.",
} timestamp: new Date().toISOString(),
},
}); });
// WebSocket rate limiter middleware // WebSocket rate limiter middleware with enhanced configuration
export const wsRateLimiter = rateLimit({ export const wsRateLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute windowMs: 60 * 1000, // 1 minute
max: RATE_LIMIT_CONFIG.WEBSOCKET, max: RATE_LIMIT_CONFIG.WEBSOCKET,
standardHeaders: true,
legacyHeaders: false,
message: { message: {
success: false, success: false,
message: 'Too many WebSocket connections, please try again later.', message: "Too many WebSocket connections, please try again later.",
reset_time: new Date(Date.now() + 60 * 1000).toISOString() reset_time: new Date(Date.now() + 60 * 1000).toISOString(),
} },
skipSuccessfulRequests: false,
keyGenerator: (req) => req.ip || req.socket.remoteAddress || "unknown",
}); });
// Security headers middleware // Authentication middleware with enhanced security
export const securityHeaders = (_req: Request, res: Response, next: NextFunction) => { export const authenticate = (
res.setHeader('X-Content-Type-Options', 'nosniff'); req: Request,
res.setHeader('X-Frame-Options', 'DENY'); res: Response,
res.setHeader('X-XSS-Protection', '1; mode=block'); next: NextFunction,
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains'); ) => {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return res.status(401).json({
success: false,
message: "Unauthorized",
error: "Missing or invalid authorization header",
timestamp: new Date().toISOString(),
});
}
const token = authHeader.replace("Bearer ", "");
const clientIp = req.ip || req.socket.remoteAddress || "";
const validationResult = TokenManager.validateToken(token, clientIp);
if (!validationResult.valid) {
return res.status(401).json({
success: false,
message: "Unauthorized",
error: validationResult.error || "Invalid token",
timestamp: new Date().toISOString(),
});
}
next(); next();
}; };
// Request validation middleware // Enhanced security headers middleware using helmet
export const validateRequest = (req: Request, res: Response, next: NextFunction) => { const helmetMiddleware = helmet({
// Validate content type for POST/PUT/PATCH requests contentSecurityPolicy: {
if (['POST', 'PUT', 'PATCH'].includes(req.method) && !req.is('application/json')) { directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:", "https:"],
connectSrc: ["'self'", "wss:", "https:"],
frameSrc: ["'none'"],
objectSrc: ["'none'"],
baseUri: ["'self'"],
formAction: ["'self'"],
frameAncestors: ["'none'"],
},
},
crossOriginEmbedderPolicy: true,
crossOriginOpenerPolicy: { policy: "same-origin" },
crossOriginResourcePolicy: { policy: "same-origin" },
dnsPrefetchControl: { allow: false },
frameguard: { action: "deny" },
hidePoweredBy: true,
hsts: {
maxAge: 31536000,
includeSubDomains: true,
preload: true,
},
ieNoOpen: true,
noSniff: true,
originAgentCluster: true,
permittedCrossDomainPolicies: { permittedPolicies: "none" },
referrerPolicy: { policy: "strict-origin-when-cross-origin" },
xssFilter: true,
});
// Wrapper for helmet middleware to handle mock responses in tests
export const securityHeaders = (
req: Request,
res: Response,
next: NextFunction,
): void => {
// Basic security headers
res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("X-XSS-Protection", "1; mode=block");
res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
res.setHeader("X-Permitted-Cross-Domain-Policies", "none");
res.setHeader("X-Download-Options", "noopen");
// Content Security Policy
res.setHeader(
"Content-Security-Policy",
[
"default-src 'self'",
"script-src 'self'",
"style-src 'self'",
"img-src 'self'",
"font-src 'self'",
"connect-src 'self'",
"media-src 'self'",
"object-src 'none'",
"frame-ancestors 'none'",
"base-uri 'self'",
"form-action 'self'",
].join("; "),
);
// HSTS (only in production)
if (process.env.NODE_ENV === "production") {
res.setHeader(
"Strict-Transport-Security",
"max-age=31536000; includeSubDomains; preload",
);
}
next();
};
/**
* Validates incoming requests for proper authentication and content type
*/
export const validateRequest = (
req: Request,
res: Response,
next: NextFunction,
): Response | void => {
// Skip validation for health and MCP schema endpoints
if (req.path === "/health" || req.path === "/mcp") {
return next();
}
// Validate content type for non-GET requests
if (["POST", "PUT", "PATCH"].includes(req.method)) {
const contentType = req.headers["content-type"] || "";
if (!contentType.toLowerCase().includes("application/json")) {
return res.status(415).json({ return res.status(415).json({
success: false, success: false,
message: 'Content-Type must be application/json' message: "Unsupported Media Type",
error: "Content-Type must be application/json",
timestamp: new Date().toISOString(),
}); });
} }
// Validate request body size
const contentLength = parseInt(req.headers['content-length'] || '0', 10);
if (contentLength > 1024 * 1024) { // 1MB limit
return res.status(413).json({
success: false,
message: 'Request body too large'
});
} }
next(); // Validate authorization header
}; const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
// Input sanitization middleware
export const sanitizeInput = (req: Request, _res: Response, next: NextFunction) => {
if (req.body) {
// Recursively sanitize object
const sanitizeObject = (obj: any): any => {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(item => sanitizeObject(item));
}
const sanitized: any = {};
for (const [key, value] of Object.entries(obj)) {
// Remove any potentially dangerous characters from keys
const sanitizedKey = key.replace(/[<>]/g, '');
sanitized[sanitizedKey] = sanitizeObject(value);
}
return sanitized;
};
req.body = sanitizeObject(req.body);
}
next();
};
// Authentication middleware
export const authenticate = (req: Request, res: Response, next: NextFunction) => {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token || token !== HASS_CONFIG.TOKEN) {
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: 'Unauthorized - Invalid token' message: "Unauthorized",
error: "Missing or invalid authorization header",
timestamp: new Date().toISOString(),
}); });
} }
next(); // Validate token
}; const token = authHeader.replace("Bearer ", "");
const validationResult = TokenManager.validateToken(token, req.ip);
if (!validationResult.valid) {
return res.status(401).json({
success: false,
message: "Unauthorized",
error: validationResult.error || "Invalid token",
timestamp: new Date().toISOString(),
});
}
// Error handling middleware // Validate request body structure
export const errorHandler = (err: Error, _req: Request, res: Response, _next: NextFunction) => { if (req.method !== "GET" && req.body) {
console.error('Error:', err); if (typeof req.body !== "object" || Array.isArray(req.body)) {
// Handle specific error types
if (err.name === 'ValidationError') {
return res.status(400).json({ return res.status(400).json({
success: false, success: false,
message: 'Validation error', message: "Bad Request",
details: err.message error: "Invalid request body structure",
timestamp: new Date().toISOString(),
}); });
} }
if (err.name === 'UnauthorizedError') {
return res.status(401).json({
success: false,
message: 'Unauthorized',
details: err.message
});
} }
// Default error response next();
res.status(500).json({ };
/**
* Sanitizes input data to prevent XSS attacks
*/
export const sanitizeInput = (
req: Request,
res: Response,
next: NextFunction,
): void => {
if (req.body && typeof req.body === "object" && !Array.isArray(req.body)) {
const sanitizeValue = (value: unknown): unknown => {
if (typeof value === "string") {
let sanitized = value;
// Remove script tags and their content
sanitized = sanitized.replace(
/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
"",
);
// Remove style tags and their content
sanitized = sanitized.replace(
/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi,
"",
);
// Remove remaining HTML tags
sanitized = sanitized.replace(/<[^>]+>/g, "");
// Remove javascript: protocol
sanitized = sanitized.replace(/javascript:/gi, "");
// Remove event handlers
sanitized = sanitized.replace(
/on\w+\s*=\s*(?:".*?"|'.*?'|[^"'>\s]+)/gi,
"",
);
// Trim whitespace
return sanitized.trim();
} else if (typeof value === "object" && value !== null) {
const result: Record<string, unknown> = {};
Object.entries(value as Record<string, unknown>).forEach(
([key, val]) => {
result[key] = sanitizeValue(val);
},
);
return result;
}
return value;
};
req.body = sanitizeValue(req.body) as Record<string, unknown>;
}
next();
};
/**
* Handles errors in a consistent way
*/
export const errorHandler = (
err: Error,
req: Request,
res: Response,
next: NextFunction,
): Response => {
const isDevelopment = process.env.NODE_ENV === "development";
const response: Record<string, unknown> = {
success: false, success: false,
message: 'Internal server error', message: "Internal Server Error",
details: process.env.NODE_ENV === 'development' ? err.message : undefined timestamp: new Date().toISOString(),
}); };
if (isDevelopment) {
response.error = err.message;
response.stack = err.stack;
}
return res.status(500).json(response);
}; };
// Export all middleware // Export all middleware
@@ -135,5 +290,5 @@ export const middleware = {
validateRequest, validateRequest,
sanitizeInput, sanitizeInput,
authenticate, authenticate,
errorHandler errorHandler,
}; };

View File

@@ -7,9 +7,9 @@
* @module logging-middleware * @module logging-middleware
*/ */
import { Request, Response, NextFunction } from 'express'; import { Request, Response, NextFunction } from "express";
import { logger } from '../utils/logger.js'; import { logger } from "../utils/logger.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
/** /**
* Interface for extended request object with timing information * Interface for extended request object with timing information
@@ -37,9 +37,9 @@ const getResponseTime = (startTime: number): number => {
*/ */
const getClientIp = (req: Request): string => { const getClientIp = (req: Request): string => {
return ( return (
(req.headers['x-forwarded-for'] as string)?.split(',')[0] || (req.headers["x-forwarded-for"] as string)?.split(",")[0] ||
req.socket.remoteAddress || req.socket.remoteAddress ||
'unknown' "unknown"
); );
}; };
@@ -59,7 +59,11 @@ const formatRequestLog = (req: TimedRequest): string => {
* @param time - Response time in milliseconds * @param time - Response time in milliseconds
* @returns Formatted log message * @returns Formatted log message
*/ */
const formatResponseLog = (req: TimedRequest, res: Response, time: number): string => { const formatResponseLog = (
req: TimedRequest,
res: Response,
time: number,
): string => {
return `${req.method} ${req.originalUrl} - ${res.statusCode} - ${time.toFixed(2)}ms`; return `${req.method} ${req.originalUrl} - ${res.statusCode} - ${time.toFixed(2)}ms`;
}; };
@@ -67,7 +71,11 @@ const formatResponseLog = (req: TimedRequest, res: Response, time: number): stri
* Request logging middleware * Request logging middleware
* Logs information about incoming requests and their responses * Logs information about incoming requests and their responses
*/ */
export const requestLogger = (req: TimedRequest, res: Response, next: NextFunction): void => { export const requestLogger = (
req: TimedRequest,
res: Response,
next: NextFunction,
): void => {
if (!APP_CONFIG.LOGGING.LOG_REQUESTS) { if (!APP_CONFIG.LOGGING.LOG_REQUESTS) {
next(); next();
return; return;
@@ -80,9 +88,9 @@ export const requestLogger = (req: TimedRequest, res: Response, next: NextFuncti
logger.http(formatRequestLog(req)); logger.http(formatRequestLog(req));
// Log response // Log response
res.on('finish', () => { res.on("finish", () => {
const responseTime = Date.now() - (req.startTime || 0); const responseTime = Date.now() - (req.startTime || 0);
const logLevel = res.statusCode >= 400 ? 'warn' : 'http'; const logLevel = res.statusCode >= 400 ? "warn" : "http";
logger[logLevel](formatResponseLog(req, res, responseTime)); logger[logLevel](formatResponseLog(req, res, responseTime));
}); });
@@ -93,14 +101,22 @@ export const requestLogger = (req: TimedRequest, res: Response, next: NextFuncti
* Error logging middleware * Error logging middleware
* Logs errors that occur during request processing * Logs errors that occur during request processing
*/ */
export const errorLogger = (err: Error, req: Request, res: Response, next: NextFunction): void => { export const errorLogger = (
logger.error(`Error processing ${req.method} ${req.originalUrl}: ${err.message}`, { err: Error,
req: Request,
res: Response,
next: NextFunction,
): void => {
logger.error(
`Error processing ${req.method} ${req.originalUrl}: ${err.message}`,
{
error: err.stack, error: err.stack,
method: req.method, method: req.method,
url: req.originalUrl, url: req.originalUrl,
body: req.body, body: req.body,
query: req.query, query: req.query,
ip: getClientIp(req) ip: getClientIp(req),
}); },
);
next(err); next(err);
}; };

View File

@@ -1,6 +1,6 @@
import { exec } from 'child_process'; import { exec } from "child_process";
import { promisify } from 'util'; import { promisify } from "util";
import { EventEmitter } from 'events'; import { EventEmitter } from "events";
const execAsync = promisify(exec); const execAsync = promisify(exec);
@@ -25,7 +25,7 @@ class MacOSIntegration extends EventEmitter {
this.permissions = { this.permissions = {
notifications: false, notifications: false,
automation: false, automation: false,
accessibility: false accessibility: false,
}; };
} }
@@ -38,51 +38,55 @@ class MacOSIntegration extends EventEmitter {
try { try {
// Check notification permissions // Check notification permissions
const { stdout: notifPerms } = await execAsync( const { stdout: notifPerms } = await execAsync(
'osascript -e \'tell application "System Events" to get properties\'' "osascript -e 'tell application \"System Events\" to get properties'",
);
this.permissions.notifications = notifPerms.includes(
"notifications enabled:true",
); );
this.permissions.notifications = notifPerms.includes('notifications enabled:true');
// Check automation permissions // Check automation permissions
const { stdout: autoPerms } = await execAsync( const { stdout: autoPerms } = await execAsync(
'osascript -e \'tell application "System Events" to get UI elements enabled\'' "osascript -e 'tell application \"System Events\" to get UI elements enabled'",
); );
this.permissions.automation = autoPerms.includes('true'); this.permissions.automation = autoPerms.includes("true");
// Check accessibility permissions // Check accessibility permissions
const { stdout: accessPerms } = await execAsync( const { stdout: accessPerms } = await execAsync(
'osascript -e \'tell application "System Events" to get processes\'' "osascript -e 'tell application \"System Events\" to get processes'",
); );
this.permissions.accessibility = !accessPerms.includes('error'); this.permissions.accessibility = !accessPerms.includes("error");
return this.permissions; return this.permissions;
} catch (error) { } catch (error) {
console.error('Error checking permissions:', error); console.error("Error checking permissions:", error);
return this.permissions; return this.permissions;
} }
} }
async sendNotification(notification: MacOSNotification): Promise<void> { async sendNotification(notification: MacOSNotification): Promise<void> {
if (!this.permissions.notifications) { if (!this.permissions.notifications) {
throw new Error('Notification permission not granted'); throw new Error("Notification permission not granted");
} }
const script = ` const script = `
display notification "${notification.message}"${notification.subtitle ? ` with subtitle "${notification.subtitle}"` : '' display notification "${notification.message}"${
} with title "${notification.title}"${notification.sound ? ' sound name "default"' : '' notification.subtitle ? ` with subtitle "${notification.subtitle}"` : ""
} with title "${notification.title}"${
notification.sound ? ' sound name "default"' : ""
} }
`; `;
try { try {
await execAsync(`osascript -e '${script}'`); await execAsync(`osascript -e '${script}'`);
} catch (error) { } catch (error) {
console.error('Error sending notification:', error); console.error("Error sending notification:", error);
throw error; throw error;
} }
} }
async registerSystemEvents(): Promise<void> { async registerSystemEvents(): Promise<void> {
if (!this.permissions.automation) { if (!this.permissions.automation) {
throw new Error('Automation permission not granted'); throw new Error("Automation permission not granted");
} }
// Monitor system events // Monitor system events
@@ -112,23 +116,23 @@ class MacOSIntegration extends EventEmitter {
try { try {
const { stdout } = await execAsync(`osascript -e '${script}'`); const { stdout } = await execAsync(`osascript -e '${script}'`);
const events = stdout.split(',').map(e => e.trim()); const events = stdout.split(",").map((e) => e.trim());
events.forEach(event => this.emit('system_event', event)); events.forEach((event) => this.emit("system_event", event));
} catch (error) { } catch (error) {
console.error('Error monitoring system events:', error); console.error("Error monitoring system events:", error);
} }
} }
async executeAutomation(script: string): Promise<string> { async executeAutomation(script: string): Promise<string> {
if (!this.permissions.automation) { if (!this.permissions.automation) {
throw new Error('Automation permission not granted'); throw new Error("Automation permission not granted");
} }
try { try {
const { stdout } = await execAsync(`osascript -e '${script}'`); const { stdout } = await execAsync(`osascript -e '${script}'`);
return stdout; return stdout;
} catch (error) { } catch (error) {
console.error('Error executing automation:', error); console.error("Error executing automation:", error);
throw error; throw error;
} }
} }
@@ -138,36 +142,40 @@ class MacOSIntegration extends EventEmitter {
try { try {
// Get macOS version // Get macOS version
const { stdout: version } = await execAsync('sw_vers -productVersion'); const { stdout: version } = await execAsync("sw_vers -productVersion");
info.os_version = version.trim(); info.os_version = version.trim();
// Get hardware info // Get hardware info
const { stdout: hardware } = await execAsync('system_profiler SPHardwareDataType'); const { stdout: hardware } = await execAsync(
"system_profiler SPHardwareDataType",
);
info.hardware = this.parseSystemProfile(hardware); info.hardware = this.parseSystemProfile(hardware);
// Get power info // Get power info
const { stdout: power } = await execAsync('pmset -g batt'); const { stdout: power } = await execAsync("pmset -g batt");
info.power = this.parsePowerInfo(power); info.power = this.parsePowerInfo(power);
// Get network info // Get network info
const { stdout: network } = await execAsync('networksetup -listallhardwareports'); const { stdout: network } = await execAsync(
"networksetup -listallhardwareports",
);
info.network = this.parseNetworkInfo(network); info.network = this.parseNetworkInfo(network);
return info; return info;
} catch (error) { } catch (error) {
console.error('Error getting system info:', error); console.error("Error getting system info:", error);
throw error; throw error;
} }
} }
private parseSystemProfile(output: string): Record<string, any> { private parseSystemProfile(output: string): Record<string, any> {
const info: Record<string, any> = {}; const info: Record<string, any> = {};
const lines = output.split('\n'); const lines = output.split("\n");
for (const line of lines) { for (const line of lines) {
const [key, value] = line.split(':').map(s => s.trim()); const [key, value] = line.split(":").map((s) => s.trim());
if (key && value) { if (key && value) {
info[key.toLowerCase().replace(/\s+/g, '_')] = value; info[key.toLowerCase().replace(/\s+/g, "_")] = value;
} }
} }
@@ -176,12 +184,12 @@ class MacOSIntegration extends EventEmitter {
private parsePowerInfo(output: string): Record<string, any> { private parsePowerInfo(output: string): Record<string, any> {
const info: Record<string, any> = {}; const info: Record<string, any> = {};
const lines = output.split('\n'); const lines = output.split("\n");
for (const line of lines) { for (const line of lines) {
if (line.includes('Now drawing from')) { if (line.includes("Now drawing from")) {
info.power_source = line.includes('Battery') ? 'battery' : 'ac_power'; info.power_source = line.includes("Battery") ? "battery" : "ac_power";
} else if (line.includes('%')) { } else if (line.includes("%")) {
const matches = line.match(/(\d+)%/); const matches = line.match(/(\d+)%/);
if (matches) { if (matches) {
info.battery_percentage = parseInt(matches[1]); info.battery_percentage = parseInt(matches[1]);
@@ -194,17 +202,17 @@ class MacOSIntegration extends EventEmitter {
private parseNetworkInfo(output: string): Record<string, any> { private parseNetworkInfo(output: string): Record<string, any> {
const info: Record<string, any> = {}; const info: Record<string, any> = {};
const lines = output.split('\n'); const lines = output.split("\n");
let currentInterface: string | null = null; let currentInterface: string | null = null;
for (const line of lines) { for (const line of lines) {
if (line.includes('Hardware Port:')) { if (line.includes("Hardware Port:")) {
currentInterface = line.split(':')[1].trim(); currentInterface = line.split(":")[1].trim();
info[currentInterface] = {}; info[currentInterface] = {};
} else if (currentInterface && line.includes('Device:')) { } else if (currentInterface && line.includes("Device:")) {
info[currentInterface].device = line.split(':')[1].trim(); info[currentInterface].device = line.split(":")[1].trim();
} else if (currentInterface && line.includes('Ethernet Address:')) { } else if (currentInterface && line.includes("Ethernet Address:")) {
info[currentInterface].mac = line.split(':')[1].trim(); info[currentInterface].mac = line.split(":")[1].trim();
} }
} }

9
src/polyfills.js Normal file
View File

@@ -0,0 +1,9 @@
// Add necessary polyfills for Node.js compatibility in Bun
import { webcrypto } from 'node:crypto';
// Polyfill for crypto.subtle in Bun
if (!globalThis.crypto?.subtle) {
globalThis.crypto = webcrypto;
}
// Add any other necessary polyfills here

View File

@@ -1,14 +1,14 @@
import { Router } from 'express'; import { Router } from "express";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
const router = Router(); const router = Router();
// Health check endpoint // Health check endpoint
router.get('/', (_req, res) => { router.get("/", (_req, res) => {
res.json({ res.json({
status: 'ok', status: "ok",
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
version: APP_CONFIG.VERSION version: APP_CONFIG.VERSION,
}); });
}); });

View File

@@ -8,11 +8,11 @@
* @module routes * @module routes
*/ */
import { Router } from 'express'; import { Router } from "express";
import { mcpRoutes } from './mcp.routes.js'; import { mcpRoutes } from "./mcp.routes.js";
import { sseRoutes } from './sse.routes.js'; import { sseRoutes } from "./sse.routes.js";
import { toolRoutes } from './tool.routes.js'; import { toolRoutes } from "./tool.routes.js";
import { healthRoutes } from './health.routes.js'; import { healthRoutes } from "./health.routes.js";
/** /**
* Create main router instance * Create main router instance
@@ -27,10 +27,10 @@ const router = Router();
* - /tools: Tool management endpoints * - /tools: Tool management endpoints
* - /health: Health check endpoint * - /health: Health check endpoint
*/ */
router.use('/mcp', mcpRoutes); router.use("/mcp", mcpRoutes);
router.use('/sse', sseRoutes); router.use("/sse", sseRoutes);
router.use('/tools', toolRoutes); router.use("/tools", toolRoutes);
router.use('/health', healthRoutes); router.use("/health", healthRoutes);
/** /**
* Export the configured router * Export the configured router

View File

@@ -7,10 +7,10 @@
* @module mcp-routes * @module mcp-routes
*/ */
import { Router } from 'express'; import { Router } from "express";
import { MCP_SCHEMA } from '../mcp/schema.js'; import { MCP_SCHEMA } from "../mcp/schema.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
import { Tool } from '../types/index.js'; import { Tool } from "../types/index.js";
/** /**
* Create router instance for MCP routes * Create router instance for MCP routes
@@ -28,7 +28,7 @@ const tools: Tool[] = [];
* Returns the MCP schema without requiring authentication * Returns the MCP schema without requiring authentication
* This endpoint allows clients to discover available tools and their parameters * This endpoint allows clients to discover available tools and their parameters
*/ */
router.get('/', (_req, res) => { router.get("/", (_req, res) => {
res.json(MCP_SCHEMA); res.json(MCP_SCHEMA);
}); });
@@ -44,26 +44,26 @@ router.get('/', (_req, res) => {
* @throws {404} If tool is not found * @throws {404} If tool is not found
* @throws {500} If execution fails * @throws {500} If execution fails
*/ */
router.post('/execute', async (req, res) => { router.post("/execute", async (req, res) => {
try { try {
// Get token from Authorization header // Get token from Authorization header
const token = req.headers.authorization?.replace('Bearer ', ''); const token = req.headers.authorization?.replace("Bearer ", "");
if (!token || token !== APP_CONFIG.HASS_TOKEN) { if (!token || token !== APP_CONFIG.HASS_TOKEN) {
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: 'Unauthorized - Invalid token' message: "Unauthorized - Invalid token",
}); });
} }
const { tool: toolName, parameters } = req.body; const { tool: toolName, parameters } = req.body;
// Find the requested tool // Find the requested tool
const tool = tools.find(t => t.name === toolName); const tool = tools.find((t) => t.name === toolName);
if (!tool) { if (!tool) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: `Tool '${toolName}' not found` message: `Tool '${toolName}' not found`,
}); });
} }
@@ -73,7 +73,8 @@ router.post('/execute', async (req, res) => {
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}); });
} }
}); });

View File

@@ -1,99 +1,115 @@
import { Router } from 'express'; import { Router } from "express";
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from "uuid";
import { sseManager } from '../sse/index.js'; import { sseManager } from "../sse/index.js";
import { TokenManager } from '../security/index.js'; import { TokenManager } from "../security/index.js";
import { middleware } from "../middleware/index.js";
const router = Router(); const router = Router();
// SSE endpoints // SSE endpoints
router.get('/subscribe', (req, res) => { router.get("/subscribe_events", middleware.wsRateLimiter, (req, res) => {
try { try {
// Get token from query parameter // Get token from query parameter and validate
const token = req.query.token?.toString(); const token = req.query.token?.toString() || "";
const clientIp = req.ip || req.socket.remoteAddress || "";
const validationResult = TokenManager.validateToken(token, clientIp);
if (!token || !TokenManager.validateToken(token)) { if (!validationResult.valid) {
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: 'Unauthorized - Invalid token' message: "Unauthorized",
error: validationResult.error,
timestamp: new Date().toISOString(),
}); });
} }
// Set SSE headers // Set SSE headers with enhanced security
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'text/event-stream', "Content-Type": "text/event-stream",
'Cache-Control': 'no-cache', "Cache-Control": "no-cache, no-transform",
'Connection': 'keep-alive', Connection: "keep-alive",
'Access-Control-Allow-Origin': '*' "X-Accel-Buffering": "no",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
}); });
// Send initial connection message // Send initial connection message
res.write(`data: ${JSON.stringify({ res.write(
type: 'connection', `data: ${JSON.stringify({
status: 'connected', type: "connection",
timestamp: new Date().toISOString() status: "connected",
})}\n\n`); timestamp: new Date().toISOString(),
})}\n\n`,
);
const clientId = uuidv4(); const clientId = uuidv4();
const client = { const client = {
id: clientId, id: clientId,
ip: clientIp,
connectedAt: new Date(),
send: (data: string) => { send: (data: string) => {
res.write(`data: ${data}\n\n`); res.write(`data: ${data}\n\n`);
} },
}; };
// Add client to SSE manager // Add client to SSE manager with enhanced tracking
const sseClient = sseManager.addClient(client, token); const sseClient = sseManager.addClient(client, token);
if (!sseClient || !sseClient.authenticated) { if (!sseClient || !sseClient.authenticated) {
res.write(`data: ${JSON.stringify({ const errorMessage = JSON.stringify({
type: 'error', type: "error",
message: sseClient ? 'Authentication failed' : 'Maximum client limit reached', message: sseClient
timestamp: new Date().toISOString() ? "Authentication failed"
})}\n\n`); : "Maximum client limit reached",
timestamp: new Date().toISOString(),
});
res.write(`data: ${errorMessage}\n\n`);
return res.end(); return res.end();
} }
// Subscribe to events if specified
const events = req.query.events?.toString().split(',').filter(Boolean);
if (events?.length) {
events.forEach(event => sseManager.subscribeToEvent(clientId, event));
}
// Subscribe to entity if specified
const entityId = req.query.entity_id?.toString();
if (entityId) {
sseManager.subscribeToEntity(clientId, entityId);
}
// Subscribe to domain if specified
const domain = req.query.domain?.toString();
if (domain) {
sseManager.subscribeToDomain(clientId, domain);
}
// Handle client disconnect // Handle client disconnect
req.on('close', () => { req.on("close", () => {
sseManager.removeClient(clientId); sseManager.removeClient(clientId);
console.log(
`Client ${clientId} disconnected at ${new Date().toISOString()}`,
);
}); });
// Handle errors
req.on("error", (error) => {
console.error(`SSE Error for client ${clientId}:`, error);
const errorMessage = JSON.stringify({
type: "error",
message: "Connection error",
timestamp: new Date().toISOString(),
});
res.write(`data: ${errorMessage}\n\n`);
sseManager.removeClient(clientId);
res.end();
});
} catch (error) { } catch (error) {
console.error("SSE Setup Error:", error);
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message: "Internal Server Error",
error:
error instanceof Error ? error.message : "An unexpected error occurred",
timestamp: new Date().toISOString(),
}); });
} }
}); });
// Get SSE stats endpoint // Get SSE stats endpoint
router.get('/stats', async (req, res) => { router.get("/stats", async (req, res) => {
try { try {
const stats = await sseManager.getStatistics(); const stats = await sseManager.getStatistics();
res.json(stats); res.json(stats);
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}); });
} }
}); });
export { router as sseRoutes }; export default router;

View File

@@ -1,6 +1,6 @@
import { Router } from 'express'; import { Router } from "express";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
import { Tool } from '../types/index.js'; import { Tool } from "../types/index.js";
const router = Router(); const router = Router();
@@ -8,23 +8,23 @@ const router = Router();
const tools: Tool[] = []; const tools: Tool[] = [];
// List devices endpoint // List devices endpoint
router.get('/devices', async (req, res) => { router.get("/devices", async (req, res) => {
try { try {
// Get token from Authorization header // Get token from Authorization header
const token = req.headers.authorization?.replace('Bearer ', ''); const token = req.headers.authorization?.replace("Bearer ", "");
if (!token || token !== APP_CONFIG.HASS_TOKEN) { if (!token || token !== APP_CONFIG.HASS_TOKEN) {
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: 'Unauthorized - Invalid token' message: "Unauthorized - Invalid token",
}); });
} }
const tool = tools.find(t => t.name === 'list_devices'); const tool = tools.find((t) => t.name === "list_devices");
if (!tool) { if (!tool) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'Tool not found' message: "Tool not found",
}); });
} }
@@ -33,41 +33,43 @@ router.get('/devices', async (req, res) => {
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}); });
} }
}); });
// Control device endpoint // Control device endpoint
router.post('/control', async (req, res) => { router.post("/control", async (req, res) => {
try { try {
// Get token from Authorization header // Get token from Authorization header
const token = req.headers.authorization?.replace('Bearer ', ''); const token = req.headers.authorization?.replace("Bearer ", "");
if (!token || token !== APP_CONFIG.HASS_TOKEN) { if (!token || token !== APP_CONFIG.HASS_TOKEN) {
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: 'Unauthorized - Invalid token' message: "Unauthorized - Invalid token",
}); });
} }
const tool = tools.find(t => t.name === 'control'); const tool = tools.find((t) => t.name === "control");
if (!tool) { if (!tool) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'Tool not found' message: "Tool not found",
}); });
} }
const result = await tool.execute({ const result = await tool.execute({
...req.body, ...req.body,
token token,
}); });
res.json(result); res.json(result);
} catch (error) { } catch (error) {
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}); });
} }
}); });

View File

@@ -1,6 +1,5 @@
import { z } from "zod"; import { z } from "zod";
export const DomainSchema = z.enum([ export const DomainSchema = z.enum([
"light", "light",
"climate", "climate",
@@ -14,7 +13,7 @@ export const DomainSchema = z.enum([
"vacuum", "vacuum",
"scene", "scene",
"script", "script",
"camera" "camera",
]); ]);
// Generic list request schema // Generic list request schema
@@ -58,12 +57,10 @@ export const AlarmSchema = z.object({
state_attributes: AlarmAttributesSchema, state_attributes: AlarmAttributesSchema,
}); });
export const ListAlarmsResponseSchema = z.object({ export const ListAlarmsResponseSchema = z.object({
alarms: z.array(AlarmSchema), alarms: z.array(AlarmSchema),
}); });
// Devices // Devices
export const DeviceSchema = z.object({ export const DeviceSchema = z.object({
@@ -87,12 +84,12 @@ export const DeviceSchema = z.object({
modified_at: z.number(), modified_at: z.number(),
identifiers: z.array(z.any()), identifiers: z.array(z.any()),
labels: z.array(z.string()), labels: z.array(z.string()),
serial_number: z.string().optional() serial_number: z.string().optional(),
}); });
export const ListDevicesResponseSchema = z.object({ export const ListDevicesResponseSchema = z.object({
_meta: z.object({}).optional(), _meta: z.object({}).optional(),
devices: z.array(DeviceSchema) devices: z.array(DeviceSchema),
}); });
// Media Player // Media Player

View File

@@ -1,5 +1,5 @@
import { JSONSchemaType } from 'ajv'; import { JSONSchemaType } from "ajv";
import { Entity, StateChangedEvent } from '../types/hass.js'; import { Entity, StateChangedEvent } from "../types/hass.js";
// Define base types for automation components // Define base types for automation components
type TriggerType = { type TriggerType = {
@@ -31,14 +31,22 @@ type ActionType = {
type AutomationType = { type AutomationType = {
alias: string; alias: string;
description?: string | null; description?: string | null;
mode?: ('single' | 'parallel' | 'queued' | 'restart') | null; mode?: ("single" | "parallel" | "queued" | "restart") | null;
trigger: TriggerType[]; trigger: TriggerType[];
condition?: ConditionType[] | null; condition?: ConditionType[] | null;
action: ActionType[]; action: ActionType[];
}; };
type DeviceControlType = { type DeviceControlType = {
domain: 'light' | 'switch' | 'climate' | 'cover' | 'fan' | 'scene' | 'script' | 'media_player'; domain:
| "light"
| "switch"
| "climate"
| "cover"
| "fan"
| "scene"
| "script"
| "media_player";
command: string; command: string;
entity_id: string | string[]; entity_id: string | string[];
parameters?: Record<string, any> | null; parameters?: Record<string, any> | null;
@@ -75,194 +83,210 @@ export interface Config {
// Define base schemas // Define base schemas
const contextSchema = { const contextSchema = {
type: 'object', type: "object",
properties: { properties: {
id: { type: 'string' }, id: { type: "string" },
parent_id: { type: 'string', nullable: true }, parent_id: { type: "string", nullable: true },
user_id: { type: 'string', nullable: true } user_id: { type: "string", nullable: true },
}, },
required: ['id', 'parent_id', 'user_id'], required: ["id", "parent_id", "user_id"],
additionalProperties: false additionalProperties: false,
} as const; } as const;
// Entity schema // Entity schema
export const entitySchema = { export const entitySchema = {
type: 'object', type: "object",
properties: { properties: {
entity_id: { type: 'string' }, entity_id: { type: "string" },
state: { type: 'string' }, state: { type: "string" },
attributes: { attributes: {
type: 'object', type: "object",
additionalProperties: true additionalProperties: true,
}, },
last_changed: { type: 'string' }, last_changed: { type: "string" },
last_updated: { type: 'string' }, last_updated: { type: "string" },
context: contextSchema context: contextSchema,
}, },
required: ['entity_id', 'state', 'attributes', 'last_changed', 'last_updated', 'context'], required: [
additionalProperties: false "entity_id",
"state",
"attributes",
"last_changed",
"last_updated",
"context",
],
additionalProperties: false,
} as const; } as const;
// Service schema // Service schema
export const serviceSchema = { export const serviceSchema = {
type: 'object', type: "object",
properties: { properties: {
name: { type: 'string' }, name: { type: "string" },
description: { type: 'string' }, description: { type: "string" },
target: { target: {
type: 'object', type: "object",
nullable: true, nullable: true,
properties: { properties: {
entity: { type: 'array', items: { type: 'string' }, nullable: true }, entity: { type: "array", items: { type: "string" }, nullable: true },
device: { type: 'array', items: { type: 'string' }, nullable: true }, device: { type: "array", items: { type: "string" }, nullable: true },
area: { type: 'array', items: { type: 'string' }, nullable: true } area: { type: "array", items: { type: "string" }, nullable: true },
}, },
required: [], required: [],
additionalProperties: false additionalProperties: false,
}, },
fields: { fields: {
type: 'object', type: "object",
additionalProperties: true additionalProperties: true,
}
}, },
required: ['name', 'description', 'fields'], },
additionalProperties: false required: ["name", "description", "fields"],
additionalProperties: false,
} as const; } as const;
// Define the trigger schema without type assertion // Define the trigger schema without type assertion
export const triggerSchema = { export const triggerSchema = {
type: 'object', type: "object",
properties: { properties: {
platform: { type: 'string' }, platform: { type: "string" },
event: { type: 'string', nullable: true }, event: { type: "string", nullable: true },
entity_id: { type: 'string', nullable: true }, entity_id: { type: "string", nullable: true },
to: { type: 'string', nullable: true }, to: { type: "string", nullable: true },
from: { type: 'string', nullable: true }, from: { type: "string", nullable: true },
offset: { type: 'string', nullable: true } offset: { type: "string", nullable: true },
}, },
required: ['platform'], required: ["platform"],
additionalProperties: true additionalProperties: true,
}; };
// Define the automation schema // Define the automation schema
export const automationSchema = { export const automationSchema = {
type: 'object', type: "object",
properties: { properties: {
alias: { type: 'string' }, alias: { type: "string" },
description: { type: 'string', nullable: true }, description: { type: "string", nullable: true },
mode: { mode: {
type: 'string', type: "string",
enum: ['single', 'parallel', 'queued', 'restart'], enum: ["single", "parallel", "queued", "restart"],
nullable: true nullable: true,
}, },
trigger: { trigger: {
type: 'array', type: "array",
items: triggerSchema items: triggerSchema,
}, },
condition: { condition: {
type: 'array', type: "array",
items: { items: {
type: 'object', type: "object",
additionalProperties: true additionalProperties: true,
}, },
nullable: true nullable: true,
}, },
action: { action: {
type: 'array', type: "array",
items: { items: {
type: 'object', type: "object",
additionalProperties: true additionalProperties: true,
}
}
}, },
required: ['alias', 'trigger', 'action'], },
additionalProperties: false },
required: ["alias", "trigger", "action"],
additionalProperties: false,
}; };
export const deviceControlSchema: JSONSchemaType<DeviceControlType> = { export const deviceControlSchema: JSONSchemaType<DeviceControlType> = {
type: 'object', type: "object",
properties: { properties: {
domain: { domain: {
type: 'string', type: "string",
enum: ['light', 'switch', 'climate', 'cover', 'fan', 'scene', 'script', 'media_player'] enum: [
"light",
"switch",
"climate",
"cover",
"fan",
"scene",
"script",
"media_player",
],
}, },
command: { type: 'string' }, command: { type: "string" },
entity_id: { entity_id: {
anyOf: [ anyOf: [
{ type: 'string' }, { type: "string" },
{ {
type: 'array', type: "array",
items: { type: 'string' } items: { type: "string" },
} },
] ],
}, },
parameters: { parameters: {
type: 'object', type: "object",
nullable: true, nullable: true,
additionalProperties: true additionalProperties: true,
}
}, },
required: ['domain', 'command', 'entity_id'], },
additionalProperties: false required: ["domain", "command", "entity_id"],
additionalProperties: false,
}; };
// State changed event schema // State changed event schema
export const stateChangedEventSchema = { export const stateChangedEventSchema = {
type: 'object', type: "object",
properties: { properties: {
event_type: { type: 'string', const: 'state_changed' }, event_type: { type: "string", const: "state_changed" },
data: { data: {
type: 'object', type: "object",
properties: { properties: {
entity_id: { type: 'string' }, entity_id: { type: "string" },
new_state: { ...entitySchema, nullable: true }, new_state: { ...entitySchema, nullable: true },
old_state: { ...entitySchema, nullable: true } old_state: { ...entitySchema, nullable: true },
}, },
required: ['entity_id', 'new_state', 'old_state'], required: ["entity_id", "new_state", "old_state"],
additionalProperties: false additionalProperties: false,
}, },
origin: { type: 'string' }, origin: { type: "string" },
time_fired: { type: 'string' }, time_fired: { type: "string" },
context: contextSchema context: contextSchema,
}, },
required: ['event_type', 'data', 'origin', 'time_fired', 'context'], required: ["event_type", "data", "origin", "time_fired", "context"],
additionalProperties: false additionalProperties: false,
} as const; } as const;
// Config schema // Config schema
export const configSchema = { export const configSchema = {
type: 'object', type: "object",
properties: { properties: {
components: { type: 'array', items: { type: 'string' } }, components: { type: "array", items: { type: "string" } },
config_dir: { type: 'string' }, config_dir: { type: "string" },
elevation: { type: 'number' }, elevation: { type: "number" },
latitude: { type: 'number' }, latitude: { type: "number" },
longitude: { type: 'number' }, longitude: { type: "number" },
location_name: { type: 'string' }, location_name: { type: "string" },
time_zone: { type: 'string' }, time_zone: { type: "string" },
unit_system: { unit_system: {
type: 'object', type: "object",
properties: { properties: {
length: { type: 'string' }, length: { type: "string" },
mass: { type: 'string' }, mass: { type: "string" },
temperature: { type: 'string' }, temperature: { type: "string" },
volume: { type: 'string' } volume: { type: "string" },
}, },
required: ['length', 'mass', 'temperature', 'volume'], required: ["length", "mass", "temperature", "volume"],
additionalProperties: false additionalProperties: false,
}, },
version: { type: 'string' } version: { type: "string" },
}, },
required: [ required: [
'components', "components",
'config_dir', "config_dir",
'elevation', "elevation",
'latitude', "latitude",
'longitude', "longitude",
'location_name', "location_name",
'time_zone', "time_zone",
'unit_system', "unit_system",
'version' "version",
], ],
additionalProperties: false additionalProperties: false,
} as const; } as const;

View File

@@ -0,0 +1,184 @@
import { describe, expect, it, beforeEach } from "bun:test";
import { TokenManager } from "../index";
import jwt from "jsonwebtoken";
const validSecret = "test_secret_that_is_at_least_32_chars_long";
const testIp = "127.0.0.1";
// Mock the rate limit window for faster tests
const MOCK_RATE_LIMIT_WINDOW = 100; // 100ms instead of 15 minutes
describe("Security Module", () => {
beforeEach(() => {
process.env.JWT_SECRET = validSecret;
// Reset failed attempts map
(TokenManager as any).failedAttempts = new Map();
// Mock the security config
(TokenManager as any).SECURITY_CONFIG = {
...(TokenManager as any).SECURITY_CONFIG,
LOCKOUT_DURATION: MOCK_RATE_LIMIT_WINDOW,
MAX_FAILED_ATTEMPTS: 5,
MAX_TOKEN_AGE: 30 * 24 * 60 * 60 * 1000 // 30 days
};
});
describe("TokenManager", () => {
it("should encrypt and decrypt tokens", () => {
const originalToken = "test-token";
const encryptedToken = TokenManager.encryptToken(originalToken, validSecret);
expect(encryptedToken).toBeDefined();
expect(encryptedToken.includes(originalToken)).toBe(false);
const decryptedToken = TokenManager.decryptToken(encryptedToken, validSecret);
expect(decryptedToken).toBeDefined();
expect(decryptedToken).toBe(originalToken);
});
it("should validate tokens correctly", () => {
const payload = { userId: "123", role: "user" };
const token = jwt.sign(payload, validSecret, { expiresIn: "1h" });
const result = TokenManager.validateToken(token, testIp);
expect(result.valid).toBe(true);
expect(result.error).toBeUndefined();
// Verify payload separately
const decoded = jwt.verify(token, validSecret) as typeof payload;
expect(decoded.userId).toBe(payload.userId);
expect(decoded.role).toBe(payload.role);
});
it("should handle empty tokens", () => {
const result = TokenManager.validateToken("", testIp);
expect(result.valid).toBe(false);
expect(result.error).toBe("Invalid token format");
});
it("should handle expired tokens", () => {
const now = Math.floor(Date.now() / 1000);
const payload = {
userId: "123",
role: "user",
iat: now - 3600, // issued 1 hour ago
exp: now - 1800 // expired 30 minutes ago
};
const token = jwt.sign(payload, validSecret);
const result = TokenManager.validateToken(token, testIp);
expect(result.valid).toBe(false);
expect(result.error).toBe("Token has expired");
});
it("should handle token tampering", () => {
// Use a different IP for this test to avoid rate limiting
const uniqueIp = "192.168.1.1";
const payload = { userId: "123", role: "user" };
const token = jwt.sign(payload, validSecret);
const tamperedToken = token.slice(0, -5) + "xxxxx"; // Tamper with signature
const result = TokenManager.validateToken(tamperedToken, uniqueIp);
expect(result.valid).toBe(false);
expect(result.error).toBe("Invalid token signature");
});
});
describe("Token Encryption", () => {
it("should use different IVs for same token", () => {
const token = "test-token";
const encrypted1 = TokenManager.encryptToken(token, validSecret);
const encrypted2 = TokenManager.encryptToken(token, validSecret);
expect(encrypted1).toBeDefined();
expect(encrypted2).toBeDefined();
expect(encrypted1 === encrypted2).toBe(false);
});
it("should handle large tokens", () => {
const largeToken = "x".repeat(1024);
const encrypted = TokenManager.encryptToken(largeToken, validSecret);
const decrypted = TokenManager.decryptToken(encrypted, validSecret);
expect(decrypted).toBe(largeToken);
});
it("should fail gracefully with invalid encrypted data", () => {
expect(() => TokenManager.decryptToken("invalid-encrypted-data", validSecret))
.toThrow("Invalid encrypted token");
});
});
describe("Rate Limiting", () => {
beforeEach(() => {
// Reset failed attempts before each test
(TokenManager as any).failedAttempts = new Map();
});
it("should track failed attempts by IP", () => {
const invalidToken = "x".repeat(64);
const ip1 = "1.1.1.1";
const ip2 = "2.2.2.2";
// Make a single failed attempt for each IP
TokenManager.validateToken(invalidToken, ip1);
TokenManager.validateToken(invalidToken, ip2);
const attempts = (TokenManager as any).failedAttempts;
expect(attempts.has(ip1)).toBe(true);
expect(attempts.has(ip2)).toBe(true);
expect(attempts.get(ip1).count).toBe(1);
expect(attempts.get(ip2).count).toBe(1);
expect(attempts.get(ip1).lastAttempt).toBeGreaterThan(0);
expect(attempts.get(ip2).lastAttempt).toBeGreaterThan(0);
});
it("should handle rate limiting for failed attempts", async () => {
const invalidToken = "x".repeat(64);
const uniqueIp = "10.0.0.1";
// Make multiple failed attempts
for (let i = 0; i < 5; i++) {
const result = TokenManager.validateToken(invalidToken, uniqueIp);
expect(result.valid).toBe(false);
if (i < 4) {
expect(result.error).toBe("Invalid token signature");
} else {
expect(result.error).toBe("Too many failed attempts. Please try again later.");
}
}
// Next attempt should be rate limited
const result = TokenManager.validateToken(invalidToken, uniqueIp);
expect(result.valid).toBe(false);
expect(result.error).toBe("Too many failed attempts. Please try again later.");
// Wait for rate limit window to expire
await new Promise(resolve => setTimeout(resolve, MOCK_RATE_LIMIT_WINDOW + 50));
// After window expires, should get normal error again
const finalResult = TokenManager.validateToken(invalidToken, uniqueIp);
expect(finalResult.valid).toBe(false);
expect(finalResult.error).toBe("Invalid token signature");
});
it("should reset rate limits after window expires", async () => {
const invalidToken = "x".repeat(64);
const uniqueIp = "172.16.0.1";
// Make some failed attempts
for (let i = 0; i < 3; i++) {
const result = TokenManager.validateToken(invalidToken, uniqueIp);
expect(result.valid).toBe(false);
expect(result.error).toBe("Invalid token signature");
}
// Wait for rate limit window to expire
await new Promise(resolve => setTimeout(resolve, MOCK_RATE_LIMIT_WINDOW + 50));
// After window expires, should get normal error
const result = TokenManager.validateToken(invalidToken, uniqueIp);
expect(result.valid).toBe(false);
expect(result.error).toBe("Invalid token signature");
// Should have one new attempt recorded
const attempts = (TokenManager as any).failedAttempts;
expect(attempts.has(uniqueIp)).toBe(true);
expect(attempts.get(uniqueIp).count).toBe(1);
});
});
});

View File

@@ -1,33 +1,64 @@
import crypto from 'crypto'; import crypto from "crypto";
import { Request, Response, NextFunction } from 'express'; import helmet from "helmet";
import rateLimit from 'express-rate-limit'; import { HelmetOptions } from "helmet";
import helmet from 'helmet'; import jwt from "jsonwebtoken";
import { HelmetOptions } from 'helmet'; import { Elysia, type Context } from "elysia";
import jwt from 'jsonwebtoken';
// Security configuration // Security configuration
const RATE_LIMIT_WINDOW = 15 * 60 * 1000; // 15 minutes const RATE_LIMIT_WINDOW = 15 * 60 * 1000; // 15 minutes
const RATE_LIMIT_MAX = 100; // requests per window const RATE_LIMIT_MAX = 100; // requests per window
const TOKEN_EXPIRY = 24 * 60 * 60 * 1000; // 24 hours const TOKEN_EXPIRY = 24 * 60 * 60 * 1000; // 24 hours
// Rate limiting middleware // Rate limiting state
export const rateLimiter = rateLimit({ const rateLimitStore = new Map<string, { count: number; resetTime: number }>();
windowMs: RATE_LIMIT_WINDOW,
max: RATE_LIMIT_MAX, interface RequestContext {
message: 'Too many requests from this IP, please try again later' request: Request;
set: Context['set'];
}
// Extracted rate limiting logic
export function checkRateLimit(ip: string, maxRequests: number = RATE_LIMIT_MAX, windowMs: number = RATE_LIMIT_WINDOW) {
const now = Date.now();
const record = rateLimitStore.get(ip) || {
count: 0,
resetTime: now + windowMs,
};
if (now > record.resetTime) {
record.count = 0;
record.resetTime = now + windowMs;
}
record.count++;
rateLimitStore.set(ip, record);
if (record.count > maxRequests) {
throw new Error("Too many requests from this IP, please try again later");
}
return true;
}
// Rate limiting middleware for Elysia
export const rateLimiter = new Elysia().derive(({ request }: RequestContext) => {
const ip = request.headers.get("x-forwarded-for") || "unknown";
checkRateLimit(ip);
}); });
// Security configuration // Extracted security headers logic
const helmetConfig: HelmetOptions = { export function applySecurityHeaders(request: Request, helmetConfig?: HelmetOptions) {
const config: HelmetOptions = helmetConfig || {
contentSecurityPolicy: { contentSecurityPolicy: {
useDefaults: true, useDefaults: true,
directives: { directives: {
defaultSrc: ["'self'"], defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"], scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"], styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", 'data:', 'https:'], imgSrc: ["'self'", "data:", "https:"],
connectSrc: ["'self'", 'wss:', 'https:'] connectSrc: ["'self'", "wss:", "https:"],
} },
}, },
dnsPrefetchControl: true, dnsPrefetchControl: true,
frameguard: true, frameguard: true,
@@ -36,27 +67,155 @@ const helmetConfig: HelmetOptions = {
ieNoOpen: true, ieNoOpen: true,
noSniff: true, noSniff: true,
referrerPolicy: { referrerPolicy: {
policy: ['no-referrer', 'strict-origin-when-cross-origin'] policy: ["no-referrer", "strict-origin-when-cross-origin"],
},
};
const headers = helmet(config);
// Apply helmet headers to the request
Object.entries(headers).forEach(([key, value]) => {
if (typeof value === 'string') {
request.headers.set(key, value);
} }
}; });
// Security headers middleware return headers;
export const securityHeaders = helmet(helmetConfig); }
const ALGORITHM = 'aes-256-gcm'; // Security headers middleware for Elysia
export const securityHeaders = new Elysia().derive(({ request }: RequestContext) => {
applySecurityHeaders(request);
});
// Extracted request validation logic
export function validateRequestHeaders(request: Request, requiredContentType = 'application/json') {
// Validate content type for POST/PUT/PATCH requests
if (["POST", "PUT", "PATCH"].includes(request.method)) {
const contentType = request.headers.get("content-type");
if (!contentType?.includes(requiredContentType)) {
throw new Error(`Content-Type must be ${requiredContentType}`);
}
}
// Validate request size
const contentLength = request.headers.get("content-length");
if (contentLength && parseInt(contentLength) > 1024 * 1024) {
throw new Error("Request body too large");
}
// Validate authorization header if required
const authHeader = request.headers.get("authorization");
if (authHeader) {
const [type, token] = authHeader.split(" ");
if (type !== "Bearer" || !token) {
throw new Error("Invalid authorization header");
}
const ip = request.headers.get("x-forwarded-for");
const validation = TokenManager.validateToken(token, ip || undefined);
if (!validation.valid) {
throw new Error(validation.error || "Invalid token");
}
}
return true;
}
// Request validation middleware for Elysia
export const validateRequest = new Elysia().derive(({ request }: RequestContext) => {
validateRequestHeaders(request);
});
// Extracted input sanitization logic
export function sanitizeValue(value: unknown): unknown {
if (typeof value === "string") {
// Basic XSS protection
return value
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#x27;")
.replace(/\//g, "&#x2F;");
}
if (Array.isArray(value)) {
return value.map(sanitizeValue);
}
if (typeof value === "object" && value !== null) {
return Object.fromEntries(
Object.entries(value).map(([k, v]) => [k, sanitizeValue(v)])
);
}
return value;
}
// Input sanitization middleware for Elysia
export const sanitizeInput = new Elysia().derive(async ({ request }: RequestContext) => {
if (["POST", "PUT", "PATCH"].includes(request.method)) {
const body = await request.json();
request.json = () => Promise.resolve(sanitizeValue(body));
}
});
// Extracted error handling logic
export function handleError(error: Error, env: string = process.env.NODE_ENV || 'production') {
console.error("Error:", error);
const baseResponse = {
error: true,
message: "Internal server error",
timestamp: new Date().toISOString(),
};
if (env === 'development') {
return {
...baseResponse,
error: error.message,
stack: error.stack,
};
}
return baseResponse;
}
// Error handling middleware for Elysia
export const errorHandler = new Elysia().onError(({ error, set }: { error: Error; set: Context['set'] }) => {
set.status = error instanceof jwt.JsonWebTokenError ? 401 : 500;
return handleError(error);
});
const ALGORITHM = "aes-256-gcm";
const IV_LENGTH = 16; const IV_LENGTH = 16;
const AUTH_TAG_LENGTH = 16; const AUTH_TAG_LENGTH = 16;
// Security configuration
const SECURITY_CONFIG = {
TOKEN_EXPIRY: 24 * 60 * 60 * 1000, // 24 hours
MAX_TOKEN_AGE: 30 * 24 * 60 * 60 * 1000, // 30 days
MIN_TOKEN_LENGTH: 32,
MAX_FAILED_ATTEMPTS: 5,
LOCKOUT_DURATION: 15 * 60 * 1000, // 15 minutes
};
// Track failed authentication attempts
const failedAttempts = new Map<
string,
{ count: number; lastAttempt: number }
>();
export class TokenManager { export class TokenManager {
/** /**
* Encrypts a token using AES-256-GCM * Encrypts a token using AES-256-GCM
*/ */
static encryptToken(token: string, key: string): string { static encryptToken(token: string, key: string): string {
if (!token || typeof token !== 'string') { if (!token || typeof token !== "string") {
throw new Error('Invalid token'); throw new Error("Invalid token");
} }
if (!key || typeof key !== 'string' || key.length < 32) { if (!key || typeof key !== "string" || key.length < 32) {
throw new Error('Invalid encryption key'); throw new Error("Invalid encryption key");
} }
try { try {
@@ -64,15 +223,15 @@ export class TokenManager {
const cipher = crypto.createCipheriv(ALGORITHM, key.slice(0, 32), iv); const cipher = crypto.createCipheriv(ALGORITHM, key.slice(0, 32), iv);
const encrypted = Buffer.concat([ const encrypted = Buffer.concat([
cipher.update(token, 'utf8'), cipher.update(token, "utf8"),
cipher.final() cipher.final(),
]); ]);
const tag = cipher.getAuthTag(); const tag = cipher.getAuthTag();
// Format: algorithm:iv:tag:encrypted // Format: algorithm:iv:tag:encrypted
return `${ALGORITHM}:${iv.toString('base64')}:${tag.toString('base64')}:${encrypted.toString('base64')}`; return `${ALGORITHM}:${iv.toString("base64")}:${tag.toString("base64")}:${encrypted.toString("base64")}`;
} catch (error) { } catch (error) {
throw new Error('Failed to encrypt token'); throw new Error("Failed to encrypt token");
} }
} }
@@ -80,141 +239,183 @@ export class TokenManager {
* Decrypts a token using AES-256-GCM * Decrypts a token using AES-256-GCM
*/ */
static decryptToken(encryptedToken: string, key: string): string { static decryptToken(encryptedToken: string, key: string): string {
if (!encryptedToken || typeof encryptedToken !== 'string') { if (!encryptedToken || typeof encryptedToken !== "string") {
throw new Error('Invalid encrypted token'); throw new Error("Invalid encrypted token");
} }
if (!key || typeof key !== 'string' || key.length < 32) { if (!key || typeof key !== "string" || key.length < 32) {
throw new Error('Invalid encryption key'); throw new Error("Invalid encryption key");
} }
try { try {
const [algorithm, ivBase64, tagBase64, encryptedBase64] = encryptedToken.split(':'); const [algorithm, ivBase64, tagBase64, encryptedBase64] =
encryptedToken.split(":");
if (algorithm !== ALGORITHM || !ivBase64 || !tagBase64 || !encryptedBase64) { if (
throw new Error('Invalid encrypted token format'); algorithm !== ALGORITHM ||
!ivBase64 ||
!tagBase64 ||
!encryptedBase64
) {
throw new Error("Invalid encrypted token format");
} }
const iv = Buffer.from(ivBase64, 'base64'); const iv = Buffer.from(ivBase64, "base64");
const tag = Buffer.from(tagBase64, 'base64'); const tag = Buffer.from(tagBase64, "base64");
const encrypted = Buffer.from(encryptedBase64, 'base64'); const encrypted = Buffer.from(encryptedBase64, "base64");
const decipher = crypto.createDecipheriv(ALGORITHM, key.slice(0, 32), iv); const decipher = crypto.createDecipheriv(ALGORITHM, key.slice(0, 32), iv);
decipher.setAuthTag(tag); decipher.setAuthTag(tag);
return Buffer.concat([ return Buffer.concat([
decipher.update(encrypted), decipher.update(encrypted),
decipher.final() decipher.final(),
]).toString('utf8'); ]).toString("utf8");
} catch (error) { } catch (error) {
if (error instanceof Error && error.message === 'Invalid encrypted token format') { if (
error instanceof Error &&
error.message === "Invalid encrypted token format"
) {
throw error; throw error;
} }
throw new Error('Invalid encrypted token'); throw new Error("Invalid encrypted token");
} }
} }
/** /**
* Validates a JWT token * Validates a JWT token with enhanced security checks
*/ */
static validateToken(token: string): boolean { static validateToken(
if (!token || typeof token !== 'string') { token: string | undefined | null,
return false; ip?: string,
): { valid: boolean; error?: string } {
// Check basic token format
if (!token || typeof token !== "string") {
return { valid: false, error: "Invalid token format" };
}
// Check for token length
if (token.length < SECURITY_CONFIG.MIN_TOKEN_LENGTH) {
if (ip) this.recordFailedAttempt(ip);
return { valid: false, error: "Token length below minimum requirement" };
}
// Check for rate limiting
if (ip && this.isRateLimited(ip)) {
return {
valid: false,
error: "Too many failed attempts. Please try again later.",
};
}
// Get JWT secret
const secret = process.env.JWT_SECRET;
if (!secret) {
return { valid: false, error: "JWT secret not configured" };
} }
try { try {
const decoded = jwt.decode(token); // Verify token signature and decode
if (!decoded || typeof decoded !== 'object') { const decoded = jwt.verify(token, secret, {
return false; algorithms: ["HS256"],
clockTolerance: 0, // No clock skew tolerance
ignoreExpiration: false, // Always check expiration
}) as jwt.JwtPayload;
// Verify token structure
if (!decoded || typeof decoded !== "object") {
if (ip) this.recordFailedAttempt(ip);
return { valid: false, error: "Invalid token structure" };
} }
// Check for expiration // Check required claims
if (!decoded.exp) { if (!decoded.exp || !decoded.iat) {
return false; if (ip) this.recordFailedAttempt(ip);
return { valid: false, error: "Token missing required claims" };
} }
const now = Math.floor(Date.now() / 1000); const now = Math.floor(Date.now() / 1000);
// Check expiration
if (decoded.exp <= now) { if (decoded.exp <= now) {
if (ip) this.recordFailedAttempt(ip);
return { valid: false, error: "Token has expired" };
}
// Check token age
const tokenAge = (now - decoded.iat) * 1000;
if (tokenAge > SECURITY_CONFIG.MAX_TOKEN_AGE) {
if (ip) this.recordFailedAttempt(ip);
return { valid: false, error: "Token exceeds maximum age limit" };
}
// Reset failed attempts on successful validation
if (ip) {
failedAttempts.delete(ip);
}
return { valid: true };
} catch (error) {
if (ip) this.recordFailedAttempt(ip);
if (error instanceof jwt.TokenExpiredError) {
return { valid: false, error: "Token has expired" };
}
if (error instanceof jwt.JsonWebTokenError) {
return { valid: false, error: "Invalid token signature" };
}
return { valid: false, error: "Token validation failed" };
}
}
/**
* Records a failed authentication attempt for rate limiting
*/
private static recordFailedAttempt(ip?: string): void {
if (!ip) return;
const attempt = failedAttempts.get(ip) || {
count: 0,
lastAttempt: Date.now(),
};
attempt.count++;
attempt.lastAttempt = Date.now();
failedAttempts.set(ip, attempt);
}
/**
* Checks if an IP is rate limited due to too many failed attempts
*/
private static isRateLimited(ip: string): boolean {
const attempt = failedAttempts.get(ip);
if (!attempt) return false;
// Reset if lockout duration has passed
if (Date.now() - attempt.lastAttempt >= SECURITY_CONFIG.LOCKOUT_DURATION) {
failedAttempts.delete(ip);
return false; return false;
} }
// Verify signature using the secret from environment variable return attempt.count >= SECURITY_CONFIG.MAX_FAILED_ATTEMPTS;
}
/**
* Generates a new JWT token
*/
static generateToken(payload: Record<string, any>): string {
const secret = process.env.JWT_SECRET; const secret = process.env.JWT_SECRET;
if (!secret) { if (!secret) {
return false; throw new Error("JWT secret not configured");
} }
try { // Add required claims
jwt.verify(token, secret); const now = Math.floor(Date.now() / 1000);
return true; const tokenPayload = {
} catch { ...payload,
return false; iat: now,
} exp: now + Math.floor(TOKEN_EXPIRY / 1000),
} catch { };
return false;
}
}
}
// Request validation middleware return jwt.sign(tokenPayload, secret, {
export function validateRequest(req: Request, res: Response, next: NextFunction) { algorithm: "HS256",
// Skip validation for health and MCP schema endpoints
if (req.path === '/health' || req.path === '/mcp') {
return next();
}
// Validate content type
if (req.method !== 'GET' && !req.is('application/json')) {
return res.status(415).json({
error: 'Unsupported Media Type - Content-Type must be application/json'
}); });
} }
// Validate token
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token || !TokenManager.validateToken(token)) {
return res.status(401).json({
error: 'Invalid or expired token'
});
}
// Validate request body
if (req.method !== 'GET' && (!req.body || typeof req.body !== 'object')) {
return res.status(400).json({
error: 'Invalid request body'
});
}
next();
} }
// Input sanitization middleware
export function sanitizeInput(req: Request, res: Response, next: NextFunction) {
if (req.body && typeof req.body === 'object') {
const sanitized = JSON.parse(
JSON.stringify(req.body).replace(/[<>]/g, '')
);
req.body = sanitized;
}
next();
}
// Error handling middleware
export function errorHandler(err: Error, req: Request, res: Response, next: NextFunction) {
console.error(err.stack);
res.status(500).json({
error: 'Internal Server Error',
message: process.env.NODE_ENV === 'development' ? err.message : undefined
});
}
// Export security middleware chain
export const securityMiddleware = [
helmet(helmetConfig),
rateLimit({
windowMs: 15 * 60 * 1000,
max: 100
}),
validateRequest,
sanitizeInput,
errorHandler
];

View File

@@ -1,9 +1,9 @@
import { Request, Response, NextFunction } from 'express'; import { Request, Response, NextFunction } from "express";
import { validateRequest, sanitizeInput } from '../../src/security/middleware'; import { validateRequest, sanitizeInput } from "../../src/security/middleware";
type MockRequest = { type MockRequest = {
headers: { headers: {
'content-type'?: string; "content-type"?: string;
authorization?: string; authorization?: string;
}; };
body?: any; body?: any;
@@ -16,7 +16,7 @@ type MockResponse = {
setHeader: jest.MockInstance<MockResponse, [name: string, value: string]>; setHeader: jest.MockInstance<MockResponse, [name: string, value: string]>;
}; };
describe('Security Middleware', () => { describe("Security Middleware", () => {
let mockRequest: MockRequest; let mockRequest: MockRequest;
let mockResponse: MockResponse; let mockResponse: MockResponse;
let nextFunction: jest.Mock; let nextFunction: jest.Mock;
@@ -25,83 +25,117 @@ describe('Security Middleware', () => {
mockRequest = { mockRequest = {
headers: {}, headers: {},
body: {}, body: {},
is: jest.fn<string | false | null, [string | string[]]>().mockReturnValue('json') is: jest
.fn<string | false | null, [string | string[]]>()
.mockReturnValue("json"),
}; };
mockResponse = { mockResponse = {
status: jest.fn<MockResponse, [number]>().mockReturnThis(), status: jest.fn<MockResponse, [number]>().mockReturnThis(),
json: jest.fn<MockResponse, [any]>().mockReturnThis(), json: jest.fn<MockResponse, [any]>().mockReturnThis(),
setHeader: jest.fn<MockResponse, [string, string]>().mockReturnThis() setHeader: jest.fn<MockResponse, [string, string]>().mockReturnThis(),
}; };
nextFunction = jest.fn(); nextFunction = jest.fn();
}); });
describe('validateRequest', () => { describe("validateRequest", () => {
it('should pass valid requests', () => { it("should pass valid requests", () => {
mockRequest.headers.authorization = 'Bearer valid-token'; mockRequest.headers.authorization = "Bearer valid-token";
validateRequest(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); validateRequest(
mockRequest as unknown as Request,
mockResponse as unknown as Response,
nextFunction,
);
expect(nextFunction).toHaveBeenCalled(); expect(nextFunction).toHaveBeenCalled();
}); });
it('should reject requests without authorization header', () => { it("should reject requests without authorization header", () => {
validateRequest(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); validateRequest(
mockRequest as unknown as Request,
mockResponse as unknown as Response,
nextFunction,
);
expect(mockResponse.status).toHaveBeenCalledWith(401); expect(mockResponse.status).toHaveBeenCalledWith(401);
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({ expect(mockResponse.json).toHaveBeenCalledWith(
error: expect.stringContaining('authorization') expect.objectContaining({
})); error: expect.stringContaining("authorization"),
}),
);
}); });
it('should reject requests with invalid authorization format', () => { it("should reject requests with invalid authorization format", () => {
mockRequest.headers.authorization = 'invalid-format'; mockRequest.headers.authorization = "invalid-format";
validateRequest(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); validateRequest(
mockRequest as unknown as Request,
mockResponse as unknown as Response,
nextFunction,
);
expect(mockResponse.status).toHaveBeenCalledWith(401); expect(mockResponse.status).toHaveBeenCalledWith(401);
expect(mockResponse.json).toHaveBeenCalledWith(expect.objectContaining({ expect(mockResponse.json).toHaveBeenCalledWith(
error: expect.stringContaining('Bearer') expect.objectContaining({
})); error: expect.stringContaining("Bearer"),
}),
);
}); });
}); });
describe('sanitizeInput', () => { describe("sanitizeInput", () => {
it('should pass requests without body', () => { it("should pass requests without body", () => {
delete mockRequest.body; delete mockRequest.body;
sanitizeInput(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); sanitizeInput(
mockRequest as unknown as Request,
mockResponse as unknown as Response,
nextFunction,
);
expect(nextFunction).toHaveBeenCalled(); expect(nextFunction).toHaveBeenCalled();
}); });
it('should sanitize HTML in request body', () => { it("should sanitize HTML in request body", () => {
mockRequest.body = { mockRequest.body = {
text: '<script>alert("xss")</script>Hello', text: '<script>alert("xss")</script>Hello',
nested: { nested: {
html: '<img src="x" onerror="alert(1)">World' html: '<img src="x" onerror="alert(1)">World',
} },
}; };
sanitizeInput(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); sanitizeInput(
expect(mockRequest.body.text).toBe('Hello'); mockRequest as unknown as Request,
expect(mockRequest.body.nested.html).toBe('World'); mockResponse as unknown as Response,
nextFunction,
);
expect(mockRequest.body.text).toBe("Hello");
expect(mockRequest.body.nested.html).toBe("World");
expect(nextFunction).toHaveBeenCalled(); expect(nextFunction).toHaveBeenCalled();
}); });
it('should handle non-object bodies', () => { it("should handle non-object bodies", () => {
mockRequest.body = '<p>text</p>'; mockRequest.body = "<p>text</p>";
sanitizeInput(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); sanitizeInput(
expect(mockRequest.body).toBe('text'); mockRequest as unknown as Request,
mockResponse as unknown as Response,
nextFunction,
);
expect(mockRequest.body).toBe("text");
expect(nextFunction).toHaveBeenCalled(); expect(nextFunction).toHaveBeenCalled();
}); });
it('should preserve non-string values', () => { it("should preserve non-string values", () => {
mockRequest.body = { mockRequest.body = {
number: 42, number: 42,
boolean: true, boolean: true,
null: null, null: null,
array: [1, 2, 3] array: [1, 2, 3],
}; };
sanitizeInput(mockRequest as unknown as Request, mockResponse as unknown as Response, nextFunction); sanitizeInput(
mockRequest as unknown as Request,
mockResponse as unknown as Response,
nextFunction,
);
expect(mockRequest.body).toEqual({ expect(mockRequest.body).toEqual({
number: 42, number: 42,
boolean: true, boolean: true,
null: null, null: null,
array: [1, 2, 3] array: [1, 2, 3],
}); });
expect(nextFunction).toHaveBeenCalled(); expect(nextFunction).toHaveBeenCalled();
}); });

View File

@@ -0,0 +1,230 @@
import { SSEManager } from "../index";
import { TokenManager } from "../../security/index";
import type { SSEClient } from "../types";
import {
describe,
it,
expect,
beforeEach,
afterEach,
mock,
Mock,
} from "bun:test";
describe("SSE Security Features", () => {
const TEST_IP = "127.0.0.1";
const validToken = "valid_token";
let sseManager: SSEManager;
let validateTokenMock: Mock<
(token: string, ip: string) => { valid: boolean; error?: string }
>;
beforeEach(() => {
sseManager = new SSEManager({
maxClients: 2,
rateLimit: {
MAX_MESSAGES: 2,
WINDOW_MS: 1000,
BURST_LIMIT: 1,
},
});
validateTokenMock = mock((token: string) => ({
valid: token === validToken,
error: token !== validToken ? "Invalid token" : undefined,
}));
TokenManager.validateToken = validateTokenMock;
});
afterEach(() => {
validateTokenMock.mockReset();
});
function createTestClient(
id: string,
): Omit<SSEClient, "authenticated" | "subscriptions" | "rateLimit"> {
return {
id,
ip: TEST_IP,
connectedAt: new Date(),
connectionTime: Date.now(),
send: mock((data: string) => {}),
};
}
describe("Client Authentication", () => {
it("should authenticate valid clients", () => {
const client = createTestClient("test-client-1");
const result = sseManager.addClient(client, validToken);
expect(result).toBeTruthy();
expect(validateTokenMock).toHaveBeenCalledWith(validToken, TEST_IP);
expect(result?.authenticated).toBe(true);
});
it("should reject invalid tokens", () => {
const client = createTestClient("test-client-2");
const result = sseManager.addClient(client, "invalid_token");
expect(result).toBeNull();
expect(validateTokenMock).toHaveBeenCalledWith("invalid_token", TEST_IP);
});
it("should enforce maximum client limit", () => {
// Add max number of clients
const client1 = createTestClient("test-client-0");
const client2 = createTestClient("test-client-1");
const client3 = createTestClient("test-client-2");
expect(sseManager.addClient(client1, validToken)).toBeTruthy();
expect(sseManager.addClient(client2, validToken)).toBeTruthy();
expect(sseManager.addClient(client3, validToken)).toBeNull();
});
});
describe("Client Management", () => {
it("should track client connections", () => {
const client = createTestClient("test-client");
sseManager.addClient(client, validToken);
const stats = sseManager.getStatistics();
expect(stats.totalClients).toBe(1);
expect(stats.authenticatedClients).toBe(1);
});
it("should remove disconnected clients", () => {
const client = createTestClient("test-client");
sseManager.addClient(client, validToken);
sseManager.removeClient("test-client");
const stats = sseManager.getStatistics();
expect(stats.totalClients).toBe(0);
});
it("should cleanup inactive clients", async () => {
const client = createTestClient("test-client");
sseManager.addClient(client, validToken);
// Wait for cleanup interval
await new Promise((resolve) => setTimeout(resolve, 250));
const stats = sseManager.getStatistics();
expect(stats.totalClients).toBe(0);
});
});
describe("Rate Limiting", () => {
it("should enforce rate limits for message sending", () => {
const client = createTestClient("test-client");
const sseClient = sseManager.addClient(client, validToken);
expect(sseClient).toBeTruthy();
// Send messages up to the limit
sseManager["sendToClient"](sseClient!, {
type: "test",
data: { value: "first" },
});
sseManager["sendToClient"](sseClient!, {
type: "test",
data: { value: "second" },
});
// Next message should be rate limited
sseManager["sendToClient"](sseClient!, {
type: "test",
data: { value: "overflow" },
});
const sendMock = client.send as Mock<(data: string) => void>;
expect(sendMock.mock.calls.length).toBe(2);
});
it("should reset rate limits after window expires", async () => {
const client = createTestClient("test-client");
const sseClient = sseManager.addClient(client, validToken);
expect(sseClient).toBeTruthy();
// Send messages up to the limit
sseManager["sendToClient"](sseClient!, {
type: "test",
data: { value: "first" },
});
sseManager["sendToClient"](sseClient!, {
type: "test",
data: { value: "second" },
});
// Wait for rate limit window to expire
await new Promise((resolve) => setTimeout(resolve, 1100));
// Should be able to send messages again
sseManager["sendToClient"](sseClient!, {
type: "test",
data: { value: "new message" },
});
const sendMock = client.send as Mock<(data: string) => void>;
expect(sendMock.mock.calls.length).toBe(3);
});
});
describe("Event Broadcasting", () => {
it("should only send events to authenticated clients", () => {
const client1 = createTestClient("client1");
const client2 = createTestClient("client2");
const sseClient1 = sseManager.addClient(client1, validToken);
const sseClient2 = sseManager.addClient(client2, "invalid_token");
expect(sseClient1).toBeTruthy();
expect(sseClient2).toBeNull();
sseClient1!.subscriptions.add("event:test_event");
const event = {
event_type: "test_event",
data: { value: "test" },
origin: "test",
time_fired: new Date().toISOString(),
context: { id: "test" },
};
sseManager.broadcastEvent(event);
const client1SendMock = client1.send as Mock<(data: string) => void>;
const client2SendMock = client2.send as Mock<(data: string) => void>;
expect(client1SendMock.mock.calls.length).toBe(1);
expect(client2SendMock.mock.calls.length).toBe(0);
});
it("should respect subscription filters", () => {
const client = createTestClient("test-client");
const sseClient = sseManager.addClient(client, validToken);
expect(sseClient).toBeTruthy();
sseClient!.subscriptions.add("event:test_event");
// Send matching event
sseManager.broadcastEvent({
event_type: "test_event",
data: { value: "test" },
origin: "test",
time_fired: new Date().toISOString(),
context: { id: "test" },
});
// Send non-matching event
sseManager.broadcastEvent({
event_type: "other_event",
data: { value: "test" },
origin: "test",
time_fired: new Date().toISOString(),
context: { id: "test" },
});
const sendMock = client.send as Mock<(data: string) => void>;
expect(sendMock.mock.calls.length).toBe(1);
});
});
});

View File

@@ -1,71 +1,118 @@
import { EventEmitter } from 'events'; import { EventEmitter } from "events";
import { HassEntity, HassEvent } from '../interfaces/hass.js'; import { HassEntity, HassEvent } from "../interfaces/hass.js";
import { TokenManager } from '../security/index.js'; import { TokenManager } from "../security/index.js";
// Constants
const DEFAULT_MAX_CLIENTS = 1000;
const DEFAULT_PING_INTERVAL = 30000; // 30 seconds
const DEFAULT_CLEANUP_INTERVAL = 60000; // 1 minute
const DEFAULT_MAX_CONNECTION_AGE = 24 * 60 * 60 * 1000; // 24 hours
const DEFAULT_RATE_LIMIT = {
MAX_MESSAGES: 100, // messages
WINDOW_MS: 60000, // 1 minute
BURST_LIMIT: 10, // max messages per second
};
interface RateLimit { interface RateLimit {
count: number; count: number;
lastReset: number; lastReset: number;
burstCount: number;
lastBurstReset: number;
} }
export interface SSEClient { export interface SSEClient {
id: string; id: string;
send: (data: string) => void; ip: string;
subscriptions: { connectedAt: Date;
entities: Set<string>; lastPingAt?: Date;
events: Set<string>; subscriptions: Set<string>;
domains: Set<string>;
};
authenticated: boolean; authenticated: boolean;
send: (data: string) => void;
rateLimit: RateLimit; rateLimit: RateLimit;
lastPing: number;
connectionTime: number; connectionTime: number;
} }
interface ClientStats {
id: string;
ip: string;
connectedAt: Date;
lastPingAt?: Date;
subscriptionCount: number;
connectionDuration: number;
messagesSent: number;
lastActivity: Date;
}
export class SSEManager extends EventEmitter { export class SSEManager extends EventEmitter {
private clients: Map<string, SSEClient> = new Map(); private clients: Map<string, SSEClient> = new Map();
private static instance: SSEManager | null = null; private static instance: SSEManager | null = null;
private entityStates: Map<string, HassEntity> = new Map(); private entityStates: Map<string, HassEntity> = new Map();
private readonly maxClients: number;
private readonly pingInterval: number;
private readonly cleanupInterval: number;
private readonly maxConnectionAge: number;
private readonly rateLimit: typeof DEFAULT_RATE_LIMIT;
// Configuration constructor(
private readonly MAX_CLIENTS = 100; options: {
private readonly RATE_LIMIT_WINDOW = 60000; // 1 minute maxClients?: number;
private readonly RATE_LIMIT_MAX_REQUESTS = 1000; pingInterval?: number;
private readonly CLIENT_TIMEOUT = 300000; // 5 minutes cleanupInterval?: number;
private readonly PING_INTERVAL = 30000; // 30 seconds maxConnectionAge?: number;
rateLimit?: Partial<typeof DEFAULT_RATE_LIMIT>;
private constructor() { } = {},
) {
super(); super();
console.log('Initializing SSE Manager...'); this.maxClients = options.maxClients || DEFAULT_MAX_CLIENTS;
this.startMaintenanceInterval(); this.pingInterval = options.pingInterval || DEFAULT_PING_INTERVAL;
this.cleanupInterval = options.cleanupInterval || DEFAULT_CLEANUP_INTERVAL;
this.maxConnectionAge =
options.maxConnectionAge || DEFAULT_MAX_CONNECTION_AGE;
this.rateLimit = { ...DEFAULT_RATE_LIMIT, ...options.rateLimit };
console.log("Initializing SSE Manager...");
this.startMaintenanceTasks();
} }
private startMaintenanceInterval() { private startMaintenanceTasks(): void {
// Send periodic pings to keep connections alive
setInterval(() => { setInterval(() => {
this.performMaintenance(); this.clients.forEach((client) => {
}, 60000); // Run every minute if (!this.isRateLimited(client)) {
try {
client.send(
JSON.stringify({
type: "ping",
timestamp: new Date().toISOString(),
}),
);
client.lastPingAt = new Date();
} catch (error) {
console.error(`Failed to ping client ${client.id}:`, error);
this.removeClient(client.id);
} }
}
});
}, this.pingInterval);
private performMaintenance() { // Cleanup inactive or expired connections
setInterval(() => {
const now = Date.now(); const now = Date.now();
this.clients.forEach((client, clientId) => {
const connectionAge = now - client.connectedAt.getTime();
const lastPingAge = client.lastPingAt
? now - client.lastPingAt.getTime()
: 0;
// Check each client for timeouts and rate limits if (
for (const [clientId, client] of this.clients.entries()) { connectionAge > this.maxConnectionAge ||
// Remove inactive clients lastPingAge > this.pingInterval * 2
if (now - client.lastPing > this.CLIENT_TIMEOUT) { ) {
console.log(`Removing inactive client: ${clientId}`); console.log(`Removing inactive client ${clientId}`);
this.removeClient(clientId); this.removeClient(clientId);
continue;
} }
});
// Reset rate limits if window has passed }, this.cleanupInterval);
if (now - client.rateLimit.lastReset > this.RATE_LIMIT_WINDOW) {
client.rateLimit.count = 0;
client.rateLimit.lastReset = now;
}
}
// Log statistics
console.log(`Maintenance complete - Active clients: ${this.clients.size}`);
} }
static getInstance(): SSEManager { static getInstance(): SSEManager {
@@ -75,314 +122,264 @@ export class SSEManager extends EventEmitter {
return SSEManager.instance; return SSEManager.instance;
} }
addClient(client: { id: string; send: (data: string) => void }, token?: string): SSEClient | null { addClient(
// Check maximum client limit client: Omit<SSEClient, "authenticated" | "subscriptions" | "rateLimit">,
if (this.clients.size >= this.MAX_CLIENTS) { token: string,
console.warn('Maximum client limit reached, rejecting new connection'); ): SSEClient | null {
// Validate token
const validationResult = TokenManager.validateToken(token, client.ip);
if (!validationResult.valid) {
console.warn(
`Invalid token for client ${client.id} from IP ${client.ip}: ${validationResult.error}`,
);
return null; return null;
} }
const now = Date.now(); // Check client limit
const sseClient: SSEClient = { if (this.clients.size >= this.maxClients) {
id: client.id, console.warn(`Maximum client limit (${this.maxClients}) reached`);
send: client.send, return null;
subscriptions: { }
entities: new Set<string>(),
events: new Set<string>(), // Create new client with authentication and subscriptions
domains: new Set<string>() const newClient: SSEClient = {
}, ...client,
authenticated: this.validateToken(token), authenticated: true,
subscriptions: new Set(),
lastPingAt: new Date(),
rateLimit: { rateLimit: {
count: 0, count: 0,
lastReset: now lastReset: Date.now(),
burstCount: 0,
lastBurstReset: Date.now(),
}, },
lastPing: now,
connectionTime: now
}; };
this.clients.set(client.id, sseClient); this.clients.set(client.id, newClient);
console.log(`SSE client connected: ${client.id} (authenticated: ${sseClient.authenticated})`); console.log(`New client ${client.id} connected from IP ${client.ip}`);
// Start ping interval for this client return newClient;
this.startClientPing(client.id);
// Send initial connection success message
this.sendToClient(sseClient, {
type: 'connection',
status: 'connected',
id: client.id,
authenticated: sseClient.authenticated,
timestamp: new Date().toISOString()
});
return sseClient;
} }
private startClientPing(clientId: string) { private isRateLimited(client: SSEClient): boolean {
const interval = setInterval(() => { const now = Date.now();
const client = this.clients.get(clientId);
if (!client) { // Reset window counters if needed
clearInterval(interval); if (now - client.rateLimit.lastReset >= this.rateLimit.WINDOW_MS) {
return; client.rateLimit.count = 0;
client.rateLimit.lastReset = now;
} }
this.sendToClient(client, { // Reset burst counters if needed (every second)
type: 'ping', if (now - client.rateLimit.lastBurstReset >= 1000) {
timestamp: new Date().toISOString() client.rateLimit.burstCount = 0;
}); client.rateLimit.lastBurstReset = now;
}, this.PING_INTERVAL);
} }
removeClient(clientId: string) { // Check both window and burst limits
return (
client.rateLimit.count >= this.rateLimit.MAX_MESSAGES ||
client.rateLimit.burstCount >= this.rateLimit.BURST_LIMIT
);
}
private updateRateLimit(client: SSEClient): void {
const now = Date.now();
client.rateLimit.count++;
client.rateLimit.burstCount++;
// Update timestamps if needed
if (now - client.rateLimit.lastReset >= this.rateLimit.WINDOW_MS) {
client.rateLimit.lastReset = now;
client.rateLimit.count = 1;
}
if (now - client.rateLimit.lastBurstReset >= 1000) {
client.rateLimit.lastBurstReset = now;
client.rateLimit.burstCount = 1;
}
}
removeClient(clientId: string): void {
if (this.clients.has(clientId)) { if (this.clients.has(clientId)) {
this.clients.delete(clientId); this.clients.delete(clientId);
console.log(`SSE client disconnected: ${clientId}`); console.log(`SSE client disconnected: ${clientId}`);
this.emit("client_disconnected", {
clientId,
timestamp: new Date().toISOString(),
});
} }
} }
subscribeToEntity(clientId: string, entityId: string) { subscribeToEntity(clientId: string, entityId: string): void {
const client = this.clients.get(clientId); const client = this.clients.get(clientId);
if (client?.authenticated) { if (!client?.authenticated) {
client.subscriptions.entities.add(entityId); console.warn(
`Unauthenticated client ${clientId} attempted to subscribe to entity: ${entityId}`,
);
return;
}
client.subscriptions.add(`entity:${entityId}`);
console.log(`Client ${clientId} subscribed to entity: ${entityId}`); console.log(`Client ${clientId} subscribed to entity: ${entityId}`);
// Send current state if available // Send current state if available
const currentState = this.entityStates.get(entityId); const currentState = this.entityStates.get(entityId);
if (currentState) { if (currentState && !this.isRateLimited(client)) {
this.sendToClient(client, { this.sendToClient(client, {
type: 'state_changed', type: "state_changed",
data: { data: {
entity_id: currentState.entity_id, entity_id: currentState.entity_id,
state: currentState.state, state: currentState.state,
attributes: currentState.attributes, attributes: currentState.attributes,
last_changed: currentState.last_changed, last_changed: currentState.last_changed,
last_updated: currentState.last_updated last_updated: currentState.last_updated,
} },
}); });
} }
} }
subscribeToDomain(clientId: string, domain: string): void {
const client = this.clients.get(clientId);
if (!client?.authenticated) {
console.warn(
`Unauthenticated client ${clientId} attempted to subscribe to domain: ${domain}`,
);
return;
} }
subscribeToDomain(clientId: string, domain: string) { client.subscriptions.add(`domain:${domain}`);
const client = this.clients.get(clientId);
if (client?.authenticated) {
client.subscriptions.domains.add(domain);
console.log(`Client ${clientId} subscribed to domain: ${domain}`); console.log(`Client ${clientId} subscribed to domain: ${domain}`);
} }
subscribeToEvent(clientId: string, eventType: string): void {
const client = this.clients.get(clientId);
if (!client?.authenticated) {
console.warn(
`Unauthenticated client ${clientId} attempted to subscribe to event: ${eventType}`,
);
return;
} }
subscribeToEvent(clientId: string, eventType: string) { client.subscriptions.add(`event:${eventType}`);
const client = this.clients.get(clientId);
if (client?.authenticated) {
client.subscriptions.events.add(eventType);
console.log(`Client ${clientId} subscribed to event: ${eventType}`); console.log(`Client ${clientId} subscribed to event: ${eventType}`);
} }
}
broadcastStateChange(entity: HassEntity) { broadcastStateChange(entity: HassEntity): void {
// Update stored state // Update stored state
this.entityStates.set(entity.entity_id, entity); this.entityStates.set(entity.entity_id, entity);
const domain = entity.entity_id.split('.')[0]; const domain = entity.entity_id.split(".")[0];
const message = { const message = {
type: 'state_changed', type: "state_changed",
data: { data: {
entity_id: entity.entity_id, entity_id: entity.entity_id,
state: entity.state, state: entity.state,
attributes: entity.attributes, attributes: entity.attributes,
last_changed: entity.last_changed, last_changed: entity.last_changed,
last_updated: entity.last_updated last_updated: entity.last_updated,
}, },
timestamp: new Date().toISOString() timestamp: new Date().toISOString(),
}; };
console.log(`Broadcasting state change for ${entity.entity_id}`); console.log(`Broadcasting state change for ${entity.entity_id}`);
// Send to relevant subscribers only // Send to relevant subscribers only
for (const client of this.clients.values()) { this.clients.forEach((client) => {
if (!client.authenticated) continue; if (!client.authenticated || this.isRateLimited(client)) return;
if ( if (
client.subscriptions.entities.has(entity.entity_id) || client.subscriptions.has(`entity:${entity.entity_id}`) ||
client.subscriptions.domains.has(domain) || client.subscriptions.has(`domain:${domain}`) ||
client.subscriptions.events.has('state_changed') client.subscriptions.has("event:state_changed")
) { ) {
this.sendToClient(client, message); this.sendToClient(client, message);
} }
} });
} }
broadcastEvent(event: HassEvent) { broadcastEvent(event: HassEvent): void {
const message = { const message = {
type: event.event_type, type: event.event_type,
data: event.data, data: event.data,
origin: event.origin, origin: event.origin,
time_fired: event.time_fired, time_fired: event.time_fired,
context: event.context, context: event.context,
timestamp: new Date().toISOString() timestamp: new Date().toISOString(),
}; };
console.log(`Broadcasting event: ${event.event_type}`); console.log(`Broadcasting event: ${event.event_type}`);
// Send to relevant subscribers only // Send to relevant subscribers only
for (const client of this.clients.values()) { this.clients.forEach((client) => {
if (!client.authenticated) continue; if (!client.authenticated || this.isRateLimited(client)) return;
if (client.subscriptions.events.has(event.event_type)) { if (client.subscriptions.has(`event:${event.event_type}`)) {
this.sendToClient(client, message); this.sendToClient(client, message);
} }
}
}
private sendToClient(client: SSEClient, data: any) {
try {
// Check rate limit
const now = Date.now();
if (now - client.rateLimit.lastReset > this.RATE_LIMIT_WINDOW) {
client.rateLimit.count = 0;
client.rateLimit.lastReset = now;
}
if (client.rateLimit.count >= this.RATE_LIMIT_MAX_REQUESTS) {
console.warn(`Rate limit exceeded for client ${client.id}`);
this.sendToClient(client, {
type: 'error',
error: 'rate_limit_exceeded',
message: 'Too many requests, please try again later',
timestamp: new Date().toISOString()
}); });
}
private sendToClient(client: SSEClient, data: unknown): void {
try {
if (!client.authenticated) {
console.warn(
`Attempted to send message to unauthenticated client ${client.id}`,
);
return; return;
} }
client.rateLimit.count++; if (this.isRateLimited(client)) {
client.lastPing = now; console.warn(`Rate limit exceeded for client ${client.id}`);
client.send(JSON.stringify(data)); return;
}
const message = typeof data === "string" ? data : JSON.stringify(data);
client.send(message);
this.updateRateLimit(client);
} catch (error) { } catch (error) {
console.error(`Error sending message to client ${client.id}:`, error); console.error(`Failed to send message to client ${client.id}:`, error);
this.removeClient(client.id); this.removeClient(client.id);
} }
} }
private validateToken(token?: string): boolean { getStatistics(): {
if (!token) return false; totalClients: number;
return TokenManager.validateToken(token); authenticatedClients: number;
} clientStats: ClientStats[];
subscriptionStats: { [key: string]: number };
// Utility methods } {
getConnectedClients(): number {
return this.clients.size;
}
getClientSubscriptions(clientId: string) {
return this.clients.get(clientId)?.subscriptions;
}
getEntityState(entityId: string): HassEntity | undefined {
return this.entityStates.get(entityId);
}
// Add new event types
broadcastServiceCall(domain: string, service: string, data: any) {
const message = {
type: 'service_called',
data: {
domain,
service,
service_data: data
},
timestamp: new Date().toISOString()
};
this.broadcastToSubscribers('service_called', message);
}
broadcastAutomationTriggered(automationId: string, trigger: any) {
const message = {
type: 'automation_triggered',
data: {
automation_id: automationId,
trigger
},
timestamp: new Date().toISOString()
};
this.broadcastToSubscribers('automation_triggered', message);
}
broadcastScriptExecuted(scriptId: string, data: any) {
const message = {
type: 'script_executed',
data: {
script_id: scriptId,
execution_data: data
},
timestamp: new Date().toISOString()
};
this.broadcastToSubscribers('script_executed', message);
}
private broadcastToSubscribers(eventType: string, message: any) {
for (const client of this.clients.values()) {
if (!client.authenticated) continue;
if (client.subscriptions.events.has(eventType)) {
this.sendToClient(client, message);
}
}
}
// Add statistics methods
getStatistics() {
const now = Date.now(); const now = Date.now();
const stats = { const clientStats: ClientStats[] = [];
total_clients: this.clients.size, const subscriptionStats: { [key: string]: number } = {};
authenticated_clients: 0, let authenticatedClients = 0;
total_subscriptions: 0,
clients_by_connection_time: {
less_than_1m: 0,
less_than_5m: 0,
less_than_1h: 0,
more_than_1h: 0
},
total_entities_tracked: this.entityStates.size,
subscriptions: {
entities: new Set<string>(),
events: new Set<string>(),
domains: new Set<string>()
}
};
for (const client of this.clients.values()) { this.clients.forEach((client) => {
if (client.authenticated) stats.authenticated_clients++; if (client.authenticated) {
authenticatedClients++;
// Count subscriptions
stats.total_subscriptions +=
client.subscriptions.entities.size +
client.subscriptions.events.size +
client.subscriptions.domains.size;
// Add to subscription sets
client.subscriptions.entities.forEach(entity => stats.subscriptions.entities.add(entity));
client.subscriptions.events.forEach(event => stats.subscriptions.events.add(event));
client.subscriptions.domains.forEach(domain => stats.subscriptions.domains.add(domain));
// Calculate connection duration
const connectionDuration = now - client.connectionTime;
if (connectionDuration < 60000) stats.clients_by_connection_time.less_than_1m++;
else if (connectionDuration < 300000) stats.clients_by_connection_time.less_than_5m++;
else if (connectionDuration < 3600000) stats.clients_by_connection_time.less_than_1h++;
else stats.clients_by_connection_time.more_than_1h++;
} }
// Convert Sets to Arrays for JSON serialization clientStats.push({
id: client.id,
ip: client.ip,
connectedAt: client.connectedAt,
lastPingAt: client.lastPingAt,
subscriptionCount: client.subscriptions.size,
connectionDuration: now - client.connectedAt.getTime(),
messagesSent: client.rateLimit.count,
lastActivity: new Date(client.rateLimit.lastReset),
});
client.subscriptions.forEach((sub) => {
subscriptionStats[sub] = (subscriptionStats[sub] || 0) + 1;
});
});
return { return {
...stats, totalClients: this.clients.size,
subscriptions: { authenticatedClients,
entities: Array.from(stats.subscriptions.entities), clientStats,
events: Array.from(stats.subscriptions.events), subscriptionStats,
domains: Array.from(stats.subscriptions.domains)
}
}; };
} }
} }

67
src/sse/types.ts Normal file
View File

@@ -0,0 +1,67 @@
import type { Mock } from "bun:test";
export interface SSEClient {
id: string;
ip: string;
connectedAt: Date;
send: Mock<(data: string) => void>;
rateLimit: {
count: number;
lastReset: number;
};
connectionTime: number;
}
export interface HassEventData {
[key: string]: unknown;
}
export interface SSEEvent {
event_type: string;
data: HassEventData;
origin: string;
time_fired: string;
context: {
id: string;
[key: string]: unknown;
};
}
export interface SSEMessage {
type: string;
data?: unknown;
error?: string;
}
export interface SSEManagerConfig {
maxClients?: number;
pingInterval?: number;
cleanupInterval?: number;
maxConnectionAge?: number;
rateLimitWindow?: number;
maxRequestsPerWindow?: number;
}
export type MockSendFn = (data: string) => void;
export type MockSend = Mock<MockSendFn>;
export type ValidateTokenFn = (
token: string,
ip?: string,
) => { valid: boolean; error?: string };
export type MockValidateToken = Mock<ValidateTokenFn>;
// Type guard for mock functions
export function isMockFunction(value: unknown): value is Mock<unknown> {
return typeof value === "function" && "mock" in value;
}
// Safe type assertion for mock objects
export function asMockFunction<T extends (...args: any[]) => any>(
value: unknown,
): Mock<T> {
if (!isMockFunction(value)) {
throw new Error("Value is not a mock function");
}
return value as Mock<T>;
}

View File

@@ -1,31 +1,54 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool, AddonParams, HassAddonResponse, HassAddonInfoResponse } from '../types/index.js'; import {
import { APP_CONFIG } from '../config/app.config.js'; Tool,
AddonParams,
HassAddonResponse,
HassAddonInfoResponse,
} from "../types/index.js";
import { APP_CONFIG } from "../config/app.config.js";
export const addonTool: Tool = { export const addonTool: Tool = {
name: 'addon', name: "addon",
description: 'Manage Home Assistant add-ons', description: "Manage Home Assistant add-ons",
parameters: z.object({ parameters: z.object({
action: z.enum(['list', 'info', 'install', 'uninstall', 'start', 'stop', 'restart']) action: z
.describe('Action to perform with add-on'), .enum([
slug: z.string().optional().describe('Add-on slug (required for all actions except list)'), "list",
version: z.string().optional().describe('Version to install (only for install action)'), "info",
"install",
"uninstall",
"start",
"stop",
"restart",
])
.describe("Action to perform with add-on"),
slug: z
.string()
.optional()
.describe("Add-on slug (required for all actions except list)"),
version: z
.string()
.optional()
.describe("Version to install (only for install action)"),
}), }),
execute: async (params: AddonParams) => { execute: async (params: AddonParams) => {
try { try {
if (params.action === 'list') { if (params.action === "list") {
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/hassio/store`, { const response = await fetch(
`${APP_CONFIG.HASS_HOST}/api/hassio/store`,
{
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch add-ons: ${response.statusText}`); throw new Error(`Failed to fetch add-ons: ${response.statusText}`);
} }
const data = await response.json() as HassAddonResponse; const data = (await response.json()) as HassAddonResponse;
return { return {
success: true, success: true,
addons: data.data.addons.map((addon) => ({ addons: data.data.addons.map((addon) => ({
@@ -40,39 +63,39 @@ export const addonTool: Tool = {
}; };
} else { } else {
if (!params.slug) { if (!params.slug) {
throw new Error('Add-on slug is required for this action'); throw new Error("Add-on slug is required for this action");
} }
let endpoint = ''; let endpoint = "";
let method = 'GET'; let method = "GET";
const body: Record<string, any> = {}; const body: Record<string, any> = {};
switch (params.action) { switch (params.action) {
case 'info': case "info":
endpoint = `/api/hassio/addons/${params.slug}/info`; endpoint = `/api/hassio/addons/${params.slug}/info`;
break; break;
case 'install': case "install":
endpoint = `/api/hassio/addons/${params.slug}/install`; endpoint = `/api/hassio/addons/${params.slug}/install`;
method = 'POST'; method = "POST";
if (params.version) { if (params.version) {
body.version = params.version; body.version = params.version;
} }
break; break;
case 'uninstall': case "uninstall":
endpoint = `/api/hassio/addons/${params.slug}/uninstall`; endpoint = `/api/hassio/addons/${params.slug}/uninstall`;
method = 'POST'; method = "POST";
break; break;
case 'start': case "start":
endpoint = `/api/hassio/addons/${params.slug}/start`; endpoint = `/api/hassio/addons/${params.slug}/start`;
method = 'POST'; method = "POST";
break; break;
case 'stop': case "stop":
endpoint = `/api/hassio/addons/${params.slug}/stop`; endpoint = `/api/hassio/addons/${params.slug}/stop`;
method = 'POST'; method = "POST";
break; break;
case 'restart': case "restart":
endpoint = `/api/hassio/addons/${params.slug}/restart`; endpoint = `/api/hassio/addons/${params.slug}/restart`;
method = 'POST'; method = "POST";
break; break;
} }
@@ -80,16 +103,18 @@ export const addonTool: Tool = {
method, method,
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
...(Object.keys(body).length > 0 && { body: JSON.stringify(body) }), ...(Object.keys(body).length > 0 && { body: JSON.stringify(body) }),
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to ${params.action} add-on: ${response.statusText}`); throw new Error(
`Failed to ${params.action} add-on: ${response.statusText}`,
);
} }
const data = await response.json() as HassAddonInfoResponse; const data = (await response.json()) as HassAddonInfoResponse;
return { return {
success: true, success: true,
message: `Successfully ${params.action}ed add-on ${params.slug}`, message: `Successfully ${params.action}ed add-on ${params.slug}`,
@@ -99,7 +124,8 @@ export const addonTool: Tool = {
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred', message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
}, },

View File

@@ -1,95 +1,136 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool, AutomationConfigParams, AutomationConfig, AutomationResponse } from '../types/index.js'; import {
import { APP_CONFIG } from '../config/app.config.js'; Tool,
AutomationConfigParams,
AutomationConfig,
AutomationResponse,
} from "../types/index.js";
import { APP_CONFIG } from "../config/app.config.js";
export const automationConfigTool: Tool = { export const automationConfigTool: Tool = {
name: 'automation_config', name: "automation_config",
description: 'Advanced automation configuration and management', description: "Advanced automation configuration and management",
parameters: z.object({ parameters: z.object({
action: z.enum(['create', 'update', 'delete', 'duplicate']) action: z
.describe('Action to perform with automation config'), .enum(["create", "update", "delete", "duplicate"])
automation_id: z.string().optional() .describe("Action to perform with automation config"),
.describe('Automation ID (required for update, delete, and duplicate)'), automation_id: z
config: z.object({ .string()
alias: z.string().describe('Friendly name for the automation'), .optional()
description: z.string().optional().describe('Description of what the automation does'), .describe("Automation ID (required for update, delete, and duplicate)"),
mode: z.enum(['single', 'parallel', 'queued', 'restart']).optional() config: z
.describe('How multiple triggerings are handled'), .object({
trigger: z.array(z.any()).describe('List of triggers'), alias: z.string().describe("Friendly name for the automation"),
condition: z.array(z.any()).optional().describe('List of conditions'), description: z
action: z.array(z.any()).describe('List of actions'), .string()
}).optional().describe('Automation configuration (required for create and update)'), .optional()
.describe("Description of what the automation does"),
mode: z
.enum(["single", "parallel", "queued", "restart"])
.optional()
.describe("How multiple triggerings are handled"),
trigger: z.array(z.any()).describe("List of triggers"),
condition: z.array(z.any()).optional().describe("List of conditions"),
action: z.array(z.any()).describe("List of actions"),
})
.optional()
.describe("Automation configuration (required for create and update)"),
}), }),
execute: async (params: AutomationConfigParams) => { execute: async (params: AutomationConfigParams) => {
try { try {
switch (params.action) { switch (params.action) {
case 'create': { case "create": {
if (!params.config) { if (!params.config) {
throw new Error('Configuration is required for creating automation'); throw new Error(
"Configuration is required for creating automation",
);
} }
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/config/automation/config`, { const response = await fetch(
method: 'POST', `${APP_CONFIG.HASS_HOST}/api/config/automation/config`,
{
method: "POST",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify(params.config), body: JSON.stringify(params.config),
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to create automation: ${response.statusText}`); throw new Error(
`Failed to create automation: ${response.statusText}`,
);
} }
const responseData = await response.json() as { automation_id: string }; const responseData = (await response.json()) as {
automation_id: string;
};
return { return {
success: true, success: true,
message: 'Successfully created automation', message: "Successfully created automation",
automation_id: responseData.automation_id, automation_id: responseData.automation_id,
}; };
} }
case 'update': { case "update": {
if (!params.automation_id || !params.config) { if (!params.automation_id || !params.config) {
throw new Error('Automation ID and configuration are required for updating automation'); throw new Error(
"Automation ID and configuration are required for updating automation",
);
} }
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/config/automation/config/${params.automation_id}`, { const response = await fetch(
method: 'PUT', `${APP_CONFIG.HASS_HOST}/api/config/automation/config/${params.automation_id}`,
{
method: "PUT",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify(params.config), body: JSON.stringify(params.config),
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to update automation: ${response.statusText}`); throw new Error(
`Failed to update automation: ${response.statusText}`,
);
} }
const responseData = await response.json() as { automation_id: string }; const responseData = (await response.json()) as {
automation_id: string;
};
return { return {
success: true, success: true,
automation_id: responseData.automation_id, automation_id: responseData.automation_id,
message: 'Automation updated successfully' message: "Automation updated successfully",
}; };
} }
case 'delete': { case "delete": {
if (!params.automation_id) { if (!params.automation_id) {
throw new Error('Automation ID is required for deleting automation'); throw new Error(
"Automation ID is required for deleting automation",
);
} }
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/config/automation/config/${params.automation_id}`, { const response = await fetch(
method: 'DELETE', `${APP_CONFIG.HASS_HOST}/api/config/automation/config/${params.automation_id}`,
{
method: "DELETE",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to delete automation: ${response.statusText}`); throw new Error(
`Failed to delete automation: ${response.statusText}`,
);
} }
return { return {
@@ -98,41 +139,54 @@ export const automationConfigTool: Tool = {
}; };
} }
case 'duplicate': { case "duplicate": {
if (!params.automation_id) { if (!params.automation_id) {
throw new Error('Automation ID is required for duplicating automation'); throw new Error(
"Automation ID is required for duplicating automation",
);
} }
// First, get the existing automation config // First, get the existing automation config
const getResponse = await fetch(`${APP_CONFIG.HASS_HOST}/api/config/automation/config/${params.automation_id}`, { const getResponse = await fetch(
`${APP_CONFIG.HASS_HOST}/api/config/automation/config/${params.automation_id}`,
{
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); },
);
if (!getResponse.ok) { if (!getResponse.ok) {
throw new Error(`Failed to get automation config: ${getResponse.statusText}`); throw new Error(
`Failed to get automation config: ${getResponse.statusText}`,
);
} }
const config = await getResponse.json() as AutomationConfig; const config = (await getResponse.json()) as AutomationConfig;
config.alias = `${config.alias} (Copy)`; config.alias = `${config.alias} (Copy)`;
// Create new automation with modified config // Create new automation with modified config
const createResponse = await fetch(`${APP_CONFIG.HASS_HOST}/api/config/automation/config`, { const createResponse = await fetch(
method: 'POST', `${APP_CONFIG.HASS_HOST}/api/config/automation/config`,
{
method: "POST",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify(config), body: JSON.stringify(config),
}); },
);
if (!createResponse.ok) { if (!createResponse.ok) {
throw new Error(`Failed to create duplicate automation: ${createResponse.statusText}`); throw new Error(
`Failed to create duplicate automation: ${createResponse.statusText}`,
);
} }
const newAutomation = await createResponse.json() as AutomationResponse; const newAutomation =
(await createResponse.json()) as AutomationResponse;
return { return {
success: true, success: true,
message: `Successfully duplicated automation ${params.automation_id}`, message: `Successfully duplicated automation ${params.automation_id}`,
@@ -143,7 +197,8 @@ export const automationConfigTool: Tool = {
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred', message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
}, },

View File

@@ -1,62 +1,85 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool, AutomationParams, HassState, AutomationResponse } from '../types/index.js'; import {
import { APP_CONFIG } from '../config/app.config.js'; Tool,
AutomationParams,
HassState,
AutomationResponse,
} from "../types/index.js";
import { APP_CONFIG } from "../config/app.config.js";
export const automationTool: Tool = { export const automationTool: Tool = {
name: 'automation', name: "automation",
description: 'Manage Home Assistant automations', description: "Manage Home Assistant automations",
parameters: z.object({ parameters: z.object({
action: z.enum(['list', 'toggle', 'trigger']).describe('Action to perform with automation'), action: z
automation_id: z.string().optional().describe('Automation ID (required for toggle and trigger actions)'), .enum(["list", "toggle", "trigger"])
.describe("Action to perform with automation"),
automation_id: z
.string()
.optional()
.describe("Automation ID (required for toggle and trigger actions)"),
}), }),
execute: async (params: AutomationParams) => { execute: async (params: AutomationParams) => {
try { try {
if (params.action === 'list') { if (params.action === "list") {
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/states`, { const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/states`, {
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch automations: ${response.statusText}`); throw new Error(
`Failed to fetch automations: ${response.statusText}`,
);
} }
const states = (await response.json()) as HassState[]; const states = (await response.json()) as HassState[];
const automations = states.filter((state) => state.entity_id.startsWith('automation.')); const automations = states.filter((state) =>
state.entity_id.startsWith("automation."),
);
return { return {
success: true, success: true,
automations: automations.map((automation) => ({ automations: automations.map((automation) => ({
entity_id: automation.entity_id, entity_id: automation.entity_id,
name: automation.attributes.friendly_name || automation.entity_id.split('.')[1], name:
automation.attributes.friendly_name ||
automation.entity_id.split(".")[1],
state: automation.state, state: automation.state,
last_triggered: automation.attributes.last_triggered, last_triggered: automation.attributes.last_triggered,
})), })),
}; };
} else { } else {
if (!params.automation_id) { if (!params.automation_id) {
throw new Error('Automation ID is required for toggle and trigger actions'); throw new Error(
"Automation ID is required for toggle and trigger actions",
);
} }
const service = params.action === 'toggle' ? 'toggle' : 'trigger'; const service = params.action === "toggle" ? "toggle" : "trigger";
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/services/automation/${service}`, { const response = await fetch(
method: 'POST', `${APP_CONFIG.HASS_HOST}/api/services/automation/${service}`,
{
method: "POST",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
entity_id: params.automation_id, entity_id: params.automation_id,
}), }),
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to ${service} automation: ${response.statusText}`); throw new Error(
`Failed to ${service} automation: ${response.statusText}`,
);
} }
const responseData = await response.json() as AutomationResponse; const responseData = (await response.json()) as AutomationResponse;
return { return {
success: true, success: true,
message: `Successfully ${service}d automation ${params.automation_id}`, message: `Successfully ${service}d automation ${params.automation_id}`,
@@ -66,7 +89,8 @@ export const automationTool: Tool = {
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred', message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
}, },

View File

@@ -1,51 +1,94 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool, CommandParams } from '../types/index.js'; import { Tool, CommandParams } from "../types/index.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
import { DomainSchema } from '../schemas.js'; import { DomainSchema } from "../schemas.js";
// Define command constants // Define command constants
const commonCommands = ['turn_on', 'turn_off', 'toggle'] as const; const commonCommands = ["turn_on", "turn_off", "toggle"] as const;
const coverCommands = [...commonCommands, 'open', 'close', 'stop', 'set_position', 'set_tilt_position'] as const; const coverCommands = [
const climateCommands = [...commonCommands, 'set_temperature', 'set_hvac_mode', 'set_fan_mode', 'set_humidity'] as const; ...commonCommands,
"open",
"close",
"stop",
"set_position",
"set_tilt_position",
] as const;
const climateCommands = [
...commonCommands,
"set_temperature",
"set_hvac_mode",
"set_fan_mode",
"set_humidity",
] as const;
export const controlTool: Tool = { export const controlTool: Tool = {
name: 'control', name: "control",
description: 'Control Home Assistant devices and services', description: "Control Home Assistant devices and services",
parameters: z.object({ parameters: z.object({
command: z.enum([...commonCommands, ...coverCommands, ...climateCommands]) command: z
.describe('The command to execute'), .enum([...commonCommands, ...coverCommands, ...climateCommands])
entity_id: z.string().describe('The entity ID to control'), .describe("The command to execute"),
entity_id: z.string().describe("The entity ID to control"),
// Common parameters // Common parameters
state: z.string().optional().describe('The desired state for the entity'), state: z.string().optional().describe("The desired state for the entity"),
// Light parameters // Light parameters
brightness: z.number().min(0).max(255).optional() brightness: z
.describe('Brightness level for lights (0-255)'), .number()
color_temp: z.number().optional() .min(0)
.describe('Color temperature for lights'), .max(255)
rgb_color: z.tuple([z.number(), z.number(), z.number()]).optional() .optional()
.describe('RGB color values'), .describe("Brightness level for lights (0-255)"),
color_temp: z.number().optional().describe("Color temperature for lights"),
rgb_color: z
.tuple([z.number(), z.number(), z.number()])
.optional()
.describe("RGB color values"),
// Cover parameters // Cover parameters
position: z.number().min(0).max(100).optional() position: z
.describe('Position for covers (0-100)'), .number()
tilt_position: z.number().min(0).max(100).optional() .min(0)
.describe('Tilt position for covers (0-100)'), .max(100)
.optional()
.describe("Position for covers (0-100)"),
tilt_position: z
.number()
.min(0)
.max(100)
.optional()
.describe("Tilt position for covers (0-100)"),
// Climate parameters // Climate parameters
temperature: z.number().optional() temperature: z
.describe('Target temperature for climate devices'), .number()
target_temp_high: z.number().optional() .optional()
.describe('Target high temperature for climate devices'), .describe("Target temperature for climate devices"),
target_temp_low: z.number().optional() target_temp_high: z
.describe('Target low temperature for climate devices'), .number()
hvac_mode: z.enum(['off', 'heat', 'cool', 'heat_cool', 'auto', 'dry', 'fan_only']).optional() .optional()
.describe('HVAC mode for climate devices'), .describe("Target high temperature for climate devices"),
fan_mode: z.enum(['auto', 'low', 'medium', 'high']).optional() target_temp_low: z
.describe('Fan mode for climate devices'), .number()
humidity: z.number().min(0).max(100).optional() .optional()
.describe('Target humidity for climate devices') .describe("Target low temperature for climate devices"),
hvac_mode: z
.enum(["off", "heat", "cool", "heat_cool", "auto", "dry", "fan_only"])
.optional()
.describe("HVAC mode for climate devices"),
fan_mode: z
.enum(["auto", "low", "medium", "high"])
.optional()
.describe("Fan mode for climate devices"),
humidity: z
.number()
.min(0)
.max(100)
.optional()
.describe("Target humidity for climate devices"),
}), }),
execute: async (params: CommandParams) => { execute: async (params: CommandParams) => {
try { try {
const domain = params.entity_id.split('.')[0] as keyof typeof DomainSchema.Values; const domain = params.entity_id.split(
".",
)[0] as keyof typeof DomainSchema.Values;
if (!Object.values(DomainSchema.Values).includes(domain)) { if (!Object.values(DomainSchema.Values).includes(domain)) {
throw new Error(`Unsupported domain: ${domain}`); throw new Error(`Unsupported domain: ${domain}`);
@@ -53,12 +96,12 @@ export const controlTool: Tool = {
const service = params.command; const service = params.command;
const serviceData: Record<string, any> = { const serviceData: Record<string, any> = {
entity_id: params.entity_id entity_id: params.entity_id,
}; };
// Handle domain-specific parameters // Handle domain-specific parameters
switch (domain) { switch (domain) {
case 'light': case "light":
if (params.brightness !== undefined) { if (params.brightness !== undefined) {
serviceData.brightness = params.brightness; serviceData.brightness = params.brightness;
} }
@@ -70,17 +113,20 @@ export const controlTool: Tool = {
} }
break; break;
case 'cover': case "cover":
if (service === 'set_position' && params.position !== undefined) { if (service === "set_position" && params.position !== undefined) {
serviceData.position = params.position; serviceData.position = params.position;
} }
if (service === 'set_tilt_position' && params.tilt_position !== undefined) { if (
service === "set_tilt_position" &&
params.tilt_position !== undefined
) {
serviceData.tilt_position = params.tilt_position; serviceData.tilt_position = params.tilt_position;
} }
break; break;
case 'climate': case "climate":
if (service === 'set_temperature') { if (service === "set_temperature") {
if (params.temperature !== undefined) { if (params.temperature !== undefined) {
serviceData.temperature = params.temperature; serviceData.temperature = params.temperature;
} }
@@ -91,19 +137,19 @@ export const controlTool: Tool = {
serviceData.target_temp_low = params.target_temp_low; serviceData.target_temp_low = params.target_temp_low;
} }
} }
if (service === 'set_hvac_mode' && params.hvac_mode !== undefined) { if (service === "set_hvac_mode" && params.hvac_mode !== undefined) {
serviceData.hvac_mode = params.hvac_mode; serviceData.hvac_mode = params.hvac_mode;
} }
if (service === 'set_fan_mode' && params.fan_mode !== undefined) { if (service === "set_fan_mode" && params.fan_mode !== undefined) {
serviceData.fan_mode = params.fan_mode; serviceData.fan_mode = params.fan_mode;
} }
if (service === 'set_humidity' && params.humidity !== undefined) { if (service === "set_humidity" && params.humidity !== undefined) {
serviceData.humidity = params.humidity; serviceData.humidity = params.humidity;
} }
break; break;
case 'switch': case "switch":
case 'contact': case "contact":
// These domains only support basic operations (turn_on, turn_off, toggle) // These domains only support basic operations (turn_on, turn_off, toggle)
break; break;
@@ -112,28 +158,34 @@ export const controlTool: Tool = {
} }
// Call Home Assistant service // Call Home Assistant service
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/services/${domain}/${service}`, { const response = await fetch(
method: 'POST', `${APP_CONFIG.HASS_HOST}/api/services/${domain}/${service}`,
{
method: "POST",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify(serviceData), body: JSON.stringify(serviceData),
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to execute ${service} for ${params.entity_id}: ${response.statusText}`); throw new Error(
`Failed to execute ${service} for ${params.entity_id}: ${response.statusText}`,
);
} }
return { return {
success: true, success: true,
message: `Successfully executed ${service} for ${params.entity_id}` message: `Successfully executed ${service} for ${params.entity_id}`,
}; };
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
} },
}; };

View File

@@ -1,21 +1,35 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool, HistoryParams } from '../types/index.js'; import { Tool, HistoryParams } from "../types/index.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
export const historyTool: Tool = { export const historyTool: Tool = {
name: 'get_history', name: "get_history",
description: 'Get state history for Home Assistant entities', description: "Get state history for Home Assistant entities",
parameters: z.object({ parameters: z.object({
entity_id: z.string().describe('The entity ID to get history for'), entity_id: z.string().describe("The entity ID to get history for"),
start_time: z.string().optional().describe('Start time in ISO format. Defaults to 24 hours ago'), start_time: z
end_time: z.string().optional().describe('End time in ISO format. Defaults to now'), .string()
minimal_response: z.boolean().optional().describe('Return minimal response to reduce data size'), .optional()
significant_changes_only: z.boolean().optional().describe('Only return significant state changes'), .describe("Start time in ISO format. Defaults to 24 hours ago"),
end_time: z
.string()
.optional()
.describe("End time in ISO format. Defaults to now"),
minimal_response: z
.boolean()
.optional()
.describe("Return minimal response to reduce data size"),
significant_changes_only: z
.boolean()
.optional()
.describe("Only return significant state changes"),
}), }),
execute: async (params: HistoryParams) => { execute: async (params: HistoryParams) => {
try { try {
const now = new Date(); const now = new Date();
const startTime = params.start_time ? new Date(params.start_time) : new Date(now.getTime() - 24 * 60 * 60 * 1000); const startTime = params.start_time
? new Date(params.start_time)
: new Date(now.getTime() - 24 * 60 * 60 * 1000);
const endTime = params.end_time ? new Date(params.end_time) : now; const endTime = params.end_time ? new Date(params.end_time) : now;
// Build query parameters // Build query parameters
@@ -27,12 +41,15 @@ export const historyTool: Tool = {
end_time: endTime.toISOString(), end_time: endTime.toISOString(),
}); });
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/history/period/${startTime.toISOString()}?${queryParams.toString()}`, { const response = await fetch(
`${APP_CONFIG.HASS_HOST}/api/history/period/${startTime.toISOString()}?${queryParams.toString()}`,
{
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch history: ${response.statusText}`); throw new Error(`Failed to fetch history: ${response.statusText}`);
@@ -46,7 +63,8 @@ export const historyTool: Tool = {
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred', message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
}, },

View File

@@ -1,28 +1,28 @@
import { Tool } from '../types/index.js'; import { Tool } from "../types/index.js";
import { listDevicesTool } from './list-devices.tool.js'; import { listDevicesTool } from "./list-devices.tool.js";
import { controlTool } from './control.tool.js'; import { controlTool } from "./control.tool.js";
import { historyTool } from './history.tool.js'; import { historyTool } from "./history.tool.js";
import { sceneTool } from './scene.tool.js'; import { sceneTool } from "./scene.tool.js";
import { notifyTool } from './notify.tool.js'; import { notifyTool } from "./notify.tool.js";
import { automationTool } from './automation.tool.js'; import { automationTool } from "./automation.tool.js";
import { addonTool } from './addon.tool.js'; import { addonTool } from "./addon.tool.js";
import { packageTool } from './package.tool.js'; import { packageTool } from "./package.tool.js";
import { automationConfigTool } from './automation-config.tool.js'; import { automationConfigTool } from "./automation-config.tool.js";
import { subscribeEventsTool } from './subscribe-events.tool.js'; import { subscribeEventsTool } from "./subscribe-events.tool.js";
import { getSSEStatsTool } from './sse-stats.tool.js'; import { getSSEStatsTool } from "./sse-stats.tool.js";
// Tool category types // Tool category types
export enum ToolCategory { export enum ToolCategory {
DEVICE = 'device', DEVICE = "device",
SYSTEM = 'system', SYSTEM = "system",
AUTOMATION = 'automation' AUTOMATION = "automation",
} }
// Tool priority levels // Tool priority levels
export enum ToolPriority { export enum ToolPriority {
HIGH = 'high', HIGH = "high",
MEDIUM = 'medium', MEDIUM = "medium",
LOW = 'low' LOW = "low",
} }
interface ToolMetadata { interface ToolMetadata {
@@ -47,12 +47,12 @@ export const tools: Tool[] = [
packageTool, packageTool,
automationConfigTool, automationConfigTool,
subscribeEventsTool, subscribeEventsTool,
getSSEStatsTool getSSEStatsTool,
]; ];
// Function to get a tool by name // Function to get a tool by name
export function getToolByName(name: string): Tool | undefined { export function getToolByName(name: string): Tool | undefined {
return tools.find(tool => tool.name === name); return tools.find((tool) => tool.name === name);
} }
// Function to get all tools // Function to get all tools
@@ -72,5 +72,5 @@ export {
packageTool, packageTool,
automationConfigTool, automationConfigTool,
subscribeEventsTool, subscribeEventsTool,
getSSEStatsTool getSSEStatsTool,
}; };

View File

@@ -1,18 +1,18 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool } from '../types/index.js'; import { Tool } from "../types/index.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
import { HassState } from '../types/index.js'; import { HassState } from "../types/index.js";
export const listDevicesTool: Tool = { export const listDevicesTool: Tool = {
name: 'list_devices', name: "list_devices",
description: 'List all available Home Assistant devices', description: "List all available Home Assistant devices",
parameters: z.object({}).describe('No parameters required'), parameters: z.object({}).describe("No parameters required"),
execute: async () => { execute: async () => {
try { try {
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/states`, { const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/states`, {
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); });
@@ -20,12 +20,12 @@ export const listDevicesTool: Tool = {
throw new Error(`Failed to fetch devices: ${response.statusText}`); throw new Error(`Failed to fetch devices: ${response.statusText}`);
} }
const states = await response.json() as HassState[]; const states = (await response.json()) as HassState[];
const devices: Record<string, HassState[]> = {}; const devices: Record<string, HassState[]> = {};
// Group devices by domain // Group devices by domain
states.forEach(state => { states.forEach((state) => {
const [domain] = state.entity_id.split('.'); const [domain] = state.entity_id.split(".");
if (!devices[domain]) { if (!devices[domain]) {
devices[domain] = []; devices[domain] = [];
} }
@@ -34,13 +34,14 @@ export const listDevicesTool: Tool = {
return { return {
success: true, success: true,
devices devices,
}; };
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
} },
}; };

View File

@@ -1,33 +1,41 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool, NotifyParams } from '../types/index.js'; import { Tool, NotifyParams } from "../types/index.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
export const notifyTool: Tool = { export const notifyTool: Tool = {
name: 'notify', name: "notify",
description: 'Send notifications through Home Assistant', description: "Send notifications through Home Assistant",
parameters: z.object({ parameters: z.object({
message: z.string().describe('The notification message'), message: z.string().describe("The notification message"),
title: z.string().optional().describe('The notification title'), title: z.string().optional().describe("The notification title"),
target: z.string().optional().describe('Specific notification target (e.g., mobile_app_phone)'), target: z
data: z.record(z.any()).optional().describe('Additional notification data'), .string()
.optional()
.describe("Specific notification target (e.g., mobile_app_phone)"),
data: z.record(z.any()).optional().describe("Additional notification data"),
}), }),
execute: async (params: NotifyParams) => { execute: async (params: NotifyParams) => {
try { try {
const service = params.target ? `notify.${params.target}` : 'notify.notify'; const service = params.target
const [domain, service_name] = service.split('.'); ? `notify.${params.target}`
: "notify.notify";
const [domain, service_name] = service.split(".");
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/services/${domain}/${service_name}`, { const response = await fetch(
method: 'POST', `${APP_CONFIG.HASS_HOST}/api/services/${domain}/${service_name}`,
{
method: "POST",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
message: params.message, message: params.message,
title: params.title, title: params.title,
data: params.data, data: params.data,
}), }),
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to send notification: ${response.statusText}`); throw new Error(`Failed to send notification: ${response.statusText}`);
@@ -35,12 +43,13 @@ export const notifyTool: Tool = {
return { return {
success: true, success: true,
message: 'Notification sent successfully', message: "Notification sent successfully",
}; };
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred', message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
}, },

View File

@@ -1,76 +1,93 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool, PackageParams, HacsResponse } from '../types/index.js'; import { Tool, PackageParams, HacsResponse } from "../types/index.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
export const packageTool: Tool = { export const packageTool: Tool = {
name: 'package', name: "package",
description: 'Manage HACS packages and custom components', description: "Manage HACS packages and custom components",
parameters: z.object({ parameters: z.object({
action: z.enum(['list', 'install', 'uninstall', 'update']) action: z
.describe('Action to perform with package'), .enum(["list", "install", "uninstall", "update"])
category: z.enum(['integration', 'plugin', 'theme', 'python_script', 'appdaemon', 'netdaemon']) .describe("Action to perform with package"),
.describe('Package category'), category: z
repository: z.string().optional().describe('Repository URL or name (required for install)'), .enum([
version: z.string().optional().describe('Version to install'), "integration",
"plugin",
"theme",
"python_script",
"appdaemon",
"netdaemon",
])
.describe("Package category"),
repository: z
.string()
.optional()
.describe("Repository URL or name (required for install)"),
version: z.string().optional().describe("Version to install"),
}), }),
execute: async (params: PackageParams) => { execute: async (params: PackageParams) => {
try { try {
const hacsBase = `${APP_CONFIG.HASS_HOST}/api/hacs`; const hacsBase = `${APP_CONFIG.HASS_HOST}/api/hacs`;
if (params.action === 'list') { if (params.action === "list") {
const response = await fetch(`${hacsBase}/repositories?category=${params.category}`, { const response = await fetch(
`${hacsBase}/repositories?category=${params.category}`,
{
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch packages: ${response.statusText}`); throw new Error(`Failed to fetch packages: ${response.statusText}`);
} }
const data = await response.json() as HacsResponse; const data = (await response.json()) as HacsResponse;
return { return {
success: true, success: true,
packages: data.repositories, packages: data.repositories,
}; };
} else { } else {
if (!params.repository) { if (!params.repository) {
throw new Error('Repository is required for this action'); throw new Error("Repository is required for this action");
} }
let endpoint = ''; let endpoint = "";
const body: Record<string, any> = { const body: Record<string, any> = {
category: params.category, category: params.category,
repository: params.repository, repository: params.repository,
}; };
switch (params.action) { switch (params.action) {
case 'install': case "install":
endpoint = '/repository/install'; endpoint = "/repository/install";
if (params.version) { if (params.version) {
body.version = params.version; body.version = params.version;
} }
break; break;
case 'uninstall': case "uninstall":
endpoint = '/repository/uninstall'; endpoint = "/repository/uninstall";
break; break;
case 'update': case "update":
endpoint = '/repository/update'; endpoint = "/repository/update";
break; break;
} }
const response = await fetch(`${hacsBase}${endpoint}`, { const response = await fetch(`${hacsBase}${endpoint}`, {
method: 'POST', method: "POST",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to ${params.action} package: ${response.statusText}`); throw new Error(
`Failed to ${params.action} package: ${response.statusText}`,
);
} }
return { return {
@@ -81,7 +98,8 @@ export const packageTool: Tool = {
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred', message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
}, },

View File

@@ -1,21 +1,26 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool, SceneParams, HassState } from '../types/index.js'; import { Tool, SceneParams, HassState } from "../types/index.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
export const sceneTool: Tool = { export const sceneTool: Tool = {
name: 'scene', name: "scene",
description: 'Manage and activate Home Assistant scenes', description: "Manage and activate Home Assistant scenes",
parameters: z.object({ parameters: z.object({
action: z.enum(['list', 'activate']).describe('Action to perform with scenes'), action: z
scene_id: z.string().optional().describe('Scene ID to activate (required for activate action)'), .enum(["list", "activate"])
.describe("Action to perform with scenes"),
scene_id: z
.string()
.optional()
.describe("Scene ID to activate (required for activate action)"),
}), }),
execute: async (params: SceneParams) => { execute: async (params: SceneParams) => {
try { try {
if (params.action === 'list') { if (params.action === "list") {
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/states`, { const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/states`, {
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); });
@@ -24,31 +29,37 @@ export const sceneTool: Tool = {
} }
const states = (await response.json()) as HassState[]; const states = (await response.json()) as HassState[];
const scenes = states.filter((state) => state.entity_id.startsWith('scene.')); const scenes = states.filter((state) =>
state.entity_id.startsWith("scene."),
);
return { return {
success: true, success: true,
scenes: scenes.map((scene) => ({ scenes: scenes.map((scene) => ({
entity_id: scene.entity_id, entity_id: scene.entity_id,
name: scene.attributes.friendly_name || scene.entity_id.split('.')[1], name:
scene.attributes.friendly_name || scene.entity_id.split(".")[1],
description: scene.attributes.description, description: scene.attributes.description,
})), })),
}; };
} else if (params.action === 'activate') { } else if (params.action === "activate") {
if (!params.scene_id) { if (!params.scene_id) {
throw new Error('Scene ID is required for activate action'); throw new Error("Scene ID is required for activate action");
} }
const response = await fetch(`${APP_CONFIG.HASS_HOST}/api/services/scene/turn_on`, { const response = await fetch(
method: 'POST', `${APP_CONFIG.HASS_HOST}/api/services/scene/turn_on`,
{
method: "POST",
headers: { headers: {
Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`, Authorization: `Bearer ${APP_CONFIG.HASS_TOKEN}`,
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
entity_id: params.scene_id, entity_id: params.scene_id,
}), }),
}); },
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to activate scene: ${response.statusText}`); throw new Error(`Failed to activate scene: ${response.statusText}`);
@@ -60,11 +71,12 @@ export const sceneTool: Tool = {
}; };
} }
throw new Error('Invalid action specified'); throw new Error("Invalid action specified");
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred', message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
}, },

View File

@@ -1,33 +1,34 @@
import { z } from 'zod'; import { z } from "zod";
import { Tool } from '../types/index.js'; import { Tool } from "../types/index.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
import { sseManager } from '../sse/index.js'; import { sseManager } from "../sse/index.js";
export const getSSEStatsTool: Tool = { export const getSSEStatsTool: Tool = {
name: 'get_sse_stats', name: "get_sse_stats",
description: 'Get SSE connection statistics', description: "Get SSE connection statistics",
parameters: z.object({ parameters: z.object({
token: z.string().describe('Authentication token (required)') token: z.string().describe("Authentication token (required)"),
}), }),
execute: async (params: { token: string }) => { execute: async (params: { token: string }) => {
try { try {
if (params.token !== APP_CONFIG.HASS_TOKEN) { if (params.token !== APP_CONFIG.HASS_TOKEN) {
return { return {
success: false, success: false,
message: 'Authentication failed' message: "Authentication failed",
}; };
} }
const stats = await sseManager.getStatistics(); const stats = await sseManager.getStatistics();
return { return {
success: true, success: true,
statistics: stats statistics: stats,
}; };
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
message: error instanceof Error ? error.message : 'Unknown error occurred' message:
error instanceof Error ? error.message : "Unknown error occurred",
}; };
} }
} },
}; };

View File

@@ -1,25 +1,35 @@
import { z } from 'zod'; import { z } from "zod";
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from "uuid";
import { Tool, SSEParams } from '../types/index.js'; import { Tool, SSEParams } from "../types/index.js";
import { sseManager } from '../sse/index.js'; import { sseManager } from "../sse/index.js";
export const subscribeEventsTool: Tool = { export const subscribeEventsTool: Tool = {
name: 'subscribe_events', name: "subscribe_events",
description: 'Subscribe to Home Assistant events via Server-Sent Events (SSE)', description:
"Subscribe to Home Assistant events via Server-Sent Events (SSE)",
parameters: z.object({ parameters: z.object({
token: z.string().describe('Authentication token (required)'), token: z.string().describe("Authentication token (required)"),
events: z.array(z.string()).optional().describe('List of event types to subscribe to'), events: z
entity_id: z.string().optional().describe('Specific entity ID to monitor for state changes'), .array(z.string())
domain: z.string().optional().describe('Domain to monitor (e.g., "light", "switch", etc.)'), .optional()
.describe("List of event types to subscribe to"),
entity_id: z
.string()
.optional()
.describe("Specific entity ID to monitor for state changes"),
domain: z
.string()
.optional()
.describe('Domain to monitor (e.g., "light", "switch", etc.)'),
}), }),
execute: async (params: SSEParams) => { execute: async (params: SSEParams) => {
const clientId = uuidv4(); const clientId = uuidv4();
// Set up SSE headers // Set up SSE headers
const responseHeaders = { const responseHeaders = {
'Content-Type': 'text/event-stream', "Content-Type": "text/event-stream",
'Cache-Control': 'no-cache', "Cache-Control": "no-cache",
'Connection': 'keep-alive', Connection: "keep-alive",
}; };
// Create SSE client // Create SSE client
@@ -29,9 +39,9 @@ export const subscribeEventsTool: Tool = {
return { return {
headers: responseHeaders, headers: responseHeaders,
body: `data: ${data}\n\n`, body: `data: ${data}\n\n`,
keepAlive: true keepAlive: true,
}; };
} },
}; };
// Add client to SSE manager with authentication // Add client to SSE manager with authentication
@@ -40,7 +50,9 @@ export const subscribeEventsTool: Tool = {
if (!sseClient || !sseClient.authenticated) { if (!sseClient || !sseClient.authenticated) {
return { return {
success: false, success: false,
message: sseClient ? 'Authentication failed' : 'Maximum client limit reached' message: sseClient
? "Authentication failed"
: "Maximum client limit reached",
}; };
} }
@@ -54,7 +66,10 @@ export const subscribeEventsTool: Tool = {
// Subscribe to specific entity if provided // Subscribe to specific entity if provided
if (params.entity_id) { if (params.entity_id) {
console.log(`Client ${clientId} subscribing to entity:`, params.entity_id); console.log(
`Client ${clientId} subscribing to entity:`,
params.entity_id,
);
sseManager.subscribeToEntity(clientId, params.entity_id); sseManager.subscribeToEntity(clientId, params.entity_id);
} }
@@ -67,18 +82,18 @@ export const subscribeEventsTool: Tool = {
return { return {
headers: responseHeaders, headers: responseHeaders,
body: `data: ${JSON.stringify({ body: `data: ${JSON.stringify({
type: 'connection', type: "connection",
status: 'connected', status: "connected",
id: clientId, id: clientId,
authenticated: true, authenticated: true,
subscriptions: { subscriptions: {
events: params.events || [], events: params.events || [],
entities: params.entity_id ? [params.entity_id] : [], entities: params.entity_id ? [params.entity_id] : [],
domains: params.domain ? [params.domain] : [] domains: params.domain ? [params.domain] : [],
}, },
timestamp: new Date().toISOString() timestamp: new Date().toISOString(),
})}\n\n`, })}\n\n`,
keepAlive: true keepAlive: true,
}; };
} },
}; };

50
src/types/bun.d.ts vendored Normal file
View File

@@ -0,0 +1,50 @@
declare module "bun:test" {
export interface Mock<T extends (...args: any[]) => any> {
(...args: Parameters<T>): ReturnType<T>;
mock: {
calls: Array<{ args: Parameters<T>; returned: ReturnType<T> }>;
results: Array<{ type: "return" | "throw"; value: any }>;
instances: any[];
lastCall: { args: Parameters<T>; returned: ReturnType<T> } | undefined;
};
mockImplementation(fn: T): this;
mockReturnValue(value: ReturnType<T>): this;
mockResolvedValue<U>(value: U): Mock<() => Promise<U>>;
mockRejectedValue(value: any): Mock<() => Promise<never>>;
mockReset(): void;
}
export function mock<T extends (...args: any[]) => any>(
implementation?: T,
): Mock<T>;
export function describe(name: string, fn: () => void): void;
export function it(name: string, fn: () => void | Promise<void>): void;
export function test(name: string, fn: () => void | Promise<void>): void;
export function expect(actual: any): {
toBe(expected: any): void;
toEqual(expected: any): void;
toBeDefined(): void;
toBeUndefined(): void;
toBeNull(): void;
toBeTruthy(): void;
toBeFalsy(): void;
toBeGreaterThan(expected: number): void;
toBeLessThan(expected: number): void;
toContain(expected: any): void;
toHaveLength(expected: number): void;
toHaveBeenCalled(): void;
toHaveBeenCalledTimes(expected: number): void;
toHaveBeenCalledWith(...args: any[]): void;
toThrow(expected?: string | RegExp): void;
resolves: any;
rejects: any;
};
export function beforeAll(fn: () => void | Promise<void>): void;
export function afterAll(fn: () => void | Promise<void>): void;
export function beforeEach(fn: () => void | Promise<void>): void;
export function afterEach(fn: () => void | Promise<void>): void;
export const mock: {
resetAll(): void;
};
}

6
src/types/hass.d.ts vendored
View File

@@ -30,17 +30,17 @@ declare namespace HomeAssistant {
} }
interface AuthMessage extends WebsocketMessage { interface AuthMessage extends WebsocketMessage {
type: 'auth'; type: "auth";
access_token: string; access_token: string;
} }
interface SubscribeEventsMessage extends WebsocketMessage { interface SubscribeEventsMessage extends WebsocketMessage {
type: 'subscribe_events'; type: "subscribe_events";
event_type?: string; event_type?: string;
} }
interface StateChangedEvent { interface StateChangedEvent {
event_type: 'state_changed'; event_type: "state_changed";
data: { data: {
entity_id: string; entity_id: string;
new_state: Entity | null; new_state: Entity | null;

View File

@@ -1,11 +1,11 @@
export interface AuthMessage { export interface AuthMessage {
type: 'auth'; type: "auth";
access_token: string; access_token: string;
} }
export interface ResultMessage { export interface ResultMessage {
id: number; id: number;
type: 'result'; type: "result";
success: boolean; success: boolean;
result?: any; result?: any;
} }
@@ -41,7 +41,7 @@ export interface Entity {
} }
export interface StateChangedEvent extends Event { export interface StateChangedEvent extends Event {
event_type: 'state_changed'; event_type: "state_changed";
data: { data: {
entity_id: string; entity_id: string;
new_state: Entity | null; new_state: Entity | null;
@@ -83,4 +83,3 @@ export interface HassEvent {
user_id?: string; user_id?: string;
}; };
} }

View File

@@ -1,4 +1,4 @@
import { z } from 'zod'; import { z } from "zod";
/** /**
* Interface for a tool that can be executed by the MCP * Interface for a tool that can be executed by the MCP
@@ -186,7 +186,7 @@ export interface AutomationConfig {
/** Automation description */ /** Automation description */
description?: string; description?: string;
/** How multiple triggers are handled */ /** How multiple triggers are handled */
mode?: 'single' | 'parallel' | 'queued' | 'restart'; mode?: "single" | "parallel" | "queued" | "restart";
/** List of triggers */ /** List of triggers */
trigger: any[]; trigger: any[];
/** List of conditions */ /** List of conditions */
@@ -251,7 +251,7 @@ export interface HistoryParams {
*/ */
export interface SceneParams { export interface SceneParams {
/** Action to perform */ /** Action to perform */
action: 'list' | 'activate'; action: "list" | "activate";
/** Scene ID for activation */ /** Scene ID for activation */
scene_id?: string; scene_id?: string;
} }
@@ -277,7 +277,7 @@ export interface NotifyParams {
*/ */
export interface AutomationParams { export interface AutomationParams {
/** Action to perform */ /** Action to perform */
action: 'list' | 'toggle' | 'trigger'; action: "list" | "toggle" | "trigger";
/** Automation ID */ /** Automation ID */
automation_id?: string; automation_id?: string;
} }
@@ -288,7 +288,14 @@ export interface AutomationParams {
*/ */
export interface AddonParams { export interface AddonParams {
/** Action to perform */ /** Action to perform */
action: 'list' | 'info' | 'install' | 'uninstall' | 'start' | 'stop' | 'restart'; action:
| "list"
| "info"
| "install"
| "uninstall"
| "start"
| "stop"
| "restart";
/** Add-on slug */ /** Add-on slug */
slug?: string; slug?: string;
/** Version to install */ /** Version to install */
@@ -301,9 +308,15 @@ export interface AddonParams {
*/ */
export interface PackageParams { export interface PackageParams {
/** Action to perform */ /** Action to perform */
action: 'list' | 'install' | 'uninstall' | 'update'; action: "list" | "install" | "uninstall" | "update";
/** Package category */ /** Package category */
category: 'integration' | 'plugin' | 'theme' | 'python_script' | 'appdaemon' | 'netdaemon'; category:
| "integration"
| "plugin"
| "theme"
| "python_script"
| "appdaemon"
| "netdaemon";
/** Repository URL or name */ /** Repository URL or name */
repository?: string; repository?: string;
/** Version to install */ /** Version to install */
@@ -316,7 +329,7 @@ export interface PackageParams {
*/ */
export interface AutomationConfigParams { export interface AutomationConfigParams {
/** Action to perform */ /** Action to perform */
action: 'create' | 'update' | 'delete' | 'duplicate'; action: "create" | "update" | "delete" | "duplicate";
/** Automation ID */ /** Automation ID */
automation_id?: string; automation_id?: string;
/** Automation configuration */ /** Automation configuration */
@@ -326,7 +339,7 @@ export interface AutomationConfigParams {
/** Automation description */ /** Automation description */
description?: string; description?: string;
/** How multiple triggers are handled */ /** How multiple triggers are handled */
mode?: 'single' | 'parallel' | 'queued' | 'restart'; mode?: "single" | "parallel" | "queued" | "restart";
/** List of triggers */ /** List of triggers */
trigger: any[]; trigger: any[];
/** List of conditions */ /** List of conditions */

View File

@@ -7,14 +7,14 @@
* @module log-rotation * @module log-rotation
*/ */
import fs from 'fs/promises'; import fs from "fs/promises";
import path from 'path'; import path from "path";
import { glob } from 'glob'; import { glob } from "glob";
import { logger } from './logger.js'; import { logger } from "./logger.js";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
import { unlink } from 'fs/promises'; import { unlink } from "fs/promises";
import { join } from 'path'; import { join } from "path";
import { promisify } from 'util'; import { promisify } from "util";
const globPromise = promisify(glob); const globPromise = promisify(glob);
@@ -73,7 +73,7 @@ const parseDuration = (duration: string): number => {
*/ */
const getLogFiles = async (): Promise<LogFileInfo[]> => { const getLogFiles = async (): Promise<LogFileInfo[]> => {
const logDir = APP_CONFIG.LOGGING.DIR; const logDir = APP_CONFIG.LOGGING.DIR;
const files: string[] = await glob('*.log*', { cwd: logDir }); const files: string[] = await glob("*.log*", { cwd: logDir });
const fileInfos: LogFileInfo[] = []; const fileInfos: LogFileInfo[] = [];
for (const file of files) { for (const file of files) {
@@ -97,9 +97,12 @@ const getLogFiles = async (): Promise<LogFileInfo[]> => {
/** /**
* Clean up old log files * Clean up old log files
*/ */
export async function cleanupOldLogs(logDir: string, maxDays: number): Promise<void> { export async function cleanupOldLogs(
logDir: string,
maxDays: number,
): Promise<void> {
try { try {
const files: string[] = await glob('*.log*', { cwd: logDir }); const files: string[] = await glob("*.log*", { cwd: logDir });
const now = Date.now(); const now = Date.now();
const maxAge = maxDays * 24 * 60 * 60 * 1000; const maxAge = maxDays * 24 * 60 * 60 * 1000;
@@ -115,7 +118,7 @@ export async function cleanupOldLogs(logDir: string, maxDays: number): Promise<v
} }
} }
} catch (error) { } catch (error) {
logger.error('Error cleaning up old logs:', error); logger.error("Error cleaning up old logs:", error);
} }
} }
@@ -128,15 +131,15 @@ const checkLogSize = async (): Promise<void> => {
const files = await getLogFiles(); const files = await getLogFiles();
for (const file of files) { for (const file of files) {
if (file.size > maxSize && !file.filename.endsWith('.gz')) { if (file.size > maxSize && !file.filename.endsWith(".gz")) {
// Current log file is handled by winston-daily-rotate-file // Current log file is handled by winston-daily-rotate-file
if (!file.filename.includes(new Date().toISOString().split('T')[0])) { if (!file.filename.includes(new Date().toISOString().split("T")[0])) {
logger.debug(`Log file exceeds max size: ${file.filename}`); logger.debug(`Log file exceeds max size: ${file.filename}`);
} }
} }
} }
} catch (error) { } catch (error) {
logger.error('Error checking log sizes:', error); logger.error("Error checking log sizes:", error);
} }
}; };
@@ -146,30 +149,40 @@ const checkLogSize = async (): Promise<void> => {
*/ */
export const initLogRotation = (): void => { export const initLogRotation = (): void => {
// Check log sizes every hour // Check log sizes every hour
setInterval(() => { setInterval(
checkLogSize().catch(error => { () => {
logger.error('Error checking log sizes:', error); checkLogSize().catch((error) => {
logger.error("Error checking log sizes:", error);
}); });
}, 60 * 60 * 1000); },
60 * 60 * 1000,
);
// Clean up old logs daily // Clean up old logs daily
setInterval(() => { setInterval(
cleanupOldLogs(APP_CONFIG.LOGGING.DIR, parseDuration(APP_CONFIG.LOGGING.MAX_DAYS)) () => {
.catch(error => { cleanupOldLogs(
logger.error('Error cleaning up old logs:', error); APP_CONFIG.LOGGING.DIR,
parseDuration(APP_CONFIG.LOGGING.MAX_DAYS),
).catch((error) => {
logger.error("Error cleaning up old logs:", error);
}); });
}, 24 * 60 * 60 * 1000); },
24 * 60 * 60 * 1000,
);
// Initial check // Initial check
checkLogSize().catch(error => { checkLogSize().catch((error) => {
logger.error('Error in initial log size check:', error); logger.error("Error in initial log size check:", error);
}); });
// Initial cleanup // Initial cleanup
cleanupOldLogs(APP_CONFIG.LOGGING.DIR, parseDuration(APP_CONFIG.LOGGING.MAX_DAYS)) cleanupOldLogs(
.catch(error => { APP_CONFIG.LOGGING.DIR,
logger.error('Error in initial log cleanup:', error); parseDuration(APP_CONFIG.LOGGING.MAX_DAYS),
).catch((error) => {
logger.error("Error in initial log cleanup:", error);
}); });
logger.info('Log rotation initialized'); logger.info("Log rotation initialized");
}; };

View File

@@ -7,9 +7,9 @@
* @module logger * @module logger
*/ */
import winston from 'winston'; import winston from "winston";
import DailyRotateFile from 'winston-daily-rotate-file'; import DailyRotateFile from "winston-daily-rotate-file";
import { APP_CONFIG } from '../config/app.config.js'; import { APP_CONFIG } from "../config/app.config.js";
/** /**
* Log levels configuration * Log levels configuration
@@ -28,11 +28,11 @@ const levels = {
* Defines colors for different log levels * Defines colors for different log levels
*/ */
const colors = { const colors = {
error: 'red', error: "red",
warn: 'yellow', warn: "yellow",
info: 'green', info: "green",
http: 'magenta', http: "magenta",
debug: 'white', debug: "white",
}; };
/** /**
@@ -45,7 +45,7 @@ winston.addColors(colors);
* Defines how log messages are formatted * Defines how log messages are formatted
*/ */
const format = winston.format.combine( const format = winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }), winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss:ms" }),
winston.format.colorize({ all: true }), winston.format.colorize({ all: true }),
winston.format.printf( winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`, (info) => `${info.timestamp} ${info.level}: ${info.message}`,
@@ -57,16 +57,16 @@ const format = winston.format.combine(
* Configures how logs are rotated and stored * Configures how logs are rotated and stored
*/ */
const dailyRotateFileTransport = new DailyRotateFile({ const dailyRotateFileTransport = new DailyRotateFile({
filename: 'logs/%DATE%.log', filename: "logs/%DATE%.log",
datePattern: 'YYYY-MM-DD', datePattern: "YYYY-MM-DD",
zippedArchive: true, zippedArchive: true,
maxSize: '20m', maxSize: "20m",
maxFiles: '14d', maxFiles: "14d",
format: winston.format.combine( format: winston.format.combine(
winston.format.uncolorize(), winston.format.uncolorize(),
winston.format.timestamp(), winston.format.timestamp(),
winston.format.json() winston.format.json(),
) ),
}); });
/** /**
@@ -74,35 +74,35 @@ const dailyRotateFileTransport = new DailyRotateFile({
* Stores error logs in a separate file * Stores error logs in a separate file
*/ */
const errorFileTransport = new DailyRotateFile({ const errorFileTransport = new DailyRotateFile({
filename: 'logs/error-%DATE%.log', filename: "logs/error-%DATE%.log",
datePattern: 'YYYY-MM-DD', datePattern: "YYYY-MM-DD",
level: 'error', level: "error",
zippedArchive: true, zippedArchive: true,
maxSize: '20m', maxSize: "20m",
maxFiles: '14d', maxFiles: "14d",
format: winston.format.combine( format: winston.format.combine(
winston.format.uncolorize(), winston.format.uncolorize(),
winston.format.timestamp(), winston.format.timestamp(),
winston.format.json() winston.format.json(),
) ),
}); });
/** /**
* Create the logger instance * Create the logger instance
*/ */
const logger = winston.createLogger({ const logger = winston.createLogger({
level: APP_CONFIG.NODE_ENV === 'development' ? 'debug' : 'info', level: APP_CONFIG.NODE_ENV === "development" ? "debug" : "info",
levels, levels,
format, format,
transports: [ transports: [
new winston.transports.Console({ new winston.transports.Console({
format: winston.format.combine( format: winston.format.combine(
winston.format.colorize(), winston.format.colorize(),
winston.format.simple() winston.format.simple(),
) ),
}), }),
dailyRotateFileTransport, dailyRotateFileTransport,
errorFileTransport errorFileTransport,
], ],
}); });

View File

@@ -1,5 +1,5 @@
import WebSocket from 'ws'; import WebSocket from "ws";
import { EventEmitter } from 'events'; import { EventEmitter } from "events";
export class HassWebSocketClient extends EventEmitter { export class HassWebSocketClient extends EventEmitter {
private ws: WebSocket | null = null; private ws: WebSocket | null = null;
@@ -17,7 +17,7 @@ export class HassWebSocketClient extends EventEmitter {
autoReconnect?: boolean; autoReconnect?: boolean;
maxReconnectAttempts?: number; maxReconnectAttempts?: number;
reconnectDelay?: number; reconnectDelay?: number;
} = {} } = {},
) { ) {
super(); super();
this.maxReconnectAttempts = options.maxReconnectAttempts || 5; this.maxReconnectAttempts = options.maxReconnectAttempts || 5;
@@ -29,32 +29,32 @@ export class HassWebSocketClient extends EventEmitter {
try { try {
this.ws = new WebSocket(this.url); this.ws = new WebSocket(this.url);
this.ws.on('open', () => { this.ws.on("open", () => {
this.authenticate(); this.authenticate();
}); });
this.ws.on('message', (data: string) => { this.ws.on("message", (data: string) => {
const message = JSON.parse(data); const message = JSON.parse(data);
this.handleMessage(message); this.handleMessage(message);
}); });
this.ws.on('close', () => { this.ws.on("close", () => {
this.handleDisconnect(); this.handleDisconnect();
}); });
this.ws.on('error', (error) => { this.ws.on("error", (error) => {
this.emit('error', error); this.emit("error", error);
reject(error); reject(error);
}); });
this.once('auth_ok', () => { this.once("auth_ok", () => {
this.authenticated = true; this.authenticated = true;
this.reconnectAttempts = 0; this.reconnectAttempts = 0;
resolve(); resolve();
}); });
this.once('auth_invalid', () => { this.once("auth_invalid", () => {
reject(new Error('Authentication failed')); reject(new Error("Authentication failed"));
}); });
} catch (error) { } catch (error) {
reject(error); reject(error);
@@ -64,26 +64,26 @@ export class HassWebSocketClient extends EventEmitter {
private authenticate(): void { private authenticate(): void {
this.send({ this.send({
type: 'auth', type: "auth",
access_token: this.token access_token: this.token,
}); });
} }
private handleMessage(message: any): void { private handleMessage(message: any): void {
switch (message.type) { switch (message.type) {
case 'auth_required': case "auth_required":
this.authenticate(); this.authenticate();
break; break;
case 'auth_ok': case "auth_ok":
this.emit('auth_ok'); this.emit("auth_ok");
break; break;
case 'auth_invalid': case "auth_invalid":
this.emit('auth_invalid'); this.emit("auth_invalid");
break; break;
case 'event': case "event":
this.handleEvent(message); this.handleEvent(message);
break; break;
case 'result': case "result":
this.emit(`result_${message.id}`, message); this.emit(`result_${message.id}`, message);
break; break;
} }
@@ -94,26 +94,35 @@ export class HassWebSocketClient extends EventEmitter {
if (subscription) { if (subscription) {
subscription(message.event.data); subscription(message.event.data);
} }
this.emit('event', message.event); this.emit("event", message.event);
} }
private handleDisconnect(): void { private handleDisconnect(): void {
this.authenticated = false; this.authenticated = false;
this.emit('disconnected'); this.emit("disconnected");
if (this.options.autoReconnect && this.reconnectAttempts < this.maxReconnectAttempts) { if (
setTimeout(() => { this.options.autoReconnect &&
this.reconnectAttempts < this.maxReconnectAttempts
) {
setTimeout(
() => {
this.reconnectAttempts++; this.reconnectAttempts++;
this.connect().catch((error) => { this.connect().catch((error) => {
this.emit('error', error); this.emit("error", error);
}); });
}, this.reconnectDelay * Math.pow(2, this.reconnectAttempts)); },
this.reconnectDelay * Math.pow(2, this.reconnectAttempts),
);
} }
} }
public async subscribeEvents(eventType: string, callback: (data: any) => void): Promise<number> { public async subscribeEvents(
eventType: string,
callback: (data: any) => void,
): Promise<number> {
if (!this.authenticated) { if (!this.authenticated) {
throw new Error('Not authenticated'); throw new Error("Not authenticated");
} }
const id = this.messageId++; const id = this.messageId++;
@@ -122,15 +131,15 @@ export class HassWebSocketClient extends EventEmitter {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.send({ this.send({
id, id,
type: 'subscribe_events', type: "subscribe_events",
event_type: eventType event_type: eventType,
}); });
this.once(`result_${id}`, (message) => { this.once(`result_${id}`, (message) => {
if (message.success) { if (message.success) {
resolve(id); resolve(id);
} else { } else {
reject(new Error(message.error?.message || 'Subscription failed')); reject(new Error(message.error?.message || "Subscription failed"));
} }
}); });
}); });
@@ -138,22 +147,22 @@ export class HassWebSocketClient extends EventEmitter {
public async unsubscribeEvents(subscription: number): Promise<void> { public async unsubscribeEvents(subscription: number): Promise<void> {
if (!this.authenticated) { if (!this.authenticated) {
throw new Error('Not authenticated'); throw new Error("Not authenticated");
} }
const id = this.messageId++; const id = this.messageId++;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.send({ this.send({
id, id,
type: 'unsubscribe_events', type: "unsubscribe_events",
subscription subscription,
}); });
this.once(`result_${id}`, (message) => { this.once(`result_${id}`, (message) => {
if (message.success) { if (message.success) {
resolve(); resolve();
} else { } else {
reject(new Error(message.error?.message || 'Unsubscribe failed')); reject(new Error(message.error?.message || "Unsubscribe failed"));
} }
}); });
}); });

View File

@@ -1,47 +1,52 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2022", "target": "esnext",
"module": "NodeNext", "module": "esnext",
"moduleResolution": "NodeNext", "lib": [
"outDir": "./dist", "esnext",
"rootDir": ".", "dom"
],
"strict": true, "strict": true,
"esModuleInterop": true, "esModuleInterop": true,
"skipLibCheck": true, "skipLibCheck": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true, "resolveJsonModule": true,
"declaration": true, "isolatedModules": true,
"sourceMap": true, "noEmit": true,
"allowJs": true, "jsx": "react-jsx",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": [ "types": [
"node", "bun-types",
"jest" "@types/node",
], "@types/ws",
"typeRoots": [ "@types/jsonwebtoken",
"./node_modules/@types" "@types/sanitize-html",
], "@types/jest"
"lib": [
"ES2022"
], ],
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@src/*": [ "@/*": [
"src/*" "./src/*"
], ],
"@tests/*": [ "@test/*": [
"__tests__/*" "__tests__/*"
] ]
} },
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"sourceMap": true,
"declaration": true,
"declarationMap": true
}, },
"include": [ "include": [
"src/**/*", "src/**/*",
"__tests__/**/*" "__tests__/**/*",
"*.d.ts"
], ],
"exclude": [ "exclude": [
"node_modules", "node_modules",
"**/__tests__/**/*.ts", "dist",
"**/*.test.ts" "coverage"
] ]
} }