@bonnard/agentops 0.1.1
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/dist/bin/agentops.mjs +193 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Bonnard (bonnard-data)
|
|
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.
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import pc from "picocolors";
|
|
4
|
+
import http from "node:http";
|
|
5
|
+
import open from "open";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import os from "node:os";
|
|
9
|
+
//#region src/lib/credentials.ts
|
|
10
|
+
const AGENTOPS_DIR = path.join(os.homedir(), ".agentops");
|
|
11
|
+
const CREDENTIALS_PATH = path.join(AGENTOPS_DIR, "credentials.json");
|
|
12
|
+
const CONFIG_PATH = path.join(AGENTOPS_DIR, "config.json");
|
|
13
|
+
function ensureDir() {
|
|
14
|
+
if (!fs.existsSync(AGENTOPS_DIR)) fs.mkdirSync(AGENTOPS_DIR, { recursive: true });
|
|
15
|
+
}
|
|
16
|
+
function saveCredentials(creds) {
|
|
17
|
+
ensureDir();
|
|
18
|
+
fs.writeFileSync(CREDENTIALS_PATH, JSON.stringify(creds, null, 2), { mode: 384 });
|
|
19
|
+
}
|
|
20
|
+
function loadCredentials() {
|
|
21
|
+
try {
|
|
22
|
+
const raw = fs.readFileSync(CREDENTIALS_PATH, "utf-8");
|
|
23
|
+
return JSON.parse(raw);
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function clearCredentials() {
|
|
29
|
+
try {
|
|
30
|
+
fs.unlinkSync(CREDENTIALS_PATH);
|
|
31
|
+
} catch {}
|
|
32
|
+
}
|
|
33
|
+
function saveConfig(config) {
|
|
34
|
+
ensureDir();
|
|
35
|
+
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2));
|
|
36
|
+
}
|
|
37
|
+
function loadConfig() {
|
|
38
|
+
try {
|
|
39
|
+
const raw = fs.readFileSync(CONFIG_PATH, "utf-8");
|
|
40
|
+
return JSON.parse(raw);
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/lib/api.ts
|
|
47
|
+
function getBaseUrl(urlOverride) {
|
|
48
|
+
if (urlOverride) return urlOverride.replace(/\/$/, "");
|
|
49
|
+
const config = loadConfig();
|
|
50
|
+
if (config?.url) return config.url.replace(/\/$/, "");
|
|
51
|
+
return "https://agentops.bonnard.ai";
|
|
52
|
+
}
|
|
53
|
+
async function post(path, body, baseUrl) {
|
|
54
|
+
return fetch(`${baseUrl}${path}`, {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: {
|
|
57
|
+
...getHeaders(),
|
|
58
|
+
"Content-Type": "application/json"
|
|
59
|
+
},
|
|
60
|
+
body: JSON.stringify(body)
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function getHeaders() {
|
|
64
|
+
const headers = {};
|
|
65
|
+
const creds = loadCredentials();
|
|
66
|
+
if (creds) headers["Authorization"] = `Bearer ${creds.accessToken}`;
|
|
67
|
+
return headers;
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/commands/login.ts
|
|
71
|
+
const CALLBACK_PORT = 9876;
|
|
72
|
+
async function loginCommand(options) {
|
|
73
|
+
const baseUrl = getBaseUrl(options.url);
|
|
74
|
+
console.log(pc.dim(`Server: ${baseUrl}`));
|
|
75
|
+
let authRes;
|
|
76
|
+
try {
|
|
77
|
+
authRes = await fetch(`${baseUrl}/auth/url?redirect_uri=${encodeURIComponent(`http://localhost:${CALLBACK_PORT}/callback`)}`);
|
|
78
|
+
} catch {
|
|
79
|
+
console.error(pc.red(`Cannot reach server at ${baseUrl}`));
|
|
80
|
+
console.error(pc.dim("Check the URL and ensure the server is running."));
|
|
81
|
+
process.exit(1);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (!authRes.ok) {
|
|
85
|
+
console.error(pc.red(`Failed to get auth URL: ${authRes.status} ${await authRes.text()}`));
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
const { url: authUrl } = await authRes.json();
|
|
89
|
+
const { code, state } = await captureCallback(authUrl);
|
|
90
|
+
console.log(pc.dim("Exchanging code for tokens..."));
|
|
91
|
+
const callbackRes = await post("/auth/callback", {
|
|
92
|
+
code,
|
|
93
|
+
state
|
|
94
|
+
}, baseUrl);
|
|
95
|
+
if (!callbackRes.ok) {
|
|
96
|
+
const body = await callbackRes.text();
|
|
97
|
+
console.error(pc.red(`Authentication failed: ${body}`));
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
const data = await callbackRes.json();
|
|
101
|
+
saveCredentials(data);
|
|
102
|
+
saveConfig({ url: baseUrl });
|
|
103
|
+
console.log();
|
|
104
|
+
console.log(pc.green("✓ Logged in successfully"));
|
|
105
|
+
console.log(` ${pc.bold(data.user.email)} (${data.org.name})`);
|
|
106
|
+
console.log(` Role: ${data.user.role}`);
|
|
107
|
+
console.log();
|
|
108
|
+
}
|
|
109
|
+
function captureCallback(authUrl) {
|
|
110
|
+
return new Promise((resolve, reject) => {
|
|
111
|
+
const server = http.createServer((req, res) => {
|
|
112
|
+
const url = new URL(req.url ?? "/", `http://localhost:${CALLBACK_PORT}`);
|
|
113
|
+
if (url.pathname !== "/callback") {
|
|
114
|
+
res.writeHead(404);
|
|
115
|
+
res.end("Not found");
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const code = url.searchParams.get("code");
|
|
119
|
+
const state = url.searchParams.get("state");
|
|
120
|
+
if (!code) {
|
|
121
|
+
const error = url.searchParams.get("error") ?? "No code received";
|
|
122
|
+
res.writeHead(400, { "Content-Type": "text/html" });
|
|
123
|
+
res.end(errorPage(error));
|
|
124
|
+
server.close();
|
|
125
|
+
reject(new Error(error));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
res.writeHead(200, { "Content-Type": "text/html" });
|
|
129
|
+
res.end(successPage());
|
|
130
|
+
server.close();
|
|
131
|
+
resolve({
|
|
132
|
+
code,
|
|
133
|
+
state: state ?? ""
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
server.listen(CALLBACK_PORT, () => {
|
|
137
|
+
console.log(pc.dim(`Listening on http://localhost:${CALLBACK_PORT}`));
|
|
138
|
+
console.log("Opening browser for authentication...");
|
|
139
|
+
console.log();
|
|
140
|
+
open(authUrl);
|
|
141
|
+
});
|
|
142
|
+
server.on("error", (err) => {
|
|
143
|
+
if (err.code === "EADDRINUSE") reject(/* @__PURE__ */ new Error(`Port ${CALLBACK_PORT} is in use. Close the process using it and try again.`));
|
|
144
|
+
else reject(err);
|
|
145
|
+
});
|
|
146
|
+
setTimeout(() => {
|
|
147
|
+
server.close();
|
|
148
|
+
reject(/* @__PURE__ */ new Error("Login timed out after 5 minutes"));
|
|
149
|
+
}, 300 * 1e3);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function successPage() {
|
|
153
|
+
return `<!DOCTYPE html>
|
|
154
|
+
<html><head><title>AgentOps</title></head>
|
|
155
|
+
<body style="font-family:system-ui;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#0a0a0a;color:#fafafa">
|
|
156
|
+
<div style="text-align:center">
|
|
157
|
+
<h1>Authenticated</h1>
|
|
158
|
+
<p>You can close this tab and return to the terminal.</p>
|
|
159
|
+
</div>
|
|
160
|
+
</body></html>`;
|
|
161
|
+
}
|
|
162
|
+
function errorPage(error) {
|
|
163
|
+
return `<!DOCTYPE html>
|
|
164
|
+
<html><head><title>AgentOps</title></head>
|
|
165
|
+
<body style="font-family:system-ui;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#0a0a0a;color:#fafafa">
|
|
166
|
+
<div style="text-align:center">
|
|
167
|
+
<h1>Authentication Failed</h1>
|
|
168
|
+
<p>${error}</p>
|
|
169
|
+
</div>
|
|
170
|
+
</body></html>`;
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/bin/agentops.ts
|
|
174
|
+
const program = new Command();
|
|
175
|
+
program.name("agentops").description("AgentOps CLI — setup and manage your AI agent skills").version("0.1.0");
|
|
176
|
+
program.command("login").description("Authenticate with AgentOps via your browser").option("--url <url>", "AgentOps server URL").action(loginCommand);
|
|
177
|
+
program.command("logout").description("Clear saved credentials").action(() => {
|
|
178
|
+
clearCredentials();
|
|
179
|
+
console.log(pc.green("✓ Logged out"));
|
|
180
|
+
});
|
|
181
|
+
program.command("whoami").description("Show current logged-in user").action(() => {
|
|
182
|
+
const creds = loadCredentials();
|
|
183
|
+
if (!creds) {
|
|
184
|
+
console.log(pc.yellow("Not logged in. Run: agentops login"));
|
|
185
|
+
process.exit(1);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
console.log(`${pc.bold(creds.user.email)} (${creds.org.name})`);
|
|
189
|
+
console.log(`Role: ${creds.user.role}`);
|
|
190
|
+
});
|
|
191
|
+
program.parse();
|
|
192
|
+
//#endregion
|
|
193
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bonnard/agentops",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"bin": {
|
|
6
|
+
"agentops": "./dist/bin/agentops.mjs"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/bonnard-data/agentops-cli"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20.0.0"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "tsx src/bin/agentops.ts",
|
|
20
|
+
"build": "tsdown src/bin/agentops.ts --format esm --out-dir dist/bin",
|
|
21
|
+
"typecheck": "tsc --noEmit",
|
|
22
|
+
"lint": "eslint ."
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"commander": "^14.0.3",
|
|
26
|
+
"open": "^11.0.0",
|
|
27
|
+
"picocolors": "^1.1.1"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@eslint/js": "^10.0.1",
|
|
31
|
+
"@types/node": "^22.15.0",
|
|
32
|
+
"eslint": "^10.0.1",
|
|
33
|
+
"tsdown": "^0.21.4",
|
|
34
|
+
"tsx": "^4.21.0",
|
|
35
|
+
"typescript": "^6.0.2",
|
|
36
|
+
"typescript-eslint": "^8.57.2"
|
|
37
|
+
}
|
|
38
|
+
}
|