@vatio-ai/cli 0.112.0 → 0.114.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.
@@ -34,15 +34,6 @@ export class ApiClient extends HttpClient {
34
34
  return this.get("/test_phones");
35
35
  }
36
36
 
37
- // "Your numbers", which need no number connected.
38
- ownerNumbers() {
39
- return this.get("/whatsapp/owner_numbers");
40
- }
41
-
42
- updateOwnerNumbers(numbers) {
43
- return this.patch("/whatsapp/owner_numbers", { owner_numbers: numbers });
44
- }
45
-
46
37
  deployedManifest({ environment = "live" } = {}) {
47
38
  return this.get("/manifest", { environment });
48
39
  }
@@ -185,10 +176,14 @@ export class ApiClient extends HttpClient {
185
176
  return this.get("/api_keys");
186
177
  }
187
178
 
188
- createApiKey({ kind, environment, label = null, allowedOrigins = [] }) {
189
- const body = { kind, environment };
179
+ // `scopes` (a secret key's) left null takes the platform's default: every
180
+ // scope but `team`. `confirmation` is { code } or { recovery_code }, which a
181
+ // `team` key needs.
182
+ createApiKey({ kind, environment, label = null, allowedOrigins = [], scopes = null, confirmation = {} }) {
183
+ const body = { kind, environment, ...confirmation };
190
184
  if (String(label ?? "").trim() !== "") body.label = String(label).trim();
191
185
  if (allowedOrigins.length > 0) body.allowed_origins = allowedOrigins;
186
+ if (Array.isArray(scopes) && scopes.length > 0) body.scopes = scopes;
192
187
  return this.post("/api_keys", body);
193
188
  }
194
189
 
@@ -2,23 +2,25 @@
2
2
  // number or account needs Meta's consent screen, and test phones and accounts
3
3
  // are a form with a code. The CLI only says where, and that depends on whether
4
4
  // the workspace has published: before, "Try the agent", where test phones and
5
- // accounts are verified; after, the channel's own page under Channels.
5
+ // accounts are verified; after, the channel's own page (WhatsApp, Instagram).
6
6
  // vatio.ai/workspaces is only for a server too old to send the link, and
7
7
  // redirects to the inbox.
8
- //
9
- // One thing about WhatsApp is set here too: your numbers (`vatio whatsapp
10
- // mine`), the business's own conversations, which need no number connected.
11
8
 
12
9
  import { deployClient } from "./deploy.mjs";
13
10
  import { fail, paint, takeFlag } from "../support.mjs";
14
11
 
15
12
  const WHATSAPP_USAGE =
16
- "Usage: vatio whatsapp [test-phones [--json] | mine [NUMBER... | --clear]]";
13
+ "Usage: vatio whatsapp [test-phones [--json]]";
17
14
  // The list of who the agent answered on your own number is gone: it answers
18
15
  // everyone, and Live off in the inbox is how it answers nobody.
19
16
  const AGENT_NUMBERS_REMOVED =
20
17
  "`vatio whatsapp only` and `everyone` were removed: the agent answers everyone who writes to your number. " +
21
18
  "To have it answer nobody, turn Live off in the inbox, on the WhatsApp page.";
19
+ // "Your numbers" were a list typed by hand; the team's numbers took its place.
20
+ const MINE_REMOVED =
21
+ "`vatio whatsapp mine` was removed: the team's numbers are the ones each member verified on their account " +
22
+ "(Account in the inbox), so there is no list to set. A tool declares which role may use it with " +
23
+ "`access: supervisor`, `developer` or `owner`.";
22
24
 
23
25
  export async function whatsapp(config, args = []) {
24
26
  const sub = args.shift();
@@ -28,11 +30,7 @@ export async function whatsapp(config, args = []) {
28
30
  await printTestPhones(config).catch((error) => {
29
31
  if (error?.status !== 404 && error?.body?.error?.code !== "not_found") throw error;
30
32
  });
31
- await printNumber(config);
32
- // A server that predates the route answers 404; nothing more to show.
33
- return printOwnerNumbers(config).catch((error) => {
34
- if (error?.status !== 404 && error?.body?.error?.code !== "not_found") throw error;
35
- });
33
+ return printNumber(config);
36
34
  }
37
35
  if (sub === "test-phones") {
38
36
  const json = takeFlag(args, "--json");
@@ -40,12 +38,7 @@ export async function whatsapp(config, args = []) {
40
38
  return printTestPhones(config, { json });
41
39
  }
42
40
  if (sub === "only" || sub === "everyone") fail(AGENT_NUMBERS_REMOVED);
43
- if (sub === "mine") {
44
- const clear = takeFlag(args, "--clear");
45
- if (clear && args.length > 0) fail(WHATSAPP_USAGE);
46
- if (!clear && args.length === 0) return printOwnerNumbers(config);
47
- return setOwnerNumbers(config, clear ? [] : args.splice(0));
48
- }
41
+ if (sub === "mine") fail(MINE_REMOVED);
49
42
  fail(WHATSAPP_USAGE);
50
43
  }
51
44
 
@@ -91,30 +84,6 @@ async function printNumber(config) {
91
84
  }
92
85
  }
93
86
 
94
- async function setOwnerNumbers(config, numbers) {
95
- const client = deployClient(config, config.resolveWorkspaceRequired());
96
- printOwners(await client.updateOwnerNumbers(numbers));
97
- }
98
-
99
- async function printOwnerNumbers(config) {
100
- const client = deployClient(config, config.resolveWorkspaceRequired());
101
- printOwners(await client.ownerNumbers());
102
- }
103
-
104
- function printOwners(body) {
105
- const list = Array.isArray(body.owner_numbers) ? body.owner_numbers : [];
106
- if (list.length === 0) {
107
- console.log("\nYour numbers: none yet.");
108
- console.log(" On the preview number every verified test phone counts as one until you list some.");
109
- console.log(" List the business's own: vatio whatsapp mine +56912345678");
110
- return;
111
- }
112
- console.log(`\nYour numbers: ${list.join(", ")}`);
113
- console.log(" In these conversations the agent works for the business: `access: owner` tools are");
114
- console.log(" there, and what you send from the WhatsApp Business app is a message to the agent.");
115
- console.log(" Change them: vatio whatsapp mine NUMBER... Empty the list: vatio whatsapp mine --clear");
116
- }
117
-
118
87
  function printLive(number) {
119
88
  const live = number.live
120
89
  ? paint("green", "live: the agent answers everyone who writes")
@@ -104,27 +104,39 @@ function secretScope({ environment, tests }) {
104
104
 
105
105
  // Repeatable: `--origin https://a.com --origin https://b.com`.
106
106
  function takeOrigins(args) {
107
- const origins = [];
108
- for (let value = takeValue(args, "--origin"); value !== null; value = takeValue(args, "--origin")) {
109
- origins.push(value);
107
+ return takeRepeated(args, "--origin");
108
+ }
109
+
110
+ function takeRepeated(args, flag) {
111
+ const values = [];
112
+ for (let value = takeValue(args, flag); value !== null; value = takeValue(args, flag)) {
113
+ values.push(value);
110
114
  }
111
- return origins;
115
+ return values;
112
116
  }
113
117
 
114
118
  const KINDS = ["publishable", "secret"];
119
+ // What a secret key may call on the server API. The platform validates them;
120
+ // this list is only for the help text. `team` is never given by default.
121
+ const SCOPES = ["messages", "instagram", "chats", "team"];
115
122
  const KEYS_USAGE =
116
- "Usage: vatio keys list [--json]|create publishable|secret [--env live|preview] [--label NAME] [--origin URL]... [--json]|" +
117
- "origins PREFIX URL...|revoke PREFIX";
123
+ "Usage: vatio keys list [--json]|create publishable|secret [--env live|preview] [--label NAME] [--origin URL]... " +
124
+ "[--scope NAME]... [--code CODE | --recovery-code CODE] [--json]|origins PREFIX URL...|revoke PREFIX";
118
125
 
119
126
  // API keys, of two kinds. A publishable key is for a page or an app: public by
120
127
  // design, accepted only from the origins it lists. A secret key is for the
121
- // developer's own backend (today, to have the agent write to a contact first):
122
- // it never goes in a page or an app, and unlike the CLI's own token it belongs
123
- // to the workspace, so it outlives whoever created it.
128
+ // developer's own backend (to have the agent write to a contact first, read a
129
+ // chat, keep the team): it never goes in a page or an app, and unlike the
130
+ // CLI's own token it belongs to the workspace, so it outlives whoever created
131
+ // it. A secret key carries scopes; without --scope it gets every one but
132
+ // `team`.
124
133
  export async function keys(config, args) {
125
134
  const environment = environmentOption(args, "live");
126
135
  const label = takeValue(args, "--label") ?? takeValue(args, "-l");
127
136
  const origins = takeOrigins(args);
137
+ const scopes = takeRepeated(args, "--scope");
138
+ const code = takeValue(args, "--code");
139
+ const recoveryCode = takeValue(args, "--recovery-code");
128
140
  const json = takeFlag(args, "--json");
129
141
  const workspace = config.resolveWorkspaceRequired();
130
142
  const client = deployClient(config, workspace);
@@ -151,20 +163,29 @@ export async function keys(config, args) {
151
163
  parts.push(key.last_used_at ? `last used ${key.last_used_at}` : "never used");
152
164
  console.log(` ${parts.filter(Boolean).join(" ")}`);
153
165
  if (key.kind === "publishable") console.log(` origins: ${originsLine(key.allowed_origins)}`);
166
+ if (key.kind === "secret") console.log(` scopes: ${asArray(key.scopes).join(", ") || "none"}`);
154
167
  }
155
168
  return;
156
169
  }
157
170
  if (sub === "create") {
158
171
  const kind = args.shift();
159
- if (!KINDS.includes(kind)) fail("Say which kind: vatio keys create publishable|secret [--env NAME] [--label NAME] [--origin URL]...");
172
+ if (!KINDS.includes(kind)) {
173
+ fail("Say which kind: vatio keys create publishable|secret [--env NAME] [--label NAME] [--origin URL]... [--scope NAME]...");
174
+ }
160
175
  if (kind === "secret" && origins.length > 0) {
161
176
  fail("--origin only applies to a publishable key. A secret key is for your backend and has no origins.");
162
177
  }
163
- const payload = await client.createApiKey({
178
+ if (kind === "publishable" && scopes.length > 0) {
179
+ fail(`--scope only applies to a secret key (${SCOPES.join(", ")}).`);
180
+ }
181
+ const payload = await createKey(client, {
164
182
  kind,
165
183
  environment,
166
184
  label,
167
- allowedOrigins: kind === "publishable" ? origins : []
185
+ allowedOrigins: kind === "publishable" ? origins : [],
186
+ scopes: kind === "secret" && scopes.length > 0 ? scopes : null,
187
+ code,
188
+ recoveryCode
168
189
  });
169
190
  // For a script that hands the key straight to something else: the key or a
170
191
  // failed command, never a line of prose to grep it out of.
@@ -174,6 +195,7 @@ export async function keys(config, args) {
174
195
  }
175
196
  console.log(`Created a ${kind} key for ${environment} on workspace ${workspace}`);
176
197
  if (kind === "publishable") console.log(` origins: ${originsLine(payload.api_key?.allowed_origins)}`);
198
+ if (kind === "secret") console.log(` scopes: ${asArray(payload.api_key?.scopes).join(", ")}`);
177
199
  console.log("");
178
200
  // Shown in full exactly once: the platform stores a digest, so this is the
179
201
  // only moment the value exists anywhere the developer can copy it.
@@ -187,9 +209,16 @@ export async function keys(config, args) {
187
209
  console.log("");
188
210
  console.log(` VATIO_SERVER_KEY=${payload.key}`);
189
211
  console.log("");
190
- console.log("Try it (lists the templates your number can send):");
191
- console.log(` curl ${config.resolveBaseUrl()}/api/server/v1/${workspace}/templates \\`);
192
- console.log(' -H "Authorization: Bearer $VATIO_SERVER_KEY"');
212
+ // A first call the key's scopes allow, that changes nothing.
213
+ const granted = asArray(payload.api_key?.scopes);
214
+ const [what, path] = granted.includes("messages")
215
+ ? ["lists the templates your number can send", "templates"]
216
+ : granted.includes("team") ? ["lists the workspace's team", "team_members"] : [null, null];
217
+ if (path) {
218
+ console.log(`Try it (${what}):`);
219
+ console.log(` curl ${config.resolveBaseUrl()}/api/server/v1/${workspace}/${path} \\`);
220
+ console.log(' -H "Authorization: Bearer $VATIO_SERVER_KEY"');
221
+ }
193
222
  return;
194
223
  }
195
224
  if (sub === "origins") {
@@ -210,6 +239,39 @@ export async function keys(config, args) {
210
239
  fail(KEYS_USAGE);
211
240
  }
212
241
 
242
+ // A `team` key needs the owner's second factor, as `vatio team invite` does:
243
+ // from a terminal the code is asked for; a coding agent has none, so it fails
244
+ // with what to ask the person for.
245
+ async function createKey(client, { code, recoveryCode, ...fields }) {
246
+ const confirmation = {};
247
+ if (code) confirmation.code = code;
248
+ if (recoveryCode) confirmation.recovery_code = recoveryCode;
249
+ try {
250
+ return await client.createApiKey({ ...fields, confirmation }).catch(explainKeyError);
251
+ } catch (error) {
252
+ const needsCode = error?.status === 403 && error?.body?.error?.code === "reauthentication_required";
253
+ // explainKeyError let it through only when the terminal can answer it.
254
+ if (!needsCode || code || recoveryCode) throw error;
255
+ const typed = await askSecret(`${paint("green", "?")} Code from your authenticator app: `);
256
+ return await client.createApiKey({ ...fields, confirmation: { code: typed } }).catch(explainKeyError);
257
+ }
258
+ }
259
+
260
+ // The answers whose own words say what to do, which the generic 403 and 422
261
+ // lines would hide. Left to throw: everything else, and a missing code the
262
+ // terminal can still ask for.
263
+ const KEY_ERRORS_THAT_SAY_WHY = new Set(["owner_only", "invalid_confirmation", "invalid_scopes"]);
264
+
265
+ function explainKeyError(error) {
266
+ const body = error?.body ?? {};
267
+ const code = body.error?.code ?? body.error_key;
268
+ if (code === "reauthentication_required" && !(process.stdin.isTTY && /--code/.test(error.message))) {
269
+ fail(error.message);
270
+ }
271
+ if (KEY_ERRORS_THAT_SAY_WHY.has(code)) fail(body.error_description ?? error.message);
272
+ throw error;
273
+ }
274
+
213
275
  // A flag whose value is optional: `--flag`, `--flag VALUE` or `--flag=VALUE`.
214
276
  // Returns null when it is absent and "" when it is there with no value, so
215
277
  // the caller can tell those two apart. A following token that starts with `-`
package/lib/help.mjs CHANGED
@@ -162,9 +162,14 @@ the two you are in. Commands default as listed.
162
162
  otherwise the value everywhere. With --tests, the
163
163
  value preview and every test run use, like a staging URL
164
164
  keys list [--json] API keys of both kinds: publishable (for a page or
165
- app) and secret (for your backend)
165
+ app) and secret (for your backend, with its scopes)
166
166
  keys create publishable|secret [--env NAME] [--label NAME] [--origin URL]... [--json]
167
167
  --origin only for a publishable key
168
+ keys create secret --scope NAME... [--code CODE | --recovery-code CODE]
169
+ What the secret key may call: messages, instagram,
170
+ chats, team (repeatable). Without --scope, all but
171
+ team. team (add people to the team) is the owner's,
172
+ with a code from their authenticator app
168
173
  keys origins PREFIX URL...
169
174
  The origins a publishable key is accepted from
170
175
  (replaces the list)
@@ -203,10 +208,6 @@ the two you are in. Commands default as listed.
203
208
  whatsapp test-phones [--json]
204
209
  The phones verified in Try the agent, to send a
205
210
  preview message to
206
- whatsapp mine [NUMBER...|--clear]
207
- Your numbers: the business's own conversations,
208
- the only ones with access: owner tools. Needs
209
- no number connected; prints them without args
210
211
  instagram The same, for Instagram
211
212
 
212
213
  Everything else:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vatio-ai/cli",
3
- "version": "0.112.0",
3
+ "version": "0.114.0",
4
4
  "description": "Vatio CLI — deploy and manage Vatio agent workspaces",
5
5
  "keywords": [
6
6
  "vatio",