Skip to content

Repository files navigation

@aprovan/copilot-proxy

GitHub Copilot OpenAI-compatible proxy.

Use GitHub Copilot as a backend for OpenAI-compatible API calls, either directly via the SDK or through a local HTTP proxy server.

Installation

pnpm add @aprovan/copilot-proxy

Quick Start

1. Authenticate

npx copilot-proxy connect

This will initiate the GitHub device flow - you'll be given a code to enter at github.com.

2. Start the Server

npx copilot-proxy serve

The server will start on http://127.0.0.1:6433 by default.

3. Use with OpenAI SDK

import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'http://127.0.0.1:6433/v1',
  apiKey: 'not-needed', // Required by SDK but not validated
});

const response = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }],
});

CLI Commands

copilot-proxy connect      # Authenticate with GitHub
copilot-proxy disconnect   # Remove stored credentials
copilot-proxy status       # Check connection status
copilot-proxy models       # List available models
copilot-proxy serve        # Start HTTP proxy server
  --port, -p <port>        # Port (default: 6433)
  --host, -h <host>        # Host (default: 127.0.0.1)
  --verbose, -v            # Enable verbose logging

SDK Usage

Use the SDK directly without starting an HTTP server:

import { CopilotClient, connect, isConfigured } from '@aprovan/copilot-proxy';

// Check if authenticated
if (!await isConfigured()) {
  const { userCode, verificationUrl, waitForAuth } = await connect();
  console.log(`Go to ${verificationUrl} and enter: ${userCode}`);
  await waitForAuth();
}

// Create client
const client = new CopilotClient();

// List models
const models = await client.listModels();
console.log(models);

// Chat completion
const response = await client.createChatCompletion({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }],
});

// Streaming
for await (const chunk of client.createChatCompletionStream({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Tell me a story' }],
})) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

Server Integration

Start the proxy server programmatically:

import { createProxyServer } from '@aprovan/copilot-proxy/server';

const server = createProxyServer({
  port: 6433,
  host: '127.0.0.1',
  verbose: true,
});

await server.start();
console.log('Server running on', server.address());

// Later...
await server.stop();

API Endpoints

The HTTP server implements OpenAI-compatible endpoints:

Endpoint Method Description
/v1/models GET List available models
/v1/chat/completions POST Create chat completion (streaming supported)
/health GET Health check

Transport Layer

For advanced use cases, get an OpenAI-compatible transport:

import { getOpenAICompatibleTransport } from '@aprovan/copilot-proxy';

const transport = await getOpenAICompatibleTransport();

// Use with custom fetch calls
const response = await transport.fetch(
  `${transport.baseURL}${transport.chatCompletionsPath}`,
  {
    method: 'POST',
    body: JSON.stringify({
      model: 'gpt-4o',
      messages: [{ role: 'user', content: 'Hello!' }],
    }),
  }
);

License

MIT

About

GitHub Copilot OpenAI-compatible proxy

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages