@vatio-ai/cli 0.58.1 → 0.60.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/bin/vatio.mjs CHANGED
@@ -18,9 +18,10 @@ import { chat } from "../lib/commands/chat.mjs";
18
18
  import { kb } from "../lib/commands/knowledge.mjs";
19
19
  import { business } from "../lib/commands/business.mjs";
20
20
  import { instagram, whatsapp } from "../lib/commands/channels.mjs";
21
- import { docs } from "../lib/commands/support-commands.mjs";
21
+ import { docs, feedback } from "../lib/commands/support-commands.mjs";
22
22
  import { evalCommand, flags } from "../lib/commands/improve.mjs";
23
23
  import { printUpdateNotice, startUpdateCheck } from "../lib/update-notice.mjs";
24
+ import { beginRun, isCodingAgent, reportRun, telemetryEnabled } from "../lib/telemetry.mjs";
24
25
 
25
26
  // Commands that existed and are gone, answered by name rather than as
26
27
  // unknown: removed from the product, or what npm does now.
@@ -39,16 +40,19 @@ const RETIRED = {
39
40
  "`npm install -g @vatio-ai/cli@latest` updates a global install."
40
41
  };
41
42
 
43
+ const config = new Config({ startDir: process.cwd() });
44
+
42
45
  async function main(argv) {
43
46
  const args = argv.slice(2);
44
47
  const command = args.shift();
48
+ beginRun(command, args);
45
49
  const updateCheck = startUpdateCheck(command);
46
50
  await run(command, args);
51
+ await reportRun(config, { exitCode: process.exitCode ?? 0 });
47
52
  await printUpdateNotice(updateCheck);
48
53
  }
49
54
 
