summaryrefslogtreecommitdiff
path: root/server/continuedev/plugins/steps/chroma.py
blob: f357a8726ff4c510efb3637c9f3d71376b03d794 (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
from textwrap import dedent
from typing import Coroutine, Union

from ...core.main import Step
from ...core.observation import Observation
from ...core.sdk import ContinueSDK
from ...core.steps import EditFileStep
from ...libs.chroma.query import ChromaIndexManager


class CreateCodebaseIndexChroma(Step):
    name: str = "Create Codebase Index"
    hide: bool = True

    async def describe(self, llm) -> Coroutine[str, None, None]:
        return "Indexing the codebase..."

    async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]:
        index = ChromaIndexManager(await sdk.ide.getWorkspaceDirectory())
        if not index.check_index_exists():
            self.hide = False
        index.create_codebase_index()


class AnswerQuestionChroma(Step):
    question: str
    _answer: Union[str, None] = None
    name: str = "Answer Question"

    async def describe(self, llm) -> Coroutine[str, None, None]:
        if self._answer is None:
            return f"Answering the question: {self.question}"
        else:
            return self._answer

    async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]:
        index = ChromaIndexManager(await sdk.ide.getWorkspaceDirectory())
        results = index.query_codebase_index(self.question)

        code_snippets = ""

        files = []
        for node in results.source_nodes:
            resource_name = list(node.node.relationships.values())[0]
            filepath = resource_name[: resource_name.index("::")]
            files.append(filepath)
            code_snippets += f"""{filepath}```\n{node.node.text}\n```\n\n"""

        prompt = dedent(
            f"""Here are a few snippets of code that might be useful in answering the question:

            {code_snippets}

            Here is the question to answer:

            {self.question}

            Here is the answer:"""
        )

        answer = await sdk.models.summarize.complete(prompt)
        # Make paths relative to the workspace directory
        answer = answer.replace(await sdk.ide.getWorkspaceDirectory(), "")

        self._answer = answer

        await sdk.ide.setFileOpen(files[0])


class EditFileChroma(Step):
    request: str
    hide: bool = True

    async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]:
        index = ChromaIndexManager(await sdk.ide.getWorkspaceDirectory())
        results = index.query_codebase_index(self.request)

        resource_name = list(results.source_nodes[0].node.relationships.values())[0]
        filepath = resource_name[: resource_name.index("::")]

        await sdk.run_step(
            EditFileStep(
                filepath=filepath,
                prompt=f"Here is the code:\n\n{{code}}\n\nHere is the user request:\n\n{self.request}\n\nHere is the code after making the requested changes:\n",
            )
        )