Skip to content

Commit 1b97279

Browse files
authored
feat: add basic logging (#14)
1 parent 2d847ec commit 1b97279

File tree

5 files changed

+43
-18
lines changed

5 files changed

+43
-18
lines changed

src/git_draft/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import logging
2+
13
from .assistants import Assistant, Session, Toolbox
24

35
__all__ = [
46
"Assistant",
57
"Session",
68
"Toolbox",
79
]
10+
11+
logging.getLogger(__name__).addHandler(logging.NullHandler())

src/git_draft/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import importlib.metadata
4+
import logging
45
import optparse
56
import sys
67
import textwrap
@@ -10,6 +11,9 @@
1011
from .manager import Manager, enclosing_repo
1112

1213

14+
logging.basicConfig(level=logging.INFO)
15+
16+
1317
EPILOG = """\
1418
More information via `man git-draft` and https://mtth.github.io/git-draft.
1519
"""

src/git_draft/assistants/openai.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,38 @@
1414
{
1515
"type": "function",
1616
"function": {
17-
"name": "get_current_temperature",
18-
"description": "Get the current temperature for a specific location",
17+
"name": "read_file",
18+
"description": "Get a file's contents",
1919
"parameters": {
2020
"type": "object",
2121
"properties": {
22-
"location": {
22+
"path": {
2323
"type": "string",
24-
"description": "The city and state, e.g., San Francisco, CA",
25-
},
26-
"unit": {
27-
"type": "string",
28-
"enum": ["Celsius", "Fahrenheit"],
29-
"description": "The temperature unit to use. Infer this from the user's location.",
24+
"description": "Path of the file to be read",
3025
},
3126
},
32-
"required": ["location", "unit"],
27+
"required": ["path"],
3328
},
3429
},
3530
},
3631
{
3732
"type": "function",
3833
"function": {
39-
"name": "get_rain_probability",
40-
"description": "Get the probability of rain for a specific location",
34+
"name": "write_file",
35+
"description": "Update a file's contents",
4136
"parameters": {
4237
"type": "object",
4338
"properties": {
44-
"location": {
39+
"path": {
4540
"type": "string",
46-
"description": "The city and state, e.g., San Francisco, CA",
47-
}
41+
"description": "Path of the file to be updated",
42+
},
43+
"contents": {
44+
"type": "string",
45+
"description": "New contents of the file",
46+
},
4847
},
49-
"required": ["location"],
48+
"required": ["path", "contents"],
5049
},
5150
},
5251
},

src/git_draft/manager.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
import dataclasses
44
import git
55
import json
6+
import logging
67
from pathlib import PurePosixPath
78
import re
89
import tempfile
10+
import time
911
from typing import Callable, ClassVar, Match, Self, Sequence
1012

1113
from .assistants import Assistant, Toolbox
1214

1315

16+
_logger = logging.getLogger(__name__)
17+
18+
1419
def enclosing_repo(path: str | None = None) -> git.Repo:
1520
"""Returns the repository to which the given path belongs"""
1621
return git.Repo(path, search_parent_directories=True)
@@ -31,6 +36,7 @@ def read(cls, repo: git.Repo, ref: str) -> Self | None:
3136
for line in repo.git.notes("show", ref).splitlines():
3237
if line.startswith(cls.__prefix):
3338
data = json.loads(line[len(cls.__prefix) :])
39+
_logger.debug("Read %r note. [ref=%s]", cls.__prefix, ref)
3440
return cls(**data)
3541
return None
3642

@@ -41,6 +47,7 @@ def write(self, repo: git.Repo, ref: str) -> None:
4147
repo.git.notes(
4248
"append", "--no-separator", "-m", f"{self.__prefix}{value}", ref
4349
)
50+
_logger.debug("Write %r note. [ref=%s]", self.__prefix, ref)
4451

4552

4653
@dataclasses.dataclass(frozen=True)
@@ -80,6 +87,9 @@ def needs_rebase(self, repo: git.Repo) -> bool:
8087
head_commit = repo.commit(self.init_note.origin_branch)
8188
return origin_commit != head_commit
8289

90+
def __str__(self) -> str:
91+
return self.name
92+
8393
@classmethod
8494
def create(cls, repo: git.Repo, sync: Callable[[], str | None]) -> _Branch:
8595
if not repo.active_branch:
@@ -159,12 +169,20 @@ def generate_draft(
159169

160170
branch = _Branch.active(self._repo)
161171
if branch:
172+
_logger.debug("Reusing active branch %s.", branch)
162173
self._sync()
163174
else:
164175
branch = _Branch.create(self._repo, self._sync)
176+
_logger.debug("Created branch %s.", branch)
177+
178+
start_time = time.perf_counter()
179+
session = assistant.run(prompt, _Toolbox(self._repo))
180+
end_time = time.perf_counter()
181+
commit = self._repo.index.commit(f"draft! prompt\n\n{prompt}")
182+
note = _SessionNote(session.token_count, end_time - start_time)
183+
note.write(self._repo, commit.hexsha)
184+
_logger.info("Generated draft. [token_count=%s]", session.token_count)
165185

166-
assistant.run(prompt, _Toolbox(self._repo))
167-
self._repo.index.commit(f"draft! prompt\n\n{prompt}")
168186
if checkout:
169187
self._repo.git.checkout("--", ".")
170188

src/git_draft/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)