@lobehub/lobehub 2.0.0-next.116 → 2.0.0-next.117

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/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ## [Version 2.0.0-next.117](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.116...v2.0.0-next.117)
6
+
7
+ <sup>Released on **2025-11-25**</sup>
8
+
9
+ #### ✨ Features
10
+
11
+ - **misc**: Bedrock claude model thinking support.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### What's improved
19
+
20
+ - **misc**: Bedrock claude model thinking support, closes [#10422](https://github.com/lobehub/lobe-chat/issues/10422) ([8b41638](https://github.com/lobehub/lobe-chat/commit/8b41638))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ## [Version 2.0.0-next.116](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.115...v2.0.0-next.116)
6
31
 
7
32
  <sup>Released on **2025-11-25**</sup>
package/changelog/v1.json CHANGED
@@ -1,4 +1,13 @@
1
1
  [
2
+ {
3
+ "children": {
4
+ "features": [
5
+ "Bedrock claude model thinking support."
6
+ ]
7
+ },
8
+ "date": "2025-11-25",
9
+ "version": "2.0.0-next.117"
10
+ },
2
11
  {
3
12
  "children": {
4
13
  "features": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/lobehub",
3
- "version": "2.0.0-next.116",
3
+ "version": "2.0.0-next.117",
4
4
  "description": "LobeHub - an open-source,comprehensive AI Agent framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
5
5
  "keywords": [
6
6
  "framework",
@@ -13,7 +13,7 @@ const bedrockChatModels: AIChatModelCard[] = [
13
13
  'Claude Opus 4.5 是 Anthropic 的旗舰模型,结合了卓越的智能与可扩展性能,适合需要最高质量回应和推理能力的复杂任务。',
14
14
  displayName: 'Claude Opus 4.5',
15
15
  enabled: true,
16
- id: 'us.anthropic.claude-opus-4-5-20251101-v1:0',
16
+ id: 'global.anthropic.claude-opus-4-5-20251101-v1:0',
17
17
  maxOutput: 64_000,
18
18
  pricing: {
19
19
  units: [
@@ -23,6 +23,9 @@ const bedrockChatModels: AIChatModelCard[] = [
23
23
  ],
24
24
  },
25
25
  releasedAt: '2025-11-24',
26
+ settings: {
27
+ extendParams: ['disableContextCaching', 'enableReasoning', 'reasoningBudgetToken'],
28
+ },
26
29
  type: 'chat',
27
30
  },
28
31
  {
@@ -45,6 +48,9 @@ const bedrockChatModels: AIChatModelCard[] = [
45
48
  ],
46
49
  },
47
50
  releasedAt: '2025-09-29',
51
+ settings: {
52
+ extendParams: ['disableContextCaching', 'enableReasoning', 'reasoningBudgetToken'],
53
+ },
48
54
  type: 'chat',
49
55
  },
50
56
  {
@@ -68,6 +74,9 @@ const bedrockChatModels: AIChatModelCard[] = [
68
74
  ],
69
75
  },
70
76
  releasedAt: '2025-10-15',
77
+ settings: {
78
+ extendParams: ['disableContextCaching', 'enableReasoning', 'reasoningBudgetToken'],
79
+ },
71
80
  type: 'chat',
72
81
  },
73
82
  /*
@@ -103,7 +112,7 @@ const bedrockChatModels: AIChatModelCard[] = [
103
112
  'Claude 3.7 sonnet 是 Anthropic 最快的下一代模型。与 Claude 3 Haiku 相比,Claude 3.7 Sonnet 在各项技能上都有所提升,并在许多智力基准测试中超越了上一代最大的模型 Claude 3 Opus。',
104
113
  displayName: 'Claude 3.7 Sonnet',
105
114
  id: 'us.anthropic.claude-3-7-sonnet-20250219-v1:0',
106
- maxOutput: 8192,
115
+ maxOutput: 64_000,
107
116
  pricing: {
108
117
  units: [
109
118
  { name: 'textInput', rate: 3, strategy: 'fixed', unit: 'millionTokens' },
@@ -111,6 +120,9 @@ const bedrockChatModels: AIChatModelCard[] = [
111
120
  ],
112
121
  },
113
122
  releasedAt: '2025-02-24',
123
+ settings: {
124
+ extendParams: ['disableContextCaching', 'enableReasoning', 'reasoningBudgetToken'],
125
+ },
114
126
  type: 'chat',
115
127
  },
116
128
  {
@@ -131,6 +143,9 @@ const bedrockChatModels: AIChatModelCard[] = [
131
143
  ],
132
144
  },
133
145
  releasedAt: '2024-10-22',
146
+ settings: {
147
+ extendParams: ['disableContextCaching'],
148
+ },
134
149
  type: 'chat',
135
150
  },
136
151
  {
@@ -151,6 +166,9 @@ const bedrockChatModels: AIChatModelCard[] = [
151
166
  ],
152
167
  },
153
168
  releasedAt: '2024-10-22',
169
+ settings: {
170
+ extendParams: ['disableContextCaching'],
171
+ },
154
172
  type: 'chat',
155
173
  },
156
174
  {
@@ -171,6 +189,9 @@ const bedrockChatModels: AIChatModelCard[] = [
171
189
  ],
172
190
  },
173
191
  releasedAt: '2024-06-20',
192
+ settings: {
193
+ extendParams: ['disableContextCaching'],
194
+ },
174
195
  type: 'chat',
175
196
  },
176
197
  {
@@ -191,6 +212,9 @@ const bedrockChatModels: AIChatModelCard[] = [
191
212
  ],
192
213
  },
193
214
  releasedAt: '2024-03-07',
215
+ settings: {
216
+ extendParams: ['disableContextCaching'],
217
+ },
194
218
  type: 'chat',
195
219
  },
196
220
  {
@@ -228,6 +252,9 @@ const bedrockChatModels: AIChatModelCard[] = [
228
252
  ],
229
253
  },
230
254
  releasedAt: '2024-02-29',
255
+ settings: {
256
+ extendParams: ['disableContextCaching'],
257
+ },
231
258
  type: 'chat',
232
259
  },
233
260
  {
@@ -264,6 +291,7 @@ const bedrockChatModels: AIChatModelCard[] = [
264
291
  '一款快速、经济且仍然非常有能力的模型,可以处理包括日常对话、文本分析、总结和文档问答在内的一系列任务。',
265
292
  displayName: 'Claude Instant',
266
293
  id: 'anthropic.claude-instant-v1',
294
+ maxOutput: 4096,
267
295
  pricing: {
268
296
  units: [
269
297
  { name: 'textInput', rate: 0.8, strategy: 'fixed', unit: 'millionTokens' },
@@ -5,14 +5,14 @@ export const systemToUserModels = new Set([
5
5
  'o1-mini-2024-09-12',
6
6
  ]);
7
7
 
8
- // TODO: 临时写法,后续要重构成 model card 展示配置
8
+ // TODO: temporary implementation, needs to be refactored into model card display configuration
9
9
  export const disableStreamModels = new Set([
10
10
  'o1',
11
11
  'o1-2024-12-17',
12
12
  'o1-pro',
13
13
  'o1-pro-2025-03-19',
14
14
  /*
15
- 官网显示不支持,但是实际试下来支持 Streaming,暂时注释掉
15
+ Official documentation shows no support, but actual testing shows Streaming is supported, temporarily commented out
16
16
  'o3-pro',
17
17
  'o3-pro-2025-06-10',
18
18
  */
@@ -38,30 +38,68 @@ export const responsesAPIModels = new Set([
38
38
  'gpt-5-codex',
39
39
  'gpt-5-pro',
40
40
  'gpt-5-pro-2025-10-06',
41
+ 'gpt-5.1-codex',
42
+ 'gpt-5.1-codex-mini',
41
43
  ]);
42
44
 
43
45
  /**
44
- * models support context caching
46
+ * Regex patterns for models that support context caching (3.5+)
45
47
  */
46
- export const contextCachingModels = new Set([
47
- 'claude-opus-4-latest',
48
- 'claude-opus-4-20250514',
49
- 'claude-sonnet-4-latest',
50
- 'claude-sonnet-4-20250514',
51
- 'claude-3-7-sonnet-latest',
52
- 'claude-3-7-sonnet-20250219',
53
- 'claude-3-5-sonnet-latest',
54
- 'claude-3-5-sonnet-20241022',
55
- 'claude-3-5-sonnet-20240620',
56
- 'claude-3-5-haiku-latest',
57
- 'claude-3-5-haiku-20241022',
58
- ]);
48
+ export const contextCachingModelPatterns: RegExp[] = [
49
+ // Claude 4.5 series - Anthropic API
50
+ /^claude-(opus|sonnet|haiku)-4-5-/,
51
+ // Claude 4 series - Anthropic API
52
+ /^claude-(opus|sonnet)-4-/,
53
+ // Claude 3.7 - Anthropic API
54
+ /^claude-3-7-sonnet-/,
55
+ // Claude 3.5 series - Anthropic API
56
+ /^claude-3-5-(sonnet|haiku)-/,
57
+ // OpenRouter format (3.5+)
58
+ /^anthropic\/claude-(opus|sonnet|haiku)-(4\.5|4|3\.7|3\.5)/,
59
+ /^anthropic\/claude-(4\.5|4|3\.7|3\.5)-(opus|sonnet|haiku)/,
60
+ // AWS Bedrock format: [region.]anthropic.claude-xxx
61
+ /anthropic\.claude-(opus|sonnet|haiku)-(4-5|4|3-7|3-5)-/,
62
+ ];
59
63
 
60
- export const thinkingWithToolClaudeModels = new Set([
61
- 'claude-opus-4-latest',
62
- 'claude-opus-4-20250514',
63
- 'claude-sonnet-4-latest',
64
- 'claude-sonnet-4-20250514',
65
- 'claude-3-7-sonnet-latest',
66
- 'claude-3-7-sonnet-20250219',
67
- ]);
64
+ export const isContextCachingModel = (model: string): boolean => {
65
+ return contextCachingModelPatterns.some((pattern) => pattern.test(model));
66
+ };
67
+
68
+ /**
69
+ * Regex patterns for Claude models that support thinking with tools (3.7+)
70
+ */
71
+ export const thinkingWithToolClaudeModelPatterns: RegExp[] = [
72
+ // Claude 4.5 series - Anthropic API
73
+ /^claude-(opus|sonnet|haiku)-4-5-/,
74
+ // Claude 4 series - Anthropic API
75
+ /^claude-(opus|sonnet)-4-/,
76
+ // Claude 3.7 - Anthropic API
77
+ /^claude-3-7-sonnet-/,
78
+ // OpenRouter format (3.7+)
79
+ /^anthropic\/claude-(opus|sonnet|haiku)-(4\.5|4|3\.7)/,
80
+ /^anthropic\/claude-(4\.5|4|3\.7)-(opus|sonnet|haiku)/,
81
+ // AWS Bedrock format: [region.]anthropic.claude-xxx
82
+ /anthropic\.claude-(opus|sonnet|haiku)-(4-5|4|3-7)-/,
83
+ ];
84
+
85
+ export const isThinkingWithToolClaudeModel = (model: string): boolean => {
86
+ return thinkingWithToolClaudeModelPatterns.some((pattern) => pattern.test(model));
87
+ };
88
+
89
+ /**
90
+ * Regex patterns for Claude 4+ models that have temperature/top_p parameter conflict
91
+ * (cannot set both temperature and top_p at the same time)
92
+ */
93
+ export const temperatureTopPConflictModelPatterns: RegExp[] = [
94
+ // Claude 4+ series - Anthropic API (4, 4.1, 4.5)
95
+ /^claude-(opus|sonnet|haiku)-4/,
96
+ // OpenRouter format
97
+ /^anthropic\/claude-(opus|sonnet|haiku)-(4\.5|4\.1|4)/,
98
+ /^anthropic\/claude-(4\.5|4\.1|4)-(opus|sonnet|haiku)/,
99
+ // AWS Bedrock format: [region.]anthropic.claude-xxx
100
+ /anthropic\.claude-(opus|sonnet|haiku)-4/,
101
+ ];
102
+
103
+ export const hasTemperatureTopPConflict = (model: string): boolean => {
104
+ return temperatureTopPConflictModelPatterns.some((pattern) => pattern.test(model));
105
+ };
@@ -223,3 +223,17 @@ export const buildAnthropicTools = (
223
223
  }),
224
224
  );
225
225
  };
226
+
227
+ export const buildSearchTool = (): Anthropic.WebSearchTool20250305 => {
228
+ const maxUses = process.env.ANTHROPIC_MAX_USES;
229
+
230
+ return {
231
+ name: 'web_search',
232
+ type: 'web_search_20250305',
233
+ ...(maxUses &&
234
+ Number.isInteger(Number(maxUses)) &&
235
+ Number(maxUses) > 0 && {
236
+ max_uses: Number(maxUses),
237
+ }),
238
+ };
239
+ };
@@ -1,10 +1,7 @@
1
1
  import { describe, expect, it } from 'vitest';
2
2
 
3
- import {
4
- MODEL_PARAMETER_CONFLICTS,
5
- createParameterResolver,
6
- resolveParameters,
7
- } from './parameterResolver';
3
+ import { hasTemperatureTopPConflict } from '../const/models';
4
+ import { createParameterResolver, resolveParameters } from './parameterResolver';
8
5
 
9
6
  describe('resolveParameters', () => {
10
7
  describe('Basic functionality', () => {
@@ -247,54 +244,41 @@ describe('createParameterResolver', () => {
247
244
  });
248
245
  });
249
246
 
250
- describe('MODEL_PARAMETER_CONFLICTS', () => {
251
- describe('ANTHROPIC_CLAUDE_4_PLUS', () => {
252
- it('should contain expected Claude 4+ models', () => {
253
- expect(MODEL_PARAMETER_CONFLICTS.ANTHROPIC_CLAUDE_4_PLUS.has('claude-opus-4-1')).toBe(true);
254
- expect(
255
- MODEL_PARAMETER_CONFLICTS.ANTHROPIC_CLAUDE_4_PLUS.has('claude-opus-4-1-20250805'),
256
- ).toBe(true);
257
- expect(
258
- MODEL_PARAMETER_CONFLICTS.ANTHROPIC_CLAUDE_4_PLUS.has('claude-sonnet-4-5-20250929'),
259
- ).toBe(true);
260
- });
261
-
262
- it('should not contain Claude 3.x models', () => {
263
- expect(MODEL_PARAMETER_CONFLICTS.ANTHROPIC_CLAUDE_4_PLUS.has('claude-3-opus-20240229')).toBe(
264
- false,
265
- );
266
- expect(
267
- MODEL_PARAMETER_CONFLICTS.ANTHROPIC_CLAUDE_4_PLUS.has('claude-3.5-sonnet-20240620'),
268
- ).toBe(false);
247
+ describe('hasTemperatureTopPConflict', () => {
248
+ describe('Anthropic Claude 4+ models', () => {
249
+ it('should return true for Claude 4+ models', () => {
250
+ expect(hasTemperatureTopPConflict('claude-opus-4-1-20250805')).toBe(true);
251
+ expect(hasTemperatureTopPConflict('claude-sonnet-4-5-20250929')).toBe(true);
252
+ expect(hasTemperatureTopPConflict('claude-haiku-4-5-20251001')).toBe(true);
253
+ });
254
+
255
+ it('should return false for Claude 3.x models', () => {
256
+ expect(hasTemperatureTopPConflict('claude-3-opus-20240229')).toBe(false);
257
+ expect(hasTemperatureTopPConflict('claude-3-5-sonnet-20240620')).toBe(false);
258
+ });
259
+ });
260
+
261
+ describe('OpenRouter Claude 4+ models', () => {
262
+ it('should return true for OpenRouter Claude 4+ models', () => {
263
+ expect(hasTemperatureTopPConflict('anthropic/claude-opus-4.5')).toBe(true);
264
+ expect(hasTemperatureTopPConflict('anthropic/claude-sonnet-4.1')).toBe(true);
265
+ expect(hasTemperatureTopPConflict('anthropic/claude-4.5-opus')).toBe(true);
266
+ });
267
+
268
+ it('should return false for OpenRouter Claude 3.x models', () => {
269
+ expect(hasTemperatureTopPConflict('anthropic/claude-3.5-sonnet')).toBe(false);
270
+ expect(hasTemperatureTopPConflict('anthropic/claude-3.7-sonnet')).toBe(false);
269
271
  });
270
272
  });
271
273
 
272
- describe('BEDROCK_CLAUDE_4_PLUS', () => {
273
- it('should contain both standard and Bedrock-specific model IDs', () => {
274
- expect(MODEL_PARAMETER_CONFLICTS.BEDROCK_CLAUDE_4_PLUS.has('claude-opus-4-1')).toBe(true);
275
- expect(
276
- MODEL_PARAMETER_CONFLICTS.BEDROCK_CLAUDE_4_PLUS.has(
277
- 'anthropic.claude-opus-4-1-20250805-v1:0',
278
- ),
279
- ).toBe(true);
280
- expect(
281
- MODEL_PARAMETER_CONFLICTS.BEDROCK_CLAUDE_4_PLUS.has(
282
- 'us.anthropic.claude-sonnet-4-5-20250929-v1:0',
283
- ),
284
- ).toBe(true);
285
- });
286
-
287
- it('should contain all Bedrock regional variants', () => {
288
- expect(
289
- MODEL_PARAMETER_CONFLICTS.BEDROCK_CLAUDE_4_PLUS.has(
290
- 'anthropic.claude-opus-4-20250514-v1:0',
291
- ),
292
- ).toBe(true);
293
- expect(
294
- MODEL_PARAMETER_CONFLICTS.BEDROCK_CLAUDE_4_PLUS.has(
295
- 'us.anthropic.claude-opus-4-20250514-v1:0',
296
- ),
297
- ).toBe(true);
274
+ describe('Bedrock Claude 4+ models', () => {
275
+ it('should return true for Bedrock Claude 4+ models', () => {
276
+ expect(hasTemperatureTopPConflict('anthropic.claude-opus-4-1-20250805-v1:0')).toBe(true);
277
+ expect(hasTemperatureTopPConflict('us.anthropic.claude-sonnet-4-5-20250929-v1:0')).toBe(true);
278
+ });
279
+
280
+ it('should return false for Bedrock Claude 3.x models', () => {
281
+ expect(hasTemperatureTopPConflict('anthropic.claude-3-5-sonnet-20240620-v1:0')).toBe(false);
298
282
  });
299
283
  });
300
284
  });
@@ -239,44 +239,3 @@ export const createParameterResolver = (options: ParameterResolverOptions) => {
239
239
  return resolveParameters(config, options);
240
240
  };
241
241
  };
242
-
243
- /**
244
- * Common model sets that have parameter conflicts
245
- */
246
- export const MODEL_PARAMETER_CONFLICTS = {
247
- /**
248
- * Claude models after Opus 4.1 that don't allow both temperature and top_p
249
- */
250
- ANTHROPIC_CLAUDE_4_PLUS: new Set([
251
- 'claude-opus-4-1',
252
- 'claude-opus-4-1-20250805',
253
- 'claude-sonnet-4-5-20250929',
254
- 'claude-haiku-4-5-20251001',
255
- 'claude-opus-4-5-20251101',
256
- ]),
257
-
258
- /**
259
- * Bedrock Claude 4+ models (including Bedrock-specific model IDs)
260
- */
261
- BEDROCK_CLAUDE_4_PLUS: new Set([
262
- 'claude-opus-4-1',
263
- 'claude-opus-4-1-20250805',
264
- 'claude-opus-4-20250514',
265
- 'claude-sonnet-4-20250514',
266
- 'claude-sonnet-4-5-20250929',
267
- 'claude-haiku-4-5-20251001',
268
- // Bedrock model IDs
269
- 'anthropic.claude-opus-4-1-20250805-v1:0',
270
- 'us.anthropic.claude-opus-4-1-20250805-v1:0',
271
- 'anthropic.claude-opus-4-20250514-v1:0',
272
- 'us.anthropic.claude-opus-4-20250514-v1:0',
273
- 'anthropic.claude-sonnet-4-20250514-v1:0',
274
- 'us.anthropic.claude-sonnet-4-20250514-v1:0',
275
- 'anthropic.claude-sonnet-4-5-20250929-v1:0',
276
- 'us.anthropic.claude-sonnet-4-5-20250929-v1:0',
277
- 'anthropic.claude-haiku-4-5-20251001-v1:0',
278
- 'us.anthropic.claude-haiku-4-5-20251001-v1:0',
279
- 'global.anthropic.claude-opus-4-5-20251101-v1:0',
280
- 'anthropic.claude-opus-4-5-20251101-v1:0',
281
- ]),
282
- };
@@ -1,3 +1,4 @@
1
+ export * from './const/models';
1
2
  export * from './core/BaseAI';
2
3
  export { pruneReasoningPayload } from './core/contextBuilders/openai';
3
4
  export { ModelRuntime } from './core/ModelRuntime';
@@ -1,9 +1,14 @@
1
1
  import Anthropic, { ClientOptions } from '@anthropic-ai/sdk';
2
2
  import { ModelProvider } from 'model-bank';
3
3
 
4
+ import { hasTemperatureTopPConflict } from '../../const/models';
4
5
  import { LobeRuntimeAI } from '../../core/BaseAI';
5
- import { buildAnthropicMessages, buildAnthropicTools } from '../../core/contextBuilders/anthropic';
6
- import { MODEL_PARAMETER_CONFLICTS, resolveParameters } from '../../core/parameterResolver';
6
+ import {
7
+ buildAnthropicMessages,
8
+ buildAnthropicTools,
9
+ buildSearchTool,
10
+ } from '../../core/contextBuilders/anthropic';
11
+ import { resolveParameters } from '../../core/parameterResolver';
7
12
  import { AnthropicStream } from '../../core/streams';
8
13
  import {
9
14
  type ChatCompletionErrorPayload,
@@ -22,6 +27,7 @@ import { StreamingResponse } from '../../utils/response';
22
27
  import { createAnthropicGenerateObject } from './generateObject';
23
28
  import { handleAnthropicError } from './handleAnthropicError';
24
29
  import { resolveCacheTTL } from './resolveCacheTTL';
30
+ import { resolveMaxTokens } from './resolveMaxTokens';
25
31
 
26
32
  export interface AnthropicModelCard {
27
33
  created_at: string;
@@ -31,8 +37,6 @@ export interface AnthropicModelCard {
31
37
 
32
38
  type anthropicTools = Anthropic.Tool | Anthropic.WebSearchTool20250305;
33
39
 
34
- const modelsWithSmallContextWindow = new Set(['claude-3-opus-20240229', 'claude-3-haiku-20240307']);
35
-
36
40
  const DEFAULT_BASE_URL = 'https://api.anthropic.com';
37
41
 
38
42
  interface AnthropicAIParams extends ClientOptions {
@@ -140,15 +144,13 @@ export class LobeAnthropicAI implements LobeRuntimeAI {
140
144
  } = payload;
141
145
 
142
146
  const { anthropic: anthropicModels } = await import('model-bank');
143
- const modelConfig = anthropicModels.find((m) => m.id === model);
144
- const defaultMaxOutput = modelConfig?.maxOutput;
145
147
 
146
- // 配置优先级:用户设置 > 模型配置 > 硬编码默认值
147
- const getMaxTokens = () => {
148
- if (max_tokens) return max_tokens;
149
- if (defaultMaxOutput) return defaultMaxOutput;
150
- return undefined;
151
- };
148
+ const resolvedMaxTokens = await resolveMaxTokens({
149
+ max_tokens,
150
+ model,
151
+ providerModels: anthropicModels,
152
+ thinking,
153
+ });
152
154
 
153
155
  const system_message = messages.find((m) => m.role === 'system');
154
156
  const user_messages = messages.filter((m) => m.role !== 'system');
@@ -170,20 +172,8 @@ export class LobeAnthropicAI implements LobeRuntimeAI {
170
172
  });
171
173
 
172
174
  if (enabledSearch) {
173
- // Limit the number of searches per request
174
- const maxUses = process.env.ANTHROPIC_MAX_USES;
175
-
176
- const webSearchTool: Anthropic.WebSearchTool20250305 = {
177
- name: 'web_search',
178
- type: 'web_search_20250305',
179
- ...(maxUses &&
180
- Number.isInteger(Number(maxUses)) &&
181
- Number(maxUses) > 0 && {
182
- max_uses: Number(maxUses),
183
- }),
184
- };
185
-
186
- // 如果已有工具,则添加到现有工具列表中;否则创建新的工具列表
175
+ const webSearchTool = buildSearchTool();
176
+
187
177
  if (postTools && postTools.length > 0) {
188
178
  postTools = [...postTools, webSearchTool];
189
179
  } else {
@@ -192,19 +182,17 @@ export class LobeAnthropicAI implements LobeRuntimeAI {
192
182
  }
193
183
 
194
184
  if (!!thinking && thinking.type === 'enabled') {
195
- const maxTokens = getMaxTokens() || 32_000; // Claude Opus 4 has minimum maxOutput
196
-
197
185
  // `temperature` may only be set to 1 when thinking is enabled.
198
186
  // `top_p` must be unset when thinking is enabled.
199
187
  return {
200
- max_tokens: maxTokens,
188
+ max_tokens: resolvedMaxTokens,
201
189
  messages: postMessages,
202
190
  model,
203
191
  system: systemPrompts,
204
192
  thinking: {
205
193
  ...thinking,
206
194
  budget_tokens: thinking?.budget_tokens
207
- ? Math.min(thinking.budget_tokens, maxTokens - 1) // `max_tokens` must be greater than `thinking.budget_tokens`.
195
+ ? Math.min(thinking.budget_tokens, resolvedMaxTokens - 1) // `max_tokens` must be greater than `thinking.budget_tokens`.
208
196
  : 1024,
209
197
  },
210
198
  tools: postTools,
@@ -212,7 +200,7 @@ export class LobeAnthropicAI implements LobeRuntimeAI {
212
200
  }
213
201
 
214
202
  // Resolve temperature and top_p parameters based on model constraints
215
- const hasConflict = MODEL_PARAMETER_CONFLICTS.ANTHROPIC_CLAUDE_4_PLUS.has(model);
203
+ const hasConflict = hasTemperatureTopPConflict(model);
216
204
  const resolvedParams = resolveParameters(
217
205
  { temperature, top_p },
218
206
  { hasConflict, normalizeTemperature: true, preferTemperature: true },
@@ -221,7 +209,7 @@ export class LobeAnthropicAI implements LobeRuntimeAI {
221
209
  return {
222
210
  // claude 3 series model hax max output token of 4096, 3.x series has 8192
223
211
  // https://docs.anthropic.com/en/docs/about-claude/models/all-models#:~:text=200K-,Max%20output,-Normal%3A
224
- max_tokens: getMaxTokens() || (modelsWithSmallContextWindow.has(model) ? 4096 : 8192),
212
+ max_tokens: resolvedMaxTokens,
225
213
  messages: postMessages,
226
214
  model,
227
215
  system: systemPrompts,
@@ -0,0 +1,35 @@
1
+ import type { ChatStreamPayload } from '../../types';
2
+
3
+ const smallContextWindowPatterns = [
4
+ /claude-3-opus-20240229/,
5
+ /claude-3-haiku-20240307/,
6
+ /claude-v2(:1)?$/,
7
+ ];
8
+
9
+ /**
10
+ * Resolve the max_tokens value to align Anthropic and Bedrock behavior.
11
+ * Priority: user input > model-bank default maxOutput > hardcoded fallback (context-window aware).
12
+ */
13
+ export const resolveMaxTokens = async ({
14
+ max_tokens,
15
+ model,
16
+ thinking,
17
+ providerModels,
18
+ }: {
19
+ max_tokens?: number;
20
+ model: string;
21
+ providerModels: { id: string; maxOutput?: number }[];
22
+ thinking?: ChatStreamPayload['thinking'];
23
+ }) => {
24
+ const defaultMaxOutput = providerModels.find((m) => m.id === model)?.maxOutput;
25
+
26
+ const preferredMaxTokens = max_tokens ?? defaultMaxOutput;
27
+
28
+ if (preferredMaxTokens) return preferredMaxTokens;
29
+
30
+ if (thinking?.type === 'enabled') return 32_000;
31
+
32
+ const hasSmallContextWindow = smallContextWindowPatterns.some((pattern) => pattern.test(model));
33
+
34
+ return hasSmallContextWindow ? 4096 : 8192;
35
+ };
@@ -10,8 +10,6 @@ import { AgentRuntimeErrorType } from '../../types/error';
10
10
  import * as debugStreamModule from '../../utils/debugStream';
11
11
  import { LobeBedrockAI, experimental_buildLlama2Prompt } from './index';
12
12
 
13
- const provider = 'bedrock';
14
-
15
13
  // Mock the console.error to avoid polluting test output
16
14
  vi.spyOn(console, 'error').mockImplementation(() => {});
17
15
 
@@ -478,7 +476,7 @@ describe('LobeBedrockAI', () => {
478
476
  accept: 'application/json',
479
477
  body: JSON.stringify({
480
478
  anthropic_version: 'bedrock-2023-05-31',
481
- max_tokens: 4096,
479
+ max_tokens: 8192,
482
480
  messages: [
483
481
  {
484
482
  content: [
@@ -521,7 +519,7 @@ describe('LobeBedrockAI', () => {
521
519
  accept: 'application/json',
522
520
  body: JSON.stringify({
523
521
  anthropic_version: 'bedrock-2023-05-31',
524
- max_tokens: 4096,
522
+ max_tokens: 8192,
525
523
  messages: [
526
524
  {
527
525
  content: [
@@ -565,7 +563,7 @@ describe('LobeBedrockAI', () => {
565
563
  accept: 'application/json',
566
564
  body: JSON.stringify({
567
565
  anthropic_version: 'bedrock-2023-05-31',
568
- max_tokens: 4096,
566
+ max_tokens: 8192,
569
567
  messages: [
570
568
  {
571
569
  content: [
@@ -610,7 +608,7 @@ describe('LobeBedrockAI', () => {
610
608
  accept: 'application/json',
611
609
  body: JSON.stringify({
612
610
  anthropic_version: 'bedrock-2023-05-31',
613
- max_tokens: 4096,
611
+ max_tokens: 64_000,
614
612
  messages: [
615
613
  {
616
614
  content: [
@@ -654,7 +652,7 @@ describe('LobeBedrockAI', () => {
654
652
  accept: 'application/json',
655
653
  body: JSON.stringify({
656
654
  anthropic_version: 'bedrock-2023-05-31',
657
- max_tokens: 4096,
655
+ max_tokens: 8192,
658
656
  messages: [
659
657
  {
660
658
  content: [
@@ -1,3 +1,4 @@
1
+ import type Anthropic from '@anthropic-ai/sdk';
1
2
  import {
2
3
  BedrockRuntimeClient,
3
4
  InvokeModelCommand,
@@ -5,9 +6,10 @@ import {
5
6
  } from '@aws-sdk/client-bedrock-runtime';
6
7
  import { ModelProvider } from 'model-bank';
7
8
 
9
+ import { hasTemperatureTopPConflict } from '../../const/models';
8
10
  import { LobeRuntimeAI } from '../../core/BaseAI';
9
11
  import { buildAnthropicMessages, buildAnthropicTools } from '../../core/contextBuilders/anthropic';
10
- import { MODEL_PARAMETER_CONFLICTS, resolveParameters } from '../../core/parameterResolver';
12
+ import { resolveParameters } from '../../core/parameterResolver';
11
13
  import {
12
14
  AWSBedrockClaudeStream,
13
15
  AWSBedrockLlamaStream,
@@ -26,6 +28,7 @@ import { debugStream } from '../../utils/debugStream';
26
28
  import { getModelPricing } from '../../utils/getModelPricing';
27
29
  import { StreamingResponse } from '../../utils/response';
28
30
  import { resolveCacheTTL } from '../anthropic/resolveCacheTTL';
31
+ import { resolveMaxTokens } from '../anthropic/resolveMaxTokens';
29
32
 
30
33
  /**
31
34
  * A prompt constructor for HuggingFace LLama 2 chat models.
@@ -62,19 +65,24 @@ export function experimental_buildLlama2Prompt(messages: { content: string; role
62
65
  export interface LobeBedrockAIParams {
63
66
  accessKeyId?: string;
64
67
  accessKeySecret?: string;
68
+ id?: string;
65
69
  region?: string;
66
70
  sessionToken?: string;
67
71
  }
68
72
 
69
73
  export class LobeBedrockAI implements LobeRuntimeAI {
70
74
  private client: BedrockRuntimeClient;
75
+ private id: string;
71
76
 
72
77
  region: string;
73
78
 
74
- constructor({ region, accessKeyId, accessKeySecret, sessionToken }: LobeBedrockAIParams = {}) {
79
+ constructor(options: LobeBedrockAIParams = {}) {
80
+ const { id, region, accessKeyId, accessKeySecret, sessionToken } = options;
81
+
75
82
  if (!(accessKeyId && accessKeySecret))
76
83
  throw AgentRuntimeError.createError(AgentRuntimeErrorType.InvalidBedrockCredentials);
77
84
  this.region = region ?? 'us-east-1';
85
+ this.id = id ?? ModelProvider.Bedrock;
78
86
  this.client = new BedrockRuntimeClient({
79
87
  credentials: {
80
88
  accessKeyId: accessKeyId,
@@ -158,18 +166,28 @@ export class LobeBedrockAI implements LobeRuntimeAI {
158
166
  temperature,
159
167
  top_p,
160
168
  tools,
169
+ thinking,
161
170
  } = payload;
162
171
  const inputStartAt = Date.now();
163
172
  const system_message = messages.find((m) => m.role === 'system');
164
173
  const user_messages = messages.filter((m) => m.role !== 'system');
165
174
 
166
175
  // Resolve temperature and top_p parameters based on model constraints
167
- const hasConflict = MODEL_PARAMETER_CONFLICTS.BEDROCK_CLAUDE_4_PLUS.has(model);
176
+ const hasConflict = hasTemperatureTopPConflict(model);
168
177
  const resolvedParams = resolveParameters(
169
178
  { temperature, top_p },
170
179
  { hasConflict, normalizeTemperature: true, preferTemperature: true },
171
180
  );
172
181
 
182
+ const { bedrock: bedrockModels } = await import('model-bank');
183
+
184
+ const resolvedMaxTokens = await resolveMaxTokens({
185
+ max_tokens,
186
+ model,
187
+ providerModels: bedrockModels,
188
+ thinking,
189
+ });
190
+
173
191
  const systemPrompts = !!system_message?.content
174
192
  ? ([
175
193
  {
@@ -177,19 +195,40 @@ export class LobeBedrockAI implements LobeRuntimeAI {
177
195
  text: system_message.content as string,
178
196
  type: 'text',
179
197
  },
180
- ] as any)
198
+ ] as Anthropic.TextBlockParam[])
181
199
  : undefined;
182
200
 
183
- const anthropicPayload = {
201
+ const postTools = buildAnthropicTools(tools, {
202
+ enabledContextCaching,
203
+ });
204
+
205
+ const anthropicBase = {
184
206
  anthropic_version: 'bedrock-2023-05-31',
185
- max_tokens: max_tokens || 4096,
207
+ max_tokens: resolvedMaxTokens,
186
208
  messages: await buildAnthropicMessages(user_messages, { enabledContextCaching }),
187
209
  system: systemPrompts,
188
- temperature: resolvedParams.temperature,
189
- tools: buildAnthropicTools(tools, { enabledContextCaching }),
190
- top_p: resolvedParams.top_p,
210
+ tools: postTools,
191
211
  };
192
212
 
213
+ const anthropicPayload =
214
+ thinking?.type === 'enabled'
215
+ ? {
216
+ ...anthropicBase,
217
+ thinking: {
218
+ ...thinking,
219
+ // `max_tokens` must be greater than `budget_tokens`
220
+ budget_tokens: Math.max(
221
+ 1,
222
+ Math.min(thinking.budget_tokens || 1024, resolvedMaxTokens - 1),
223
+ ),
224
+ },
225
+ }
226
+ : {
227
+ ...anthropicBase,
228
+ temperature: resolvedParams.temperature,
229
+ top_p: resolvedParams.top_p,
230
+ };
231
+
193
232
  const command = new InvokeModelWithResponseStreamCommand({
194
233
  accept: 'application/json',
195
234
  body: JSON.stringify(anthropicPayload),
@@ -209,7 +248,7 @@ export class LobeBedrockAI implements LobeRuntimeAI {
209
248
  debugStream(debug).catch(console.error);
210
249
  }
211
250
 
212
- const pricing = await getModelPricing(payload.model, ModelProvider.Bedrock);
251
+ const pricing = await getModelPricing(payload.model, this.id);
213
252
  const cacheTTL = resolveCacheTTL({ ...payload, enabledContextCaching }, anthropicPayload);
214
253
  const pricingOptions = cacheTTL ? { lookupParams: { ttl: cacheTTL } } : undefined;
215
254
 
@@ -218,7 +257,7 @@ export class LobeBedrockAI implements LobeRuntimeAI {
218
257
  AWSBedrockClaudeStream(prod, {
219
258
  callbacks: options?.callback,
220
259
  inputStartAt,
221
- payload: { model, pricing, pricingOptions, provider: ModelProvider.Bedrock },
260
+ payload: { model, pricing, pricingOptions, provider: this.id },
222
261
  }),
223
262
  {
224
263
  headers: options?.headers,
@@ -1,4 +1,5 @@
1
- import { contextCachingModels, thinkingWithToolClaudeModels } from '@/const/models';
1
+ import { isContextCachingModel, isThinkingWithToolClaudeModel } from '@lobechat/model-runtime';
2
+
2
3
  import { DEFAULT_AGENT_CHAT_CONFIG, DEFAULT_AGENT_SEARCH_FC_MODEL } from '@/const/settings';
3
4
  import { AgentStoreState } from '@/store/agent/initialState';
4
5
  import { LobeAgentChatConfig } from '@/types/agent';
@@ -24,12 +25,12 @@ const enableHistoryCount = (s: AgentStoreState) => {
24
25
  // 如果开启了上下文缓存,且当前模型类型匹配,则不开启历史记录
25
26
  const enableContextCaching = !chatConfig.disableContextCaching;
26
27
 
27
- if (enableContextCaching && contextCachingModels.has(config.model)) return false;
28
+ if (enableContextCaching && isContextCachingModel(config.model)) return false;
28
29
 
29
30
  // 当开启搜索时,针对 claude 3.7 sonnet 模型不开启历史记录
30
31
  const enableSearch = isAgentEnableSearch(s);
31
32
 
32
- if (enableSearch && thinkingWithToolClaudeModels.has(config.model)) return false;
33
+ if (enableSearch && isThinkingWithToolClaudeModel(config.model)) return false;
33
34
 
34
35
  return chatConfig.enableHistoryCount;
35
36
  };
@@ -1,93 +0,0 @@
1
- export const systemToUserModels = new Set([
2
- 'o1-preview',
3
- 'o1-preview-2024-09-12',
4
- 'o1-mini',
5
- 'o1-mini-2024-09-12',
6
- ]);
7
-
8
- // TODO: temporary implementation, needs to be refactored into model card display configuration
9
- export const disableStreamModels = new Set([
10
- 'o1',
11
- 'o1-2024-12-17',
12
- 'o1-pro',
13
- 'o1-pro-2025-03-19',
14
- /*
15
- Official documentation shows no support, but actual testing shows Streaming is supported, temporarily commented out
16
- 'o3-pro',
17
- 'o3-pro-2025-06-10',
18
- */
19
- 'computer-use-preview',
20
- 'computer-use-preview-2025-03-11',
21
- ]);
22
-
23
- /**
24
- * models use Responses API only
25
- */
26
- export const responsesAPIModels = new Set([
27
- 'o1-pro',
28
- 'o1-pro-2025-03-19',
29
- 'o3-deep-research',
30
- 'o3-deep-research-2025-06-26',
31
- 'o3-pro',
32
- 'o3-pro-2025-06-10',
33
- 'o4-mini-deep-research',
34
- 'o4-mini-deep-research-2025-06-26',
35
- 'codex-mini-latest',
36
- 'computer-use-preview',
37
- 'computer-use-preview-2025-03-11',
38
- 'gpt-5-codex',
39
- 'gpt-5-pro',
40
- 'gpt-5-pro-2025-10-06',
41
- 'gpt-5.1-codex',
42
- 'gpt-5.1-codex-mini',
43
- ]);
44
-
45
- /**
46
- * models support context caching
47
- */
48
- export const contextCachingModels = new Set([
49
- 'claude-opus-4-5-20251101',
50
- 'claude-haiku-4-5-20251001',
51
- 'claude-sonnet-4-5-latest',
52
- 'claude-sonnet-4-5-20250929',
53
- 'anthropic/claude-sonnet-4.5',
54
- 'claude-opus-4-latest',
55
- 'claude-opus-4-20250514',
56
- 'claude-sonnet-4-latest',
57
- 'claude-sonnet-4-20250514',
58
- 'claude-3-7-sonnet-latest',
59
- 'claude-3-7-sonnet-20250219',
60
- 'claude-3-5-sonnet-latest',
61
- 'claude-3-5-sonnet-20241022',
62
- 'claude-3-5-sonnet-20240620',
63
- 'claude-3-5-haiku-latest',
64
- 'claude-3-5-haiku-20241022',
65
- // Bedrock model IDs
66
- 'us.anthropic.claude-sonnet-4-5-20250929-v1:0',
67
- 'anthropic.claude-sonnet-4-5-20250929-v1:0',
68
- 'us.anthropic.claude-haiku-4-5-20251001-v1:0',
69
- 'anthropic.claude-haiku-4-5-20251001-v1:0',
70
- 'global.anthropic.claude-opus-4-5-20251101-v1:0',
71
- 'anthropic.claude-opus-4-5-20251101-v1:0',
72
- ]);
73
-
74
- export const thinkingWithToolClaudeModels = new Set([
75
- 'claude-opus-4-5-20251101',
76
- 'claude-opus-4-latest',
77
- 'claude-opus-4-20250514',
78
- 'claude-sonnet-4-latest',
79
- 'claude-sonnet-4-20250514',
80
- 'claude-sonnet-4-5-latest',
81
- 'claude-sonnet-4-5-20250929',
82
- 'claude-haiku-4-5-20251001',
83
- 'anthropic/claude-sonnet-4.5',
84
- 'claude-3-7-sonnet-latest',
85
- 'claude-3-7-sonnet-20250219',
86
- // Bedrock model IDs
87
- 'us.anthropic.claude-sonnet-4-5-20250929-v1:0',
88
- 'anthropic.claude-sonnet-4-5-20250929-v1:0',
89
- 'us.anthropic.claude-haiku-4-5-20251001-v1:0',
90
- 'anthropic.claude-haiku-4-5-20251001-v1:0',
91
- 'global.anthropic.claude-opus-4-5-20251101-v1:0',
92
- 'anthropic.claude-opus-4-5-20251101-v1:0',
93
- ]);