Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/components/sidebar/thread-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function ThreadItemComponent({
}: ThreadItemProps) {
const router = useRouter();
const pathname = usePathname();
const openrouterKey = useChatUIStore(state => state.clientKeys.openrouter);
const clientKeys = useChatUIStore(state => state.clientKeys);
const isStreaming = useChatUIStore(state => state.streamingThreadId === id);
const { data: settings } = useUserSettings();
const sidebarIconsDisabled = settings?.showSidebarIcons ?? false;
Expand Down Expand Up @@ -172,7 +172,7 @@ function ThreadItemComponent({

const handleRegenerateNameClick = async () => {
try {
await regenerateThreadNameMutation.mutateAsync({ threadId: id, clientKey: openrouterKey ?? undefined, useAllMessages: true });
await regenerateThreadNameMutation.mutateAsync({ threadId: id, clientKeys: Object.keys(clientKeys).length > 0 ? clientKeys : undefined, useAllMessages: true });
toast.success("Thread name regenerated");
}
catch (error) {
Expand All @@ -183,7 +183,7 @@ function ThreadItemComponent({

const handleRegenerateIconClick = async () => {
try {
await regenerateThreadIconMutation.mutateAsync({ threadId: id, clientKey: openrouterKey ?? undefined });
await regenerateThreadIconMutation.mutateAsync({ threadId: id, clientKeys: Object.keys(clientKeys).length > 0 ? clientKeys : undefined });
toast.success("Thread icon regenerated");
}
catch (error) {
Expand Down
14 changes: 8 additions & 6 deletions src/features/chat/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { TagRow } from "~/features/chat/queries";
import type { ChatUIMessage } from "~/features/chat/types";
import type { ApiKeyProvider } from "~/lib/api-keys/types";
import type { ThreadIcon } from "~/lib/db/schema/chat";

import { deleteFile } from "~/features/attachments/lib/storage";
Expand Down Expand Up @@ -156,18 +157,19 @@ export async function archiveThread(threadId: string, archive: boolean): Promise
* Ownership is verified atomically by the renameThreadById query.
*
* @param threadId ID of the thread to rename
* @param clientKey Optional API key provided by the client (from localStorage)
* @param clientKeys Optional API keys provided by the client (from localStorage)
* @param useAllMessages Optional flag to send all messages for name context, instead of just the initial.
* @return {Promise<string>} The new title
*/
export async function regenerateThreadName(threadId: string, clientKey?: string, useAllMessages = false): Promise<string> {
export async function regenerateThreadName(threadId: string, clientKeys?: Partial<Record<ApiKeyProvider, string>>, useAllMessages = false): Promise<string> {
const session = await getRequiredSession();

const userId = session.user.id;

// Fetch API keys, settings, tier, and messages in parallel
const { getUserSettingsAndKeys } = await import("~/features/settings/queries");
const [{ settings, resolvedKeys }, threadMessages, tier] = await Promise.all([
getUserSettingsAndKeys(userId, clientKey ? { openrouter: clientKey } : undefined),
getUserSettingsAndKeys(userId, clientKeys),
getMessagesByThreadId(threadId),
getUserTier(userId),
]);
Expand Down Expand Up @@ -223,17 +225,17 @@ export async function regenerateThreadName(threadId: string, clientKey?: string,
* Ownership is verified atomically by the updateThreadIcon query.
*
* @param threadId ID of the thread to update
* @param clientKey Optional API key provided by the client (from localStorage)
* @param clientKeys Optional API keys provided by the client (from localStorage)
* @return {Promise<ThreadIcon>} The new icon
*/
export async function regenerateThreadIcon(threadId: string, clientKey?: string): Promise<ThreadIcon> {
export async function regenerateThreadIcon(threadId: string, clientKeys?: Partial<Record<ApiKeyProvider, string>>): Promise<ThreadIcon> {
const session = await getRequiredSession();

const userId = session.user.id;

const { getUserSettingsAndKeys } = await import("~/features/settings/queries");
const [{ settings, resolvedKeys }, threadMessages, tier] = await Promise.all([
getUserSettingsAndKeys(userId, clientKey ? { openrouter: clientKey } : undefined),
getUserSettingsAndKeys(userId, clientKeys),
getMessagesByThreadId(threadId),
getUserTier(userId),
]);
Expand Down
9 changes: 5 additions & 4 deletions src/features/chat/hooks/use-threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tansta
import { useMemo } from "react";

import type { GroupedThreads, TagGroup } from "~/features/chat/utils/thread-grouper";
import type { ApiKeyProvider } from "~/lib/api-keys/types";
import type { ThreadIcon } from "~/lib/db/schema/chat";

import { archiveThread, deleteThread, fetchThreadStats, regenerateThreadIcon, regenerateThreadName, renameThread, setThreadIcon } from "~/features/chat/actions";
Expand Down Expand Up @@ -151,8 +152,8 @@ export function useRegenerateThreadName() {
const queryClient = useQueryClient();

return useMutation({
mutationFn: ({ threadId, clientKey, useAllMessages }: { threadId: string; clientKey?: string; useAllMessages?: boolean }) =>
regenerateThreadName(threadId, clientKey, useAllMessages),
mutationFn: ({ threadId, clientKeys, useAllMessages }: { threadId: string; clientKeys?: Partial<Record<ApiKeyProvider, string>>; useAllMessages?: boolean }) =>
regenerateThreadName(threadId, clientKeys, useAllMessages),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: THREADS_KEY });
},
Expand Down Expand Up @@ -217,8 +218,8 @@ export function useRegenerateThreadIcon() {
const queryClient = useQueryClient();

return useMutation({
mutationFn: ({ threadId, clientKey }: { threadId: string; clientKey?: string }) =>
regenerateThreadIcon(threadId, clientKey),
mutationFn: ({ threadId, clientKeys }: { threadId: string; clientKeys?: Partial<Record<ApiKeyProvider, string>> }) =>
regenerateThreadIcon(threadId, clientKeys),
onSuccess: (newIcon, { threadId }) => {
queryClient.setQueryData<InfiniteData<ThreadsResponse>>(THREADS_KEY, (old) => {
if (!old)
Expand Down
Loading