skill-extract-scripts

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Extract Scripts from Skills

从Skill中提取脚本

Review a skill file and extract the mechanical, deterministic steps into standalone shell scripts. The skill keeps only the judgment calls — summarising, interpreting, choosing, writing.
审核Skill文件,将机械性、确定性步骤提取为独立的Shell脚本。Skill仅保留判断类工作——总结、解读、选择、撰写。

When to Use

使用场景

Activate when the user:
  • Asks to extract scripts from a skill
  • Wants to make a skill more reliable or deterministic
  • Says a skill is inconsistent or keeps getting CLI commands wrong
  • Asks to split a skill into script + prompt
  • Wants to audit a skill for script extraction candidates
当用户有以下需求时激活:
  • 要求从Skill中提取脚本
  • 希望让Skill更可靠或更具确定性
  • 反馈Skill结果不一致,或CLI命令经常出错
  • 要求将Skill拆分为脚本+提示词
  • 希望审核Skill以确定可提取为脚本的步骤

Process

流程

Step 1: Read the skill

步骤1:读取Skill文件

Read the target skill file. If the user doesn't specify one, ask which skill to review.
读取目标Skill文件。如果用户未指定具体文件,询问需要审核的Skill文件。

Step 2: Classify each step

步骤2:对每个步骤进行分类

Go through every instruction in the skill and classify it:
  • Script (S) — has one right answer, runs the same every time. Examples:
    • CLI commands with specific flags (
      git log --since="1 week ago" --oneline
      )
    • File creation with fixed paths or formats (
      echo "# Title" > report.md
      )
    • Data collection (
      npm test 2>&1 | tail -20
      )
    • Directory setup, file copying, environment checks
    • Any step where you could write it once and it works forever
  • AI (A) — needs thinking, interpretation, or creativity. Examples:
    • Summarising collected data
    • Choosing between options
    • Writing prose, recommendations, analysis
    • Interpreting results or making judgment calls
    • Anything where the "right answer" depends on context
