@octen.ai/cli 0.7.0 → 0.9.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.
Files changed (52) hide show
  1. package/README.md +325 -4
  2. package/dist/api/chat.d.ts +4 -1
  3. package/dist/api/client.d.ts +25 -1
  4. package/dist/api/client.js +144 -17
  5. package/dist/api/errors.d.ts +27 -0
  6. package/dist/api/errors.js +52 -0
  7. package/dist/api/search.js +5 -2
  8. package/dist/api/sse.d.ts +15 -3
  9. package/dist/api/sse.js +131 -36
  10. package/dist/auth/constants.d.ts +47 -0
  11. package/dist/auth/constants.js +89 -0
  12. package/dist/auth/exchange.d.ts +74 -0
  13. package/dist/auth/exchange.js +124 -0
  14. package/dist/auth/login.d.ts +64 -0
  15. package/dist/auth/login.js +163 -0
  16. package/dist/auth/loopback.d.ts +32 -0
  17. package/dist/auth/loopback.js +95 -0
  18. package/dist/auth/oauthClient.d.ts +70 -0
  19. package/dist/auth/oauthClient.js +136 -0
  20. package/dist/auth/pkce.d.ts +6 -0
  21. package/dist/auth/pkce.js +13 -0
  22. package/dist/auth/revoke.d.ts +58 -0
  23. package/dist/auth/revoke.js +91 -0
  24. package/dist/auth/store.d.ts +34 -0
  25. package/dist/auth/store.js +149 -0
  26. package/dist/cli.js +6 -0
  27. package/dist/commands/broadSearch.js +6 -6
  28. package/dist/commands/chat.js +12 -8
  29. package/dist/commands/configureMcp.js +38 -20
  30. package/dist/commands/configureSkills.js +25 -10
  31. package/dist/commands/imageSearch.js +3 -3
  32. package/dist/commands/login.d.ts +49 -0
  33. package/dist/commands/login.js +161 -0
  34. package/dist/commands/logout.d.ts +46 -0
  35. package/dist/commands/logout.js +142 -0
  36. package/dist/commands/reset.js +43 -5
  37. package/dist/commands/search.js +6 -6
  38. package/dist/commands/utils.d.ts +21 -2
  39. package/dist/commands/utils.js +43 -6
  40. package/dist/commands/whoami.d.ts +27 -0
  41. package/dist/commands/whoami.js +109 -0
  42. package/dist/config/resolve.d.ts +44 -1
  43. package/dist/config/resolve.js +80 -5
  44. package/dist/mcp/clients.js +7 -3
  45. package/dist/mcp/install.js +15 -2
  46. package/dist/mcp/write.js +24 -3
  47. package/dist/output/pretty/whoami.d.ts +59 -0
  48. package/dist/output/pretty/whoami.js +103 -0
  49. package/dist/skills/setkey.js +5 -4
  50. package/dist/util/secretFile.d.ts +24 -0
  51. package/dist/util/secretFile.js +35 -0
  52. package/package.json +1 -1
