@empirical-gauzza/cli 1.0.37 → 1.0.38

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,176 +1,176 @@
1
- 'use strict';
2
-
3
- const os = require('os');
4
- const path = require('path');
5
- const fs = require('fs');
6
- const { execFileSync } = require('child_process');
7
- const { httpJson } = require('./http');
8
- const { getEnv } = require('./env');
9
- const { resolveAccessToken, resolveApiBaseUrl, buildApiAuthHeaders } = require('./auth');
10
- const { readStoredOAuthClient } = require('./oauthClient');
11
- const { readJsonIfExists, writeFile, ensureDir } = require('./fsUtils');
12
-
13
- const WORKSPACE_CONTEXT_TIMEOUT_MS = 2000;
14
- const SELECTED_WORKSPACE_PATH = path.join(os.homedir(), '.empirical', 'workspace-selection.json');
15
-
16
- // Per-project binding (2026-09-24). The global selection above is one file per machine, so any
17
- // session in any repo flips it for every other session: a WOB session left the CLI on the WOB team
18
- // workspace and every tag-scoped recall from an empirical_v2 session returned [] while the memories
19
- // existed. The unit that actually varies is the project directory, like git config, so a project
20
- // can pin its workspace in <repo>/.empirical/workspace.json and `use` writes there by default when
21
- // run inside a git repo. Resolution order, first hit wins: env, project, global, then the server's
22
- // per-client selection exactly as before. No project file and no env means today's behavior.
23
- const PROJECT_WORKSPACE_DIRNAME = '.empirical';
24
- const PROJECT_WORKSPACE_FILENAME = 'workspace.json';
25
- const WORKSPACE_ENV_VAR = 'EMPIRICAL_WORKSPACE_ID';
26
-
27
- function readSelectedWorkspace() {
28
- const value = readJsonIfExists(SELECTED_WORKSPACE_PATH);
29
- return value && typeof value.workspaceId === 'string' ? value : null;
30
- }
31
-
32
- function writeSelectedWorkspace(workspace) {
33
- ensureDir(path.dirname(SELECTED_WORKSPACE_PATH));
34
- writeFile(SELECTED_WORKSPACE_PATH, `${JSON.stringify(workspace, null, 2)}\n`);
35
- }
36
-
37
- function clearSelectedWorkspace() {
38
- if (fs.existsSync(SELECTED_WORKSPACE_PATH)) fs.unlinkSync(SELECTED_WORKSPACE_PATH);
39
- }
40
-
41
- /**
42
- * Nearest <dir>/.empirical/workspace.json walking up from `startDir` to the filesystem root.
43
- * Returns { path, value } or null. A malformed file is ignored rather than fatal: a broken
44
- * binding must not take the CLI down, it just falls through to the next source.
45
- */
46
- function findProjectWorkspaceFile(startDir = process.cwd()) {
47
- let dir = path.resolve(startDir);
48
- for (;;) {
49
- const candidate = path.join(dir, PROJECT_WORKSPACE_DIRNAME, PROJECT_WORKSPACE_FILENAME);
50
- const value = readJsonIfExists(candidate);
51
- if (value && typeof value.workspaceId === 'string' && value.workspaceId.trim()) {
52
- return { path: candidate, value };
53
- }
54
- const parent = path.dirname(dir);
55
- if (parent === dir) return null;
56
- dir = parent;
57
- }
58
- }
59
-
60
- /**
61
- * The git toplevel for `cwd`, or null outside a repo (or without git). Used to decide where
62
- * `workspace use` writes by default and where the project binding lives.
63
- */
64
- function findGitRoot(cwd = process.cwd()) {
65
- try {
66
- const out = execFileSync('git', ['rev-parse', '--show-toplevel'], {
67
- cwd,
68
- stdio: ['ignore', 'pipe', 'ignore'],
69
- encoding: 'utf8'
70
- });
71
- const root = String(out || '').trim();
72
- return root ? path.resolve(root) : null;
73
- } catch (_error) {
74
- return null;
75
- }
76
- }
77
-
78
- function projectWorkspacePathFor(rootDir) {
79
- return path.join(rootDir, PROJECT_WORKSPACE_DIRNAME, PROJECT_WORKSPACE_FILENAME);
80
- }
81
-
82
- function writeProjectWorkspace(rootDir, workspace) {
83
- const filePath = projectWorkspacePathFor(rootDir);
84
- ensureDir(path.dirname(filePath));
85
- writeFile(filePath, `${JSON.stringify({ ...workspace, boundAt: new Date().toISOString() }, null, 2)}\n`);
86
- return filePath;
87
- }
88
-
89
- function clearProjectWorkspace(rootDir) {
90
- const filePath = projectWorkspacePathFor(rootDir);
91
- if (fs.existsSync(filePath)) {
92
- fs.unlinkSync(filePath);
93
- return filePath;
94
- }
95
- return null;
96
- }
97
-
98
- /**
99
- * The single answer to "which workspace does this CLI invocation act on", before asking the
100
- * server. Every memory command and `workspace current` read this, so reads and writes can never
101
- * disagree again. Returns { workspaceId, label, key, source, path } or null when nothing local
102
- * is set (the caller then falls back to the server's per-client selection).
103
- */
104
- function resolveLocalWorkspaceSelection({ cwd = process.cwd(), env = process.env } = {}) {
105
- const fromEnv = String(env[WORKSPACE_ENV_VAR] || '').trim();
106
- if (fromEnv) {
107
- return { workspaceId: fromEnv, label: null, key: null, source: 'env', path: WORKSPACE_ENV_VAR };
108
- }
109
- const project = findProjectWorkspaceFile(cwd);
110
- if (project) {
111
- const { workspaceId, label = null, key = null } = project.value;
112
- return { workspaceId: workspaceId.trim(), label, key, source: 'project', path: project.path };
113
- }
114
- const global = readSelectedWorkspace();
115
- if (global) {
116
- return { workspaceId: global.workspaceId, label: global.label || null, key: global.key || null, source: 'global', path: SELECTED_WORKSPACE_PATH };
117
- }
118
- return null;
119
- }
120
-
121
- async function withSelectedWorkspaceContext(fn) {
122
- const local = resolveLocalWorkspaceSelection();
123
- if (local?.workspaceId) {
124
- const previousWorkspaceId = globalThis.__empiricalCliWorkspaceId || null;
125
- globalThis.__empiricalCliWorkspaceId = local.workspaceId;
126
- try {
127
- return await fn();
128
- } finally {
129
- globalThis.__empiricalCliWorkspaceId = previousWorkspaceId;
130
- }
131
- }
132
-
133
- const accessToken = resolveAccessToken();
134
- const clientId = getEnv('EMPIRICAL_CLIENT_ID') || readStoredOAuthClient()?.clientId;
135
- if (!accessToken || !clientId) return fn();
136
-
137
- const previousWorkspaceId = globalThis.__empiricalCliWorkspaceId || null;
138
- globalThis.__empiricalCliWorkspaceId = null;
139
- try {
140
- const baseUrl = resolveApiBaseUrl();
141
- const currentUrl = new URL(
142
- `/oauth/clients/${encodeURIComponent(clientId)}/workspace-selection`,
143
- baseUrl
144
- ).toString();
145
- try {
146
- const payload = await httpJson(
147
- currentUrl,
148
- 'GET',
149
- null,
150
- buildApiAuthHeaders(accessToken),
151
- WORKSPACE_CONTEXT_TIMEOUT_MS
152
- );
153
- globalThis.__empiricalCliWorkspaceId = payload?.current?.workspaceId || null;
154
- } catch (_error) {
155
- globalThis.__empiricalCliWorkspaceId = null;
156
- }
157
- return await fn();
158
- } finally {
159
- globalThis.__empiricalCliWorkspaceId = previousWorkspaceId;
160
- }
161
- }
162
-
163
- module.exports = {
164
- withSelectedWorkspaceContext,
165
- resolveLocalWorkspaceSelection,
166
- readSelectedWorkspace,
167
- writeSelectedWorkspace,
168
- clearSelectedWorkspace,
169
- findProjectWorkspaceFile,
170
- findGitRoot,
171
- projectWorkspacePathFor,
172
- writeProjectWorkspace,
173
- clearProjectWorkspace,
174
- WORKSPACE_ENV_VAR,
175
- SELECTED_WORKSPACE_PATH
176
- };
1
+ 'use strict';
2
+
3
+ const os = require('os');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const { execFileSync } = require('child_process');
7
+ const { httpJson } = require('./http');
8
+ const { getEnv } = require('./env');
9
+ const { resolveAccessToken, resolveApiBaseUrl, buildApiAuthHeaders } = require('./auth');
10
+ const { readStoredOAuthClient } = require('./oauthClient');
11
+ const { readJsonIfExists, writeFile, ensureDir } = require('./fsUtils');
12
+
13
+ const WORKSPACE_CONTEXT_TIMEOUT_MS = 2000;
14
+ const SELECTED_WORKSPACE_PATH = path.join(os.homedir(), '.empirical', 'workspace-selection.json');
15
+
16
+ // Per-project binding (2026-09-24). The global selection above is one file per machine, so any
17
+ // session in any repo flips it for every other session: a WOB session left the CLI on the WOB team
18
+ // workspace and every tag-scoped recall from an empirical_v2 session returned [] while the memories
19
+ // existed. The unit that actually varies is the project directory, like git config, so a project
20
+ // can pin its workspace in <repo>/.empirical/workspace.json and `use` writes there by default when
21
+ // run inside a git repo. Resolution order, first hit wins: env, project, global, then the server's
22
+ // per-client selection exactly as before. No project file and no env means today's behavior.
23
+ const PROJECT_WORKSPACE_DIRNAME = '.empirical';
24
+ const PROJECT_WORKSPACE_FILENAME = 'workspace.json';
25
+ const WORKSPACE_ENV_VAR = 'EMPIRICAL_WORKSPACE_ID';
26
+
27
+ function readSelectedWorkspace() {
28
+ const value = readJsonIfExists(SELECTED_WORKSPACE_PATH);
29
+ return value && typeof value.workspaceId === 'string' ? value : null;
30
+ }
31
+
32
+ function writeSelectedWorkspace(workspace) {
33
+ ensureDir(path.dirname(SELECTED_WORKSPACE_PATH));
34
+ writeFile(SELECTED_WORKSPACE_PATH, `${JSON.stringify(workspace, null, 2)}\n`);
35
+ }
36
+
37
+ function clearSelectedWorkspace() {
38
+ if (fs.existsSync(SELECTED_WORKSPACE_PATH)) fs.unlinkSync(SELECTED_WORKSPACE_PATH);
39
+ }
40
+
41
+ /**
42
+ * Nearest <dir>/.empirical/workspace.json walking up from `startDir` to the filesystem root.
43
+ * Returns { path, value } or null. A malformed file is ignored rather than fatal: a broken
44
+ * binding must not take the CLI down, it just falls through to the next source.
45
+ */
46
+ function findProjectWorkspaceFile(startDir = process.cwd()) {
47
+ let dir = path.resolve(startDir);
48
+ for (;;) {
49
+ const candidate = path.join(dir, PROJECT_WORKSPACE_DIRNAME, PROJECT_WORKSPACE_FILENAME);
50
+ const value = readJsonIfExists(candidate);
51
+ if (value && typeof value.workspaceId === 'string' && value.workspaceId.trim()) {
52
+ return { path: candidate, value };
53
+ }
54
+ const parent = path.dirname(dir);
55
+ if (parent === dir) return null;
56
+ dir = parent;
57
+ }
58
+ }
59
+
60
+ /**
61
+ * The git toplevel for `cwd`, or null outside a repo (or without git). Used to decide where
62
+ * `workspace use` writes by default and where the project binding lives.
63
+ */
64
+ function findGitRoot(cwd = process.cwd()) {
65
+ try {
66
+ const out = execFileSync('git', ['rev-parse', '--show-toplevel'], {
67
+ cwd,
68
+ stdio: ['ignore', 'pipe', 'ignore'],
69
+ encoding: 'utf8'
70
+ });
71
+ const root = String(out || '').trim();
72
+ return root ? path.resolve(root) : null;
73
+ } catch (_error) {
74
+ return null;
75
+ }
76
+ }
77
+
78
+ function projectWorkspacePathFor(rootDir) {
79
+ return path.join(rootDir, PROJECT_WORKSPACE_DIRNAME, PROJECT_WORKSPACE_FILENAME);
80
+ }
81
+
82
+ function writeProjectWorkspace(rootDir, workspace) {
83
+ const filePath = projectWorkspacePathFor(rootDir);
84
+ ensureDir(path.dirname(filePath));
85
+ writeFile(filePath, `${JSON.stringify({ ...workspace, boundAt: new Date().toISOString() }, null, 2)}\n`);
86
+ return filePath;
87
+ }
88
+
89
+ function clearProjectWorkspace(rootDir) {
90
+ const filePath = projectWorkspacePathFor(rootDir);
91
+ if (fs.existsSync(filePath)) {
92
+ fs.unlinkSync(filePath);
93
+ return filePath;
94
+ }
95
+ return null;
96
+ }
97
+
98
+ /**
99
+ * The single answer to "which workspace does this CLI invocation act on", before asking the
100
+ * server. Every memory command and `workspace current` read this, so reads and writes can never
101
+ * disagree again. Returns { workspaceId, label, key, source, path } or null when nothing local
102
+ * is set (the caller then falls back to the server's per-client selection).
103
+ */
104
+ function resolveLocalWorkspaceSelection({ cwd = process.cwd(), env = process.env } = {}) {
105
+ const fromEnv = String(env[WORKSPACE_ENV_VAR] || '').trim();
106
+ if (fromEnv) {
107
+ return { workspaceId: fromEnv, label: null, key: null, source: 'env', path: WORKSPACE_ENV_VAR };
108
+ }
109
+ const project = findProjectWorkspaceFile(cwd);
110
+ if (project) {
111
+ const { workspaceId, label = null, key = null } = project.value;
112
+ return { workspaceId: workspaceId.trim(), label, key, source: 'project', path: project.path };
113
+ }
114
+ const global = readSelectedWorkspace();
115
+ if (global) {
116
+ return { workspaceId: global.workspaceId, label: global.label || null, key: global.key || null, source: 'global', path: SELECTED_WORKSPACE_PATH };
117
+ }
118
+ return null;
119
+ }
120
+
121
+ async function withSelectedWorkspaceContext(fn) {
122
+ const local = resolveLocalWorkspaceSelection();
123
+ if (local?.workspaceId) {
124
+ const previousWorkspaceId = globalThis.__empiricalCliWorkspaceId || null;
125
+ globalThis.__empiricalCliWorkspaceId = local.workspaceId;
126
+ try {
127
+ return await fn();
128
+ } finally {
129
+ globalThis.__empiricalCliWorkspaceId = previousWorkspaceId;
130
+ }
131
+ }
132
+
133
+ const accessToken = resolveAccessToken();
134
+ const clientId = getEnv('EMPIRICAL_CLIENT_ID') || readStoredOAuthClient()?.clientId;
135
+ if (!accessToken || !clientId) return fn();
136
+
137
+ const previousWorkspaceId = globalThis.__empiricalCliWorkspaceId || null;
138
+ globalThis.__empiricalCliWorkspaceId = null;
139
+ try {
140
+ const baseUrl = resolveApiBaseUrl();
141
+ const currentUrl = new URL(
142
+ `/oauth/clients/${encodeURIComponent(clientId)}/workspace-selection`,
143
+ baseUrl
144
+ ).toString();
145
+ try {
146
+ const payload = await httpJson(
147
+ currentUrl,
148
+ 'GET',
149
+ null,
150
+ buildApiAuthHeaders(accessToken),
151
+ WORKSPACE_CONTEXT_TIMEOUT_MS
152
+ );
153
+ globalThis.__empiricalCliWorkspaceId = payload?.current?.workspaceId || null;
154
+ } catch (_error) {
155
+ globalThis.__empiricalCliWorkspaceId = null;
156
+ }
157
+ return await fn();
158
+ } finally {
159
+ globalThis.__empiricalCliWorkspaceId = previousWorkspaceId;
160
+ }
161
+ }
162
+
163
+ module.exports = {
164
+ withSelectedWorkspaceContext,
165
+ resolveLocalWorkspaceSelection,
166
+ readSelectedWorkspace,
167
+ writeSelectedWorkspace,
168
+ clearSelectedWorkspace,
169
+ findProjectWorkspaceFile,
170
+ findGitRoot,
171
+ projectWorkspacePathFor,
172
+ writeProjectWorkspace,
173
+ clearProjectWorkspace,
174
+ WORKSPACE_ENV_VAR,
175
+ SELECTED_WORKSPACE_PATH
176
+ };
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
- {
2
- "name": "@empirical-gauzza/cli",
3
- "version": "1.0.37",
4
- "description": "Persistent cross-tool memory for AI agents. Give Claude Code, Codex, Copilot, Cursor, and ChatGPT one shared memory: save project context once, recall it everywhere via CLI or MCP.",
5
- "keywords": [
6
- "memory",
7
- "mcp",
8
- "agent memory",
9
- "ai memory",
10
- "claude",
11
- "chatgpt",
12
- "model context protocol",
13
- "persistent memory",
14
- "claude-code",
15
- "codex",
16
- "knowledge base"
17
- ],
18
- "type": "commonjs",
19
- "license": "MIT",
20
- "homepage": "https://empirical.gauzza.com/docs/cli-details",
21
- "publishConfig": {
22
- "access": "public"
23
- },
24
- "bin": {
25
- "empirical": "bin/empirical.js"
26
- },
27
- "engines": {
28
- "node": ">=18.0.0"
29
- },
30
- "dependencies": {
31
- "gray-matter": "^4.0.3"
32
- }
33
- }
1
+ {
2
+ "name": "@empirical-gauzza/cli",
3
+ "version": "1.0.38",
4
+ "description": "Persistent cross-tool memory for AI agents. Give Claude Code, Codex, Copilot, Cursor, and ChatGPT one shared memory: save project context once, recall it everywhere via CLI or MCP.",
5
+ "keywords": [
6
+ "memory",
7
+ "mcp",
8
+ "agent memory",
9
+ "ai memory",
10
+ "claude",
11
+ "chatgpt",
12
+ "model context protocol",
13
+ "persistent memory",
14
+ "claude-code",
15
+ "codex",
16
+ "knowledge base"
17
+ ],
18
+ "type": "commonjs",
19
+ "license": "MIT",
20
+ "homepage": "https://empirical.gauzza.com/docs/cli-details",
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "bin": {
25
+ "empirical": "bin/empirical.js"
26
+ },
27
+ "engines": {
28
+ "node": ">=18.0.0"
29
+ },
30
+ "dependencies": {
31
+ "gray-matter": "^4.0.3"
32
+ }
33
+ }