介绍
# Smalltalk Skill
通过 MCP 执行 Smalltalk 代码并浏览实时的 Squeak/Cuis 镜像。
## 前置条件
**首先获取 ClaudeSmalltalk 仓库:**
```bash git clone https://github.com/CorporateSmalltalkConsultingLtd/ClaudeSmalltalk.git ```
该仓库包含: - 用于 Squeak 的 MCP 服务器代码 (`MCP-Server-Squeak.st`) - 安装文档 (`SQUEAK-SETUP.md`, `CLAWDBOT-SETUP.md`) - 此 Clawdbot 技能 (`clawdbot/`)
## 设置
1. **设置带有 MCP 服务器的 Squeak** — 请参阅 [SQUEAK-SETUP.md](https://github.com/CorporateSmalltalkConsultingLtd/ClaudeSmalltalk/blob/main/SQUEAK-SETUP.md) 2. **配置 Clawdbot** — 请参阅 [CLAWDBOT-SETUP.md](https://github.com/CorporateSmalltalkConsultingLtd/ClaudeSmalltalk/blob/main/CLAWDBOT-SETUP.md)
## 用法
```bash # Check setup python3 smalltalk.py --check
# Evaluate code python3 smalltalk.py evaluate "3 factorial" python3 smalltalk.py evaluate "Date today"
# Browse a class python3 smalltalk.py browse OrderedCollection
# View method source (instance side) python3 smalltalk.py method-source String asUppercase
# View method source (class side) python3 smalltalk.py method-source "MCPServer class" version python3 smalltalk.py method-source MCPServer version --class-side
# List classes (with optional prefix filter) python3 smalltalk.py list-classes Collection
# Get class hierarchy python3 smalltalk.py hierarchy OrderedCollection
# Get subclasses python3 smalltalk.py subclasses Collection
# List all categories python3 smalltalk.py list-categories
# List classes in a category python3 smalltalk.py classes-in-category "Collections-Sequenceable"
# Define a new class python3 smalltalk.py define-class "Object subclass: #Counter instanceVariableNames: 'count' classVariableNames: '' poolDictionaries: '' category: 'MyApp'"
# Define a method python3 smalltalk.py define-method Counter "increment count := (count ifNil: [0]) + 1. ^ count"
# Delete a method python3 smalltalk.py delete-method Counter increment
# Delete a class python3 smalltalk.py delete-class Counter ```
## 操作模式
### Playground (默认)
标准镜像,临时性。当守护进程停止时,更改将被丢弃。
用户说:"load Smalltalk skill" 或 "invoke Smalltalk" — 无需特殊标志。
```bash # Start playground daemon nohup python3 smalltalk-daemon.py start > /tmp/daemon.log 2>&1 & ```
### 开发模式
用户提供自己的镜像/更改对。更改在会话之间持久保存。
用户说:"load Smalltalk skill in dev mode with ~/MyProject.image"
```bash # Start dev daemon with custom image nohup python3 smalltalk-daemon.py start --dev --image ~/MyProject.image > /tmp/daemon.log 2>&1 & ```
开发模式设置 `SMALLTALK_DEV_MODE=1`,以便 MCP 服务器保留 .changes 文件 (而不是重定向到 /dev/null)。提供的镜像旁边必须有一个匹配的 .changes 文件。
### 常用命令 ```bash # Check status python3 smalltalk.py --daemon-status
# Stop daemon python3 smalltalk-daemon.py stop
# Restart in dev mode python3 smalltalk-daemon.py restart --dev --image ~/MyProject.image ```
## 命令
| 命令 | 描述 | |---------|-------------| | `--check` | 验证 VM/镜像路径和依赖项 | | `--daemon-status` | 检查守护进程是否正在运行 | | `--debug` | 调试卡死的系统(发送 SIGUSR1,捕获堆栈跟踪)| | `evaluate <code>` | 执行 Smalltalk 代码,返回结果 | | `browse <class>` | 获取类元数据(超类、实例变量、实例 `methods` 和 `classMethods`)| | `method-source <class> <selector> [--class-side]` | 查看方法源代码(支持 `"Class class"` 语法或 `--class-side` 标志)| | `define-class <definition>` | 创建或修改类 | | `define-method <class> <source>` | 添加或更新方法 | | `delete-method <class> <selector>` | 移除方法 | | `delete-class <class>` | 移除类 | | `list-classes [prefix]` | 列出类,可选择过滤 | | `hierarchy <class>` | 获取超类链 | | `subclasses <class>` | 获取直接子类 | | `list-categories` | 列出所有系统类别 | | `classes-in-category <cat>` | 列出类别中的类 | | `explain <code>` | 解释 Smalltalk 代码(需要 `ANTHROPIC_API_KEY` 或 `OPENAI_API_KEY`)| | `explain-method <class> <sel> [--class-side] [--source <code>]` | 从镜像获取方法并解释它(或使用 `--source`/`--source-file`/`--source-stdin` 绕过守护进程)| | `audit-comment <class> <sel> [--class-side] [--source <code>]` | 审计方法注释与实现(或使用 `--source`/`--source-file`/`--source-stdin` 绕过守护进程)| | `audit-class <class>` | 审计类中的所有方法(实例端 + 类端)| | `generate-sunit <targets> [--force] [--class-name <name>]` | 为方法生成 SUnit 测试并归档到镜像中 |
## 环境变量
| 变量 | 描述 | |----------|-------------| | `SQUEAK_VM_PATH` | Squeak/Cuis VM 可执行文件的路径 | | `SQUEAK_IMAGE_PATH` | 带有 MCP 服务器的 Smalltalk 镜像的路径 | | `ANTHROPIC_API_KEY` | Anthropic Claude 的 API 密钥(LLM 工具首选)| | `ANTHROPIC_MODEL` | Anthropic 模型(默认:`claude-opus-4-20250514`)| | `OPENAI_API_KEY` | OpenAI 的 API 密钥(LLM 工具备选)| | `OPENAI_MODEL` | OpenAI 模型(默认:`gpt-4o`)| | `LLM_PROVIDER` | 强制 LLM 提供商:`anthropic` 或 `openai`(如果未设置则自动检测)|
## 与 Claude Code 一起使用(MCP 模式)
当 Claude Code 通过 MCP 连接到实时的 Smalltalk 镜像时,`explain-method` 和 `audit-comment` 可以使用预获取的源代码,而不需要运行守护进程。使用 `--source`、`--source-file` 或 `--source-stdin` 直接传递方法源代码:
```bash # Inline source (fetched via MCP, passed on command line) python3 smalltalk.py explain-method SmallInteger + --source "+ aNumber <primitive: 1> ^ super + aNumber"
# Source from a file python3 smalltalk.py audit-comment Integer factorial --source-file /tmp/factorial.st
# Source piped via stdin echo "printString ^ self printStringLimitedTo: 50000" | python3 smalltalk.py explain-method Object printString --source-stdin ```
这三个源标志互斥。如果未提供任何标志,则像以前一样使用守护进程。
## 生成 SUnit 测试
`generate-sunit` 命令使用 LLM 为 Smalltalk 方法生成 SUnit 测试用例,并将它们直接归档到运行的镜像中:
```bash # Generate tests for a single method python3 smalltalk.py generate-sunit "String>>asUppercase"
# Generate tests for multiple methods python3 smalltalk.py generate-sunit "Random>>next" "Random>>nextInt:" "Random>>seed:"
# Generate tests for an entire class (all instance methods) python3 smalltalk.py generate-sunit "OrderedCollection"
# Generate tests for class-side methods python3 smalltalk.py generate-sunit "Date class>>today"
# Custom test class name python3 smalltalk.py generate-sunit "String>>asUppercase" --class-name MyStringTests
# Overwrite existing test class python3 smalltalk.py generate-sunit "String>>asUppercase" --force
# Run the generated tests python3 smalltalk.py evaluate "StringGeneratedTest buildSuite run printString" ```
生成的 TestCase 使用标准的 SUnit 断言(`assert:`、`assert:equals:`、`deny:`、`should:raise:`),并归档到 `GeneratedSUnit-*` 类别中。
## 注意事项
- 在 Linux 服务器上无头操作需要 xvfb - 使用 Squeak 6.0 MCP 服务器(如果显示器可用,GUI 保持响应)| - 出于安全考虑,故意排除了 `saveImage` - 需要 MCPServer 版本 7+(v7 添加了类端方法支持)| - Playground 模式:临时性,.changes → /dev/null - 开发模式:持久化,保留 .changes,需要 `--dev --image PATH`