← 返回
未分类 Key 中文

Session Context Injector

Reorient a Telegram chat after a session reset. Reads a project's STATUS.md (resume point, blockers, next action) and sends a project-specific context inject...
会话重置后重新定位 Telegram 聊天,读取项目的 STATUS.md(恢复点、阻碍、下一步),并发送项目特定的上下文注入...
nissan nissan 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 需要
★ 0
Stars
📥 276
下载
💾 1
安装
1
版本
#latest

概述

SKILL: session-context-injector

Use when: A Telegram chat session has been cleared, a collaborator joins a project room, a new project room is created, or any context needs to be re-anchored in a specific chat after a reset.

Why this exists: After a session clear, the next person to message gets a blank-slate AI with no project awareness. This skill ensures every reset is followed by a project-specific reorientation message — so the first reply in a fresh session is always oriented, not confused.

Invoked by:

  • playbooks/session-refresh/PLAYBOOK.md — Phase 3, after each session clear
  • playbooks/new-project/PLAYBOOK.md — Stage 5b, after room creation
  • playbooks/telegram-collaborator-room/PLAYBOOK.md — on collaborator join

Inputs

InputSourceRequired
---------
chat_idsessions.json or telegram-groups.json
slugmemory/telegram-groups.json[chat_id].slug✅ for project rooms
project_namememory/telegram-groups.json[chat_id].name
bot_tokenop read "op://OpenClaw/Telegram Bot Token/credential"
STATUS.mdprojects//STATUS.mdOptional — enriches message
chat_typegroup / direct / tui

Step 1 — Parse STATUS.md

If projects//STATUS.md exists, extract:

import re

def parse_status(slug: str) -> dict:
    path = PROJECTS_DIR / slug / "STATUS.md"
    if not path.exists():
        return {}
    text = path.read_text()

    def extract(heading: str) -> str:
        m = re.search(
            rf"##+ {re.escape(heading)}\s*\n(.*?)(?=\n##|\Z)",
            text, re.DOTALL
        )
        return m.group(1).strip()[:400] if m else ""

    resume   = extract("RESUME FROM HERE") or extract("Resume From Here") or ""
    blockers = extract("Blockers") or extract("Open Blockers") or ""

    # Strip markdown bullets
    resume   = re.sub(r"^[-*] ", "", resume, flags=re.MULTILINE).strip()
    blockers = re.sub(r"^[-*] ", "", blockers, flags=re.MULTILINE).strip()

    return {
        "resume":   resume[:300],
        "blockers": blockers[:200],
    }

Fallback: If STATUS.md is missing or empty, use the generic template (Step 2b).


Step 2a — Build Project Room Message (group chat)

def build_group_message(project_name: str, slug: str, status: dict) -> str:
    resume   = status.get("resume", "")
    blockers = status.get("blockers", "")

    lines = [
        f"🔄 <b>Session Refresh — {project_name}</b>",
        "",
        "I've refreshed my context. Here's where we are:",
        "",
    ]

    if slug:
        lines.append(f"📌 <b>Project:</b> {slug.upper()}")
    if resume:
        lines.append(f"📍 <b>Resume from:</b> {resume}")
    if blockers and blockers.lower() not in ("none", "—", "-", ""):
        lines.append(f"🚧 <b>Blockers:</b> {blockers}")

    lines += [
        "",
        "My memory and project files are fully updated. "
        "Jump back in whenever you're ready — just pick up the thread. 🐾",
    ]
    return "\n".join(lines)

Character limit: Keep under 4096 chars (Telegram max). Truncate resume and blockers at the values above if STATUS.md is long.


Step 2b — Build Direct Chat Message (Nissan DM)

Use when chat_type == "direct" and the chat_id is Nissan's (821071206).

def build_direct_message(touched_projects: list[dict]) -> str:
    lines = [
        "🔄 <b>Daily Session Refresh</b>",
        "",
        "Context cleared. Memory and STATUS files are up to date.",
    ]
    if touched_projects:
        lines.append("")
        lines.append("Active projects refreshed:")
        for p in touched_projects[:10]:
            lines.append(f"• <b>{p['slug'].upper()}</b> — {p['name']}")
    lines += ["", "Anything you want to jump straight into? 🐾"]
    return "\n".join(lines)

Step 2c — Build New Room / Collaborator Join Message

