87 lines
2.2 KiB
Python
87 lines
2.2 KiB
Python
import argparse
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
from agent import build_graph
|
|
from shell import ShellSession
|
|
from tools import set_workdir
|
|
|
|
|
|
def create_agent():
|
|
"""Create and return the compiled agent graph."""
|
|
return build_graph()
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="LangGraph file-system agent")
|
|
parser.add_argument("--workdir", type=str, default=None)
|
|
args = parser.parse_args()
|
|
|
|
if args.workdir is None:
|
|
print("Error: --workdir argument is required", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
workdir = Path(args.workdir).resolve()
|
|
|
|
if not workdir.exists():
|
|
print(f"Error: Workdir does not exist: {args.workdir}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
if not workdir.is_dir():
|
|
print(f"Error: Workdir is not a directory: {args.workdir}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
set_workdir(workdir)
|
|
|
|
shell = ShellSession(cwd=str(workdir))
|
|
agent = create_agent()
|
|
|
|
print(f"Agent ready. Working directory: {workdir}")
|
|
print("Type '.exit' or '.quit' to exit.")
|
|
print("Prefix with '!' to run a shell command.\n")
|
|
|
|
while True:
|
|
try:
|
|
user_input = input("> ")
|
|
except KeyboardInterrupt:
|
|
print("\nExiting...")
|
|
break
|
|
|
|
stripped = user_input.strip()
|
|
|
|
if stripped.lower() in (".exit", ".quit"):
|
|
print("Goodbye!")
|
|
break
|
|
|
|
if not stripped:
|
|
continue
|
|
|
|
if stripped.startswith("!"):
|
|
cmd = stripped[1:].strip()
|
|
if cmd:
|
|
try:
|
|
print(shell.run(cmd), end="")
|
|
except Exception as e:
|
|
print(f"Shell error: {e}")
|
|
continue
|
|
|
|
try:
|
|
response = agent.invoke({"messages": [("user", user_input)]})
|
|
messages = response.get("messages", [])
|
|
for msg in reversed(messages):
|
|
if hasattr(msg, "type") and msg.type == "ai" and msg.content:
|
|
print(msg.content)
|
|
break
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
shell.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|