@vatio-ai/cli 0.60.0 → 0.62.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 +1 -0
- package/bin/vatio.mjs +8 -1
- package/lib/api-client.mjs +53 -0
- package/lib/commands/identity-key.mjs +6 -1
- package/lib/commands/supabase-auth.mjs +226 -0
- package/lib/commands/webhooks.mjs +142 -0
- package/lib/commands/workspace-admin.mjs +60 -3
- package/lib/help.mjs +12 -0
- package/lib/telemetry.mjs +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -70,6 +70,7 @@ kb write|cat|rm-entry Entries, from a file or stdin
|
|
|
70
70
|
secrets list|set|rm Credentials your tools read
|
|
71
71
|
tokens list|create|origins|revoke Publishable tokens and the origins each accepts
|
|
72
72
|
auth --new-key Keypair that signs the visitor's JWT
|
|
73
|
+
auth --supabase [URL] Your users sign in with Supabase: no keys, no code
|
|
73
74
|
|
|
74
75
|
whatsapp | instagram Set up in the console; prints the link
|
|
75
76
|
|
package/bin/vatio.mjs
CHANGED
|
@@ -12,7 +12,8 @@ import { login, logout } from "../lib/commands/auth.mjs";
|
|
|
12
12
|
import { configCommand, doctor, init, version } from "../lib/commands/misc.mjs";
|
|
13
13
|
import { diff, publish, push, rollback, status } from "../lib/commands/deploy.mjs";
|
|
14
14
|
import { env } from "../lib/commands/env.mjs";
|
|
15
|
-
import { secrets, tokens } from "../lib/commands/workspace-admin.mjs";
|
|
15
|
+
import { keys, secrets, tokens } from "../lib/commands/workspace-admin.mjs";
|
|
16
|
+
import { templates, webhooks } from "../lib/commands/webhooks.mjs";
|
|
16
17
|
import { auth } from "../lib/commands/identity-key.mjs";
|
|
17
18
|
import { chat } from "../lib/commands/chat.mjs";
|
|
18
19
|
import { kb } from "../lib/commands/knowledge.mjs";
|
|
@@ -83,6 +84,12 @@ async function run(command, args) {
|
|
|
83
84
|
return await diff(config, args);
|
|
84
85
|
case "tokens":
|
|
85
86
|
return await tokens(config, args);
|
|
87
|
+
case "keys":
|
|
88
|
+
return await keys(config, args);
|
|
89
|
+
case "templates":
|
|
90
|
+
return await templates(config, args);
|
|
91
|
+
case "webhooks":
|
|
92
|
+
return await webhooks(config, args);
|
|
86
93
|
case "auth":
|
|
87
94
|
return await auth(config, args);
|
|
88
95
|
case "chat":
|
package/lib/api-client.mjs
CHANGED
|
@@ -65,6 +65,11 @@ export class ApiClient extends HttpClient {
|
|
|
65
65
|
return this.delete(`/env/${encode(name)}`);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
// The project "Connect Supabase" picked in the console, if any.
|
|
69
|
+
supabase() {
|
|
70
|
+
return this.get("/supabase");
|
|
71
|
+
}
|
|
72
|
+
|
|
68
73
|
// --- secrets --------------------------------------------------------
|
|
69
74
|
|
|
70
75
|
// Without an environment, the value everywhere; with one, the value only
|
|
@@ -171,6 +176,54 @@ export class ApiClient extends HttpClient {
|
|
|
171
176
|
return this.patch("/business", fields);
|
|
172
177
|
}
|
|
173
178
|
|
|
179
|
+
// --- server keys ----------------------------------------------------
|
|
180
|
+
|
|
181
|
+
listServerKeys() {
|
|
182
|
+
return this.get("/server_keys");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
createServerKey({ environment, label = null }) {
|
|
186
|
+
const body = { environment };
|
|
187
|
+
if (String(label ?? "").trim() !== "") body.label = String(label).trim();
|
|
188
|
+
return this.post("/server_keys", body);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
revokeServerKey({ prefix }) {
|
|
192
|
+
return this.delete(`/server_keys/${encode(prefix)}`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// --- templates, webhooks ---------------------------------------------
|
|
196
|
+
|
|
197
|
+
listTemplates({ environment }) {
|
|
198
|
+
return this.get("/templates", { environment });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
listWebhookEndpoints() {
|
|
202
|
+
return this.get("/webhook_endpoints");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
createWebhookEndpoint({ environment, url, events = [], label = null }) {
|
|
206
|
+
const body = { environment, url, events };
|
|
207
|
+
if (String(label ?? "").trim() !== "") body.label = String(label).trim();
|
|
208
|
+
return this.post("/webhook_endpoints", body);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
updateWebhookEndpoint({ id, ...attrs }) {
|
|
212
|
+
return this.patch(`/webhook_endpoints/${encode(id)}`, attrs);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
deleteWebhookEndpoint({ id }) {
|
|
216
|
+
return this.delete(`/webhook_endpoints/${encode(id)}`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
testWebhookEndpoint({ id }) {
|
|
220
|
+
return this.post(`/webhook_endpoints/${encode(id)}/test`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
listWebhookDeliveries({ id }) {
|
|
224
|
+
return this.get(`/webhook_endpoints/${encode(id)}/deliveries`);
|
|
225
|
+
}
|
|
226
|
+
|
|
174
227
|
// --- chats ----------------------------------------------------------
|
|
175
228
|
|
|
176
229
|
createChat({ environment }) {
|
|
@@ -11,13 +11,18 @@ import { execFileSync } from "node:child_process";
|
|
|
11
11
|
import { existsSync, writeFileSync, readFileSync, unlinkSync } from "node:fs";
|
|
12
12
|
import { join } from "node:path";
|
|
13
13
|
import { fail } from "../support.mjs";
|
|
14
|
+
import { supabaseAuth } from "./supabase-auth.mjs";
|
|
14
15
|
|
|
15
16
|
const PUBLIC_FILE = "identity.pub";
|
|
16
17
|
const PRIVATE_FILE = "identity.pem";
|
|
17
18
|
|
|
18
19
|
export async function auth(config, args) {
|
|
20
|
+
if (args.includes("--supabase")) return supabaseAuth(config, args);
|
|
19
21
|
if (!args.includes("--new-key")) {
|
|
20
|
-
fail(
|
|
22
|
+
fail(
|
|
23
|
+
"Usage: vatio auth --new-key (generates the keypair that signs your JWT)\n" +
|
|
24
|
+
" vatio auth --supabase (your users sign in with Supabase: no keys, no code)"
|
|
25
|
+
);
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
const root = config.workspaceRootRequired();
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// `vatio auth --supabase` — signed-in users for an app whose users already sign
|
|
2
|
+
// in with Supabase Auth, with no keypair and no signing code.
|
|
3
|
+
//
|
|
4
|
+
// The project is found where the app already keeps it (a .env, or the client
|
|
5
|
+
// Lovable generates), checked for the asymmetric signing keys Vatio verifies
|
|
6
|
+
// with, and written into vatio.yml. Its URL and public anon key go into the
|
|
7
|
+
// workspace secrets so a tool can call the project's REST API as the visitor,
|
|
8
|
+
// with their own Row Level Security, in two headers.
|
|
9
|
+
|
|
10
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { dirname, join, resolve } from "node:path";
|
|
12
|
+
import { parseDocument } from "yaml";
|
|
13
|
+
import { deployClient } from "./deploy.mjs";
|
|
14
|
+
import { fail, paint, takeValue } from "../support.mjs";
|
|
15
|
+
|
|
16
|
+
const PROJECT_URL = /^https:\/\/[a-z0-9]+\.supabase\.co$/;
|
|
17
|
+
const URL_IN_SOURCE = /https:\/\/[a-z0-9]+\.supabase\.co/;
|
|
18
|
+
const URL_VARIABLE = /^\s*(?:export\s+)?[A-Z_]*SUPABASE_URL\s*=\s*["']?(https:\/\/[a-z0-9]+\.supabase\.co)/m;
|
|
19
|
+
// Anon or publishable only: a service-role or secret key would let whoever
|
|
20
|
+
// holds it read past every policy, and it never belongs to a widget's tools.
|
|
21
|
+
const KEY_VARIABLE = /^\s*(?:export\s+|const\s+)?[A-Z_]*SUPABASE_(?:ANON|PUBLISHABLE)[A-Z_]*KEY\s*[=:]\s*["']?([A-Za-z0-9._-]{20,})/m;
|
|
22
|
+
const PLACES = [
|
|
23
|
+
".env",
|
|
24
|
+
".env.local",
|
|
25
|
+
".env.development",
|
|
26
|
+
".env.development.local",
|
|
27
|
+
".env.production",
|
|
28
|
+
"src/integrations/supabase/client.ts",
|
|
29
|
+
"src/integrations/supabase/client.js"
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
export async function supabaseAuth(config, args) {
|
|
33
|
+
const root = config.workspaceRootRequired();
|
|
34
|
+
const workspace = config.resolveWorkspaceRequired();
|
|
35
|
+
const signInUrl = takeValue(args, "--sign-in-url");
|
|
36
|
+
const force = args.includes("--force");
|
|
37
|
+
const skipSecrets = args.includes("--no-secrets");
|
|
38
|
+
const index = args.indexOf("--supabase");
|
|
39
|
+
const given = args[index + 1]?.startsWith("https://") ? args[index + 1] : null;
|
|
40
|
+
|
|
41
|
+
if (signInUrl && !/^https:\/\/[^/\s?#]+/.test(signInUrl)) fail("--sign-in-url must be an https url on your own site");
|
|
42
|
+
|
|
43
|
+
// The console's "Connect Supabase" is the one explicit answer short of a URL
|
|
44
|
+
// on the command line; a .env is a guess about which project the app uses.
|
|
45
|
+
const connected = given ? null : await connectedProject(config, workspace);
|
|
46
|
+
const found = connected ? {} : detect([process.cwd(), root]);
|
|
47
|
+
const url = normalize(given ?? connected?.url ?? found.url);
|
|
48
|
+
if (!url) {
|
|
49
|
+
fail(
|
|
50
|
+
"Could not find your Supabase project URL in a .env or src/integrations/supabase/client.ts.\n" +
|
|
51
|
+
"Pass it: vatio auth --supabase https://<project-ref>.supabase.co\n" +
|
|
52
|
+
"or connect Supabase in the console (Channels)."
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
if (!PROJECT_URL.test(url)) {
|
|
56
|
+
fail(`${url} is not a Supabase project URL (https://<project-ref>.supabase.co). Custom domains are not supported yet.`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await checkSigningKeys(url);
|
|
60
|
+
writeManifest(config.manifestPath(), url, signInUrl, force);
|
|
61
|
+
|
|
62
|
+
console.log(`${paint("green", "✓")} vatio.yml now verifies visitors with ${url}`);
|
|
63
|
+
if (connected) console.log(` (the project connected in the console: ${connected.name})`);
|
|
64
|
+
else if (found.from && !given) console.log(` (found in ${found.from})`);
|
|
65
|
+
|
|
66
|
+
if (connected) console.log(" SUPABASE_URL and SUPABASE_ANON_KEY were set when it was connected.");
|
|
67
|
+
else if (!skipSecrets) await storeSecrets(config, workspace, url, found.key);
|
|
68
|
+
|
|
69
|
+
console.log("");
|
|
70
|
+
console.log("What happens after `vatio push` and `vatio publish`:");
|
|
71
|
+
console.log(" • Web: the widget reads the Supabase session already on your page. Nothing to add —");
|
|
72
|
+
console.log(" signed in, the visitor is known; signed out, they are anonymous.");
|
|
73
|
+
if (signInUrl || existingSignInUrl(config.manifestPath())) {
|
|
74
|
+
console.log(" • WhatsApp and Instagram: the Sign in button opens your sign_in_url. Keep the widget");
|
|
75
|
+
console.log(" script on that page: it waits for the person to sign in and hands the session over.");
|
|
76
|
+
} else {
|
|
77
|
+
console.log(" • WhatsApp and Instagram: add your login page, with the widget script on it —");
|
|
78
|
+
console.log(" vatio auth --supabase --sign-in-url https://your-app.com/login");
|
|
79
|
+
}
|
|
80
|
+
console.log("");
|
|
81
|
+
console.log("A private tool reads the visitor's own rows, under your Row Level Security:");
|
|
82
|
+
console.log("");
|
|
83
|
+
console.log(" # tools/my_orders.yml");
|
|
84
|
+
console.log(" access: private");
|
|
85
|
+
console.log(" request:");
|
|
86
|
+
console.log(" method: GET");
|
|
87
|
+
console.log(" base_url: $env.SUPABASE_URL");
|
|
88
|
+
console.log(" path: /rest/v1/orders");
|
|
89
|
+
console.log(" query: { select: \"*\" }");
|
|
90
|
+
console.log(" headers:");
|
|
91
|
+
console.log(" apikey: $env.SUPABASE_ANON_KEY");
|
|
92
|
+
console.log(" Authorization: Bearer $auth.token");
|
|
93
|
+
console.log(" respond:");
|
|
94
|
+
console.log(" data: { orders: \"$\" }");
|
|
95
|
+
console.log(" message: { when_empty: No orders yet., default: \"Found {{count}} orders.\" }");
|
|
96
|
+
console.log("");
|
|
97
|
+
console.log("Docs: https://docs.vatio.ai/authentication/supabase");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function connectedProject(config, workspace) {
|
|
101
|
+
if (!config.resolveBaseUrl() || !config.resolveToken()) return null;
|
|
102
|
+
try {
|
|
103
|
+
return (await deployClient(config, workspace).supabase())?.project ?? null;
|
|
104
|
+
} catch {
|
|
105
|
+
// An older platform, or no network: the .env is still there to read.
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Where the app already says which project it uses: the nearest directory, from
|
|
111
|
+
// here and from the workspace root upwards, that names one.
|
|
112
|
+
function detect(starts) {
|
|
113
|
+
const seen = new Set();
|
|
114
|
+
for (const start of starts) {
|
|
115
|
+
let dir = resolve(start);
|
|
116
|
+
for (let depth = 0; depth < 4; depth += 1) {
|
|
117
|
+
if (!seen.has(dir)) {
|
|
118
|
+
seen.add(dir);
|
|
119
|
+
const found = detectIn(dir);
|
|
120
|
+
if (found.url) return found;
|
|
121
|
+
}
|
|
122
|
+
const parent = dirname(dir);
|
|
123
|
+
if (parent === dir) break;
|
|
124
|
+
dir = parent;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return {};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function detectIn(dir) {
|
|
131
|
+
let url = null;
|
|
132
|
+
let key = null;
|
|
133
|
+
let from = null;
|
|
134
|
+
for (const place of PLACES) {
|
|
135
|
+
const path = join(dir, place);
|
|
136
|
+
if (!existsSync(path)) continue;
|
|
137
|
+
const text = readFileSync(path, "utf8");
|
|
138
|
+
const foundUrl = text.match(URL_VARIABLE)?.[1] ?? (place.startsWith("src/") ? text.match(URL_IN_SOURCE)?.[0] : null);
|
|
139
|
+
if (!url && foundUrl) {
|
|
140
|
+
url = foundUrl;
|
|
141
|
+
from = path;
|
|
142
|
+
}
|
|
143
|
+
const foundKey = text.match(KEY_VARIABLE)?.[1];
|
|
144
|
+
if (!key && foundKey && publicKey(foundKey)) key = foundKey;
|
|
145
|
+
}
|
|
146
|
+
return { url, key, from };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// The anon JWT (role "anon") or a publishable key; never a secret or
|
|
150
|
+
// service-role one, whatever the variable was called.
|
|
151
|
+
function publicKey(value) {
|
|
152
|
+
if (value.startsWith("sb_publishable_")) return true;
|
|
153
|
+
if (value.startsWith("sb_secret_")) return false;
|
|
154
|
+
try {
|
|
155
|
+
const payload = JSON.parse(Buffer.from(value.split(".")[1], "base64url").toString("utf8"));
|
|
156
|
+
return payload.role === "anon";
|
|
157
|
+
} catch {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function normalize(value) {
|
|
163
|
+
return value ? value.trim().toLowerCase().replace(/\/+$/, "") : null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function checkSigningKeys(url) {
|
|
167
|
+
let keys;
|
|
168
|
+
try {
|
|
169
|
+
const response = await fetch(`${url}/auth/v1/.well-known/jwks.json`, { signal: AbortSignal.timeout(8000) });
|
|
170
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
171
|
+
keys = (await response.json()).keys ?? [];
|
|
172
|
+
} catch (error) {
|
|
173
|
+
console.warn(`${paint("yellow", "!")} Could not reach ${url} to check its signing keys (${error.message}); the deploy checks again.`);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (keys.length > 0) return;
|
|
177
|
+
|
|
178
|
+
fail(
|
|
179
|
+
`${url} still signs with the legacy JWT secret (HS256), which Vatio cannot verify without being able\n` +
|
|
180
|
+
"to mint tokens too. In the Supabase dashboard: Project Settings → JWT Keys → migrate, then rotate\n" +
|
|
181
|
+
"to an ES256 key. Your app keeps working through the switch. Then run this again."
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function writeManifest(path, url, signInUrl, force) {
|
|
186
|
+
const doc = parseDocument(readFileSync(path, "utf8"));
|
|
187
|
+
if (doc.hasIn(["auth", "public_key"]) && !force) {
|
|
188
|
+
fail(
|
|
189
|
+
"vatio.yml already verifies a JWT your backend signs (auth.public_key). Switching to Supabase signs out\n" +
|
|
190
|
+
"every visitor holding one of those — re-run with --force if that is what you want."
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
for (const key of [ "public_key", "algorithm" ]) {
|
|
194
|
+
if (doc.hasIn(["auth", key])) doc.deleteIn(["auth", key]);
|
|
195
|
+
}
|
|
196
|
+
doc.setIn(["auth", "supabase"], url);
|
|
197
|
+
if (signInUrl) doc.setIn(["auth", "sign_in_url"], signInUrl);
|
|
198
|
+
writeFileSync(path, String(doc));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function existingSignInUrl(path) {
|
|
202
|
+
return parseDocument(readFileSync(path, "utf8")).hasIn(["auth", "sign_in_url"]);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function storeSecrets(config, workspace, url, key) {
|
|
206
|
+
const commands = [ `vatio secrets set SUPABASE_URL ${url}` ];
|
|
207
|
+
if (key) commands.push(`vatio secrets set SUPABASE_ANON_KEY ${key}`);
|
|
208
|
+
else commands.push("vatio secrets set SUPABASE_ANON_KEY <your anon or publishable key>");
|
|
209
|
+
|
|
210
|
+
if (!config.resolveBaseUrl() || !config.resolveToken()) {
|
|
211
|
+
console.log("Not logged in, so the secrets your tools use are not set yet. After `vatio login`:");
|
|
212
|
+
for (const command of commands) console.log(` ${command}`);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
const client = deployClient(config, workspace);
|
|
218
|
+
await client.upsertSecret({ key: "SUPABASE_URL", value: url, environment: null });
|
|
219
|
+
if (key) await client.upsertSecret({ key: "SUPABASE_ANON_KEY", value: key, environment: null });
|
|
220
|
+
console.log(`${paint("green", "✓")} Set secret SUPABASE_URL${key ? " and SUPABASE_ANON_KEY" : ""} on workspace ${workspace}`);
|
|
221
|
+
if (!key) console.log(` Add the anon key yourself: ${commands[1]}`);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
console.warn(`${paint("yellow", "!")} Could not set the secrets (${error.message}). Run:`);
|
|
224
|
+
for (const command of commands) console.warn(` ${command}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// `vatio templates` / `vatio webhooks` — what a developer sets up so their
|
|
2
|
+
// backend can send WhatsApp messages through the server API: the templates
|
|
3
|
+
// the number can send, and where Vatio reports what happened. Sending itself
|
|
4
|
+
// is the backend's job, with a server key, not the CLI's.
|
|
5
|
+
|
|
6
|
+
import { deployClient } from "./deploy.mjs";
|
|
7
|
+
import { fail, paint, sleep, takeEnv, takeFlag, takeValue } from "../support.mjs";
|
|
8
|
+
|
|
9
|
+
const WAIT_INTERVAL_MS = 2_000;
|
|
10
|
+
|
|
11
|
+
// Repeatable: `--event message.sent --event message.replied`.
|
|
12
|
+
function takeAll(args, name) {
|
|
13
|
+
const values = [];
|
|
14
|
+
for (let value = takeValue(args, name); value !== null; value = takeValue(args, name)) values.push(value);
|
|
15
|
+
return values;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function templates(config, args) {
|
|
19
|
+
const environment = takeEnv(args, { fallback: "live", cwd: config.workspaceRoot });
|
|
20
|
+
const json = takeFlag(args, "--json");
|
|
21
|
+
const workspace = config.resolveWorkspaceRequired();
|
|
22
|
+
const client = deployClient(config, workspace);
|
|
23
|
+
const sub = args.shift() ?? "list";
|
|
24
|
+
if (sub !== "list") fail("Usage: vatio templates list [--env live] [--json]");
|
|
25
|
+
|
|
26
|
+
const payload = await client.listTemplates({ environment });
|
|
27
|
+
const list = Array.isArray(payload.templates) ? payload.templates : [];
|
|
28
|
+
if (json) {
|
|
29
|
+
console.log(JSON.stringify(list, null, 2));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (list.length === 0) {
|
|
33
|
+
console.log(environment === "live"
|
|
34
|
+
? "No templates on this workspace's WhatsApp number. Create them on the console's Channels → WhatsApp page."
|
|
35
|
+
: "Templates belong to the workspace's own number; the preview number has none of yours. Use --env live.");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
console.log(`Templates on the workspace's number (${list.length}):`);
|
|
39
|
+
for (const template of list) {
|
|
40
|
+
const status = template.status === "APPROVED" ? paint("green", template.status) : paint("cyan", template.status);
|
|
41
|
+
const count = template.parameter_count ?? "?";
|
|
42
|
+
console.log(` ${template.name} (${template.language}) ${template.category} ${status} ${count} param${count === 1 ? "" : "s"}`);
|
|
43
|
+
if (template.body) console.log(` ${template.body.replace(/\n/g, "\n ")}`);
|
|
44
|
+
if (template.status === "APPROVED") console.log(` "template": ${JSON.stringify(template.example)}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function webhooks(config, args) {
|
|
49
|
+
const environment = takeEnv(args, { fallback: "live", cwd: config.workspaceRoot });
|
|
50
|
+
const label = takeValue(args, "--label");
|
|
51
|
+
const events = takeAll(args, "--event");
|
|
52
|
+
const workspace = config.resolveWorkspaceRequired();
|
|
53
|
+
const client = deployClient(config, workspace);
|
|
54
|
+
const sub = args.shift() ?? "list";
|
|
55
|
+
|
|
56
|
+
if (sub === "list") {
|
|
57
|
+
const payload = await client.listWebhookEndpoints();
|
|
58
|
+
const list = Array.isArray(payload.webhook_endpoints) ? payload.webhook_endpoints : [];
|
|
59
|
+
if (list.length === 0) {
|
|
60
|
+
console.log(`No webhook endpoints on workspace ${workspace}`);
|
|
61
|
+
console.log(" Add one: vatio webhooks create https://your-app.com/vatio/webhooks --env live");
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
console.log(`Webhook endpoints on workspace ${workspace} (${list.length}):`);
|
|
65
|
+
for (const endpoint of list) {
|
|
66
|
+
const state = endpoint.enabled ? paint("green", "enabled") : paint("red", "disabled");
|
|
67
|
+
console.log(` #${endpoint.id} ${endpoint.environment} ${endpoint.url} ${state}`);
|
|
68
|
+
console.log(` events: ${endpoint.events.length > 0 ? endpoint.events.join(", ") : "all"}` +
|
|
69
|
+
(endpoint.consecutive_failures > 0 ? ` failing: ${endpoint.consecutive_failures} in a row` : ""));
|
|
70
|
+
}
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (sub === "create") {
|
|
74
|
+
const url = args.shift();
|
|
75
|
+
if (!url) fail("Usage: vatio webhooks create URL [--event NAME]... [--label NAME] [--env NAME]");
|
|
76
|
+
const payload = await client.createWebhookEndpoint({ environment, url, events, label });
|
|
77
|
+
console.log(`Created webhook endpoint #${payload.id} for ${environment}: ${payload.url}`);
|
|
78
|
+
console.log(` events: ${payload.events.length > 0 ? payload.events.join(", ") : "all"}`);
|
|
79
|
+
console.log("");
|
|
80
|
+
// Vatio signs every delivery with it; it is not shown again.
|
|
81
|
+
console.log(` ${payload.secret}`);
|
|
82
|
+
console.log("");
|
|
83
|
+
console.log("Copy the signing secret now — it is not shown again. Check it with: vatio webhooks test " + payload.id);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (sub === "test") {
|
|
87
|
+
const id = args.shift();
|
|
88
|
+
if (!id) fail("Usage: vatio webhooks test ID");
|
|
89
|
+
const delivery = await client.testWebhookEndpoint({ id });
|
|
90
|
+
console.log(`Sent a ping (${delivery.id}). Waiting for your endpoint…`);
|
|
91
|
+
const deadline = Date.now() + 30_000;
|
|
92
|
+
for (;;) {
|
|
93
|
+
await sleep(WAIT_INTERVAL_MS);
|
|
94
|
+
const { deliveries = [] } = await client.listWebhookDeliveries({ id });
|
|
95
|
+
const current = deliveries.find((entry) => entry.id === delivery.id);
|
|
96
|
+
if (current && (current.status === "delivered" || current.attempts > 0)) {
|
|
97
|
+
printDelivery(current);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (Date.now() > deadline) {
|
|
101
|
+
console.log("No answer yet. Check later with: vatio webhooks deliveries " + id);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (sub === "deliveries") {
|
|
107
|
+
const id = args.shift();
|
|
108
|
+
if (!id) fail("Usage: vatio webhooks deliveries ID");
|
|
109
|
+
const { deliveries = [] } = await client.listWebhookDeliveries({ id });
|
|
110
|
+
if (deliveries.length === 0) {
|
|
111
|
+
console.log("No deliveries yet.");
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
for (const delivery of deliveries) printDelivery(delivery);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (sub === "enable") {
|
|
118
|
+
const id = args.shift();
|
|
119
|
+
if (!id) fail("Usage: vatio webhooks enable ID");
|
|
120
|
+
await client.updateWebhookEndpoint({ id, enabled: true });
|
|
121
|
+
console.log(`Enabled webhook endpoint #${id}`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (sub === "rm" || sub === "remove" || sub === "delete") {
|
|
125
|
+
const id = args.shift();
|
|
126
|
+
if (!id) fail("Usage: vatio webhooks rm ID");
|
|
127
|
+
await client.deleteWebhookEndpoint({ id });
|
|
128
|
+
console.log(`Removed webhook endpoint #${id}`);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
fail("Usage: vatio webhooks list|create URL [--event NAME]...|test ID|deliveries ID|enable ID|rm ID [--env NAME]");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function printDelivery(delivery) {
|
|
136
|
+
const status = delivery.status === "delivered"
|
|
137
|
+
? paint("green", "delivered")
|
|
138
|
+
: delivery.status === "failed" ? paint("red", "failed") : paint("cyan", "retrying");
|
|
139
|
+
const http = delivery.response_status ? ` HTTP ${delivery.response_status}` : "";
|
|
140
|
+
const error = delivery.last_error ? ` — ${delivery.last_error}` : "";
|
|
141
|
+
console.log(` ${delivery.id} ${delivery.event} ${status}${http} attempts ${delivery.attempts} ${delivery.created_at}${error}`);
|
|
142
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// secrets / tokens — the
|
|
2
|
-
// deliberately does not carry. A secret is a credential, and so
|
|
3
|
-
// publishable token (with the origins it accepts)
|
|
1
|
+
// secrets / tokens / keys — the things a workspace has that its manifest
|
|
2
|
+
// deliberately does not carry. A secret is a credential, and so are a
|
|
3
|
+
// publishable token (with the origins it accepts) and a server key; none of
|
|
4
|
+
// them is in vatio.yml.
|
|
4
5
|
|
|
5
6
|
import { deployClient } from "./deploy.mjs";
|
|
6
7
|
import { askSecret, branchDefault, fail, paint, takeValue } from "../support.mjs";
|
|
@@ -137,6 +138,62 @@ export async function tokens(config, args) {
|
|
|
137
138
|
);
|
|
138
139
|
}
|
|
139
140
|
|
|
141
|
+
// Server keys: what the developer's own backend calls the server API with --
|
|
142
|
+
// today, to have the agent write to a contact first. Unlike a publishable
|
|
143
|
+
// token it is a secret, and unlike the CLI's own token it belongs to the
|
|
144
|
+
// workspace, so it outlives whoever created it.
|
|
145
|
+
export async function keys(config, args) {
|
|
146
|
+
const environment = environmentOption(args, "live", config.workspaceRoot);
|
|
147
|
+
const label = takeValue(args, "--label") ?? takeValue(args, "-l");
|
|
148
|
+
const workspace = config.resolveWorkspaceRequired();
|
|
149
|
+
const client = deployClient(config, workspace);
|
|
150
|
+
const sub = args.shift() ?? "list";
|
|
151
|
+
|
|
152
|
+
if (sub === "list") {
|
|
153
|
+
const payload = await client.listServerKeys();
|
|
154
|
+
const list = asArray(payload.server_keys);
|
|
155
|
+
if (list.length === 0) {
|
|
156
|
+
console.log(`No server keys on workspace ${workspace}`);
|
|
157
|
+
console.log(" Create one: vatio keys create --env live --label backend");
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
console.log(`Server keys on workspace ${workspace} (${list.length}):`);
|
|
161
|
+
for (const key of list) {
|
|
162
|
+
const parts = [key.prefix, key.environment];
|
|
163
|
+
if (key.label) parts.push(key.label);
|
|
164
|
+
parts.push(key.last_used_at ? `last used ${key.last_used_at}` : "never used");
|
|
165
|
+
console.log(` ${parts.join(" ")}`);
|
|
166
|
+
}
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (sub === "create") {
|
|
170
|
+
const payload = await client.createServerKey({ environment, label });
|
|
171
|
+
console.log(`Created a server key for ${environment} on workspace ${workspace}`);
|
|
172
|
+
console.log("");
|
|
173
|
+
// The platform keeps only a digest: this is the one time the key exists
|
|
174
|
+
// anywhere it can be copied from.
|
|
175
|
+
console.log(` ${payload.key}`);
|
|
176
|
+
console.log("");
|
|
177
|
+
console.log("Copy it now — it is not shown again. Keep it on your server, never in a page or an app:");
|
|
178
|
+
console.log("");
|
|
179
|
+
console.log(` VATIO_SERVER_KEY=${payload.key}`);
|
|
180
|
+
console.log("");
|
|
181
|
+
console.log("Try it (lists the templates your number can send):");
|
|
182
|
+
console.log(` curl ${config.resolveBaseUrl()}/api/server/v1/${workspace}/templates \\`);
|
|
183
|
+
console.log(' -H "Authorization: Bearer $VATIO_SERVER_KEY"');
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (sub === "revoke") {
|
|
187
|
+
const prefix = args.shift();
|
|
188
|
+
if (!prefix) fail("Usage: vatio keys revoke PREFIX");
|
|
189
|
+
await client.revokeServerKey({ prefix });
|
|
190
|
+
console.log(`Revoked ${prefix} on workspace ${workspace}`);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
fail("Usage: vatio keys list|create [--env live|preview|NAME] [--label NAME]|revoke PREFIX");
|
|
195
|
+
}
|
|
196
|
+
|
|
140
197
|
// An empty list is not "anywhere": only Vatio's own pages can use the token.
|
|
141
198
|
function originsLine(origins) {
|
|
142
199
|
const list = asArray(origins);
|
package/lib/help.mjs
CHANGED
|
@@ -92,7 +92,19 @@ and the one its pull request deploys to. Elsewhere commands default as listed.
|
|
|
92
92
|
tokens list|create [--env NAME] [--label NAME] [--origin URL]...|revoke PREFIX
|
|
93
93
|
tokens origins PREFIX URL...
|
|
94
94
|
The origins a token is accepted from (replaces the list)
|
|
95
|
+
keys list|create [--env NAME] [--label NAME]|revoke PREFIX
|
|
96
|
+
Server keys, for your backend to call the server API
|
|
95
97
|
auth --new-key Keypair that signs the JWT saying who a visitor is
|
|
98
|
+
auth --supabase [URL] [--sign-in-url URL]
|
|
99
|
+
Your users sign in with Supabase: verify their
|
|
100
|
+
session, no keys or signing code
|
|
101
|
+
|
|
102
|
+
Sending from your backend (WhatsApp, with a server key):
|
|
103
|
+
templates list [--env live] [--json]
|
|
104
|
+
The number's templates, parameter counts, and the
|
|
105
|
+
"template" object to send
|
|
106
|
+
webhooks list|create URL [--event NAME]...|test ID|deliveries ID|enable ID|rm ID
|
|
107
|
+
Where Vatio tells your backend what happened
|
|
96
108
|
|
|
97
109
|
Channels:
|
|
98
110
|
whatsapp Set up in the console; prints the link
|
package/lib/telemetry.mjs
CHANGED
|
@@ -31,6 +31,9 @@ const SUBCOMMANDS = {
|
|
|
31
31
|
business: ["show", "set", "hours"],
|
|
32
32
|
secrets: ["list", "set", "rm"],
|
|
33
33
|
tokens: ["list", "create", "revoke", "origins"],
|
|
34
|
+
keys: ["list", "create", "revoke"],
|
|
35
|
+
templates: ["list"],
|
|
36
|
+
webhooks: ["list", "create", "test", "deliveries", "enable", "rm"],
|
|
34
37
|
config: ["show", "get", "set", "unset"]
|
|
35
38
|
};
|
|
36
39
|
|