src — copilot

Module: src-copilot Cohesion: 0.80 Members: 0

src — copilot

The src/copilot/copilot-proxy.ts module provides a robust, configurable HTTP proxy server designed to emulate the GitHub Copilot API. Its primary purpose is to intercept and process AI completion requests, delegating the actual generation of completions to a custom, pluggable handler. This allows other modules to integrate their own AI models or logic while benefiting from the proxy's built-in features like authentication, rate limiting, and standardized API responses.

Overview

The CopilotProxy class creates a local HTTP server that listens for incoming requests, primarily targeting the /v1/completions endpoint. It acts as a middleware layer, handling common concerns such as:

This module is crucial for integrating custom AI completion services into environments that expect a Copilot-compatible API.

Core Components

CopilotProxy Class

The central component of this module. CopilotProxy is an EventEmitter, allowing consumers to subscribe to lifecycle events like listening (when the server starts) and error (for unhandled server errors).

Key Methods:

CopilotProxyConfig Interface

This interface defines the configuration options for the CopilotProxy.

export interface CopilotProxyConfig {
  port: number;
  host: string;
  authToken?: string;
  requireAuth?: boolean;
  maxTokens: number;
  maxTokensLimit?: number;
  rateLimitPerMinute?: number;
  onCompletion: (req: CopilotCompletionRequest) => Promise<CopilotCompletionResponse>;
}

Key Configuration Properties:

CopilotCompletionRequest & CopilotCompletionResponse Interfaces

These interfaces define the standardized structure for completion requests and responses, mirroring the expected format of the Copilot API.

How It Works

The CopilotProxy operates by creating a standard Node.js HTTP server and routing incoming requests through a series of checks and handlers.

sequenceDiagram
    participant Client
    participant CopilotProxy
    participant CustomCompletionHandler

    Client->>CopilotProxy: HTTP Request (e.g., POST /v1/completions)
    CopilotProxy->>CopilotProxy: handleRequest(req, res)
    CopilotProxy->>CopilotProxy: checkRateLimit(clientIp)
    alt Rate Limit Exceeded
        CopilotProxy-->>Client: 429 Rate Limit Exceeded
    else
        CopilotProxy->>CopilotProxy: authenticate(req)
        alt Unauthorized
            CopilotProxy-->>Client: 401 Unauthorized
        else
            alt GET /health
                CopilotProxy-->>Client: 200 OK
            alt GET /v1/models
                CopilotProxy-->>Client: 200 OK (model list)
            alt POST /v1/completions
                CopilotProxy->>CopilotProxy: parseBody(req)
                CopilotProxy->>CopilotProxy: Validate & Clamp max_tokens
                CopilotProxy->>CustomCompletionHandler: config.onCompletion(request)
                CustomCompletionHandler-->>CopilotProxy: CopilotCompletionResponse
                CopilotProxy-->>Client: 200 OK (Completion Response)
            else Other Path
                CopilotProxy-->>Client: 404 Not Found
            end
        end
    end

  1. Server Start: When start() is called, an http.Server instance is created and begins listening on the configured port and host. All incoming requests are directed to the handleRequest method.
  2. Request Handling (handleRequest):

  1. Error Handling:

Integration and Extensibility

The CopilotProxy is designed to be easily integrated into any Node.js application that needs to expose a Copilot-compatible API for custom AI models.

The primary point of integration and extensibility is the onCompletion callback in CopilotProxyConfig. This function is where you connect your custom AI logic.

Example Usage:

import { CopilotProxy, CopilotCompletionRequest, CopilotCompletionResponse } from &#39;./copilot-proxy&#39;;

async function myCustomCompletionHandler(
  request: CopilotCompletionRequest
): Promise<CopilotCompletionResponse> {
  console.log(&#39;Received completion request:&#39;, request.prompt);

  class="hl-cmt">// Simulate an AI model&#39;s response
  const generatedText = `class="hl-cmt">// This is a simulated completion for: ${request.prompt}`;

  return {
    id: &#39;simulated-completion-123&#39;,
    choices: [{
      text: generatedText,
      index: 0,
      finish_reason: &#39;stop&#39;,
    }],
    usage: {
      prompt_tokens: request.prompt.length, class="hl-cmt">// Simplified token count
      completion_tokens: generatedText.length,
      total_tokens: request.prompt.length + generatedText.length,
    },
  };
}

async function main() {
  const proxy = new CopilotProxy({
    port: 3000,
    host: &#39;127.0.0.1&#39;,
    authToken: &#39;my-secret-token&#39;, class="hl-cmt">// Optional: set for authentication
    maxTokens: 100,
    maxTokensLimit: 500,
    rateLimitPerMinute: 10,
    onCompletion: myCustomCompletionHandler, class="hl-cmt">// Plug in your custom logic here
  });

  proxy.on(&#39;listening&#39;, () => {
    console.log(`Copilot Proxy listening on http:class="hl-cmt">//${proxy.config.host}:${proxy.config.port}`);
    console.log(&#39;Try: curl -H "Authorization: Bearer my-secret-token" -X POST -H "Content-Type: application/json" -d \&#39;{"prompt": "function helloWorld() {"}\&#39; http:class="hl-cmt">//127.0.0.1:3000/v1/completions&#39;);
  });

  proxy.on(&#39;error&#39;, (err) => {
    console.error(&#39;Copilot Proxy error:&#39;, err);
  });

  await proxy.start();

  class="hl-cmt">// To stop the proxy later:
  class="hl-cmt">// await proxy.stop();
}

main().catch(console.error);

This module provides a flexible and robust foundation for building custom AI completion services that are compatible with existing Copilot-aware clients.