@hop-top/kit 0.4.0-alpha.1

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 (174) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +25 -0
  3. package/README.md +189 -0
  4. package/dist/aim.d.ts +116 -0
  5. package/dist/aim.js +417 -0
  6. package/dist/alias.d.ts +73 -0
  7. package/dist/alias.js +207 -0
  8. package/dist/api.d.ts +62 -0
  9. package/dist/api.js +165 -0
  10. package/dist/auth.d.ts +26 -0
  11. package/dist/auth.js +41 -0
  12. package/dist/cli.d.ts +309 -0
  13. package/dist/cli.js +885 -0
  14. package/dist/config.d.ts +97 -0
  15. package/dist/config.js +79 -0
  16. package/dist/errcorrect.d.ts +37 -0
  17. package/dist/errcorrect.js +69 -0
  18. package/dist/llm.d.ts +188 -0
  19. package/dist/llm.js +356 -0
  20. package/dist/output.d.ts +213 -0
  21. package/dist/output.js +447 -0
  22. package/dist/progress.d.ts +30 -0
  23. package/dist/progress.js +56 -0
  24. package/dist/provenance.d.ts +24 -0
  25. package/dist/provenance.js +32 -0
  26. package/dist/routellm.d.ts +64 -0
  27. package/dist/routellm.js +258 -0
  28. package/dist/rpc.d.ts +25 -0
  29. package/dist/rpc.js +101 -0
  30. package/dist/safety.d.ts +24 -0
  31. package/dist/safety.js +44 -0
  32. package/dist/scope.d.ts +121 -0
  33. package/dist/scope.js +380 -0
  34. package/dist/sqlstore.d.ts +84 -0
  35. package/dist/sqlstore.js +85 -0
  36. package/dist/stream.d.ts +35 -0
  37. package/dist/stream.js +52 -0
  38. package/dist/tui/index.d.ts +300 -0
  39. package/dist/tui/index.js +332 -0
  40. package/dist/upgrade.d.ts +95 -0
  41. package/dist/upgrade.js +115 -0
  42. package/dist/xdg.d.ts +169 -0
  43. package/dist/xdg.js +129 -0
  44. package/eslint.config.mjs +17 -0
  45. package/package.json +133 -0
  46. package/src/README.md +3 -0
  47. package/src/aim.test.ts +331 -0
  48. package/src/aim.ts +388 -0
  49. package/src/alias.test.ts +423 -0
  50. package/src/alias.ts +256 -0
  51. package/src/api.test.ts +190 -0
  52. package/src/api.ts +214 -0
  53. package/src/auth.test.ts +24 -0
  54. package/src/auth.ts +37 -0
  55. package/src/bus.test.ts +178 -0
  56. package/src/bus.ts +114 -0
  57. package/src/cli.test.ts +690 -0
  58. package/src/cli.ts +741 -0
  59. package/src/completion-values.test.ts +202 -0
  60. package/src/completion-values.ts +173 -0
  61. package/src/completion.test.ts +212 -0
  62. package/src/completion.ts +158 -0
  63. package/src/compliance.test.ts +134 -0
  64. package/src/compliance.ts +669 -0
  65. package/src/config.test.ts +110 -0
  66. package/src/config.ts +164 -0
  67. package/src/errcorrect.test.ts +134 -0
  68. package/src/errcorrect.ts +84 -0
  69. package/src/flagregister.test.ts +103 -0
  70. package/src/flagregister.ts +78 -0
  71. package/src/gen/crud_connect.ts +76 -0
  72. package/src/gen/crud_pb.ts +269 -0
  73. package/src/hint.test.ts +315 -0
  74. package/src/hint.ts +178 -0
  75. package/src/llm.test.ts +612 -0
  76. package/src/llm.ts +577 -0
  77. package/src/log.test.ts +145 -0
  78. package/src/log.ts +155 -0
  79. package/src/output/builtins.ts +34 -0
  80. package/src/output/dispatch.ts +195 -0
  81. package/src/output/flags.ts +151 -0
  82. package/src/output/format_help.ts +80 -0
  83. package/src/output/formatter.ts +167 -0
  84. package/src/output/formatters/csv.ts +72 -0
  85. package/src/output/formatters/json.ts +44 -0
  86. package/src/output/formatters/table.ts +44 -0
  87. package/src/output/formatters/text.ts +109 -0
  88. package/src/output/formatters/yaml.ts +47 -0
  89. package/src/output/projection.ts +126 -0
  90. package/src/output/registry.ts +88 -0
  91. package/src/output/template.ts +63 -0
  92. package/src/output.test.ts +117 -0
  93. package/src/output.ts +90 -0
  94. package/src/progress.test.ts +93 -0
  95. package/src/progress.ts +58 -0
  96. package/src/provenance.test.ts +59 -0
  97. package/src/provenance.ts +26 -0
  98. package/src/routellm.test.ts +179 -0
  99. package/src/routellm.ts +365 -0
  100. package/src/router/bert.test.ts +180 -0
  101. package/src/router/bert.ts +145 -0
  102. package/src/router/controller.test.ts +183 -0
  103. package/src/router/controller.ts +165 -0
  104. package/src/router/integration.test.ts +309 -0
  105. package/src/router/intent.test.ts +187 -0
  106. package/src/router/intent.ts +208 -0
  107. package/src/router/mf.test.ts +66 -0
  108. package/src/router/mf.ts +52 -0
  109. package/src/router/random.test.ts +23 -0
  110. package/src/router/random.ts +13 -0
  111. package/src/router/router.test.ts +31 -0
  112. package/src/router/router.ts +62 -0
  113. package/src/router/server.test.ts +193 -0
  114. package/src/router/server.ts +134 -0
  115. package/src/router/sw.test.ts +199 -0
  116. package/src/router/sw.ts +232 -0
  117. package/src/rpc.test.ts +47 -0
  118. package/src/rpc.ts +91 -0
  119. package/src/safety.test.ts +53 -0
  120. package/src/safety.ts +42 -0
  121. package/src/scope-defaults.json +72 -0
  122. package/src/scope.test.ts +177 -0
  123. package/src/scope.ts +382 -0
  124. package/src/setflag.test.ts +110 -0
  125. package/src/setflag.ts +102 -0
  126. package/src/sqlstore.test.ts +110 -0
  127. package/src/sqlstore.ts +144 -0
  128. package/src/stream.test.ts +69 -0
  129. package/src/stream.ts +47 -0
  130. package/src/textflag.test.ts +105 -0
  131. package/src/textflag.ts +68 -0
  132. package/src/toolspec.test.ts +187 -0
  133. package/src/toolspec.ts +279 -0
  134. package/src/triton/client.test.ts +205 -0
  135. package/src/triton/client.ts +261 -0
  136. package/src/tui/anim.test.ts +146 -0
  137. package/src/tui/anim.ts +134 -0
  138. package/src/tui/badge.test.ts +41 -0
  139. package/src/tui/badge.ts +55 -0
  140. package/src/tui/confirm.test.ts +68 -0
  141. package/src/tui/confirm.ts +49 -0
  142. package/src/tui/index.ts +29 -0
  143. package/src/tui/list.test.ts +84 -0
  144. package/src/tui/list.ts +72 -0
  145. package/src/tui/parity.ts +41 -0
  146. package/src/tui/pills.test.ts +40 -0
  147. package/src/tui/pills.ts +43 -0
  148. package/src/tui/progress.test.ts +164 -0
  149. package/src/tui/progress.ts +120 -0
  150. package/src/tui/spinner.test.ts +111 -0
  151. package/src/tui/spinner.ts +96 -0
  152. package/src/tui/status.test.ts +68 -0
  153. package/src/tui/status.ts +66 -0
  154. package/src/upgrade.test.ts +298 -0
  155. package/src/upgrade.ts +205 -0
  156. package/src/uri.ts +199 -0
  157. package/src/wizard.test.ts +441 -0
  158. package/src/wizard.ts +662 -0
  159. package/src/xdg.test.ts +202 -0
  160. package/src/xdg.ts +280 -0
  161. package/test/output/cols.test.ts +148 -0
  162. package/test/output/csv.test.ts +129 -0
  163. package/test/output/dispatch.test.ts +194 -0
  164. package/test/output/format_help.test.ts +131 -0
  165. package/test/output/formatter.test.ts +137 -0
  166. package/test/output/output_flag.test.ts +124 -0
  167. package/test/output/registry.test.ts +127 -0
  168. package/test/output/smoke-capture.ts +83 -0
  169. package/test/output/smoke.test.ts +142 -0
  170. package/test/output/template.test.ts +95 -0
  171. package/test/output/text.test.ts +75 -0
  172. package/test/testdata/api-fixture.json +115 -0
  173. package/test/testdata/query-vectors.json +187 -0
  174. package/tsconfig.json +12 -0