50
55
  async function run(command, args) {
51
- const config = new Config({ startDir: process.cwd() });
52
56
 
53
57
  switch (command) {
54
58
  case "version":
@@ -99,6 +103,8 @@ async function run(command, args) {
99
103
  return await evalCommand(config, args);
100
104
  case "docs":
101
105
  return await docs(config, args);
106
+ case "feedback":
107
+ return await feedback(config, args);
102
108
  case "help":
103
109
  case "-h":
104
110
  case "--help":
@@ -111,35 +117,58 @@ async function run(command, args) {
111
117
  }
112
118
 
113
119
 
114
- main(process.argv).catch((error) => {
120
+ main(process.argv).catch(async (error) => {
121
+ printError(error);
122
+ await reportRun(config, { exitCode: 1, error });
123
+ process.exit(1);
124
+ });
125
+
126
+ function printError(error) {
115
127
  if (error instanceof UserError || error instanceof ConfigError) {
116
128
  console.error(error.message);
117
- process.exit(1);
129
+ return suggestFeedback();
118
130
  }
119
131
  if (error instanceof UnauthorizedError) {
120
132
  console.error("Authentication failed (401). Run `vatio login`.");
121
- if (error.requestId) console.error(`request_id=${error.requestId}`);
122
- process.exit(1);
133
+ return printTrailer(error);
123
134
  }
124
135
  if (error instanceof ForbiddenError) {
125
136
  console.error("Access denied (403). Your token cannot access this workspace.");
126
- if (error.requestId) console.error(`request_id=${error.requestId}`);
127
- process.exit(1);
137
+ return printTrailer(error);
128
138
  }
129
139
  if (error instanceof NotFoundError) {
130
140
  console.error(
131
141
  "Remote resource not found (404). Check the workspace slug; `vatio push` creates a missing remote automatically."
132
142
  );
133
- if (error.requestId) console.error(`request_id=${error.requestId}`);
134
- process.exit(1);
143
+ return printTrailer(error);
135
144
  }
136
145
  if (error instanceof HttpError) {
137
146
  console.error(error.message);
138
- if (error.requestId) console.error(`request_id=${error.requestId}`);
139
- process.exit(1);
147
+ return printTrailer(error);
140
148
  }
141
149
  // Anything left is a bug in this CLI rather than something the developer
142
150
  // did, so it keeps its stack: that is what makes it reportable.
143
151
  console.error(error?.stack ?? String(error));
144
- process.exit(1);
145
- });
152
+ suggestFeedback();
153
+ }
154
+
155
+ // What the platform said to do next, and the ids that find this request in
156
+ // its logs.
157
+ function printTrailer(error) {
158
+ const hint = error.body?.hint;
159
+ if (hint && !(error instanceof UnauthorizedError)) console.error(`Next: ${hint}`);
160
+
161
+ const code = error.body?.error_key ?? (typeof error.body?.error === "string" ? error.body.error : error.body?.error?.code);
162
+ const ids = [code && `error=${code}`, error.requestId && `request_id=${error.requestId}`].filter(Boolean);
163
+ if (ids.length > 0) console.error(ids.join(" "));
164
+ suggestFeedback();
165
+ }
166
+
167
+ // Said to coding agents only: a person at a terminal has better ways to
168
+ // complain, and does not need the line on every typo.
169
+ function suggestFeedback() {
170
+ if (!telemetryEnabled() || !isCodingAgent() || process.argv[2] === "feedback") return;
171
+ console.error(
172
+ '\nIf this has you stuck, tell the Vatio team: vatio feedback "what you were trying to do and what got in the way"'
173
+ );
174
+ }
@@ -1,64 +1,83 @@
1
- // login / logout, and the device-code dance they share with `init`.
1
+ // login / logout, and the device-code dance they share with `push`.
2
2
 
3
- import { DeviceAuthClient, ExpiredError, PendingError } from "../device-auth.mjs";
3
+ import { DeniedError, DeviceAuthClient, ExpiredError, PendingError } from "../device-auth.mjs";
4
4
  import { DEFAULT_BASE_URL } from "../config.mjs";
5
- import { ask, fail, openBrowser, sleep, takeValue } from "../support.mjs";
5
+ import { fail, openBrowser, sleep, takeValue } from "../support.mjs";
6
6
 
7
- // A token is minted by a human approving it in a browser, never by the CLI
8
- // alone. The email turns that approval into a link we mail, which is what lets
9
- // somebody with no account finish it; typing nothing falls back to the URL.
10
- export async function deviceLogin({ baseUrl, message, email }) {
7
+ // A token is minted by a human signing in on the page this opens -- email,
8
+ // Google or GitHub -- never by the CLI alone, which is why it asks nothing
9
+ // itself: a coding agent running it has no one to ask. The code in flight is
10
+ // kept in the config file, so an agent whose shell gave up waiting picks the
11
+ // same page back up on its next run instead of starting over.
12
+ export async function deviceLogin({ config, baseUrl, message, workspace = null }) {
11
13
  const resolved = String(baseUrl || process.env.VATIO_BASE_URL || DEFAULT_BASE_URL).replace(/\/$/, "");
12
14
  const client = new DeviceAuthClient({ baseUrl: resolved });
13
15
 
14
16
  console.log(message);
15
- const address = String(email ?? (await ask("Your email (we send you a link): "))).trim();
16
-
17
- const auth = await client.start({ email: address });
18
- const deviceCode = auth.device_code;
19
- const userCode = auth.user_code;
20
- const verificationUri = auth.verification_uri_complete ?? auth.verification_uri;
21
- const interval = Number(auth.interval ?? 5);
22
- const expiresIn = Number(auth.expires_in ?? 900);
23
- if (!deviceCode || !verificationUri) fail("The platform did not return a device code");
17
+ let pending = config.pendingLogin();
18
+ const resumed = pending && pending.base_url === resolved && pending.expires_at > Date.now();
19
+ if (!resumed) pending = await startLogin({ config, client, resolved, workspace });
24
20
 
25
21
  console.log("");
26
- if (auth.email_sent) {
27
- console.log(`We emailed ${address} a link.`);
28
- console.log(`This terminal is ${userCode} — check it matches before you approve.`);
29
- } else {
30
- console.log(`Open: ${verificationUri}`);
31
- console.log(`Code: ${userCode}`);
32
- openBrowser(verificationUri);
33
- }
22
+ console.log(resumed ? "Still waiting for you to sign in here:" : "Sign in to Vatio here:");
23
+ console.log(` ${pending.url}`);
24
+ console.log(`This terminal is ${pending.user_code} — check the page shows the same code.`);
25
+ console.log("(A coding agent running this: if no browser opened, show this link to the person you work for.)");
26
+ if (!resumed) openBrowser(pending.url);
34
27
  console.log("");
35
- console.log("Waiting…");
28
+ console.log("Waiting… (if this gets cut off, run the same command again once you have signed in)");
36
29
 
37
- const deadline = Date.now() + expiresIn * 1000;
38
30
  for (;;) {
39
- if (Date.now() > deadline) fail("Timed out waiting for authorization");
31
+ if (Date.now() > pending.expires_at) {
32
+ config.clearPendingLogin();
33
+ fail("The link expired. Run the command again to get a new one.");
34
+ }
40
35
  try {
41
- const result = await client.poll({ deviceCode });
36
+ const result = await client.poll({ deviceCode: pending.device_code });
37
+ config.clearPendingLogin();
42
38
  return {
43
39
  token: result.token,
44
40
  baseUrl: String(result.base_url ?? "").trim() || resolved
45
41
  };
46
42
  } catch (error) {
47
43
  if (error instanceof PendingError) {
48
- await sleep(interval * 1000);
44
+ await sleep(pending.interval * 1000);
49
45
  continue;
50
46
  }
51
- if (error instanceof ExpiredError) fail("The code expired. Run `vatio login` again.");
47
+ if (error instanceof ExpiredError || error instanceof DeniedError) {
48
+ config.clearPendingLogin();
49
+ fail(error instanceof DeniedError
50
+ ? "The sign-in was turned down in the browser. Run the command again to get a new link."
51
+ : "The link expired. Run the command again to get a new one.");
52
+ }
52
53
  throw error;
53
54
  }
54
55
  }
55
56
  }
56
57
 
58
+ async function startLogin({ config, client, resolved, workspace }) {
59
+ const auth = await client.start({ workspace });
60
+ const url = auth.verification_uri_complete ?? auth.verification_uri;
61
+ if (!auth.device_code || !url) fail("The platform did not return a device code");
62
+
63
+ const pending = {
64
+ base_url: resolved,
65
+ url,
66
+ device_code: auth.device_code,
67
+ user_code: auth.user_code,
68
+ interval: Number(auth.interval ?? 5),
69
+ expires_at: Date.now() + Number(auth.expires_in ?? 900) * 1000
70
+ };
71
+ config.writePendingLogin(pending);
72
+ return pending;
73
+ }
74
+
57
75
  export async function login(config, args) {
58
76
  const baseUrl = takeValue(args, "--base-url");
59
77
  const { token, baseUrl: resolved } = await deviceLogin({
78
+ config,
60
79
  baseUrl,
61
- message: "Starting device authorization for Vatio CLI…"
80
+ message: "Signing this machine in to Vatio…"
62
81
  });
63
82
 
64
83
  config.writeAuth({ baseUrl: resolved, token });
@@ -23,10 +23,10 @@ import { detectGitSha, fail, paint, takeEnv, takeFlag, takeValue } from "../supp
23
23
 
24
24
  // The first command that makes something exist on a server, so the first one
25
25
  // that needs to know whose it is.
26
- export async function ensureAuthorized(config) {
26
+ export async function ensureAuthorized(config, { workspace = null } = {}) {
27
27
  if (config.resolveToken()) return;
28
28
 
29
- const auth = await deviceLogin({ message: "Before I can push this, I need to know whose it is." });
29
+ const auth = await deviceLogin({ config, workspace, message: "Before I can push this, I need to know whose it is." });
30
30
  config.writeAuth({ baseUrl: auth.baseUrl, token: auth.token });
31
31
  console.log(`Token saved to ${config.configPath}`);
32
32
  console.log("");
@@ -90,7 +90,7 @@ export async function push(config, args) {
90
90
  const environment = takeEnv(args, { fallback: null, cwd: config.workspaceRoot });
91
91
  const workspace = config.resolveWorkspaceRequired();
92
92
 
93
- await ensureAuthorized(config);
93
+ await ensureAuthorized(config, { workspace });
94
94
 
95
95
  // The remote has to exist before the workspace can be checked: the slug is in
96
96
  // the URL of the call that checks it. It is created unnamed; the display name
@@ -52,7 +52,6 @@ async function show(client, workspace, name) {
52
52
  ? ` deployment: #${row.deployment_id}${row.git_sha ? ` (${row.git_sha.slice(0, 7)})` : ""}`
53
53
  : " deployment: none yet — `vatio push`");
54
54
  if (row.share_url) console.log(` try it: ${row.share_url}`);
55
- if (row.page_url) console.log(` page with the bubble: ${row.page_url}`);
56
55
  if (row.knowledge_url) console.log(` knowledge diff: ${row.knowledge_url}`);
57
56
 
58
57
  if (row.token) {
@@ -8,6 +8,7 @@ import { mkdirSync, writeFileSync } from "node:fs";
8
8
  import { dirname, resolve } from "node:path";
9
9
 
10
10
  import { HttpClient } from "../http.mjs";
11
+ import { clientHeaders } from "../telemetry.mjs";
11
12
  import { fail, sleep, takeEnv, takeFlag, takeValue } from "../support.mjs";
12
13
 
13
14
  const POLL_MS = 3_000;
@@ -23,7 +24,7 @@ function workspaceBase(config) {
23
24
  async function fetchMarkdown(config, path, what) {
24
25
  const { url, token } = workspaceBase(config);
25
26
  const response = await fetch(`${url}${path}`, {
26
- headers: { Accept: "text/markdown, text/plain", Authorization: `Bearer ${token}` },
27
+ headers: { ...clientHeaders(), Accept: "text/markdown, text/plain", Authorization: `Bearer ${token}` },
27
28
  signal: AbortSignal.timeout(30_000)
28
29
  });
29
30
  if (!response.ok) fail(`Could not read ${what} (HTTP ${response.status})`);
@@ -1,9 +1,11 @@
1
- // docs — the command that is about Vatio rather than about a deployment.
1
+ // docs and feedback — the commands that are about Vatio rather than about a
2
+ // deployment.
2
3
 
3
4
  import { mkdirSync, writeFileSync } from "node:fs";
4
5
  import { dirname, resolve } from "node:path";
5
6
 
6
7
  import { fail, takeFlag, takeValue } from "../support.mjs";
8
+ import { clientHeaders, sendFeedback } from "../telemetry.mjs";
7
9
  import { DOCS_URL } from "./misc.mjs";
8
10
 
9
11
  // The whole developer contract as markdown, live from the platform. Fetched
@@ -17,7 +19,7 @@ export async function docs(config, args) {
17
19
  let markdown;
18
20
  try {
19
21
  const response = await fetch(`${baseUrl}/docs.md`, {
20
- headers: { Accept: "text/markdown, text/plain" },
22
+ headers: { ...clientHeaders(), Accept: "text/markdown, text/plain" },
21
23
  signal: AbortSignal.timeout(30_000)
22
24
  });
23
25
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
@@ -37,3 +39,30 @@ export async function docs(config, args) {
37
39
  console.log(`Wrote ${target} (${markdown.split("\n").length} lines, from ${baseUrl})`);
38
40
  console.log("Point your coding agent at it, and re-run `vatio docs --save` to refresh.");
39
41
  }
42
+
43
+ // What got in the way, in the words of whoever hit it -- usually a coding agent
44
+ // that just spent ten commands on something the docs should have said. It goes
45
+ // to the Vatio team with the commands that came before it in this session.
46
+ export async function feedback(config, args) {
47
+ let message = args.join(" ").trim();
48
+ if (message === "" && !process.stdin.isTTY) message = (await readStdin()).trim();
49
+ if (message === "") {
50
+ fail(
51
+ 'Say what you were trying to do and what got in the way:\n\n' +
52
+ ' vatio feedback "I ran push twice because the error did not say which field was wrong"'
53
+ );
54
+ }
55
+
56
+ try {
57
+ await sendFeedback(config, message);
58
+ } catch (error) {
59
+ fail(`Could not send it: ${error.message}. Open an issue at https://github.com/vatio-ai/cli/issues instead.`);
60
+ }
61
+ console.log("Sent to the Vatio team, with the commands this session ran before it. Thank you.");
62
+ }
63
+
64
+ async function readStdin() {
65
+ const chunks = [];
66
+ for await (const chunk of process.stdin) chunks.push(chunk);
67
+ return Buffer.concat(chunks).toString("utf8");
68
+ }
package/lib/config.mjs CHANGED
@@ -156,6 +156,24 @@ export class Config {
156
156
  return this.save(data);
157
157
  }
158
158
 
159
+ pendingLogin() {
160
+ const pending = this.load().pending_login;
161
+ return pending && typeof pending === "object" && pending.device_code ? pending : null;
162
+ }
163
+
164
+ writePendingLogin(pending) {
165
+ const data = this.load();
166
+ data.pending_login = pending;
167
+ return this.save(data);
168
+ }
169
+
170
+ clearPendingLogin() {
171
+ const data = this.load();
172
+ if (!Object.hasOwn(data, "pending_login")) return data;
173
+ delete data.pending_login;
174
+ return this.save(data);
175
+ }
176
+
159
177
  displayHash() {
160
178
  const out = {};
161
179
  const base = this.resolveBaseUrl();
@@ -5,6 +5,8 @@
5
5
  // Unauthenticated by definition -- this is what mints the token -- so it does
6
6
  // not go through HttpClient, which requires one.
7
7
 
8
+ import { clientHeaders } from "./telemetry.mjs";
9
+
8
10
  export class DeviceAuthError extends Error {}
9
11
  export class PendingError extends DeviceAuthError {}
10
12
  export class DeniedError extends DeviceAuthError {}
@@ -15,11 +17,11 @@ export class DeviceAuthClient {
15
17
  this.baseUrl = String(baseUrl ?? "").replace(/\/$/, "");
16
18
  }
17
19
 
18
- // With an `email` the platform mails the approval link; without one the
19
- // response is exactly what it always was.
20
- start({ email } = {}) {
20
+ // A `workspace` lets the page the developer signs in on wait for that push
21
+ // and land on its test screen.
22
+ start({ workspace } = {}) {
21
23
  const body = {};
22
- if (String(email ?? "").trim() !== "") body.email = String(email).trim();
24
+ if (workspace) body.workspace = workspace;
23
25
  return this.#post("/api/developer/v1/device_authorizations", body);
24
26
  }
25
27
 
@@ -33,7 +35,7 @@ export class DeviceAuthClient {
33
35
  try {
34
36
  response = await fetch(url, {
35
37
  method: "POST",
36
- headers: { "Content-Type": "application/json", Accept: "application/json" },
38
+ headers: { ...clientHeaders(), "Content-Type": "application/json", Accept: "application/json" },
37
39
  body: JSON.stringify(body),
38
40
  signal: AbortSignal.timeout(30_000)
39
41
  });
package/lib/help.mjs CHANGED
@@ -19,14 +19,17 @@ and the one its pull request deploys to. Elsewhere commands default as listed.
19
19
 
20
20
  Getting started:
21
21
  init [SLUG] [--name NAME] Create vatio.yml here, and the remote to match
22
- login [--base-url URL] Authorize this machine in a browser
22
+ login [--base-url URL] Sign this machine in from the browser: email link,
23
+ Google or GitHub. Creates the account if needed
23
24
  logout Forget the token
24
25
  doctor Node, config, workspace and token status
25
26
  version What this CLI is
26
27
  docs [--save [PATH]] The whole developer contract, live from Vatio
28
+ feedback "TEXT" Tell the Vatio team what got in the way
27
29
 
28
30
  Deploy:
29
31
  push [--env NAME] Validate the workspace, then update a preview
32
+ (the first one signs in like \`login\`)
30
33
  publish [--env NAME] Promote an environment to live, with its knowledge
31
34
  changes. Without --env or a branch, the latest
32
35
  push. Refused when GitHub is connected: merge
@@ -99,7 +102,8 @@ and the one its pull request deploys to. Elsewhere commands default as listed.
99
102
  config show|get KEY|set KEY VALUE|unset KEY
100
103
  Keys: base_url, token
101
104
 
102
- Environment: VATIO_BASE_URL, VATIO_TOKEN, VATIO_HOME, VATIO_NO_UPDATE_NOTIFIER
105
+ Environment: VATIO_BASE_URL, VATIO_TOKEN, VATIO_HOME, VATIO_NO_UPDATE_NOTIFIER,
106
+ VATIO_TELEMETRY=0 (see docs.vatio.ai/cli/workspace#telemetry)
103
107
 
104
108
  Docs: https://docs.vatio.ai
105
109
  `;
package/lib/http.mjs CHANGED
@@ -5,6 +5,8 @@
5
5
  // fetch() rather than a library: it has been in Node since 18, and this package
6
6
  // stays at one dependency (yaml, for the one file a workspace still parses).
7
7
 
8
+ import { clientHeaders } from "./telemetry.mjs";
9
+
8
10
  export class HttpError extends Error {
9
11
  constructor(message, { status = null, requestId = null, body = {} } = {}) {
10
12
  super(message);
@@ -85,6 +87,7 @@ export class HttpClient {
85
87
  async #perform(method, path, body) {
86
88
  const url = `${this.baseUrl}${path}`;
87
89
  const headers = {
90
+ ...clientHeaders(),
88
91
  Authorization: `Bearer ${this.token}`,
89
92
  Accept: "application/json"
90
93
  };
package/lib/support.mjs CHANGED
@@ -100,6 +100,12 @@ export function detectGitSha(cwd) {
100
100
  }
101
101
  }
102
102
 
103
+ export function sleep(ms) {
104
+ return new Promise((resolve) => setTimeout(resolve, ms));
105
+ }
106
+
107
+ // Works without a terminal too: a coding agent running on the developer's own
108
+ // machine still opens their browser.
103
109
  export function openBrowser(url) {
104
110
  if (!url) return;
105
111
  const command =
@@ -114,12 +120,8 @@ export function openBrowser(url) {
114
120
  }
115
121
  }
116
122
 
117
- export function sleep(ms) {
118
- return new Promise((resolve) => setTimeout(resolve, ms));
119
- }
120
-
121
123
  // Returns "" with nothing attached to stdin, so an agent running `vatio push`
122
- // in a pipeline falls back to the URL instead of hanging on a prompt.
124
+ // in a pipeline never hangs on a prompt.
123
125
  export async function ask(question) {
124
126
  if (!process.stdin.isTTY) return "";
125
127
 
@@ -0,0 +1,196 @@
1
+ // What the CLI tells Vatio about how it is being used. The reader of this CLI
2
+ // is usually a coding agent, and the only way to learn where it gets stuck is
3
+ // to see the commands it runs, in order, and the errors it is shown.
4
+ //
5
+ // Every request says which CLI made it, which coding agent is driving it, the
6
+ // session it belongs to and the command it is part of. When a command ends,
7
+ // one event says how: the exit code and, on failure, what was printed. Never
8
+ // sent: arguments, file contents, secrets, or what a chat said.
9
+ //
10
+ // VATIO_TELEMETRY=0 (or DO_NOT_TRACK=1) turns all of it off except the
11
+ // User-Agent, which only says which version is calling.
12
+
13
+ import { randomUUID } from "node:crypto";
14
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
15
+ import { dirname, join } from "node:path";
16
+ import { arch, homedir, platform } from "node:os";
17
+
18
+ import { configHome } from "./config.mjs";
19
+ import { VERSION } from "./version.mjs";
20
+
21
+ const SESSION_IDLE_MS = 30 * 60 * 1000;
22
+ const REPORT_TIMEOUT_MS = 1_500;
23
+ const MAX_MESSAGE = 2_000;
24
+
25
+ // Only words a command takes as its first argument are kept: anything else
26
+ // there is a value (`vatio chat "hola"`), and values are not sent.
27
+ const SUBCOMMANDS = {
28
+ chat: ["show"],
29
+ env: ["list", "rm"],
30
+ kb: ["list", "show", "create", "rm", "write", "cat", "rm-entry", "status", "publish", "history"],
31
+ business: ["show", "set", "hours"],
32
+ secrets: ["list", "set", "rm"],
33
+ tokens: ["list", "create", "revoke", "origins"],
34
+ config: ["show", "get", "set", "unset"]
35
+ };
36
+
37
+ export const USER_AGENT = `vatio-cli/${VERSION} (node ${process.version}; ${platform()} ${arch()})`;
38
+
39
+ const run = { command: null };
40
+
41
+ export function telemetryEnabled() {
42
+ const off = ["0", "false", "off", "no"];
43
+ if (off.includes(String(process.env.VATIO_TELEMETRY ?? "").trim().toLowerCase())) return false;
44
+ return !["1", "true"].includes(String(process.env.DO_NOT_TRACK ?? "").trim().toLowerCase());
45
+ }
46
+
47
+ // The coding agent running this CLI, from what each one sets in the
48
+ // environment of the commands it runs. VATIO_AGENT names one we do not know.
49
+ export function codingAgent() {
50
+ const env = process.env;
51
+ if (env.VATIO_AGENT) return String(env.VATIO_AGENT).slice(0, 64);
52
+ if (env.CLAUDECODE) return "claude-code";
53
+ if (env.CURSOR_AGENT || env.CURSOR_TRACE_ID) return "cursor";
54
+ if (env.CODEX_SANDBOX || env.CODEX_MANAGED_BY_NPM) return "codex";
55
+ if (env.GEMINI_CLI) return "gemini-cli";
56
+ if (env.CI) return "ci";
57
+ return process.stdout.isTTY ? "human" : "unknown";
58
+ }
59
+
60
+ export function isCodingAgent() {
61
+ return !["human", "ci"].includes(codingAgent());
62
+ }
63
+
64
+ export function beginRun(command, args) {
65
+ run.command = commandLabel(command, args);
66
+ noticeOnce();
67
+ }
68
+
69
+ // Said once per machine, on stderr, before anything is sent: a coding agent
70
+ // reads it there and can pass it on, which a prompt could never do -- nobody
71
+ // is at the terminal to answer one.
72
+ function noticeOnce() {
73
+ if (!telemetryEnabled()) return;
74
+
75
+ const path = join(configHome(), "telemetry-notice");
76
+ if (existsSync(path)) return;
77
+ process.stderr.write(
78
+ "vatio: the CLI tells Vatio how each command ended (never arguments, files or secrets),\n" +
79
+ "so we can see where coding agents get stuck. Turn it off with VATIO_TELEMETRY=0.\n" +
80
+ "Details: https://docs.vatio.ai/cli/workspace#telemetry\n\n"
81
+ );
82
+ safely(() => {
83
+ mkdirSync(dirname(path), { recursive: true });
84
+ writeFileSync(path, `${new Date().toISOString()}\n`);
85
+ });
86
+ }
87
+
88
+ export function commandLabel(command, args = []) {
89
+ if (!command) return "help";
90
+ const sub = args[0];
91
+ return SUBCOMMANDS[command]?.includes(sub) ? `${command} ${sub}` : command;
92
+ }
93
+
94
+ export function clientHeaders() {
95
+ const headers = { "User-Agent": USER_AGENT };
96
+ if (!telemetryEnabled()) return headers;
97
+
98
+ headers["X-Vatio-Session"] = sessionId();
99
+ headers["X-Vatio-Agent"] = codingAgent();
100
+ if (run.command) headers["X-Vatio-Command"] = run.command;
101
+ return headers;
102
+ }
103
+
104
+ // How the command ended. Awaited, but never for long and never to the point
105
+ // of failing: a CLI that hangs or errors because telemetry could not be sent
106
+ // would be worse than not knowing.
107
+ export async function reportRun(config, { exitCode, error = null }) {
108
+ if (!telemetryEnabled() || !run.command || run.command === "feedback") return;
109
+
110
+ await post(config, {
111
+ kind: "run",
112
+ command: run.command,
113
+ exit_code: exitCode,
114
+ error: error ? describeError(error) : undefined
115
+ }).catch(() => {});
116
+ }
117
+
118
+ // `vatio feedback`: sent even with telemetry off, because running it is the
119
+ // developer (or their agent) choosing to tell us something.
120
+ export async function sendFeedback(config, message) {
121
+ await post(config, { kind: "feedback", command: "feedback", message: message.slice(0, MAX_MESSAGE) }, { strict: true });
122
+ }
123
+
124
+ async function post(config, body, { strict = false } = {}) {
125
+ const token = safely(() => config.resolveToken());
126
+ const headers = { ...clientHeaders(), "Content-Type": "application/json", Accept: "application/json" };
127
+ if (strict) {
128
+ headers["X-Vatio-Session"] ??= sessionId();
129
+ headers["X-Vatio-Agent"] ??= codingAgent();
130
+ }
131
+ if (token) headers.Authorization = `Bearer ${token}`;
132
+
133
+ const response = await fetch(`${config.resolveBaseUrl()}/api/developer/v1/cli/events`, {
134
+ method: "POST",
135
+ headers,
136
+ body: JSON.stringify({
137
+ ...body,
138
+ workspace: safely(() => config.resolveWorkspace())
139
+ }),
140
+ signal: AbortSignal.timeout(strict ? 10_000 : REPORT_TIMEOUT_MS)
141
+ });
142
+ if (strict && !response.ok) throw new Error(`HTTP ${response.status}`);
143
+ }
144
+
145
+ function describeError(error) {
146
+ const body = error?.body && typeof error.body === "object" ? error.body : {};
147
+ const code = typeof body.error === "string" ? body.error : body.error?.code;
148
+ // A bug in the CLI keeps the top of its stack: that is where it happened.
149
+ // Everything else is a sentence the CLI chose to print, and that is the
150
+ // sentence worth knowing.
151
+ const expected = ["UserError", "ConfigError"].includes(error?.name) || (error && "requestId" in error);
152
+ const message = expected
153
+ ? String(error.message ?? "")
154
+ : String(error?.stack ?? error).split("\n").slice(0, 6).join("\n");
155
+
156
+ return {
157
+ class: error?.constructor?.name ?? "Error",
158
+ key: body.error_key ?? code ?? undefined,
159
+ message: message.replaceAll(homedir(), "~").slice(0, MAX_MESSAGE),
160
+ request_id: error?.requestId ?? undefined
161
+ };
162
+ }
163
+
164
+ // One session is one stretch of work in one directory: a new id after half an
165
+ // hour without a command, so an agent coming back tomorrow is a new session,
166
+ // and two agents in two projects are two.
167
+ function sessionId() {
168
+ if (run.sessionId) return run.sessionId;
169
+
170
+ const path = join(configHome(), "sessions.json");
171
+ const key = process.cwd();
172
+ const sessions = safely(() => JSON.parse(readFileSync(path, "utf8"))) ?? {};
173
+ const now = Date.now();
174
+ const current = sessions[key];
175
+ const id = current && now - Date.parse(current.last_used_at) < SESSION_IDLE_MS ? current.id : randomUUID();
176
+
177
+ for (const [dir, session] of Object.entries(sessions)) {
178
+ if (now - Date.parse(session.last_used_at) > SESSION_IDLE_MS) delete sessions[dir];
179
+ }
180
+ sessions[key] = { id, last_used_at: new Date(now).toISOString() };
181
+ safely(() => {
182
+ mkdirSync(dirname(path), { recursive: true });
183
+ writeFileSync(path, JSON.stringify(sessions));
184
+ });
185
+
186
+ run.sessionId = id;
187
+ return id;
188
+ }
189
+
190
+ function safely(fn) {
191
+ try {
192
+ return fn();
193
+ } catch {
194
+ return null;
195
+ }
196
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vatio-ai/cli",
3
- "version": "0.58.1",
3
+ "version": "0.60.0",
4
4
  "description": "Vatio CLI — deploy and manage Vatio agent workspaces",
5
5
  "keywords": [
6
6
  "vatio",