@agentrouter/agentrouter 0.1.13 → 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 +16 -803
- package/dist/cli.js +3 -4
- package/dist/core.d.ts +28 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +596 -0
- package/dist/credentials.d.ts +43 -0
- package/dist/credentials.d.ts.map +1 -0
- package/dist/credentials.js +98 -0
- package/dist/login-page.d.ts +2 -0
- package/dist/login-page.d.ts.map +1 -0
- package/dist/login-page.js +45 -0
- package/dist/login.d.ts +23 -0
- package/dist/login.d.ts.map +1 -0
- package/dist/login.js +90 -0
- package/dist/terminal.d.ts +3 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +32 -0
- package/icon.svg +11 -0
- package/package.json +9 -17
- package/dist/cli-core.d.ts +0 -13
- package/dist/cli-core.d.ts.map +0 -1
- package/dist/cli-core.js +0 -627
- package/dist/index.d.ts +0 -553
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -797
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export declare const PRODUCTION_API_ORIGIN = "https://api.agentrouter.to";
|
|
2
|
+
export declare const CLI_VERSION: string;
|
|
3
|
+
export declare const DEFAULT_BASE_URL: string;
|
|
4
|
+
export declare const CLI_CLIENT_ID = "agentrouter-cli";
|
|
5
|
+
export type StoredCredential = {
|
|
6
|
+
kind: "oauth";
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
accessToken: string;
|
|
9
|
+
refreshToken: string;
|
|
10
|
+
expiresAt: number;
|
|
11
|
+
scope: string;
|
|
12
|
+
} | {
|
|
13
|
+
kind: "api_key";
|
|
14
|
+
baseUrl: string;
|
|
15
|
+
apiKey: string;
|
|
16
|
+
};
|
|
17
|
+
export type Env = Record<string, string | undefined>;
|
|
18
|
+
export declare function configDir(env: Env): string;
|
|
19
|
+
export declare function loadCredential(env: Env): Promise<StoredCredential | null>;
|
|
20
|
+
/** Written with 0600 permissions; never printed. */
|
|
21
|
+
export declare function saveCredential(env: Env, credential: StoredCredential): Promise<string>;
|
|
22
|
+
export declare function deleteCredential(env: Env): Promise<void>;
|
|
23
|
+
/** Cached describe results: tool_id → contract summary, so invoke can pass pricing_version. */
|
|
24
|
+
export interface CachedContract {
|
|
25
|
+
toolVersion: string;
|
|
26
|
+
pricingVersion: string;
|
|
27
|
+
maxPerCallUsd: string | null;
|
|
28
|
+
describedAt: string;
|
|
29
|
+
}
|
|
30
|
+
export declare function loadContracts(env: Env): Promise<Record<string, CachedContract>>;
|
|
31
|
+
export declare function saveContract(env: Env, toolId: string, contract: CachedContract): Promise<void>;
|
|
32
|
+
export interface SavedInvocation {
|
|
33
|
+
tool_id: string;
|
|
34
|
+
tool_version?: string;
|
|
35
|
+
pricing_version: string;
|
|
36
|
+
input: unknown;
|
|
37
|
+
max_cost_usd?: string;
|
|
38
|
+
session_id?: string;
|
|
39
|
+
}
|
|
40
|
+
export declare function invocationPath(env: Env, baseUrl: string, owner: string, key: string): string;
|
|
41
|
+
export declare function loadInvocation(path: string): Promise<SavedInvocation | null>;
|
|
42
|
+
export declare function saveInvocation(env: Env, path: string, body: SavedInvocation): Promise<SavedInvocation>;
|
|
43
|
+
//# sourceMappingURL=credentials.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../src/credentials.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,qBAAqB,+BAA+B,CAAC;AAElE,eAAO,MAAM,WAAW,EAAE,MAAyB,CAAC;AACpD,eAAO,MAAM,gBAAgB,EAAE,MAA+D,CAAC;AAC/F,eAAO,MAAM,aAAa,oBAAoB,CAAC;AAE/C,MAAM,MAAM,gBAAgB,GACxB;IACE,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;CACf,GACD;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;AAErD,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAI1C;AAMD,wBAAsB,cAAc,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAQ/E;AAED,oDAAoD;AACpD,wBAAsB,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC,CAY5F;AAED,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAE9D;AAED,+FAA+F;AAC/F,MAAM,WAAW,cAAc;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,WAAW,EAAE,MAAM,CAAC;CACrB;AAMD,wBAAsB,aAAa,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,CAMrF;AAED,wBAAsB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAKpG;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,CAAC;IACxB,KAAK,EAAE,OAAO,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAG5F;AAED,wBAAsB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CASlF;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,CAY5G"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
export const PRODUCTION_API_ORIGIN = "https://api.agentrouter.to";
|
|
7
|
+
const manifest = createRequire(import.meta.url)("../package.json");
|
|
8
|
+
export const CLI_VERSION = manifest.version;
|
|
9
|
+
export const DEFAULT_BASE_URL = manifest.agentrouterApiOrigin ?? PRODUCTION_API_ORIGIN;
|
|
10
|
+
export const CLI_CLIENT_ID = "agentrouter-cli";
|
|
11
|
+
export function configDir(env) {
|
|
12
|
+
if (env.AGENTROUTER_CONFIG_DIR)
|
|
13
|
+
return env.AGENTROUTER_CONFIG_DIR;
|
|
14
|
+
const base = env.XDG_CONFIG_HOME || join(homedir(), ".config");
|
|
15
|
+
return join(base, "agentrouter");
|
|
16
|
+
}
|
|
17
|
+
function credentialsPath(env) {
|
|
18
|
+
return join(configDir(env), "credentials.json");
|
|
19
|
+
}
|
|
20
|
+
export async function loadCredential(env) {
|
|
21
|
+
try {
|
|
22
|
+
const saved = JSON.parse(await readFile(credentialsPath(env), "utf8"));
|
|
23
|
+
if (saved && saved.kind === undefined && typeof saved.apiKey === "string")
|
|
24
|
+
return { ...saved, kind: "api_key" };
|
|
25
|
+
return saved;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Written with 0600 permissions; never printed. */
|
|
32
|
+
export async function saveCredential(env, credential) {
|
|
33
|
+
const dir = configDir(env);
|
|
34
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
35
|
+
const path = credentialsPath(env);
|
|
36
|
+
const temporary = path + "." + randomUUID();
|
|
37
|
+
try {
|
|
38
|
+
await writeFile(temporary, JSON.stringify(credential) + "\n", { mode: 0o600, flag: "wx" });
|
|
39
|
+
await rename(temporary, path);
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
await rm(temporary, { force: true });
|
|
43
|
+
}
|
|
44
|
+
return path;
|
|
45
|
+
}
|
|
46
|
+
export async function deleteCredential(env) {
|
|
47
|
+
await rm(credentialsPath(env), { force: true });
|
|
48
|
+
}
|
|
49
|
+
function contractsPath(env) {
|
|
50
|
+
return join(configDir(env), "contracts.json");
|
|
51
|
+
}
|
|
52
|
+
export async function loadContracts(env) {
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(await readFile(contractsPath(env), "utf8"));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export async function saveContract(env, toolId, contract) {
|
|
61
|
+
const contracts = await loadContracts(env);
|
|
62
|
+
contracts[toolId] = contract;
|
|
63
|
+
await mkdir(configDir(env), { recursive: true, mode: 0o700 });
|
|
64
|
+
await writeFile(contractsPath(env), `${JSON.stringify(contracts, null, 2)}\n`, { mode: 0o600 });
|
|
65
|
+
}
|
|
66
|
+
export function invocationPath(env, baseUrl, owner, key) {
|
|
67
|
+
const name = createHash("sha256").update(JSON.stringify([baseUrl, owner, key])).digest("hex");
|
|
68
|
+
return join(configDir(env), "invocations", `${name}.json`);
|
|
69
|
+
}
|
|
70
|
+
export async function loadInvocation(path) {
|
|
71
|
+
try {
|
|
72
|
+
const body = JSON.parse(await readFile(path, "utf8"));
|
|
73
|
+
if (!body || typeof body.tool_id !== "string" || typeof body.pricing_version !== "string" || !("input" in body))
|
|
74
|
+
throw new Error("Invalid saved invocation; keep the file and recover the run through runs list/get");
|
|
75
|
+
return body;
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (error.code === "ENOENT")
|
|
79
|
+
return null;
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export async function saveInvocation(env, path, body) {
|
|
84
|
+
await mkdir(join(configDir(env), "invocations"), { recursive: true, mode: 0o700 });
|
|
85
|
+
try {
|
|
86
|
+
// Never overwrite an earlier request, including one whose response was lost.
|
|
87
|
+
await writeFile(path, `${JSON.stringify(body)}\n`, { mode: 0o600, flag: "wx" });
|
|
88
|
+
return body;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (error.code !== "EEXIST")
|
|
92
|
+
throw error;
|
|
93
|
+
const saved = await loadInvocation(path);
|
|
94
|
+
if (!saved)
|
|
95
|
+
throw new Error("Saved invocation disappeared; retry the command");
|
|
96
|
+
return saved;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"login-page.d.ts","sourceRoot":"","sources":["../src/login-page.ts"],"names":[],"mappings":"AAKA,wBAAgB,eAAe,CAAC,EAAE,EAAE,OAAO,GAAG,MAAM,CAyCnD"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
const brandMark = `data:image/svg+xml;base64,${readFileSync(createRequire(import.meta.url).resolve("../icon.svg"), "base64")}`;
|
|
4
|
+
export function renderLoginPage(ok) {
|
|
5
|
+
const title = ok ? "Browser sign‑in complete." : "Sign‑in wasn't completed.";
|
|
6
|
+
return `<!doctype html>
|
|
7
|
+
<html lang="en">
|
|
8
|
+
<head>
|
|
9
|
+
<meta charset="utf-8">
|
|
10
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
11
|
+
<meta name="referrer" content="no-referrer">
|
|
12
|
+
<link rel="icon" href="${brandMark}">
|
|
13
|
+
<title>${title} · AgentRouter CLI</title>
|
|
14
|
+
<style>
|
|
15
|
+
:root { color-scheme: light; color: #141414; background: #fff; font-family: "Hanken Grotesk", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
|
|
16
|
+
* { box-sizing: border-box; }
|
|
17
|
+
body { margin: 0; -webkit-font-smoothing: antialiased; }
|
|
18
|
+
main { width: min(400px, calc(100% - 40px)); margin: 120px auto 48px; text-align: center; }
|
|
19
|
+
.brand { width: 40px; height: 40px; display: block; margin: auto; }
|
|
20
|
+
h1 { margin: 40px 0 16px; font-size: 32px; line-height: 36px; font-weight: 600; letter-spacing: -.01em; }
|
|
21
|
+
p { margin: 0; font-size: 16px; line-height: 24px; color: #6b6b6b; }
|
|
22
|
+
.next-step { margin: 32px 0; padding: 16px 20px; border-radius: 16px; background: #f5f5f5; text-align: left; }
|
|
23
|
+
.command-label { margin-bottom: 8px; font-size: 14px; line-height: 20px; }
|
|
24
|
+
code, .prompt { font: 14px/22px "JetBrains Mono", ui-monospace, monospace; }
|
|
25
|
+
.prompt { color: #6b6b6b; user-select: none; }
|
|
26
|
+
a { color: #6b6b6b; font-size: 14px; text-underline-offset: 3px; }
|
|
27
|
+
a:hover { color: #141414; }
|
|
28
|
+
a:focus-visible { outline: 2px solid #1f6feb; outline-offset: 4px; }
|
|
29
|
+
@media (max-width: 767px) { main { margin-top: 56px; } }
|
|
30
|
+
</style>
|
|
31
|
+
</head>
|
|
32
|
+
<body>
|
|
33
|
+
<main aria-labelledby="result-title">
|
|
34
|
+
<img class="brand" src="${brandMark}" alt="AgentRouter" width="40" height="40">
|
|
35
|
+
<h1 id="result-title">${title}</h1>
|
|
36
|
+
<p>${ok ? "You can close this tab. Your terminal will confirm when sign-in finishes." : "Close this tab and return to your terminal to start a new sign-in."}</p>
|
|
37
|
+
<div class="next-step">
|
|
38
|
+
<p class="command-label">${ok ? "Once connected, check your account" : "Run this command to try again"}</p>
|
|
39
|
+
<span class="prompt" aria-hidden="true">$ </span><code>agentrouter ${ok ? "whoami" : "connect"}</code>
|
|
40
|
+
</div>
|
|
41
|
+
<a href="https://docs.agentrouter.to/integrations/cli">CLI documentation</a>
|
|
42
|
+
</main>
|
|
43
|
+
</body>
|
|
44
|
+
</html>`;
|
|
45
|
+
}
|
package/dist/login.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface LoginResult {
|
|
2
|
+
accessToken: string;
|
|
3
|
+
refreshToken: string;
|
|
4
|
+
expiresIn: number;
|
|
5
|
+
scope: string;
|
|
6
|
+
}
|
|
7
|
+
export interface LoginOptions {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
scopes: string[];
|
|
10
|
+
openBrowser: boolean;
|
|
11
|
+
timeoutMs: number;
|
|
12
|
+
fetchImpl: typeof fetch;
|
|
13
|
+
log: (line: string) => void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* OAuth authorization code + PKCE over a loopback redirect (RFC 8252). The browser
|
|
17
|
+
* shows AgentRouter's client sign-in page; the code comes back to 127.0.0.1 and is
|
|
18
|
+
* exchanged here. No key is copied by hand.
|
|
19
|
+
*/
|
|
20
|
+
export declare function browserLogin(options: LoginOptions): Promise<LoginResult>;
|
|
21
|
+
export declare function refreshTokens(baseUrl: string, refreshToken: string, fetchImpl: typeof fetch): Promise<LoginResult>;
|
|
22
|
+
export declare function revokeToken(baseUrl: string, token: string, fetchImpl: typeof fetch): Promise<void>;
|
|
23
|
+
//# sourceMappingURL=login.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"login.d.ts","sourceRoot":"","sources":["../src/login.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,WAAW;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,WAAW,EAAE,OAAO,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,OAAO,KAAK,CAAC;IACxB,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7B;AAMD;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CA4D9E;AAED,wBAAsB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,CAWxH;AAED,wBAAsB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAMxG"}
|
package/dist/login.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { openInBrowser } from "./terminal.js";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import { CLI_CLIENT_ID } from "./credentials.js";
|
|
5
|
+
import { renderLoginPage } from "./login-page.js";
|
|
6
|
+
function base64url(buffer) {
|
|
7
|
+
return buffer.toString("base64url");
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* OAuth authorization code + PKCE over a loopback redirect (RFC 8252). The browser
|
|
11
|
+
* shows AgentRouter's client sign-in page; the code comes back to 127.0.0.1 and is
|
|
12
|
+
* exchanged here. No key is copied by hand.
|
|
13
|
+
*/
|
|
14
|
+
export async function browserLogin(options) {
|
|
15
|
+
const verifier = base64url(randomBytes(32));
|
|
16
|
+
const challenge = base64url(createHash("sha256").update(verifier).digest());
|
|
17
|
+
const state = base64url(randomBytes(16));
|
|
18
|
+
const server = createServer();
|
|
19
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
20
|
+
const port = server.address().port;
|
|
21
|
+
const redirectUri = `http://127.0.0.1:${port}/callback`;
|
|
22
|
+
const authorizeUrl = new URL(`${options.baseUrl}/oauth/authorize`);
|
|
23
|
+
authorizeUrl.search = new URLSearchParams({
|
|
24
|
+
response_type: "code",
|
|
25
|
+
client_id: CLI_CLIENT_ID,
|
|
26
|
+
redirect_uri: redirectUri,
|
|
27
|
+
code_challenge: challenge,
|
|
28
|
+
code_challenge_method: "S256",
|
|
29
|
+
scope: options.scopes.join(" "),
|
|
30
|
+
state,
|
|
31
|
+
resource: options.baseUrl
|
|
32
|
+
}).toString();
|
|
33
|
+
const code = await new Promise((resolve, reject) => {
|
|
34
|
+
const timer = setTimeout(() => {
|
|
35
|
+
server.close();
|
|
36
|
+
reject(new Error("Login timed out waiting for the browser. Run `agentrouter connect` again, or use an API key (AGENTROUTER_API_KEY)."));
|
|
37
|
+
}, options.timeoutMs);
|
|
38
|
+
server.on("request", (req, res) => {
|
|
39
|
+
const url = new URL(req.url ?? "/", redirectUri);
|
|
40
|
+
if (url.pathname !== "/callback") {
|
|
41
|
+
res.writeHead(404).end();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const error = url.searchParams.get("error");
|
|
45
|
+
const returned = url.searchParams.get("code");
|
|
46
|
+
const ok = !error && returned && url.searchParams.get("state") === state;
|
|
47
|
+
res.writeHead(ok ? 200 : 400, { "content-type": "text/html; charset=utf-8" });
|
|
48
|
+
res.end(renderLoginPage(Boolean(ok)));
|
|
49
|
+
clearTimeout(timer);
|
|
50
|
+
server.close();
|
|
51
|
+
if (ok)
|
|
52
|
+
resolve(returned);
|
|
53
|
+
else
|
|
54
|
+
reject(new Error(error === "access_denied" ? "Access was denied in the browser." : "The login response did not match this request."));
|
|
55
|
+
});
|
|
56
|
+
options.log(`Opening ${authorizeUrl.toString()}`);
|
|
57
|
+
options.log("If the browser does not open, paste that URL into a browser on this machine.");
|
|
58
|
+
if (options.openBrowser)
|
|
59
|
+
openInBrowser(authorizeUrl.toString());
|
|
60
|
+
});
|
|
61
|
+
const response = await options.fetchImpl(`${options.baseUrl}/oauth/token`, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
64
|
+
body: new URLSearchParams({ grant_type: "authorization_code", code, redirect_uri: redirectUri, client_id: CLI_CLIENT_ID, code_verifier: verifier }).toString()
|
|
65
|
+
});
|
|
66
|
+
const body = (await response.json());
|
|
67
|
+
if (!response.ok || typeof body.access_token !== "string" || typeof body.refresh_token !== "string") {
|
|
68
|
+
throw new Error(`Token exchange failed: ${String(body.error_description ?? body.error ?? response.status)}`);
|
|
69
|
+
}
|
|
70
|
+
return { accessToken: body.access_token, refreshToken: body.refresh_token, expiresIn: Number(body.expires_in) || 3600, scope: String(body.scope ?? "") };
|
|
71
|
+
}
|
|
72
|
+
export async function refreshTokens(baseUrl, refreshToken, fetchImpl) {
|
|
73
|
+
const response = await fetchImpl(`${baseUrl}/oauth/token`, {
|
|
74
|
+
method: "POST",
|
|
75
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
76
|
+
body: new URLSearchParams({ grant_type: "refresh_token", refresh_token: refreshToken, client_id: CLI_CLIENT_ID }).toString()
|
|
77
|
+
});
|
|
78
|
+
const body = (await response.json());
|
|
79
|
+
if (!response.ok || typeof body.access_token !== "string" || typeof body.refresh_token !== "string") {
|
|
80
|
+
throw new Error("Your login expired. Run `agentrouter connect` again.");
|
|
81
|
+
}
|
|
82
|
+
return { accessToken: body.access_token, refreshToken: body.refresh_token, expiresIn: Number(body.expires_in) || 3600, scope: String(body.scope ?? "") };
|
|
83
|
+
}
|
|
84
|
+
export async function revokeToken(baseUrl, token, fetchImpl) {
|
|
85
|
+
await fetchImpl(`${baseUrl}/oauth/revoke`, {
|
|
86
|
+
method: "POST",
|
|
87
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
88
|
+
body: new URLSearchParams({ token }).toString()
|
|
89
|
+
}).catch(() => undefined);
|
|
90
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terminal.d.ts","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"mappings":"AAIA,wBAAsB,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAY/C;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAW3D"}
|
package/dist/terminal.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import { Writable } from "node:stream";
|
|
4
|
+
export async function readKey() {
|
|
5
|
+
const terminal = Boolean(process.stdin.isTTY);
|
|
6
|
+
if (terminal)
|
|
7
|
+
process.stderr.write("Paste your AgentRouter API key (hidden): ");
|
|
8
|
+
const muted = new Writable({ write(_chunk, _encoding, callback) { callback(); } });
|
|
9
|
+
const lines = createInterface({ input: process.stdin, output: muted, terminal });
|
|
10
|
+
try {
|
|
11
|
+
for await (const line of lines)
|
|
12
|
+
return line;
|
|
13
|
+
return "";
|
|
14
|
+
}
|
|
15
|
+
finally {
|
|
16
|
+
lines.close();
|
|
17
|
+
if (terminal)
|
|
18
|
+
process.stderr.write("\n");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function openInBrowser(url) {
|
|
22
|
+
const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? "rundll32" : "xdg-open";
|
|
23
|
+
const args = process.platform === "win32" ? ["url.dll,FileProtocolHandler", url] : [url];
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
const child = spawn(command, args, { stdio: "ignore", detached: true });
|
|
26
|
+
child.once("error", () => resolve(false));
|
|
27
|
+
child.once("exit", (code) => resolve(code === 0));
|
|
28
|
+
// Some Linux openers remain attached to the browser for its lifetime.
|
|
29
|
+
const timer = setTimeout(() => { child.unref(); resolve(true); }, 1000);
|
|
30
|
+
child.once("close", () => clearTimeout(timer));
|
|
31
|
+
});
|
|
32
|
+
}
|
package/icon.svg
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<svg preserveAspectRatio="none" overflow="visible" style="display: block;" width="160" height="160" viewBox="0 0 160 160" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<style>@media (prefers-color-scheme: dark) { #Grid { fill: white; } }</style>
|
|
3
|
+
<g id="Logo/Mark" clip-path="url(#clip0_0_21)">
|
|
4
|
+
<path id="Grid" d="M106.667 0C125.335 1.20353e-10 134.669 -0.000268627 141.8 3.63281C148.072 6.82859 153.171 11.9281 156.367 18.2002C160 25.3305 160 34.6648 160 53.333V106.667C160 125.335 160 134.669 156.367 141.8C153.171 148.072 148.072 153.171 141.8 156.367C134.669 160 125.335 160 106.667 160H53.333C34.6648 160 25.3305 160 18.2002 156.367C11.9281 153.171 6.82859 148.072 3.63281 141.8C-0.000268627 134.669 1.20353e-10 125.335 0 106.667V53.333C1.20353e-10 34.6648 -0.000268627 25.3305 3.63281 18.2002C6.82859 11.9281 11.9281 6.82859 18.2002 3.63281C25.3305 -0.000268627 34.6648 1.20353e-10 53.333 0H106.667ZM47.333 10C34.2654 10 27.7315 9.99986 22.7402 12.543C18.3498 14.78 14.78 18.3498 12.543 22.7402C9.99986 27.7315 10 34.2654 10 47.333V112.667C10 125.735 9.99986 132.269 12.543 137.26C14.78 141.65 18.3498 145.22 22.7402 147.457C27.7315 150 34.2654 150 47.333 150H112.667C125.735 150 132.269 150 137.26 147.457C141.65 145.22 145.22 141.65 147.457 137.26C150 132.269 150 125.735 150 112.667V47.333C150 34.2654 150 27.7315 147.457 22.7402C145.22 18.3498 141.65 14.78 137.26 12.543C132.269 9.99986 125.735 10 112.667 10H47.333ZM105 23.75C107.761 23.75 110 25.9886 110 28.75V50H131.25C134.011 50 136.25 52.2386 136.25 55C136.25 57.7614 134.011 60 131.25 60H110V100H131.25C134.011 100 136.25 102.239 136.25 105C136.25 107.761 134.011 110 131.25 110H110V131.25C110 134.011 107.761 136.25 105 136.25C102.239 136.25 100 134.011 100 131.25V110H60V131.25C60 134.011 57.7614 136.25 55 136.25C52.2386 136.25 50 134.011 50 131.25V110H28.75C25.9886 110 23.75 107.761 23.75 105C23.75 102.239 25.9886 100 28.75 100H50V60H28.75C25.9886 60 23.75 57.7614 23.75 55C23.75 52.2386 25.9886 50 28.75 50H50V28.75C50 25.9886 52.2386 23.75 55 23.75C57.7614 23.75 60 25.9886 60 28.75V50H100V28.75C100 25.9886 102.239 23.75 105 23.75ZM60 100H100V60H60V100Z" fill="#141414"/>
|
|
5
|
+
</g>
|
|
6
|
+
<defs>
|
|
7
|
+
<clipPath id="clip0_0_21">
|
|
8
|
+
<rect width="160" height="160" fill="white"/>
|
|
9
|
+
</clipPath>
|
|
10
|
+
</defs>
|
|
11
|
+
</svg>
|
package/package.json
CHANGED
|
@@ -1,46 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentrouter/agentrouter",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "AgentRouter CLI: find, describe and invoke provider tools with one wallet.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/index.js",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
6
|
"bin": {
|
|
9
7
|
"agentrouter": "dist/cli.js"
|
|
10
8
|
},
|
|
11
|
-
"exports": {
|
|
12
|
-
".": {
|
|
13
|
-
"types": "./dist/index.d.ts",
|
|
14
|
-
"default": "./dist/index.js"
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
"sideEffects": false,
|
|
18
9
|
"engines": {
|
|
19
10
|
"node": ">=18"
|
|
20
11
|
},
|
|
21
12
|
"repository": {
|
|
22
13
|
"type": "git",
|
|
23
14
|
"url": "git+https://github.com/pageonelab/web-agentrouter.git",
|
|
24
|
-
"directory": "
|
|
15
|
+
"directory": "cli/agentrouter"
|
|
25
16
|
},
|
|
26
|
-
"homepage": "https://docs.agentrouter.to/integrations/
|
|
17
|
+
"homepage": "https://docs.agentrouter.to/integrations/cli",
|
|
27
18
|
"bugs": {
|
|
28
19
|
"url": "https://github.com/pageonelab/web-agentrouter/issues"
|
|
29
20
|
},
|
|
30
21
|
"keywords": [
|
|
31
22
|
"agentrouter",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
23
|
+
"cli",
|
|
24
|
+
"agents",
|
|
25
|
+
"mcp"
|
|
35
26
|
],
|
|
36
27
|
"publishConfig": {
|
|
37
28
|
"access": "public"
|
|
38
29
|
},
|
|
39
30
|
"files": [
|
|
40
31
|
"dist",
|
|
32
|
+
"icon.svg",
|
|
41
33
|
"README.md"
|
|
42
34
|
],
|
|
43
35
|
"scripts": {
|
|
44
|
-
"build": "tsc -p tsconfig.json"
|
|
36
|
+
"build": "node --input-type=module -e \"import { rmSync } from 'node:fs'; rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.json"
|
|
45
37
|
}
|
|
46
38
|
}
|
package/dist/cli-core.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { type AgenticApiFetchLike } from "./index.js";
|
|
2
|
-
type CliEnv = Record<string, string | undefined>;
|
|
3
|
-
type CliIo = {
|
|
4
|
-
env?: CliEnv;
|
|
5
|
-
stdout?: (value: string) => void;
|
|
6
|
-
stderr?: (value: string) => void;
|
|
7
|
-
readFile?: (path: string) => Promise<string>;
|
|
8
|
-
fetch?: AgenticApiFetchLike;
|
|
9
|
-
};
|
|
10
|
-
export declare function runAgentRouterCli(argv: string[], io?: CliIo): Promise<number>;
|
|
11
|
-
export declare const runAgenticCli: typeof runAgentRouterCli;
|
|
12
|
-
export {};
|
|
13
|
-
//# sourceMappingURL=cli-core.d.ts.map
|
package/dist/cli-core.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cli-core.d.ts","sourceRoot":"","sources":["../src/cli-core.ts"],"names":[],"mappings":"AAGA,OAAO,EAKL,KAAK,mBAAmB,EAGzB,MAAM,YAAY,CAAC;AAEpB,KAAK,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;AAEjD,KAAK,KAAK,GAAG;IACX,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7C,KAAK,CAAC,EAAE,mBAAmB,CAAC;CAC7B,CAAC;AAk0BF,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,MAAM,EAAE,EACd,EAAE,GAAE,KAAU,GACb,OAAO,CAAC,MAAM,CAAC,CA2GjB;AAED,eAAO,MAAM,aAAa,0BAAoB,CAAC"}
|