Skip to content

Commit f54feb3

Browse files
committed
feat: add bot option flag
1 parent 86e9712 commit f54feb3

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

src/git_draft/__main__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ def callback(
110110
dest="bot",
111111
help="AI bot name",
112112
)
113+
parser.add_option(
114+
"-o",
115+
"--bot-option",
116+
action="append",
117+
dest="bot_options",
118+
help="AI bot options",
119+
)
113120
parser.add_option(
114121
"-e",
115122
"--edit",
@@ -193,7 +200,7 @@ async def run() -> None: # noqa: PLR0912 PLR0915
193200
bot_config = bot_configs[0]
194201
elif config.bots:
195202
bot_config = config.bots[0]
196-
bot = load_bot(bot_config)
203+
bot = load_bot(bot_config, overrides=opts.bot_options)
197204

198205
prompt: str | TemplatedPrompt
199206
if args:

src/git_draft/bots/__init__.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""Bot interfaces and built-in implementations"""
22

3+
from collections.abc import Sequence
34
import importlib
45
import os
56
import sys
67

7-
from ..common import BotConfig, reindent
8+
from ..common import BotConfig, JSONObject, UnreachableError, reindent
89
from .common import ActionSummary, Bot, Goal, UserFeedback, Worktree
910

1011

@@ -17,10 +18,13 @@
1718
]
1819

1920

20-
def load_bot(config: BotConfig | None) -> Bot:
21+
def load_bot(config: BotConfig | None, *, overrides: Sequence[str]=()) -> Bot:
2122
"""Load and return a Bot instance using the provided configuration"""
23+
options = {**config.options} if config and config.options else {}
24+
options.update(_parse_overrides(overrides))
25+
2226
if not config:
23-
return _default_bot()
27+
return _default_bot(options)
2428

2529
if config.pythonpath and config.pythonpath not in sys.path:
2630
sys.path.insert(0, config.pythonpath)
@@ -35,11 +39,23 @@ def load_bot(config: BotConfig | None) -> Bot:
3539
if not factory:
3640
raise NotImplementedError(f"Unknown bot factory: {config.factory}")
3741

38-
kwargs = config.kwargs or {}
39-
return factory(**kwargs)
42+
return factory(**options)
43+
44+
45+
def _parse_overrides(overrides: str) -> JSONObject:
46+
options = {}
47+
for override in overrides:
48+
match override.split("=", 1):
49+
case [switch]:
50+
options[switch] = True
51+
case [flag, value]:
52+
options[flag] = value
53+
case _:
54+
raise UnreachableError()
55+
return options
4056

4157

42-
def _default_bot() -> Bot:
58+
def _default_bot(options: JSONObject) -> Bot:
4359
if not os.environ.get("OPENAI_API_KEY"):
4460
raise RuntimeError(
4561
reindent(
@@ -52,7 +68,7 @@ def _default_bot() -> Bot:
5268
)
5369

5470
try:
55-
from .openai_api import new_threads_bot
71+
from .openai_api import new_completions_bot
5672

5773
except ImportError:
5874
raise RuntimeError(
@@ -65,4 +81,4 @@ def _default_bot() -> Bot:
6581
)
6682
)
6783
else:
68-
return new_threads_bot()
84+
return new_completions_bot(**options)

src/git_draft/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class BotConfig:
6868

6969
factory: str
7070
name: str | None = None
71-
kwargs: JSONObject | None = None
71+
options: JSONObject | None = None
7272
pythonpath: str | None = None
7373

7474

tests/git_draft/common_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_load_ok(self) -> None:
3030
[[bots]]
3131
name = "bar"
3232
factory = "bar"
33-
kwargs = {one=1}
33+
options = {one=1}
3434
"""
3535
path = sut.Config.folder_path()
3636
path.mkdir(parents=True, exist_ok=True)
@@ -42,7 +42,7 @@ def test_load_ok(self) -> None:
4242
log_level=logging.DEBUG,
4343
bots=[
4444
sut.BotConfig(factory="foo:load", pythonpath="./abc"),
45-
sut.BotConfig(factory="bar", name="bar", kwargs={"one": 1}),
45+
sut.BotConfig(factory="bar", name="bar", options={"one": 1}),
4646
],
4747
)
4848

0 commit comments

Comments
 (0)