Commit Graph

86 Commits

Author SHA1 Message Date
hailin 7ff701369b fix(tenant): use TenantContextModule.forRoot() for global tenant context
All services were providing TenantContextService directly without
making it global, causing DI resolution failures in child modules.
Now using TenantContextModule.forRoot() which exports TenantContextService
globally so all repositories can access it.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 04:31:56 -08:00
hailin 89d9645c02 fix(docker): fix vips package for Alpine 3.23 in file-service
Alpine 3.23 doesn't have vips-dev, use vips runtime instead.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 03:47:56 -08:00
hailin 6bb47dad85 fix(docker): copy shared module AFTER npm install to prevent wipe
npm install was clearing the @iconsulting/shared folder that was
copied before it. Moving the COPY command after npm install ensures
the shared package remains in node_modules.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 03:41:16 -08:00
hailin 7975982fc3 feat(user): add user profile and contact management
- Add UserProfile entity with immigration-specific fields:
  - Basic info (name, birth date, nationality, current location)
  - Immigration intent (target countries, types, timeline)
  - Education records with WES evaluation flag
  - Work records with NOC codes
  - Language scores (IELTS, TOEFL, etc.)
  - Family members info
  - Financial info for investment immigration
  - Profile completion percentage calculation

- Add UserContact entity for identity binding:
  - Support multiple contact types (EMAIL, WECHAT, WHATSAPP, TELEGRAM, LINE)
  - Verification code flow with expiration
  - Notification settings (paid feature)
  - Notification types: POLICY_UPDATE, DEADLINE_REMINDER, etc.

