main
 1# frozen_string_literal: true
 2
 3Elelem::Plugins.register(:shell) do |agent|
 4  strip_ansi = ->(text) do
 5    text
 6      .gsub(/^Script started.*?\n/, "")
 7      .gsub(/\nScript done.*$/, "")
 8      .gsub(/\e\].*?(?:\a|\e\\)/, "")
 9      .gsub(/\e\[[0-9;?]*[A-Za-z]/, "")
10      .gsub(/\e[PX^_].*?\e\\/, "")
11      .gsub(/\e./, "")
12      .gsub(/[\b]/, "")
13      .gsub(/\r/, "")
14  end
15
16  agent.commands.register("shell", description: "Start interactive shell") do
17    transcript = Tempfile.create do |file|
18      system("script", "-q", file.path, chdir: Dir.pwd)
19      strip_ansi.call(File.read(file.path))
20    end
21    agent.conversation.add(role: "user", content: transcript) unless transcript.strip.empty?
22  end
23end