Skip to content

Commit

Permalink
refactor: move get_workspace_prompt into prompts.py, add TODO comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 6, 2024
1 parent da90e33 commit 639e4cb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
6 changes: 4 additions & 2 deletions gptme/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from pathlib import Path

from .commands import action_descriptions, execute_cmd
from .config import get_workspace_prompt
from .constants import PROMPT_USER
from .init import init
from .interrupt import clear_interruptible, set_interruptible
from .llm import reply
from .logmanager import Log, LogManager, prepare_messages
from .message import Message
from .models import get_model
from .prompts import get_workspace_prompt
from .readline import add_history
from .tools import ToolUse, execute_msg, has_tool
from .tools.base import ConfirmFunc
Expand Down Expand Up @@ -78,7 +78,9 @@ def chat(
console.log(f"Using workspace at {path_with_tilde(workspace)}")
os.chdir(workspace)

workspace_prompt = get_workspace_prompt(str(workspace))
workspace_prompt = get_workspace_prompt(workspace)
# FIXME: this is hacky
# NOTE: needs to run after the workspace is set
# check if message is already in log, such as upon resume
if (
workspace_prompt
Expand Down
26 changes: 5 additions & 21 deletions gptme/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import glob
import logging
import os
from dataclasses import dataclass, field
Expand Down Expand Up @@ -124,12 +123,12 @@ def set_config_value(key: str, value: str) -> None: # pragma: no cover
_config = load_config()


def get_workspace_prompt(workspace: str) -> str:
def get_project_config(workspace: Path) -> ProjectConfig | None:
project_config_paths = [
p
for p in (
Path(workspace) / "gptme.toml",
Path(workspace) / ".github" / "gptme.toml",
workspace / "gptme.toml",
workspace / ".github" / "gptme.toml",
)
if p.exists()
]
Expand All @@ -141,23 +140,8 @@ def get_workspace_prompt(workspace: str) -> str:
# load project config
with open(project_config_path) as f:
project_config = tomlkit.load(f)
project = ProjectConfig(**project_config) # type: ignore
files = []
for file in project.files:
# expand user
file = str(Path(file).expanduser())
# expand with glob
if new_files := glob.glob(file):
files.extend(new_files)
else:
logger.error(
f"File {file} specified in project config does not exist"
)
exit(1)
return "\n\nSelected project files, read more with cat:\n" + "\n\n".join(
[f"```{Path(file).name}\n{Path(file).read_text()}\n```" for file in files]
)
return ""
return ProjectConfig(**project_config) # type: ignore
return None


if __name__ == "__main__":
Expand Down
25 changes: 24 additions & 1 deletion gptme/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
When prompting, it is important to provide clear instructions and avoid any ambiguity.
"""

import glob
import logging
import os
import platform
import subprocess
from collections.abc import Generator, Iterable
from pathlib import Path
from typing import Literal

from .__version__ import __version__
from .config import get_config
from .config import get_config, get_project_config
from .message import Message
from .tools import loaded_tools
from .util import document_prompt_function
Expand Down Expand Up @@ -235,6 +237,27 @@ def prompt_systeminfo() -> Generator[Message, None, None]:
)


def get_workspace_prompt(workspace: Path) -> str:
# NOTE: needs to run after the workspace is initialized (i.e. initial prompt is constructed)
# TODO: update this prompt if the files change
# TODO: include `git status/diff/log` summary, and keep it up-to-date
if project := get_project_config(workspace):
files = []
for file in project.files:
# expand user
file = str(Path(file).expanduser())
# expand with glob
if new_files := glob.glob(file):
files.extend(new_files)
else:
logger.error(f"File {file} specified in project config does not exist")
exit(1)
return "\n\nSelected project files, read more with cat:\n" + "\n\n".join(
[f"```{Path(file).name}\n{Path(file).read_text()}\n```" for file in files]
)
return ""


document_prompt_function(interactive=True)(prompt_gptme)
document_prompt_function()(prompt_user)
document_prompt_function()(prompt_project)
Expand Down

0 comments on commit 639e4cb

Please sign in to comment.