ai: persist conversation id
This commit is contained in:
parent
3a27cc9fc1
commit
299e638c6a
@ -20,6 +20,7 @@ def register(app) -> None:
|
|||||||
user_message = (payload.get("message") or "").strip()
|
user_message = (payload.get("message") or "").strip()
|
||||||
history = payload.get("history") or []
|
history = payload.get("history") or []
|
||||||
profile = (payload.get("profile") or payload.get("mode") or "atlas-quick").strip().lower()
|
profile = (payload.get("profile") or payload.get("mode") or "atlas-quick").strip().lower()
|
||||||
|
conversation_id = payload.get("conversation_id") if isinstance(payload.get("conversation_id"), str) else ""
|
||||||
|
|
||||||
if not user_message:
|
if not user_message:
|
||||||
return jsonify({"error": "message required"}), 400
|
return jsonify({"error": "message required"}), 400
|
||||||
@ -30,7 +31,7 @@ def register(app) -> None:
|
|||||||
source = "stock"
|
source = "stock"
|
||||||
else:
|
else:
|
||||||
mode = "smart" if profile in {"atlas-smart", "smart"} else "quick"
|
mode = "smart" if profile in {"atlas-smart", "smart"} else "quick"
|
||||||
reply = _atlasbot_answer(user_message, mode)
|
reply = _atlasbot_answer(user_message, mode, conversation_id)
|
||||||
source = f"atlas-{mode}"
|
source = f"atlas-{mode}"
|
||||||
if reply:
|
if reply:
|
||||||
elapsed_ms = int((time.time() - started) * 1000)
|
elapsed_ms = int((time.time() - started) * 1000)
|
||||||
@ -54,7 +55,7 @@ def register(app) -> None:
|
|||||||
_start_keep_warm()
|
_start_keep_warm()
|
||||||
|
|
||||||
|
|
||||||
def _atlasbot_answer(message: str, mode: str) -> str:
|
def _atlasbot_answer(message: str, mode: str, conversation_id: str) -> str:
|
||||||
endpoint = settings.AI_ATLASBOT_ENDPOINT
|
endpoint = settings.AI_ATLASBOT_ENDPOINT
|
||||||
if not endpoint:
|
if not endpoint:
|
||||||
return ""
|
return ""
|
||||||
@ -62,8 +63,11 @@ def _atlasbot_answer(message: str, mode: str) -> str:
|
|||||||
if settings.AI_ATLASBOT_TOKEN:
|
if settings.AI_ATLASBOT_TOKEN:
|
||||||
headers["X-Internal-Token"] = settings.AI_ATLASBOT_TOKEN
|
headers["X-Internal-Token"] = settings.AI_ATLASBOT_TOKEN
|
||||||
try:
|
try:
|
||||||
|
payload = {"prompt": message, "mode": mode}
|
||||||
|
if conversation_id:
|
||||||
|
payload["conversation_id"] = conversation_id
|
||||||
with httpx.Client(timeout=settings.AI_ATLASBOT_TIMEOUT_SEC) as client:
|
with httpx.Client(timeout=settings.AI_ATLASBOT_TIMEOUT_SEC) as client:
|
||||||
resp = client.post(endpoint, json={"prompt": message, "mode": mode}, headers=headers)
|
resp = client.post(endpoint, json=payload, headers=headers)
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
return ""
|
return ""
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
|
|||||||
@ -117,6 +117,21 @@ const draft = ref("");
|
|||||||
const sending = ref(false);
|
const sending = ref(false);
|
||||||
const chatWindow = ref(null);
|
const chatWindow = ref(null);
|
||||||
const copied = ref(false);
|
const copied = ref(false);
|
||||||
|
const conversationIds = reactive({});
|
||||||
|
|
||||||
|
function ensureConversationId(profile) {
|
||||||
|
if (conversationIds[profile]) return conversationIds[profile];
|
||||||
|
const key = `atlas-ai-conversation:${profile}`;
|
||||||
|
let value = localStorage.getItem(key);
|
||||||
|
if (!value) {
|
||||||
|
const suffix =
|
||||||
|
typeof crypto !== "undefined" && crypto.randomUUID ? crypto.randomUUID() : `${Math.random()}`.slice(2);
|
||||||
|
value = `${profile}-${Date.now()}-${suffix}`;
|
||||||
|
localStorage.setItem(key, value);
|
||||||
|
}
|
||||||
|
conversationIds[profile] = value;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => fetchMeta(activeProfile.value));
|
onMounted(() => fetchMeta(activeProfile.value));
|
||||||
watch(activeProfile, (profile) => fetchMeta(profile));
|
watch(activeProfile, (profile) => fetchMeta(profile));
|
||||||
@ -157,11 +172,12 @@ async function sendMessage() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const history = state.messages.filter((m) => !m.streaming).map((m) => ({ role: m.role, content: m.content }));
|
const history = state.messages.filter((m) => !m.streaming).map((m) => ({ role: m.role, content: m.content }));
|
||||||
|
const conversation_id = ensureConversationId(activeProfile.value);
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
const resp = await fetch(API_URL, {
|
const resp = await fetch(API_URL, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ message: text, history, profile: activeProfile.value }),
|
body: JSON.stringify({ message: text, history, profile: activeProfile.value, conversation_id }),
|
||||||
});
|
});
|
||||||
const contentType = resp.headers.get("content-type") || "";
|
const contentType = resp.headers.get("content-type") || "";
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user