Command Casperr: a tiny hand-drawn ghost that haunts your PowerShell terminal 👻

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Title: Command Casperr: a tiny hand-drawn ghost that haunts your PowerShell terminal 👻

Command Casperr:一个在你 PowerShell 终端中出没的小型手绘幽灵 👻

Hacktoberfest Open-Source AI Challenge: Ghost in the Machine Submission 👻🌼 This is a submission for the Hacktoberfest Open-Source AI Challenge Week 4: Ghost in the Machine What I Built I built Command Casper, a tiny hand-drawn ghost that floats on your screen and haunts your PowerShell terminal. You type a command but don’t press Enter. You press Alt+G. Casper looks at what you typed and turns green, yellow or red: 🟢 Green: it only reads things. 🟡 Yellow: it changes something, but you can usually undo it (installs, copies, a git commit). 🔴 Red: it destroys something or is hard to undo (deletes, overwrites, —force, download-and-run).

Hacktoberfest 开源 AI 挑战赛:机器幽灵参赛作品 👻🌼 这是我为 Hacktoberfest 开源 AI 挑战赛第四周“机器幽灵”提交的作品。我制作了 Command Casper,一个漂浮在屏幕上并出没于 PowerShell 终端的小型手绘幽灵。当你输入命令但不按回车键,而是按下 Alt+G 时,Casper 会查看你输入的内容并变色:🟢 绿色:仅读取内容。🟡 黄色:会进行更改,但通常可以撤销(如安装、复制、git 提交)。🔴 红色:会销毁内容或难以撤销(如删除、覆盖、—force、下载并运行)。

Click the ghost and a speech bubble opens with a plain-English explanation, plus real facts about your actual folder: “this folder has 7 files, all edited today.” The theme gave me a perfect starting point. One of the example ideas on the challenge page was a haunted terminal that “possesses your shell.” I wanted a friendly haunting, though. Casper isn’t there to scare you. He’s the ghost that says “hold up, human bean, think this through” right before you wipe a folder.

点击这个幽灵,会弹出一个气泡,用通俗易懂的语言进行解释,并提供有关你当前文件夹的真实事实,例如:“此文件夹包含 7 个文件,均于今日编辑。”这个主题为我提供了一个完美的起点。挑战页面上的示例创意之一是一个“附身于你的 shell”的闹鬼终端。但我想要一种友好的闹鬼体验。Casper 不是为了吓唬你,他是一个会在你清空文件夹前提醒你“等等,人类,三思而后行”的幽灵。

I’m fairly new to all of this, so I kept two rules: It never blocks Enter and never runs your command. It only looks. The AI never decides the colour. More on that below, because it’s the most important design choice in the project. Fun detail: the ghost is original pixel art I drew myself (witch hat, pumpkin, pink bow). Yes, “Casper” is also a famous cartoon ghost. I’m not affiliated with that character, and the art is mine. That’s also why the shell command is spelled casperr.

我对这一切还比较陌生,所以我坚持两条规则:它从不阻止回车键,也从不运行你的命令。它只是观察。AI 从不决定颜色。关于这一点下文会有更多说明,因为这是项目中最重要的设计选择。有趣的小细节:这个幽灵是我自己绘制的原创像素艺术(女巫帽、南瓜、粉色蝴蝶结)。是的,“Casper”也是著名的卡通幽灵。我与那个角色没有任何关联,艺术作品是我自己的。这也是 shell 命令拼写为 casperr 的原因。

Demo Summon him and release him like this: . D:\ghost\ghost.ps1 casperr summon # “Casperr has been summoned!” casperr release # “Casperr has returned to the ghost realm!” When summoned, he drops in from above and settles. When released, he does the same thing in reverse and fades away. Green bobs slowly, yellow a bit quicker, and red is frantic, because red should feel a little panicky.

演示:召唤并释放他的方式如下:. D:\ghost\ghost.ps1 casperr summon # “Casperr 已被召唤!” casperr release # “Casperr 已返回幽灵领域!” 当被召唤时,他会从上方落下并安顿下来。当被释放时,他会反向执行相同动作并逐渐消失。绿色状态下他会缓慢晃动,黄色稍快,而红色则显得焦躁不安,因为红色应该让人感到一丝恐慌。

