> ## Documentation Index
> Fetch the complete documentation index at: https://docs.productflo.io/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Communication

> Using WebSockets for AI-powered features

# AI Communication via WebSockets

ProductFlo provides powerful AI capabilities through its WebSocket API, enabling real-time interaction with the Haitch AI assistant and various AI-powered generation features. This page details how to integrate with these AI capabilities.

## Overview

The WebSocket API supports several AI-powered features:

<CardGroup cols={2}>
  <Card title="Haitch AI Assistant" icon="robot" iconType="duotone">
    Intelligent assistant for product development, engineering, and documentation
  </Card>

  <Card title="Idea Generation" icon="lightbulb" iconType="duotone">
    AI-powered brainstorming and product ideation
  </Card>

  <Card title="Document Generation" icon="file-lines" iconType="duotone">
    Create technical documentation, specifications, and reports
  </Card>

  <Card title="Engineering AI" icon="microchip" iconType="duotone">
    CAD generation, DFM analysis, and engineering assistance
  </Card>
</CardGroup>

## Message Structure

AI communication uses the standard WebSocket message structure with specific code types:

```json theme={null}
{
  "code": "ai_code_type",     // e.g., "haitch_chat", "idea", "documentation", "engineering"
  "user_id": "user-123",
  "room_id": "product-456",
  "data": {
    // AI-specific message data
  }
}
```

## Haitch AI Assistant

The Haitch AI assistant provides intelligent assistance for product development questions and tasks.

### Sending a Haitch Chat Message

```javascript theme={null}
socket.send(JSON.stringify({
  "code": "haitch_chat",
  "user_id": "user-123",
  "room_id": "product-456",
  "data": {
    "query": "What materials would work best for a waterproof enclosure?",
    "doc_type": "consultation",  // Optional: Specifies type of response needed
    "source_urls": ["https://example.com/materials-guide.pdf"]  // Optional: Reference URLs
  }
}));
```

### Receiving a Haitch Response

```json theme={null}
{
  "status": "success",
  "code": "chat_response",
  "user_id": "user-123",
  "room_id": "product-456",
  "timestamp": "2024-05-19T10:30:45Z",
  "data": {
    "message": "For a waterproof enclosure, I recommend considering these materials:\n\n1. ABS plastic with IP67 rating\n2. Polycarbonate with silicone gaskets\n3. Aluminum with O-ring seals\n\nThe choice depends on your specific requirements for impact resistance, temperature range, and manufacturing methods. Would you like me to elaborate on any of these options?",
    "sender": "haitch",
    "message_id": "msg-789",
    "sources": ["https://example.com/materials-guide.pdf"],
    "metadata": {
      "confidence": 0.92,
      "processing_time": 1.25,
      "doc_type": "consultation" 
    }
  }
}
```

### Streaming Responses

For longer AI responses, you'll receive the response in chunks as they're generated (streaming is enabled by default):

```javascript theme={null}
socket.send(JSON.stringify({
  "code": "haitch_chat",
  "user_id": "user-123",
  "room_id": "product-456",
  "data": {
    "query": "Generate a comprehensive analysis of our thermostat design",
    "doc_type": "analysis",
    "request_id": "req-123"  // Optional: Track this specific request
  }
}));
```

You'll receive multiple stream chunks with pending status:

```json theme={null}
{
  "status": "pending",
  "code": "chat_response",
  "user_id": "user-123",
  "room_id": "product-456",
  "timestamp": "2024-05-19T10:30:45Z",
  "data": {
    "message": "Here's my analysis of the thermostat design:",
    "is_complete": false,
    "sender": "haitch",
    "message_id": "stream-chunk-1"
  }
}
```

And when complete, you'll receive a final message with success status:

```json theme={null}
{
  "status": "success",
  "code": "chat_response",
  "user_id": "user-123",
  "room_id": "product-456",
  "timestamp": "2024-05-19T10:30:47Z",
  "data": {
    "message": "Here's my analysis of the thermostat design: [complete response]",
    "sender": "haitch",
    "message_id": "msg-790",
    "is_complete": true,
    "sources": [],
    "metadata": {
      "processing_time": 2.5,
      "doc_type": "analysis"
    }
  }
}
```

### Client Implementation for Streaming

```javascript theme={null}
let fullResponse = '';
let messageMap = new Map(); // Store chunks by message ID

socket.onmessage = (event) => {
  const data = JSON.parse(event.data);
  
  // Only process chat_response messages
  if (data.code === "chat_response") {
    // For pending status (streaming chunks)
    if (data.status === "pending") {
      // Accumulate the response text
      fullResponse += data.data.message || '';
      
      // Update UI to show progress
      updateProgressiveUI(fullResponse);
    }
    
    // For success status (final message)
    if (data.status === "success" && data.data.is_complete) {
      // Final message contains the complete response
      finalResponse = data.data.message;
      
      // Update UI with complete response
      finalizeUI(finalResponse);
      
      // Reset for next conversation
      fullResponse = '';
    }
  }
};
```

