# セッション開始パターン すべての LLM エージェントセッションはこの標準的なスタートアップシーケンスに従うべきです。ステップを省略すると、コンテキストの喪失、メッセージの見逃し、タスクの忘れにつながります。 ## パターン ``` 1. Recall all memories 2. Poll for unread chat messages 3. Check in-progress tasks 4. Build context from results 5. Process pending items before new work ``` ## 実装 ### ステップ 1:すべてのメモリをリコール > [!CRITICAL] > これが最も重要な呼び出しです。これがないと、過去のセッションの記憶がありません。 ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/memory/recall ``` すべてのメモリのプレーンテキストサマリーを優先度順で返します。 ### ステップ 2:未読チャットメッセージをポーリング ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/chat/poll ``` 人間からの未読メッセージを返します。**自動的に既読にします。** ### ステップ 3:進行中のタスクを確認 ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ "https://synapse.schaefer.zone/mind/tasks?status=in_progress" ``` 前回のセッションで作業していたタスクを返します。 ### ステップ 4:コンテキストを構築 3 つのレスポンスをシステムプロンプトにまとめます: ```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] > - **リコールを省略** — コンテキストなしで開始し、過去の失敗を繰り返す > - **チャットのポーリング忘れ** — 人間のメッセージが未応答のまま > - **アクティブなタスクの無視** — 作業が実行中に忘れられる > - **何も保存しない** — セッションが永続的価値を生まない ## バリエーション ### 最小パターン(低コンテキスト LLM) コンテキストウィンドウが小さい LLM の場合、完全なリコールを省略します: ```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" ``` ### 積極的パターン(長時間実行エージェント) 数時間動くエージェントには、定期的な再リコールを追加します: ```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)