-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (82 loc) · 2.78 KB
/
main.py
File metadata and controls
101 lines (82 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# /// script
# dependencies = [
# "requests",
# ]
# requires-python = ">=3.8"
# ///
from pathlib import Path
from commands.dispatcher import CommandDispatcher
from commands.commands import (
CommitCommand,
ExitCommand,
HelpCommand,
SaveCommand,
ShowDiffCommand,
UndoCommand,
HistoryCommand,
)
from git_provider import ShellGitProvider
from history import History, HistoryMessage, HistoryMessageRole
from llm_providers.ollama import OllamaLlmProvider
from pager_provider import LessPagerProvider
from view import View
def _load_system_prompt() -> str:
prompt_path = Path(__file__).parent / "system_prompt.md"
return prompt_path.read_text()
SYSTEM_PROMPT = _load_system_prompt()
view = View()
llm_provider = OllamaLlmProvider("qwen2.5-coder:14b", "http://localhost:11434", view)
history = History(llm_provider)
git_provider = ShellGitProvider()
pager = LessPagerProvider()
help_command = HelpCommand(view)
commands = (
UndoCommand(history, view),
ExitCommand(),
SaveCommand(history, git_provider, view),
CommitCommand(history, git_provider, view),
ShowDiffCommand(git_provider, pager),
HistoryCommand(history, pager),
help_command,
)
command_dispatcher = CommandDispatcher(commands)
help_command.set_command_dispatcher(command_dispatcher)
def assistant_think(user_input: str | None):
view.show_thinking()
reply = history.assistant_think(user_input)
view.show_reply(reply)
view.show_current_commit_message(history.current_message)
def loop():
user_input = None
skip_think = False
while True:
if not skip_think:
assistant_think(user_input)
view.show_user_input_prefix()
user_input = view.wait_user_input()
skip_think = False
if command_dispatcher.is_command(user_input):
command_dispatcher(user_input)
skip_think = True
if command_dispatcher.is_terminator(user_input):
break
if __name__ == "__main__":
sys_msg = HistoryMessage(role=HistoryMessageRole.system, content=SYSTEM_PROMPT)
history.talk_history.append(sys_msg)
samples = git_provider.get_commit_messages_samples()
if samples:
samples_msg = HistoryMessage(
role=HistoryMessageRole.user,
content=f"Вот примеры сообщений коммитов из вашего репозитория:\n{samples}",
)
history.talk_history.append(samples_msg)
diff = git_provider.get_last_diff()
diff_msg = HistoryMessage(
role=HistoryMessageRole.user, content=f"Вот diff изменений:\n{diff}"
)
history.talk_history.append(diff_msg)
try:
help_command()
loop()
except (KeyboardInterrupt, EOFError):
view.show_error("Прерываю выполнение.")