@trans-ai/trans-code 0.4.0 → 0.6.0
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/docs/superpowers/plans/2026-11-21-auto-update.md +138 -0
- package/docs/superpowers/specs/2026-11-21-auto-update-design.md +61 -0
- package/docs/superpowers/specs/2026-11-21-key-first-model-flow-design.md +31 -0
- package/package.json +1 -1
- package/src/cli.ts +7 -3
- package/src/config.test.ts +49 -0
- package/src/config.ts +42 -0
- package/src/main.tsx +2 -1
- package/src/provider/model-list.test.ts +31 -3
- package/src/provider/provider.test.ts +38 -2
- package/src/provider/provider.ts +41 -9
- package/src/provider/routing.test.ts +1 -1
- package/src/screens/REPL.tsx +142 -20
- package/src/screens/slash-completion.test.ts +62 -0
- package/src/screens/slash-completion.ts +41 -0
- package/src/screens/submit.test.ts +23 -0
- package/src/screens/submit.ts +17 -0
- package/src/upgrade.test.ts +242 -0
- package/src/upgrade.ts +206 -9
- package/src/version.ts +1 -1
package/src/upgrade.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { spawn } from 'node:child_process';
|
|
|
2
2
|
import { realpathSync } from 'node:fs';
|
|
3
3
|
import { readFileSync } from 'node:fs';
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { loadConfig, normalizeAutoUpdate, saveConfig } from './config.js';
|
|
5
6
|
import { VERSION } from './version.js';
|
|
6
7
|
|
|
7
8
|
const PKG = '@trans-ai/trans-code';
|
|
@@ -11,6 +12,185 @@ const PKG = '@trans-ai/trans-code';
|
|
|
11
12
|
// to the stale version.
|
|
12
13
|
const OFFICIAL = 'https://registry.npmjs.org';
|
|
13
14
|
|
|
15
|
+
const DEFAULT_INTERVAL_HOURS = 24;
|
|
16
|
+
|
|
17
|
+
export type AutoUpdatePlan =
|
|
18
|
+
| { action: 'skip'; reason: 'env-skip' | 'disabled' | 'throttled' | 'up-to-date' | 'unknown-latest' }
|
|
19
|
+
| { action: 'install'; spec: string; from: string; to: string };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Pure decision for the background auto-update: whether to skip (and why)
|
|
23
|
+
* or which package spec to install. Order matters — envSkip and the
|
|
24
|
+
* enabled flag short-circuit before the throttle check so a disabled
|
|
25
|
+
* updater never hits the registry.
|
|
26
|
+
*/
|
|
27
|
+
export function planAutoUpdate(input: {
|
|
28
|
+
current: string;
|
|
29
|
+
/** Latest version from the registry; null when it could not be reached. */
|
|
30
|
+
latest: string | null;
|
|
31
|
+
/** 'latest' or a pinned version string. */
|
|
32
|
+
target: string;
|
|
33
|
+
enabled: boolean;
|
|
34
|
+
lastCheck: number;
|
|
35
|
+
now: number;
|
|
36
|
+
intervalHours?: number;
|
|
37
|
+
envSkip?: boolean;
|
|
38
|
+
}): AutoUpdatePlan {
|
|
39
|
+
if (input.envSkip) return { action: 'skip', reason: 'env-skip' };
|
|
40
|
+
if (!input.enabled) return { action: 'skip', reason: 'disabled' };
|
|
41
|
+
const interval =
|
|
42
|
+
(input.intervalHours ?? DEFAULT_INTERVAL_HOURS) * 3_600_000;
|
|
43
|
+
if (input.now - input.lastCheck < interval) {
|
|
44
|
+
return { action: 'skip', reason: 'throttled' };
|
|
45
|
+
}
|
|
46
|
+
if (input.target === 'latest') {
|
|
47
|
+
if (input.latest === null) return { action: 'skip', reason: 'unknown-latest' };
|
|
48
|
+
if (input.latest === input.current) return { action: 'skip', reason: 'up-to-date' };
|
|
49
|
+
return { action: 'install', spec: `${PKG}@latest`, from: input.current, to: input.latest };
|
|
50
|
+
}
|
|
51
|
+
// Pinned target: stay at it exactly, downgrades included.
|
|
52
|
+
if (input.target === input.current) return { action: 'skip', reason: 'up-to-date' };
|
|
53
|
+
return { action: 'install', spec: `${PKG}@${input.target}`, from: input.current, to: input.target };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* True when the running entry file lives outside the npm global root
|
|
58
|
+
* (repo checkout, bun link) — nothing to auto-upgrade there. Both sides
|
|
59
|
+
* are realpath-normalized so macOS aliases (/tmp vs /private/tmp) and
|
|
60
|
+
* symlinked roots compare correctly.
|
|
61
|
+
*/
|
|
62
|
+
export function isDevInstall(entryPath: string, npmRoot: string): boolean {
|
|
63
|
+
if (!npmRoot) return true;
|
|
64
|
+
try {
|
|
65
|
+
const entryReal = realpathSync(entryPath);
|
|
66
|
+
const rootReal = realpathSync(npmRoot);
|
|
67
|
+
return !entryReal.startsWith(rootReal);
|
|
68
|
+
} catch {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Like sh(), but fully silent AND detached: background updates must never
|
|
75
|
+
* write into the TUI, and must never keep the event loop (and thereby a
|
|
76
|
+
* quitting REPL) alive until npm finishes. A kill timer bounds a hung npm.
|
|
77
|
+
*/
|
|
78
|
+
function shQuiet(cmd: string, args: string[]): Promise<{ code: number; stdout: string }> {
|
|
79
|
+
return new Promise(resolve => {
|
|
80
|
+
const child = spawn(cmd, args, { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
81
|
+
let settled = false;
|
|
82
|
+
const settle = (code: number, stdout: string) => {
|
|
83
|
+
if (settled) return;
|
|
84
|
+
settled = true;
|
|
85
|
+
clearTimeout(timer);
|
|
86
|
+
resolve({ code, stdout });
|
|
87
|
+
};
|
|
88
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), 120_000);
|
|
89
|
+
timer.unref?.();
|
|
90
|
+
// Detach: a REPL that quits mid-check exits promptly; an orphaned npm
|
|
91
|
+
// that still completes is benign (the next startup self-corrects).
|
|
92
|
+
child.unref?.();
|
|
93
|
+
(child.stdout as { unref?: () => void } | null)?.unref?.();
|
|
94
|
+
(child.stderr as { unref?: () => void } | null)?.unref?.();
|
|
95
|
+
let stdout = '';
|
|
96
|
+
child.stdout.on('data', (d: Buffer) => {
|
|
97
|
+
stdout += d.toString();
|
|
98
|
+
});
|
|
99
|
+
child.on('error', () => settle(1, stdout));
|
|
100
|
+
child.on('close', code => settle(code ?? 1, stdout));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let noticePromise: Promise<string | null> = Promise.resolve(null);
|
|
105
|
+
|
|
106
|
+
/** The auto-update outcome, once settled; null when nothing happened. */
|
|
107
|
+
export function autoUpdateNotice(): Promise<string | null> {
|
|
108
|
+
return noticePromise;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Kick off the background auto-update (REPL startup only). Never throws
|
|
113
|
+
* and never blocks: every failure path settles the notice to null, and
|
|
114
|
+
* npm output stays invisible unless TRANSCODE_DEBUG_UPGRADE=1.
|
|
115
|
+
*
|
|
116
|
+
* lastCheck is written only when the check reached a definitive state
|
|
117
|
+
* (up-to-date, or install succeeded) — on failure the next startup retries.
|
|
118
|
+
*/
|
|
119
|
+
export function startAutoUpdate(): void {
|
|
120
|
+
noticePromise = (async (): Promise<string | null> => {
|
|
121
|
+
const debug = process.env.TRANSCODE_DEBUG_UPGRADE === '1';
|
|
122
|
+
try {
|
|
123
|
+
const auto = normalizeAutoUpdate(loadConfig().autoUpdate);
|
|
124
|
+
const envSkip = process.env.TRANSCODE_SKIP_UPDATE === '1';
|
|
125
|
+
const intervalHours = Number(process.env.TRANSCODE_UPDATE_INTERVAL_HOURS) || undefined;
|
|
126
|
+
|
|
127
|
+
// Cheap gates first — no subprocess before we know we might install.
|
|
128
|
+
const early = planAutoUpdate({
|
|
129
|
+
current: VERSION,
|
|
130
|
+
latest: 'x', // placeholder: gates below never read it
|
|
131
|
+
target: auto.target,
|
|
132
|
+
enabled: auto.enabled,
|
|
133
|
+
lastCheck: auto.lastCheck,
|
|
134
|
+
now: Date.now(),
|
|
135
|
+
intervalHours,
|
|
136
|
+
envSkip,
|
|
137
|
+
});
|
|
138
|
+
if (early.action === 'skip' && early.reason !== 'unknown-latest') {
|
|
139
|
+
if (early.reason === 'up-to-date' && auto.target !== 'latest') return null; // pin satisfied
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const root = (await shQuiet('npm', ['root', '-g'])).stdout.trim();
|
|
144
|
+
const entry = realpathSync(fileURLToPath(import.meta.url));
|
|
145
|
+
if (isDevInstall(entry, root)) {
|
|
146
|
+
if (debug) process.stderr.write('[transcode] auto-update: dev install, skipped\n');
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const latest = auto.target === 'latest' ? await latestVersion(true) : auto.target;
|
|
151
|
+
const plan = planAutoUpdate({
|
|
152
|
+
current: VERSION,
|
|
153
|
+
latest,
|
|
154
|
+
target: auto.target,
|
|
155
|
+
enabled: auto.enabled,
|
|
156
|
+
lastCheck: auto.lastCheck,
|
|
157
|
+
now: Date.now(),
|
|
158
|
+
intervalHours,
|
|
159
|
+
envSkip,
|
|
160
|
+
});
|
|
161
|
+
if (plan.action === 'skip') {
|
|
162
|
+
// Up-to-date is a definitive outcome — record it so the next
|
|
163
|
+
// startup is throttled. Unknown-latest stays unwritten to retry.
|
|
164
|
+
if (plan.reason === 'up-to-date') writeLastCheck();
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const install = await shQuiet('npm', ['i', '-g', plan.spec, `--registry=${OFFICIAL}`]);
|
|
169
|
+
if (install.code !== 0) {
|
|
170
|
+
if (debug) process.stderr.write('[transcode] auto-update install failed\n');
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
const installed = installedVersionUnder(root);
|
|
174
|
+
writeLastCheck();
|
|
175
|
+
return `⟳ 已自动更新 ${VERSION} → ${installed ?? plan.to},重启 transcode 生效`;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (debug) process.stderr.write(`[transcode] auto-update error: ${String(error)}\n`);
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
})();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function writeLastCheck(): void {
|
|
184
|
+
try {
|
|
185
|
+
const cfg = loadConfig();
|
|
186
|
+
const auto = normalizeAutoUpdate(cfg.autoUpdate);
|
|
187
|
+
cfg.autoUpdate = { ...auto, lastCheck: Date.now() };
|
|
188
|
+
saveConfig(cfg);
|
|
189
|
+
} catch {
|
|
190
|
+
// The throttle cache is best-effort.
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
14
194
|
function sh(cmd: string, args: string[]): Promise<{ code: number; stdout: string }> {
|
|
15
195
|
return new Promise(resolve => {
|
|
16
196
|
const child = spawn(cmd, args, { stdio: ['ignore', 'pipe', 'inherit'] });
|
|
@@ -42,8 +222,11 @@ async function npmRoot(): Promise<string> {
|
|
|
42
222
|
return r.stdout.trim();
|
|
43
223
|
}
|
|
44
224
|
|
|
45
|
-
async function latestVersion(): Promise<string | null> {
|
|
46
|
-
const
|
|
225
|
+
async function latestVersion(quiet = false): Promise<string | null> {
|
|
226
|
+
const run = quiet
|
|
227
|
+
? shQuiet('npm', ['view', `${PKG}@latest`, 'version', `--registry=${OFFICIAL}`])
|
|
228
|
+
: sh('npm', ['view', `${PKG}@latest`, 'version', `--registry=${OFFICIAL}`]);
|
|
229
|
+
const r = await run;
|
|
47
230
|
if (r.code !== 0) return null;
|
|
48
231
|
const v = r.stdout.trim();
|
|
49
232
|
return v || null;
|
|
@@ -58,19 +241,20 @@ function installedVersionUnder(root: string): string | null {
|
|
|
58
241
|
}
|
|
59
242
|
}
|
|
60
243
|
|
|
61
|
-
export async function runUpgrade(): Promise<void> {
|
|
62
|
-
|
|
244
|
+
export async function runUpgrade(target?: string): Promise<void> {
|
|
245
|
+
if (target) {
|
|
246
|
+
process.stdout.write(`transcode v${VERSION} — upgrading to ${target}…\n`);
|
|
247
|
+
} else {
|
|
248
|
+
process.stdout.write(`transcode v${VERSION} — checking for upgrades…\n`);
|
|
249
|
+
}
|
|
63
250
|
|
|
64
251
|
const entry = realpathSync(fileURLToPath(import.meta.url));
|
|
65
252
|
const root = await npmRoot();
|
|
66
|
-
// Normalize both sides through realpath — macOS aliases like /tmp vs
|
|
67
|
-
// /private/tmp would otherwise break prefix comparison.
|
|
68
|
-
const rootReal = root ? realpathSync(root) : '';
|
|
69
253
|
if (process.env.TRANSCODE_DEBUG_UPGRADE === '1') {
|
|
70
|
-
process.stdout.write(`[debug] entry=${entry}\n[debug] root=${
|
|
254
|
+
process.stdout.write(`[debug] entry=${entry}\n[debug] root=${root}\n`);
|
|
71
255
|
}
|
|
72
256
|
|
|
73
|
-
if (
|
|
257
|
+
if (isDevInstall(entry, root)) {
|
|
74
258
|
process.stdout.write(
|
|
75
259
|
[
|
|
76
260
|
'This looks like a development install (bun link / repo checkout),',
|
|
@@ -84,6 +268,19 @@ export async function runUpgrade(): Promise<void> {
|
|
|
84
268
|
return;
|
|
85
269
|
}
|
|
86
270
|
|
|
271
|
+
if (target) {
|
|
272
|
+
process.stdout.write(`Upgrading ${VERSION} → ${target}…\n`);
|
|
273
|
+
const code = await shInherit('npm', ['i', '-g', `${PKG}@${target}`, `--registry=${OFFICIAL}`]);
|
|
274
|
+
if (code !== 0) {
|
|
275
|
+
process.stderr.write('[transcode] upgrade failed — see npm output above.\n');
|
|
276
|
+
process.exitCode = 1;
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
const installed = installedVersionUnder(root);
|
|
280
|
+
process.stdout.write(`Upgraded to v${installed ?? target}. Restart transcode if it is running.\n`);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
87
284
|
const latest = await latestVersion();
|
|
88
285
|
if (!latest) {
|
|
89
286
|
process.stderr.write(
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Auto-generated from package.json by scripts/sync-version.ts (npm version hook)
|
|
2
|
-
export const VERSION = '0.
|
|
2
|
+
export const VERSION = '0.6.0';
|