11"""Bot interfaces and built-in implementations"""
22
3+ from collections .abc import Sequence
34import importlib
4- import os
55import sys
66
7- from ..common import BotConfig , reindent
7+ from ..common import (
8+ BotConfig ,
9+ JSONObject ,
10+ JSONValue ,
11+ UnreachableError ,
12+ reindent ,
13+ )
814from .common import ActionSummary , Bot , Goal , UserFeedback , Worktree
915
1016
1723]
1824
1925
20- def load_bot (config : BotConfig | None ) -> Bot :
26+ def load_bot (
27+ config : BotConfig | None , * , overrides : Sequence [str ] = ()
28+ ) -> Bot :
2129 """Load and return a Bot instance using the provided configuration"""
30+ options = {** config .options } if config and config .options else {}
31+ options .update (_parse_overrides (overrides ))
32+
2233 if not config :
23- return _default_bot ()
34+ return _default_bot (options )
2435
2536 if config .pythonpath and config .pythonpath not in sys .path :
2637 sys .path .insert (0 , config .pythonpath )
@@ -35,34 +46,33 @@ def load_bot(config: BotConfig | None) -> Bot:
3546 if not factory :
3647 raise NotImplementedError (f"Unknown bot factory: { config .factory } " )
3748
38- kwargs = config .kwargs or {}
39- return factory (** kwargs )
49+ return factory (** options )
4050
4151
42- def _default_bot () -> Bot :
43- if not os .environ .get ("OPENAI_API_KEY" ):
44- raise RuntimeError (
45- reindent (
46- """
47- The default bot implementation requires an OpenAI API key.
48- Please specify one via the `$OPENAI_API_KEY` environment
49- variable or enable a different bot in your configuration.
50- """
51- )
52- )
52+ def _parse_overrides (overrides : Sequence [str ]) -> JSONObject :
53+ options = dict [str , JSONValue ]()
54+ for override in overrides :
55+ match override .split ("=" , 1 ):
56+ case [switch ]:
57+ options [switch ] = True
58+ case [flag , value ]:
59+ options [flag ] = value
60+ case _:
61+ raise UnreachableError ()
62+ return options
63+
5364
65+ def _default_bot (options : JSONObject ) -> Bot :
5466 try :
55- from .openai_api import new_threads_bot
67+ from .openai_api import new_completions_bot
5668
5769 except ImportError :
5870 raise RuntimeError (
59- reindent (
60- """
61- The default bot implementation requires the `openai` Python
62- package. Please install it or specify a different bot in
63- your configuration.
64- """
65- )
71+ reindent ("""
72+ The default bot implementation requires the `openai` Python
73+ package. Please install it or specify a different bot in
74+ your configuration.
75+ """ )
6676 )
6777 else :
68- return new_threads_bot ( )
78+ return new_completions_bot ( ** options )
0 commit comments