- Add API endpoints:
  - GET/PUT /users/me/profile/* for profile sections
  - GET/POST/PUT/DELETE /users/me/contacts for contact management
  - POST /users/me/contacts/:type/verification for verification flow
  - POST/PUT/DELETE /users/me/contacts/:type/notifications

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 19:32:17 -08:00
hailin 92ee490a57 feat(multi-tenant): complete repository tenant filtering for remaining services
- knowledge-postgres.repository: add tenant_id to all queries and raw SQL
- memory-postgres.repository: add tenant_id filtering for UserMemory and SystemExperience
- admin-postgres.repository: add tenant_id filtering (direct injection for nullable tenantId)
- All 11 repositories now have proper tenant isolation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 19:12:04 -08:00
hailin 1df5854825 feat(multi-tenant): apply tenant middleware and refactor repositories
- Apply TenantContextMiddleware to all 6 services
- Add SimpleTenantFinder for services without direct tenant DB access
- Add TenantFinderService for evolution-service with database access
- Refactor 8 repositories to extend BaseTenantRepository:
  - user-postgres.repository.ts
  - verification-code-postgres.repository.ts
  - conversation-postgres.repository.ts
  - message-postgres.repository.ts
  - token-usage-postgres.repository.ts
  - file-postgres.repository.ts
  - order-postgres.repository.ts
  - payment-postgres.repository.ts
- Add @iconsulting/shared dependency to evolution-service and knowledge-service
- Configure middleware to exclude health and super-admin paths

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 18:30:31 -08:00
hailin 422069be68 feat: add enterprise multi-tenancy infrastructure
- Add shared tenant module with AsyncLocalStorage-based context management
- Create TenantContextService, TenantContextMiddleware, TenantGuard
- Add @TenantId(), @Tenant(), @RequireFeatures() decorators
- Create BaseTenantRepository for automatic tenant filtering
- Add TenantORM entity for tenants table
- Add tenant_id column to all 16 ORM entities across 6 services
- Create database migration script for multi-tenancy support
- Add tenant-related error codes

This implements row-level isolation for 100% data separation between tenants.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 18:11:12 -08:00
hailin 2d4e6285a4 feat(admin): add global token usage statistics
- Add token aggregation to statistics/overview endpoint
- Include total tokens, cost, and API calls for all time
- Include today's token usage and cost breakdown
- Display token stats in ConversationsPage with 2 rows of cards
- Add formatNumber helper for K/M number formatting
- Export GlobalTokenStats and TodayTokenStats types

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 17:29:59 -08:00
hailin 7acdf78e0c fix(conversation): improve token tracking accuracy
- Add 'error' chunk type to StreamChunk for partial token capture
- Record partial tokens to token_usage table even on API errors
- Capture error chunk tokens in conversation.service.ts
- Save partial response and tokens before re-throwing errors
- Add token aggregation from token_usage table for accurate stats
- Display detailed token info in admin (cache tokens, cost, API calls)
- Export TokenDetails type for frontend consumption

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 17:23:25 -08:00
hailin ae99b78579 fix(conversation): track token usage and message count in conversation entity
Problem:
- Token usage was recorded to token_usage table but not to conversation entity
- Message count was not being incremented
- Dashboard showed 0 tokens for all conversations

Solution:
- Add inputTokens/outputTokens fields to StreamChunk interface
- Return token usage in 'end' chunk from ClaudeAgentServiceV2
- Capture token usage in conversation.service.ts sendMessage
- Call conversation.addTokens() and incrementMessageCount() after each exchange
- Consolidate conversation updates into single repo.update() call

Files changed:
- claude-agent-v2.service.ts: Add token fields to StreamChunk, return in 'end'
- conversation.service.ts: Track tokens and message counts properly

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 17:08:23 -08:00
hailin 931055b51f feat(admin): add conversation management with device tracking display
## Backend (conversation-service)
- Add AdminConversationController with JWT auth for admin API
- Endpoints: list conversations, by user, detail, messages, statistics
- Support filtering by status, userId, date range, conversion
- Add JWT_SECRET environment variable to docker-compose.yml
- Add jsonwebtoken dependency for admin token verification

## Frontend (admin-client)
### New Features:
- Add conversations feature module with:
  - API layer (conversations.api.ts)
  - React Query hooks (useConversations.ts)
  - ConversationsPage with full management UI

### User Management Enhancement:
- Add "最近咨询记录" section in user detail drawer
- Display device info for each conversation:
  - IP address with region
  - User-Agent (parsed to browser/OS)
  - Device fingerprint
- Show conversation status, conversion status, message count

### Navigation:
- Add "对话管理" menu item with MessageOutlined icon
- Add /conversations route

## Files Added:
- admin-conversation.controller.ts (backend admin API)
- conversations feature folder (frontend)
  - infrastructure/conversations.api.ts
  - application/useConversations.ts
  - presentation/pages/ConversationsPage.tsx

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 10:04:17 -08:00
hailin 6a3a2130bf feat(conversation): add device tracking and optimize admin-client build
## Device Tracking (conversation-service)
- Add DeviceInfoDto class for validating device information
- Extract client IP from X-Forwarded-For and X-Real-IP headers
- Capture User-Agent header automatically on conversation creation
- Support optional fingerprint and region from client
- Pass deviceInfo through service layer to entity for persistence

Files changed:
- conversation.controller.ts: Add extractClientIp() method and header capture
- conversation.dto.ts: Add DeviceInfoDto with validation decorators
- conversation.service.ts: Update CreateConversationParams interface

## Build Optimization (admin-client)
- Implement code splitting via Rollup manualChunks
- Separate vendor libraries into cacheable chunks:
  - vendor-react: react, react-dom, react-router-dom (160KB)
  - vendor-antd: antd, @ant-design/icons (1013KB)
  - vendor-charts: recharts (409KB)
  - vendor-data: @tanstack/react-query, axios, zustand (82KB)
- Main bundle reduced from 1732KB to 61KB (96% reduction)
- Set chunkSizeWarningLimit to 1100KB for antd

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 09:45:47 -08:00
hailin 3be42c8224 fix(user-service): add jsonwebtoken dependency for admin auth
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 09:02:30 -08:00
hailin e0c2462017 feat(admin): add user management and system settings pages
Backend (user-service):
- Add admin user management APIs (list, search, statistics, detail)
- Add pagination and filtering support for user queries
- Add JWT token authentication for admin endpoints

Frontend (admin-client):
- Add UsersPage with user list, search, filters and statistics
- Add SettingsPage with admin profile, password change, system info
- Update App.tsx routes to use new pages

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 08:58:35 -08:00
hailin ed5dc49b4a fix(audit): reorder routes to fix NestJS route collision
Move specific routes (logs/actions, logs/entity-types) before
parameterized route (logs/:id) to prevent NestJS from matching
'actions' and 'entity-types' as UUID parameters.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 08:40:56 -08:00
hailin f95bc71254 fix(dashboard): remove failing evolution/health API calls
The useEvolutionStatistics and useSystemHealth hooks call endpoints that
depend on a non-existent knowledge-service internal API. Removed these
calls and the related UI sections to prevent 500 errors.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 08:33:08 -08:00
hailin ccb0648f6c fix(evolution): correct knowledge-service port from 3005 to 3003
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 08:24:57 -08:00
hailin 042d2e1456 feat(analytics): implement statistics, financial reports, and audit logging
Backend (evolution-service):
- Add analytics module with scheduled statistics aggregation
- Implement daily_statistics aggregation (OVERALL, CHANNEL, CATEGORY)
- Add monthly financial report generation and management
- Create audit log service for operation tracking
- Schedule cron jobs for automatic data aggregation

Frontend (admin-client):
- Replace dashboard mock data with real API calls
- Add analytics page with trend charts and dimension breakdown
- Add financial reports page with confirm/lock workflow
- Add audit logs page with filtering and detail view
- Update navigation with analytics submenu

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 08:01:39 -08:00
hailin 65c0bdd17c feat(payment): add transaction reliability and idempotency support
- Add TransactionService for atomic database operations with optimistic lock retry
- Implement pessimistic locking in payment callback handling to prevent race conditions
- Add idempotency check via transactionId unique index to prevent duplicate processing
- Add version columns to PaymentORM and OrderORM for optimistic locking
- Add composite indexes for performance (order_status, transaction_id)
- Optimize connection pool settings for both payment and conversation services
- Update init-db.sql with version columns and new indexes

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 07:00:01 -08:00
hailin fe37267c39 fix(orm): add explicit PostgreSQL column types for all ORM entities
- user-service: user.orm.ts (lastActiveAt), verification-code.orm.ts (all fields)
- file-service: file.orm.ts (userId, originalName, storagePath, mimeType)
- conversation-service: token-usage.orm.ts (model, all token/count fields)
- knowledge-service: knowledge-article.orm.ts, knowledge-chunk.orm.ts,
  system-experience.orm.ts, user-memory.orm.ts (all numeric, boolean, date fields)

This fixes DataTypeNotSupportedError where PostgreSQL rejects "Object" type
when @Column decorator lacks explicit type specification.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 02:14:07 -08:00
hailin a1f4f7ba0e fix(orm): add explicit types for all ConversationORM and MessageORM fields
Complete fix for all @Column decorators missing explicit type:

## ConversationORM
- status: varchar(20)
- category: varchar(50)
- messageCount: int
- userMessageCount: int
- assistantMessageCount: int
- totalInputTokens: int
- totalOutputTokens: int
- hasConverted: boolean
- consultingStage: varchar(30)
- conversionPath: varchar(30)

## MessageORM
- role: varchar(20)
- type: varchar(30)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 02:09:04 -08:00
hailin 7c22c173a5 fix(orm): add explicit column types for conversation and order entities
Fixed remaining TypeORM DataTypeNotSupportedError for "Object" type.

## conversation-service/ConversationORM
- title: varchar(255)
- endedAt: timestamptz

## payment-service/OrderORM
- serviceCategory: varchar(100)
- currency: varchar(10)
- paymentMethod: varchar(50)
- paidAt: timestamptz
- completedAt: timestamptz

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 01:57:20 -08:00
hailin eb1cb9c496 fix(orm): add explicit PostgreSQL column types for nullable fields
Fixed TypeORM DataTypeNotSupportedError for "Object" type in PostgreSQL.

## Issues Fixed

1. **user-service/UserORM**
   - fingerprint: varchar(255)
   - phone: varchar(20)
   - nickname: varchar(100)
   - avatar: varchar(500)

2. **payment-service/PaymentORM**
   - transactionId: varchar(255)
   - currency: varchar(10)
   - expiresAt: timestamptz
   - paidAt: timestamptz

3. **conversation-service/MessageORM**
   - inputTokens: int
   - outputTokens: int

## Root Cause
@Column({ nullable: true }) without explicit `type` defaults to Object,
which PostgreSQL doesn't support.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 22:50:54 -08:00
hailin afd707d15f refactor(services): implement 4-layer Clean Architecture for all backend services
Refactored all 6 backend services to 4-layer Clean Architecture pattern
following knowledge-service as reference implementation.

## Architecture Pattern (4-Layer)

```
src/
├── domain/              # Pure business entities and interfaces
│   ├── entities/        # Domain entities (no ORM decorators)
│   ├── repositories/    # Repository interfaces + Symbol tokens
│   └── value-objects/   # Enums and value types
├── application/
│   ├── dtos/            # Data transfer objects
│   └── services/        # Application services (use case orchestration)
├── adapters/
│   ├── inbound/         # Controllers, gateways (API endpoints)
│   └── outbound/
│       ├── persistence/ # Repository implementations
│       ├── clients/     # External service clients
│       └── storage/     # File storage adapters
└── infrastructure/
    └── database/postgres/
        └── entities/    # ORM entities with decorators
```

## Services Refactored

### user-service
- adapters/inbound: AuthController, UserController
- adapters/outbound/persistence: UserPostgresRepository, VerificationCodePostgresRepository
- application/services: AuthService, UserService
- application/dtos: AuthDto, UserDto

### payment-service
- adapters/inbound: OrderController, PaymentController
- adapters/outbound/persistence: OrderPostgresRepository, PaymentPostgresRepository
- adapters/outbound/payment-methods: AlipayAdapter, WechatPayAdapter, StripeAdapter
- application/services: OrderService, PaymentService
- application/dtos: OrderDto, PaymentDto

### file-service
- adapters/inbound: FileController
- adapters/outbound/persistence: FilePostgresRepository
- adapters/outbound/storage: MinioStorageAdapter
- application/services: FileService
- application/dtos: UploadFileDto

### conversation-service
- adapters/inbound: ConversationController, InternalController, ConversationGateway
- adapters/outbound/persistence: ConversationPostgresRepository, MessagePostgresRepository, TokenUsagePostgresRepository
- application/services: ConversationService
- application/dtos: ConversationDto

### knowledge-service
- adapters/inbound: KnowledgeController, MemoryController, InternalMemoryController
- adapters/outbound/persistence: KnowledgePostgresRepository, MemoryPostgresRepository
- application/services: KnowledgeService, MemoryService
- application/dtos: KnowledgeDto, MemoryDto

### evolution-service
- domain/entities: AdminEntity
- domain/repositories: IAdminRepository (Symbol-based DI)
- domain/value-objects: AdminRole enum
- adapters/inbound: AdminController, EvolutionController
- adapters/outbound/persistence: AdminPostgresRepository
- adapters/outbound/clients: ConversationClient, KnowledgeClient
- application/services: AdminService, EvolutionService
- application/dtos: AdminDto, EvolutionDto
- infrastructure/database/postgres/entities: AdminORM

## Key Improvements
- Symbol-based dependency injection for repository interfaces
- ORM entities separated from domain entities
- Consistent 4-layer structure across all services
- DTOs for API contracts
- Clear separation: domain logic vs infrastructure concerns

## Configuration
- Updated turbo.json: renamed "pipeline" to "tasks" for Turbo 2.0+

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 22:18:22 -08:00
hailin 9e1dca25f2 refactor(admin-client): implement 3-layer Clean Architecture for frontend
Refactored admin-client from 1.5-layer to 3-layer architecture using
Feature-Sliced Design pattern with Zustand + TanStack Query.

## Architecture Pattern

Each feature now follows 3-layer structure:
```
features/{feature}/
├── presentation/   # React UI components, pages
├── application/    # Zustand stores, TanStack Query hooks
└── infrastructure/ # API clients (axios calls)
```

## Changes by Feature

### Auth Feature
- infrastructure/auth.api.ts: Login, verify API calls
- application/useAuthStore.ts: Zustand store for auth state
- Updated LoginPage.tsx to use useAuthStore
- shared/hooks/useAuth.ts: Re-exports for backward compatibility

### Knowledge Feature
- infrastructure/knowledge.api.ts: Article CRUD APIs
- application/useKnowledge.ts: TanStack Query hooks
  - useKnowledgeArticles, useCreateArticle, useUpdateArticle
  - useDeleteArticle, usePublishArticle, useUnpublishArticle
- Updated KnowledgePage.tsx to use application hooks

### Experience Feature
- infrastructure/experience.api.ts: Experience management APIs
- application/useExperience.ts: TanStack Query hooks
  - usePendingExperiences, useExperienceStatistics
  - useApproveExperience, useRejectExperience, useRunEvolution
- Updated ExperiencePage.tsx to use application hooks

### Dashboard Feature
- infrastructure/dashboard.api.ts: Statistics APIs
- application/useDashboard.ts: TanStack Query hooks
  - useEvolutionStatistics, useSystemHealth
- Updated DashboardPage.tsx to use application hooks

## Benefits
- Clear separation of concerns (UI / business logic / data access)
- Better testability (each layer can be tested independently)
- Reusable hooks across components
- Type-safe API interfaces
- Centralized API error handling

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 22:17:48 -08:00
hailin 02954f56db refactor(services): implement Clean Architecture across 4 services
## Overview
Refactor user-service, payment-service, file-service, and conversation-service
to follow Clean Architecture pattern based on knowledge-service reference.

## Architecture Pattern Applied

```
src/
├── domain/
│   ├── entities/           # Pure domain entities (no ORM decorators)
│   └── repositories/       # Repository interfaces + Symbol DI tokens
├── infrastructure/
│   └── database/postgres/
│       ├── entities/       # ORM entities with TypeORM decorators
│       └── *-postgres.repository.ts  # Repository implementations
└── {feature}/
    └── {feature}.module.ts # DI configuration with Symbol providers
```

## Changes by Service

### user-service (40% → 100% compliant)
- Created: IUserRepository, IVerificationCodeRepository interfaces
- Created: UserORM, VerificationCodeORM entities
- Created: UserPostgresRepository, VerificationCodePostgresRepository
- Modified: UserEntity, VerificationCodeEntity → pure domain with factory methods
- Updated: user.module.ts, auth.module.ts with Symbol-based DI

### payment-service (50% → 100% compliant)
- Created: IOrderRepository, IPaymentRepository interfaces
- Created: OrderORM, PaymentORM entities
- Created: OrderPostgresRepository, PaymentPostgresRepository
- Modified: OrderEntity, PaymentEntity → pure domain with factory methods
- Updated: order.module.ts, payment.module.ts with Symbol-based DI

### file-service (40% → 100% compliant)
- Created: IFileRepository interface
- Created: FileORM entity
- Created: FilePostgresRepository
- Modified: FileEntity → pure domain with factory methods
- Updated: file.module.ts with Symbol-based DI

### conversation-service (60% → 100% compliant)
- Created: IConversationRepository, IMessageRepository, ITokenUsageRepository
- Created: ConversationORM, MessageORM, TokenUsageORM entities
- Created: ConversationPostgresRepository, MessagePostgresRepository,
          TokenUsagePostgresRepository
- Modified: ConversationEntity, MessageEntity, TokenUsageEntity → pure domain
- Updated: conversation.module.ts with Symbol-based DI
- Updated: app.module.ts, data-source.ts entity patterns

## Key Implementation Details

1. **Symbol-based DI Pattern**:
   ```typescript
   export const USER_REPOSITORY = Symbol('IUserRepository');

   @Module({
     providers: [{ provide: USER_REPOSITORY, useClass: UserPostgresRepository }],
     exports: [UserService, USER_REPOSITORY],
   })
   ```

2. **Pure Domain Entities**: Factory methods `create()` and `fromPersistence()`
   for controlled instantiation without ORM decorators

3. **Repository Implementations**: Include `toORM()` and `toEntity()` conversion
   methods for anti-corruption layer between domain and infrastructure

4. **Entity Discovery**: Changed glob pattern from `*.entity` to `*.orm`
   in app.module.ts and data-source.ts files

## Breaking Changes
- None for API consumers
- Internal architecture restructuring only

## Testing
- All 4 services compile successfully with `pnpm build`
- Database schema compatibility verified (column mappings preserved)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 21:18:25 -08:00
hailin 3b6d178ef7 fix(evolution): add proper TypeScript types for API clients
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 20:22:42 -08:00
hailin c2b4fe19cc refactor(evolution): use knowledge-service API for system_experiences
Follow proper microservices architecture:
- knowledge-service owns system_experiences table
- evolution-service uses KnowledgeClient API to save experiences
- Deleted SystemExperienceORM from evolution-service
- Added internal API endpoints in knowledge-service
- Disabled synchronize in all services for safety

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 20:20:30 -08:00
hailin e1bcd0145e refactor(evolution): use API instead of shared database tables
Breaking change: evolution-service no longer directly accesses
conversations and messages tables.

Changes:
- Add internal API endpoints to conversation-service for service-to-service calls
- Create ConversationClient in evolution-service to call conversation-service API
- Remove ConversationORM and MessageORM from evolution-service
- Update evolution.service to use ConversationClient

This follows microservices best practices:
- Each service owns its data
- Services communicate via API, not shared tables

TODO: Apply same pattern to system_experiences (knowledge-service)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 19:48:09 -08:00
hailin 2c1edc26af fix(conversation): disable synchronize in production
Use init-db.sql for schema management instead of TypeORM auto-sync.
synchronize:true is dangerous in production and causes conflicts
when multiple services share tables.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 19:41:44 -08:00
hailin 6718fdc9e3 fix(conversation): add indexes and fix column types to match database
- Add @Index decorators for conversation_id, created_at, role
- Set created_at to timestamptz type to match database
- Set columns nullable to match database schema

This prevents synchronize:true from trying to modify columns
that have dependent indexes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 19:41:19 -08:00
hailin a821df8dc1 fix(conversation): add missing token columns to MessageEntity
Add input_tokens and output_tokens columns that evolution-service
defines to prevent synchronize:true from trying to drop them.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 19:05:44 -08:00
hailin a84030be84 fix(conversation): add missing columns to match evolution-service schema
Add columns that evolution-service defines to prevent synchronize:true
from trying to drop them:
- userMessageCount, assistantMessageCount
- totalInputTokens, totalOutputTokens
- rating, feedback, hasConverted

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 19:01:16 -08:00
hailin 7d9b87ef3c fix(conversation): use synchronize:true for 100% reliable schema sync
- Remove migration-based approach which kept failing
- Enable synchronize:true to auto-sync Entity with database
- Schema will always match Entity definition on startup

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 08:13:41 -08:00
hailin fc6078e4f8 fix(conversation): enable auto-migration on app startup
- Add migrations path to TypeORM config
- Set migrationsRun: true to run pending migrations on startup
- This ensures V2 columns are created automatically when app starts

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 08:12:50 -08:00
hailin b70035ad2f fix(migration): add production migration support for Docker
- Add data-source.prod.ts for compiled JS migrations
- Add migration:run:prod script to package.json
- Update deploy.sh to try prod migration first, fallback to dev
- Keep SQL fallback in full-reset as safety net with proper indexes

This ensures migrations work in Docker where ts-node may not be available.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 08:05:14 -08:00
hailin 9f2bdee8d9 feat(conversation): integrate ClaudeAgentServiceV2 for consulting workflow
- Switch ConversationService to use ClaudeAgentServiceV2
- Pass consultingState and deviceInfo from conversation to context
- Handle state_update chunks and save updated state to database
- Move dotenv to dependencies for migration runtime

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 06:43:19 -08:00
hailin c0a9710943 feat(conversation): add TypeORM migration scripts and data-source config
- Add migration:run, migration:revert, migration:generate scripts
- Create data-source.ts for TypeORM CLI
- Add dotenv, ts-node, tsconfig-paths dependencies

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 06:38:41 -08:00
hailin cd5399eac3 feat(agent): implement consulting strategy engine with V2 agent service
- Add 8-stage consulting workflow (greeting → handoff)
- Create StrategyEngineService for state management and transitions
- Add ClaudeAgentServiceV2 with integrated strategy guidance
- Support old user recognition via get_user_context tool
- Add device info (IP, fingerprint) for new user icebreaking
- Extend ConversationEntity with consulting state fields
- Add database migration for new JSONB columns

Stages: greeting, needs_discovery, info_collection, assessment,
recommendation, objection_handling, conversion, handoff

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 06:32:07 -08:00
hailin 8352578bd3 fix(conversation): add explicit varchar type for intentType column
TypeORM requires explicit type for nullable string columns

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 08:30:03 -08:00
hailin dd66c3a892 fix(conversation): resolve TypeScript type errors in token tracking
- Fix Usage type cast by using unknown intermediate type
- Add PricingTier interface and proper Record type for PRICING

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 08:27:03 -08:00
hailin 849a4a3099 feat(conversation): add token usage tracking for API cost analysis
- Add TokenUsageEntity to store per-request token consumption
- Add TokenUsageService with cost calculation and statistics APIs
  - Record input/output/cache tokens per API call
  - Calculate estimated cost based on Claude pricing
  - Provide user/conversation/global stats aggregation
  - Support daily stats and top users ranking
- Integrate token tracking in ClaudeAgentService
  - Track latency, tool calls, response length
  - Accumulate tokens across tool loop iterations
- Add token_usages table to init-db.sql with proper indexes

This enables:
- Per-user token consumption tracking
- Cost analysis and optimization
- Future billing/quota features

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 08:23:58 -08:00
hailin c768e2aa53 fix(agent): stricter max_tokens calculation for response length control
- Reduce tokensPerChar from 2 to 1.8 for more accurate Chinese token estimation
- Use min() instead of max() to enforce upper limits on token counts
- CHAT: max 200 tokens (was min 256)
- SIMPLE_QUERY: max 600 tokens (was min 512)
- CLARIFICATION: max 300 tokens (was min 256)
- CONFIRMATION: max 400 tokens (was min 384)
- DEEP_CONSULTATION: 800-1600 tokens (was 1024-4096)
- ACTION_NEEDED: 500-1000 tokens (was 768-2048)

This should result in more concise AI responses that better match
the intent classifier's suggested length limits.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 08:02:34 -08:00
hailin d9b4c72894 feat(agent): implement 3-layer architecture for better response quality
Implement a three-layer architecture to improve AI response quality:

Layer 1 - Intent Classifier (intent-classifier.ts):
- Classifies user intent into 6 types: SIMPLE_QUERY, DEEP_CONSULTATION,
  ACTION_NEEDED, CHAT, CLARIFICATION, CONFIRMATION
- Determines suggested response length based on intent type
- Detects follow-up questions and extracts entities (visa types, etc.)
- Uses keyword matching for fast classification (no API calls)

Layer 2 - ReAct Agent (system-prompt.ts):
- Adds ReAct thinking framework to system prompt
- 4-step process: Understand -> Evaluate -> Act -> Generate
- Emphasizes concise responses, avoids redundant phrases
- Injects intent classification results to guide response strategy

Layer 3 - Response Gate (response-gate.ts):
- Quality checks: length, relevance, redundancy, completeness, tone
- Logs gate results for analysis and future optimization
- Can trim responses and remove redundant expressions

Integration (claude-agent.service.ts):
- Integrates all 3 layers in sendMessage flow
- Dynamically adjusts max_tokens based on intent type
- Collects full response for gate analysis

Documentation:
- Added AGENT_THREE_LAYER_ARCHITECTURE.md with detailed design docs

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 07:51:19 -08:00
hailin ad0f904f98 fix(knowledge): add pgvector transformer for TypeORM embedding columns
TypeORM doesn't natively support pgvector type. Add custom transformer
to convert between JavaScript arrays and pgvector string format [1,2,3].

Fixes: invalid input syntax for type vector errors

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 07:12:28 -08:00
hailin 91f8792110 feat(embedding): add OpenAI proxy support for IP-based URLs
- Add OPENAI_BASE_URL configuration to .env.example
- Update EmbeddingService to disable TLS verification for IP-based proxy URLs
- Mirror the same proxy handling pattern used in Anthropic API

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 05:42:22 -08:00
hailin 10a2449d05 fix(conversation): use VARCHAR instead of enum for consistency with init-db.sql
- Change MessageEntity.role from enum to VARCHAR(20)
- Change MessageEntity.type from enum to VARCHAR(30)
- Change ConversationEntity.status from enum to VARCHAR(20)
- Add nullable: true to userId to match database schema
- Add length constraints to match database schema
- Convert enums to const objects with type exports for type safety

This ensures TypeORM entities match the database schema exactly,
avoiding potential issues with enum type creation in production.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 05:06:36 -08:00
hailin 0f56cea96a fix(schema): sync ORM entities with database schema
- Add missing 'type' column to MessageORM entity
- Add 'TEXT_WITH_ATTACHMENTS' to messages.type CHECK constraint
  (matches MessageType enum in conversation-service)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 05:00:38 -08:00
hailin 2069a3cf0a fix(evolution): resolve pgvector type conflict in SystemExperienceORM
The embedding column was declared as float[] but the database uses
VECTOR(1536) from pgvector. TypeORM doesn't natively support pgvector
types, causing 500 errors when querying the system_experiences table.

Fixed by:
- Changed column type to 'text' with select: false
- This prevents TypeORM from trying to select/map the vector column
- The embedding field is only used for similarity searches via raw SQL

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 04:46:18 -08:00
hailin 4c125f3276 feat(agent): add 4 real-time tools for enhanced agent capabilities
Add the following real-time tools to ImmigrationToolsService:
- get_current_datetime: Get current date/time with timezone support
- web_search: Search internet for latest immigration news/policies (Google CSE)
- get_exchange_rate: Query real-time currency exchange rates (for investment immigration)
- fetch_immigration_news: Fetch latest immigration announcements

All tools include graceful degradation with fallback responses when external APIs are unavailable.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 00:46:55 -08:00