summaryrefslogtreecommitdiff
path: root/server/continuedev/plugins/context_providers/terminal.py
blob: c63239e44c5bcdb3d10a774546d666ab0f82d1d6 (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
from typing import Any, Coroutine, List

from pydantic import Field

from ...core.context import ContextProvider
from ...core.main import ChatMessage, ContextItem, ContextItemDescription, ContextItemId


class TerminalContextProvider(ContextProvider):
    """Type '@terminal' to reference the contents of your IDE's terminal."""

    title = "terminal"
    display_title = "Terminal"
    description = "Reference the contents of the terminal"
    dynamic = True

    get_last_n_commands: int = Field(
        3, description="The number of previous commands to reference"
    )

    def _terminal_context_item(self, content: str = ""):
        return ContextItem(
            content=content,
            description=ContextItemDescription(
                name="Terminal",
                description="Reference the contents of the VS Code terminal",
                id=ContextItemId(provider_title=self.title, item_id=self.title),
            ),
        )

    async def get_chat_messages(self) -> Coroutine[Any, Any, List[ChatMessage]]:
        msgs = await super().get_chat_messages()
        for msg in msgs:
            msg.summary = msg.content[-1000:]
        return msgs

    async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]:
        return [self._terminal_context_item()]

    async def get_item(self, id: ContextItemId, query: str) -> ContextItem:
        if not id.provider_title == self.title:
            raise Exception("Invalid provider title for item")

        terminal_contents = await self.sdk.ide.getTerminalContents(
            self.get_last_n_commands
        )
        terminal_contents = terminal_contents[-5000:]

        return self._terminal_context_item(terminal_contents)