@@ -0,0 +1,103 @@
1
+ /**
2
+ * The one wording for "there is no local credential file". Two paths render
3
+ * it: pretty mode throws it as an OctenAuthError from src/commands/whoami.ts
4
+ * (to get exit code 2), json mode emits the structured payload. Spelling it
5
+ * twice is how the two drift, so both go through `notLoggedInMessage`.
6
+ */
7
+ export const NOT_LOGGED_IN_MESSAGE = "Not logged in (no local credential file). Run `octen login`, or `octen login --api-key <key>`.";
8
+ /**
9
+ * The not-logged-in message, plus — when a flag or env var is supplying a key
10
+ * regardless — the note that commands still work. Saying only "not logged in"
11
+ * to someone whose `OCTEN_API_KEY` is set describes a state they are not in.
12
+ */
13
+ export function notLoggedInMessage(effectiveSource) {
14
+ return effectiveSource === "OCTEN_API_KEY" || effectiveSource === "--api-key"
15
+ ? `${NOT_LOGGED_IN_MESSAGE} (${effectiveSource} is set, so commands use that key.)`
16
+ : NOT_LOGGED_IN_MESSAGE;
17
+ }
18
+ function formatRemaining(seconds) {
19
+ if (seconds <= 0)
20
+ return "expired";
21
+ const days = Math.floor(seconds / 86400);
22
+ const hours = Math.floor((seconds % 86400) / 3600);
23
+ const minutes = Math.floor((seconds % 3600) / 60);
24
+ if (days > 0)
25
+ return `${days}d ${hours}h`;
26
+ if (hours > 0)
27
+ return `${hours}h ${minutes}m`;
28
+ return `${minutes}m`;
29
+ }
30
+ /**
31
+ * The "In effect:" line — the one `whoami` was missing. Before this, `whoami`
32
+ * described the file's credential as live while `octen search` refused to use
33
+ * it, in the same second on the same machine. Every branch names both the
34
+ * cause and the way out.
35
+ */
36
+ function effectLine(data, stored = {}) {
37
+ if (data.inEffect)
38
+ return "In effect: yes — commands use this stored credential.";
39
+ switch (data.ignoredReason) {
40
+ case "flag-shadowed":
41
+ return ("In effect: no — --api-key was passed on this command line and takes precedence, " +
42
+ "so that key is used instead of this stored credential.");
43
+ case "env-shadowed":
44
+ return ("In effect: no — OCTEN_API_KEY is set in the environment and takes precedence, " +
45
+ "so commands use that key, not this stored credential. Unset OCTEN_API_KEY to use it.");
46
+ case "issuer-mismatch":
47
+ return (`In effect: no — this credential was issued for issuer ${stored.issuer}, ` +
48
+ `but OCTEN_AUTH_ISSUER selects ${data.expectedIssuer}, so every command ignores it. ` +
49
+ "Unset OCTEN_AUTH_ISSUER, or run `octen login` again.");
50
+ case "resource-mismatch":
51
+ return (`In effect: no — this credential was issued for resource ${stored.resource}, ` +
52
+ `but OCTEN_AUTH_RESOURCE selects ${data.expectedResource}, so every command ignores it. ` +
53
+ "Unset OCTEN_AUTH_RESOURCE, or run `octen login` again.");
54
+ case "expired":
55
+ return "In effect: no — this credential has expired. Run `octen login` again.";
56
+ default:
57
+ return "In effect: no.";
58
+ }
59
+ }
60
+ /**
61
+ * Pretty-print `octen whoami` output. Reflects only the local credentials
62
+ * file — every line here is derived from disk, never from a request, and the
63
+ * trailing note says so explicitly so a reader never mistakes this for
64
+ * confirmation that the key still works server-side.
65
+ */
66
+ export function renderWhoami(data) {
67
+ const lines = [];
68
+ if (!data.loggedIn) {
69
+ // Reachable in both modes now. This used to be dead code because pretty
70
+ // mode threw instead of rendering; whoami stopped throwing because "not
71
+ // logged in" is a status this command reports, not a failure — the
72
+ // non-zero exit code carries that signal. The comment that used to sit
73
+ // here predicted exactly this: the wording is shared with the json path
74
+ // through `notLoggedInMessage` so the two cannot drift, and that is what
75
+ // made the change a one-liner rather than a second message nothing tests.
76
+ return notLoggedInMessage(data.effectiveSource);
77
+ }
78
+ if (data.source === "api-key") {
79
+ lines.push("Source: api-key (set via `octen login --api-key` or a manually-configured key)");
80
+ lines.push(effectLine(data));
81
+ lines.push("No account or grant information is available for this credential — it was not created via the browser login flow.");
82
+ }
83
+ else {
84
+ const who = data.accountId
85
+ ? `${data.accountId}${data.accountType ? ` (${data.accountType})` : ""}`
86
+ : "(account id unknown)";
87
+ lines.push(`Account: ${who}`);
88
+ lines.push("Source: login (browser OAuth)");
89
+ lines.push(effectLine(data, { issuer: data.issuer, resource: data.resource }));
90
+ lines.push(`Issuer: ${data.issuer}`);
91
+ lines.push(`Resource: ${data.resource}`);
92
+ lines.push(`Grant: ${data.grantId} — find and revoke this device in the dashboard's authorization list.`);
93
+ if (data.apiKeyExpiresAt !== null) {
94
+ const remaining = data.apiKeyExpiresAt - Math.floor(Date.now() / 1000);
95
+ lines.push(`API key expires in: ${formatRemaining(remaining)}`);
96
+ }
97
+ else {
98
+ lines.push("API key: does not expire");
99
+ }
100
+ }
101
+ lines.push("(Read from the local credentials file only — no network request was made.)");
102
+ return lines.join("\n");
103
+ }
@@ -1,4 +1,5 @@
1
- import { mkdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
1
+ import { mkdirSync, readFileSync, existsSync } from "node:fs";
2
+ import { writeSecretFile } from "../util/secretFile.js";
2
3
  import { dirname, join } from "node:path";
3
4
  import { parse, stringify } from "smol-toml";
4
5
  import { OctenValidationError } from "../api/errors.js";
@@ -47,7 +48,7 @@ export function setClientEnvKey(clientId, key, scope, home, cwd) {
47
48
  OCTEN_API_KEY: key,
48
49
  };
49
50
  mkdirSync(dirname(path), { recursive: true });
50
- writeFileSync(path, JSON.stringify(obj, null, 2) + "\n", "utf8");
51
+ writeSecretFile(path, JSON.stringify(obj, null, 2) + "\n");
51
52
  return { clientId, written: true, path };
52
53
  }
