名称: gemini-explore 描述: 使用Gemini的大上下文窗口探索整个代码库。当开始处理不熟悉的代码库或映射大型项目中的依赖关系时使用。 参数提示: “[范围: architecture|dependencies|patterns|all] [–output <路径>] [–pro]” 允许的工具: 读取, Bash, Glob
Gemini 探索命令
将完整的代码库探索委托给Gemini CLI的大上下文窗口。输出一个结构化的Markdown报告供Claude使用。
先决条件
- Gemini CLI 已安装并配置(
npm install -g @google/gemini-cli或brew install gemini-cli) - 在环境中配置了有效的Google AI API密钥
使用方法
/google-ecosystem:gemini-explore [范围] [--output <路径>]
参数
$1(可选):探索范围 - 默认为 “all”architecture- 高层结构、模块、入口点dependencies- 包依赖、导入、关系patterns- 设计模式、约定、代码风格all- 全面探索(默认)
--output <路径>(可选):报告的输出路径(默认:docs/ai-artifacts/explorations/)--pro(可选):强制使用Gemini Pro模型,适用于非常大的代码库(>1M tokens)
示例
/google-ecosystem:gemini-explore- 完整探索,使用默认输出/google-ecosystem:gemini-explore architecture- 专注于架构/google-ecosystem:gemini-explore dependencies --output ./reports/- 依赖关系到自定义路径/google-ecosystem:gemini-explore patterns- 专注于模式和约定/google-ecosystem:gemini-explore all --pro- 强制使用Gemini Pro处理大型代码库
何时使用
在以下情况使用此命令:
- 开始处理一个不熟悉的代码库
- 代码库超过50K tokens(Claude的有效工作上下文)
- 需要在实现前获得全面的架构理解
- 想要映射monorepo中的依赖关系
- 在大规模重构前识别模式
Token 阈值指南
| 代码库大小 | 推荐操作 |
|---|---|
| <50K tokens | 使用Claude的原生探索 |
| 50K-500K | Gemini Flash(此命令) |
| 500K-1M | Gemini Flash 分块处理 |
| >1M | Gemini Pro(添加 --pro 标志) |
执行步骤
步骤 1: 估计Token数量
# 计算所有源文件并估计tokens
total_chars=$(find . -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" -o -name "*.py" -o -name "*.go" -o -name "*.rs" -o -name "*.java" \) -not -path "*/node_modules/*" -not -path "*/.git/*" -not -path "*/dist/*" -not -path "*/build/*" | xargs wc -c 2>/dev/null | tail -1 | awk '{print $1}')
estimated_tokens=$((total_chars / 4))
echo "估计的tokens: $estimated_tokens"
步骤 2: 确定模型
# 检查是否传递了--pro标志
use_pro=false
for arg in "$@"; do
if [ "$arg" = "--pro" ]; then
use_pro=true
break
fi
done
# 基于--pro标志或token数量选择模型
if [ "$use_pro" = true ]; then
model="gemini-2.5-pro"
elif [ "$estimated_tokens" -gt 1000000 ]; then
model="gemini-2.5-pro"
else
model="gemini-2.5-flash"
fi
步骤 3: 构建特定范围的提示
scope="${1:-all}"
case "$scope" in
architecture)
prompt="EXPLORATION MODE: Analyze this codebase architecture.
Provide:
1. **Project Overview**: What does this project do?
2. **Directory Structure**: Key directories and their purpose
3. **Entry Points**: Main entry files and startup flow
4. **Core Modules**: Key modules and their responsibilities
5. **Architecture Pattern**: MVC, Clean Architecture, etc.
6. **Technology Stack**: Languages, frameworks, libraries
Format as structured markdown with headers."
;;
dependencies)
prompt="EXPLORATION MODE: Map dependencies in this codebase.
Provide:
1. **External Dependencies**: Package managers, versions
2. **Internal Dependencies**: Module import graph
3. **Circular Dependencies**: Any detected cycles
4. **Dependency Health**: Outdated, deprecated, security issues
5. **Shared Libraries**: Common utilities used across modules
Format as structured markdown with tables where appropriate."
;;
patterns)
prompt="EXPLORATION MODE: Identify patterns in this codebase.
Provide:
1. **Design Patterns**: Factory, Singleton, Observer, etc.
2. **Code Conventions**: Naming, file structure, imports
3. **Error Handling**: How errors are managed
4. **State Management**: How state flows through the app
5. **Testing Patterns**: Test structure, mocking strategies
6. **Documentation Style**: Comments, JSDoc, docstrings
Format as structured markdown with code examples."
;;
*)
prompt="EXPLORATION MODE: Comprehensive codebase exploration.
Provide a complete analysis covering:
## Architecture
- Project purpose and overview
- Directory structure and organization
- Entry points and startup flow
- Core modules and responsibilities
## Dependencies
- External packages and versions
- Internal module relationships
- Any circular dependencies
## Patterns & Conventions
- Design patterns used
- Coding conventions
- Error handling approach
- Testing structure
## Recommendations for Claude
- Key files to read first
- Important patterns to follow
- Potential areas of concern
Format as structured markdown suitable for another AI agent to consume."
;;
esac
步骤 4: 收集源文件
# 收集所有源文件,尊重常见忽略规则
files=$(find . -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" -o -name "*.py" -o -name "*.go" -o -name "*.rs" -o -name "*.java" -o -name "*.md" -o -name "*.json" \) -not -path "*/node_modules/*" -not -path "*/.git/*" -not -path "*/dist/*" -not -path "*/build/*" -not -path "*/__pycache__/*" -not -path "*/.next/*" | head -500)
步骤 5: 执行探索
result=$(echo "$files" | xargs cat 2>/dev/null | gemini "$prompt" -m "$model")
步骤 6: 解析结果
response=$(echo "$result" | jq -r '.response // "Exploration failed"')
total_tokens=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add // 0')
model_used=$(echo "$result" | jq -r '.stats.models | keys[0] // "unknown"')
步骤 7: 生成报告
创建结构化的Markdown报告:
---
generated-by: gemini-cli
model: {model_used}
timestamp: {ISO8601}
tokens: {total_tokens}
scope: {scope}
---
# 代码库探索报告
## 机器可读摘要
```json
{
"type": "exploration",
"scope": "{scope}",
"tokens_used": {total_tokens},
"model": "{model_used}",
"key_findings": [
"Finding 1",
"Finding 2"
]
}
```
{response}
---
*Generated by Gemini CLI via `/gemini-explore` command*
步骤 8: 保存报告
output_dir="${output:-docs/ai-artifacts/explorations}"
mkdir -p "$output_dir"
timestamp=$(date -u +"%Y-%m-%dT%H-%M-%SZ")
output_file="$output_dir/exploration-${scope}-${timestamp}.md"
echo "$report" > "$output_file"
echo "报告保存到: $output_file"
输出格式
报告以YAML前导信息保存,便于机器解析:
- generated-by: 始终为 “gemini-cli”
- model: 使用的Gemini模型
- timestamp: ISO 8601格式
- tokens: 消耗的总tokens
- scope: 使用的探索范围
注意事项
- 默认使用Flash模型以节省成本
- 自动排除node_modules、.git、dist、build目录
- 报告默认保存到
docs/ai-artifacts/explorations/(git-tracked) - 报告token使用情况以供成本意识
- 输出设计为供Claude读取和操作