github-copilot-oauth 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 RespectMathias
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/NOTICE ADDED
@@ -0,0 +1,27 @@
1
+ This package contains OAuth and Codex integration logic originally informed by
2
+ the OpenCode project.
3
+
4
+ OpenCode
5
+ Repository: https://github.com/anomalyco/opencode
6
+
7
+ MIT License
8
+
9
+ Copyright (c) 2025 opencode
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,194 @@
1
+ # github-copilot-oauth
2
+
3
+ GitHub Copilot OAuth provider and token helpers for the Vercel AI SDK.
4
+
5
+ This package lets local apps use a user's GitHub Copilot OAuth session with AI SDK functions such as `generateText` and `streamText`. It handles GitHub device OAuth, GitHub-to-Copilot token exchange, Copilot request headers, and OpenAI-compatible AI SDK provider creation.
6
+
7
+ It is unofficial and is not affiliated with, endorsed by, or sponsored by GitHub.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install github-copilot-oauth ai @ai-sdk/openai @ai-sdk/provider
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```ts
18
+ import { generateText } from 'ai';
19
+ import { createGitHubCopilot } from 'github-copilot-oauth';
20
+
21
+ const copilot = createGitHubCopilot({
22
+ tokens: {
23
+ githubToken: process.env.GITHUB_COPILOT_OAUTH_TOKEN!,
24
+ },
25
+ });
26
+
27
+ const result = await generateText({
28
+ model: copilot('gpt-4.1'),
29
+ prompt: 'Reply with exactly: hello',
30
+ });
31
+
32
+ console.log(result.text);
33
+ ```
34
+
35
+ ## Sign In With Device OAuth
36
+
37
+ ```ts
38
+ import { startGitHubCopilotDeviceFlow } from 'github-copilot-oauth';
39
+
40
+ const flow = await startGitHubCopilotDeviceFlow();
41
+
42
+ console.log(`Open ${flow.url}`);
43
+ console.log(`Enter code ${flow.code}`);
44
+
45
+ const tokens = await flow.complete();
46
+ ```
47
+
48
+ `flow.complete()` polls until the user authorizes the code and returns:
49
+
50
+ ```ts
51
+ type GitHubCopilotOAuthTokens = {
52
+ githubToken: string;
53
+ copilotToken?: string;
54
+ copilotTokenExpiresAt?: number;
55
+ enterpriseUrl?: string;
56
+ };
57
+ ```
58
+
59
+ Persist these tokens in secure storage. The `githubToken` is used to mint short-lived Copilot API tokens.
60
+
61
+ ## Token Store
62
+
63
+ For real apps, prefer a `TokenStore` over hard-coded env values. The provider loads tokens lazily, exchanges the GitHub token for a short-lived Copilot API token, then saves the exchanged token back through the same store.
64
+
65
+ ```ts
66
+ import { createGitHubCopilot, type TokenStore } from 'github-copilot-oauth';
67
+
68
+ const tokenStore: TokenStore = {
69
+ async load() {
70
+ const raw = await secureStore.get('github-copilot-oauth');
71
+ return raw ? JSON.parse(raw) : undefined;
72
+ },
73
+ async save(tokens) {
74
+ await secureStore.set('github-copilot-oauth', JSON.stringify(tokens));
75
+ },
76
+ };
77
+
78
+ const copilot = createGitHubCopilot({ tokenStore });
79
+ ```
80
+
81
+ ## Streaming
82
+
83
+ ```ts
84
+ import { streamText } from 'ai';
85
+ import { createGitHubCopilot } from 'github-copilot-oauth';
86
+
87
+ const copilot = createGitHubCopilot({ tokenStore });
88
+
89
+ const result = streamText({
90
+ model: copilot('gpt-4.1'),
91
+ prompt: 'Write one sentence about the moon.',
92
+ });
93
+
94
+ for await (const delta of result.textStream) {
95
+ process.stdout.write(delta);
96
+ }
97
+ ```
98
+
99
+ ## Model Routing
100
+
101
+ The callable provider routes models using the behavior from Tolksyn's working integration:
102
+
103
+ - `gpt-5` and newer non-mini models use Copilot's `/responses` endpoint.
104
+ - `gpt-4` models and `gpt-5-mini` use `/chat/completions`.
105
+
106
+ You can override routing explicitly:
107
+
108
+ ```ts
109
+ const chatModel = copilot.chat('gpt-4.1');
110
+ const responsesModel = copilot.responses('gpt-5.4');
111
+ ```
112
+
113
+ ## Browser/Web Proxy Handlers
114
+
115
+ Browsers should not call GitHub OAuth or Copilot token endpoints directly. Use the proxy helpers from `github-copilot-oauth/proxy` in server routes.
116
+
117
+ ```ts
118
+ import { createGitHubCopilotProxy } from 'github-copilot-oauth/proxy';
119
+
120
+ const proxy = createGitHubCopilotProxy();
121
+
122
+ export const deviceCode = proxy.deviceCode;
123
+ export const deviceToken = proxy.deviceToken;
124
+ export const models = proxy.models;
125
+ export const chatCompletions = proxy.chatCompletions;
126
+ export const responses = proxy.responses;
127
+ ```
128
+
129
+ The proxy expects browser API requests to send the GitHub OAuth token as `Authorization: Bearer <token>`. It exchanges that token server-side and forwards to the Copilot API.
130
+
131
+ In browser runtimes, `createGitHubCopilotOAuthFetch` automatically routes Copilot API calls through `/api/proxy/github-copilot` unless `browserProxyBaseUrl: false` is set. This avoids direct Copilot API CORS failures and keeps Copilot token exchange on the server route.
132
+
133
+ ## Credential Safety
134
+
135
+ GitHub Copilot OAuth tokens are account credentials.
136
+
137
+ - Do store tokens in OS keychain storage, encrypted app storage, or a trusted server-side secret store.
138
+ - Do not store tokens in browser `localStorage`, plaintext app config, Git, logs, analytics, crash reports, or build output.
139
+ - Do not expose this provider from a shared hosted API unless each user has isolated storage and authorization.
140
+ - Do not pool, proxy, or redistribute tokens across users.
141
+ - Use `tokenStore` in production so exchanged Copilot tokens are persisted and reused until expiry.
142
+ - Pass `tokens` directly only for short-lived scripts, tests, or already-secured server runtime secrets.
143
+
144
+ The package does not phone home, does not persist tokens unless you provide a `TokenStore`, and does not log tokens.
145
+
146
+ ## API
147
+
148
+ ### `createGitHubCopilot(settings)`
149
+
150
+ Creates an AI SDK provider. The provider is callable:
151
+
152
+ ```ts
153
+ const model = copilot('gpt-4.1');
154
+ ```
155
+
156
+ Important settings:
157
+
158
+ - `tokens`: in-memory credentials for scripts/tests.
159
+ - `tokenStore`: async credential store used for loading and saving exchanged tokens.
160
+ - `fetch`: custom fetch implementation.
161
+ - `baseURL`: Copilot API base URL. Defaults to `https://api.githubcopilot.com`.
162
+ - `browserProxyBaseUrl`: browser proxy base URL. Defaults to `/api/proxy/github-copilot`; pass `false` to disable browser proxy routing.
163
+ - `enterpriseUrl`: optional GitHub Enterprise hostname.
164
+ - `allowEnterprise`: allow validated custom GitHub Enterprise hostnames. Defaults to `false`.
165
+ - `headers`: additional upstream headers.
166
+ - `initiator`: `X-Initiator` header. Defaults to `user`.
167
+ - `vision`: force the `Copilot-Vision-Request` header. Otherwise image input is detected from JSON.
168
+ - `fallbackToGitHubToken`: fallback to the GitHub OAuth token when exchange fails or returns an unusable token. Defaults to `true`.
169
+ - `onTokens`: callback invoked after a successful Copilot token exchange.
170
+
171
+ ### `startGitHubCopilotDeviceFlow(options)`
172
+
173
+ Starts GitHub's device OAuth flow and returns `{ url, code, instructions, complete }`.
174
+
175
+ ### `exchangeGitHubCopilotToken(options)`
176
+
177
+ Exchanges a GitHub OAuth token for a short-lived Copilot API token.
178
+
179
+ ### `createGitHubCopilotOAuthFetch(settings)`
180
+
181
+ Creates a `fetch` implementation that rewrites OpenAI-compatible paths, exchanges tokens, and injects Copilot headers.
182
+
183
+ In browsers, it routes through `browserProxyBaseUrl` by default and sends the GitHub OAuth token to the local proxy for server-side Copilot token exchange.
184
+
185
+ ### `createGitHubCopilotProxy(options)`
186
+
187
+ Creates framework-agnostic server handlers for browser-safe device OAuth and Copilot API proxying.
188
+
189
+ ## Limitations
190
+
191
+ - This is an unofficial integration over GitHub Copilot endpoints, which can change.
192
+ - Embedding and image model factories intentionally throw `NoSuchModelError` for now.
193
+ - Custom GitHub Enterprise hostnames are disabled by default. Pass `allowEnterprise: true` only when you trust and validate the deployment environment.
194
+ - The package does not provide a multi-user auth service. You own user isolation and secure storage.