@trans-ai/trans-code 0.1.4 → 0.2.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 +3 -3
- package/docs/ACP.md +2 -2
- package/package.json +2 -1
- package/src/acp/session.ts +7 -3
- package/src/agent/loop.test.ts +113 -0
- package/src/agent/loop.ts +27 -6
- package/src/agent/permission.test.ts +50 -0
- package/src/agent/permission.ts +34 -0
- package/src/agent/system-prompt.test.ts +54 -0
- package/src/agent/system-prompt.ts +15 -1
- package/src/agent/tools/bash.test.ts +115 -0
- package/src/agent/tools/bash.ts +315 -0
- package/src/agent/tools/diff-plumbing.test.ts +81 -0
- package/src/agent/tools/diff.test.ts +82 -0
- package/src/agent/tools/diff.ts +146 -0
- package/src/agent/tools/files.test.ts +133 -0
- package/src/agent/tools/files.ts +240 -0
- package/src/agent/tools/fs-utils.ts +117 -0
- package/src/agent/tools/index.ts +59 -0
- package/src/agent/tools/read-only.test.ts +78 -0
- package/src/agent/tools/read-only.ts +181 -0
- package/src/agent/tools/search.test.ts +55 -0
- package/src/agent/tools/search.ts +108 -0
- package/src/agent/tools/session.test.ts +34 -0
- package/src/agent/tools/session.ts +35 -0
- package/src/agent/tools/types.ts +47 -0
- package/src/cli.ts +3 -2
- package/src/commands.test.ts +56 -0
- package/src/commands.ts +31 -0
- package/src/provider/provider.ts +2 -2
- package/src/screens/REPL.tsx +108 -6
- package/src/screens/diff-render.test.ts +36 -0
- package/src/screens/diff-render.ts +26 -0
- package/src/screens/file-completion.test.ts +64 -0
- package/src/screens/file-completion.ts +49 -0
- package/src/version.ts +1 -1
- package/src/agent/tools.ts +0 -483
package/src/screens/REPL.tsx
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { Box, Text, useInput, useApp } from 'ink';
|
|
2
|
-
import { useState, useCallback, useRef } from 'react';
|
|
2
|
+
import { useState, useCallback, useEffect, useMemo, useRef } from 'react';
|
|
3
|
+
import path from 'node:path';
|
|
3
4
|
import { findCommand, getCommands, type CommandContext } from '../commands.js';
|
|
4
5
|
import { getAvailableModels, createProviderWithKey, createProvider, setPreferredModel, type ModelId } from '../provider/provider.js';
|
|
5
6
|
import { runAgentLoop, type PermissionRequest } from '../agent/loop.js';
|
|
6
|
-
import {
|
|
7
|
+
import { createToolSession } from '../agent/tools/index.js';
|
|
8
|
+
import { getPermissionMode } from '../agent/permission.js';
|
|
9
|
+
import { walkFiles } from '../agent/tools/fs-utils.js';
|
|
10
|
+
import { applyCompletion, filterFiles, mentionQuery } from './file-completion.js';
|
|
11
|
+
import { renderDiff } from './diff-render.js';
|
|
12
|
+
import { buildSystemPrompt, INIT_PROMPT, loadProjectContext } from '../agent/system-prompt.js';
|
|
7
13
|
import { HumanMessage, SystemMessage, type BaseMessage } from '@langchain/core/messages';
|
|
8
14
|
|
|
9
15
|
interface HistoryEntry {
|
|
@@ -54,6 +60,45 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
54
60
|
const busyRef = useRef(false);
|
|
55
61
|
const abortRef = useRef<AbortController | undefined>(undefined);
|
|
56
62
|
const messagesRef = useRef<BaseMessage[]>([]);
|
|
63
|
+
// One tool session per process: bash cwd and read-file state persist
|
|
64
|
+
// across prompts, like the ACP session.
|
|
65
|
+
const toolSessionRef = useRef(createToolSession(process.cwd()));
|
|
66
|
+
|
|
67
|
+
// @-mention file completion: lazy file list, active query, dismissed flag.
|
|
68
|
+
const [fileList, setFileList] = useState<string[] | null>(null);
|
|
69
|
+
const [mentionCursor, setMentionCursor] = useState(0);
|
|
70
|
+
const [mentionDismissed, setMentionDismissed] = useState(false);
|
|
71
|
+
const mention = phase === 'idle' && !busy ? mentionQuery(input) : null;
|
|
72
|
+
const mentionActive = mention !== null && !mentionDismissed;
|
|
73
|
+
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (mention === null) setMentionDismissed(false);
|
|
76
|
+
}, [mention]);
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
setMentionCursor(0);
|
|
79
|
+
}, [mention]);
|
|
80
|
+
// Load the workspace file list on first use; refresh on next open after a
|
|
81
|
+
// prompt so files the agent created/removed show up.
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
if (!mentionActive || fileList) return;
|
|
84
|
+
let cancelled = false;
|
|
85
|
+
walkFiles(process.cwd())
|
|
86
|
+
.then((files) => {
|
|
87
|
+
if (cancelled) return;
|
|
88
|
+
setFileList(
|
|
89
|
+
files.map((file) => path.relative(process.cwd(), file).replace(/\\/g, '/')).sort(),
|
|
90
|
+
);
|
|
91
|
+
})
|
|
92
|
+
.catch(() => setFileList([]));
|
|
93
|
+
return () => {
|
|
94
|
+
cancelled = true;
|
|
95
|
+
};
|
|
96
|
+
}, [mentionActive, fileList]);
|
|
97
|
+
|
|
98
|
+
const mentionCandidates = useMemo(
|
|
99
|
+
() => (mentionActive && fileList ? filterFiles(fileList, mention ?? '', 8) : []),
|
|
100
|
+
[mentionActive, fileList, mention],
|
|
101
|
+
);
|
|
57
102
|
const pendingPermissionRef = useRef<{
|
|
58
103
|
request: PermissionRequest;
|
|
59
104
|
resolve: (allow: boolean) => void;
|
|
@@ -76,7 +121,7 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
76
121
|
|
|
77
122
|
const askInTerminal = useCallback(
|
|
78
123
|
(request: PermissionRequest, signal: AbortSignal): Promise<boolean> => {
|
|
79
|
-
const mode =
|
|
124
|
+
const mode = getPermissionMode();
|
|
80
125
|
if (mode === 'auto') return Promise.resolve(true);
|
|
81
126
|
if (mode === 'readonly') return Promise.resolve(false);
|
|
82
127
|
return new Promise<boolean>(resolve => {
|
|
@@ -96,11 +141,11 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
96
141
|
);
|
|
97
142
|
|
|
98
143
|
const runChat = useCallback(
|
|
99
|
-
async (text: string) => {
|
|
144
|
+
async (text: string, display?: string) => {
|
|
100
145
|
setBusyBoth(true);
|
|
101
146
|
const controller = new AbortController();
|
|
102
147
|
abortRef.current = controller;
|
|
103
|
-
addHistory({ input: text, output: '' });
|
|
148
|
+
addHistory({ input: display ?? text, output: '' });
|
|
104
149
|
|
|
105
150
|
try {
|
|
106
151
|
const provider =
|
|
@@ -127,6 +172,7 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
127
172
|
messages: messagesRef.current,
|
|
128
173
|
systemPrompt,
|
|
129
174
|
signal: controller.signal,
|
|
175
|
+
toolSession: toolSessionRef.current,
|
|
130
176
|
onEvent: event => {
|
|
131
177
|
if (event.type === 'text') {
|
|
132
178
|
appendChatOutput(lastWasTool ? `\n${event.text}` : event.text);
|
|
@@ -135,7 +181,9 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
135
181
|
appendChatOutput(`\n⚙ ${event.title}`);
|
|
136
182
|
lastWasTool = true;
|
|
137
183
|
} else if (event.type === 'tool_result') {
|
|
138
|
-
|
|
184
|
+
const marker = event.skippedPermission ? ' ✓ (read-only)' : event.ok ? ' ✓' : ' ✗';
|
|
185
|
+
appendChatOutput(marker);
|
|
186
|
+
if (event.diff) appendChatOutput(`\n${renderDiff(event.diff)}`);
|
|
139
187
|
lastWasTool = true;
|
|
140
188
|
}
|
|
141
189
|
},
|
|
@@ -153,6 +201,8 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
153
201
|
} finally {
|
|
154
202
|
setBusyBoth(false);
|
|
155
203
|
abortRef.current = undefined;
|
|
204
|
+
// The agent may have created/removed files; rebuild @-completion list.
|
|
205
|
+
setFileList(null);
|
|
156
206
|
dragFlush();
|
|
157
207
|
}
|
|
158
208
|
},
|
|
@@ -212,6 +262,14 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
212
262
|
return;
|
|
213
263
|
}
|
|
214
264
|
|
|
265
|
+
if (cmdName === 'init') {
|
|
266
|
+
commitInput('');
|
|
267
|
+
setHistoryIndex(-1);
|
|
268
|
+
setSavedInput('');
|
|
269
|
+
await runChat(INIT_PROMPT, trimmed);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
215
273
|
const cmd = findCommand(cmdName);
|
|
216
274
|
|
|
217
275
|
if (cmd) {
|
|
@@ -251,6 +309,11 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
251
309
|
// eslint-disable-next-line no-console
|
|
252
310
|
console.error(`CB ${JSON.stringify(ch)} ret=${key.return} esc=${key.escape}`);
|
|
253
311
|
}
|
|
312
|
+
// @-mention popup: Esc dismisses it instead of exiting the app.
|
|
313
|
+
if (key.escape && mentionActive && mentionCandidates.length > 0) {
|
|
314
|
+
setMentionDismissed(true);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
254
317
|
if (key.escape) {
|
|
255
318
|
// While generating, Esc cancels instead of exiting.
|
|
256
319
|
if (busyRef.current && abortRef.current) {
|
|
@@ -299,6 +362,23 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
299
362
|
return;
|
|
300
363
|
}
|
|
301
364
|
|
|
365
|
+
// @-mention popup navigation and completion (takes precedence over
|
|
366
|
+
// history navigation, tab completion and submission).
|
|
367
|
+
if (phase === 'idle' && mentionActive && mentionCandidates.length > 0) {
|
|
368
|
+
if (key.upArrow) {
|
|
369
|
+
setMentionCursor(prev => (prev - 1 + mentionCandidates.length) % mentionCandidates.length);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (key.downArrow) {
|
|
373
|
+
setMentionCursor(prev => (prev + 1) % mentionCandidates.length);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (key.tab || key.return) {
|
|
377
|
+
commitInput(applyCompletion(inputRef.current, mentionCandidates[mentionCursor]));
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
302
382
|
if (key.return) {
|
|
303
383
|
if (busyRef.current) return; // still generating; Esc cancels
|
|
304
384
|
handleSubmit(inputRef.current);
|
|
@@ -433,6 +513,28 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
433
513
|
</Box>
|
|
434
514
|
);
|
|
435
515
|
})()}
|
|
516
|
+
|
|
517
|
+
{phase === 'idle' && !busy && mentionActive && (
|
|
518
|
+
<Box flexDirection="column" marginLeft={2}>
|
|
519
|
+
{fileList === null ? (
|
|
520
|
+
<Text color="gray" dimColor>… loading files</Text>
|
|
521
|
+
) : mentionCandidates.length === 0 ? (
|
|
522
|
+
<Text color="gray" dimColor>no files match “{mention}”</Text>
|
|
523
|
+
) : (
|
|
524
|
+
<>
|
|
525
|
+
{mentionCandidates.map((file, i) => (
|
|
526
|
+
<Box key={file}>
|
|
527
|
+
<Text color={i === mentionCursor ? 'cyan' : 'gray'}>
|
|
528
|
+
{i === mentionCursor ? '❯ ' : ' '}
|
|
529
|
+
{file}
|
|
530
|
+
</Text>
|
|
531
|
+
</Box>
|
|
532
|
+
))}
|
|
533
|
+
<Text color="gray" dimColor>↑↓ navigate · tab/enter insert · esc close</Text>
|
|
534
|
+
</>
|
|
535
|
+
)}
|
|
536
|
+
</Box>
|
|
537
|
+
)}
|
|
436
538
|
</Box>
|
|
437
539
|
);
|
|
438
540
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { renderDiff } from './diff-render.js';
|
|
3
|
+
|
|
4
|
+
const GREEN = '\x1b[32m';
|
|
5
|
+
const RED = '\x1b[31m';
|
|
6
|
+
const CYAN = '\x1b[36m';
|
|
7
|
+
const RESET = '\x1b[0m';
|
|
8
|
+
|
|
9
|
+
describe('renderDiff', () => {
|
|
10
|
+
test('colors added, removed and hunk-header lines', () => {
|
|
11
|
+
const diff = '@@ -1,2 +1,2 @@\n context\n-old\n+new';
|
|
12
|
+
const out = renderDiff(diff);
|
|
13
|
+
const lines = out.split('\n');
|
|
14
|
+
expect(lines[0]).toBe(`${CYAN}@@ -1,2 +1,2 @@${RESET}`);
|
|
15
|
+
expect(lines[1]).toBe(' context');
|
|
16
|
+
expect(lines[2]).toBe(`${RED}-old${RESET}`);
|
|
17
|
+
expect(lines[3]).toBe(`${GREEN}+new${RESET}`);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('caps output and reports omitted lines', () => {
|
|
21
|
+
const diff = Array.from({ length: 6 }, (_, i) => `+line${i}`).join('\n');
|
|
22
|
+
const out = renderDiff(diff, 3);
|
|
23
|
+
expect(out.split('\n').length).toBe(4);
|
|
24
|
+
expect(out).toContain('... (3 more lines)');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('empty diff renders empty', () => {
|
|
28
|
+
expect(renderDiff('')).toBe('');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('neutral lines like truncation markers stay uncolored', () => {
|
|
32
|
+
const out = renderDiff('+a\n... (+12 more lines)');
|
|
33
|
+
const lines = out.split('\n');
|
|
34
|
+
expect(lines[1]).toBe('... (+12 more lines)');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a unified diff as an ANSI-colored string for the ink REPL.
|
|
3
|
+
* Added lines are green, removed red, hunk headers cyan; everything else
|
|
4
|
+
* (context, truncation markers) stays uncolored. Output is capped.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const GREEN = '\x1b[32m';
|
|
8
|
+
const RED = '\x1b[31m';
|
|
9
|
+
const CYAN = '\x1b[36m';
|
|
10
|
+
const RESET = '\x1b[0m';
|
|
11
|
+
|
|
12
|
+
export function renderDiff(diff: string, maxLines = 40): string {
|
|
13
|
+
if (!diff) return '';
|
|
14
|
+
const lines = diff.split('\n');
|
|
15
|
+
const shown = lines.slice(0, maxLines);
|
|
16
|
+
const colored = shown.map((line) => {
|
|
17
|
+
if (line.startsWith('+')) return `${GREEN}${line}${RESET}`;
|
|
18
|
+
if (line.startsWith('-')) return `${RED}${line}${RESET}`;
|
|
19
|
+
if (line.startsWith('@@')) return `${CYAN}${line}${RESET}`;
|
|
20
|
+
return line;
|
|
21
|
+
});
|
|
22
|
+
if (lines.length > maxLines) {
|
|
23
|
+
colored.push(`... (${lines.length - maxLines} more lines)`);
|
|
24
|
+
}
|
|
25
|
+
return colored.join('\n');
|
|
26
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { applyCompletion, filterFiles, mentionQuery } from './file-completion.js';
|
|
3
|
+
|
|
4
|
+
describe('mentionQuery', () => {
|
|
5
|
+
test('detects a trailing @token', () => {
|
|
6
|
+
expect(mentionQuery('fix @src')).toBe('src');
|
|
7
|
+
expect(mentionQuery('fix @src/a.ts')).toBe('src/a.ts');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test('empty query right after @', () => {
|
|
11
|
+
expect(mentionQuery('hello @')).toBe('');
|
|
12
|
+
expect(mentionQuery('@')).toBe('');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('@ at the very start of the input', () => {
|
|
16
|
+
expect(mentionQuery('@readme')).toBe('readme');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('null when @ is glued to a word (like an email)', () => {
|
|
20
|
+
expect(mentionQuery('mail me a@b.com')).toBeNull();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('null when the @token is not the trailing segment', () => {
|
|
24
|
+
expect(mentionQuery('see @foo and more')).toBeNull();
|
|
25
|
+
expect(mentionQuery('no mention here')).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('filterFiles', () => {
|
|
30
|
+
const files = ['src/app.ts', 'src/main.tsx', 'README.md', 'src/util/string.ts', 'a/b/c.ts'];
|
|
31
|
+
|
|
32
|
+
test('empty query returns the first files up to the limit', () => {
|
|
33
|
+
expect(filterFiles(files, '', 3)).toEqual(files.slice(0, 3));
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('substring match, case-insensitive', () => {
|
|
37
|
+
expect(filterFiles(files, 'readme', 8)).toEqual(['README.md']);
|
|
38
|
+
expect(filterFiles(files, 'main', 8)).toEqual(['src/main.tsx']);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('substring matches rank before subsequence matches', () => {
|
|
42
|
+
const ranked = filterFiles(['axb.ts', 'ab.ts'], 'ab', 8);
|
|
43
|
+
expect(ranked).toEqual(['ab.ts', 'axb.ts']);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('subsequence fallback matches characters in order', () => {
|
|
47
|
+
expect(filterFiles(files, 'abc', 8)).toEqual(['a/b/c.ts']);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('no matches yields an empty array', () => {
|
|
51
|
+
expect(filterFiles(files, 'zzzz', 8)).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('applyCompletion', () => {
|
|
56
|
+
test('replaces the trailing token with the selected path', () => {
|
|
57
|
+
expect(applyCompletion('fix @src', 'src/app.ts')).toBe('fix @src/app.ts ');
|
|
58
|
+
expect(applyCompletion('@re', 'README.md')).toBe('@README.md ');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('leaves the input untouched when no token is active', () => {
|
|
62
|
+
expect(applyCompletion('no token here', 'README.md')).toBe('no token here');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for the REPL @-mention file completion. Rendering/keys live in
|
|
3
|
+
* REPL.tsx; these are the testable parts.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Returns the active @query at the end of the input, or null when inactive. */
|
|
7
|
+
export function mentionQuery(input: string): string | null {
|
|
8
|
+
const match = /(?:^|\s)@([^\s@]*)$/.exec(input);
|
|
9
|
+
return match === null ? null : match[1];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function isSubsequence(query: string, target: string): boolean {
|
|
13
|
+
let pos = -1;
|
|
14
|
+
for (const char of query) {
|
|
15
|
+
pos = target.indexOf(char, pos + 1);
|
|
16
|
+
if (pos === -1) return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Filters file paths by query: case-insensitive substring matches first
|
|
23
|
+
* (path order), then subsequence matches. Empty query returns the first
|
|
24
|
+
* `limit` paths unchanged.
|
|
25
|
+
*/
|
|
26
|
+
export function filterFiles(files: string[], query: string, limit: number): string[] {
|
|
27
|
+
if (query === '') return files.slice(0, limit);
|
|
28
|
+
const lower = query.toLowerCase();
|
|
29
|
+
const substring: string[] = [];
|
|
30
|
+
const subsequence: string[] = [];
|
|
31
|
+
for (const file of files) {
|
|
32
|
+
const fileLower = file.toLowerCase();
|
|
33
|
+
if (fileLower.includes(lower)) {
|
|
34
|
+
substring.push(file);
|
|
35
|
+
} else if (isSubsequence(lower, fileLower)) {
|
|
36
|
+
subsequence.push(file);
|
|
37
|
+
}
|
|
38
|
+
if (substring.length >= limit) break;
|
|
39
|
+
}
|
|
40
|
+
return [...substring, ...subsequence].slice(0, limit);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Replaces the trailing @token with the selected path (plus a space). */
|
|
44
|
+
export function applyCompletion(input: string, selectedPath: string): string {
|
|
45
|
+
const match = /(?:^|\s)@([^\s@]*)$/.exec(input);
|
|
46
|
+
if (match === null) return input;
|
|
47
|
+
const atStart = match.index + (match[0].startsWith('@') ? 0 : 1);
|
|
48
|
+
return `${input.slice(0, atStart)}@${selectedPath} `;
|
|
49
|
+
}
|
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.1
|
|
2
|
+
export const VERSION = '0.2.1';
|