ensure shared chats are shared

This commit is contained in:
pablodanswer
2024-10-14 15:02:03 -07:00
parent 494fda906d
commit 8be887f3ee
5 changed files with 39 additions and 14 deletions

View File

@@ -239,11 +239,14 @@ def get_persona(
persona_id: int, persona_id: int,
user: User | None = Depends(current_user), user: User | None = Depends(current_user),
db_session: Session = Depends(get_session), db_session: Session = Depends(get_session),
include_non_owned: bool = Query(
False, description="If true, return persona even if user doesn't own it."
),
) -> PersonaSnapshot: ) -> PersonaSnapshot:
return PersonaSnapshot.from_model( return PersonaSnapshot.from_model(
get_persona_by_id( get_persona_by_id(
persona_id=persona_id, persona_id=persona_id,
user=user, user=user if not include_non_owned else None,
db_session=db_session, db_session=db_session,
is_for_edit=False, is_for_edit=False,
) )

View File

@@ -2,7 +2,7 @@
import Prism from "prismjs"; import Prism from "prismjs";
import { humanReadableFormat } from "@/lib/time"; import { humanReadableFormat } from "@/lib/time";
import { BackendChatSession } from "../../interfaces"; import { BackendChatSession, SharedChatSession } from "../../interfaces";
import { import {
buildLatestMessageChain, buildLatestMessageChain,
getCitedDocumentsFromMessage, getCitedDocumentsFromMessage,
@@ -11,10 +11,10 @@ import {
import { AIMessage, HumanMessage } from "../../message/Messages"; import { AIMessage, HumanMessage } from "../../message/Messages";
import { Button, Callout, Divider } from "@tremor/react"; import { Button, Callout, Divider } from "@tremor/react";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { Persona } from "@/app/admin/assistants/interfaces";
import { useContext, useEffect, useState } from "react"; import { useContext, useEffect, useState } from "react";
import { SettingsContext } from "@/components/settings/SettingsProvider"; import { SettingsContext } from "@/components/settings/SettingsProvider";
import { DanswerInitializingLoader } from "@/components/DanswerInitializingLoader"; import { DanswerInitializingLoader } from "@/components/DanswerInitializingLoader";
import { Persona } from "@/app/admin/assistants/interfaces";
function BackToDanswerButton() { function BackToDanswerButton() {
const router = useRouter(); const router = useRouter();
@@ -34,10 +34,10 @@ function BackToDanswerButton() {
export function SharedChatDisplay({ export function SharedChatDisplay({
chatSession, chatSession,
availableAssistants, persona,
}: { }: {
chatSession: BackendChatSession | null; chatSession: BackendChatSession | null;
availableAssistants: Persona[]; persona: Persona;
}) { }) {
const [isReady, setIsReady] = useState(false); const [isReady, setIsReady] = useState(false);
useEffect(() => { useEffect(() => {
@@ -56,9 +56,6 @@ export function SharedChatDisplay({
</div> </div>
); );
} }
const currentPersona = availableAssistants.find(
(persona) => persona.id === chatSession.persona_id
);
const messages = buildLatestMessageChain( const messages = buildLatestMessageChain(
processRawChatHistory(chatSession.messages) processRawChatHistory(chatSession.messages)
@@ -96,7 +93,7 @@ export function SharedChatDisplay({
return ( return (
<AIMessage <AIMessage
shared shared
currentPersona={currentPersona!} currentPersona={persona}
key={message.messageId} key={message.messageId}
messageId={message.messageId} messageId={message.messageId}
content={message.message} content={message.message}

View File

@@ -11,6 +11,7 @@ import { SharedChatDisplay } from "./SharedChatDisplay";
import { Persona } from "@/app/admin/assistants/interfaces"; import { Persona } from "@/app/admin/assistants/interfaces";
import { fetchAssistantsSS } from "@/lib/assistants/fetchAssistantsSS"; import { fetchAssistantsSS } from "@/lib/assistants/fetchAssistantsSS";
import FunctionalHeader from "@/components/chat_search/Header"; import FunctionalHeader from "@/components/chat_search/Header";
import { fetchAssistantSS } from "@/lib/assistants/fetchAssistantSS";
async function getSharedChat(chatId: string) { async function getSharedChat(chatId: string) {
const response = await fetchSS( const response = await fetchSS(
@@ -43,7 +44,16 @@ export default async function Page({ params }: { params: { chatId: string } }) {
const authTypeMetadata = results[0] as AuthTypeMetadata | null; const authTypeMetadata = results[0] as AuthTypeMetadata | null;
const user = results[1] as User | null; const user = results[1] as User | null;
const chatSession = results[2] as BackendChatSession | null; const chatSession = results[2] as BackendChatSession | null;
const [availableAssistants, _] = results[3] as [Persona[], string | null]; const availableAssistants = results[3] as Persona[] | null;
let persona: Persona | null = null;
if (chatSession && chatSession.persona_id) {
try {
persona = await fetchAssistantSS(chatSession.persona_id);
} catch (e) {
console.log(`Failed to fetch persona - ${e}`);
}
}
const authDisabled = authTypeMetadata?.authType === "disabled"; const authDisabled = authTypeMetadata?.authType === "disabled";
if (!authDisabled && !user) { if (!authDisabled && !user) {
@@ -53,6 +63,9 @@ export default async function Page({ params }: { params: { chatId: string } }) {
if (user && !user.is_verified && authTypeMetadata?.requiresVerification) { if (user && !user.is_verified && authTypeMetadata?.requiresVerification) {
return redirect("/auth/waiting-on-verification"); return redirect("/auth/waiting-on-verification");
} }
if (!persona) {
persona = availableAssistants?.[0] ?? null;
}
return ( return (
<div> <div>
@@ -61,10 +74,7 @@ export default async function Page({ params }: { params: { chatId: string } }) {
</div> </div>
<div className="flex relative bg-background text-default overflow-hidden pt-16 h-screen"> <div className="flex relative bg-background text-default overflow-hidden pt-16 h-screen">
<SharedChatDisplay <SharedChatDisplay chatSession={chatSession} persona={persona!} />
chatSession={chatSession}
availableAssistants={availableAssistants}
/>
</div> </div>
</div> </div>
); );

View File

@@ -37,6 +37,7 @@ export function ProviderContextProvider({
const fetchProviderInfo = useCallback(async () => { const fetchProviderInfo = useCallback(async () => {
const { providers, options, defaultCheckSuccessful } = const { providers, options, defaultCheckSuccessful } =
await checkLlmProvider(user); await checkLlmProvider(user);
setValidProviderExists(providers.length > 0 && defaultCheckSuccessful); setValidProviderExists(providers.length > 0 && defaultCheckSuccessful);
setProviderOptions(options); setProviderOptions(options);
}, [user, setValidProviderExists, setProviderOptions]); }, [user, setValidProviderExists, setProviderOptions]);

View File

@@ -0,0 +1,14 @@
import { Persona } from "@/app/admin/assistants/interfaces";
import { fetchSS } from "../utilsSS";
export async function fetchAssistantSS(
personaId: number
): Promise<Persona | null> {
const response = await fetchSS(
`/persona/${personaId}?include_non_owned=true`
);
if (response.ok) {
return (await response.json()) as Persona;
}
return null;
}