## Document Generation

Generate professional documentation from product data and requirements.

### Requesting Document Generation

```javascript theme={null}
socket.send(JSON.stringify({
  "code": "documentation",
  "user_id": "user-123",
  "room_id": "product-456",
  "data": {
    "query": "Generate a technical specification for our thermostat",
    "doc_type": "technical_specification",  // Type of document required
    "source_urls": ["https://example.com/thermostat_requirements.pdf"]  // Optional reference URLs
  }
}));
```

### Receiving Generated Document

```json theme={null}
{
  "status": "success",
  "code": "documentation_response",
  "user_id": "user-123",
  "room_id": "product-456",
  "timestamp": "2024-05-19T10:31:45Z",
  "data": {
    "message": "Technical specification generated successfully",
    "doc_type": "technical_specification",
    "files": [
      {
        "file_id": "file-901",
        "file_name": "thermostat_technical_specification.pdf",
        "file_url": "https://api.productflo.io/files/thermostat_technical_specification.pdf",
        "file_type": "application/pdf",
        "file_size": 245789
      }
    ],
    "content": "# Technical Specification: Smart Thermostat\n\n## Overview\n...",
    "sources": ["https://example.com/thermostat_requirements.pdf"],
    "metadata": {
      "word_count": 2450,
      "page_count": 12,
      "processing_time": 3.2,
      "sections": ["overview", "specifications", "requirements", "testing"]
    }
  }
}
```

### Document Types

The system supports various document types:

* `technical_specification`: Detailed product specifications
* `bom`: Bill of Materials
* `dfm_analysis`: Design for Manufacturing analysis
* `user_manual`: End-user documentation
* `datasheet`: Technical datasheet
* `requirements`: Requirements document
* `test_plan`: Testing procedures and criteria
* `engineering_drawing`: Technical drawings
* `compliance_documentation`: Regulatory compliance docs

## Idea Generation

Generate product ideas, feature suggestions, and creative solutions.

### Requesting Idea Generation

```javascript theme={null}
socket.send(JSON.stringify({
  "code": "idea",
  "user_id": "user-123",
  "room_id": "product-456",
  "data": {
    "query": "Generate innovative ideas for a smart thermostat",
    "source_urls": ["https://example.com/smart_home_trends.pdf"],
    "doc_type": "idea_generation"  // Optional: Specifies the type of ideation
  }
}));
```

### Receiving Generated Ideas

```json theme={null}
{
  "status": "success",
  "code": "idea_response",
  "user_id": "user-123",
  "room_id": "product-456",
  "timestamp": "2024-05-19T10:32:45Z",
  "data": {
    "message": "Here are innovative smart thermostat feature ideas:",
    "content": {
      "product_name": "Smart EcoTherm",
      "product_description": "An AI-powered smart thermostat focused on energy efficiency and user comfort",
      "features": [
        {
          "title": "AI-Powered Usage Learning",
          "description": "Machine learning algorithm that adapts to user habits and optimizes heating/cooling schedules automatically",
          "benefits": ["Energy savings", "Improved comfort", "Reduced user configuration"],
          "implementation_complexity": "medium"
        },
        {
          "title": "Multi-Zone Sensing",
          "description": "Low-cost satellite temperature sensors that connect to the main thermostat for more accurate room-by-room temperature control",
          "benefits": ["Better comfort", "Increased efficiency", "Premium feature"],
          "implementation_complexity": "high"
        }
      ],
      "market_analysis": {
        "opportunity": "Strong demand for energy-efficient smart home devices",
        "competitive_landscape": "Current offerings lack advanced learning capabilities"
      }
    },
    "sources": ["https://example.com/smart_home_trends.pdf"],
    "metadata": {
      "processing_time": 2.8,
      "feature_count": 2,
      "doc_type": "idea_generation"
    }
  }
}
```

## Engineering AI Features

Generate CAD models, perform engineering analysis, and create technical designs.

### Requesting Engineering Analysis

```javascript theme={null}
socket.send(JSON.stringify({
  "code": "engineering",
  "user_id": "user-123",
  "room_id": "product-456",
  "data": {
    "query": "Generate a CAD model for a wall-mount bracket for our thermostat with dimensions 100x80x20mm, made of ABS plastic",
    "doc_type": "cad_generation",  // Type of engineering request
    "source_urls": ["https://example.com/thermostat_dimensions.pdf"]  // Optional reference URLs
  }
}));
```

### Receiving Engineering Response

```json theme={null}
{
  "status": "success",
  "code": "engineering_response",
  "user_id": "user-123",
  "room_id": "product-456",
  "timestamp": "2024-05-19T10:35:45Z",
  "data": {
    "message": "CAD model generated successfully",
    "content": {
      "description": "Wall-mount bracket for thermostat",
      "specifications": {
        "dimensions": "100x80x20mm",
        "material": "ABS plastic",
        "mounting_type": "wall_mount",
        "estimated_weight": "42g"
      },
      "manufacturing_notes": "Suitable for injection molding with standard draft angles"
    },
    "files": [
      {
        "file_id": "file-902",
        "file_name": "thermostat_bracket.step",
        "file_url": "https://api.productflo.io/files/thermostat_bracket.step",
        "file_type": "application/step",
        "file_size": 1245789
      },
      {
        "file_id": "file-903",
        "file_name": "thermostat_bracket.stl",
        "file_url": "https://api.productflo.io/files/thermostat_bracket.stl",
        "file_type": "application/stl",
        "file_size": 2345678
      },
      {
        "file_id": "file-904",
        "file_name": "thermostat_bracket_preview.png",
        "file_url": "https://api.productflo.io/files/thermostat_bracket_preview.png",
        "file_type": "image/png",
        "file_size": 245789
      }
    ],
    "sources": ["https://example.com/thermostat_dimensions.pdf"],
    "metadata": {
      "processing_time": 5.2,
      "doc_type": "cad_generation",
      "file_count": 3
    }
  }
}
```

### Engineering Request Types

The system supports various engineering request types:

* `cad_generation`: Generate 3D CAD models
* `dfm_analysis`: Design for Manufacturing analysis
* `tolerance_analysis`: Component tolerance stack-up analysis
* `structural_analysis`: Basic structural integrity checks
* `thermal_analysis`: Thermal performance simulation
* `electrical_design`: Simple electrical system design
* `pcb_layout`: PCB layout suggestions
* `manufacturing_process`: Manufacturing process recommendations

## Handling Errors

AI operations can sometimes fail due to various reasons. Error responses follow this format:

```json theme={null}
{
  "status": "error",
  "code": "system",
  "user_id": "user-123",
  "room_id": "product-456",
  "timestamp": "2024-05-19T10:35:45Z",
  "data": {
    "message": "Failed to generate CAD model",
    "details": {
      "error_code": "CAD_GENERATION_ERROR",
      "message": "Invalid specifications provided: dimensions must include all three axes"
    }
  }
}
```

Common AI-specific error codes:

* `HAITCH_CHAT_ERROR`: Error in AI assistant processing
* `DOCUMENT_GENERATION_ERROR`: Error in document generation
* `IDEA_GENERATION_ERROR`: Error in idea generation
* `CAD_GENERATION_ERROR`: Error in CAD model generation
* `ENGINEERING_ANALYSIS_ERROR`: Error in engineering analysis
* `CONTENT_POLICY_VIOLATION`: Request violates content policies
* `CONTEXT_LIMIT_EXCEEDED`: Too much context provided

## Canceling Operations

Long-running AI operations can be canceled using a cancel message:

```javascript theme={null}
socket.send(JSON.stringify({
  "code": "system",
  "user_id": "user-123",
  "room_id": "product-456",
  "data": {
    "message": "cancel",
    "request_id": "req-123"  // The request ID to cancel
  }
}));
```

You'll receive a confirmation:

```json theme={null}
{
  "status": "success",
  "code": "system",
  "user_id": "user-123",
  "room_id": "product-456",
  "timestamp": "2024-05-19T10:36:45Z",
  "data": {
    "message": "Operation canceled",
    "request_id": "req-123"
  }
}
```

## Providing Context with Files

To enhance AI results, you can attach files for additional context:

1. First, upload files using the `/files` REST API endpoint
2. Then reference the file IDs in your WebSocket message:

```javascript theme={null}
socket.send(JSON.stringify({
  "code": "haitch_chat",
  "user_id": "user-123",
  "room_id": "product-456",
  "data": {
    "message": "Analyze this PCB design and suggest improvements",
    "sender": "user-123",
    "attached_files": ["file-123", "file-456"],
    "metadata": {
      "priority_areas": ["power_efficiency", "thermal_management"]
    }
  }
}));
```

## Best Practices

1. **Use Request IDs**: Always include a unique `request_id` for tracking AI operations, especially long-running ones
2. **Enable Streaming**: For responses that might be lengthy, enable streaming for better user experience
3. **Provide Clear Context**: Include relevant product IDs and files to improve AI accuracy
4. **Handle Errors Gracefully**: Implement proper error handling on the client side
5. **Implement Cancellation**: Allow users to cancel operations that are taking too long
6. **Respect Rate Limits**: AI operations are subject to rate limiting - implement retry logic with backoff
7. **Validate Inputs**: Ensure all required fields are properly formatted before sending