@@ -0,0 +1,95 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Version check and notify — compares the running tool version against the
5
+ * latest GitHub release and writes a human-readable upgrade notice to any
6
+ * {@link https://nodejs.org/api/stream.html#class-streamwritable | Writable}
7
+ * stream when a newer release is available.
8
+ *
9
+ * ### Cache behaviour
10
+ * Results are persisted to `{stateDir}/.upgrade-{name}-cache.json`.
11
+ * The cache stores `{ version, checkedAt }`. On the next call within `cacheTTL`
12
+ * milliseconds the remote API is not contacted; the cached version is used
13
+ * directly. After the TTL expires the cache is refreshed on the next call.
14
+ * Cache I/O errors are swallowed — a stale or missing cache simply causes a
15
+ * fresh fetch.
16
+ *
17
+ * ### Error handling philosophy
18
+ * Version checking is a best-effort, non-critical operation. Any failure —
19
+ * network error, bad JSON, HTTP error, filesystem error, invalid semver — is
20
+ * silently discarded. The function always resolves without throwing. If
21
+ * something goes wrong the user sees neither a notice nor an error.
22
+ *
23
+ * ### Notice format
24
+ * ```
25
+ * \nA new release of {name} is available: v{current} → v{latest}\nhttps://github.com/{owner}/{repo}/releases/latest\n
26
+ * ```
27
+ *
28
+ * ### Usage example
29
+ * ```ts
30
+ * import { createChecker } from '@hop-top/kit/upgrade';
31
+ *
32
+ * const checker = createChecker({
33
+ * name: 'my-cli',
34
+ * currentVersion: '1.2.3',
35
+ * owner: 'myorg',
36
+ * repo: 'my-cli',
37
+ * });
38
+ *
39
+ * // At the end of your CLI command:
40
+ * await checker.notifyIfAvailable(process.stderr);
41
+ * ```
42
+ */
43
+ /**
44
+ * Static configuration for a version {@link Checker}.
45
+ */
46
+ interface CheckerOptions {
47
+ /** Human-readable tool name shown in the upgrade notice. */
48
+ name: string;
49
+ /** Currently running version string (semver, without leading `v`). */
50
+ currentVersion: string;
51
+ /** GitHub organisation or username that owns the repository. */
52
+ owner: string;
53
+ /** GitHub repository name. */
54
+ repo: string;
55
+ /**
56
+ * How long (ms) a cached result stays valid before a fresh fetch is made.
57
+ * @defaultValue 86_400_000 (24 hours)
58
+ */
59
+ cacheTTL?: number;
60
+ /**
61
+ * Directory used to store the cache file.
62
+ * @defaultValue `os.tmpdir()`
63
+ */
64
+ stateDir?: string;
65
+ /**
66
+ * HTTP request timeout in milliseconds.
67
+ * @defaultValue 10_000
68
+ */
69
+ timeout?: number;
70
+ }
71
+ /**
72
+ * Returned by {@link createChecker}. Call {@link Checker.notifyIfAvailable}
73
+ * at the end of a CLI command to surface upgrade notices non-intrusively.
74
+ */
75
+ interface Checker {
76
+ /**
77
+ * Checks for a newer GitHub release and writes a one-line notice to `out`
78
+ * if one is available. Silently swallows all errors.
79
+ *
80
+ * @param out - Any Node.js writable stream (e.g. `process.stderr`).
81
+ */
82
+ notifyIfAvailable(out: NodeJS.WritableStream): Promise<void>;
83
+ }
84
+ /**
85
+ * Creates a {@link Checker} configured with the provided options.
86
+ *
87
+ * The checker is stateless beyond what is persisted in the cache file; it is
88
+ * safe to create multiple instances with the same options.
89
+ *
90
+ * @param opts - Configuration options; see {@link CheckerOptions}.
91
+ * @returns A {@link Checker} ready to call.
92
+ */
93
+ declare function createChecker(opts: CheckerOptions): Checker;
94
+
95
+ export { type Checker, type CheckerOptions, createChecker };
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/upgrade.ts
31
+ var upgrade_exports = {};
32
+ __export(upgrade_exports, {
33
+ createChecker: () => createChecker
34
+ });
35
+ module.exports = __toCommonJS(upgrade_exports);
36
+ var fs = __toESM(require("fs"));
37
+ var os = __toESM(require("os"));
38
+ var path = __toESM(require("path"));
39
+ var import_semver = __toESM(require("semver"));
40
+ function createChecker(opts) {
41
+ const {
42
+ name,
43
+ currentVersion,
44
+ owner,
45
+ repo,
46
+ cacheTTL = 24 * 60 * 60 * 1e3,
47
+ stateDir = os.tmpdir(),
48
+ timeout = 1e4
49
+ } = opts;
50
+ const cacheFile = path.join(stateDir, `.upgrade-${name}-cache.json`);
51
+ const releaseURL = `https://api.github.com/repos/${owner}/${repo}/releases/latest`;
52
+ const releasesPageURL = `https://github.com/${owner}/${repo}/releases/latest`;
53
+ function readCache() {
54
+ try {
55
+ const raw = fs.readFileSync(cacheFile, "utf8");
56
+ const parsed = JSON.parse(raw);
57
+ if (typeof parsed.version === "string" && typeof parsed.checkedAt === "number") {
58
+ return parsed;
59
+ }
60
+ } catch {
61
+ }
62
+ return null;
63
+ }
64
+ function writeCache(version) {
65
+ try {
66
+ const entry = { version, checkedAt: Date.now() };
67
+ fs.writeFileSync(cacheFile, JSON.stringify(entry), "utf8");
68
+ } catch {
69
+ }
70
+ }
71
+ async function fetchLatestVersion() {
72
+ const controller = new AbortController();
73
+ const timer = setTimeout(() => controller.abort(), timeout);
74
+ try {
75
+ const res = await fetch(releaseURL, {
76
+ signal: controller.signal,
77
+ headers: { Accept: "application/vnd.github+json" }
78
+ });
79
+ if (!res.ok) return null;
80
+ const body = await res.json();
81
+ const tag = body.tag_name;
82
+ if (typeof tag !== "string") return null;
83
+ return tag.replace(/^v/, "");
84
+ } finally {
85
+ clearTimeout(timer);
86
+ }
87
+ }
88
+ async function notifyIfAvailable(out) {
89
+ try {
90
+ let latestVersion = null;
91
+ const cached = readCache();
92
+ if (cached !== null && Date.now() - cached.checkedAt < cacheTTL) {
93
+ latestVersion = cached.version;
94
+ } else {
95
+ latestVersion = await fetchLatestVersion();
96
+ if (latestVersion !== null) {
97
+ writeCache(latestVersion);
98
+ }
99
+ }
100
+ if (latestVersion === null) return;
101
+ if (!import_semver.default.gt(latestVersion, currentVersion)) return;
102
+ const notice = `
103
+ A new release of ${name} is available: v${currentVersion} \u2192 v${latestVersion}
104
+ ${releasesPageURL}
105
+ `;
106
+ out.write(notice);
107
+ } catch {
108
+ }
109
+ }
110
+ return { notifyIfAvailable };
111
+ }
112
+ // Annotate the CommonJS export names for ESM import in node:
113
+ 0 && (module.exports = {
114
+ createChecker
115
+ });
package/dist/xdg.d.ts ADDED
@@ -0,0 +1,169 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * XDG Base Directory path resolution with OS-native fallbacks.
5
+ *
6
+ * Each function checks the corresponding XDG environment variable first
7
+ * (`XDG_CONFIG_HOME`, `XDG_DATA_HOME`, `XDG_CACHE_HOME`, `XDG_STATE_HOME`).
8
+ * When the variable is unset or empty it falls back to the platform-native
9
+ * directory:
10
+ *
11
+ * - macOS (`darwin`): `~/Library/Application Support/<tool>` for data/state;
12
+ * `~/Library/Preferences` for config;
13
+ * `~/Library/Caches` for cache.
14
+ * - Windows (`win32`): `%LocalAppData%\<tool>` for data/state;
15
+ * `%APPDATA%\<tool>` for config/cache.
16
+ * - Linux / other: `~/.config`, `~/.local/share`,
17
+ * `~/.cache`, `~/.local/state`.
18
+ *
19
+ * All functions are pure (no side effects). Use {@link mustEnsure} to create
20
+ * the directory on disk.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { configDir, mustEnsure } from '@hop-top/kit/xdg';
25
+ *
26
+ * const cfg = mustEnsure(configDir('mytool'));
27
+ * // cfg is guaranteed to exist on disk
28
+ * ```
29
+ */
30
+ /**
31
+ * All four canonical XDG-style directories for a single tool, resolved in one
32
+ * shot.
33
+ */
34
+ interface Dirs {
35
+ /** Resolved configuration directory. */
36
+ config: string;
37
+ /** Resolved data directory. */
38
+ data: string;
39
+ /** Resolved cache directory. */
40
+ cache: string;
41
+ /** Resolved state directory. */
42
+ state: string;
43
+ }
44
+ /**
45
+ * Returns the configuration directory for the named tool.
46
+ *
47
+ * Resolution order:
48
+ * 1. `$XDG_CONFIG_HOME/<tool>` — when the variable is set and non-empty.
49
+ * 2. OS-native fallback:
50
+ * - macOS: `~/Library/Preferences/<tool>`
51
+ * - Windows: `%APPDATA%\<tool>` (throws if `APPDATA` is unset)
52
+ * - Linux/other: `~/.config/<tool>`
53
+ *
54
+ * The returned path is **not** guaranteed to exist; call {@link mustEnsure} to
55
+ * create it.
56
+ *
57
+ * @param tool - Tool name appended as the final path segment.
58
+ * @returns Absolute path to the configuration directory.
59
+ * @throws {Error} When the OS-native root cannot be determined
60
+ * (e.g. `%APPDATA%` is unset on Windows).
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const dir = configDir('myapp');
65
+ * // macOS → /Users/alice/Library/Preferences/myapp
66
+ * // Linux → /home/alice/.config/myapp
67
+ * ```
68
+ */
69
+ declare function configDir(tool: string, plt?: NodeJS.Platform): string;
70
+ /**
71
+ * Returns the data directory for the named tool.
72
+ *
73
+ * Resolution order:
74
+ * 1. `$XDG_DATA_HOME/<tool>` — when the variable is set and non-empty.
75
+ * 2. OS-native fallback:
76
+ * - macOS: `~/Library/Application Support/<tool>`
77
+ * - Windows: `%LocalAppData%\<tool>` (throws if `LocalAppData` is unset)
78
+ * - Linux/other: `~/.local/share/<tool>`
79
+ *
80
+ * @param tool - Tool name appended as the final path segment.
81
+ * @param plt - Optional platform override (defaults to `process.platform`).
82
+ * @returns Absolute path to the data directory.
83
+ * @throws {Error} When the OS-native root cannot be determined.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const dir = dataDir('myapp');
88
+ * // macOS → /Users/alice/Library/Application Support/myapp
89
+ * // Linux → /home/alice/.local/share/myapp
90
+ * // Windows → C:\Users\alice\AppData\Local\myapp
91
+ * ```
92
+ */
93
+ declare function dataDir(tool: string, plt?: NodeJS.Platform): string;
94
+ /**
95
+ * Returns the cache directory for the named tool.
96
+ *
97
+ * Resolution order:
98
+ * 1. `$XDG_CACHE_HOME/<tool>` — when the variable is set and non-empty.
99
+ * 2. OS-native fallback:
100
+ * - macOS: `~/Library/Caches/<tool>`
101
+ * - Windows: `%LocalAppData%\<tool>\cache` (throws if `LocalAppData` unset)
102
+ * - Linux/other: `~/.cache/<tool>`
103
+ *
104
+ * @param tool - Tool name appended as the final path segment.
105
+ * @param plt - Optional platform override (defaults to `process.platform`).
106
+ * @returns Absolute path to the cache directory.
107
+ * @throws {Error} When the OS-native root cannot be determined.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const dir = cacheDir('myapp');
112
+ * // macOS → /Users/alice/Library/Caches/myapp
113
+ * // Linux → /home/alice/.cache/myapp
114
+ * ```
115
+ */
116
+ declare function cacheDir(tool: string, plt?: NodeJS.Platform): string;
117
+ /**
118
+ * Returns the state directory for the named tool.
119
+ *
120
+ * State stores runtime artefacts that should persist across restarts but are
121
+ * not user-facing configuration (e.g. lock files, history, socket paths).
122
+ *
123
+ * Resolution order:
124
+ * 1. `$XDG_STATE_HOME/<tool>` — when the variable is set and non-empty.
125
+ * 2. OS-native fallback:
126
+ * - macOS: `~/Library/Application Support/<tool>/state`
127
+ * (the `/state` suffix avoids collision with {@link dataDir})
128
+ * - Windows: `%LocalAppData%\<tool>\state`
129
+ * (throws if `LocalAppData` is unset)
130
+ * - Linux/other: `~/.local/state/<tool>`
131
+ *
132
+ * @param tool - Tool name appended as the final path segment.
133
+ * @param plt - Optional platform override (defaults to `process.platform`).
134
+ * @returns Absolute path to the state directory.
135
+ * @throws {Error} When the OS-native root cannot be determined.
136
+ *
137
+ * @example
138
+ * ```ts
139
+ * const dir = stateDir('myapp');
140
+ * // macOS → /Users/alice/Library/Application Support/myapp/state
141
+ * // Linux → /home/alice/.local/state/myapp
142
+ * // Windows → C:\Users\alice\AppData\Local\myapp\state
143
+ * ```
144
+ */
145
+ declare function stateDir(tool: string, plt?: NodeJS.Platform): string;
146
+ /**
147
+ * Creates `dir` (and any parents) with mode `0o750`, then returns `dir`.
148
+ *
149
+ * Throws an `Error` if `fs.mkdirSync` fails — for example when `dir` is an
150
+ * existing regular file or a permission error prevents directory creation.
151
+ *
152
+ * Intended for startup-time path resolution where a missing directory is
153
+ * unrecoverable:
154
+ *
155
+ * @param dir - Absolute path to the directory to create.
156
+ * @returns The same `dir` value, now guaranteed to exist on disk.
157
+ * @throws {Error} On any filesystem error (file-exists-as-non-dir, EPERM, …).
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * import { dataDir, mustEnsure } from '@hop-top/kit/xdg';
162
+ *
163
+ * const data = mustEnsure(dataDir('mytool'));
164
+ * // data directory now exists on disk
165
+ * ```
166
+ */
167
+ declare function mustEnsure(dir: string): string;
168
+
169
+ export { type Dirs, cacheDir, configDir, dataDir, mustEnsure, stateDir };
package/dist/xdg.js ADDED
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/xdg.ts
31
+ var xdg_exports = {};
32
+ __export(xdg_exports, {
33
+ cacheDir: () => cacheDir,
34
+ configDir: () => configDir,
35
+ dataDir: () => dataDir,
36
+ mustEnsure: () => mustEnsure,
37
+ stateDir: () => stateDir
38
+ });
39
+ module.exports = __toCommonJS(xdg_exports);
40
+ var os = __toESM(require("os"));
41
+ var path = __toESM(require("path"));
42
+ var fs = __toESM(require("fs"));
43
+ function platform(override) {
44
+ return override ?? process.platform;
45
+ }
46
+ function configDir(tool, plt) {
47
+ const xdg = process.env["XDG_CONFIG_HOME"];
48
+ if (xdg) return path.join(xdg, tool);
49
+ const p = platform(plt);
50
+ const home = os.homedir();
51
+ switch (p) {
52
+ case "darwin":
53
+ return path.join(home, "Library", "Preferences", tool);
54
+ case "win32": {
55
+ const appdata = process.env["APPDATA"];
56
+ if (!appdata) throw new Error("%APPDATA% is not set");
57
+ return path.join(appdata, tool);
58
+ }
59
+ default:
60
+ return path.join(home, ".config", tool);
61
+ }
62
+ }
63
+ function dataDir(tool, plt) {
64
+ const xdg = process.env["XDG_DATA_HOME"];
65
+ if (xdg) return path.join(xdg, tool);
66
+ const p = platform(plt);
67
+ const home = os.homedir();
68
+ switch (p) {
69
+ case "darwin":
70
+ return path.join(home, "Library", "Application Support", tool);
71
+ case "win32": {
72
+ const local = process.env["LocalAppData"];
73
+ if (!local) throw new Error("%LocalAppData% is not set");
74
+ return path.win32.join(local, tool);
75
+ }
76
+ default:
77
+ return path.join(home, ".local", "share", tool);
78
+ }
79
+ }
80
+ function cacheDir(tool, plt) {
81
+ const xdg = process.env["XDG_CACHE_HOME"];
82
+ if (xdg) return path.join(xdg, tool);
83
+ const p = platform(plt);
84
+ const home = os.homedir();
85
+ switch (p) {
86
+ case "darwin":
87
+ return path.join(home, "Library", "Caches", tool);
88
+ case "win32": {
89
+ const local = process.env["LocalAppData"];
90
+ if (!local) throw new Error("%LocalAppData% is not set");
91
+ return path.win32.join(local, tool, "cache");
92
+ }
93
+ default:
94
+ return path.join(home, ".cache", tool);
95
+ }
96
+ }
97
+ function stateDir(tool, plt) {
98
+ const xdg = process.env["XDG_STATE_HOME"];
99
+ if (xdg) return path.join(xdg, tool);
100
+ const p = platform(plt);
101
+ const home = os.homedir();
102
+ switch (p) {
103
+ case "darwin":
104
+ return path.join(home, "Library", "Application Support", tool, "state");
105
+ case "win32": {
106
+ const local = process.env["LocalAppData"];
107
+ if (!local) throw new Error("%LocalAppData% is not set");
108
+ return path.win32.join(local, tool, "state");
109
+ }
110
+ default:
111
+ return path.join(home, ".local", "state", tool);
112
+ }
113
+ }
114
+ function mustEnsure(dir) {
115
+ fs.mkdirSync(dir, { recursive: true, mode: 488 });
116
+ const stat = fs.statSync(dir);
117
+ if (!stat.isDirectory()) {
118
+ throw new Error(`mustEnsure: path exists but is not a directory: ${dir}`);
119
+ }
120
+ return dir;
121
+ }
122
+ // Annotate the CommonJS export names for ESM import in node:
123
+ 0 && (module.exports = {
124
+ cacheDir,
125
+ configDir,
126
+ dataDir,
127
+ mustEnsure,
128
+ stateDir
129
+ });
@@ -0,0 +1,17 @@
1
+ import tseslint from 'typescript-eslint';
2
+
3
+ export default tseslint.config(
4
+ ...tseslint.configs.recommended,
5
+ {
6
+ rules: {
7
+ '@typescript-eslint/no-explicit-any': 'off',
8
+ '@typescript-eslint/no-unused-vars': ['error', {
9
+ argsIgnorePattern: '^_',
10
+ varsIgnorePattern: '^_',
11
+ }],
12
+ },
13
+ },
14
+ {
15
+ ignores: ['node_modules/', 'dist/', '*.js'],
16
+ },
17
+ );
package/package.json ADDED
@@ -0,0 +1,133 @@
1
+ {
2
+ "name": "@hop-top/kit",
3
+ "version": "0.4.0-alpha.1",
4
+ "description": "Shared CLI utilities for hop-top tools — TypeScript/CJS edition",
5
+ "type": "commonjs",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "require": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./cli": {
14
+ "require": "./dist/cli.js",
15
+ "types": "./dist/cli.d.ts"
16
+ },
17
+ "./output": {
18
+ "require": "./dist/output.js",
19
+ "types": "./dist/output.d.ts"
20
+ },
21
+ "./xdg": {
22
+ "require": "./dist/xdg.js",
23
+ "types": "./dist/xdg.d.ts"
24
+ },
25
+ "./config": {
26
+ "require": "./dist/config.js",
27
+ "types": "./dist/config.d.ts"
28
+ },
29
+ "./sqlstore": {
30
+ "require": "./dist/sqlstore.js",
31
+ "types": "./dist/sqlstore.d.ts"
32
+ },
33
+ "./upgrade": {
34
+ "require": "./dist/upgrade.js",
35
+ "types": "./dist/upgrade.d.ts"
36
+ },
37
+ "./llm": {
38
+ "require": "./dist/llm.js",
39
+ "types": "./dist/llm.d.ts"
40
+ },
41
+ "./routellm": {
42
+ "require": "./dist/routellm.js",
43
+ "types": "./dist/routellm.d.ts"
44
+ },
45
+ "./alias": {
46
+ "require": "./dist/alias.js",
47
+ "types": "./dist/alias.d.ts"
48
+ },
49
+ "./tui": {
50
+ "require": "./dist/tui/index.js",
51
+ "types": "./dist/tui/index.d.ts"
52
+ },
53
+ "./errcorrect": {
54
+ "require": "./dist/errcorrect.js",
55
+ "types": "./dist/errcorrect.d.ts"
56
+ },
57
+ "./safety": {
58
+ "require": "./dist/safety.js",
59
+ "types": "./dist/safety.d.ts"
60
+ },
61
+ "./provenance": {
62
+ "require": "./dist/provenance.js",
63
+ "types": "./dist/provenance.d.ts"
64
+ },
65
+ "./stream": {
66
+ "require": "./dist/stream.js",
67
+ "types": "./dist/stream.d.ts"
68
+ },
69
+ "./progress": {
70
+ "require": "./dist/progress.js",
71
+ "types": "./dist/progress.d.ts"
72
+ },
73
+ "./auth": {
74
+ "require": "./dist/auth.js",
75
+ "types": "./dist/auth.d.ts"
76
+ },
77
+ "./api": {
78
+ "require": "./dist/api.js",
79
+ "types": "./dist/api.d.ts"
80
+ },
81
+ "./rpc": {
82
+ "require": "./dist/rpc.js",
83
+ "types": "./dist/rpc.d.ts"
84
+ },
85
+ "./aim": {
86
+ "require": "./dist/aim.js",
87
+ "types": "./dist/aim.d.ts"
88
+ },
89
+ "./scope": {
90
+ "require": "./dist/scope.js",
91
+ "types": "./dist/scope.d.ts"
92
+ }
93
+ },
94
+ "dependencies": {
95
+ "@bufbuild/protobuf": "^2.11.0",
96
+ "@clack/prompts": "^1.2.0",
97
+ "@connectrpc/connect": "^2.1.1",
98
+ "@connectrpc/connect-web": "^2.1.1",
99
+ "@hop-top/uri": "^0.2.0-alpha.1",
100
+ "better-sqlite3": "^12.8.0",
101
+ "commander": "^14.0.3",
102
+ "csv-stringify": "^6.7.0",
103
+ "eta": "^4.6.0",
104
+ "hono": "^4.12.10",
105
+ "js-yaml": "^4.1.1",
106
+ "picomatch": "^4.0.4",
107
+ "pino": "^10.3.1",
108
+ "semver": "^7.7.4"
109
+ },
110
+ "devDependencies": {
111
+ "@types/better-sqlite3": "^7.6.13",
112
+ "@types/js-yaml": "^4.0.9",
113
+ "@types/node": "^20",
114
+ "@types/picomatch": "^4.0.3",
115
+ "@types/semver": "^7.7.1",
116
+ "esbuild": "^0.25.12",
117
+ "eslint": "^10.2.0",
118
+ "tsup": "^8",
119
+ "typescript": "^5",
120
+ "typescript-eslint": "^8.58.0",
121
+ "vitest": "^1"
122
+ },
123
+ "engines": {
124
+ "node": ">=20"
125
+ },
126
+ "license": "MIT",
127
+ "scripts": {
128
+ "build": "tsup src/alias.ts src/cli.ts src/output.ts src/xdg.ts src/config.ts src/sqlstore.ts src/upgrade.ts src/llm.ts src/routellm.ts src/tui/index.ts src/errcorrect.ts src/safety.ts src/provenance.ts src/stream.ts src/progress.ts src/auth.ts src/api.ts src/rpc.ts src/aim.ts src/scope.ts --format cjs --dts",
129
+ "lint": "eslint src/",
130
+ "lint:fix": "eslint src/ --fix",
131
+ "test": "vitest run"
132
+ }
133
+ }
package/src/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # src
2
+
3
+ TypeScript implementation source files.