Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(chat): fix rerender conversation settings issues (Issue #676, #688, #671) #678

Merged
merged 13 commits into from
Feb 13, 2024
22 changes: 15 additions & 7 deletions apps/chat/src/pages/api/[entitytype]/[...slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
return await handleGetRequest(req, token, res);
} else if (req.method === 'PUT') {
return await handlePutRequest(req, token, res, {
ifNoneMatch: undefined,
});
return await handlePutRequest(req, token, res);
} else if (req.method === 'POST') {
return await handlePutRequest(req, token, res, { ifNoneMatch: '*' });
} else if (req.method === 'DELETE') {
Expand Down Expand Up @@ -66,19 +64,24 @@ export const config = {
responseLimit: false,
},
};

interface PutOptions {
ifNoneMatch?: string;
}
async function handlePutRequest(
req: NextApiRequest,
token: JWT | null,
res: NextApiResponse,
{ ifNoneMatch }: { ifNoneMatch?: string },
options?: PutOptions,
) {
const readable = Readable.from(req);
const url = getEntityUrlFromSlugs(process.env.DIAL_API_HOST, req);
const proxyRes = await fetch(url, {
method: 'PUT',
headers: {
...getApiHeaders({ jwt: token?.access_token as string, ifNoneMatch }),
...getApiHeaders({
jwt: token?.access_token as string,
ifNoneMatch: options?.ifNoneMatch,
}),
'Content-Type': req.headers['content-type'] as string,
},
body: readable,
Expand Down Expand Up @@ -139,7 +142,12 @@ async function handleDeleteRequest(
});

if (!proxyRes.ok) {
const json: unknown = await proxyRes.json();
let json: unknown;
try {
json = await proxyRes.json();
} catch {
json = undefined;
}
throw new OpenAIError(
(typeof json === 'string' && json) || proxyRes.statusText,
'',
Expand Down
4 changes: 1 addition & 3 deletions apps/chat/src/store/prompts/prompts.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@ const deleteFolderEpic: AppEpic = (action$, state$) =>
switchMap(({ folderId, promptsToRemove, folders }) => {
const childFolders = new Set([
folderId,
...promptsToRemove.flatMap((prompt) =>
getAllPathsFromPath(prompt.folderId),
),
...promptsToRemove.map((prompt) => prompt.folderId),
]);
const actions: Observable<AnyAction>[] = [];
actions.push(
Expand Down
2 changes: 2 additions & 0 deletions apps/chat/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ export enum UploadStatus {
export const isNotLoaded = (status?: UploadStatus) => {
return !status || status === UploadStatus.UNINITIALIZED;
};

export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
25 changes: 16 additions & 9 deletions apps/chat/src/utils/app/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
MessageSettings,
Role,
} from '@/src/types/chat';
import { EntityType, UploadStatus } from '@/src/types/common';
import { EntityType, PartialBy, UploadStatus } from '@/src/types/common';
import { OpenAIEntityAddon, OpenAIEntityModel } from '@/src/types/openai';

import { getConversationApiKey, parseConversationApiKey } from '../server/api';
Expand Down Expand Up @@ -86,9 +86,11 @@ export const getNewConversationName = (
) {
return conversation.name;
}
const content = message.content.replaceAll(notAllowedSymbolsRegex, '').trim();
const content = message.content
.replaceAll(notAllowedSymbolsRegex, ' ')
.trim();
if (content.length > 0) {
return content.length > 160 ? content.substring(0, 160) + '...' : content;
return content.length > 160 ? content.substring(0, 157) + '...' : content;
} else if (message.custom_content?.attachments?.length) {
const files = message.custom_content.attachments;
return files[0].title;
Expand All @@ -103,12 +105,17 @@ export const getGeneratedConversationId = <T extends ConversationInfo>(
constructPath(conversation.folderId, getConversationApiKey(conversation));

export const addGeneratedConversationId = <T extends ConversationInfo>(
conversation: Omit<T, 'id'>,
): T =>
({
...conversation,
id: getGeneratedConversationId(conversation),
}) as T;
conversation: PartialBy<T, 'id'>,
): T => {
const newId = getGeneratedConversationId(conversation);
if (!conversation.id || newId !== conversation.id) {
return {
...conversation,
id: newId,
} as T;
}
return conversation as T;
};

export const parseConversationId = (id: string): ConversationInfo => {
const { name, parentPath } = splitPath(id);
Expand Down
21 changes: 16 additions & 5 deletions apps/chat/src/utils/app/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@/src/utils/app/file';

import { Conversation, ConversationInfo } from '@/src/types/chat';
import { ShareEntity, UploadStatus } from '@/src/types/common';
import { PartialBy, ShareEntity, UploadStatus } from '@/src/types/common';
import { DialFile } from '@/src/types/files';
import { FolderInterface, FolderType } from '@/src/types/folder';
import { Prompt, PromptInfo } from '@/src/types/prompt';
Expand Down Expand Up @@ -363,10 +363,21 @@ export const getConversationAttachmentWithPath = (
).map((file) => ({ ...file, relativePath: path, contentLength: 0 }));
};

export const addGeneratedFolderId = (folder: Omit<FolderInterface, 'id'>) => ({
...folder,
id: constructPath(folder.folderId, folder.name),
});
const getGeneratedFolderId = (folder: PartialBy<FolderInterface, 'id'>) =>
constructPath(folder.folderId, folder.name);

export const addGeneratedFolderId = (
folder: PartialBy<FolderInterface, 'id'>,
): FolderInterface => {
const newId = getGeneratedFolderId(folder);
if (!folder.id || newId !== folder.id) {
return {
...folder,
id: constructPath(folder.folderId, folder.name),
};
}
return folder as FolderInterface;
};

export const splitPath = (id: string) => {
const parts = id.split('/');
Expand Down
20 changes: 16 additions & 4 deletions apps/chat/src/utils/app/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { PartialBy } from '@/src/types/common';
import { Prompt } from '@/src/types/prompt';

import { getPromptApiKey } from '../server/api';
import { constructPath } from './file';

export const addGeneratedPromptId = (prompt: Omit<Prompt, 'id'>) => ({
...prompt,
id: constructPath(prompt.folderId, getPromptApiKey(prompt)),
});
const getGeneratedPromptId = (prompt: PartialBy<Prompt, 'id'>) =>
constructPath(prompt.folderId, getPromptApiKey(prompt));

export const addGeneratedPromptId = (
prompt: PartialBy<Prompt, 'id'>,
): Prompt => {
const newId = getGeneratedPromptId(prompt);
if (!prompt.id || newId !== prompt.id) {
return {
...prompt,
id: newId,
};
}
return prompt as Prompt;
};
2 changes: 1 addition & 1 deletion apps/chat/src/utils/server/get-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const getApiHeaders = ({
}

if (ifNoneMatch) {
headers['If-None-Match'] = '*';
headers['If-None-Match'] = ifNoneMatch;
}
return headers;
};
Loading