src — api

Module: src-api Cohesion: 0.80 Members: 0

src — api

The src/api module in Code Buddy is responsible for enabling external communication and integration. It provides two primary mechanisms: a local REST API server for programmatic interaction and a webhook manager for sending event notifications to external services.

1. REST API Server (src/api/rest-server.ts)

The RestApiServer provides a local HTTP/REST interface, allowing external scripts or applications to interact with Code Buddy. This enables automation of tasks such as sending prompts, executing tools, and querying internal status.

1.1 Purpose

1.2 Key Components

1.3 How it Works

  1. Initialization:

  1. Starting the Server:

  1. Request Handling (handleRequest):

  1. Route Management:

1.4 Request Handling Flow

graph TD
    A[HTTP Request] --> B{handleRequest};
    B --> C{Parse URL, Method, Headers};
    C --> D{Apply CORS Headers};
    D --> E{Handle OPTIONS / Check API Key};
    E -- Unauthorized --> F[Send 401 Response];
    E -- Authorized --> G{Parse Body (if POST/PUT/PATCH)};
    G --> H[Create ApiRequest Object];
    H --> I{findHandler(method, path)};
    I -- No Handler --> J[Send 404 Response];
    I -- Handler Found --> K[Execute RouteHandler];
    K --> L[Get ApiResponse];
    L --> M[sendResponse];
    M --> N[HTTP Response];
    M --> O[Emit 'request' event];

1.5 Integration with Code Buddy Core

The RestApiServer acts as a facade. Its default routes for /api/prompt, /api/tools/:tool, /api/sessions, and /api/metrics do not contain the core logic themselves. Instead, they check if corresponding public handler functions (onPrompt, onToolExecute, onGetSessions, onGetMetrics) have been assigned. The main Code Buddy application is responsible for setting these functions, effectively injecting the business logic into the API server.

Example:

import { getApiServer } from './api/rest-server';
import { handlePrompt, executeTool, getActiveSessions, getSystemMetrics } from './core-logic'; class="hl-cmt">// Assume these exist

const apiServer = getApiServer();
apiServer.onPrompt = handlePrompt;
apiServer.onToolExecute = executeTool;
apiServer.onGetSessions = getActiveSessions;
apiServer.onGetMetrics = getSystemMetrics;

apiServer.start().then(() => console.log('API Server running'));

2. Webhook Manager (src/api/webhooks.ts)

The WebhookManager provides a robust system for sending event-driven notifications to external HTTP endpoints. This allows Code Buddy to integrate with other systems that react to specific events within the application.

2.1 Purpose

2.2 Key Components

2.3 How it Works

  1. Initialization & Configuration:

  1. Event Emission (emit):

  1. Delivery (deliver):

  1. Sending Request (sendRequest):

  1. Security:

  1. Delivery History & Retries:

  1. Testing:

2.4 Webhook Delivery Flow

graph TD
    A[Code Buddy Event Occurs] --> B[emitWebhook(event, data)];
    B --> C[getWebhookManager().emit(event, data)];
    C --> D{Get Webhooks for Event};
    D -- For Each Webhook --> E[deliver(webhook, payload)];
    E --> F{Attempt Delivery (sendRequest)};
    F -- Success --> G[Record Success, Break Retry Loop];
    F -- Failure --> H{Retry?};
    H -- Yes (Exponential Backoff) --> F;
    H -- No (Max Retries) --> I[Record Failure];
    G --> J[Add to Delivery History];
    I --> J;

3. Interconnections and External Dependencies

The src/api module serves as a crucial interface layer, connecting Code Buddy's internal logic to the outside world.

3.1 Internal Dependencies

3.2 External Interactions

3.3 Observed Call Graph Anomalies

The provided call graph data shows a few unusual connections that are likely artifacts of static analysis or test-time interactions rather than core runtime dependencies:

Developers should be aware of these potential misinterpretations when analyzing the call graph and focus on the intended functional roles of each component. The primary interaction of sendRequest is to make an HTTP request, not to manage sandboxes.