Packages

ExClaw — OpenClaw rebuilt on ADK Elixir

Current section

Files

Jump to
ex_claw lib ex_claw tool write.ex
Raw

lib/ex_claw/tool/write.ex

defmodule ExClaw.Tool.Write do
@moduledoc "Write content to a file, auto-creating parent directories. PathGuard enforced."
alias ExClaw.Security
def new do
ADK.Tool.FunctionTool.new(:write,
description: "Write content to a file. Creates parent directories if needed.",
func: {__MODULE__, :call},
parameters: %{
type: "object",
properties: %{
path: %{type: "string", description: "File path to write"},
content: %{type: "string", description: "Content to write"}
},
required: ["path", "content"]
}
)
end
def call(_ctx, args) do
path = Map.fetch!(args, "path")
content = Map.fetch!(args, "content")
sec_config = load_security_config()
if Security.check_path(path, sec_config) do
path |> Path.dirname() |> File.mkdir_p!()
case File.write(path, content) do
:ok -> {:ok, "Wrote #{byte_size(content)} bytes to #{path}"}
{:error, reason} -> {:error, "Failed to write #{path}: #{reason}"}
end
else
{:error, "path not allowed: #{path}"}
end
end
defp load_security_config do
try do
config = ExClaw.Config.get([:security]) || %{}
if is_map(config), do: config, else: %{}
rescue
_ -> %{}
catch
:exit, _ -> %{}
end
end
end