@agledger/cli 2.0.0 → 2.2.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 (48) hide show
  1. package/README.md +77 -25
  2. package/SKILL.md +15 -14
  3. package/bin/run.js +2 -2
  4. package/dist/api-client.d.ts +7 -0
  5. package/dist/api-client.js +44 -8
  6. package/dist/base.d.ts +97 -12
  7. package/dist/base.js +90 -37
  8. package/dist/cli/command.d.ts +58 -0
  9. package/dist/cli/command.js +44 -0
  10. package/dist/cli/flags.d.ts +76 -0
  11. package/dist/cli/flags.js +12 -0
  12. package/dist/cli/help.d.ts +6 -0
  13. package/dist/cli/help.js +107 -0
  14. package/dist/cli/index.d.ts +3 -0
  15. package/dist/cli/index.js +3 -0
  16. package/dist/cli/main.d.ts +22 -0
  17. package/dist/cli/main.js +223 -0
  18. package/dist/cli/parse.d.ts +20 -0
  19. package/dist/cli/parse.js +138 -0
  20. package/dist/commands/api.d.ts +106 -18
  21. package/dist/commands/api.js +103 -12
  22. package/dist/commands/auth.d.ts +35 -6
  23. package/dist/commands/auth.js +1 -1
  24. package/dist/commands/config.d.ts +43 -8
  25. package/dist/commands/config.js +9 -8
  26. package/dist/commands/discover.d.ts +35 -6
  27. package/dist/commands/docs.d.ts +41 -7
  28. package/dist/commands/docs.js +4 -4
  29. package/dist/commands/help-json.d.ts +39 -7
  30. package/dist/commands/help-json.js +27 -45
  31. package/dist/commands/list-commands.d.ts +36 -7
  32. package/dist/commands/list-commands.js +2 -2
  33. package/dist/commands/login.d.ts +66 -10
  34. package/dist/commands/login.js +30 -9
  35. package/dist/commands/logout.d.ts +41 -7
  36. package/dist/commands/logout.js +8 -8
  37. package/dist/commands/verify.d.ts +131 -11
  38. package/dist/commands/verify.js +214 -25
  39. package/dist/oidc.d.ts +47 -0
  40. package/dist/oidc.js +162 -3
  41. package/dist/util/config.d.ts +16 -1
  42. package/dist/util/config.js +50 -14
  43. package/dist/util/field-parser.d.ts +3 -1
  44. package/dist/util/field-parser.js +11 -5
  45. package/package.json +9 -21
  46. package/dist/hooks/command-not-found.d.ts +0 -3
  47. package/dist/hooks/command-not-found.js +0 -63
  48. package/oclif.manifest.json +0 -857
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Profile storage at ~/.agledger/config.json. Credential-sensitive: directory 0700,
3
- * file 0600, chmodSync belt-and-braces for pre-existing files.
3
+ * file 0600, both enforced on every write, not only when they are created.
4
4
  */
5
5
  /**
6
6
  * Where `agledger login --oidc` finds an OIDC token. The source is stored,
@@ -11,6 +11,8 @@ export interface OidcProfileSource {
11
11
  tokenCommand?: string;
12
12
  tokenFile?: string;
13
13
  agentId?: string;
14
+ /** A JWK Set file each cert exchange adds its public key to (absolute). */
15
+ agentKeysFile?: string;
14
16
  }
15
17
  /** A profile holds an API key or an OIDC token source. */
