acecalisto3 commited on
Commit
52165bc
·
verified ·
1 Parent(s): 6592a3f

Update supplemental.py

Browse files
Files changed (1) hide show
  1. supplemental.py +73 -11
supplemental.py CHANGED
@@ -99,8 +99,11 @@ class EnhancedAIAgent:
99
  project_files[file_path] = content
100
  return project_files
101
 
102
- def generate_project_config(self, project_description: str) -> ProjectConfig:
103
- prompt = f"""
 
 
 
104
  Based on the following project description, generate a ProjectConfig object:
105
 
106
  Description: {project_description}
@@ -113,16 +116,75 @@ The ProjectConfig should include:
113
 
114
  Respond with a JSON object representing the ProjectConfig.
115
  """
116
- response = self.generate_agent_response(prompt)
117
-
118
- try:
119
- config_dict = json.loads(response)
 
 
 
 
 
120
  return ProjectConfig(**config_dict)
121
- except JSONDecodeError as e:
122
- self.logger.error(f"Error decoding JSON: {str(e)}")
123
- self.logger.error(f"Response from model: {response}")
124
- return None # Or handle the error differently
125
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  def implement_feature(self, feature_description: str, existing_code: Optional[str] = None) -> str:
127
  prompt = f"""
128
  Feature to implement: {feature_description}
 
99
  project_files[file_path] = content
100
  return project_files
101
 
102
+ import json
103
+ from json.decoder import JSONDecodeError
104
+
105
+ def generate_project_config(self, project_description: str) -> Optional[ProjectConfig]:
106
+ prompt = f"""
107
  Based on the following project description, generate a ProjectConfig object:
108
 
109
  Description: {project_description}
 
116
 
117
  Respond with a JSON object representing the ProjectConfig.
118
  """
119
+ response = self.generate_agent_response(prompt)
120
+
121
+ try:
122
+ # Try to find and extract a JSON object from the response
123
+ json_start = response.find('{')
124
+ json_end = response.rfind('}') + 1
125
+ if json_start != -1 and json_end != -1:
126
+ json_str = response[json_start:json_end]
127
+ config_dict = json.loads(json_str)
128
  return ProjectConfig(**config_dict)
129
+ else:
130
+ raise ValueError("No JSON object found in the response")
131
+ except (JSONDecodeError, ValueError) as e:
132
+ self.logger.error(f"Error parsing JSON from response: {str(e)}")
133
+ self.logger.error(f"Full response from model: {response}")
134
+
135
+ # Attempt to salvage partial information
136
+ try:
137
+ partial_config = self.extract_partial_config(response)
138
+ if partial_config:
139
+ self.logger.warning("Extracted partial config from malformed response")
140
+ return partial_config
141
+ except Exception as ex:
142
+ self.logger.error(f"Failed to extract partial config: {str(ex)}")
143
+
144
+ return None
145
+
146
+ def extract_partial_config(self, response: str) -> Optional[ProjectConfig]:
147
+ """Attempt to extract partial configuration from a malformed response."""
148
+ name = self.extract_field(response, "name")
149
+ description = self.extract_field(response, "description")
150
+ technologies = self.extract_list(response, "technologies")
151
+ structure = self.extract_dict(response, "structure")
152
+
153
+ if name and description:
154
+ return ProjectConfig(
155
+ name=name,
156
+ description=description,
157
+ technologies=technologies or [],
158
+ structure=structure or {}
159
+ )
160
+ return None
161
+
162
+ def extract_field(self, text: str, field: str) -> Optional[str]:
163
+ """Extract a simple field value from text."""
164
+ match = re.search(rf'"{field}"\s*:\s*"([^"]*)"', text)
165
+ return match.group(1) if match else None
166
+
167
+ def extract_list(self, text: str, field: str) -> Optional[List[str]]:
168
+ """Extract a list from text."""
169
+ match = re.search(rf'"{field}"\s*:\s*\[(.*?)\]', text, re.DOTALL)
170
+ if match:
171
+ items = re.findall(r'"([^"]*)"', match.group(1))
172
+ return items
173
+ return None
174
+
175
+ def extract_dict(self, text: str, field: str) -> Optional[Dict[str, List[str]]]:
176
+ """Extract a dictionary from text."""
177
+ match = re.search(rf'"{field}"\s*:\s*\{{(.*?)\}}', text, re.DOTALL)
178
+ if match:
179
+ dict_str = match.group(1)
180
+ result = {}
181
+ for item in re.finditer(r'"([^"]*)"\s*:\s*\[(.*?)\]', dict_str, re.DOTALL):
182
+ key = item.group(1)
183
+ values = re.findall(r'"([^"]*)"', item.group(2))
184
+ result[key] = values
185
+ return result
186
+ return None
187
+
188
  def implement_feature(self, feature_description: str, existing_code: Optional[str] = None) -> str:
189
  prompt = f"""
190
  Feature to implement: {feature_description}