@an-sdk/cli 0.0.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/dist/index.js +161 -0
- package/package.json +25 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/login.ts
|
|
4
|
+
import { createInterface } from "readline/promises";
|
|
5
|
+
|
|
6
|
+
// src/config.ts
|
|
7
|
+
import { readFileSync, writeFileSync, mkdirSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
import { homedir } from "os";
|
|
10
|
+
var AN_DIR = join(homedir(), ".an");
|
|
11
|
+
var CREDENTIALS_PATH = join(AN_DIR, "credentials");
|
|
12
|
+
var PROJECT_PATH = join(process.cwd(), ".an", "project.json");
|
|
13
|
+
function getApiKey() {
|
|
14
|
+
try {
|
|
15
|
+
const data = JSON.parse(readFileSync(CREDENTIALS_PATH, "utf-8"));
|
|
16
|
+
return data.apiKey || null;
|
|
17
|
+
} catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function saveApiKey(apiKey) {
|
|
22
|
+
mkdirSync(AN_DIR, { recursive: true });
|
|
23
|
+
writeFileSync(CREDENTIALS_PATH, JSON.stringify({ apiKey }, null, 2));
|
|
24
|
+
}
|
|
25
|
+
function getProject() {
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(readFileSync(PROJECT_PATH, "utf-8"));
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function saveProject(data) {
|
|
33
|
+
mkdirSync(join(process.cwd(), ".an"), { recursive: true });
|
|
34
|
+
writeFileSync(PROJECT_PATH, JSON.stringify(data, null, 2));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/login.ts
|
|
38
|
+
var API_BASE = process.env.AN_API_URL || "https://an.dev/api/v1";
|
|
39
|
+
async function login() {
|
|
40
|
+
const existing = getApiKey();
|
|
41
|
+
if (existing) {
|
|
42
|
+
console.log(
|
|
43
|
+
"Already logged in. Run `an login` again to re-authenticate.\n"
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
47
|
+
const apiKey = await rl.question("Enter your API key: ");
|
|
48
|
+
rl.close();
|
|
49
|
+
if (!apiKey.trim()) {
|
|
50
|
+
console.error("Error: API key cannot be empty");
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
const res = await fetch(`${API_BASE}/me`, {
|
|
54
|
+
headers: { Authorization: `Bearer ${apiKey.trim()}` }
|
|
55
|
+
});
|
|
56
|
+
if (!res.ok) {
|
|
57
|
+
console.error("Error: Invalid API key");
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
const { user, team } = await res.json();
|
|
61
|
+
saveApiKey(apiKey.trim());
|
|
62
|
+
console.log(
|
|
63
|
+
`
|
|
64
|
+
\u2713 Authenticated as ${user.displayName || user.email} (team: ${team.name})`
|
|
65
|
+
);
|
|
66
|
+
console.log(" Key saved to ~/.an/credentials\n");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/bundler.ts
|
|
70
|
+
import esbuild from "esbuild";
|
|
71
|
+
var ENTRY_CANDIDATES = [
|
|
72
|
+
"src/agent.ts",
|
|
73
|
+
"src/index.ts",
|
|
74
|
+
"agent.ts",
|
|
75
|
+
"index.ts"
|
|
76
|
+
];
|
|
77
|
+
async function findEntryPoint() {
|
|
78
|
+
const { existsSync } = await import("fs");
|
|
79
|
+
for (const candidate of ENTRY_CANDIDATES) {
|
|
80
|
+
if (existsSync(candidate)) return candidate;
|
|
81
|
+
}
|
|
82
|
+
throw new Error(
|
|
83
|
+
"Cannot find agent entry point. Expected one of:\n" + ENTRY_CANDIDATES.map((c) => ` ${c}`).join("\n")
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
async function bundleAgent(entryPoint) {
|
|
87
|
+
const result = await esbuild.build({
|
|
88
|
+
entryPoints: [entryPoint],
|
|
89
|
+
bundle: true,
|
|
90
|
+
platform: "node",
|
|
91
|
+
target: "node22",
|
|
92
|
+
format: "esm",
|
|
93
|
+
write: false,
|
|
94
|
+
external: ["@an-sdk/agent"],
|
|
95
|
+
minify: true,
|
|
96
|
+
sourcemap: false
|
|
97
|
+
});
|
|
98
|
+
if (result.errors.length > 0) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Bundle failed:
|
|
101
|
+
${result.errors.map((e) => e.text).join("\n")}`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
return Buffer.from(result.outputFiles[0].contents);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/deploy.ts
|
|
108
|
+
import { basename } from "path";
|
|
109
|
+
var API_BASE2 = process.env.AN_API_URL || "https://an.dev/api/v1";
|
|
110
|
+
async function deploy() {
|
|
111
|
+
const apiKey = getApiKey();
|
|
112
|
+
if (!apiKey) {
|
|
113
|
+
console.error("Not logged in. Run `an login` first.");
|
|
114
|
+
process.exit(1);
|
|
115
|
+
}
|
|
116
|
+
const entryPoint = await findEntryPoint();
|
|
117
|
+
console.log(` Bundling ${entryPoint}...`);
|
|
118
|
+
const bundle = await bundleAgent(entryPoint);
|
|
119
|
+
console.log(` Bundled (${(bundle.length / 1024).toFixed(1)}kb)`);
|
|
120
|
+
const project = getProject();
|
|
121
|
+
const slug = project?.slug || basename(process.cwd());
|
|
122
|
+
console.log(` Deploying ${slug}...`);
|
|
123
|
+
const res = await fetch(`${API_BASE2}/agents/deploy`, {
|
|
124
|
+
method: "POST",
|
|
125
|
+
headers: {
|
|
126
|
+
Authorization: `Bearer ${apiKey}`,
|
|
127
|
+
"Content-Type": "application/json"
|
|
128
|
+
},
|
|
129
|
+
body: JSON.stringify({
|
|
130
|
+
slug,
|
|
131
|
+
bundle: bundle.toString("base64")
|
|
132
|
+
})
|
|
133
|
+
});
|
|
134
|
+
if (!res.ok) {
|
|
135
|
+
const err = await res.json().catch(() => ({}));
|
|
136
|
+
console.error(`
|
|
137
|
+
Error: ${err.message || "Deploy failed"}`);
|
|
138
|
+
process.exit(1);
|
|
139
|
+
}
|
|
140
|
+
const result = await res.json();
|
|
141
|
+
saveProject({ agentId: result.agentId, slug: result.slug });
|
|
142
|
+
console.log(`
|
|
143
|
+
\u2713 ${result.url}
|
|
144
|
+
`);
|
|
145
|
+
console.log(` Agent: ${result.slug}`);
|
|
146
|
+
console.log(` Sandbox: ${result.sandboxId}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/index.ts
|
|
150
|
+
var command = process.argv[2];
|
|
151
|
+
if (command === "login") {
|
|
152
|
+
await login();
|
|
153
|
+
} else if (command === "deploy") {
|
|
154
|
+
await deploy();
|
|
155
|
+
} else {
|
|
156
|
+
console.log("AN CLI \u2014 deploy AI agents\n");
|
|
157
|
+
console.log("Commands:");
|
|
158
|
+
console.log(" an login Authenticate with AN platform");
|
|
159
|
+
console.log(" an deploy Bundle and deploy your agent");
|
|
160
|
+
console.log("\nGet started: an login");
|
|
161
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@an-sdk/cli",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "AN CLI — deploy AI agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"an": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsup",
|
|
14
|
+
"dev": "tsx src/index.ts"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"esbuild": "^0.24.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^22.10.2",
|
|
21
|
+
"tsup": "^8.0.0",
|
|
22
|
+
"tsx": "^4.19.2",
|
|
23
|
+
"typescript": "^5.7.2"
|
|
24
|
+
}
|
|
25
|
+
}
|