Skip to content

Commit

Permalink
feat: finish chat
Browse files Browse the repository at this point in the history
  • Loading branch information
orhanrauf committed Jan 31, 2025
1 parent 5a3c81a commit 5542101
Show file tree
Hide file tree
Showing 17 changed files with 817 additions and 333 deletions.
22 changes: 1 addition & 21 deletions backend/app/api/v1/endpoints/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,12 @@ async def stream_chat_response(
"""Stream an AI response for a chat message."""

async def event_generator():
buffer = ""
try:
async for chunk in chat_service.generate_streaming_response(
db=db, chat_id=chat_id, user=user
):
if chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content

# Add to buffer and check for complete words/markdown
buffer += content

# Only send if we have a complete word or markdown element
if buffer.endswith((" ", "\n", "|", ".", ",", ":", "-")):
yield f"data: {buffer}\n\n"
buffer = ""

# Send any remaining content in buffer
if buffer:
yield f"data: {buffer}\n\n"
yield f"data: {chunk.choices[0].delta.content}\n\n"
yield "data: [DONE]\n\n"
except Exception as e:
logger.error(f"Error in stream: {str(e)}")
Expand All @@ -134,11 +121,4 @@ async def event_generator():
return StreamingResponse(
event_generator(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
"Content-Type": "text/event-stream",
"Access-Control-Allow-Origin": "*",
},
)
18 changes: 14 additions & 4 deletions backend/app/platform/sync/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async def run(
except Exception as e:
logger.error(f"An error occured while handling deletions: {e}")
async with get_db_context() as db:
sync_job_db = await crud.sync_job.get(db, sync_job.id)
sync_job_db = await crud.sync_job.get(db, sync_job.id, current_user)
sync_job_schema = schemas.SyncJobUpdate.model_validate(sync_job_db)
sync_job_schema.status = SyncJobStatus.FAILED
sync_job_schema.failed_at = datetime.now()
Expand All @@ -155,11 +155,21 @@ async def run(
return sync

async with get_db_context() as db:
sync_job_db = await crud.sync_job.get(db, sync_job.id)
sync_job_schema = schemas.SyncJobUpdate.model_validate(sync_job_db)
sync_job_db = await crud.sync_job.get(db, sync_job.id, current_user)
sync_job_schema = schemas.SyncJobUpdate.model_validate(
sync_job_db, from_attributes=True
)
sync_job_schema.status = SyncJobStatus.COMPLETED
sync_job_schema.completed_at = datetime.now()
await crud.sync_job.update(db, sync_job_db, sync_job_schema, current_user)
try:
await crud.sync_job.update(
db, db_obj=sync_job_db, obj_in=sync_job_schema, current_user=current_user
)
except Exception as e:
logger.error(f"An error occured while updating the sync job: {e}")

sync_progress_update.is_complete = True
await sync_pubsub.publish(sync_job.id, sync_progress_update)

return sync

Expand Down
31 changes: 5 additions & 26 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Route, Routes, Navigate } from "react-router-dom";
import { Route, Routes } from "react-router-dom";
import { Toaster } from "@/components/ui/toaster";
import { Layout } from "@/components/layout/Layout";
import Dashboard from "@/pages/Dashboard";
import DashboardLayout from "@/components/DashboardLayout";
import Settings from "@/pages/Settings";
Expand All @@ -14,39 +13,18 @@ import ViewEditWhiteLabelSync from "@/pages/ViewEditWhiteLabelSync";
import Destinations from "@/pages/Destinations";
import Profile from "@/pages/Profile";
import Chat from "@/pages/Chat";
import Index from "@/pages/Index";
import Sources from "@/pages/Sources";
import { AuthCallback } from "./pages/AuthCallback";
import ViewEditWhiteLabel from "./pages/ViewEditWhiteLabel";
import Login from "./pages/Login";

// Simple auth check for demo purposes
const RequireAuth = ({ children }: { children: JSX.Element }) => {
const isAuthenticated = !!localStorage.getItem("user");
if (!isAuthenticated) {
return <Navigate to="/login" replace />;
}
return children;
};

function App() {
return (
<>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Index />} />
<Route path="login" element={<Login />} />
<Route path="*" element={<NotFound />} />
</Route>

<Route element={
<RequireAuth>
<DashboardLayout />
</RequireAuth>
}>
<Route element={<DashboardLayout />}>
<Route path="/" element={<Dashboard />} />
<Route path="/dashboard" element={<Dashboard />} />

{/* Configure section routes */}
<Route path="/sync">
<Route index element={<SyncTableView />} />
<Route path="create" element={<SyncCreate />} />
Expand All @@ -63,10 +41,11 @@ function App() {
<Route path=":id" element={<ViewEditWhiteLabel />} />
</Route>

{/* Settings and Profile routes */}
<Route path="/settings" element={<Settings />} />
<Route path="/profile" element={<Profile />} />
<Route path="/chat/:chatId?" element={<Chat />} />

<Route path="*" element={<NotFound />} />
</Route>

<Route path="/auth/callback/:short_name" element={<AuthCallback />} />
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/DashboardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ const DashboardLayout = () => {
href: "/dashboard",
icon: LayoutDashboard,
},
];

const configureNavigation = [
{
name: "Chat",
href: "/chat",
icon: MessageSquare,
},
];

const configureNavigation = [
{
name: "Synchronizations",
href: "/sync",
Expand Down
94 changes: 0 additions & 94 deletions frontend/src/components/Hero.tsx

This file was deleted.

6 changes: 3 additions & 3 deletions frontend/src/components/VectorDBSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface Connection {
}

interface VectorDBSelectorProps {
onComplete: (connectionDetails: { connectionId: string; isNative?: boolean }) => void;
onComplete: (details: ConnectionSelection, metadata: { name: string; shortName: string }) => void;
}

interface DestinationDetails {
Expand Down Expand Up @@ -144,7 +144,7 @@ export const VectorDBSelector = ({ onComplete }: VectorDBSelectorProps) => {
onComplete({
connectionId: isNative ? "" : connId,
isNative: isNative
});
}, { name: "", shortName: "" });
};

/**
Expand Down Expand Up @@ -173,7 +173,7 @@ export const VectorDBSelector = ({ onComplete }: VectorDBSelectorProps) => {
if (!response.ok) throw new Error("Failed to connect");

const data = await response.json();
onComplete({ connectionId: data.id });
onComplete({ connectionId: data.id }, { name: selectedDestination.name, shortName: selectedDestination.short_name });
setShowConfig(false);
} catch (err) {
toast({
Expand Down
Loading

0 comments on commit 5542101

Please sign in to comment.