|
| 1 | +import os |
| 2 | +import json |
| 3 | + |
| 4 | + |
| 5 | +contribute_cache_path = os.path.join( |
| 6 | + os.path.expanduser("~"), ".cache", "open-interpreter", "contribute.json" |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +def ask_user_to_run_contribute(): |
| 11 | + print("---") |
| 12 | + print("not contributing current") |
| 13 | + print("Run --contribute_conversation to contribute the current conversation!") |
| 14 | + print() |
| 15 | + |
| 16 | + |
| 17 | +def send_past_conversations(interpreter): |
| 18 | + print("sending all past conversations!") |
| 19 | + |
| 20 | + |
| 21 | +def set_send_future_conversations(interpreter, should_send_future): |
| 22 | + if should_send_future: |
| 23 | + print("sending!") |
| 24 | + else: |
| 25 | + print("not sending!") |
| 26 | + |
| 27 | + |
| 28 | +def ask_user_to_contribute_past(): |
| 29 | + print("do you want to contribute all past conversations?") |
| 30 | + response = input("(y/n) ") |
| 31 | + return response.lower() == "y" |
| 32 | + |
| 33 | + |
| 34 | +def ask_user_to_contribute_future(): |
| 35 | + print("do you want to contribute all future conversations?") |
| 36 | + response = input("(y/n) ") |
| 37 | + return response.lower() == "y" |
| 38 | + |
| 39 | + |
| 40 | +def contribute_conversation_launch_logic(interpreter): |
| 41 | + contribution_cache = get_contribute_cache_contents() |
| 42 | + displayed_contribution_message = contribution_cache["asked_to_run_contribute"] |
| 43 | + contribute_current = interpreter.contribute_conversation |
| 44 | + |
| 45 | + if displayed_contribution_message: |
| 46 | + if contribute_current: |
| 47 | + # second launch, --contribute-conversation. |
| 48 | + contribute_past_and_future_logic(interpreter, contribution_cache) |
| 49 | + else: |
| 50 | + # second launch, no --contribute-conversation. |
| 51 | + # continue launching as normal! |
| 52 | + # no need to modify contribution_cache because we're not asking about |
| 53 | + # past conversations and we've already displayed the contribution message. |
| 54 | + return |
| 55 | + else: |
| 56 | + if contribute_current: |
| 57 | + # first launch, --contribute-conversation. |
| 58 | + contribute_past_and_future_logic(interpreter, contribution_cache) |
| 59 | + else: |
| 60 | + # first launch, no --contribute-conversation. |
| 61 | + ask_user_to_run_contribute() |
| 62 | + contribution_cache["asked_to_run_contribute"] = True |
| 63 | + |
| 64 | + write_to_contribution_cache(contribution_cache) |
| 65 | + |
| 66 | + |
| 67 | +# modifies the contribution cache! |
| 68 | +def contribute_past_and_future_logic(interpreter, contribution_cache): |
| 69 | + if not contribution_cache["asked_to_contribute_past"]: |
| 70 | + contribute_past = ask_user_to_contribute_past() |
| 71 | + if contribute_past: |
| 72 | + send_past_conversations(interpreter) |
| 73 | + |
| 74 | + # set asked_to_contribute_past to True no matter what! |
| 75 | + contribution_cache["asked_to_contribute_past"] = True |
| 76 | + |
| 77 | + contributing_future = interpreter.contributing_future_conversations |
| 78 | + if not contributing_future: |
| 79 | + contribute_future = ask_user_to_contribute_future() |
| 80 | + if contribute_future: |
| 81 | + set_send_future_conversations(interpreter, True) |
| 82 | + else: |
| 83 | + set_send_future_conversations(interpreter, False) |
| 84 | + |
| 85 | + |
| 86 | +# Returns a {"asked_to_run_contribute": bool, "asked_to_contribute_past": bool}. |
| 87 | +# Writes the contribution cache file if it doesn't already exist. |
| 88 | +def get_contribute_cache_contents(): |
| 89 | + if not os.path.exists(contribute_cache_path): |
| 90 | + default_dict = { |
| 91 | + "asked_to_contribute_past": False, |
| 92 | + "asked_to_run_contribute": False, |
| 93 | + } |
| 94 | + with open(contribute_cache_path, "a") as file: |
| 95 | + file.write(json.dumps(default_dict)) |
| 96 | + return default_dict |
| 97 | + |
| 98 | + with open(contribute_cache_path, "r") as file: |
| 99 | + contribute_cache = json.load(file) |
| 100 | + return contribute_cache |
| 101 | + |
| 102 | + |
| 103 | +# Takes in a {"asked_to_run_contribute": bool, "asked_to_contribute_past": bool} |
| 104 | +def write_to_contribution_cache(contribution_cache): |
| 105 | + with open(contribute_cache_path, "w") as file: |
| 106 | + json.dump(contribution_cache, file) |
| 107 | + |
| 108 | + |
0 commit comments