Present the classification to the user as a table:
| Step | Classification | Reason |
|------|---------------|--------|
| Run git log... | S (Script) | Exact command, one right answer |
| Summarise themes | A (AI) | Needs interpretation |
逐一查看Skill中的所有指令并分类:
  • 脚本(S)——有唯一正确答案,每次执行结果一致。示例:
    • 带特定参数的CLI命令(
      git log --since="1 week ago" --oneline
    • 固定路径或格式的文件创建(
      echo "# Title" > report.md
    • 数据收集(
      npm test 2>&1 | tail -20
    • 目录设置、文件复制、环境检查
    • 任何只需编写一次即可永久生效的步骤
  • AI(A)——需要思考、解读或创造力。示例:
    • 总结收集到的数据
    • 在多个选项中做选择
    • 撰写散文、建议、分析内容
    • 解读结果或做出判断
    • 任何“正确答案”取决于上下文的步骤
将分类结果以表格形式呈现给用户:
| Step | Classification | Reason |
|------|---------------|--------|
| Run git log... | S (Script) | Exact command, one right answer |
| Summarise themes | A (AI) | Needs interpretation |

Step 3: Write the script

步骤3:编写脚本

Create a shell script that handles all the S-classified steps.
All scripts MUST be location-independent. They should work if someone pulls them to a different folder or a different computer. Follow these rules:
  • Start with
    #!/bin/bash
  • Resolve the script's own directory first:
    bash
    SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
    Use
    $SCRIPT_DIR
    as the base for any relative paths (e.g.,
    "$SCRIPT_DIR/../data"
    ).
  • Never hardcode absolute paths. No
    /Users/jv/...
    , no
    ~/specific-project/...
    . Use
    $SCRIPT_DIR
    ,
    $PWD
    , or accept paths as arguments.
  • Use
    command -v
    to check for required tools
    before running them:
    bash
    command -v jq >/dev/null 2>&1 || { echo "Error: jq is required but not installed"; exit 1; }
  • Accept inputs as arguments or environment variables, not baked-in values:
    bash
    INPUT_FILE="${1:?Usage: $0 <input-file>}"
    OUTPUT_DIR="${2:-$SCRIPT_DIR/output}"
  • Use
    mktemp
    for temporary files
    instead of hardcoded
    /tmp/my-thing.txt
    :
    bash
    TMPFILE=$(mktemp)
    trap 'rm -f "$TMPFILE"' EXIT
  • Add a comment at the top explaining what the script does
  • End with a confirmation message (
    echo "Done: output written to $OUTFILE"
    )
  • Make it executable: remind the user to run
    chmod +x script.sh
Use parameters to make scripts resilient:
  • Add
    set -euo pipefail
    after the shebang. This makes the script fail fast on errors (
    -e
    ), undefined variables (
    -u
    ), and broken pipes (
    -o pipefail
    ) instead of silently continuing with wrong data.
  • Provide sensible defaults for optional parameters using
    ${VAR:-default}
    :
    bash
    SINCE="${1:-7}"  # days to look back, defaults to 7
    FORMAT="${2:-oneline}"  # output format, defaults to oneline
  • Validate required parameters early and print usage if missing:
    bash
    if [ $# -lt 1 ]; then
      echo "Usage: $0 <input-file> [output-dir]"
      echo "  input-file: path to the skill file to process"
      echo "  output-dir: where to write results (default: ./output)"
      exit 1
    fi
  • Validate that input files/dirs actually exist before doing work:
    bash
    [ -f "$INPUT_FILE" ] || { echo "Error: $INPUT_FILE not found"; exit 1; }
    [ -d "$OUTPUT_DIR" ] || mkdir -p "$OUTPUT_DIR"
  • Use named variables instead of positional
    $1
    ,
    $2
    in the body.
    Assign arguments to descriptive names at the top, then use those names throughout.
    $INPUT_FILE
    is readable,
    $1
    buried on line 40 is not.
  • Make the script idempotent where possible. Running it twice should produce the same result, not duplicate data or fail because output already exists. Use
    mkdir -p
    instead of
    mkdir
    , overwrite output files instead of appending blindly.
Place the script next to the skill file, or in a
scripts/
directory if there are multiple.
创建一个处理所有S类步骤的Shell脚本。
所有脚本必须具备位置无关性。即使将脚本拉取到不同文件夹或不同电脑上也能正常运行。请遵循以下规则:
  • #!/bin/bash
    开头
  • 首先解析脚本自身的目录
    bash
    SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
    使用
    $SCRIPT_DIR
    作为所有相对路径的基准(例如:
    "$SCRIPT_DIR/../data"
    )。
  • 绝对不要硬编码绝对路径。禁止使用
    /Users/jv/...
    ~/specific-project/...
    这类路径。请使用
    $SCRIPT_DIR
    $PWD
    ,或接受路径作为参数。
  • 在运行前使用
    command -v
    检查所需工具是否存在
    bash
    command -v jq >/dev/null 2>&1 || { echo "Error: jq is required but not installed"; exit 1; }
  • 通过参数或环境变量接收输入,而非内置固定值:
    bash
    INPUT_FILE="${1:?Usage: $0 <input-file>}"
    OUTPUT_DIR="${2:-$SCRIPT_DIR/output}"
  • 使用
    mktemp
    创建临时文件
    ,而非硬编码的
    /tmp/my-thing.txt
    bash
    TMPFILE=$(mktemp)
    trap 'rm -f "$TMPFILE"' EXIT
  • 在脚本顶部添加注释说明其功能
  • 结尾添加确认消息(
    echo "Done: output written to $OUTFILE"
  • 提醒用户运行
    chmod +x script.sh
    以赋予脚本可执行权限
使用参数提升脚本健壮性
  • 在shebang后添加
    set -euo pipefail
    。这会让脚本在出错时快速终止(
    -e
    )、遇到未定义变量时终止(
    -u
    )、管道出错时终止(
    -o pipefail
    ),而非带着错误数据继续静默执行。
  • 使用
    ${VAR:-default}
    为可选参数提供合理默认值:
    bash
    SINCE="${1:-7}"  # days to look back, defaults to 7
    FORMAT="${2:-oneline}"  # output format, defaults to oneline
  • 提前验证必填参数,若缺失则打印使用说明:
    bash
    if [ $# -lt 1 ]; then
      echo "Usage: $0 <input-file> [output-dir]"
      echo "  input-file: path to the skill file to process"
      echo "  output-dir: where to write results (default: ./output)"
      exit 1
    fi
  • 在开始工作前验证输入文件/目录是否存在
    bash
    [ -f "$INPUT_FILE" ] || { echo "Error: $INPUT_FILE not found"; exit 1; }
    [ -d "$OUTPUT_DIR" ] || mkdir -p "$OUTPUT_DIR"
  • 在脚本主体中使用具名变量而非位置参数
    $1
    $2
    。在脚本顶部将参数赋值给描述性名称,之后全程使用这些名称。
    $INPUT_FILE
    可读性更强,而埋在第40行的
    $1
    则不然。
  • 尽可能让脚本具备幂等性。运行两次应产生相同结果,而非重复数据或因输出已存在而失败。使用
    mkdir -p
    而非
    mkdir
    ,覆盖输出文件而非盲目追加。
将脚本放在Skill文件旁边,若存在多个脚本则放在
scripts/
目录下。

Step 4: Rewrite the skill

步骤4:重写Skill

Rewrite the skill to:
  1. Call the script first (
    Run ./script-name.sh
    )
  2. Read the script's output
  3. Do only the judgment work with that output
The rewritten skill should be noticeably shorter. If it isn't, the original probably didn't have many mechanical steps and might not need this treatment — say so.
重写Skill以实现:
  1. 先调用脚本(
    Run ./script-name.sh
  2. 读取脚本的输出
  3. 仅基于该输出完成判断类工作
重写后的Skill应明显更简短。若未达到此效果,说明原Skill可能几乎没有机械性步骤,可能无需此处理——请告知用户。

Step 5: Optionally add AI calls to the script

步骤5:(可选)在脚本中添加AI调用

If the workflow benefits from it, show how the script could call AI CLIs directly:
bash
undefined
若工作流能从中受益,展示如何让脚本直接调用AI CLI:
bash
undefined

Collect data deterministically, then ask AI to analyse

Collect data deterministically, then ask AI to analyse

TMPFILE=$(mktemp) trap 'rm -f "$TMPFILE"' EXIT git log --since="1 week ago" --oneline > "$TMPFILE" codex exec "Summarise the themes: $(cat "$TMPFILE")"

This makes the entire workflow runnable as a single script — deterministic orchestration with AI judgment on demand.
TMPFILE=$(mktemp) trap 'rm -f "$TMPFILE"' EXIT git log --since="1 week ago" --oneline > "$TMPFILE" codex exec "Summarise the themes: $(cat "$TMPFILE")"

这让整个工作流可作为单个脚本运行——确定性编排,按需调用AI完成判断工作。

Output Format

输出格式

Produce three artifacts:
  1. Classification table — every step labeled S or A with reasoning
  2. Shell script — handles all S steps
  3. Rewritten skill — calls the script, then does only A steps
生成三个产物:
  1. 分类表格——每个步骤标记为S或A并说明原因
  2. Shell脚本——处理所有S类步骤
  3. 重写后的Skill——调用脚本,然后仅完成A类步骤

Tips

提示

  • Don't force it. If a skill is mostly judgment calls with one or two simple commands, it probably doesn't need a script. Say so.
  • Scripts should be independently testable. The user should be able to run the script alone to verify the data collection works before involving the AI.
  • Keep scripts focused. One script per logical phase. Don't create a mega-script that's as hard to debug as the original skill.
  • Variable names should be descriptive.
    $OUTFILE
    and
    $TODAY
    are better than
    $F
    and
    $D
    .
  • Always use
    "$VARIABLE"
    with quotes to handle spaces in paths.
  • Portability is non-negotiable. Every script must work when moved to a different directory or cloned to a different machine. No hardcoded paths, no assumptions about home directory layout, no machine-specific values baked in. If the script needs something specific to the environment, it takes it as an argument or reads it from an env var.
  • 不要强行提取。若Skill大多为判断类工作,仅包含一两个简单命令,则可能无需脚本。请告知用户。
  • 脚本应可独立测试。用户应能单独运行脚本以验证数据收集是否正常,再结合AI使用。
  • 保持脚本聚焦。每个脚本对应一个逻辑阶段。不要创建一个和原Skill一样难以调试的巨型脚本。
  • 变量名称应具描述性。
    $OUTFILE
    $TODAY
    $F
    $D
    更好。
  • 始终使用
    "$VARIABLE"
    加引号以处理路径中的空格。
  • 可移植性是硬性要求。每个脚本在移动到不同目录或克隆到不同机器时都必须能正常运行。禁止硬编码路径,禁止假设主目录结构,禁止内置机器特定值。若脚本需要环境特定内容,应通过参数接收或从环境变量读取。