@opeoginni/opencode-copilot-auto 0.1.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 +21 -0
- package/README.md +33 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +160 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Opeyemi Oginni
|
|
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/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @opeoginni/opencode-copilot-auto
|
|
2
|
+
|
|
3
|
+
Adds GitHub Copilot's **Auto** model to OpenCode. Copilot selects the appropriate available model for every request.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Add the plugin to `opencode.json`:
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{
|
|
11
|
+
"$schema": "https://opencode.ai/config.json",
|
|
12
|
+
"plugin": ["@opeoginni/opencode-copilot-auto"]
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Restart OpenCode, authenticate GitHub Copilot if necessary with `opencode auth login`, then select `github-copilot/auto`.
|
|
17
|
+
|
|
18
|
+
The plugin uses the existing OpenCode GitHub Copilot authentication and sends the prompt to Copilot's routing endpoint solely to select a model.
|
|
19
|
+
|
|
20
|
+
## Development
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
bun install
|
|
24
|
+
bun run check
|
|
25
|
+
bun run test
|
|
26
|
+
bun run build
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Publish
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm publish --access public
|
|
33
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.ts
|
|
3
|
+
var COPILOT_BASE_URL = "https://api.individual.githubcopilot.com";
|
|
4
|
+
var COPILOT_API_VERSION = "2026-06-01";
|
|
5
|
+
var SESSION_REFRESH_BUFFER_SECONDS = 30;
|
|
6
|
+
var sessions = new Map;
|
|
7
|
+
var CopilotAutoPlugin = async () => {
|
|
8
|
+
installFetchAdapter();
|
|
9
|
+
return {
|
|
10
|
+
provider: {
|
|
11
|
+
id: "github-copilot",
|
|
12
|
+
models: async () => ({ auto: autoModel() })
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
function autoModel() {
|
|
17
|
+
return {
|
|
18
|
+
id: "auto",
|
|
19
|
+
providerID: "github-copilot",
|
|
20
|
+
name: "Auto",
|
|
21
|
+
family: "gpt",
|
|
22
|
+
api: {
|
|
23
|
+
id: "auto",
|
|
24
|
+
url: COPILOT_BASE_URL,
|
|
25
|
+
npm: "@ai-sdk/github-copilot"
|
|
26
|
+
},
|
|
27
|
+
status: "active",
|
|
28
|
+
headers: {},
|
|
29
|
+
options: {},
|
|
30
|
+
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
|
|
31
|
+
limit: { context: 128000, input: 128000, output: 16384 },
|
|
32
|
+
capabilities: {
|
|
33
|
+
temperature: true,
|
|
34
|
+
reasoning: false,
|
|
35
|
+
attachment: true,
|
|
36
|
+
toolcall: true,
|
|
37
|
+
input: { text: true, audio: false, image: true, video: false, pdf: false },
|
|
38
|
+
output: { text: true, audio: false, image: false, video: false, pdf: false },
|
|
39
|
+
interleaved: false
|
|
40
|
+
},
|
|
41
|
+
release_date: "",
|
|
42
|
+
variants: {}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function installFetchAdapter() {
|
|
46
|
+
const marker = Symbol.for("opeoginni.opencode-copilot-auto.fetch-adapter");
|
|
47
|
+
const runtime = globalThis;
|
|
48
|
+
if (runtime[marker])
|
|
49
|
+
return;
|
|
50
|
+
runtime[marker] = true;
|
|
51
|
+
const originalFetch = globalThis.fetch.bind(globalThis);
|
|
52
|
+
const adapter = async (input, init) => {
|
|
53
|
+
const request = new Request(input, init);
|
|
54
|
+
if (!isAutoRequest(request))
|
|
55
|
+
return originalFetch(input, init);
|
|
56
|
+
const body = await request.clone().text();
|
|
57
|
+
const payload = parseJson(body);
|
|
58
|
+
if (!payload || payload.model !== "auto")
|
|
59
|
+
return originalFetch(input, init);
|
|
60
|
+
const session = await getSession(originalFetch, request.headers);
|
|
61
|
+
const model = await route(originalFetch, request.headers, session, payload);
|
|
62
|
+
const headers = new Headers(request.headers);
|
|
63
|
+
headers.set("copilot-session-token", session.token);
|
|
64
|
+
return originalFetch(new Request(request, {
|
|
65
|
+
headers,
|
|
66
|
+
body: JSON.stringify({ ...payload, model })
|
|
67
|
+
}));
|
|
68
|
+
};
|
|
69
|
+
globalThis.fetch = Object.assign(adapter, originalFetch);
|
|
70
|
+
}
|
|
71
|
+
function isAutoRequest(request) {
|
|
72
|
+
const url = new URL(request.url);
|
|
73
|
+
return url.origin === COPILOT_BASE_URL && request.method === "POST" && (url.pathname.endsWith("/chat/completions") || url.pathname.endsWith("/responses"));
|
|
74
|
+
}
|
|
75
|
+
async function getSession(fetcher, requestHeaders) {
|
|
76
|
+
const key = requestHeaders.get("authorization") ?? "anonymous";
|
|
77
|
+
const cached = sessions.get(key);
|
|
78
|
+
if (cached && cached.expiresAt > Math.floor(Date.now() / 1000) + SESSION_REFRESH_BUFFER_SECONDS)
|
|
79
|
+
return cached;
|
|
80
|
+
const response = await fetcher(`${COPILOT_BASE_URL}/models/session`, {
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: copilotHeaders(requestHeaders),
|
|
83
|
+
body: JSON.stringify({ auto_mode: { model_hints: ["auto"] } }),
|
|
84
|
+
signal: AbortSignal.timeout(5000)
|
|
85
|
+
});
|
|
86
|
+
if (!response.ok)
|
|
87
|
+
throw new Error(`Copilot Auto could not create a routing session: ${response.status}`);
|
|
88
|
+
const data = await response.json();
|
|
89
|
+
const session = {
|
|
90
|
+
availableModels: data.available_models,
|
|
91
|
+
selectedModel: data.selected_model,
|
|
92
|
+
token: data.session_token,
|
|
93
|
+
expiresAt: data.expires_at
|
|
94
|
+
};
|
|
95
|
+
sessions.set(key, session);
|
|
96
|
+
return session;
|
|
97
|
+
}
|
|
98
|
+
async function route(fetcher, requestHeaders, session, payload) {
|
|
99
|
+
const prompt = promptText(payload.messages);
|
|
100
|
+
const headers = copilotHeaders(requestHeaders);
|
|
101
|
+
headers.set("copilot-session-token", session.token);
|
|
102
|
+
const response = await fetcher(`${COPILOT_BASE_URL}/models/session/intent`, {
|
|
103
|
+
method: "POST",
|
|
104
|
+
headers,
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
prompt,
|
|
107
|
+
available_models: session.availableModels,
|
|
108
|
+
session_id: "opencode-session://auto",
|
|
109
|
+
reference_count: 0,
|
|
110
|
+
prompt_char_count: prompt.length,
|
|
111
|
+
turn_number: userTurns(payload.messages),
|
|
112
|
+
routing_method: "hydra",
|
|
113
|
+
copilot_plan: "individual"
|
|
114
|
+
}),
|
|
115
|
+
signal: AbortSignal.timeout(5000)
|
|
116
|
+
});
|
|
117
|
+
if (!response.ok)
|
|
118
|
+
throw new Error(`Copilot Auto could not select a model: ${response.status}`);
|
|
119
|
+
const intent = await response.json();
|
|
120
|
+
return intent.chosen_model ?? session.selectedModel;
|
|
121
|
+
}
|
|
122
|
+
function copilotHeaders(requestHeaders) {
|
|
123
|
+
const headers = new Headers(requestHeaders);
|
|
124
|
+
headers.set("Content-Type", "text/plain;charset=UTF-8");
|
|
125
|
+
headers.set("X-GitHub-Api-Version", COPILOT_API_VERSION);
|
|
126
|
+
return headers;
|
|
127
|
+
}
|
|
128
|
+
function parseJson(value) {
|
|
129
|
+
try {
|
|
130
|
+
const parsed = JSON.parse(value);
|
|
131
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : undefined;
|
|
132
|
+
} catch {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function promptText(messages) {
|
|
137
|
+
if (!Array.isArray(messages))
|
|
138
|
+
return "";
|
|
139
|
+
const message = [...messages].reverse().find((item) => isRecord(item) && item.role === "user");
|
|
140
|
+
if (!message)
|
|
141
|
+
return "";
|
|
142
|
+
const content = message.content;
|
|
143
|
+
if (typeof content === "string")
|
|
144
|
+
return content;
|
|
145
|
+
if (!Array.isArray(content))
|
|
146
|
+
return "";
|
|
147
|
+
return content.map((part) => isRecord(part) && typeof part.text === "string" ? part.text : "").filter(Boolean).join(`
|
|
148
|
+
`);
|
|
149
|
+
}
|
|
150
|
+
function userTurns(messages) {
|
|
151
|
+
return Array.isArray(messages) ? messages.filter((item) => isRecord(item) && item.role === "user").length : 0;
|
|
152
|
+
}
|
|
153
|
+
function isRecord(value) {
|
|
154
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
155
|
+
}
|
|
156
|
+
var src_default = CopilotAutoPlugin;
|
|
157
|
+
export {
|
|
158
|
+
src_default as default,
|
|
159
|
+
CopilotAutoPlugin
|
|
160
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@opeoginni/opencode-copilot-auto",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Add GitHub Copilot Auto model routing to OpenCode",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"opencode",
|
|
7
|
+
"opencode-plugin",
|
|
8
|
+
"github-copilot",
|
|
9
|
+
"copilot",
|
|
10
|
+
"auto-model"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Opeyemi Oginni",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "rm -rf dist && bun build src/index.ts --outdir dist --target bun --format esm && tsc -p tsconfig.build.json",
|
|
34
|
+
"check": "tsc --noEmit",
|
|
35
|
+
"test": "bun test",
|
|
36
|
+
"prepack": "bun run check && bun run test && bun run build"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@opencode-ai/plugin": ">=1.17.20"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@opencode-ai/plugin": "^1.17.20",
|
|
43
|
+
"@opencode-ai/sdk": "^1.17.20",
|
|
44
|
+
"@types/bun": "^1.2.6",
|
|
45
|
+
"typescript": "^5.8.2"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"bun": ">=1.2.0"
|
|
49
|
+
}
|
|
50
|
+
}
|