iconsulting/packages/services/conversation-service/src/infrastructure/agents/agents.module.ts

163 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Agents Module
* 多 Agent 系统 NestJS 模块
*
* 注册所有 Agent 服务:
* - CoordinatorAgentService主协调器替代 ClaudeAgentServiceV2
* - 6 个专家 Agent
* - ContextInjectorService动态上下文注入
*/
import { Module, Global } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import Anthropic from '@anthropic-ai/sdk';
import { ConfigService } from '@nestjs/config';
// Coordinator
import { CoordinatorAgentService } from './coordinator/coordinator-agent.service';
import { ContextInjectorService } from './coordinator/context-injector.service';
import { InputGateService } from './coordinator/input-gate.service';
// Specialists
import { PolicyExpertService } from './specialists/policy-expert.service';
import { AssessmentExpertService } from './specialists/assessment-expert.service';
import { StrategistService } from './specialists/strategist.service';
import { ObjectionHandlerService } from './specialists/objection-handler.service';
import { CaseAnalystService } from './specialists/case-analyst.service';
import { MemoryManagerService } from './specialists/memory-manager.service';
import { CollectionExpertService } from './specialists/collection-expert.service';
// External dependencies
import { KnowledgeModule } from '../knowledge/knowledge.module';
import { TokenUsageService } from '../claude/token-usage.service';
import { ImmigrationToolsService } from '../claude/tools/immigration-tools.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TokenUsageORM } from '../database/postgres/entities/token-usage.orm';
import { ConversationORM } from '../database/postgres/entities/conversation.orm';
import { UserArtifactORM } from '../database/postgres/entities/user-artifact.orm';
// MCP Integration
import { McpModule } from './mcp/mcp.module';
// Payment Integration
import { PaymentModule } from '../payment/payment.module';
// Evaluation Gate
import { EvaluationGateService } from './coordinator/evaluation-gate.service';
import { EvaluationRuleORM } from '../database/postgres/entities/evaluation-rule.orm';
import { AssessmentDirectiveORM } from '../database/postgres/entities/assessment-directive.orm';
import { CollectionDirectiveORM } from '../database/postgres/entities/collection-directive.orm';
import { EvaluationRulePostgresRepository } from '../database/postgres/repositories/evaluation-rule.repository';
import { EVALUATION_RULE_REPOSITORY } from '../../domain/repositories/evaluation-rule.repository.interface';
// Tool Hooks
import { ToolHooksService } from './hooks/tool-hooks.service';
// Admin directive chat
import { DirectiveChatService } from './admin/directive-chat.service';
import { CollectionDirectiveChatService } from './admin/collection-directive-chat.service';
import { SystemSupervisorChatService } from './admin/system-supervisor-chat.service';
// Agent execution tracking
import { AgentExecutionORM } from '../database/postgres/entities/agent-execution.orm';
// Redis (optional — checkpoint persistence)
import { RedisModule } from '../cache/redis.module';
/**
* Anthropic Client Provider
* 共享的 Anthropic SDK 实例,所有 Agent 共用
*/
const AnthropicClientProvider = {
provide: Anthropic,
useFactory: (configService: ConfigService) => {
const baseUrl = configService.get<string>('ANTHROPIC_BASE_URL');
const isProxyUrl =
baseUrl &&
(baseUrl.includes('67.223.119.33') || baseUrl.match(/^\d+\.\d+\.\d+\.\d+/));
if (isProxyUrl) {
console.log(`[AgentsModule] Using Anthropic proxy: ${baseUrl}`);
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
return new Anthropic({
apiKey: configService.get<string>('ANTHROPIC_API_KEY'),
baseURL: baseUrl || undefined,
});
},
inject: [ConfigService],
};
@Global()
@Module({
imports: [
ConfigModule,
KnowledgeModule,
TypeOrmModule.forFeature([TokenUsageORM, EvaluationRuleORM, ConversationORM, UserArtifactORM, AssessmentDirectiveORM, CollectionDirectiveORM, AgentExecutionORM]),
McpModule,
PaymentModule,
RedisModule,
],
providers: [
// Shared Anthropic client
AnthropicClientProvider,
// Token tracking
TokenUsageService,
// Legacy tool implementations (production-tested, reused by Coordinator)
ImmigrationToolsService,
// Context injection
ContextInjectorService,
// Specialist agents
PolicyExpertService,
AssessmentExpertService,
StrategistService,
ObjectionHandlerService,
CaseAnalystService,
MemoryManagerService,
CollectionExpertService,
// Input gate
InputGateService,
// Evaluation gate
{
provide: EVALUATION_RULE_REPOSITORY,
useClass: EvaluationRulePostgresRepository,
},
EvaluationGateService,
// Tool hooks (PreToolUse/PostToolUse interception)
ToolHooksService,
// Admin directive chat
DirectiveChatService,
CollectionDirectiveChatService,
SystemSupervisorChatService,
// Main coordinator
CoordinatorAgentService,
],
exports: [
CoordinatorAgentService,
DirectiveChatService,
CollectionDirectiveChatService,
SystemSupervisorChatService,
EvaluationGateService,
EVALUATION_RULE_REPOSITORY,
// Export specialists for potential direct use
PolicyExpertService,
AssessmentExpertService,
StrategistService,
ObjectionHandlerService,
CaseAnalystService,
MemoryManagerService,
CollectionExpertService,
],
})
export class AgentsModule {}