Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -210,7 +210,7 @@ async def read_items():
|
|
| 210 |
var(--paper);
|
| 211 |
font-family: 'Inter', sans-serif;
|
| 212 |
max-width: 720px;
|
| 213 |
-
min-height:
|
| 214 |
display: flex;
|
| 215 |
flex-direction: column;
|
| 216 |
color: var(--ink);
|
|
@@ -326,7 +326,7 @@ async def read_items():
|
|
| 326 |
.chat-wrapper {
|
| 327 |
display: flex;
|
| 328 |
flex-direction: column;
|
| 329 |
-
height:
|
| 330 |
flex-shrink: 0;
|
| 331 |
min-height: 0;
|
| 332 |
position: relative;
|
|
@@ -655,7 +655,7 @@ async def read_items():
|
|
| 655 |
border-radius: 16px;
|
| 656 |
width: 90%;
|
| 657 |
max-width: 640px;
|
| 658 |
-
max-height:
|
| 659 |
overflow-y: auto;
|
| 660 |
text-align: left;
|
| 661 |
box-shadow: 6px 6px 0 var(--ink);
|
|
@@ -804,6 +804,51 @@ async def read_items():
|
|
| 804 |
.input-bar { padding: 10px; }
|
| 805 |
#sendButton { width: 38px; height: 38px; }
|
| 806 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 807 |
</style>
|
| 808 |
<script>
|
| 809 |
document.addEventListener("DOMContentLoaded", () => {
|
|
@@ -812,10 +857,16 @@ async def read_items():
|
|
| 812 |
const sendButton = document.getElementById("sendButton");
|
| 813 |
const clearBtn = document.getElementById("clearBtn");
|
| 814 |
const API_ENDPOINT = "optimize";
|
| 815 |
-
const
|
|
|
|
|
|
|
| 816 |
const modal = document.getElementById("apiModal");
|
| 817 |
const btn = document.getElementById("apiKeyBtn");
|
| 818 |
const close = document.getElementById("closeModal");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
|
| 820 |
btn.onclick = () => modal.style.display = "block";
|
| 821 |
close.onclick = () => modal.style.display = "none";
|
|
@@ -841,21 +892,59 @@ async def read_items():
|
|
| 841 |
.replace(/>/g, ">");
|
| 842 |
return escaped.replace(/\\n/g, "<br>");
|
| 843 |
}
|
|
|
|
| 844 |
|
| 845 |
const BOT_ICON = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 3v2.2" stroke="white" stroke-width="1.8" stroke-linecap="round"/><circle cx="12" cy="6.4" r="1.1" fill="white"/><rect x="5" y="8" width="14" height="10" rx="3" stroke="white" stroke-width="1.8"/><circle cx="9" cy="13" r="1.3" fill="white"/><circle cx="15" cy="13" r="1.3" fill="white"/><path d="M9 16.2h6" stroke="white" stroke-width="1.6" stroke-linecap="round"/></svg>`;
|
| 846 |
-
|
| 847 |
const USER_ICON = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 20c0-3.6 3.4-6 8-6s8 2.4 8 6" stroke="#241F1A" stroke-width="1.8" stroke-linecap="round"/><circle cx="12" cy="8.5" r="4" stroke="#241F1A" stroke-width="1.8"/></svg>`;
|
| 848 |
-
|
| 849 |
const ERROR_ICON = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 3.5l8.5 15h-17L12 3.5z" stroke="white" stroke-width="1.8" stroke-linejoin="round"/><path d="M12 10v3.5" stroke="white" stroke-width="1.8" stroke-linecap="round"/><circle cx="12" cy="16" r="0.9" fill="white"/></svg>`;
|
| 850 |
|
| 851 |
-
function isErrorText(text) {
|
| 852 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 853 |
}
|
| 854 |
|
|
|
|
| 855 |
function appendMessage(role, text, options = {}) {
|
| 856 |
const { withCopy = false, isLoading = false } = options;
|
| 857 |
const msgDiv = document.createElement("div");
|
| 858 |
-
msgDiv.classList.add("message", role === "bot" ? "bot-message" : "user-message");
|
| 859 |
|
| 860 |
const isError = role === "bot" && !isLoading && isErrorText(text);
|
| 861 |
|
|
@@ -870,86 +959,81 @@ async def read_items():
|
|
| 870 |
}
|
| 871 |
|
| 872 |
if (role === "bot" && withCopy) {
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
|
| 921 |
-
|
| 922 |
-
|
| 923 |
-
|
| 924 |
-
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
card.appendChild(bodyDiv);
|
| 949 |
-
|
| 950 |
-
msgDiv.appendChild(avatar);
|
| 951 |
-
msgDiv.appendChild(card);
|
| 952 |
-
} else {
|
| 953 |
const bubble = document.createElement("div");
|
| 954 |
bubble.classList.add("bubble");
|
| 955 |
if (isLoading) {
|
|
@@ -972,90 +1056,185 @@ async def read_items():
|
|
| 972 |
if (loadingMsg) chatWindow.removeChild(loadingMsg);
|
| 973 |
}
|
| 974 |
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
|
| 978 |
-
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
|
| 992 |
-
|
| 993 |
-
|
| 994 |
-
|
| 995 |
-
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
|
| 999 |
-
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1020 |
});
|
| 1021 |
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
`🚨 Connection error: ${error.message}
|
| 1052 |
-
|
| 1053 |
-
💡 Tip: Make sure your optimization API is running and accessible.`
|
| 1054 |
-
);
|
| 1055 |
-
await saveHistory();
|
| 1056 |
-
}
|
| 1057 |
-
}
|
| 1058 |
|
|
|
|
|
|
|
|
|
|
| 1059 |
|
| 1060 |
sendButton.addEventListener("click", handleSend);
|
| 1061 |
userInput.addEventListener("input", () => {
|
|
@@ -1069,15 +1248,27 @@ async def read_items():
|
|
| 1069 |
}
|
| 1070 |
});
|
| 1071 |
|
| 1072 |
-
|
|
|
|
| 1073 |
});
|
| 1074 |
</script>
|
| 1075 |
</head>
|
| 1076 |
<body>
|
| 1077 |
<div class="chat-wrapper">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1078 |
<div class="header">
|
| 1079 |
<h1 class="logo">Prompt Optimizer<span class="tag">✎ paper edition</span></h1>
|
| 1080 |
<div class="header-actions">
|
|
|
|
| 1081 |
<div class="button" id="apiKeyBtn">Get API</div>
|
| 1082 |
<button id="clearBtn">🧹 Clear</button>
|
| 1083 |
</div>
|
|
|
|
| 210 |
var(--paper);
|
| 211 |
font-family: 'Inter', sans-serif;
|
| 212 |
max-width: 720px;
|
| 213 |
+
min-height: 100dvh;
|
| 214 |
display: flex;
|
| 215 |
flex-direction: column;
|
| 216 |
color: var(--ink);
|
|
|
|
| 326 |
.chat-wrapper {
|
| 327 |
display: flex;
|
| 328 |
flex-direction: column;
|
| 329 |
+
height: 100dvh;
|
| 330 |
flex-shrink: 0;
|
| 331 |
min-height: 0;
|
| 332 |
position: relative;
|
|
|
|
| 655 |
border-radius: 16px;
|
| 656 |
width: 90%;
|
| 657 |
max-width: 640px;
|
| 658 |
+
max-height: 84dvh;
|
| 659 |
overflow-y: auto;
|
| 660 |
text-align: left;
|
| 661 |
box-shadow: 6px 6px 0 var(--ink);
|
|
|
|
| 804 |
.input-bar { padding: 10px; }
|
| 805 |
#sendButton { width: 38px; height: 38px; }
|
| 806 |
}
|
| 807 |
+
|
| 808 |
+
/* ===== Conversations drawer ===== */
|
| 809 |
+
.history-scrim { display:none; position:fixed; inset:0; background:rgba(36,31,26,0.45); z-index:4; }
|
| 810 |
+
.history-scrim.show { display:block; }
|
| 811 |
+
.history-panel {
|
| 812 |
+
position:fixed; top:0; bottom:0; left:0; width:310px; max-width:88%;
|
| 813 |
+
background:var(--paper); border-right:3px solid var(--ink);
|
| 814 |
+
box-shadow:5px 0 0 var(--shadow); z-index:5;
|
| 815 |
+
display:flex; flex-direction:column; gap:12px; padding:16px;
|
| 816 |
+
transform:translateX(-105%); transition:transform 0.22s ease;
|
| 817 |
+
}
|
| 818 |
+
.history-panel.open { transform:translateX(0); }
|
| 819 |
+
.history-head { display:flex; align-items:center; justify-content:space-between; }
|
| 820 |
+
.history-head .sticky-tag {
|
| 821 |
+
font-family:'Caveat',cursive; font-weight:700; font-size:1.3rem; background:var(--marigold);
|
| 822 |
+
border:2px solid var(--ink); border-radius:8px; padding:3px 14px 5px 12px; transform:rotate(-1deg);
|
| 823 |
+
}
|
| 824 |
+
.history-panel .new-chat { width:100%; text-align:center; padding:9px 12px; font-size:0.85em; }
|
| 825 |
+
#searchInput {
|
| 826 |
+
border:2px dashed var(--ink-muted); border-radius:12px; padding:9px 12px; font-size:0.95em;
|
| 827 |
+
font-family:'Inter',sans-serif; background:white; color:var(--ink); outline:none; width:100%;
|
| 828 |
+
}
|
| 829 |
+
#searchInput:focus { border-color:var(--cobalt); border-style:solid; }
|
| 830 |
+
#convList { list-style:none; margin:0; padding:2px 4px 8px 0; overflow-y:auto; flex:1; display:flex; flex-direction:column; gap:10px; }
|
| 831 |
+
.conv-item {
|
| 832 |
+
display:flex; align-items:stretch; gap:6px; background:white; border:2px solid var(--ink);
|
| 833 |
+
border-radius:12px; box-shadow:3px 3px 0 var(--shadow); padding:6px 6px 6px 4px;
|
| 834 |
+
}
|
| 835 |
+
.conv-item.active { background:var(--paper-2); box-shadow:3px 3px 0 var(--ink); }
|
| 836 |
+
.conv-main {
|
| 837 |
+
flex:1; min-width:0; text-align:left; background:none; border:none; cursor:pointer;
|
| 838 |
+
font-family:'Inter',sans-serif; color:var(--ink); padding:4px 8px; display:flex; flex-direction:column; gap:2px;
|
| 839 |
+
}
|
| 840 |
+
.conv-title { font-weight:700; font-size:0.92em; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
|
| 841 |
+
.conv-snippet { font-size:0.78em; color:var(--ink-muted); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
|
| 842 |
+
.conv-date { font-family:'Caveat',cursive; font-weight:700; font-size:0.95rem; color:var(--coral); }
|
| 843 |
+
.conv-actions { display:flex; flex-direction:column; gap:4px; justify-content:center; }
|
| 844 |
+
.conv-actions .vote-btn { padding:4px 8px; font-size:0.78em; }
|
| 845 |
+
.rename-input {
|
| 846 |
+
flex:1; border:2px solid var(--cobalt); border-radius:8px; padding:6px 10px; font-size:0.92em;
|
| 847 |
+
font-family:'Inter',sans-serif; outline:none; margin:4px; min-width:0;
|
| 848 |
+
}
|
| 849 |
+
.conv-empty { text-align:center; color:var(--ink-muted); font-size:0.88em; padding:18px 0; }
|
| 850 |
+
mark { background:var(--marigold); color:var(--ink); border-radius:3px; padding:0 2px; }
|
| 851 |
+
@media (max-width: 480px) { .header .tag { display:none; } }
|
| 852 |
</style>
|
| 853 |
<script>
|
| 854 |
document.addEventListener("DOMContentLoaded", () => {
|
|
|
|
| 857 |
const sendButton = document.getElementById("sendButton");
|
| 858 |
const clearBtn = document.getElementById("clearBtn");
|
| 859 |
const API_ENDPOINT = "optimize";
|
| 860 |
+
const KEY = "prompt_optimizer_conversations";
|
| 861 |
+
const OLD_KEY = "prompt_optimizer_chat_history";
|
| 862 |
+
const GREETING = "Hi! Got a rough prompt? ✎ Paste it below — let's tighten it up.";
|
| 863 |
const modal = document.getElementById("apiModal");
|
| 864 |
const btn = document.getElementById("apiKeyBtn");
|
| 865 |
const close = document.getElementById("closeModal");
|
| 866 |
+
const panel = document.getElementById("historyPanel");
|
| 867 |
+
const scrim = document.getElementById("historyScrim");
|
| 868 |
+
const convList = document.getElementById("convList");
|
| 869 |
+
const searchInput = document.getElementById("searchInput");
|
| 870 |
|
| 871 |
btn.onclick = () => modal.style.display = "block";
|
| 872 |
close.onclick = () => modal.style.display = "none";
|
|
|
|
| 892 |
.replace(/>/g, ">");
|
| 893 |
return escaped.replace(/\\n/g, "<br>");
|
| 894 |
}
|
| 895 |
+
const esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
| 896 |
|
| 897 |
const BOT_ICON = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 3v2.2" stroke="white" stroke-width="1.8" stroke-linecap="round"/><circle cx="12" cy="6.4" r="1.1" fill="white"/><rect x="5" y="8" width="14" height="10" rx="3" stroke="white" stroke-width="1.8"/><circle cx="9" cy="13" r="1.3" fill="white"/><circle cx="15" cy="13" r="1.3" fill="white"/><path d="M9 16.2h6" stroke="white" stroke-width="1.6" stroke-linecap="round"/></svg>`;
|
|
|
|
| 898 |
const USER_ICON = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 20c0-3.6 3.4-6 8-6s8 2.4 8 6" stroke="#241F1A" stroke-width="1.8" stroke-linecap="round"/><circle cx="12" cy="8.5" r="4" stroke="#241F1A" stroke-width="1.8"/></svg>`;
|
|
|
|
| 899 |
const ERROR_ICON = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 3.5l8.5 15h-17L12 3.5z" stroke="white" stroke-width="1.8" stroke-linejoin="round"/><path d="M12 10v3.5" stroke="white" stroke-width="1.8" stroke-linecap="round"/><circle cx="12" cy="16" r="0.9" fill="white"/></svg>`;
|
| 900 |
|
| 901 |
+
function isErrorText(text) { return /^🚨/.test(text.trim()); }
|
| 902 |
+
|
| 903 |
+
/* ---------- Conversation store ---------- */
|
| 904 |
+
const pending = new Set(); // conversation ids with a request in flight
|
| 905 |
+
let editingId = null;
|
| 906 |
+
|
| 907 |
+
const newId = () => "c" + Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
|
| 908 |
+
const newConv = () => ({ id: newId(), title: "New chat", custom: false, created: Date.now(), updated: Date.now(), messages: [] });
|
| 909 |
+
const titleFrom = (msgs) => {
|
| 910 |
+
const u = msgs.find((m) => m.role === "user");
|
| 911 |
+
if (!u) return "New chat";
|
| 912 |
+
const t = u.text.replace(/\s+/g, " ").trim();
|
| 913 |
+
return t.length > 40 ? t.slice(0, 40) + "…" : t;
|
| 914 |
+
};
|
| 915 |
+
|
| 916 |
+
function loadStore() {
|
| 917 |
+
try {
|
| 918 |
+
const raw = localStorage.getItem(KEY);
|
| 919 |
+
if (raw) {
|
| 920 |
+
const s = JSON.parse(raw);
|
| 921 |
+
if (s && Array.isArray(s.convs) && s.convs.length) return s;
|
| 922 |
+
}
|
| 923 |
+
} catch (e) {}
|
| 924 |
+
const first = newConv();
|
| 925 |
+
try { // migrate the old single-chat history
|
| 926 |
+
const old = JSON.parse(localStorage.getItem(OLD_KEY) || "[]");
|
| 927 |
+
if (old.length) { first.messages = old; first.title = titleFrom(old); }
|
| 928 |
+
} catch (e) {}
|
| 929 |
+
return { activeId: first.id, convs: [first] };
|
| 930 |
+
}
|
| 931 |
+
let store = loadStore();
|
| 932 |
+
|
| 933 |
+
const active = () => store.convs.find((c) => c.id === store.activeId) || store.convs[0];
|
| 934 |
+
function save() {
|
| 935 |
+
try { localStorage.setItem(KEY, JSON.stringify(store)); }
|
| 936 |
+
catch (err) { console.warn("⚠️ Failed to save chat history:", err); }
|
| 937 |
+
}
|
| 938 |
+
function setVote(uniqueId, voted) {
|
| 939 |
+
store.convs.forEach((c) => c.messages.forEach((m) => { if (m.uniqueId === uniqueId) m.voted = voted; }));
|
| 940 |
+
save();
|
| 941 |
}
|
| 942 |
|
| 943 |
+
/* ---------- Messages ---------- */
|
| 944 |
function appendMessage(role, text, options = {}) {
|
| 945 |
const { withCopy = false, isLoading = false } = options;
|
| 946 |
const msgDiv = document.createElement("div");
|
| 947 |
+
msgDiv.classList.add(options.invite ? "invite" : "message", role === "bot" ? "bot-message" : "user-message");
|
| 948 |
|
| 949 |
const isError = role === "bot" && !isLoading && isErrorText(text);
|
| 950 |
|
|
|
|
| 959 |
}
|
| 960 |
|
| 961 |
if (role === "bot" && withCopy) {
|
| 962 |
+
const card = document.createElement("div");
|
| 963 |
+
card.classList.add("output-card");
|
| 964 |
+
if (options.uniqueId) card.dataset.uniqueId = options.uniqueId;
|
| 965 |
+
|
| 966 |
+
const tag = document.createElement("div");
|
| 967 |
+
tag.classList.add("sticky-tag");
|
| 968 |
+
tag.textContent = "Refined Prompt ✨";
|
| 969 |
+
|
| 970 |
+
const bodyDiv = document.createElement("div");
|
| 971 |
+
bodyDiv.classList.add("card-body");
|
| 972 |
+
bodyDiv.innerHTML = formatTextWithLineBreaks(text);
|
| 973 |
+
card.dataset.text = text;
|
| 974 |
+
|
| 975 |
+
const actionRow = document.createElement("div");
|
| 976 |
+
actionRow.classList.add("action-row");
|
| 977 |
+
|
| 978 |
+
const copyBtn = document.createElement("button");
|
| 979 |
+
copyBtn.classList.add("copy-btn");
|
| 980 |
+
copyBtn.textContent = "Copy";
|
| 981 |
+
copyBtn.addEventListener("click", () => {
|
| 982 |
+
navigator.clipboard.writeText(text);
|
| 983 |
+
copyBtn.textContent = "Copied!";
|
| 984 |
+
setTimeout(() => (copyBtn.textContent = "Copy"), 1500);
|
| 985 |
+
});
|
| 986 |
+
|
| 987 |
+
const upBtn = document.createElement("button");
|
| 988 |
+
upBtn.classList.add("vote-btn");
|
| 989 |
+
upBtn.textContent = "👍";
|
| 990 |
+
upBtn.setAttribute("aria-label", "Upvote this result");
|
| 991 |
+
|
| 992 |
+
const downBtn = document.createElement("button");
|
| 993 |
+
downBtn.classList.add("vote-btn");
|
| 994 |
+
downBtn.textContent = "👎";
|
| 995 |
+
downBtn.setAttribute("aria-label", "Downvote this result");
|
| 996 |
+
|
| 997 |
+
if (options.voted === "up") upBtn.classList.add("selected-up");
|
| 998 |
+
if (options.voted === "down") downBtn.classList.add("selected-down");
|
| 999 |
+
if (options.voted) { upBtn.disabled = true; downBtn.disabled = true; }
|
| 1000 |
+
|
| 1001 |
+
async function castVote(isUpvote, chosenBtn) {
|
| 1002 |
+
const uid = card.dataset.uniqueId;
|
| 1003 |
+
if (!uid) return;
|
| 1004 |
+
upBtn.disabled = true;
|
| 1005 |
+
downBtn.disabled = true;
|
| 1006 |
+
chosenBtn.classList.add(isUpvote ? "selected-up" : "selected-down");
|
| 1007 |
+
try {
|
| 1008 |
+
await fetch("vote", {
|
| 1009 |
+
method: "POST",
|
| 1010 |
+
headers: { "Content-Type": "application/json" },
|
| 1011 |
+
body: JSON.stringify({ unique_id: uid, is_upvote: isUpvote }),
|
| 1012 |
+
});
|
| 1013 |
+
setVote(uid, isUpvote ? "up" : "down");
|
| 1014 |
+
} catch (err) {
|
| 1015 |
+
console.warn("⚠️ Failed to record vote:", err);
|
| 1016 |
+
upBtn.disabled = false;
|
| 1017 |
+
downBtn.disabled = false;
|
| 1018 |
+
chosenBtn.classList.remove(isUpvote ? "selected-up" : "selected-down");
|
| 1019 |
+
}
|
| 1020 |
+
}
|
| 1021 |
+
|
| 1022 |
+
upBtn.addEventListener("click", () => castVote(true, upBtn));
|
| 1023 |
+
downBtn.addEventListener("click", () => castVote(false, downBtn));
|
| 1024 |
+
|
| 1025 |
+
actionRow.appendChild(copyBtn);
|
| 1026 |
+
actionRow.appendChild(upBtn);
|
| 1027 |
+
actionRow.appendChild(downBtn);
|
| 1028 |
+
|
| 1029 |
+
card.appendChild(tag);
|
| 1030 |
+
bodyDiv.appendChild(document.createElement("br"));
|
| 1031 |
+
bodyDiv.appendChild(actionRow);
|
| 1032 |
+
card.appendChild(bodyDiv);
|
| 1033 |
+
|
| 1034 |
+
msgDiv.appendChild(avatar);
|
| 1035 |
+
msgDiv.appendChild(card);
|
| 1036 |
+
} else {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1037 |
const bubble = document.createElement("div");
|
| 1038 |
bubble.classList.add("bubble");
|
| 1039 |
if (isLoading) {
|
|
|
|
| 1056 |
if (loadingMsg) chatWindow.removeChild(loadingMsg);
|
| 1057 |
}
|
| 1058 |
|
| 1059 |
+
const showMsg = (m) => appendMessage(m.role, m.text, { withCopy: m.withCopy, uniqueId: m.uniqueId, voted: m.voted });
|
| 1060 |
+
|
| 1061 |
+
function renderChat() {
|
| 1062 |
+
chatWindow.innerHTML = "";
|
| 1063 |
+
appendMessage("bot", GREETING, { invite: true });
|
| 1064 |
+
const conv = active();
|
| 1065 |
+
conv.messages.forEach(showMsg);
|
| 1066 |
+
if (pending.has(conv.id)) appendMessage("bot", "", { isLoading: true });
|
| 1067 |
+
}
|
| 1068 |
+
|
| 1069 |
+
function pushMsg(conv, msg) {
|
| 1070 |
+
conv.messages.push(msg);
|
| 1071 |
+
conv.updated = Date.now();
|
| 1072 |
+
if (!conv.custom) conv.title = titleFrom(conv.messages);
|
| 1073 |
+
if (conv.id === store.activeId) { removeLoadingMessage(); showMsg(msg); if (pending.has(conv.id) && msg.role === "user") appendMessage("bot", "", { isLoading: true }); }
|
| 1074 |
+
save();
|
| 1075 |
+
renderList();
|
| 1076 |
+
}
|
| 1077 |
+
|
| 1078 |
+
/* ---------- History drawer ---------- */
|
| 1079 |
+
const openPanel = () => { renderList(); panel.classList.add("open"); scrim.classList.add("show"); };
|
| 1080 |
+
const closePanel = () => { panel.classList.remove("open"); scrim.classList.remove("show"); editingId = null; };
|
| 1081 |
+
document.getElementById("historyBtn").addEventListener("click", openPanel);
|
| 1082 |
+
document.getElementById("closeHistory").addEventListener("click", closePanel);
|
| 1083 |
+
scrim.addEventListener("click", closePanel);
|
| 1084 |
+
document.addEventListener("keydown", (e) => { if (e.key === "Escape" && panel.classList.contains("open") && !editingId) closePanel(); });
|
| 1085 |
+
|
| 1086 |
+
function switchTo(id) {
|
| 1087 |
+
store.activeId = id;
|
| 1088 |
+
save();
|
| 1089 |
+
renderChat();
|
| 1090 |
+
renderList();
|
| 1091 |
+
closePanel();
|
| 1092 |
+
}
|
| 1093 |
+
|
| 1094 |
+
document.getElementById("newChatBtn").addEventListener("click", () => {
|
| 1095 |
+
const cur = active();
|
| 1096 |
+
if (cur.messages.length === 0 && !pending.has(cur.id)) { closePanel(); userInput.focus(); return; }
|
| 1097 |
+
const c = newConv();
|
| 1098 |
+
store.convs.push(c);
|
| 1099 |
+
searchInput.value = "";
|
| 1100 |
+
switchTo(c.id);
|
| 1101 |
+
userInput.focus();
|
| 1102 |
+
});
|
| 1103 |
+
|
| 1104 |
+
searchInput.addEventListener("input", renderList);
|
| 1105 |
+
|
| 1106 |
+
function fmtDate(ts) {
|
| 1107 |
+
const d = new Date(ts), now = new Date();
|
| 1108 |
+
return d.toDateString() === now.toDateString()
|
| 1109 |
+
? d.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" })
|
| 1110 |
+
: d.toLocaleDateString([], { month: "short", day: "numeric" });
|
| 1111 |
+
}
|
| 1112 |
+
function hl(text, q) {
|
| 1113 |
+
const e = esc(text);
|
| 1114 |
+
if (!q) return e;
|
| 1115 |
+
const re = new RegExp(esc(q).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi");
|
| 1116 |
+
return e.replace(re, (m) => `<mark>${m}</mark>`);
|
| 1117 |
+
}
|
| 1118 |
+
|
| 1119 |
+
function renderList() {
|
| 1120 |
+
const q = searchInput.value.trim();
|
| 1121 |
+
const ql = q.toLowerCase();
|
| 1122 |
+
convList.innerHTML = "";
|
| 1123 |
+
const items = [...store.convs].sort((a, b) => b.updated - a.updated).map((c) => {
|
| 1124 |
+
let match = !ql || c.title.toLowerCase().includes(ql);
|
| 1125 |
+
let snippet = "";
|
| 1126 |
+
if (ql) {
|
| 1127 |
+
const m = c.messages.find((m) => m.text.toLowerCase().includes(ql));
|
| 1128 |
+
if (m) {
|
| 1129 |
+
match = true;
|
| 1130 |
+
const i = m.text.toLowerCase().indexOf(ql);
|
| 1131 |
+
snippet = (i > 20 ? "…" : "") + m.text.slice(Math.max(0, i - 20), i + 60).replace(/\s+/g, " ");
|
| 1132 |
+
}
|
| 1133 |
+
} else {
|
| 1134 |
+
const last = c.messages[c.messages.length - 1];
|
| 1135 |
+
snippet = last ? last.text.replace(/\s+/g, " ").slice(0, 60) : "Empty conversation";
|
| 1136 |
+
}
|
| 1137 |
+
return { c, match, snippet };
|
| 1138 |
+
}).filter((x) => x.match);
|
| 1139 |
+
|
| 1140 |
+
if (!items.length) {
|
| 1141 |
+
const li = document.createElement("li");
|
| 1142 |
+
li.className = "conv-empty";
|
| 1143 |
+
li.textContent = "No chats found ✎";
|
| 1144 |
+
convList.appendChild(li);
|
| 1145 |
+
return;
|
| 1146 |
+
}
|
| 1147 |
+
|
| 1148 |
+
items.forEach(({ c, snippet }) => {
|
| 1149 |
+
const li = document.createElement("li");
|
| 1150 |
+
li.className = "conv-item" + (c.id === store.activeId ? " active" : "");
|
| 1151 |
+
|
| 1152 |
+
if (editingId === c.id) {
|
| 1153 |
+
const input = document.createElement("input");
|
| 1154 |
+
input.className = "rename-input";
|
| 1155 |
+
input.value = c.title;
|
| 1156 |
+
input.maxLength = 60;
|
| 1157 |
+
input.setAttribute("aria-label", "Rename chat");
|
| 1158 |
+
let done = false;
|
| 1159 |
+
const finish = (commit) => {
|
| 1160 |
+
if (done) return;
|
| 1161 |
+
done = true;
|
| 1162 |
+
const v = input.value.trim();
|
| 1163 |
+
if (commit && v) { c.title = v; c.custom = true; save(); }
|
| 1164 |
+
editingId = null;
|
| 1165 |
+
renderList();
|
| 1166 |
+
};
|
| 1167 |
+
input.addEventListener("keydown", (e) => {
|
| 1168 |
+
if (e.key === "Enter") finish(true);
|
| 1169 |
+
if (e.key === "Escape") finish(false);
|
| 1170 |
+
});
|
| 1171 |
+
input.addEventListener("blur", () => finish(true));
|
| 1172 |
+
li.appendChild(input);
|
| 1173 |
+
convList.appendChild(li);
|
| 1174 |
+
setTimeout(() => { input.focus(); input.select(); }, 0);
|
| 1175 |
+
return;
|
| 1176 |
+
}
|
| 1177 |
+
|
| 1178 |
+
li.innerHTML = `<button class="conv-main"><span class="conv-title">${hl(c.title, q)}</span><span class="conv-snippet">${hl(snippet, q)}</span><span class="conv-date">${fmtDate(c.updated)}</span></button>
|
| 1179 |
+
<div class="conv-actions"><button class="vote-btn" data-act="rename" aria-label="Rename chat">✎</button><button class="vote-btn" data-act="del" aria-label="Delete chat">✕</button></div>`;
|
| 1180 |
+
li.querySelector(".conv-main").addEventListener("click", () => switchTo(c.id));
|
| 1181 |
+
li.querySelector('[data-act="rename"]').addEventListener("click", () => { editingId = c.id; renderList(); });
|
| 1182 |
+
li.querySelector('[data-act="del"]').addEventListener("click", () => {
|
| 1183 |
+
if (!confirm(`Delete "${c.title}"?`)) return;
|
| 1184 |
+
store.convs = store.convs.filter((x) => x.id !== c.id);
|
| 1185 |
+
if (!store.convs.length) store.convs.push(newConv());
|
| 1186 |
+
if (store.activeId === c.id) { store.activeId = [...store.convs].sort((a, b) => b.updated - a.updated)[0].id; renderChat(); }
|
| 1187 |
+
save();
|
| 1188 |
+
renderList();
|
| 1189 |
+
});
|
| 1190 |
+
convList.appendChild(li);
|
| 1191 |
+
});
|
| 1192 |
+
}
|
| 1193 |
+
|
| 1194 |
+
/* ---------- Clear + send ---------- */
|
| 1195 |
+
clearBtn.addEventListener("click", () => {
|
| 1196 |
+
const conv = active();
|
| 1197 |
+
conv.messages = [];
|
| 1198 |
+
conv.updated = Date.now();
|
| 1199 |
+
if (!conv.custom) conv.title = "New chat";
|
| 1200 |
+
save();
|
| 1201 |
+
renderChat();
|
| 1202 |
+
renderList();
|
| 1203 |
});
|
| 1204 |
|
| 1205 |
+
async function handleSend() {
|
| 1206 |
+
const prompt = userInput.value.trim();
|
| 1207 |
+
if (!prompt) return;
|
| 1208 |
+
const conv = active();
|
| 1209 |
+
if (pending.has(conv.id)) return;
|
| 1210 |
+
userInput.value = "";
|
| 1211 |
+
userInput.style.height = "auto";
|
| 1212 |
+
pending.add(conv.id);
|
| 1213 |
+
pushMsg(conv, { role: "user", text: prompt, withCopy: false, uniqueId: null, voted: null });
|
| 1214 |
+
try {
|
| 1215 |
+
const response = await fetch(API_ENDPOINT, {
|
| 1216 |
+
method: "POST",
|
| 1217 |
+
headers: { "Content-Type": "application/json" },
|
| 1218 |
+
body: JSON.stringify({ input_prompt: prompt }),
|
| 1219 |
+
});
|
| 1220 |
+
if (!response.ok) throw new Error(`API Error: ${response.status} ${response.statusText}`);
|
| 1221 |
+
const data = await response.json();
|
| 1222 |
+
const optimized = data.optimized_text || data.result || data.output || null;
|
| 1223 |
+
pending.delete(conv.id);
|
| 1224 |
+
if (optimized) {
|
| 1225 |
+
pushMsg(conv, { role: "bot", text: optimized, withCopy: true, uniqueId: data.id || null, voted: null });
|
| 1226 |
+
} else {
|
| 1227 |
+
pushMsg(conv, { role: "bot", text: "⚠️ The API responded successfully but didn't return optimized text.", withCopy: false, uniqueId: null, voted: null });
|
| 1228 |
+
}
|
| 1229 |
+
} catch (error) {
|
| 1230 |
+
console.error("Optimization failed:", error);
|
| 1231 |
+
pending.delete(conv.id);
|
| 1232 |
+
pushMsg(conv, { role: "bot", withCopy: false, uniqueId: null, voted: null, text:
|
| 1233 |
+
`🚨 Connection error: ${error.message}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1234 |
|
| 1235 |
+
💡 Tip: Make sure your optimization API is running and accessible.` });
|
| 1236 |
+
}
|
| 1237 |
+
}
|
| 1238 |
|
| 1239 |
sendButton.addEventListener("click", handleSend);
|
| 1240 |
userInput.addEventListener("input", () => {
|
|
|
|
| 1248 |
}
|
| 1249 |
});
|
| 1250 |
|
| 1251 |
+
renderChat();
|
| 1252 |
+
renderList();
|
| 1253 |
});
|
| 1254 |
</script>
|
| 1255 |
</head>
|
| 1256 |
<body>
|
| 1257 |
<div class="chat-wrapper">
|
| 1258 |
+
<div class="history-scrim" id="historyScrim"></div>
|
| 1259 |
+
<aside class="history-panel" id="historyPanel" aria-label="Conversations">
|
| 1260 |
+
<div class="history-head">
|
| 1261 |
+
<span class="sticky-tag">Chats ✎</span>
|
| 1262 |
+
<button class="close" id="closeHistory" aria-label="Close">×</button>
|
| 1263 |
+
</div>
|
| 1264 |
+
<button class="button new-chat" id="newChatBtn">+ New chat</button>
|
| 1265 |
+
<input type="search" id="searchInput" placeholder="Search chats..." aria-label="Search conversations" />
|
| 1266 |
+
<ul id="convList"></ul>
|
| 1267 |
+
</aside>
|
| 1268 |
<div class="header">
|
| 1269 |
<h1 class="logo">Prompt Optimizer<span class="tag">✎ paper edition</span></h1>
|
| 1270 |
<div class="header-actions">
|
| 1271 |
+
<div class="button" id="historyBtn">☰ Chats</div>
|
| 1272 |
<div class="button" id="apiKeyBtn">Get API</div>
|
| 1273 |
<button id="clearBtn">🧹 Clear</button>
|
| 1274 |
</div>
|