|
| 1 | +# ═══════════════════════════════════════════════════════════════════════════════ |
| 2 | +# UNIFIED API LAYER v1 — REST + GraphQL + gRPC + WebSocket |
| 3 | +# Coverage: 130 TRI CLI commands (58 core + 72 demo/bench) |
| 4 | +# φ² + 1/φ² = 3 = TRINITY | Golden Chain #101 |
| 5 | +# ═══════════════════════════════════════════════════════════════════════════════ |
| 6 | + |
| 7 | +name: unified_api |
| 8 | +version: "1.0.0" |
| 9 | +language: zig |
| 10 | +module: api |
| 11 | + |
| 12 | +# ───────────────────────────────────────────────────────────────────────────── |
| 13 | +# TYPES |
| 14 | +# ───────────────────────────────────────────────────────────────────────────── |
| 15 | + |
| 16 | +types: |
| 17 | + # Protocol enum — which API protocol to use |
| 18 | + ApiProtocol: |
| 19 | + enum: |
| 20 | + - REST |
| 21 | + - GRAPHQL |
| 22 | + - GRPC |
| 23 | + - WEBSOCKET |
| 24 | + |
| 25 | + # Request/response wrapper |
| 26 | + ApiRequest: |
| 27 | + fields: |
| 28 | + command: String |
| 29 | + args: List<String> |
| 30 | + protocol: ApiProtocol |
| 31 | + request_id: String? |
| 32 | + |
| 33 | + ApiResponse: |
| 34 | + fields: |
| 35 | + success: Bool |
| 36 | + data: String? |
| 37 | + error: String? |
| 38 | + request_id: String? |
| 39 | + timestamp: Int |
| 40 | + |
| 41 | + # Command metadata (for auto-registry) |
| 42 | + CommandMetadata: |
| 43 | + fields: |
| 44 | + name: String |
| 45 | + category: String |
| 46 | + description: String |
| 47 | + api_exposed: Bool |
| 48 | + protocols: List<ApiProtocol> |
| 49 | + rate_limit: Int? |
| 50 | + auth_required: Bool |
| 51 | + |
| 52 | + # Server configuration |
| 53 | + ApiServerConfig: |
| 54 | + fields: |
| 55 | + rest_port: Int # 8080 |
| 56 | + graphql_port: Int # 8080 (same) |
| 57 | + grpc_port: Int # 9335 |
| 58 | + ws_port: Int # 8080 (same) |
| 59 | + max_connections: Int |
| 60 | + enable_cors: Bool |
| 61 | + enable_auth: Bool |
| 62 | + |
| 63 | +# ───────────────────────────────────────────────────────────────────────────── |
| 64 | +# BEHAVIORS — REST API Endpoints |
| 65 | +# ───────────────────────────────────────────────────────────────────────────── |
| 66 | + |
| 67 | +behaviors: |
| 68 | + # GET /api/commands — List all available commands |
| 69 | + - name: listCommands |
| 70 | + given: HTTP GET request to /api/commands |
| 71 | + when: Client requests command registry |
| 72 | + then: Return list of 130 commands with metadata |
| 73 | + |
| 74 | + # POST /api/execute — Execute any TRI command |
| 75 | + - name: executeCommand |
| 76 | + given: HTTP POST request to /api/execute with {command, args[]} |
| 77 | + when: Client wants to run TRI CLI command remotely |
| 78 | + then: Execute command and return ApiResponse |
| 79 | + |
| 80 | + # GET /api/status — Server + cluster status |
| 81 | + - name: getStatus |
| 82 | + given: HTTP GET request to /api/status |
| 83 | + when: Client requests current system status |
| 84 | + then: Return server health, active connections, cluster info |
| 85 | + |
| 86 | + # POST /api/chat — Chat endpoint |
| 87 | + - name: chatEndpoint |
| 88 | + given: HTTP POST request to /api/chat with {message, stream?} |
| 89 | + when: Client sends chat message |
| 90 | + then: Process through AI and return response |
| 91 | + |
| 92 | + # POST /api/gen — Generate code from .vibee spec |
| 93 | + - name: genEndpoint |
| 94 | + given: HTTP POST request to /api/gen with {spec_path} |
| 95 | + when: Client wants to generate Zig code |
| 96 | + then: Run VIBEE compiler and return generated code |
| 97 | + |
| 98 | + # POST /api/math — Sacred mathematics |
| 99 | + - name: mathEndpoint |
| 100 | + given: HTTP POST request to /api/math with {expression} |
| 101 | + when: Client calculates φ-based formulas |
| 102 | + then: Return result with sacred formatting |
| 103 | + |
| 104 | + # GET /api/multi-cluster/status — Cluster management |
| 105 | + - name: multiClusterStatus |
| 106 | + given: HTTP GET request to /api/multi-cluster/status |
| 107 | + when: Client requests cluster status |
| 108 | + then: Return CRDT state, nodes, $TRI balances |
| 109 | + |
| 110 | +# ───────────────────────────────────────────────────────────────────────────── |
| 111 | +# BEHAVIORS — GraphQL Schema |
| 112 | +# ───────────────────────────────────────────────────────────────────────────── |
| 113 | + |
| 114 | + # GraphQL: Auto-generated schema from .tri specs |
| 115 | + - name: graphqlSchema |
| 116 | + given: GraphQL request to /graphql |
| 117 | + when: Client queries with GraphQL syntax |
| 118 | + then: Execute query against auto-generated schema |
| 119 | + |
| 120 | + # Example GraphQL queries: |
| 121 | + # query { commands { name category description } } |
| 122 | + # query { status { cluster_id nodes { id tier earned_tri } } } |
| 123 | + # mutation { execute(command: "chat", args: ["hello"]) { success data } } |
| 124 | + |
| 125 | +# ───────────────────────────────────────────────────────────────────────────── |
| 126 | +# BEHAVIORS — gRPC Service |
| 127 | +# ───────────────────────────────────────────────────────────────────────────── |
| 128 | + |
| 129 | + # gRPC: Proto auto-generated from .tri specs |
| 130 | + - name: grpcService |
| 131 | + given: gRPC request to port 9335 |
| 132 | + when: Client calls any TRI command via gRPC |
| 133 | + then: Execute and return protobuf response |
| 134 | + |
| 135 | + # Proto service definition: |
| 136 | + # service TrinityEngine { |
| 137 | + # rpc Execute(CommandRequest) returns (CommandResponse); |
| 138 | + # rpc StreamExecute(CommandRequest) returns (stream CommandResponse); |
| 139 | + # rpc GetStatus(StatusRequest) returns (StatusResponse); |
| 140 | + # } |
| 141 | + |
| 142 | +# ───────────────────────────────────────────────────────────────────────────── |
| 143 | +# BEHAVIORS — WebSocket |
| 144 | +# ───────────────────────────────────────────────────────────────────────────── |
| 145 | + |
| 146 | + # WebSocket: Real-time status + command streaming |
| 147 | + - name: websocketHandler |
| 148 | + given: WebSocket connection to /ws |
| 149 | + when: Client connects for real-time updates |
| 150 | + then: Accept connection and enable bi-directional messaging |
| 151 | + |
| 152 | + # WS: Real-time cluster status updates |
| 153 | + - name: clusterStatusStream |
| 154 | + given: WebSocket connection subscribed to cluster.status |
| 155 | + when: Cluster state changes (node joins/leaves, $TRI earned) |
| 156 | + then: Push JSON update to all subscribed clients |
| 157 | + |
| 158 | + # WS: Command execution streaming |
| 159 | + - name: commandStream |
| 160 | + given: WebSocket connection executing long-running command |
| 161 | + when: Command produces output incrementally |
| 162 | + then: Stream each output line as WS message |
| 163 | + |
| 164 | +# ───────────────────────────────────────────────────────────────────────────── |
| 165 | +# BEHAVIORS — Auto-Documentation |
| 166 | +# ───────────────────────────────────────────────────────────────────────────── |
| 167 | + |
| 168 | + # OpenAPI 3.0 spec generation |
| 169 | + - name: openApiSpec |
| 170 | + given: HTTP GET request to /api/openapi.json |
| 171 | + when: Client needs OpenAPI specification |
| 172 | + then: Return auto-generated OpenAPI 3.0 JSON |
| 173 | + |
| 174 | + # GraphQL Playground |
| 175 | + - name: graphqlPlayground |
| 176 | + given: HTTP GET request to /graphql |
| 177 | + when: Browser requests GraphQL interface |
| 178 | + then: Return GraphQL Playground HTML |
| 179 | + |
| 180 | + # gRPC reflection |
| 181 | + - name: grpcReflection |
| 182 | + given: gRPC reflection request |
| 183 | + when: Client needs service definition |
| 184 | + then: Return proto descriptors for all services |
| 185 | + |
| 186 | +# ───────────────────────────────────────────────────────────────────────────── |
| 187 | +# BEHAVIORS — Command Registry (Auto-Discovery) |
| 188 | +# ───────────────────────────────────────────────────────────────────────────── |
| 189 | + |
| 190 | + # Auto-register all TRI CLI commands on startup |
| 191 | + - name: autoRegisterCommands |
| 192 | + given: Server startup |
| 193 | + when: ApiServer initializes |
| 194 | + then: Scan all 130 commands and build metadata registry |
| 195 | + |
| 196 | + # Rate limiting per command |
| 197 | + - name: rateLimitCommand |
| 198 | + given: Command execution request |
| 199 | + when: Command exceeds rate limit |
| 200 | + then: Return 429 Too Many Requests with retry-after |
| 201 | + |
| 202 | + # Authentication (optional, per-command) |
| 203 | + - name: authenticateRequest |
| 204 | + given: Command marked auth_required=true |
| 205 | + when: Client sends request without auth |
| 206 | + then: Return 401 Unauthorized |
| 207 | + |
| 208 | +# ───────────────────────────────────────────────────────────────────────────── |
| 209 | +# CATEGORIES — 130 Commands grouped by function |
| 210 | +# ───────────────────────────────────────────────────────────────────────────── |
| 211 | + |
| 212 | +categories: |
| 213 | + # Core (8): chat, code, fix, explain, test, doc, refactor, reason |
| 214 | + # VIBEE (5): gen, convert, serve, bench, evolve |
| 215 | + # Git (4): commit, diff, status, log |
| 216 | + # Pipeline (3): pipeline, decompose, plan |
| 217 | + # Multi-Cluster (1): multi_cluster |
| 218 | + # Verify (2): verify, verdict |
| 219 | + # Spec (2): spec_create, loop_decide |
| 220 | + # TVC (2): tvc_demo, tvc_stats |
| 221 | + # Demos (72): all *_demo and *_bench commands |
| 222 | + # Math (9): math, constants, phi, fib, lucas, spiral, gematria, formula, sacred |
| 223 | + # Intelligence (1): intelligence |
| 224 | + # Doctor (5): doctor, clean, fmt, stats, igla |
| 225 | + # Identity (3): identity, swarm, govern |
| 226 | + # Analyze (3): analyze, search, context_info |
| 227 | + # Advanced (8): auto_commit, ml_optimize, deploy_dashboard, self_host, safeguards_show, safeguards_disable |
| 228 | + # Info (4): deps, info, version, help |
| 229 | + |
| 230 | +# ───────────────────────────────────────────────────────────────────────────── |
| 231 | +# PORT MAPPING |
| 232 | +# ───────────────────────────────────────────────────────────────────────────── |
| 233 | + |
| 234 | +ports: |
| 235 | + REST: 8080 |
| 236 | + GraphQL: 8080/graphql |
| 237 | + gRPC: 9335 |
| 238 | + WebSocket: 8080/ws |
| 239 | + OpenAPI: 8080/api/openapi.json |
| 240 | + Playground: 8080/graphql (browser) |
| 241 | + |
| 242 | +# ───────────────────────────────────────────────────────────────────────────── |
| 243 | +# KPI — Success Metrics for Golden Chain #101 |
| 244 | +# ───────────────────────────────────────────────────────────────────────────── |
| 245 | + |
| 246 | +kpi: |
| 247 | + - "130 commands × 4 protocols = 520 endpoints" |
| 248 | + - "REST latency < 50ms p95" |
| 249 | + - "GraphQL query < 100ms p95" |
| 250 | + - "gRPC call < 25ms p95" |
| 251 | + - "WS message < 10ms p95" |
| 252 | + - "OpenAPI spec auto-generated" |
| 253 | + - "GraphQL schema auto-generated" |
| 254 | + - "gRPC proto auto-generated" |
0 commit comments