Skip to content

Add AsyncAPI Contract for WebSocket Protocol with Automated Validation #59

@ahelme

Description

@ahelme

🔌 Add AsyncAPI Contract for WebSocket Protocol with Automated Validation

📋 Overview

Complete WebSocket protocol documentation using AsyncAPI specification to establish single source of truth for protocol contracts, following our established AgileAI methodology.

🎯 Goals

  • Single Source of Truth: AsyncAPI contract defines WebSocket protocol
  • Automated Validation: GitHub workflows prevent protocol drift
  • AI-Agent Discoverable: Standard AsyncAPI format for tooling integration
  • Documentation Integration: Extend existing docs server (port 3020)

📁 Current State Analysis

Existing Work Found:

  • chrome-extension/contracts/http.yaml - OpenAPI 3.0.3 REST contract
  • chrome-extension/contracts/events.md - Agent lifecycle events
  • chrome-extension/websocket.js - Complete WebSocket implementation
  • chrome-extension/contracts/QUALITY_GATE.md - Quality framework
  • Docs server at port 3020 serving OpenAPI docs

Missing:

  • WebSocket protocol contract (AsyncAPI format)
  • Runtime protocol validation
  • GitHub workflow validation
  • AsyncAPI integration in docs server

🚀 Implementation Plan

Phase 1: Create AsyncAPI Contract

File: chrome-extension/contracts/websocket.asyncapi.yaml

Based on real WebSocket analysis, document:

asyncapi: 3.0.0
info:
  title: Browser Tools WebSocket Protocol
  version: 1.1.0
channels:
  /extension-ws:
    messages:
      ping: { ... }
      pong: { ... }
      tabId: { ... }
      url: { ... }
      navigationResult: { ... }

Implementation Details:

  • Document all message types from actual logs
  • Include heartbeat protocol (30-second intervals)
  • Navigation result schema with performance metrics
  • Connection lifecycle events
  • Error handling patterns

Phase 2: Extend Documentation Server

File: chrome-extension/docs-server.mjs

Add AsyncAPI support:

// Serve AsyncAPI alongside existing OpenAPI
app.get('/asyncapi.yaml', (req, res) => {
  res.sendFile('contracts/websocket.asyncapi.yaml');
});

app.get('/ws-docs', (req, res) => {
  // AsyncAPI Studio UI for WebSocket docs
});

Integration Points:

  • Port 3020 serves both REST and WebSocket docs
  • Update health endpoint to include AsyncAPI links
  • Add WebSocket docs to main /docs page navigation

Phase 3: GitHub Workflow Validation

File: .github/workflows/protocol-validation.yml

name: Protocol Contract Validation
on:
  pull_request:
    paths:
      - 'chrome-extension/websocket.js'
      - 'chrome-extension/contracts/websocket.asyncapi.yaml'

jobs:
  validate-websocket-contract:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Validate AsyncAPI Contract
        run: |
          npm install -g @asyncapi/cli
          asyncapi validate chrome-extension/contracts/websocket.asyncapi.yaml
      
      - name: Check Protocol Consistency
        run: |
          echo "🚨 WebSocket files changed!"
          echo "Verify contract matches implementation:"
          echo "- chrome-extension/websocket.js" 
          echo "- chrome-extension/contracts/websocket.asyncapi.yaml"

Phase 4: Runtime Validation (Optional)

File: chrome-extension/websocket-validator.js

// Validate messages against AsyncAPI schema
const validator = new AsyncAPIValidator(contractSchema);
webSocket.on('message', (msg) => {
  if (!validator.validate(msg)) {
    console.warn('🚨 Protocol violation:', validator.errors);
  }
});

📚 Documentation Updates Required

README.md Updates:

## 📚 API Documentation

**Complete API Documentation** available at http://localhost:3020/docs

**Available Documentation:**
- 📚 **REST API**: Interactive OpenAPI/Swagger docs
- 🔌 **WebSocket Protocol**: AsyncAPI specification  
- 📄 **Raw Contracts**: OpenAPI and AsyncAPI specifications
- 🤖 **AI-Discoverable**: Standard format endpoints

**Endpoints:**
- http://localhost:3020/docs - Complete API documentation
- http://localhost:3020/ws-docs - WebSocket protocol docs
- http://localhost:3020/openapi.yaml - REST API contract
- http://localhost:3020/asyncapi.yaml - WebSocket protocol contract

CLAUDE.md Updates:

## API Documentation

**Comprehensive API Documentation** with both REST and WebSocket protocols:

```bash
# Start complete documentation server
./chrome-extension/start-docs.sh

AI-Agent Discoverable Endpoints:


### **chrome-extension/start-docs.sh Updates:**
```bash
echo "🔌 WebSocket Protocol docs: http://localhost:3020/ws-docs"
echo "📄 AsyncAPI contract: http://localhost:3020/asyncapi.yaml"

Acceptance Criteria

Documentation:

  • AsyncAPI contract created in contracts/websocket.asyncapi.yaml
  • Contract documents all message types from real WebSocket logs
  • README.md updated with WebSocket documentation links
  • CLAUDE.md updated with AsyncAPI endpoints
  • start-docs.sh includes WebSocket documentation URLs

Automation:

  • GitHub workflow validates AsyncAPI contract syntax
  • Workflow triggers on WebSocket-related file changes
  • Documentation server serves AsyncAPI specification
  • Health endpoint includes AsyncAPI discovery links

Integration:

  • AsyncAPI served alongside OpenAPI at port 3020
  • WebSocket docs integrated into main documentation UI
  • AI-agent discoverable endpoints for both protocols
  • Consistent documentation patterns with existing work

Quality:

  • Contract matches actual WebSocket implementation
  • All message types documented with examples
  • Schema validation passes AsyncAPI CLI
  • Documentation accessible and user-friendly

🔗 Related Work

  • Builds on existing OpenAPI contract: chrome-extension/contracts/http.yaml
  • Integrates with existing docs server: chrome-extension/docs-server.mjs
  • Follows AgileAI contract methodology established in Phase 1
  • Complements agent lifecycle events: chrome-extension/contracts/events.md

Priority: High - Protocol contracts are foundational for multi-agent development
Complexity: Medium - Extends existing patterns to new protocol type
Dependencies: None - builds on existing infrastructure

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions