minimal-agent 0.18.13 → 0.18.15

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.
Files changed (181) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -3
  3. package/plugins/workflow-runner/src/runtime/context.js +1 -1
  4. package/plugins/workflow-runner/src/runtime/context.ts +1 -1
  5. package/plugins/workflow-runner/test/embeddedExecution.test.ts +2 -5
  6. package/plugins/workflow-runner/test/execLifetime.test.ts +15 -2
  7. package/skills/ssh-remote-dev/SKILL.md +61 -0
  8. package/skills/ssh-remote-dev/agents/openai.yaml +4 -0
  9. package/src/advisor/README.md +62 -24
  10. package/src/advisor/advisorNudge.js +199 -0
  11. package/src/advisor/advisorNudge.ts +232 -0
  12. package/src/advisor/deliveryReview.js +35 -12
  13. package/src/advisor/deliveryReview.ts +35 -11
  14. package/src/advisor/presentation.js +7 -10
  15. package/src/advisor/presentation.ts +10 -13
  16. package/src/advisor/reviewContext.js +22 -58
  17. package/src/advisor/reviewContext.ts +24 -67
  18. package/src/cli/configMenu.js +17 -128
  19. package/src/cli/configMenu.tsx +17 -193
  20. package/src/cli/print.js +2 -0
  21. package/src/cli/print.ts +2 -0
  22. package/src/cli/streamJson.js +2 -0
  23. package/src/cli/streamJson.ts +2 -0
  24. package/src/cli/wizards/advisorWizard.js +9 -7
  25. package/src/cli/wizards/advisorWizard.tsx +9 -7
  26. package/src/cli/wizards/contextAdvisorWizard.js +9 -7
  27. package/src/cli/wizards/contextAdvisorWizard.tsx +9 -7
  28. package/src/cli/wizards/providerPresets.js +55 -18
  29. package/src/cli/wizards/providerPresets.ts +72 -19
  30. package/src/cli/wizards/providerWizard.js +51 -10
  31. package/src/cli/wizards/providerWizard.tsx +59 -9
  32. package/src/config/configDefaults.js +1 -1
  33. package/src/config/configDefaults.ts +1 -1
  34. package/src/config/configDiagnostics.js +1 -21
  35. package/src/config/configDiagnostics.ts +3 -23
  36. package/src/config/configFile.js +11 -4
  37. package/src/config/configFile.ts +27 -7
  38. package/src/config/configManagement.js +1 -0
  39. package/src/config/configManagement.ts +2 -0
  40. package/src/config/configService.js +12 -8
  41. package/src/config/configService.ts +16 -11
  42. package/src/config/numericLimits.js +0 -4
  43. package/src/config/numericLimits.ts +0 -4
  44. package/src/config/reasoningEffort.js +16 -0
  45. package/src/config/reasoningEffort.ts +18 -0
  46. package/src/context/budgetPlan.js +1 -1
  47. package/src/context/budgetPlan.ts +1 -1
  48. package/src/context/optimizeConversation.js +1 -1
  49. package/src/context/optimizeConversation.ts +1 -1
  50. package/src/harness/stallGuard.js +8 -13
  51. package/src/harness/stallGuard.ts +8 -12
  52. package/src/llm/client.js +64 -17
  53. package/src/llm/client.ts +62 -7
  54. package/src/llm/providers/anthropicFormat.js +3 -1
  55. package/src/llm/providers/anthropicFormat.ts +4 -1
  56. package/src/llm/providers/openaiFormat.js +14 -3
  57. package/src/llm/providers/openaiFormat.ts +17 -3
  58. package/src/llm/providers/responsesFormat.js +277 -0
  59. package/src/llm/providers/responsesFormat.ts +308 -0
  60. package/src/loop/README.md +18 -3
  61. package/src/loop/liveDiagramGate.js +63 -0
  62. package/src/loop/liveDiagramGate.ts +61 -0
  63. package/src/loop/messages.js +12 -0
  64. package/src/loop/messages.ts +13 -0
  65. package/src/loop/options.ts +7 -2
  66. package/src/loop/toolBatch.js +0 -1
  67. package/src/loop/toolBatch.ts +0 -1
  68. package/src/loop/toolExecutor.js +11 -46
  69. package/src/loop/toolExecutor.ts +12 -55
  70. package/src/loop/toolPresentation.js +2 -13
  71. package/src/loop/toolPresentation.ts +2 -13
  72. package/src/loop/toolSemantics.js +3 -4
  73. package/src/loop/toolSemantics.ts +4 -6
  74. package/src/loop.js +222 -214
  75. package/src/loop.ts +226 -232
  76. package/src/memory/dependencies.js +11 -3
  77. package/src/memory/dependencies.ts +11 -3
  78. package/src/memory/episodicMemory.js +59 -18
  79. package/src/memory/episodicMemory.ts +58 -18
  80. package/src/memory/experienceStore.js +4 -3
  81. package/src/memory/experienceStore.ts +4 -3
  82. package/src/memory/factCandidates.js +53 -1
  83. package/src/memory/factCandidates.ts +54 -1
  84. package/src/memory/factLearning.js +2 -2
  85. package/src/memory/factLearning.ts +2 -2
  86. package/src/memory/jobPolicy.js +24 -5
  87. package/src/memory/jobPolicy.ts +25 -5
  88. package/src/memory/learning.js +2 -2
  89. package/src/memory/learning.ts +2 -2
  90. package/src/memory/learningModel.js +7 -0
  91. package/src/memory/learningModel.ts +7 -0
  92. package/src/memory/managementService.js +8 -7
  93. package/src/memory/managementService.ts +16 -8
  94. package/src/memory/memoryService.js +104 -33
  95. package/src/memory/memoryService.ts +132 -28
  96. package/src/memory/projectKnowledge.js +39 -3
  97. package/src/memory/projectKnowledge.ts +45 -3
  98. package/src/memory/recallQuery.js +56 -0
  99. package/src/memory/recallQuery.ts +58 -0
  100. package/src/memory/retrieval.js +39 -10
  101. package/src/memory/retrieval.ts +46 -11
  102. package/src/memory/turnJournal.js +75 -10
  103. package/src/memory/turnJournal.ts +66 -10
  104. package/src/memory/types.ts +7 -1
  105. package/src/prompts/delegation.js +2 -1
  106. package/src/prompts/delegation.ts +3 -1
  107. package/src/prompts/policyRegistry.js +9 -12
  108. package/src/prompts/policyRegistry.ts +9 -12
  109. package/src/prompts/promptAssembly.js +6 -5
  110. package/src/prompts/promptAssembly.ts +8 -6
  111. package/src/prompts/visualPresentation.js +39 -10
  112. package/src/prompts/visualPresentation.ts +38 -9
  113. package/src/runtime/answerPreparation.js +1 -1
  114. package/src/runtime/answerPreparation.ts +1 -1
  115. package/src/runtime/commandCatalog.js +1 -0
  116. package/src/runtime/commandCatalog.ts +9 -0
  117. package/src/runtime/fileLock.js +19 -3
  118. package/src/runtime/fileLock.ts +19 -3
  119. package/src/runtime/invocationPolicy.js +1 -1
  120. package/src/runtime/invocationPolicy.ts +1 -1
  121. package/src/runtime/outputContract.js +0 -4
  122. package/src/runtime/outputContract.ts +0 -7
  123. package/src/session/runAgentTurn.js +3 -0
  124. package/src/session/runAgentTurn.ts +3 -0
  125. package/src/team/onlineToolContract.js +0 -1
  126. package/src/team/onlineToolContract.ts +0 -1
  127. package/src/tools/advisor/advisor.js +207 -365
  128. package/src/tools/advisor/advisor.ts +211 -383
  129. package/src/tools/agent/agentTypes.js +0 -1
  130. package/src/tools/agent/agentTypes.ts +0 -1
  131. package/src/tools/agent/coordinator.js +4 -1
  132. package/src/tools/agent/coordinator.ts +4 -1
  133. package/src/tools/bash/semantics.js +4 -5
  134. package/src/tools/bash/semantics.ts +4 -5
  135. package/src/tools/capabilities.js +2 -3
  136. package/src/tools/capabilities.ts +2 -4
  137. package/src/tools/index.js +0 -16
  138. package/src/tools/index.ts +0 -16
  139. package/src/tools/memory/maintenance.js +8 -9
  140. package/src/tools/memory/maintenance.ts +13 -11
  141. package/src/tools/memory/memory.js +5 -1
  142. package/src/tools/memory/memory.ts +5 -0
  143. package/src/tools/optimizeConversation/README.md +4 -6
  144. package/src/tools/toolPromptGuides.js +2 -9
  145. package/src/tools/toolPromptGuides.ts +2 -9
  146. package/src/types.ts +28 -105
  147. package/src/ui/App.js +22 -1
  148. package/src/ui/App.tsx +26 -1
  149. package/src/ui/MessageList.js +8 -44
  150. package/src/ui/MessageList.tsx +3 -81
  151. package/src/ui/effortCommand.js +19 -0
  152. package/src/ui/effortCommand.ts +36 -0
  153. package/src/ui/hooks/useChat.js +2 -0
  154. package/src/ui/hooks/useChat.ts +2 -0
  155. package/src/ui/toolProgress.js +1 -1
  156. package/src/ui/toolProgress.ts +1 -1
  157. package/src/web/protocol.ts +10 -3
  158. package/src/advisor/advisorCoordinator.js +0 -734
  159. package/src/advisor/advisorCoordinator.ts +0 -825
  160. package/src/cli/wizards/sshHostWizard.js +0 -156
  161. package/src/cli/wizards/sshHostWizard.tsx +0 -214
  162. package/src/context/sshHosts.js +0 -290
  163. package/src/context/sshHosts.ts +0 -312
  164. package/src/loop/publication.js +0 -94
  165. package/src/loop/publication.ts +0 -124
  166. package/src/runtime/finalAnswer.js +0 -9
  167. package/src/runtime/finalAnswer.ts +0 -13
  168. package/src/tools/advisor/advisorDecision.js +0 -79
  169. package/src/tools/advisor/advisorDecision.ts +0 -92
  170. package/src/tools/ssh/ssh.js +0 -152
  171. package/src/tools/ssh/ssh.ts +0 -167
  172. package/src/tools/ssh/sshAvailability.js +0 -35
  173. package/src/tools/ssh/sshAvailability.ts +0 -39
  174. package/src/tools/ssh/sshClient.js +0 -177
  175. package/src/tools/ssh/sshClient.ts +0 -189
  176. package/src/tools/ssh/sshDownload.js +0 -77
  177. package/src/tools/ssh/sshDownload.ts +0 -92
  178. package/src/tools/ssh/sshHostsTool.js +0 -36
  179. package/src/tools/ssh/sshHostsTool.ts +0 -48
  180. package/src/tools/ssh/sshUpload.js +0 -72
  181. package/src/tools/ssh/sshUpload.ts +0 -85
