summaryrefslogtreecommitdiff
path: root/server/continuedev/libs/util/paths.py
blob: 33b590867f0d0963922dcafa0af5cb629aad75ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import re
from typing import Optional

from ..constants.default_config import default_config
from ..constants.main import (
    CONTINUE_GLOBAL_FOLDER,
    CONTINUE_SERVER_FOLDER,
    CONTINUE_SESSIONS_FOLDER,
)


def find_data_file(filename):
    datadir = os.path.dirname(__file__)
    return os.path.abspath(os.path.join(datadir, filename))


def getGlobalFolderPath():
    path = os.path.join(os.path.expanduser("~"), CONTINUE_GLOBAL_FOLDER)
    os.makedirs(path, exist_ok=True)
    return path


def getSessionsFolderPath():
    path = os.path.join(getGlobalFolderPath(), CONTINUE_SESSIONS_FOLDER)
    os.makedirs(path, exist_ok=True)
    return path


def getServerFolderPath():
    path = os.path.join(getGlobalFolderPath(), CONTINUE_SERVER_FOLDER)
    os.makedirs(path, exist_ok=True)
    return path


def getDevDataFolderPath():
    path = os.path.join(getGlobalFolderPath(), "dev_data")
    os.makedirs(path, exist_ok=True)
    return path


def getDiffsFolderPath():
    path = os.path.join(getGlobalFolderPath(), "diffs")
    os.makedirs(path, exist_ok=True)
    return path


def getDevDataFilePath(table_name: str):
    filepath = os.path.join(getDevDataFolderPath(), f"{table_name}.jsonl")
    if not os.path.exists(filepath):
        with open(filepath, "w") as f:
            f.write("")

    return filepath


def getMeilisearchExePath():
    binary_name = "meilisearch.exe" if os.name == "nt" else "meilisearch"
    path = os.path.join(getServerFolderPath(), binary_name)
    return path


def getSessionFilePath(session_id: str):
    path = os.path.join(getSessionsFolderPath(), f"{session_id}.json")
    os.makedirs(os.path.dirname(path), exist_ok=True)
    return path


def getSessionsListFilePath():
    path = os.path.join(getSessionsFolderPath(), "sessions.json")
    os.makedirs(os.path.dirname(path), exist_ok=True)
    if not os.path.exists(path):
        with open(path, "w") as f:
            f.write("[]")
    return path


def migrateConfigFile(existing: str) -> Optional[str]:
    if existing.strip() == "":
        return default_config

    migrated = (
        existing.replace("MaybeProxyOpenAI", "OpenAIFreeTrial")
        .replace("maybe_proxy_openai", "openai_free_trial")
        .replace("unused=", "saved=")
        .replace("medium=", "summarize=")
    )
    if migrated != existing:
        return migrated

    return None


def getConfigFilePath() -> str:
    path = os.path.join(getGlobalFolderPath(), "config.py")
    os.makedirs(os.path.dirname(path), exist_ok=True)

    if not os.path.exists(path):
        with open(path, "w") as f:
            f.write(default_config)
    else:
        # Make any necessary migrations
        with open(path, "r") as f:
            existing_content = f.read()

        migrated = migrateConfigFile(existing_content)

        if migrated is not None:
            with open(path, "w") as f:
                f.write(migrated)

    return path


def convertConfigImports(shorten: bool) -> str:
    path = getConfigFilePath()
    # Make any necessary migrations
    with open(path, "r") as f:
        existing_content = f.read()

    if shorten:
        migrated = existing_content.replace(
            "from continuedev.src.continuedev.", "from continuedev."
        )
    else:
        migrated = re.sub(
            r"(?<!src\.)continuedev\.(?!src)",
            "continuedev.",
            existing_content,
        )

    with open(path, "w") as f:
        f.write(migrated)


def getLogFilePath():
    path = os.path.join(getGlobalFolderPath(), "continue.log")
    os.makedirs(os.path.dirname(path), exist_ok=True)
    return path


def getSavedContextGroupsPath():
    path = os.path.join(getGlobalFolderPath(), "saved_context_groups.json")
    os.makedirs(os.path.dirname(path), exist_ok=True)
    if not os.path.exists(path):
        with open(path, "w") as f:
            f.write("{}")
    return path