MCPcopy
hub / github.com/stitionai/devika / start_interaction

Function start_interaction

src/browser/interaction.py:485–547  ·  view source on GitHub ↗
(model_id, objective, project_name)

Source from the content-addressed store, hash-verified

483 return elements_of_interest
484
485def start_interaction(model_id, objective, project_name):
486 _crawler = Crawler()
487
488 def print_help():
489 print(
490 "(g) to visit url\n(u) scroll up\n(d) scroll down\n(c) to click\n(t) to type\n" +
491 "(h) to view commands again\n(r/enter) to run suggested command\n(o) change objective"
492 )
493
494 def get_gpt_command(objective, url, previous_command, browser_content):
495 prompt = prompt_template
496 prompt = prompt.replace("$objective", objective)
497 prompt = prompt.replace("$url", url[:100])
498 prompt = prompt.replace("$previous_command", previous_command)
499 prompt = prompt.replace("$browser_content", browser_content[:4500])
500 response = LLM(model_id=model_id).inference(prompt)
501 return response
502
503 def run_cmd(cmd):
504 cmd = cmd.split("\n")[0]
505
506 if cmd.startswith("SCROLL UP"):
507 _crawler.scroll("up")
508 elif cmd.startswith("SCROLL DOWN"):
509 _crawler.scroll("down")
510 elif cmd.startswith("CLICK"):
511 commasplit = cmd.split(",")
512 id = commasplit[0].split(" ")[1]
513 _crawler.click(id)
514 elif cmd.startswith("TYPE"):
515 spacesplit = cmd.split(" ")
516 id = spacesplit[1]
517 text = " ".join(spacesplit[2:])
518 text = text[1:-1]
519 if cmd.startswith("TYPESUBMIT"):
520 text += '\n'
521 _crawler.type(id, text)
522
523 time.sleep(2)
524
525 gpt_cmd = ""
526 prev_cmd = ""
527 _crawler.go_to_page("google.com")
528
529 try:
530 visits = 0
531
532 while True and visits < 5:
533 browser_content = "\n".join(_crawler.crawl())
534 prev_cmd = gpt_cmd
535
536 current_url = _crawler.page.url
537
538 _crawler.screenshot(project_name)
539
540 gpt_cmd = get_gpt_command(objective, current_url, prev_cmd, browser_content).strip()
541 run_cmd(gpt_cmd)
542

Callers 1

make_decisionMethod · 0.90

Calls 6

go_to_pageMethod · 0.95
crawlMethod · 0.95
screenshotMethod · 0.95
CrawlerClass · 0.85
get_gpt_commandFunction · 0.85
run_cmdFunction · 0.85

Tested by

no test coverage detected