forked from marc-shade/Ollama-Workbench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
1092 lines (952 loc) · 42.1 KB
/
build.py
File metadata and controls
1092 lines (952 loc) · 42.1 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ast
import json
import os
import re
import shutil
import subprocess
import sys
import tempfile
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Tuple, Union
import ollama
import pytest
from duckduckgo_search import DDGS
from googleapiclient.discovery import build
from ollama import Client
from openai import ChatCompletion, OpenAI
from rich.console import Console
from streamlit import session_state as st_ss
from openai_utils import *
from groq_utils import *
from ollama_utils import *
from external_providers import *
API_KEYS_FILE = "api_keys.json"
SETTINGS_FILE = "build_settings.json"
# Initialize the Rich Console
console = Console()
# Initialize the Ollama client
client = Client(host="http://localhost:11434")
def load_json_file(filepath: str) -> dict:
"""Loads JSON data from a file if it exists."""
if os.path.exists(filepath):
with open(filepath, "r") as f:
return json.load(f)
return {}
def save_json_file(filepath: str, data: dict) -> None:
"""Saves JSON data to a file."""
with open(filepath, "w") as f:
json.dump(data, f, indent=4)
def load_api_keys() -> dict:
"""Loads API keys from the JSON file."""
return load_json_file(API_KEYS_FILE)
def save_api_keys(api_keys: dict) -> None:
"""Saves API keys to the JSON file."""
save_json_file(API_KEYS_FILE, api_keys)
def load_settings() -> dict:
"""Loads settings from the JSON file."""
return load_json_file(SETTINGS_FILE)
def save_settings(settings: dict) -> None:
"""Saves settings to the JSON file."""
save_json_file(SETTINGS_FILE, settings)
def set_openai_api_key(api_key: str) -> None:
"""Sets the OpenAI API key."""
api_keys = load_api_keys()
api_keys['openai_api_key'] = api_key
save_api_keys(api_keys)
console.print("[bold green]OpenAI API key has been set.[/bold green]")
def call_openai_api(
model: str,
messages: List[Dict[str, Any]],
temperature: float = 0.7,
max_tokens: int = 1000,
frequency_penalty: float = 0.0,
presence_penalty: float = 0.0,
stream: bool = False,
openai_api_key: str = None
) -> Any:
"""Wrapper function to call the OpenAI Chat API with a unified interface."""
try:
temperature = float(temperature)
max_tokens = int(max_tokens)
frequency_penalty = float(frequency_penalty)
presence_penalty = float(presence_penalty)
except ValueError as ve:
raise ValueError(f"Invalid value for one of the numerical parameters: {ve}")
if not openai_api_key or not isinstance(openai_api_key, str):
api_keys = load_api_keys()
openai_api_key = api_keys.get('openai_api_key')
if not openai_api_key or not isinstance(openai_api_key, str):
raise ValueError("Invalid or missing OpenAI API key.")
openai.api_key = openai_api_key
try:
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
stream=stream
)
if stream:
return response # Return the stream object
else:
return response.choices[0].message['content'].strip()
except json.JSONDecodeError as json_err:
raise ValueError(f"Failed to parse response as JSON: {json_err}")
except Exception as e:
console.print(f"[bold red]Error calling OpenAI API:[/bold red] {e}")
return "Error occurred while calling OpenAI API"
def is_groq_model(model_name: str) -> bool:
"""Determines if a given model name is a Groq model."""
api_keys = load_api_keys()
available_groq_models = get_available_groq_models(api_keys)
return model_name in available_groq_models
def perform_search(
query: str,
search_method: str,
api_keys: Dict[str, str],
num_results: int = 5
) -> List[Dict[str, str]]:
"""Performs a web search using the specified method."""
if search_method == "duckduckgo":
with DDGS() as ddgs:
results = list(ddgs.text(query, max_results=num_results))
return [{"title": result["title"], "url": result["href"]} for result in results]
elif search_method == "google":
if "google_api_key" not in api_keys or "google_cse_id" not in api_keys:
console.print("[bold red]Error: Google API Key or CSE ID not provided.[/bold red]")
return []
service = build("customsearch", "v1", developerKey=api_keys["google_api_key"])
res = service.cse().list(q=query, cx=api_keys["google_cse_id"], num=num_results).execute()
return [{"title": item["title"], "url": item["link"]} for item in res.get("items", [])]
elif search_method == "serpapi":
if "serpapi_api_key" not in api_keys:
console.print("[bold red]Error: SerpAPI Key not provided.[/bold red]")
return []
try:
params = {
"engine": "google",
"q": query,
"api_key": api_keys["serpapi_api_key"],
"num": num_results,
}
search = GoogleSearch(params)
results = search.get_dict()
organic_results = results.get("organic_results", [])
return [{"title": result["title"], "url": result["link"]} for result in organic_results]
except Exception as e:
console.print(f"Error in SerpApi search: {str(e)}")
return []
else:
console.print(f"[bold red]Unsupported search method: {search_method}[/bold red]")
return []
def create_agent_context(
project_state: dict,
current_task: str
) -> dict:
"""Creates the context for agents based on the project state."""
return {
"project_state": {
"status": project_state["status"],
"current_step": project_state["current_step"],
"iterations": project_state["iterations"],
"max_iterations": project_state["max_iterations"],
},
"current_task": current_task,
"previous_tasks": project_state.get("previous_tasks", []),
}
def manager_agent_task(
context: Dict[str, Any],
model: str,
temperature: float,
max_tokens: int,
openai_api_key=None
) -> Tuple[Dict[str, Any], Any]:
"""Handles the manager agent task based on the context."""
prompt = f"""
You are the manager agent in an Agile software development process.
Current project state: {json.dumps(context['project_state'], indent=2)}
Current task: {context['current_task']}
Based on the current state, please:
1. Analyze the current project step.
2. Update the work plan for the next sprint.
3. Prioritize tasks for the coding agents.
4. If repository files haven't been created yet, make this the top priority.
Respond with a JSON object containing:
1. "analysis": Your analysis of the current state.
2. "work_plan": The updated work plan for the next sprint.
3. "priorities": A list of prioritized tasks.
4. "instructions": Clear instructions for the coding agents.
5. "create_files": true if repository files should be created, false otherwise.
Your response must be a valid JSON object.
"""
try:
temperature = float(temperature)
max_tokens = int(max_tokens)
if model.startswith("gpt-"):
client = OpenAI(api_key=openai_api_key)
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=temperature,
max_tokens=max_tokens,
response_format={"type": "json_object"}
)
response_content = response.choices[0].message.content
elif model in GROQ_MODELS:
response_content = call_groq_api(model, prompt, temperature, max_tokens, groq_api_key=openai_api_key)
else:
# Assume it's an Ollama model
response_content, _, _, _ = call_ollama_endpoint(
model=model,
prompt=prompt,
temperature=temperature,
max_tokens=max_tokens
)
parsed_response = json.loads(response_content)
return parsed_response, None
except json.JSONDecodeError as e:
error_msg = f"Error parsing JSON response from Manager Agent: {e}"
console.print(error_msg)
console.print(f"Raw response that caused the error: {response_content}")
return {
"analysis": "Error parsing response. Please review the raw output.",
"work_plan": "Unable to generate work plan due to parsing error.",
"priorities": ["Review and fix JSON parsing issues"],
"instructions": "Please review the raw output and manually extract relevant information.",
}, None
except Exception as e:
error_msg = f"An error occurred during the manager agent task: {str(e)}"
console.print(error_msg)
return {}, None
def create_repository_files(project_dir: Path, refined_output: str) -> Dict[str, str]:
"""
Creates repository files based on the refined output.
Returns a dictionary of filenames and their contents.
"""
created_files = {}
# Extract folder structure
folder_structure = parse_folder_structure(refined_output)
if folder_structure:
create_folder_structure(project_dir, folder_structure)
# Extract and create code files
code_blocks = extract_code_blocks(refined_output)
for filename, code in code_blocks.items():
file_path = project_dir / filename
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(code, encoding="utf-8")
created_files[str(file_path.relative_to(project_dir))] = code
return created_files
def coding_agent_task(
prompt: str,
model: str,
previous_tasks=None,
use_search=False,
continuation=False,
temperature: float = 0.2,
max_tokens: int = 8000,
search_results=None,
groq_api_key=None,
openai_api_key=None
) -> Dict[str, Any]:
"""Handles the coding agent task based on the provided prompt and context."""
if previous_tasks is None:
previous_tasks = []
previous_tasks_str = "\n".join(
[
f"Task: {task.get('task', '')}\nResult: {task.get('result', '')}"
for task in previous_tasks
if isinstance(task, dict)
]
)
continuation_prompt = (
"Continuing from the previous answer, please complete the response."
)
previous_tasks_summary = (
f"Previous Sub-agent tasks:\n{previous_tasks_str}"
if previous_tasks_str
else "No previous sub-agent tasks."
)
if continuation:
prompt = continuation_prompt
full_prompt = f"{previous_tasks_summary}\n\n{prompt}"
if use_search and search_results:
full_prompt += f"\n\nSearch Results:\n{json.dumps(search_results, indent=2)}"
if not full_prompt.strip():
raise ValueError("Prompt cannot be empty")
full_prompt += "\n\nRespond with a JSON object containing your implementation details and any necessary explanations. Your response must be a valid JSON object."
messages = [{"role": "user", "content": full_prompt}]
try:
temperature = float(temperature)
max_tokens = int(max_tokens)
if model.startswith("gpt-"):
client = OpenAI(api_key=openai_api_key)
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
response_format={"type": "json_object"}
)
response_content = response.choices[0].message.content
elif model in GROQ_MODELS:
response_content = call_groq_api(model, full_prompt, temperature, max_tokens, groq_api_key=groq_api_key)
else:
# Assume it's an Ollama model
response_content, _, _, _ = call_ollama_endpoint(
model=model,
prompt=full_prompt,
temperature=temperature,
max_tokens=max_tokens
)
parsed_response = json.loads(response_content)
return parsed_response
except json.JSONDecodeError as e:
error_msg = f"Error parsing JSON response from Coding Agent: {e}"
console.print(error_msg)
console.print(f"Raw response that caused the error: {response_content}")
return {"error": error_msg, "raw_response": response_content}
except Exception as e:
error_msg = f"An error occurred during the coding agent task: {str(e)}"
console.print(error_msg)
return {"error": error_msg}
def refine_task(
objective: str,
model: str,
sub_task_results: List[str],
filename: str,
projectname: str,
continuation=False,
temperature: float = 0.2,
max_tokens: int = 8000,
groq_api_key=None,
openai_api_key=None
) -> str:
"""Handles the task refinement process for the final output."""
console.print("\nCalling Refiner to provide the refined final output for your objective:")
messages = [
{
"role": "user",
"content": "Objective: "
+ objective
+ "\n\nSub-task results:\n"
+ "\n".join(sub_task_results)
+ "\n\nPlease review and refine the sub-task results into a cohesive final output. Add any missing information or details as needed. When working on code projects, ONLY AND ONLY IF THE PROJECT IS CLEARLY A CODING ONE please provide the following:\n1. Project Name: Create a concise and appropriate project name that fits the project based on what it's creating. The project name should be no more than 20 characters long.\n2. Folder Structure: Provide the folder structure as a valid JSON object, where each key represents a folder or file, and nested keys represent subfolders. Use null values for files. Ensure the JSON is properly formatted without any syntax errors. Please make sure all keys are enclosed in double quotes, and ensure objects are correctly encapsulated with braces, separating items with commas as necessary.\nWrap the JSON object in <folder_structure> tags.\n3. Code Files: For each code file, include ONLY the file name NEVER EVER USE THE FILE PATH OR ANY OTHER FORMATTING YOU ONLY USE THE FOLLOWING format 'Filename: <filename>' followed by the code block enclosed in triple backticks, with the language identifier after the opening backticks, like this:\n\nFilename: <filename>\n```python\n<code>\n```",
}
]
try:
if isinstance(model, str) and model.startswith("gpt-"):
response_text = call_openai_api(model, messages, temperature, max_tokens, openai_api_key=openai_api_key)
elif isinstance(model, str) and is_groq_model(model):
response_text = call_groq_api(model, messages[0]["content"], temperature, max_tokens, groq_api_key=groq_api_key)
else:
response_text = call_ollama_endpoint(
model=model,
prompt=messages[0]["content"],
temperature=temperature,
max_tokens=max_tokens
)
# Ensure response_text is a string
if isinstance(response_text, tuple):
response_text = response_text[0]
if not isinstance(response_text, str):
response_text = str(response_text)
if len(response_text) >= 8000 and not continuation:
console.print("Warning: Output may be truncated. Attempting to continue the response.")
continuation_response_text = refine_task(
objective,
model,
sub_task_results + [response_text],
filename,
projectname,
continuation=True,
temperature=temperature,
max_tokens=max_tokens,
groq_api_key=groq_api_key,
openai_api_key=openai_api_key,
)
response_text += "\n" + continuation_response_text
return response_text
except Exception as e:
error_msg = f"An error occurred during the refine task: {str(e)}"
console.print(error_msg)
return error_msg
def parse_folder_structure(structure_string: str) -> dict:
"""Parses the folder structure from the response string."""
structure_string = re.sub(r"\s+", " ", structure_string)
match = re.search(r"<folder_structure>(.*?)</folder_structure>", structure_string)
if not match:
return None
json_string = match.group(1)
try:
structure = json.loads(json_string)
return structure
except json.JSONDecodeError as e:
console.print(f"[bold red]Error parsing JSON:[/bold red] {e}")
console.print(f"[bold red]Invalid JSON string:[/bold red] {json_string}")
return None
def extract_code_blocks(refined_output: str) -> Dict[str, str]:
"""Extracts code blocks from the refined output."""
code_blocks = {}
pattern = r"Filename: (.*?)\n```(.*?)\n```"
matches = re.finditer(pattern, refined_output, re.DOTALL)
for match in matches:
filename = match.group(1).strip()
code = match.group(2).strip()
code_blocks[filename] = code
return code_blocks
def save_file(content: str, filename: str, project_dir: str) -> None:
"""Saves content to a file within the specified project directory."""
try:
file_path = Path(project_dir) / "code" / filename
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(content, encoding="utf-8")
console.print(f"[bold green]Saved file: {file_path}[/bold green]")
except Exception as e:
console.print(f"[bold red]Error saving file '{filename}': {str(e)}[/bold red]")
def dump_repository(repo_path: str) -> Dict[str, str]:
"""Dumps the contents of a repository to a dictionary."""
repo_contents = {}
for root, _, files in os.walk(repo_path):
for file in files:
if file.endswith((".py", ".json", ".md", ".txt", ".yml", ".yaml")):
file_path = os.path.join(root, file)
with open(file_path, "r", encoding="utf-8") as f:
try:
content = f.read()
relative_path = os.path.relpath(file_path, repo_path)
repo_contents[relative_path] = content
except UnicodeDecodeError:
console.print(f"[bold yellow]Skipping binary file: {file_path}[/bold yellow]")
return repo_contents
def generate_test_cases(code_analysis: Dict[str, List[str]]) -> str:
"""Generates test cases based on the analyzed code."""
test_cases = []
for func in code_analysis["functions"]:
test_cases.append(
f"""
def test_{func}():
# TODO: Implement test for {func}
assert True
"""
)
for cls in code_analysis["classes"]:
test_cases.append(
f"""
class Test{cls}:
def test_init(self):
# TODO: Implement test for {cls} initialization
assert True
def test_methods(self):
# TODO: Implement tests for {cls} methods
assert True
"""
)
return "\n".join(test_cases)
def run_tests(project_dir: str) -> Dict[str, Any]:
"""Runs tests in the project directory and returns the results."""
test_dir = os.path.join(project_dir, "tests")
if not os.path.exists(test_dir):
os.makedirs(test_dir)
for root, _, files in os.walk(project_dir):
for file in files:
if file.endswith(".py") and not file.startswith("test_"):
file_path = os.path.join(root, file)
with open(file_path, "r") as f:
code = f.read()
code_analysis = analyze_code(code)
test_cases = generate_test_cases(code_analysis)
test_file_path = os.path.join(test_dir, f"test_{file}")
with open(test_file_path, "w") as f:
f.write(
f"import sys\nimport os\nsys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))\n"
)
f.write(f"from {file[:-3]} import *\n\n")
f.write(test_cases)
try:
pytest_result = subprocess.run(
["pytest", "-v", test_dir], capture_output=True, text=True
)
return {
"pytest_output": pytest_result.stdout,
"passed": pytest_result.stdout.count("PASSED"),
"failed": pytest_result.stdout.count("FAILED"),
"errors": pytest_result.stdout.count("ERROR"),
"warnings": pytest_result.stdout.count("WARN"),
}
except Exception as e:
console.print(f"[bold red]An error occurred during testing: {str(e)}[/bold red]")
return {}
def generate_readme(
project_name: str,
user_request: str,
project_type: str,
refined_output: str,
refiner_model: str
) -> str:
"""Generates a README.md file for the project."""
readme_prompt = f"""
Create a comprehensive README.md file for the following project:
Project Name: {project_name}
Project Type: {project_type}
User Request: {user_request}
Include the following sections:
1. Project Title and Description
2. Installation Instructions
3. Usage Guide
4. Features
5. Dependencies
6. Contributing Guidelines
7. License Information (Assume MIT license)
8. Contact/Support Information (Use generic placeholder information)
Use appropriate Markdown formatting to make the README visually appealing and easy to read.
Base the content on the following refined output from the project generation:
{refined_output}
"""
messages = [{"role": "user", "content": readme_prompt}]
try:
api_keys = load_api_keys()
if refiner_model.startswith("gpt-"):
readme_content = call_openai_api(
model=refiner_model,
messages=messages,
temperature=0.7,
max_tokens=2000,
openai_api_key=api_keys.get("openai_api_key")
)
elif is_groq_model(refiner_model):
readme_content = call_groq_api(
model=refiner_model,
prompt=messages[0]["content"],
temperature=0.7,
max_tokens=2000,
groq_api_key=api_keys.get("groq_api_key")
)
else:
readme_content = call_ollama_endpoint(
model=refiner_model,
prompt=messages[0]["content"],
temperature=0.7,
max_tokens=2000
)
# Ensure readme_content is a string
if isinstance(readme_content, tuple):
readme_content = readme_content[0]
if not isinstance(readme_content, str):
readme_content = str(readme_content)
return readme_content
except Exception as e:
error_msg = f"An error occurred during README generation: {str(e)}"
console.print(error_msg)
return f"# README\n\nError generating README: {error_msg}\n\nPlease check the API connection and try again."
def execute_code(code: str, project_type: str) -> str:
"""Executes the generated code based on the project type."""
if project_type == "Command-line Tool":
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".py") as temp_file:
temp_file.write(code)
temp_file_path = temp_file.name
try:
result = subprocess.run(
[sys.executable, temp_file_path], capture_output=True, text=True
)
return result.stdout
except Exception as e:
return f"Error executing command-line tool: {str(e)}"
finally:
os.remove(temp_file_path)
elif project_type == "Streamlit App":
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".py") as temp_file:
temp_file.write(code)
temp_file_path = temp_file.name
try:
subprocess.Popen(
[sys.executable, "-m", "streamlit", "run", temp_file_path]
)
return "Streamlit app launched in a new window."
except Exception as e:
return f"Error launching Streamlit app: {str(e)}"
finally:
os.remove(temp_file_path)
elif project_type == "API":
return "API execution not yet implemented."
elif project_type == "Data Analysis Script":
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".py") as temp_file:
temp_file.write(code)
temp_file_path = temp_file.name
try:
result = subprocess.run(
[sys.executable, temp_file_path], capture_output=True, text=True
)
return result.stdout
except Exception as e:
return f"Error executing data analysis script: {str(e)}"
finally:
os.remove(temp_file_path)
else:
return "Unsupported project type for execution."
def build_project(user_request: str, repo_contents: dict, project_type: str) -> Dict[str, Any]:
"""
Implements the core build logic without running tests.
Args:
user_request (str): The user's project request.
repo_contents (dict): The contents of the repository.
project_type (str): The type of the project.
Returns:
dict: A dictionary containing the build results.
"""
try:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
project_dir = Path("generated_projects") / f"project_{timestamp}"
project_dir.mkdir(parents=True, exist_ok=True)
return {"success": True, "project_dir": project_dir}
except Exception as e:
return {"success": False, "error": str(e)}
def build_interface() -> None:
"""The main interface for the build system."""
os.environ["USER_AGENT"] = "BuildAgent/1.0"
st.title("🔨 Build: Autonomous Multi-Agent Software Development System")
if "project_state" not in st.session_state:
st.session_state.project_state = {
"status": "Not Started",
"current_step": "",
"iterations": 0,
"max_iterations": 3,
"code": "",
"documentation": "",
"test_results": {},
"quality_review": "",
"project_dir": "",
"errors": [],
"warnings": [],
"agent_logs": [],
"progress": 0,
"previous_tasks": [],
}
if "settings" not in st.session_state:
st.session_state.settings = load_settings()
if "api_keys" not in st.session_state:
st.session_state.api_keys = load_api_keys()
all_models = get_all_models()
available_groq_models = get_available_groq_models(st.session_state.api_keys)
all_models = [
model for model in all_models if model not in GROQ_MODELS or model in available_groq_models
]
progress_bar = st.progress(0)
status_text = st.empty()
# Sidebar
with st.sidebar:
with st.expander("🤖 Model Selection"):
def get_valid_model(model_key, default_index=0):
saved_model = st.session_state.settings.get(model_key)
try:
index = all_models.index(saved_model)
except ValueError:
index = default_index
return st.selectbox(
f"{model_key.replace('_', ' ').title()}", all_models, index=index
)
# Manager settings
st.session_state.settings["manager_model"] = get_valid_model("manager_model")
st.session_state.settings["manager_temperature"] = st.slider(
"Manager Temperature",
min_value=0.0,
max_value=1.0,
value=float(st.session_state.settings.get("manager_temperature", 0.2)),
step=0.1,
)
st.session_state.settings["manager_max_tokens"] = st.slider(
"Manager Max Tokens",
min_value=1000,
max_value=128000,
value=int(st.session_state.settings.get("manager_max_tokens", 8000)),
step=1000,
)
# Subagent settings
st.session_state.settings["subagent_model"] = get_valid_model("subagent_model")
st.session_state.settings["subagent_temperature"] = st.slider(
"Subagent Temperature",
min_value=0.0,
max_value=1.0,
value=float(st.session_state.settings.get("subagent_temperature", 0.2)),
step=0.1,
)
st.session_state.settings["subagent_max_tokens"] = st.slider(
"Subagent Max Tokens",
min_value=1000,
max_value=128000,
value=int(st.session_state.settings.get("subagent_max_tokens", 8000)),
step=1000,
)
# Refiner settings
st.session_state.settings["refiner_model"] = get_valid_model("refiner_model")
st.session_state.settings["refiner_temperature"] = st.slider(
"Refiner Temperature",
min_value=0.0,
max_value=1.0,
value=float(st.session_state.settings.get("refiner_temperature", 0.2)),
step=0.1,
)
st.session_state.settings["refiner_max_tokens"] = st.slider(
"Refiner Max Tokens",
min_value=1000,
max_value=128000,
value=int(st.session_state.settings.get("refiner_max_tokens", 8000)),
step=1000,
)
if st.button("💾 Save Settings"):
save_settings(st.session_state.settings)
st.success("Settings saved successfully!")
input_method = st.radio(
"Choose input method:", ["Enter Project Request", "Provide Repository Directory"]
)
if input_method == "Enter Project Request":
user_request = st.text_area(
"Enter your project request:", height=100, key="user_request"
)
project_type = st.selectbox(
"Select Project Type",
["Command-line Tool", "Streamlit App", "API", "Data Analysis Script"],
key="project_type",
)
repo_contents = None
else:
repo_dir = st.text_input("Enter the path to your repository directory:")
if repo_dir and os.path.isdir(repo_dir):
repo_contents = dump_repository(repo_dir)
st.success(
f"Repository loaded successfully. Found {len(repo_contents)} files."
)
else:
repo_contents = None
if repo_dir:
st.error("Invalid directory path. Please enter a valid directory.")
user_request = None
project_type = None
use_search = st.checkbox("Use search functionality")
if use_search:
search_method = st.selectbox(
"Select search method", ["duckduckgo", "google", "serpapi"]
)
if search_method == "google":
st.session_state.api_keys["google_api_key"] = st.text_input(
"Google API Key",
value=st.session_state.api_keys.get("google_api_key", ""),
type="password",
)
st.session_state.api_keys["google_cse_id"] = st.text_input(
"Google Custom Search Engine ID",
value=st.session_state.api_keys.get("google_cse_id", ""),
type="password",
)
elif search_method == "serpapi":
st.session_state.api_keys["serpapi_api_key"] = st.text_input(
"SerpApi API Key",
value=st.session_state.api_keys.get("serpapi_api_key", ""),
type="password",
)
num_results = st.number_input(
"Number of search results", min_value=1, max_value=20, value=5
)
save_api_keys(st.session_state.api_keys)
else:
search_method = None
num_results = None
(tab1, tab2, tab3) = st.tabs(
["Workflow", "Documentation", "Test Results"]
)
if st.button("🚀 Build Project"):
if user_request or repo_contents:
st.info(f"Starting new project build")
if user_request:
st.info(f"User Request: {user_request}")
st.info(f"Project Type: {project_type}")
else:
st.info(f"Repository loaded with {len(repo_contents)} files")
st.session_state.project_state["status"] = "In Progress"
st.session_state.project_state["agent_logs"] = []
st.session_state.project_state["progress"] = 0
st.session_state.project_state["iterations"] = 0
st.session_state.project_state["errors"].clear()
# Create project directory
sanitized_objective = re.sub(r"\W+", "_", user_request if user_request else "repository_improvement")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
project_dir = Path("generated_projects") / f"{sanitized_objective}_{timestamp}"
project_dir.mkdir(parents=True, exist_ok=True)
st.success(f"Created project folder: {project_dir}")
search_results = None
if use_search:
search_query = user_request or "Repository improvement techniques"
search_results = perform_search(
search_query, search_method, st.session_state.api_keys, num_results
)
st.json(search_results)
task_exchanges = []
worker_tasks = []
refined_output = ""
while (
st.session_state.project_state["iterations"] < st.session_state.project_state["max_iterations"]
):
manager_response, _ = manager_agent_task(
create_agent_context(
st.session_state.project_state,
user_request or "Analyze and improve the provided repository",
),
model=st.session_state.settings["manager_model"],
temperature=float(st.session_state.settings["manager_temperature"]),
max_tokens=int(st.session_state.settings["manager_max_tokens"]),
openai_api_key=st.session_state.api_keys.get("openai_api_key"),
)
st.subheader(f"Manager Response - Iteration {st.session_state.project_state['iterations'] + 1}")
st.json(manager_response)
if manager_response.get("create_files", False):
st.subheader("Creating Repository Files")
created_files = create_repository_files(project_dir, refined_output)
st.success(f"Created {len(created_files)} repository files")
for filename, content in created_files.items():
st.text(f"Created file: {filename}")
st.code(content, language="python")
else:
sub_agent_response = coding_agent_task(
manager_response["instructions"],
model=st.session_state.settings["subagent_model"],
previous_tasks=worker_tasks,
use_search=use_search,
temperature=float(st.session_state.settings["subagent_temperature"]),
max_tokens=int(st.session_state.settings["subagent_max_tokens"]),
search_results=search_results,
openai_api_key=st.session_state.api_keys.get("openai_api_key"),
)
st.subheader(f"Sub-agent Response - Iteration {st.session_state.project_state['iterations'] + 1}")
st.json(sub_agent_response)
worker_tasks.append({"task": manager_response["instructions"], "result": sub_agent_response})
task_exchanges.append((manager_response["instructions"], sub_agent_response))
st.session_state.project_state["iterations"] += 1
st.session_state.project_state["progress"] = min(
st.session_state.project_state["iterations"]
/ st.session_state.project_state["max_iterations"],
1.0
)
progress_bar.progress(st.session_state.project_state["progress"])
status_text.text(
f"Iteration {st.session_state.project_state['iterations']} of {st.session_state.project_state['max_iterations']}"
)
# Refiner task
st.subheader("Refiner Task")
refined_output = refine_task(
user_request or "Analyze and improve the provided repository",
model=st.session_state.settings["refiner_model"],
sub_task_results=[json.dumps(result) if isinstance(result, dict) else str(result) for _, result in task_exchanges],
filename=timestamp,
projectname=sanitized_objective,
temperature=float(st.session_state.settings["refiner_temperature"]),
max_tokens=int(st.session_state.settings["refiner_max_tokens"]),
openai_api_key=st.session_state.api_keys.get("openai_api_key"),
)
if isinstance(refined_output, tuple):
refined_output = refined_output[0]
st.text_area("Refined Output:", refined_output, height=400)
project_name_match = re.search(r"Project Name: (.*)", refined_output)
project_name = (
project_name_match.group(1).strip()
if project_name_match
else sanitized_objective
)
folder_structure = parse_folder_structure(refined_output)
code_blocks = extract_code_blocks(refined_output)
project_dir = Path("generated_projects") / f"{project_name}_{timestamp}"
project_dir.mkdir(parents=True, exist_ok=True)
st.success(f"Created project folder: {project_dir}")
# Create folder structure
if folder_structure:
create_folder_structure(project_dir, folder_structure)
st.success("Created folder structure")
# Create code files
if code_blocks:
for filename, code in code_blocks.items():
file_path = project_dir / filename
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(code, encoding="utf-8")
st.success(f"Created file: {file_path}")
# Generate README.md
readme_content = generate_readme(
project_name,
user_request,
project_type,