# g

### **📋 สิ่งที่ต้องการจากหลังบ้าน (Spec สำหรับทีม Supabase)**

#### **API Keys ที่ต้องตั้งค่าบน MCP/RAG Server**

| Key | ใช้ทำอะไร | ตั้งที่ไหน |
| --- | --- | --- |
| `GEMINI_API_KEY` | Gemini Embedding 2 (MRL 768d) สำหรับ embed ข้อความ | `rag.gtsalphamcp.com` server env |
| `ANTHROPIC_API_KEY` หรือ `OPENAI_API_KEY` | LLM สำหรับ classify, summarize, extract nuggets | `rag.gtsalphamcp.com` server env |

#### **Endpoints ใหม่ที่ต้องสร้างบน** `rag.gtsalphamcp.com`

**1\.** `POST /memory/ingest` — เก็บความจำจากการสนทนา

```plaintext
Input:  { userId, sessionId, messages: [{ role, content }] }
Logic:
  1. Classify แต่ละข้อความ → transient ("ขอบคุณ", "โอเค", "555") ข้าม / valuable สกัด
  2. Extract nuggets (facts, preferences, decisions)
  3. Embed nugget ผ่าน Gemini Embedding 2 (dimension 768 ← MRL)
  4. Store ใน KV: mem:{userId}:nug:{uuid} → { text, embedding[768], type, source_sid, created_at, expires_at }
  5. Update identity ถ้าเจอข้อมูลตัวตน: mem:{userId}:identity
Output: { nuggets_created: N, identity_updated: bool }
```

**2\.** `POST /memory/recall` — ดึงความจำที่เกี่ยวข้อง

```plaintext
Input:  { userId, query, limit: 5 }
Logic:
  1. Embed query ผ่าน Gemini Embedding 2 (768d)
  2. ดึง kv.getByPrefix("mem:{userId}:nug:")
  3. Cosine similarity → top-K
  4. ดึง identity: mem:{userId}:identity
Output: { memories: [{ text, type, score, created_at }], identity: string|null }
```

**3\.** `POST /memory/summarize` — สรุปย่อ session

```plaintext
Input:  { userId, sessionId, messages: [...last 15 msgs] }
Logic:
  1. LLM สรุป → 1 ย่อหน้า
  2. Store: sum:{sessionId}:chunk_{N} → { summary, created_at, msg_count }
  3. Optional: ลบ raw messages เก่ากว่า 7 วัน
Output: { summary: string, chunk_id: string }
```

**4\.** `GET /memory/identity/:userId` — ดึงข้อมูลตัวตนผู้ใช้

```plaintext
Output: { identity: string } // e.g. "ชื่อ X, ชอบ Dark theme, ใช้ MCP เป็นหลัก, พูดไทย"
```

#### **Edge Function Proxy Routes ใหม่ (เพิ่มใน** `index.tsx`**)**

```plaintext
POST /make-server-*/memory/ingest    → proxy → rag.gtsalphamcp.com/memory/ingest
POST /make-server-*/memory/recall    → proxy → rag.gtsalphamcp.com/memory/recall
POST /make-server-*/memory/summarize → proxy → rag.gtsalphamcp.com/memory/summarize
GET  /make-server-*/memory/identity  → proxy → rag.gtsalphamcp.com/memory/identity/{userId}
```

#### **Modified Chat Flow (เมื่อหลังบ้านพร้อม)**

```plaintext
User sends message
  │
  ├─ 1. /memory/recall(userId, query)  ← ดึง relevant memories + identity
  │
  ├─ 2. ประกอบ prompt:
  │     [System] + [Identity] + [Top-3 Memories]
  │     + [Session Summary] + [Recent 3-5 msgs] + [Query]
  │
  ├─ 3. ส่ง LLM → stream response
  │
  └─ 4. Background: /memory/ingest(userId, sessionId, [userMsg, aiMsg])
         ↓ ทุก 15 ข้อความ → /memory/summarize
```

#### **KV Key Pattern (ทั้งหมดใช้ KV Store เดิม)**

| Key Pattern | เก็บอะไร | TTL |
| --- | --- | --- |
| `mem:{uid}:identity` | ข้อมูลตัวตน/ความชอบ | ถาวร |
| `mem:{uid}:nug:{uuid}` | Memory nugget + embedding 768d | 90 วัน |
| `sum:{sid}:chunk_{n}` | สรุปย่อทุก 15 ข้อความ | 30 วัน |
| `chatmsg:{uid}:{sid}:{id}` | ข้อความดิบ (มีอยู่แล้ว) | 7 วัน |
| `providerkeys:{uid}` | API Keys จาก UI popup (encrypted) | ถาวร |

#### **RLS / Security**

*   ทุก endpoint ตรวจ `Authorization` header → `getUser()` หรือ `getUserOrAgent()`
    
*   KV key ทุกตัวมี `{userId}` prefix → ผู้ใช้ A ไม่เห็นข้อมูลผู้ใช้ B
    
*   API Keys จาก popup เก็บ encrypted ใน KV ไม่ส่งกลับ frontend เป็น plaintext
    
*   A2A agents เข้าถึง memory ได้ผ่าน `ApiKey gts_live_*` (scopes: `memory:read`)
    

* * *

### **<mark class="bg-yellow-200 dark:bg-yellow-500/30">⚡️ สรุปลำดับงาน</mark>**

| <mark class="bg-yellow-200 dark:bg-yellow-500/30">ขั้น</mark> | <mark class="bg-yellow-200 dark:bg-yellow-500/30">ใครทำ</mark> | <mark class="bg-yellow-200 dark:bg-yellow-500/30">อะไร</mark> |
| --- | --- | --- |
| **<mark class="bg-yellow-200 dark:bg-yellow-500/30">1</mark>** | **<mark class="bg-yellow-200 dark:bg-yellow-500/30">ผม (Figma Make)</mark>** | <mark class="bg-yellow-200 dark:bg-yellow-500/30">สร้าง "AI Providers" popup/tab ใน Settings + frontend hooks ใน api.ts</mark> |
| **<mark class="bg-yellow-200 dark:bg-yellow-500/30">2</mark>** | **<mark class="bg-yellow-200 dark:bg-yellow-500/30">ทีม Supabase</mark>** | <mark class="bg-yellow-200 dark:bg-yellow-500/30">Implement 4 endpoints บน rag.gtsalphamcp.com ตาม spec ด้านบน</mark> |
| **<mark class="bg-yellow-200 dark:bg-yellow-500/30">3</mark>** | **<mark class="bg-yellow-200 dark:bg-yellow-500/30">ทีม Supabase</mark>** | <mark class="bg-yellow-200 dark:bg-yellow-500/30">เพิ่ม proxy routes ใน Edge Function index.tsx</mark> |
| **<mark class="bg-yellow-200 dark:bg-yellow-500/30">4</mark>** | **<mark class="bg-yellow-200 dark:bg-yellow-500/30">ผม (Figma Make)</mark>** | <mark class="bg-yellow-200 dark:bg-yellow-500/30">เชื่อม ChatPage → memory recall/ingest เมื่อหลังบ้านพร้อม</mark> |

* * *

### **❓ ยืนยัน 3 ข้อ**

1.  **Spec ด้านบนตรงกับที่ต้องการหรือไม่?** ปรับอะไรบอกได้
    
2.  **Popup ใส่ API Keys** — ทำเป็น tab ใหม่ "Providers" ใน SettingsPanel เลยดีไหม? หรืออยากเป็น modal แยก?
    
3.  **พร้อมให้ผมเริ่มทำ ขั้นที่ 1** (popup + frontend hooks) ได้เลยหรือยัง?
