بنقرة واحدة
context-optimizer
上下文优化专家。专注于长对话中的上下文管理、token 效率和性能优化。解决 lost-in-middle、context poisoning 等问题,提升 AI 代理在复杂任务中的表现。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
上下文优化专家。专注于长对话中的上下文管理、token 效率和性能优化。解决 lost-in-middle、context poisoning 等问题,提升 AI 代理在复杂任务中的表现。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Query foreign LLM for chat. Use this skill if a foreign LLM like OpenAI ChatGPT, Google Gemini, DeepSeek or xAI Grok should be queried with a single chat message.
Query Multiple AIs for Quorum Answer.
Safety-reviewed guide for @xquik/tweetclaw, the Xquik OpenClaw plugin for structured X/Twitter workflows. Covers setup, credential boundaries, explicit approval for writes and paid actions, spending limits, private-data handling, and monitor controls.
Use when the user needs X (Twitter) data through Xquik: REST API integration, MCP setup, SDK setup, tweet search, user lookup, timeline reads, follower export, media download, monitoring, webhooks, bulk extraction, giveaway draws, or confirmation-gated publishing workflows. Read-only by default, API-key only, no X login material, and every write, private read, monitor, webhook, or metered bulk job requires explicit approval.
Use when the user wants to add ONE table (typically a newly-added Dataverse table) to an existing offline profile without re-running the full /setup-offline-profile wizard. Parallel to /add-dataverse — same single-table flow.
Use when the user wants to change ONE aspect of an existing offline profile (row scope for a table, column list, sync frequency) without re-running the full /setup-offline-profile wizard. Mirrors the /edit-app gated edit pattern.
| name | context-optimizer |
| description | 上下文优化专家。专注于长对话中的上下文管理、token 效率和性能优化。解决 lost-in-middle、context poisoning 等问题,提升 AI 代理在复杂任务中的表现。 |
| metadata | {"short-description":"上下文管理与优化","keywords":["context-optimizer","上下文优化","token 效率","长对话","压缩策略","缓存机制","性能优化","上下文窗口"],"category":"性能优化","author":"Bensz Conan","platform":"Claude Code | OpenAI Codex | ChatGPT"} |
上下文优化 是长对话性能的关键:
┌─────────────────────────────────────────────────────────┐
│ 识别问题 → 压缩历史 → 掩码加载 → 缓存重用 → 性能提升 │
└─────────────────────────────────────────────────────────┘
核心问题:
在以下场景时激活:
表现:
检测:
def detect_lost_in_middle(conversation: list) -> bool:
"""检测是否出现 lost-in-middle 问题"""
# 1. 检查对话长度
if len(conversation) < 10:
return False
# 2. 检查是否有重复提问
questions = [msg for msg in conversation if '?' in msg]
unique_questions = set(questions)
if len(questions) > len(unique_questions) * 1.5:
return True # 存在重复提问
# 3. 检查中间内容是否被引用
middle_start = len(conversation) // 3
middle_end = len(conversation) * 2 // 3
middle_content = conversation[middle_start:middle_end]
# 检查后续对话是否引用中间内容
later_refs = sum(
1 for msg in conversation[middle_end:]
if any(keyword in msg for keyword in extract_keywords(middle_content))
)
if later_refs < len(middle_content) * 0.1:
return True # 中间内容被遗忘
return False
表现:
检测:
def detect_context_poisoning(conversation: list) -> list:
"""检测上下文污染"""
conflicts = []
# 1. 提取所有事实陈述
facts = extract_facts(conversation)
# 2. 检测矛盾
for fact1, fact2 in combinations(facts, 2):
if are_contradictory(fact1, fact2):
conflicts.append({
'type': 'contradiction',
'fact1': fact1,
'fact2': fact2,
'severity': 'high'
})
# 3. 检测信息源冲突
sources = group_by_source(facts)
for source, source_facts in sources.items():
if has_internal_conflicts(source_facts):
conflicts.append({
'type': 'source_conflict',
'source': source,
'severity': 'medium'
})
return conflicts
class ContextCompressor:
"""上下文压缩器"""
def compress_history(
self,
conversation: list,
max_tokens: int,
retention_priority: list[str] = None
) -> list:
"""
压缩对话历史
Args:
conversation: 对话历史
max_tokens: 最大 token 数
retention_priority: 保留优先级 ["current_task", "decisions", "errors"]
Returns:
压缩后的对话
"""
priority = retention_priority or ["current_task", "decisions", "errors"]
# 1. 分类消息
categorized = self._categorize_messages(conversation)
# 2. 按优先级保留
retained = []
current_tokens = 0
for category in priority:
messages = categorized.get(category, [])
for msg in messages:
tokens = self._count_tokens(msg)
if current_tokens + tokens > max_tokens:
# 尝试压缩
compressed = self._compress_message(msg)
if current_tokens + self._count_tokens(compressed) <= max_tokens:
retained.append(compressed)
current_tokens += self._count_tokens(compressed)
else:
retained.append(msg)
current_tokens += tokens
return retained
def _categorize_messages(self, conversation: list) -> dict:
"""分类消息"""
categories = {
'current_task': [],
'decisions': [],
'errors': [],
'context': []
}
for msg in conversation:
if self._is_task_related(msg):
categories['current_task'].append(msg)
elif self._is_decision(msg):
categories['decisions'].append(msg)
elif self._is_error(msg):
categories['errors'].append(msg)
else:
categories['context'].append(msg)
return categories
def _compress_message(self, message: str) -> str:
"""压缩单条消息"""
# 提取关键信息
key_points = extract_key_points(message)
# 生成摘要
summary = summarize(key_points)
return f"[摘要] {summary}"
def _count_tokens(self, text: str) -> int:
"""估算 token 数量"""
return len(text.split()) * 1.3 # 粗略估计
class IncrementalSummarizer:
"""增量摘要器"""
def __init__(self, summary_interval: int = 10):
self.summary_interval = summary_interval
self.summaries = []
def add_messages(self, messages: list) -> str:
"""添加消息并生成摘要"""
# 每隔 N 条消息生成一次摘要
if len(messages) % self.summary_interval == 0:
summary = self._generate_summary(messages[-self.summary_interval:])
self.summaries.append(summary)
# 返回完整的摘要历史
return "\n\n".join(self.summaries)
def _generate_summary(self, messages: list) -> str:
"""生成消息摘要"""
# 提取关键信息
key_info = {
'tasks': self._extract_tasks(messages),
'decisions': self._extract_decisions(messages),
'errors': self._extract_errors(messages),
'outcomes': self._extract_outcomes(messages)
}
# 格式化摘要
summary_parts = []
if key_info['tasks']:
summary_parts.append(f"任务: {', '.join(key_info['tasks'])}")
if key_info['decisions']:
summary_parts.append(f"决策: {', '.join(key_info['decisions'])}")
if key_info['errors']:
summary_parts.append(f"错误: {', '.join(key_info['errors'])}")
if key_info['outcomes']:
summary_parts.append(f"结果: {', '.join(key_info['outcomes'])}")
return " | ".join(summary_parts)
class LazyContextLoader:
"""懒加载上下文"""
def __init__(self):
self.loaded_references = {}
self.reference_metadata = {}
def load_reference(
self,
ref_name: str,
force: bool = False
) -> str | None:
"""
按需加载参考文档
Args:
ref_name: 参考文档名称
force: 是否强制重新加载
"""
# 已加载且不强制
if ref_name in self.loaded_references and not force:
return self.loaded_references[ref_name]
# 检查元数据
metadata = self.reference_metadata.get(ref_name)
if not metadata:
return None
# 按需决策
if self._should_load(metadata):
content = self._load_from_disk(ref_name)
self.loaded_references[ref_name] = content
return content
return None
def _should_load(self, metadata: dict) -> bool:
"""判断是否应该加载"""
# 判断逻辑:
# 1. 是否被明确请求
# 2. 相关性分数
# 3. 当前 token 使用率
relevance = metadata.get('relevance', 0)
token_usage = metadata.get('token_usage', 0)
return relevance > 0.7 or token_usage < 0.8
class SmartCache:
"""智能缓存系统"""
def __init__(self, max_size: int = 100):
self.cache = {}
self.max_size = max_size
self.access_count = {}
def get(self, key: str) -> any:
"""获取缓存"""
if key in self.cache:
# 更新访问计数
self.access_count[key] = self.access_count.get(key, 0) + 1
return self.cache[key]
return None
def set(self, key: str, value: any, priority: int = 1):
"""设置缓存"""
# 缓存已满,清理低优先级项
if len(self.cache) >= self.max_size:
self._evict_low_priority()
self.cache[key] = value
self.access_count[key] = 0
def _evict_low_priority(self):
"""淘汰低优先级缓存"""
# 按 (访问次数 * 优先级) 排序
items = list(self.cache.items())
items.sort(key=lambda x: self.access_count.get(x[0], 0) * x[1].get('priority', 1))
# 移除最低分项
if items:
key_to_remove = items[0][0]
del self.cache[key_to_remove]
del self.access_count[key_to_remove]
# 使用示例
cache = SmartCache()
# 缓存解析结果
code_structure = parse_code('main.py')
cache.set('code:main.py', code_structure, priority=2)
# 获取缓存
cached = cache.get('code:main.py')
if cached:
use_cached_structure(cached)
# ❌ 一次性处理所有信息
def process_large_file(filename):
content = read_file(filename) # 可能很大
result = analyze(content)
return result
# ✅ 分阶段处理
def process_large_file(filename):
# 第一阶段:获取结构
structure = get_file_structure(filename)
# 第二阶段:按需加载
for section in structure.sections:
content = load_section(filename, section)
result = analyze_section(content)
return aggregate_results(results)
# ❌ 一次性提供所有信息
def provide_context():
return """
这是项目的完整文档,包括架构、API、配置等...
(可能 10000+ tokens)
"""
# ✅ 渐进式披露
def provide_context():
return """
项目概述:这是一个 Web 应用
需要详细信息时,可查阅:
- [架构设计](docs/architecture.md)
- [API 文档](docs/api.md)
- [配置指南](docs/config.md)
(约 100 tokens)
"""