Update core.py
Browse files
core.py
CHANGED
@@ -1,119 +1,49 @@
|
|
1 |
-
# /core.py
|
2 |
-
|
3 |
-
"""
|
4 |
-
Core business logic for the code generation application.
|
5 |
-
|
6 |
-
This module orchestrates the entire process from receiving a user query to
|
7 |
-
generating the final code. It interacts with the services, extractors, and
|
8 |
-
utility modules to fulfill the request.
|
9 |
-
"""
|
10 |
from typing import Dict, List, Optional, Tuple, Generator, Any
|
11 |
-
|
12 |
-
|
13 |
-
HTML_SYSTEM_PROMPT, GENERIC_SYSTEM_PROMPT,
|
14 |
-
HTML_SYSTEM_PROMPT_WITH_SEARCH, GENERIC_SYSTEM_PROMPT_WITH_SEARCH,
|
15 |
-
FOLLOW_UP_SYSTEM_PROMPT
|
16 |
-
)
|
17 |
from services import llm_service, search_service
|
18 |
from extractor import extract_text_from_file, extract_website_content
|
19 |
-
from utils import (
|
20 |
-
|
21 |
-
remove_code_block,
|
22 |
-
process_image_for_model,
|
23 |
-
apply_search_replace_changes # <--- FIX: Corrected the function name here
|
24 |
-
)
|
25 |
|
26 |
-
# --- Type Definitions ---
|
27 |
History = List[Tuple[Optional[str], Optional[str]]]
|
28 |
|
29 |
def _determine_system_prompt(language: str, enable_search: bool, history: History) -> Tuple[str, bool]:
|
30 |
-
|
31 |
-
is_follow_up
|
32 |
-
if history and history[-1][1] and ("<!DOCTYPE html>" in history[-1][1] or "<html" in history[-1][1]):
|
33 |
-
is_follow_up = True
|
34 |
-
return FOLLOW_UP_SYSTEM_PROMPT, is_follow_up
|
35 |
|
36 |
if language == "html":
|
37 |
-
|
38 |
else:
|
39 |
-
|
40 |
-
|
41 |
-
return prompt, is_follow_up
|
42 |
|
43 |
-
def _prepare_user_content(
|
44 |
-
query: str, image_data: Optional[Any], file_path: Optional[str],
|
45 |
-
website_url: Optional[str], enable_search: bool
|
46 |
-
) -> any:
|
47 |
-
"""Constructs the final user prompt including context from files, web, and search."""
|
48 |
context_parts = [query]
|
49 |
-
|
50 |
-
if
|
51 |
-
file_text = extract_text_from_file(file_path)
|
52 |
-
context_parts.append(f"\n\n--- Reference File Content ---\n{file_text[:8000]}")
|
53 |
-
|
54 |
-
if website_url:
|
55 |
-
web_text = extract_website_content(website_url)
|
56 |
-
context_parts.append(f"\n\n--- Website Content for Redesign ---\n{web_text[:8000]}")
|
57 |
-
|
58 |
full_query = "".join(context_parts)
|
59 |
-
|
60 |
-
|
61 |
-
search_results = search_service.search(full_query)
|
62 |
-
full_query += f"\n\n--- Web Search Results ---\n{search_results}"
|
63 |
-
|
64 |
if image_data is not None:
|
65 |
-
return [
|
66 |
-
{"type": "text", "text": full_query},
|
67 |
-
{"type": "image_url", "image_url": {"url": process_image_for_model(image_data)}}
|
68 |
-
]
|
69 |
return full_query
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
query: str,
|
74 |
-
image_data: Optional[Any],
|
75 |
-
file_path: Optional[str],
|
76 |
-
website_url: Optional[str],
|
77 |
-
history: History,
|
78 |
-
model_config: Dict[str, str],
|
79 |
-
enable_search: bool,
|
80 |
-
language: str
|
81 |
-
) -> Generator[Dict[str, Any], None, None]:
|
82 |
-
"""
|
83 |
-
Main generator function to handle a user request and stream responses.
|
84 |
-
"""
|
85 |
system_prompt, is_follow_up = _determine_system_prompt(language, enable_search, history)
|
86 |
messages = history_to_messages(history, system_prompt)
|
87 |
user_content = _prepare_user_content(query, image_data, file_path, website_url, enable_search)
|
88 |
messages.append({'role': 'user', 'content': user_content})
|
89 |
|
90 |
content_stream = ""
|
91 |
-
|
92 |
-
|
93 |
-
for chunk in stream:
|
94 |
content_stream += chunk
|
95 |
-
processed_code =
|
96 |
-
|
97 |
-
if is_follow_up:
|
98 |
-
last_html = history[-1][1] if history and history[-1][1] else ""
|
99 |
-
# <--- FIX: Use the corrected function name here
|
100 |
-
modified_html = apply_search_replace_changes(last_html, content_stream)
|
101 |
-
processed_code = modified_html
|
102 |
-
else:
|
103 |
-
processed_code = remove_code_block(content_stream)
|
104 |
-
|
105 |
yield {"code_output": processed_code}
|
106 |
|
107 |
-
|
108 |
-
final_content = content_stream
|
109 |
-
if is_follow_up:
|
110 |
-
last_html = history[-1][1] if history and history[-1][1] else ""
|
111 |
-
# <--- FIX: And use the corrected function name here as well
|
112 |
-
final_code = apply_search_replace_changes(last_html, final_content)
|
113 |
-
else:
|
114 |
-
final_code = remove_code_block(final_content)
|
115 |
-
|
116 |
-
# Use the original query for history display, not the enhanced one.
|
117 |
history.append((query, final_code))
|
118 |
-
|
119 |
yield {"code_output": final_code, "history": history}
|
|
|
1 |
+
# /core.py
|
2 |
+
""" Core business logic for the code generation application. """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from typing import Dict, List, Optional, Tuple, Generator, Any
|
4 |
+
from config import (HTML_SYSTEM_PROMPT, GENERIC_SYSTEM_PROMPT, HTML_SYSTEM_PROMPT_WITH_SEARCH,
|
5 |
+
GENERIC_SYSTEM_PROMPT_WITH_SEARCH, FollowUpSystemPrompt)
|
|
|
|
|
|
|
|
|
6 |
from services import llm_service, search_service
|
7 |
from extractor import extract_text_from_file, extract_website_content
|
8 |
+
from utils import (history_to_messages, remove_code_block, process_image_for_model,
|
9 |
+
apply_search_replace_changes)
|
|
|
|
|
|
|
|
|
10 |
|
|
|
11 |
History = List[Tuple[Optional[str], Optional[str]]]
|
12 |
|
13 |
def _determine_system_prompt(language: str, enable_search: bool, history: History) -> Tuple[str, bool]:
|
14 |
+
is_follow_up = bool(history and history[-1][1] and ("<html" in history[-1][1]))
|
15 |
+
if is_follow_up: return FollowUpSystemPrompt, True
|
|
|
|
|
|
|
16 |
|
17 |
if language == "html":
|
18 |
+
return (HTML_SYSTEM_PROMPT_WITH_SEARCH if enable_search else HTML_SYSTEM_PROMPT), False
|
19 |
else:
|
20 |
+
base = GENERIC_SYSTEM_PROMPT_WITH_SEARCH if enable_search else GENERIC_SYSTEM_PROMPT
|
21 |
+
return base.format(language=language), False
|
|
|
22 |
|
23 |
+
def _prepare_user_content(query: str, image_data: Optional[Any], file_path: Optional[str], website_url: Optional[str], enable_search: bool) -> Any:
|
|
|
|
|
|
|
|
|
24 |
context_parts = [query]
|
25 |
+
if file_path: context_parts.append(f"\n\n--- Reference File ---\n{extract_text_from_file(file_path)[:8000]}")
|
26 |
+
if website_url: context_parts.append(f"\n\n--- Website to Redesign ---\n{extract_website_content(website_url)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
full_query = "".join(context_parts)
|
28 |
+
if enable_search and search_service.is_available(): full_query += f"\n\n--- Web Search Results ---\n{search_service.search(full_query)}"
|
29 |
+
|
|
|
|
|
|
|
30 |
if image_data is not None:
|
31 |
+
return [{"type": "text", "text": full_query}, {"type": "image_url", "image_url": {"url": process_image_for_model(image_data)}}]
|
|
|
|
|
|
|
32 |
return full_query
|
33 |
|
34 |
+
def generate_code(query: str, image_data: Optional[Any], file_path: Optional[str], website_url: Optional[str], history: History,
|
35 |
+
model_config: Dict[str, str], enable_search: bool, language: str) -> Generator[Dict[str, Any], None, None]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
system_prompt, is_follow_up = _determine_system_prompt(language, enable_search, history)
|
37 |
messages = history_to_messages(history, system_prompt)
|
38 |
user_content = _prepare_user_content(query, image_data, file_path, website_url, enable_search)
|
39 |
messages.append({'role': 'user', 'content': user_content})
|
40 |
|
41 |
content_stream = ""
|
42 |
+
for chunk in llm_service.generate_code_stream(model_config['id'], messages):
|
|
|
|
|
43 |
content_stream += chunk
|
44 |
+
processed_code = apply_search_replace_changes(history[-1][1], content_stream) if is_follow_up else remove_code_block(content_stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
yield {"code_output": processed_code}
|
46 |
|
47 |
+
final_code = apply_search_replace_changes(history[-1][1], content_stream) if is_follow_up else remove_code_block(content_stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
history.append((query, final_code))
|
|
|
49 |
yield {"code_output": final_code, "history": history}
|