@trans-ai/trans-code 0.2.3 → 0.3.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/README.md +58 -17
- package/package.json +1 -1
- package/src/acp/session.ts +15 -1
- package/src/agent/loop.test.ts +401 -1
- package/src/agent/loop.ts +137 -13
- package/src/agent/system-prompt.test.ts +24 -1
- package/src/agent/system-prompt.ts +25 -0
- package/src/agent/tools/ask-model.test.ts +54 -0
- package/src/agent/tools/ask-model.ts +44 -0
- package/src/agent/tools/dispatch.test.ts +115 -0
- package/src/agent/tools/dispatch.ts +141 -0
- package/src/agent/tools/files.test.ts +96 -0
- package/src/agent/tools/files.ts +111 -29
- package/src/agent/tools/fs-utils.ts +40 -0
- package/src/agent/tools/index.ts +27 -2
- package/src/agent/tools/search.test.ts +55 -1
- package/src/agent/tools/search.ts +56 -8
- package/src/agent/tools/session.ts +9 -0
- package/src/agent/tools/todo.test.ts +54 -0
- package/src/agent/tools/todo.ts +47 -0
- package/src/agent/tools/types.ts +13 -1
- package/src/cli.ts +1 -0
- package/src/commands.ts +37 -2
- package/src/config.ts +21 -3
- package/src/main.tsx +6 -8
- package/src/provider/provider.test.ts +117 -0
- package/src/provider/provider.ts +278 -34
- package/src/provider/routing.test.ts +109 -0
- package/src/screens/REPL.tsx +204 -31
- package/src/screens/banner.test.ts +21 -0
- package/src/screens/banner.ts +9 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { AgentTool } from './types.js';
|
|
3
|
+
import type { TodoItem } from './session.js';
|
|
4
|
+
|
|
5
|
+
const STATUS_MARK: Record<TodoItem['status'], string> = {
|
|
6
|
+
pending: '[ ]',
|
|
7
|
+
in_progress: '[~]',
|
|
8
|
+
completed: '[x]',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const todoWriteTool: AgentTool = {
|
|
12
|
+
name: 'todo_write',
|
|
13
|
+
description:
|
|
14
|
+
'Create or replace the task todo list. Use it for any task with 3 or more steps: write the ' +
|
|
15
|
+
'list BEFORE you start, keep exactly one step in_progress at a time, mark steps completed ' +
|
|
16
|
+
'immediately after finishing them, and update the list as plans change. ' +
|
|
17
|
+
'This keeps you on the deliverable instead of endless exploration. ' +
|
|
18
|
+
'Each call replaces the entire list. Do not use it for single-step tasks.',
|
|
19
|
+
kind: 'think',
|
|
20
|
+
isMutating: false,
|
|
21
|
+
schema: z.object({
|
|
22
|
+
todos: z
|
|
23
|
+
.array(
|
|
24
|
+
z.object({
|
|
25
|
+
content: z.string().min(1).describe('Short step description'),
|
|
26
|
+
status: z.enum(['pending', 'in_progress', 'completed']).describe('Step status'),
|
|
27
|
+
}),
|
|
28
|
+
)
|
|
29
|
+
.describe('The full todo list (replaces the previous one)'),
|
|
30
|
+
}),
|
|
31
|
+
title: () => 'todos',
|
|
32
|
+
async execute(input, ctx) {
|
|
33
|
+
const todos = (input.todos as TodoItem[]).map((item) => ({
|
|
34
|
+
content: String(item.content),
|
|
35
|
+
status: item.status,
|
|
36
|
+
}));
|
|
37
|
+
ctx.session.todos = todos;
|
|
38
|
+
|
|
39
|
+
if (todos.length === 0) return 'Todo list cleared.';
|
|
40
|
+
|
|
41
|
+
const lines = todos.map(
|
|
42
|
+
(item, index) => `${STATUS_MARK[item.status]} ${index + 1}. ${item.content}`,
|
|
43
|
+
);
|
|
44
|
+
const allDone = todos.every((item) => item.status === 'completed');
|
|
45
|
+
return `${lines.join('\n')}${allDone ? '\n(all complete)' : ''}`;
|
|
46
|
+
},
|
|
47
|
+
};
|
package/src/agent/tools/types.ts
CHANGED
|
@@ -18,13 +18,23 @@ export interface ToolContext {
|
|
|
18
18
|
signal?: AbortSignal;
|
|
19
19
|
/** Session-scoped state shared across tool calls (shell cwd, read files). */
|
|
20
20
|
session: ToolSession;
|
|
21
|
+
/** Forwarded permission asker, so nested agents respect the same gate. */
|
|
22
|
+
requestPermission?: (request: {
|
|
23
|
+
toolCallId: string;
|
|
24
|
+
name: string;
|
|
25
|
+
title: string;
|
|
26
|
+
kind: ToolKind;
|
|
27
|
+
input: Record<string, unknown>;
|
|
28
|
+
}) => Promise<boolean>;
|
|
21
29
|
}
|
|
22
30
|
|
|
23
|
-
/** Rich execute result: model-facing output plus
|
|
31
|
+
/** Rich execute result: model-facing output plus optional extras. */
|
|
24
32
|
export interface ToolOutput {
|
|
25
33
|
output: string;
|
|
26
34
|
/** Unified diff text for TUI rendering; never sent to the model. */
|
|
27
35
|
diff?: string;
|
|
36
|
+
/** Base64 images sent to the model as multimodal content blocks. */
|
|
37
|
+
images?: Array<{ data: string; mediaType: string }>;
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
export interface AgentTool {
|
|
@@ -44,4 +54,6 @@ export interface InvokeResult {
|
|
|
44
54
|
output: string;
|
|
45
55
|
/** Unified diff text for UI rendering, when the tool produced one. */
|
|
46
56
|
diff?: string;
|
|
57
|
+
/** Base64 images for multimodal-capable models. */
|
|
58
|
+
images?: Array<{ data: string; mediaType: string }>;
|
|
47
59
|
}
|
package/src/cli.ts
CHANGED
|
@@ -14,6 +14,7 @@ Environment:
|
|
|
14
14
|
TRANSCODE_DEFAULT_PROVIDER Provider for bare model names (default: openai)
|
|
15
15
|
TRANSCODE_PERMISSION_MODE ask | auto | readonly (default: ask in ACP mode;
|
|
16
16
|
switch at runtime in the REPL with /mode)
|
|
17
|
+
TRANSCODE_MAX_TURNS Agent tool-call turn cap (default: 200)
|
|
17
18
|
ANTHROPIC_API_KEY / OPENAI_API_KEY / DEEPSEEK_API_KEY / ... provider keys
|
|
18
19
|
|
|
19
20
|
Examples:
|
package/src/commands.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { ModelId } from './provider/provider.js';
|
|
2
2
|
import { getAvailableModels } from './provider/provider.js';
|
|
3
3
|
import { getPermissionMode, PERMISSION_MODES, setPermissionMode } from './agent/permission.js';
|
|
4
|
+
import { getRouting, resolveRole, setRouting } from './provider/provider.js';
|
|
4
5
|
|
|
5
6
|
export interface CommandContext {
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
/** Registry id (deepseek) or provider:model spec (deepseek:deepseek-reasoner). */
|
|
8
|
+
modelId: string;
|
|
9
|
+
setModelId: (id: string) => void;
|
|
8
10
|
clearHistory: () => void;
|
|
9
11
|
}
|
|
10
12
|
|
|
@@ -68,6 +70,39 @@ const commands: Command[] = [
|
|
|
68
70
|
return { output: `Permission mode switched to ${target}` };
|
|
69
71
|
},
|
|
70
72
|
},
|
|
73
|
+
{
|
|
74
|
+
name: 'route',
|
|
75
|
+
description: 'Show or set role→model routing (analysis/design/vision/…)',
|
|
76
|
+
execute: (args) => {
|
|
77
|
+
if (args.length === 0) {
|
|
78
|
+
const routing = getRouting();
|
|
79
|
+
const lines = Object.entries(routing).map(([role, profile]) => {
|
|
80
|
+
const resolved = resolveRole(role);
|
|
81
|
+
return ` ${role.padEnd(10)} → ${profile}${resolved ? ` (${resolved.model})` : ' ⚠ 未解析'}`;
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
output:
|
|
85
|
+
`Role routing:\n${lines.length ? lines.join('\n') : ' (none)'}\n` +
|
|
86
|
+
'Usage: /route <role> <profile|-> — e.g. /route analysis glmc ; "-" removes the role',
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const [role, profile] = args;
|
|
90
|
+
if (!role || !profile) {
|
|
91
|
+
return { output: 'Usage: /route <role> <profile|->' };
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
setRouting(role, profile);
|
|
95
|
+
return {
|
|
96
|
+
output:
|
|
97
|
+
profile === '-'
|
|
98
|
+
? `Role "${role}" removed.`
|
|
99
|
+
: `Role "${role}" → ${profile}`,
|
|
100
|
+
};
|
|
101
|
+
} catch (error) {
|
|
102
|
+
return { output: (error as Error).message };
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
},
|
|
71
106
|
{
|
|
72
107
|
name: 'clear',
|
|
73
108
|
description: 'Clear screen',
|
package/src/config.ts
CHANGED
|
@@ -2,16 +2,34 @@ import { readFileSync, writeFileSync } from 'node:fs';
|
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
|
|
5
|
+
/** A named model endpoint: model id → {address, model name, credential}. */
|
|
6
|
+
export interface ProfileConfig {
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
model: string;
|
|
9
|
+
/** Key store id this profile uses (e.g. "glm" for both zhipu endpoints). */
|
|
10
|
+
keyId?: string;
|
|
11
|
+
/** Fallback environment variable for the credential. */
|
|
12
|
+
envKey?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
5
15
|
export interface TranscodeConfig {
|
|
6
16
|
model?: string;
|
|
7
17
|
apiKeys?: Record<string, string>;
|
|
18
|
+
/** Custom OpenAI-compatible gateway chosen in the /models flow. */
|
|
19
|
+
custom?: { baseUrl: string; model: string };
|
|
20
|
+
/** User-defined model profiles; override built-in registry ids by name. */
|
|
21
|
+
profiles?: Record<string, ProfileConfig>;
|
|
22
|
+
/** Role → profile routing (default / analysis / design / vision / …). */
|
|
23
|
+
routing?: Record<string, string>;
|
|
8
24
|
}
|
|
9
25
|
|
|
10
|
-
|
|
26
|
+
// Overridable so tests never touch the user's real ~/.transcode.json.
|
|
27
|
+
const configPath = (): string =>
|
|
28
|
+
process.env.TRANSCODE_CONFIG_PATH ?? join(homedir(), '.transcode.json');
|
|
11
29
|
|
|
12
30
|
export function loadConfig(): TranscodeConfig {
|
|
13
31
|
try {
|
|
14
|
-
const parsed = JSON.parse(readFileSync(
|
|
32
|
+
const parsed = JSON.parse(readFileSync(configPath(), 'utf8'));
|
|
15
33
|
if (parsed && typeof parsed === 'object') return parsed as TranscodeConfig;
|
|
16
34
|
} catch {
|
|
17
35
|
// missing or invalid — treat as empty
|
|
@@ -20,5 +38,5 @@ export function loadConfig(): TranscodeConfig {
|
|
|
20
38
|
}
|
|
21
39
|
|
|
22
40
|
export function saveConfig(config: TranscodeConfig): void {
|
|
23
|
-
writeFileSync(
|
|
41
|
+
writeFileSync(configPath(), JSON.stringify(config, null, 2) + '\n');
|
|
24
42
|
}
|
package/src/main.tsx
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { render, Box, Text } from 'ink';
|
|
2
2
|
import { useState } from 'react';
|
|
3
|
-
import { envModelLabel, getAvailableModels, preferredModelId
|
|
3
|
+
import { envModelLabel, getAvailableModels, preferredModelId } from './provider/provider.js';
|
|
4
4
|
import { type CommandContext } from './commands.js';
|
|
5
5
|
import REPL from './screens/REPL.js';
|
|
6
|
+
import { bannerLines } from './screens/banner.js';
|
|
6
7
|
import { createKeyedStdin } from './tui/keyed-stdin.js';
|
|
7
8
|
import { VERSION } from './version.js';
|
|
8
|
-
const DEFAULT_MODEL:
|
|
9
|
+
const DEFAULT_MODEL: string = preferredModelId() ?? 'opus';
|
|
9
10
|
|
|
10
|
-
function Banner({ modelId, cwd, envLabel }: { modelId:
|
|
11
|
+
function Banner({ modelId, cwd, envLabel }: { modelId: string; cwd: string; envLabel?: string }) {
|
|
11
12
|
const models = getAvailableModels();
|
|
12
13
|
const current = models.find(m => m.id === modelId);
|
|
13
14
|
|
|
14
|
-
const logo =
|
|
15
|
-
┌──────────────────────────────────────────────┐
|
|
16
|
-
│ T R A N S C O D E · Multi-model Agent │
|
|
17
|
-
└──────────────────────────────────────────────┘`
|
|
15
|
+
const logo = bannerLines('T R A N S C O D E · Multi-model Agent');
|
|
18
16
|
|
|
19
17
|
return (
|
|
20
18
|
<Box flexDirection="column" marginBottom={1}>
|
|
@@ -39,7 +37,7 @@ function Banner({ modelId, cwd, envLabel }: { modelId: ModelId; cwd: string; env
|
|
|
39
37
|
}
|
|
40
38
|
|
|
41
39
|
function App() {
|
|
42
|
-
const [modelId, setModelId] = useState<
|
|
40
|
+
const [modelId, setModelId] = useState<string>(DEFAULT_MODEL);
|
|
43
41
|
const cwd = process.cwd();
|
|
44
42
|
|
|
45
43
|
const ctx: CommandContext = {
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
2
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {
|
|
6
|
+
clearRegisteredApiKeys,
|
|
7
|
+
createModel,
|
|
8
|
+
getAvailableModels,
|
|
9
|
+
lookupApiKey,
|
|
10
|
+
registerApiKey,
|
|
11
|
+
resolveModel,
|
|
12
|
+
setCustomModel,
|
|
13
|
+
vendorModelSpec,
|
|
14
|
+
vendorModels,
|
|
15
|
+
verifyModel,
|
|
16
|
+
} from './provider.js';
|
|
17
|
+
|
|
18
|
+
let configDir: string;
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
configDir = mkdtempSync(path.join(tmpdir(), 'transcode-provider-test-'));
|
|
21
|
+
process.env.TRANSCODE_CONFIG_PATH = path.join(configDir, 'config.json');
|
|
22
|
+
clearRegisteredApiKeys();
|
|
23
|
+
});
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
delete process.env.TRANSCODE_CONFIG_PATH;
|
|
26
|
+
delete process.env.TRANSCODE_MODEL;
|
|
27
|
+
rmSync(configDir, { recursive: true, force: true });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('GLM Coding Plan preset', () => {
|
|
31
|
+
test('resolves the glmc registry id to the coding endpoint', () => {
|
|
32
|
+
const resolved = resolveModel('glmc');
|
|
33
|
+
expect(resolved.id).toBe('glmc');
|
|
34
|
+
expect(resolved.baseUrl).toBe('https://open.bigmodel.cn/api/coding/paas/v4');
|
|
35
|
+
expect(resolved.envKey).toBe('GLM_API_KEY');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('the glmc:MODEL spec overrides the model name but keeps the coding endpoint', () => {
|
|
39
|
+
const resolved = resolveModel('glmc:glm-4.5');
|
|
40
|
+
expect(resolved.model).toBe('glm-4.5');
|
|
41
|
+
expect(resolved.baseUrl).toBe('https://open.bigmodel.cn/api/coding/paas/v4');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('createModel wires the coding base URL into the client config', () => {
|
|
45
|
+
registerApiKey('glmc', 'test-key');
|
|
46
|
+
const provider = createModel('glmc');
|
|
47
|
+
expect(provider.config.baseUrl).toContain('/api/coding/');
|
|
48
|
+
expect(provider.config.model).toBe('glm-4.6');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('appears in the model list for /models', () => {
|
|
52
|
+
const entry = getAvailableModels().find((model) => model.id === 'glmc');
|
|
53
|
+
expect(entry?.name).toContain('Coding');
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('vendor model lists', () => {
|
|
58
|
+
test('every vendor exposes at least one curated model', () => {
|
|
59
|
+
for (const model of getAvailableModels()) {
|
|
60
|
+
if (model.id === 'custom') continue; // empty until the gateway is set up
|
|
61
|
+
expect(vendorModels(model.id).length).toBeGreaterThan(0);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('glm lists the current model line', () => {
|
|
66
|
+
const glm = vendorModels('glm');
|
|
67
|
+
expect(glm).toContain('glm-4.6');
|
|
68
|
+
expect(glm).toContain('glm-4.5');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('spec mapping routes registry ids to provider prefixes', () => {
|
|
72
|
+
expect(vendorModelSpec('deepseek', 'deepseek-reasoner')).toBe('deepseek:deepseek-reasoner');
|
|
73
|
+
expect(vendorModelSpec('opus', 'claude-sonnet-4')).toBe('anthropic:claude-sonnet-4');
|
|
74
|
+
expect(vendorModelSpec('gpt', 'gpt-4o-mini')).toBe('openai:gpt-4o-mini');
|
|
75
|
+
expect(vendorModelSpec('glmc', 'glm-4.5')).toBe('glmc:glm-4.5');
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('key reuse', () => {
|
|
80
|
+
test('a spec inherits the key registered for its vendor prefix', () => {
|
|
81
|
+
registerApiKey('deepseek', 'vendor-key');
|
|
82
|
+
expect(lookupApiKey('deepseek:deepseek-reasoner')).toBe('vendor-key');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('glmc reuses the key saved for glm (same credential family)', () => {
|
|
86
|
+
registerApiKey('glm', 'glm-key');
|
|
87
|
+
expect(lookupApiKey('glmc')).toBe('glm-key');
|
|
88
|
+
expect(lookupApiKey('glmc:glm-4.5')).toBe('glm-key');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('custom gateway', () => {
|
|
93
|
+
test('persists and resolves base URL and model', () => {
|
|
94
|
+
setCustomModel({ baseUrl: 'https://gw.example.com/v1', model: 'my-model' });
|
|
95
|
+
|
|
96
|
+
const resolved = resolveModel('custom');
|
|
97
|
+
expect(resolved.model).toBe('my-model');
|
|
98
|
+
expect(resolved.baseUrl).toBe('https://gw.example.com/v1');
|
|
99
|
+
expect(resolved.provider).toBe('openai');
|
|
100
|
+
|
|
101
|
+
const overridden = resolveModel('custom:other-model');
|
|
102
|
+
expect(overridden.model).toBe('other-model');
|
|
103
|
+
expect(overridden.baseUrl).toBe('https://gw.example.com/v1');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('appears in the model list', () => {
|
|
107
|
+
expect(getAvailableModels().some((model) => model.id === 'custom')).toBe(true);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('verifyModel', () => {
|
|
112
|
+
test('fails fast with a readable message when no key is available', async () => {
|
|
113
|
+
const result = await verifyModel('deepseek:deepseek-chat', '');
|
|
114
|
+
expect(result.ok).toBe(false);
|
|
115
|
+
expect(result.message).toMatch(/key/i);
|
|
116
|
+
});
|
|
117
|
+
});
|