Update app.py
Browse files
app.py
CHANGED
@@ -140,4 +140,307 @@ Please analyze and correct the following Apex Trigger code for migration from Cl
|
|
140 |
Identify any issues, optimize it according to best practices, and provide the corrected code.
|
141 |
|
142 |
```apex
|
143 |
-
{trigger_code}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
Identify any issues, optimize it according to best practices, and provide the corrected code.
|
141 |
|
142 |
```apex
|
143 |
+
{trigger_code}
|
144 |
+
```
|
145 |
+
|
146 |
+
Please return the corrected code with explanations of changes.
|
147 |
+
"""
|
148 |
+
return call_llm(model, prompt)
|
149 |
+
|
150 |
+
def convert_cc_object(model, cc_object_code):
|
151 |
+
if not cc_object_code.strip():
|
152 |
+
return "Please provide CloudCraze Object code to convert."
|
153 |
+
prompt = f"""
|
154 |
+
Please convert the following CloudCraze Object code to B2B Lightning Experience format.
|
155 |
+
Identify the corresponding B2B LEx system object, map fields, and provide the full definition.
|
156 |
+
|
157 |
+
```
|
158 |
+
{cc_object_code}
|
159 |
+
```
|
160 |
+
|
161 |
+
Return:
|
162 |
+
1. Equivalent B2B LEx object name
|
163 |
+
2. Field mappings
|
164 |
+
3. Full implementation code
|
165 |
+
4. Additional steps if needed
|
166 |
+
"""
|
167 |
+
return call_llm(model, prompt)
|
168 |
+
|
169 |
+
def validate_apex_trigger(validation_model, original_code, corrected_code):
|
170 |
+
if not validation_model or not original_code.strip() or not corrected_code.strip():
|
171 |
+
return "Please provide all required inputs for validation."
|
172 |
+
|
173 |
+
prompt = f"""
|
174 |
+
I need you to validate and review the following Apex code migration. This involves both CloudCraze to B2B Lightning Experience conversions AND general Apex code optimization.
|
175 |
+
|
176 |
+
ORIGINAL CODE:
|
177 |
+
```apex
|
178 |
+
{original_code}
|
179 |
+
```
|
180 |
+
|
181 |
+
CORRECTED CODE:
|
182 |
+
```apex
|
183 |
+
{corrected_code}
|
184 |
+
```
|
185 |
+
|
186 |
+
Please review the corrected code and provide:
|
187 |
+
1. Is the correction accurate and complete? Highlight any missed issues.
|
188 |
+
2. Are there any potential runtime errors or performance concerns?
|
189 |
+
3. Does it follow Salesforce best practices for B2B Lightning Experience?
|
190 |
+
4. Are there optimization opportunities that were missed?
|
191 |
+
5. Evaluate both the B2B Commerce conversion aspects AND the general Apex optimization.
|
192 |
+
6. Rate the quality of the correction on a scale of 1-10 with explanation.
|
193 |
+
|
194 |
+
Additionally, provide a structured assessment in JSON format following this schema:
|
195 |
+
{json.dumps(VALIDATION_SCHEMA, indent=2)}
|
196 |
+
|
197 |
+
Be thorough and detailed in your assessment, addressing both the conversion and optimization aspects.
|
198 |
+
"""
|
199 |
+
return call_llm(validation_model, prompt)
|
200 |
+
|
201 |
+
def validate_cc_object_conversion(validation_model, original_object, converted_object):
|
202 |
+
if not validation_model or not original_object.strip() or not converted_object.strip():
|
203 |
+
return "Please provide all required inputs for validation."
|
204 |
+
|
205 |
+
prompt = f"""
|
206 |
+
I need you to validate and review the following CloudCraze Object conversion to B2B Lightning Experience format. This involves both CloudCraze to B2B Lightning Experience conversions AND optimization of the resulting code.
|
207 |
+
|
208 |
+
ORIGINAL CLOUDCRAZE OBJECT:
|
209 |
+
```
|
210 |
+
{original_object}
|
211 |
+
```
|
212 |
+
|
213 |
+
CONVERTED B2B LEX OBJECT:
|
214 |
+
```
|
215 |
+
{converted_object}
|
216 |
+
```
|
217 |
+
|
218 |
+
Please review the conversion and provide:
|
219 |
+
1. Is the object mapping correct and complete? Identify any missed fields or relationships.
|
220 |
+
2. Are there any potential data migration issues or concerns?
|
221 |
+
3. Does the converted object follow B2B Lightning Experience best practices?
|
222 |
+
4. Are there optimization opportunities that were missed in the conversion?
|
223 |
+
5. Evaluate both the B2B Commerce conversion aspects AND the optimization of the resulting code.
|
224 |
+
6. Rate the quality of the conversion on a scale of 1-10 with explanation.
|
225 |
+
|
226 |
+
Additionally, provide a structured assessment in JSON format following this schema:
|
227 |
+
{json.dumps(VALIDATION_SCHEMA, indent=2)}
|
228 |
+
|
229 |
+
Be thorough and detailed in your assessment, addressing both the conversion and optimization aspects.
|
230 |
+
"""
|
231 |
+
return call_llm(validation_model, prompt)
|
232 |
+
|
233 |
+
def extract_validation_metrics(validation_text):
|
234 |
+
try:
|
235 |
+
# Try to find JSON in the response
|
236 |
+
start_idx = validation_text.find('{')
|
237 |
+
end_idx = validation_text.rfind('}') + 1
|
238 |
+
|
239 |
+
if start_idx != -1 and end_idx != -1 and start_idx < end_idx:
|
240 |
+
json_str = validation_text[start_idx:end_idx]
|
241 |
+
data = json.loads(json_str)
|
242 |
+
|
243 |
+
# Extract the required metrics
|
244 |
+
metrics = {
|
245 |
+
"quality_rating": data.get("quality_rating", 0),
|
246 |
+
"accuracy": data.get("accuracy", 0.0),
|
247 |
+
"completeness": data.get("completeness", 0.0),
|
248 |
+
"best_practices_alignment": data.get("best_practices_alignment", 0.0)
|
249 |
+
}
|
250 |
+
|
251 |
+
return metrics
|
252 |
+
return None
|
253 |
+
except Exception as e:
|
254 |
+
print(f"Error extracting metrics: {e}")
|
255 |
+
return None
|
256 |
+
|
257 |
+
def create_radar_chart(metrics):
|
258 |
+
if not metrics:
|
259 |
+
return None
|
260 |
+
|
261 |
+
# Create data for the radar chart
|
262 |
+
categories = ["Quality", "Accuracy", "Completeness", "Best Practices"]
|
263 |
+
values = [
|
264 |
+
metrics["quality_rating"] / 10, # Normalize to 0-1 scale
|
265 |
+
metrics["accuracy"],
|
266 |
+
metrics["completeness"],
|
267 |
+
metrics["best_practices_alignment"]
|
268 |
+
]
|
269 |
+
|
270 |
+
# Create a DataFrame for plotting
|
271 |
+
df = pd.DataFrame({
|
272 |
+
'Category': categories,
|
273 |
+
'Value': values
|
274 |
+
})
|
275 |
+
|
276 |
+
# Create the radar chart
|
277 |
+
fig = px.line_polar(
|
278 |
+
df, r='Value', theta='Category', line_close=True,
|
279 |
+
range_r=[0, 1], title="Validation Assessment"
|
280 |
+
)
|
281 |
+
fig.update_traces(fill='toself')
|
282 |
+
|
283 |
+
return fig
|
284 |
+
|
285 |
+
def main():
|
286 |
+
with gr.Blocks(title="Salesforce B2B Commerce Migration Assistant") as app:
|
287 |
+
gr.Markdown("# Salesforce B2B Commerce Migration Assistant")
|
288 |
+
gr.Markdown("This tool helps migrate CloudCraze code to B2B Lightning Experience.")
|
289 |
+
|
290 |
+
# Create model dropdowns
|
291 |
+
with gr.Row():
|
292 |
+
with gr.Column():
|
293 |
+
gr.Markdown("### Primary Model")
|
294 |
+
primary_model_dropdown = gr.Dropdown(
|
295 |
+
choices=all_models,
|
296 |
+
value=anthropic_models[0], # Default to Claude 3.7 Sonnet
|
297 |
+
label="Select Primary AI Model for Conversion"
|
298 |
+
)
|
299 |
+
|
300 |
+
with gr.Column():
|
301 |
+
gr.Markdown("### Validation Model")
|
302 |
+
validation_model_dropdown = gr.Dropdown(
|
303 |
+
choices=all_models,
|
304 |
+
value=anthropic_models[1], # Default to Claude 3 Haiku
|
305 |
+
label="Select Validation AI Model for Review",
|
306 |
+
info="This model will validate and review the output from the primary model"
|
307 |
+
)
|
308 |
+
|
309 |
+
with gr.Tab("Apex Trigger Correction"):
|
310 |
+
gr.Markdown("### Apex Trigger Correction")
|
311 |
+
gr.Markdown("Paste your Apex Trigger code below:")
|
312 |
+
|
313 |
+
trigger_input = gr.Textbox(
|
314 |
+
lines=12,
|
315 |
+
placeholder="Paste Apex Trigger code here...",
|
316 |
+
label="Apex Trigger Code"
|
317 |
+
)
|
318 |
+
trigger_output = gr.Textbox(
|
319 |
+
lines=15,
|
320 |
+
label="Corrected Apex Trigger",
|
321 |
+
interactive=True
|
322 |
+
)
|
323 |
+
trigger_button = gr.Button("Correct Apex Trigger")
|
324 |
+
trigger_button.click(
|
325 |
+
fn=correct_apex_trigger,
|
326 |
+
inputs=[primary_model_dropdown, trigger_input],
|
327 |
+
outputs=trigger_output,
|
328 |
+
show_progress=True
|
329 |
+
)
|
330 |
+
|
331 |
+
gr.Markdown("### Validation Results")
|
332 |
+
with gr.Row():
|
333 |
+
with gr.Column(scale=2):
|
334 |
+
trigger_validation_output = gr.Textbox(
|
335 |
+
lines=15,
|
336 |
+
label="Validation Assessment",
|
337 |
+
placeholder="Validation results will appear here after clicking 'Validate Correction'",
|
338 |
+
interactive=True
|
339 |
+
)
|
340 |
+
with gr.Column(scale=1):
|
341 |
+
trigger_chart = gr.Plot(label="Validation Metrics")
|
342 |
+
|
343 |
+
validate_trigger_button = gr.Button("Validate Correction")
|
344 |
+
|
345 |
+
def validate_and_chart_trigger(model, original, corrected):
|
346 |
+
validation_text = validate_apex_trigger(model, original, corrected)
|
347 |
+
metrics = extract_validation_metrics(validation_text)
|
348 |
+
chart = create_radar_chart(metrics) if metrics else None
|
349 |
+
return validation_text, chart
|
350 |
+
|
351 |
+
validate_trigger_button.click(
|
352 |
+
fn=validate_and_chart_trigger,
|
353 |
+
inputs=[validation_model_dropdown, trigger_input, trigger_output],
|
354 |
+
outputs=[trigger_validation_output, trigger_chart],
|
355 |
+
show_progress=True
|
356 |
+
)
|
357 |
+
|
358 |
+
with gr.Row():
|
359 |
+
trigger_clear = gr.Button("Clear Input")
|
360 |
+
trigger_clear.click(lambda: "", [], trigger_input)
|
361 |
+
|
362 |
+
results_clear = gr.Button("Clear Results")
|
363 |
+
results_clear.click(
|
364 |
+
lambda: ["", "", None],
|
365 |
+
[],
|
366 |
+
[trigger_output, trigger_validation_output, trigger_chart]
|
367 |
+
)
|
368 |
+
|
369 |
+
with gr.Tab("CloudCraze Object Conversion"):
|
370 |
+
gr.Markdown("### CloudCraze Object Conversion")
|
371 |
+
gr.Markdown("Paste your CloudCraze Object code below:")
|
372 |
+
|
373 |
+
object_input = gr.Textbox(
|
374 |
+
lines=12,
|
375 |
+
placeholder="Paste CC object code here...",
|
376 |
+
label="CloudCraze Object Code"
|
377 |
+
)
|
378 |
+
object_output = gr.Textbox(
|
379 |
+
lines=15,
|
380 |
+
label="Converted B2B LEx Object",
|
381 |
+
interactive=True
|
382 |
+
)
|
383 |
+
object_button = gr.Button("Convert Object")
|
384 |
+
object_button.click(
|
385 |
+
fn=convert_cc_object,
|
386 |
+
inputs=[primary_model_dropdown, object_input],
|
387 |
+
outputs=object_output,
|
388 |
+
show_progress=True
|
389 |
+
)
|
390 |
+
|
391 |
+
gr.Markdown("### Validation Results")
|
392 |
+
with gr.Row():
|
393 |
+
with gr.Column(scale=2):
|
394 |
+
object_validation_output = gr.Textbox(
|
395 |
+
lines=15,
|
396 |
+
label="Validation Assessment",
|
397 |
+
placeholder="Validation results will appear here after clicking 'Validate Conversion'",
|
398 |
+
interactive=True
|
399 |
+
)
|
400 |
+
with gr.Column(scale=1):
|
401 |
+
object_chart = gr.Plot(label="Validation Metrics")
|
402 |
+
|
403 |
+
validate_object_button = gr.Button("Validate Conversion")
|
404 |
+
|
405 |
+
def validate_and_chart_object(model, original, converted):
|
406 |
+
validation_text = validate_cc_object_conversion(model, original, converted)
|
407 |
+
metrics = extract_validation_metrics(validation_text)
|
408 |
+
chart = create_radar_chart(metrics) if metrics else None
|
409 |
+
return validation_text, chart
|
410 |
+
|
411 |
+
validate_object_button.click(
|
412 |
+
fn=validate_and_chart_object,
|
413 |
+
inputs=[validation_model_dropdown, object_input, object_output],
|
414 |
+
outputs=[object_validation_output, object_chart],
|
415 |
+
show_progress=True
|
416 |
+
)
|
417 |
+
|
418 |
+
with gr.Row():
|
419 |
+
object_clear = gr.Button("Clear Input")
|
420 |
+
object_clear.click(lambda: "", [], object_input)
|
421 |
+
|
422 |
+
object_results_clear = gr.Button("Clear Results")
|
423 |
+
object_results_clear.click(
|
424 |
+
lambda: ["", "", None],
|
425 |
+
[],
|
426 |
+
[object_output, object_validation_output, object_chart]
|
427 |
+
)
|
428 |
+
|
429 |
+
gr.Markdown("### About This Tool")
|
430 |
+
gr.Markdown(
|
431 |
+
"""
|
432 |
+
- **Primary Model**: Performs the initial code conversion or correction.
|
433 |
+
- **Validation Model**: Reviews and validates the output from the primary model, identifying potential issues or improvements.
|
434 |
+
- **Trigger Correction**: Fixes Apex Triggers for B2B LEx compatibility.
|
435 |
+
- **Object Conversion**: Maps and converts CloudCraze object definitions to B2B LEx.
|
436 |
+
- **Model Selection**: Choose from Together AI models or Anthropic's Claude models.
|
437 |
+
- **Visualization**: See validation metrics in a radar chart for easy assessment.
|
438 |
+
|
439 |
+
Always review AI-generated code before production use.
|
440 |
+
"""
|
441 |
+
)
|
442 |
+
|
443 |
+
app.launch()
|
444 |
+
|
445 |
+
if __name__ == "__main__":
|
446 |
+
main()
|