跳到正文
DEV Community· TAIL·· 2 小時前AI 評分43

Haunted Terminal:以本機 Gemma 為 PowerShell 指令加入幽靈低語

Haunted Terminal

AI 導讀

Haunted Terminal 是一個 PowerShell 腳本,使用 Ollama 本機執行的 Gemma,在每次指令執行後低語評論;預設模型為 gemma3:4b。指令失敗時會顯示血滴效果,Ollama 無法回應時則以預設文字代替;指令和輸出只會傳送至 127.0.0.1。

正文

This is a submission for the Hacktoberfest Open-Source AI Challenge Week 4: Ghost in the Machine

What I Built

A haunted terminal. Haunted.ps1 is a single PowerShell file that possesses your real shell. After every command you run, a ghost whispers a short, eerie comment about what you just did. It's written by a local Gemma model and typed onto the screen one character at a time, like breath fogging a window.

I took "Ghost in the Machine" literally: the machine is your terminal, and the ghost is a model living inside it. Failed commands get a blood drip and a mournful whisper. Successful ones get something quieter and creepier. The prompt becomes ☠ ~\path ›, and each new session opens with a small ghost and a few drips of blood. In Windows Terminal, the window becomes frosted glass.

It's one file with three modes, so it's easy to try and easy to remove:

  • . .\Haunted.ps1 haunts the current tab only, and unhaunt undoes it
  • . .\Haunted.ps1 -Install makes every new tab haunted
  • . .\Haunted.ps1 -Uninstall removes everything it added

Demo

A walkthrough of one session, from loading the script to a failed command.

1. Loading the script

After loading the Haunted.ps1

I run . .\Haunted.ps1. The terminal clears, a small ghost appears with blood dripping from it, and the prompt changes to ☠ ~\path ›. Nothing else is installed or changed, and unhaunt undoes it.

2. The ghost speaks

Echo marked reply 1

Echo marked reply 2

After each command, a whisper is typed onto the screen under it. These two replies are marked (echo). That is the offline fallback: when Ollama doesn't answer, the ghost leaves a canned line instead of going silent.

3. A normal command

ls command

ls runs as usual, and the ghost comments on it afterward. The output is untouched, and the whisper appears below it.

4. A command that fails

ls -lf error bleed

I typed ls -lf out of Linux habit, and PowerShell rejected it. A failed command makes the glass "bleed": a blood-red line and drip are drawn before the ghost speaks, with a more mournful whisper.

The ghost's whisper through genma, then a reply to git --version

A live Gemma whisper. This one is real, not the (echo) fallback. After the ghost's entrance, I ran git --version. The output is untouched: git version 2.55.0.windows.5. Then, the local Gemma model replied by reacting to the actual output, mentioning the version number, "2.55.0".

Code

How I Built It

  • Model: Gemma (gemma3:4b by default, or any tag via $env:HAUNT_MODEL or haunt model <name>), served locally by Ollama.
  • The hook: I replace PowerShell's prompt function. Each time it runs, it checks whether a new command appeared in history, then wakes the ghost.
  • What the ghost sees: the command, whether it succeeded or failed, and the last 1200 characters of its output. PowerShell doesn't keep output after a command, so the script reads it from a private temporary transcript. That file is deleted when the session ends.
  • Streaming: the reply comes from Ollama's streaming /api/chat endpoint. Each token is printed the moment it arrives, with a tiny per-character delay. This removes most of the waiting and gives the "breathing on glass" effect.
  • Prompting: the system prompt makes the ghost lowercase, intimate and brief (under 28 words), tied to the actual command and its output, and forbids advice, so it whispers instead of helping.
  • Look: 24-bit ANSI colours for the prompt, syntax highlighting and whispers. The glass theme is a Windows Terminal JSON fragment, so my own settings.json is never edited.
  • Safe to remove: -Install adds one marked line to $PROFILE plus the fragment file. -Uninstall deletes exactly those two things.
  • Offline fallback: if Ollama isn't running, the ghost leaves a canned echo marked (echo) instead of going silent.

Known limits: full-screen programs such as vim or ssh write straight to the console, so the ghost sees only the command, not the output. The glass effect only works in Windows Terminal.

Why Does Open Innovation Matter?

  • Your shell history is private. Commands and output can contain paths, hostnames, tokens and project names. Here they go to 127.0.0.1 and nowhere else. I wouldn't want to stream every keystroke of my terminal to a hosted API for a joke.
  • It works with the internet off. A ghost that lives in your machine should keep talking when the Wi-Fi dies.
  • Swappable and tunable. Changing the model is one command (haunt model gemma3:12b). The personality is a plain system prompt anyone can change, whether to make the ghost a grumpy sysadmin or an ancient programming language.
  • Free to run. Every command gets a whisper. With a hosted model that would be a metered bill for a Halloween joke. Locally it costs electricity.
  • It won't disappear. Open weights plus a plain script mean this keeps working years from now, even if an API gets deprecated or repriced.

Prize Categories

  • Best Use of Gemma:

來源:DEV Community · dev.to