Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Adds cache to cli-chat commands #738

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ alembic = "==1.14.1"
pygments = "==2.19.1"
sqlite-vec-sl-tmp = "==0.0.4"
greenlet = "==3.1.1"
cachetools = "==5.5.1"

[tool.poetry.group.dev.dependencies]
pytest = "==8.3.4"
Expand Down
46 changes: 44 additions & 2 deletions src/codegate/pipeline/cli/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod
from typing import Awaitable, Callable, Dict, List, Tuple
from typing import Awaitable, Callable, Dict, List, Optional, Tuple

from cachetools import TTLCache
from pydantic import ValidationError

from codegate import __version__
Expand All @@ -16,6 +17,11 @@ class NoSubcommandError(Exception):
pass


# 1 second cache. 1 second is to be short enough to not affect UX but long enough to
# reply the same to concurrent requests. Needed for Copilot.
command_cache = TTLCache(maxsize=10, ttl=1)


class CodegateCommand(ABC):
@abstractmethod
async def run(self, args: List[str]) -> str:
Expand All @@ -31,10 +37,46 @@ def command_name(self) -> str:
def help(self) -> str:
pass

async def _get_full_command(self, args: List[str]) -> str:
"""
Get the full command string with the command name and args.
"""
joined_args = " ".join(args)
return f"{self.command_name} {joined_args}"

async def _record_in_cache(self, args: List[str], cmd_out: str) -> None:
"""
Record the command in the cache.
"""
full_command = await self._get_full_command(args)
command_cache[full_command] = cmd_out

async def _cache_lookup(self, args: List[str]) -> Optional[str]:
"""
Look up the command in the cache. If the command was executed less than 1 second ago,
return the cached output.
"""
full_command = await self._get_full_command(args)
cmd_out = command_cache.get(full_command)
return cmd_out

async def exec(self, args: List[str]) -> str:
"""
Execute the command and cache the output. The cache is invalidated after 1 second.

1. Check if the command is help. If it is, return the help text.
2. Check if the command is in the cache. If it is, return the cached output.
3. Run the command and cache the output.
4. Return the output.
"""
if args and args[0] == "-h":
return self.help
return await self.run(args)
cached_out = await self._cache_lookup(args)
if cached_out:
return cached_out
cmd_out = await self.run(args)
await self._record_in_cache(args, cmd_out)
return cmd_out


class Version(CodegateCommand):
Expand Down
Loading