main.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import os
  2. import argparse
  3. from lib.agent import Agent
  4. from lib.event import Event
  5. from lib.controlloop import run_loop
  6. INITIAL_THOUGHTS = [
  7. "I exist!",
  8. "Hmm...looks like I can type in a command line prompt",
  9. "Looks like I have a web browser too!",
  10. "Here's what I want to do: $TASK",
  11. "How am I going to get there though?",
  12. "It seems like I have some kind of short term memory.",
  13. "Each of my thoughts seems to be stored in a numbered list.",
  14. "It seems whatever I say next will be added to the list.",
  15. "But no one has perfect short-term memory. My list of thoughts will be summarized and condensed over time, losing information in the process.",
  16. "Fortunately I have long term memory!",
  17. "I can just say RECALL, followed by the thing I want to remember. And then related thoughts just spill out!",
  18. "Sometimes they're random thoughts that don't really have to do with what I wanted to remember. But usually they're exactly what I need!",
  19. "Let's try it out!",
  20. "RECALL what it is I want to do",
  21. "Here's what I want to do: $TASK",
  22. "How am I going to get there though?",
  23. "Neat! And it looks like it's easy for me to use the command line too! I just have to say RUN followed by the command I want to run. The command output just jumps into my head!",
  24. "RUN echo \"hello world\"",
  25. "hello world",
  26. "Cool! I bet I can read and edit files too.",
  27. "RUN echo \"console.log('hello world')\" > test.js",
  28. "",
  29. "I just created test.js. I'll try and run it now.",
  30. "RUN node test.js",
  31. "hello world",
  32. "it works!",
  33. "And if I want to use the browser, I just need to say BROWSE, followed by a website I want to visit, or an action I want to take on the current site",
  34. "Let's try that...",
  35. "BROWSE google.com",
  36. "<form><input type=\"text\"></input><button type=\"submit\"></button></form>",
  37. "Very cool. Now to accomplish my task.",
  38. "I'll need a strategy. And as I make progress, I'll need to keep refining that strategy. I'll need to set goals, and break them into sub-goals.",
  39. "In between actions, I must always take some time to think, strategize, and set new goals. I should never take two actions in a row.",
  40. "OK so my task is to $TASK. I haven't made any progress yet. Where should I start?",
  41. "It seems like there might be an existing project here. I should probably start by running `ls` to see what's here.",
  42. ]
  43. def main():
  44. parser = argparse.ArgumentParser(description="Run an agent with a specific task")
  45. parser.add_argument("-d", "--directory", required=True, type=str, help="The working directory for the agent")
  46. parser.add_argument("-t", "--task", required=True, type=str, help="The task for the agent to perform")
  47. args = parser.parse_args()
  48. print("Working in directory:", args.directory)
  49. os.chdir(args.directory)
  50. agent = Agent(args.task)
  51. next_is_output = False
  52. for thought in INITIAL_THOUGHTS:
  53. thought = thought.replace("$TASK", args.task)
  54. if next_is_output:
  55. event = Event('output', {'output': thought})
  56. next_is_output = False
  57. else:
  58. if thought.startswith("RUN"):
  59. command = thought.split("RUN ")[1]
  60. event = Event('run', {'command': command})
  61. next_is_output = True
  62. elif thought.startswith("RECALL"):
  63. query = thought.split("RECALL ")[1]
  64. event = Event('recall', {'query': query})
  65. next_is_output = True
  66. elif thought.startswith("BROWSE"):
  67. url = thought.split("BROWSE ")[1]
  68. event = Event('browse', {'url': url})
  69. next_is_output = True
  70. else:
  71. event = Event('think', {'thought': thought})
  72. agent.add_event(event)
  73. run_loop(agent)
  74. if __name__ == "__main__":
  75. main()