artifact-type-tailored-context by closedloop-ai/claude-plugins
npx skills add https://github.com/closedloop-ai/claude-plugins --skill artifact-type-tailored-context使用分层摘要策略,在指定的令牌预算内压缩单个工件。此技能在隔离的派生上下文中运行,以防止大型原始工件污染父代理的上下文。
您负责压缩单个工件文件以使其符合令牌预算。您的职责:
成功标准:
您收到三个必需参数:
| 参数 | 类型 | 描述 |
|---|---|---|
artifact_path | 字符串 | 相对于 $CLOSEDLOOP_WORKDIR 的工件文件路径 |
task_description |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 字符串 |
| 压缩指导(例如,"保留函数签名") |
token_budget | 整数 | 压缩输出的最大允许令牌数 |
从其绝对路径读取工件:
# 构造完整路径
ARTIFACT_FULL_PATH="$CLOSEDLOOP_WORKDIR/$artifact_path"
使用 Read 工具加载工件内容。如果文件不存在,则跳转到错误处理。
调用 count_tokens.py 脚本以获取准确的令牌计数:
cd "$CLOSEDLOOP_WORKDIR" && uv run count_tokens.py "$artifact_path"
预期输出格式:
{
"input_tokens": 1234
}
解析 JSON 输出并将 input_tokens 提取为 raw_tokens。
错误处理:
raw_tokens = len(content) / 4[WARNING: Token count estimated via heuristic due to count_tokens.py failure]\n\n根据原始令牌数相对于预算的情况选择策略:
条件: raw_tokens <= token_budget
操作: 返回未更改的工件
元数据:
compacted_tokens = raw_tokenstruncated = false无需进一步处理。
条件: token_budget < raw_tokens <= token_budget * 1.5
操作: 应用特定于工件类型的压缩,保留结构
按工件类型的压缩策略:
| 工件类型 | 策略 |
|---|---|
| 代码差异 | 保留函数签名、类声明和错误消息。使用 // ... (implementation omitted for brevity) 摘要方法体。移除注释和空行。 |
| JSON 文件 | 保留所有键和结构。对于长度超过 10 项的数组,保留前 5 项和后 2 项,中间用 {"_truncated": "N items omitted"} 替换。截断超过 200 个字符的字符串值。 |
| 日志文件 | 保留所有 ERROR 和 WARNING 行。使用 ... (N info lines omitted) 摘要连续的 INFO 行。保持前 10 行和后 10 行完整。 |
| 计划/PRD Markdown | 保留所有标题、表格和代码块。摘要段落文本,保留关键名词和动作动词。移除冗余示例。 |
验证:
压缩后,再次计算令牌数:
cd "$CLOSEDLOOP_WORKDIR" && uv run count_tokens.py <(echo "$compressed_content")
决策树:
compacted_tokens <= token_budget:成功 → 返回 truncated = falsecompacted_tokens > token_budget:压缩失败 → 回退到层级 3条件: raw_tokens > token_budget * 1.5 或层级 2 压缩超出预算
操作: 在字符边界处硬截断
算法:
char_limit = token_budget * 4(启发式:每个令牌 4 个字符)char_limit 之前找到最后一个段落边界(双换行符 \n\n)truncated_tokens = raw_tokens - token_budget[TRUNCATED: content exceeds budget, remaining {truncated_tokens} tokens omitted]
元数据:
compacted_tokens = token_budget(近似值)truncated = true返回具有严格模式的结构化 JSON:
{
"artifact_name": "path/to/artifact.ext",
"raw_tokens": 5000,
"compacted_tokens": 2000,
"truncated": false,
"content": "compressed artifact content here..."
}
字段描述:
| 字段 | 类型 | 描述 |
|---|---|---|
artifact_name | 字符串 | 原始 artifact_path 参数 |
raw_tokens | 整数 | 对原始工件使用 count_tokens.py 计算的令牌数 |
compacted_tokens | 整数 | 压缩后的令牌数(来自 count_tokens.py 验证或层级 1/3 的估算) |
truncated | 布尔值 | 如果应用了层级 3 截断则为 true,否则为 false |
content | 字符串 | 压缩或截断后的工件内容 |
条件: artifact_path 在 $CLOSEDLOOP_WORKDIR/<artifact_path> 处不存在
响应:
{
"artifact_name": "path/to/missing.ext",
"raw_tokens": 0,
"compacted_tokens": 0,
"truncated": true,
"content": "[ERROR: artifact not found at $CLOSEDLOOP_WORKDIR/path/to/missing.ext]"
}
条件: 脚本以非零代码退出或返回无效 JSON
操作:
raw_tokens = len(content) / 4[WARNING: Token count estimated via heuristic due to count_tokens.py failure]
<original content follows>
3. 使用估算的令牌数继续执行分层策略
4. 除非应用了层级 3,否则设置 truncated = false
条件: 层级 2 压缩产生格式错误的内容(例如,JSON 工件的 JSON 语法无效)
操作: 立即回退到层级 3 硬截断,并设置 truncated = true
输入:
artifact_path: plan.jsontoken_budget: 5000输出:
{
"artifact_name": "plan.json",
"raw_tokens": 3200,
"compacted_tokens": 3200,
"truncated": false,
"content": "<full plan.json content>"
}
输入:
artifact_path: git_difftoken_budget: 10000应用的压缩: 移除注释,摘要方法体,保留签名
输出:
{
"artifact_name": "git_diff",
"raw_tokens": 12000,
"compacted_tokens": 9500,
"truncated": false,
"content": "<compressed diff with function signatures preserved>"
}
输入:
artifact_path: outcomes.logtoken_budget: 3000操作: 在约 12000 个字符处硬截断(3000 令牌 * 4)
输出:
{
"artifact_name": "outcomes.log",
"raw_tokens": 25000,
"compacted_tokens": 3000,
"truncated": true,
"content": "<first ~2900 tokens of log>\n\n[TRUNCATED: content exceeds budget, remaining 22000 tokens omitted]"
}
每周安装数
1
仓库
GitHub 星标数
71
首次出现
今天
安全审计
安装于
windsurf1
amp1
cline1
openclaw1
opencode1
cursor1
Compress individual artifacts within a specified token budget using tiered summarization strategies. This skill operates in isolated forked context to prevent polluting the parent agent's context with large raw artifacts.
You are responsible for compressing a single artifact file to fit within a token budget. Your responsibilities:
Success criteria:
You receive three required parameters:
| Parameter | Type | Description |
|---|---|---|
artifact_path | string | Path to artifact file relative to $CLOSEDLOOP_WORKDIR |
task_description | string | Compression guidance (e.g., "preserve function signatures") |
token_budget | integer | Maximum allowed tokens for compressed output |
Read the artifact from its absolute path:
# Construct full path
ARTIFACT_FULL_PATH="$CLOSEDLOOP_WORKDIR/$artifact_path"
Use the Read tool to load the artifact content. If the file does not exist, skip to error handling.
Invoke the count_tokens.py script to get accurate token count:
cd "$CLOSEDLOOP_WORKDIR" && uv run count_tokens.py "$artifact_path"
Expected output format:
{
"input_tokens": 1234
}
Parse the JSON output and extract input_tokens as raw_tokens.
Error handling:
raw_tokens = len(content) / 4[WARNING: Token count estimated via heuristic due to count_tokens.py failure]\n\nChoose strategy based on raw token count relative to budget:
Condition: raw_tokens <= token_budget
Action: Return artifact unchanged
Metadata:
compacted_tokens = raw_tokenstruncated = falseNo further processing needed.
Condition: token_budget < raw_tokens <= token_budget * 1.5
Action: Apply artifact-type-specific compression preserving structure
Compression strategies by artifact type:
| Artifact Type | Strategy |
|---|---|
| Code diffs | Keep function signatures, class declarations, and error messages. Summarize method bodies with // ... (implementation omitted for brevity). Remove comments and blank lines. |
| JSON files | Keep all keys and structure. For arrays longer than 10 items, keep first 5 and last 2, replace middle with {"_truncated": "N items omitted"}. Truncate string values over 200 chars. |
| Log files | Keep all ERROR and WARNING lines. Summarize consecutive INFO lines with ... (N info lines omitted). Keep first and last 10 lines intact. |
| Plan/PRD markdown | Keep all headings, tables, and code blocks. Summarize paragraph text preserving key nouns and action verbs. Remove redundant examples. |
Validation:
After compression, count tokens again:
cd "$CLOSEDLOOP_WORKDIR" && uv run count_tokens.py <(echo "$compressed_content")
Decision tree:
compacted_tokens <= token_budget: Success → Return with truncated = falsecompacted_tokens > token_budget: Compression failed → Fallback to Tier 3Condition: raw_tokens > token_budget * 1.5 OR compression in Tier 2 exceeded budget
Action: Hard truncate at character boundary
Algorithm:
char_limit = token_budget * 4 (heuristic: 4 chars per token)\n\n) before char_limittruncated_tokens = raw_tokens - token_budget[TRUNCATED: content exceeds budget, remaining {truncated_tokens} tokens omitted]
Metadata:
compacted_tokens = token_budget (approximate)truncated = trueReturn structured JSON with strict schema:
{
"artifact_name": "path/to/artifact.ext",
"raw_tokens": 5000,
"compacted_tokens": 2000,
"truncated": false,
"content": "compressed artifact content here..."
}
Field descriptions:
| Field | Type | Description |
|---|---|---|
artifact_name | string | Original artifact_path parameter |
raw_tokens | integer | Token count from count_tokens.py on raw artifact |
compacted_tokens | integer | Token count after compression (from count_tokens.py validation or estimate for Tier 1/3) |
truncated | boolean | true if Tier 3 truncation applied, false otherwise |
Condition: artifact_path does not exist at $CLOSEDLOOP_WORKDIR/<artifact_path>
Response:
{
"artifact_name": "path/to/missing.ext",
"raw_tokens": 0,
"compacted_tokens": 0,
"truncated": true,
"content": "[ERROR: artifact not found at $CLOSEDLOOP_WORKDIR/path/to/missing.ext]"
}
Condition: Script exits with non-zero code or returns invalid JSON
Action:
raw_tokens = len(content) / 4[WARNING: Token count estimated via heuristic due to count_tokens.py failure]
<original content follows>
3. Proceed with tiered strategy using estimated token count
4. Set truncated = false unless Tier 3 is applied
Condition: Tier 2 compression produces malformed content (e.g., invalid JSON syntax for JSON artifacts)
Action: Immediately fallback to Tier 3 hard truncation with truncated = true
Input:
artifact_path: plan.jsontoken_budget: 5000Output:
{
"artifact_name": "plan.json",
"raw_tokens": 3200,
"compacted_tokens": 3200,
"truncated": false,
"content": "<full plan.json content>"
}
Input:
artifact_path: git_difftoken_budget: 10000Compression applied: Remove comments, summarize method bodies, keep signatures
Output:
{
"artifact_name": "git_diff",
"raw_tokens": 12000,
"compacted_tokens": 9500,
"truncated": false,
"content": "<compressed diff with function signatures preserved>"
}
Input:
artifact_path: outcomes.logtoken_budget: 3000Action: Hard truncate at ~12000 chars (3000 tokens * 4)
Output:
{
"artifact_name": "outcomes.log",
"raw_tokens": 25000,
"compacted_tokens": 3000,
"truncated": true,
"content": "<first ~2900 tokens of log>\n\n[TRUNCATED: content exceeds budget, remaining 22000 tokens omitted]"
}
Weekly Installs
1
Repository
GitHub Stars
71
First Seen
Today
Security Audits
Gen Agent Trust HubFailSocketFailSnykFail
Installed on
windsurf1
amp1
cline1
openclaw1
opencode1
cursor1
AI Elements:基于shadcn/ui的AI原生应用组件库,快速构建对话界面
60,400 周安装
content | string | Compressed or truncated artifact content |