@emesoft/tokenhub 0.9.3 → 0.9.4

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.
@@ -1,5 +1,5 @@
1
1
  import { hostname } from "node:os";
2
- import { appDataCacheDir, appDataDir, fetchModels, isInstalled, writeDesktopConfig } from "./desktop.js";
2
+ import { appDataDir, fetchModels, isInstalled, writeDesktopConfig } from "./desktop.js";
3
3
  import { openBrowser, pairDevice, resolvePoolUrl } from "./pair.js";
4
4
 
5
5
  /**
@@ -37,7 +37,7 @@ export async function runDesktop({
37
37
  return 1;
38
38
  }
39
39
  // Judged before the write, which creates the folder.
40
- const installed = isInstalled(dir, platform === "win32" ? appDataCacheDir(env, home) : null);
40
+ const installed = isInstalled(dir);
41
41
  let pool;
42
42
  try {
43
43
  pool = resolvePoolUrl(poolUrl);
package/lib/desktop.js CHANGED
@@ -1,4 +1,5 @@
1
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
1
+ import { randomUUID } from "node:crypto";
2
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
3
  import { homedir } from "node:os";
3
4
  import { join } from "node:path";
4
5
 
@@ -11,20 +12,25 @@ import { join } from "node:path";
11
12
  export const APP_FOLDER = "Claude-3p";
12
13
  /** Name of the configuration this writes and applies. */
13
14
  export const CONFIG_NAME = "TokenHub";
15
+ /** Desktop loads a configuration only when its id has this shape (a UUID) and ignores any other id (#829). */
16
+ const ID_SHAPE = /^[a-f0-9-]{36}$/;
17
+ /** The id releases before #829 wrote, which Desktop never loaded. */
18
+ const LEGACY_ID = "tokenhub";
14
19
  /** Effort levels Desktop knows, lowest first. */
15
20
  const EFFORTS = ["low", "medium", "high", "xhigh", "max"];
16
21
  /** Families Desktop recognises in `anthropicFamilyTier`. */
17
22
  const TIERS = ["sonnet", "opus", "haiku", "fable", "mythos"];
18
23
 
19
24
  /**
20
- * Where Claude Desktop keeps its 3p data per platform.
25
+ * Where Claude Desktop keeps its 3p data per platform. On Windows that is Local app data, not Roaming: Desktop 5.x
26
+ * reads its 3p config from `%LOCALAPPDATA%` (#829).
21
27
  * @param {string} [platform] Node platform name.
22
- * @param {Record<string, string | undefined>} [env] Environment to read APPDATA / XDG_CONFIG_HOME from.
28
+ * @param {Record<string, string | undefined>} [env] Environment to read LOCALAPPDATA / XDG_CONFIG_HOME from.
23
29
  * @param {string} [home] Home directory used when the variable is unset.
24
30
  * @returns {string | null} Absolute folder, or null on a platform Desktop does not ship for.
25
31
  */
