Phase 5-6: API usage tracking + Calendar/Mail/DEVONthink/OmniFocus/News pipeline

- 파이프라인 42→51노드 확장 (calendar/mail/note 핸들러 추가)
- 네이티브 서비스 6개: heic_converter(:8090), chat_bridge(:8091),
  caldav_bridge(:8092), devonthink_bridge(:8093), inbox_processor, news_digest
- 분류기 v2→v3: calendar, reminder, mail, note intent 추가
- Mail Processing Pipeline (7노드, IMAP 폴링)
- LaunchAgent plist 6개 + manage_services.sh
- migrate-v3.sql: news_digest_log + calendar_events 확장
- 개발 문서 현행화 (CLAUDE.md, QUICK_REFERENCE.md, docs/architecture.md)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hyungi
2026-03-14 17:09:04 +09:00
parent f7cccc9c5e
commit 612933c2d3
23 changed files with 2492 additions and 67 deletions

View File

@@ -10,7 +10,7 @@
│ Outgoing Webhook
┌────────────────────────────────────────────────────────────┐
│ bot-n8n (맥미니 Docker :5678) — 37노드 파이프라인 │
│ bot-n8n (맥미니 Docker :5678) — 51노드 파이프라인 │
│ │
│ ⓪ 토큰 검증 + Rate Limit (username별 10초/5건) │
│ │
@@ -20,10 +20,19 @@
│ ② 명령어 체크 (/설정, /모델, /성격, /문서등록, /보고서) │
│ └─ 권한 체크 (ADMIN_USERNAMES allowlist) │
│ │
│ ③ GPU Qwen 9B 분류 v2 (10초 타임아웃) │
│ ③ GPU Qwen 9B 분류 v3 (10초 타임아웃) │
│ → {intent, response_tier, needs_rag, rag_target, ...} │
│ └─ 실패 시 fallback → api_light │
│ │
│ ③-1 Route by Intent │
│ ├─ log_event → Qwen 추출 → bge-m3 → tk_company 저장 │
│ ├─ report → 현장 리포트 → field_reports DB 저장 │
│ ├─ calendar → CalDAV Bridge → Synology Calendar │
│ ├─ reminder → calendar로 통합 처리 │
│ ├─ mail → 메일 요약 조회 (mail_logs) │
│ ├─ note → DEVONthink Bridge → 문서 저장 │
│ └─ fallback → 일반 대화 (RAG + 3단계 라우팅) │
│ │
│ ④ [needs_rag=true] 멀티-컬렉션 RAG 검색 │
│ documents + tk_company + chat_memory │
│ → bge-m3 임베딩 → Qdrant 검색 → reranker → top-3 │
@@ -34,7 +43,7 @@
│ └─ api_heavy → 예산 체크 → Claude Opus (or 다운그레이드) │
│ │
│ ⑥ 응답 전송 + chat_logs + api_usage_monthly │
│ ⑦ [비동기] Qwen 메모리 판단 → 가치 있으면 벡터화
│ ⑦ [비동기] Qwen 메모리 판단 → 가치 있으면 벡터화 + DEVONthink
│ └─ classification_logs 기록 │
└──┬──────────┬───────────┬───────────┬──────────────────────┘
│ │ │ │
@@ -47,6 +56,46 @@
│ │ │tk_company││비전모델 │ │(분류+응답) │
│ │ │chat_memory│ │ │ │
└──────┘ └────────┘ └─────────┘ └──────────────┘
┌────────────────────────────────────────────────────────────┐
│ 별도 워크플로우 │
│ Mail Processing Pipeline (7노드) │
│ └─ MailPlus IMAP 폴링 → Qwen 분류 → mail_logs 저장 │
└────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────────────────┐
│ 네이티브 서비스 (맥미니) │
│ │
│ heic_converter.py (:8090) │
│ └─ HEIC→JPEG 변환 (macOS sips) │
│ │
│ chat_bridge.py (:8091) │
│ ├─ DSM Chat API 폴링 (5초) → 사진 감지 + ack │
│ ├─ POST /chat/recent-photo → 사진 다운+변환 │
│ └─ HEIC 자동 변환 (→ heic_converter :8090) │
│ │
│ caldav_bridge.py (:8092) │
│ └─ CalDAV REST 래퍼 (Synology Calendar CRUD) │
│ │
│ devonthink_bridge.py (:8093) │
│ └─ DEVONthink AppleScript 래퍼 (문서 저장·검색) │
│ │
│ inbox_processor.py (LaunchAgent, 5분) │
│ └─ OmniFocus Inbox 폴링 → Qwen 분류 → 자동 정리 │
│ │
│ news_digest.py (LaunchAgent, 매일 07:00) │
│ └─ RSS 뉴스 수집 → Qwen 번역·요약 → Qdrant + Synology Chat│
└────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────┐
│ NAS 서비스 (192.168.1.227) │
│ Synology Chat / Calendar (CalDAV) / MailPlus │
└────────────────────────────────────────────────┘
┌────────────────────────────────────────────────┐
│ DEVONthink 4 (맥미니) │
│ AppleScript 경유 문서 저장·검색 │
└────────────────────────────────────────────────┘
```
## 3단계 라우팅 상세
@@ -64,11 +113,11 @@
- ~50% → Haiku (저비용)
- ~10% → Opus (복잡한 질문만)
### 분류기 v2 출력 스키마
### 분류기 v3 출력 스키마
```json
{
"intent": "greeting|question|calendar|reminder|mail|photo|command|report|other",
"intent": "greeting|question|log_event|calendar|reminder|mail|note|photo|command|report|other",
"response_tier": "local|api_light|api_heavy",
"needs_rag": true,
"rag_target": ["documents", "tk_company", "chat_memory"],
@@ -78,6 +127,13 @@
}
```
**Intent별 처리:**
- `calendar` — CalDAV Bridge로 일정 생성/조회 (Synology Calendar)
- `reminder` — calendar로 통합 (알림 시간 포함 일정 생성)
- `mail` — mail_logs에서 최근 메일 요약 조회
- `note` — DEVONthink Bridge로 문서 저장
```
### 프리필터 → 분류기 → 모델 라우팅 흐름
```
@@ -199,6 +255,19 @@ Should Memorize?
-- api_usage_monthly: API 사용량 + 예산 상한
-- year + month + tier UNIQUE
-- estimated_cost vs budget_limit 비교 → 다운그레이드
-- news_digest_log: 뉴스 요약 이력
-- source_url UNIQUE, translated_title, summary
-- published_at, processed_at, qdrant_point_id
```
### calendar_events 확장 (v3)
```sql
-- v3에서 추가된 컬럼:
-- caldav_uid: CalDAV 이벤트 UID (Synology Calendar 연동)
-- description: 이벤트 상세 설명
-- created_by: 생성 출처 (chat, caldav_sync, manual)
```
### SLA 기준표
@@ -232,18 +301,26 @@ Should Memorize?
- **분류기 fallback**: Qwen 10초 타임아웃 → {response_tier: "api_light"}
- **리랭커 fallback**: bge-reranker 실패 → Qdrant score 정렬
- **비전 모델 fallback**: 사진 분석 실패 → 사용자 설명만으로 구조화
- **HEIC 자동 변환**: macOS sips 기반 HEIC→JPEG 변환 (heic_converter.py, port 8090)
- **사진 브릿지**: chat_bridge.py가 DSM Chat API 폴링 → 사진 감지 → ack → n8n 요청 시 다운로드+변환+base64 반환
- **이미지 base64 변환**: Ollama API는 URL 미지원, chat_bridge가 자동 다운로드 후 base64 전달
## 메인 채팅 파이프라인 v2 (37노드)
## 메인 채팅 파이프라인 v3 (51노드)
```
Webhook POST /chat
[Parse Input] — 토큰 검증 + Rate Limit
[Parse Input] — 토큰 검증 + Rate Limit + channelId/userId 추출
├─ rejected → [Reject Response] → Send + Respond
[Has Pending Doc?] — 문서 등록 대기
├─ pending → [Process Document] → [Log Doc Ingestion] → Send + Respond
[Regex Pre-filter] — 인사/감사 정규식
├─ match → [Pre-filter Response] → Send + Respond
@@ -255,10 +332,19 @@ Webhook POST /chat
│ ├─ DB 필요 → [Command DB Query] → [Format] → Send + Respond
│ └─ 직접 → [Direct Response] → Send + Respond
└─ false → [Qwen Classify v2] (10초 타임아웃)
└─ false → [Qwen Classify v3] (10초 타임아웃)
├─ [Log Classification] (비동기, PostgreSQL)
├─ [Route by Intent]
│ ├─ log_event → [Handle Log Event] (Qwen 추출→임베딩→tk_company 저장→확인응답)
│ ├─ report → [Handle Field Report] → [Save Field Report DB]
│ ├─ calendar → [Handle Calendar] → CalDAV Bridge → 확인응답
│ ├─ reminder → [Handle Calendar] (calendar로 통합)
│ ├─ mail → [Handle Mail] → mail_logs 조회 → 요약응답
│ ├─ note → [Handle Note] → DEVONthink Bridge → 확인응답
│ └─ fallback → [Needs RAG?]
├─ needs_rag=true
│ → [Get Embedding] → [Multi-Collection Search]
│ → [Build RAG Context] (출처 표시)
@@ -278,7 +364,7 @@ Webhook POST /chat
▼ [비동기]
[Memorization Check] → [Should Memorize?]
├─ true → [Embed & Save Memory]
├─ true → [Embed & Save Memory] + [DEVONthink 저장]
└─ false → (끝)
```
@@ -315,17 +401,31 @@ Webhook POST /chat
간결하게 답하고, 모르면 솔직히 말하세요. 이모지는 핵심에만.
```
## 향후 기능 (Phase 4-6)
## 구현 완료
### Phase 4: 회사 문서 등록
- `/문서등록 [부서] [유형] [제목]` → 청킹 → tk_company 저장
- hash 중복 체크, 문서 버전 관리
### Phase 5: 현장 리포팅
- 사진 + 텍스트 → 비전 모델 → 구조화 → field_reports + tk_company
- `/보고서 [영역] [년월]` → 월간 보고서 생성
- SLA 트래킹 + 긴급 에스컬레이션
### Phase 5: 현장 리포팅 + API 사용량 추적
- 사진 + 텍스트 → Claude Haiku Vision → 구조화 → field_reports + tk_company
- `/보고서 [영역] [년월]` → 월간 보고서 생성 (report_cache)
- API 사용량 추적 (api_usage_monthly UPSERT)
- HEIC→JPEG 변환 (heic_converter.py) + chat_bridge.py (DSM Chat API 브릿지)
### Phase 6: 메일 + 캘린더
- IMAP 폴링 → Qwen 분석 → mail_logs + Qdrant
- CalDAV 연동 → calendar_events
### Phase 6: 캘린더·메일·DEVONthink·OmniFocus·뉴스
- 분류기 v3: calendar, reminder, mail, note intent 추가
- caldav_bridge.py: CalDAV REST 래퍼 (Synology Calendar CRUD)
- devonthink_bridge.py: DEVONthink AppleScript 래퍼
- inbox_processor.py: OmniFocus Inbox 폴링 (LaunchAgent, 5분)
- news_digest.py: 뉴스 번역·요약 (LaunchAgent, 매일 07:00)
- Mail Processing Pipeline (7노드): IMAP 폴링 → 분류 → mail_logs
- 51노드 파이프라인: calendar/mail/note 핸들러 추가
## 향후 기능 (Phase 7+)
- SLA 트래킹 스케줄 워크플로우 + 긴급 에스컬레이션
- CalDAV 양방향 동기화 (Synology Calendar → bot-postgres)
- 메일 발송 (SMTP via MailPlus)
- reminder 실구현 (알림 시간에 Synology Chat 푸시)
- DEVONthink 검색 결과 RAG 연동