Use when a room is freshly created or a collaborator joins for the first time (not a reset).

def build_welcome_message(project_name: str, slug: str, purpose: str, allowed_topics: list[str]) -> str:
    topic_list = "\n".join(f"• {t}" for t in allowed_topics[:8])
    return (
        f"👋 <b>Welcome to {project_name}</b>\n\n"
        f"{purpose}\n\n"
        f"<b>What I can help with here:</b>\n{topic_list}\n\n"
        f"I have full project context loaded. Ask me anything within scope. 🐾"
    )

Step 3 — Send via Telegram Bot API

import urllib.request, json

def send_injection(bot_token: str, chat_id: str, text: str, dry_run: bool = False) -> bool:
    if dry_run:
        print(f"[DRY-RUN] → {chat_id}: {text[:100].replace(chr(10),' ')}…")
        return True
    try:
        url     = f"https://api.telegram.org/bot{bot_token}/sendMessage"
        payload = json.dumps({
            "chat_id":    chat_id,
            "text":       text,
            "parse_mode": "HTML",
        }).encode()
        req = urllib.request.Request(
            url, data=payload,
            headers={"Content-Type": "application/json"}
        )
        with urllib.request.urlopen(req, timeout=10) as r:
            return r.status == 200
    except Exception as e:
        print(f"⚠️  Injection failed ({chat_id}): {e}")
        return False

Parse mode: Always use HTML (not Markdown). Markdown requires escaping; HTML is more predictable with and .

On failure: Log the failure, continue with other chats. Never block the refresh loop on a single failed send.


Step 4 — Log the Injection

After each successful send, append to memory/YYYY-MM-DD.md:

### Context Injection Sent — [project_name] — YYYY-MM-DD HH:MM AEST
- **Chat ID:** [chat_id]
- **Slug:** [slug]
- **Type:** [session-refresh / room-creation / collaborator-join]
- **Status:** ✅ sent / ❌ failed

And if the project has a STATUS.md, append a one-liner:

_Context injection sent: YYYY-MM-DD HH:MM AEST — session cleared._

Decision Rules

ConditionAction
------
STATUS.md exists + has contentUse Step 2a with extracted resume/blockers
STATUS.md missing or emptyUse Step 2a with slug + name only (no resume/blockers section)
chat_type == "direct" + NissanUse Step 2b
New room / first joinUse Step 2c
chat_id is NoneSkip Telegram send; log to memory only
bot_token unavailableLog failure to memory; do not crash
Message > 4096 charsTruncate resume to 200 chars, blockers to 100 chars

Example Output (group chat, STATUS.md found)

🔄 Session Refresh — OpenClaw — Portkey Gateway Integration

I've refreshed my context. Here's where we are:

📌 Project: PORTKEY
📍 Resume from: Test latency comparison between direct Anthropic calls and Portkey-routed calls. Script is at scripts/portkey-bench.py — needs --compare flag wired up.
🚧 Blockers: Portkey dashboard shows incorrect token counts for cache_read events — filed upstream.

My memory and project files are fully updated. Jump back in whenever you're ready — just pick up the thread. 🐾

Reuse Checklist

Before calling this skill from a playbook or script:

  • [ ] chat_id is confirmed (from sessions.json or telegram-groups.json)
  • [ ] bot_token retrieved from 1Password (not hardcoded)
  • [ ] slug verified — projects//STATUS.md exists or graceful fallback confirmed
  • [ ] parse_mode: "HTML" — not Markdown
  • [ ] Failure is logged, not raised

ClawHub Tags

telegram, context-injection, session-refresh, project-rooms, reorientation


Changelog

  • 2026-04-03: Extracted from playbooks/session-refresh/PLAYBOOK.md Phase 3. Added welcome/join variant (Step 2c). Formalised as standalone reusable skill.

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-07 18:27 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

ai-agent

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,417 📥 325,744
ai-agent

Agent Browser

rez0
用于 AI 代理的浏览器自动化 CLI。当用户需要与网站交互(包括浏览页面、填写表单、点击按钮、截图等)时使用。
★ 848 📥 328,426
knowledge-management

Fact Checker

nissan
对照源数据验证 Markdown 草稿中的声明、数字和事实。适用场景:发布前审核博客文章、报告或文档的准确性。
★ 3 📥 2,216