Skip to content

Commit

Permalink
functional
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Oct 16, 2024
1 parent 293e617 commit 09f4342
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 41 deletions.
20 changes: 7 additions & 13 deletions web/src/app/chat/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export function ChatPage({
);

const currentPersonaId = searchParams.get(SEARCH_PARAM_NAMES.PERSONA_ID);
const modelVersionFromSearchParams = searchParams.get(
SEARCH_PARAM_NAMES.SRUCTURED_MODEL
);

// Effect to handle sendOnLoad
useEffect(() => {
if (sendOnLoad) {
Expand Down Expand Up @@ -235,7 +239,7 @@ export function ChatPage({
};

const llmOverrideManager = useLlmOverride(
user?.preferences.default_model ?? null,
modelVersionFromSearchParams || (user?.preferences.default_model ?? null),
selectedChatSession,
defaultTemperature
);
Expand Down Expand Up @@ -1896,12 +1900,7 @@ export function ChatPage({
<ShareChatSessionModal
assistantId={liveAssistant?.id}
message={message}
modelVersion={
llmOverrideManager.llmOverride.modelName ||
searchParams.get(SEARCH_PARAM_NAMES.MODEL_VERSION) ||
llmOverrideManager.globalDefault.modelName ||
undefined
}
modelOverride={llmOverrideManager.llmOverride}
chatSessionId={sharedChatSession.id}
existingSharedStatus={sharedChatSession.shared_status}
onClose={() => setSharedChatSession(null)}
Expand All @@ -1918,12 +1917,7 @@ export function ChatPage({
<ShareChatSessionModal
message={message}
assistantId={liveAssistant?.id}
modelVersion={
llmOverrideManager.llmOverride.modelName ||
searchParams.get(SEARCH_PARAM_NAMES.MODEL_VERSION) ||
llmOverrideManager.globalDefault.modelName ||
undefined
}
modelOverride={llmOverrideManager.llmOverride}
chatSessionId={chatSessionIdRef.current}
existingSharedStatus={chatSessionSharedStatus}
onClose={() => setSharingModalVisible(false)}
Expand Down
89 changes: 61 additions & 28 deletions web/src/app/chat/modal/ShareChatSessionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { ChatSessionSharedStatus } from "../interfaces";
import { FiCopy } from "react-icons/fi";
import { CopyButton } from "@/components/CopyButton";
import { SEARCH_PARAM_NAMES } from "../searchParams";
import { usePopup } from "@/components/admin/connectors/Popup";
import { destructureValue, structureValue } from "@/lib/llm/utils";
import { LlmOverride } from "@/lib/hooks";

function buildShareLink(chatSessionId: number) {
const baseUrl = `${window.location.protocol}//${window.location.host}`;
Expand All @@ -30,9 +33,16 @@ async function generateShareLink(chatSessionId: number) {
async function generateCloneLink(
message?: string,
assistantId?: number,
modelVersion?: string
modelOverride?: LlmOverride
) {
const baseUrl = `${window.location.protocol}//${window.location.host}`;
const model = modelOverride
? structureValue(
modelOverride.name,
modelOverride.provider,
modelOverride.modelName
)
: null;
return `${baseUrl}/chat${
message
? `?${SEARCH_PARAM_NAMES.USER_PROMPT}=${encodeURIComponent(message)}`
Expand All @@ -42,8 +52,8 @@ async function generateCloneLink(
? `${message ? "&" : "?"}${SEARCH_PARAM_NAMES.PERSONA_ID}=${assistantId}`
: ""
}${
modelVersion
? `${message || assistantId ? "&" : "?"}${SEARCH_PARAM_NAMES.MODEL_VERSION}=${encodeURIComponent(modelVersion)}`
model
? `${message || assistantId ? "&" : "?"}${SEARCH_PARAM_NAMES.SRUCTURED_MODEL}=${encodeURIComponent(model)}`
: ""
}${message ? `&${SEARCH_PARAM_NAMES.SEND_ON_LOAD}=true` : ""}`;
}
Expand All @@ -67,24 +77,26 @@ export function ShareChatSessionModal({
onClose,
message,
assistantId,
modelVersion,
modelOverride,
}: {
chatSessionId: number;
existingSharedStatus: ChatSessionSharedStatus;
onShare?: (shared: boolean) => void;
onClose: () => void;
message?: string;
assistantId?: number;
modelVersion?: string;
modelOverride?: LlmOverride;
}) {
const [shareLink, setShareLink] = useState<string>(
existingSharedStatus === ChatSessionSharedStatus.Public
? buildShareLink(chatSessionId)
: ""
);
const { popup, setPopup } = usePopup();

return (
<>
{popup}
<ModalWrapper onClose={onClose} modalClassName="max-w-3xl">
<>
<div className="flex mb-4">
Expand Down Expand Up @@ -169,33 +181,54 @@ export function ShareChatSessionModal({
>
Generate and Copy Share Link
</Button>
<Button
icon={FiCopy}
onClick={async () => {
// NOTE: for "inescure" non-https setup, the `navigator.clipboard.writeText` may fail
// as the browser may not allow the clipboard to be accessed.
try {
const shareLink = await generateCloneLink(
message,
assistantId,
modelVersion
);
console.log(shareLink);
console.log(message, assistantId, modelVersion);
navigator.clipboard.writeText(shareLink);
} catch (e) {
console.error(e);
}
}}
size="xs"
color="green"
>
Generate and Copy Link to Current Query
</Button>
</div>
</div>
)}
</div>

<Divider className="my-4" />
<div className="mb-4">
<Callout title="Clone Chat" color="blue">
Generate a link to clone this chat session with the current query.
This allows others to start a new chat with the same initial
message and settings.
</Callout>
</div>
<div className="flex w-full justify-between">
<Button
icon={FiCopy}
onClick={async () => {
// NOTE: for "insecure" non-https setup, the `navigator.clipboard.writeText` may fail
// as the browser may not allow the clipboard to be accessed.
try {
const cloneLink = await generateCloneLink(
message,
assistantId,
modelOverride
);
if (!cloneLink) {
setPopup({
message: "Failed to generate clone link",
type: "error",
});
} else {
navigator.clipboard.writeText(cloneLink);
setPopup({
message: "Link copied to clipboard!",
type: "success",
});
}
} catch (e) {
console.error(e);
alert("Failed to generate or copy link.");
}
}}
size="xs"
color="blue"
>
Generate and Copy Clone Link
</Button>
</div>
</>
</ModalWrapper>
</>
Expand Down
1 change: 1 addition & 0 deletions web/src/app/chat/searchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const SEARCH_PARAM_NAMES = {
TEMPERATURE: "temperature",
MODEL_VERSION: "model-version",
SYSTEM_PROMPT: "system-prompt",
SRUCTURED_MODEL: "structured-model",
// user message
USER_PROMPT: "user-prompt",
SUBMIT_ON_LOAD: "submit-on-load",
Expand Down

0 comments on commit 09f4342

Please sign in to comment.