AgentSkillsCN

memory-summarize

利用低成本 AI 模型(Claude Haiku、GPT-4o-mini)将记忆压缩为结构化的摘要。当记忆条目数量超过 50 条,或令牌估算值超过 40000 个时,该功能会自动触发。输出结构化 JSON,并附带压缩指标。

SKILL.md
--- frontmatter
name: memory-summarize
description: Compress memories into structured summaries using low-cost AI models (Claude Haiku, GPT-4o-mini). Triggers when memory count exceeds 50 or token estimate exceeds 40000. Outputs structured JSON with compression metrics.
license: Apache-2.0
compatibility: Requires BrAIny API at localhost:3000, Bun runtime
metadata:
  author: brainy
  version: "1.1"
  category: memory-management
  subagent: summarizer
allowed-tools: Bash(bun:*)

Memory Summarization

Compresses multiple memories into structured summaries. Reduces context size while preserving critical information.

Execution Path

code
┌─────────────────────────────────────────────────────────┐
│                   EXECUTION DECISION                    │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  ┌──────────┐     ┌───────────────────┐                │
│  │  START   │────▶│ SUBAGENT AVAILABLE?│                │
│  └──────────┘     └─────────┬─────────┘                │
│                             │                           │
│              ┌──────────────┴──────────────┐           │
│              ▼                             ▼           │
│       ┌────────────┐                ┌────────────┐     │
│       │    YES     │                │     NO     │     │
│       └─────┬──────┘                └─────┬──────┘     │
│             │                             │            │
│             ▼                             ▼            │
│  ┌─────────────────────┐      ┌─────────────────────┐  │
│  │ Task(summarizer,    │      │ Bash(bun scripts/   │  │
│  │   prompt: "...")    │      │   create-summary.ts)│  │
│  └─────────────────────┘      └─────────────────────┘  │
│                                                         │
└─────────────────────────────────────────────────────────┘

Path A: Subagent Available (Preferred)

Use the summarizer subagent for lower token cost:

code
Task(
  subagent_type: "summarizer",
  description: "Summarize memories",
  prompt: "Check stats and create coding_session summary for project 3afb861a-b783-45a6-bba2-4b6d96468aeb"
)

Path B: Script Fallback

If subagent unavailable, execute scripts directly:

bash
bun .opencode/skills/memory-summarize/scripts/check-memory-stats.ts PROJECT_ID
bun .opencode/skills/memory-summarize/scripts/create-summary.ts PROJECT_ID TYPE

State Machine

code
┌─────────────────────────────────────────────────────────────────────┐
│                     SUMMARIZATION WORKFLOW                          │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  ┌──────────┐     ┌──────────────┐     ┌─────────────────────────┐ │
│  │  START   │────▶│ CHECK_STATS  │────▶│ EVALUATE_THRESHOLDS     │ │
│  └──────────┘     └──────────────┘     └─────────────────────────┘ │
│                                                   │                 │
│                          ┌────────────────────────┼────────────┐   │
│                          ▼                        ▼            │   │
│                   ┌────────────┐          ┌────────────┐       │   │
│                   │ SKIP       │          │ SUMMARIZE  │       │   │
│                   │ (below     │          │ (above     │       │   │
│                   │ threshold) │          │ threshold) │       │   │
│                   └────────────┘          └─────┬──────┘       │   │
│                          │                      │              │   │
│                          │               ┌──────▼──────┐       │   │
│                          │               │ SELECT_TYPE │       │   │
│                          │               └──────┬──────┘       │   │
│                          │                      │              │   │
│                          │    ┌─────────────────┼─────────────┐│   │
│                          │    ▼                 ▼             ▼│   │
│                          │ ┌────────┐    ┌────────────┐ ┌──────┴┐  │
│                          │ │coding_ │    │conversation│ │custom │  │
│                          │ │session │    │            │ │       │  │
│                          │ └───┬────┘    └─────┬──────┘ └───┬───┘  │
│                          │     └───────────────┼────────────┘      │
│                          │                     ▼                   │
│                          │              ┌─────────────┐            │
│                          │              │ CREATE_     │            │
│                          │              │ SUMMARY     │            │
│                          │              └──────┬──────┘            │
│                          │                     │                   │
│                          │              ┌──────▼──────┐            │
│                          │              │ VERIFY_     │            │
│                          │              │ COMPRESSION │            │
│                          │              └──────┬──────┘            │
│                          │                     │                   │
│                          │         ┌───────────┴───────────┐       │
│                          │         ▼                       ▼       │
│                          │  ┌────────────┐         ┌────────────┐  │
│                          │  │ SUCCESS    │         │ FAILURE    │  │
│                          │  │ ratio > 2.0│         │ ratio < 2.0│  │
│                          │  └─────┬──────┘         └─────┬──────┘  │
│                          │        │                      │         │
│                          │        ▼                      ▼         │
│                          │  ┌──────────┐          ┌──────────┐     │
│                          └─▶│   END    │◀─────────│  RETRY   │     │
│                             └──────────┘          └──────────┘     │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