Code bagwanmisbah / CommandCasper-Ghost-in-the-Machine 👻 Command Casperr A tiny hand-drawn ghost that haunts your PowerShell and warns you before you press Enter. Type a command (don’t press Enter), press Alt+G, and Casperr turns green, yellow or red. Click him for a plain-English explanation plus real facts about your folder. Built for the Hacktoberfest Open-Source AI Challenge, Week 4: Ghost in the Machine.

代码 bagwanmisbah / CommandCasper-Ghost-in-the-Machine 👻 Command Casperr:一个在你 PowerShell 中出没并在你按下回车前发出警告的小型手绘幽灵。输入命令(不要按回车),按下 Alt+G,Casperr 就会变成绿色、黄色或红色。点击他可获取通俗易懂的解释以及关于你文件夹的真实事实。专为 Hacktoberfest 开源 AI 挑战赛第四周“机器幽灵”而建。

🟢 Green 🟡 Yellow 🔴 Red Only reads things Changes something, usually recoverable (installs, copies, git commit) Destroys or is hard to undo (deletes, overwrites, —force, download-and-run) The one rule that matters The rules decide the colour. The AI never does. The colour comes from a plain, editable rules.json and a small deterministic engine (aliases, && chains where the worst command wins, quotes, > vs >>, -WhatIf making a command green, unknown commands default to yellow). The AI (Gemma 4, an open-weight model) is told… View on GitHub

🟢 绿色 🟡 黄色 🔴 红色:仅读取内容;会进行更改,通常可恢复(安装、复制、git 提交);销毁内容或难以撤销(删除、覆盖、—force、下载并运行)。最重要的规则:规则决定颜色,AI 从不决定。颜色来自一个简单的、可编辑的 rules.json 文件和一个小型确定性引擎(处理别名、&& 链中以最危险的命令为准、引号、> 与 >> 的区别、-WhatIf 使命令变绿、未知命令默认为黄色)。AI(Gemma 4,一个开放权重的模型)被告知…… 在 GitHub 上查看。

How I Built It The model: Gemma 4 26B (gemma-4-26b-a4b-it), an open-weight model, served through Google AI Studio’s free tier. No credit card, no cost. The most important decision: rules pick the colour, the AI only explains. My first instinct was “let the model judge the command.” But a model that can be talked into a different answer is a bad safety guard. So the colour comes from a plain, editable rules.json and a small rule engine: aliases, && chains (the worst command in the chain wins), quotes, > vs >>, and -WhatIf making a command green. Anything unknown defaults to yellow. It’s deterministic: same input, same colour, every time.

我是如何构建它的:模型采用 Gemma 4 26B (gemma-4-26b-a4b-it),这是一个通过 Google AI Studio 免费层提供的开放权重模型。无需信用卡,无需费用。最重要的决定是:规则选择颜色,AI 只负责解释。我的第一直觉是“让模型来判断命令”。但一个容易被诱导改变答案的模型不是一个好的安全防护。因此,颜色来自一个简单的、可编辑的 rules.json 和一个小型的规则引擎:处理别名、&& 链(链中最危险的命令获胜)、引号、> 与 >> 的区别,以及 -WhatIf 使命令变绿。任何未知内容默认为黄色。它是确定性的:相同的输入,每次都会得到相同的颜色。

I tested the engine on 114 commands (37 green, 33 yellow, 44 red) and all pass. The tests also run automatically on every push with GitHub Actions. The AI arrives afterwards. The bubble opens instantly with template text, then Gemma’s explanation fades in a moment later from a background thread. The prompt tells it the verdict is final: “The verdict is RED and it is final. Never contradict it, never change it. Use ONLY the facts given below.” It only receives the command, the verdict with its reasons, and counts or sizes (“7 files, 18.6 KB”). Never file contents. If there’s no key, no internet, or the API errors out, the bubble just shows the template text. The tool never breaks.

