Commit 069b5f3c authored by Kantz's avatar Kantz
Browse files

Merge branch 'old_retrival' into 'main'

Old retrival

See merge request kantz/tutor_react!2
parents b903391a 8c452e91
VITE_BACKEND_URL=""
\ No newline at end of file
......@@ -6,6 +6,8 @@ import DocPanel from "../components/Retrieval/DocPanel";
import type { ChatMessage } from "../components/Chat/MessageList";
import type { RetrievedDoc } from "../components/Retrieval/DocPanel";
const backendUrl = import.meta.env.VITE_BACKEND_URL || "http://localhost:8000";
const initialMessages: ChatMessage[] = [];
type ArchivedChatSummary = {
......@@ -21,11 +23,59 @@ type ArchivedChatDetail = {
history: ChatMessage[];
};
type ContextSource = {
source_id: {
chapter_title?: string | null;
section_title?: string | null;
subsection_title?: string | null;
title: string;
doc_type: string;
};
retrieved_as?: string | null;
source_type?: string | null;
score?: number;
markdown?: string | null;
};
const createSessionId = () =>
`session_${Date.now().toString(36)}_${Math.random()
.toString(36)
.slice(2, 8)}`;
const sourceIdToUid = (source: ContextSource, index: number) => {
const parts = [
source.source_id.chapter_title,
source.source_id.section_title,
source.source_id.subsection_title,
source.source_id.title,
source.source_id.doc_type,
].filter(Boolean);
return parts.length ? parts.join("|") : `source_${index}`;
};
const sourceIdToPath = (source: ContextSource) => {
const parts = [
source.source_id.chapter_title,
source.source_id.section_title,
source.source_id.subsection_title,
].filter(Boolean);
return parts.length ? parts.join(" / ") : null;
};
const toRetrievedDoc = (source: ContextSource, index: number): RetrievedDoc => ({
uid: sourceIdToUid(source, index),
doc_type: source.source_id.doc_type,
score: source.score,
metadata: {
section_title: source.source_id.section_title ?? null,
subsection_title: source.source_id.subsection_title ?? null,
title: source.source_id.title ?? null,
type: source.source_type ?? null,
path: sourceIdToPath(source),
},
markdown: source.markdown || "",
});
export default function App() {
const [messages, setMessages] = useState<ChatMessage[]>(initialMessages);
const [draft, setDraft] = useState("");
......@@ -62,30 +112,11 @@ export default function App() {
setMessages((prev) => [...prev, userMessage]);
setDraft("");
try {
setRetrievalLoading(true);
setRetrievalError(null);
const retrievalRequest = fetch("http://localhost:8000/api/retrieval/query", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: trimmed,
k: 4,
expand_links: true,
}),
}).then(async (res) => {
if (!res.ok) {
throw new Error(`Retrieval failed: ${res.status}`);
}
const payload = await res.json();
setDirectChildren(payload.children_direct || []);
setIndirectChildren(payload.children_expanded || []);
setSubsections(payload.subsections || []);
setSections(payload.sections || []);
});
setRetrievalLoading(true);
setRetrievalError(null);
const response = await fetch("http://localhost:8000/api/chat", {
try {
const response = await fetch(`${backendUrl}/api/chat`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
......@@ -112,8 +143,6 @@ export default function App() {
},
]);
}
await retrievalRequest;
} catch (error) {
setMessages((prev) => [
...prev,
......@@ -123,6 +152,63 @@ export default function App() {
text: "Chat request failed. Check the API logs.",
},
]);
setRetrievalError("Retrieval failed. Check the API logs.");
setRetrievalLoading(false);
void error;
return;
}
try {
const contextResponse = await fetch(`${backendUrl}/api/context/retrieval`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
messages: [...messages, userMessage].map((message) => ({
role: message.role,
text: message.text,
})),
draft: chatSessionId,
}),
});
if (!contextResponse.ok) {
throw new Error(`Retrieval failed: ${contextResponse.status}`);
}
const sources: ContextSource[] = await contextResponse.json();
const nextDirect: RetrievedDoc[] = [];
const nextIndirect: RetrievedDoc[] = [];
const nextSubsections: RetrievedDoc[] = [];
const nextSections: RetrievedDoc[] = [];
sources.forEach((source, index) => {
const doc = toRetrievedDoc(source, index);
switch (source.retrieved_as) {
case "children_direct":
nextDirect.push(doc);
break;
case "children_expanded":
nextIndirect.push(doc);
break;
case "subsections":
nextSubsections.push(doc);
break;
case "sections":
nextSections.push(doc);
break;
case "neighbors":
nextIndirect.push(doc);
break;
default:
nextIndirect.push(doc);
}
});
setDirectChildren(nextDirect);
setIndirectChildren(nextIndirect);
setSubsections(nextSubsections);
setSections(nextSections);
} catch (error) {
setRetrievalError("Retrieval failed. Check the API logs.");
void error;
} finally {
......@@ -151,7 +237,7 @@ export default function App() {
setArchiveError(null);
try {
const response = await fetch(
"http://localhost:8000/api/chat/archives?limit=50"
`${backendUrl}/api/chat/archives?limit=50`
);
if (!response.ok) {
throw new Error(`Archive list failed: ${response.status}`);
......@@ -175,7 +261,7 @@ export default function App() {
setArchiveError(null);
try {
const response = await fetch(
`http://localhost:8000/api/chat/archive/${targetId}`
`${backendUrl}/api/chat/archive/${targetId}`
);
if (!response.ok) {
throw new Error(`Archive load failed: ${response.status}`);
......@@ -194,7 +280,7 @@ export default function App() {
if (messages.length) {
setIsArchiving(true);
try {
await fetch("http://localhost:8000/api/chat/archive", {
await fetch(`${backendUrl}/api/chat/archive`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
......@@ -229,7 +315,7 @@ export default function App() {
});
try {
const response = await fetch("http://localhost:8000/api/canvas/save", {
const response = await fetch(`${backendUrl}/api/canvas/save`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ data_url: dataUrl, filename_hint: "canvas" }),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment