creditdaddy 1.7.0 → 1.7.2

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.
@@ -4,12 +4,19 @@
4
4
  * 数据面端点:POST /gateway/trae/v1/messages
5
5
  * (上游为 ByteDance Trae SOLO 远程 Agent 协议:https://solo.trae.cn/api/remote/v1)
6
6
  *
7
+ * 双通道(同步 10Router 44b2bb27):普通模型走**直连非沙箱通道**
8
+ * (agent_type: "chat" + env: "local",不开云端沙箱容器,纯文本对话更快更省);
9
+ * 模型名显式含 solo/work/agent- 的走 SOLO 沙箱通道(solo_agent_remote + remote)。
10
+ * 服务端 chat_v3 的 ConfigName 严格区分大小写,模型名先过 canonicalTraeModel
11
+ * 规范化(传错报 4001 config empty)。
12
+ *
7
13
  * 架构特性:
8
14
  * 1. 账号多路轮换(provider=trae):自动在有效账号间按剩余积分加权轮询(SWRR)
9
15
  * 2. 凭据热对齐:401 时若为本机导入账号,尝试重读 storage.json 最新登录态
10
16
  * 3. 429 限流保护:429 限流自动冷却(退避阶梯 30s → 1min → 2min → 5min → 10min 封顶,成功一次即清零)
11
17
  * 4. 局域网访问控制:继承相同的白名单与回环检查机制
12
- * 5. SSE 增量转换:将 Trae SOLO 累积流转换为标准 Anthropic 流式 SSE 或一次性 JSON 响应
18
+ * 5. SSE 增量转换:将 Trae 累积流(沙箱 plan_item.thought / 直连 content/delta/text)
19
+ * 转换为标准 Anthropic 流式 SSE 或一次性 JSON 响应
13
20
  */
14
21
 
15
22
  import { fetchJsonRace } from './zcodeClient.js';
@@ -29,6 +36,93 @@ const UPSTREAM_TIMEOUT_MS = 600_000;
29
36
  const UPSTREAM_CONNECT_MS = 15_000;
30
37
  const DEFAULT_MODEL = 'Doubao-Seed-Code';
31
38
 
39
+ // 模型名规范化映射(同步 10Router CANONICAL_TRAE_MODELS,#99 真机口径:
40
+ // 服务端严格区分大小写,传错 4001 config empty);表外原样透传。
41
+ const CANONICAL_TRAE_MODELS = {
42
+ 'doubao-seed-code': 'Doubao-Seed-Code',
43
+ 'doubao-seed-2.1-turbo': 'Doubao-Seed-2.1-Turbo',
44
+ 'doubao-seed-2.1-pro': 'Doubao-Seed-2.1-Pro',
45
+ 'doubao-seed-2.0-code': 'Doubao-Seed-2.0-Code',
46
+ 'doubao-seed-evolving': 'Doubao-Seed-Evolving',
47
+ 'deepseek-v4.1-flash': 'DeepSeek-V4-Flash',
48
+ 'deepseek-v4-flash': 'DeepSeek-V4-Flash',
49
+ 'deepseek-v4-pro': 'DeepSeek-V4-Pro',
50
+ 'deepseek-v4-pro-official': 'DeepSeek-V4-Pro-Official',
51
+ 'glm-5.3': 'GLM-5.3',
52
+ 'glm-5.3-flash': 'GLM-5.3-Flash',
53
+ 'glm-5.3-flashx': 'GLM-5.3-FlashX',
54
+ 'kimi-k3.1-pro': 'Kimi-K3.1-Pro',
55
+ 'kimi-k3': 'Kimi-K3',
56
+ 'minimax-m3.1-flash': 'MiniMax-M3.1-Flash',
57
+ 'minimax-m3': 'MiniMax-M3',
58
+ 'step-5-preview': 'Step-5-Preview',
59
+ 'qwen3.8-flash': 'qwen3.8-flash',
60
+ 'qwen3.8-max': 'qwen3.8-max',
61
+ 'mimo-v2.6-flash': 'mimo-v2.6-flash',
62
+ 'mimo-v2.6-pro': 'mimo-v2.6-pro',
63
+ };
64
+
65
+ export function canonicalTraeModel(model) {
66
+ if (!model) return '';
67
+ return CANONICAL_TRAE_MODELS[String(model).trim().toLowerCase()] || model;
68
+ }
69
+
70
+ // 显式要求 SOLO 沙箱通道的模型名(含 solo/work/agent- 字样);其余走直连非沙箱
71
+ export function isSoloAgentMode(model = '') {
72
+ const m = String(model).trim().toLowerCase();
73
+ return m.includes('solo') || m.includes('work') || m.startsWith('agent-');
74
+ }
75
+
76
+ // mode/strategy 判定(同步 10Router resolveMode):work 族 = mode work + auto + 空模型名;
77
+ // auto/空 = mode code + auto;其余 = mode code + manual + 规范化模型名
78
+ export function resolveTraeMode(model = '') {
79
+ const m = String(model || '').trim().toLowerCase();
80
+ if (m === 'work' || m === 'auto-work' || m === 'solo-work') {
81
+ return { mode: 'work', strategy: 'auto', modelName: '' };
82
+ }
83
+ const auto = !m || m === 'auto';
84
+ return { mode: 'code', strategy: auto ? 'auto' : 'manual', modelName: auto ? '' : canonicalTraeModel(model) };
85
+ }
86
+
87
+ /** 会话载荷构造(按通道分流):直连 = agent_type chat + env local;沙箱 = solo_agent_remote + remote */
88
+ export function buildTraeSessionPayload(promptText, requestedModel) {
89
+ const { mode, strategy, modelName } = resolveTraeMode(requestedModel);
90
+ const solo = isSoloAgentMode(requestedModel);
91
+ return {
92
+ mode,
93
+ environment_id: 'default',
94
+ initial_message: {
95
+ chat_session_id: '',
96
+ content: [],
97
+ query: JSON.stringify([{ type: 'text', data: { content: promptText } }]),
98
+ model_name: modelName,
99
+ agent_type: solo ? 'solo_agent_remote' : 'chat',
100
+ model_selection_strategy: strategy,
101
+ common_params: JSON.stringify({
102
+ language: 'zh-cn',
103
+ app_language: 'zh-cn',
104
+ quality: 'stable',
105
+ app_version: '1.0.0.1229',
106
+ web_id: '',
107
+ user_identity: 'Free',
108
+ is_freshman: '0',
109
+ biz_user_id: '',
110
+ user_unique_id: '',
111
+ scope: 'marscode-cn',
112
+ tenant: 'marscode',
113
+ region: 'CN',
114
+ aiRegion: 'CN',
115
+ is_privacy_mode: 0,
116
+ privacy_mode: 'off',
117
+ solo_chat_mode: mode,
118
+ }),
119
+ },
120
+ env: solo ? 'remote' : 'local',
121
+ auto_create_project: false,
122
+ origin: 'web',
123
+ };
124
+ }
125
+
32
126
  // ── 开关(持久化在 settings.traeGateway) ──
33
127
 
34
128
  export async function gatewayEnabled() {
@@ -81,6 +175,7 @@ export const stats = {
81
175
  lastCallAt: null,
82
176
  calls: 0,
83
177
  lastAccount: null,
178
+ lastAccountAt: null, // 「接口使用中」徽章的 30 分钟窗口锚点(面板 gwRecentWindow 内才亮)
84
179
  };
85
180
 
86
181
  const MIN_WEIGHT = 1;
@@ -135,6 +230,7 @@ export function __resetForTests() {
135
230
  stats.lastCallAt = null;
136
231
  stats.calls = 0;
137
232
  stats.lastAccount = null;
233
+ stats.lastAccountAt = null;
138
234
  }
139
235
 
140
236
  async function accountWeight(a) {
@@ -386,39 +482,7 @@ export async function handleGateway(req, res) {
386
482
  const label = account.name || account.uid || account.id;
387
483
 
388
484
  let token = account.token;
389
- const sessionPayload = {
390
- mode: 'code',
391
- environment_id: 'default',
392
- initial_message: {
393
- chat_session_id: '',
394
- content: [],
395
- query: JSON.stringify([{ type: 'text', data: { content: promptText } }]),
396
- model_name: requestedModel,
397
- agent_type: 'solo_agent_remote',
398
- model_selection_strategy: 'manual',
399
- common_params: JSON.stringify({
400
- language: 'zh-cn',
401
- app_language: 'zh-cn',
402
- quality: 'stable',
403
- app_version: '1.0.0.1229',
404
- web_id: '',
405
- user_identity: 'Free',
406
- is_freshman: '0',
407
- biz_user_id: '',
408
- user_unique_id: '',
409
- scope: 'marscode-cn',
410
- tenant: 'marscode',
411
- region: 'CN',
412
- aiRegion: 'CN',
413
- is_privacy_mode: 0,
414
- privacy_mode: 'off',
415
- solo_chat_mode: 'code',
416
- }),
417
- },
418
- env: 'remote',
419
- auto_create_project: false,
420
- origin: 'web',
421
- };
485
+ const sessionPayload = buildTraeSessionPayload(promptText, requestedModel);
422
486
 
423
487
  const makeHeaders = (tok) => ({
424
488
  Authorization: tok.startsWith('Cloud-IDE-JWT ') ? tok : `Cloud-IDE-JWT ${tok.trim()}`,
@@ -549,6 +613,7 @@ export async function handleGateway(req, res) {
549
613
  }
550
614
 
551
615
  stats.lastAccount = label;
616
+ stats.lastAccountAt = Date.now();
552
617
  resetCoolingStreak(account.id); // 成功一次即清零连续 429 计数
553
618
  logger.debug('TRAE-GW', '{label} 连接成功,开始流式输出 ({model})', { label, model: requestedModel });
554
619
 
@@ -630,8 +695,9 @@ export async function handleGateway(req, res) {
630
695
  });
631
696
  }
632
697
 
633
- // 2. Text delta (thought or summary)
634
- const candidateText = data.thought || data.tool_call_info?.params?.summary || '';
698
+ // 2. Text delta:兼顾沙箱通道的 plan_item.thought 与直连 chat 通道的
699
+ // content / delta.content / text(同步 10Router renderNewText 字段链)
700
+ const candidateText = data.thought || data.content || data.delta?.content || data.text || data.tool_call_info?.params?.summary || '';
635
701
  if (candidateText && candidateText.length > emittedText.length) {
636
702
  const delta = candidateText.slice(emittedText.length);
637
703
  emittedText = candidateText;
@@ -721,7 +787,7 @@ export async function handleGateway(req, res) {
721
787
  try {
722
788
  const data = JSON.parse(raw);
723
789
  if (data.reasoning_content) fullThinking = data.reasoning_content;
724
- const t = data.thought || data.tool_call_info?.params?.summary;
790
+ const t = data.thought || data.content || data.delta?.content || data.text || data.tool_call_info?.params?.summary;
725
791
  if (t) fullText = t;
726
792
  if (data.prompt_tokens !== undefined || data.last_turn_total_tokens !== undefined) {
727
793
  usageInfo = data;
@@ -788,6 +854,7 @@ export async function gatewayStatus() {
788
854
  lastCallAt: stats.lastCallAt,
789
855
  calls: stats.calls,
790
856
  lastAccount: stats.lastAccount,
857
+ lastAccountAt: stats.lastAccountAt,
791
858
  },
792
859
  accounts: all.length,
793
860
  // cooling 保持字符串数组(历史形状);冷却细节走 coolingDetail(与 zcodeGateway 同款)