26
32
  export function appDataDir(platform = process.platform, env = process.env, home = homedir()) {
27
- if (platform === "win32") return join(env.APPDATA || join(home, "AppData", "Roaming"), APP_FOLDER);
33
+ if (platform === "win32") return join(env.LOCALAPPDATA || join(home, "AppData", "Local"), APP_FOLDER);
28
34
  if (platform === "darwin") return join(home, "Library", "Application Support", APP_FOLDER);
29
35
  if (platform === "linux") return join(env.XDG_CONFIG_HOME || join(home, ".config"), APP_FOLDER);
30
36
  return null;
@@ -42,15 +48,15 @@ export function configLibraryDir(base) {
42
48
 
43
49
  /**
44
50
  * The configuration id this machine already uses for TokenHub, so a second run updates that record rather than
45
- * adding another. Falls back to a stable new id derived from the name, which is all Desktop needs.
51
+ * adding another. Falls back to a new UUID, the only id shape Desktop loads.
46
52
  * @param {string} dir Config library folder.
47
53
  * @returns {string} Configuration id.
48
54
  */
49
55
  export function configId(dir) {
50
56
  const meta = readJson(join(dir, "_meta.json"));
51
57
  const entries = Array.isArray(meta?.entries) ? meta.entries : [];
52
- const found = entries.find((entry) => entry?.name === CONFIG_NAME && typeof entry.id === "string");
53
- return found ? found.id : "tokenhub";
58
+ const found = entries.find((entry) => entry?.name === CONFIG_NAME && ID_SHAPE.test(String(entry.id)));
59
+ return found ? found.id : randomUUID();
54
60
  }
55
61
 
56
62
  /**
@@ -97,13 +103,14 @@ export function buildConfig({ baseUrl, key, models, effort = "medium" }) {
97
103
  }
98
104
 
99
105
  /**
100
- * Merge the configuration into `_meta.json` and make it the applied one.
106
+ * Merge the configuration into `_meta.json` and make it the applied one, dropping the entry an older release wrote
107
+ * under the id Desktop ignores.
101
108
  * @param {unknown} meta Existing `_meta.json` (null when absent).
102
109
  * @param {string} id Configuration id.
103
110
  * @returns {Record<string, unknown>} New `_meta.json`.
104
111
  */
105
112
  export function mergeMeta(meta, id) {
106
- const entries = Array.isArray(meta?.entries) ? meta.entries.filter((entry) => entry?.id !== id) : [];
113
+ const entries = Array.isArray(meta?.entries) ? meta.entries.filter((entry) => entry?.id !== id && entry?.id !== LEGACY_ID) : [];
107
114
  return { ...meta, appliedId: id, entries: [...entries, { id, name: CONFIG_NAME }] };
108
115
  }
109
116
 
@@ -156,29 +163,17 @@ export function writeDesktopConfig({ dir, baseUrl, key, models, effort }) {
156
163
  writeFileSync(configPath, `${JSON.stringify(buildConfig({ baseUrl, key, models, effort }), null, 2)}\n`);
157
164
  writeFileSync(metaPath, `${JSON.stringify(mergeMeta(readJson(metaPath), id), null, 2)}\n`);
158
165
  writeFileSync(desktopConfigPath, `${JSON.stringify(mergeDesktopConfig(readJson(desktopConfigPath)), null, 2)}\n`);
166
+ rmSync(join(library, `${LEGACY_ID}.json`), { force: true });
159
167
  return { configPath, metaPath, desktopConfigPath, models: models.length };
160
168
  }
161
169
 
162
170
  /**
163
- * Where Claude Desktop keeps its caches, storage and logs on Windows, beside the folder `appDataDir` returns: Desktop
164
- * splits its app data across Roaming (config) and Local (everything else), and either can exist first.
165
- * @param {Record<string, string | undefined>} [env] Environment to read LOCALAPPDATA from.
166
- * @param {string} [home] Home directory used when the variable is unset.
167
- * @returns {string | null} Absolute folder, or null on a platform Desktop does not split.
168
- */
169
- export function appDataCacheDir(env = process.env, home = homedir()) {
170
- return join(env.LOCALAPPDATA || join(home, "AppData", "Local"), APP_FOLDER);
171
- }
172
-
173
- /**
174
- * Whether Desktop is installed here, judged by its app-data folders: the one it reads config from, or the one it keeps
175
- * caches in. Desktop creates one before the other, so either proves an install (#790).
176
- * @param {string | null} dir Config folder from `appDataDir`.
177
- * @param {string | null} [cacheDir] Cache folder from `appDataCacheDir`.
178
- * @returns {boolean} True when either exists.
171
+ * Whether Desktop is installed here, judged by its 3p app-data folder.
172
+ * @param {string | null} dir Folder from `appDataDir`.
173
+ * @returns {boolean} True when it exists.
179
174
  */
180
- export function isInstalled(dir, cacheDir = null) {
181
- return [dir, cacheDir].some((folder) => Boolean(folder) && existsSync(folder));
175
+ export function isInstalled(dir) {
176
+ return Boolean(dir) && existsSync(dir);
182
177
  }
183
178
 
184
179
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emesoft/tokenhub",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "description": "Connect Claude Code to a TokenHub pool in one command, and track your spend from the terminal",
5
5
  "keywords": [
6
6
  "claude",