Skip to content

Commit

Permalink
feat: added --workspace option, added project-level gptme.toml config…
Browse files Browse the repository at this point in the history
… file support (incl files to include in context by default)
  • Loading branch information
ErikBjare committed Aug 26, 2024
1 parent aacfb93 commit d914810
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions gptme.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
files = ["README.md", "gptme/cli.py", "docs/*.rst", "docs/*.md"]
12 changes: 12 additions & 0 deletions gptme/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rich.console import Console

from .commands import CMDFIX, action_descriptions, execute_cmd
from .config import get_workspace_prompt
from .constants import MULTIPROMPT_SEPARATOR, PROMPT_USER
from .dirs import get_logs_dir
from .init import init, init_logging
Expand Down Expand Up @@ -111,6 +112,11 @@
is_flag=True,
help="Show version and configuration information",
)
@click.option(
"--workspace",
help="Path to workspace directory.",
default=".",
)
def main(
prompts: list[str],
prompt_system: str,
Expand All @@ -123,6 +129,7 @@ def main(
show_hidden: bool,
version: bool,
resume: bool,
workspace: str,
):
"""Main entrypoint for the CLI."""
if version:
Expand All @@ -146,8 +153,13 @@ def main(
if no_confirm:
logger.warning("Skipping all confirmation prompts.")

workspace_prompt = get_workspace_prompt(workspace)

# get initial system prompt
initial_msgs = [get_prompt(prompt_system)]
initial_msgs[
0
].content += f"\n\nSelected project files, read more with cat: {workspace_prompt}"

# if stdin is not a tty, we're getting piped input, which we should include in the prompt
if not sys.stdin.isatty():
Expand Down
48 changes: 47 additions & 1 deletion gptme/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import glob
import logging
import os
from dataclasses import dataclass
from dataclasses import dataclass, field
from pathlib import Path

import tomlkit
from tomlkit import TOMLDocument
from tomlkit.container import Container

logger = logging.getLogger(__name__)


@dataclass
class Config:
Expand Down Expand Up @@ -35,6 +40,13 @@ def dict(self) -> dict:
}


@dataclass
class ProjectConfig:
"""Project-level configuration, such as which files to include in the context by default."""

files: list[str] = field(default_factory=list)


ABOUT_ACTIVITYWATCH = """ActivityWatch is a free and open-source automated time-tracker that helps you track how you spend your time on your devices."""
ABOUT_GPTME = "gptme is a CLI to interact with large language models in a Chat-style interface, enabling the assistant to execute commands and code on the local machine, letting them assist in all kinds of development and terminal-based work."

Expand Down Expand Up @@ -108,6 +120,40 @@ def set_config_value(key: str, value: str) -> None:
_config = load_config()


def get_workspace_prompt(workspace: str) -> str:
if not os.path.exists(workspace):
logger.error(f"Workspace directory {workspace} does not exist")
exit(1)
os.chdir(workspace)
project_config_paths = [
p
for p in (
Path(workspace) / "gptme.toml",
Path(workspace) / ".github" / "gptme.toml",
)
if p.exists()
]
if project_config_paths:
project_config_path = project_config_paths[0]
logger.info(f"Using project configuration at {project_config_path}")
# load project config
with open(project_config_path) as f:
project_config = tomlkit.load(f)
project = ProjectConfig(**project_config) # type: ignore
# expand with glob
files = [p for file in project.files for p in glob.glob(file)]
for file in files:
if not Path(file).exists():
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".join(
[f"```{Path(file).name}\n{Path(file).read_text()}\n```" for file in files]
)
return ""


if __name__ == "__main__":
config = get_config()
print(config)

0 comments on commit d914810

Please sign in to comment.