feat(conversation): add proxy support for Anthropic API

- Add https-proxy-agent dependency
- Configure httpAgent in ClaudeAgentService when ANTHROPIC_PROXY_URL is set
- Add ANTHROPIC_PROXY_URL environment variable to docker-compose.yml

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-09 20:34:13 -08:00
parent 210e752223
commit c6c9623f36
3 changed files with 17 additions and 3 deletions

View File

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

View File

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

View File

@ -1,6 +1,7 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Anthropic from '@anthropic-ai/sdk';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { ImmigrationToolsService } from './tools/immigration-tools.service';
import { buildSystemPrompt, SystemPromptConfig } from './prompts/system-prompt';
@ -33,9 +34,19 @@ export class ClaudeAgentService implements OnModuleInit {
) {}
onModuleInit() {
this.client = new Anthropic({
const proxyUrl = this.configService.get<string>('ANTHROPIC_PROXY_URL');
const clientOptions: Anthropic.ClientOptions = {
apiKey: this.configService.get<string>('ANTHROPIC_API_KEY'),
});
};
// Configure proxy if specified
if (proxyUrl) {
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
this.systemPromptConfig = {