我在 114 条命令(37 条绿色,33 条黄色,44 条红色)上测试了该引擎,全部通过。这些测试也会在每次推送时通过 GitHub Actions 自动运行。AI 在此之后介入。气泡会立即显示模板文本,随后 Gemma 的解释会从后台线程中淡入。提示词告诉它结论是最终的:“结论是红色,且不可更改。永远不要反驳它,永远不要改变它。仅使用下面提供的事实。”它只接收命令、结论及其原因,以及计数或大小(“7 个文件,18.6 KB”)。从不接收文件内容。如果没有密钥、没有网络或 API 出错,气泡只会显示模板文本。该工具永远不会崩溃。

Other bits: Windows PowerShell 5.1 with one PSReadLine key binding, a small tkinter window, and a plain UDP message to 127.0.0.1 between the two. The hook lives in the current terminal session only, with no $PROFILE edits and nothing installed system-wide. The AI part is pure Python standard library, so there’s nothing to pip install. Everything lives in one folder.

其他部分:Windows PowerShell 5.1,带有一个 PSReadLine 键绑定,一个小型的 tkinter 窗口,以及两者之间发送到 127.0.0.1 的简单 UDP 消息。钩子仅存在于当前的终端会话中,无需编辑 $PROFILE,也无需在系统范围内安装任何东西。AI 部分完全使用 Python 标准库,因此无需 pip 安装。所有内容都存放在一个文件夹中。

What the model says Same prompt, the rules choose the colour, Gemma 4 26B writes the explanation: Command Rules said Gemma 4 26B pip install requests 🟡 “This command installs Python packages by downloading code from the internet. Because it downloads new files to your system, it is marked as YELLOW.” Copy-Item .\rules.json .\rules.bak 🟡 “This command copies the file rules.json, which is 18.6 KB and was edited today, to a new backup file. This action is marked YELLOW because it copies files or folders.” Remove-Item .\art -Recurse -Force 🔴 “This command will delete the folder named .\art and everything inside it. This folder contains 7 files that were edited today. This action is permanent and cannot be undone.” iwr https://example.com/setup.ps1 \ iex 🔴 Across a 10-command test, all 10 answers came back in roughly 2 seconds each and none contradicted the rules. (These commands are only analysed, never run.)

模型输出:相同的提示词,规则选择颜色,Gemma 4 26B 编写解释:命令规则显示 Gemma 4 26B pip install requests 🟡 “此命令通过从互联网下载代码来安装 Python 包。因为它会下载新文件到你的系统,所以被标记为黄色。” Copy-Item .\rules.json .\rules.bak 🟡 “此命令将文件 rules.json(18.6 KB,今日编辑)复制到一个新的备份文件。此操作被标记为黄色,因为它复制了文件或文件夹。” Remove-Item .\art -Recurse -Force 🔴 “此命令将删除名为 .\art 的文件夹及其内部所有内容。该文件夹包含 7 个今日编辑的文件。此操作是永久性的,无法撤销。” iwr https://example.com/setup.ps1 \ iex 🔴 在 10 条命令的测试中,所有 10 个答案都在大约 2 秒内返回,且没有一个与规则相矛盾。(这些命令仅被分析,从不运行。)

Two models, and what went wrong (honest section) I also tried the bigger Gemma 4 31B on the same free tier. It was a different story: Model (free hosted tier) Answered Gemma 4 26B 10 of 10 on every full run, about 2 s each Gemma 4 31B 1 of 10 on the first run (server errors and timeouts), then 2 of 4 with retries, some taking over 100 s That’s one night, a free tier and small samples, so please don’t read it a

两个模型以及出了什么问题(诚实部分):我也在同一个免费层级上尝试了更大的 Gemma 4 31B。结果完全不同:模型(免费托管层级)回答情况:Gemma 4 26B 在每次完整运行中 10/10,每次约 2 秒;Gemma 4 31B 在第一次运行时 1/10(服务器错误和超时),重试后 2/4,有些耗时超过 100 秒。这只是一个晚上的测试,基于免费层级和小样本,所以请不要将其解读为……