opencode-muse-auth 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/NOTICE +5 -0
- package/README.md +53 -0
- package/dist/auth.d.ts +24 -0
- package/dist/auth.js +149 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +38 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TheStreamCode
|
|
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,5 @@
|
|
|
1
|
+
Flow parameters compatible with the published behavior of oh-my-pi
|
|
2
|
+
(https://github.com/can1357/oh-my-pi), MIT-licensed (c) 2025 Mario Zechner,
|
|
3
|
+
(c) 2025-2026 Can Bölük, (c) 2026 Stencil Labs, Inc. No oh-my-pi code is
|
|
4
|
+
vendored here; the device-code exchange is an independent implementation.
|
|
5
|
+
No oh-my-pi branding appears in the user experience.
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# opencode-muse-auth
|
|
2
|
+
|
|
3
|
+
Use Meta **Muse Spark** in [opencode](https://opencode.ai) billed to your
|
|
4
|
+
**Muse Code monthly subscription** — no pay-as-you-go API key, no third-party CLI.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```jsonc
|
|
9
|
+
// opencode.json
|
|
10
|
+
{
|
|
11
|
+
"$schema": "https://opencode.ai/config.json",
|
|
12
|
+
"plugin": ["opencode-muse-auth"]
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Then run `/connect` in opencode, pick the `meta` provider and the
|
|
17
|
+
*Muse Code subscription* method: it shows a Meta device page URL and a code.
|
|
18
|
+
Enter the code in your browser (your own Meta account) and the login
|
|
19
|
+
completes by itself.
|
|
20
|
+
|
|
21
|
+
## Requirements
|
|
22
|
+
|
|
23
|
+
- opencode with plugin support
|
|
24
|
+
- A Meta account with an active Muse Code subscription
|
|
25
|
+
|
|
26
|
+
## How it works
|
|
27
|
+
|
|
28
|
+
The plugin registers an `auth` hook for the built-in `meta` provider
|
|
29
|
+
(Routes through the Responses API, `meta.txt` system prompt applies
|
|
30
|
+
automatically to `muse-*` model ids):
|
|
31
|
+
|
|
32
|
+
- **Login** — Meta device-code exchange (RFC 8628) inside `/connect`, then
|
|
33
|
+
mints the stable account-bound inference key via the Model API. The key is
|
|
34
|
+
cached locally (`~/.config/opencode/muse-code-sub.json`, owner-only
|
|
35
|
+
permissions where supported) and never printed.
|
|
36
|
+
- **Runtime** — an auth `loader` injects the cached key on every startup.
|
|
37
|
+
The mint endpoint is aggressively rate-limited, so the plugin never
|
|
38
|
+
re-mints on its own; re-run `/connect` only if access is revoked (401).
|
|
39
|
+
|
|
40
|
+
Flow parameters are compatible with the published behavior of oh-my-pi (MIT);
|
|
41
|
+
see [NOTICE](NOTICE).
|
|
42
|
+
|
|
43
|
+
## Development
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
npm install
|
|
47
|
+
npm run build
|
|
48
|
+
npm test
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
MIT — see [LICENSE](LICENSE).
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const CLIENT_ID = "1031625952748946";
|
|
2
|
+
export declare const DEVICE_URL = "https://auth.meta.com/oidc/device/authorization/";
|
|
3
|
+
export declare const TOKEN_URL = "https://auth.meta.com/oidc/device/token/";
|
|
4
|
+
export declare const KEY_URL = "https://api.meta.ai/muse-code/key";
|
|
5
|
+
export declare const CACHE_PATH: string;
|
|
6
|
+
export interface DeviceAuthorization {
|
|
7
|
+
device_code: string;
|
|
8
|
+
user_code: string;
|
|
9
|
+
verification_uri: string;
|
|
10
|
+
verification_uri_complete?: string;
|
|
11
|
+
interval: number;
|
|
12
|
+
expires_in: number;
|
|
13
|
+
}
|
|
14
|
+
export interface MintedCredential {
|
|
15
|
+
oauthAccessToken: string;
|
|
16
|
+
apiKey: string;
|
|
17
|
+
accountId: string;
|
|
18
|
+
email?: string;
|
|
19
|
+
}
|
|
20
|
+
export declare function readCache(path?: string): Promise<string>;
|
|
21
|
+
export declare function writeCache(credentials: MintedCredential, path?: string): Promise<void>;
|
|
22
|
+
export declare function deviceAuthorize(): Promise<DeviceAuthorization>;
|
|
23
|
+
export declare function pollToken(deviceCode: string, intervalS: number, expiresInS: number): Promise<string>;
|
|
24
|
+
export declare function mintKey(accessToken: string): Promise<MintedCredential>;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Shared Meta device-login logic (no third-party CLI).
|
|
2
|
+
// Flow parameters are compatible with the published behavior of oh-my-pi
|
|
3
|
+
// (MIT-licensed; see NOTICE).
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { join, dirname } from "node:path";
|
|
6
|
+
import { readFile, writeFile, mkdir, chmod } from "node:fs/promises";
|
|
7
|
+
export const CLIENT_ID = "1031625952748946";
|
|
8
|
+
export const DEVICE_URL = "https://auth.meta.com/oidc/device/authorization/";
|
|
9
|
+
export const TOKEN_URL = "https://auth.meta.com/oidc/device/token/";
|
|
10
|
+
export const KEY_URL = "https://api.meta.ai/muse-code/key";
|
|
11
|
+
export const CACHE_PATH = join(homedir(), ".config", "opencode", "muse-code-sub.json");
|
|
12
|
+
const HEADERS = {
|
|
13
|
+
Accept: "application/json",
|
|
14
|
+
"x-api-version": "1.0.0",
|
|
15
|
+
};
|
|
16
|
+
const REQUEST_TIMEOUT_MS = 25_000;
|
|
17
|
+
const MIN_INTERVAL_S = 1;
|
|
18
|
+
const SLOW_DOWN_STEP_S = 5;
|
|
19
|
+
export async function readCache(path = CACHE_PATH) {
|
|
20
|
+
try {
|
|
21
|
+
const blob = (await readFile(path, "utf8").then(JSON.parse));
|
|
22
|
+
return typeof blob?.apiKey === "string" && blob.apiKey.trim() ? blob.apiKey.trim() : "";
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export async function writeCache(credentials, path = CACHE_PATH) {
|
|
29
|
+
await mkdir(dirname(path), { recursive: true });
|
|
30
|
+
await writeFile(path, JSON.stringify(credentials, null, 2));
|
|
31
|
+
try {
|
|
32
|
+
await chmod(path, 0o600);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
/* Windows ACLs inherit profile permissions */
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function postForm(url, params) {
|
|
39
|
+
const res = await fetch(url, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: { ...HEADERS, "Content-Type": "application/x-www-form-urlencoded" },
|
|
42
|
+
body: new URLSearchParams(params).toString(),
|
|
43
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
44
|
+
});
|
|
45
|
+
const text = await res.text();
|
|
46
|
+
if (!res.ok)
|
|
47
|
+
throw new Error(`HTTP ${res.status} ${text.slice(0, 200)}`.trim());
|
|
48
|
+
return JSON.parse(text);
|
|
49
|
+
}
|
|
50
|
+
export async function deviceAuthorize() {
|
|
51
|
+
const body = (await postForm(DEVICE_URL, { client_id: CLIENT_ID }));
|
|
52
|
+
const { device_code, user_code, verification_uri, interval, expires_in } = body;
|
|
53
|
+
if (typeof device_code !== "string" ||
|
|
54
|
+
!device_code ||
|
|
55
|
+
typeof user_code !== "string" ||
|
|
56
|
+
!user_code ||
|
|
57
|
+
typeof verification_uri !== "string" ||
|
|
58
|
+
!verification_uri ||
|
|
59
|
+
typeof interval !== "number" ||
|
|
60
|
+
typeof expires_in !== "number") {
|
|
61
|
+
throw new Error("device authorization response is missing fields");
|
|
62
|
+
}
|
|
63
|
+
const complete = body.verification_uri_complete;
|
|
64
|
+
return {
|
|
65
|
+
device_code,
|
|
66
|
+
user_code,
|
|
67
|
+
verification_uri,
|
|
68
|
+
...(typeof complete === "string" && complete ? { verification_uri_complete: complete } : {}),
|
|
69
|
+
interval,
|
|
70
|
+
expires_in,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export async function pollToken(deviceCode, intervalS, expiresInS) {
|
|
74
|
+
let interval = Math.max(MIN_INTERVAL_S, Number(intervalS) || 5);
|
|
75
|
+
const deadline = Date.now() + Number(expiresInS || 600) * 1000;
|
|
76
|
+
let slowDowns = 0;
|
|
77
|
+
while (Date.now() < deadline) {
|
|
78
|
+
// The token endpoint answers HTTP errors (e.g. 400 authorization_pending)
|
|
79
|
+
// as part of the normal flow: never throw here, classify instead.
|
|
80
|
+
let body = null;
|
|
81
|
+
try {
|
|
82
|
+
const res = await fetch(TOKEN_URL, {
|
|
83
|
+
method: "POST",
|
|
84
|
+
headers: { ...HEADERS, "Content-Type": "application/x-www-form-urlencoded" },
|
|
85
|
+
body: new URLSearchParams({
|
|
86
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
87
|
+
client_id: CLIENT_ID,
|
|
88
|
+
device_code: deviceCode,
|
|
89
|
+
}).toString(),
|
|
90
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
91
|
+
});
|
|
92
|
+
try {
|
|
93
|
+
body = (await res.json());
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
body = null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
body = null;
|
|
101
|
+
}
|
|
102
|
+
if (body && !body.error) {
|
|
103
|
+
if (typeof body.access_token !== "string" || !body.access_token) {
|
|
104
|
+
throw new Error("token response has no access_token");
|
|
105
|
+
}
|
|
106
|
+
return body.access_token;
|
|
107
|
+
}
|
|
108
|
+
if (body?.error === "slow_down") {
|
|
109
|
+
slowDowns += 1;
|
|
110
|
+
interval = Math.max(MIN_INTERVAL_S, interval + SLOW_DOWN_STEP_S);
|
|
111
|
+
}
|
|
112
|
+
else if (body?.error !== "authorization_pending") {
|
|
113
|
+
throw new Error(`login failed: ${body?.error || "unreadable token response"}`);
|
|
114
|
+
}
|
|
115
|
+
const remaining = deadline - Date.now();
|
|
116
|
+
if (remaining <= 0)
|
|
117
|
+
break;
|
|
118
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
119
|
+
setTimeout(resolve, Math.min(interval, remaining) * 1000);
|
|
120
|
+
await promise;
|
|
121
|
+
}
|
|
122
|
+
throw new Error(slowDowns ? "login timed out after slow_down responses" : "login timed out waiting for approval");
|
|
123
|
+
}
|
|
124
|
+
export async function mintKey(accessToken) {
|
|
125
|
+
const res = await fetch(KEY_URL, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
headers: { ...HEADERS, "Content-Type": "application/json", Authorization: `Bearer ${accessToken}` },
|
|
128
|
+
body: JSON.stringify({ onboard: true }),
|
|
129
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
130
|
+
});
|
|
131
|
+
const text = await res.text();
|
|
132
|
+
if (!res.ok)
|
|
133
|
+
throw new Error(`key exchange failed: HTTP ${res.status} ${text.slice(0, 200)}`.trim());
|
|
134
|
+
const body = JSON.parse(text);
|
|
135
|
+
if (body.is_subs_active === false)
|
|
136
|
+
throw new Error("Muse Code subscription is inactive for this account");
|
|
137
|
+
if (typeof body.api_key !== "string" || !body.api_key.trim()) {
|
|
138
|
+
const action = String(body.action_url || body.require_payment_action_url || "").trim();
|
|
139
|
+
if (body.require_payment === true || action) {
|
|
140
|
+
throw new Error(`Muse Code subscription is required${action ? ": " + action : ""}`);
|
|
141
|
+
}
|
|
142
|
+
throw new Error("key response is missing api_key");
|
|
143
|
+
}
|
|
144
|
+
const email = String(body.user_email || "").trim().toLowerCase() || undefined;
|
|
145
|
+
const accountId = String(body.user_id || "").trim() || email;
|
|
146
|
+
if (!accountId)
|
|
147
|
+
throw new Error("key response is missing account identity");
|
|
148
|
+
return { oauthAccessToken: accessToken, apiKey: body.api_key.trim(), accountId, email };
|
|
149
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { readCache, writeCache, deviceAuthorize, pollToken, mintKey } from "./auth.js";
|
|
2
|
+
export const MuseCodeAuth = async () => {
|
|
3
|
+
return {
|
|
4
|
+
auth: {
|
|
5
|
+
provider: "meta",
|
|
6
|
+
methods: [
|
|
7
|
+
{
|
|
8
|
+
type: "oauth",
|
|
9
|
+
label: "Muse Code subscription (Meta device login)",
|
|
10
|
+
async authorize() {
|
|
11
|
+
const device = await deviceAuthorize();
|
|
12
|
+
const url = device.verification_uri_complete || device.verification_uri;
|
|
13
|
+
return {
|
|
14
|
+
url,
|
|
15
|
+
method: "auto",
|
|
16
|
+
instructions: `Open ${url}\nEnter code: ${device.user_code}`,
|
|
17
|
+
callback: async () => {
|
|
18
|
+
try {
|
|
19
|
+
const access = await pollToken(device.device_code, device.interval, device.expires_in);
|
|
20
|
+
const minted = await mintKey(access);
|
|
21
|
+
await writeCache(minted);
|
|
22
|
+
return { type: "success", key: minted.apiKey };
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return { type: "failed" };
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
loader: async () => {
|
|
33
|
+
const key = await readCache();
|
|
34
|
+
return key ? { apiKey: key } : {};
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opencode-muse-auth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Muse Spark in opencode billed to the Muse Code monthly subscription (Meta device login, no API key)",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"author": "TheStreamCode",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/TheStreamCode/opencode-muse-auth.git"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/TheStreamCode/opencode-muse-auth#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/TheStreamCode/opencode-muse-auth/issues"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"opencode",
|
|
20
|
+
"opencode-plugin",
|
|
21
|
+
"meta",
|
|
22
|
+
"muse",
|
|
23
|
+
"muse-spark",
|
|
24
|
+
"subscription",
|
|
25
|
+
"oauth",
|
|
26
|
+
"auth",
|
|
27
|
+
"plugin"
|
|
28
|
+
],
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"NOTICE"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"test": "node --test --test-concurrency=1 \"tests/*.test.ts\"",
|
|
38
|
+
"prepublishOnly": "npm run build"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@opencode-ai/plugin": "^1.18.31",
|
|
42
|
+
"@types/node": "^22.20.3",
|
|
43
|
+
"typescript": "^7.0.2"
|
|
44
|
+
}
|
|
45
|
+
}
|