@synapsor/client 0.1.1 → 0.1.2
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/bin/synapsor.mjs +294 -0
- package/generated-contract.mjs +1 -1
- package/package.json +9 -4
package/bin/synapsor.mjs
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { createInterface } from "node:readline/promises";
|
|
8
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
9
|
+
|
|
10
|
+
const PACKAGE_VERSION = "0.1.2";
|
|
11
|
+
const DEFAULT_BASE_URL = "https://synapsor.ai";
|
|
12
|
+
const ERROR_HINTS = new Map([
|
|
13
|
+
["AUTH_REQUIRED", "Set SYNAPSOR_API_KEY or run `synapsor config set api-key`."],
|
|
14
|
+
["BILLING_REQUIRED", "Builder resources require active or trialing Builder billing."],
|
|
15
|
+
["LIMIT_EXCEEDED", "You reached a controlled-beta hard limit. Upgrade or reduce usage."],
|
|
16
|
+
["FORBIDDEN", "The API key is not allowed to perform this operation."],
|
|
17
|
+
["NOT_FOUND", "Check the project, database, handle, or run id."],
|
|
18
|
+
["BETA_UNAVAILABLE", "This feature is not self-serve in the controlled beta."],
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
function configPath() {
|
|
22
|
+
const override = process.env.SYNAPSOR_CLI_CONFIG_HOME;
|
|
23
|
+
return join(override || join(homedir(), ".config"), "synapsor", "config.json");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function loadConfig(profile = "default") {
|
|
27
|
+
const path = configPath();
|
|
28
|
+
if (!existsSync(path)) return { path, profile, data: {}, current: {} };
|
|
29
|
+
const raw = JSON.parse(await readFile(path, "utf8"));
|
|
30
|
+
const profiles = raw.profiles && typeof raw.profiles === "object" ? raw.profiles : {};
|
|
31
|
+
return { path, profile, data: raw, current: profiles[profile] || {} };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function saveConfig(profile, patch) {
|
|
35
|
+
const existing = await loadConfig(profile);
|
|
36
|
+
const data = existing.data.profiles ? existing.data : { profiles: {} };
|
|
37
|
+
data.profiles[profile] = { ...(data.profiles[profile] || {}), ...patch };
|
|
38
|
+
const path = configPath();
|
|
39
|
+
await mkdir(dirname(path), { recursive: true });
|
|
40
|
+
await writeFile(path, JSON.stringify(data, null, 2) + "\n", { mode: 0o600 });
|
|
41
|
+
try {
|
|
42
|
+
await chmod(path, 0o600);
|
|
43
|
+
} catch {
|
|
44
|
+
// Best effort on platforms without POSIX modes.
|
|
45
|
+
}
|
|
46
|
+
return path;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function parse(argv) {
|
|
50
|
+
const globals = { json: false, profile: "default", project: "", db: "", baseUrl: "" };
|
|
51
|
+
const args = [];
|
|
52
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
53
|
+
const arg = argv[i];
|
|
54
|
+
if (arg === "--json") {
|
|
55
|
+
const next = argv[i + 1] || "";
|
|
56
|
+
if (/^\s*[\[{]/.test(next)) {
|
|
57
|
+
args.push(arg);
|
|
58
|
+
args.push(argv[++i]);
|
|
59
|
+
} else {
|
|
60
|
+
globals.json = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else if (arg === "--yes" || arg === "-y") globals.yes = true;
|
|
64
|
+
else if (["--profile", "--project", "--db", "--base-url"].includes(arg)) {
|
|
65
|
+
const key = arg === "--base-url" ? "baseUrl" : arg.slice(2);
|
|
66
|
+
globals[key] = argv[++i] || "";
|
|
67
|
+
}
|
|
68
|
+
else args.push(arg);
|
|
69
|
+
}
|
|
70
|
+
return { globals, args };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function redact(value) {
|
|
74
|
+
const text = String(value || "");
|
|
75
|
+
if (!text) return "";
|
|
76
|
+
return `${text.slice(0, 8)}...redacted`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function print(payload, globals) {
|
|
80
|
+
if (globals.json) {
|
|
81
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (Array.isArray(payload)) {
|
|
85
|
+
for (const item of payload) console.log(formatLine(item));
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (payload && typeof payload === "object") {
|
|
89
|
+
console.log(formatLine(payload));
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
console.log(String(payload));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function formatLine(item) {
|
|
96
|
+
if (!item || typeof item !== "object") return String(item);
|
|
97
|
+
const preferred = ["project_id", "database_id", "name", "plan", "status", "billing_status", "runtime_branch", "key_id"];
|
|
98
|
+
const parts = [];
|
|
99
|
+
for (const key of preferred) {
|
|
100
|
+
if (item[key] !== undefined && item[key] !== null && item[key] !== "") parts.push(`${key}=${item[key]}`);
|
|
101
|
+
}
|
|
102
|
+
return parts.length ? parts.join(" ") : JSON.stringify(item);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function requireApiKey(config) {
|
|
106
|
+
const key = process.env.SYNAPSOR_API_KEY || config.current.apiKey || "";
|
|
107
|
+
if (!key) {
|
|
108
|
+
const error = new Error("AUTH_REQUIRED: configure an API key from the Synapsor dashboard. Public demo: https://synapsor.ai/demo");
|
|
109
|
+
error.code = "AUTH_REQUIRED";
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
return key;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function client(globals) {
|
|
116
|
+
const config = await loadConfig(globals.profile);
|
|
117
|
+
const baseUrl = (globals.baseUrl || process.env.SYNAPSOR_BASE_URL || config.current.baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
118
|
+
const apiKey = await requireApiKey(config);
|
|
119
|
+
return { baseUrl, apiKey };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function request(globals, method, path, body) {
|
|
123
|
+
const { baseUrl, apiKey } = await client(globals);
|
|
124
|
+
const init = { method, headers: { accept: "application/json", authorization: `Bearer ${apiKey}` } };
|
|
125
|
+
if (body !== undefined) {
|
|
126
|
+
init.headers["content-type"] = "application/json";
|
|
127
|
+
init.body = JSON.stringify(body);
|
|
128
|
+
}
|
|
129
|
+
const response = await fetch(`${baseUrl}${path}`, init);
|
|
130
|
+
const text = await response.text();
|
|
131
|
+
let payload = {};
|
|
132
|
+
if (text.trim()) {
|
|
133
|
+
try {
|
|
134
|
+
payload = JSON.parse(text);
|
|
135
|
+
} catch {
|
|
136
|
+
payload = { ok: false, error: "INVALID_JSON_RESPONSE", body_preview: text.slice(0, 240) };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (!response.ok || payload.ok === false) {
|
|
140
|
+
const errorCode = mapError(payload.error || response.status);
|
|
141
|
+
const error = new Error(`${errorCode}: ${payload.message || payload.error || `HTTP ${response.status}`}`);
|
|
142
|
+
error.code = errorCode;
|
|
143
|
+
error.payload = payload;
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
return payload;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function mapError(error) {
|
|
150
|
+
const text = String(error || "").toLowerCase();
|
|
151
|
+
if (text.includes("auth") || text.includes("session") || text.includes("token")) return "AUTH_REQUIRED";
|
|
152
|
+
if (text.includes("payment") || text.includes("billing")) return "BILLING_REQUIRED";
|
|
153
|
+
if (text.includes("quota") || text.includes("limit")) return "LIMIT_EXCEEDED";
|
|
154
|
+
if (text.includes("denied") || text.includes("forbidden")) return "FORBIDDEN";
|
|
155
|
+
if (text.includes("not_found")) return "NOT_FOUND";
|
|
156
|
+
if (text.includes("plan_not_available")) return "BETA_UNAVAILABLE";
|
|
157
|
+
return String(error || "REQUEST_FAILED").toUpperCase();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function confirm(globals, label) {
|
|
161
|
+
if (globals.yes) return;
|
|
162
|
+
const rl = createInterface({ input, output });
|
|
163
|
+
const answer = await rl.question(`${label} Type yes to continue: `);
|
|
164
|
+
rl.close();
|
|
165
|
+
if (answer.trim().toLowerCase() !== "yes") throw new Error("FORBIDDEN: confirmation required");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function readStdinSecret() {
|
|
169
|
+
const rl = createInterface({ input, output });
|
|
170
|
+
const value = await rl.question("Paste Synapsor API key: ");
|
|
171
|
+
rl.close();
|
|
172
|
+
return value.trim();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function main(argv = process.argv.slice(2)) {
|
|
176
|
+
const { globals, args } = parse(argv);
|
|
177
|
+
const [cmd, sub, third, ...rest] = args;
|
|
178
|
+
if (!cmd || cmd === "--help" || cmd === "help") return help();
|
|
179
|
+
if (cmd === "--version" || cmd === "version") return console.log(PACKAGE_VERSION);
|
|
180
|
+
|
|
181
|
+
if (cmd === "config") {
|
|
182
|
+
if (sub === "get") {
|
|
183
|
+
const config = await loadConfig(globals.profile);
|
|
184
|
+
return print({ profile: globals.profile, baseUrl: config.current.baseUrl || DEFAULT_BASE_URL, apiKey: redact(process.env.SYNAPSOR_API_KEY || config.current.apiKey) }, globals);
|
|
185
|
+
}
|
|
186
|
+
if (sub === "set" && third === "base-url") {
|
|
187
|
+
const value = rest[0];
|
|
188
|
+
if (!value) throw new Error("BASE_URL_REQUIRED: usage `synapsor config set base-url https://synapsor.ai`");
|
|
189
|
+
await saveConfig(globals.profile, { baseUrl: value.replace(/\/+$/, "") });
|
|
190
|
+
return print({ ok: true, profile: globals.profile, baseUrl: value.replace(/\/+$/, "") }, globals);
|
|
191
|
+
}
|
|
192
|
+
if (sub === "set" && third === "api-key") {
|
|
193
|
+
const value = rest[0] || await readStdinSecret();
|
|
194
|
+
if (!value) throw new Error("AUTH_REQUIRED: API key required");
|
|
195
|
+
await saveConfig(globals.profile, { apiKey: value });
|
|
196
|
+
return print({ ok: true, profile: globals.profile, apiKey: redact(value) }, globals);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (cmd === "projects") {
|
|
201
|
+
if (sub === "list") {
|
|
202
|
+
const payload = await request(globals, "GET", "/v1/control/projects");
|
|
203
|
+
return print(payload.projects || [], globals);
|
|
204
|
+
}
|
|
205
|
+
if (sub === "create") {
|
|
206
|
+
const name = third;
|
|
207
|
+
if (!name) throw new Error("PROJECT_NAME_REQUIRED: usage `synapsor projects create <name>`");
|
|
208
|
+
return print(await request(globals, "POST", "/v1/control/projects", { name, plan: "free" }), globals);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (cmd === "db") {
|
|
213
|
+
if (sub === "list") {
|
|
214
|
+
const payload = await request(globals, "GET", "/v1/control/databases");
|
|
215
|
+
return print(payload.databases || [], globals);
|
|
216
|
+
}
|
|
217
|
+
if (sub === "create") {
|
|
218
|
+
const name = third;
|
|
219
|
+
if (!name) throw new Error("DATABASE_NAME_REQUIRED: usage `synapsor db create <name> --project <project>`");
|
|
220
|
+
return print(await request(globals, "POST", "/v1/control/databases", { name, project_id: globals.project, plan: "free" }), globals);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (cmd === "sql") {
|
|
225
|
+
const db = globals.db || "";
|
|
226
|
+
let sql = args.slice(1).join(" ").trim();
|
|
227
|
+
const fileIndex = args.indexOf("--file");
|
|
228
|
+
if (fileIndex >= 0) sql = await readFile(args[fileIndex + 1], "utf8");
|
|
229
|
+
if (!db) throw new Error("DATABASE_REQUIRED: use `--db <database_id>`");
|
|
230
|
+
if (!sql) throw new Error("SQL_REQUIRED: pass SQL after --db or use --file");
|
|
231
|
+
return print(await request(globals, "POST", "/v1/control/console/sql", { project_id: globals.project, database_id: db, sql }), globals);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (cmd === "invoke") {
|
|
235
|
+
const capability = sub;
|
|
236
|
+
const jsonIndex = args.indexOf("--json");
|
|
237
|
+
const body = jsonIndex >= 0 ? JSON.parse(args[jsonIndex + 1] || "{}") : {};
|
|
238
|
+
if (!capability) throw new Error("CAPABILITY_REQUIRED: usage `synapsor invoke <capability> --db <database> --json '{...}'`");
|
|
239
|
+
if (!globals.db) throw new Error("DATABASE_REQUIRED: use `--db <database_id>`");
|
|
240
|
+
return print(await request(globals, "POST", "/v1/control/console/invoke", { project_id: globals.project, database_id: globals.db, capability, arguments: body }), globals);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (cmd === "evidence" && sub === "get") {
|
|
244
|
+
if (!third || !globals.db) throw new Error("EVIDENCE_OR_DATABASE_REQUIRED: usage `synapsor evidence get <handle> --db <database>`");
|
|
245
|
+
return print(await request(globals, "POST", "/v1/control/console/evidence", { project_id: globals.project, database_id: globals.db, uri: third }), globals);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (cmd === "proposal" && ["preview", "approve", "reject"].includes(sub)) {
|
|
249
|
+
if (!third || !globals.db) throw new Error("PROPOSAL_OR_DATABASE_REQUIRED: usage `synapsor proposal preview <handle> --db <database>`");
|
|
250
|
+
if (sub !== "preview") await confirm(globals, `Proposal ${sub} is auditable and may change workflow state.`);
|
|
251
|
+
return print(await request(globals, "POST", `/v1/control/console/proposals/${sub}`, { project_id: globals.project, database_id: globals.db, proposal: third }), globals);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (cmd === "replay") {
|
|
255
|
+
if (!sub || !globals.db) throw new Error("RUN_OR_DATABASE_REQUIRED: usage `synapsor replay <run-id> --db <database>`");
|
|
256
|
+
return print(await request(globals, "POST", "/v1/control/console/replay", { project_id: globals.project, database_id: globals.db, run_id: sub }), globals);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
throw new Error(`UNKNOWN_COMMAND: ${args.join(" ")}`);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function help() {
|
|
263
|
+
console.log(`Synapsor CLI ${PACKAGE_VERSION}
|
|
264
|
+
|
|
265
|
+
Usage:
|
|
266
|
+
synapsor --version
|
|
267
|
+
synapsor config set base-url https://synapsor.ai
|
|
268
|
+
synapsor config set api-key
|
|
269
|
+
synapsor config get
|
|
270
|
+
synapsor projects list
|
|
271
|
+
synapsor projects create <name>
|
|
272
|
+
synapsor db list --project <project>
|
|
273
|
+
synapsor db create <name> --project <project>
|
|
274
|
+
synapsor sql --db <database> "SELECT 1;"
|
|
275
|
+
synapsor sql --db <database> --file ./query.sql
|
|
276
|
+
synapsor invoke <capability> --db <database> --json '{"ticket_id":"TICK-1001"}'
|
|
277
|
+
synapsor evidence get <evidence-handle> --db <database>
|
|
278
|
+
synapsor proposal preview <proposal-handle> --db <database>
|
|
279
|
+
synapsor proposal approve <proposal-handle> --db <database> --yes
|
|
280
|
+
synapsor proposal reject <proposal-handle> --db <database> --yes
|
|
281
|
+
synapsor replay <run-id> --db <database>
|
|
282
|
+
|
|
283
|
+
Environment:
|
|
284
|
+
SYNAPSOR_API_KEY, SYNAPSOR_BASE_URL
|
|
285
|
+
|
|
286
|
+
Synapsor CLI runs Synapsor SQL/API commands only. It never executes server shell commands.`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
main().catch((error) => {
|
|
290
|
+
const code = error.code || String(error.message || "REQUEST_FAILED").split(":", 1)[0];
|
|
291
|
+
console.error(error.message || String(error));
|
|
292
|
+
if (ERROR_HINTS.has(code)) console.error(ERROR_HINTS.get(code));
|
|
293
|
+
process.exitCode = 1;
|
|
294
|
+
});
|
package/generated-contract.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Generated by tools/generate_openapi_sdk_contract.py. Do not edit by hand.
|
|
2
2
|
export const PROTOCOL_VERSION = "v1";
|
|
3
|
-
export const CONTRACT_SHA256 = "
|
|
3
|
+
export const CONTRACT_SHA256 = "d6f052592bc48920a04ecdce12ca82160a57662b67a14a8ce95f0262cb86d6d0";
|
|
4
4
|
export const OPENAPI_OPERATIONS = Object.freeze({
|
|
5
5
|
"approveAgentProposal": {
|
|
6
6
|
"method": "POST",
|
package/package.json
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@synapsor/client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Node.js SDK for Synapsor, the agent-native database for auditable AI applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./synapsor.mjs",
|
|
7
|
+
"bin": {
|
|
8
|
+
"synapsor": "bin/synapsor.mjs"
|
|
9
|
+
},
|
|
7
10
|
"exports": {
|
|
8
|
-
".": "./synapsor.mjs"
|
|
11
|
+
".": "./synapsor.mjs",
|
|
12
|
+
"./bin/synapsor": "./bin/synapsor.mjs"
|
|
9
13
|
},
|
|
10
14
|
"files": [
|
|
15
|
+
"bin/synapsor.mjs",
|
|
11
16
|
"synapsor.mjs",
|
|
12
17
|
"generated-contract.mjs",
|
|
13
18
|
"README.md",
|
|
14
19
|
"package.json"
|
|
15
20
|
],
|
|
16
21
|
"scripts": {
|
|
17
|
-
"build": "node --check synapsor.mjs && node --check generated-contract.mjs",
|
|
22
|
+
"build": "node --check synapsor.mjs && node --check generated-contract.mjs && node --check bin/synapsor.mjs",
|
|
18
23
|
"test": "node --test test/*.test.mjs"
|
|
19
24
|
},
|
|
20
25
|
"keywords": [
|
|
@@ -28,7 +33,7 @@
|
|
|
28
33
|
"homepage": "https://synapsor.ai",
|
|
29
34
|
"repository": {
|
|
30
35
|
"type": "git",
|
|
31
|
-
"url": "git+https://github.com/
|
|
36
|
+
"url": "git+https://github.com/synapsor/synapsor.git"
|
|
32
37
|
},
|
|
33
38
|
"bugs": {
|
|
34
39
|
"url": "https://synapsor.ai/contact"
|