53
54
  case "codex": {
@@ -59,7 +60,7 @@ export function setClientEnvKey(clientId, key, scope, home, cwd) {
59
60
  OCTEN_API_KEY: key,
60
61
  };
61
62
  mkdirSync(dirname(path), { recursive: true });
62
- writeFileSync(path, stringify(obj), "utf8");
63
+ writeSecretFile(path, stringify(obj));
63
64
  return { clientId, written: true, path };
64
65
  }
65
66
  case "openclaw": {
@@ -77,7 +78,7 @@ export function setClientEnvKey(clientId, key, scope, home, cwd) {
77
78
  if (!replaced)
78
79
  lines.push(`OCTEN_API_KEY=${key}`);
79
80
  mkdirSync(dirname(path), { recursive: true });
80
- writeFileSync(path, lines.join("\n") + "\n", "utf8");
81
+ writeSecretFile(path, lines.join("\n") + "\n");
81
82
  return { clientId, written: true, path };
82
83
  }
83
84
  default:
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Write a file that contains an API key, restricted to 0600.
3
+ *
4
+ * Two callers need this and they must not drift: `mcp/write.ts` (the MCP
5
+ * client configs) and `skills/setkey.ts` (the per-client env files). Both used
6
+ * to call `writeFileSync(path, data, "utf8")` with no mode, i.e. 0644 under the
7
+ * default umask.
8
+ *
9
+ * That was tolerable while the only key that could reach them was one the user
10
+ * had already exposed in argv (`--api-key`) or the environment
11
+ * (`OCTEN_API_KEY`). It stopped being tolerable once credential resolution
12
+ * learned to read `~/.octen/credentials.json`: a key that exists nowhere but a
13
+ * 0600 file now flows into these files, and writing it 0644 undoes the store's
14
+ * whole point.
15
+ *
16
+ * The explicit `chmodSync` is load-bearing, not belt-and-braces: `writeFileSync`
17
+ * applies `mode` only when it *creates* the file, and these functions merge into
18
+ * configs that almost always already exist — an existing 0644 file would keep
19
+ * its mode without it.
20
+ *
21
+ * chmod is a no-op on Windows. That is the same limitation the credential store
22
+ * already has, and it is a platform property, not something this can fix.
23
+ */
24
+ export declare function writeSecretFile(filePath: string, data: string): void;
@@ -0,0 +1,35 @@
1
+ import { writeFileSync, chmodSync } from "node:fs";
2
+ /**
3
+ * Write a file that contains an API key, restricted to 0600.
4
+ *
5
+ * Two callers need this and they must not drift: `mcp/write.ts` (the MCP
6
+ * client configs) and `skills/setkey.ts` (the per-client env files). Both used
7
+ * to call `writeFileSync(path, data, "utf8")` with no mode, i.e. 0644 under the
8
+ * default umask.
9
+ *
10
+ * That was tolerable while the only key that could reach them was one the user
11
+ * had already exposed in argv (`--api-key`) or the environment
12
+ * (`OCTEN_API_KEY`). It stopped being tolerable once credential resolution
13
+ * learned to read `~/.octen/credentials.json`: a key that exists nowhere but a
14
+ * 0600 file now flows into these files, and writing it 0644 undoes the store's
15
+ * whole point.
16
+ *
17
+ * The explicit `chmodSync` is load-bearing, not belt-and-braces: `writeFileSync`
18
+ * applies `mode` only when it *creates* the file, and these functions merge into
19
+ * configs that almost always already exist — an existing 0644 file would keep
20
+ * its mode without it.
21
+ *
22
+ * chmod is a no-op on Windows. That is the same limitation the credential store
23
+ * already has, and it is a platform property, not something this can fix.
24
+ */
25
+ export function writeSecretFile(filePath, data) {
26
+ writeFileSync(filePath, data, { encoding: "utf8", mode: 0o600 });
27
+ try {
28
+ chmodSync(filePath, 0o600);
29
+ }
30
+ catch {
31
+ // Best effort. A filesystem that refuses chmod must not fail the install —
32
+ // the content itself was written successfully, and failing here would leave
33
+ // the user with a half-configured client and no way forward.
34
+ }
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octen.ai/cli",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "Command-line tool for Octen — web search, extract, chat, embeddings, and one-command MCP/Skills setup.",
5
5
  "type": "module",
6
6
  "license": "MIT",