File size: 5,678 Bytes
79180e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import time
import json
from together import Together

# Set environment variables
# Note: For Hugging Face Spaces, you'll set these in the Secrets tab
# os.environ["TOGETHER_API_KEY"] = "your-api-key-here"

# Model options for dropdown
TOGETHER_MODELS = [
    "Qwen/Qwen2.5-Coder-32B-Instruct",
    "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
    "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
    "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
]

# Initialize Together client
def get_together_client():
    api_key = os.environ.get("TOGETHER_API_KEY")
    if not api_key:
        raise ValueError("TOGETHER_API_KEY not found. Please set it in the environment variables.")
    return Together(api_key=api_key)

# Function to call LLM
def call_llm(model, prompt, temperature=0.7, max_tokens=1500):
    client = get_together_client()
    
    # System message for Salesforce expertise
    system_message = "You are a Salesforce development expert specializing in B2B Commerce migrations, CloudCraze to B2B Lightning Experience conversions, and Apex code optimization. Provide clear, step-by-step guidance and corrected code."
    
    start_time = time.time()
    
    try:
        response = client.chat.completions.create(
            model=model,
            messages=[
                {"role": "system", "content": system_message},
                {"role": "user", "content": prompt}
            ],
            temperature=temperature,
            max_tokens=max_tokens,
            top_p=0.9
        )
        
        completion_text = response.choices[0].message.content
        
        end_time = time.time()
        duration = end_time - start_time
        
        # Add duration to the completion text if needed
        # completion_text += f"\n\nResponse time: {duration:.2f} seconds"
        
        return completion_text
        
    except Exception as e:
        return f"Error calling API: {str(e)}"

# For Apex Trigger correction
def correct_apex_trigger(model, trigger_code):
    if not trigger_code.strip():
        return "Please provide Apex Trigger code to correct."
    
    prompt = f"""
Please analyze and correct the following Apex Trigger code for migration from CloudCraze to B2B Lightning Experience.
Identify any issues, optimize it according to best practices, and provide the corrected code.

```apex
{trigger_code}
```

Please return the corrected code with explanations of what was changed and why.
"""
    
    return call_llm(model, prompt)

# For CloudCraze Object conversion
def convert_cc_object(model, cc_object_code):
    if not cc_object_code.strip():
        return "Please provide CloudCraze Object code to convert."
    
    prompt = f"""
Please convert the following CloudCraze Object code to B2B Lightning Experience format.
Identify the appropriate B2B LEx system object that corresponds to this CloudCraze object,
map the fields correctly, and provide the complete definition for the B2B LEx version.

```
{cc_object_code}
```

Please return:
1. The equivalent B2B LEx system object name
2. Field mapping between CloudCraze and B2B LEx
3. The complete definition for the B2B LEx implementation
4. Any additional steps needed for proper migration
"""
    
    return call_llm(model, prompt)

# Gradio Interface
with gr.Blocks(title="Salesforce B2B Commerce Migration Assistant") as app:
    gr.Markdown("# Salesforce B2B Commerce Migration Assistant")
    gr.Markdown("This tool helps with migrating from CloudCraze to B2B Lightning Experience")
    
    model_dropdown = gr.Dropdown(
        choices=TOGETHER_MODELS,
        value=TOGETHER_MODELS[0],
        label="Select AI Model"
    )
    
    with gr.Tab("Apex Trigger Correction"):
        gr.Markdown("### Apex Trigger Correction")
        gr.Markdown("Paste your Apex Trigger code below to correct issues for B2B LEx compatibility")
        
        trigger_input = gr.Textbox(
            lines=15,
            placeholder="Paste your Apex Trigger code here...",
            label="Apex Trigger Code"
        )
        
        trigger_output = gr.Textbox(
            lines=20,
            label="Corrected Apex Trigger"
        )
        
        trigger_button = gr.Button("Correct Apex Trigger")
        trigger_button.click(
            fn=correct_apex_trigger,
            inputs=[model_dropdown, trigger_input],
            outputs=trigger_output
        )
    
    with gr.Tab("CloudCraze Object Conversion"):
        gr.Markdown("### CloudCraze to B2B LEx Object Conversion")
        gr.Markdown("Paste your CloudCraze Object code to convert it to B2B Lightning Experience format")
        
        object_input = gr.Textbox(
            lines=15,
            placeholder="Paste your CloudCraze Object code here...",
            label="CloudCraze Object Code"
        )
        
        object_output = gr.Textbox(
            lines=20,
            label="Converted B2B LEx Object"
        )
        
        object_button = gr.Button("Convert Object")
        object_button.click(
            fn=convert_cc_object,
            inputs=[model_dropdown, object_input],
            outputs=object_output
        )
    
    gr.Markdown("### About This Tool")
    gr.Markdown("""
    This tool uses advanced AI models to assist with Salesforce B2B Commerce migrations.
    - The Apex Trigger Correction tool helps fix trigger code for B2B Lightning Experience compatibility
    - The CloudCraze Object Conversion tool maps CC objects to their B2B LEx equivalents
    
    **Note**: Always review AI-generated output before implementing in production environments.
    """)

# Launch the app
if __name__ == "__main__":
    app.launch()