@nick3/copilot-api 1.2.7 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -23,7 +23,8 @@ English | [中文](./README_CN.md)
23
23
 
24
24
  ---
25
25
 
26
- **Note:** If you are using [opencode](https://github.com/sst/opencode), you do not need this project. Opencode supports GitHub Copilot provider out of the box.
26
+ > [!NOTE]
27
+ > [opencode](https://github.com/sst/opencode) already ships with a built-in GitHub Copilot provider, so you may not need this project for basic usage. This proxy is still useful if you want OpenCode to talk to Copilot through `@ai-sdk/anthropic`, preserve Anthropic Messages semantics for tool use, prefer the native Messages API over plain Chat Completions for Claude-family models, use `gpt-5.4` phase-aware commentary, or fine-tune premium-request usage with small-model fallbacks.
27
28
 
28
29
  ---
29
30
 
@@ -31,9 +32,17 @@ English | [中文](./README_CN.md)
31
32
 
32
33
  A reverse-engineered proxy for the GitHub Copilot API that exposes it as an OpenAI and Anthropic compatible service. This allows you to use GitHub Copilot with any tool that supports the OpenAI Chat Completions API or the Anthropic Messages API, including to power [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview).
33
34
 
35
+ Compared with routing everything through plain Chat Completions compatibility, this proxy can prefer Copilot's native Anthropic-style Messages API for Claude-family models, preserve more native thinking/tool semantics, reduce unnecessary Premium request consumption on warmup or resumed tool turns, and expose phase-aware `gpt-5.4` / `gpt-5.3-codex` responses that are easier for users to follow.
36
+
34
37
  ## Features
35
38
 
36
39
  - **OpenAI & Anthropic Compatibility**: Exposes GitHub Copilot as an OpenAI-compatible (`/v1/responses`, `/v1/chat/completions`, `/v1/models`, `/v1/embeddings`) and Anthropic-compatible (`/v1/messages`) API.
40
+ - **Anthropic-First Routing for Claude Models**: When a model supports Copilot's native `/v1/messages` endpoint, the proxy prefers it over `/responses` or `/chat/completions`, preserving Anthropic-style `tool_use` / `tool_result` flows and more Claude-native behavior.
41
+ - **Fewer Unnecessary Premium Requests**: Reduces wasted premium usage by routing warmup and compact/background requests to `smallModel`, merging `tool_result` follow-ups back into the tool flow, and treating resumed tool turns as continuation traffic instead of fresh premium interactions.
42
+ - **Phase-Aware `gpt-5.4` and `gpt-5.3-codex`**: These models can emit user-friendly commentary before deeper reasoning or tool use, so long-running coding actions are easier to understand instead of appearing as a sudden tool burst.
43
+ - **Claude Native Beta Support**: On the Messages API path, supports Anthropic-native capabilities such as `interleaved-thinking`, `advanced-tool-use`, and `context-management`, which are difficult or unavailable through plain Chat Completions compatibility.
44
+ - **Subagent Marker Integration**: Optional Claude Code and opencode plugins can inject `__SUBAGENT_MARKER__...` and propagate `x-session-id` so subagent traffic keeps the correct root session and agent/user semantics.
45
+ - **OpenCode via `@ai-sdk/anthropic`**: Point OpenCode at this proxy as an Anthropic provider so Anthropic Messages semantics, premium-request optimizations, and Claude-native behavior are preserved end to end.
37
46
  - **Claude Code Integration**: Easily configure and launch [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) to use Copilot as its backend with a simple command-line flag (`--claude-code`).
38
47
  - **Usage Dashboard**: A web-based dashboard to monitor your Copilot API usage, view quotas, and see detailed statistics.
39
48
  - **Admin UI**: Modern admin console (`/admin`) to inspect account runtime status and request history, with rich filtering and a request-detail JSON viewer (search/copy/download). Includes theme (system/light/dark) and motion (magic/subtle/off) toggles.
@@ -48,6 +57,45 @@ A reverse-engineered proxy for the GitHub Copilot API that exposes it as an Open
48
57
  - **Custom Data Directory**: Change the default data directory (where tokens and config are stored) by setting `COPILOT_API_HOME` environment variable.
49
58
  - **Multi-Provider Anthropic Proxy Routes**: Add global provider configs and call external Anthropic-compatible APIs via `/:provider/v1/messages` and `/:provider/v1/models`.
50
59
 
60
+ ## Better Agent Semantics
61
+
62
+ ### Native Anthropic Messages API when available
63
+
64
+ For models that advertise Copilot support for `/v1/messages`, this project sends the request to the native Messages API first and only falls back to `/responses` or `/chat/completions` when needed.
65
+
66
+ Compared with using Claude-family models only through Chat Completions compatibility, the Messages API path keeps more Anthropic-native behavior, including support for:
67
+
68
+ - `interleaved-thinking-2025-05-14`
69
+ - `advanced-tool-use-2025-11-20`
70
+ - `context-management-2025-06-27`
71
+
72
+ Supported `anthropic-beta` values are filtered and forwarded on the native Messages path, and `interleaved-thinking` is added automatically when a thinking budget is requested for non-adaptive extended thinking.
73
+
74
+ ### Fewer unnecessary Premium requests
75
+
76
+ The proxy includes request-accounting safeguards designed for tool-heavy coding workflows:
77
+
78
+ - tool-less warmup or probe requests can be forced onto `smallModel` so background checks do not spend premium usage;
79
+ - compact/background requests can be downgraded to `smallModel` automatically;
80
+ - mixed `tool_result` + reminder text blocks are merged back into the `tool_result` flow instead of being counted like fresh user turns;
81
+ - `x-initiator` is derived from the latest message or item, not stale assistant history.
82
+
83
+ This helps resumed tool turns continue the existing workflow instead of consuming an extra Premium request as a brand-new interaction.
84
+
85
+ ### Phase-aware `gpt-5.4` and `gpt-5.3-codex`
86
+
87
+ By default, the built-in `extraPrompts` for `gpt-5.4` and `gpt-5.3-codex` enable intermediary-update behavior, and the proxy translates assistant turns into `phase: "commentary"` before tool calls and `phase: "final_answer"` for the final response.
88
+
89
+ That gives clients a short, user-friendly explanation of what the model is about to do before deeper reasoning or tool execution begins.
90
+
91
+ ### Subagent marker integration
92
+
93
+ For subagent-based clients, this project can preserve root session context and correctly classify subagent-originated traffic.
94
+
95
+ The marker flow uses `__SUBAGENT_MARKER__...` inside a `<system-reminder>` block together with root `x-session-id` propagation. When a marker is detected, the proxy can keep the parent session identity, infer `x-initiator: agent`, and tag the interaction as subagent traffic instead of a fresh top-level request.
96
+
97
+ Optional marker producers are included for both Claude Code and opencode; see [Subagent Marker Integration](#subagent-marker-integration-optional) below for setup details.
98
+
51
99
  ## Demo
52
100
 
53
101
  https://github.com/user-attachments/assets/7654b383-669d-4eb9-b23c-06d7aefee8c5
@@ -250,24 +298,32 @@ The `<target>` can be either the account ID (GitHub username) or a 1-based index
250
298
  "apiKeys": []
251
299
  },
252
300
  "providers": {
253
- "openrouter": {
301
+ "custom": {
254
302
  "type": "anthropic",
255
303
  "enabled": true,
256
- "baseUrl": "https://openrouter.ai/api",
304
+ "baseUrl": "your-base-url",
257
305
  "apiKey": "sk-your-provider-key",
258
- "defaultTemperature": 0.7,
259
- "defaultTopP": 0.9,
260
- "defaultTopK": 20
306
+ "models": {
307
+ "kimi-k2.5": {
308
+ "temperature": 1,
309
+ "topP": 0.95
310
+ }
311
+ }
261
312
  }
262
313
  },
263
314
  "extraPrompts": {
264
315
  "gpt-5-mini": "<built-in exploration prompt>",
265
- "gpt-5.1-codex-max": "<built-in exploration prompt>"
316
+ "gpt-5.3-codex": "<built-in commentary prompt>",
317
+ "gpt-5.4": "<built-in commentary prompt>"
318
+ "useMessagesApi": true
266
319
  },
267
320
  "smallModel": "gpt-5-mini",
268
321
  "freeModelLoadBalancing": true,
322
+ "responsesApiContextManagementModels": [],
269
323
  "modelReasoningEfforts": {
270
- "gpt-5-mini": "low"
324
+ "gpt-5-mini": "low",
325
+ "gpt-5.3-codex": "xhigh",
326
+ "gpt-5.4": "xhigh"
271
327
  },
272
328
  "modelAliases": {
273
329
  "fast": { "target": "gpt-5-mini", "allowOriginal": false },
@@ -276,19 +332,22 @@ The `<target>` can be either the account ID (GitHub username) or a 1-based index
276
332
  "allowOriginalModelNamesForAliases": false,
277
333
  "useFunctionApplyPatch": true,
278
334
  "forceAgent": false,
279
- "compactUseSmallModel": true
335
+ "compactUseSmallModel": true,
336
+ "useMessagesApi": true
280
337
  }
281
338
  ```
282
339
  - **auth.apiKeys:** API keys used for request authentication. Supports multiple keys for rotation. Requests can authenticate with either `x-api-key: <key>` or `Authorization: Bearer <key>`. If empty or omitted, authentication is disabled.
283
- - **extraPrompts:** Map of `model -> prompt` appended to the first system prompt when translating Anthropic-style requests to Copilot. Use this to inject guardrails or guidance per model. Missing default entries are auto-added without overwriting your custom prompts.
284
- - **providers:** Global upstream provider map. Each provider key (for example `openrouter`) becomes a route prefix (`/openrouter/v1/messages`). Currently only `type: "anthropic"` is supported.
340
+ - **extraPrompts:** Map of `model -> prompt` appended to the first system prompt when translating Anthropic-style requests to Copilot. Use this to inject guardrails or guidance per model. Missing default entries are auto-added without overwriting your custom prompts. The built-in prompts for `gpt-5.3-codex` and `gpt-5.4` enable phase-aware commentary, which lets the model emit a short user-facing progress update before tools or deeper reasoning.
341
+ - **providers:** Global upstream provider map. Each provider key (for example `custom`) becomes a route prefix (`/custom/v1/messages`). Currently only `type: "anthropic"` is supported.
285
342
  - `enabled` defaults to `true` if omitted.
286
343
  - `baseUrl` should be provider API base URL without trailing `/v1/messages`.
287
344
  - `apiKey` is used as upstream `x-api-key`.
288
- - `defaultTemperature` (optional): Default temperature value used when the request does not specify one.
289
- - `defaultTopP` (optional): Default top_p value used when the request does not specify one.
290
- - `defaultTopK` (optional): Default top_k value used when the request does not specify one.
291
- - **smallModel:** Fallback model used for tool-less warmup messages (e.g., Claude Code probe requests) to avoid spending premium requests; defaults to `gpt-5-mini`. If original names are blocked and this points to an aliased target, it resolves to the first alias.
345
+ - `models` (optional): Per-model configuration map. Each key is a model ID (matching the model name in requests), and the value is:
346
+ - `temperature` (optional): Default temperature value used when the request does not specify one.
347
+ - `topP` (optional): Default top_p value used when the request does not specify one.
348
+ - `topK` (optional): Default top_k value used when the request does not specify one.
349
+ - **responsesApiContextManagementModels:** List of model IDs that should receive Responses API `context_management` compaction instructions. Use this when a model supports server-side context management and you want the proxy to keep only the latest compaction carrier on follow-up turns.
350
+ - **smallModel:** Fallback model used for tool-less warmup messages, compact/background requests, and other short housekeeping turns (for example from Claude Code or OpenCode) to avoid spending premium requests; defaults to `gpt-5-mini`. If original names are blocked and this points to an aliased target, it resolves to the preferred alias.
292
351
  - **freeModelLoadBalancing:** Enable round-robin routing for free-model requests across multiple accounts. Defaults to `true`. Set to `false` to route free-model requests sequentially (same ordering strategy as premium models).
293
352
  - **apiKey (deprecated):** Legacy single-key field kept for migration compatibility. Prefer `auth.apiKeys`. When `auth.apiKeys` is empty, the server falls back to `COPILOT_API_KEY` and then `apiKey`.
294
353
  - **modelReasoningEfforts:** Per-model `reasoning.effort` sent to the Copilot Responses API. Allowed values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`. If a model isn’t listed, `high` is used by default.
@@ -297,6 +356,7 @@ The `<target>` can be either the account ID (GitHub username) or a 1-based index
297
356
  - **useFunctionApplyPatch:** When `true` (default), `POST /v1/responses` converts a `tools` entry with `{ "type": "custom", "name": "apply_patch" }` into an OpenAI-style `function` tool (with a parameter schema) for upstream compatibility. Set to `false` to leave custom tools untouched.
298
357
  - **forceAgent:** When `true`, `POST /v1/responses` treats a request as agent-initiated if **any** input item has `role: "assistant"`. When `false` (default), only the **last** input item is checked.
299
358
  - **compactUseSmallModel:** When `true`, detected "compact" requests (e.g., from Claude Code or opencode compact mode) will automatically use the configured `smallModel` to avoid consuming premium usage for short/background tasks. Defaults to `true`.
359
+ - **useMessagesApi:** When `true` (default), Claude-family models that support Copilot's native `/v1/messages` endpoint may use the Messages API path. Set to `false` to skip the Messages API candidate and fall back to `/responses` (if supported) or `/chat/completions`.
300
360
 
301
361
  Edit this file to customize prompts or swap in your own fast model. If you edit it manually, restart the server (or call `GET /api/admin/config`) so the cached config is refreshed. Changes made through the Admin UI/API are validated, written to disk, and applied immediately; unknown keys are rejected.
302
362
 
@@ -494,6 +554,98 @@ Or use inline environment variable:
494
554
  COPILOT_API_OAUTH_APP=opencode npx @nick3/copilot-api@latest start
495
555
  ```
496
556
 
557
+ ## Using with OpenCode
558
+
559
+ OpenCode already has a direct GitHub Copilot provider. Use this section when you want OpenCode to point at this proxy through `@ai-sdk/anthropic` and reuse the agent behaviors described earlier in this README.
560
+
561
+ ### Minimal setup
562
+
563
+ Start the proxy with the OpenCode OAuth app:
564
+
565
+ ```sh
566
+ COPILOT_API_OAUTH_APP=opencode npx @nick3/copilot-api@latest start
567
+ ```
568
+
569
+ Then point OpenCode at the proxy with `@ai-sdk/anthropic`.
570
+
571
+ Example `~/.config/opencode/opencode.json`:
572
+
573
+ ```json
574
+ {
575
+ "$schema": "https://opencode.ai/config.json",
576
+ "model": "local/gpt-5.4",
577
+ "small_model": "local/gpt-5-mini",
578
+ "agent": {
579
+ "build": {
580
+ "model": "local/gpt-5.4"
581
+ },
582
+ "plan": {
583
+ "model": "local/gpt-5.4"
584
+ },
585
+ "explore": {
586
+ "model": "local/gpt-5-mini"
587
+ }
588
+ },
589
+ "provider": {
590
+ "local": {
591
+ "npm": "@ai-sdk/anthropic",
592
+ "name": "Copilot API Proxy",
593
+ "options": {
594
+ "baseURL": "http://localhost:4141/v1",
595
+ "apiKey": "dummy"
596
+ },
597
+ "models": {
598
+ "gpt-5.4": {
599
+ "name": "gpt-5.4",
600
+ "modalities": {
601
+ "input": ["text", "image"],
602
+ "output": ["text"]
603
+ },
604
+ "limit": {
605
+ "context": 272000,
606
+ "output": 128000
607
+ }
608
+ },
609
+ "gpt-5-mini": {
610
+ "name": "gpt-5-mini",
611
+ "limit": {
612
+ "context": 128000,
613
+ "output": 64000
614
+ }
615
+ },
616
+ "claude-sonnet-4.6": {
617
+ "id": "claude-sonnet-4.6",
618
+ "name": "claude-sonnet-4.6",
619
+ "modalities": {
620
+ "input": ["text", "image"],
621
+ "output": ["text"]
622
+ },
623
+ "limit": {
624
+ "context": 128000,
625
+ "output": 32000
626
+ },
627
+ "options": {
628
+ "thinking": {
629
+ "type": "enabled",
630
+ "budgetTokens": 31999
631
+ }
632
+ }
633
+ }
634
+ }
635
+ }
636
+ }
637
+ }
638
+ ```
639
+
640
+ Why these fields matter:
641
+
642
+ - `npm: "@ai-sdk/anthropic"` is the important part. OpenCode will speak Anthropic Messages semantics to this proxy instead of flattening everything into OpenAI Chat Completions.
643
+ - `options.baseURL` should be `http://localhost:4141/v1`; the Anthropic SDK will append `/messages`, `/models`, and `/messages/count_tokens` automatically.
644
+ - `model`, `small_model`, and `agent.*.model` let you keep `gpt-5.4` for build/plan work while routing exploration and background work to `gpt-5-mini`.
645
+ - If you enable `auth.apiKeys` in this proxy, replace `dummy` with a real key. Otherwise any placeholder value is fine.
646
+
647
+ ## Using the Usage Viewer
648
+
497
649
  ### Admin API examples
498
650
 
499
651
  ```sh
@@ -591,8 +743,8 @@ Here is an example `.claude/settings.json` file:
591
743
  "env": {
592
744
  "ANTHROPIC_BASE_URL": "http://localhost:4141",
593
745
  "ANTHROPIC_AUTH_TOKEN": "dummy",
594
- "ANTHROPIC_MODEL": "gpt-5.2",
595
- "ANTHROPIC_DEFAULT_SONNET_MODEL": "gpt-5.2",
746
+ "ANTHROPIC_MODEL": "gpt-5.4",
747
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "gpt-5.4",
596
748
  "ANTHROPIC_DEFAULT_HAIKU_MODEL": "gpt-5-mini",
597
749
  "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
598
750
  "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
@@ -608,20 +760,22 @@ Here is an example `.claude/settings.json` file:
608
760
  }
609
761
  ```
610
762
 
611
- ### CLAUDE.md Recommended Content
763
+ ### CLAUDE.md or AGENTS.md Recommended Content
612
764
 
613
- Please include the following in `CLAUDE.md` (for Claude usage):
765
+ Please include the following in `CLAUDE.md` or `AGENTS.md`:
614
766
 
615
- - Prohibited from directly asking questions to users, MUST use AskUserQuestion tool.
616
- - Once you can confirm that the task is complete, MUST use AskUserQuestion tool to make user confirm. The user may respond with feedback if they are not satisfied with the result, which you can use to make improvements and try again.
767
+ ```
768
+ - Prohibited from directly asking questions to users, MUST use question tool.
769
+ - Once you can confirm that the task is complete, MUST use question tool to make user confirm. The user may respond with feedback if they are not satisfied with the result, which you can use to make improvements and try again, after try again, MUST use question tool to make user confirm again.
770
+ ```
617
771
 
618
772
  You can find more options here: [Claude Code settings](https://docs.anthropic.com/en/docs/claude-code/settings#environment-variables)
619
773
 
620
774
  You can also read more about IDE integration here: [Add Claude Code to your IDE](https://docs.anthropic.com/en/docs/claude-code/ide-integrations)
621
775
 
622
- ### Subagent Marker Integration (Optional)
776
+ ## Subagent Marker Integration (Optional)
623
777
 
624
- This project supports `x-initiator: agent` for subagent-originated requests.
778
+ This project supports `x-initiator: agent` for subagent-originated requests and can preserve the root session identity with `x-session-id` when a subagent marker is present.
625
779
 
626
780
  #### Claude Code plugin producer (marketplace-based)
627
781
 
@@ -539,8 +539,11 @@ const getRootSessionId = (anthropicPayload, c) => {
539
539
  return sessionId;
540
540
  };
541
541
  const getUUID = (content) => {
542
- const hash32 = createHash("sha256").update(content).digest("hex").slice(0, 32);
543
- return `${hash32.slice(0, 8)}-${hash32.slice(8, 12)}-${hash32.slice(12, 16)}-${hash32.slice(16, 20)}-${hash32.slice(20)}`;
542
+ const uuidBytes = createHash("sha256").update(content).digest().subarray(0, 16);
543
+ uuidBytes[6] = uuidBytes[6] & 15 | 64;
544
+ uuidBytes[8] = uuidBytes[8] & 63 | 128;
545
+ const uuidHex = uuidBytes.toString("hex");
546
+ return `${uuidHex.slice(0, 8)}-${uuidHex.slice(8, 12)}-${uuidHex.slice(12, 16)}-${uuidHex.slice(16, 20)}-${uuidHex.slice(20)}`;
544
547
  };
545
548
 
546
549
  //#endregion
@@ -599,7 +602,8 @@ const defaultConfig = {
599
602
  useFunctionApplyPatch: true,
600
603
  compactUseSmallModel: true,
601
604
  messageStartInputTokensFallback: false,
602
- modelRefreshIntervalHours: 24
605
+ modelRefreshIntervalHours: 24,
606
+ useMessagesApi: true
603
607
  };
604
608
  let cachedConfig = null;
605
609
  function isPlainObject(value) {
@@ -907,11 +911,12 @@ function getProviderConfig(name) {
907
911
  type,
908
912
  baseUrl,
909
913
  apiKey,
910
- defaultTemperature: provider.defaultTemperature,
911
- defaultTopP: provider.defaultTopP,
912
- defaultTopK: provider.defaultTopK
914
+ models: provider.models
913
915
  };
914
916
  }
917
+ function isMessagesApiEnabled() {
918
+ return getConfig().useMessagesApi ?? true;
919
+ }
915
920
 
916
921
  //#endregion
917
922
  //#region src/lib/accounts-manager-auth.ts
@@ -1718,5 +1723,5 @@ var AccountsManager = class {
1718
1723
  const accountsManager = new AccountsManager();
1719
1724
 
1720
1725
  //#endregion
1721
- export { HTTPError, PATHS, accountFromState, accountsManager, addAccountToRegistry, cacheMacMachineId, cacheVSCodeVersion, cacheVsCodeSessionId, copilotBaseUrl, copilotHeaders, ensurePaths, forwardError, generateRequestIdFromPayload, getAliasTargetSet, getConfig, getCopilotUsage, getExtraPromptForModel, getGitHubUser, getModelAliases, getModelAliasesInfo, getModelRefreshIntervalMs, getOauthAppConfig, getOauthUrls, getProviderConfig, getReasoningEffortForModel, getRootSessionId, getSmallModel, getUUID, isForceAgentEnabled, isFreeModelLoadBalancingEnabled, isMessageStartInputTokensFallbackEnabled, isNullish, isResponsesApiContextManagementModel, listAccountsFromRegistry, loadAccountToken, mergeConfigWithDefaults, prepareInteractionHeaders, removeAccountFromRegistry, removeAccountToken, saveAccountToken, saveRegistry, shouldCompactUseSmallModel, sleep, state };
1722
- //# sourceMappingURL=accounts-manager-iJwqQUkM.js.map
1726
+ export { HTTPError, PATHS, accountFromState, accountsManager, addAccountToRegistry, cacheMacMachineId, cacheVSCodeVersion, cacheVsCodeSessionId, copilotBaseUrl, copilotHeaders, ensurePaths, forwardError, generateRequestIdFromPayload, getAliasTargetSet, getConfig, getCopilotUsage, getExtraPromptForModel, getGitHubUser, getModelAliases, getModelAliasesInfo, getModelRefreshIntervalMs, getOauthAppConfig, getOauthUrls, getProviderConfig, getReasoningEffortForModel, getRootSessionId, getSmallModel, getUUID, isForceAgentEnabled, isFreeModelLoadBalancingEnabled, isMessageStartInputTokensFallbackEnabled, isMessagesApiEnabled, isNullish, isResponsesApiContextManagementModel, listAccountsFromRegistry, loadAccountToken, mergeConfigWithDefaults, prepareInteractionHeaders, removeAccountFromRegistry, removeAccountToken, saveAccountToken, saveRegistry, shouldCompactUseSmallModel, sleep, state };
1727
+ //# sourceMappingURL=accounts-manager-H7YGTVk8.js.map