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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
else:
|
65 |
-
|
|
|
1 |
+
async def respond(
|
2 |
+
command: str,
|
3 |
+
history: List[Tuple[str, str]],
|
4 |
+
system_message: str,
|
5 |
+
max_tokens: int,
|
6 |
+
temperature: float,
|
7 |
+
top_p: float,
|
8 |
+
github_api_token: str,
|
9 |
+
github_username: str,
|
10 |
+
github_repository: str,
|
11 |
+
selected_model: str,
|
12 |
+
severity: str,
|
13 |
+
programming_language: str,
|
14 |
+
*args
|
15 |
+
) -> str:
|
16 |
+
global GITHUB_API_TOKEN
|
17 |
+
GITHUB_API_TOKEN = github_api_token
|
18 |
+
global issues
|
19 |
+
issues = []
|
20 |
+
global github_client
|
21 |
+
github_client = None
|
22 |
+
|
23 |
+
messages = [{"role": "system", "content": system_message}]
|
24 |
+
logging.info("System message: {}".format(system_message))
|
25 |
+
|
26 |
+
for user_msg, assistant_msg in history:
|
27 |
+
if user_msg:
|
28 |
+
messages.append({"role": "user", "content": user_msg})
|
29 |
+
logging.info("User message: {}".format(user_msg))
|
30 |
+
if assistant_msg:
|
31 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
32 |
+
logging.info("Assistant message: {}".format(assistant_msg))
|
33 |
+
|
34 |
+
logging.info("Command received: {}".format(command))
|
35 |
+
|
36 |
+
try:
|
37 |
+
command, *args = command.split(' ', 1)
|
38 |
+
args = args[0] if args else ''
|
39 |
+
except ValueError:
|
40 |
+
yield "❌ Invalid command format. Use /help for instructions"
|
41 |
+
|
42 |
+
if command == "/github":
|
43 |
+
try:
|
44 |
+
if not args:
|
45 |
+
if github_client:
|
46 |
+
yield f"ℹ️ Current GitHub connection: {github_client.config.username}/{github_client.config.repository}"
|
47 |
+
else:
|
48 |
+
yield "ℹ️ Not connected to GitHub"
|
49 |
+
|
50 |
+
parts = args.split(maxsplit=2) # Allow spaces in token
|
51 |
+
if len(parts) < 3:
|
52 |
+
raise ValueError("Format: /github <username> <repo> <token>")
|
53 |
+
|
54 |
+
github_client = GitHubIntegration(GitHubConfig(
|
55 |
+
username=parts[0],
|
56 |
+
repository=parts[1],
|
57 |
+
api_token=SecretStr(parts[2])
|
58 |
+
))
|
59 |
+
issues = await github_client.fetch_issues() # Fetch issues after successful connection
|
60 |
+
yield "✅ GitHub configured successfully"
|
61 |
+
except Exception as e:
|
62 |
+
github_client = None
|
63 |
+
yield f"❌ Error: {str(e)}"
|
64 |
+
|
65 |
+
elif command == "/help":
|
66 |
+
help_message = """Available commands:
|
67 |
+
- `/github <username> <repo> <token>`: Connect to a GitHub repository.
|
68 |
+
- `/help`: Show this help message.
|
69 |
+
- `/generate_code [code description]`: Generate code based on the description.
|
70 |
+
- `/explain_concept [concept]`: Explain a concept.
|
71 |
+
- `/write_documentation [topic]`: Write documentation for a given topic.
|
72 |
+
- `/translate_code [code] to [target language]`: Translate code to another language.
|
73 |
+
- `/analyze [issue number]`: Analyze a GitHub issue.
|
74 |
+
- `/list_issues`: List all issues in the connected repository.
|
75 |
+
"""
|
76 |
+
yield help_message
|
77 |
+
|
78 |
+
elif command.isdigit() and issues:
|
79 |
+
try:
|
80 |
+
issue_number = int(command) - 1
|
81 |
+
issue = issues[issue_number]
|
82 |
+
issue_text = issue['title'] + "\n\n" + issue['body']
|
83 |
+
resolution = analyze_issues(issue_text, selected_model, severity, programming_language)
|
84 |
+
|
85 |
+
related_issues = find_related_issues(issue_text, issues)
|
86 |
+
related_issue_text = "\n".join(
|
87 |
+
["- {} (Similarity: {:.2f})".format(issue['title'], similarity) for issue, similarity in related_issues]
|
88 |
+
)
|
89 |
+
|
90 |
+
yield "Resolution for Issue '{}':\n{}\n\nRelated Issues:\n{}".format(issue['title'], resolution, related_issue_text)
|
91 |
+
except Exception as e:
|
92 |
+
logging.error("Error analyzing issue: {}".format(e))
|
93 |
+
yield "Error analyzing issue: {}".format(e)
|
94 |
+
|
95 |
+
elif command.startswith("/generate_code"):
|
96 |
+
code_description = command.replace("/generate_code", "").strip()
|
97 |
+
if not code_description:
|
98 |
+
yield "Please provide a description of the code you want to generate."
|
99 |
+
else:
|
100 |
+
prompt = "Generate code for the following: {}\nProgramming Language: {}".format(code_description, programming_language)
|
101 |
+
try:
|
102 |
+
generated_code = analyze_issues(prompt, selected_model)
|
103 |
+
code_output = "<pre>{}</pre>".format(generated_code)
|
104 |
+
yield code_output
|
105 |
+
except Exception as e:
|
106 |
+
logging.error("Error generating code: {}".format(e))
|
107 |
+
yield "Error generating code: {}".format(e)
|
108 |
+
|
109 |
+
elif command.startswith("/explain_concept"):
|
110 |
+
concept = command.replace("/explain_concept", "").strip()
|
111 |
+
if not concept:
|
112 |
+
yield "Please provide a concept to explain."
|
113 |
+
else:
|
114 |
+
prompt = "Explain the concept of {} in detail.".format(concept)
|
115 |
+
try:
|
116 |
+
explanation = analyze_issues(prompt, selected_model)
|
117 |
+
yield "<pre>{}</pre>".format(explanation)
|
118 |
+
except Exception as e:
|
119 |
+
logging.error("Error explaining concept: {}".format(e))
|
120 |
+
yield "Error explaining concept: {}".format(e)
|
121 |
+
|
122 |
+
elif command.startswith("/write_documentation"):
|
123 |
+
topic = command.replace("/write_documentation", "").strip()
|
124 |
+
if not topic:
|
125 |
+
yield "Please provide a topic for documentation."
|
126 |
+
else:
|
127 |
+
prompt = "Write documentation for the topic: {}".format(topic)
|
128 |
+
try:
|
129 |
+
documentation = analyze_issues(prompt, selected_model)
|
130 |
+
yield "<pre>{}</pre>".format(documentation)
|
131 |
+
except Exception as e:
|
132 |
+
logging.error("Error writing documentation: {}".format(e))
|
133 |
+
yield "Error writing documentation: {}".format(e)
|
134 |
+
|
135 |
+
elif command.startswith("/translate_code"):
|
136 |
+
try:
|
137 |
+
code, _, target_language = command.replace("/translate_code", "").strip().partition(" to ")
|
138 |
+
if not code or not target_language:
|
139 |
+
yield "Please provide code and target language in the format: `/translate_code [code] to [target language]`"
|
140 |
+
else:
|
141 |
+
prompt = f"Translate the following code to {target_language}:\n```\n{code}\n```"
|
142 |
+
try:
|
143 |
+
translated_code = analyze_issues(prompt, selected_model)
|
144 |
+
yield "<pre>{}</pre>".format(translated_code)
|
145 |
+
except Exception as e:
|
146 |
+
logging.error("Error translating code: {}".format(e))
|
147 |
+
yield "Error translating code: {}".format(e)
|
148 |
+
except Exception as e:
|
149 |
+
logging.error("Error parsing translate_code command: {}".format(e))
|
150 |
+
yield "Error parsing translate_code command: {}".format(e)
|
151 |
+
|
152 |
+
elif command.startswith("/analyze"):
|
153 |
+
try:
|
154 |
+
if not github_client:
|
155 |
+
yield "❌ You need to connect to a GitHub repository first using `/github <username> <repo> <token>`."
|
156 |
+
issue_number = int(command.replace("/analyze", "").strip()) - 1
|
157 |
+
if 0 <= issue_number < len(issues):
|
158 |
+
issue = issues[issue_number]
|
159 |
+
issue_text = issue['title'] + "\n\n" + issue['body']
|
160 |
+
resolution = analyze_issues(issue_text, selected_model, severity, programming_language)
|
161 |
+
|
162 |
+
related_issues = find_related_issues(issue_text, issues)
|
163 |
+
related_issue_text = "\n".join(
|
164 |
+
["- {} (Similarity: {:.2f})".format(issue['title'], similarity) for issue, similarity in related_issues]
|
165 |
+
)
|
166 |
+
|
167 |
+
yield "Resolution for Issue '{}':\n{}\n\nRelated Issues:\n{}".format(issue['title'], resolution, related_issue_text)
|
168 |
+
else:
|
169 |
+
yield "❌ Invalid issue number. Please enter a valid issue number from the list."
|
170 |
+
except Exception as e:
|
171 |
+
logging.error("Error analyzing issue: {}".format(e))
|
172 |
+
yield "Error analyzing issue: {}".format(e)
|
173 |
+
|
174 |
+
elif command == "/list_issues":
|
175 |
+
try:
|
176 |
+
if not github_client:
|
177 |
+
yield "❌ You need to connect to a GitHub repository first using `/github <username> <repo> <token>`."
|
178 |
+
if issues:
|
179 |
+
issue_list = "\n".join(
|
180 |
+
[f"- {issue['title']} (Issue #{issue['number']})" for issue in issues]
|
181 |
+
)
|
182 |
+
yield f"Issues in {github_client.config.username}/{github_client.config.repository}:\n{issue_list}"
|
183 |
+
else:
|
184 |
+
yield "❌ No issues found in the connected repository."
|
185 |
+
except Exception as e:
|
186 |
+
logging.error("Error listing issues: {}".format(e))
|
187 |
+
yield "Error listing issues: {}".format(e)
|
188 |
+
|
189 |
else:
|
190 |
+
yield "I'm not sure what you mean. Try using `/help` for a list of available commands."
|