reasonally 0.0.0-stage → 0.2.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/README.md +70 -2
- package/dist/api.js +50 -0
- package/dist/browser/command.js +93 -0
- package/dist/browser/guards.js +138 -0
- package/dist/browser/pairing.js +123 -0
- package/dist/browser/protocol.js +172 -0
- package/dist/browser/server.js +157 -0
- package/dist/browser/sessions.js +478 -0
- package/dist/catalog.js +55 -0
- package/dist/cli.js +220 -0
- package/dist/config.js +86 -0
- package/dist/harnesses/cline.js +26 -0
- package/dist/harnesses/crush.js +47 -0
- package/dist/harnesses/hermes.js +33 -0
- package/dist/harnesses/index.js +7 -0
- package/dist/harnesses/opencode.js +44 -0
- package/dist/harnesses/pi.js +46 -0
- package/dist/harnesses/types.js +18 -0
- package/dist/index.js +3 -0
- package/dist/launch.js +59 -0
- package/dist/login.js +113 -0
- package/dist/pkce.js +10 -0
- package/package.json +31 -4
package/dist/login.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { hostname } from "node:os";
|
|
4
|
+
import { exchangeCode } from "./api.js";
|
|
5
|
+
import { createPkce } from "./pkce.js";
|
|
6
|
+
/** How long to wait for the user to approve in the browser. */
|
|
7
|
+
const LOGIN_TIMEOUT_MS = 5 * 60 * 1000;
|
|
8
|
+
export function authorizeUrl(appUrl, params) {
|
|
9
|
+
const query = new URLSearchParams({
|
|
10
|
+
redirect_uri: params.redirectUri,
|
|
11
|
+
code_challenge: params.challenge,
|
|
12
|
+
code_challenge_method: "S256",
|
|
13
|
+
state: params.state,
|
|
14
|
+
device: params.device,
|
|
15
|
+
});
|
|
16
|
+
return `${appUrl}/cli/authorize?${query}`;
|
|
17
|
+
}
|
|
18
|
+
/** Interprets a request to the loopback server. Only `/callback` with our `state` counts. */
|
|
19
|
+
export function readCallback(requestUrl, state) {
|
|
20
|
+
const url = new URL(requestUrl, "http://127.0.0.1");
|
|
21
|
+
if (url.pathname !== "/callback")
|
|
22
|
+
return { kind: "ignore" };
|
|
23
|
+
if (url.searchParams.get("state") !== state)
|
|
24
|
+
return { kind: "invalid" };
|
|
25
|
+
if (url.searchParams.get("error") === "access_denied")
|
|
26
|
+
return { kind: "denied" };
|
|
27
|
+
const code = url.searchParams.get("code");
|
|
28
|
+
return code ? { kind: "code", code } : { kind: "invalid" };
|
|
29
|
+
}
|
|
30
|
+
const page = (title, body) => `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${title}</title><style>body{font:16px/1.5 system-ui,sans-serif;display:grid;place-items:center;min-height:100vh;margin:0;padding:0 16px;color:#111;background:#fafafa}@media(prefers-color-scheme:dark){body{color:#eee;background:#111}}main{max-width:28rem;text-align:center}h1{font-size:1.25rem}</style></head><body><main><h1>${title}</h1><p>${body}</p></main></body></html>`;
|
|
31
|
+
function openBrowser(url) {
|
|
32
|
+
const [command, args] = process.platform === "darwin"
|
|
33
|
+
? ["open", [url]]
|
|
34
|
+
: process.platform === "win32"
|
|
35
|
+
? ["cmd", ["/c", "start", '""', url.replaceAll("&", "^&")]]
|
|
36
|
+
: ["xdg-open", [url]];
|
|
37
|
+
try {
|
|
38
|
+
const child = spawn(command, args, { stdio: "ignore", detached: true });
|
|
39
|
+
child.on("error", () => { }); // no browser: the printed URL still works
|
|
40
|
+
child.unref();
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// same
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Waits for the browser to come back to the loopback server with a code (or a refusal). */
|
|
47
|
+
function waitForCallback(server, state) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const timer = setTimeout(() => reject(new Error("Timed out waiting for approval in the browser. Run the login again.")), LOGIN_TIMEOUT_MS);
|
|
50
|
+
server.on("request", (req, res) => {
|
|
51
|
+
const result = readCallback(req.url ?? "/", state);
|
|
52
|
+
const reply = (status, title, body) => {
|
|
53
|
+
res.writeHead(status, { "content-type": "text/html; charset=utf-8", connection: "close" });
|
|
54
|
+
res.end(page(title, body));
|
|
55
|
+
};
|
|
56
|
+
if (result.kind === "ignore")
|
|
57
|
+
return reply(404, "Not found", "Nothing to see here.");
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
if (result.kind === "code") {
|
|
60
|
+
reply(200, "You're logged in", "You can close this tab and go back to your terminal.");
|
|
61
|
+
resolve(result.code);
|
|
62
|
+
}
|
|
63
|
+
else if (result.kind === "denied") {
|
|
64
|
+
reply(200, "Login cancelled", "You can close this tab.");
|
|
65
|
+
reject(new Error("Login was cancelled in the browser."));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
reply(400, "Login failed", "This response didn't match the login in your terminal.");
|
|
69
|
+
reject(new Error("The browser returned an unexpected response. Run the login again."));
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Browser login: opens the approval page, receives the code on a one-shot loopback server and
|
|
76
|
+
* exchanges it (with the PKCE verifier) for an API key.
|
|
77
|
+
*/
|
|
78
|
+
export async function browserLogin(appUrl, log, { open = true } = {}) {
|
|
79
|
+
const pkce = createPkce();
|
|
80
|
+
const server = createServer();
|
|
81
|
+
await new Promise((resolve, reject) => {
|
|
82
|
+
server.once("error", reject);
|
|
83
|
+
server.listen(0, "127.0.0.1", () => resolve());
|
|
84
|
+
});
|
|
85
|
+
try {
|
|
86
|
+
const { port } = server.address();
|
|
87
|
+
const redirectUri = `http://127.0.0.1:${port}/callback`;
|
|
88
|
+
const url = authorizeUrl(appUrl, {
|
|
89
|
+
redirectUri,
|
|
90
|
+
challenge: pkce.challenge,
|
|
91
|
+
state: pkce.state,
|
|
92
|
+
device: hostname().replace(/\.local$/, ""),
|
|
93
|
+
});
|
|
94
|
+
if (open) {
|
|
95
|
+
log("Opening your browser to approve this computer…");
|
|
96
|
+
log(`If it doesn't open, visit:\n\n ${url}\n`);
|
|
97
|
+
openBrowser(url);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
log(`Open this link in the browser where you're signed in, on this computer:\n\n ${url}\n`);
|
|
101
|
+
}
|
|
102
|
+
const code = await waitForCallback(server, pkce.state);
|
|
103
|
+
return await exchangeCode(appUrl, {
|
|
104
|
+
code,
|
|
105
|
+
code_verifier: pkce.verifier,
|
|
106
|
+
redirect_uri: redirectUri,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
server.closeAllConnections();
|
|
111
|
+
server.close();
|
|
112
|
+
}
|
|
113
|
+
}
|
package/dist/pkce.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
/** A PKCE pair (RFC 7636, S256) and a `state` value for one browser login. */
|
|
3
|
+
export function createPkce() {
|
|
4
|
+
const verifier = randomBytes(32).toString("base64url");
|
|
5
|
+
return {
|
|
6
|
+
verifier,
|
|
7
|
+
challenge: createHash("sha256").update(verifier).digest("base64url"),
|
|
8
|
+
state: randomBytes(16).toString("base64url"),
|
|
9
|
+
};
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reasonally",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Run the coding agents you already use on Reasonally models.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"reasonally": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=20"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"playwright-core": "1.64.1"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.build.json",
|
|
20
|
+
"prepublishOnly": "npm run build"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"llm",
|
|
24
|
+
"cli",
|
|
25
|
+
"coding-agent",
|
|
26
|
+
"opencode",
|
|
27
|
+
"crush",
|
|
28
|
+
"cline",
|
|
29
|
+
"pi",
|
|
30
|
+
"hermes"
|
|
31
|
+
],
|
|
32
|
+
"license": "UNLICENSED"
|
|
33
|
+
}
|