@afffun/codexbot 1.0.88 → 1.0.90

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@afffun/codexbot",
3
- "version": "1.0.88",
3
+ "version": "1.0.90",
4
4
  "description": "Thin npm bootstrap CLI for installing and operating Codexbot nodes",
5
5
  "type": "module",
6
6
  "author": "john88188 <john88188@outlook.com>",
@@ -3,6 +3,12 @@ import path from 'node:path';
3
3
  import { spawn } from 'node:child_process';
4
4
  import { resolveInstalledControlLayout } from './installed_layout_service.mjs';
5
5
 
6
+ const PROJECTED_FILES = Object.freeze([
7
+ Object.freeze({ name: 'auth.json', mode: '640' }),
8
+ Object.freeze({ name: 'config.toml', mode: '640' }),
9
+ Object.freeze({ name: 'models_cache.json', mode: '640' }),
10
+ ]);
11
+
6
12
  function runChild(spawnFn, command, args, options = {}) {
7
13
  return new Promise((resolve, reject) => {
8
14
  const child = spawnFn(command, args, {
@@ -33,6 +39,33 @@ function mapAuthModeToCodexArgs(mode) {
33
39
  throw new Error(`unsupported auth mode: ${mode}`);
34
40
  }
35
41
 
42
+ function buildProjectionScript(layout) {
43
+ const controlDir = trim(layout.codexHomeDir);
44
+ const agentHomeDir = trim(layout.agentHomeDir);
45
+ if (!controlDir || !agentHomeDir) return '';
46
+ const agentDir = path.join(agentHomeDir, '.codex');
47
+ const lines = [
48
+ 'set -euo pipefail',
49
+ `control_dir=${JSON.stringify(controlDir)}`,
50
+ `agent_dir=${JSON.stringify(agentDir)}`,
51
+ 'if [[ -d "$agent_dir" ]]; then',
52
+ ' :',
53
+ 'else',
54
+ ' mkdir -p "$agent_dir"',
55
+ 'fi',
56
+ ];
57
+ for (const entry of PROJECTED_FILES) {
58
+ lines.push(`src="$control_dir/${entry.name}"`);
59
+ lines.push(`dst="$agent_dir/${entry.name}"`);
60
+ lines.push('if [[ -f "$src" ]]; then');
61
+ lines.push(' rm -f "$dst"');
62
+ lines.push(' cp "$src" "$dst"');
63
+ lines.push(` chmod ${entry.mode} "$dst"`);
64
+ lines.push('fi');
65
+ }
66
+ return lines.join('\n');
67
+ }
68
+
36
69
  export function createNpmDistributionAuthService(deps = {}) {
37
70
  const fsSync = deps.fsSync || fs;
38
71
  const pathModule = deps.pathModule || path;
@@ -64,19 +97,40 @@ export function createNpmDistributionAuthService(deps = {}) {
64
97
  const isRoot = typeof processImpl.getuid === 'function' ? processImpl.getuid() === 0 : false;
65
98
  const envArgs = ['env', `CODEX_HOME=${layout.codexHomeDir}`, `PATH=${pathModule.dirname(layout.codexBinPath)}:${processImpl.env.PATH || ''}`, layout.codexBinPath, ...codexArgs];
66
99
  logger.log(`==> Run Codex auth command for ${layout.profile} install as ${layout.controlUser}`);
100
+ let exitCode;
67
101
  if (isRoot) {
68
- return runChild(spawnFn, 'sudo', ['-u', layout.controlUser, ...envArgs]);
69
- }
70
- if (currentUser && currentUser === layout.controlUser) {
71
- return runChild(spawnFn, layout.codexBinPath, codexArgs, {
102
+ exitCode = await runChild(spawnFn, 'sudo', ['-u', layout.controlUser, ...envArgs]);
103
+ } else if (currentUser && currentUser === layout.controlUser) {
104
+ exitCode = await runChild(spawnFn, layout.codexBinPath, codexArgs, {
72
105
  env: {
73
106
  ...processImpl.env,
74
107
  CODEX_HOME: layout.codexHomeDir,
75
108
  PATH: `${pathModule.dirname(layout.codexBinPath)}:${processImpl.env.PATH || ''}`,
76
109
  },
77
110
  });
111
+ } else {
112
+ exitCode = await runChild(spawnFn, 'sudo', ['-u', layout.controlUser, ...envArgs]);
78
113
  }
79
- return runChild(spawnFn, 'sudo', ['-u', layout.controlUser, ...envArgs]);
114
+
115
+ if (exitCode === 0 && layout.profile === 'split' && (mode === 'login' || mode === 'login-link')) {
116
+ const projectionScript = buildProjectionScript(layout);
117
+ if (projectionScript) {
118
+ const projectionArgs = ['-u', layout.controlUser, 'env', `CODEX_HOME=${layout.codexHomeDir}`, `PATH=${pathModule.dirname(layout.codexBinPath)}:${processImpl.env.PATH || ''}`, 'bash', '-lc', projectionScript];
119
+ if (isRoot || !(currentUser && currentUser === layout.controlUser)) {
120
+ await runChild(spawnFn, 'sudo', projectionArgs);
121
+ } else {
122
+ await runChild(spawnFn, 'bash', ['-lc', projectionScript], {
123
+ env: {
124
+ ...processImpl.env,
125
+ CODEX_HOME: layout.codexHomeDir,
126
+ PATH: `${pathModule.dirname(layout.codexBinPath)}:${processImpl.env.PATH || ''}`,
127
+ },
128
+ });
129
+ }
130
+ }
131
+ }
132
+
133
+ return exitCode;
80
134
  }
81
135
 
82
136
  function formatDoctorReport(options = {}) {
@@ -8,6 +8,7 @@ const SPLIT_DEFAULTS = Object.freeze({
8
8
  controlEnvFile: '/etc/codexbot/control.env',
9
9
  controlUser: 'codexbotd',
10
10
  codexHomeDir: '/var/lib/codexbot/control/home/.codex',
11
+ agentHomeDir: '/var/lib/codexbot/agent/home',
11
12
  });
12
13
 
13
14
  const LEGACY_DEFAULTS = Object.freeze({
@@ -16,6 +17,7 @@ const LEGACY_DEFAULTS = Object.freeze({
16
17
  controlEnvFile: '/home/codexbot/codexbot-telegram/.env',
17
18
  controlUser: 'codexbot',
18
19
  codexHomeDir: '/home/codexbot/codexbot-home/.codex',
20
+ agentHomeDir: '',
19
21
  });
20
22
 
21
23
  function trim(value, fallback = '') {
@@ -72,6 +74,9 @@ export function resolveInstalledControlLayout({
72
74
  || fileEnv.CODEXBOT_CONTROL_HOME && `${fileEnv.CODEXBOT_CONTROL_HOME}/.codex`,
73
75
  defaults.codexHomeDir,
74
76
  ));
77
+ const resolvedAgentHomeDir = resolvedProfile === 'split'
78
+ ? pathModule.resolve(trim(fileEnv.CODEXBOT_AGENT_HOME, defaults.agentHomeDir))
79
+ : '';
75
80
  const codexBinPath = pathModule.join(resolvedAppDir, 'node_modules', '.bin', 'codex');
76
81
  return {
77
82
  profile: resolvedProfile,
@@ -79,6 +84,7 @@ export function resolveInstalledControlLayout({
79
84
  controlEnvFile: envFile,
80
85
  controlUser: resolvedControlUser,
81
86
  codexHomeDir: resolvedCodexHomeDir,
87
+ agentHomeDir: resolvedAgentHomeDir,
82
88
  codexBinPath,
83
89
  env: fileEnv,
84
90
  currentUser: trim(env.USER || env.LOGNAME || ''),