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
This commit is contained in:
jango-blockchained
2025-02-04 00:56:45 +01:00
parent 9a02bdaf11
commit bc1dc8278a
65 changed files with 7094 additions and 7675 deletions

98
src/types/bun.d.ts vendored
View File

@@ -1,50 +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;
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;
};
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;
};
}
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;
};
}

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

@@ -1,81 +1,81 @@
declare namespace HomeAssistant {
interface Entity {
entity_id: string;
state: string;
attributes: Record<string, any>;
last_changed: string;
last_updated: string;
context: {
id: string;
parent_id?: string;
user_id?: string;
};
}
interface Entity {
entity_id: string;
state: string;
attributes: Record<string, any>;
last_changed: string;
last_updated: string;
context: {
id: string;
parent_id?: string;
user_id?: string;
};
}
interface Service {
domain: string;
service: string;
target?: {
entity_id?: string | string[];
device_id?: string | string[];
area_id?: string | string[];
};
service_data?: Record<string, any>;
}
interface Service {
domain: string;
service: string;
target?: {
entity_id?: string | string[];
device_id?: string | string[];
area_id?: string | string[];
};
service_data?: Record<string, any>;
}
interface WebsocketMessage {
type: string;
id?: number;
[key: string]: any;
}
interface WebsocketMessage {
type: string;
id?: number;
[key: string]: any;
}
interface AuthMessage extends WebsocketMessage {
type: 'auth';
access_token: string;
}
interface AuthMessage extends WebsocketMessage {
type: "auth";
access_token: string;
}
interface SubscribeEventsMessage extends WebsocketMessage {
type: 'subscribe_events';
event_type?: string;
}
interface SubscribeEventsMessage extends WebsocketMessage {
type: "subscribe_events";
event_type?: string;
}
interface StateChangedEvent {
event_type: 'state_changed';
data: {
entity_id: string;
new_state: Entity | null;
old_state: Entity | null;
};
origin: string;
time_fired: string;
context: {
id: string;
parent_id?: string;
user_id?: string;
};
}
interface StateChangedEvent {
event_type: "state_changed";
data: {
entity_id: string;
new_state: Entity | null;
old_state: Entity | null;
};
origin: string;
time_fired: string;
context: {
id: string;
parent_id?: string;
user_id?: string;
};
}
interface Config {
latitude: number;
longitude: number;
elevation: number;
unit_system: {
length: string;
mass: string;
temperature: string;
volume: string;
};
location_name: string;
time_zone: string;
components: string[];
version: string;
}
interface Config {
latitude: number;
longitude: number;
elevation: number;
unit_system: {
length: string;
mass: string;
temperature: string;
volume: string;
};
location_name: string;
time_zone: string;
components: string[];
version: string;
}
interface ApiError {
code: string;
message: string;
details?: Record<string, any>;
}
interface ApiError {
code: string;
message: string;
details?: Record<string, any>;
}
}
export = HomeAssistant;
export = HomeAssistant;

View File

@@ -1,86 +1,85 @@
export interface AuthMessage {
type: 'auth';
access_token: string;
type: "auth";
access_token: string;
}
export interface ResultMessage {
id: number;
type: 'result';
success: boolean;
result?: any;
id: number;
type: "result";
success: boolean;
result?: any;
}
export interface WebSocketError {
code: string;
message: string;
code: string;
message: string;
}
export interface Event {
event_type: string;
data: any;
origin: string;
time_fired: string;
context: {
id: string;
parent_id: string | null;
user_id: string | null;
};
event_type: string;
data: any;
origin: string;
time_fired: string;
context: {
id: string;
parent_id: string | null;
user_id: string | null;
};
}
export interface Entity {
entity_id: string;
state: string;
attributes: Record<string, any>;
last_changed: string;
last_updated: string;
context: {
id: string;
parent_id: string | null;
user_id: string | null;
};
entity_id: string;
state: string;
attributes: Record<string, any>;
last_changed: string;
last_updated: string;
context: {
id: string;
parent_id: string | null;
user_id: string | null;
};
}
export interface StateChangedEvent extends Event {
event_type: 'state_changed';
data: {
entity_id: string;
new_state: Entity | null;
old_state: Entity | null;
};
event_type: "state_changed";
data: {
entity_id: string;
new_state: Entity | null;
old_state: Entity | null;
};
}
export interface HassEntity {
entity_id: string;
state: string;
attributes: Record<string, any>;
last_changed?: string;
last_updated?: string;
context?: {
id: string;
parent_id?: string;
user_id?: string;
};
entity_id: string;
state: string;
attributes: Record<string, any>;
last_changed?: string;
last_updated?: string;
context?: {
id: string;
parent_id?: string;
user_id?: string;
};
}
export interface HassState {
entity_id: string;
state: string;
attributes: {
friendly_name?: string;
description?: string;
[key: string]: any;
};
entity_id: string;
state: string;
attributes: {
friendly_name?: string;
description?: string;
[key: string]: any;
};
}
export interface HassEvent {
event_type: string;
data: any;
origin: string;
time_fired: string;
context: {
id: string;
parent_id?: string;
user_id?: string;
};
event_type: string;
data: any;
origin: string;
time_fired: string;
context: {
id: string;
parent_id?: string;
user_id?: string;
};
}

