refactor: simplify Anthropic client config using baseURL

Remove https-proxy-agent dependency since ANTHROPIC_BASE_URL already
supports pointing to a proxy server directly.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-09 20:45:44 -08:00
parent a43e0b40e8
commit 7f2fc153b5
4 changed files with 8 additions and 33 deletions

View File

@ -232,7 +232,6 @@ services:
REDIS_URL: redis://:${REDIS_PASSWORD:-redis123}@redis:6379 REDIS_URL: redis://:${REDIS_PASSWORD:-redis123}@redis:6379
ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY} ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
ANTHROPIC_BASE_URL: ${ANTHROPIC_BASE_URL:-https://api.anthropic.com} ANTHROPIC_BASE_URL: ${ANTHROPIC_BASE_URL:-https://api.anthropic.com}
ANTHROPIC_PROXY_URL: ${ANTHROPIC_PROXY_URL:-}
KNOWLEDGE_SERVICE_URL: http://knowledge-service:3003 KNOWLEDGE_SERVICE_URL: http://knowledge-service:3003
CORS_ORIGINS: https://iconsulting.szaiai.com,http://localhost:5173 CORS_ORIGINS: https://iconsulting.szaiai.com,http://localhost:5173
ports: ports:
@ -263,7 +262,6 @@ services:
REDIS_URL: redis://:${REDIS_PASSWORD:-redis123}@redis:6379 REDIS_URL: redis://:${REDIS_PASSWORD:-redis123}@redis:6379
ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY} ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
ANTHROPIC_BASE_URL: ${ANTHROPIC_BASE_URL:-https://api.anthropic.com} ANTHROPIC_BASE_URL: ${ANTHROPIC_BASE_URL:-https://api.anthropic.com}
ANTHROPIC_PROXY_URL: ${ANTHROPIC_PROXY_URL:-}
ports: ports:
- "3005:3005" - "3005:3005"
networks: networks:

View File

@ -31,8 +31,7 @@
"rxjs": "^7.8.0", "rxjs": "^7.8.0",
"socket.io": "^4.8.3", "socket.io": "^4.8.3",
"typeorm": "^0.3.19", "typeorm": "^0.3.19",
"uuid": "^9.0.0", "uuid": "^9.0.0"
"https-proxy-agent": "^7.0.6"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^10.0.0", "@nestjs/cli": "^10.0.0",

View File

@ -1,7 +1,6 @@
import { Injectable, OnModuleInit } from '@nestjs/common'; import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import Anthropic from '@anthropic-ai/sdk'; import Anthropic from '@anthropic-ai/sdk';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { ImmigrationToolsService } from './tools/immigration-tools.service'; import { ImmigrationToolsService } from './tools/immigration-tools.service';
import { buildSystemPrompt, SystemPromptConfig } from './prompts/system-prompt'; import { buildSystemPrompt, SystemPromptConfig } from './prompts/system-prompt';
@ -34,20 +33,17 @@ export class ClaudeAgentService implements OnModuleInit {
) {} ) {}
onModuleInit() { onModuleInit() {
const proxyUrl = this.configService.get<string>('ANTHROPIC_PROXY_URL'); const baseUrl = this.configService.get<string>('ANTHROPIC_BASE_URL');
const clientOptions: ConstructorParameters<typeof Anthropic>[0] = {
this.client = new Anthropic({
apiKey: this.configService.get<string>('ANTHROPIC_API_KEY'), apiKey: this.configService.get<string>('ANTHROPIC_API_KEY'),
}; baseURL: baseUrl || undefined,
});
// Configure proxy if specified if (baseUrl) {
if (proxyUrl) { console.log(`Using Anthropic API base URL: ${baseUrl}`);
console.log(`Using proxy for Anthropic API: ${proxyUrl}`);
const agent = new HttpsProxyAgent(proxyUrl);
clientOptions.httpAgent = agent;
} }
this.client = new Anthropic(clientOptions);
// Initialize with default config // Initialize with default config
this.systemPromptConfig = { this.systemPromptConfig = {
identity: '专业、友善、耐心的香港移民顾问', identity: '专业、友善、耐心的香港移民顾问',

View File

@ -132,9 +132,6 @@ importers:
class-validator: class-validator:
specifier: ^0.14.0 specifier: ^0.14.0
version: 0.14.3 version: 0.14.3
https-proxy-agent:
specifier: ^7.0.6
version: 7.0.6
ioredis: ioredis:
specifier: ^5.3.0 specifier: ^5.3.0
version: 5.9.1 version: 5.9.1
@ -3553,11 +3550,6 @@ packages:
- supports-color - supports-color
dev: false dev: false
/agent-base@7.1.4:
resolution: {integrity: sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==}
engines: {node: '>= 14'}
dev: false
/agentkeepalive@4.6.0: /agentkeepalive@4.6.0:
resolution: {integrity: sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==} resolution: {integrity: sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==}
engines: {node: '>= 8.0.0'} engines: {node: '>= 8.0.0'}
@ -5596,16 +5588,6 @@ packages:
- supports-color - supports-color
dev: false dev: false
/https-proxy-agent@7.0.6:
resolution: {integrity: sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==}
engines: {node: '>= 14'}
dependencies:
agent-base: 7.1.4
debug: 4.4.3
transitivePeerDependencies:
- supports-color
dev: false
/human-signals@2.1.0: /human-signals@2.1.0:
resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==} resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==}
engines: {node: '>=10.17.0'} engines: {node: '>=10.17.0'}