@trans-ai/trans-code 0.1.0 → 0.1.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/README.md +6 -0
- package/package.json +1 -1
- package/src/cli.ts +7 -0
- package/src/screens/REPL.tsx +156 -5
- package/src/upgrade.ts +110 -0
package/README.md
CHANGED
|
@@ -97,6 +97,12 @@ bun run test:acp:openai # 真实 LangChain 流式 + 工具循环(本地假 O
|
|
|
97
97
|
bun run test:acp:real # 真实模型 + 真实工具调用(无 key 时自动跳过)
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
+
npm 全局安装的用户可以自升级:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
transcode upgrade # 检查 registry 最新版并升级(开发安装会给出指引)
|
|
104
|
+
```
|
|
105
|
+
|
|
100
106
|
## 接入 multica
|
|
101
107
|
|
|
102
108
|
multica daemon 支持“自定义 runtime profile”:选择一个已支持的协议家族,但用你自己的可执行文件启动。transcode 实现的是 **ACP** 协议,因此用 `hermes` 家族接入。multica 会用 `<command> acp` 启动它。
|
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -5,6 +5,7 @@ const USAGE = `transcode v${VERSION}
|
|
|
5
5
|
Usage:
|
|
6
6
|
transcode Start the interactive REPL
|
|
7
7
|
transcode acp Start the ACP stdio server (for multica and other ACP clients)
|
|
8
|
+
transcode upgrade Upgrade the npm global install to the latest version
|
|
8
9
|
transcode --version Print the version
|
|
9
10
|
transcode --help Show this help
|
|
10
11
|
|
|
@@ -42,6 +43,12 @@ export async function runCli(argv: string[]): Promise<void> {
|
|
|
42
43
|
return;
|
|
43
44
|
}
|
|
44
45
|
|
|
46
|
+
if (command === 'upgrade' || command === 'update') {
|
|
47
|
+
const { runUpgrade } = await import('./upgrade.js');
|
|
48
|
+
await runUpgrade();
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
45
52
|
if (command && !command.startsWith('-')) {
|
|
46
53
|
process.stderr.write(`Unknown command: ${command}\n\n${USAGE}`);
|
|
47
54
|
process.exitCode = 2;
|
package/src/screens/REPL.tsx
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { Box, Text, useInput, useApp } from 'ink';
|
|
2
2
|
import { useState, useCallback, useRef } from 'react';
|
|
3
3
|
import { findCommand, getCommands, type CommandContext } from '../commands.js';
|
|
4
|
-
import { getAvailableModels, createProviderWithKey, type ModelId } from '../provider/provider.js';
|
|
4
|
+
import { getAvailableModels, createProviderWithKey, createProvider, type ModelId } from '../provider/provider.js';
|
|
5
|
+
import { runAgentLoop, type PermissionRequest } from '../agent/loop.js';
|
|
6
|
+
import { buildSystemPrompt, loadProjectContext } from '../agent/system-prompt.js';
|
|
7
|
+
import { HumanMessage, SystemMessage, type BaseMessage } from '@langchain/core/messages';
|
|
5
8
|
|
|
6
9
|
interface HistoryEntry {
|
|
7
10
|
input: string;
|
|
8
11
|
output: string;
|
|
9
12
|
}
|
|
10
13
|
|
|
11
|
-
type PromptPhase = 'idle' | 'model_select' | 'model_apikey';
|
|
14
|
+
type PromptPhase = 'idle' | 'model_select' | 'model_apikey' | 'permission';
|
|
12
15
|
|
|
13
16
|
interface REPLProps {
|
|
14
17
|
ctx: CommandContext;
|
|
@@ -46,6 +49,112 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
46
49
|
setHistory(prev => [...prev, entry]);
|
|
47
50
|
}, []);
|
|
48
51
|
|
|
52
|
+
// ---- chat state ----
|
|
53
|
+
const [busy, setBusy] = useState(false);
|
|
54
|
+
const busyRef = useRef(false);
|
|
55
|
+
const abortRef = useRef<AbortController | undefined>(undefined);
|
|
56
|
+
const messagesRef = useRef<BaseMessage[]>([]);
|
|
57
|
+
const pendingPermissionRef = useRef<{
|
|
58
|
+
request: PermissionRequest;
|
|
59
|
+
resolve: (allow: boolean) => void;
|
|
60
|
+
} | null>(null);
|
|
61
|
+
|
|
62
|
+
const setBusyBoth = useCallback((v: boolean) => {
|
|
63
|
+
busyRef.current = v;
|
|
64
|
+
setBusy(v);
|
|
65
|
+
}, []);
|
|
66
|
+
|
|
67
|
+
const appendChatOutput = useCallback((chunk: string) => {
|
|
68
|
+
setHistory(prev => {
|
|
69
|
+
if (prev.length === 0) return prev;
|
|
70
|
+
const next = [...prev];
|
|
71
|
+
const last = next[next.length - 1];
|
|
72
|
+
next[next.length - 1] = { ...last, output: last.output + chunk };
|
|
73
|
+
return next;
|
|
74
|
+
});
|
|
75
|
+
}, []);
|
|
76
|
+
|
|
77
|
+
const askInTerminal = useCallback(
|
|
78
|
+
(request: PermissionRequest, signal: AbortSignal): Promise<boolean> => {
|
|
79
|
+
const mode = process.env.TRANSCODE_PERMISSION_MODE ?? 'ask';
|
|
80
|
+
if (mode === 'auto') return Promise.resolve(true);
|
|
81
|
+
if (mode === 'readonly') return Promise.resolve(false);
|
|
82
|
+
return new Promise<boolean>(resolve => {
|
|
83
|
+
const settle = (allow: boolean) => {
|
|
84
|
+
signal.removeEventListener('abort', onAbort);
|
|
85
|
+
pendingPermissionRef.current = null;
|
|
86
|
+
setPhase('idle');
|
|
87
|
+
resolve(allow);
|
|
88
|
+
};
|
|
89
|
+
const onAbort = () => settle(false);
|
|
90
|
+
signal.addEventListener('abort', onAbort);
|
|
91
|
+
pendingPermissionRef.current = { request, resolve: settle };
|
|
92
|
+
setPhase('permission');
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
[],
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const runChat = useCallback(
|
|
99
|
+
async (text: string) => {
|
|
100
|
+
setBusyBoth(true);
|
|
101
|
+
const controller = new AbortController();
|
|
102
|
+
abortRef.current = controller;
|
|
103
|
+
addHistory({ input: text, output: '' });
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
const systemPrompt = buildSystemPrompt(await loadProjectContext(process.cwd()));
|
|
107
|
+
if (messagesRef.current[0]?.getType() === 'system') {
|
|
108
|
+
messagesRef.current[0] = new SystemMessage(systemPrompt);
|
|
109
|
+
} else {
|
|
110
|
+
messagesRef.current.unshift(new SystemMessage(systemPrompt));
|
|
111
|
+
}
|
|
112
|
+
messagesRef.current.push(new HumanMessage(text));
|
|
113
|
+
|
|
114
|
+
const provider =
|
|
115
|
+
process.env.TRANSCODE_MOCK === '1'
|
|
116
|
+
? undefined
|
|
117
|
+
: createProvider(ctx.modelId);
|
|
118
|
+
|
|
119
|
+
let lastWasTool = false;
|
|
120
|
+
const result = await runAgentLoop({
|
|
121
|
+
model: provider?.model,
|
|
122
|
+
cwd: process.cwd(),
|
|
123
|
+
messages: messagesRef.current,
|
|
124
|
+
systemPrompt,
|
|
125
|
+
signal: controller.signal,
|
|
126
|
+
onEvent: event => {
|
|
127
|
+
if (event.type === 'text') {
|
|
128
|
+
appendChatOutput(lastWasTool ? `\n${event.text}` : event.text);
|
|
129
|
+
lastWasTool = false;
|
|
130
|
+
} else if (event.type === 'tool_call') {
|
|
131
|
+
appendChatOutput(`\n⚙ ${event.title}`);
|
|
132
|
+
lastWasTool = true;
|
|
133
|
+
} else if (event.type === 'tool_result') {
|
|
134
|
+
appendChatOutput(event.ok ? ' ✓' : ' ✗');
|
|
135
|
+
lastWasTool = true;
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
requestPermission: request => askInTerminal(request, controller.signal),
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
messagesRef.current = result.messages;
|
|
142
|
+
if (controller.signal.aborted) appendChatOutput('\n(cancelled)');
|
|
143
|
+
} catch (error) {
|
|
144
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
145
|
+
appendChatOutput(`\n⚠ ${message}`);
|
|
146
|
+
if (/api.?key/i.test(message)) {
|
|
147
|
+
appendChatOutput(`\n(run "models" to switch model + set key, or export its env var)`);
|
|
148
|
+
}
|
|
149
|
+
} finally {
|
|
150
|
+
setBusyBoth(false);
|
|
151
|
+
abortRef.current = undefined;
|
|
152
|
+
dragFlush();
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
[ctx.modelId, addHistory, appendChatOutput, askInTerminal, setBusyBoth, dragFlush],
|
|
156
|
+
);
|
|
157
|
+
|
|
49
158
|
const enterModelSelect = useCallback(() => {
|
|
50
159
|
const currentIdx = models.findIndex(m => m.id === ctx.modelId);
|
|
51
160
|
setModelCursor(currentIdx >= 0 ? currentIdx : 0);
|
|
@@ -114,15 +223,23 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
114
223
|
} else if (result.output) {
|
|
115
224
|
addHistory({ input: trimmed, output: result.output });
|
|
116
225
|
}
|
|
117
|
-
} else {
|
|
226
|
+
} else if (trimmed.startsWith('/')) {
|
|
227
|
+
// Explicit slash prefix means the user intended a command.
|
|
118
228
|
addHistory({ input: trimmed, output: `Unknown command: ${cmdName}. Type "help" for available commands.` });
|
|
229
|
+
} else {
|
|
230
|
+
// Free text — converse with the current model.
|
|
231
|
+
commitInput('');
|
|
232
|
+
setHistoryIndex(-1);
|
|
233
|
+
setSavedInput('');
|
|
234
|
+
await runChat(trimmed);
|
|
235
|
+
return;
|
|
119
236
|
}
|
|
120
237
|
dragFlush();
|
|
121
238
|
|
|
122
239
|
commitInput('');
|
|
123
240
|
setHistoryIndex(-1);
|
|
124
241
|
setSavedInput('');
|
|
125
|
-
}, [exit, ctx, phase, handleApiKey, addHistory, enterModelSelect, commitInput, dragFlush]);
|
|
242
|
+
}, [exit, ctx, phase, handleApiKey, addHistory, enterModelSelect, commitInput, dragFlush, runChat]);
|
|
126
243
|
|
|
127
244
|
useInput((ch, key) => {
|
|
128
245
|
if (process.env.TRANSCODE_DEBUG_KEYS === '1') {
|
|
@@ -130,6 +247,11 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
130
247
|
console.error(`CB ${JSON.stringify(ch)} ret=${key.return} esc=${key.escape}`);
|
|
131
248
|
}
|
|
132
249
|
if (key.escape) {
|
|
250
|
+
// While generating, Esc cancels instead of exiting.
|
|
251
|
+
if (busyRef.current && abortRef.current) {
|
|
252
|
+
abortRef.current.abort();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
133
255
|
if (phase !== 'idle') {
|
|
134
256
|
setPhase('idle');
|
|
135
257
|
setPendingModel(null);
|
|
@@ -141,6 +263,18 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
141
263
|
return;
|
|
142
264
|
}
|
|
143
265
|
|
|
266
|
+
if (phase === 'permission') {
|
|
267
|
+
if (ch === 'y' || ch === 'Y' || key.return) {
|
|
268
|
+
pendingPermissionRef.current?.resolve(true);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (ch === 'n' || ch === 'N') {
|
|
272
|
+
pendingPermissionRef.current?.resolve(false);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
144
278
|
if (phase === 'model_select') {
|
|
145
279
|
if (key.return) {
|
|
146
280
|
const selected = models[modelCursor];
|
|
@@ -161,6 +295,7 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
161
295
|
}
|
|
162
296
|
|
|
163
297
|
if (key.return) {
|
|
298
|
+
if (busyRef.current) return; // still generating; Esc cancels
|
|
164
299
|
handleSubmit(inputRef.current);
|
|
165
300
|
return;
|
|
166
301
|
}
|
|
@@ -221,7 +356,9 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
221
356
|
? 'model> '
|
|
222
357
|
: phase === 'model_apikey'
|
|
223
358
|
? 'apikey> '
|
|
224
|
-
:
|
|
359
|
+
: phase === 'permission'
|
|
360
|
+
? 'allow?> '
|
|
361
|
+
: '> ';
|
|
225
362
|
|
|
226
363
|
return (
|
|
227
364
|
<Box flexDirection="column" paddingX={1}>
|
|
@@ -241,6 +378,20 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
241
378
|
</Box>
|
|
242
379
|
))}
|
|
243
380
|
|
|
381
|
+
{phase === 'permission' && pendingPermissionRef.current && (
|
|
382
|
+
<Box marginLeft={2}>
|
|
383
|
+
<Text color="yellow">
|
|
384
|
+
⚙ {pendingPermissionRef.current.request.title} ({pendingPermissionRef.current.request.kind}) — allow? y/n
|
|
385
|
+
</Text>
|
|
386
|
+
</Box>
|
|
387
|
+
)}
|
|
388
|
+
|
|
389
|
+
{busy && phase === 'idle' && (
|
|
390
|
+
<Box marginLeft={2}>
|
|
391
|
+
<Text dimColor>… generating (Esc to cancel)</Text>
|
|
392
|
+
</Box>
|
|
393
|
+
)}
|
|
394
|
+
|
|
244
395
|
{phase === 'model_select' && (
|
|
245
396
|
<Box flexDirection="column" marginLeft={2}>
|
|
246
397
|
{models.map((m, i) => (
|
package/src/upgrade.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { realpathSync } from 'node:fs';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { VERSION } from './version.js';
|
|
6
|
+
|
|
7
|
+
const PKG = '@trans-ai/trans-code';
|
|
8
|
+
|
|
9
|
+
function sh(cmd: string, args: string[]): Promise<{ code: number; stdout: string }> {
|
|
10
|
+
return new Promise(resolve => {
|
|
11
|
+
const child = spawn(cmd, args, { stdio: ['ignore', 'pipe', 'inherit'] });
|
|
12
|
+
let stdout = '';
|
|
13
|
+
child.stdout.on('data', (d: Buffer) => {
|
|
14
|
+
stdout += d.toString();
|
|
15
|
+
});
|
|
16
|
+
child.on('error', err => {
|
|
17
|
+
process.stderr.write(`[transcode] failed to run ${cmd}: ${err.message}\n`);
|
|
18
|
+
resolve({ code: 1, stdout });
|
|
19
|
+
});
|
|
20
|
+
child.on('close', code => resolve({ code: code ?? 1, stdout }));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function shInherit(cmd: string, args: string[]): Promise<number> {
|
|
25
|
+
return new Promise(resolve => {
|
|
26
|
+
const child = spawn(cmd, args, { stdio: 'inherit' });
|
|
27
|
+
child.on('error', err => {
|
|
28
|
+
process.stderr.write(`[transcode] failed to run ${cmd}: ${err.message}\n`);
|
|
29
|
+
resolve(1);
|
|
30
|
+
});
|
|
31
|
+
child.on('close', code => resolve(code ?? 1));
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function npmRoot(): Promise<string> {
|
|
36
|
+
const r = await sh('npm', ['root', '-g']);
|
|
37
|
+
return r.stdout.trim();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function latestVersion(): Promise<string | null> {
|
|
41
|
+
const r = await sh('npm', ['view', `${PKG}@latest`, 'version']);
|
|
42
|
+
if (r.code !== 0) return null;
|
|
43
|
+
const v = r.stdout.trim();
|
|
44
|
+
return v || null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function installedVersionUnder(root: string): string | null {
|
|
48
|
+
try {
|
|
49
|
+
const pkg = JSON.parse(readFileSync(`${root}/${PKG}/package.json`, 'utf8'));
|
|
50
|
+
return typeof pkg.version === 'string' ? pkg.version : null;
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function runUpgrade(): Promise<void> {
|
|
57
|
+
process.stdout.write(`transcode v${VERSION} — checking for upgrades…\n`);
|
|
58
|
+
|
|
59
|
+
const entry = realpathSync(fileURLToPath(import.meta.url));
|
|
60
|
+
const root = await npmRoot();
|
|
61
|
+
// Normalize both sides through realpath — macOS aliases like /tmp vs
|
|
62
|
+
// /private/tmp would otherwise break prefix comparison.
|
|
63
|
+
const rootReal = root ? realpathSync(root) : '';
|
|
64
|
+
if (process.env.TRANSCODE_DEBUG_UPGRADE === '1') {
|
|
65
|
+
process.stdout.write(`[debug] entry=${entry}\n[debug] root=${rootReal}\n`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!rootReal || !entry.startsWith(rootReal)) {
|
|
69
|
+
process.stdout.write(
|
|
70
|
+
[
|
|
71
|
+
'This looks like a development install (bun link / repo checkout),',
|
|
72
|
+
'not an npm global install — nothing to auto-upgrade.',
|
|
73
|
+
'',
|
|
74
|
+
'Options:',
|
|
75
|
+
' · pull the latest repo: git pull && bun install',
|
|
76
|
+
' · switch to npm install: npm i -g @trans-ai/trans-code',
|
|
77
|
+
].join('\n') + '\n',
|
|
78
|
+
);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const latest = await latestVersion();
|
|
83
|
+
if (!latest) {
|
|
84
|
+
process.stderr.write(
|
|
85
|
+
'[transcode] could not reach the npm registry to check the latest version.\n',
|
|
86
|
+
);
|
|
87
|
+
process.exitCode = 1;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (latest === VERSION) {
|
|
92
|
+
process.stdout.write(`Already up to date (v${VERSION}).\n`);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
process.stdout.write(`Upgrading ${VERSION} → ${latest}…\n`);
|
|
97
|
+
const code = await shInherit('npm', ['i', '-g', `${PKG}@latest`]);
|
|
98
|
+
if (code !== 0) {
|
|
99
|
+
process.stderr.write('[transcode] upgrade failed — see npm output above.\n');
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const installed = installedVersionUnder(root);
|
|
105
|
+
if (installed) {
|
|
106
|
+
process.stdout.write(`Upgraded to v${installed}. Restart transcode if it is running.\n`);
|
|
107
|
+
} else {
|
|
108
|
+
process.stdout.write(`Upgrade finished. Restart transcode if it is running.\n`);
|
|
109
|
+
}
|
|
110
|
+
}
|