View File

@@ -1,18 +1,18 @@
import { z } from 'zod';
import { z } from "zod";
/**
* Interface for a tool that can be executed by the MCP
* @interface Tool
*/
export interface Tool {
/** Unique name identifier for the tool */
name: string;
/** Description of what the tool does */
description: string;
/** Zod schema for validating tool parameters */
parameters: z.ZodType<any>;
/** Function to execute the tool with the given parameters */
execute: (params: any) => Promise<any>;
/** Unique name identifier for the tool */
name: string;
/** Description of what the tool does */
description: string;
/** Zod schema for validating tool parameters */
parameters: z.ZodType<any>;
/** Function to execute the tool with the given parameters */
execute: (params: any) => Promise<any>;
}
/**
@@ -20,26 +20,26 @@ export interface Tool {
* @interface CommandParams
*/
export interface CommandParams {
/** Command to execute (e.g., turn_on, turn_off) */
command: string;
/** Entity ID to control */
entity_id: string;
/** Common parameters */
state?: string;
/** Light parameters */
brightness?: number;
color_temp?: number;
rgb_color?: [number, number, number];
/** Cover parameters */
position?: number;
tilt_position?: number;
/** Climate parameters */
temperature?: number;
target_temp_high?: number;
target_temp_low?: number;
hvac_mode?: string;
fan_mode?: string;
humidity?: number;
/** Command to execute (e.g., turn_on, turn_off) */
command: string;
/** Entity ID to control */
entity_id: string;
/** Common parameters */
state?: string;
/** Light parameters */
brightness?: number;
color_temp?: number;
rgb_color?: [number, number, number];
/** Cover parameters */
position?: number;
tilt_position?: number;
/** Climate parameters */
temperature?: number;
target_temp_high?: number;
target_temp_low?: number;
hvac_mode?: string;
fan_mode?: string;
humidity?: number;
}
/**
@@ -47,22 +47,22 @@ export interface CommandParams {
* @interface HassEntity
*/
export interface HassEntity {
/** Entity ID in format domain.name */
entity_id: string;
/** Current state of the entity */
state: string;
/** Entity attributes */
attributes: Record<string, any>;
/** Last state change timestamp */
last_changed?: string;
/** Last update timestamp */
last_updated?: string;
/** Context information */
context?: {
id: string;
parent_id?: string;
user_id?: string;
};
/** Entity ID in format domain.name */
entity_id: string;
/** Current state of the entity */
state: string;
/** Entity attributes */
attributes: Record<string, any>;
/** Last state change timestamp */
last_changed?: string;
/** Last update timestamp */
last_updated?: string;
/** Context information */
context?: {
id: string;
parent_id?: string;
user_id?: string;
};
}
/**
@@ -70,19 +70,19 @@ export interface HassEntity {
* @interface HassState
*/
export interface HassState {
/** Entity ID in format domain.name */
entity_id: string;
/** Current state of the entity */
state: string;
/** Entity attributes */
attributes: {
/** Human-readable name */
friendly_name?: string;
/** Entity description */
description?: string;
/** Additional attributes */
[key: string]: any;
};
/** Entity ID in format domain.name */
entity_id: string;
/** Current state of the entity */
state: string;
/** Entity attributes */
attributes: {
/** Human-readable name */
friendly_name?: string;
/** Entity description */
description?: string;
/** Additional attributes */
[key: string]: any;
};
}
/**
@@ -90,6 +90,41 @@ export interface HassState {
* @interface HassAddon
*/
export interface HassAddon {
/** Add-on name */
name: string;
/** Add-on slug identifier */
slug: string;
/** Add-on description */
description: string;
/** Add-on version */
version: string;
/** Whether the add-on is installed */
installed: boolean;
/** Whether the add-on is available */
available: boolean;
/** Current state of the add-on */
state: string;
}
/**
* Response from Home Assistant add-on API
* @interface HassAddonResponse
*/
export interface HassAddonResponse {
/** Response data */
data: {
/** List of add-ons */
addons: HassAddon[];
};
}
/**
* Response from Home Assistant add-on info API
* @interface HassAddonInfoResponse
*/
export interface HassAddonInfoResponse {
/** Response data */
data: {
/** Add-on name */
name: string;
/** Add-on slug identifier */
@@ -98,50 +133,15 @@ export interface HassAddon {
description: string;
/** Add-on version */
version: string;
/** Whether the add-on is installed */
installed: boolean;
/** Whether the add-on is available */
available: boolean;
/** Current state of the add-on */
/** Current state */
state: string;
}
/**
* Response from Home Assistant add-on API
* @interface HassAddonResponse
*/
export interface HassAddonResponse {
/** Response data */
data: {
/** List of add-ons */
addons: HassAddon[];
};
}
/**
* Response from Home Assistant add-on info API
* @interface HassAddonInfoResponse
*/
export interface HassAddonInfoResponse {
/** Response data */
data: {
/** Add-on name */
name: string;
/** Add-on slug identifier */
slug: string;
/** Add-on description */
description: string;
/** Add-on version */
version: string;
/** Current state */
state: string;
/** Status information */
status: string;
/** Add-on options */
options: Record<string, any>;
/** Additional properties */
[key: string]: any;
};
/** Status information */
status: string;
/** Add-on options */
options: Record<string, any>;
/** Additional properties */
[key: string]: any;
};
}
/**
@@ -149,22 +149,22 @@ export interface HassAddonInfoResponse {
* @interface HacsRepository
*/
export interface HacsRepository {
/** Repository name */
name: string;
/** Repository description */
description: string;
/** Repository category */
category: string;
/** Whether the repository is installed */
installed: boolean;
/** Installed version */
version_installed: string;
/** Available version */
available_version: string;
/** Repository authors */
authors: string[];
/** Repository domain */
domain: string;
/** Repository name */
name: string;
/** Repository description */
description: string;
/** Repository category */
category: string;
/** Whether the repository is installed */
installed: boolean;
/** Installed version */
version_installed: string;
/** Available version */
available_version: string;
/** Repository authors */
authors: string[];
/** Repository domain */
domain: string;
}
/**
@@ -172,8 +172,8 @@ export interface HacsRepository {
* @interface HacsResponse
*/
export interface HacsResponse {
/** List of repositories */
repositories: HacsRepository[];
/** List of repositories */
repositories: HacsRepository[];
}
/**
@@ -181,18 +181,18 @@ export interface HacsResponse {
* @interface AutomationConfig
*/
export interface AutomationConfig {
/** Automation name */
alias: string;
/** Automation description */
description?: string;
/** How multiple triggers are handled */
mode?: 'single' | 'parallel' | 'queued' | 'restart';
/** List of triggers */
trigger: any[];
/** List of conditions */
condition?: any[];
/** List of actions */
action: any[];
/** Automation name */
alias: string;
/** Automation description */
description?: string;
/** How multiple triggers are handled */
mode?: "single" | "parallel" | "queued" | "restart";
/** List of triggers */
trigger: any[];
/** List of conditions */
condition?: any[];
/** List of actions */
action: any[];
}
/**
@@ -200,8 +200,8 @@ export interface AutomationConfig {
* @interface AutomationResponse
*/
export interface AutomationResponse {
/** Created/updated automation ID */
automation_id: string;
/** Created/updated automation ID */
automation_id: string;
}
/**
@@ -209,8 +209,8 @@ export interface AutomationResponse {
* @interface SSEHeaders
*/
export interface SSEHeaders {
/** Callback for connection abort */
onAbort?: () => void;
/** Callback for connection abort */
onAbort?: () => void;
}
/**
@@ -218,14 +218,14 @@ export interface SSEHeaders {
* @interface SSEParams
*/
export interface SSEParams {
/** Authentication token */
token: string;
/** Event types to subscribe to */
events?: string[];
/** Entity ID to monitor */
entity_id?: string;
/** Domain to monitor */
domain?: string;
/** Authentication token */
token: string;
/** Event types to subscribe to */
events?: string[];
/** Entity ID to monitor */
entity_id?: string;
/** Domain to monitor */
domain?: string;
}
/**
@@ -233,16 +233,16 @@ export interface SSEParams {
* @interface HistoryParams
*/
export interface HistoryParams {
/** Entity ID to get history for */
entity_id: string;
/** Start time in ISO format */
start_time?: string;
/** End time in ISO format */
end_time?: string;
/** Whether to return minimal response */
minimal_response?: boolean;
/** Whether to only return significant changes */
significant_changes_only?: boolean;
/** Entity ID to get history for */
entity_id: string;
/** Start time in ISO format */
start_time?: string;
/** End time in ISO format */
end_time?: string;
/** Whether to return minimal response */
minimal_response?: boolean;
/** Whether to only return significant changes */
significant_changes_only?: boolean;
}
/**
@@ -250,10 +250,10 @@ export interface HistoryParams {
* @interface SceneParams
*/
export interface SceneParams {
/** Action to perform */
action: 'list' | 'activate';
/** Scene ID for activation */
scene_id?: string;
/** Action to perform */
action: "list" | "activate";
/** Scene ID for activation */
scene_id?: string;
}
/**
@@ -261,14 +261,14 @@ export interface SceneParams {
* @interface NotifyParams
*/
export interface NotifyParams {
/** Notification message */
message: string;
/** Notification title */
title?: string;
/** Notification target */
target?: string;
/** Additional notification data */
data?: Record<string, any>;
/** Notification message */
message: string;
/** Notification title */
title?: string;
/** Notification target */
target?: string;
/** Additional notification data */
data?: Record<string, any>;
}
/**
@@ -276,10 +276,10 @@ export interface NotifyParams {
* @interface AutomationParams
*/
export interface AutomationParams {
/** Action to perform */
action: 'list' | 'toggle' | 'trigger';
/** Automation ID */
automation_id?: string;
/** Action to perform */
action: "list" | "toggle" | "trigger";
/** Automation ID */
automation_id?: string;
}
/**
@@ -287,12 +287,19 @@ export interface AutomationParams {
* @interface AddonParams
*/
export interface AddonParams {
/** Action to perform */
action: 'list' | 'info' | 'install' | 'uninstall' | 'start' | 'stop' | 'restart';
/** Add-on slug */
slug?: string;
/** Version to install */
version?: string;
/** Action to perform */
action:
| "list"
| "info"
| "install"
| "uninstall"
| "start"
| "stop"
| "restart";
/** Add-on slug */
slug?: string;
/** Version to install */
version?: string;
}
/**
@@ -300,14 +307,20 @@ export interface AddonParams {
* @interface PackageParams
*/
export interface PackageParams {
/** Action to perform */
action: 'list' | 'install' | 'uninstall' | 'update';
/** Package category */
category: 'integration' | 'plugin' | 'theme' | 'python_script' | 'appdaemon' | 'netdaemon';
/** Repository URL or name */
repository?: string;
/** Version to install */
version?: string;
/** Action to perform */
action: "list" | "install" | "uninstall" | "update";
/** Package category */
category:
| "integration"
| "plugin"
| "theme"
| "python_script"
| "appdaemon"
| "netdaemon";
/** Repository URL or name */
repository?: string;
/** Version to install */
version?: string;
}
/**
@@ -315,23 +328,23 @@ export interface PackageParams {
* @interface AutomationConfigParams
*/
export interface AutomationConfigParams {
/** Action to perform */
action: 'create' | 'update' | 'delete' | 'duplicate';
/** Automation ID */
automation_id?: string;
/** Automation configuration */
config?: {
/** Automation name */
alias: string;
/** Automation description */
description?: string;
/** How multiple triggers are handled */
mode?: 'single' | 'parallel' | 'queued' | 'restart';
/** List of triggers */
trigger: any[];
/** List of conditions */
condition?: any[];
/** List of actions */
action: any[];
};
}
/** Action to perform */
action: "create" | "update" | "delete" | "duplicate";
/** Automation ID */
automation_id?: string;
/** Automation configuration */
config?: {
/** Automation name */
alias: string;
/** Automation description */
description?: string;
/** How multiple triggers are handled */
mode?: "single" | "parallel" | "queued" | "restart";
/** List of triggers */
trigger: any[];
/** List of conditions */
condition?: any[];
/** List of actions */
action: any[];
};
}