package/src/llm/client.js CHANGED
@@ -26,6 +26,7 @@
26
26
  import { dlog } from '../utils/debugLog.js';
27
27
  import { buildOpenAIRequest, createOpenAISSEParser, isImageInputRejected, isMaxTokensRejected, isPromptCacheKeyRejected, isResponseFormatUnsupported, } from './providers/openaiFormat.js';
28
28
  import { promptMaterialHash, shortCacheScope } from './promptCache.js';
29
+ import { buildResponsesRequest, createResponsesSSEParser, isReasoningEffortRejected, } from './providers/responsesFormat.js';
29
30
  import { buildAnthropicRequest, createAnthropicSSEParser, } from './providers/anthropicFormat.js';
30
31
  import { chatGateway } from './gatewayClient.js';
31
32
  import { prepareMessagesForProvider } from './messageProvenance.js';
@@ -42,6 +43,11 @@ const rfUnsupported = new Set();
42
43
  */
43
44
  const maxTokensUnsupported = new Set();
44
45
  const promptCacheKeyUnsupported = new Set();
46
+ /**
47
+ * 记住"这个 baseURL::model 不认我们配的推理强度"。强度档位因模型而异(有的没有 none/minimal,
48
+ * 非推理模型根本没有 reasoning 参数),拒绝时去掉该参数重发一次,而不是让整个对话 400。
49
+ */
50
+ const reasoningEffortUnsupported = new Set();
45
51
  /** 调试日志用的请求自增号(vote 并发多 voter 时靠它在日志里区分是哪一路)。 */
