|
| 1 | +# Copilot Instructions - Qoder OpenAI Proxy |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +This is an OpenAI-compatible API proxy for Qoder CLI (qodercli). It translates OpenAI-format API requests into qodercli commands, enabling any OpenAI-compatible tool (Cursor, LangChain, Open WebUI) to use Qoder models. |
| 6 | + |
| 7 | +Key responsibilities: |
| 8 | +- Accept /v1/chat/completions and /v1/completions requests in OpenAI format |
| 9 | +- Spawn qodercli child processes with appropriate model/prompt arguments |
| 10 | +- Stream responses back to clients via Server-Sent Events (SSE) |
| 11 | +- Provide a web dashboard for testing and monitoring at /dashboard/ |
| 12 | + |
| 13 | +## Build, Test, and Run |
| 14 | + |
| 15 | +Start server (development): |
| 16 | +npm run dev # Runs with --watch for auto-reload on file changes |
| 17 | + |
| 18 | +Start server (production): |
| 19 | +npm start # Runs src/server.js directly |
| 20 | + |
| 21 | +Docker: |
| 22 | +docker build -t qoder-proxy . |
| 23 | +docker run -p 3000:3000 -e QODER_PERSONAL_ACCESS_TOKEN="..." -e PROXY_API_KEY="..." qoder-proxy |
| 24 | + |
| 25 | +No tests or linters are configured. |
| 26 | + |
| 27 | +## Architecture |
| 28 | + |
| 29 | +### Core Flow |
| 30 | + |
| 31 | +Client → Express → Auth → Routes → spawn.js → qodercli (child process) → Parse stream-json output → SSE/JSON response |
| 32 | + |
| 33 | +1. Routes (src/routes/) receive OpenAI-format requests |
| 34 | +2. format.js translates OpenAI model names → Qoder tiers and converts messages[] → single prompt string |
| 35 | +3. spawn.js spawns qodercli -p <prompt> -f stream-json --model <tier> as a child process |
| 36 | +4. Stream parsing reads line-delimited JSON from stdout, extracts text, and sends SSE chunks |
| 37 | +5. Logging (logStore.js) captures all requests/responses in RAM (circular buffer, max 500 entries) |
| 38 | + |
| 39 | +### Key Components |
| 40 | + |
| 41 | +| Component | Purpose | |
| 42 | +|-----------|---------| |
| 43 | +| src/server.js | Express app setup, route mounting, startup health checks | |
| 44 | +| src/config.js | Central config from env vars (PORT, API keys, timeouts) | |
| 45 | +| src/helpers/spawn.js | Spawns qodercli, handles stdout/stderr, implements timeout logic | |
| 46 | +| src/helpers/format.js | Model mapping (OpenAI aliases → Qoder tiers), message→prompt conversion, response builders | |
| 47 | +| src/store/logStore.js | In-memory circular log buffer for requests + system events | |
| 48 | +| src/middleware/auth.js | Bearer token validation for /v1/* endpoints | |
| 49 | +| src/middleware/dashboardAuth.js | Cookie-based HMAC auth for /dashboard/* | |
| 50 | +| src/routes/chat.js | /v1/chat/completions endpoint (supports streaming + non-streaming) | |
| 51 | +| src/routes/completions.js | /v1/completions (legacy text completion) | |
| 52 | +| src/routes/dashboard.js | Dashboard UI + API endpoints for logs/playground | |
| 53 | + |
| 54 | +## Key Conventions |
| 55 | + |
| 56 | +### 1. Model Mapping Strategy |
| 57 | + |
| 58 | +The proxy accepts OpenAI/Anthropic model names and maps them to Qoder tiers: |
| 59 | + |
| 60 | +gpt-4, gpt-4o, claude-3.5-sonnet → auto (paid) |
| 61 | +o1, claude-3-opus → ultimate (paid) |
| 62 | +o1-mini, claude-3-sonnet → performance (paid) |
| 63 | +claude-3.5-haiku, gemini-flash → efficient (paid) |
| 64 | +gpt-3.5-turbo, gpt-4o-mini → lite (free) |
| 65 | + |
| 66 | +- Direct Qoder tier names (auto, lite, etc.) pass through unchanged |
| 67 | +- Unknown names pass through as-is (allows custom model IDs) |
| 68 | +- Default model is lite if none specified |
| 69 | +- Model mapping logic lives in src/helpers/format.js (ALIAS_MAP and getModelMapping) |
| 70 | + |
| 71 | +### 2. Qodercli Integration |
| 72 | + |
| 73 | +Critical details: |
| 74 | +- Uses qodercli -p <prompt> -f stream-json --model <tier> for all requests |
| 75 | +- Parses line-delimited JSON from stdout (format: {"type":"assistant","subtype":"message","message":{...}}) |
| 76 | +- Must handle both Windows (cmd.exe /c qodercli.cmd) and Unix (qodercli) spawn paths |
| 77 | +- Implements timeout killing (default 120s) to prevent hung processes |
| 78 | +- Cleans up child processes on client disconnect (req.on('close')) |
| 79 | + |
| 80 | +Never: |
| 81 | +- Buffer full response before sending (breaks streaming) |
| 82 | +- Forget to kill child processes on errors or timeouts |
| 83 | +- Parse non-JSON lines (some lines may be warnings/errors) |
| 84 | + |
| 85 | +### 3. Message → Prompt Conversion |
| 86 | + |
| 87 | +OpenAI messages array → single prompt string for qodercli (src/helpers/format.js — messagesToPrompt): |
| 88 | + |
| 89 | +- system messages → System: <content> |
| 90 | +- user messages → User: <content> |
| 91 | +- assistant messages → Assistant: <content> |
| 92 | +- Multi-part content [{type:'text', text:'...'}] → flattened to plain text |
| 93 | +- Last system message wins if multiple provided |
| 94 | + |
| 95 | +### 4. Response Building |
| 96 | + |
| 97 | +Two response formats: |
| 98 | + |
| 99 | +- Streaming: SSE chunks with delta.content, final chunk with finish_reason, then [DONE] |
| 100 | +- Non-streaming: Full response with message.content |
| 101 | + |
| 102 | +All responses follow OpenAI format (id, object, created, model, choices, usage). |
| 103 | +Response builders live in src/helpers/format.js (buildStreamChunk, buildDoneChunk, buildFullChatResponse, etc.). |
| 104 | + |
| 105 | +### 5. Logging Architecture |
| 106 | + |
| 107 | +All logging goes through src/store/logStore.js: |
| 108 | + |
| 109 | +- addRequest(entry) — logs API requests with payload, status, duration |
| 110 | +- addSystem(message, level, source) — logs system events (startup, errors, auth) |
| 111 | +- Circular buffers (max 500 entries each by default) |
| 112 | +- Logs are RAM-only (cleared on restart) |
| 113 | +- Payloads are truncated to LOG_BODY_MAX_BYTES (default 8KB) |
| 114 | + |
| 115 | +Console output format: [timestamp] METHOD /path → status (duration) [stream] |
| 116 | + |
| 117 | +### 6. Authentication Patterns |
| 118 | + |
| 119 | +Two auth systems: |
| 120 | + |
| 121 | +/v1/* endpoints: |
| 122 | +- Bearer token auth (PROXY_API_KEY env var) |
| 123 | +- Skips auth entirely if PROXY_API_KEY not set |
| 124 | +- Middleware: src/middleware/auth.js |
| 125 | + |
| 126 | +/dashboard/* routes: |
| 127 | +- Cookie-based HMAC session tokens |
| 128 | +- Login form at /dashboard/login (checks DASHBOARD_PASSWORD) |
| 129 | +- Token = base64url(timestamp.random.hmac(timestamp.random)) |
| 130 | +- 7-day cookie expiry |
| 131 | +- Middleware: src/middleware/dashboardAuth.js |
| 132 | + |
| 133 | +### 7. Error Handling |
| 134 | + |
| 135 | +- 400 for invalid requests (missing messages, empty prompt) |
| 136 | +- 401 for auth failures |
| 137 | +- 500 for qodercli spawn errors or non-zero exit codes |
| 138 | +- 501 for unsupported endpoints (/v1/embeddings) |
| 139 | +- 504 for timeouts |
| 140 | + |
| 141 | +Return OpenAI-format errors: |
| 142 | +{ |
| 143 | + "error": { |
| 144 | + "message": "...", |
| 145 | + "type": "invalid_request_error|api_error|timeout_error|not_implemented_error", |
| 146 | + "code": "invalid_api_key|endpoint_not_supported|...", |
| 147 | + "details": "..." (optional, for debugging) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +### 8. Environment Configuration |
| 152 | + |
| 153 | +All config lives in src/config.js, loaded from .env: |
| 154 | + |
| 155 | +Required for production: |
| 156 | +- QODER_PERSONAL_ACCESS_TOKEN — Qoder API credentials |
| 157 | +- PROXY_API_KEY — Bearer token for /v1/* auth |
| 158 | +- DASHBOARD_PASSWORD — Password for /dashboard/ access |
| 159 | + |
| 160 | +Optional tuning: |
| 161 | +- PORT (default 3000) |
| 162 | +- QODER_TIMEOUT_MS (default 120000) |
| 163 | +- LOG_MAX_ENTRIES (default 500) |
| 164 | +- LOG_BODY_MAX_BYTES (default 8192) |
| 165 | +- CORS_ORIGIN (default *) |
| 166 | +- DASHBOARD_ENABLED (default true) |
| 167 | +- DASHBOARD_SECRET (auto-generated if not set) |
| 168 | + |
| 169 | +## Common Tasks |
| 170 | + |
| 171 | +### Adding a New Model Alias |
| 172 | + |
| 173 | +Edit src/helpers/format.js: |
| 174 | + |
| 175 | +1. Add to ALIAS_MAP (maps OpenAI name → qodercli tier) |
| 176 | +2. Add to OPENAI_ALIASES in src/routes/misc.js (for /v1/models endpoint) |
| 177 | + |
| 178 | +### Adding a New Qoder Model |
| 179 | + |
| 180 | +Edit src/helpers/format.js: |
| 181 | + |
| 182 | +1. Add to QODER_MODELS array with {id, label, tier, description} |
| 183 | + |
| 184 | +### Modifying Stream Parsing |
| 185 | + |
| 186 | +Edit src/helpers/spawn.js — runQoderRequest(): |
| 187 | + |
| 188 | +- Look for data.type === 'assistant' && data.subtype === 'message' |
| 189 | +- Extract content via extractTextContent(data.message) |
| 190 | +- Handle stop_reason from data.message.stop_reason |
| 191 | + |
| 192 | +### Changing Timeout Behavior |
| 193 | + |
| 194 | +Edit QODER_TIMEOUT_MS in src/config.js or .env file. |
| 195 | +Timeout logic lives in src/helpers/spawn.js (setTimeout → child.kill()). |
| 196 | + |
| 197 | +## Important Notes |
| 198 | + |
| 199 | +- **RAM-only design**: All logs/state are in-memory. Restart = data loss. |
| 200 | +- **Process cleanup**: Always kill child processes on errors, timeouts, or client disconnects to avoid zombies. |
| 201 | +- **SSE headers**: Must set X-Accel-Buffering: no for nginx compatibility. |
| 202 | +- **Windows compatibility**: All child_process spawns check process.platform === 'win32' and use cmd.exe wrapper. |
| 203 | +- **No token counting**: Qoder CLI doesn't provide token usage — all usage fields return null. |
| 204 | +- **Embeddings not supported**: /v1/embeddings returns 501 with explanation. |
0 commit comments