# PyWatt SDK Changes for Service Integration

This document outlines the changes needed to the PyWatt SDK to support service integration with the orchestrator.

## IPC Messages

Add the following message types to `src/ipc_types`:

```rust
/// Request a service connection from the orchestrator
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceRequest {
    /// Identifier for the service
    pub id: String,
    /// Type of service
    pub service_type: ServiceType,
    /// Optional configuration override
    pub config: Option<serde_json::Value>,
}

/// Response to a service request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceResponse {
    /// ID from the request
    pub id: String,
    /// Type of service
    pub service_type: ServiceType,
    /// Whether the request was successful
    pub success: bool,
    /// Error message if unsuccessful
    pub error: Option<String>,
    /// Unique ID for the connection
    pub connection_id: Option<String>,
}

/// Type of service
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ServiceType {
    /// Database service
    Database,
    /// Cache service
    Cache,
    /// JWT authentication service
    Jwt,
}

/// Perform an operation on a service
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceOperation {
    /// ID of the connection to use
    pub connection_id: String,
    /// Type of service
    pub service_type: ServiceType,
    /// Name of the operation
    pub operation: String,
    /// Parameters for the operation
    pub params: serde_json::Value,
}

/// Result of a service operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceOperationResult {
    /// Whether the operation was successful
    pub success: bool,
    /// Result data if successful
    pub result: Option<serde_json::Value>,
    /// Error message if unsuccessful
    pub error: Option<String>,
}
```

## SDK Adapters

Implement the following adapter classes:

1. `src/database/proxy_connection.rs` - Proxy for database connections
2. `src/cache/proxy_service.rs` - Proxy for cache services
3. `src/jwt_auth/proxy_adapter.rs` - Proxy for JWT operations

These adapters will:
1. Send IPC messages to orchestrator for each operation
2. Wait for responses
3. Convert results to appropriate types
4. Handle error mapping

## Changes to Service Factories

Update the following factory functions:

1. `create_database_connection` - Add an IPC-based connection option
2. `create_cache_service` - Add an IPC-based service option
3. JWT helpers - Add IPC-based JWT validation

These functions should:
1. Check if running as a module (under orchestrator)
2. If yes, send service requests via IPC
3. If no, continue with direct connection setup

## Error Handling

Add appropriate error types and mapping:

1. Add `IpcError` to database and cache error enums
2. Implement conversion between IPC errors and service-specific errors
3. Add proper logging for IPC failures

## Serialization

Implement serialization/deserialization for complex types:

1. Database values and rows
2. Cache values and statistics
3. JWT claims and tokens

## Connection Management

Add connection tracking and cleanup:

1. Track active connections
2. Clean up on module shutdown
3. Handle reconnection when IPC is disrupted

## Testing

Add comprehensive tests:

1. Unit tests for each adapter
2. Integration tests with mock orchestrator
3. End-to-end tests with real orchestrator 