# InsuRo Edge Function → 아누 서버 전환 — 텍스트 AI 4건

## 작업 레벨: Lv.3

## 프로젝트 시스템 3문서
- DevSystem: `/home/jay/workspace/memory/plans/anu-guide-system/plan.md`

## 프로젝트
- InsuRo: `/home/jay/projects/InsuRo`
- 서버: `/home/jay/projects/InsuRo/server`

## 배경
Supabase Edge Function에서 Gemini API를 호출하는 AI 기능 4건이 GOOGLE_AI_API_KEY 미설정으로 작동 안 됨. 아누 서버(claude CLI, Max200)로 전환.

## 전환 대상 4건

### 1. 성과 분석 (analyze-performance)
- Edge Function: `supabase/functions/analyze-performance/index.ts`
- 호출처: `src/components/AIUsageTab.tsx:121`
- 현재: `supabase.functions.invoke("analyze-performance")`
- AI: gemini-2.0-flash-lite로 콘텐츠 성과 분석
- 전환: 서버 `POST /api/insuro/analyze-performance` + claude CLI haiku

### 2. 고객 분석 (analyze-customer)
- Edge Function: `supabase/functions/analyze-customer/index.ts`
- 호출처: `src/pages/CrmCustomerDetail.tsx:262`
- 현재: `supabase.functions.invoke("analyze-customer", { body: { customerId } })`
- AI: gemini-2.0-flash-lite로 고객 데이터 분석/인사이트
- 전환: 서버 `POST /api/insuro/analyze-customer` + claude CLI haiku

### 3. 상담 평가 (evaluate-consultation)
- Edge Function: `supabase/functions/evaluate-consultation/index.ts`
- 호출처: `src/components/crm/ConsultationEval.tsx:39`
- 현재: `supabase.functions.invoke("evaluate-consultation", { body: { consultationId, content } })`
- AI: gemini-2.0-flash-lite로 상담 내용 평가/피드백
- 전환: 서버 `POST /api/insuro/evaluate-consultation` + claude CLI haiku

### 4. 통화 녹취록 (transcribe-call)
- Edge Function: `supabase/functions/transcribe-call/index.ts`
- 호출처: 확인 필요 (음성 녹음 후 호출)
- AI: gemini-2.0-flash-lite로 음성→텍스트 변환
- 전환: 서버 `POST /api/insuro/transcribe-call` + claude CLI haiku
- ★ 주의: 음성 파일 처리가 필요. claude CLI가 음성을 직접 처리 못하면 대안 검토 필요 (Whisper API 등)

## 구현 패턴 (4건 공통)

### 서버 엔드포인트 추가
```python
@app.post("/api/insuro/{기능명}")
@limiter.limit("10/minute")
async def {기능명}(request: Request, body: {Request모델}, payload: dict = Depends(verify_jwt)):
    prompt = f"... {body에서 추출한 데이터} ..."
    result = subprocess.run(
        ["claude", "-p", prompt, "--model", "haiku", "--output-format", "json"],
        capture_output=True, text=True, timeout=30, cwd="/tmp",
    )
    return json.loads(result.stdout)
```

### 프론트엔드 호출 변경
```tsx
// 기존
const { data } = await supabase.functions.invoke("기능명", { body: {...} });

// 변경
const res = await fetch(`${INSURO_API_BASE}/api/insuro/기능명`, {
  method: "POST",
  headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}` },
  body: JSON.stringify({...}),
});
const data = await res.json();
```

## affected_files
- `server/main.py` (수정 — 4개 엔드포인트 추가)
- `src/components/AIUsageTab.tsx` (수정 — analyze-performance 호출)
- `src/pages/CrmCustomerDetail.tsx` (수정 — analyze-customer 호출)
- `src/components/crm/ConsultationEval.tsx` (수정 — evaluate-consultation 호출)
- 통화 녹취록 호출처 (확인 후 수정)

## 검증 시나리오
1. 각 기능 호출 → Edge Function 대신 아누 서버에서 처리
2. claude CLI haiku로 AI 응답 정상 반환
3. 기존 프론트엔드 UI 동일하게 작동
4. pytest 전량 통과
5. npm run build 성공
6. 서버 재시작 정상