🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🔨 chore(model-runtime): add User-Agent header for Anthropic API calls (#12433)
Mike Lambert22072789b6d0
diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.test.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.test.tsnew file mode 100644index 00000000000..be1ce29643a--- /dev/null+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.test.ts@@ -0,0 +1,48 @@+// @vitest-environment node+import Anthropic from '@anthropic-ai/sdk';+import { describe, expect, it, vi } from 'vitest';++import { createDefaultAnthropicClient } from './index';++vi.mock('@anthropic-ai/sdk', () => {+ const MockAnthropic = vi.fn();+ return { default: MockAnthropic };+});++vi.mock('@lobechat/const', () => ({+ CURRENT_VERSION: '1.0.0-test',+}));++const MockedAnthropic = vi.mocked(Anthropic);++describe('createDefaultAnthropicClient', () => {+ it('should include User-Agent header with current version', () => {+ MockedAnthropic.mockClear();++ createDefaultAnthropicClient({ apiKey: 'test-key' });++ expect(MockedAnthropic).toHaveBeenCalledWith(+ expect.objectContaining({+ defaultHeaders: expect.objectContaining({+ 'User-Agent': 'lobehub/1.0.0-test',+ }),+ }),+ );+ });++ it('should preserve caller-provided default headers alongside User-Agent', () => {+ MockedAnthropic.mockClear();++ createDefaultAnthropicClient({+ apiKey: 'test-key',+ defaultHeaders: { 'X-Custom': 'value' },+ });++ const passedOptions = MockedAnthropic.mock.calls[0][0] as any;++ expect(passedOptions.defaultHeaders).toMatchObject({+ 'User-Agent': 'lobehub/1.0.0-test',+ 'X-Custom': 'value',+ });+ });+});diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.tsindex 8f59985093a..e676b6c7f47 100644--- a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts@@ -1,5 +1,6 @@import Anthropic, { type ClientOptions } from '@anthropic-ai/sdk';import type { Stream } from '@anthropic-ai/sdk/streaming';+import { CURRENT_VERSION } from '@lobechat/const';import type { ChatModelCard } from '@lobechat/types';import debug from 'debug';@@ -236,6 +237,7 @@ export const createDefaultAnthropicClient = <T extends Record<string, any> = any) => {const betaHeaders = process.env.ANTHROPIC_BETA_HEADERS;const defaultHeaders = {+ 'User-Agent': `lobehub/${CURRENT_VERSION}`,...options.defaultHeaders,...(betaHeaders ? { 'anthropic-beta': betaHeaders } : {}),};