46
52
  let reqCounter = 0;
47
53
  // ============================================================
@@ -278,7 +284,11 @@ async function* streamLocal(args) {
278
284
  const maxAttempts = args.maxAttempts ?? DEFAULT_RETRY.maxAttempts;
279
285
  if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1)
280
286
  throw new Error('Invalid maxAttempts');
281
- const format = provider.apiFormat === 'anthropic' ? 'anthropic' : 'openai';
287
+ const format = provider.apiFormat === 'anthropic'
288
+ ? 'anthropic'
289
+ : provider.apiFormat === 'openai-responses'
290
+ ? 'openai-responses'
291
+ : 'openai';
282
292
  if (format === 'anthropic' && responseFormat) {
283
293
  throw new Error('output_schema 结构化输出目前不支持 Anthropic Messages 格式的 provider' +
284
294
  '(Anthropic 的强制工具输出模式与 OpenAI 的 response_format.json_schema 形状不同,尚未实现映射)。' +
@@ -292,15 +302,17 @@ async function* streamLocal(args) {
292
302
  const rfKey = `${provider.baseURL}::${provider.model}`;
293
303
  // OpenAI 分支专属的「就地退化」状态;Anthropic 无此概念,两个 flag 恒为 false,
294
304
  // 下面的退化 if 分支天然不会进入(不需要额外的 format 判断散落各处)。
295
- let wantResponseFormat = format === 'openai' &&
305
+ let wantResponseFormat = format !== 'anthropic' &&
296
306
  responseFormat !== undefined &&
297
307
  !rfUnsupported.has(rfKey);
298
- let wantMaxTokens = format === 'openai' &&
308
+ let wantMaxTokens = format !== 'anthropic' &&
299
309
  typeof provider.maxTokens === 'number' &&
300
310
  provider.maxTokens > 0 &&
301
311
  !maxTokensUnsupported.has(rfKey);
302
312
  let imagesStripped = false;
303
- let wantPromptCacheKey = format === 'openai' &&
313
+ let wantReasoningEffort = !!provider.reasoningEffort &&
314
+ !reasoningEffortUnsupported.has(rfKey);
315
+ let wantPromptCacheKey = format !== 'anthropic' &&
304
316
  provider.promptCache?.mode === 'session-key' &&
305
317
  !!promptCache?.scopeKey &&
306
318
  !promptCacheKeyUnsupported.has(rfKey);
@@ -328,20 +340,34 @@ async function* streamLocal(args) {
328
340
  provider,
329
341
  cleanedMessages,
330
342
  tools,
343
+ withReasoningEffort: wantReasoningEffort,
331
344
  ...(cacheEdits ? { cacheEdits } : {}),
332
345
  })
333
- : buildOpenAIRequest({
334
- provider,
335
- cleanedMessages,
336
- tools,
337
- withResponseFormat: wantResponseFormat,
338
- withMaxTokens: wantMaxTokens,
339
- includeUsage,
340
- promptCache,
341
- withPromptCacheKey: wantPromptCacheKey,
342
- responseFormat,
343
- reasoning: provider.reasoning,
344
- });
346
+ : format === 'openai-responses'
347
+ ? buildResponsesRequest({
348
+ provider,
349
+ cleanedMessages,
350
+ tools,
351
+ withMaxTokens: wantMaxTokens,
352
+ withReasoningEffort: wantReasoningEffort,
353
+ withPromptCacheKey: wantPromptCacheKey,
354
+ promptCache,
355
+ responseFormat,
356
+ withResponseFormat: wantResponseFormat,
357
+ })
358
+ : buildOpenAIRequest({
359
+ provider,
360
+ cleanedMessages,
361
+ tools,
362
+ withResponseFormat: wantResponseFormat,
363
+ withMaxTokens: wantMaxTokens,
364
+ includeUsage,
365
+ promptCache,
366
+ withPromptCacheKey: wantPromptCacheKey,
367
+ responseFormat,
368
+ reasoning: provider.reasoning,
369
+ withReasoningEffort: wantReasoningEffort,
370
+ });
345
371
  return fetch(req.url, {
346
372
  method: 'POST',
347
373
  headers: req.headers,
@@ -440,6 +466,25 @@ async function* streamLocal(args) {
440
466
  break;
441
467
  errText = await resp.text().catch(() => '');
442
468
  }
469
+ // 推理强度不被该模型接受 → 去掉 reasoning 参数重发一次(沿用服务端默认强度),并告警。
470
+ if (wantReasoningEffort && isReasoningEffortRejected(resp.status, errText)) {
471
+ reasoningEffortUnsupported.add(rfKey);
472
+ wantReasoningEffort = false;
473
+ console.warn(`[minimal-agent] provider model "${provider.model}" 不接受推理强度 "${provider.reasoningEffort}",` +
474
+ `已去掉该参数重发(沿用服务端默认强度)。可在 /config 里改选该模型支持的档位。`);
475
+ try {
476
+ resp = await postChat();
477
+ watchdog.kick();
478
+ }
479
+ catch (e) {
480
+ if ((await handleNetworkError(e, attempt, 'reasoning-effort fallback fetch failed')) === 'break')
481
+ break;
482
+ continue;
483
+ }
484
+ if (resp.ok)
485
+ break;
486
+ errText = await resp.text().catch(() => '');
487
+ }
443
488
  // 退化①:response_format.json_schema 不支持
444
489
  if (wantResponseFormat &&
445
490
  isResponseFormatUnsupported(resp.status, errText)) {
@@ -554,7 +599,9 @@ async function* streamLocal(args) {
554
599
  let buffer = '';
555
600
  const parser = format === 'anthropic'
556
601
  ? createAnthropicSSEParser()
557
- : createOpenAISSEParser();
602
+ : format === 'openai-responses'
603
+ ? createResponsesSSEParser()
604
+ : createOpenAISSEParser();
558
605
  try {
559
606
  while (true) {
560
607
  // 每次 read 前检查 abort:用户按 ESC/Ctrl+C 时 signal.aborted 为 true。
package/src/llm/client.ts CHANGED
@@ -42,6 +42,11 @@ import {
42
42
  isResponseFormatUnsupported,
43
43
  } from './providers/openaiFormat.js';
44
44
  import { promptMaterialHash, shortCacheScope } from './promptCache.js';
45
+ import {
46
+ buildResponsesRequest,
47
+ createResponsesSSEParser,
48
+ isReasoningEffortRejected,
49
+ } from './providers/responsesFormat.js';
45
50
  import {
46
51
  buildAnthropicRequest,
47
52
  createAnthropicSSEParser,
@@ -64,6 +69,11 @@ const rfUnsupported = new Set<string>();
64
69
  */
65
70
  const maxTokensUnsupported = new Set<string>();
66
71
  const promptCacheKeyUnsupported = new Set<string>();
72
+ /**
73
+ * 记住"这个 baseURL::model 不认我们配的推理强度"。强度档位因模型而异(有的没有 none/minimal,
74
+ * 非推理模型根本没有 reasoning 参数),拒绝时去掉该参数重发一次,而不是让整个对话 400。
75
+ */
76
+ const reasoningEffortUnsupported = new Set<string>();
67
77
 
68
78
  /** 调试日志用的请求自增号(vote 并发多 voter 时靠它在日志里区分是哪一路)。 */
69
79
  let reqCounter = 0;
@@ -385,8 +395,12 @@ async function* streamLocal(args: {
385
395
  } = args;
386
396
  const maxAttempts = args.maxAttempts ?? DEFAULT_RETRY.maxAttempts;
387
397
  if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1) throw new Error('Invalid maxAttempts');
388
- const format: 'openai' | 'anthropic' =
389
- provider.apiFormat === 'anthropic' ? 'anthropic' : 'openai';
398
+ const format: 'openai' | 'openai-responses' | 'anthropic' =
399
+ provider.apiFormat === 'anthropic'
400
+ ? 'anthropic'
401
+ : provider.apiFormat === 'openai-responses'
402
+ ? 'openai-responses'
403
+ : 'openai';
390
404
 
391
405
  if (format === 'anthropic' && responseFormat) {
392
406
  throw new Error(
@@ -410,17 +424,20 @@ async function* streamLocal(args: {
410
424
  // OpenAI 分支专属的「就地退化」状态;Anthropic 无此概念,两个 flag 恒为 false,
411
425
  // 下面的退化 if 分支天然不会进入(不需要额外的 format 判断散落各处)。
412
426
  let wantResponseFormat =
413
- format === 'openai' &&
427
+ format !== 'anthropic' &&
414
428
  responseFormat !== undefined &&
415
429
  !rfUnsupported.has(rfKey);
416
430
  let wantMaxTokens =
417
- format === 'openai' &&
431
+ format !== 'anthropic' &&
418
432
  typeof provider.maxTokens === 'number' &&
419
433
  provider.maxTokens > 0 &&
420
434
  !maxTokensUnsupported.has(rfKey);
421
435
  let imagesStripped = false;
436
+ let wantReasoningEffort =
437
+ !!provider.reasoningEffort &&
438
+ !reasoningEffortUnsupported.has(rfKey);
422
439
  let wantPromptCacheKey =
423
- format === 'openai' &&
440
+ format !== 'anthropic' &&
424
441
  provider.promptCache?.mode === 'session-key' &&
425
442
  !!promptCache?.scopeKey &&
426
443
  !promptCacheKeyUnsupported.has(rfKey);
@@ -453,9 +470,22 @@ async function* streamLocal(args: {
453
470
  provider,
454
471
  cleanedMessages,
455
472
  tools,
473
+ withReasoningEffort: wantReasoningEffort,
456
474
  ...(cacheEdits ? { cacheEdits } : {}),
457
475
  })
458
- : buildOpenAIRequest({
476
+ : format === 'openai-responses'
477
+ ? buildResponsesRequest({
478
+ provider,
479
+ cleanedMessages,
480
+ tools,
481
+ withMaxTokens: wantMaxTokens,
482
+ withReasoningEffort: wantReasoningEffort,
483
+ withPromptCacheKey: wantPromptCacheKey,
484
+ promptCache,
485
+ responseFormat,
486
+ withResponseFormat: wantResponseFormat,
487
+ })
488
+ : buildOpenAIRequest({
459
489
  provider,
460
490
  cleanedMessages,
461
491
  tools,
@@ -466,6 +496,7 @@ async function* streamLocal(args: {
466
496
  withPromptCacheKey: wantPromptCacheKey,
467
497
  responseFormat,
468
498
  reasoning: provider.reasoning,
499
+ withReasoningEffort: wantReasoningEffort,
469
500
  });
470
501
  return fetch(req.url, {
471
502
  method: 'POST',
@@ -590,6 +621,28 @@ async function* streamLocal(args: {
590
621
  errText = await resp.text().catch(() => '');
591
622
  }
592
623
 
624
+ // 推理强度不被该模型接受 → 去掉 reasoning 参数重发一次(沿用服务端默认强度),并告警。
625
+ if (wantReasoningEffort && isReasoningEffortRejected(resp.status, errText)) {
626
+ reasoningEffortUnsupported.add(rfKey);
627
+ wantReasoningEffort = false;
628
+ console.warn(
629
+ `[minimal-agent] provider model "${provider.model}" 不接受推理强度 "${provider.reasoningEffort}",` +
630
+ `已去掉该参数重发(沿用服务端默认强度)。可在 /config 里改选该模型支持的档位。`,
631
+ );
632
+ try {
633
+ resp = await postChat();
634
+ watchdog.kick();
635
+ } catch (e) {
636
+ if (
637
+ (await handleNetworkError(e, attempt, 'reasoning-effort fallback fetch failed')) === 'break'
638
+ )
639
+ break;
640
+ continue;
641
+ }
642
+ if (resp.ok) break;
643
+ errText = await resp.text().catch(() => '');
644
+ }
645
+
593
646
  // 退化①:response_format.json_schema 不支持
594
647
  if (
595
648
  wantResponseFormat &&
@@ -730,7 +783,9 @@ async function* streamLocal(args: {
730
783
  const parser =
731
784
  format === 'anthropic'
732
785
  ? createAnthropicSSEParser()
733
- : createOpenAISSEParser();
786
+ : format === 'openai-responses'
787
+ ? createResponsesSSEParser()
788
+ : createOpenAISSEParser();
734
789
 
735
790
  try {
736
791
  while (true) {
@@ -140,7 +140,7 @@ export function buildAnthropicUrl(provider) {
140
140
  return url;
141
141
  }
142
142
  export function buildAnthropicRequest(args) {
143
- const { provider, cleanedMessages, tools, cacheEdits } = args;
143
+ const { provider, cleanedMessages, tools, cacheEdits, withReasoningEffort } = args;
144
144
  const { system, messages } = toAnthropicMessages(cleanedMessages);
145
145
  const anthropicTools = toAnthropicTools(tools);
146
146
  if (cacheEdits)
@@ -158,6 +158,8 @@ export function buildAnthropicRequest(args) {
158
158
  // tools: [] 会被部分 provider 拒绝或产生意外行为——空数组直接不发这个字段。
159
159
  if (anthropicTools.length > 0)
160
160
  body.tools = anthropicTools;
161
+ if (withReasoningEffort && provider.reasoningEffort)
162
+ body.output_config = { effort: provider.reasoningEffort };
161
163
  return {
162
164
  url: buildAnthropicUrl(provider),
163
165
  headers: {
@@ -160,10 +160,12 @@ export interface AnthropicRequestArgs {
160
160
  * 传入即进入协议组装:pinned 重发 + pendingNew 发送并 pin + cache_reference 标注。
161
161
  */
162
162
  cacheEdits?: CacheEditsState;
163
+ /** 发送 provider.reasoningEffort(Anthropic 兼容端点认 output_config.effort;被拒绝过的由 client.ts 关掉)。 */
164
+ withReasoningEffort?: boolean;
163
165
  }
164
166
 
165
167
  export function buildAnthropicRequest(args: AnthropicRequestArgs): { url: string; headers: Record<string, string>; body: string } {
166
- const { provider, cleanedMessages, tools, cacheEdits } = args;
168
+ const { provider, cleanedMessages, tools, cacheEdits, withReasoningEffort } = args;
167
169
  const { system, messages } = toAnthropicMessages(cleanedMessages);
168
170
  const anthropicTools = toAnthropicTools(tools);
169
171
  if (cacheEdits) applyCacheEdits(messages, cacheEdits);
@@ -179,6 +181,7 @@ export function buildAnthropicRequest(args: AnthropicRequestArgs): { url: string
179
181
  };
180
182
  // tools: [] 会被部分 provider 拒绝或产生意外行为——空数组直接不发这个字段。
181
183
  if (anthropicTools.length > 0) body.tools = anthropicTools;
184
+ if (withReasoningEffort && provider.reasoningEffort) body.output_config = { effort: provider.reasoningEffort };
182
185
  return {
183
186
  url: buildAnthropicUrl(provider),
184
187
  headers: {
@@ -15,7 +15,8 @@ export function isResponseFormatUnsupported(status, errText) {
15
15
  if (status !== 400)
16
16
  return false;
17
17
  const t = errText.toLowerCase();
18
- if (!t.includes('response_format'))
18
+ // Responses API 把它叫 text.format。
19
+ if (!t.includes('response_format') && !t.includes('text.format'))
19
20
  return false;
20
21
  return (t.includes('not support') ||
21
22
  t.includes('unsupported') ||
@@ -31,7 +32,7 @@ export function isMaxTokensRejected(status, errText) {
31
32
  if (status !== 400)
32
33
  return false;
33
34
  const t = errText.toLowerCase();
34
- if (!t.includes('max_tokens') && !t.includes('max tokens'))
35
+ if (!t.includes('max_tokens') && !t.includes('max tokens') && !t.includes('max_output_tokens'))
35
36
  return false;
36
37
  return (t.includes('not support') ||
37
38
  t.includes('unsupported') ||
@@ -189,6 +190,9 @@ export function buildOpenAIUrl(provider) {
189
190
  }
190
191
  return url;
191
192
  }
193
+ function isOpenRouter(baseURL) {
194
+ return /(?:^|\/\/|\.)openrouter\.ai(?:[/:]|$)/i.test(baseURL);
195
+ }
192
196
  export function buildOpenAIRequest(args) {
193
197
  const { provider, cleanedMessages, tools, withResponseFormat, withMaxTokens, includeUsage, responseFormat, promptCache, withPromptCacheKey, } = args;
194
198
  // 转换 image_url.detail: 'default' → 'auto',兼容所有 OpenAI 兼容 provider
@@ -224,7 +228,14 @@ export function buildOpenAIRequest(args) {
224
228
  // OpenRouter and other compatible providers accept this optional extension.
225
229
  // Omit it unless explicitly enabled so ordinary OpenAI-compatible endpoints
226
230
  // retain their default reasoning behavior.
227
- reasoning: args.reasoning ? { enabled: true } : undefined,
231
+ // reasoningEffort 优先:OpenRouter 认 reasoning.effort,OpenAI 官方 Chat Completions
232
+ // 认顶层 reasoning_effort,二者按端点分开发,避免把 OpenRouter 形状发给官方端点。
233
+ reasoning: args.withReasoningEffort && provider.reasoningEffort && isOpenRouter(provider.baseURL)
234
+ ? { effort: provider.reasoningEffort }
235
+ : args.reasoning ? { enabled: true } : undefined,
236
+ reasoning_effort: args.withReasoningEffort && provider.reasoningEffort && !isOpenRouter(provider.baseURL)
237
+ ? provider.reasoningEffort
238
+ : undefined,
228
239
  // ADR-05: structured output;缺省 undefined 不出现在 body(JSON.stringify 自动剥离)
229
240
  response_format: withResponseFormat ? responseFormat : undefined,
230
241
  ...(withPromptCacheKey && promptCache?.scopeKey
@@ -35,7 +35,8 @@ interface OpenAIToolSpec {
35
35
  export function isResponseFormatUnsupported(status: number, errText: string): boolean {
36
36
  if (status !== 400) return false;
37
37
  const t = errText.toLowerCase();
38
- if (!t.includes('response_format')) return false;
38
+ // Responses API 把它叫 text.format。
39
+ if (!t.includes('response_format') && !t.includes('text.format')) return false;
39
40
  return (
40
41
  t.includes('not support') ||
41
42
  t.includes('unsupported') ||
@@ -52,7 +53,7 @@ export function isResponseFormatUnsupported(status: number, errText: string): bo
52
53
  export function isMaxTokensRejected(status: number, errText: string): boolean {
53
54
  if (status !== 400) return false;
54
55
  const t = errText.toLowerCase();
55
- if (!t.includes('max_tokens') && !t.includes('max tokens')) return false;
56
+ if (!t.includes('max_tokens') && !t.includes('max tokens') && !t.includes('max_output_tokens')) return false;
56
57
  return (
57
58
  t.includes('not support') ||
58
59
  t.includes('unsupported') ||
@@ -121,6 +122,8 @@ export interface OpenAIRequestArgs {
121
122
  promptCache?: PromptCacheContext;
122
123
  withPromptCacheKey?: boolean;
123
124
  reasoning?: boolean;
125
+ /** 发送 provider.reasoningEffort(被服务端拒绝过的模型由 client.ts 关掉)。 */
126
+ withReasoningEffort?: boolean;
124
127
  responseFormat?: {
125
128
  type: 'json_schema';
126
129
  json_schema: { name: string; schema: Record<string, unknown>; strict?: boolean };
@@ -236,6 +239,10 @@ export function buildOpenAIUrl(provider: LocalProvider): string {
236
239
  return url;
237
240
  }
238
241
 
242
+ function isOpenRouter(baseURL: string): boolean {
243
+ return /(?:^|\/\/|\.)openrouter\.ai(?:[/:]|$)/i.test(baseURL);
244
+ }
245
+
239
246
  export function buildOpenAIRequest(args: OpenAIRequestArgs): { url: string; headers: Record<string, string>; body: string } {
240
247
  const {
241
248
  provider,
@@ -281,7 +288,14 @@ export function buildOpenAIRequest(args: OpenAIRequestArgs): { url: string; head
281
288
  // OpenRouter and other compatible providers accept this optional extension.
282
289
  // Omit it unless explicitly enabled so ordinary OpenAI-compatible endpoints
283
290
  // retain their default reasoning behavior.
284
- reasoning: args.reasoning ? { enabled: true } : undefined,
291
+ // reasoningEffort 优先:OpenRouter 认 reasoning.effort,OpenAI 官方 Chat Completions
292
+ // 认顶层 reasoning_effort,二者按端点分开发,避免把 OpenRouter 形状发给官方端点。
293
+ reasoning: args.withReasoningEffort && provider.reasoningEffort && isOpenRouter(provider.baseURL)
294
+ ? { effort: provider.reasoningEffort }
295
+ : args.reasoning ? { enabled: true } : undefined,
296
+ reasoning_effort: args.withReasoningEffort && provider.reasoningEffort && !isOpenRouter(provider.baseURL)
297
+ ? provider.reasoningEffort
298
+ : undefined,
285
299
  // ADR-05: structured output;缺省 undefined 不出现在 body(JSON.stringify 自动剥离)
286
300
  response_format: withResponseFormat ? responseFormat : undefined,
287
301
  ...(withPromptCacheKey && promptCache?.scopeKey