Skip to content

Commit 55c5f91

Browse files
authored
feat: tweak prompt CLI args (#37)
1 parent 296ad81 commit 55c5f91

File tree

8 files changed

+40
-38
lines changed

8 files changed

+40
-38
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,5 @@
1111

1212
## Ideas
1313

14-
* Change prompt CLI inputs to `[PROMPT] [--] [ARGS]`. If `PROMPT` does not
15-
contain any spaces or `ARGS` (or `--`) is present, it will be interpreted as a
16-
template name. Otherwise an inline prompt.
17-
* Add `--generate` timeout option.
1814
* Add a compatibility OpenAI bot version which does not use threads, so that it
1915
can be used with tools only. Gemini only supports the latter.

src/git_draft/__main__.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ def callback(_option, _opt, _value, parser) -> None:
6868
help="delete draft after finalizing or discarding",
6969
action="store_true",
7070
)
71-
parser.add_option(
72-
"-p",
73-
"--prompt",
74-
dest="prompt",
75-
help="inline prompt",
76-
)
7771
parser.add_option(
7872
"-r",
7973
"--reset",
@@ -88,9 +82,9 @@ def callback(_option, _opt, _value, parser) -> None:
8882
)
8983
parser.add_option(
9084
"-t",
91-
"--template",
92-
dest="template",
93-
help="prompt template",
85+
"--timeout",
86+
dest="timeout",
87+
help="bot generation timeout",
9488
)
9589

9690
return parser
@@ -119,6 +113,7 @@ def main() -> None:
119113
if command == "generate":
120114
if not config.bots:
121115
raise ValueError("No bots configured")
116+
122117
if opts.bot:
123118
bot_configs = [c for c in config.bots if c.name == opts.bot]
124119
if len(bot_configs) != 1:
@@ -128,11 +123,11 @@ def main() -> None:
128123
bot_config = config.bots[0]
129124
bot = load_bot(bot_config)
130125

131-
prompt = opts.prompt
132-
if not prompt:
133-
if opts.template:
134-
prompt = TemplatedPrompt.parse(opts.template, *args)
135-
elif sys.stdin.isatty():
126+
prompt: str | TemplatedPrompt
127+
if args:
128+
prompt = TemplatedPrompt.parse(args[0], *args[1:])
129+
else:
130+
if sys.stdin.isatty():
136131
prompt = open_editor("Enter your prompt here...")
137132
else:
138133
prompt = sys.stdin.read()

src/git_draft/bots/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import sys
88

99
from ..common import BotConfig
10-
from .common import Action, Bot, Operation, OperationHook, Toolbox
10+
from .common import Action, Bot, Goal, Operation, OperationHook, Toolbox
1111

