@codelia/cli 0.1.90 → 0.1.91

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.
@@ -2,13 +2,13 @@ import {
2
2
  CLI_VERSION,
3
3
  resolveCliPackageRoot,
4
4
  resolveLoadedCliPackageRoot
5
- } from "./chunk-PG452V5E.js";
5
+ } from "./chunk-445VMRAL.js";
6
6
  import {
7
7
  NOOP_LAUNCH_UPDATE_SESSION
8
- } from "./chunk-QYWLK4HY.js";
8
+ } from "./chunk-KQTBRN7C.js";
9
9
  import {
10
10
  UPDATE_REEXEC_ENV
11
- } from "./chunk-S7OVJIGV.js";
11
+ } from "./chunk-BYLYQTEO.js";
12
12
 
13
13
  // src/tui/launcher.ts
14
14
  import { spawn } from "child_process";
@@ -26,7 +26,6 @@ var TUI_PACKAGE_BY_TARGET = {
26
26
  };
27
27
  var describeError = (error) => error instanceof Error ? error.message : String(error);
28
28
  var splitArgs = (value) => value.split(/\s+/).map((part) => part.trim()).filter((part) => part.length > 0);
29
- var quoteShellWord = (value) => `'${value.replace(/'/g, "'\\''")}'`;
30
29
  var isExecutable = (candidate) => {
31
30
  try {
32
31
  fsSync.accessSync(candidate, fsSync.constants.X_OK);
@@ -83,29 +82,97 @@ var resolveOptionalTuiBinaryPath = (platform = process.platform, arch = process.
83
82
  resolveTuiBinaryName(platform)
84
83
  );
85
84
  };
86
- var resolveRuntimeEnvForTui = (env, resolveRuntimeEntry = resolveBundledRuntimeEntry) => {
87
- if (env.CODELIA_RUNTIME_CMD || env.CODELIA_RUNTIME_ARGS) {
88
- return env;
89
- }
85
+ var resolveBundledRuntimeLaunch = (env, resolveRuntimeEntry = resolveBundledRuntimeEntry) => {
86
+ if (env.CODELIA_RUNTIME_CMD || env.CODELIA_RUNTIME_ARGS) return void 0;
90
87
  const runtimeEntry = resolveRuntimeEntry();
91
- if (!runtimeEntry) {
92
- return env;
93
- }
88
+ return runtimeEntry ? { command: process.execPath, args: [runtimeEntry] } : void 0;
89
+ };
90
+ var TUI_LAUNCH_CONTEXT_FLAG = "--internal-launch-context";
91
+ var buildTuiLaunchContext = ({
92
+ env,
93
+ cliVersion = CLI_VERSION,
94
+ update = NOOP_LAUNCH_UPDATE_SESSION,
95
+ launcherEntry,
96
+ resolveRuntimeEntry = resolveBundledRuntimeEntry
97
+ }) => {
98
+ const runtime = resolveBundledRuntimeLaunch(env, resolveRuntimeEntry);
99
+ const version = cliVersion.trim();
94
100
  return {
95
- ...env,
96
- CODELIA_RUNTIME_CMD: process.execPath,
97
- CODELIA_RUNTIME_ARGS: quoteShellWord(runtimeEntry)
101
+ ...version ? { cli_version: version } : {},
102
+ ...runtime ? { runtime } : {},
103
+ ...update.noticeFile ? { update_notice_file: update.noticeFile } : {},
104
+ // Bind update controls to this launcher, not to PATH or the runtime.
105
+ ...launcherEntry ? {
106
+ update_control: {
107
+ node: process.execPath,
108
+ entry: path.resolve(launcherEntry)
109
+ }
110
+ } : {},
111
+ ...update.startupConfirmations?.length ? { startup_confirmations: update.startupConfirmations } : {}
98
112
  };
99
113
  };
100
- var resolveLaunchEnvForTui = (env, cliVersion = CLI_VERSION, resolveRuntimeEntry = resolveBundledRuntimeEntry) => {
101
- const runtimeEnv = resolveRuntimeEnvForTui(env, resolveRuntimeEntry);
102
- if (runtimeEnv.CODELIA_CLI_VERSION || cliVersion.trim().length === 0) {
103
- return runtimeEnv;
114
+ var TUI_VALUE_FLAGS = /* @__PURE__ */ new Set([
115
+ "--initial-message",
116
+ "--initial-user-message",
117
+ "--tui-mode",
118
+ "--sandbox-mode",
119
+ "--approval-mode"
120
+ ]);
121
+ var isLaunchContextOption = (arg) => arg.startsWith(`${TUI_LAUNCH_CONTEXT_FLAG}=`);
122
+ var buildTuiArgs = (context, forwarded, prefixArgs = []) => {
123
+ const cleanedPrefix = [];
124
+ let pendingValue = false;
125
+ for (const arg of prefixArgs) {
126
+ if (pendingValue) {
127
+ cleanedPrefix.push(arg);
128
+ pendingValue = false;
129
+ continue;
130
+ }
131
+ if (TUI_VALUE_FLAGS.has(arg)) {
132
+ cleanedPrefix.push(arg);
133
+ pendingValue = true;
134
+ continue;
135
+ }
136
+ if (isLaunchContextOption(arg)) continue;
137
+ cleanedPrefix.push(arg);
104
138
  }
105
- return {
106
- ...runtimeEnv,
107
- CODELIA_CLI_VERSION: cliVersion
108
- };
139
+ let startIndex = 0;
140
+ if (pendingValue && forwarded.length > 0) {
141
+ cleanedPrefix.push(forwarded[0]);
142
+ pendingValue = false;
143
+ startIndex = 1;
144
+ }
145
+ const cleanedForwarded = [];
146
+ let afterDoubleDash = false;
147
+ for (let i = startIndex; i < forwarded.length; i += 1) {
148
+ const arg = forwarded[i];
149
+ if (afterDoubleDash) {
150
+ cleanedForwarded.push(arg);
151
+ continue;
152
+ }
153
+ if (pendingValue) {
154
+ cleanedForwarded.push(arg);
155
+ pendingValue = false;
156
+ continue;
157
+ }
158
+ if (arg === "--") {
159
+ cleanedForwarded.push(arg);
160
+ afterDoubleDash = true;
161
+ continue;
162
+ }
163
+ if (TUI_VALUE_FLAGS.has(arg)) {
164
+ cleanedForwarded.push(arg);
165
+ pendingValue = true;
166
+ continue;
167
+ }
168
+ if (isLaunchContextOption(arg)) continue;
169
+ cleanedForwarded.push(arg);
170
+ }
171
+ return [
172
+ ...cleanedPrefix,
173
+ `${TUI_LAUNCH_CONTEXT_FLAG}=${JSON.stringify(context)}`,
174
+ ...cleanedForwarded
175
+ ];
109
176
  };
110
177
  var resolveTuiCommand = (options = {}) => {
111
178
  const env = options.env ?? process.env;
@@ -157,9 +224,14 @@ var runTui = (args, update = NOOP_LAUNCH_UPDATE_SESSION, cliPackageRoot = resolv
157
224
  void initStorage();
158
225
  const tuiCmd = resolveTuiCommand({ cliPackageRoot });
159
226
  const tuiArgs = process.env.CODELIA_TUI_ARGS ? splitArgs(process.env.CODELIA_TUI_ARGS) : [];
160
- const tuiEnv = { ...resolveLaunchEnvForTui(process.env), ...update.tuiEnv };
227
+ const context = buildTuiLaunchContext({
228
+ env: process.env,
229
+ update,
230
+ launcherEntry: process.argv[1]
231
+ });
232
+ const tuiEnv = { ...process.env };
161
233
  delete tuiEnv[UPDATE_REEXEC_ENV];
162
- const child = spawn(tuiCmd, [...tuiArgs, ...args], {
234
+ const child = spawn(tuiCmd, buildTuiArgs(context, args, tuiArgs), {
163
235
  stdio: "inherit",
164
236
  env: tuiEnv
165
237
  });
@@ -195,8 +267,10 @@ var runTui = (args, update = NOOP_LAUNCH_UPDATE_SESSION, cliPackageRoot = resolv
195
267
  export {
196
268
  resolvePlatformTuiPackageName,
197
269
  resolveOptionalTuiBinaryPath,
198
- resolveRuntimeEnvForTui,
199
- resolveLaunchEnvForTui,
270
+ resolveBundledRuntimeLaunch,
271
+ TUI_LAUNCH_CONTEXT_FLAG,
272
+ buildTuiLaunchContext,
273
+ buildTuiArgs,
200
274
  resolveTuiCommand,
201
275
  runTui
202
276
  };
@@ -36,7 +36,7 @@ var resolveLoadedCliPackageRoot = () => {
36
36
  };
37
37
 
38
38
  // src/version.ts
39
- var CLI_VERSION = "0.1.90".trim().length > 0 ? "0.1.90" : "0.0.0";
39
+ var CLI_VERSION = "0.1.91".trim().length > 0 ? "0.1.91" : "0.0.0";
40
40
 
41
41
  export {
42
42
  resolveCliPackageRoot,
@@ -545,8 +545,8 @@ var recordApplyFailure = async (stateFile, version, message, now, unmanaged) =>
545
545
  };
546
546
 
547
547
  // src/update/check.ts
548
- var CHECK_SUCCESS_TTL_MS = 24 * 60 * 60 * 1e3;
549
- var CHECK_FAILURE_TTL_MS = 60 * 60 * 1e3;
548
+ var CHECK_SUCCESS_TTL_MS = 2 * 60 * 60 * 1e3;
549
+ var CHECK_FAILURE_TTL_MS = 5 * 60 * 1e3;
550
550
  var resolveEffectiveChannel = (currentVersion, configuredChannel) => configuredChannel ?? inferChannelFromVersion(currentVersion);
551
551
  var isMatchingCheckScope = (state, scope) => state.registry_id === scope.registryId && state.channel === scope.channel && state.configured_channel === scope.configuredChannel;
552
552
  var shouldCheckNow = (state, options) => {
@@ -857,78 +857,8 @@ var buildUpdateCommand = (method, target) => {
857
857
  }
858
858
  };
859
859
 
860
- // src/update/notices.ts
861
- import { randomBytes as randomBytes2 } from "crypto";
862
- import fsSync4, { promises as fs2 } from "fs";
863
- import path5 from "path";
864
- var UPDATE_NOTICE_FILE_ENV = "CODELIA_UPDATE_NOTICE_FILE";
865
- var STALE_NOTICE_AGE_MS = 60 * 60 * 1e3;
866
- var commandHint = (command) => command ? `Update with: ${command}` : "Update using the package manager that installed Codelia.";
867
- var buildStartupNotices = (state, options) => {
868
- const notices = [];
869
- if (options.reexecFrom && options.reexecFrom !== options.currentVersion) {
870
- notices.push(
871
- `Updated Codelia ${options.reexecFrom} -> ${options.currentVersion}.`
872
- );
873
- }
874
- if (options.mode === "off") return notices;
875
- const latest = state.latest_version;
876
- if (!latest || !isNewerVersion(latest, options.currentVersion)) {
877
- return notices;
878
- }
879
- const channel = state.channel ?? "latest";
880
- const command = options.method ? buildUpdateCommand(options.method, { version: latest, channel }) : null;
881
- if (state.apply_failure && state.apply_failure.version === latest) {
882
- const hint = state.apply_failure.unmanaged ? commandHint(null) : commandHint(command);
883
- notices.push(
884
- `Automatic update to ${latest} failed (${state.apply_failure.message}). ${hint}`
885
- );
886
- return notices;
887
- }
888
- if (options.mode === "notify" || !options.method?.autoApply.supported) {
889
- const hint = options.method?.autoApply.supported ? "Run `codelia update` to install." : commandHint(command);
890
- notices.push(
891
- `Update available: ${options.currentVersion} -> ${latest}. ${hint}`
892
- );
893
- }
894
- return notices;
895
- };
896
- var writeStartupNoticeFile = async (paths, notices) => {
897
- if (notices.length === 0) return null;
898
- await fs2.mkdir(paths.noticesDir, { recursive: true, mode: 448 });
899
- void cleanupStaleNotices(paths.noticesDir);
900
- const file = path5.join(
901
- paths.noticesDir,
902
- `notice-${process.pid}-${randomBytes2(4).toString("hex")}.json`
903
- );
904
- await writeJsonAtomic(file, { notices: [...notices] });
905
- return file;
906
- };
907
- var removeNoticeFileSync = (file) => {
908
- if (!file) return;
909
- try {
910
- fsSync4.rmSync(file, { force: true });
911
- } catch {
912
- }
913
- };
914
- var cleanupStaleNotices = async (dir) => {
915
- try {
916
- const cutoff = Date.now() - STALE_NOTICE_AGE_MS;
917
- const entries = await fs2.readdir(dir);
918
- for (const entry of entries) {
919
- if (!entry.startsWith("notice-") || !entry.endsWith(".json")) continue;
920
- const full = path5.join(dir, entry);
921
- const stat = await fs2.lstat(full).catch(() => null);
922
- if (stat?.isFile() && stat.mtimeMs < cutoff) {
923
- await fs2.rm(full, { force: true }).catch(() => void 0);
924
- }
925
- }
926
- } catch {
927
- }
928
- };
929
-
930
860
  // src/update/settings.ts
931
- import { promises as fs3 } from "fs";
861
+ import { promises as fs2 } from "fs";
932
862
  import {
933
863
  isValidUpdateChannel as isValidUpdateChannel3,
934
864
  parseUpdateMode
@@ -960,18 +890,21 @@ var readGlobalUpdateConfig = async (configFile) => {
960
890
  const warnings = [];
961
891
  let rawText;
962
892
  try {
963
- rawText = await fs3.readFile(configFile, "utf8");
964
- } catch {
965
- return { warnings };
893
+ rawText = await fs2.readFile(configFile, "utf8");
894
+ } catch (error) {
895
+ return { modeUnset: isRecord2(error) && error.code === "ENOENT", warnings };
966
896
  }
967
897
  let parsed;
968
898
  try {
969
899
  parsed = JSON.parse(rawText);
970
900
  } catch {
971
- return { warnings };
901
+ return { modeUnset: false, warnings };
972
902
  }
973
- if (!isRecord2(parsed) || !isRecord2(parsed.update)) {
974
- return { warnings };
903
+ if (!isRecord2(parsed)) {
904
+ return { modeUnset: false, warnings };
905
+ }
906
+ if (!isRecord2(parsed.update)) {
907
+ return { modeUnset: parsed.update === void 0, warnings };
975
908
  }
976
909
  const update = parsed.update;
977
910
  let mode;
@@ -993,7 +926,7 @@ var readGlobalUpdateConfig = async (configFile) => {
993
926
  );
994
927
  }
995
928
  }
996
- return { mode, channel, warnings };
929
+ return { mode, modeUnset: update.mode === void 0, channel, warnings };
997
930
  };
998
931
  var resolveUpdateSettings = async (options) => {
999
932
  const env = options.env ?? process.env;
@@ -1018,11 +951,85 @@ var resolveUpdateSettings = async (options) => {
1018
951
  );
1019
952
  }
1020
953
  }
1021
- let mode = envMode.value ?? fromConfig.mode ?? "notify";
1022
- if (!options.interactive || isCiEnvironment(env)) {
1023
- mode = "off";
954
+ const savedMode = fromConfig.mode ?? "notify";
955
+ const disabled = !options.interactive || isCiEnvironment(env);
956
+ return {
957
+ mode: disabled ? "off" : envMode.value ?? savedMode,
958
+ savedMode,
959
+ offerModeChoice: !disabled && fromConfig.modeUnset && !envMode.value && !envMode.invalid,
960
+ modeSource: disabled ? "disabled" : envMode.value ? "environment" : "config",
961
+ channel,
962
+ warnings
963
+ };
964
+ };
965
+
966
+ // src/update/notices.ts
967
+ import { randomBytes as randomBytes2 } from "crypto";
968
+ import fsSync4, { promises as fs3 } from "fs";
969
+ import path5 from "path";
970
+ var STALE_NOTICE_AGE_MS = 60 * 60 * 1e3;
971
+ var commandHint = (command) => command ? `Update with: ${command}` : "Update using the package manager that installed Codelia.";
972
+ var buildStartupNotices = (state, options) => {
973
+ const notices = [];
974
+ if (options.reexecFrom && options.reexecFrom !== options.currentVersion) {
975
+ notices.push(
976
+ `Updated Codelia ${options.reexecFrom} -> ${options.currentVersion}.`
977
+ );
978
+ }
979
+ if (options.mode === "off") return notices;
980
+ const latest = state.latest_version;
981
+ if (!latest || !isNewerVersion(latest, options.currentVersion)) {
982
+ return notices;
983
+ }
984
+ const channel = state.channel ?? "latest";
985
+ const command = options.method ? buildUpdateCommand(options.method, { version: latest, channel }) : null;
986
+ if (state.apply_failure && state.apply_failure.version === latest) {
987
+ const hint = state.apply_failure.unmanaged ? commandHint(null) : commandHint(command);
988
+ notices.push(
989
+ `Automatic update to ${latest} failed (${state.apply_failure.message}). ${hint}`
990
+ );
991
+ return notices;
992
+ }
993
+ if (options.mode === "notify" || !options.method?.autoApply.supported) {
994
+ const hint = options.method?.autoApply.supported ? "Run `codelia update` to install." : commandHint(command);
995
+ notices.push(
996
+ `Update available: ${options.currentVersion} -> ${latest}. ${hint}`
997
+ );
998
+ }
999
+ return notices;
1000
+ };
1001
+ var writeStartupNoticeFile = async (paths, notices) => {
1002
+ if (notices.length === 0) return null;
1003
+ await fs3.mkdir(paths.noticesDir, { recursive: true, mode: 448 });
1004
+ void cleanupStaleNotices(paths.noticesDir);
1005
+ const file = path5.join(
1006
+ paths.noticesDir,
1007
+ `notice-${process.pid}-${randomBytes2(4).toString("hex")}.json`
1008
+ );
1009
+ await writeJsonAtomic(file, { notices: [...notices] });
1010
+ return file;
1011
+ };
1012
+ var removeNoticeFileSync = (file) => {
1013
+ if (!file) return;
1014
+ try {
1015
+ fsSync4.rmSync(file, { force: true });
1016
+ } catch {
1017
+ }
1018
+ };
1019
+ var cleanupStaleNotices = async (dir) => {
1020
+ try {
1021
+ const cutoff = Date.now() - STALE_NOTICE_AGE_MS;
1022
+ const entries = await fs3.readdir(dir);
1023
+ for (const entry of entries) {
1024
+ if (!entry.startsWith("notice-") || !entry.endsWith(".json")) continue;
1025
+ const full = path5.join(dir, entry);
1026
+ const stat = await fs3.lstat(full).catch(() => null);
1027
+ if (stat?.isFile() && stat.mtimeMs < cutoff) {
1028
+ await fs3.rm(full, { force: true }).catch(() => void 0);
1029
+ }
1030
+ }
1031
+ } catch {
1024
1032
  }
1025
- return { mode, channel, warnings };
1026
1033
  };
1027
1034
 
1028
1035
  export {
@@ -1043,10 +1050,9 @@ export {
1043
1050
  runRegistryCheck,
1044
1051
  detectInstallMethod,
1045
1052
  buildUpdateCommand,
1046
- UPDATE_NOTICE_FILE_ENV,
1053
+ resolveUpdateSettings,
1047
1054
  commandHint,
1048
1055
  buildStartupNotices,
1049
1056
  writeStartupNoticeFile,
1050
- removeNoticeFileSync,
1051
- resolveUpdateSettings
1057
+ removeNoticeFileSync
1052
1058
  };
@@ -1,5 +1,4 @@
1
1
  import {
2
- UPDATE_NOTICE_FILE_ENV,
3
2
  UPDATE_REEXEC_ENV,
4
3
  applyUpdate,
5
4
  buildStartupNotices,
@@ -19,12 +18,11 @@ import {
19
18
  runRegistryCheck,
20
19
  shouldCheckNow,
21
20
  writeStartupNoticeFile
22
- } from "./chunk-S7OVJIGV.js";
21
+ } from "./chunk-BYLYQTEO.js";
23
22
 
24
23
  // src/update/launch.ts
25
24
  import { resolveStoragePaths } from "@codelia/storage";
26
25
  var NOOP_LAUNCH_UPDATE_SESSION = {
27
- tuiEnv: {},
28
26
  startBackground: async () => void 0,
29
27
  dispose: () => void 0
30
28
  };
@@ -143,7 +141,8 @@ var prepareTuiLaunch = async (options) => {
143
141
  return {
144
142
  kind: "launch",
145
143
  session: {
146
- tuiEnv: noticeFile ? { [UPDATE_NOTICE_FILE_ENV]: noticeFile } : {},
144
+ ...noticeFile ? { noticeFile } : {},
145
+ startupConfirmations: settings.offerModeChoice ? ["update-mode"] : [],
147
146
  startBackground,
148
147
  dispose
149
148
  }