Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/app/(general)/_components/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface Props {
isReadonly: boolean;
isNew: boolean;
workbench?: Workbench;
prefillQuery?: string;
autoSubmitQuery?: boolean;
}

export const Chat = async ({
Expand All @@ -25,6 +27,8 @@ export const Chat = async ({
isReadonly,
isNew,
workbench,
prefillQuery,
autoSubmitQuery,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is autoSubmitQuery ever used? Feels to me like if prefillQuery is defined it should autosubmit as the user has been linked into that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, without it the query just sits in the box. I included it last, it was the last request of the issue. #232

}: Props) => {
const initialMessages = isNew
? []
Expand All @@ -44,7 +48,7 @@ export const Chat = async ({
const initialPreferences = {
selectedChatModel: serverPreferences.selectedChatModel,
imageGenerationModel: serverPreferences.imageGenerationModel,
useNativeSearch: serverPreferences.useNativeSearch,
useNativeSearch: prefillQuery ? true : serverPreferences.useNativeSearch, // Enable web search when query is provided
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking back at it now, i think this might be redundant now too, since web search is auto enabled based on model capabilities, so no need to actually specify this.
I originally did this because the issue specifically requested enabling web search as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if web search is auto-enabled, that's correct yeah, this is redundant! I wasnt sure

toolkits: serverPreferences.toolkits
?.map((persistedToolkit: PersistedToolkit) => {
const clientToolkit =
Expand Down Expand Up @@ -112,6 +116,8 @@ export const Chat = async ({
initialVisibilityType={initialVisibilityType}
autoResume={!isNew}
workbench={workbench}
initialInput={prefillQuery}
autoSubmitInitialInput={autoSubmitQuery}
initialPreferences={initialPreferences}
>
<ChatContent
Expand Down
41 changes: 33 additions & 8 deletions src/app/(general)/_contexts/chat-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ interface ChatProviderProps {
initialVisibilityType: "public" | "private";
autoResume: boolean;
workbench?: Workbench;
initialInput?: string;
autoSubmitInitialInput?: boolean;
initialPreferences?: {
selectedChatModel?: LanguageModel;
imageGenerationModel?: ImageModel;
Expand All @@ -102,6 +104,8 @@ export function ChatProvider({
initialVisibilityType,
autoResume,
workbench,
initialInput,
autoSubmitInitialInput,
initialPreferences,
}: ChatProviderProps) {
const utils = api.useUtils();
Expand Down Expand Up @@ -222,6 +226,7 @@ export function ChatProvider({
} = useChat({
id,
initialMessages,
initialInput,
experimental_throttle: 100,
sendExtraMessageFields: true,
generateId: generateUUID,
Expand Down Expand Up @@ -282,14 +287,14 @@ export function ChatProvider({
onStreamError,
});

const handleSubmit: UseChatHelpers["handleSubmit"] = (
event,
chatRequestOptions,
) => {
// Reset stream stopped flag when submitting new message
setStreamStopped(false);
originalHandleSubmit(event, chatRequestOptions);
};
const handleSubmit: UseChatHelpers["handleSubmit"] = useCallback(
(event, chatRequestOptions) => {
// Reset stream stopped flag when submitting new message
setStreamStopped(false);
originalHandleSubmit(event, chatRequestOptions);
},
[originalHandleSubmit, setStreamStopped],
);

useEffect(() => {
if (
Expand All @@ -303,6 +308,26 @@ export function ChatProvider({
}
}, [selectedChatModel]);

useEffect(() => {
if (
autoSubmitInitialInput &&
initialInput &&
input === initialInput &&
messages.length === 0
) {
const timer = setTimeout(() => {
handleSubmit();
}, 100);
return () => clearTimeout(timer);
}
}, [
autoSubmitInitialInput,
initialInput,
input,
messages.length,
handleSubmit,
]);

const value = {
messages,
setMessages,
Expand Down
39 changes: 39 additions & 0 deletions src/app/(general)/new/page.tsx
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the need for this page?

Seems like we can keep the query param behavior in the index route

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can, but the route was requested, that's why I created it, also following the pattern of I think two out of the three examples that were provided uses it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was part of the scope for the issue #232, to give it parity with the other providers and to reduce confusion about landing page vs. authed route, but maybe we dont need a /new?

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { redirect } from "next/navigation";

import { Chat } from "@/app/(general)/_components/chat";
import { auth } from "@/server/auth";
import { generateUUID } from "@/lib/utils";

interface NewChatPageProps {
searchParams: Promise<{
q?: string;
}>;
}

export default async function NewChatPage(props: NewChatPageProps) {
const searchParams = await props.searchParams;
const { q } = searchParams;

const session = await auth();

if (!session) {
const redirectUrl = q
? `/login?redirect=${encodeURIComponent(`/new?q=${encodeURIComponent(q)}`)}`
: "/login?redirect=/new";
redirect(redirectUrl);
}

const id = generateUUID();

return (
<Chat
key={id}
id={id}
initialVisibilityType="private"
isReadonly={false}
isNew={true}
prefillQuery={q}
autoSubmitQuery={q ? true : false}
/>
);
}
12 changes: 11 additions & 1 deletion src/app/(general)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import { auth } from "@/server/auth";

import { generateUUID } from "@/lib/utils";

export default async function Page() {
interface HomePageProps {
searchParams: Promise<{
q?: string;
}>;
}

export default async function Page(props: HomePageProps) {
const searchParams = await props.searchParams;
const { q } = searchParams;

const session = await auth();

if (!session) {
Expand All @@ -21,6 +30,7 @@ export default async function Page() {
initialVisibilityType="private"
isReadonly={false}
isNew={true}
prefillQuery={q}
/>
);
}