@answerloops/agent-sdk 0.5.0 → 0.6.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 +26 -2
- package/dist/cli/index.cjs +264 -0
- package/dist/cli/index.d.cts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +263 -0
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
# @answerloops/agent-sdk
|
|
2
2
|
|
|
3
|
-
Typed Node/browser client for the [answerLoops Agent API](https://answerloops.com/docs/integrations/agent-api) — search the knowledge base, read the FAQ digest, list/create tickets, and generate grounded answers.
|
|
3
|
+
Typed Node/browser client for the [answerLoops Agent API](https://answerloops.com/docs/integrations/agent-api) — search the knowledge base, read the FAQ digest, list/create tickets, and generate grounded answers. Also ships a CLI (`answerloops`) that bootstraps a self-hosted instance or installs the [Claude Code agent skills](https://answerloops.com/docs/integrations/agent-skills).
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Getting started
|
|
6
|
+
|
|
7
|
+
Don't have a running answerLoops instance yet? Bootstrap one:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx @answerloops/agent-sdk setup
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Clones the repo if needed, checks Docker + git are present, generates
|
|
14
|
+
`AUTH_SECRET`/`ENCRYPTION_KEY`, starts the published image via Docker
|
|
15
|
+
Compose, and polls `/api/health` until it's actually up. It never invents a
|
|
16
|
+
real credential — if `DATABASE_URL`, `AUTH_URL`, or the Google OAuth pair are
|
|
17
|
+
missing, it tells you exactly what to add to `.env` and exits, rather than
|
|
18
|
+
guessing.
|
|
19
|
+
|
|
20
|
+
Already have a workspace (hosted or self-hosted) and just want an agent
|
|
21
|
+
connected to it? Install the Claude Code skills instead:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx @answerloops/agent-sdk skills answerloops-setup answerloops-operate
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Writes both into `.claude/skills/` — install just one by naming it alone.
|
|
28
|
+
|
|
29
|
+
Building against the API directly instead? Install the library:
|
|
6
30
|
|
|
7
31
|
```bash
|
|
8
32
|
npm install @answerloops/agent-sdk
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
// src/cli/index.ts
|
|
5
|
+
var import_node_util = require("util");
|
|
6
|
+
|
|
7
|
+
// src/cli/setup.ts
|
|
8
|
+
var import_node_fs4 = require("fs");
|
|
9
|
+
var import_node_path4 = require("path");
|
|
10
|
+
|
|
11
|
+
// src/cli/util.ts
|
|
12
|
+
var import_node_child_process = require("child_process");
|
|
13
|
+
var import_node_fs = require("fs");
|
|
14
|
+
function log(msg) {
|
|
15
|
+
console.log(msg);
|
|
16
|
+
}
|
|
17
|
+
function fail(msg) {
|
|
18
|
+
console.error(`\u2717 ${msg}`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
function run(cmd, args, opts = {}) {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
const child = (0, import_node_child_process.spawn)(cmd, args, {
|
|
24
|
+
cwd: opts.cwd,
|
|
25
|
+
env: opts.env ?? process.env,
|
|
26
|
+
stdio: "inherit"
|
|
27
|
+
});
|
|
28
|
+
child.on("error", reject);
|
|
29
|
+
child.on("exit", (code) => resolve(code ?? 1));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function commandExists(cmd, args) {
|
|
33
|
+
return new Promise((resolve) => {
|
|
34
|
+
const child = (0, import_node_child_process.spawn)(cmd, args, { stdio: "ignore" });
|
|
35
|
+
child.on("error", () => resolve(false));
|
|
36
|
+
child.on("exit", (code) => resolve(code === 0));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function fileExists(path) {
|
|
40
|
+
return (0, import_node_fs.existsSync)(path);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/cli/prereqs.ts
|
|
44
|
+
async function checkPrereqs() {
|
|
45
|
+
const missing = [];
|
|
46
|
+
if (!await commandExists("git", ["--version"])) {
|
|
47
|
+
missing.push("git \u2014 https://git-scm.com/downloads");
|
|
48
|
+
}
|
|
49
|
+
if (!await commandExists("docker", ["--version"])) {
|
|
50
|
+
missing.push("Docker \u2014 https://docs.docker.com/get-docker/");
|
|
51
|
+
} else if (!await commandExists("docker", ["compose", "version"])) {
|
|
52
|
+
missing.push("Docker Compose v2 \u2014 update Docker Desktop or install the compose plugin");
|
|
53
|
+
}
|
|
54
|
+
if (missing.length > 0) {
|
|
55
|
+
for (const m of missing) console.error(`\u2717 ${m}`);
|
|
56
|
+
fail("Missing prerequisites. Install the above, then re-run.");
|
|
57
|
+
}
|
|
58
|
+
log("\u2713 git and Docker (with Compose v2) found");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/cli/repo.ts
|
|
62
|
+
var import_node_path = require("path");
|
|
63
|
+
var REPO_URL = "https://github.com/answerLoops/answerLoops.git";
|
|
64
|
+
var MARKER = "docker-compose.ghcr.yml";
|
|
65
|
+
async function ensureRepo(targetDir) {
|
|
66
|
+
if (fileExists((0, import_node_path.join)(process.cwd(), MARKER))) {
|
|
67
|
+
return process.cwd();
|
|
68
|
+
}
|
|
69
|
+
if (fileExists((0, import_node_path.join)(targetDir, MARKER))) {
|
|
70
|
+
log(`\u2713 using existing checkout at ${targetDir}`);
|
|
71
|
+
return targetDir;
|
|
72
|
+
}
|
|
73
|
+
log(`Cloning ${REPO_URL} into ${targetDir}...`);
|
|
74
|
+
const code = await run("git", ["clone", REPO_URL, targetDir]);
|
|
75
|
+
if (code !== 0) fail(`git clone failed (exit ${code})`);
|
|
76
|
+
return targetDir;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/cli/env.ts
|
|
80
|
+
var import_node_crypto = require("crypto");
|
|
81
|
+
var import_node_fs2 = require("fs");
|
|
82
|
+
var import_node_path2 = require("path");
|
|
83
|
+
var REQUIRED_VARS = ["DATABASE_URL", "AUTH_URL", "AUTH_GOOGLE_ID", "AUTH_GOOGLE_SECRET"];
|
|
84
|
+
var HOW_TO_GET = {
|
|
85
|
+
DATABASE_URL: "a Postgres connection string you run and control, e.g. postgresql://user:pass@host:5432/db",
|
|
86
|
+
AUTH_URL: "the public URL this instance will be reachable at, e.g. http://localhost:3000",
|
|
87
|
+
AUTH_GOOGLE_ID: "a Google OAuth client ID \u2014 console.cloud.google.com, register callback {AUTH_URL}/api/auth/callback/google",
|
|
88
|
+
AUTH_GOOGLE_SECRET: "the matching Google OAuth client secret from the same client"
|
|
89
|
+
};
|
|
90
|
+
function parseEnvFile(path) {
|
|
91
|
+
if (!(0, import_node_fs2.existsSync)(path)) return {};
|
|
92
|
+
const out = {};
|
|
93
|
+
for (const line of (0, import_node_fs2.readFileSync)(path, "utf8").split("\n")) {
|
|
94
|
+
const trimmed = line.trim();
|
|
95
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
96
|
+
const eq = trimmed.indexOf("=");
|
|
97
|
+
if (eq === -1) continue;
|
|
98
|
+
out[trimmed.slice(0, eq).trim()] = trimmed.slice(eq + 1).trim();
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
function genSecret() {
|
|
103
|
+
return (0, import_node_crypto.randomBytes)(32).toString("hex");
|
|
104
|
+
}
|
|
105
|
+
function ensureEnv(repoDir) {
|
|
106
|
+
const envPath = (0, import_node_path2.join)(repoDir, ".env");
|
|
107
|
+
const existing = parseEnvFile(envPath);
|
|
108
|
+
const toAppend = [];
|
|
109
|
+
for (const secretVar of ["AUTH_SECRET", "ENCRYPTION_KEY"]) {
|
|
110
|
+
if (!existing[secretVar] && !process.env[secretVar]) {
|
|
111
|
+
const value = genSecret();
|
|
112
|
+
toAppend.push(`${secretVar}=${value}`);
|
|
113
|
+
log(`\u2713 generated ${secretVar}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (toAppend.length > 0) {
|
|
117
|
+
(0, import_node_fs2.appendFileSync)(envPath, ((0, import_node_fs2.existsSync)(envPath) ? "\n" : "") + toAppend.join("\n") + "\n");
|
|
118
|
+
} else if (!(0, import_node_fs2.existsSync)(envPath)) {
|
|
119
|
+
(0, import_node_fs2.writeFileSync)(envPath, "");
|
|
120
|
+
}
|
|
121
|
+
const missing = REQUIRED_VARS.filter((v) => !existing[v] && !process.env[v]);
|
|
122
|
+
if (missing.length > 0) {
|
|
123
|
+
console.error(`Missing required configuration in ${envPath}:`);
|
|
124
|
+
for (const v of missing) console.error(` ${v} \u2014 ${HOW_TO_GET[v]}`);
|
|
125
|
+
console.error(
|
|
126
|
+
`
|
|
127
|
+
Add these to ${envPath} (or export them before running this command), then re-run.`
|
|
128
|
+
);
|
|
129
|
+
fail("Required environment variables not set.");
|
|
130
|
+
}
|
|
131
|
+
log(`\u2713 ${envPath} has everything required`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/cli/health.ts
|
|
135
|
+
async function waitForHealth(url, timeoutSeconds = 120) {
|
|
136
|
+
const intervalMs = 3e3;
|
|
137
|
+
const deadline = Date.now() + timeoutSeconds * 1e3;
|
|
138
|
+
log(`Waiting for ${url} to become healthy (timeout ${timeoutSeconds}s)...`);
|
|
139
|
+
while (Date.now() < deadline) {
|
|
140
|
+
try {
|
|
141
|
+
const res = await fetch(url);
|
|
142
|
+
if (res.ok) {
|
|
143
|
+
log(`\u2713 healthy`);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
} catch {
|
|
147
|
+
}
|
|
148
|
+
await new Promise((r) => setTimeout(r, intervalMs));
|
|
149
|
+
}
|
|
150
|
+
fail(
|
|
151
|
+
`${url} still unhealthy after ${timeoutSeconds}s. Check logs: docker compose -f docker-compose.ghcr.yml logs app -f`
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/cli/skills.ts
|
|
156
|
+
var import_node_fs3 = require("fs");
|
|
157
|
+
var import_node_path3 = require("path");
|
|
158
|
+
var RAW_BASE = "https://raw.githubusercontent.com/answerLoops/answerLoops/main";
|
|
159
|
+
var SKILLS = {
|
|
160
|
+
"answerloops-setup": ["skills/setup/SKILL.md"],
|
|
161
|
+
"answerloops-operate": ["skills/operate/SKILL.md"]
|
|
162
|
+
};
|
|
163
|
+
async function installSkills(names) {
|
|
164
|
+
for (const name of names) {
|
|
165
|
+
const files = SKILLS[name];
|
|
166
|
+
if (!files) fail(`Unknown skill "${name}". Known: ${Object.keys(SKILLS).join(", ")}`);
|
|
167
|
+
const destDir = (0, import_node_path3.join)(".claude", "skills", name);
|
|
168
|
+
(0, import_node_fs3.mkdirSync)(destDir, { recursive: true });
|
|
169
|
+
for (const relPath of files) {
|
|
170
|
+
const url = `${RAW_BASE}/${relPath}`;
|
|
171
|
+
const res = await fetch(url);
|
|
172
|
+
if (!res.ok) fail(`Could not fetch ${url} (${res.status})`);
|
|
173
|
+
const body = await res.text();
|
|
174
|
+
const destPath = (0, import_node_path3.join)(destDir, relPath.split("/").pop());
|
|
175
|
+
(0, import_node_fs3.writeFileSync)(destPath, body);
|
|
176
|
+
}
|
|
177
|
+
log(`\u2713 installed ${name} -> ${destDir}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// src/cli/setup.ts
|
|
182
|
+
async function runSetup(opts) {
|
|
183
|
+
await checkPrereqs();
|
|
184
|
+
const repoDir = await ensureRepo(opts.targetDir);
|
|
185
|
+
ensureEnv(repoDir);
|
|
186
|
+
log("Starting the stack (docker compose -f docker-compose.ghcr.yml up -d)...");
|
|
187
|
+
const code = await run("docker", ["compose", "-f", "docker-compose.ghcr.yml", "up", "-d"], {
|
|
188
|
+
cwd: repoDir
|
|
189
|
+
});
|
|
190
|
+
if (code !== 0) fail(`docker compose up failed (exit ${code})`);
|
|
191
|
+
const authUrl = readAuthUrl(repoDir) ?? "http://localhost:3000";
|
|
192
|
+
await waitForHealth(`${authUrl.replace(/\/+$/, "")}/api/health`);
|
|
193
|
+
if (opts.withSkills) {
|
|
194
|
+
await installSkills(["answerloops-setup", "answerloops-operate"]);
|
|
195
|
+
}
|
|
196
|
+
log(`
|
|
197
|
+
Your instance is live at ${authUrl}. Sign in with Google and complete onboarding.`);
|
|
198
|
+
}
|
|
199
|
+
function readAuthUrl(repoDir) {
|
|
200
|
+
try {
|
|
201
|
+
const text = (0, import_node_fs4.readFileSync)((0, import_node_path4.join)(repoDir, ".env"), "utf8");
|
|
202
|
+
const line = text.split("\n").find((l) => l.trim().startsWith("AUTH_URL="));
|
|
203
|
+
return line?.slice(line.indexOf("=") + 1).trim();
|
|
204
|
+
} catch {
|
|
205
|
+
return void 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/cli/index.ts
|
|
210
|
+
var USAGE = `Usage: answerloops <command> [options]
|
|
211
|
+
|
|
212
|
+
Commands:
|
|
213
|
+
setup Bootstrap a self-hosted answerLoops instance (Docker,
|
|
214
|
+
the published image). Requires Docker + git.
|
|
215
|
+
--dir <path> Directory to clone into if not already in a
|
|
216
|
+
checkout (default: ./answerLoops)
|
|
217
|
+
--with-skills Also install the Claude Code setup + operate
|
|
218
|
+
skills into ./.claude/skills/
|
|
219
|
+
|
|
220
|
+
skills <name...> Install one or more Claude Code skills into
|
|
221
|
+
./.claude/skills/. Names: answerloops-setup,
|
|
222
|
+
answerloops-operate
|
|
223
|
+
|
|
224
|
+
help Show this message
|
|
225
|
+
`;
|
|
226
|
+
async function main() {
|
|
227
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
228
|
+
switch (command) {
|
|
229
|
+
case "setup": {
|
|
230
|
+
const { values } = (0, import_node_util.parseArgs)({
|
|
231
|
+
args: rest,
|
|
232
|
+
options: {
|
|
233
|
+
dir: { type: "string", default: "answerLoops" },
|
|
234
|
+
"with-skills": { type: "boolean", default: false }
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
await runSetup({ targetDir: values.dir, withSkills: values["with-skills"] });
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
case "skills": {
|
|
241
|
+
if (rest.length === 0) {
|
|
242
|
+
console.error("Usage: answerloops skills <name...>");
|
|
243
|
+
process.exit(1);
|
|
244
|
+
}
|
|
245
|
+
await installSkills(rest);
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
case "help":
|
|
249
|
+
case void 0:
|
|
250
|
+
case "--help":
|
|
251
|
+
case "-h":
|
|
252
|
+
console.log(USAGE);
|
|
253
|
+
break;
|
|
254
|
+
default:
|
|
255
|
+
console.error(`Unknown command "${command}"
|
|
256
|
+
`);
|
|
257
|
+
console.log(USAGE);
|
|
258
|
+
process.exit(1);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
main().catch((err) => {
|
|
262
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
263
|
+
process.exit(1);
|
|
264
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli/index.ts
|
|
4
|
+
import { parseArgs } from "util";
|
|
5
|
+
|
|
6
|
+
// src/cli/setup.ts
|
|
7
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
8
|
+
import { join as join4 } from "path";
|
|
9
|
+
|
|
10
|
+
// src/cli/util.ts
|
|
11
|
+
import { spawn } from "child_process";
|
|
12
|
+
import { existsSync } from "fs";
|
|
13
|
+
function log(msg) {
|
|
14
|
+
console.log(msg);
|
|
15
|
+
}
|
|
16
|
+
function fail(msg) {
|
|
17
|
+
console.error(`\u2717 ${msg}`);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
function run(cmd, args, opts = {}) {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const child = spawn(cmd, args, {
|
|
23
|
+
cwd: opts.cwd,
|
|
24
|
+
env: opts.env ?? process.env,
|
|
25
|
+
stdio: "inherit"
|
|
26
|
+
});
|
|
27
|
+
child.on("error", reject);
|
|
28
|
+
child.on("exit", (code) => resolve(code ?? 1));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function commandExists(cmd, args) {
|
|
32
|
+
return new Promise((resolve) => {
|
|
33
|
+
const child = spawn(cmd, args, { stdio: "ignore" });
|
|
34
|
+
child.on("error", () => resolve(false));
|
|
35
|
+
child.on("exit", (code) => resolve(code === 0));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function fileExists(path) {
|
|
39
|
+
return existsSync(path);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/cli/prereqs.ts
|
|
43
|
+
async function checkPrereqs() {
|
|
44
|
+
const missing = [];
|
|
45
|
+
if (!await commandExists("git", ["--version"])) {
|
|
46
|
+
missing.push("git \u2014 https://git-scm.com/downloads");
|
|
47
|
+
}
|
|
48
|
+
if (!await commandExists("docker", ["--version"])) {
|
|
49
|
+
missing.push("Docker \u2014 https://docs.docker.com/get-docker/");
|
|
50
|
+
} else if (!await commandExists("docker", ["compose", "version"])) {
|
|
51
|
+
missing.push("Docker Compose v2 \u2014 update Docker Desktop or install the compose plugin");
|
|
52
|
+
}
|
|
53
|
+
if (missing.length > 0) {
|
|
54
|
+
for (const m of missing) console.error(`\u2717 ${m}`);
|
|
55
|
+
fail("Missing prerequisites. Install the above, then re-run.");
|
|
56
|
+
}
|
|
57
|
+
log("\u2713 git and Docker (with Compose v2) found");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/cli/repo.ts
|
|
61
|
+
import { join } from "path";
|
|
62
|
+
var REPO_URL = "https://github.com/answerLoops/answerLoops.git";
|
|
63
|
+
var MARKER = "docker-compose.ghcr.yml";
|
|
64
|
+
async function ensureRepo(targetDir) {
|
|
65
|
+
if (fileExists(join(process.cwd(), MARKER))) {
|
|
66
|
+
return process.cwd();
|
|
67
|
+
}
|
|
68
|
+
if (fileExists(join(targetDir, MARKER))) {
|
|
69
|
+
log(`\u2713 using existing checkout at ${targetDir}`);
|
|
70
|
+
return targetDir;
|
|
71
|
+
}
|
|
72
|
+
log(`Cloning ${REPO_URL} into ${targetDir}...`);
|
|
73
|
+
const code = await run("git", ["clone", REPO_URL, targetDir]);
|
|
74
|
+
if (code !== 0) fail(`git clone failed (exit ${code})`);
|
|
75
|
+
return targetDir;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/cli/env.ts
|
|
79
|
+
import { randomBytes } from "crypto";
|
|
80
|
+
import { existsSync as existsSync2, readFileSync, appendFileSync, writeFileSync } from "fs";
|
|
81
|
+
import { join as join2 } from "path";
|
|
82
|
+
var REQUIRED_VARS = ["DATABASE_URL", "AUTH_URL", "AUTH_GOOGLE_ID", "AUTH_GOOGLE_SECRET"];
|
|
83
|
+
var HOW_TO_GET = {
|
|
84
|
+
DATABASE_URL: "a Postgres connection string you run and control, e.g. postgresql://user:pass@host:5432/db",
|
|
85
|
+
AUTH_URL: "the public URL this instance will be reachable at, e.g. http://localhost:3000",
|
|
86
|
+
AUTH_GOOGLE_ID: "a Google OAuth client ID \u2014 console.cloud.google.com, register callback {AUTH_URL}/api/auth/callback/google",
|
|
87
|
+
AUTH_GOOGLE_SECRET: "the matching Google OAuth client secret from the same client"
|
|
88
|
+
};
|
|
89
|
+
function parseEnvFile(path) {
|
|
90
|
+
if (!existsSync2(path)) return {};
|
|
91
|
+
const out = {};
|
|
92
|
+
for (const line of readFileSync(path, "utf8").split("\n")) {
|
|
93
|
+
const trimmed = line.trim();
|
|
94
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
95
|
+
const eq = trimmed.indexOf("=");
|
|
96
|
+
if (eq === -1) continue;
|
|
97
|
+
out[trimmed.slice(0, eq).trim()] = trimmed.slice(eq + 1).trim();
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
function genSecret() {
|
|
102
|
+
return randomBytes(32).toString("hex");
|
|
103
|
+
}
|
|
104
|
+
function ensureEnv(repoDir) {
|
|
105
|
+
const envPath = join2(repoDir, ".env");
|
|
106
|
+
const existing = parseEnvFile(envPath);
|
|
107
|
+
const toAppend = [];
|
|
108
|
+
for (const secretVar of ["AUTH_SECRET", "ENCRYPTION_KEY"]) {
|
|
109
|
+
if (!existing[secretVar] && !process.env[secretVar]) {
|
|
110
|
+
const value = genSecret();
|
|
111
|
+
toAppend.push(`${secretVar}=${value}`);
|
|
112
|
+
log(`\u2713 generated ${secretVar}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (toAppend.length > 0) {
|
|
116
|
+
appendFileSync(envPath, (existsSync2(envPath) ? "\n" : "") + toAppend.join("\n") + "\n");
|
|
117
|
+
} else if (!existsSync2(envPath)) {
|
|
118
|
+
writeFileSync(envPath, "");
|
|
119
|
+
}
|
|
120
|
+
const missing = REQUIRED_VARS.filter((v) => !existing[v] && !process.env[v]);
|
|
121
|
+
if (missing.length > 0) {
|
|
122
|
+
console.error(`Missing required configuration in ${envPath}:`);
|
|
123
|
+
for (const v of missing) console.error(` ${v} \u2014 ${HOW_TO_GET[v]}`);
|
|
124
|
+
console.error(
|
|
125
|
+
`
|
|
126
|
+
Add these to ${envPath} (or export them before running this command), then re-run.`
|
|
127
|
+
);
|
|
128
|
+
fail("Required environment variables not set.");
|
|
129
|
+
}
|
|
130
|
+
log(`\u2713 ${envPath} has everything required`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/cli/health.ts
|
|
134
|
+
async function waitForHealth(url, timeoutSeconds = 120) {
|
|
135
|
+
const intervalMs = 3e3;
|
|
136
|
+
const deadline = Date.now() + timeoutSeconds * 1e3;
|
|
137
|
+
log(`Waiting for ${url} to become healthy (timeout ${timeoutSeconds}s)...`);
|
|
138
|
+
while (Date.now() < deadline) {
|
|
139
|
+
try {
|
|
140
|
+
const res = await fetch(url);
|
|
141
|
+
if (res.ok) {
|
|
142
|
+
log(`\u2713 healthy`);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
} catch {
|
|
146
|
+
}
|
|
147
|
+
await new Promise((r) => setTimeout(r, intervalMs));
|
|
148
|
+
}
|
|
149
|
+
fail(
|
|
150
|
+
`${url} still unhealthy after ${timeoutSeconds}s. Check logs: docker compose -f docker-compose.ghcr.yml logs app -f`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/cli/skills.ts
|
|
155
|
+
import { mkdirSync, writeFileSync as writeFileSync2 } from "fs";
|
|
156
|
+
import { join as join3 } from "path";
|
|
157
|
+
var RAW_BASE = "https://raw.githubusercontent.com/answerLoops/answerLoops/main";
|
|
158
|
+
var SKILLS = {
|
|
159
|
+
"answerloops-setup": ["skills/setup/SKILL.md"],
|
|
160
|
+
"answerloops-operate": ["skills/operate/SKILL.md"]
|
|
161
|
+
};
|
|
162
|
+
async function installSkills(names) {
|
|
163
|
+
for (const name of names) {
|
|
164
|
+
const files = SKILLS[name];
|
|
165
|
+
if (!files) fail(`Unknown skill "${name}". Known: ${Object.keys(SKILLS).join(", ")}`);
|
|
166
|
+
const destDir = join3(".claude", "skills", name);
|
|
167
|
+
mkdirSync(destDir, { recursive: true });
|
|
168
|
+
for (const relPath of files) {
|
|
169
|
+
const url = `${RAW_BASE}/${relPath}`;
|
|
170
|
+
const res = await fetch(url);
|
|
171
|
+
if (!res.ok) fail(`Could not fetch ${url} (${res.status})`);
|
|
172
|
+
const body = await res.text();
|
|
173
|
+
const destPath = join3(destDir, relPath.split("/").pop());
|
|
174
|
+
writeFileSync2(destPath, body);
|
|
175
|
+
}
|
|
176
|
+
log(`\u2713 installed ${name} -> ${destDir}`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/cli/setup.ts
|
|
181
|
+
async function runSetup(opts) {
|
|
182
|
+
await checkPrereqs();
|
|
183
|
+
const repoDir = await ensureRepo(opts.targetDir);
|
|
184
|
+
ensureEnv(repoDir);
|
|
185
|
+
log("Starting the stack (docker compose -f docker-compose.ghcr.yml up -d)...");
|
|
186
|
+
const code = await run("docker", ["compose", "-f", "docker-compose.ghcr.yml", "up", "-d"], {
|
|
187
|
+
cwd: repoDir
|
|
188
|
+
});
|
|
189
|
+
if (code !== 0) fail(`docker compose up failed (exit ${code})`);
|
|
190
|
+
const authUrl = readAuthUrl(repoDir) ?? "http://localhost:3000";
|
|
191
|
+
await waitForHealth(`${authUrl.replace(/\/+$/, "")}/api/health`);
|
|
192
|
+
if (opts.withSkills) {
|
|
193
|
+
await installSkills(["answerloops-setup", "answerloops-operate"]);
|
|
194
|
+
}
|
|
195
|
+
log(`
|
|
196
|
+
Your instance is live at ${authUrl}. Sign in with Google and complete onboarding.`);
|
|
197
|
+
}
|
|
198
|
+
function readAuthUrl(repoDir) {
|
|
199
|
+
try {
|
|
200
|
+
const text = readFileSync2(join4(repoDir, ".env"), "utf8");
|
|
201
|
+
const line = text.split("\n").find((l) => l.trim().startsWith("AUTH_URL="));
|
|
202
|
+
return line?.slice(line.indexOf("=") + 1).trim();
|
|
203
|
+
} catch {
|
|
204
|
+
return void 0;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/cli/index.ts
|
|
209
|
+
var USAGE = `Usage: answerloops <command> [options]
|
|
210
|
+
|
|
211
|
+
Commands:
|
|
212
|
+
setup Bootstrap a self-hosted answerLoops instance (Docker,
|
|
213
|
+
the published image). Requires Docker + git.
|
|
214
|
+
--dir <path> Directory to clone into if not already in a
|
|
215
|
+
checkout (default: ./answerLoops)
|
|
216
|
+
--with-skills Also install the Claude Code setup + operate
|
|
217
|
+
skills into ./.claude/skills/
|
|
218
|
+
|
|
219
|
+
skills <name...> Install one or more Claude Code skills into
|
|
220
|
+
./.claude/skills/. Names: answerloops-setup,
|
|
221
|
+
answerloops-operate
|
|
222
|
+
|
|
223
|
+
help Show this message
|
|
224
|
+
`;
|
|
225
|
+
async function main() {
|
|
226
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
227
|
+
switch (command) {
|
|
228
|
+
case "setup": {
|
|
229
|
+
const { values } = parseArgs({
|
|
230
|
+
args: rest,
|
|
231
|
+
options: {
|
|
232
|
+
dir: { type: "string", default: "answerLoops" },
|
|
233
|
+
"with-skills": { type: "boolean", default: false }
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
await runSetup({ targetDir: values.dir, withSkills: values["with-skills"] });
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
case "skills": {
|
|
240
|
+
if (rest.length === 0) {
|
|
241
|
+
console.error("Usage: answerloops skills <name...>");
|
|
242
|
+
process.exit(1);
|
|
243
|
+
}
|
|
244
|
+
await installSkills(rest);
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case "help":
|
|
248
|
+
case void 0:
|
|
249
|
+
case "--help":
|
|
250
|
+
case "-h":
|
|
251
|
+
console.log(USAGE);
|
|
252
|
+
break;
|
|
253
|
+
default:
|
|
254
|
+
console.error(`Unknown command "${command}"
|
|
255
|
+
`);
|
|
256
|
+
console.log(USAGE);
|
|
257
|
+
process.exit(1);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
main().catch((err) => {
|
|
261
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
262
|
+
process.exit(1);
|
|
263
|
+
});
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@answerloops/agent-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Typed client for the answerLoops Agent API (knowledge base search, FAQ, tickets, grounded answers).",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Typed client for the answerLoops Agent API (knowledge base search, FAQ, tickets, grounded answers) and a CLI to self-host answerLoops or install its Claude Code skills.",
|
|
5
5
|
"license": "AGPL-3.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.cjs",
|
|
8
8
|
"module": "./dist/index.js",
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
|
+
"bin": {
|
|
11
|
+
"answerloops": "./dist/cli/index.js"
|
|
12
|
+
},
|
|
10
13
|
"exports": {
|
|
11
14
|
".": {
|
|
12
15
|
"types": "./dist/index.d.ts",
|
|
@@ -38,7 +41,7 @@
|
|
|
38
41
|
"knowledge-base"
|
|
39
42
|
],
|
|
40
43
|
"scripts": {
|
|
41
|
-
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
44
|
+
"build": "tsup src/index.ts src/cli/index.ts --format esm,cjs --dts --clean && chmod +x dist/cli/index.js",
|
|
42
45
|
"typecheck": "tsc --noEmit"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|