16
18
  export interface Profile {
@@ -24,5 +26,18 @@ export interface Config {
24
26
  }
25
27
  export declare function configDir(): string;
26
28
  export declare function configPath(): string;
29
+ /** The config file exists but cannot be read as a profile store. Never replaced silently. */
30
+ export declare class ConfigFileError extends Error {
31
+ readonly path: string;
32
+ constructor(message: string, path: string);
33
+ }
34
+ /** A profile by name, own entries only: `toString` and `__proto__` name no profile. */
35
+ export declare function getProfile(config: Config, name: string): Profile | undefined;
36
+ /**
37
+ * Read the stored profiles. A missing file is an empty store. A file that is
38
+ * not valid JSON, or not the profile shape, throws: returning an empty store
39
+ * there let the next `login` overwrite the file and lose every profile.
40
+ */
27
41
  export declare function readConfig(): Config;
42
+ /** Write the store through a temp file in the same directory and a rename, so a crash never leaves half a file. */
28
43
  export declare function writeConfig(config: Config): void;
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Profile storage at ~/.agledger/config.json. Credential-sensitive: directory 0700,
3
- * file 0600, chmodSync belt-and-braces for pre-existing files.
3
+ * file 0600, both enforced on every write, not only when they are created.
4
4
  */
5
- import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
5
+ import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
6
6
  import { join } from 'node:path';
7
7
  import { homedir } from 'node:os';
8
8
  export function configDir() {
@@ -11,26 +11,62 @@ export function configDir() {
11
11
  export function configPath() {
12
12
  return join(configDir(), 'config.json');
13
13
  }
14
+ /** The config file exists but cannot be read as a profile store. Never replaced silently. */
15
+ export class ConfigFileError extends Error {
16
+ path;
17
+ constructor(message, path) {
18
+ super(message);
19
+ this.path = path;
20
+ this.name = 'ConfigFileError';
21
+ }
22
+ }
23
+ /** A profile by name, own entries only: `toString` and `__proto__` name no profile. */
24
+ export function getProfile(config, name) {
25
+ return Object.hasOwn(config.profiles, name) ? config.profiles[name] : undefined;
26
+ }
27
+ function isRecord(value) {
28
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
29
+ }
30
+ /**
31
+ * Read the stored profiles. A missing file is an empty store. A file that is
32
+ * not valid JSON, or not the profile shape, throws: returning an empty store
33
+ * there let the next `login` overwrite the file and lose every profile.
34
+ */
14
35
  export function readConfig() {
15
36
  const path = configPath();
16
37
  if (!existsSync(path))
17
- return { profiles: {} };
38
+ return { profiles: Object.create(null) };
39
+ let parsed;
18
40
  try {
19
- const parsed = JSON.parse(readFileSync(path, 'utf-8'));
20
- return {
21
- profiles: parsed.profiles ?? {},
22
- activeProfile: parsed.activeProfile,
23
- };
41
+ parsed = JSON.parse(readFileSync(path, 'utf-8'));
42
+ }
43
+ catch (err) {
44
+ throw new ConfigFileError(`The config file ${path} cannot be read: ${err instanceof Error ? err.message : String(err)}`, path);
24
45
  }
25
- catch {
26
- return { profiles: {} };
46
+ if (!isRecord(parsed) || (parsed.profiles !== undefined && !isRecord(parsed.profiles))) {
47
+ throw new ConfigFileError(`The config file ${path} is not a profile store (expected an object with a "profiles" object).`, path);
27
48
  }
49
+ const activeProfile = typeof parsed.activeProfile === 'string' ? parsed.activeProfile : undefined;
50
+ return {
51
+ profiles: Object.assign(Object.create(null), parsed.profiles ?? {}),
52
+ activeProfile,
53
+ };
28
54
  }
55
+ /** Write the store through a temp file in the same directory and a rename, so a crash never leaves half a file. */
29
56
  export function writeConfig(config) {
30
57
  const dir = configDir();
31
- if (!existsSync(dir))
32
- mkdirSync(dir, { recursive: true, mode: 0o700 });
58
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
59
+ // The mode above applies only when the directory is created.
60
+ chmodSync(dir, 0o700);
33
61
  const path = configPath();
34
- writeFileSync(path, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });
35
- chmodSync(path, 0o600);
62
+ const temp = join(dir, `.config.json.${process.pid}.tmp`);
63
+ try {
64
+ writeFileSync(temp, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });
65
+ chmodSync(temp, 0o600);
66
+ renameSync(temp, path);
67
+ }
68
+ catch (err) {
69
+ rmSync(temp, { force: true });
70
+ throw err;
71
+ }
36
72
  }
@@ -4,7 +4,9 @@
4
4
  * Conventions (modeled on `gh api`, which has the same two flags):
5
5
  * - `key=value` → { key: "value" }
6
6
  * - `key=true|false|null` → typed literal (`-F` only)
7
- * - `key=42` / `key=3.14` → number (`-F` only)
7
+ * - `key=42` / `key=3.14` → number (`-F` only; only when the number
8
+ * prints back as typed, so `0123`, `-0` and 20-digit
9
+ * ids stay strings)
8
10
  * - `key=[...]` / `key={...}` → JSON-parsed array/object (`-F` only)
9
11
  * - `a.b.c=value` → nested: { a: { b: { c: "value" } } }
10
12
  * - `arr[]=a arr[]=b` → { arr: ["a", "b"] }
@@ -4,7 +4,9 @@
4
4
  * Conventions (modeled on `gh api`, which has the same two flags):
5
5
  * - `key=value` → { key: "value" }
6
6
  * - `key=true|false|null` → typed literal (`-F` only)
7
- * - `key=42` / `key=3.14` → number (`-F` only)
7
+ * - `key=42` / `key=3.14` → number (`-F` only; only when the number
8
+ * prints back as typed, so `0123`, `-0` and 20-digit
9
+ * ids stay strings)
8
10
  * - `key=[...]` / `key={...}` → JSON-parsed array/object (`-F` only)
9
11
  * - `a.b.c=value` → nested: { a: { b: { c: "value" } } }
10
12
  * - `arr[]=a arr[]=b` → { arr: ["a", "b"] }
@@ -46,10 +48,14 @@ function coerceValue(raw) {
46
48
  return null;
47
49
  if (raw === '')
48
50
  return '';
49
- if (/^-?\d+$/.test(raw))
50
- return Number(raw);
51
- if (/^-?\d*\.\d+$/.test(raw))
52
- return Number(raw);
51
+ // A number only when it reads back as the same text: 0123, -0 and an integer
52
+ // past 2^53 would otherwise reach the Server as a different value than typed.
53
+ if (/^-?\d*\.?\d+$/.test(raw)) {
54
+ const number = Number(raw);
55
+ if (Number.isFinite(number) && String(number) === raw)
56
+ return number;
57
+ return raw;
58
+ }
53
59
  // JSON literals only if clearly bracketed, which avoids misinterpreting strings.
54
60
  if ((raw.startsWith('{') && raw.endsWith('}')) || (raw.startsWith('[') && raw.endsWith(']'))) {
55
61
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agledger/cli",
3
- "version": "2.0.0",
4
- "description": "AGLedger CLI: developer and agent interface for AGLedger",
3
+ "version": "2.2.0",
4
+ "description": "AGLedger CLI: the AGLedger API from the command line, for developers and agents. Self-hosted, signed, hash-chained. Patent pending.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "module",
7
7
  "bin": {
@@ -12,47 +12,35 @@
12
12
  "!dist/**/*.map",
13
13
  "bin",
14
14
  "LICENSE",
15
- "SKILL.md",
16
- "oclif.manifest.json"
15
+ "SKILL.md"
17
16
  ],
18
17
  "scripts": {
19
18
  "prebuild": "rm -rf dist",
20
19
  "build": "tsc",
21
- "postbuild": "oclif manifest",
22
20
  "typecheck": "tsc --noEmit",
23
21
  "clean": "rm -rf dist *.tsbuildinfo",
24
- "lint": "tsc --noEmit",
22
+ "lint": "oxlint --type-aware .",
25
23
  "pretest": "npm run build",
26
24
  "test": "vitest run",
27
25
  "deps:lag": "node scripts/dep-lag.mjs",
28
26
  "deps:refresh": "npm update && node scripts/dep-lag.mjs",
29
- "prepack": "npm run build",
27
+ "prepack": "node -e \"fs.existsSync('tsconfig.json')&&child_process.execSync('npm run build',{stdio:'inherit'})\"",
30
28
  "lint:pkg": "publint --strict && attw --pack . --ignore-rules cjs-resolves-to-esm no-resolution",
31
29
  "prepublishOnly": "npm run build && npm run lint:pkg"
32
30
  },
33
31
  "dependencies": {
34
- "@agledger/verify-core": "^2.0.0",
35
- "@oclif/core": "^5.0.0"
32
+ "@agledger/verify-core": "^2.2.0"
36
33
  },
37
34
  "devDependencies": {
38
35
  "@arethetypeswrong/cli": "^0.18.3",
36
+ "@cyclonedx/cyclonedx-npm": "7.0.1",
39
37
  "@types/node": "^24.0.0",
40
- "oclif": "^5.0.1",
38
+ "oxlint": "1.94.0",
39
+ "oxlint-tsgolint": "7.0.2008",
41
40
  "publint": "^0.3.21",
42
41
  "typescript": "^7.0.2",
43
42
  "vitest": "^5.0.0"
44
43
  },
45
- "oclif": {
46
- "bin": "agledger",
47
- "commands": {
48
- "strategy": "pattern",
49
- "target": "./dist/commands"
50
- },
51
- "hooks": {
52
- "command_not_found": "./dist/hooks/command-not-found.js"
53
- },
54
- "topicSeparator": " "
55
- },
56
44
  "repository": {
57
45
  "type": "git",
58
46
  "url": "git+https://github.com/agledger-ai/cli.git"
@@ -1,3 +0,0 @@
1
- import type { Hook } from '@oclif/core';
2
- declare const hook: Hook<'command_not_found'>;
3
- export default hook;
@@ -1,63 +0,0 @@
1
- /**
2
- * Unknown commands printed only "command X not found", with no nearest match
3
- * and no pointer to how to find the real one. An agent that
4
- * guessed a verb had nothing to recover from, which matters here because the
5
- * CLI deliberately has few commands: almost everything goes through
6
- * `agledger api`, and a wrong guess is the expected first move.
7
- */
8
- /** Levenshtein distance, iterative two-row form. */
9
- function distance(a, b) {
10
- if (a === b)
11
- return 0;
12
- if (a.length === 0)
13
- return b.length;
14
- if (b.length === 0)
15
- return a.length;
16
- let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
17
- for (let i = 0; i < a.length; i++) {
18
- const current = [i + 1];
19
- for (let j = 0; j < b.length; j++) {
20
- const substitute = previous[j] + (a[i] === b[j] ? 0 : 1);
21
- current.push(Math.min(current[j] + 1, previous[j + 1] + 1, substitute));
22
- }
23
- previous = current;
24
- }
25
- return previous[b.length];
26
- }
27
- /** Closest command id, when it is close enough to be worth suggesting. */
28
- function nearest(id, candidates) {
29
- let best;
30
- let bestScore = Infinity;
31
- for (const candidate of candidates) {
32
- const score = distance(id.toLowerCase(), candidate.toLowerCase());
33
- if (score < bestScore) {
34
- bestScore = score;
35
- best = candidate;
36
- }
37
- }
38
- // Scale with the length of what was typed so short ids don't match everything.
39
- return best !== undefined && bestScore <= Math.max(2, Math.floor(id.length / 2))
40
- ? best
41
- : undefined;
42
- }
43
- const hook = async function (opts) {
44
- const id = opts.id;
45
- const ids = opts.config.commandIDs;
46
- const suggestion = nearest(id, ids);
47
- const parts = [
48
- suggestion ? `Did you mean \`agledger ${suggestion}\`?` : undefined,
49
- 'Run `agledger list-commands` to see every command.',
50
- // The real surface area is the API passthrough, not the command list.
51
- 'Most operations go through `agledger api <METHOD> <PATH>`, which reaches every route.',
52
- ].filter(Boolean);
53
- process.stderr.write(JSON.stringify({
54
- error: true,
55
- code: 'COMMAND_NOT_FOUND',
56
- message: `Command "${id}" not found.`,
57
- ...(suggestion ? { didYouMean: suggestion } : {}),
58
- suggestion: parts.join(' '),
59
- }) + '\n');
60
- // 2 = usage error, matching the exit code oclif already used for this case.
61
- process.exit(2);
62
- };
63
- export default hook;