@trans-ai/trans-code 0.0.0-stage → 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 +209 -2
- package/bin/transcode.ts +4 -0
- package/docs/ACP.md +231 -0
- package/package.json +42 -4
- package/src/acp/protocol.ts +115 -0
- package/src/acp/server.ts +202 -0
- package/src/acp/session.ts +269 -0
- package/src/acp/transport.ts +216 -0
- package/src/agent/loop.ts +334 -0
- package/src/agent/system-prompt.ts +86 -0
- package/src/agent/tools.ts +483 -0
- package/src/cli.ts +60 -0
- package/src/commands.ts +61 -0
- package/src/main.tsx +62 -0
- package/src/provider/provider.ts +373 -0
- package/src/screens/REPL.tsx +433 -0
- package/src/tui/cli.tsx +2 -0
- package/src/tui/keyed-stdin.ts +83 -0
- package/src/upgrade.ts +110 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { ChatOpenAI } from '@langchain/openai';
|
|
2
|
+
import { ChatAnthropic } from '@langchain/anthropic';
|
|
3
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
4
|
+
|
|
5
|
+
export type ModelId = 'opus' | 'gpt' | 'deepseek' | 'glm' | 'minimax' | 'kimi';
|
|
6
|
+
|
|
7
|
+
export type ProviderKind = 'openai' | 'anthropic';
|
|
8
|
+
|
|
9
|
+
export interface ModelConfig {
|
|
10
|
+
name: string;
|
|
11
|
+
provider: ProviderKind;
|
|
12
|
+
model: string;
|
|
13
|
+
baseUrl?: string;
|
|
14
|
+
envKey: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const MODEL_REGISTRY: Record<ModelId, ModelConfig> = {
|
|
18
|
+
opus: {
|
|
19
|
+
name: 'Claude Opus',
|
|
20
|
+
provider: 'anthropic',
|
|
21
|
+
model: 'claude-opus-4-20250514',
|
|
22
|
+
envKey: 'ANTHROPIC_API_KEY',
|
|
23
|
+
},
|
|
24
|
+
gpt: {
|
|
25
|
+
name: 'GPT-4o',
|
|
26
|
+
provider: 'openai',
|
|
27
|
+
model: 'gpt-4o',
|
|
28
|
+
envKey: 'OPENAI_API_KEY',
|
|
29
|
+
},
|
|
30
|
+
deepseek: {
|
|
31
|
+
name: 'DeepSeek',
|
|
32
|
+
provider: 'openai',
|
|
33
|
+
model: 'deepseek-chat',
|
|
34
|
+
baseUrl: 'https://api.deepseek.com/v1',
|
|
35
|
+
envKey: 'DEEPSEEK_API_KEY',
|
|
36
|
+
},
|
|
37
|
+
glm: {
|
|
38
|
+
name: 'GLM-4',
|
|
39
|
+
provider: 'openai',
|
|
40
|
+
model: 'glm-4',
|
|
41
|
+
baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
|
|
42
|
+
envKey: 'GLM_API_KEY',
|
|
43
|
+
},
|
|
44
|
+
minimax: {
|
|
45
|
+
name: 'MiniMax',
|
|
46
|
+
provider: 'openai',
|
|
47
|
+
model: 'MiniMax-Text-01',
|
|
48
|
+
baseUrl: 'https://api.minimax.chat/v1',
|
|
49
|
+
envKey: 'MINIMAX_API_KEY',
|
|
50
|
+
},
|
|
51
|
+
kimi: {
|
|
52
|
+
name: 'Kimi',
|
|
53
|
+
provider: 'openai',
|
|
54
|
+
model: 'moonshot-v1-128k',
|
|
55
|
+
baseUrl: 'https://api.moonshot.cn/v1',
|
|
56
|
+
envKey: 'KIMI_API_KEY',
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const PROVIDER_DEFAULTS: Record<
|
|
61
|
+
string,
|
|
62
|
+
{ provider: ProviderKind; baseUrl?: string; envKey: string }
|
|
63
|
+
> = {
|
|
64
|
+
openai: { provider: 'openai', envKey: 'OPENAI_API_KEY' },
|
|
65
|
+
anthropic: { provider: 'anthropic', envKey: 'ANTHROPIC_API_KEY' },
|
|
66
|
+
deepseek: {
|
|
67
|
+
provider: 'openai',
|
|
68
|
+
baseUrl: 'https://api.deepseek.com/v1',
|
|
69
|
+
envKey: 'DEEPSEEK_API_KEY',
|
|
70
|
+
},
|
|
71
|
+
glm: {
|
|
72
|
+
provider: 'openai',
|
|
73
|
+
baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
|
|
74
|
+
envKey: 'GLM_API_KEY',
|
|
75
|
+
},
|
|
76
|
+
minimax: {
|
|
77
|
+
provider: 'openai',
|
|
78
|
+
baseUrl: 'https://api.minimax.chat/v1',
|
|
79
|
+
envKey: 'MINIMAX_API_KEY',
|
|
80
|
+
},
|
|
81
|
+
kimi: {
|
|
82
|
+
provider: 'openai',
|
|
83
|
+
baseUrl: 'https://api.moonshot.cn/v1',
|
|
84
|
+
envKey: 'KIMI_API_KEY',
|
|
85
|
+
},
|
|
86
|
+
moonshot: {
|
|
87
|
+
provider: 'openai',
|
|
88
|
+
baseUrl: 'https://api.moonshot.cn/v1',
|
|
89
|
+
envKey: 'KIMI_API_KEY',
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export interface TransProvider {
|
|
94
|
+
modelId: string;
|
|
95
|
+
model: BaseChatModel;
|
|
96
|
+
config: ModelConfig;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface ResolvedModel {
|
|
100
|
+
/** Stable id shown in the UI / accepted by `session/set_model`. */
|
|
101
|
+
id: string;
|
|
102
|
+
name: string;
|
|
103
|
+
provider: ProviderKind;
|
|
104
|
+
model: string;
|
|
105
|
+
baseUrl?: string;
|
|
106
|
+
envKey: string;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function getAvailableModels(): { id: ModelId; name: string }[] {
|
|
110
|
+
return Object.entries(MODEL_REGISTRY).map(([id, cfg]) => ({
|
|
111
|
+
id: id as ModelId,
|
|
112
|
+
name: cfg.name,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function getModelConfig(id: ModelId): ModelConfig {
|
|
117
|
+
return MODEL_REGISTRY[id];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function isKnownModelId(value: string): value is ModelId {
|
|
121
|
+
return Object.prototype.hasOwnProperty.call(MODEL_REGISTRY, value);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function defaultProvider(): string {
|
|
125
|
+
return (process.env.TRANSCODE_DEFAULT_PROVIDER ?? 'openai').toLowerCase();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function anthropicAuthToken(): string {
|
|
129
|
+
return (process.env.ANTHROPIC_AUTH_TOKEN ?? '').trim();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function anthropicGatewayUrl(): string | undefined {
|
|
133
|
+
return (process.env.ANTHROPIC_BASE_URL ?? '').trim() || undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Model configured by Claude Code / fable-style Anthropic-compatible setups. */
|
|
137
|
+
function fableModelName(): string {
|
|
138
|
+
const value =
|
|
139
|
+
process.env.ANTHROPIC_DEFAULT_FABLE_MODEL_NAME ??
|
|
140
|
+
process.env.ANTHROPIC_DEFAULT_FABLE_MODEL ??
|
|
141
|
+
process.env.ANTHROPIC_MODEL ??
|
|
142
|
+
'';
|
|
143
|
+
// Some Fable setups append a context-size marker such as "[1M]".
|
|
144
|
+
return value.replace(/\[[^\]]+\]$/, '').trim();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function defaultModelSpec(): string {
|
|
148
|
+
const explicit = process.env.TRANSCODE_MODEL?.trim();
|
|
149
|
+
if (explicit) return explicit;
|
|
150
|
+
|
|
151
|
+
if (anthropicAuthToken() || anthropicGatewayUrl()) {
|
|
152
|
+
const fable = fableModelName();
|
|
153
|
+
if (fable) return `anthropic:${fable}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const anthropicModel = process.env.ANTHROPIC_MODEL?.trim();
|
|
157
|
+
if (anthropicModel) return `anthropic:${anthropicModel}`;
|
|
158
|
+
|
|
159
|
+
return 'opus';
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function apiKeyFor(envKey: string): string {
|
|
163
|
+
const direct = process.env[envKey]?.trim();
|
|
164
|
+
if (direct) return direct;
|
|
165
|
+
|
|
166
|
+
if (envKey === 'ANTHROPIC_API_KEY') {
|
|
167
|
+
const token = anthropicAuthToken();
|
|
168
|
+
if (token) return token;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return (process.env.TRANSCODE_API_KEY ?? '').trim();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function maxTokens(): number {
|
|
175
|
+
const raw = Number(process.env.TRANSCODE_MAX_TOKENS ?? '');
|
|
176
|
+
if (Number.isFinite(raw) && raw > 0) return Math.floor(raw);
|
|
177
|
+
return 8192;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Resolve a model spec into a concrete model configuration.
|
|
182
|
+
*
|
|
183
|
+
* Accepted forms:
|
|
184
|
+
* - a registry id (opus, gpt, deepseek, glm, minimax, kimi)
|
|
185
|
+
* - provider:model (openai:gpt-4o, anthropic:claude-sonnet-4, deepseek:deepseek-chat, ...)
|
|
186
|
+
* - a bare model name, routed by its name / TRANSCODE_DEFAULT_PROVIDER
|
|
187
|
+
* - an empty string, falling back to TRANSCODE_MODEL, the Fable/Anthropic
|
|
188
|
+
* gateway settings, then "opus"
|
|
189
|
+
*/
|
|
190
|
+
export function resolveModel(spec?: string): ResolvedModel {
|
|
191
|
+
const raw = (spec?.trim() || defaultModelSpec()).trim();
|
|
192
|
+
|
|
193
|
+
if (!raw) return resolveModel('opus');
|
|
194
|
+
|
|
195
|
+
if (raw.toLowerCase() === 'fable') {
|
|
196
|
+
const fable = fableModelName();
|
|
197
|
+
if (fable) {
|
|
198
|
+
return {
|
|
199
|
+
id: 'fable',
|
|
200
|
+
name: fable,
|
|
201
|
+
provider: 'anthropic',
|
|
202
|
+
model: fable,
|
|
203
|
+
baseUrl: anthropicGatewayUrl(),
|
|
204
|
+
envKey: 'ANTHROPIC_API_KEY',
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (isKnownModelId(raw)) {
|
|
210
|
+
const cfg = MODEL_REGISTRY[raw];
|
|
211
|
+
return {
|
|
212
|
+
id: raw,
|
|
213
|
+
name: cfg.name,
|
|
214
|
+
provider: cfg.provider,
|
|
215
|
+
model: cfg.model,
|
|
216
|
+
baseUrl: cfg.baseUrl,
|
|
217
|
+
envKey: cfg.envKey,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const colon = raw.indexOf(':');
|
|
222
|
+
if (colon > 0) {
|
|
223
|
+
const prefix = raw.slice(0, colon).toLowerCase();
|
|
224
|
+
const model = raw.slice(colon + 1);
|
|
225
|
+
const defaults = PROVIDER_DEFAULTS[prefix];
|
|
226
|
+
if (defaults && model) {
|
|
227
|
+
return {
|
|
228
|
+
id: raw,
|
|
229
|
+
name: `${prefix}/${model}`,
|
|
230
|
+
provider: defaults.provider,
|
|
231
|
+
model,
|
|
232
|
+
baseUrl:
|
|
233
|
+
defaults.provider === 'anthropic' ? anthropicGatewayUrl() : defaults.baseUrl,
|
|
234
|
+
envKey: defaults.envKey,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Bare unknown model name: route through a provider guessed from the name,
|
|
240
|
+
// then fall back to TRANSCODE_DEFAULT_PROVIDER. This lets multica users type
|
|
241
|
+
// a model id without remembering the provider:model syntax.
|
|
242
|
+
const lower = raw.toLowerCase();
|
|
243
|
+
let fallback = PROVIDER_DEFAULTS[defaultProvider()] ?? PROVIDER_DEFAULTS.openai;
|
|
244
|
+
if (lower.startsWith('claude')) fallback = PROVIDER_DEFAULTS.anthropic;
|
|
245
|
+
else if (lower.startsWith('deepseek')) fallback = PROVIDER_DEFAULTS.deepseek;
|
|
246
|
+
else if (lower.startsWith('glm')) fallback = PROVIDER_DEFAULTS.glm;
|
|
247
|
+
else if (lower.startsWith('moonshot') || lower.startsWith('kimi')) {
|
|
248
|
+
fallback = PROVIDER_DEFAULTS.kimi;
|
|
249
|
+
} else if (lower.startsWith('minimax')) fallback = PROVIDER_DEFAULTS.minimax;
|
|
250
|
+
|
|
251
|
+
// An Anthropic-compatible gateway can host arbitrary model names. Prefer it
|
|
252
|
+
// when the guessed provider has no credentials of its own.
|
|
253
|
+
if (fallback.provider !== 'anthropic' && anthropicGatewayUrl()) {
|
|
254
|
+
const guessedKey = process.env[fallback.envKey]?.trim();
|
|
255
|
+
if (!guessedKey) {
|
|
256
|
+
return {
|
|
257
|
+
id: raw,
|
|
258
|
+
name: raw,
|
|
259
|
+
provider: 'anthropic',
|
|
260
|
+
model: raw,
|
|
261
|
+
baseUrl: anthropicGatewayUrl(),
|
|
262
|
+
envKey: 'ANTHROPIC_API_KEY',
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
id: raw,
|
|
269
|
+
name: raw,
|
|
270
|
+
provider: fallback.provider,
|
|
271
|
+
model: raw,
|
|
272
|
+
baseUrl: fallback.baseUrl,
|
|
273
|
+
envKey: fallback.envKey,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function baseUrlFor(resolved: ResolvedModel): string | undefined {
|
|
278
|
+
if (process.env.TRANSCODE_BASE_URL) return process.env.TRANSCODE_BASE_URL;
|
|
279
|
+
if (resolved.provider === 'anthropic') {
|
|
280
|
+
return resolved.baseUrl ?? anthropicGatewayUrl();
|
|
281
|
+
}
|
|
282
|
+
return (
|
|
283
|
+
resolved.baseUrl ??
|
|
284
|
+
process.env.OPENAI_BASE_URL ??
|
|
285
|
+
process.env.OPENAI_API_BASE
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Display name for the model selected by the environment, if it is not a registry id. */
|
|
290
|
+
export function envModelLabel(): string | undefined {
|
|
291
|
+
const resolved = resolveModel();
|
|
292
|
+
return isKnownModelId(resolved.id) ? undefined : resolved.model;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function createModel(spec?: string): TransProvider {
|
|
296
|
+
const resolved = resolveModel(spec);
|
|
297
|
+
const apiKey = apiKeyFor(resolved.envKey);
|
|
298
|
+
|
|
299
|
+
if (!apiKey) {
|
|
300
|
+
throw new Error(
|
|
301
|
+
`Missing API key for ${resolved.name} (${resolved.id}). ` +
|
|
302
|
+
`Set ${resolved.envKey} in the environment.`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
let model: BaseChatModel;
|
|
307
|
+
const baseUrl = baseUrlFor(resolved);
|
|
308
|
+
|
|
309
|
+
if (resolved.provider === 'anthropic') {
|
|
310
|
+
// Only use bearer auth when there is no explicit ANTHROPIC_API_KEY, so a
|
|
311
|
+
// real Anthropic endpoint never receives both x-api-key and Authorization.
|
|
312
|
+
const authToken = process.env.ANTHROPIC_API_KEY?.trim() ? '' : anthropicAuthToken();
|
|
313
|
+
model = new ChatAnthropic({
|
|
314
|
+
modelName: resolved.model,
|
|
315
|
+
anthropicApiKey: apiKey,
|
|
316
|
+
...(baseUrl ? { anthropicApiUrl: baseUrl } : {}),
|
|
317
|
+
...(authToken ? { clientOptions: { authToken } } : {}),
|
|
318
|
+
temperature: 0,
|
|
319
|
+
maxTokens: maxTokens(),
|
|
320
|
+
// @langchain/anthropic defaults top_k/top_p to -1 and forwards them on
|
|
321
|
+
// every request. Strict Anthropic-compatible gateways reject that
|
|
322
|
+
// ("top_p is in (0, 1.0]"), so drop both keys unless explicitly set.
|
|
323
|
+
invocationKwargs: { top_k: undefined, top_p: undefined },
|
|
324
|
+
maxRetries: 1,
|
|
325
|
+
});
|
|
326
|
+
} else {
|
|
327
|
+
model = new ChatOpenAI({
|
|
328
|
+
modelName: resolved.model,
|
|
329
|
+
openAIApiKey: apiKey,
|
|
330
|
+
temperature: 0,
|
|
331
|
+
maxTokens: maxTokens(),
|
|
332
|
+
maxRetries: 1,
|
|
333
|
+
configuration: baseUrl ? { baseURL: baseUrl } : undefined,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return {
|
|
338
|
+
modelId: resolved.id,
|
|
339
|
+
model,
|
|
340
|
+
config: {
|
|
341
|
+
name: resolved.name,
|
|
342
|
+
provider: resolved.provider,
|
|
343
|
+
model: resolved.model,
|
|
344
|
+
baseUrl,
|
|
345
|
+
envKey: resolved.envKey,
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** @deprecated Use createModel instead. Kept for the existing REPL. */
|
|
351
|
+
export function createProviderWithKey(id: ModelId, apiKey: string): TransProvider {
|
|
352
|
+
const config = MODEL_REGISTRY[id];
|
|
353
|
+
if (!config) throw new Error(`Unknown model: ${id}`);
|
|
354
|
+
|
|
355
|
+
const model =
|
|
356
|
+
config.provider === 'anthropic'
|
|
357
|
+
? new ChatAnthropic({
|
|
358
|
+
modelName: config.model,
|
|
359
|
+
anthropicApiKey: apiKey,
|
|
360
|
+
})
|
|
361
|
+
: new ChatOpenAI({
|
|
362
|
+
modelName: config.model,
|
|
363
|
+
openAIApiKey: apiKey,
|
|
364
|
+
configuration: config.baseUrl ? { baseURL: config.baseUrl } : undefined,
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
return { modelId: id, model, config };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** @deprecated Use createModel instead. Kept for the existing REPL. */
|
|
371
|
+
export function createProvider(id: ModelId): TransProvider {
|
|
372
|
+
return createModel(id);
|
|
373
|
+
}
|