States

StateEntry ConditionActionExit Condition
CHECK_STATSStartRun check-memory-stats.tsStats retrieved
EVALUATE_THRESHOLDSStats availableCompare against thresholdsDecision made
SKIPcount < 50 AND tokens < 40000Log "below threshold"End
SELECT_TYPEAbove thresholdDetermine summary typeType selected
CREATE_SUMMARYType selectedRun create-summary.tsAPI response
VERIFY_COMPRESSIONSummary createdCheck ratio > 2.0Verified
SUCCESSratio >= 2.0Output summary IDEnd
FAILUREratio < 2.0Log warningRetry or End

Thresholds

MetricThresholdAction
Memory count>= 50Summarize
Token estimate>= 40000Summarize
Compression ratio< 2.0Warning

Summary Types

coding_session

Input: Development memories (file changes, errors, debugging)

Output sections:

  • objective: string (one sentence)
  • progress: string[] (action → result)
  • filesTouched: {path, operation, changes}[]
  • currentState: string
  • criticalDetails: string[]

conversation

Input: Dialogue memories (discussions, decisions)

Output sections:

  • topics: string[]
  • decisions: string[]
  • actionItems: string[]
  • context: string[]
  • openQuestions: string[]

custom

Input: Any memories with custom prompt

Output sections:

  • prompt: string
  • content: string
  • keyPoints: string[]

Commands

Check Statistics

bash
bun .opencode/skills/memory-summarize/scripts/check-memory-stats.ts PROJECT_ID [--show-compression]

Output:

code
Memory Count: 150
Estimated Tokens: 60000
⚠️  Memory count exceeds threshold (50)
Recommendation: Run summarization

Create Summary

bash
bun .opencode/skills/memory-summarize/scripts/create-summary.ts PROJECT_ID TYPE [CUSTOM_PROMPT]

Parameters:

  • PROJECT_ID: UUID (required)
  • TYPE: coding_session | conversation | custom (default: coding_session)
  • CUSTOM_PROMPT: string (required if TYPE=custom)

Output:

code
Summary ID: abc-123
Compression Ratio: 6.25x
Token Reduction: 84%
  Original: 5000 tokens
  Compressed: 800 tokens
Memories:
  Summarized: 44
  Retained: 6

Get Hybrid Context

bash
bun .opencode/skills/memory-summarize/scripts/get-context.ts PROJECT_ID [QUERY]

Output:

json
{
  "summaries": [...],
  "recentMemories": [...],
  "relevantMemories": [...]
}

Configuration

VariableDefaultDescription
BRAINY_API_URLhttp://localhost:3000API endpoint
RETAIN_LAST_N6Memories to keep unsummarized
SUMMARIZATION_AUTO_TRIGGER_COUNT50Memory count threshold
SUMMARIZATION_AUTO_TRIGGER_TOKENS40000Token threshold

Error Codes

ErrorCauseResolution
"No memories found"Empty projectCreate memories first
"All memories in retain window"count <= RETAIN_LAST_NReduce RETAIN_LAST_N or add memories
"Project not found"Invalid PROJECT_IDVerify UUID
"API connection refused"Server not runningStart BrAIny API

Decision Tree

code
Is memory count >= 50?
├── YES → Summarize
└── NO → Is token estimate >= 40000?
         ├── YES → Summarize
         └── NO → Skip (no action needed)

If Summarize:
├── Contains file changes, errors, code? → coding_session
├── Contains discussion, decisions? → conversation
└── User specified prompt? → custom