# รูปแบบการเริ่ม Session ทุก LLM agent session ควรทำตามลำดับ startup แบบ canonical นี้ การข้ามขั้นตอนนำไปสู่ context สูญหาย, ข้อความที่พลาด และ task ที่ลืม ## รูปแบบ ``` 1. Recall memory ทั้งหมด 2. Poll ข้อความแช็ตที่ยังไม่ได้อ่าน 3. ตรวจ task ที่กำลังดำเนินการ 4. สร้าง context จากผลลัพธ์ 5. ประมวลผลรายการที่ค้างก่อนงานใหม่ ``` ## การ implement ### ขั้นตอนที่ 1: Recall Memory ทั้งหมด > [!CRITICAL] > นี่เป็นการเรียกที่สำคัญที่สุด หากไม่มี คุณไม่มี memory ของ session ที่ผ่านมา ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/memory/recall ``` ส่งกลับสรุปแบบ plain-text ของ memory ทั้งหมด เรียงตาม priority ### ขั้นตอนที่ 2: Poll ข้อความแช็ตที่ยังไม่ได้อ่าน ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/chat/poll ``` ส่งกลับข้อความที่ยังไม่ได้อ่านจาก human **ทำเครื่องหมายว่าอ่านแล้วโดยอัตโนมัติ** ### ขั้นตอนที่ 3: ตรวจ Task ที่กำลังดำเนินการ ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ "https://synapse.schaefer.zone/mind/tasks?status=in_progress" ``` ส่งกลับ task ที่คุณทำอยู่ session ที่แล้ว ### ขั้นตอนที่ 4: สร้าง Context รวม response ทั้งสามเข้าใน system prompt ของคุณ: ```python def build_context(memories, messages, tasks): context = f"""# SESSION CONTEXT ## Memories (from previous sessions) {memories} ## Unread Messages from Human {format_messages(messages)} ## Active Tasks {format_tasks(tasks)} ## Instructions - Address unread messages first - Resume active tasks before starting new work - Store new learnings as they happen (POST /memory) - Poll for new messages every 30-60 seconds """ return context ``` ### ขั้นตอนที่ 5: ประมวลผลรายการที่ค้าง ``` For each unread message: - Acknowledge receipt (POST /chat/reply) - Address the message content - Store any new commitments as memories For each in-progress task: - Recall why you were working on it - Continue from where you left off - Update task status as you progress ``` ## ตัวอย่างสมบูรณ์ ```python import os import requests URL = "https://synapse.schaefer.zone" KEY = os.environ["SYNAPSE_MIND_KEY"] def session_start(): """Canonical session start sequence.""" headers = {"Authorization": f"Bearer {KEY}"} # 1. Recall memories r = requests.get(f"{URL}/memory/recall", headers=headers) memories = r.text # 2. Poll chat r = requests.get(f"{URL}/chat/poll", headers=headers) messages = r.json().get("messages", []) # 3. Check tasks r = requests.get(f"{URL}/mind/tasks?status=in_progress", headers=headers) tasks = r.json().get("tasks", []) # 4. Build context context = f"""You are a Synapse-enabled AI assistant. MEMORIES FROM PREVIOUS SESSIONS: {memories} UNREAD MESSAGES FROM HUMAN: {chr(10).join(f'- {m["content"]}' for m in messages) or 'None'} ACTIVE TASKS: {chr(10).join(f'- [{t["id"]}] {t["title"]}: {t.get("description", "")}' for t in tasks) or 'None'} INSTRUCTIONS: 1. Acknowledge each unread message 2. Resume active tasks 3. Store new learnings via POST /memory 4. Poll /chat/poll every 30-60 seconds """ return context # At session start system_prompt = session_start() # Pass to LLM... ``` ## ความผิดพลาดทั่วไป > [!WARNING] > - **ข้าม recall** — คุณเริ่มโดยไม่มี context, ทำ mistake ซ้ำ > - **ลืม poll แช็ต** — ข้อความของ human ไม่ได้รับการตอบ > - **ละเลย task active** — งานถูกลืมกลางการปฏิบัติ > - **ไม่เก็บอะไร** — session ไม่สร้างค่าถาวร ## รูปแบบแปรผัน ### รูปแบบ minimal (LLM context ต่ำ) สำหรับ LLM ที่มี context window เล็ก ข้าม full recall: ```bash # Just get stats, not full content curl -H "Authorization: Bearer $KEY" .../memory/stats ``` จากนั้นค้นหาหัวข้อเฉพาะตามต้องการ: ```bash curl -H "Authorization: Bearer $KEY" ".../memory/search?q=current+project" ``` ### รูปแบบ aggressive (agent รันนาน) สำหรับ agent ที่รันเป็นชั่วโมง เพิ่ม re-recall เป็นคาบ: ```python while working: if time.time() - last_recall > 3600: # every hour memories = recall() last_recall = time.time() # ... do work ... ``` ## ขั้นตอนถัดไป - [Memory Tagging Strategy](/docs/llm-cookbook/memory-tagging-strategy) - [Task-Driven Workflow](/docs/llm-cookbook/task-driven-workflow) - [Chat Polling Pattern](/docs/llm-cookbook/chat-polling-pattern)