t3code-session-manager 0.0.0-stage → 0.4.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.
- package/.claude-plugin/marketplace.json +21 -0
- package/.claude-plugin/plugin.json +19 -0
- package/CHANGELOG.md +149 -0
- package/LICENSE +21 -0
- package/README.md +661 -2
- package/README.pt.md +617 -0
- package/bin/entry.d.mts +2 -0
- package/bin/entry.mjs +19 -0
- package/bin/t3sm.mjs +44 -0
- package/commands/import.md +30 -0
- package/commands/organize.md +36 -0
- package/commands/status.md +24 -0
- package/commands/sync.md +26 -0
- package/dist/apply/apply.js +993 -0
- package/dist/apply/claudeWriter.js +168 -0
- package/dist/apply/copyTranscript.js +116 -0
- package/dist/apply/importer.js +697 -0
- package/dist/apply/materialize.js +335 -0
- package/dist/apply/waves.js +108 -0
- package/dist/assistant/core.js +295 -0
- package/dist/assistant/deps.js +256 -0
- package/dist/assistant/i18n.js +38 -0
- package/dist/assistant/messages.en.js +494 -0
- package/dist/assistant/messages.pt.js +495 -0
- package/dist/assistant/runner.js +64 -0
- package/dist/assistant/steps.js +1362 -0
- package/dist/build.json +1 -0
- package/dist/cli/applyScreen.js +158 -0
- package/dist/cli/commands/accounts.js +254 -0
- package/dist/cli/commands/apply.js +199 -0
- package/dist/cli/commands/changelog.js +61 -0
- package/dist/cli/commands/doctor.js +105 -0
- package/dist/cli/commands/help.js +158 -0
- package/dist/cli/commands/merge.js +224 -0
- package/dist/cli/commands/organize.js +872 -0
- package/dist/cli/commands/plan.js +161 -0
- package/dist/cli/commands/projects.js +430 -0
- package/dist/cli/commands/quarantine.js +203 -0
- package/dist/cli/commands/recover.js +230 -0
- package/dist/cli/commands/rollback.js +281 -0
- package/dist/cli/commands/scan.js +117 -0
- package/dist/cli/commands/schedule.js +288 -0
- package/dist/cli/commands/start.js +45 -0
- package/dist/cli/commands/status.js +615 -0
- package/dist/cli/commands/sync.js +336 -0
- package/dist/cli/commands/update.js +398 -0
- package/dist/cli/errors.js +53 -0
- package/dist/cli/failures.js +91 -0
- package/dist/cli/flags.js +53 -0
- package/dist/cli/help.en.js +549 -0
- package/dist/cli/help.pt.js +566 -0
- package/dist/cli/helpText.js +211 -0
- package/dist/cli/helpTopics.js +58 -0
- package/dist/cli/locked.js +12 -0
- package/dist/cli/main.js +155 -0
- package/dist/cli/screen.js +3 -0
- package/dist/cli/ui.js +111 -0
- package/dist/cli/words.js +2 -0
- package/dist/core/activity.js +16 -0
- package/dist/core/changelog.js +47 -0
- package/dist/core/config.js +530 -0
- package/dist/core/fsutil.js +171 -0
- package/dist/core/glob.js +34 -0
- package/dist/core/history.js +32 -0
- package/dist/core/journal.js +274 -0
- package/dist/core/lock.js +337 -0
- package/dist/core/mask.js +31 -0
- package/dist/core/paths.js +68 -0
- package/dist/core/pid.js +101 -0
- package/dist/core/run.js +4 -0
- package/dist/core/semver.js +18 -0
- package/dist/core/sysroot.js +29 -0
- package/dist/core/types.js +229 -0
- package/dist/core/uuid.js +29 -0
- package/dist/core/version.js +72 -0
- package/dist/help/convert.md +20 -0
- package/dist/help/convert.pt.md +21 -0
- package/dist/help/cwd-unreadable.md +22 -0
- package/dist/help/cwd-unreadable.pt.md +22 -0
- package/dist/help/decisions.md +20 -0
- package/dist/help/decisions.pt.md +20 -0
- package/dist/help/errors.md +20 -0
- package/dist/help/errors.pt.md +22 -0
- package/dist/help/held.md +20 -0
- package/dist/help/held.pt.md +20 -0
- package/dist/help/history.md +27 -0
- package/dist/help/history.pt.md +29 -0
- package/dist/help/instance-unavailable.md +18 -0
- package/dist/help/instance-unavailable.pt.md +19 -0
- package/dist/help/not-imported.md +19 -0
- package/dist/help/not-imported.pt.md +19 -0
- package/dist/help/orphaned.md +19 -0
- package/dist/help/orphaned.pt.md +19 -0
- package/dist/help/project-mismatch.md +15 -0
- package/dist/help/project-mismatch.pt.md +15 -0
- package/dist/help/t3-native.md +22 -0
- package/dist/help/t3-native.pt.md +22 -0
- package/dist/help/updates.md +26 -0
- package/dist/help/updates.pt.md +28 -0
- package/dist/help/watcher.md +24 -0
- package/dist/help/watcher.pt.md +25 -0
- package/dist/i18n/cli.js +41 -0
- package/dist/i18n/lang.js +55 -0
- package/dist/i18n/messages.en.js +1140 -0
- package/dist/i18n/messages.pt.js +1149 -0
- package/dist/i18n/words.js +12 -0
- package/dist/ops/accounts.js +170 -0
- package/dist/ops/autoUpdate.js +223 -0
- package/dist/ops/discoverAccounts.js +343 -0
- package/dist/ops/doctor.js +811 -0
- package/dist/ops/elevated.js +47 -0
- package/dist/ops/errorCodes.js +39 -0
- package/dist/ops/follow.js +558 -0
- package/dist/ops/merge.js +830 -0
- package/dist/ops/notify.js +249 -0
- package/dist/ops/plugin.js +92 -0
- package/dist/ops/projects.js +113 -0
- package/dist/ops/quarantine.js +415 -0
- package/dist/ops/rollback.js +868 -0
- package/dist/ops/schedule.js +464 -0
- package/dist/ops/successor.js +51 -0
- package/dist/ops/sync.js +859 -0
- package/dist/ops/syncStatus.js +290 -0
- package/dist/ops/update.js +273 -0
- package/dist/ops/updater.js +641 -0
- package/dist/ops/watch.js +676 -0
- package/dist/ops/watcherControl.js +212 -0
- package/dist/ops/watcherHealth.js +75 -0
- package/dist/organize/apply.js +495 -0
- package/dist/organize/history.js +192 -0
- package/dist/organize/plan.js +283 -0
- package/dist/organize/rename.js +58 -0
- package/dist/organize/sessions.js +100 -0
- package/dist/organize/titles.js +70 -0
- package/dist/organize/types.js +73 -0
- package/dist/plan/categories.js +104 -0
- package/dist/plan/dedupe.js +137 -0
- package/dist/plan/folders.js +82 -0
- package/dist/plan/names.js +93 -0
- package/dist/plan/ownership.js +316 -0
- package/dist/plan/planner.js +669 -0
- package/dist/plan/rehome.js +101 -0
- package/dist/plan/report.js +407 -0
- package/dist/sources/cache.js +89 -0
- package/dist/sources/claude.js +297 -0
- package/dist/sources/codex.js +496 -0
- package/dist/sources/flatten.js +23 -0
- package/dist/sources/jsonl.js +169 -0
- package/dist/sources/native.js +233 -0
- package/dist/sources/opencode.js +483 -0
- package/dist/sources/pi.js +133 -0
- package/dist/sources/registry.js +67 -0
- package/dist/sources/types.js +80 -0
- package/dist/t3/api.js +492 -0
- package/dist/t3/auth.js +248 -0
- package/dist/t3/compat.js +62 -0
- package/dist/t3/decode.js +408 -0
- package/dist/t3/errors.js +185 -0
- package/dist/t3/rpc.js +185 -0
- package/dist/t3/runtime.js +80 -0
- package/dist/ui/caps.js +56 -0
- package/dist/ui/components.js +202 -0
- package/dist/ui/layout.js +205 -0
- package/dist/ui/list.js +423 -0
- package/dist/ui/locale.js +73 -0
- package/dist/ui/organizePreview.js +72 -0
- package/dist/ui/progress.js +237 -0
- package/dist/ui/style.js +32 -0
- package/dist/ui/terminal.js +126 -0
- package/dist/ui/text.js +186 -0
- package/package.json +58 -4
- package/skills/t3code-sessions/SKILL.md +183 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
2
|
+
import { homedir, tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { DEFAULT_HISTORY } from "./history.js";
|
|
6
|
+
import { expandHome, normalizeCwd, sameCwd } from "./paths.js";
|
|
7
|
+
import { globToRegExp } from "./glob.js";
|
|
8
|
+
import { atomicWrite } from "./fsutil.js";
|
|
9
|
+
import { isT3Refused } from "../plan/folders.js";
|
|
10
|
+
|
|
11
|
+
export class ConfigError extends Error {
|
|
12
|
+
name = "ConfigError";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A command that needs a saved plan found none (the screen words it; the message is for logs). */
|
|
16
|
+
export class NoPlanError extends ConfigError {
|
|
17
|
+
name = "NoPlanError";
|
|
18
|
+
constructor() { super("No plan found. Run `t3sm plan` first."); }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ID_RE = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
|
|
22
|
+
const ID_MAX = 64;
|
|
23
|
+
const COLOR_RE = /^#[0-9A-Fa-f]{6}$/;
|
|
24
|
+
const HINT = "Fix the file, or delete it to regenerate defaults with `t3sm accounts --init`.";
|
|
25
|
+
const CONFIG_FILE = "config.json";
|
|
26
|
+
|
|
27
|
+
/** Wave packing defaults (S2 §5 estimates); every caller without a config uses these. */
|
|
28
|
+
export const DEFAULT_BATCH = Object.freeze({ filesPerProject: 50, projectsPerWave: 5 });
|
|
29
|
+
/** `t3sm schedule` and doctor's schedule check without a config. */
|
|
30
|
+
export const DEFAULT_SCHEDULE_MINUTES = 15;
|
|
31
|
+
|
|
32
|
+
export function defaultConfig(accounts ) {
|
|
33
|
+
return {
|
|
34
|
+
version: 1,
|
|
35
|
+
accounts,
|
|
36
|
+
defaultAccount: accounts[0]?.id ?? "",
|
|
37
|
+
routes: [],
|
|
38
|
+
remap: [],
|
|
39
|
+
exclude: [],
|
|
40
|
+
includeBackups: false,
|
|
41
|
+
batch: { ...DEFAULT_BATCH },
|
|
42
|
+
schedule: { everyMinutes: DEFAULT_SCHEDULE_MINUTES },
|
|
43
|
+
// autoMerge off by default: sync only reports sessions continued in a terminal; `t3sm merge` brings them in.
|
|
44
|
+
sync: { autoMerge: false, watchDebounceMs: 30_000, maxUnattendedImports: 200 },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function sanitizeId(suffix ) {
|
|
49
|
+
const clean = suffix.replace(/[^a-zA-Z0-9_-]/g, "");
|
|
50
|
+
const id = "claude-" + (clean === "" ? "account" : clean);
|
|
51
|
+
return id.slice(0, ID_MAX);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** An alias or folder name as an account id: kept when it is already a valid id start, else prefixed `claude-`. */
|
|
55
|
+
function idFromName(name ) {
|
|
56
|
+
const clean = name.replace(/^\.+/, "").replace(/[^a-zA-Z0-9_-]/g, "");
|
|
57
|
+
if (clean === "") return null;
|
|
58
|
+
return (/^[a-zA-Z]/.test(clean) ? clean : "claude-" + clean).slice(0, ID_MAX);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function lastSegment(p ) {
|
|
62
|
+
return p.replace(/[\\/]+$/, "").split(/[\\/]/).at(-1) ?? "";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function isDir(p ) {
|
|
66
|
+
try {
|
|
67
|
+
return (await stat(p)).isDirectory();
|
|
68
|
+
} catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Find Claude account directories under `home`: `~/.claude` and every `~/.claude-<suffix>`
|
|
75
|
+
* that holds a `projects/` directory, plus `extra` candidate homes (account discovery: aliases, profiles, pins) that
|
|
76
|
+
* hold a `projects/` directory. Id: existing T3 instance id for that home (which also keeps its display name and accent
|
|
77
|
+
* colour) -> `claudeAgent` for ~/.claude -> the alias name -> the folder name; made unique with -2, -3.
|
|
78
|
+
*/
|
|
79
|
+
export async function detectAccounts(
|
|
80
|
+
home , existingInstances , extra = [],
|
|
81
|
+
) {
|
|
82
|
+
const dirs = [];
|
|
83
|
+
if (await isDir(join(home, ".claude"))) dirs.push({ dir: join(home, ".claude"), suffix: null, alias: null });
|
|
84
|
+
const names = await readdir(home).catch(() => [] );
|
|
85
|
+
for (const name of names.sort()) {
|
|
86
|
+
if (!name.startsWith(".claude-") || name.length <= ".claude-".length) continue;
|
|
87
|
+
const dir = join(home, name);
|
|
88
|
+
if (await isDir(join(dir, "projects"))) dirs.push({ dir, suffix: name.slice(".claude-".length), alias: null });
|
|
89
|
+
}
|
|
90
|
+
for (const x of extra) {
|
|
91
|
+
const known = dirs.find((d) => sameCwd(d.dir, x.home));
|
|
92
|
+
if (known) { known.alias ??= x.alias; continue; }
|
|
93
|
+
if (await isDir(join(x.home, "projects"))) dirs.push({ dir: x.home, suffix: lastSegment(x.home), alias: x.alias });
|
|
94
|
+
}
|
|
95
|
+
const used = new Set ();
|
|
96
|
+
const out = [];
|
|
97
|
+
for (const { dir, suffix, alias } of dirs) {
|
|
98
|
+
const existing = existingInstances.find((i) => i.homePath !== "" && sameCwd(normalizeCwd(i.homePath), normalizeCwd(dir)));
|
|
99
|
+
const aliasId = alias === null ? null : idFromName(alias);
|
|
100
|
+
let acc ;
|
|
101
|
+
if (existing) {
|
|
102
|
+
acc = { id: existing.id, home: dir, displayName: existing.displayName, accentColor: existing.accentColor };
|
|
103
|
+
} else if (suffix === null) {
|
|
104
|
+
acc = { id: "claudeAgent", home: dir, displayName: "Claude", accentColor: null };
|
|
105
|
+
} else if (aliasId !== null) {
|
|
106
|
+
acc = { id: aliasId, home: dir, displayName: `Claude · ${alias .replace(/^claude[-_]?/i, "") || alias}`, accentColor: null };
|
|
107
|
+
} else if (sameCwd(dir, join(home, `.claude-${suffix}`))) {
|
|
108
|
+
acc = { id: sanitizeId(suffix), home: dir, displayName: `Claude · ${suffix}`, accentColor: null };
|
|
109
|
+
} else {
|
|
110
|
+
acc = { id: idFromName(suffix) ?? "claude-account", home: dir, displayName: `Claude · ${suffix}`, accentColor: null };
|
|
111
|
+
}
|
|
112
|
+
const base = acc.id;
|
|
113
|
+
for (let n = 2; used.has(acc.id); n++) acc.id = `${base.slice(0, ID_MAX - 4)}-${n}`;
|
|
114
|
+
used.add(acc.id);
|
|
115
|
+
out.push(acc);
|
|
116
|
+
}
|
|
117
|
+
return out.sort((a, b) => {
|
|
118
|
+
if (a.id === "claudeAgent") return -1;
|
|
119
|
+
if (b.id === "claudeAgent") return 1;
|
|
120
|
+
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function isObj(u ) {
|
|
125
|
+
return typeof u === "object" && u !== null && !Array.isArray(u);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function intIn(v , lo , hi ) {
|
|
129
|
+
return typeof v === "number" && Number.isInteger(v) && v >= lo && v <= hi;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function nonEmpty(v ) {
|
|
133
|
+
return typeof v === "string" && v.trim() !== "";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const NOTIFY = ["attention", "all", "off"];
|
|
137
|
+
const FOLDER_CATEGORIES = ["home", "t3-refused", "temp", "system"] ;
|
|
138
|
+
const FOLDER_ACTIONS = ["import", "skip", "rehome"];
|
|
139
|
+
|
|
140
|
+
/** A folder written in config.json (`~` allowed) is absolute once expanded. */
|
|
141
|
+
function absoluteFolder(p , platform , home ) {
|
|
142
|
+
const n = normalizeCwd(expandHome(p.trim(), home), platform);
|
|
143
|
+
const abs = platform === "win32" ? /^[A-Za-z]:\//.test(n) || n.startsWith("//") : n.startsWith("/");
|
|
144
|
+
return abs ? n : null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Why a folder written in config.json cannot hold a T3 Code project (not absolute, or refused by T3 Code), or null. */
|
|
148
|
+
function projectFolderError(p , platform , home ) {
|
|
149
|
+
const target = absoluteFolder(p, platform, home);
|
|
150
|
+
if (target === null) return "must name an absolute folder (or one starting with ~)";
|
|
151
|
+
const t3Base = normalizeCwd(expandHome("~/.t3", home), platform);
|
|
152
|
+
if (isT3Refused(target, { platform, home, tmpdir: tmpdir(), env: {}, t3BaseDir: t3Base, t3WorktreesDir: `${t3Base}/worktrees` })) {
|
|
153
|
+
return "is a folder T3 Code does not accept as a project (the home folder, Downloads, Documents/Codex, T3 Code's own folders or the temp folder itself)";
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Every field optional; present means checked. Errors name the key (`organize.names.maxLength: ...`). A
|
|
160
|
+
* `rehome:<folder>` override must name an absolute folder T3 Code accepts as a project (spec 4.3, 3.4).
|
|
161
|
+
*/
|
|
162
|
+
function validateOrganize(o , errors , platform , home ) {
|
|
163
|
+
if (!isObj(o)) return void errors.push("organize: must be an object");
|
|
164
|
+
const has = (x , k ) => x[k] !== undefined;
|
|
165
|
+
const sub = (k ) => {
|
|
166
|
+
if (!has(o, k)) return null;
|
|
167
|
+
if (isObj(o[k])) return o[k];
|
|
168
|
+
errors.push(`organize.${k}: must be an object`);
|
|
169
|
+
return null;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const folders = sub("folders");
|
|
173
|
+
if (folders) {
|
|
174
|
+
for (const k of Object.keys(folders)) {
|
|
175
|
+
if (!(FOLDER_CATEGORIES ).includes(k)) errors.push(`organize.folders.${k}: unknown folder category (use ${FOLDER_CATEGORIES.join(", ")})`);
|
|
176
|
+
else if (!FOLDER_ACTIONS.includes(folders[k] )) errors.push(`organize.folders.${k}: must be "import", "skip" or "rehome"`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const sessions = sub("sessions");
|
|
180
|
+
if (sessions && has(sessions, "automated") && sessions["automated"] !== "skip" && sessions["automated"] !== "import") {
|
|
181
|
+
errors.push(`organize.sessions.automated: must be "skip" or "import"`);
|
|
182
|
+
}
|
|
183
|
+
if (sessions && has(sessions, "orphaned") && sessions["orphaned"] !== "copy" && sessions["orphaned"] !== "skip") {
|
|
184
|
+
errors.push(`organize.sessions.orphaned: must be "copy" or "skip"`);
|
|
185
|
+
}
|
|
186
|
+
if (has(o, "generalFolder")) {
|
|
187
|
+
const g = o["generalFolder"];
|
|
188
|
+
if (!nonEmpty(g)) errors.push("organize.generalFolder: must be a non-empty string");
|
|
189
|
+
else {
|
|
190
|
+
const why = projectFolderError(g, platform, home);
|
|
191
|
+
if (why !== null) errors.push(`organize.generalFolder: ${g} ${why}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const overrides = o["overrides"];
|
|
196
|
+
if (has(o, "overrides")) {
|
|
197
|
+
if (!Array.isArray(overrides)) errors.push("organize.overrides: must be an array");
|
|
198
|
+
else overrides.forEach((r , i) => {
|
|
199
|
+
const p = `organize.overrides[${i}]`;
|
|
200
|
+
if (!isObj(r)) return void errors.push(`${p}: must be an object`);
|
|
201
|
+
if (!nonEmpty(r["match"])) errors.push(`${p}.match: must be a non-empty string`);
|
|
202
|
+
const a = r["action"];
|
|
203
|
+
const ok = a === "import" || a === "skip" || (typeof a === "string" && a.startsWith("rehome:") && a.slice(7).trim() !== "");
|
|
204
|
+
if (!ok) return void errors.push(`${p}.action: must be "import", "skip" or "rehome:<folder>" with a folder`);
|
|
205
|
+
if (typeof a !== "string" || !a.startsWith("rehome:")) return;
|
|
206
|
+
const why = projectFolderError(a.slice(7), platform, home);
|
|
207
|
+
if (why !== null) errors.push(`${p}.action: ${a} ${why}`);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const names = sub("names");
|
|
212
|
+
if (names && has(names, "maxLength") && !intIn(names["maxLength"], 16, 120)) errors.push("organize.names.maxLength: must be an integer 16..120");
|
|
213
|
+
|
|
214
|
+
if (has(o, "history")) {
|
|
215
|
+
const h = o["history"];
|
|
216
|
+
if (isObj(h)) {
|
|
217
|
+
if (has(h, "days") && h["days"] !== "all" && !intIn(h["days"], 1, 3650)) errors.push(`organize.history.days: must be "all" or an integer 1..3650`);
|
|
218
|
+
if (has(h, "minPerProject") && !intIn(h["minPerProject"], 0, 1000)) errors.push("organize.history.minPerProject: must be an integer 0..1000");
|
|
219
|
+
} else {
|
|
220
|
+
const m = typeof h === "string" ? /^days:(\d+)$/.exec(h) : null;
|
|
221
|
+
const ok = h === "all" || h === "off" || (m !== null && Number(m[1]) >= 1 && Number(m[1]) <= 3650);
|
|
222
|
+
if (!ok) errors.push(`organize.history: must be {days, minPerProject}, or "all", "off" or "days:N" with N 1..3650`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
for (const k of ["titles", "archiveAutomated"]) {
|
|
226
|
+
if (has(o, k) && typeof o[k] !== "boolean") errors.push(`organize.${k}: must be a boolean`);
|
|
227
|
+
}
|
|
228
|
+
const pin = sub("pinRecent");
|
|
229
|
+
if (pin) {
|
|
230
|
+
if (has(pin, "enabled") && typeof pin["enabled"] !== "boolean") errors.push("organize.pinRecent.enabled: must be a boolean");
|
|
231
|
+
if (has(pin, "count") && !intIn(pin["count"], 1, 20)) errors.push("organize.pinRecent.count: must be an integer 1..20");
|
|
232
|
+
}
|
|
233
|
+
const decided = o["decided"];
|
|
234
|
+
if (has(o, "decided")) {
|
|
235
|
+
if (!Array.isArray(decided)) errors.push("organize.decided: must be an array");
|
|
236
|
+
else decided.forEach((d , i) => { if (!nonEmpty(d)) errors.push(`organize.decided[${i}]: must be a non-empty string`); });
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export { DEFAULT_HISTORY, HISTORY_V2, historyRule, legacyHistory, normalizeHistory } from "./history.js";
|
|
241
|
+
|
|
242
|
+
/** `organize` with every omitted field filled from the spec defaults (nested objects per field). `lang` only picks the default `generalFolder`. */
|
|
243
|
+
export function organizeSettings(c , lang ) {
|
|
244
|
+
const o = c.organize ?? {};
|
|
245
|
+
return {
|
|
246
|
+
folders: { home: "rehome", "t3-refused": "rehome", temp: "skip", system: "skip", ...o.folders },
|
|
247
|
+
sessions: { automated: "skip", orphaned: "copy", ...o.sessions },
|
|
248
|
+
generalFolder: o.generalFolder ?? (lang === "pt" ? "~/T3 Geral" : "~/T3 General"),
|
|
249
|
+
overrides: o.overrides ?? [],
|
|
250
|
+
names: { maxLength: 40, ...o.names },
|
|
251
|
+
history: o.history ?? { ...DEFAULT_HISTORY },
|
|
252
|
+
titles: o.titles ?? true,
|
|
253
|
+
archiveAutomated: o.archiveAutomated ?? true,
|
|
254
|
+
pinRecent: { enabled: true, count: 5, ...o.pinRecent },
|
|
255
|
+
decided: o.decided ?? [],
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** What the guided assistant saves under `organize`: each field only when answered; nested objects merge per field. */
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
/** `c` with `patch` applied to its `organize` section; the rest of the config, and `c` itself, are untouched. */
|
|
274
|
+
export function applyOrganizePatch(c , patch ) {
|
|
275
|
+
const o = c.organize ?? {};
|
|
276
|
+
const next = { ...o };
|
|
277
|
+
if (patch.folders) next.folders = { ...o.folders, ...patch.folders } ;
|
|
278
|
+
if (patch.sessions) next.sessions = { ...o.sessions, ...patch.sessions };
|
|
279
|
+
if (patch.overrides) next.overrides = patch.overrides.map((r) => ({ ...r }));
|
|
280
|
+
if (patch.history !== undefined) next.history = patch.history;
|
|
281
|
+
if (patch.titles !== undefined) next.titles = patch.titles;
|
|
282
|
+
if (patch.archiveAutomated !== undefined) next.archiveAutomated = patch.archiveAutomated;
|
|
283
|
+
if (patch.pinRecent) next.pinRecent = { ...o.pinRecent, ...patch.pinRecent } ;
|
|
284
|
+
if (patch.decided) next.decided = [...new Set([...(o.decided ?? []), ...patch.decided])];
|
|
285
|
+
return { ...c, organize: next };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** True when the question `key` (see `OrganizeConfig.decided`) was already asked. */
|
|
289
|
+
export function isDecided(c , key ) {
|
|
290
|
+
return c.organize?.decided?.includes(key) ?? false;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function validateConfig(
|
|
294
|
+
u ,
|
|
295
|
+
platform = process.platform,
|
|
296
|
+
home = homedir(),
|
|
297
|
+
) {
|
|
298
|
+
const errors = [];
|
|
299
|
+
if (!isObj(u)) return { ok: false, errors: ["config: must be an object"] };
|
|
300
|
+
|
|
301
|
+
if (u["version"] !== 1) errors.push("version: must be 1");
|
|
302
|
+
|
|
303
|
+
const ids = new Set ();
|
|
304
|
+
const homes = [];
|
|
305
|
+
const accs = u["accounts"];
|
|
306
|
+
if (!Array.isArray(accs)) {
|
|
307
|
+
errors.push("accounts: must be an array");
|
|
308
|
+
} else {
|
|
309
|
+
if (accs.length === 0) errors.push("accounts: must not be empty");
|
|
310
|
+
accs.forEach((a , i) => {
|
|
311
|
+
const p = `accounts[${i}]`;
|
|
312
|
+
if (!isObj(a)) {
|
|
313
|
+
errors.push(`${p}: must be an object`);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const id = a["id"];
|
|
317
|
+
if (typeof id !== "string" || !ID_RE.test(id) || id.length > ID_MAX) {
|
|
318
|
+
errors.push(`${p}.id: must match ${ID_RE.source} and be at most ${ID_MAX} characters`);
|
|
319
|
+
} else if (ids.has(id)) {
|
|
320
|
+
errors.push(`${p}.id: duplicate account id "${id}"`);
|
|
321
|
+
} else {
|
|
322
|
+
ids.add(id);
|
|
323
|
+
}
|
|
324
|
+
if (!nonEmpty(a["home"])) {
|
|
325
|
+
errors.push(`${p}.home: must be a non-empty string`);
|
|
326
|
+
} else {
|
|
327
|
+
const h = normalizeCwd(expandHome(a["home"], home), platform);
|
|
328
|
+
if (homes.some((x) => sameCwd(x, h, platform))) errors.push(`${p}.home: duplicate home "${a["home"]}"`);
|
|
329
|
+
else homes.push(h);
|
|
330
|
+
}
|
|
331
|
+
if (!nonEmpty(a["displayName"])) errors.push(`${p}.displayName: must be a non-empty string`);
|
|
332
|
+
const c = a["accentColor"];
|
|
333
|
+
if (c !== null && !(typeof c === "string" && COLOR_RE.test(c))) errors.push(`${p}.accentColor: must be null or #RRGGBB`);
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (typeof u["defaultAccount"] !== "string" || !ids.has(u["defaultAccount"])) {
|
|
338
|
+
errors.push(`defaultAccount: unknown account "${String(u["defaultAccount"])}"`);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Every key below is optional: absent means defaultConfig's value (nested objects per field); present means checked.
|
|
342
|
+
const has = (o , k ) => o[k] !== undefined;
|
|
343
|
+
const routes = u["routes"];
|
|
344
|
+
if (!has(u, "routes")) { /* default [] */ } else if (!Array.isArray(routes)) errors.push("routes: must be an array");
|
|
345
|
+
else routes.forEach((r , i) => {
|
|
346
|
+
if (!isObj(r)) return void errors.push(`routes[${i}]: must be an object`);
|
|
347
|
+
if (!nonEmpty(r["match"])) errors.push(`routes[${i}].match: must be a non-empty string`);
|
|
348
|
+
if (typeof r["account"] !== "string" || !ids.has(r["account"])) errors.push(`routes[${i}].account: unknown account "${String(r["account"])}"`);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
const remap = u["remap"];
|
|
352
|
+
if (!has(u, "remap")) { /* default [] */ } else if (!Array.isArray(remap)) errors.push("remap: must be an array");
|
|
353
|
+
else remap.forEach((r , i) => {
|
|
354
|
+
if (!isObj(r)) return void errors.push(`remap[${i}]: must be an object`);
|
|
355
|
+
if (!nonEmpty(r["from"])) errors.push(`remap[${i}].from: must be a non-empty string`);
|
|
356
|
+
if (!nonEmpty(r["to"])) errors.push(`remap[${i}].to: must be a non-empty string`);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
const exclude = u["exclude"];
|
|
360
|
+
if (!has(u, "exclude")) { /* default [] */ } else if (!Array.isArray(exclude)) errors.push("exclude: must be an array");
|
|
361
|
+
else exclude.forEach((e , i) => {
|
|
362
|
+
if (!nonEmpty(e)) errors.push(`exclude[${i}]: must be a non-empty string`);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
if (has(u, "includeBackups") && typeof u["includeBackups"] !== "boolean") errors.push("includeBackups: must be a boolean");
|
|
366
|
+
|
|
367
|
+
const batch = u["batch"];
|
|
368
|
+
if (!has(u, "batch")) { /* defaults */ } else if (!isObj(batch)) errors.push("batch: must be an object");
|
|
369
|
+
else {
|
|
370
|
+
if (has(batch, "filesPerProject") && !intIn(batch["filesPerProject"], 1, 100)) errors.push("batch.filesPerProject: must be an integer 1..100");
|
|
371
|
+
if (has(batch, "projectsPerWave") && !intIn(batch["projectsPerWave"], 1, 50)) errors.push("batch.projectsPerWave: must be an integer 1..50");
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const schedule = u["schedule"];
|
|
375
|
+
if (!has(u, "schedule")) { /* defaults */ } else if (!isObj(schedule)) errors.push("schedule: must be an object");
|
|
376
|
+
else if (has(schedule, "everyMinutes") && !intIn(schedule["everyMinutes"], 1, 1440)) errors.push("schedule.everyMinutes: must be an integer 1..1440");
|
|
377
|
+
|
|
378
|
+
const sync = u["sync"];
|
|
379
|
+
if (!has(u, "sync")) { /* defaults */ } else if (!isObj(sync)) errors.push("sync: must be an object");
|
|
380
|
+
else {
|
|
381
|
+
if (has(sync, "autoMerge") && typeof sync["autoMerge"] !== "boolean") errors.push("sync.autoMerge: must be a boolean");
|
|
382
|
+
if (has(sync, "watchDebounceMs") && !intIn(sync["watchDebounceMs"], 1000, 600_000)) errors.push("sync.watchDebounceMs: must be an integer 1000..600000");
|
|
383
|
+
if (has(sync, "maxUnattendedImports") && !intIn(sync["maxUnattendedImports"], 1, 100_000)) errors.push("sync.maxUnattendedImports: must be an integer 1..100000");
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const codex = u["codex"];
|
|
387
|
+
if (!has(u, "codex")) { /* default: convert compressed rollouts */ } else if (!isObj(codex)) errors.push("codex: must be an object");
|
|
388
|
+
else if (has(codex, "convertCompressed") && typeof codex["convertCompressed"] !== "boolean") errors.push("codex.convertCompressed: must be a boolean");
|
|
389
|
+
|
|
390
|
+
if (has(u, "language") && u["language"] !== "en" && u["language"] !== "pt") errors.push(`language: must be "en" or "pt"`);
|
|
391
|
+
|
|
392
|
+
if (has(u, "notify") && !NOTIFY.includes(u["notify"] )) errors.push(`notify: must be "attention", "all" or "off"`);
|
|
393
|
+
|
|
394
|
+
const update = u["update"];
|
|
395
|
+
if (!has(u, "update")) { /* default: automatic updates on */ } else if (!isObj(update)) errors.push("update: must be an object");
|
|
396
|
+
else if (has(update, "auto") && typeof update["auto"] !== "boolean") errors.push("update.auto: must be a boolean");
|
|
397
|
+
if (has(u, "organize")) validateOrganize(u["organize"], errors, platform, home);
|
|
398
|
+
|
|
399
|
+
if (errors.length > 0) return { ok: false, errors };
|
|
400
|
+
return { ok: true, config: withDefaults(u, accs ) };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Whether the watcher installs newer patch and minor releases by itself (`update.auto`, default true; spec §5.3). */
|
|
404
|
+
export function updateAuto(c ) {
|
|
405
|
+
return c?.update?.auto ?? true;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Fills every omitted key from defaultConfig; nested objects (batch, schedule, sync) per field. Input is not mutated. */
|
|
409
|
+
function withDefaults(u , accounts ) {
|
|
410
|
+
const d = defaultConfig(accounts);
|
|
411
|
+
const pick = (k , dv ) => (u[k] === undefined ? dv : (u[k] ));
|
|
412
|
+
const merge = (k , dv ) => {
|
|
413
|
+
const o = (u[k] ?? {}) ;
|
|
414
|
+
return Object.fromEntries(Object.entries(dv).map(([f, v]) => [f, o[f] === undefined ? v : o[f]]).concat(
|
|
415
|
+
Object.entries(o).filter(([f, v]) => !(f in dv) && v !== undefined))) ;
|
|
416
|
+
};
|
|
417
|
+
return {
|
|
418
|
+
...(u ),
|
|
419
|
+
version: 1,
|
|
420
|
+
accounts,
|
|
421
|
+
defaultAccount: u["defaultAccount"] ,
|
|
422
|
+
routes: pick("routes", d.routes),
|
|
423
|
+
remap: pick("remap", d.remap),
|
|
424
|
+
exclude: pick("exclude", d.exclude),
|
|
425
|
+
includeBackups: pick("includeBackups", d.includeBackups),
|
|
426
|
+
batch: merge("batch", d.batch),
|
|
427
|
+
schedule: merge("schedule", d.schedule),
|
|
428
|
+
sync: merge("sync", d.sync),
|
|
429
|
+
} ;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** A glob whose first segment is `~`: that segment becomes the user home (forward slashes, as globs are written). */
|
|
433
|
+
function expandGlobHome(g , home , platform ) {
|
|
434
|
+
if (g !== "~" && !g.startsWith("~/") && !g.startsWith("~\\")) return g;
|
|
435
|
+
return normalizeCwd(home, platform).replace(/\/+$/, "") + g.slice(1).replaceAll("\\", "/");
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Every path of the config as an absolute path: account homes, remap from/to, and route / exclude globs whose first
|
|
440
|
+
* segment is `~`. Done once at load, so every consumer sees the same absolute paths. Pure.
|
|
441
|
+
*/
|
|
442
|
+
export function expandConfig(c , home , platform = process.platform) {
|
|
443
|
+
return {
|
|
444
|
+
...c,
|
|
445
|
+
accounts: c.accounts.map((a) => ({ ...a, home: expandHome(a.home, home) })),
|
|
446
|
+
remap: c.remap.map((r) => ({ ...r, from: expandHome(r.from, home), to: expandHome(r.to, home) })),
|
|
447
|
+
routes: c.routes.map((r) => ({ ...r, match: expandGlobHome(r.match, home, platform) })),
|
|
448
|
+
exclude: c.exclude.map((g) => expandGlobHome(g, home, platform)),
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* `c` as the user wrote it: every path that equals the expansion of the same field's value in the config file on disk
|
|
454
|
+
* goes back to the file's spelling (e.g. `~/.claude`). Anything new or changed is written as it is.
|
|
455
|
+
*/
|
|
456
|
+
async function asWritten(file , c , home , platform ) {
|
|
457
|
+
let disk ;
|
|
458
|
+
try { disk = JSON.parse((await readFile(file, "utf8")).replace(/^\uFEFF/, "")); } catch { return c; }
|
|
459
|
+
if (!isObj(disk)) return c;
|
|
460
|
+
const raw = new Map ();
|
|
461
|
+
const strs = (u , f ) =>
|
|
462
|
+
Array.isArray(u) ? u.flatMap((x) => (isObj(x) ? f(x) : [x])).filter((x) => typeof x === "string") : [];
|
|
463
|
+
for (const p of [...strs(disk["accounts"], (a) => [a["home"]]), ...strs(disk["remap"], (r) => [r["from"], r["to"]])]) raw.set(expandHome(p, home), p);
|
|
464
|
+
for (const g of [...strs(disk["routes"], (r) => [r["match"]]), ...strs(disk["exclude"], () => [])]) raw.set(expandGlobHome(g, home, platform), g);
|
|
465
|
+
const back = (s ) => raw.get(s) ?? s;
|
|
466
|
+
return {
|
|
467
|
+
...c,
|
|
468
|
+
accounts: c.accounts.map((a) => ({ ...a, home: back(a.home) })),
|
|
469
|
+
remap: c.remap.map((r) => ({ ...r, from: back(r.from), to: back(r.to) })),
|
|
470
|
+
routes: c.routes.map((r) => ({ ...r, match: back(r.match) })),
|
|
471
|
+
exclude: c.exclude.map(back),
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** Reads and validates `<dir>/config.json` (null when absent); `~` paths come back expanded against `home`. */
|
|
476
|
+
export async function loadConfig(dir , home = homedir(), platform = process.platform) {
|
|
477
|
+
const file = join(dir, CONFIG_FILE);
|
|
478
|
+
let raw ;
|
|
479
|
+
try {
|
|
480
|
+
raw = await readFile(file, "utf8");
|
|
481
|
+
} catch (e) {
|
|
482
|
+
if ((e ).code === "ENOENT") return null;
|
|
483
|
+
throw e;
|
|
484
|
+
}
|
|
485
|
+
let parsed ;
|
|
486
|
+
try {
|
|
487
|
+
parsed = JSON.parse(raw.replace(/^\uFEFF/, ""));
|
|
488
|
+
} catch (e) {
|
|
489
|
+
throw new ConfigError(`${file}: invalid JSON (${(e ).message}). ${HINT}`);
|
|
490
|
+
}
|
|
491
|
+
const r = validateConfig(parsed, platform, home);
|
|
492
|
+
if (!r.ok) throw new ConfigError(`${file}: invalid config:\n ${r.errors.join("\n ")}\n${HINT}`);
|
|
493
|
+
return expandConfig(r.config, home, platform);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** Writes config.json; paths loadConfig expanded are written back as the existing file spells them. */
|
|
497
|
+
export async function saveConfig(dir , c , home = homedir(), platform = process.platform) {
|
|
498
|
+
const file = join(dir, CONFIG_FILE);
|
|
499
|
+
await atomicWrite(file, JSON.stringify(await asWritten(file, c, home, platform), null, 2) + "\n");
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/** First `remap` entry whose `from` is a whole-segment path prefix of `cwd` wins. */
|
|
503
|
+
export function resolveRemap(
|
|
504
|
+
cwd ,
|
|
505
|
+
c ,
|
|
506
|
+
platform = process.platform,
|
|
507
|
+
) {
|
|
508
|
+
const n = normalizeCwd(cwd, platform);
|
|
509
|
+
const fold = platform === "win32" || platform === "darwin";
|
|
510
|
+
const key = (s ) => (fold ? s.toLowerCase() : s);
|
|
511
|
+
for (const r of c.remap) {
|
|
512
|
+
const from = normalizeCwd(r.from, platform);
|
|
513
|
+
const rest = key(n).startsWith(key(from)) ? n.slice(from.length) : null;
|
|
514
|
+
if (rest === null) continue;
|
|
515
|
+
if (rest === "" || rest.startsWith("/") || from.endsWith("/")) {
|
|
516
|
+
const to = normalizeCwd(r.to, platform);
|
|
517
|
+
const tail = rest.startsWith("/") || rest === "" ? rest : "/" + rest;
|
|
518
|
+
return { cwd: normalizeCwd(to + tail, platform), remapped: true };
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return { cwd: n, remapped: false };
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
export function routeAccount(cwd , c , platform = process.platform) {
|
|
525
|
+
const n = normalizeCwd(cwd, platform);
|
|
526
|
+
for (const r of c.routes) {
|
|
527
|
+
if (globToRegExp(r.match, platform).test(n)) return r.account;
|
|
528
|
+
}
|
|
529
|
+
return null;
|
|
530
|
+
}
|