Update app.py
Browse files
app.py
CHANGED
|
@@ -21,25 +21,38 @@ from langgraph.prebuilt import create_react_agent
|
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
.stCodeBlock {
|
| 24 |
-
background-color: #
|
| 25 |
-
border: 1px solid #
|
| 26 |
-
border-radius:
|
| 27 |
-
padding:
|
| 28 |
-
|
| 29 |
}
|
| 30 |
.stCodeBlock pre {
|
| 31 |
margin: 0;
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
.copyButton {
|
| 34 |
position: absolute;
|
| 35 |
top: 5px;
|
| 36 |
right: 5px;
|
| 37 |
padding: 5px 10px;
|
| 38 |
-
background-color: #
|
| 39 |
color: white;
|
| 40 |
border: none;
|
| 41 |
border-radius: 3px;
|
| 42 |
cursor: pointer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
</style>
|
| 45 |
""", unsafe_allow_html=True)
|
|
@@ -378,22 +391,35 @@ else:
|
|
| 378 |
)
|
| 379 |
|
| 380 |
def format_ai_response(response):
|
| 381 |
-
# Function to replace code blocks with formatted versions
|
| 382 |
def replace_code_block(match):
|
| 383 |
code = match.group(1).strip()
|
| 384 |
-
# Remove language specifier if present
|
| 385 |
code = re.sub(r'^(python|typescript|javascript)\n', '', code, flags=re.IGNORECASE)
|
| 386 |
-
return f"
|
| 387 |
|
| 388 |
-
# Replace code blocks
|
| 389 |
formatted_response = re.sub(r'```(.*?)```', replace_code_block, response, flags=re.DOTALL)
|
|
|
|
| 390 |
|
| 391 |
-
#
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
|
| 396 |
-
return formatted_response
|
| 397 |
async def run_github_editor(query: str, thread_id: str = "default"):
|
| 398 |
inputs = {"messages": [HumanMessage(content=query)]}
|
| 399 |
config = {
|
|
|
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
.stCodeBlock {
|
| 24 |
+
background-color: #f6f8fa;
|
| 25 |
+
border: 1px solid #e1e4e8;
|
| 26 |
+
border-radius: 6px;
|
| 27 |
+
padding: 16px;
|
| 28 |
+
position: relative;
|
| 29 |
}
|
| 30 |
.stCodeBlock pre {
|
| 31 |
margin: 0;
|
| 32 |
+
padding: 0;
|
| 33 |
+
white-space: pre-wrap;
|
| 34 |
+
word-break: break-word;
|
| 35 |
}
|
| 36 |
.copyButton {
|
| 37 |
position: absolute;
|
| 38 |
top: 5px;
|
| 39 |
right: 5px;
|
| 40 |
padding: 5px 10px;
|
| 41 |
+
background-color: #0366d6;
|
| 42 |
color: white;
|
| 43 |
border: none;
|
| 44 |
border-radius: 3px;
|
| 45 |
cursor: pointer;
|
| 46 |
+
font-size: 12px;
|
| 47 |
+
}
|
| 48 |
+
.copyButton:hover {
|
| 49 |
+
background-color: #0056b3;
|
| 50 |
+
}
|
| 51 |
+
code {
|
| 52 |
+
padding: 2px 4px;
|
| 53 |
+
background-color: #f6f8fa;
|
| 54 |
+
border-radius: 3px;
|
| 55 |
+
font-family: monospace;
|
| 56 |
}
|
| 57 |
</style>
|
| 58 |
""", unsafe_allow_html=True)
|
|
|
|
| 391 |
)
|
| 392 |
|
| 393 |
def format_ai_response(response):
|
|
|
|
| 394 |
def replace_code_block(match):
|
| 395 |
code = match.group(1).strip()
|
|
|
|
| 396 |
code = re.sub(r'^(python|typescript|javascript)\n', '', code, flags=re.IGNORECASE)
|
| 397 |
+
return f'<div class="stCodeBlock"><button class="copyButton" onclick="copyCode(this)">Copy</button><pre><code>{code}</code></pre></div>'
|
| 398 |
|
|
|
|
| 399 |
formatted_response = re.sub(r'```(.*?)```', replace_code_block, response, flags=re.DOTALL)
|
| 400 |
+
formatted_response = re.sub(r'`([^`\n]+)`', r'<code>\1</code>', formatted_response)
|
| 401 |
|
| 402 |
+
# Add JavaScript for copy functionality
|
| 403 |
+
js = """
|
| 404 |
+
<script>
|
| 405 |
+
function copyCode(button) {
|
| 406 |
+
const pre = button.nextElementSibling;
|
| 407 |
+
const code = pre.querySelector('code');
|
| 408 |
+
const range = document.createRange();
|
| 409 |
+
range.selectNode(code);
|
| 410 |
+
window.getSelection().removeAllRanges();
|
| 411 |
+
window.getSelection().addRange(range);
|
| 412 |
+
document.execCommand('copy');
|
| 413 |
+
window.getSelection().removeAllRanges();
|
| 414 |
+
button.textContent = 'Copied!';
|
| 415 |
+
setTimeout(() => {
|
| 416 |
+
button.textContent = 'Copy';
|
| 417 |
+
}, 2000);
|
| 418 |
+
}
|
| 419 |
+
</script>
|
| 420 |
+
"""
|
| 421 |
+
return js + formatted_response
|
| 422 |
|
|
|
|
| 423 |
async def run_github_editor(query: str, thread_id: str = "default"):
|
| 424 |
inputs = {"messages": [HumanMessage(content=query)]}
|
| 425 |
config = {
|