1212
__all__ = [
1313
"Action",
1414
"Bot",
15+
"Goal",
1516
"Operation",
1617
"OperationHook",
1718
"Toolbox",

src/git_draft/bots/common.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ class Operation:
9191
type OperationHook = Callable[[Operation], None]
9292

9393

94+
@dataclasses.dataclass(frozen=True)
95+
class Goal:
96+
prompt: str
97+
timeout: float | None
98+
99+
94100
@dataclasses.dataclass(frozen=True)
95101
class Action:
96102
title: str | None = None
@@ -107,5 +113,5 @@ def state_folder_path(cls, ensure_exists=False) -> Path:
107113
path.mkdir(parents=True, exist_ok=True)
108114
return path
109115

110-
def act(self, prompt: str, toolbox: Toolbox) -> Action:
116+
def act(self, goal: Goal, toolbox: Toolbox) -> Action:
111117
raise NotImplementedError()

src/git_draft/bots/openai.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import textwrap
77
from typing import Any, Mapping, Self, Sequence, override
88

9-
from .common import Action, Bot, Toolbox
9+
from .common import Action, Bot, Goal, Toolbox
1010

1111

1212
_logger = logging.getLogger(__name__)
@@ -130,22 +130,22 @@ def create(cls, client: openai.OpenAI) -> Self:
130130
try:
131131
with open(path) as f:
132132
assistant_id = f.read()
133-
except FileNotFoundError:
133+
client.beta.assistants.update(assistant_id, **config)
134+
except (FileNotFoundError, openai.NotFoundError):
134135
assistant = client.beta.assistants.create(**config)
135136
assistant_id = assistant.id
136137
with open(path, "w") as f:
137138
f.write(assistant_id)
138-
else:
139-
client.beta.assistants.update(assistant_id, **config)
140139
return cls(client, assistant_id)
141140

142-
def act(self, prompt: str, toolbox: Toolbox) -> Action:
141+
def act(self, goal: Goal, toolbox: Toolbox) -> Action:
142+
# TODO: Use timeout.
143143
thread = self._client.beta.threads.create()
144144

145145
self._client.beta.threads.messages.create(
146146
thread_id=thread.id,
147147
role="user",
148-
content=prompt,
148+
content=goal.prompt,
149149
)
150150

151151
with self._client.beta.threads.runs.stream(

src/git_draft/drafter.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import time
1212
from typing import Match, Sequence, override
1313

14-
from .bots import Bot, OperationHook, Toolbox
14+
from .bots import Bot, Goal, OperationHook, Toolbox
1515
from .common import random_id
1616
from .prompt import PromptRenderer, TemplatedPrompt
1717
from .store import Store, sql
@@ -85,7 +85,7 @@ def _write(self, path: PurePosixPath, contents: str) -> None:
8585
f"{mode},{sha},{path}", add=True, cacheinfo=True
8686
)
8787

88-
def update_index(self) -> None:
88+
def trim_index(self) -> None:
8989
diff = self._repo.git.diff(name_only=True, cached=True)
9090
untouched = [
9191
path
@@ -125,9 +125,10 @@ def generate_draft(
125125
self,
126126
prompt: str | TemplatedPrompt,
127127
bot: Bot,
128-
checkout=False,
129-
reset=False,
130-
sync=False,
128+
checkout: bool = False,
129+
reset: bool = False,
130+
sync: bool = False,
131+
timeout: float | None = None,
131132
) -> None:
132133
if isinstance(prompt, str) and not prompt.strip():
133134
raise ValueError("Empty prompt")
@@ -159,11 +160,12 @@ def generate_draft(
159160
)
160161

161162
start_time = time.perf_counter()
163+
goal = Goal(prompt_contents, timeout)
162164
toolbox = _Toolbox(self._repo, self._operation_hook)
163-
action = bot.act(prompt_contents, toolbox)
165+
action = bot.act(goal, toolbox)
164166
end_time = time.perf_counter()
165167

166-
toolbox.update_index()
168+
toolbox.trim_index()
167169
title = action.title
168170
if not title:
169171
title = _default_title(prompt_contents)

src/git_draft/prompts/add-docstrings.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{% if path is defined %}
1+
{% if symbol is defined and path is defined %}
2+
Add a docstring to {{ symbol }} defined in {{ path }}.
3+
{% elif path is defined %}
24
Add docstrings to all public functions and classes in {{ path }}.
35
{% else %}
46
Add docstrings to all public functions and classes in this repository.

tests/git_draft/drafter_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from typing import Sequence
55

6-
from git_draft.bots import Action, Bot, Toolbox
6+
from git_draft.bots import Action, Bot, Goal, Toolbox
77
import git_draft.drafter as sut
88
from git_draft.prompt import TemplatedPrompt
99
from git_draft.store import Store
@@ -46,8 +46,8 @@ def test_write_file(self, repo: git.Repo) -> None:
4646

4747

4848
class FakeBot(Bot):
49-
def act(self, prompt: str, toolbox: Toolbox) -> Action:
50-
toolbox.write_file(PurePosixPath("PROMPT"), prompt)
49+
def act(self, goal: Goal, toolbox: Toolbox) -> Action:
50+
toolbox.write_file(PurePosixPath("PROMPT"), goal.prompt)
5151
return Action()
5252

5353

@@ -86,7 +86,7 @@ def test_generate_stages_then_resets_worktree(self) -> None:
8686
self._write("p2", "b")
8787

8888
class CustomBot(Bot):
89-
def act(self, prompt: str, toolbox: Toolbox) -> Action:
89+
def act(self, _goal: Goal, toolbox: Toolbox) -> Action:
9090
assert toolbox.read_file(PurePosixPath("p1")) == "a"
9191
toolbox.write_file(PurePosixPath("p2"), "B")
9292
toolbox.write_file(PurePosixPath("p3"), "C")

0 commit comments

Comments
 (0)