Commit 08fd905

mo khan <mo@mokhan.ca>
2026-01-22 04:58:45
refactor: restore the /shell command
1 parent da61a22
Changed files (2)
lib/elelem/agent.rb
@@ -2,7 +2,7 @@
 
 module Elelem
   class Agent
-    COMMANDS = %w[/clear /context /init /exit /help].freeze
+    COMMANDS = %w[/clear /context /init /shell /exit /help].freeze
     MAX_CONTEXT_MESSAGES = 50
     INIT_PROMPT = <<~PROMPT
       AGENTS.md generator. Analyze codebase and write AGENTS.md to project root.
@@ -52,6 +52,10 @@ module Elelem
       case input
       when "/exit" then exit(0)
       when "/init" then init_agents_md
+      when "/shell"
+        transcript = start_shell
+        history << { role: "user", content: transcript } unless transcript.strip.empty?
+        terminal.say "  → shell session captured"
       when "/clear"
         @history = []
         @memory = nil
@@ -59,7 +63,7 @@ module Elelem
       when "/context"
         terminal.say JSON.pretty_generate(combined_history)
       else
-        terminal.say "/clear /context /exit"
+        terminal.say COMMANDS.join(" ")
       end
     end
 
@@ -111,6 +115,22 @@ module Elelem
       sub.turn("Generate AGENTS.md for this project")
     end
 
+    def start_shell
+      Tempfile.create do |file|
+        system("script", "-q", file.path, chdir: Dir.pwd)
+        strip_ansi(File.read(file.path))
+      end
+    end
+
+    def strip_ansi(text)
+      text.gsub(/^Script started.*?\n/, "")
+          .gsub(/\nScript done.*$/, "")
+          .gsub(/\e\[[0-9;]*[a-zA-Z]/, "")
+          .gsub(/\e\[\?[0-9]+[hl]/, "")
+          .gsub(/[\b]/, "")
+          .gsub(/\r/, "")
+    end
+
     def fetch_response(ctx)
       content = ""
       tool_calls = client.fetch(combined_history + ctx, toolbox.to_a) do |delta|
CHANGELOG.md
@@ -8,6 +8,7 @@
 - **MCP (Model Context Protocol)** server support via `.mcp.json` configuration
 - **AGENTS.md** file support - searches up directory tree for project instructions
 - **`/init` command** to generate an AGENTS.md file for the current project
+- **`/shell` command** to drop into a shell session and capture the transcript to context
 - **`task` tool** for delegating subtasks to focused sub-agents
 - **`edit` tool** for replacing first occurrence of text in a file
 - **`verify` tool** for syntax checking and running project tests
@@ -36,8 +37,8 @@
 - **Breaking**: Tool schema uses OpenAI format (`{type: "function", function: {...}}`)
 - **Breaking**: Tool definitions use `description:` key (was `desc:`)
 - **Breaking**: Removed modes and permissions system entirely
-- **Breaking**: Removed slash commands (`/mode`, `/env`, `/shell`, `/provider`, `/model`)
-  - Only `/clear`, `/context`, `/exit`, `/help` remain
+- **Breaking**: Removed slash commands (`/mode`, `/env`, `/provider`, `/model`)
+  - Remaining: `/clear`, `/context`, `/init`, `/shell`, `/exit`, `/help`
 - **Breaking**: Removed many dependencies
   - Removed: `thor`, `cli-ui`, `erb`, `cgi`, `set`, `timeout`, `logger`, `net-llm`, `json-schema`
   - Added: `json_schemer`, `optparse`, `tempfile`, `stringio`, `uri`